Radium Engine  1.5.0
OFFFileManager.cpp
1 #include <IO/deprecated/OFFFileManager.hpp>
2 
3 namespace Ra {
4 
5 using namespace Core;
6 
7 namespace IO {
8 
12 OFFFileManager::OFFFileManager() : FileManager<Geometry::TriangleMesh>() {}
13 
18 
22 std::string OFFFileManager::header() const {
23  return "OFF";
24 }
25 
29 std::string OFFFileManager::fileExtension() const {
30  return "off";
31 }
32 
33 bool OFFFileManager::importData( std::istream& file, Geometry::TriangleMesh& data ) {
34  std::string h;
35  file >> h;
36  if ( h != header() ) {
37  addLogErrorEntry( "HEADER IS NOT CORRECT." );
38  return false;
39  }
40  uint v_size;
41  uint f_size;
42  uint e_size;
43  file >> v_size >> f_size >> e_size;
44  data.clear();
45  Geometry::TriangleMesh::PointAttribHandle::Container vertices;
46  Geometry::TriangleMesh::IndexContainerType indices;
47  vertices.resize( v_size );
48  indices.resize( f_size );
49 
50  // Vertices
51  for ( uint i = 0; i < v_size; ++i ) {
52  Vector3 v;
53  file >> v[0] >> v[1] >> v[2];
54  vertices[i] = v;
55  }
56 
57  // Edge
58  for ( uint i = 0; i < e_size; ++i ) {
59  break;
60  }
61 
62  // Triangle
63  for ( uint i = 0; i < f_size; ++i ) {
64  uint side;
65  Vector3ui f;
66  file >> side;
67  if ( side == 3 ) {
68  file >> f[0] >> f[1] >> f[2];
69  indices.push_back( f );
70  }
71  }
72 
73  data.setVertices( std::move( vertices ) );
74  data.setIndices( std::move( indices ) );
75  return true;
76 }
77 
78 bool OFFFileManager::exportData( std::ostream& file, const Geometry::TriangleMesh& data ) {
79  std::string content = "";
80  const uint v_size = data.vertices().size();
81  const uint f_size = data.getIndices().size();
82  const uint e_size = 0;
83 
84  if ( v_size == 0 ) {
85  addLogErrorEntry( "NO VERTICES PRESENT." );
86  return false;
87  }
88 
89  // Header
90  content += header() + "\n" + std::to_string( v_size ) + " " + std::to_string( f_size ) + " " +
91  std::to_string( e_size ) + "\n";
92 
93  // Vertices
94  for ( const auto& v : data.vertices() ) {
95  content += std::to_string( v[0] ) + " " + std::to_string( v[1] ) + " " +
96  std::to_string( v[2] ) + "\n";
97  }
98 
99  // Triangle
100  for ( const auto& f : data.getIndices() ) {
101  content += "3 " + std::to_string( f[0] ) + " " + std::to_string( f[1] ) + " " +
102  std::to_string( f[2] ) + "\n";
103  }
104 
105  file << content;
106 
107  return true;
108 }
109 
110 } // namespace IO
111 } // namespace Ra
std::string fileExtension() const override
INTERFACE.
~OFFFileManager() override
DESTRUCTOR.
std::string header() const
HEADER.
Definition: Cage.cpp:3