Radium Engine  1.5.0
OBJFileManager.cpp
1 #include <IO/deprecated/OBJFileManager.hpp>
2 
3 namespace Ra {
4 
5 using namespace Core;
6 
7 namespace IO {
8 
12 OBJFileManager::OBJFileManager() : FileManager<Geometry::TriangleMesh>() {}
13 
18 
22 std::string OBJFileManager::fileExtension() const {
23  return "obj";
24 }
25 
26 bool OBJFileManager::importData( std::istream& file, Geometry::TriangleMesh& data ) {
27  data.clear();
28  std::string line;
29  Geometry::TriangleMesh::PointAttribHandle::Container vertices;
30  Geometry::TriangleMesh::NormalAttribHandle::Container normals;
31  Geometry::TriangleMesh::IndexContainerType indices;
32  while ( std::getline( file, line ) ) {
33  std::istringstream iss( line );
34  std::string token;
35  iss >> token;
36 
37  if ( token == "#" ) { continue; }
38  if ( token == "v" ) {
39  Vector3 v;
40  iss >> v[0] >> v[1] >> v[2];
41  vertices.push_back( v );
42  }
43  if ( token == "vn" ) {
44  Vector3 n;
45  iss >> n[0] >> n[1] >> n[2];
46  normals.push_back( n );
47  }
48  if ( token == "vt" ) { continue; }
49  if ( token == "vp" ) { continue; }
50  if ( token == "f" ) {
51  Vector3ui f;
52  std::string ltoken;
53  int count = 0;
54  // skip first space
55  std::getline( iss, ltoken, ' ' );
56  while ( std::getline( iss, ltoken, ' ' ) ) {
57  std::istringstream ss( ltoken );
58  if ( count < 3 ) {
59  ss >> f[count];
60  f[count] -= 1;
61  }
62  else {
63  addLogErrorEntry( "MESH CONTAINS QUADS." );
64  return false;
65  }
66  count++;
67  }
68  indices.push_back( f );
69  }
70  }
71  if ( vertices.size() == 0 ) {
72  addLogErrorEntry( "MESH IS EMPTY." );
73  return false;
74  }
75 
76  data.setVertices( std::move( vertices ) );
77  data.setNormals( std::move( normals ) );
78  data.setIndices( std::move( indices ) );
79  return true;
80 }
81 
82 bool OBJFileManager::exportData( std::ostream& file, const Geometry::TriangleMesh& data ) {
83  std::string content = "";
84  if ( data.vertices().size() == 0 ) {
85  addLogErrorEntry( "MESH IS EMPTY." );
86  return false;
87  }
88  // Vertices
89  for ( const auto& v : data.vertices() ) {
90  content += "v " + std::to_string( v[0] ) + " " + std::to_string( v[1] ) + " " +
91  std::to_string( v[2] ) + "\n";
92  }
93  // Normals
94  for ( const auto& n : data.normals() ) {
95  content += "vn " + std::to_string( n[0] ) + " " + std::to_string( n[1] ) + " " +
96  std::to_string( n[2] ) + "\n";
97  }
98  // Triangle
99  for ( const auto& f : data.getIndices() ) {
100  content += "f " + std::to_string( f[0] + 1 ) + " " + std::to_string( f[1] + 1 ) + " " +
101  std::to_string( f[2] + 1 ) + "\n";
102  }
103  file << content;
104  return true;
105 }
106 
107 } // namespace IO
108 } // namespace Ra
std::string fileExtension() const override
INTERFACE.
~OBJFileManager() override
DESTRUCTOR.
Definition: Cage.cpp:3