OpenWalnut  1.5.0dev
WMVectorAlign.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 "WMVectorAlign.h"
30 #include "WMVectorAlign.xpm"
31 #include "core/dataHandler/WDataSetVector.h"
32 #include "core/kernel/WKernel.h"
33 
34 // This line is needed by the module loader to actually find your module. Do not remove. Do NOT add a ";" here.
35 W_LOADABLE_MODULE( WMVectorAlign )
36 
37 WMVectorAlign::WMVectorAlign():
38  WModule()
39 {
40 }
41 
42 WMVectorAlign::~WMVectorAlign()
43 {
44 }
45 
46 std::shared_ptr< WModule > WMVectorAlign::factory() const
47 {
48  return std::shared_ptr< WModule >( new WMVectorAlign() );
49 }
50 
51 const char** WMVectorAlign::getXPMIcon() const
52 {
53  return WMVectorAlign_xpm; // Please put a real icon here.
54 }
55 
56 const std::string WMVectorAlign::getName() const
57 {
58  return "Vector Align";
59 }
60 
61 const std::string WMVectorAlign::getDescription() const
62 {
63  return "Aligns vectors of each grid cell, so they are aligned in same halfspace.";
64 }
65 
67 {
68  m_vectorIC = WModuleInputData< WDataSetVector >::createAndAdd( shared_from_this(), "vectors", "Some vector dataset." );
69  m_vectorOC = WModuleOutputData< WDataSetVector >::createAndAdd( shared_from_this(), "alignedVectors",
70  "Dataset where vectors are aligned inside the halfspace" );
71 
73 }
74 
76 {
78 }
79 
81 {
82 }
83 
84 namespace
85 {
86  std::shared_ptr< WDataSetVector > alignVectors( std::shared_ptr< WDataSetVector > vectors )
87  {
88  size_t numVecs = vectors->getValueSet()->size();
89  std::vector< WVector3d > newData;
90  newData.reserve( numVecs );
91  for( size_t i = 0; i < numVecs; ++i )
92  {
93  newData.push_back( vectors->getVectorAt( i ) );
94  }
95 
96  std::shared_ptr< WGridRegular3D > grid = std::dynamic_pointer_cast< WGridRegular3D >( vectors->getGrid() );
97  if( !grid )
98  {
99  return vectors;
100  }
101 
102  std::shared_ptr< std::vector< double > > data( new std::vector< double > );
103 
104  for( size_t i = 0; i < numVecs; ++i )
105  {
106  std::vector< size_t > neighbours = grid->getNeighbours( i );
107  for( size_t j = 0; j < neighbours.size(); ++j )
108  {
109  double sign = 1.0;
110  if( neighbours[j] > i ) // just to speed up and ensure that once vectors written to data will never be changed afterwards
111  {
112  if( dot( newData[i], newData[ neighbours[j] ] ) < 0.0 )
113  {
114  sign = -1.0;
115  }
116  newData[ neighbours[ j ] ] *= sign;
117  }
118  }
119  // newData[i] will never change from now on anymore
120  data->push_back( newData[i][0] );
121  data->push_back( newData[i][1] );
122  data->push_back( newData[i][2] );
123  }
124  std::shared_ptr< WValueSet< double > > values( new WValueSet< double >( 1, 3, data, W_DT_DOUBLE ) );
125  return std::shared_ptr< WDataSetVector >( new WDataSetVector( values, grid ) );
126  }
127 }
128 
130 {
131  // get notified about data changes
132  m_moduleState.setResetable( true, true );
133  m_moduleState.add( m_vectorIC->getDataChangedCondition() );
134 
135  ready();
136 
137  // main loop
138  while( !m_shutdownFlag() )
139  {
140  infoLog() << "Waiting ...";
142 
143  // woke up since the module is requested to finish?
144  if( m_shutdownFlag() )
145  {
146  break;
147  }
148 
149  // save data behind connectors since it might change during processing
150  std::shared_ptr< WDataSetVector > vectors = m_vectorIC->getData();
151 
152  if( !vectors ) // if data valid
153  {
154  continue;
155  }
156 
157  m_vectorOC->updateData( alignVectors( vectors ) );
158  }
159 }
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.
This data set type contains vectors as values.
This module aligns vectors so that they are inside of the same halfspace by just mirroring their dire...
Definition: WMVectorAlign.h:43
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< WModuleOutputData< WDataSetVector > > m_vectorOC
Output connector for the vector dataset.
virtual const std::string getDescription() const
Gives back a description of this module.
virtual const std::string getName() const
Gives back the name of this module.
std::shared_ptr< WModuleInputData< WDataSetVector > > m_vectorIC
Input connector for the vector dataset.
virtual void moduleMain()
Entry point after loading the module.
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 requirements()
Initialize requirements for this 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 ...
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
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
wlog::WStreamedLogger infoLog() const
Logger instance for comfortable info logging.
Definition: WModule.cpp:565
virtual void connectors()
Initialize connectors in this function.
Definition: WModule.cpp:208
WBoolFlag m_shutdownFlag
Condition getting fired whenever the thread should quit.
Base Class for all value set types.
Definition: WValueSet.h:47