33 #include "WMGaussFiltering.h"
34 #include "WMGaussFiltering.xpm"
35 #include "core/common/WAssert.h"
36 #include "core/common/WProgress.h"
37 #include "core/common/WStringUtils.h"
38 #include "core/dataHandler/WGridRegular3D.h"
39 #include "core/kernel/WKernel.h"
64 return gaussfiltering_xpm;
69 return "Gauss Filtering";
74 return "Runs a discretized Gauss filter as mask over a simple scalar field.";
77 size_t getId(
size_t xDim,
size_t yDim,
size_t ,
size_t x,
size_t y,
size_t z,
size_t offset = 0,
size_t elements = 1 )
79 return offset + ( elements * ( z * xDim * yDim + y * xDim + x ) );
82 double mask(
size_t i,
size_t j,
size_t k )
84 WAssert( i < 3,
"Index larger than two where [0,1,2] is expected." );
85 WAssert( j < 3,
"Index larger than two where [0,1,2] is expected." );
86 WAssert( k < 3,
"Index larger than two where [0,1,2] is expected." );
87 double maskEntries[3][3][3] = {
88 { { 0, 1, 0 }, { 1, 2, 1 }, { 0, 1, 0 } },
89 { { 1, 2, 1 }, { 2, 4, 2 }, { 1, 2, 1 } },
90 { { 0, 1, 0 }, { 1, 2, 1 }, { 0, 1, 0 } } };
91 return maskEntries[i][j][k];
95 std::shared_ptr<
WValueSet<T> > vals,
size_t nX,
size_t nY,
size_t nZ,
96 size_t x,
size_t y,
size_t z,
size_t offset )
100 size_t filterSize = 3;
101 for(
size_t k = 0; k < filterSize; ++k )
103 for(
size_t j = 0; j < filterSize; ++j )
105 for(
size_t i = 0; i < filterSize; ++i )
107 filtered += mask( i, j, k ) * vals->getScalar(
108 getId( nX, nY, nZ, x - 1 + i, y - 1 + j, z - 1 + k, offset, vals->elementsPerValue() ) );
109 maskSum += mask( i, j, k );
118 template<
typename T >
120 std::shared_ptr< WProgress > prog )
122 size_t nX = grid->getNbCoordsX();
123 size_t nY = grid->getNbCoordsY();
124 size_t nZ = grid->getNbCoordsZ();
128 std::vector<double> newVals( vals->elementsPerValue() * nX * nY * nZ, 0. );
130 for(
size_t z = 1; z < nZ - 1; z++ )
133 for(
size_t y = 1; y < nY - 1; y++ )
135 for(
size_t x = 1; x < nX - 1; x++ )
137 for(
size_t offset = 0; offset < vals->elementsPerValue(); ++offset )
139 newVals[getId( nX, nY, nZ, x, y, z, offset, vals->elementsPerValue() )] =
149 std::vector<double> newVals1( vals->elementsPerValue() * nX * nY * nZ, 0. );
150 std::vector<double> newVals2( vals->elementsPerValue() * nX * nY * nZ, 0. );
152 filterField1D( &newVals1, vals, prog, nX, nY, nZ, 1, nX, nX*nY );
153 filterField1D( &newVals2, newVals1, prog, nY, nX, nZ, nX, 1, nX*nY );
154 filterField1D( &newVals1, newVals2, prog, nZ, nX, nY, nX*nY, 1, nX );
160 template<
typename T >
163 std::shared_ptr< WProgress > prog,
164 size_t nX,
size_t nY,
size_t nZ,
size_t dx,
size_t dy,
size_t dz )
166 for(
size_t z = 1; z < nZ - 1; ++z )
169 for(
size_t y = 1; y < nY - 1; ++y )
171 for(
size_t x = 1; x < nX - 1; ++x )
173 size_t id = x*dx + y*dy + z*dz;
175 0.25 * ( vals->getScalar(
id - dx ) + 2. * vals->getScalar(
id ) + vals->getScalar(
id + dx ) );
181 template<
typename T >
183 const std::vector<T>& vals,
184 std::shared_ptr< WProgress > prog,
185 size_t nX,
size_t nY,
size_t nZ,
size_t dx,
size_t dy,
size_t dz )
187 for(
size_t z = 1; z < nZ - 1; ++z )
190 for(
size_t y = 1; y < nY - 1; ++y )
192 for(
size_t x = 1; x < nX - 1; ++x )
194 size_t id = x*dx + y*dy + z*dz;
196 0.25 * ( vals[
id - dx ] + 2. * vals[ id ] + vals[
id + dx ] );
202 template<
typename T >
206 std::shared_ptr<WGridRegular3D> grid = std::dynamic_pointer_cast< WGridRegular3D >(
m_dataSet->getGrid() );
207 WAssert( grid,
"Grid is not of type WGridRegular3D." );
210 std::shared_ptr< WProgress > prog;
214 prog = std::shared_ptr< WProgress >(
215 new WProgress(
"Gauss Filter Iteration", iterations * grid->getNbCoordsZ() ) );
219 prog = std::shared_ptr< WProgress >(
220 new WProgress(
"Gauss Filter Iteration", 3 * iterations * grid->getNbCoordsZ() ) );
225 std::shared_ptr< WValueSet< double > > valueSet = std::shared_ptr< WValueSet< double > >(
227 std::shared_ptr< std::vector< double > >(
new std::vector< double >(
filterField( vals, grid, prog ) ) ),
230 for(
unsigned int i = 1; i < iterations; ++i )
232 valueSet = std::shared_ptr< WValueSet< double > >(
234 std::shared_ptr< std::vector< double > >(
new std::vector< double >(
filterField( valueSet, grid, prog ) ) ),
255 unsigned int iterations = 1;
272 std::shared_ptr< WDataSetScalar > newDataSet =
m_input->getData();
273 bool dataChanged = (
m_dataSet != newDataSet );
297 dataChanged = ( iterations >= 1 );
307 std::shared_ptr< WValueSet< double > > newValueSet;
309 switch( ( *m_dataSet ).getValueSet()->getDataType() )
311 case W_DT_UNSIGNED_CHAR:
313 std::shared_ptr<WValueSet<unsigned char> > vals;
314 vals = std::dynamic_pointer_cast<WValueSet<unsigned char> >( ( *m_dataSet ).getValueSet() );
315 WAssert( vals,
"Data type and data type indicator must fit." );
321 std::shared_ptr<WValueSet<int16_t> > vals;
322 vals = std::dynamic_pointer_cast<WValueSet<int16_t> >( ( *m_dataSet ).getValueSet() );
323 WAssert( vals,
"Data type and data type indicator must fit." );
329 std::shared_ptr<WValueSet<uint16_t> > vals;
330 vals = std::dynamic_pointer_cast<WValueSet<uint16_t> >( ( *m_dataSet ).getValueSet() );
331 WAssert( vals,
"Data type and data type indicator must fit." );
335 case W_DT_SIGNED_INT:
337 std::shared_ptr<WValueSet<int32_t> > vals;
338 vals = std::dynamic_pointer_cast<WValueSet<int32_t> >( ( *m_dataSet ).getValueSet() );
339 WAssert( vals,
"Data type and data type indicator must fit." );
345 std::shared_ptr<WValueSet<float> > vals;
346 vals = std::dynamic_pointer_cast<WValueSet<float> >( ( *m_dataSet ).getValueSet() );
347 WAssert( vals,
"Data type and data type indicator must fit." );
353 std::shared_ptr<WValueSet<double> > vals;
354 vals = std::dynamic_pointer_cast<WValueSet<double> >( ( *m_dataSet ).getValueSet() );
355 WAssert( vals,
"Data type and data type indicator must fit." );
360 WAssert(
false,
"Unknown data type in Gauss Filtering module" );
367 if( iterations == 0 )
377 m_input = std::shared_ptr<WModuleInputData<WDataSetScalar> >(
379 "The dataset to filter" ) );
385 m_output = std::shared_ptr<WModuleOutputData<WDataSetScalar> >(
387 "The filtered data set." ) );
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.
Class to encapsulate boost::condition_variable_any.
This data set type contains scalars as values.
Gauss filtering for WDataSetScalar.
std::shared_ptr< WDataSetScalar > m_dataSet
Pointer providing access to the treated data set in the whole module.
double filterAtPosition(std::shared_ptr< WValueSet< T > > vals, size_t nX, size_t nY, size_t nZ, size_t x, size_t y, size_t z, size_t offset)
Simple convolution using a small gauss-like mask.
virtual void moduleMain()
Entry point after loading the module.
virtual void properties()
Initialize the properties for this module.
~WMGaussFiltering()
Destructor.
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...
std::shared_ptr< WModuleInputData< WDataSetScalar > > m_input
Input connector required by this module.
std::shared_ptr< WModuleOutputData< WDataSetScalar > > m_output
The only output of this filter module.
void filterField1D(std::vector< T > *newVals, const std::vector< T > &vals, std::shared_ptr< WProgress > prog, size_t Nx, size_t Ny, size_t Nz, size_t dx, size_t dy, size_t dz)
Run the 1D Gaussian filter over the field.
std::vector< double > filterField(std::shared_ptr< WValueSet< T > > vals, std::shared_ptr< WGridRegular3D > grid, std::shared_ptr< WProgress > prog)
Run the filter over the field.
std::shared_ptr< WValueSet< double > > iterativeFilterField(std::shared_ptr< WValueSet< T > > vals, unsigned int iterations)
Run the filter iteratively over the field.
virtual const std::string getDescription() const
Gives back a description of this module.
WPropBool m_3DMaskMode
1D or 3D filtering flag
virtual void connectors()
Initialize the connectors this module is using.
virtual const char ** getXPMIcon() const
Get the icon for this module in XPM format.
WMGaussFiltering()
Standard constructor.
std::shared_ptr< WCondition > m_propCondition
A condition used to notify about changes in several properties.
virtual const std::string getName() const
Gives back the name of this module.
WPropInt m_iterations
The number of iterations to use for filtering.
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.
std::shared_ptr< WProperties > m_properties
The property object for the module.
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.