OBJImporter.hh 11.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
/*===========================================================================*\
*                                                                            *
*                              OpenFlipper                                   *
*      Copyright (C) 2001-2011 by Computer Graphics Group, RWTH Aachen       *
*                           www.openflipper.org                              *
*                                                                            *
*--------------------------------------------------------------------------- *
*  This file is part of OpenFlipper.                                         *
*                                                                            *
*  OpenFlipper is free software: you can redistribute it and/or modify       *
*  it under the terms of the GNU Lesser General Public License as            *
*  published by the Free Software Foundation, either version 3 of            *
*  the License, or (at your option) any later version with the               *
*  following exceptions:                                                     *
*                                                                            *
*  If other files instantiate templates or use macros                        *
*  or inline functions from this file, or you compile this file and          *
*  link it with other files to produce an executable, this file does         *
*  not by itself cause the resulting executable to be covered by the         *
*  GNU Lesser General Public License. This exception does not however        *
*  invalidate any other reasons why the executable file might be             *
*  covered by the GNU Lesser General Public License.                         *
*                                                                            *
*  OpenFlipper is distributed in the hope that it will be useful,            *
*  but WITHOUT ANY WARRANTY; without even the implied warranty of            *
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
*  GNU Lesser General Public License for more details.                       *
*                                                                            *
*  You should have received a copy of the GNU LesserGeneral Public           *
*  License along with OpenFlipper. If not,                                   *
*  see <http://www.gnu.org/licenses/>.                                       *
*                                                                            *
\*===========================================================================*/

/*===========================================================================*\
*                                                                            *
*   $Revision$                                                       *
*   $LastChangedBy$                                                *
*   $Date$                     *
*                                                                            *
\*===========================================================================*/


#ifndef OBJIMPORTER_HH
#define OBJIMPORTER_HH

#define TEXTUREINDEX "OriginalTexIndexMapping"

//=== INCLUDES ================================================================


// STL
#include <vector>

// OpenMesh
#include <OpenFlipper/common/GlobalDefines.hh>
#include <OpenMesh/Core/Geometry/VectorT.hh>
#include <ObjectTypes/PolyMesh/PolyMesh.hh>
#include <ObjectTypes/TriangleMesh/TriangleMesh.hh>
#include <OpenFlipper/common/BaseObject.hh>


#ifdef ENABLE_BSPLINECURVE_SUPPORT
#include <ObjectTypes/BSplineCurve/BSplineCurve.hh>
#endif

#ifdef ENABLE_BSPLINESURFACE_SUPPORT
#include <ObjectTypes/BSplineSurface/BSplineSurface.hh>
#endif

#include "Material.hh"

//=== IMPLEMENTATION ==========================================================

typedef int VertexHandle;
typedef int FaceHandle;
typedef std::vector<VertexHandle> VHandles;
typedef std::vector<OpenMesh::VertexHandle> OMVHandles;
typedef OpenMesh::Vec3f Vec3f;
typedef OpenMesh::Vec2f Vec2f;
typedef OpenMesh::Vec3uc Vec3uc;
typedef OpenMesh::Vec4uc Vec4uc;



class OBJImporter
{
  public:

    enum ObjectOptionsE
    {
      NONE             = 0,
      TRIMESH          = 1,
      POLYMESH         = 1 << 1,
      CURVE            = 1 << 2,
      SURFACE          = 1 << 3,
      NORMALS          = 1 << 4,
      TEXCOORDS        = 1 << 5,
      FACECOLOR        = 1 << 6,
      TEXTURE          = 1 << 7,
      FORCE_NOCOLOR    = 1 << 8,
      FORCE_NONORMALS  = 1 << 9,
      FORCE_NOTEXTURES = 1 << 10
    };
    
    typedef unsigned int ObjectOptions;

    /// Constructor
    OBJImporter() : degreeU_(0),degreeV_(0),currentGroup_(0) {
        // Add default group
        addGroup("DefaultGroup");
    }

    /// base class needs virtual destructor
    ~OBJImporter();

    /// add a vertex with coordinate \c _point
    VertexHandle addVertex(const Vec3f& _point);

    /// get vertex with given index
    Vec3f vertex(unsigned int _index);
    
    /// add texture coordinates
    int addTexCoord(const Vec2f& _coord);
    
    /// add a normal
    int addNormal(const Vec3f& _normal);
    
    /// set degree
    void setDegreeU(int _degree);
    void setDegreeV(int _degree);
    
    /// get current degree
    int degreeU();
    int degreeV();
    
    /// add an object
    void setObject( BaseObject* _object, int _groupId );
    
    /// get id of the active object
    int currentObject();
    
    /// get a pointer to the active polyMesh
    PolyMesh* currentPolyMesh();
    
    /// get a pointer to the active triMesh
    TriMesh* currentTriMesh();

#ifdef ENABLE_BSPLINECURVE_SUPPORT
    BSplineCurve* currentCurve();
    /// returns the number of curves
    unsigned int numCurves() { return curvesMap_.size(); }
    /// maps the counted curve to a group id
    void setCurveGroupId(const unsigned int _count, const int _id);
    /// get the group id corresponding to the counted curve
    int getCurveGroupId(const unsigned int _count);
    /// sets the parent group id of the curve group
    void setCurveParentId(const int _curveGroup, const int _parentGroup);
    /// get the parent group id of the curve group
    int getCurveParentId(const int _curveGroup);
#endif

#ifdef ENABLE_BSPLINECURVE_SUPPORT
    BSplineSurface* currentSurface();
    /// returns the number of surfaces
    unsigned int numSurfaces() { return surfacesMap_.size(); }
    /// maps the counted surface to a group id
    void setSurfaceGroupId(const unsigned int _count, const int _id);
    /// get the group id corresponding to the counted surface
    int getSurfaceGroupId(const unsigned int _count);
    /// sets the parent group id of the surface group
    void setSurfaceParentId(const int _surfaceGroup, const int _parentGroup);
    /// get the parent group id of the surface group
    int getSurfaceParentId(const int _surfaceGroup);
#endif

