- Main Page
- Modules
- Namespaces
- Classes
- Files
- Related Pages

- File List
- File Members

# Core/IO/importer/ImporterT.hh

    00001 /*===========================================================================*\
    00002  *                                                                           *
    00003  *                               OpenMesh                                    *
    00004  *      Copyright (C) 2001-2005 by Computer Graphics Group, RWTH Aachen      *
    00005  *                           www.openmesh.org                                *
    00006  *                                                                           *
    00007  *---------------------------------------------------------------------------*
    00008  *                                                                           *
    00009  *                                License                                    *
    00010  *                                                                           *
    00011  *  This library is free software; you can redistribute it and/or modify it  *
    00012  *  under the terms of the GNU Library General Public License as published   *
    00013  *  by the Free Software Foundation, version 2.                              *
    00014  *                                                                           *
    00015  *  This library is distributed in the hope that it will be useful, but      *
    00016  *  WITHOUT ANY WARRANTY; without even the implied warranty of               *
    00017  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU        *
    00018  *  Library General Public License for more details.                         *
    00019  *                                                                           *
    00020  *  You should have received a copy of the GNU Library General Public        *
    00021  *  License along with this library; if not, write to the Free Software      *
    00022  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.                *
    00023  *                                                                           *
    00024 \*===========================================================================*/
    00025
    00026
    00027 //=============================================================================
    00028 //
    00029 //  Implements an importer module for arbitrary OpenMesh meshes
    00030 //
    00031 //=============================================================================
    00032
    00033
    00034 #ifndef __IMPORTERT_HH__
    00035 #define __IMPORTERT_HH__
    00036
    00037
    00038 //=== INCLUDES ================================================================
    00039
    00040
    00041 #include <OpenMesh/Core/IO/importer/BaseImporter.hh>
    00042 #include <OpenMesh/Core/Utils/vector_cast.hh>
    00043 #include <OpenMesh/Core/Attributes/Attributes.hh>
    00044 #include <OpenMesh/Core/System/omstream.hh>
    00045
    00046
    00047 //== NAMESPACES ===============================================================
    00048
    00049
    00050 namespace OpenMesh {
    00051 namespace IO {
    00052
    00053
    00054 //=== IMPLEMENTATION ==========================================================
    00055
    00056
    00060 template <class Mesh>
    00061 class ImporterT : public BaseImporter
    00062 {
    00063 public:
    00064
    00065   typedef typename Mesh::Point       Point;
    00066   typedef typename Mesh::Normal      Normal;
    00067   typedef typename Mesh::Color       Color;
    00068   typedef typename Mesh::TexCoord2D  TexCoord2D;
    00069   typedef std::vector<VertexHandle>  VHandles;
    00070
    00071
    00072   ImporterT(Mesh& _mesh) : mesh_(_mesh) {}
    00073
    00074
    00075   virtual VertexHandle add_vertex(const Vec3f& _point)
    00076   {
    00077     return mesh_.add_vertex(vector_cast<Point>(_point));
    00078   }
    00079
    00080
    00081   virtual FaceHandle add_face(const VHandles& _indices)
    00082   {
    00083     FaceHandle fh;
    00084
    00085     if (_indices.size() > 2)
    00086     {
    00087       VHandles::const_iterator it, it2, end(_indices.end());
    00088
    00089
    00090       // test for valid vertex indices
    00091       for (it=_indices.begin(); it!=end; ++it)
    00092         if (! mesh_.is_valid_handle(*it))
    00093         {
    00094           omerr() << "ImporterT: Face contains invalid vertex index\n";
    00095           return fh;
    00096         }
    00097
    00098
    00099       // don't allow double vertices
    00100       for (it=_indices.begin(); it!=end; ++it)
    00101         for (it2=it+1; it2!=end; ++it2)
    00102           if (*it == *it2)
    00103           {
    00104             omerr() << "ImporterT: Face has equal vertices\n";
    00105             failed_faces_.push_back(_indices);
    00106             return fh;
    00107           }
    00108
    00109
    00110       // try to add face
    00111       fh = mesh_.add_face(_indices);
    00112       if (!fh.is_valid())
    00113       {
    00114         failed_faces_.push_back(_indices);
    00115         return fh;
    00116       }
    00117     }
    00118
    00119     return fh;
    00120   }
    00121
    00122
    00123   // vertex attributes
    00124
    00125   virtual void set_normal(VertexHandle _vh, const Vec3f& _normal)
    00126   {
    00127     if (mesh_.has_vertex_normals())
    00128       mesh_.set_normal(_vh, vector_cast<Normal>(_normal));
    00129   }
    00130
    00131
    00132   virtual void set_color(VertexHandle _vh, const Vec3uc& _color)
    00133   {
    00134     if (mesh_.has_vertex_colors())
    00135       mesh_.set_color(_vh, vector_cast<Color>(_color));
    00136   }
    00137
    00138
    00139   virtual void set_texcoord(VertexHandle _vh, const Vec2f& _texcoord)
    00140   {
    00141     if (mesh_.has_vertex_texcoords2D())
    00142     mesh_.set_texcoord2D(_vh, vector_cast<TexCoord2D>(_texcoord));
    00143   }
    00144
    00145
    00146   // face attributes
    00147
    00148   virtual void set_normal(FaceHandle _fh, const Vec3f& _normal)
    00149   {
    00150     if (mesh_.has_face_normals())
    00151       mesh_.set_normal(_fh, vector_cast<Normal>(_normal));
    00152   }
    00153
    00154   virtual void set_color(FaceHandle _fh, const Vec3uc& _color)
    00155   {
    00156     if (mesh_.has_face_colors())
    00157       mesh_.set_color(_fh, vector_cast<Color>(_color));
    00158   }
    00159
    00160
    00161   // low-level access to mesh
    00162
    00163   virtual BaseKernel* kernel() { return &mesh_; }
    00164
    00165   bool is_triangle_mesh() const
    00166   {
    00167     typedef typename Mesh::Face Face; // must use typedef for gcc 2.95.x
    00168     return Face::is_triangle();
    00169   }
    00170
    00171   void reserve(unsigned int nV, unsigned int nE, unsigned int nF)
    00172   {
    00173     mesh_.reserve(nV, nE, nF);
    00174   }
    00175
    00176   // query number of faces, vertices, normals, texcoords
    00177   size_t n_vertices()  const { return mesh_.n_vertices(); }
    00178   size_t n_faces()     const { return mesh_.n_faces(); }
    00179   size_t n_edges()     const { return mesh_.n_edges(); }
    00180
    00181
    00182   void prepare() { failed_faces_.clear(); }
    00183
    00184
    00185   void finish()
    00186   {
    00187     if (!failed_faces_.empty())
    00188     {
    00189       omerr() << failed_faces_.size()
    00190             << " faces failed, adding them as isolated faces\n";
    00191
    00192       for (unsigned int i=0; i<failed_faces_.size(); ++i)
    00193       {
    00194         VHandles&  vhandles = failed_faces_[i];
    00195
    00196         // double vertices
    00197         for (unsigned int j=0; j<vhandles.size(); ++j)
    00198         {
    00199           Point p = mesh_.point(vhandles[j]);
    00200           vhandles[j] = mesh_.add_vertex(p);
    00201           // DO STORE p, reference may not work since vertex array
    00202           // may be relocated after adding a new vertex !
    00203         }
    00204
    00205         // add face
    00206         mesh_.add_face(vhandles);
    00207       }
    00208
    00209       failed_faces_.clear();
    00210     }
    00211   }
    00212
    00213
    00214
    00215 private:
    00216
    00217   Mesh& mesh_;
    00218   std::vector<VHandles>  failed_faces_;
    00219 };
    00220
    00221
    00222 //=============================================================================
    00223 } // namespace IO
    00224 } // namespace OpenMesh
    00225 //=============================================================================
    00226 #endif
    00227 //=============================================================================