29 #include "WMeshReaderVTK.h"
36 "If the module can load vectors from the file "
37 "it will interpret them as colors of the surface.",
47 boost::filesystem::path file )
51 std::string fileName = file.string();
52 WAssert( !fileName.empty(),
"No filename specified." );
54 std::shared_ptr< WProgress > progress(
new WProgress(
"Read Mesh", 3 ) );
55 parentProgress->addSubProgress( progress );
58 ifs.open( fileName.c_str(), std::ifstream::in );
59 if( !ifs || ifs.bad() )
62 throw std::runtime_error(
"Problem during reading file. Probably file not found." );
67 std::vector< std::string > header;
68 for(
int i = 0; i < 4; ++i )
70 std::getline( ifs, line );
75 return std::shared_ptr< WTriangleMesh >();
77 header.push_back( line );
79 if( header.at( 0 ) !=
"# vtk DataFile Version 2.0" )
84 if( su::toUpper( su::trim( header.at( 2 ) ) ) !=
"ASCII" )
88 return std::shared_ptr< WTriangleMesh >();
91 if( su::tokenize( header.at( 3 ) ).size() < 2 ||
92 su::toUpper( su::tokenize( header.at( 3 ) )[1] ) !=
"UNSTRUCTURED_GRID" )
96 return std::shared_ptr< WTriangleMesh >();
101 std::getline( ifs, line );
102 size_t numPoints = 0;
103 std::vector< std::string > tokens = su::tokenize( line );
104 if( tokens.size() != 3 || su::toLower( tokens.at( 2 ) ) !=
"float" || su::toLower( tokens.at( 0 ) ) !=
"points" )
108 return std::shared_ptr< WTriangleMesh >();
112 numPoints = string_utils::fromString< size_t >( tokens.at( 1 ) );
114 catch(
const std::exception &e )
118 return std::shared_ptr< WTriangleMesh >();
122 std::vector< WPosition > points;
123 points.reserve( numPoints );
125 for(
unsigned int i = 0; i < numPoints; ++i )
127 ifs >> pointData[0] >> pointData[1] >> pointData[2];
128 points.push_back(
WPosition( pointData[0], pointData[1], pointData[2] ) );
132 char* cellsMarker =
new char[30];
135 ifs >> cellsMarker >> nbCells >> nbNumbers;
137 std::shared_ptr< WTriangleMesh > triMesh(
new WTriangleMesh( numPoints, nbCells ) );
139 for(
unsigned int i = 0; i < numPoints; ++i )
141 triMesh->addVertex( osg::Vec3( points[i][0], points[i][1], points[i][2] ) );
145 unsigned int nbCellVerts;
146 for(
unsigned int i = 0; i < nbCells; ++i )
149 ifs >> nbCellVerts >> tri[0] >> tri[1] >> tri[2];
150 triMesh->addTriangle( tri[0], tri[1], tri[2] );
151 if( nbCellVerts != 3 )
156 return std::shared_ptr< WTriangleMesh >();
163 char* cells_typesMarker =
new char[30];
165 ifs >> cells_typesMarker >> nbCellTypes;
166 unsigned int cellType;
167 for(
unsigned int i = 0; i < nbCellTypes; ++i )
174 return std::shared_ptr< WTriangleMesh >();
178 char* marker =
new char[30];
180 ifs >> marker >> nbVectors;
181 if( std::string( marker ) ==
"POINT_DATA" && nbVectors == numPoints )
184 char* vectorMarker =
new char[30];
185 char* vectorName =
new char[30];
186 char* vectorInfo1 =
new char[30];
187 char* vectorInfo2 =
new char[30];
188 ifs >> vectorMarker >> vectorName >> vectorInfo1 >> vectorInfo2;
190 if( std::string( vectorMarker ) ==
"VECTORS"
192 && std::string( vectorInfo1 ) ==
"float" )
196 for(
unsigned int i = 0; i < nbVectors; ++i )
199 std::getline( ifs, line,
'\n' );
200 ifs >> vectorComp[0] >> vectorComp[1] >> vectorComp[2];
201 triMesh->setVertexColor( i, vectorComp );
204 if( std::string( vectorMarker ) ==
"ARRAYS" )
207 WAssert( vectorInfo1[0] ==
'2' || vectorInfo1[0] ==
'3',
"Can only deal with 2D or 3D arrays." );
209 && std::string( vectorInfo2 ) ==
"float" )
213 for(
unsigned int i = 0; i < nbVectors; ++i )
216 std::getline( ifs, line,
'\n' );
217 if( vectorInfo1[0] ==
'2' )
219 ifs >> vectorComp[0] >> vectorComp[1];
222 if( vectorInfo1[0] ==
'3' )
224 ifs >> vectorComp[0] >> vectorComp[1] >> vectorComp[2];
226 triMesh->setVertexColor( i, vectorComp );
230 triMesh->rescaleVertexColors();
231 delete[] vectorMarker;
233 delete[] vectorInfo1;
234 delete[] vectorInfo2;
241 delete[] cellsMarker;
242 delete[] cells_typesMarker;
void addLogMessage(std::string message, std::string source="", LogLevel level=LL_DEBUG)
Appends a log message to the logging queue.
static WLogger * getLogger()
Returns pointer to the currently running logger instance.
Define the interface which is injected into an WObjectNDIP.
WMeshReaderVTK()
Constructor.
virtual WTriangleMesh::SPtr operator()(WProgressCombiner::SPtr parentProgress, boost::filesystem::path file)
Load the dataset.
virtual ~WMeshReaderVTK()
Destructor.
WPropBool m_propVectorAsColor
Use vectors from file as color of surface.
This is a base class for everything which has a Name,Description,Icon and Properties (=NDIP).
WProperties::SPtr m_properties
the properties of the object.
This only is a 3d double vector.
std::shared_ptr< WProgressCombiner > SPtr
Abbreviate shared_ptr for this class.
Class managing progress inside of modules.
Triangle mesh data structure allowing for convenient access of the elements.
std::shared_ptr< WTriangleMesh > SPtr
Shared pointer.
Some utilities for string manipulation and output operations.
std::string toString(const T &value)
Convert a given value to a string.