31 #include "WMDistanceMap.h"
32 #include "WMDistanceMap.xpm"
33 #include "core/common/WAssert.h"
34 #include "core/common/WProgress.h"
35 #include "core/dataHandler/WGridRegular3D.h"
36 #include "core/dataHandler/WSubject.h"
37 #include "core/kernel/WKernel.h"
38 #include "core/kernel/WModuleFactory.h"
64 return distancemap_xpm;
70 return "Distance Map";
75 return "Computes a smoothed version of the dataset and a distance map on it.";
109 debugLog() <<
"Data changed. Updating ...";
124 m_input = std::shared_ptr<WModuleInputData< WDataSetScalar > >(
126 "in",
"Dataset to compute distance map for." )
132 m_output = std::shared_ptr<WModuleOutputData< WDataSetScalar > >(
134 "out",
"Distance map for the input data set." )
149 template<
typename T > std::shared_ptr< WValueSet< float > > makeFloatValueSetHelper( std::shared_ptr<
WValueSet< T > > inSet )
151 WAssert( inSet->dimension() == 1,
"Works only for scalar data." );
152 WAssert( inSet->order() == 0,
"Works only for scalar data." );
154 std::shared_ptr< std::vector< float > > data(
new std::vector< float >( inSet->size() ) );
155 for(
unsigned int i = 0; i < inSet->size(); ++i )
157 ( *data )[i] =
static_cast< float >( inSet->getScalar( i ) );
160 std::shared_ptr< WValueSet< float > > outSet;
161 outSet = std::shared_ptr< WValueSet< float > >(
new WValueSet< float >( ( *inSet ).order(), ( *inSet ).dimension(), data, W_DT_FLOAT ) );
166 std::shared_ptr< WValueSet< float > > makeFloatValueSet( std::shared_ptr< WValueSetBase > inSet )
168 WAssert( inSet->dimension() == 1,
"Works only for scalar data." );
169 WAssert( inSet->order() == 0,
"Works only for scalar data." );
171 switch( inSet->getDataType() )
173 case W_DT_UNSIGNED_CHAR:
179 case W_DT_SIGNED_INT:
183 return std::dynamic_pointer_cast< WValueSet< float > >( inSet );
186 WAssert(
false,
"Unknow data type in makeFloatDataSet" );
189 WAssert(
false,
"If this assertion is reached, the code above has to be fixed." );
190 return std::shared_ptr< WValueSet< float > >();
195 std::vector<float> floatDataset;
199 std::shared_ptr< WValueSet< float > > valueSet = makeFloatValueSet( ( *dataSet ).getValueSet() );
200 WAssert( valueSet,
"Works only for float data sets." );
202 std::shared_ptr< WGridRegular3D > grid = std::dynamic_pointer_cast< WGridRegular3D >( ( *dataSet ).getGrid() );
203 WAssert( grid,
"Works only for data on regular 3D grids." );
205 int b, r, c, bb, rr, r0, b0, c0;
207 int nbands, nrows, ncols, npixels;
208 int d, d1, d2, cc1, cc2;
213 nbands = grid->getNbCoordsZ();
214 nrows = grid->getNbCoordsY();
215 ncols = grid->getNbCoordsX();
217 npixels = std::max( nbands, nrows );
219 array =
new double[npixels];
221 npixels = nbands * nrows * ncols;
223 floatDataset.resize( npixels );
224 for(
int i = 0; i < npixels; ++i )
226 floatDataset[i] = 0.0;
229 bool* bitmask =
new bool[npixels];
230 for(
int i = 0; i < npixels; ++i )
232 if( valueSet->getScalar( i ) < 0.01 )
246 std::shared_ptr< WProgress > progress1 = std::shared_ptr< WProgress >(
247 new WProgress(
"Distance Map", nbands + nbands + nrows + nbands + nbands + nbands )
250 for( b = 0; b < nbands; ++b )
253 for( r = 0; r < nrows; ++r )
255 for( c = 0; c < ncols; ++c )
258 if( bitmask[b * nrows * ncols + r * ncols + c] )
260 floatDataset[b * nrows * ncols + r * ncols + c] = 0;
264 srcpix = bitmask + b * nrows * ncols + r * ncols + c;
266 while( cc1 < ncols && *srcpix++ == 0 )
268 d1 = ( cc1 >= ncols ? ncols : ( cc1 - c ) );
270 srcpix = bitmask + b * nrows * ncols + r * ncols + c;
272 while( cc2 >= 0 && *srcpix-- == 0 )
274 d2 = ( cc2 <= 0 ? ncols : ( c - cc2 ) );
286 floatDataset[b * nrows * ncols + r * ncols + c] =
static_cast< float >( d * d );
292 for( b = 0; b < nbands; b++ )
295 for( c = 0; c < ncols; c++ )
297 for( r = 0; r < nrows; r++ )
298 array[r] =
static_cast< double >( floatDataset[b * nrows * ncols + r * ncols + c] );
300 for( r = 0; r < nrows; r++ )
302 if( bitmask[b * nrows * ncols + r * ncols + c] == 1 )
307 g = sqrt( array[r] );
308 istart = r -
static_cast< int >( g );
311 iend = r +
static_cast< int >( g + 1 );
315 for( rr = istart; rr < iend; rr++ )
317 u = array[rr] + ( r - rr ) * ( r - rr );
324 floatDataset[b * nrows * ncols + r * ncols + c] = dmin;
330 for( r = 0; r < nrows; r++ )
333 for( c = 0; c < ncols; c++ )
335 for( b = 0; b < nbands; b++ )
336 array[b] =
static_cast< double >( floatDataset[b * nrows * ncols + r * ncols + c] );
338 for( b = 0; b < nbands; b++ )
340 if( bitmask[b * nrows * ncols + r * ncols + c] == 1 )
346 g = sqrt( array[b] );
347 istart = b -
static_cast< int >( g - 1 );
350 iend = b +
static_cast< int >( g + 1 );
354 for( bb = istart; bb < iend; bb++ )
356 u = array[bb] + ( b - bb ) * ( b - bb );
363 floatDataset[b * nrows * ncols + r * ncols + c] = dmin;
371 for( i = 0; i < npixels; ++i )
373 floatDataset[i] = sqrt(
static_cast< double >( floatDataset[i] ) );
374 if( floatDataset[i] > max )
375 max = floatDataset[i];
377 for( i = 0; i < npixels; ++i )
379 floatDataset[i] = floatDataset[i] / max;
386 int dim =
static_cast< int >( ( 3.0 * sigma + 1 ) );
390 float* kernel =
new float[n];
393 double x = -
static_cast< float >( dim );
396 for(
int i = 0; i < n; ++i )
405 for(
int i = 0; i < n; ++i )
414 std::vector<float> tmp( npixels );
417 for(
int i = 0; i < npixels; ++i )
422 for( b = 0; b < nbands; ++b )
425 for( r = 0; r < nrows; ++r )
427 for( c = d; c < ncols - d; ++c )
433 for( cc = c0; cc <= c1; cc++ )
435 x = floatDataset[b * nrows * ncols + r * ncols + cc];
436 sum += x * ( *float_pp++ );
438 tmp[b * nrows * ncols + r * ncols + c] = sum;
443 for( b = 0; b < nbands; ++b )
446 for( r = d; r < nrows - d; ++r )
448 for( c = 0; c < ncols; ++c )
454 for( rr = r0; rr <= r1; rr++ )
456 x = tmp[b * nrows * ncols + rr * ncols + c];
457 sum += x * ( *float_pp++ );
459 floatDataset[b * nrows * ncols + r * ncols + c] = sum;
464 for( b = d; b < nbands - d; ++b )
467 for( r = 0; r < nrows; ++r )
469 for( c = 0; c < ncols; ++c )
475 for( bb = b0; bb <= b1; bb++ )
477 x = floatDataset[bb * nrows * ncols + r * ncols + c];
478 sum += x * ( *float_pp++ );
480 tmp[b * nrows * ncols + r * ncols + c] = sum;
488 std::shared_ptr< WValueSet< float > > resultValueSet;
489 resultValueSet = std::shared_ptr< WValueSet< float > >(
491 std::shared_ptr< std::vector< float > >(
new std::vector< float >( floatDataset ) ), W_DT_FLOAT ) );
495 return resultValueSet;
500 double y, z, a = 2.506628273;
502 y = exp( -z * z * 0.5 ) / ( sigma * a );
virtual void wait() const
Wait for the condition.
void setResetable(bool resetable=true, bool autoReset=true)
Sets the resetable flag.
virtual void add(std::shared_ptr< WCondition > condition)
Adds another condition to the set of conditions to wait for.
This data set type contains scalars as values.
void addLogMessage(std::string message, std::string source="", LogLevel level=LL_DEBUG)
Appends a log message to the logging queue.
static WLogger * getLogger()
Returns pointer to the currently running logger instance.
Computes a distance map from an anatomy dataset.
std::shared_ptr< WDataSetScalar > m_dataSet
Source dataset.
virtual const std::string getDescription() const
Gives back a description of this module.
std::shared_ptr< WDataSetScalar > m_distanceMapDataSet
Target dataset.
std::shared_ptr< WModuleInputData< WDataSetScalar > > m_input
Input connector required by this module.
virtual void moduleMain()
Entry point after loading the module.
std::shared_ptr< WValueSet< float > > createOffset(std::shared_ptr< const WDataSetScalar > dataSet)
Function to create a distance map from Anatomy data set.
virtual std::shared_ptr< WModule > factory() const
Due to the prototype design pattern used to build modules, this method returns a new instance of this...
double xxgauss(double x, double sigma)
Gauss function.
virtual const std::string getName() const
Gives back the name of this module.
virtual void connectors()
Initialize the connectors this module is using.
WMDistanceMap()
Standard constructor.
virtual void properties()
Initialize the properties for this module.
std::shared_ptr< WModuleOutputData< WDataSetScalar > > m_output
Connector to provide the distance map to other modules.
virtual const char ** getXPMIcon() const
Get the icon for this module in XPM format.
~WMDistanceMap()
Destructor.
Class offering an instantiate-able data connection between modules.
Class representing a single module of OpenWalnut.
virtual void properties()
Initialize properties in this function.
wlog::WStreamedLogger debugLog() const
Logger instance for comfortable debug logging.
void removeConnectors()
Removes all connectors properly.
void addConnector(std::shared_ptr< WModuleInputConnector > con)
Adds the specified connector to the list of inputs.
void ready()
Call this whenever your module is ready and can react on property changes.
WConditionSet m_moduleState
The internal state of the module.
std::shared_ptr< WProgressCombiner > m_progress
Progress indicator used as parent for all progress' of this module.
virtual void connectors()
Initialize connectors in this function.
Class managing progress inside of modules.
WBoolFlag m_shutdownFlag
Condition getting fired whenever the thread should quit.
Base Class for all value set types.