OpenWalnut  1.5.0dev
WMScalarSegmentation.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 <memory>
26 #include <string>
27 #include <vector>
28 
29 #include "WMScalarSegmentation.h"
30 #include "WMScalarSegmentation.xpm"
31 #include "core/common/WColor.h"
32 #include "core/common/WPropertyHelper.h"
33 #include "core/kernel/WKernel.h"
34 
35 // This line is needed by the module loader to actually find your module.
36 W_LOADABLE_MODULE( WMScalarSegmentation )
37 
39  WModule()
40 {
41  m_algoIndex = 0;
42  m_algos.push_back( std::shared_ptr< WSegmentationAlgo >( new WSegmentationAlgoThreshold() ) );
43 #ifdef OW_USE_ITK
44  m_algos.push_back( std::shared_ptr< WSegmentationAlgo >( new WSegmentationAlgoWatershed() ) );
45  m_algos.push_back( std::shared_ptr< WSegmentationAlgo >( new WSegmentationAlgoOtsu() ) );
46  m_algos.push_back( std::shared_ptr< WSegmentationAlgo >( new WSegmentationAlgoRegionGrowingConfidenceConnected() ) );
47  m_algos.push_back( std::shared_ptr< WSegmentationAlgo >( new WSegmentationAlgoLevelSetCanny() ) );
48 #endif // OW_USE_ITK
49 }
50 
52 {
53  m_algos.clear();
54 }
55 
56 std::shared_ptr< WModule > WMScalarSegmentation::factory() const
57 {
58  return std::shared_ptr< WModule >( new WMScalarSegmentation() );
59 }
60 
62 {
63  return scalarSegmentation_xpm;
64 }
65 
66 const std::string WMScalarSegmentation::getName() const
67 {
68  return "Scalar Segmentation";
69 }
70 
71 const std::string WMScalarSegmentation::getDescription() const
72 {
73  return "This module segments scalar datasets.";
74 }
75 
77 {
78  m_input = std::shared_ptr< WModuleInputData < WDataSetScalar > >(
79  new WModuleInputData< WDataSetScalar >( shared_from_this(), "inputSet", "The dataset to segment." )
80  );
81 
83 
84  m_output = std::shared_ptr< WModuleOutputData < WDataSetScalar > >(
85  new WModuleOutputData< WDataSetScalar >( shared_from_this(), "outputSet", "The calculated dataset." )
86  );
87 
89 
91 }
92 
94 {
95  m_propCondition = std::shared_ptr< WCondition >( new WCondition() );
96 
97  m_algoSelection = std::shared_ptr< WItemSelection >( new WItemSelection );
98  for( AlgoList::iterator it = m_algos.begin(); it != m_algos.end(); ++it )
99  {
100  m_algoSelection->addItem( ( *it )->getName(), ( *it )->getDescription() );
101  }
102  m_algoType = m_properties->addProperty( "Segmentation algorithm", "Choose a segmentation method.",
103  m_algoSelection->getSelectorFirst(), m_propCondition );
104 
105  for( AlgoList::iterator it = m_algos.begin(); it != m_algos.end(); ++it )
106  {
107  ( *it )->initProperties( m_properties->addPropertyGroup( ( *it )->getName(), "The properties for this segmentation algorithm.", true ) );
108  }
109 
110  m_algos.at( 0 )->hideProperties( false );
111 
114 
116 }
117 
119 {
120  m_moduleState.setResetable( true, true );
121  m_moduleState.add( m_input->getDataChangedCondition() );
123  for( AlgoList::iterator it = m_algos.begin(); it != m_algos.end(); ++it )
124  {
125  m_moduleState.add( ( *it )->getCondition() );
126  }
127 
128  ready();
129 
130  while( !m_shutdownFlag() )
131  {
133 
134  if( m_shutdownFlag() )
135  {
136  break;
137  }
138 
139  std::shared_ptr< WDataSetScalar > newDataSet = m_input->getData();
140  bool dataChanged = ( m_dataSet != newDataSet );
141  bool dataValid = ( newDataSet != NULL );
142 
143  if( dataChanged && dataValid )
144  {
145  m_dataSet = newDataSet;
146  if( !m_dataSet->getValueSet() || !m_dataSet->getGrid()
147  || m_dataSet->getValueSet()->dimension() != 1 || m_dataSet->getValueSet()->order() != 0 )
148  {
149  m_dataSet = std::shared_ptr< WDataSetScalar >();
150  }
151  }
152 
153  bool algoChanged = m_algoType->changed();
154 
155  if( algoChanged )
156  {
157  WItemSelector w = m_algoType->get( true );
158  m_algos.at( m_algoIndex )->hideProperties( true );
160  m_algos.at( m_algoIndex )->hideProperties( false );
161  }
162 
163  bool propChanged = m_algos.at( m_algoIndex )->propChanged();
164  if( m_dataSet && ( dataChanged || propChanged || algoChanged ) )
165  {
166  // redo calculation
167  doSegmentation();
168  m_output->updateData( m_result );
169  }
170  }
171 
172  for( AlgoList::iterator it = m_algos.begin(); it != m_algos.end(); ++it )
173  {
174  m_moduleState.remove( ( *it )->getCondition() );
175  }
176 }
177 
179 {
181 }
182 
184 {
185  debugLog() << "Starting segmentation.";
186  m_result = m_algos.at( m_algoIndex )->segment( m_dataSet );
187  debugLog() << "Segmentation finished.";
188 }
virtual void wait() const
Wait for the condition.
void setResetable(bool resetable=true, bool autoReset=true)
Sets the resetable flag.
virtual void remove(std::shared_ptr< WCondition > condition)
Removes the specified 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
A class containing a list of named items.
This class represents a subset of a WItemSelection.
Definition: WItemSelector.h:53
virtual size_t getItemIndexOfSelected(size_t index) const
Helps to get the index of an selected item in the WItemSelection.
First version of a module that implements 3D-image segmentation algorithms.
virtual void properties()
Initialize the properties for this module.
std::shared_ptr< WCondition > m_propCondition
A condition used to notify about changes in several properties.
void doSegmentation()
Do a segmentation depending on the current module property values.
virtual void connectors()
Initialize the connectors this module is using.
virtual void moduleMain()
Entry point after loading the module.
virtual void activate()
Callback for m_active.
std::shared_ptr< WModuleInputData< WDataSetScalar > > m_input
An input connector used to get datasets from other modules.
std::shared_ptr< WItemSelection > m_algoSelection
A list of possible segmentation algorithms.
AlgoList m_algos
A list of algorithm objects.
virtual std::shared_ptr< WModule > factory() const
Return a new instance of this module.
WMScalarSegmentation()
Default constructor.
virtual const char ** getXPMIcon() const
Get the icon for this module in XPM format.
virtual const std::string getDescription() const
Return a description of this module.
virtual ~WMScalarSegmentation()
Destructor.
virtual const std::string getName() const
Return the name of this module.
WPropSelection m_algoType
A selection box for segmentation algorithms.
std::shared_ptr< WModuleOutputData< WDataSetScalar > > m_output
The output connector used to provide the calculated data to other modules.
std::shared_ptr< WDataSetScalar > m_dataSet
This is a pointer to the dataset the module is currently working on.
std::size_t m_algoIndex
The number of the currently selected segmentation method.
std::shared_ptr< WDataSetScalar > m_result
This is a pointer to the segmented dataset.
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
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
WConditionSet m_moduleState
The internal state of the module.
Definition: WModule.h:703
virtual void activate()
Callback for m_active.
Definition: WModule.cpp:220
virtual void connectors()
Initialize connectors in this function.
Definition: WModule.cpp:208
A very simple threshold segmentation working in two modi: If in LOWER_THRESHOLD mode than voxels that...
WBoolFlag m_shutdownFlag
Condition getting fired whenever the thread should quit.
void addTo(WPropSelection prop)
Add the PC_NOTEMPTY constraint to the property.
void addTo(WPropSelection prop)
Add the PC_SELECTONLYONE constraint to the property.