32 #include <boost/foreach.hpp>
34 #include "WMReadSimpleTextLineData.h"
35 #include "core/common/WPathHelper.h"
36 #include "core/common/WStringUtils.h"
37 #include "core/kernel/WDataModuleInputFile.h"
38 #include "core/kernel/WDataModuleInputFilterFile.h"
39 #include "core/kernel/WKernel.h"
64 return "Read Simple Text Line Data";
70 return "This module reads simple text-based line files.";
109 std::vector< WDataModuleInputFilter::ConstSPtr > filters;
157 boost::filesystem::path p = inputFile->getFilename();
160 ifs.open( p.string().c_str(), std::ifstream::in );
161 if( !ifs || ifs.bad() )
163 errorLog() <<
"Could not open file \"" << p.string() <<
"\".";
167 std::shared_ptr< WProgress > progress1(
new WProgress(
"Loading" ) );
176 std::vector< std::string > tokens;
180 std::vector< WVector3f > loadedVertices;
181 typedef std::vector< size_t > LineStrip;
182 std::vector< LineStrip > loadedLineStrips;
183 std::map< size_t, std::vector< double > > loadedPointAttribs;
184 std::map< size_t, std::vector< double > > loadedLineAttribs;
188 std::getline( ifs, line );
194 if( tokens.size() < 2 )
203 string_utils::fromString< float >( tokens[1] ),
204 string_utils::fromString< float >( tokens[2] ),
205 string_utils::fromString< float >( tokens[3] )
211 loadedVertices.push_back( coord );
220 for(
size_t i = 1; i < tokens.size(); ++i )
222 size_t idx = string_utils::fromString< size_t >( tokens[i] );
227 loadedLineStrips.push_back( ls );
233 size_t attrIdx = string_utils::fromString< size_t >( tokens[1] );
234 loadedPointAttribs[ attrIdx ].push_back( string_utils::fromString< float >( tokens[2] ) );
240 size_t attrIdx = string_utils::fromString< size_t >( tokens[1] );
241 loadedLineAttribs[ attrIdx ].push_back( string_utils::fromString< float >( tokens[2] ) );
253 std::vector< WDataSetFibers::VertexParemeterArray > pAttribs;
254 std::vector< WDataSetFibers::LineParemeterArray > lAttribs;
256 for( std::map<
size_t, std::vector< double > >::const_iterator i = loadedLineAttribs.begin(); i != loadedLineAttribs.end(); ++i )
258 size_t desiredSize = loadedLineStrips.size();
259 size_t realSize = ( *i ).second.size();
261 if( desiredSize != realSize )
263 warnLog() <<
"Ignoring line attribute " << ( *i ).first <<
" as there are too few/too much items.";
267 std::shared_ptr< WDataSetFibers::LineParemeterArray::element_type > vec(
268 new WDataSetFibers::LineParemeterArray::element_type( realSize ) );
269 std::copy( ( *i ).second.begin(), ( *i ).second.end(), vec->begin() );
270 lAttribs.push_back( vec );
274 std::map< size_t, size_t > pAttribIdxMap;
275 for( std::map<
size_t, std::vector< double > >::const_iterator i = loadedPointAttribs.begin(); i != loadedPointAttribs.end(); ++i )
277 size_t desiredSize = loadedVertices.size();
278 size_t realSize = ( *i ).second.size();
280 if( desiredSize != realSize )
282 warnLog() <<
"Ignoring point attribute " << ( *i ).first <<
" as there are too few/too much items.";
286 std::shared_ptr< WDataSetFibers::VertexParemeterArray::element_type > vec(
287 new WDataSetFibers::VertexParemeterArray::element_type() );
288 pAttribIdxMap[ pAttribs.size() ] = ( *i ).first;
289 pAttribs.push_back( vec );
293 size_t currentStartIndex = 0;
296 for( std::vector< LineStrip >::const_iterator iter = loadedLineStrips.begin(); iter != loadedLineStrips.end(); ++iter )
298 const LineStrip& ls = *iter;
301 for( LineStrip::const_iterator indexIter = ls.begin(); ls.end() != indexIter; ++indexIter )
304 size_t pIdx = *indexIter;
308 vertices->push_back( p[ 0 ] );
309 vertices->push_back( p[ 1 ] );
310 vertices->push_back( p[ 2 ] );
312 for( std::vector< WDataSetFibers::VertexParemeterArray >::const_iterator i = pAttribs.begin(); i != pAttribs.end(); ++i )
314 size_t attrIdx = pAttribIdxMap[ i - pAttribs.begin() ];
315 ( *i )->push_back( loadedPointAttribs[ attrIdx ][ pIdx ] );
319 verticesReverse->push_back( iter - loadedLineStrips.begin() );
323 lengths->push_back( ls.size() );
325 lineStartIndices->push_back( currentStartIndex );
326 currentStartIndex += ls.
size();
330 ds->setVertexParameters( pAttribs );
331 ds->setLineParameters( lAttribs );
339 infoLog() <<
"Loaded " << loadedLineStrips.size() <<
" line strips from file, having " << pAttribs.size() <<
" point attributes and "
340 << lAttribs.size() <<
" line attributes. Done.";
void expandBy(const WBoundingBoxImpl< VT > &bb)
Expands this bounding box to include the given bounding box.
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.
Base for all data loader modules.
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< float > > VertexArray
List of vertex coordinates in term of components of vertices.
This module loads simple text-based line data files.
virtual const std::string getName() const
Gives back the name of this module.
virtual void handleInputChange()
Handle a newly set input.
bool m_reload
True if the load function needs to be called.
virtual ~WMReadSimpleTextLineData()
Destructor.
std::shared_ptr< WModuleOutputData< WDataSetFibers > > m_output
The output connector for the data.
WMReadSimpleTextLineData()
Default constructor.
virtual void moduleMain()
Entry point after loading the module.
virtual const std::string getDescription() const
Gives back a description of this module.
virtual void load()
Load data.
virtual std::vector< WDataModuleInputFilter::ConstSPtr > getInputFilter() const
Define a list of file filters we support.
virtual void connectors()
Initialize the connectors this module is using.
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...
A fixed size matrix class.
size_t size() const
The number of entries.
Class offering an instantiate-able data connection between modules.
wlog::WStreamedLogger warnLog() const
Logger instance for comfortable warning- logs.
void ready()
Call this whenever your module is ready and can react on property changes.
WConditionSet m_moduleState
The internal state of the module.
wlog::WStreamedLogger errorLog() const
Logger instance for comfortable error logging.
std::shared_ptr< WProgressCombiner > m_progress
Progress indicator used as parent for all progress' of this module.
wlog::WStreamedLogger infoLog() const
Logger instance for comfortable info logging.
virtual void connectors()
Initialize connectors in this function.
void waitRestored()
This method waits for the module to be restored completely.
Class managing progress inside of modules.
WBoolFlag m_shutdownFlag
Condition getting fired whenever the thread should quit.
std::string toLower(const std::string &source)
Transforms all characters in the given string into lower case characters.
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).
std::string trim(const std::string &source, const std::string &t=WHITESPACE)
Trims any occurence of each character given in parameter t from both ends (right and left side) of th...