Developer Documentation

OBJImporter.hh
1 /*===========================================================================*\
2 * *
3 * OpenFlipper *
4 * Copyright (c) 2001-2015, RWTH-Aachen University *
5 * Department of Computer Graphics and Multimedia *
6 * All rights reserved. *
7 * www.openflipper.org *
8 * *
9 *---------------------------------------------------------------------------*
10 * This file is part of OpenFlipper. *
11 *---------------------------------------------------------------------------*
12 * *
13 * Redistribution and use in source and binary forms, with or without *
14 * modification, are permitted provided that the following conditions *
15 * are met: *
16 * *
17 * 1. Redistributions of source code must retain the above copyright notice, *
18 * this list of conditions and the following disclaimer. *
19 * *
20 * 2. Redistributions in binary form must reproduce the above copyright *
21 * notice, this list of conditions and the following disclaimer in the *
22 * documentation and/or other materials provided with the distribution. *
23 * *
24 * 3. Neither the name of the copyright holder nor the names of its *
25 * contributors may be used to endorse or promote products derived from *
26 * this software without specific prior written permission. *
27 * *
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39 * *
40 \*===========================================================================*/
41
42 #pragma once
43
44 #define TEXTUREINDEX "OriginalTexIndexMapping"
45
46 //=== INCLUDES ================================================================
47
48
49 // STL
50 #include <vector>
51
52 // OpenMesh
53 #include <OpenFlipper/common/GlobalDefines.hh>
54 #include <OpenMesh/Core/Geometry/VectorT.hh>
55 #include <ObjectTypes/PolyMesh/PolyMesh.hh>
56 #include <ObjectTypes/TriangleMesh/TriangleMesh.hh>
57 #include <OpenFlipper/common/BaseObject.hh>
58
59
60 #ifdef ENABLE_BSPLINECURVE_SUPPORT
61 #include <ObjectTypes/BSplineCurve/BSplineCurve.hh>
62 #endif
63
64 #ifdef ENABLE_BSPLINESURFACE_SUPPORT
65 #include <ObjectTypes/BSplineSurface/BSplineSurface.hh>
66 #endif
67
68 #include "Material.hh"
69
70 //=== IMPLEMENTATION ==========================================================
71
72 typedef int VertexHandle;
73 typedef int FaceHandle;
74 typedef std::vector<VertexHandle> VHandles;
75 typedef std::vector<OpenMesh::VertexHandle> OMVHandles;
76 typedef OpenMesh::Vec3f Vec3f;
77 typedef OpenMesh::Vec2f Vec2f;
78 typedef OpenMesh::Vec3uc Vec3uc;
79 typedef OpenMesh::Vec4uc Vec4uc;
80
81
82
83 class OBJImporter
84 {
85 public:
86
87 enum ObjectOptionsE
88 {
89 NONE = 0,
90 TRIMESH = 1,
91 POLYMESH = 1 << 1,
92 CURVE = 1 << 2,
93 SURFACE = 1 << 3,
94 NORMALS = 1 << 4,
95 TEXCOORDS = 1 << 5,
96 FACECOLOR = 1 << 6,
97 TEXTURE = 1 << 7,
98 FORCE_NOCOLOR = 1 << 8,
99 FORCE_NONORMALS = 1 << 9,
100 FORCE_NOTEXTURES = 1 << 10
101 };
102
103 typedef unsigned int ObjectOptions;
104
106 OBJImporter() : degreeU_(0),degreeV_(0),currentGroup_(0) {
107 // Add default group
108 addGroup("DefaultGroup");
109 }
110
112 ~OBJImporter();
113
115 VertexHandle addVertex(const Vec3f& _point);
116
118 Vec3f vertex(unsigned int _index);
119
121 int addTexCoord(const Vec2f& _coord);
122
124 int addNormal(const Vec3f& _normal);
125
127 void setDegreeU(int _degree);
128 void setDegreeV(int _degree);
129
131 int degreeU();
132 int degreeV();
133
135 void setObject( BaseObject* _object, int _groupId );
136
145 int currentGroup();
146
148 PolyMesh* currentPolyMesh();
149
151 TriMesh* currentTriMesh();
152
153 #ifdef ENABLE_BSPLINECURVE_SUPPORT
154 BSplineCurve* currentCurve();
156 unsigned int numCurves() { return curvesMap_.size(); }
158 void setCurveGroupId(const unsigned int _count, const int _id);
160 int getCurveGroupId(const unsigned int _count);
162 void setCurveParentId(const int _curveGroup, const int _parentGroup);
164 int getCurveParentId(const int _curveGroup);
165 #endif
166
167 #ifdef ENABLE_BSPLINECURVE_SUPPORT
168 BSplineSurface* currentSurface();
170 unsigned int numSurfaces() { return surfacesMap_.size(); }
172 void setSurfaceGroupId(const unsigned int _count, const int _id);
174 int getSurfaceGroupId(const unsigned int _count);
176 void setSurfaceParentId(const int _surfaceGroup, const int _parentGroup);
178 int getSurfaceParentId(const int _surfaceGroup);
179 #endif
180
182 void addUsedVertices(int _groupId);
183
185 void setVertexTexCoord(VertexHandle _vh, int _texCoordID);
186
188 void setNormal(int _index, int _normalID);
189
191 void addFace(const VHandles& _indices);
192
194 void addFace(const VHandles& _indices, const std::vector<int>& _face_texcoords);
195
196 private:
197 bool addFace(const VHandles& _indices, OpenMesh::FaceHandle &_outFH, std::vector< TriMesh::VertexHandle > &_outTriVertices, std::vector< PolyMesh::VertexHandle > &_outPolyVertices);
198 public:
199
201 void forceMeshType( ObjectOptions _meshType );
202
204 bool hasNormals(int _objectID);
205 bool hasTexture(int _objectID);
206 bool hasTextureCoords(int _objectID);
207 bool isTriangleMesh(int _objectID);
208 bool isPolyMesh(int _objectID);
209 bool isCurve(int _objectID);
210 bool isSurface(int _objectID);
211 bool isNone(int _objectID);
212
214 void setOption( ObjectOptionsE _option);
215
217 void setOption( ObjectOptionsE _option, int _groupId);
218
220 unsigned int n_vertices();
221 unsigned int n_normals();
222 unsigned int n_texCoords();
223
230 unsigned int groupCount();
231
233 BaseObject* object(int _groupId );
234
236 MaterialList& materials();
237
239 void addMaterial(std::string _materialName);
240
242 const std::vector<std::string> usedMaterials(unsigned int _objectID);
243 void useMaterial( std::string _materialName );
244
246 void useVertex(int _vertex_index);
247
249 QString path();
250 void setPath(QString _path);
251
254 void setObjectOptions(ObjectOptions _options);
255
257 bool noOptions();
258
260 bool hasOption( unsigned int _id, ObjectOptions _option );
261
263 void setObjectName(int _objectID, QString _name);
264
265 // Add a new group
266 int addGroup(const QString& _groupName);
267 int groupId(const QString& _groupName) const;
268 unsigned int numGroups() const { return groupNames_.size(); }
269 const QString groupName(const int _grpId) const;
270 void setGroupName(const int _grp, const QString& _name);
271
272 void setCurrentGroup(const int _current);
273 int currentGroup() const;
274
278 void finish();
279
280 private:
281
282 bool vertexListIsManifold(const std::vector<PolyMesh::VertexHandle>& _vertices) const;
283
284 // general data
285 std::vector< Vec3f > vertices_;
286 std::vector< Vec3f > normals_;
287 std::vector< Vec2f > texCoords_;
288
289 //stores half edge normals of the current face
290 std::map<TriMesh::VertexHandle,TriMesh::Normal> storedTriHENormals_;
291 std::map<TriMesh::VertexHandle,PolyMesh::Normal> storedPolyHENormals_;
292
293 int degreeU_;
294 int degreeV_;
295
296 MaterialList materials_;
297
298 QString path_;
299
300 std::vector<QString> groupNames_;
301 int currentGroup_;
302
303 // polyMesh data
304 std::vector<std::map< int, PolyMesh::VertexHandle > > vertexMapPoly_;
305
306 PolyMesh::FaceHandle addedFacePoly_;
307
308 // triMesh data
309 std::vector<std::map< int, TriMesh::VertexHandle > > vertexMapTri_;
310
311 std::vector<std::vector< TriMesh::FaceHandle > > addedFacesTri_;
312
313 //std::vector< BaseObject* > objects_;
314
315 //object data
316 std::vector<TriMeshObject*> triMeshes_;
317 std::vector<PolyMeshObject*> polyMeshes_;
318 #ifdef ENABLE_BSPLINECURVE_SUPPORT
319 std::vector<BSplineCurveObject*> bSplineCurves_;
320 #endif
321 #ifdef ENABLE_BSPLINESURFACE_SUPPORT
322 std::vector<BSplineSurfaceObject*> bSplineSurfaces_;
323 #endif
324
325 std::vector< ObjectOptions > objectOptions_;
326
327 // for each object a vector of materialNames
328 std::vector< std::vector< std::string > > usedMaterials_;
329
330 // for each object a vector of vertex indices
331 // this ensures that a vertex defined first gets a lower index
332 std::vector< std::map< int, VertexHandle > > usedVertices_;
333
334 // Store vertices of invalid faces due to non-manifold
335 // configurations.
336 std::vector< std::vector< OMVHandles > > invalidFaces_;
337
338 #ifdef ENABLE_BSPLINECURVE_SUPPORT
339 std::map<unsigned int, int> curvesMap_;
342 std::map<int, int> curveParentGroupMap_;
343 #endif
344
345 #ifdef ENABLE_BSPLINESURFACE_SUPPORT
346 std::map<unsigned int, int> surfacesMap_;
349 std::map<int, int> surfaceParentGroupMap_;
350 #endif
351
352 };
353
OpenMesh::FaceHandle
Handle for a face entity.
Definition: Handles.hh:141
BSplineSurface.hh
OBJImporter::usedMaterials
const std::vector< std::string > usedMaterials(unsigned int _objectID)
used materials
Definition: OBJImporter.cc:1171
OBJImporter::setObjectOptions
void setObjectOptions(ObjectOptions _options)
Definition: OBJImporter.cc:906
OBJImporter::setNormal
void setNormal(int _index, int _normalID)
set vertex normal
Definition: OBJImporter.cc:305
OBJImporter::addMaterial
void addMaterial(std::string _materialName)
Add a material.
Definition: OBJImporter.cc:566
OBJImporter::vertex
Vec3f vertex(unsigned int _index)
get vertex with given index
Definition: OBJImporter.cc:64
OBJImporter::addFace
void addFace(const VHandles &_indices)
add a face with indices _indices refering to vertices
Definition: OBJImporter.cc:468
BaseObject
Definition: BaseObject.hh:82
BaseObject.hh
OBJImporter::setVertexTexCoord
void setVertexTexCoord(VertexHandle _vh, int _texCoordID)
set vertex texture coordinate
Definition: OBJImporter.cc:260
OpenMesh::TriMesh_ArrayKernelT
Definition: PolyMesh_ArrayKernelT.hh:73
OpenMesh::VectorT< float, 3 >
OBJImporter::useVertex
void useVertex(int _vertex_index)
used vertices
Definition: OBJImporter.cc:1198
OBJImporter::object
BaseObject * object(int _groupId)
return object for the given group
Definition: OBJImporter.cc:869
OBJImporter::currentTriMesh
TriMesh * currentTriMesh()
get a pointer to the active triMesh
Definition: OBJImporter.cc:175
OBJImporter::addTexCoord
int addTexCoord(const Vec2f &_coord)
add texture coordinates
Definition: OBJImporter.cc:75
OpenMesh::PolyMesh_ArrayKernelT
Definition: PolyMesh_ArrayKernelT.hh:94
OBJImporter::setOption
void setOption(ObjectOptionsE _option)
Set Object Option.
Definition: OBJImporter.cc:834
OBJImporter::setDegreeV
void setDegreeV(int _degree)
set degree V direction
Definition: OBJImporter.cc:100
OBJImporter::finish
void finish()
Definition: OBJImporter.cc:1013
OBJImporter::~OBJImporter
~OBJImporter()
base class needs virtual destructor
Definition: OBJImporter.cc:50
OBJImporter::setObjectName
void setObjectName(int _objectID, QString _name)
change the name of an object
Definition: OBJImporter.cc:934
TriangleMesh.hh
OBJImporter::setObject
void setObject(BaseObject *_object, int _groupId)
add an object
Definition: OBJImporter.cc:122
OBJImporter::currentGroup
int currentGroup()
Get the id of the current group.
Definition: OBJImporter.cc:159
ACG::BSplineCurveT
Definition: BSplineCurveT.hh:82
ACG::BSplineSurfaceT
Definition: BSplineSurfaceT.hh:82
PolyMesh.hh
OBJImporter::addUsedVertices
void addUsedVertices(int _groupId)
add all vertices that are used to the mesh (in correct order)
Definition: OBJImporter.cc:205
OBJImporter::addVertex
VertexHandle addVertex(const Vec3f &_point)
add a vertex with coordinate _point
Definition: OBJImporter.cc:57
OBJImporter::hasOption
bool hasOption(unsigned int _id, ObjectOptions _option)
check if object with given id has given option
Definition: OBJImporter.cc:924
OBJImporter::addNormal
int addNormal(const Vec3f &_normal)
add a normal
Definition: OBJImporter.cc:84
OBJImporter::n_vertices
unsigned int n_vertices()
Global Properties.
Definition: OBJImporter.cc:845
OBJImporter::hasNormals
bool hasNormals(int _objectID)
Query Object Options.
Definition: OBJImporter.cc:814
OBJImporter::path
QString path()
Path of the OBJ file.
Definition: OBJImporter.cc:894
OBJImporter::groupCount
unsigned int groupCount()
Number of groups currently stored in the importer.
Definition: OBJImporter.cc:863
BSplineCurve.hh
OBJImporter::forceMeshType
void forceMeshType(ObjectOptions _meshType)
force all meshes to be opened with specific type
Definition: OBJImporter.cc:764
GlobalDefines.hh
OBJImporter
Definition: OBJImporter.hh:83
OBJImporter::degreeU
int degreeU()
get current degree
Definition: OBJImporter.cc:108
OBJImporter::degreeV
int degreeV()
get current degree
Definition: OBJImporter.cc:115
OBJImporter::materials
MaterialList & materials()
return all loaded materials
Definition: OBJImporter.cc:888
OBJImporter::OBJImporter
OBJImporter()
Constructor.
Definition: OBJImporter.hh:106
OBJImporter::noOptions
bool noOptions()
Return true if the importer has no options stored.
Definition: OBJImporter.cc:917
OBJImporter::currentPolyMesh
PolyMesh * currentPolyMesh()
get a pointer to the active polyMesh
Definition: OBJImporter.cc:167
OBJImporter::setDegreeU
void setDegreeU(int _degree)
set degree
Definition: OBJImporter.cc:93