OpenWalnut  1.5.0dev
WMDistanceMapIsosurface.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 <algorithm>
26 #include <memory>
27 #include <stdint.h>
28 #include <string>
29 #include <vector>
30 
31 #include "WMDistanceMapIsosurface.h"
32 #include "WMDistanceMapIsosurface.xpm"
33 #include "core/dataHandler/WGridRegular3D.h"
34 #include "core/dataHandler/WSubject.h"
35 #include "core/kernel/WKernel.h"
36 #include "core/kernel/WModuleFactory.h"
37 #include "core/kernel/WPrototypeRequirement.h"
38 
39 // This line is needed by the module loader to actually find your module.
40 W_LOADABLE_MODULE( WMDistanceMapIsosurface )
41 
43  WModuleContainer( "Distance Map Isosurface",
44  "Computes a smoothed version of the dataset"
45  " and a distance map on it. Finally it renders"
46  " this distance map using an isosurface. This isosurface"
47  " can be textured with values from scalar data sets in order to display"
48  " the structures at the given distance."
49  " This is <b>only</b> useful for peeled data." )
50 {
51  // WARNING: initializing connectors inside the constructor will lead to an exception.
52  // NOTE: Do not use the module factory inside this constructor. This will cause a dead lock as the module factory is locked
53  // during construction of this instance and can then not be used to create another instance (Isosurface in this case). If you
54  // want to initialize some modules using the module factory BEFORE the moduleMain() call, overwrite WModule::initialize().
55 }
56 
58 {
59 }
60 
61 std::shared_ptr< WModule > WMDistanceMapIsosurface::factory() const
62 {
63  return std::shared_ptr< WModule >( new WMDistanceMapIsosurface() );
64 }
65 
67 {
68  return distancemapIsosurface_xpm;
69 }
70 
72 {
73  //////////////////////////////////////////////////////////////////////////////////
74  // Isosurface
75  //////////////////////////////////////////////////////////////////////////////////
76 
77  // create an instance using the prototypes
78  m_isosurfaceModule = WModuleFactory::getModuleFactory()->create( WModuleFactory::getModuleFactory()->getPrototypeByName( "Isosurface" ) );
79 
80  // add the isosurface to the container
82 
83  // now wait for it to be ready
84  m_isosurfaceModule->isReady().wait();
85  std::shared_ptr< WProperties > mcProps = m_isosurfaceModule->getProperties();
86  m_isoValueProp = mcProps->getProperty( "Iso value" )->toPropDouble();
87  m_isoValueProp->set( 0.2 );
88  m_isoValueProp->setMin( 0.0 );
89  m_isoValueProp->setMax( 1.0 );
90  m_properties->addProperty( m_isoValueProp );
91 
92 
93  m_useTextureProp = mcProps->getProperty( "Use texture" )->toPropBool();
94  m_useTextureProp->set( true );
95  m_properties->addProperty( m_useTextureProp );
96 
97  m_surfaceColorProp = mcProps->getProperty( "Surface color" )->toPropColor();
98  m_properties->addProperty( m_surfaceColorProp );
99 
100  m_opacityProp = mcProps->getProperty( "Opacity %" )->toPropInt();
101  m_properties->addProperty( m_opacityProp );
102 
103 
104  //////////////////////////////////////////////////////////////////////////////////
105  // Distance Map
106  //////////////////////////////////////////////////////////////////////////////////
107 
108  // create a new instance of WMDistanceMap
109  m_distanceMapModule = WModuleFactory::getModuleFactory()->create( WModuleFactory::getModuleFactory()->getPrototypeByName( "Distance Map" ) );
110 
111  // add it to the container
113 
114  // wait until it is ready
115  m_distanceMapModule->isReady().wait();
116 
117  //////////////////////////////////////////////////////////////////////////////////
118  // Hard wire both modules
119  //////////////////////////////////////////////////////////////////////////////////
120 
121  // NOTE: you can use the WModuleContainer::applyModule functions here, which, in this case, is possible, since the connectors
122  // can be connected unambiguously (one to one connection). But to show how hard wiring works, we do it manually here.
123  m_isosurfaceModule->getInputConnector( "values" )->connect( m_distanceMapModule->getOutputConnector( "out" ) );
124  // this is the same as doing it the other way around.
125  // m_distanceMapModule->getOutputConnector( "out" )->connect( m_isosurfaceModule->getInputConnector( "values" ) );
126  // simple, isn't it? ;-)
127 
128  //////////////////////////////////////////////////////////////////////////////////
129  // Setup forwarding of this modules connectors with the contained ones
130  //////////////////////////////////////////////////////////////////////////////////
131 
132  // connect the distance map output to the container output to ensure other modules can use the distance map if they want to
133  m_output->forward( m_distanceMapModule->getOutputConnector( "out" ) );
134  // we want the container input connector "in" to be connected to the input of WMDistanceMap
135  m_input->forward( m_distanceMapModule->getInputConnector( "in" ) );
136 
137  //////////////////////////////////////////////////////////////////////////////////
138  // Done! Modules are set up.
139  //////////////////////////////////////////////////////////////////////////////////
140 
141  // signal ready state
142  ready();
143 
144  // wait for stop request
145  waitForStop();
146 
147  // stop container and the contained modules.
148  stop();
149 }
150 
152 {
153  // initialize connectors
154 
155  // this is the scalar field input
156  m_input = std::shared_ptr< WModuleInputForwardData< WDataSetScalar > >(
157  new WModuleInputForwardData< WDataSetScalar >( shared_from_this(),
158  "in", "Dataset to compute distance map for." )
159  );
160 
161  // add it to the list of connectors. Please note, that a connector NOT added via addConnector will not work as expected.
163 
164  // this output is used to provide the distance map to other modules.
165  m_output = std::shared_ptr< WModuleOutputForwardData< WDataSetScalar > >(
166  new WModuleOutputForwardData< WDataSetScalar >( shared_from_this(),
167  "out", "Distance map for the input data set." )
168  );
169 
170  // add it to the list of connectors. Please note, that a connector NOT added via addConnector will not work as expected.
172 
173  // call WModules initialization
175 }
176 
178 {
179  m_requirements.push_back( new WPrototypeRequirement( "Distance Map" ) );
180  m_requirements.push_back( new WPrototypeRequirement( "Isosurface" ) );
181 }
182 
184 {
185  m_isosurfaceModule->getProperties()->getProperty( "active" )->toPropBool()->set( m_active->get() );
186 }
187 
Computes a distance map from an anatomy dataset and renders it as isosurface.
std::shared_ptr< WModuleOutputForwardData< WDataSetScalar > > m_output
Connector to provide the distance map to other modules.
std::shared_ptr< WModuleInputForwardData< WDataSetScalar > > m_input
Input connector required by this module.
virtual void activate()
Callback for m_active.
WPropColor m_surfaceColorProp
Property indicating which color to use for non-textured surface.
std::shared_ptr< WModule > m_isosurfaceModule
The isosurface module used in this container.
virtual void connectors()
Initialize the connectors this module is using.
WMDistanceMapIsosurface()
Standard constructor.
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...
WPropInt m_opacityProp
Property holding the value for the opacity of the surface.
virtual const char ** getXPMIcon() const
Get the icon for this module in XPM format.
WPropDouble m_isoValueProp
Property holding the value for the distance.
virtual void requirements()
Initialize requirements for this module.
std::shared_ptr< WModule > m_distanceMapModule
The distance map module used in this container.
virtual void moduleMain()
Entry point after loading the module.
WPropBool m_useTextureProp
Property indicating whether to use texturing with scalar data sets.
Class able to contain other modules.
virtual void stop()
Stops all modules inside this container.
virtual void add(std::shared_ptr< WModule > module, bool run=true)
Add a module to this container and start it.
static SPtr getModuleFactory()
Returns instance of the module factory to use to create modules.
Requirements m_requirements
The list of requirements.
Definition: WModule.h:754
void addConnector(std::shared_ptr< WModuleInputConnector > con)
Adds the specified connector to the list of inputs.
Definition: WModule.cpp:108
std::shared_ptr< WProperties > m_properties
The property object for the module.
Definition: WModule.h:640
void ready()
Call this whenever your module is ready and can react on property changes.
Definition: WModule.cpp:505
WPropBool m_active
True whenever the module should be active.
Definition: WModule.h:723
virtual void connectors()
Initialize connectors in this function.
Definition: WModule.cpp:208
This requirement ensures that the specified prototype exists in the factory.
void waitForStop()
Let the thread sleep until a stop request was given.