29 #include "WMSpatialDerivative.h"
30 #include "WMSpatialDerivative.xpm"
31 #include "core/common/WPropertyHelper.h"
32 #include "core/dataHandler/WDataHandler.h"
33 #include "core/kernel/WKernel.h"
55 return WMSpatialDerivative_xpm;
60 return "Spatial Derivative";
65 return "This module a scalar field and derives it spatially.";
72 "Needs to be in the same grid as the mesh." );
76 " derivate of the input field." );
117 std::shared_ptr< WDataSetScalar > dataSet =
m_scalarIn->getData();
119 bool dataValid = ( dataSet != NULL );
128 if( dataValid && !dataUpdated )
134 std::shared_ptr< WProgress > progress(
new WProgress(
"Processing", 0 ) );
138 std::shared_ptr< WGridRegular3D > grid = std::dynamic_pointer_cast< WGridRegular3D >( dataSet->getGrid() );
141 errorLog() <<
"Only regular 3D grids allowed.";
145 switch( dataSet->getValueSet()->getDataType() )
147 case W_DT_UNSIGNED_CHAR:
149 std::shared_ptr<WValueSet<unsigned char> > vals;
150 vals = std::dynamic_pointer_cast<WValueSet<unsigned char> >( dataSet->getValueSet() );
151 WAssert( vals,
"Data type and data type indicator must fit." );
157 std::shared_ptr<WValueSet<int16_t> > vals;
158 vals = std::dynamic_pointer_cast<WValueSet<int16_t> >( dataSet->getValueSet() );
159 WAssert( vals,
"Data type and data type indicator must fit." );
163 case W_DT_SIGNED_INT:
165 std::shared_ptr<WValueSet<int32_t> > vals;
166 vals = std::dynamic_pointer_cast<WValueSet<int32_t> >( dataSet->getValueSet() );
167 WAssert( vals,
"Data type and data type indicator must fit." );
173 std::shared_ptr< WValueSet< float > > vals;
174 vals = std::dynamic_pointer_cast< WValueSet< float > >( dataSet->getValueSet() );
175 WAssert( vals,
"Data type and data type indicator must fit." );
181 std::shared_ptr< WValueSet< double > > vals;
182 vals = std::dynamic_pointer_cast< WValueSet< double > >( dataSet->getValueSet() );
183 WAssert( vals,
"Data type and data type indicator must fit." );
188 WAssert(
false,
"Unknown data type in." );
199 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 )
201 return offset + ( elements * ( z * xDim * yDim + y * xDim + x ) );
204 template<
typename T >
207 size_t nX = grid->getNbCoordsX();
208 size_t nY = grid->getNbCoordsY();
209 size_t nZ = grid->getNbCoordsZ();
211 std::shared_ptr< std::vector< double > > vectors =
212 std::shared_ptr< std::vector< double > >(
new std::vector< double >( 3 * nX * nY * nZ, 0.0 ) );
215 for(
size_t z = 1; z < nZ - 1; z++ )
217 for(
size_t y = 1; y < nY - 1; y++ )
219 for(
size_t x = 1; x < nX - 1; x++ )
223 float xp = values->getScalar( getId( nX, nY, nZ, x + 1, y, z ) );
224 float xm = values->getScalar( getId( nX, nY, nZ, x - 1, y, z ) );
225 float yp = values->getScalar( getId( nX, nY, nZ, x, y + 1, z ) );
226 float ym = values->getScalar( getId( nX, nY, nZ, x, y - 1, z ) );
227 float zp = values->getScalar( getId( nX, nY, nZ, x, y, z + 1 ) );
228 float zm = values->getScalar( getId( nX, nY, nZ, x, y, z - 1 ) );
230 float vx = ( xp - xm ) / 2.0;
231 float vy = ( yp - ym ) / 2.0;
232 float vz = ( zp - zm ) / 2.0;
234 float sqsum = vx * vx + vy * vy + vz * vz;
235 float len = sqrt( sqsum );
236 float scal =
m_normalize->get(
true ) ? 1.0 / len : 1.0;
240 ( *vectors )[ getId( nX, nY, nZ, x, y, z, 0, 3 ) ] = scal * vx;
241 ( *vectors )[ getId( nX, nY, nZ, x, y, z, 1, 3 ) ] = scal * vy;
242 ( *vectors )[ getId( nX, nY, nZ, x, y, z, 2, 3 ) ] = scal * vz;
247 std::shared_ptr< WValueSet< double > > valueset = std::shared_ptr< WValueSet< double > >(
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 vectors as values.
This modules takes a dataset and derives it spatially.
virtual const std::string getName() const
Gives back the name of this module.
std::shared_ptr< WModuleInputData< WDataSetScalar > > m_scalarIn
An input connector used to get datasets from other modules.
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...
WMSpatialDerivative()
Default constructor.
virtual const char ** getXPMIcon() const
Get the icon for this module in XPM format.
std::shared_ptr< WCondition > m_propCondition
A condition used to notify about changes in several properties.
virtual void connectors()
Initialize the connectors this module is using.
virtual void moduleMain()
Entry point after loading the module.
std::shared_ptr< WModuleOutputData< WDataSetVector > > m_vectorOut
The output connector used to provide the calculated data to other modules.
void derive(std::shared_ptr< WGridRegular3D > grid, std::shared_ptr< WValueSet< T > > values)
Derives the specified scalar data set spatially and creates a new vector value set.
virtual const std::string getDescription() const
Gives back a description of this module.
virtual ~WMSpatialDerivative()
Destructor.
virtual void properties()
Initialize the properties for this module.
WPropBool m_normalize
If true, the vectors get normalized.
static PtrType createAndAdd(std::shared_ptr< WModule > module, std::string name="", std::string description="")
Convenience method to create a new instance of this out data connector with proper type and add it to...
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.
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.
wlog::WStreamedLogger errorLog() const
Logger instance for comfortable error logging.
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.