OpenWalnut  1.5.0dev
WMApplyMask.cpp
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #include <cmath>
26 #include <fstream>
27 #include <iostream>
28 #include <memory>
29 #include <stdint.h>
30 #include <string>
31 #include <utility>
32 #include <vector>
33 
34 #include "WMApplyMask.h"
35 #include "WMApplyMask.xpm"
36 #include "core/common/WAssert.h"
37 #include "core/common/WProgress.h"
38 #include "core/kernel/WKernel.h"
39 
40 // This line is needed by the module loader to actually find your module.
41 W_LOADABLE_MODULE( WMApplyMask )
42 
44  WModule()
45 {
46  // WARNING: initializing connectors inside the constructor will lead to an exception.
47  // Implement WModule::initializeConnectors instead.
48 }
49 
51 {
52  // cleanup
54 }
55 
56 std::shared_ptr< WModule > WMApplyMask::factory() const
57 {
58  return std::shared_ptr< WModule >( new WMApplyMask() );
59 }
60 
61 const char** WMApplyMask::getXPMIcon() const
62 {
63  return apply_mask_xpm;
64 }
65 
66 const std::string WMApplyMask::getName() const
67 {
68  return "Apply Mask";
69 }
70 
71 const std::string WMApplyMask::getDescription() const
72 {
73  return "Applies a mask to a data set, i.e. sets all voxels to zero which are zero in the mask.";
74 }
75 
77 {
78  // use the m_input "data changed" flag
79  m_moduleState.setResetable( true, true );
80  m_moduleState.add( m_dataInput->getDataChangedCondition() );
81  m_moduleState.add( m_maskInput->getDataChangedCondition() );
82 
83  // signal ready state
84  ready();
85 
86  // loop until the module container requests the module to quit
87  while( !m_shutdownFlag() )
88  {
89  // acquire data from the input connector
90  m_dataSet = m_dataInput->getData();
91  m_mask = m_maskInput->getData();
92  if( !m_dataSet || !m_mask )
93  {
94  // ok, the output has not yet sent data
95  // NOTE: see comment at the end of this while loop for m_moduleState
96  debugLog() << "Waiting for data ...";
98  continue;
99  }
100  dataType type = m_dataSet->getValueSet()->getDataType();
101  switch( type )
102  {
103  case W_DT_UNSIGNED_CHAR:
104  {
105  std::shared_ptr< WValueSet< unsigned char > > vals;
106  vals = std::dynamic_pointer_cast< WValueSet< unsigned char > >( ( *m_dataSet ).getValueSet() );
107  WAssert( vals, "Data type and data type indicator must fit." );
108  applyMask( vals, type );
109  break;
110  }
111  case W_DT_INT16:
112  {
113  std::shared_ptr< WValueSet< int16_t > > vals;
114  vals = std::dynamic_pointer_cast< WValueSet< int16_t > >( ( *m_dataSet ).getValueSet() );
115  WAssert( vals, "Data type and data type indicator must fit." );
116  applyMask( vals, type );
117  break;
118  }
119  case W_DT_SIGNED_INT:
120  {
121  std::shared_ptr< WValueSet< int32_t > > vals;
122  vals = std::dynamic_pointer_cast< WValueSet< int32_t > >( ( *m_dataSet ).getValueSet() );
123  WAssert( vals, "Data type and data type indicator must fit." );
124  applyMask( vals, type );
125  break;
126  }
127  case W_DT_FLOAT:
128  {
129  std::shared_ptr< WValueSet< float > > vals;
130  vals = std::dynamic_pointer_cast< WValueSet< float > >( ( *m_dataSet ).getValueSet() );
131  WAssert( vals, "Data type and data type indicator must fit." );
132  applyMask( vals, type );
133  break;
134  }
135  case W_DT_DOUBLE:
136  {
137  std::shared_ptr< WValueSet< double > > vals;
138  vals = std::dynamic_pointer_cast< WValueSet< double > >( ( *m_dataSet ).getValueSet() );
139  WAssert( vals, "Data type and data type indicator must fit." );
140  applyMask( vals, type );
141  break;
142  }
143  default:
144  throw WException( std::string( "Data type of value set not supported by this module." ) );
145  }
146 
147  // this waits for m_moduleState to fire. By default, this is only the m_shutdownFlag condition.
148  // NOTE: you can add your own conditions to m_moduleState using m_moduleState.add( ... )
150  }
151 }
152 
154 {
155  // initialize connectors
156  m_dataInput = std::shared_ptr< WModuleInputData< WDataSetScalar > >( new WModuleInputData< WDataSetScalar >(
157  shared_from_this(), "dataSet", "The dataset to apply the mask to." ) );
158 
159  // add it to the list of connectors. Please note, that a connector NOT added via addConnector will not work as expected.
161 
162  // initialize connectors
163  m_maskInput = std::shared_ptr< WModuleInputData< WDataSetScalar > >(
164  new WModuleInputData< WDataSetScalar >( shared_from_this(), "mask",
165  "The mask applied to the data." ) );
166 
167  // add it to the list of connectors. Please note, that a connector NOT added via addConnector will not work as expected.
169 
170  // initialize connectors
171  m_output = std::shared_ptr< WModuleOutputData< WDataSetScalar > >(
172  new WModuleOutputData< WDataSetScalar >( shared_from_this(), "out",
173  "The filtered data set." ) );
174 
175  // add it to the list of connectors. Please note, that a connector NOT added via addConnector will not work as expected.
177 
178  // call WModules initialization
180 }
181 
183 {
185 }
186 
187 template< typename T > void WMApplyMask::applyMask( std::shared_ptr< WValueSet< T > > valSet, dataType type )
188 {
189  std::shared_ptr< WValueSetBase > maskBase = m_mask->getValueSet();
190  std::shared_ptr< WValueSet< float > > mask = std::dynamic_pointer_cast< WValueSet< float > >( maskBase );
191 
192  if( !mask )
193  {
194  throw WException( std::string( "Mask is not of type float." ) );
195  }
196 
197  std::shared_ptr< WProgress > progress( new WProgress( "Apply Mask", valSet->size() ) );
198  m_progress->addSubProgress( progress );
199 
200  std::shared_ptr< std::vector< T > > newVals( new std::vector< T >( valSet->size() ) );
201  for( size_t i = 0; i < valSet->size(); ++i )
202  {
203  ++*progress;
204  if( mask->getScalar( i ) == 0 )
205  {
206  ( *newVals )[i] = 0;
207  }
208  else
209  {
210  ( *newVals )[i] = valSet->getScalar( i );
211  }
212  }
213  progress->finish();
214 
215  std::shared_ptr< WValueSet< T > > valueSet;
216  valueSet = std::shared_ptr< WValueSet< T > >( new WValueSet< T >( 0, 1, newVals, type ) );
217 
218  m_dataSetOut = std::shared_ptr< WDataSetScalar >( new WDataSetScalar( valueSet, m_dataSet->getGrid() ) );
219  m_output->updateData( m_dataSetOut );
220 }
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.
Basic exception handler.
Definition: WException.h:39
Set all voxels in a dataset to zero if the other dataset is zero there.
Definition: WMApplyMask.h:51
WMApplyMask()
Standard constructor.
Definition: WMApplyMask.cpp:43
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...
Definition: WMApplyMask.cpp:56
std::shared_ptr< WDataSetScalar > m_dataSet
Pointer providing access to the data set in the whole module.
Definition: WMApplyMask.h:117
virtual void connectors()
Initialize the connectors this module is using.
std::shared_ptr< WModuleInputData< WDataSetScalar > > m_dataInput
Input connector for getting the data.
Definition: WMApplyMask.h:114
virtual void moduleMain()
Entry point after loading the module.
Definition: WMApplyMask.cpp:76
virtual const char ** getXPMIcon() const
Get the icon for this module in XPM format.
Definition: WMApplyMask.cpp:61
virtual void properties()
Initialize the properties for this module.
std::shared_ptr< WDataSetScalar > m_mask
Pointer providing access to the mask in the whole module.
Definition: WMApplyMask.h:119
void applyMask(std::shared_ptr< WValueSet< T > > valSet, dataType type)
Apply the mask to the data.
virtual const std::string getName() const
Gives back the name of this module.
Definition: WMApplyMask.cpp:66
virtual const std::string getDescription() const
Gives back a description of this module.
Definition: WMApplyMask.cpp:71
~WMApplyMask()
Destructor.
Definition: WMApplyMask.cpp:50
std::shared_ptr< WModuleInputData< WDataSetScalar > > m_maskInput
Input connector for getting the mask.
Definition: WMApplyMask.h:115
std::shared_ptr< WModuleOutputData< WDataSetScalar > > m_output
The only output of this mask module.
Definition: WMApplyMask.h:116
std::shared_ptr< WDataSetScalar > m_dataSetOut
Pointer providing access to the resulting data set in the whole module.
Definition: WMApplyMask.h:118
Class offering an instantiate-able data connection between modules.
Class offering an instantiate-able data connection between modules.
Class representing a single module of OpenWalnut.
Definition: WModule.h:72
virtual void properties()
Initialize properties in this function.
Definition: WModule.cpp:212
wlog::WStreamedLogger debugLog() const
Logger instance for comfortable debug logging.
Definition: WModule.cpp:575
void removeConnectors()
Removes all connectors properly.
Definition: WModule.cpp:194
void addConnector(std::shared_ptr< WModuleInputConnector > con)
Adds the specified connector to the list of inputs.
Definition: WModule.cpp:108
void ready()
Call this whenever your module is ready and can react on property changes.
Definition: WModule.cpp:505
WConditionSet m_moduleState
The internal state of the module.
Definition: WModule.h:703
std::shared_ptr< WProgressCombiner > m_progress
Progress indicator used as parent for all progress' of this module.
Definition: WModule.h:652
virtual void connectors()
Initialize connectors in this function.
Definition: WModule.cpp:208
Class managing progress inside of modules.
Definition: WProgress.h:42
WBoolFlag m_shutdownFlag
Condition getting fired whenever the thread should quit.
Base Class for all value set types.
Definition: WValueSet.h:47
dataType
Data types and number values taken from the nifti1.h, at this point it's unknown if it makes sense to...