OpenWalnut  1.5.0dev
WMFiberTranslator.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 <fstream>
26 #include <memory>
27 #include <string>
28 #include <vector>
29 
30 #include "WMFiberTranslator.h"
31 #include "WMFiberTranslator.xpm"
32 #include "core/dataHandler/WFiberAccumulator.h"
33 #include "core/kernel/WKernel.h"
34 
35 // This line is needed by the module loader to actually find your module. Do not remove. Do NOT add a ";" here.
36 W_LOADABLE_MODULE( WMFiberTranslator )
37 
39  WModule()
40 {
41 }
42 
44 {
45  // Cleanup!
46 }
47 
48 std::shared_ptr< WModule > WMFiberTranslator::factory() const
49 {
50  return std::shared_ptr< WModule >( new WMFiberTranslator() );
51 }
52 
53 const char** WMFiberTranslator::getXPMIcon() const
54 {
55  return WMFiberTranslator_xpm;
56 }
57 const std::string WMFiberTranslator::getName() const
58 {
59  return "Fiber translator";
60 }
61 
62 const std::string WMFiberTranslator::getDescription() const
63 {
64  return "Creates a fiber dataset from various data sources.";
65 }
66 
68 {
69  m_output = std::shared_ptr< WModuleOutputData< WDataSetFibers > >(
70  new WModuleOutputData< WDataSetFibers >( shared_from_this(), "outFibers", "" ) );
71 
73 
75 }
76 
78 {
79  m_propCondition = std::shared_ptr< WCondition >( new WCondition() );
80 
81  m_propInputDirectory = m_properties->addProperty( "Input data directory", "A directory containing .txt files, each file"
82  " contains the coordinates of one fiber (e.g. produced by mrtrix).",
83  boost::filesystem::path( "/" ), m_propCondition );
86 
87  m_propInputFile = m_properties->addProperty( "Input txt file", "A .txt file that contains the fiber coords (ExploreDTI format).",
88  boost::filesystem::path( "/" ), m_propCondition );
90 
92 }
93 
95 {
96 }
97 
99 {
100  m_moduleState.setResetable( true, true );
102 
103  ready();
104 
105  // remove changed flag
106  m_propInputDirectory->get( true );
107 
108  while( !m_shutdownFlag() )
109  {
111 
112  if( m_shutdownFlag() )
113  {
114  break;
115  }
116 
117  if( m_propInputDirectory->changed() )
118  {
119  std::shared_ptr< WDataSetFibers > fibers = mergeFibers( m_propInputDirectory->get( true ) );
120  // std::shared_ptr< WDataSetFibers > fibers = mergeFibersBinary( m_propInputDirectory->get( true ) );
121  if( fibers )
122  {
123  m_output->updateData( fibers );
124  }
125  }
126 
127  if( m_propInputFile->changed() )
128  {
129  std::shared_ptr< WDataSetFibers > fibers = loadExploreDTIFibers( m_propInputFile->get( true ) );
130  if( fibers )
131  {
132  m_output->updateData( fibers );
133  }
134  }
135  }
136 }
137 
138 std::shared_ptr< WDataSetFibers > WMFiberTranslator::mergeFibers( boost::filesystem::path dir )
139 {
140  debugLog() << "Merging fibers.";
141 
142  if( !boost::filesystem::exists( dir ) || !boost::filesystem::is_directory( dir ) )
143  {
144  errorLog() << "Directory " << dir.string() << " does not exist!";
145  return std::shared_ptr< WDataSetFibers >();
146  }
147 
148  WFiberAccumulator accu;
149 
150  boost::filesystem::directory_iterator it( dir );
151  boost::filesystem::directory_iterator end;
152  while( it != end )
153  {
154  std::vector< WVector3d > fiber;
155 
156  if( boost::filesystem::is_directory( it->path() ) || it->path().extension() != ".txt" )
157  {
158  debugLog() << "Ignoring file " << it->path();
159  continue;
160  }
161 
162  std::fstream f( it->path().string().c_str(), std::ios::in );
163 
164  WVector3d v;
165  while( f >> v[ 0 ] >> v[ 1 ] >> v[ 2 ] )
166  {
167  fiber.push_back( v );
168  }
169 
170  f.close();
171 
172  accu.add( fiber );
173 
174  ++it;
175  }
176 
177  debugLog() << "Finished.";
178 
179  return accu.buildDataSet();
180 }
181 
182 std::shared_ptr< WDataSetFibers > WMFiberTranslator::mergeFibersNrrd( boost::filesystem::path dir )
183 {
184  debugLog() << "Merging fibers (binary).";
185 
186  if( !boost::filesystem::exists( dir ) || !boost::filesystem::is_directory( dir ) )
187  {
188  errorLog() << "Directory " << dir.string() << " does not exist!";
189  return std::shared_ptr< WDataSetFibers >();
190  }
191 
192  WFiberAccumulator accu;
193 
194  boost::filesystem::directory_iterator it( dir );
195  boost::filesystem::directory_iterator end;
196  while( it != end )
197  {
198  std::vector< WVector3d > fiber;
199 
200  if( boost::filesystem::is_directory( it->path() ) )
201  {
202  debugLog() << "Ignoring file " << it->path();
203  continue;
204  }
205 
206  std::fstream f( it->path().string().c_str(), std::ios::in | std::ios::binary );
207 
208  WVector3d v;
209  while( f >> v[ 0 ] >> v[ 1 ] >> v[ 2 ] )
210  {
211  fiber.push_back( v );
212  }
213 
214  f.close();
215 
216  infoLog() << "Fiber with " << fiber.size() << " points.";
217 
218  accu.add( fiber );
219 
220  ++it;
221  }
222 
223  debugLog() << "Finished.";
224 
225  return accu.buildDataSet();
226 }
227 
228 std::shared_ptr< WDataSetFibers > WMFiberTranslator::loadExploreDTIFibers( boost::filesystem::path file )
229 {
230  debugLog() << "Loading fibers from ExploreDTI text format.";
231 
232  if( !boost::filesystem::exists( file ) || boost::filesystem::is_directory( file ) )
233  {
234  errorLog() << "File " << file.string() << " does not exist!";
235  return std::shared_ptr< WDataSetFibers >();
236  }
237 
238  // the file should end in "_coordinates.txt"
239  if( file.string().length() < 16 || file.string().find( "_coordinates.txt" ) != file.string().length() - 16 )
240  {
241  errorLog() << "File " << file << " does not end in _coordinates.txt. Ignoring.";
242  return std::shared_ptr< WDataSetFibers >();
243  }
244 
245  // get the name of the corresponding fiber length file
246  std::string lengthFile = file.string();
247  lengthFile.resize( lengthFile.length() - 16 );
248  lengthFile += "_lengths.txt";
249 
250  WFiberAccumulator accu;
251 
252  // open files
253  std::fstream coords( file.string().c_str(), std::ios::in );
254  std::fstream lengths( lengthFile.c_str(), std::ios::in );
255 
256  while( !lengths.eof() && !lengths.bad() )
257  {
258  double l;
259  lengths >> l;
260 
261  if( l < 2.0 )
262  {
263  errorLog() << "Lengths file contains an invalid length of " << l << ", aborting.";
264  return std::shared_ptr< WDataSetFibers >();
265  }
266 
267  std::vector< WVector3d > fiber;
268  for( int i = 0; i < static_cast< int >( l ); ++i )
269  {
270  WVector3d v;
271  coords >> v[ 0 ] >> v[ 1 ] >> v[ 2 ];
272  fiber.push_back( v );
273  }
274 
275  accu.add( fiber );
276  }
277 
278  lengths.close();
279  coords.close();
280 
281  debugLog() << "Finished.";
282 
283  return accu.buildDataSet();
284 }
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
A class that encapsulates the data needed to construct a WDataSetFibers.
void add(std::vector< WVector3d > const &in)
Add a fiber to the dataset.
std::shared_ptr< WDataSetFibers > buildDataSet()
Return the dataset that has been accumulated to this point and start a new dataset.
Creates a fiber dataset from various data sources.
std::shared_ptr< WDataSetFibers > mergeFibers(boost::filesystem::path dir)
Merge the fibers in the given directory.
virtual const std::string getName() const
Gives back the name of this module.
virtual const char ** getXPMIcon() const
Get the icon for this module in XPM format.
virtual void moduleMain()
Entry point after loading the module.
std::shared_ptr< WCondition > m_propCondition
A condition for property updates.
std::shared_ptr< WModuleOutputData< WDataSetFibers > > m_output
The output connector.
WPropFilename m_propInputFile
A property for choosing a file that contains the fiber coordinates.
virtual void properties()
Initialize the properties for this module.
WMFiberTranslator()
Constructor.
virtual const std::string getDescription() const
Gives back a description of this module.
WPropFilename m_propInputDirectory
A property for choosing the directory that contains the fiber text files.
virtual ~WMFiberTranslator()
Destructor.
virtual void connectors()
Initialize the connectors this module is using.
virtual void requirements()
Initialize requirements for this module.
std::shared_ptr< WDataSetFibers > loadExploreDTIFibers(boost::filesystem::path file)
Load fibers from an ExploreDTI .txt file.
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< WDataSetFibers > mergeFibersNrrd(boost::filesystem::path dir)
Merge the fibers from given directory.
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
wlog::WStreamedLogger errorLog() const
Logger instance for comfortable error logging.
Definition: WModule.cpp:570
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.
void addTo(WPropFilename prop)
Add the PC_PATHEXISTS constraint to the property.
void addTo(WPropFilename prop)
Add the PC_PATHEXISTS constraint to the property.