OpenWalnut  1.5.0dev
WMFiberTransform.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 
28 #include "WMFiberTransform.h"
29 #include "WMFiberTransform.xpm"
30 #include "core/common/WPropertyHelper.h"
31 #include "core/dataHandler/io/WWriterFiberVTK.h"
32 
33 // This line is needed by the module loader to actually find your module.
34 W_LOADABLE_MODULE( WMFiberTransform )
35 
37  : WModule(),
38  m_recompute( new WCondition() )
39 {
40 }
41 
43 {
44  // cleanup
46 }
47 
48 std::shared_ptr< WModule > WMFiberTransform::factory() const
49 {
50  // See "src/modules/template/" for an extensively documented example.
51  return std::shared_ptr< WModule >( new WMFiberTransform() );
52 }
53 
54 const char** WMFiberTransform::getXPMIcon() const
55 {
56  return fiberTransform_xpm;
57 }
58 
60 {
61  // Put the code for your connectors here. See "src/modules/template/" for an extensively documented example.
62  typedef WModuleInputData< WDataSetFibers > FiberInputData; // just an alias
63  m_fiberInput = std::shared_ptr< FiberInputData >( new FiberInputData( shared_from_this(), "fiberInput", "A loaded fiber dataset." ) );
64 
65  typedef WModuleOutputData< WDataSetFibers > FiberOutputData; // just an alias
66  m_output = std::shared_ptr< FiberOutputData >( new FiberOutputData( shared_from_this(), "fiberOutput", "The transformed fibers." ) );
67 
70 
71  // call WModules initialization
73 }
74 
76 {
77  // Put the code for your properties here. See "src/modules/template/" for an extensively documented example.
78  m_save = m_properties->addProperty( "Save result", "If true the transformed fibers are saved to a file", false );
79  m_savePath = m_properties->addProperty( "Save path", "Where to save the result", boost::filesystem::path( "/no/such/file" ) );
80  m_run = m_properties->addProperty( "Run", "Do the transformation", WPVBaseTypes::PV_TRIGGER_READY, m_recompute );
81  m_run->get( true ); // reset so no initial run occurs
83 
84  m_translationProp = m_properties->addProperty( "Translation",
85  "Translation part of the transformation. You need to press enter to make the values effective.",
86  WPosition( 0.0, 0.0, 0.0 ) );
87  m_matrix0Prop = m_properties->addProperty( "M Row 0",
88  "Row 0 of matrix part of the transformation. You need to press enter to make the values effective.",
89  WPosition( 1.0, 0.0, 0.0 ) );
90  m_matrix1Prop = m_properties->addProperty( "M Row 1",
91  "Row 1 of matrix part of the transformation. You need to press enter to make the values effective.",
92  WPosition( 0.0, 1.0, 0.0 ) );
93  m_matrix2Prop = m_properties->addProperty( "M Row 2",
94  "Row 2 of matrix part of the transformation. You need to press enter to make the values effective.",
95  WPosition( 0.0, 0.0, 1.0 ) );
96  m_matrix3Prop = m_properties->addProperty( "M Row 3",
97  "Row 3 of matrix part of the transformation. You need to press enter to make the values effective.",
98  WPosition( 0.0, 0.0, 0.0 ) );
99 
101 }
102 
104 {
105  // When conditions are firing while wait() is not reached: wait terminates
106  // and behaves as if the appropriate conditions have had fired. But it is
107  // not detectable how many times a condition has fired.
109  m_moduleState.add( m_fiberInput->getDataChangedCondition() );
111 
112 
113 
114  ready();
115 
116  while( !m_shutdownFlag() ) // loop until the module container requests the module to quit
117  {
118  if( !m_fiberInput->getData().get() ) // ok, the output has not yet sent data
119  {
121  continue;
122  }
123  if( m_rawDataset != m_fiberInput->getData() ) // in case data has changed
124  {
125  m_rawDataset = m_fiberInput->getData();
126  WAssert( m_rawDataset, "Couldn't load dataset" );
127  }
128 
129  WAssert( m_savePath, "No save path property" );
130  if( m_savePath->get().string() == "/no/such/file" )
131  {
132  m_savePath->set( saveFileName( m_rawDataset->getFilename() ) );
133  }
134 
135  if( m_run->get( true ) == WPVBaseTypes::PV_TRIGGER_TRIGGERED )
136  {
137  update();
138  m_run->set( WPVBaseTypes::PV_TRIGGER_READY, false );
139  }
140 
142  }
143 }
144 
146 {
147  // get parameter from GUI (properties)
148  bool save = m_save->get();
149  boost::filesystem::path savePath = m_savePath->get();
150 
151  // set the transformation matrix
152  WMatrix< double > transformationMatrix( 4, 4 ); //!< matrix which is multiplied with each point to linear transform it.
153  transformationMatrix( 0, 0 ) = m_matrix0Prop->get()[0];
154  transformationMatrix( 0, 1 ) = m_matrix0Prop->get()[1];
155  transformationMatrix( 0, 2 ) = m_matrix0Prop->get()[2];
156  transformationMatrix( 0, 3 ) = m_translationProp->get()[0];
157 
158  transformationMatrix( 1, 0 ) = m_matrix1Prop->get()[0];
159  transformationMatrix( 1, 1 ) = m_matrix1Prop->get()[1];
160  transformationMatrix( 1, 2 ) = m_matrix1Prop->get()[2];
161  transformationMatrix( 1, 3 ) = m_translationProp->get()[1];
162 
163  transformationMatrix( 2, 0 ) = m_matrix2Prop->get()[0];
164  transformationMatrix( 2, 1 ) = m_matrix2Prop->get()[1];
165  transformationMatrix( 2, 2 ) = m_matrix2Prop->get()[2];
166  transformationMatrix( 2, 3 ) = m_translationProp->get()[2];
167 
168  transformationMatrix( 3, 0 ) = m_matrix3Prop->get()[0];
169  transformationMatrix( 3, 1 ) = m_matrix3Prop->get()[1];
170  transformationMatrix( 3, 2 ) = m_matrix3Prop->get()[2];
171  transformationMatrix( 3, 3 ) = 1.0;
172 
173  std::shared_ptr< WProgress > progress( new WProgress( "Transforming", 4 + save ) );
174  m_progress->addSubProgress( progress );
175 
176  infoLog() << "Start: WDataSetFibers => WDataSetFiberVector";
177  std::shared_ptr< WDataSetFiberVector > dataset( new WDataSetFiberVector( m_rawDataset ) );
178  infoLog() << "Stop: WDataSetFibers => WDataSetFiberVector";
179  ++*progress;
180 
181  //transform
182  WValue< double > vec( 4 );
183  WValue< double > vec_transformed( 4 );
184  for( std::size_t fiberID = 0; fiberID < dataset->size(); ++fiberID )
185  {
186  WFiber& fiber = (*dataset)[fiberID];
187  for( std::size_t positionID = 0; positionID < fiber.size(); ++positionID )
188  {
189  vec[0] = fiber[positionID][0];
190  vec[1] = fiber[positionID][1];
191  vec[2] = fiber[positionID][2];
192  vec[3] = 1.0;
193  vec_transformed = transformationMatrix * vec;
194  vec_transformed = ( 1.0 / vec_transformed[3] ) * vec_transformed;
195  fiber[positionID] = WPosition( vec_transformed[0], vec_transformed[1], vec_transformed[2] );
196  }
197  }
198  ++*progress;
199 
200  infoLog() << "Start: WDataSetFibers <= WDataSetFiberVector";
201  m_output->updateData( dataset->toWDataSetFibers() );
202  infoLog() << "Stop: WDataSetFibers <= WDataSetFiberVector";
203  ++*progress;
204 
205  if( save )
206  {
207  WWriterFiberVTK w( savePath, true );
208  w.writeFibs( dataset );
209  ++*progress;
210  }
211 
212  progress->finish();
213 }
214 
215 boost::filesystem::path WMFiberTransform::saveFileName( std::string dataFileName ) const
216 {
217  boost::filesystem::path fibFileName( dataFileName );
218  return fibFileName.replace_extension( ".transformed.fib" );
219 }
220 inline const std::string WMFiberTransform::getName() const
221 {
222  // Specify your module name here. This name must be UNIQUE!
223  return std::string( "Fiber Transform" );
224 }
225 
226 inline const std::string WMFiberTransform::getDescription() const
227 {
228  // Specify your module description here. Be detailed. This text is read by the user.
229  // See "src/modules/template/" for an extensively documented example.
230  return std::string( "Transforms a fiber dataset" );
231 }
232 
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
Represents a simple set of WFibers.
Represents a neural pathway.
Definition: WFiber.h:40
Transforms a fiber dataset.
virtual ~WMFiberTransform()
Destructor.
std::shared_ptr< WDataSetFibers > m_rawDataset
Pointer to the fiber data set in WDataSetFibers format.
WPropTrigger m_run
Indicates if the algorithm should start.
WPropFilename m_savePath
Path where transformed fibers should be stored.
virtual const std::string getName() const
Gives back the name of this module.
virtual void connectors()
Initialize the connectors this module is using.
virtual const std::string getDescription() const
Gives back a description of this module.
WPropPosition m_matrix0Prop
Row 0 of matrix part of the transformation.
virtual void properties()
Initialize the properties for this module.
WMFiberTransform()
Constructor.
WPropPosition m_translationProp
Translation part of the transformation.
WPropPosition m_matrix2Prop
Row 2 of matrix part of the transformation.
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...
std::shared_ptr< WModuleInputData< WDataSetFibers > > m_fiberInput
Input connector for a fiber dataset.
virtual void moduleMain()
Entry point after loading the module.
void update()
ReTransforms the scene.
std::shared_ptr< WModuleOutputData< WDataSetFibers > > m_output
Output connector for the culled fibers.
WPropPosition m_matrix1Prop
Row 1 of matrix part of the transformation.
WPropBool m_save
If true, transformed fibers are saved to a file.
std::shared_ptr< WCondition > m_recompute
A condition which indicates complete recomputation.
virtual const char ** getXPMIcon() const
Get the icon for this module in XPM format.
boost::filesystem::path saveFileName(std::string dataFileName) const
Generates the file name for saving the transformed fibers.
WPropPosition m_matrix3Prop
Row 3 of matrix part of the transformation.
size_type size() const
Wrapper around std::vector member function.
Definition: WMixinVector.h:267
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
void removeConnectors()
Removes all connectors properly.
Definition: WModule.cpp:194
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
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
This only is a 3d double vector.
Class managing progress inside of modules.
Definition: WProgress.h:42
WBoolFlag m_shutdownFlag
Condition getting fired whenever the thread should quit.
Base class for all higher level values like tensors, vectors, matrices and so on.
Definition: WValue.h:41
Writes a FiberVTK file.
void writeFibs(std::shared_ptr< const WDataSetFiberVector > fiberDS) const
Writes a WDataSetFiberVector down to the previousely given file.
@ PV_TRIGGER_TRIGGERED
Trigger property: got triggered.
@ PV_TRIGGER_READY
Trigger property: is ready to be triggered (again)
void addTo(WPropSelection prop)
Add the PC_NOTEMPTY constraint to the property.