    /// add all vertices that are used to the mesh (in correct order)
    void addUsedVertices(int _groupId);
    
    /// set vertex texture coordinate
    void setVertexTexCoord(VertexHandle _vh, int _texCoordID);
    
    /// set vertex normal
    void setNormal(int _index, int _normalID);

    /// add a face with indices _indices refering to vertices
    void addFace(const VHandles& _indices);
    
    /// add face and texture coordinates
    void addFace(const VHandles& _indices, const std::vector<int>& _face_texcoords);
    
  private:
    bool addFace(const VHandles& _indices, OpenMesh::FaceHandle &_outFH, std::vector< TriMesh::VertexHandle > &_outTriVertices, std::vector< PolyMesh::VertexHandle > &_outPolyVertices);
  public:

    /// force all meshes to be opened with specific type
    void forceMeshType( ObjectOptions _meshType );
    
    /// Query Object Options
    bool hasNormals(int _objectID);
    bool hasTexture(int _objectID);
    bool hasTextureCoords(int _objectID);
    bool isTriangleMesh(int _objectID);
    bool isPolyMesh(int _objectID);
    bool isCurve(int _objectID);
    bool isSurface(int _objectID);

    /// Global Properties
    unsigned int n_vertices();
    unsigned int n_normals();
    unsigned int n_texCoords();

    unsigned int objectCount();
    
    /// return object with given index
    BaseObject* object(int _objectID );
    
    /// return all loaded materials
    MaterialList& materials();
    
    /// Add a material
    void addMaterial(std::string _materialName);
    
    /// used materials
    const std::vector<std::string> usedMaterials(unsigned int _objectID);
    void useMaterial( std::string _materialName );
    
    ///used vertices
    void useVertex(int _vertex_index);
    
    /// Path of the OBJ file
    QString path();
    void setPath(QString _path);
    
    /// store an initial options object for an object
    /// containing info about the meshType
    void setObjectOptions(ObjectOptions _options);
    
    /// Object Options for all objects
    std::vector< ObjectOptions >& objectOptions();
    
    /// check if object with given id has given option
    bool hasOption( unsigned int _id, ObjectOptions _option );
    
    /// change the name of an object
    void setObjectName(int _objectID, QString _name);
    
    // Add a new group
    int addGroup(const QString& _groupName);
    int groupId(const QString& _groupName) const;
    unsigned int numGroups() const { return groupNames_.size(); }
    const QString groupName(const int _grpId) const;
    void setGroupName(const int _grp, const QString& _name);

    void setCurrentGroup(const int _current);
    int currentGroup() const;

    /// Finish up importing:
    /// Duplicate vertices of non-manifold faces and
    /// add new face as isolated one
    void finish();

  private:
    
    bool vertexListIsManifold(const std::vector<PolyMesh::VertexHandle>& _vertices) const;

    // general data
    std::vector< Vec3f > vertices_;
    std::vector< Vec3f > normals_;
    std::vector< Vec2f > texCoords_;
    
    //stores half edge normals of the current face
    std::map<TriMesh::VertexHandle,TriMesh::Normal> storedTriHENormals_;
    std::map<TriMesh::VertexHandle,PolyMesh::Normal> storedPolyHENormals_;

    int degreeU_;
    int degreeV_;
    
    MaterialList materials_;
    
    QString path_;
    
    std::vector<QString> groupNames_;
    int currentGroup_;

    // polyMesh data
    std::vector<std::map< int, PolyMesh::VertexHandle > > vertexMapPoly_;
    
    PolyMesh::FaceHandle addedFacePoly_;
    
    // triMesh data
    std::vector<std::map< int, TriMesh::VertexHandle > > vertexMapTri_;
    
    std::vector<std::vector< TriMesh::FaceHandle > > addedFacesTri_;
    
    //std::vector< BaseObject* > objects_;

    //object data
    std::vector<TriMeshObject*> triMeshes_;
    std::vector<PolyMeshObject*> polyMeshes_;
#ifdef ENABLE_BSPLINECURVE_SUPPORT
    std::vector<BSplineCurveObject*> bSplineCurves_;
#endif
#ifdef ENABLE_BSPLINESURFACE_SUPPORT
    std::vector<BSplineSurfaceObject*> bSplineSurfaces_;
#endif

    std::vector< ObjectOptions > objectOptions_;
    
    // for each object a vector of materialNames
    std::vector< std::vector< std::string > > usedMaterials_;
    
    // for each object a vector of vertex indices
    // this ensures that a vertex defined first gets a lower index
    std::vector< std::map< int, VertexHandle > > usedVertices_;

    // Store vertices of invalid faces due to non-manifold
    // configurations.
    std::vector< OMVHandles > invalidFaces_;

#ifdef ENABLE_BSPLINECURVE_SUPPORT
    /// maps each counted curve to a group id
    std::map<unsigned int, int> curvesMap_;
    /// maps each curve group to a parent group id
    std::map<int, int> curveParentGroupMap_;
#endif

#ifdef ENABLE_BSPLINESURFACE_SUPPORT
    /// maps each counted curve to a group id
    std::map<unsigned int, int> surfacesMap_;
    /// maps each surface group  to a parent group id
    std::map<int, int> surfaceParentGroupMap_;
#endif

};

//=============================================================================
#endif // OBJIMPORTER_HH
//=============================================================================