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
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050 #ifndef __IMPORTERT_HH__
00051 #define __IMPORTERT_HH__
00052
00053
00054
00055
00056
00057 #include <OpenMesh/Core/IO/importer/BaseImporter.hh>
00058 #include <OpenMesh/Core/Utils/vector_cast.hh>
00059 #include <OpenMesh/Core/Utils/color_cast.hh>
00060 #include <OpenMesh/Core/Mesh/Attributes.hh>
00061 #include <OpenMesh/Core/System/omstream.hh>
00062
00063
00064
00065
00066
00067 namespace OpenMesh {
00068 namespace IO {
00069
00070
00071
00072
00073
00077 template <class Mesh>
00078 class ImporterT : public BaseImporter
00079 {
00080 public:
00081
00082 typedef typename Mesh::Point Point;
00083 typedef typename Mesh::Normal Normal;
00084 typedef typename Mesh::Color Color;
00085 typedef typename Mesh::TexCoord2D TexCoord2D;
00086 typedef std::vector<VertexHandle> VHandles;
00087
00088
00089 ImporterT(Mesh& _mesh) : mesh_(_mesh) {}
00090
00091
00092 virtual VertexHandle add_vertex(const Vec3f& _point)
00093 {
00094 return mesh_.add_vertex(vector_cast<Point>(_point));
00095 }
00096
00097
00098 virtual FaceHandle add_face(const VHandles& _indices)
00099 {
00100 FaceHandle fh;
00101
00102 if (_indices.size() > 2)
00103 {
00104 VHandles::const_iterator it, it2, end(_indices.end());
00105
00106
00107
00108 for (it=_indices.begin(); it!=end; ++it)
00109 if (! mesh_.is_valid_handle(*it))
00110 {
00111 omerr() << "ImporterT: Face contains invalid vertex index\n";
00112 return fh;
00113 }
00114
00115
00116
00117 for (it=_indices.begin(); it!=end; ++it)
00118 for (it2=it+1; it2!=end; ++it2)
00119 if (*it == *it2)
00120 {
00121 omerr() << "ImporterT: Face has equal vertices\n";
00122 failed_faces_.push_back(_indices);
00123 return fh;
00124 }
00125
00126
00127
00128 fh = mesh_.add_face(_indices);
00129 if (!fh.is_valid())
00130 {
00131 failed_faces_.push_back(_indices);
00132 return fh;
00133 }
00134 }
00135
00136 return fh;
00137 }
00138
00139
00140
00141 virtual void set_normal(VertexHandle _vh, const Vec3f& _normal)
00142 {
00143 if (mesh_.has_vertex_normals())
00144 mesh_.set_normal(_vh, vector_cast<Normal>(_normal));
00145 }
00146
00147 virtual void set_color(VertexHandle _vh, const Vec4uc& _color)
00148 {
00149 if (mesh_.has_vertex_colors())
00150 mesh_.set_color(_vh, color_cast<Color>(_color));
00151 }
00152
00153 virtual void set_color(VertexHandle _vh, const Vec3uc& _color)
00154 {
00155 if (mesh_.has_vertex_colors())
00156 mesh_.set_color(_vh, color_cast<Color>(_color));
00157 }
00158
00159
00160 virtual void set_texcoord(VertexHandle _vh, const Vec2f& _texcoord)
00161 {
00162 if (mesh_.has_vertex_texcoords2D())
00163 mesh_.set_texcoord2D(_vh, vector_cast<TexCoord2D>(_texcoord));
00164 }
00165
00166 virtual void set_texcoord(HalfedgeHandle _heh, const Vec2f& _texcoord)
00167 {
00168 if (mesh_.has_halfedge_texcoords2D())
00169 mesh_.set_texcoord2D(_heh, vector_cast<TexCoord2D>(_texcoord));
00170 }
00171
00172
00173
00174
00175 virtual void set_normal(FaceHandle _fh, const Vec3f& _normal)
00176 {
00177 if (mesh_.has_face_normals())
00178 mesh_.set_normal(_fh, vector_cast<Normal>(_normal));
00179 }
00180
00181 virtual void set_color(FaceHandle _fh, const Vec3uc& _color)
00182 {
00183 if (mesh_.has_face_colors())
00184 mesh_.set_color(_fh, color_cast<Color>(_color));
00185 }
00186
00187 virtual void set_color(FaceHandle _fh, const Vec4uc& _color)
00188 {
00189 if (mesh_.has_face_colors())
00190 mesh_.set_color(_fh, color_cast<Color>(_color));
00191 }
00192
00193 virtual void add_face_texcoords( FaceHandle _fh, VertexHandle _vh, const std::vector<Vec2f>& _face_texcoords)
00194 {
00195
00196 HalfedgeHandle cur_heh = mesh_.halfedge_handle(_fh);
00197 HalfedgeHandle end_heh = mesh_.prev_halfedge_handle(cur_heh);
00198
00199
00200 while( mesh_.to_vertex_handle(cur_heh) != _vh && cur_heh != end_heh )
00201 cur_heh = mesh_.next_halfedge_handle( cur_heh);
00202
00203 for(unsigned int i=0; i<_face_texcoords.size(); ++i)
00204 {
00205 set_texcoord( cur_heh, _face_texcoords[i]);
00206 cur_heh = mesh_.next_halfedge_handle( cur_heh);
00207 }
00208 }
00209
00210 virtual void set_face_texindex( FaceHandle _fh, int _texId ) {
00211 if ( mesh_.has_face_texture_index() ) {
00212 mesh_.set_texture_index(_fh , _texId);
00213 }
00214 }
00215
00216 virtual void add_texture_information( int _id , std::string _name ) {
00217 OpenMesh::MPropHandleT< std::map< int, std::string > > property;
00218
00219 if ( !mesh_.get_property_handle(property,"TextureMapping") ) {
00220 mesh_.add_property(property,"TextureMapping");
00221 }
00222
00223 if ( mesh_.property(property).find( _id ) == mesh_.property(property).end() )
00224 mesh_.property(property)[_id] = _name;
00225 }
00226
00227
00228
00229 virtual BaseKernel* kernel() { return &mesh_; }
00230
00231 bool is_triangle_mesh() const
00232 { return Mesh::is_triangles(); }
00233
00234 void reserve(unsigned int nV, unsigned int nE, unsigned int nF)
00235 {
00236 mesh_.reserve(nV, nE, nF);
00237 }
00238
00239
00240 size_t n_vertices() const { return mesh_.n_vertices(); }
00241 size_t n_faces() const { return mesh_.n_faces(); }
00242 size_t n_edges() const { return mesh_.n_edges(); }
00243
00244
00245 void prepare() { failed_faces_.clear(); }
00246
00247
00248 void finish()
00249 {
00250 if (!failed_faces_.empty())
00251 {
00252 omerr() << failed_faces_.size()
00253 << " faces failed, adding them as isolated faces\n";
00254
00255 for (unsigned int i=0; i<failed_faces_.size(); ++i)
00256 {
00257 VHandles& vhandles = failed_faces_[i];
00258
00259
00260 for (unsigned int j=0; j<vhandles.size(); ++j)
00261 {
00262 Point p = mesh_.point(vhandles[j]);
00263 vhandles[j] = mesh_.add_vertex(p);
00264
00265
00266 }
00267
00268
00269 mesh_.add_face(vhandles);
00270 }
00271
00272 failed_faces_.clear();
00273 }
00274 }
00275
00276
00277
00278 private:
00279
00280 Mesh& mesh_;
00281 std::vector<VHandles> failed_faces_;
00282 };
00283
00284
00285
00286 }
00287 }
00288
00289 #endif
00290