OpenWalnut  1.5.0dev
WMFiberFilterROI.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 "WMFiberFilterROI.h"
30 #include "core/kernel/WKernel.h"
31 
32 // This line is needed by the module loader to actually find your module. You need to add this to your module too. Do NOT add a ";" here.
33 W_LOADABLE_MODULE( WMFiberFilterROI )
34 
36  WModule()
37 {
38  // Init
39 }
40 
42 {
43  // Cleanup!
44 }
45 
46 std::shared_ptr< WModule > WMFiberFilterROI::factory() const
47 {
48  return std::shared_ptr< WModule >( new WMFiberFilterROI() );
49 }
50 
51 const std::string WMFiberFilterROI::getName() const
52 {
53  // Specify your module name here. This name must be UNIQUE!
54  return "Fiber Filter ROI";
55 }
56 
57 const std::string WMFiberFilterROI::getDescription() const
58 {
59  // Specify your module description here. Be detailed. This text is read by the user.
60  return "This module provides fiber data filtered by the current ROI configuration.";
61 }
62 
64 {
65  m_input = WModuleInputData< WDataSetFibers >::createAndAdd( shared_from_this(), "in", "The dataset to filter" );
66 
67  m_fiberOutput = WModuleOutputData < WDataSetFibers >::createAndAdd( shared_from_this(), "out", "The filtered dataset" );
69  "A cluster dataset splitting the input data into two clusters: the selected fibers and the rest." );
70 
71  // call WModule's initialization
73 }
74 
76 {
77  m_propCondition = std::shared_ptr< WCondition >( new WCondition() );
78  m_aTrigger = m_properties->addProperty( "Update", "Trigger an update of your result data.", WPVBaseTypes::PV_TRIGGER_READY,
80 
82 }
83 
85 {
86  m_moduleState.setResetable( true, true );
87  m_moduleState.add( m_input->getDataChangedCondition() );
89 
90  // Signal ready state. Now your module can be connected by the container, which owns the module.
91  ready();
92  waitRestored();
93 
94  // main loop
95  while( !m_shutdownFlag() )
96  {
98 
99  // woke up since the module is requested to finish
100  if( m_shutdownFlag() )
101  {
102  break;
103  }
104 
105  // Remember the above criteria. We now need to check if the data is valid. After a connect-update, it might be NULL.
106  std::shared_ptr< WDataSetFibers > dataSet = m_input->getData();
107  bool dataValid = ( dataSet != NULL );
108  bool dataChanged = dataSet != m_fibers;
109 
110  // do something with the data
111  if( dataChanged )
112  {
113  m_fibers = dataSet;
114 
115  if( dataValid )
116  {
117  // The data is valid and we received an update. The data is not NULL but may be the same as in previous loops.
118  debugLog() << "Received new data. Creating new selector.";
120 
121  debugLog() << "Data changed. Recalculating output.";
122  updateOutput();
123  }
124  else
125  {
126  m_fiberSelector.reset();
127  }
128  }
129 
130  if( m_aTrigger->get( true ) == WPVBaseTypes::PV_TRIGGER_TRIGGERED )
131  {
132  updateOutput();
134  }
135  }
136 }
137 
139 {
140  // target memory
141  std::shared_ptr< std::vector< bool > > active = m_fiberSelector->getBitfield();
142  std::shared_ptr< std::vector< float > > vertices( new std::vector< float >() );
143  std::shared_ptr< std::vector< size_t > > lineStartIndexes( new std::vector< size_t >() );
144  std::shared_ptr< std::vector< size_t > > lineLengths( new std::vector< size_t >() );
145  std::shared_ptr< std::vector< size_t > > verticesReverse( new std::vector< size_t >() );
146 
147  std::shared_ptr< WProgress > progress1( new WProgress( "Filtering", active->size() ) );
148  m_progress->addSubProgress( progress1 );
149 
150  size_t countLines = 0;
151  for( size_t l = 0; l < active->size(); ++l )
152  {
153  if( ( *active )[l] )
154  {
155  size_t pc = m_fibers->getLineStartIndexes()->at( l ) * 3;
156 
157  lineStartIndexes->push_back( vertices->size() / 3 );
158  lineLengths->push_back( m_fibers->getLineLengths()->at( l ) );
159 
160  for( size_t j = 0; j < m_fibers->getLineLengths()->at( l ); ++j )
161  {
162  vertices->push_back( m_fibers->getVertices()->at( pc ) );
163  ++pc;
164  vertices->push_back( m_fibers->getVertices()->at( pc ) );
165  ++pc;
166  vertices->push_back( m_fibers->getVertices()->at( pc ) );
167  ++pc;
168  verticesReverse->push_back( countLines );
169  }
170  ++countLines;
171  }
172  }
173 
174  std::shared_ptr< WDataSetFibers> newOutput( new WDataSetFibers( vertices, lineStartIndexes, lineLengths, verticesReverse,
175  m_fibers->getBoundingBox() ) );
176  m_fiberOutput->updateData( newOutput );
177  progress1->finish();
178 
179  progress1 = std::shared_ptr< WProgress >( new WProgress( "Create Clustering", active->size() ) );
180  m_progress->addSubProgress( progress1 );
181 
182  std::shared_ptr< WDataSetFiberClustering > clustering( new WDataSetFiberClustering() );
183  std::shared_ptr< WFiberCluster > cl0( new WFiberCluster() );
184  std::shared_ptr< WFiberCluster > cl1( new WFiberCluster() );
185 
186  for( size_t l = 0; l < active->size(); ++l )
187  {
188  if( ( *active )[l] )
189  {
190  WFiberCluster cl( l );
191  cl1->merge( cl );
192  }
193  else
194  {
195  WFiberCluster cl( l );
196  cl0->merge( cl );
197  }
198  }
199 
200  if( cl0->size() > 0 )
201  clustering->setCluster( 0, cl0 );
202  if( cl1->size() > 0 )
203  clustering->setCluster( 1, cl1 );
204 
205  m_clusterOutput->updateData( clustering );
206 
207  progress1->finish();
208 }
209 
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.
Class to encapsulate boost::condition_variable_any.
Definition: WCondition.h:42
This is a dataset which represent a clustering of fibers.
Represents a simple set of WFibers.
Represents a cluster of indices of a WDataSetFiberVector.
Definition: WFiberCluster.h:45
Adaptor class between the roi manager and the fiber display.
std::shared_ptr< WFiberSelector > SPtr
Fiber selector pointer.
This module is intended to allow the user to filter a fiber dataset using the current ROI config.
std::shared_ptr< WModuleOutputData< WDataSetFiberClustering > > m_clusterOutput
The output connector for the resulting clustering.
WFiberSelector::SPtr m_fiberSelector
Selector for the current fiber data or NULL of none.
virtual const std::string getName() const
Gives back the name of this module.
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.
virtual const std::string getDescription() const
Gives back a description of this module.
virtual ~WMFiberFilterROI()
Destructor.
virtual void connectors()
Initialize the connectors this module is using.
void updateOutput()
Updates the output using the ROI configuration.
WMFiberFilterROI()
Default constructor.
std::shared_ptr< WModuleInputData< WDataSetFibers > > m_input
An input for the fiber data.
std::shared_ptr< WModuleOutputData< WDataSetFibers > > m_fiberOutput
The output connector for the filtered data.
WPropTrigger m_aTrigger
Trigger output update.
WDataSetFibers::SPtr m_fibers
Fiber data.
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...
virtual void moduleMain()
Entry point after loading the module.
static PtrType createAndAdd(std::shared_ptr< WModule > module, std::string name="", std::string description="")
Convenience method to create a new instance of this in data connector with proper type and add it to ...
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
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
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
void waitRestored()
This method waits for the module to be restored completely.
Definition: WModule.cpp:625
Class managing progress inside of modules.
Definition: WProgress.h:42
WBoolFlag m_shutdownFlag
Condition getting fired whenever the thread should quit.
@ PV_TRIGGER_TRIGGERED
Trigger property: got triggered.
@ PV_TRIGGER_READY
Trigger property: is ready to be triggered (again)