Loading [MathJax]/extensions/TeX/AMSmath.js
Radium Engine  1.6.3
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
OBJFileManager.cpp
1#include <Core/Containers/AlignedStdVector.hpp>
2#include <Core/Containers/VectorArray.hpp>
3#include <Core/Geometry/IndexedGeometry.hpp>
4#include <Core/Types.hpp>
5#include <Core/Utils/Attribs.hpp>
6#include <Eigen/Core>
7#include <IO/deprecated/OBJFileManager.hpp>
8#include <istream>
9#include <utility>
10#include <vector>
11
12namespace Ra {
13
14using namespace Core;
15
16namespace IO {
17
22
27
32 return "obj";
33}
34
35bool OBJFileManager::importData( std::istream& file, Geometry::TriangleMesh& data ) {
36 data.clear();
37 std::string line;
38 Geometry::TriangleMesh::PointAttribHandle::Container vertices;
39 Geometry::TriangleMesh::NormalAttribHandle::Container normals;
40 Geometry::TriangleMesh::IndexContainerType indices;
41 while ( std::getline( file, line ) ) {
42 std::istringstream iss( line );
43 std::string token;
44 iss >> token;
45
46 if ( token == "#" ) { continue; }
47 if ( token == "v" ) {
48 Vector3 v;
49 iss >> v[0] >> v[1] >> v[2];
50 vertices.push_back( v );
51 }
52 if ( token == "vn" ) {
53 Vector3 n;
54 iss >> n[0] >> n[1] >> n[2];
55 normals.push_back( n );
56 }
57 if ( token == "vt" ) { continue; }
58 if ( token == "vp" ) { continue; }
59 if ( token == "f" ) {
60 Vector3ui f;
61 std::string ltoken;
62 int count = 0;
63 // skip first space
64 std::getline( iss, ltoken, ' ' );
65 while ( std::getline( iss, ltoken, ' ' ) ) {
66 std::istringstream ss( ltoken );
67 if ( count < 3 ) {
68 ss >> f[count];
69 f[count] -= 1;
70 }
71 else {
72 addLogErrorEntry( "MESH CONTAINS QUADS." );
73 return false;
74 }
75 count++;
76 }
77 indices.push_back( f );
78 }
79 }
80 if ( vertices.size() == 0 ) {
81 addLogErrorEntry( "MESH IS EMPTY." );
82 return false;
83 }
84
85 data.setVertices( std::move( vertices ) );
86 data.setNormals( std::move( normals ) );
87 data.setIndices( std::move( indices ) );
88 return true;
89}
90
91bool OBJFileManager::exportData( std::ostream& file, const Geometry::TriangleMesh& data ) {
92 std::string content = "";
93 if ( data.vertices().size() == 0 ) {
94 addLogErrorEntry( "MESH IS EMPTY." );
95 return false;
96 }
97 // Vertices
98 for ( const auto& v : data.vertices() ) {
99 content += "v " + std::to_string( v[0] ) + " " + std::to_string( v[1] ) + " " +
100 std::to_string( v[2] ) + "\n";
101 }
102 // Normals
103 for ( const auto& n : data.normals() ) {
104 content += "vn " + std::to_string( n[0] ) + " " + std::to_string( n[1] ) + " " +
105 std::to_string( n[2] ) + "\n";
106 }
107 // Triangle
108 for ( const auto& f : data.getIndices() ) {
109 content += "f " + std::to_string( f[0] + 1 ) + " " + std::to_string( f[1] + 1 ) + " " +
110 std::to_string( f[2] + 1 ) + "\n";
111 }
112 file << content;
113 return true;
114}
115
116} // namespace IO
117} // namespace Ra
void setNormals(PointAttribHandle::Container &&normals)
Set normals.
void setVertices(PointAttribHandle::Container &&vertices)
Set vertices.
void setIndices(IndexContainerType &&indices)
void clear() override
Erases all data, making the AttribArrayGeometry empty.
std::string fileExtension() const override
INTERFACE.
~OBJFileManager() override
DESTRUCTOR.
T count(T... args)
T getline(T... args)
T move(T... args)
@ Geometry
"Geometry" render objects are those loaded using Radium::IO and generated by GeometrySystem
hepler function to manage enum as underlying types in VariableSet
Definition Cage.cpp:4
T to_string(T... args)