OpenWalnut  1.5.0dev
WMReadVIM.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 "WMReadVIM.h"
31 #include "core/common/WPathHelper.h"
32 #include "core/common/WStringUtils.h"
33 #include "core/kernel/WDataModuleInputFile.h"
34 #include "core/kernel/WDataModuleInputFilterFile.h"
35 #include "core/kernel/WKernel.h"
36 
37 // 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.
38 W_LOADABLE_MODULE( WMReadVIM )
39 
41  WDataModule(),
42  m_reload( false )
43 {
44  // Init
45 }
46 
48 {
49  // Cleanup!
50 }
51 
52 std::shared_ptr< WModule > WMReadVIM::factory() const
53 {
54  return std::shared_ptr< WModule >( new WMReadVIM() );
55 }
56 
57 const std::string WMReadVIM::getName() const
58 {
59  // Specify your module name here. This name must be UNIQUE!
60  return "Read VIM";
61 }
62 
63 const std::string WMReadVIM::getDescription() const
64 {
65  // Specify your module description here. Be detailed. This text is read by the user.
66  return "This module reads VIM files containing particle data.";
67 }
68 
70 {
71  m_output = WModuleOutputData < WDataSetPoints >::createAndAdd( shared_from_this(), "out", "The loaded dataset" );
72 
73  // call WModule's initialization
75 }
76 
78 {
79  m_moduleState.setResetable( true, true );
80 
81  // Signal ready state. Now your module can be connected by the container, which owns the module.
82  ready();
83  waitRestored();
84 
85  // main loop
86  while( !m_shutdownFlag() )
87  {
89 
90  // woke up since the module is requested to finish
91  if( m_shutdownFlag() )
92  {
93  break;
94  }
95 
96  if( m_reload )
97  {
98  load();
99  }
100  }
101 }
102 
103 std::vector< WDataModuleInputFilter::ConstSPtr > WMReadVIM::getInputFilter() const
104 {
105  std::vector< WDataModuleInputFilter::ConstSPtr > filters;
106 
107  // NOTE: plain extension. No wildcards or prefixing "."!
108  filters.push_back( WDataModuleInputFilter::ConstSPtr( new WDataModuleInputFilterFile( "vim", "VIM point cloud files" ) ) );
109 
110  return filters;
111 }
112 
114 {
115  // notify the module only
116  m_reload = true;
118 }
119 
121 {
122  m_reload = false;
123 
124  // open file
125  WDataModuleInputFile::SPtr inputFile = getInputAs< WDataModuleInputFile >();
126  if( !inputFile )
127  {
128  // No input? Reset output too.
129  m_output->updateData( WDataSetPoints::SPtr() );
130  return;
131  }
132  boost::filesystem::path p = inputFile->getFilename();
133 
134  std::ifstream ifs;
135  ifs.open( p.string().c_str(), std::ifstream::in );
136  if( !ifs || ifs.bad() )
137  {
138  errorLog() << "Could not open file \"" << p.string() << "\".";
139  return;
140  }
141 
142  std::shared_ptr< WProgress > progress1( new WProgress( "Loading" ) );
143  m_progress->addSubProgress( progress1 );
144 
145  // target memory
146  WDataSetPoints::VertexArray vertices( new WDataSetPoints::VertexArray::element_type() );
147  WDataSetPoints::ColorArray colors( new WDataSetPoints::ColorArray::element_type() );
148  WBoundingBox bb;
149 
150  infoLog() << "Start Loading ...";
151 
152  // interpret file
153  std::string line;
154  std::vector< std::string > tokens;
155  size_t numPoints = 0;
156  while( !ifs.eof() )
157  {
158  std::getline( ifs, line );
159  tokens = string_utils::tokenize( line );
160 
161  if( ( tokens.size() == 9 ) && ( tokens[ 0 ] == "!" ) ) // mathc
162  {
163  // coordinate:
164  WVector3f coord(
165  string_utils::fromString< float >( tokens[2] ),
166  string_utils::fromString< float >( tokens[3] ),
167  string_utils::fromString< float >( tokens[4] )
168  );
169 
170  // expand bb
171  bb.expandBy( coord );
172 
173  // read 3rd to 5th number
174  vertices->push_back( coord.x() );
175  vertices->push_back( coord.y() );
176  vertices->push_back( coord.z() );
177  colors->push_back( 1.0 );
178  colors->push_back( 1.0 );
179  colors->push_back( 1.0 );
180 
181  numPoints++;
182  }
183  }
184 
185  infoLog() << "Loaded " << numPoints << " points from file. Done.";
186 
187  // finally provide output data
188  std::shared_ptr< WDataSetPoints> newOutput( new WDataSetPoints( vertices, colors, bb ) );
189  m_output->updateData( newOutput );
190 
191  // done. close file and report finish
192  progress1->finish();
193  ifs.close();
194 }
195 
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 notify()
Notifies all waiting threads.
std::shared_ptr< WDataModuleInputFile > SPtr
Convenience typedef for a std::shared_ptr< WDataModuleInputFile >.
Checks a given WDataModuleInput against a file type.
std::shared_ptr< const WDataModuleInputFilter > ConstSPtr
Convenience typedef for a std::shared_ptr< const WDataModuleInputFilter >.
Base for all data loader modules.
Definition: WDataModule.h:47
Dataset to store a bunch of points without order or topology.
std::shared_ptr< WDataSetPoints > SPtr
Pointer to dataset.
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 module loads VIM files (point data).
Definition: WMReadVIM.h:42
virtual void load()
Load data.
Definition: WMReadVIM.cpp:120
std::shared_ptr< WModuleOutputData< WDataSetPoints > > m_output
The output connector for the data.
Definition: WMReadVIM.h:111
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...
Definition: WMReadVIM.cpp:52
virtual const std::string getDescription() const
Gives back a description of this module.
Definition: WMReadVIM.cpp:63
virtual void connectors()
Initialize the connectors this module is using.
Definition: WMReadVIM.cpp:69
virtual void handleInputChange()
Handle a newly set input.
Definition: WMReadVIM.cpp:113
virtual void moduleMain()
Entry point after loading the module.
Definition: WMReadVIM.cpp:77
virtual std::vector< WDataModuleInputFilter::ConstSPtr > getInputFilter() const
Define a list of file filters we support.
Definition: WMReadVIM.cpp:103
virtual ~WMReadVIM()
Destructor.
Definition: WMReadVIM.cpp:47
bool m_reload
True if the load function needs to be called.
Definition: WMReadVIM.h:116
virtual const std::string getName() const
Gives back the name of this module.
Definition: WMReadVIM.cpp:57
WMReadVIM()
Default constructor.
Definition: WMReadVIM.cpp:40
A fixed size matrix class.
Definition: WMatrixFixed.h:150
ValueT & z()
Access z element of vector.
ValueT & y()
Access y element of vector.
ValueT & x()
Access x element of vector.
Class offering an instantiate-able data connection between modules.
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
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
void waitRestored()
This method waits for the module to be restored completely.
Definition: WModule.cpp:625
Class managing progress inside of modules.
Definition: WProgress.h:42
WBoolFlag m_shutdownFlag
Condition getting fired whenever the thread should quit.
std::vector< std::string > tokenize(const std::string &source, const std::string &delim=WHITESPACE, bool compress=true)
Splits the given string into a vector of strings (so called tokens).