geolib2
export.cpp
Go to the documentation of this file.
1 #include "geolib/io/export.h"
2 
3 #ifdef ASSIMP_VERSION_3
4  #include <assimp/Exporter.hpp>
5  #include <assimp/scene.h>
6  #include <assimp/mesh.h>
7 #else
8  #include <assimp/assimp.hpp>
9  #include <assimp/aiScene.h>
10  #include <assimp/aiMesh.h>
11 #endif
12 
13 #include <console_bridge/console.h>
14 
15 #include <geolib/Shape.h>
16 
17 #include <algorithm>
18 #include <sstream>
19 #include <string>
20 
21 
22 namespace geo {
23 
24 namespace io {
25 
26 // ----------------------------------------------------------------------------------------------------
27 
28 bool writeMeshFile(const std::string& filename, const Shape& shape, std::string format)
29 {
30  aiScene aScene;
31  aScene.mMeshes = new aiMesh*[1];
32  aScene.mMeshes[0] = new aiMesh();
33  auto aMesh = aScene.mMeshes[0];
34  aScene.mNumMeshes = 1;
35 
36  aScene.mMaterials = new aiMaterial*[1];
37  aScene.mMaterials[0] = new aiMaterial;
38  aScene.mNumMaterials = 1;
39  aMesh->mMaterialIndex = 0;
40 
41  aScene.mRootNode = new aiNode;
42  auto aNode = aScene.mRootNode;
43  aNode->mMeshes = new uint[1];
44  aNode->mMeshes[0] = uint(0);
45  aNode->mNumMeshes = 1;
46 
47  // Get mesh and its element from shape
48  geo::Mesh mesh = shape.getMesh();
49  const std::vector<geo::Vector3>& points = mesh.getPoints();
50  const std::vector<geo::TriangleI>& triangleIs = mesh.getTriangleIs();
51 
52  // Transfer points to Assimp mesh
53  aMesh->mVertices = new aiVector3D[ points.size() ];
54  aMesh->mNumVertices = points.size();
55  for (std::vector<Vector3>::const_iterator it = points.cbegin(); it != points.cend(); ++it)
56  {
57  const geo::Vector3& v = *it;
58  aMesh->mVertices[it - points.begin()] = aiVector3D( v.x, v.y, v.z );
59  }
60 
61  // Transfer faces to Assimp mesh
62  aMesh->mFaces = new aiFace[triangleIs.size()];
63  aMesh->mNumFaces = triangleIs.size();
64  for (std::vector<TriangleI>::const_iterator it = triangleIs.cbegin(); it != triangleIs.cend(); ++it)
65  {
66  aiFace& aFace = aMesh->mFaces[it - triangleIs.begin()];
67  aFace.mIndices = new uint[ 3 ];
68  aFace.mNumIndices = 3;
69 
70  aFace.mIndices[0] = it->i1_;
71  aFace.mIndices[1] = it->i2_;
72  aFace.mIndices[2] = it->i3_;
73  }
74 
75  //Check format
76  std::transform(format.begin(), format.end(), format.begin(), ::tolower);
77  if (format.empty())
78  {
79  if (filename.substr(filename.size() - 3) == "3ds")
80  format = "3ds";
81  else if (filename.substr(filename.size() - 3) == "3mf")
82  format = "3mf";
83  else if (filename.substr(filename.size() - 3) == "dae")
84  format = "collada";
85  else if (filename.substr(filename.size() - 3) == "ply")
86  format = "ply";
87  else if (filename.substr(filename.size() - 3) == "obj")
88  format = "obj";
89  else if (filename.substr(filename.size() - 3) == "stp")
90  format = "stp";
91  else if (filename.substr(filename.size() - 3) == "stl")
92  format = "stl";
93  else if (filename.substr(filename.size() - 1) == "x")
94  format = "x";
95  else if (filename.substr(filename.size() - 3) == "x3d")
96  format = "x3d";
97  }
98 
99  Assimp::Exporter aExp;
100  aiReturn result = aExp.Export(&aScene, format, filename);
101  if (result != AI_SUCCESS)
102  {
104  ss << "Error" << std::endl << aExp.GetErrorString() << std::endl;
105  const std::string& str = ss.str();
106  CONSOLE_BRIDGE_logError(str.c_str());
107  return false;
108  }
109  return true;
110 }
111 
112 }
113 
114 }
geo::Vector3::y
const real & y() const
Definition: matrix.h:32
Shape.h
sstream
std::string
geo
Definition: Box.h:6
geo::Mesh::getTriangleIs
const std::vector< TriangleI > & getTriangleIs() const
Definition: Mesh.cpp:46
std::vector< geo::Vector3 >
std::vector::size
T size(T... args)
std::stringstream
geo::io::writeMeshFile
bool writeMeshFile(const std::string &filename, const Shape &shape, std::string format="")
Definition: export.cpp:28
geo::Shape::getMesh
virtual const Mesh & getMesh() const
return the mesh defining the shape
Definition: Shape.cpp:425
algorithm
geo::Mesh::getPoints
const std::vector< geo::Vector3 > & getPoints() const
Definition: Mesh.cpp:42
std::string::c_str
T c_str(T... args)
geo::Vector3
Definition: matrix.h:12
std::transform
T transform(T... args)
geo::Vector3::z
const real & z() const
Definition: matrix.h:33
std::string::substr
T substr(T... args)
std::endl
T endl(T... args)
std::vector::cbegin
T cbegin(T... args)
geo::Vector3::x
const real & x() const
Definition: matrix.h:31
std::stringstream::str
T str(T... args)
std::vector::cend
T cend(T... args)
geo::Mesh
Definition: Mesh.h:25
geo::Shape
A geometric description of a shape.
Definition: Shape.h:19
export.h
string