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