OpenWalnut  1.5.0dev
WMFibersToPoints.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 <string>
28 
29 #include "WMFibersToPoints.h"
30 #include "core/common/WPropertyHelper.h"
31 #include "core/common/math/WMath.h"
32 #include "core/dataHandler/WDataHandler.h"
33 #include "core/kernel/WKernel.h"
34 
35 // 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.
36 W_LOADABLE_MODULE( WMFibersToPoints )
37 
39  WModule()
40 {
41 }
42 
44 {
45  // Cleanup!
46 }
47 
48 std::shared_ptr< WModule > WMFibersToPoints::factory() const
49 {
50  return std::shared_ptr< WModule >( new WMFibersToPoints() );
51 }
52 
53 const char** WMFibersToPoints::getXPMIcon() const
54 {
55  return NULL;
56 }
57 
58 const std::string WMFibersToPoints::getName() const
59 {
60  return "Fibers To Points";
61 }
62 
63 const std::string WMFibersToPoints::getDescription() const
64 {
65  return "This module converts fiber data to points by using the fiber vertex points and colors.";
66 }
67 
69 {
70  // The input fiber dataset
71  m_fiberInput = std::shared_ptr< WModuleInputData < WDataSetFibers > >(
72  new WModuleInputData< WDataSetFibers >( shared_from_this(), "fibers", "The fiber dataset" )
73  );
74 
75  // As properties, every connector needs to be added to the list of connectors.
77 
78  // the points
79  m_pointsOutput = std::shared_ptr< WModuleOutputData < WDataSetPoints > >(
80  new WModuleOutputData< WDataSetPoints >( shared_from_this(), "out", "The point data." )
81  );
82 
83  // As above: make it known.
85 
86  // call WModule's initialization
88 }
89 
91 {
92  m_propCondition = std::shared_ptr< WCondition >( new WCondition() );
93 
94  m_filterGroup = m_properties->addPropertyGroup( "Filtering", "Filtering based on fiber parameters." );
95  m_paramHint = m_filterGroup->addProperty( "Hint", "If you see this, your data does not contain fiber parameters.",
96  std::string( "Your data cannot be filtered due to missing parameters." ) );
97  m_paramHint->setPurpose( PV_PURPOSE_INFORMATION );
98 
99  m_color = m_filterGroup->addProperty( "Color", "The color of the resulting points", defaultColor::WHITE, m_propCondition );
100  m_parametersFilterValue = m_filterGroup->addProperty( "Parameter", "Value", 0.0, m_propCondition );
101  m_parametersFilterWidth = m_filterGroup->addProperty( "Filter Width", "Width", 0.1, m_propCondition );
102 
103  // call WModule's initialization
105 }
106 
107 
109 {
110  // get notified about data changes
111  m_moduleState.setResetable( true, true );
112  m_moduleState.add( m_fiberInput->getDataChangedCondition() );
113  // Remember the condition provided to some properties in properties()? The condition can now be used with this condition set.
115 
116  ready();
117 
118  // main loop
119  while( !m_shutdownFlag() )
120  {
121  debugLog() << "Waiting ...";
123 
124  // woke up since the module is requested to finish?
125  if( m_shutdownFlag() )
126  {
127  break;
128  }
129 
130  // To query whether an input was updated, simply ask the input:
131  bool dataUpdated = m_fiberInput->handledUpdate();
132  std::shared_ptr< WDataSetFibers > dataSet = m_fiberInput->getData();
133  bool dataValid = ( dataSet != NULL );
134  bool propsChanged = m_parametersFilterValue->changed() ||
135  m_parametersFilterWidth->changed() ||
136  m_color->changed();
137 
138  // reset everything if input was disconnected/invalid
139  if( !dataValid )
140  {
141  debugLog() << "Resetting output.";
142  m_pointsOutput->reset();
143  continue;
144  }
145 
146  if( dataValid && dataUpdated )
147  {
148  if( dataSet->getVertexParameters() )
149  {
150  m_parameterMin = dataSet->getVertexParameters()->operator[]( 0 );
151  m_parameterMax = dataSet->getVertexParameters()->operator[]( 0 );
152  for( WDataSetFibers::VertexParemeterArray::element_type::const_iterator it = dataSet->getVertexParameters()->begin();
153  it != dataSet->getVertexParameters()->end(); ++it )
154  {
155  m_parameterMax = std::max( *it, m_parameterMax );
156  m_parameterMin = std::min( *it, m_parameterMin );
157  }
158  }
159  else
160  {
161  m_parameterMin = 0.0;
162  m_parameterMax = 1.0;
163  }
164 
165  m_parametersFilterValue->setRecommendedValue( m_parameterMin );
168  m_parametersFilterWidth->setRecommendedValue( m_parameterMax - m_parameterMin );
169  m_parametersFilterWidth->setMin( 0.0 );
171  }
172 
173  if( dataValid && !dataUpdated && !propsChanged )
174  {
175  continue;
176  }
177 
178  // progress indication
179  WProgress::SPtr progress = WProgress::SPtr( new WProgress( "Creating Points from Fibers." ) );
180  m_progress->addSubProgress( progress );
181 
182  debugLog() << "Creating point data. Num Points = " << dataSet->getVertices()->size() / 3 << ".";
183  WDataSetFibers::VertexArray fibVerts = dataSet->getVertices();
184  WDataSetFibers::ColorArray fibColors = dataSet->getColorScheme()->getColor();
185  WDataSetFibers::VertexParemeterArray fibParams = dataSet->getVertexParameters();
186 
187  WDataSetFibers::VertexArray filteredVerts( new WDataSetFibers::VertexArray::element_type() );
188  WDataSetFibers::ColorArray filteredColors( new WDataSetFibers::ColorArray::element_type() );
189  WBoundingBox filteredBB;
190 
191  if( fibParams )
192  {
193  m_paramHint->setHidden( true );
194 
195  WIntervalDouble interval( std::max( m_parametersFilterValue->get( true ) - m_parametersFilterWidth->get( true ), m_parameterMin ),
196  std::min( m_parametersFilterValue->get( true ) + m_parametersFilterWidth->get( true ), m_parameterMax ) );
197 
198  // filter
199  for( size_t idx = 0; idx < fibVerts->size() / 3; ++idx )
200  {
201  if( isInClosed( interval, fibParams->operator[]( idx ) ) )
202  {
203  filteredBB.expandBy( fibVerts->operator[]( idx * 3 + 0 ),
204  fibVerts->operator[]( idx * 3 + 1 ),
205  fibVerts->operator[]( idx * 3 + 2 ) );
206 
207  filteredVerts->push_back( fibVerts->operator[]( idx * 3 + 0 ) );
208  filteredVerts->push_back( fibVerts->operator[]( idx * 3 + 1 ) );
209  filteredVerts->push_back( fibVerts->operator[]( idx * 3 + 2 ) );
210 
211  filteredColors->push_back( m_color->get().x() );
212  filteredColors->push_back( m_color->get().y() );
213  filteredColors->push_back( m_color->get().z() );
214  filteredColors->push_back( m_color->get().w() );
215  }
216  }
217  }
218  else
219  {
220  m_paramHint->setHidden( false );
221 
222  filteredVerts = fibVerts;
223  filteredColors = fibColors;
224  filteredBB = dataSet->getBoundingBox();
225  }
226 
227 
228  debugLog() << "Done filtering. Result are " << filteredVerts->size() / 3 << " points.";
229  WDataSetPoints::SPtr result( new WDataSetPoints( filteredVerts, filteredColors, filteredBB ) );
230  m_pointsOutput->updateData( result );
231 
232  progress->finish();
233  m_progress->removeSubProgress( progress );
234  }
235 }
236 
void expandBy(const WBoundingBoxImpl< VT > &bb)
Expands this bounding box to include the given bounding box.
Definition: WBoundingBox.h:240
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
std::shared_ptr< std::vector< float > > ColorArray
Colors for each vertex in VertexArray.
std::shared_ptr< std::vector< double > > VertexParemeterArray
Parameter storage for each vertex.
std::shared_ptr< std::vector< float > > VertexArray
List of vertex coordinates in term of components of vertices.
Dataset to store a bunch of points without order or topology.
std::shared_ptr< WDataSetPoints > SPtr
Pointer to dataset.
This modules takes a fiber dataset and extracts its vertices to build a point dataset.
double m_parameterMax
fiber parameter max
virtual ~WMFibersToPoints()
Destructor.
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...
WPropDouble m_parametersFilterValue
Filter fib point according to its parameter value and this value.
WPropString m_paramHint
A string which informs the user whether filtering is possible.
std::shared_ptr< WModuleOutputData< WDataSetPoints > > m_pointsOutput
The output connector used to provide the calculated point data to other modules.
double m_parameterMin
fiber parameter min
virtual void moduleMain()
Entry point after loading the module.
virtual const std::string getDescription() const
Gives back a description of this module.
WPropDouble m_parametersFilterWidth
Filter width.
virtual const std::string getName() const
Gives back the name of this module.
virtual void properties()
Initialize the properties for this module.
WMFibersToPoints()
Default constructor.
WPropGroup m_filterGroup
Group contains the filtering options.
virtual const char ** getXPMIcon() const
Get the icon for this module in XPM format.
virtual void connectors()
Initialize the connectors this module is using.
std::shared_ptr< WCondition > m_propCondition
A condition used to notify about changes in several properties.
std::shared_ptr< WModuleInputData< WDataSetFibers > > m_fiberInput
The fiber dataset which is going to be used.
WPropColor m_color
The color to use for the resulting points.
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
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
std::shared_ptr< WProgress > SPtr
Shared pointer on a WProgress.
Definition: WProgress.h:48
WBoolFlag m_shutdownFlag
Condition getting fired whenever the thread should quit.