OpenWalnut  1.5.0dev
WMReadVCL.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 "WMReadVCL.h"
31 #include "core/common/WPathHelper.h"
32 #include "core/common/WStringUtils.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( WMReadVCL )
37 
39  WModule()
40 {
41 }
42 
44 {
45  // Cleanup!
46 }
47 
48 std::shared_ptr< WModule > WMReadVCL::factory() const
49 {
50  return std::shared_ptr< WModule >( new WMReadVCL() );
51 }
52 
53 const std::string WMReadVCL::getName() const
54 {
55  return "Read VCL";
56 }
57 
58 const std::string WMReadVCL::getDescription() const
59 {
60  return "Read VCL line data and their attributes.";
61 }
62 
64 {
65  m_output = WModuleOutputData< WDataSetFibers >::createAndAdd( shared_from_this(), "out", "The data that has been loaded." );
66 
68 }
69 
71 {
72  m_propCondition = std::shared_ptr< WCondition >( new WCondition() );
73  m_dataFile = m_properties->addProperty( "File", "File to load.", WPathHelper::getAppPath() );
75 
76  m_loadAttrib = m_properties->addProperty( "Load Attribute", "If true, load the specified attribute from file.", false );
77  m_attrib = m_properties->addProperty( "Attribute", "Index of the attribute to use if there are any. Begins at 1.", 1 );
78  m_attrib->setMin( 1 );
79 
80  m_readTriggerProp = m_properties->addProperty( "Do read", "Press!", WPVBaseTypes::PV_TRIGGER_READY, m_propCondition );
82 }
83 
85 {
87  ready();
88  while( !m_shutdownFlag() )
89  {
91 
92  if( m_shutdownFlag() )
93  {
94  break;
95  }
96 
97  debugLog() << "Loading " << m_dataFile->get().string() << ".";
98  std::shared_ptr< WProgress > progress = std::shared_ptr< WProgress >( new WProgress( "Read Data" ) );
99  m_progress->addSubProgress( progress );
100  readData( m_dataFile->get().string() );
101  m_output->updateData( m_dataSet );
102  progress->finish();
104  }
105 }
106 
107 void WMReadVCL::readData( std::string filename )
108 {
109  std::ifstream ifs;
110  ifs.open( filename.c_str(), std::ifstream::in );
111  if( !ifs || ifs.bad() )
112  {
113  errorLog() << "Could not open file \"" << filename << "\".";
114  return;
115  }
116 
117  // target memory
118  WDataSetFibers::VertexArray vertices( new WDataSetFibers::VertexArray::element_type() );
119  WDataSetFibers::LengthArray lengths( new WDataSetFibers::LengthArray::element_type() );
120  WDataSetFibers::IndexArray lineStartIndices( new WDataSetFibers::IndexArray::element_type() );
121  WDataSetFibers::IndexArray verticesReverse( new WDataSetFibers::IndexArray::element_type() );
122  WDataSetFibers::VertexParemeterArray attribs( new WDataSetFibers::VertexParemeterArray::element_type() );
123 
124  // Go through, line by line
125  std::string line;
126  std::vector< std::string > tokens;
127  size_t numLines = 0;
128  WBoundingBox bb;
129 
130  unsigned int attribIndex = m_attrib->get( true ) - 1;
131  bool loadAttrib = m_loadAttrib->get( true );
132  size_t currentIndex = 0;
133  size_t currentLength = 0;
134  size_t currentStartIndex = 0;
135  size_t currentLineCount = 0;
136 
137  while( !ifs.eof() )
138  {
139  std::getline( ifs, line );
140  tokens = string_utils::tokenize( line );
141  numLines++;
142 
143  // point line
144  if( ( tokens.size() >= 4 ) && ( tokens[ 0 ] == "p" ) )
145  {
146  // coordinate:
147  WVector3f coord(
148  string_utils::fromString< float >( tokens[1] ),
149  string_utils::fromString< float >( tokens[2] ),
150  string_utils::fromString< float >( tokens[3] )
151  );
152 
153  // store attributes
154  if( loadAttrib )
155  {
156  if( tokens.size() - 1 >= 4 + attribIndex )
157  {
158  attribs->push_back( string_utils::fromString< double >( tokens[ 4 + attribIndex ] ) );
159  }
160  else
161  {
162  attribs->push_back( 0.5 );
163  }
164  }
165 
166  // expand bb
167  bb.expandBy( coord );
168 
169  vertices->push_back( coord.x() );
170  vertices->push_back( coord.y() );
171  vertices->push_back( coord.z() );
172  verticesReverse->push_back( currentLineCount );
173 
174  // keep track of indices until we reach an "n" line.
175  currentIndex++;
176  currentLength++;
177  }
178 
179  // new line? (or beginning of file
180  if( ( tokens.size() == 1 ) && ( tokens[ 0 ] == "n" ) )
181  {
182  // new start index
183  lineStartIndices->push_back( currentStartIndex );
184  // record length of last line
185  lengths->push_back( currentLength );
186 
187  currentStartIndex = currentIndex;
188  currentLineCount++;
189  currentLength = 0;
190  }
191  }
192 
193  ifs.close();
194 
195  debugLog() << " = Num Lines: " << currentLineCount;
196  debugLog() << " = Num Vertices: " << currentIndex;
197 
198  // create dataset
199  if( loadAttrib )
200  {
201  m_dataSet = WDataSetFibers::SPtr( new WDataSetFibers( vertices, lineStartIndices, lengths, verticesReverse, bb, attribs ) );
202  }
203  else
204  {
205  m_dataSet = WDataSetFibers::SPtr( new WDataSetFibers( vertices, lineStartIndices, lengths, verticesReverse, bb ) );
206  }
207 }
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.
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.
std::shared_ptr< std::vector< size_t > > IndexArray
Index list indexing fibers in VertexArray in terms of vertex numbers.
std::shared_ptr< std::vector< size_t > > LengthArray
Lengths of fibers in terms of vertices.
std::shared_ptr< WDataSetFibers > SPtr
Pointer to dataset.
std::shared_ptr< std::vector< double > > VertexParemeterArray
Parameter storage for each vertex.
std::shared_ptr< std::vector< float > > VertexArray
List of vertex coordinates in term of components of vertices.
A module to read VCL line data.
Definition: WMReadVCL.h:43
WPropFilename m_dataFile
The data will be read from this file.
Definition: WMReadVCL.h:93
virtual void moduleMain()
Entry point after loading the module.
Definition: WMReadVCL.cpp:84
virtual const std::string getName() const
Gives back the name of this module.
Definition: WMReadVCL.cpp:53
virtual void connectors()
Initialize the connectors this module is using.
Definition: WMReadVCL.cpp:63
virtual ~WMReadVCL()
Destructor of module.
Definition: WMReadVCL.cpp:43
virtual void properties()
Initialize the properties for this module.
Definition: WMReadVCL.cpp:70
WPropInt m_attrib
The index of the attribute to use.
Definition: WMReadVCL.h:95
WPropBool m_loadAttrib
Load additional attribute.
Definition: WMReadVCL.h:94
virtual const std::string getDescription() const
Gives back a description of this module.
Definition: WMReadVCL.cpp:58
std::shared_ptr< WDataSetFibers > m_dataSet
This data set is provided as output through the connector.
Definition: WMReadVCL.h:98
WPropTrigger m_readTriggerProp
The trigger for loading.
Definition: WMReadVCL.h:96
WMReadVCL()
Constructor of module.
Definition: WMReadVCL.cpp:38
std::shared_ptr< WModuleOutputData< WDataSetFibers > > m_output
Output connector provided by this module.
Definition: WMReadVCL.h:99
std::shared_ptr< WCondition > m_propCondition
A condition used to notify about changes in several properties.
Definition: WMReadVCL.h:92
void readData(std::string filename)
Read VCL data.
Definition: WMReadVCL.cpp:107
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: WMReadVCL.cpp:48
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.
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
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
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
static boost::filesystem::path getAppPath()
The path where the binary file resides in.
Definition: WPathHelper.cpp:93
Class managing progress inside of modules.
Definition: WProgress.h:42
WBoolFlag m_shutdownFlag
Condition getting fired whenever the thread should quit.
@ PV_TRIGGER_READY
Trigger property: is ready to be triggered (again)
void addTo(WPropFilename prop)
Add the PC_PATHEXISTS constraint to the property.
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).