1
//---------------------------------------------------------------------------
3
// Project: OpenWalnut ( http://www.openwalnut.org )
5
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6
// For more information see http://www.openwalnut.org/copying
8
// This file is part of OpenWalnut.
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.
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.
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/>.
23
//---------------------------------------------------------------------------
35
#include "core/common/WProgress.h"
36
#include "core/common/WAssert.h"
37
#include "core/kernel/WKernel.h"
38
#include "WMApplyMask.h"
39
#include "WMApplyMask.xpm"
41
// This line is needed by the module loader to actually find your module.
42
W_LOADABLE_MODULE( WMApplyMask )
44
WMApplyMask::WMApplyMask() :
47
// WARNING: initializing connectors inside the constructor will lead to an exception.
48
// Implement WModule::initializeConnectors instead.
51
WMApplyMask::~WMApplyMask()
57
boost::shared_ptr< WModule > WMApplyMask::factory() const
59
return boost::shared_ptr< WModule >( new WMApplyMask() );
62
const char** WMApplyMask::getXPMIcon() const
64
return apply_mask_xpm;
67
const std::string WMApplyMask::getName() const
72
const std::string WMApplyMask::getDescription() const
74
return "Applies a mask to a data set, i.e. sets all voxels to zero which are zero in the mask.";
77
void WMApplyMask::moduleMain()
79
// use the m_input "data changed" flag
80
m_moduleState.setResetable( true, true );
81
m_moduleState.add( m_dataInput->getDataChangedCondition() );
82
m_moduleState.add( m_maskInput->getDataChangedCondition() );
87
// loop until the module container requests the module to quit
88
while( !m_shutdownFlag() )
90
// acquire data from the input connector
91
m_dataSet = m_dataInput->getData();
92
m_mask = m_maskInput->getData();
93
if( !m_dataSet || !m_mask )
95
// ok, the output has not yet sent data
96
// NOTE: see comment at the end of this while loop for m_moduleState
97
debugLog() << "Waiting for data ...";
101
dataType type = m_dataSet->getValueSet()->getDataType();
104
case W_DT_UNSIGNED_CHAR:
106
boost::shared_ptr< WValueSet< unsigned char > > vals;
107
vals = boost::shared_dynamic_cast< WValueSet< unsigned char > >( ( *m_dataSet ).getValueSet() );
108
WAssert( vals, "Data type and data type indicator must fit." );
109
applyMask( vals, type );
114
boost::shared_ptr< WValueSet< int16_t > > vals;
115
vals = boost::shared_dynamic_cast< WValueSet< int16_t > >( ( *m_dataSet ).getValueSet() );
116
WAssert( vals, "Data type and data type indicator must fit." );
117
applyMask( vals, type );
120
case W_DT_SIGNED_INT:
122
boost::shared_ptr< WValueSet< int32_t > > vals;
123
vals = boost::shared_dynamic_cast< WValueSet< int32_t > >( ( *m_dataSet ).getValueSet() );
124
WAssert( vals, "Data type and data type indicator must fit." );
125
applyMask( vals, type );
130
boost::shared_ptr< WValueSet< float > > vals;
131
vals = boost::shared_dynamic_cast< WValueSet< float > >( ( *m_dataSet ).getValueSet() );
132
WAssert( vals, "Data type and data type indicator must fit." );
133
applyMask( vals, type );
138
boost::shared_ptr< WValueSet< double > > vals;
139
vals = boost::shared_dynamic_cast< WValueSet< double > >( ( *m_dataSet ).getValueSet() );
140
WAssert( vals, "Data type and data type indicator must fit." );
141
applyMask( vals, type );
145
throw WException( std::string( "Data type of value set not supported by this module." ) );
148
// this waits for m_moduleState to fire. By default, this is only the m_shutdownFlag condition.
149
// NOTE: you can add your own conditions to m_moduleState using m_moduleState.add( ... )
150
m_moduleState.wait();
154
void WMApplyMask::connectors()
156
// initialize connectors
157
m_dataInput = boost::shared_ptr< WModuleInputData< WDataSetScalar > >( new WModuleInputData< WDataSetScalar >(
158
shared_from_this(), "dataSet", "The dataset to apply the mask to." ) );
160
// add it to the list of connectors. Please note, that a connector NOT added via addConnector will not work as expected.
161
addConnector( m_dataInput );
163
// initialize connectors
164
m_maskInput = boost::shared_ptr< WModuleInputData< WDataSetScalar > >(
165
new WModuleInputData< WDataSetScalar >( shared_from_this(), "mask",
166
"The mask applied to the data." ) );
168
// add it to the list of connectors. Please note, that a connector NOT added via addConnector will not work as expected.
169
addConnector( m_maskInput );
171
// initialize connectors
172
m_output = boost::shared_ptr< WModuleOutputData< WDataSetScalar > >(
173
new WModuleOutputData< WDataSetScalar >( shared_from_this(), "out",
174
"The filtered data set." ) );
176
// add it to the list of connectors. Please note, that a connector NOT added via addConnector will not work as expected.
177
addConnector( m_output );
179
// call WModules initialization
180
WModule::connectors();
183
void WMApplyMask::properties()
185
WModule::properties();
188
template< typename T > void WMApplyMask::applyMask( boost::shared_ptr< WValueSet< T > > valSet, dataType type )
190
boost::shared_ptr< WValueSetBase > maskBase = m_mask->getValueSet();
191
boost::shared_ptr< WValueSet< float > > mask = boost::shared_dynamic_cast< WValueSet< float > >( maskBase );
195
throw WException( std::string( "Mask is not of type float." ) );
198
boost::shared_ptr< WProgress > progress = boost::shared_ptr< WProgress >( new WProgress( "Apply Mask", valSet->size() ) );
199
m_progress->addSubProgress( progress );
201
boost::shared_ptr< std::vector< T > > newVals = boost::shared_ptr< std::vector< T > >( new std::vector< T >( valSet->size() ) );
202
for( size_t i = 0; i < valSet->size(); ++i )
205
if( mask->getScalar( i ) == 0 )
211
( *newVals )[i] = valSet->getScalar( i );
216
boost::shared_ptr< WValueSet< T > > valueSet;
217
valueSet = boost::shared_ptr< WValueSet< T > >( new WValueSet< T >( 0, 1, newVals, type ) );
219
m_dataSetOut = boost::shared_ptr< WDataSetScalar >( new WDataSetScalar( valueSet, m_dataSet->getGrid() ) );
220
m_output->updateData( m_dataSetOut );