33 #include <boost/variant.hpp>
35 #include "WMScalarOperator.h"
36 #include "WMScalarOperator.xpm"
37 #include "core/common/WAssert.h"
38 #include "core/common/WProgress.h"
39 #include "core/common/WStringUtils.h"
40 #include "core/common/WTypeTraits.h"
41 #include "core/common/exceptions/WTypeMismatch.h"
42 #include "core/dataHandler/WDataHandler.h"
43 #include "core/dataHandler/WDataHandlerEnums.h"
44 #include "core/dataHandler/WGridRegular3D.h"
45 #include "core/dataHandler/exceptions/WDHValueSetMismatch.h"
46 #include "core/kernel/WKernel.h"
70 return WMScalarOperator_xpm;
75 return "Scalar Operator";
80 return "Applies an selected operator on both datasets on a per-voxel basis. Until now, it assumes that both grids are the same.";
104 m_operations->addItem(
"abs( A - B )",
"Absolute value of A - B." );
105 m_operations->addItem(
"abs( A )",
"Absolute value of A." );
106 m_operations->addItem(
"clamp( lower, upper, A )",
"Clamp A between lower and upper so that l <= A <= u." );
107 m_operations->addItem(
"A * upper",
"Scale data by factor." );
108 m_operations->addItem(
"Binarize A by upper",
"Values > upper, become 1, below or equal become 0" );
136 template <
typename T >
139 return std::abs( u );
149 inline int64_t
wabs( int64_t u )
151 return u < 0 ? -u : u;
161 inline uint8_t
wabs( uint8_t u )
173 inline uint16_t
wabs( uint16_t u )
185 inline uint32_t
wabs( uint32_t u )
197 inline uint64_t
wabs( uint64_t u )
212 template<
typename T >
213 inline T opAbsMinus( T a, T b )
227 template<
typename T >
228 inline T opPlus( T a, T b )
242 template<
typename T >
243 inline T opMinus( T a, T b )
257 template<
typename T >
258 inline T opTimes( T a, T b )
272 template<
typename T >
273 inline T opDiv( T a, T b )
288 template<
typename T >
289 inline T opAbs( T a, T , T )
304 template<
typename T >
305 inline T opBinarizeByA( T a, T , T u )
307 return ( a > u ) ? 1 : 0;
320 template<
typename T >
321 inline T opClamp( T a, T l, T u )
323 T uclamp = a > u ? u : a;
324 return uclamp < l ? l : uclamp;
337 template<
typename T >
338 inline T opScaleByA( T v, T , T u )
349 template<
typename VSetAType >
350 class VisitorVSetB:
public boost::static_visitor< std::shared_ptr< WValueSetBase > >
361 boost::static_visitor< result_type >(),
375 template <
typename VSetBType >
384 std::vector< ResultT > data;
390 boost::function< ResultT( ResultT, ResultT ) > op;
394 op = &opMinus< ResultT >;
397 op = &opTimes< ResultT >;
400 op = &opDiv< ResultT >;
403 op = &opAbsMinus< ResultT >;
407 op = &opPlus< ResultT >;
413 const VSetBType* b = vsetB->rawData();
416 data[ i ] = op(
static_cast< ResultT
>( a[ i ] ),
static_cast< ResultT
>( b[ i ] ) );
420 std::shared_ptr< WValueSet< ResultT > > result = std::shared_ptr< WValueSet< ResultT > >(
423 std::shared_ptr< std::vector< ResultT > >(
new std::vector< ResultT >( data ) ),
444 class VisitorVSetA:
public boost::static_visitor< std::shared_ptr< WValueSetBase > >
454 boost::static_visitor< result_type >(),
468 template <
typename T >
499 boost::static_visitor< result_type >(),
512 template <
typename T >
518 size_t order = vsetA->order();
519 size_t dim = vsetA->dimension();
521 std::vector< ResultT > data;
522 data.resize( vsetA->rawSize() );
527 boost::function< ResultT( ResultT, ResultT l, ResultT u ) > op;
531 op = &opAbs< ResultT >;
534 op = &opClamp< ResultT >;
537 op = &opScaleByA< ResultT >;
540 op = &opBinarizeByA< ResultT >;
543 op = &opAbs< ResultT >;
548 const T* a = vsetA->rawData();
549 for(
size_t i = 0; i < vsetA->rawSize(); ++i )
555 std::shared_ptr< WValueSet< ResultT > > result = std::shared_ptr< WValueSet< ResultT > >(
558 std::shared_ptr< std::vector< ResultT > >(
new std::vector< ResultT >( data ) ),
622 std::shared_ptr< WDataSetScalar > dataSetA =
m_inputA->getData();
623 std::shared_ptr< WDataSetScalar > dataSetB =
m_inputB->getData();
633 std::shared_ptr< WValueSetBase > valueSetA = dataSetA->getValueSet();
636 std::shared_ptr< WProgress > prog = std::shared_ptr< WProgress >(
637 new WProgress(
"Applying operator on data" ) );
647 std::shared_ptr< WValueSetBase > newValueSet;
650 if( ( s == 5 ) || ( s == 6 ) || ( s == 7 ) || ( s == 8 ) )
654 newValueSet = valueSetA->applyFunction( visitor );
661 std::shared_ptr< WValueSetBase > valueSetB = dataSetB->getValueSet();
664 bool match = ( valueSetA->dimension() == valueSetB->dimension() ) &&
665 ( valueSetA->order() == valueSetB->order() ) &&
666 ( valueSetA->rawSize() == valueSetB->rawSize() );
669 throw WDHValueSetMismatch( std::string(
"The two value sets are not of equal size, dimension and order." ) );
673 newValueSet = valueSetA->applyFunction( visitor );
695 m_output->updateData( std::shared_ptr<WDataSetScalar>(
new WDataSetScalar( newValueSet, dataSetA->getGrid() ) ) );
Visitor for discriminating the type of the first valueset.
result_type operator()(const WValueSet< T > *const &vsetA) const
Called by boost::varying during static visiting.
size_t m_opIdx
The operator index.
VisitorVSetA(WValueSetBase *vsetB, size_t opIdx=0)
Create visitor instance.
WValueSetBase * m_vsetB
The valueset where to cascade.
The second visitor which got applied to the second value set.
result_type operator()(const WValueSet< VSetBType > *const &vsetB) const
Visitor on the second valueset.
const WValueSet< VSetAType > *const m_vsetA
The first valueset.
VisitorVSetB(const WValueSet< VSetAType > *const vsetA, size_t opIdx=0)
Creates visitor for the second level of cascading.
size_t m_opIdx
The operator index.
Visitor for discriminating the type of the first valueset.
result_type operator()(const WValueSet< T > *const &vsetA) const
Called by boost::varying during static visiting.
VisitorVSetSingleArgument(size_t opIdx=0)
Create visitor instance.
size_t m_opIdx
The operator index.
void setBorder(double l, double u)
Set lower and upper border needed for several ops.
double m_lowerBorder
Lower border needed for several ops.
double m_upperBorder
Upper border needed for several ops.
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.
An exception that should be used whenever two valuesets are used which need to be of same size,...
This data set type contains scalars as values.
A class containing a list of named items.
This class represents a subset of a WItemSelection.
Operators for processing two WDataSetScalar.
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...
WMScalarOperator()
Standard constructor.
virtual const char ** getXPMIcon() const
Get the icon for this module in XPM format.
virtual const std::string getDescription() const
Gives back a description of this module.
std::shared_ptr< WModuleOutputData< WDataSetScalar > > m_output
The only output of this filter module.
virtual const std::string getName() const
Gives back the name of this module.
WPropDouble m_lowerBorder
Lower border used for clamping.
WPropDouble m_upperBorder
Upper border used for clamping.
std::shared_ptr< WItemSelection > m_operations
A list of operations that are possible.
std::shared_ptr< WCondition > m_propCondition
A condition used to notify about changes in several properties.
~WMScalarOperator()
Destructor.
std::shared_ptr< WModuleInputData< WDataSetScalar > > m_inputB
Input connector required by this module.
virtual void properties()
Initialize the properties for this module.
WPropSelection m_opSelection
The currently selected operation.
std::shared_ptr< WModuleInputData< WDataSetScalar > > m_inputA
Input connector required by this module.
virtual void moduleMain()
Entry point after loading the module.
virtual void connectors()
Initialize the connectors this module is using.
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.
void removeConnectors()
Removes all connectors properly.
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.
Abstract base class to all WValueSets.
virtual size_t dimension() const
Func_T::result_type applyFunction(Func_T const &func)
Apply a function object to this valueset.
virtual size_t order() const
const T * rawData() const
Sometimes we need raw access to the data array, for e.g.
virtual size_t rawSize() const
dataType
Data types and number values taken from the nifti1.h, at this point it's unknown if it makes sense to...
void addTo(WPropSelection prop)
Add the PC_NOTEMPTY constraint to the property.
void addTo(WPropSelection prop)
Add the PC_SELECTONLYONE constraint to the property.
T wabs(T u)
Absolute value of the specified parameter.
An object that knows an appropriate dataType flag for the typename T.