OpenWalnut  1.5.0dev
WMeshReaderDIP.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 <memory>
26 #include <string>
27 #include <vector>
28 
29 #include "WMeshReaderDIP.h"
30 
32  WObjectNDIP< WMeshReaderInterface >( "DIP", "Load DIP Meshes." )
33 {
34  // add properties
35  m_propDatasetSizeX = m_properties->addProperty( "Dataset size X", "Size of the dataset in x direction", 160 );
36  m_propDatasetSizeY = m_properties->addProperty( "Dataset size Y", "Size of the dataset in y direction", 200 );
37  m_propDatasetSizeZ = m_properties->addProperty( "Dataset size Z", "Size of the dataset in z direction", 160 );
38 }
39 
41 {
42  // cleanup
43 }
44 
46  boost::filesystem::path file )
47 {
48  namespace su = string_utils;
49 
50  std::string fileName = file.string();
51  WAssert( !fileName.empty(), "No filename specified." );
52 
53  std::shared_ptr< WProgress > progress( new WProgress( "Read Mesh", 3 ) );
54  parentProgress->addSubProgress( progress );
55 
56  std::ifstream ifs;
57  ifs.open( fileName.c_str(), std::ifstream::in );
58  if( !ifs || ifs.bad() )
59  {
60  WLogger::getLogger()->addLogMessage( "Trying to load a broken file '" + fileName + "'", "Read Mesh", LL_ERROR );
61  throw std::runtime_error( "Problem during reading file. Probably file not found." );
62  }
63  std::string line;
64  size_t numPoints = 0;
65  size_t numCells = 0;
66  size_t typeCells = 0;
67  std::vector< std::string > tokens;
68 
69  // first pass, try to read all information that might be anywhere in the file
70  while( !ifs.eof() )
71  {
72  std::getline( ifs, line );
73  tokens = su::tokenize( line );
74  if( tokens.size() == 2 && su::toLower( tokens.at( 0 ) ) == "numberpositions=" )
75  {
76  try
77  {
78  numPoints = string_utils::fromString< size_t >( tokens.at( 1 ) );
79  }
80  catch( const std::exception &e )
81  {
82  WLogger::getLogger()->addLogMessage( "Invalid number of points: " + tokens.at( 1 ), "Read Mesh", LL_ERROR );
83  progress->finish();
84  return std::shared_ptr< WTriangleMesh >();
85  }
86  }
87  if( tokens.size() == 2 && su::toLower( tokens.at( 0 ) ) == "numberpolygons=" )
88  {
89  try
90  {
91  numCells = string_utils::fromString< size_t >( tokens.at( 1 ) );
92  }
93  catch( const std::exception &e )
94  {
95  WLogger::getLogger()->addLogMessage( "Invalid number of polygons: " + tokens.at( 1 ), "Read Mesh", LL_ERROR );
96  progress->finish();
97  return std::shared_ptr< WTriangleMesh >();
98  }
99  }
100  if( tokens.size() == 2 && su::toLower( tokens.at( 0 ) ) == "typepolygons=" )
101  {
102  try
103  {
104  typeCells = string_utils::fromString< size_t >( tokens.at( 1 ) );
105  }
106  catch( const std::exception &e )
107  {
108  WLogger::getLogger()->addLogMessage( "Invalid type of polygons: " + tokens.at( 1 ), "Read Mesh", LL_ERROR );
109  progress->finish();
110  return std::shared_ptr< WTriangleMesh >();
111  }
112  if( typeCells != 3 )
113  {
114  WLogger::getLogger()->addLogMessage( "Invalid type of polygons: " + tokens.at( 1 ), "Read Mesh", LL_ERROR );
115  progress->finish();
116  return std::shared_ptr< WTriangleMesh >();
117  }
118  }
119  }
120  wlog::debug( "Read Mesh" ) << "Positions: " << numPoints << " Cells: " << numCells;
121 
122  //ifs.seekg( 0, std::ios::beg );
123  ifs.close();
124  ifs.open( fileName.c_str(), std::ifstream::in );
125  wlog::debug( "Read Mesh" ) << "current position: " << ifs.tellg();
126  while( !ifs.eof() )
127  {
128  std::getline( ifs, line );
129  tokens = su::tokenize( line );
130  if( tokens.size() == 1 && su::toLower( tokens.at( 0 ) ) == "positionsfixed" )
131  {
132  wlog::debug( "Read Mesh" ) << "found vertex info at file position: " << ifs.tellg();
133  break;
134  }
135  }
136  if( ifs.eof() )
137  {
138  WLogger::getLogger()->addLogMessage( "Couldn't find vertex info in the file", "Read Mesh", LL_ERROR );
139  progress->finish();
140  return std::shared_ptr< WTriangleMesh >();
141  }
142 
143  std::vector< osg::Vec3 > points;
144  points.reserve( numPoints );
145 
146  wlog::debug( "Read Mesh" ) << "Start reading vertex info";
147  for( unsigned int i = 0; i < numPoints; ++i )
148  {
149  std::getline( ifs, line );
150  tokens = su::tokenize( line );
151 
152  try
153  {
154  points.push_back( osg::Vec3( string_utils::fromString< float >( tokens.at( 1 ) ) + ( m_propDatasetSizeX->get( true )/2.0 ),
155  string_utils::fromString< float >( tokens.at( 0 ) ) + ( m_propDatasetSizeY->get( true )/2.0 ),
156  string_utils::fromString< float >( tokens.at( 2 ) ) + ( m_propDatasetSizeZ->get( true )/2.0 ) ) );
157  }
158  catch( const std::exception &e )
159  {
160  WLogger::getLogger()->addLogMessage( "Invalid vertex position", "Read Mesh", LL_ERROR );
161  progress->finish();
162  return std::shared_ptr< WTriangleMesh >();
163  }
164  }
165  wlog::debug( "Read Mesh" ) << "Finished reading vertex info";
166 
167  std::shared_ptr< WTriangleMesh > triMesh( new WTriangleMesh( numPoints, numCells ) );
168 
169  for( unsigned int i = 0; i < numPoints; ++i )
170  {
171  triMesh->addVertex( points[i] );
172  }
173  ++*progress;
174 
175 
176  //ifs.seekg( 0, std::ios::beg );
177  ifs.close();
178  ifs.open( fileName.c_str(), std::ifstream::in );
179  while( !ifs.eof() )
180  {
181  std::getline( ifs, line );
182  tokens = su::tokenize( line );
183  if( tokens.size() == 1 && su::toLower( tokens.at( 0 ) ) == "polygons" )
184  {
185  wlog::debug( "Read Mesh" ) << "found polygon info at file postion: " << ifs.tellg();
186  break;
187  }
188  }
189  if( ifs.eof() )
190  {
191  WLogger::getLogger()->addLogMessage( "Couldn't find polygon info in the file", "Read Mesh", LL_ERROR );
192  progress->finish();
193  return std::shared_ptr< WTriangleMesh >();
194  }
195 
196 
197  wlog::debug( "Read Mesh" ) << "Start reading polygon info";
198  for( unsigned int i = 0; i < numCells; ++i )
199  {
200  std::getline( ifs, line );
201  tokens = su::tokenize( line );
202 
203  try
204  {
205  triMesh->addTriangle( string_utils::fromString< size_t >( tokens.at( 0 ) ),
206  string_utils::fromString< size_t >( tokens.at( 1 ) ),
207  string_utils::fromString< size_t >( tokens.at( 2 ) ) );
208  }
209  catch( const std::exception &e )
210  {
211  WLogger::getLogger()->addLogMessage( "Invalid triangle ID", "Read Mesh", LL_ERROR );
212  progress->finish();
213  return std::shared_ptr< WTriangleMesh >();
214  }
215  }
216  wlog::debug( "Read Mesh" ) << "Finished reading polygon info";
217  ++*progress;
218  progress->finish();
219 
220  return triMesh;
221 }
void addLogMessage(std::string message, std::string source="", LogLevel level=LL_DEBUG)
Appends a log message to the logging queue.
Definition: WLogger.cpp:84
static WLogger * getLogger()
Returns pointer to the currently running logger instance.
Definition: WLogger.cpp:64
WMeshReaderDIP()
Constructor.
WPropInt m_propDatasetSizeY
Size of the dataset (Y)
virtual ~WMeshReaderDIP()
Destructor.
WPropInt m_propDatasetSizeX
Size of the dataset (X)
WPropInt m_propDatasetSizeZ
Size of the dataset (Z)
virtual WTriangleMesh::SPtr operator()(WProgressCombiner::SPtr parentProgress, boost::filesystem::path file)
Load the dataset.
Define the interface which is injected into an WObjectNDIP.
This is a base class for everything which has a Name,Description,Icon and Properties (=NDIP).
Definition: WObjectNDIP.h:42
WProperties::SPtr m_properties
the properties of the object.
Definition: WObjectNDIP.h:99
std::shared_ptr< WProgressCombiner > SPtr
Abbreviate shared_ptr for this class.
Class managing progress inside of modules.
Definition: WProgress.h:42
Triangle mesh data structure allowing for convenient access of the elements.
Definition: WTriangleMesh.h:46
std::shared_ptr< WTriangleMesh > SPtr
Shared pointer.
Definition: WTriangleMesh.h:55
Some utilities for string manipulation and output operations.
Definition: WStringUtils.h:59
WStreamedLogger debug(const std::string &source)
Logging a debug message.
Definition: WLogger.h:331