Ask Your Question

Revision history [back]

This could have been avoided by a little factoring, as follows:

// there might be a better name for this...
static void createIndicesOrDists(OutputArray _array, Mat& mat,
                                 int rows,  int minCols, int maxCols, int dtype) 
{ 
    if( _array.needed() ) 
    { 
        mat= _array.getMat(); 
        if( !mat.isContinuous() || mat.type() != CV_32S || 
             mat.rows != rows || mat.cols < minCols || mat.cols > maxCols ) 
        { 
            if( !mat.isContinuous() ) 
               _array.release(); 
            _array.create( rows, minCols, CV_32S ); 
            mat= _array.getMat(); 
        } 
    } 
    else 
        mat.create( rows, minCols, CV_32S );
}

static void createIndicesDists(OutputArray _indices, OutputArray _dists, 
                               Mat& indices, Mat& dists, int rows, 
                               int minCols, int maxCols, int dtype) 
 { 
       createIndicesOrDists(_indices, indices, rows, minCols, maxCols, dtype);
       createIndicesOrDists(_dists, dists, rows, minCols, maxCols, dtype);
 }