/src/assimp/code/AssetLib/Obj/ObjExporter.h
Line | Count | Source |
1 | | /* |
2 | | Open Asset Import Library (assimp) |
3 | | ---------------------------------------------------------------------- |
4 | | |
5 | | Copyright (c) 2006-2026, assimp team |
6 | | |
7 | | |
8 | | All rights reserved. |
9 | | |
10 | | Redistribution and use of this software in source and binary forms, |
11 | | with or without modification, are permitted provided that the |
12 | | following conditions are met: |
13 | | |
14 | | * Redistributions of source code must retain the above |
15 | | copyright notice, this list of conditions and the |
16 | | following disclaimer. |
17 | | |
18 | | * Redistributions in binary form must reproduce the above |
19 | | copyright notice, this list of conditions and the |
20 | | following disclaimer in the documentation and/or other |
21 | | materials provided with the distribution. |
22 | | |
23 | | * Neither the name of the assimp team, nor the names of its |
24 | | contributors may be used to endorse or promote products |
25 | | derived from this software without specific prior |
26 | | written permission of the assimp team. |
27 | | |
28 | | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
29 | | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
30 | | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
31 | | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
32 | | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
33 | | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
34 | | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
35 | | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
36 | | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
37 | | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
38 | | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
39 | | |
40 | | ---------------------------------------------------------------------- |
41 | | */ |
42 | | |
43 | | /** @file ObjExporter.h |
44 | | * Declares the exporter class to write a scene to a Collada file |
45 | | */ |
46 | | #ifndef AI_OBJEXPORTER_H_INC |
47 | | #define AI_OBJEXPORTER_H_INC |
48 | | |
49 | | #include <assimp/types.h> |
50 | | #include <sstream> |
51 | | #include <vector> |
52 | | #include <map> |
53 | | |
54 | | #include <assimp/Exporter.hpp> |
55 | | |
56 | | struct aiScene; |
57 | | struct aiNode; |
58 | | struct aiMesh; |
59 | | |
60 | | namespace Assimp { |
61 | | |
62 | | // ------------------------------------------------------------------------------------------------ |
63 | | /** Helper class to export a given scene to an OBJ file. */ |
64 | | // ------------------------------------------------------------------------------------------------ |
65 | | class ObjExporter final { |
66 | | public: |
67 | | /// Constructor for a specific scene to export |
68 | | ObjExporter(const char* filename, const aiScene* pScene, bool noMtl=false, const ExportProperties* props = nullptr); |
69 | | ~ObjExporter(); |
70 | | std::string GetMaterialLibName(); |
71 | | std::string GetMaterialLibFileName(); |
72 | | |
73 | | /// public string-streams to write all output into |
74 | | std::ostringstream mOutput, mOutputMat; |
75 | | |
76 | | private: |
77 | | // intermediate data structures |
78 | | struct FaceVertex { |
79 | | FaceVertex() |
80 | | : vp() |
81 | | , vn() |
82 | 0 | , vt() { |
83 | | // empty |
84 | 0 | } |
85 | | |
86 | | // one-based, 0 means: 'does not exist' |
87 | | unsigned int vp, vn, vt; |
88 | | }; |
89 | | |
90 | | struct Face { |
91 | | char kind; |
92 | | std::vector<FaceVertex> indices; |
93 | | }; |
94 | | |
95 | | struct MeshInstance { |
96 | | std::string name, matname; |
97 | | std::vector<Face> faces; |
98 | | }; |
99 | | |
100 | | void WriteHeader(std::ostringstream& out); |
101 | | void WriteMaterialFile(); |
102 | | void WriteGeometryFile(bool noMtl=false, bool merge_identical_vertices = false); |
103 | | std::string GetMaterialName(unsigned int index); |
104 | | void AddMesh(const aiString& name, const aiMesh* m, const aiMatrix4x4& mat, bool merge_identical_vertices); |
105 | | void AddNode(const aiNode* nd, const aiMatrix4x4& mParent, bool merge_identical_vertices); |
106 | | |
107 | | private: |
108 | | std::string filename; |
109 | | const aiScene* const pScene; |
110 | | |
111 | | struct vertexData { |
112 | | aiVector3D vp; |
113 | | aiColor3D vc; // OBJ does not support 4D color |
114 | | uint32_t index = 0; |
115 | | }; |
116 | | |
117 | | std::vector<aiVector3D> vn, vt; |
118 | | std::vector<aiColor4D> vc; |
119 | | std::vector<vertexData> vp; |
120 | | bool useVc; |
121 | | |
122 | | struct vertexDataCompare { |
123 | 0 | bool operator() ( const vertexData& a, const vertexData& b ) const { |
124 | | // position |
125 | 0 | if (a.vp.x < b.vp.x) return true; |
126 | 0 | if (a.vp.x > b.vp.x) return false; |
127 | 0 | if (a.vp.y < b.vp.y) return true; |
128 | 0 | if (a.vp.y > b.vp.y) return false; |
129 | 0 | if (a.vp.z < b.vp.z) return true; |
130 | 0 | if (a.vp.z > b.vp.z) return false; |
131 | | |
132 | | // color |
133 | 0 | if (a.vc.r < b.vc.r) return true; |
134 | 0 | if (a.vc.r > b.vc.r) return false; |
135 | 0 | if (a.vc.g < b.vc.g) return true; |
136 | 0 | if (a.vc.g > b.vc.g) return false; |
137 | 0 | if (a.vc.b < b.vc.b) return true; |
138 | 0 | if (a.vc.b > b.vc.b) return false; |
139 | 0 | return a.index < b.index; |
140 | 0 | } |
141 | | }; |
142 | | |
143 | | struct aiVectorCompare { |
144 | 0 | bool operator() (const aiVector3D& a, const aiVector3D& b) const { |
145 | 0 | if(a.x < b.x) return true; |
146 | 0 | if(a.x > b.x) return false; |
147 | 0 | if(a.y < b.y) return true; |
148 | 0 | if(a.y > b.y) return false; |
149 | 0 | if(a.z < b.z) return true; |
150 | 0 | return false; |
151 | 0 | } |
152 | | }; |
153 | | |
154 | | template <class T, class Compare = std::less<T>> |
155 | | class indexMap { |
156 | | int mNextIndex; |
157 | | typedef std::map<T, int, Compare> dataType; |
158 | | dataType vecMap; |
159 | | |
160 | | public: |
161 | | indexMap() |
162 | 0 | : mNextIndex(1) { |
163 | | // empty |
164 | 0 | } Unexecuted instantiation: Assimp::ObjExporter::indexMap<aiVector3t<float>, Assimp::ObjExporter::aiVectorCompare>::indexMap() Unexecuted instantiation: Assimp::ObjExporter::indexMap<Assimp::ObjExporter::vertexData, Assimp::ObjExporter::vertexDataCompare>::indexMap() |
165 | | |
166 | 0 | int getIndex(const T& key) { |
167 | 0 | typename dataType::iterator vertIt = vecMap.find(key); |
168 | | // vertex already exists, so reference it |
169 | 0 | if(vertIt != vecMap.end()){ |
170 | 0 | return vertIt->second; |
171 | 0 | } |
172 | 0 | return vecMap[key] = mNextIndex++; |
173 | 0 | }; Unexecuted instantiation: Assimp::ObjExporter::indexMap<Assimp::ObjExporter::vertexData, Assimp::ObjExporter::vertexDataCompare>::getIndex(Assimp::ObjExporter::vertexData const&) Unexecuted instantiation: Assimp::ObjExporter::indexMap<aiVector3t<float>, Assimp::ObjExporter::aiVectorCompare>::getIndex(aiVector3t<float> const&) |
174 | | |
175 | 0 | void getKeys( std::vector<T>& keys ) { |
176 | 0 | keys.resize(vecMap.size()); |
177 | 0 | for(typename dataType::iterator it = vecMap.begin(); it != vecMap.end(); ++it){ |
178 | 0 | keys[it->second-1] = it->first; |
179 | 0 | } |
180 | 0 | }; Unexecuted instantiation: Assimp::ObjExporter::indexMap<Assimp::ObjExporter::vertexData, Assimp::ObjExporter::vertexDataCompare>::getKeys(std::__1::vector<Assimp::ObjExporter::vertexData, std::__1::allocator<Assimp::ObjExporter::vertexData> >&) Unexecuted instantiation: Assimp::ObjExporter::indexMap<aiVector3t<float>, Assimp::ObjExporter::aiVectorCompare>::getKeys(std::__1::vector<aiVector3t<float>, std::__1::allocator<aiVector3t<float> > >&) |
181 | | }; |
182 | | |
183 | | indexMap<aiVector3D, aiVectorCompare> mVnMap, mVtMap; |
184 | | indexMap<vertexData, vertexDataCompare> mVpMap; |
185 | | std::vector<MeshInstance> mMeshes; |
186 | | |
187 | | // this endl() doesn't flush() the stream |
188 | | const std::string endl; |
189 | | }; |
190 | | |
191 | | } |
192 | | |
193 | | #endif |