Skip to content

Commit

Permalink
Avoid multiple creation and destruction of arrays during calculation …
Browse files Browse the repository at this point in the history
…of distance transform in shapeburst fills
  • Loading branch information
nyalldawson committed Mar 21, 2014
1 parent 281b45e commit 1d21beb
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 14 deletions.
23 changes: 10 additions & 13 deletions src/core/symbology-ng/qgsfillsymbollayerv2.cpp
Expand Up @@ -1074,11 +1074,8 @@ void QgsShapeburstFillSymbolLayerV2::renderPolygon( const QPolygonF& points, QLi
//fast distance transform code, adapted from http://cs.brown.edu/~pff/dt/

/* distance transform of a 1d function using squared distance */
double * QgsShapeburstFillSymbolLayerV2::distanceTransform1d( double *f, int n )
void QgsShapeburstFillSymbolLayerV2::distanceTransform1d( double *f, int n, int *v, double *z, double *d )
{
double *d = new double[n];
int *v = new int[n];
double *z = new double[n+1];
int k = 0;
v[0] = 0;
z[0] = -INF;
Expand All @@ -1104,16 +1101,15 @@ double * QgsShapeburstFillSymbolLayerV2::distanceTransform1d( double *f, int n )
k++;
d[q] = ( q - v[k] ) * ( q - v[k] ) + f[v[k]];
}

delete [] v;
delete [] z;
return d;
}

/* distance transform of 2d function using squared distance */
void QgsShapeburstFillSymbolLayerV2::distanceTransform2d( double * im, int width, int height )
{
double *f = new double[ qMax( width,height )];
int *v = new int[ qMax( width,height )];
double *z = new double[ qMax( width,height ) + 1 ];
double *d = new double[ qMax( width,height )];

// transform along columns
for ( int x = 0; x < width; x++ )
Expand All @@ -1122,12 +1118,11 @@ void QgsShapeburstFillSymbolLayerV2::distanceTransform2d( double * im, int width
{
f[y] = im[ x + y * width ];
}
double *d = distanceTransform1d( f, height );
distanceTransform1d( f, height, v, z, d );
for ( int y = 0; y < height; y++ )
{
im[ x + y * width ] = d[y];
}
delete [] d;
}

// transform along rows
Expand All @@ -1137,15 +1132,17 @@ void QgsShapeburstFillSymbolLayerV2::distanceTransform2d( double * im, int width
{
f[x] = im[ x + y*width ];
}
double *d = distanceTransform1d( f, width );
distanceTransform1d( f, width, v, z, d );
for ( int x = 0; x < width; x++ )
{
im[ x + y*width ] = d[x];
}
delete [] d;
}

delete f;
delete [] d;
delete [] f;
delete [] v;
delete [] z;
}

/* distance transform of a binary QImage */
Expand Down
2 changes: 1 addition & 1 deletion src/core/symbology-ng/qgsfillsymbollayerv2.h
Expand Up @@ -476,7 +476,7 @@ class CORE_EXPORT QgsShapeburstFillSymbolLayerV2 : public QgsFillSymbolLayerV2
double& maxDistance );

/* distance transform of a 1d function using squared distance */
double * distanceTransform1d( double *f, int n );
void distanceTransform1d( double *f, int n, int *v, double *z, double *d );
/* distance transform of 2d function using squared distance */
void distanceTransform2d( double * im, int width, int height );
/* distance transform of a binary QImage */
Expand Down

0 comments on commit 1d21beb

Please sign in to comment.