OpenWalnut  1.5.0dev
WMResampleRegular.cpp
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2015 OpenWalnut Community, Nemtics, BSV@Uni-Leipzig
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 <memory>
26 #include <string>
27 #include <vector>
28 
29 #include "WMResampleRegular.h"
30 #include "core/common/WProgress.h"
31 #include "core/dataHandler/WGridTransformOrtho.h"
32 #include "core/kernel/WKernel.h"
33 
34 // This line is needed by the module loader to actually find your module. Do not remove. Do NOT add a ";" here.
35 W_LOADABLE_MODULE( WMResampleRegular )
36 
38  WModule()
39 {
40 }
41 
43 {
44  // Cleanup!
45 }
46 
47 std::shared_ptr< WModule > WMResampleRegular::factory() const
48 {
49  // See "src/modules/template/" for an extensively documented example.
50  return std::shared_ptr< WModule >( new WMResampleRegular() );
51 }
52 
53 const std::string WMResampleRegular::getName() const
54 {
55  return "Resample Regular";
56 }
57 
58 const std::string WMResampleRegular::getDescription() const
59 {
60  return "A module to resample a regular dataset with a different regular grid.";
61 }
62 
64 {
65  m_original = std::shared_ptr<WModuleInputData<WDataSetScalar> >(
66  new WModuleInputData<WDataSetScalar> ( shared_from_this(), "Original",
67  "The dataset to resample." ) );
69 
70  m_target = std::shared_ptr<WModuleInputData<WDataSetScalar> >(
71  new WModuleInputData<WDataSetScalar> ( shared_from_this(), "Target",
72  "The original dataset is resamples to the grid of this dataset." ) );
74 
75  m_resampled = std::shared_ptr<WModuleOutputData<WDataSetScalar> >(
76  new WModuleOutputData<WDataSetScalar> ( shared_from_this(), "Resampled",
77  "The resampled data set." ) );
79 
81 }
82 
84 {
85  m_propCondition = std::shared_ptr< WCondition >( new WCondition() );
86 
88 }
89 
91 {
92  // Put the code for your requirements here. See "src/modules/template/" for an extensively documented example.
93 }
94 
96 {
97  m_moduleState.add( m_original->getDataChangedCondition() );
98  m_moduleState.add( m_target->getDataChangedCondition() );
100 
101  // signal ready state
102  ready();
103 
104  // loop until the module container requests the module to quit
105  while( !m_shutdownFlag() )
106  {
107  debugLog() << "Waiting ...";
109 
110  // woke up since the module is requested to finish
111  if( m_shutdownFlag() )
112  {
113  break;
114  }
115 
116  std::shared_ptr< WDataSetScalar > originalData = m_original->getData();
117  std::shared_ptr< WDataSetScalar > targetData = m_target->getData();
118 
119  // If no data found go into waiting state again.
120  if( !originalData || !targetData )
121  {
122  continue;
123  }
124 
125 
126  std::shared_ptr< WGridRegular3D > resampledGrid = std::dynamic_pointer_cast< WGridRegular3D >( targetData->getGrid() );
127 
128  std::shared_ptr<WValueSetBase> vals;
129  vals = std::dynamic_pointer_cast<WValueSetBase >( originalData->getValueSet() );
130 
131  std::shared_ptr< std::vector< float > > theValues;
132  theValues = std::make_shared< std::vector< float > >();
133 
134  size_t nX = resampledGrid->getNbCoordsX();
135  size_t nY = resampledGrid->getNbCoordsY();
136  size_t nZ = resampledGrid->getNbCoordsZ();
137 
138  std::shared_ptr< WProgress > progress( new WProgress( "Resampling", nZ ) );
139  m_progress->addSubProgress( progress );
140 
141  for( size_t idZ = 0; idZ < nZ; ++idZ )
142  {
143  ++*progress;
144  for( size_t idY = 0; idY < nY; ++idY )
145  {
146  for( size_t idX = 0; idX < nX; ++idX )
147  {
148  bool valid;
149  theValues->push_back( static_cast<float>( originalData->interpolate( resampledGrid->getPosition( idX, idY, idZ ), &valid ) ) );
150  }
151  }
152  }
153 
154  std::shared_ptr< WValueSet< float > > newValueSet;
155  newValueSet = std::shared_ptr< WValueSet< float > >( new WValueSet<float>( vals->order(), vals->dimension(), theValues ) );
156 
157  m_resampled->updateData( std::shared_ptr<WDataSetScalar>( new WDataSetScalar( newValueSet, resampledGrid ) ) );
158  progress->finish();
159  }
160 }
virtual void wait() const
Wait for the condition.
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.
Definition: WCondition.h:42
This data set type contains scalars as values.
A module to resample a regular dataset with a different regular grid.
virtual void requirements()
Initialize requirements for this module.
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_original
Connector providing original input data.
virtual const std::string getDescription() const
Gives back a description of this module.
WMResampleRegular()
Constructor currently only initializing the class members.
std::shared_ptr< WModuleInputData< WDataSetScalar > > m_target
Connector providing target grid.
virtual const std::string getName() const
Gives back the name of this module.
virtual void connectors()
Initialize the connectors this module is using.
virtual ~WMResampleRegular()
Still empty destructor.
virtual void moduleMain()
Entry point after loading the module.
std::shared_ptr< WModuleOutputData< WDataSetScalar > > m_resampled
Connector yielding resampled data.
std::shared_ptr< WCondition > m_propCondition
A condition used to notify about changes in several properties.
virtual void properties()
Initialize the properties for this module.
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 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