Developer Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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 /*===========================================================================*\
43 * *
44 * $Revision$ *
45 * $LastChangedBy$ *
46 * $Date$ *
47 * *
48 \*===========================================================================*/
49 
50 
51 #ifndef OBJIMPORTER_HH
52 #define OBJIMPORTER_HH
53 
54 #define TEXTUREINDEX "OriginalTexIndexMapping"
55 
56 //=== INCLUDES ================================================================
57 
58 
59 // STL
60 #include <vector>
61 
62 // OpenMesh
64 #include <OpenMesh/Core/Geometry/VectorT.hh>
68 
69 
70 #ifdef ENABLE_BSPLINECURVE_SUPPORT
72 #endif
73 
74 #ifdef ENABLE_BSPLINESURFACE_SUPPORT
76 #endif
77 
78 #include "Material.hh"
79 
80 //=== IMPLEMENTATION ==========================================================
81 
82 typedef int VertexHandle;
83 typedef int FaceHandle;
84 typedef std::vector<VertexHandle> VHandles;
85 typedef std::vector<OpenMesh::VertexHandle> OMVHandles;
86 typedef OpenMesh::Vec3f Vec3f;
87 typedef OpenMesh::Vec2f Vec2f;
88 typedef OpenMesh::Vec3uc Vec3uc;
89 typedef OpenMesh::Vec4uc Vec4uc;
90 
91 
92 
94 {
95  public:
96 
97  enum ObjectOptionsE
98  {
99  NONE = 0,
100  TRIMESH = 1,
101  POLYMESH = 1 << 1,
102  CURVE = 1 << 2,
103  SURFACE = 1 << 3,
104  NORMALS = 1 << 4,
105  TEXCOORDS = 1 << 5,
106  FACECOLOR = 1 << 6,
107  TEXTURE = 1 << 7,
108  FORCE_NOCOLOR = 1 << 8,
109  FORCE_NONORMALS = 1 << 9,
110  FORCE_NOTEXTURES = 1 << 10
111  };
112 
113  typedef unsigned int ObjectOptions;
114 
116  OBJImporter() : degreeU_(0),degreeV_(0),currentGroup_(0) {
117  // Add default group
118  addGroup("DefaultGroup");
119  }
120 
122  ~OBJImporter();
123 
125  VertexHandle addVertex(const Vec3f& _point);
126 
128  Vec3f vertex(unsigned int _index);
129 
131  int addTexCoord(const Vec2f& _coord);
132 
134  int addNormal(const Vec3f& _normal);
135 
137  void setDegreeU(int _degree);
138  void setDegreeV(int _degree);
139 
141  int degreeU();
142  int degreeV();
143 
145  void setObject( BaseObject* _object, int _groupId );
146 
148  int currentObject();
149 
151  PolyMesh* currentPolyMesh();
152 
154  TriMesh* currentTriMesh();
155 
156 #ifdef ENABLE_BSPLINECURVE_SUPPORT
157  BSplineCurve* currentCurve();
159  unsigned int numCurves() { return curvesMap_.size(); }
161  void setCurveGroupId(const unsigned int _count, const int _id);
163  int getCurveGroupId(const unsigned int _count);
165  void setCurveParentId(const int _curveGroup, const int _parentGroup);
167  int getCurveParentId(const int _curveGroup);
168 #endif
169 
170 #ifdef ENABLE_BSPLINECURVE_SUPPORT
171  BSplineSurface* currentSurface();
173  unsigned int numSurfaces() { return surfacesMap_.size(); }
175  void setSurfaceGroupId(const unsigned int _count, const int _id);
177  int getSurfaceGroupId(const unsigned int _count);
179  void setSurfaceParentId(const int _surfaceGroup, const int _parentGroup);
181  int getSurfaceParentId(const int _surfaceGroup);
182 #endif
183 
185  void addUsedVertices(int _groupId);
186 
188  void setVertexTexCoord(VertexHandle _vh, int _texCoordID);
189 
191  void setNormal(int _index, int _normalID);
192 
194  void addFace(const VHandles& _indices);
195 
197  void addFace(const VHandles& _indices, const std::vector<int>& _face_texcoords);
198 
199  private:
200  bool addFace(const VHandles& _indices, OpenMesh::FaceHandle &_outFH, std::vector< TriMesh::VertexHandle > &_outTriVertices, std::vector< PolyMesh::VertexHandle > &_outPolyVertices);
201  public:
202 
204  void forceMeshType( ObjectOptions _meshType );
205 
207  bool hasNormals(int _objectID);
208  bool hasTexture(int _objectID);
209  bool hasTextureCoords(int _objectID);
210  bool isTriangleMesh(int _objectID);
211  bool isPolyMesh(int _objectID);
212  bool isCurve(int _objectID);
213  bool isSurface(int _objectID);
214 
216  unsigned int n_vertices();
217  unsigned int n_normals();
218  unsigned int n_texCoords();
219 
220  unsigned int objectCount();
221 
223  BaseObject* object(int _objectID );
224 
226  MaterialList& materials();
227 
229  void addMaterial(std::string _materialName);
230 
232  const std::vector<std::string> usedMaterials(unsigned int _objectID);
233  void useMaterial( std::string _materialName );
234 
236  void useVertex(int _vertex_index);
237 
239  QString path();
240  void setPath(QString _path);
241 
244  void setObjectOptions(ObjectOptions _options);
245 
247  std::vector< ObjectOptions >& objectOptions();
248 
250  bool hasOption( unsigned int _id, ObjectOptions _option );
251 
253  void setObjectName(int _objectID, QString _name);
254 
255  // Add a new group
256  int addGroup(const QString& _groupName);
257  int groupId(const QString& _groupName) const;
258  unsigned int numGroups() const { return groupNames_.size(); }
259  const QString groupName(const int _grpId) const;
260  void setGroupName(const int _grp, const QString& _name);
261 
262  void setCurrentGroup(const int _current);
263  int currentGroup() const;
264 
268  void finish();
269 
270  private:
271 
272  bool vertexListIsManifold(const std::vector<PolyMesh::VertexHandle>& _vertices) const;
273 
274  // general data
275  std::vector< Vec3f > vertices_;
276  std::vector< Vec3f > normals_;
277  std::vector< Vec2f > texCoords_;
278 
279  //stores half edge normals of the current face
280  std::map<TriMesh::VertexHandle,TriMesh::Normal> storedTriHENormals_;
281  std::map<TriMesh::VertexHandle,PolyMesh::Normal> storedPolyHENormals_;
282 
283  int degreeU_;
284  int degreeV_;
285 
286  MaterialList materials_;
287 
288  QString path_;
289 
290  std::vector<QString> groupNames_;
291  int currentGroup_;
292 
293  // polyMesh data
294  std::vector<std::map< int, PolyMesh::VertexHandle > > vertexMapPoly_;
295 
296  PolyMesh::FaceHandle addedFacePoly_;
297 
298  // triMesh data
299  std::vector<std::map< int, TriMesh::VertexHandle > > vertexMapTri_;
300 
301  std::vector<std::vector< TriMesh::FaceHandle > > addedFacesTri_;
302 
303  //std::vector< BaseObject* > objects_;
304 
305  //object data
306  std::vector<TriMeshObject*> triMeshes_;
307  std::vector<PolyMeshObject*> polyMeshes_;
308 #ifdef ENABLE_BSPLINECURVE_SUPPORT
309  std::vector<BSplineCurveObject*> bSplineCurves_;
310 #endif
311 #ifdef ENABLE_BSPLINESURFACE_SUPPORT
312  std::vector<BSplineSurfaceObject*> bSplineSurfaces_;
313 #endif
314 
315  std::vector< ObjectOptions > objectOptions_;
316 
317  // for each object a vector of materialNames
318  std::vector< std::vector< std::string > > usedMaterials_;
319 
320  // for each object a vector of vertex indices
321  // this ensures that a vertex defined first gets a lower index
322  std::vector< std::map< int, VertexHandle > > usedVertices_;
323 
324  // Store vertices of invalid faces due to non-manifold
325  // configurations.
326  std::vector< std::vector< OMVHandles > > invalidFaces_;
327 
328 #ifdef ENABLE_BSPLINECURVE_SUPPORT
329  std::map<unsigned int, int> curvesMap_;
332  std::map<int, int> curveParentGroupMap_;
333 #endif
334 
335 #ifdef ENABLE_BSPLINESURFACE_SUPPORT
336  std::map<unsigned int, int> surfacesMap_;
339  std::map<int, int> surfaceParentGroupMap_;
340 #endif
341 
342 };
343 
344 //=============================================================================
345 #endif // OBJIMPORTER_HH
346 //=============================================================================
void forceMeshType(ObjectOptions _meshType)
force all meshes to be opened with specific type
Definition: OBJImporter.cc:699
QString path()
Path of the OBJ file.
Definition: OBJImporter.cc:806
void addMaterial(std::string _materialName)
Add a material.
Definition: OBJImporter.cc:572
void setObject(BaseObject *_object, int _groupId)
add an object
Definition: OBJImporter.cc:128
~OBJImporter()
base class needs virtual destructor
Definition: OBJImporter.cc:56
void finish()
Definition: OBJImporter.cc:925
std::vector< ObjectOptions > & objectOptions()
Object Options for all objects.
Definition: OBJImporter.cc:829
OBJImporter()
Constructor.
Definition: OBJImporter.hh:116
bool hasOption(unsigned int _id, ObjectOptions _option)
check if object with given id has given option
Definition: OBJImporter.cc:836
PolyMesh * currentPolyMesh()
get a pointer to the active polyMesh
Definition: OBJImporter.cc:173
MaterialList & materials()
return all loaded materials
Definition: OBJImporter.cc:800
bool hasNormals(int _objectID)
Query Object Options.
Definition: OBJImporter.cc:737
void setDegreeV(int _degree)
set degree V direction
Definition: OBJImporter.cc:106
void setDegreeU(int _degree)
set degree
Definition: OBJImporter.cc:99
unsigned int n_vertices()
Global Properties.
Definition: OBJImporter.cc:757
void setVertexTexCoord(VertexHandle _vh, int _texCoordID)
set vertex texture coordinate
Definition: OBJImporter.cc:266
void addFace(const VHandles &_indices)
add a face with indices _indices refering to vertices
Definition: OBJImporter.cc:474
Vec3f vertex(unsigned int _index)
get vertex with given index
Definition: OBJImporter.cc:70
void setNormal(int _index, int _normalID)
set vertex normal
Definition: OBJImporter.cc:311
void addUsedVertices(int _groupId)
add all vertices that are used to the mesh (in correct order)
Definition: OBJImporter.cc:211
VertexHandle addVertex(const Vec3f &_point)
add a vertex with coordinate _point
Definition: OBJImporter.cc:63
Handle for a face entity.
Definition: Handles.hh:146
TriMesh * currentTriMesh()
get a pointer to the active triMesh
Definition: OBJImporter.cc:181
BaseObject * object(int _objectID)
return object with given index
Definition: OBJImporter.cc:781
int currentObject()
get id of the active object
Definition: OBJImporter.cc:165
void setObjectOptions(ObjectOptions _options)
Definition: OBJImporter.cc:818
int addTexCoord(const Vec2f &_coord)
add texture coordinates
Definition: OBJImporter.cc:81
const std::vector< std::string > usedMaterials(unsigned int _objectID)
used materials
int degreeU()
get current degree
Definition: OBJImporter.cc:114
void setObjectName(int _objectID, QString _name)
change the name of an object
Definition: OBJImporter.cc:846
void useVertex(int _vertex_index)
used vertices
int degreeV()
get current degree
Definition: OBJImporter.cc:121
int addNormal(const Vec3f &_normal)
add a normal
Definition: OBJImporter.cc:90