OpenWalnut  1.5.0dev
WMMergePoints.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 "WMMergePoints.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( WMMergePoints )
37 
39  WModule()
40 {
41 }
42 
44 {
45  // Cleanup!
46 }
47 
48 std::shared_ptr< WModule > WMMergePoints::factory() const
49 {
50  return std::shared_ptr< WModule >( new WMMergePoints() );
51 }
52 
53 const char** WMMergePoints::getXPMIcon() const
54 {
55  return NULL;
56 }
57 
58 const std::string WMMergePoints::getName() const
59 {
60  return "Merge Points";
61 }
62 
63 const std::string WMMergePoints::getDescription() const
64 {
65  return "This module merges several given point datasets into one new dataset";
66 }
67 
69 {
70  m_pointsInput1 = WModuleInputData< WDataSetPoints >::createAndAdd( shared_from_this(), "in1", "The dataset" );
71  m_pointsInput2 = WModuleInputData< WDataSetPoints >::createAndAdd( shared_from_this(), "in2", "The dataset" );
72  m_pointsOutput = WModuleOutputData< WDataSetPoints >::createAndAdd( shared_from_this(), "out", "The merged dataset" );
73 
74  // call WModule's initialization
76 }
77 
79 {
80  m_propCondition = std::shared_ptr< WCondition >( new WCondition() );
81 
82  // call WModule's initialization
84 }
85 
86 /**
87  * Copy colors from a source field to a target field as RGBA.
88  *
89  * \param src source field
90  * \param srcType source color type
91  * \param dest destination field
92  */
94 {
95  for( size_t i = 0; i < src->size() / srcType; ++i )
96  {
97  switch( srcType )
98  {
99  case WDataSetPoints::GRAY:
100  dest->push_back( src->operator[]( i ) );
101  dest->push_back( src->operator[]( i ) );
102  dest->push_back( src->operator[]( i ) );
103  dest->push_back( 1.0 );
104  break;
105  case WDataSetPoints::RGB:
106  dest->push_back( src->operator[]( srcType * i + 0 ) );
107  dest->push_back( src->operator[]( srcType * i + 1 ) );
108  dest->push_back( src->operator[]( srcType * i + 2 ) );
109  dest->push_back( 1.0 );
110  break;
111  case WDataSetPoints::RGBA:
112  dest->push_back( src->operator[]( srcType * i + 0 ) );
113  dest->push_back( src->operator[]( srcType * i + 1 ) );
114  dest->push_back( src->operator[]( srcType * i + 2 ) );
115  dest->push_back( src->operator[]( srcType * i + 3 ) );
116  break;
117  }
118  }
119 }
120 
122 {
123  // get notified about data changes
124  m_moduleState.setResetable( true, true );
125  m_moduleState.add( m_pointsInput1->getDataChangedCondition() );
126  m_moduleState.add( m_pointsInput2->getDataChangedCondition() );
127  // Remember the condition provided to some properties in properties()? The condition can now be used with this condition set.
129 
130  ready();
131 
132  // main loop
133  while( !m_shutdownFlag() )
134  {
135  debugLog() << "Waiting ...";
137 
138  // woke up since the module is requested to finish?
139  if( m_shutdownFlag() )
140  {
141  break;
142  }
143 
144  // To query whether an input was updated, simply ask the input:
145  bool dataUpdated = m_pointsInput1->handledUpdate() || m_pointsInput2->handledUpdate();
146  std::shared_ptr< WDataSetPoints > dataSet1 = m_pointsInput1->getData();
147  std::shared_ptr< WDataSetPoints > dataSet2 = m_pointsInput2->getData();
148  bool dataValid = ( dataSet1 && dataSet2 );
149 
150  // reset everything if input was disconnected/invalid
151  if( !dataValid )
152  {
153  debugLog() << "Resetting output.";
154  m_pointsOutput->reset();
155  continue;
156  }
157 
158  if( dataValid && dataUpdated )
159  {
160  WProgress::SPtr progress = WProgress::SPtr( new WProgress( "Creating Points from Fibers." ) );
161  m_progress->addSubProgress( progress );
162 
163  debugLog() << "Merging " << dataSet1->size() << " with " << dataSet2->size() << " points.";
164 
165  WDataSetPoints::VertexArray verts( new WDataSetPoints::VertexArray::element_type( *dataSet1->getVertices() ) );
167 
168  verts->resize( verts->size() + dataSet2->getVertices()->size() );
169  std::copy( dataSet2->getVertices()->begin(), dataSet2->getVertices()->end(), verts->begin() + dataSet1->getVertices()->size() );
170 
171  // we need to handle the case where both datasets have different color types
172  if( dataSet1->getColorType() != dataSet2->getColorType() )
173  {
174  colors = WDataSetPoints::ColorArray( new WDataSetPoints::ColorArray::element_type() );
175 
176  debugLog() << "Color types are different. Handling them properly.";
177 
178  // create mem and use the "larger" color type
179  colors->reserve( dataSet1->size() * dataSet1->size() * 4 ); // NOTE: we always create RGBA results
180 
181  // copy values
182  copyColors( dataSet1->getColors(), dataSet1->getColorType(), colors );
183  copyColors( dataSet2->getColors(), dataSet2->getColorType(), colors );
184  }
185  else
186  {
187  debugLog() << "Color types are equal.";
188  colors = WDataSetPoints::ColorArray( new WDataSetPoints::ColorArray::element_type( *dataSet1->getColors() ) );
189 
190  // merging colors of the same type is as easy as for the vertex data
191  colors->resize( colors->size() + dataSet2->getColors()->size() );
192  std::copy( dataSet2->getColors()->begin(), dataSet2->getColors()->end(), colors->begin() + dataSet1->getColors()->size() );
193  }
194 
195  // directly merge bounding boxes
196  WBoundingBox bb = dataSet1->getBoundingBox();
197  bb.expandBy( dataSet2->getBoundingBox() );
198 
199  debugLog() << "Done merging. Result are " << verts->size() << " points.";
200  // done
201  WDataSetPoints::SPtr result( new WDataSetPoints( verts, colors, bb ) );
202  m_pointsOutput->updateData( result );
203  progress->finish();
204  m_progress->removeSubProgress( progress );
205  }
206  }
207 }
208 
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
Dataset to store a bunch of points without order or topology.
std::shared_ptr< WDataSetPoints > SPtr
Pointer to dataset.
ColorType
The type of colors we have for each point.
std::shared_ptr< std::vector< float > > ColorArray
Colors for each vertex in VertexArray.
std::shared_ptr< std::vector< float > > VertexArray
List of vertex coordinates in term of components of vertices.
This modules takes some point data and merges them into one new point dataset.
Definition: WMMergePoints.h:42
std::shared_ptr< WModuleInputData< WDataSetPoints > > m_pointsInput1
The input data.
virtual const std::string getName() const
Gives back the name of this module.
virtual ~WMMergePoints()
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...
virtual void properties()
Initialize the properties for this module.
virtual void moduleMain()
Entry point after loading the module.
virtual void connectors()
Initialize the connectors this module is using.
std::shared_ptr< WModuleOutputData< WDataSetPoints > > m_pointsOutput
The output connector used to provide the calculated point data to other modules.
std::shared_ptr< WCondition > m_propCondition
A condition used to notify about changes in several properties.
virtual const char ** getXPMIcon() const
Get the icon for this module in XPM format.
virtual const std::string getDescription() const
Gives back a description of this module.
std::shared_ptr< WModuleInputData< WDataSetPoints > > m_pointsInput2
The input data.
WMMergePoints()
Default constructor.
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 ...
static PtrType createAndAdd(std::shared_ptr< WModule > module, std::string name="", std::string description="")
Convenience method to create a new instance of this out data connector with proper type and add it to...
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 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.