00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 #ifndef __IMPORTERT_HH__
00035 #define __IMPORTERT_HH__
00036
00037
00038
00039
00040
00041 #include <OpenMesh/Core/IO/importer/BaseImporter.hh>
00042 #include <OpenMesh/Core/Utils/vector_cast.hh>
00043 #include <OpenMesh/Core/Mesh/Attributes.hh>
00044 #include <OpenMesh/Core/System/omstream.hh>
00045
00046
00047
00048
00049
00050 namespace OpenMesh {
00051 namespace IO {
00052
00053
00054
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
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
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
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
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
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
00162
00163 virtual BaseKernel* kernel() { return &mesh_; }
00164
00165 bool is_triangle_mesh() const
00166 { return Mesh::is_triangles(); }
00167
00168 void reserve(unsigned int nV, unsigned int nE, unsigned int nF)
00169 {
00170 mesh_.reserve(nV, nE, nF);
00171 }
00172
00173
00174 size_t n_vertices() const { return mesh_.n_vertices(); }
00175 size_t n_faces() const { return mesh_.n_faces(); }
00176 size_t n_edges() const { return mesh_.n_edges(); }
00177
00178
00179 void prepare() { failed_faces_.clear(); }
00180
00181
00182 void finish()
00183 {
00184 if (!failed_faces_.empty())
00185 {
00186 omerr() << failed_faces_.size()
00187 << " faces failed, adding them as isolated faces\n";
00188
00189 for (unsigned int i=0; i<failed_faces_.size(); ++i)
00190 {
00191 VHandles& vhandles = failed_faces_[i];
00192
00193
00194 for (unsigned int j=0; j<vhandles.size(); ++j)
00195 {
00196 Point p = mesh_.point(vhandles[j]);
00197 vhandles[j] = mesh_.add_vertex(p);
00198
00199
00200 }
00201
00202
00203 mesh_.add_face(vhandles);
00204 }
00205
00206 failed_faces_.clear();
00207 }
00208 }
00209
00210
00211
00212 private:
00213
00214 Mesh& mesh_;
00215 std::vector<VHandles> failed_faces_;
00216 };
00217
00218
00219
00220 }
00221 }
00222
00223 #endif
00224