OpenMesh
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
ImporterT.hh
1 /*===========================================================================*\
2  * *
3  * OpenMesh *
4  * Copyright (C) 2001-2011 by Computer Graphics Group, RWTH Aachen *
5  * www.openmesh.org *
6  * *
7  *---------------------------------------------------------------------------*
8  * This file is part of OpenMesh. *
9  * *
10  * OpenMesh is free software: you can redistribute it and/or modify *
11  * it under the terms of the GNU Lesser General Public License as *
12  * published by the Free Software Foundation, either version 3 of *
13  * the License, or (at your option) any later version with the *
14  * following exceptions: *
15  * *
16  * If other files instantiate templates or use macros *
17  * or inline functions from this file, or you compile this file and *
18  * link it with other files to produce an executable, this file does *
19  * not by itself cause the resulting executable to be covered by the *
20  * GNU Lesser General Public License. This exception does not however *
21  * invalidate any other reasons why the executable file might be *
22  * covered by the GNU Lesser General Public License. *
23  * *
24  * OpenMesh is distributed in the hope that it will be useful, *
25  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
26  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
27  * GNU Lesser General Public License for more details. *
28  * *
29  * You should have received a copy of the GNU LesserGeneral Public *
30  * License along with OpenMesh. If not, *
31  * see <http://www.gnu.org/licenses/>. *
32  * *
33 \*===========================================================================*/
34 
35 /*===========================================================================*\
36  * *
37  * $Revision: 362 $ *
38  * $Date: 2011-01-26 10:21:12 +0100 (Mi, 26 Jan 2011) $ *
39  * *
40 \*===========================================================================*/
41 
42 
43 //=============================================================================
44 //
45 // Implements an importer module for arbitrary OpenMesh meshes
46 //
47 //=============================================================================
48 
49 
50 #ifndef __IMPORTERT_HH__
51 #define __IMPORTERT_HH__
52 
53 
54 //=== INCLUDES ================================================================
55 
56 
57 #include <OpenMesh/Core/IO/importer/BaseImporter.hh>
58 #include <OpenMesh/Core/Utils/vector_cast.hh>
59 #include <OpenMesh/Core/Utils/color_cast.hh>
62 
63 
64 //== NAMESPACES ===============================================================
65 
66 
67 namespace OpenMesh {
68 namespace IO {
69 
70 
71 //=== IMPLEMENTATION ==========================================================
72 
73 
77 template <class Mesh>
78 class ImporterT : public BaseImporter
79 {
80 public:
81 
82  typedef typename Mesh::Point Point;
83  typedef typename Mesh::Normal Normal;
84  typedef typename Mesh::Color Color;
85  typedef typename Mesh::TexCoord2D TexCoord2D;
86  typedef std::vector<VertexHandle> VHandles;
87 
88 
89  ImporterT(Mesh& _mesh) : mesh_(_mesh) {}
90 
91 
92  virtual VertexHandle add_vertex(const Vec3f& _point)
93  {
94  return mesh_.add_vertex(vector_cast<Point>(_point));
95  }
96 
97 
98  virtual FaceHandle add_face(const VHandles& _indices)
99  {
100  FaceHandle fh;
101 
102  if (_indices.size() > 2)
103  {
104  VHandles::const_iterator it, it2, end(_indices.end());
105 
106 
107  // test for valid vertex indices
108  for (it=_indices.begin(); it!=end; ++it)
109  if (! mesh_.is_valid_handle(*it))
110  {
111  omerr() << "ImporterT: Face contains invalid vertex index\n";
112  return fh;
113  }
114 
115 
116  // don't allow double vertices
117  for (it=_indices.begin(); it!=end; ++it)
118  for (it2=it+1; it2!=end; ++it2)
119  if (*it == *it2)
120  {
121  omerr() << "ImporterT: Face has equal vertices\n";
122  failed_faces_.push_back(_indices);
123  return fh;
124  }
125 
126 
127  // try to add face
128  fh = mesh_.add_face(_indices);
129  if (!fh.is_valid())
130  {
131  failed_faces_.push_back(_indices);
132  return fh;
133  }
134  }
135 
136  return fh;
137  }
138 
139  // vertex attributes
140 
141  virtual void set_normal(VertexHandle _vh, const Vec3f& _normal)
142  {
143  if (mesh_.has_vertex_normals())
144  mesh_.set_normal(_vh, vector_cast<Normal>(_normal));
145  }
146 
147  virtual void set_color(VertexHandle _vh, const Vec4uc& _color)
148  {
149  if (mesh_.has_vertex_colors())
150  mesh_.set_color(_vh, color_cast<Color>(_color));
151  }
152 
153  virtual void set_color(VertexHandle _vh, const Vec3uc& _color)
154  {
155  if (mesh_.has_vertex_colors())
156  mesh_.set_color(_vh, color_cast<Color>(_color));
157  }
158 
159 
160  virtual void set_texcoord(VertexHandle _vh, const Vec2f& _texcoord)
161  {
162  if (mesh_.has_vertex_texcoords2D())
163  mesh_.set_texcoord2D(_vh, vector_cast<TexCoord2D>(_texcoord));
164  }
165 
166  virtual void set_texcoord(HalfedgeHandle _heh, const Vec2f& _texcoord)
167  {
168  if (mesh_.has_halfedge_texcoords2D())
169  mesh_.set_texcoord2D(_heh, vector_cast<TexCoord2D>(_texcoord));
170  }
171 
172  // edge attributes
173 
174  virtual void set_color(EdgeHandle _eh, const Vec4uc& _color)
175  {
176  if (mesh_.has_edge_colors())
177  mesh_.set_color(_eh, color_cast<Color>(_color));
178  }
179 
180  virtual void set_color(EdgeHandle _eh, const Vec3uc& _color)
181  {
182  if (mesh_.has_edge_colors())
183  mesh_.set_color(_eh, color_cast<Color>(_color));
184  }
185 
186  // face attributes
187 
188  virtual void set_normal(FaceHandle _fh, const Vec3f& _normal)
189  {
190  if (mesh_.has_face_normals())
191  mesh_.set_normal(_fh, vector_cast<Normal>(_normal));
192  }
193 
194  virtual void set_color(FaceHandle _fh, const Vec3uc& _color)
195  {
196  if (mesh_.has_face_colors())
197  mesh_.set_color(_fh, color_cast<Color>(_color));
198  }
199 
200  virtual void set_color(FaceHandle _fh, const Vec4uc& _color)
201  {
202  if (mesh_.has_face_colors())
203  mesh_.set_color(_fh, color_cast<Color>(_color));
204  }
205 
206  virtual void add_face_texcoords( FaceHandle _fh, VertexHandle _vh, const std::vector<Vec2f>& _face_texcoords)
207  {
208  // get first halfedge handle
209  HalfedgeHandle cur_heh = mesh_.halfedge_handle(_fh);
210  HalfedgeHandle end_heh = mesh_.prev_halfedge_handle(cur_heh);
211 
212  // find start heh
213  while( mesh_.to_vertex_handle(cur_heh) != _vh && cur_heh != end_heh )
214  cur_heh = mesh_.next_halfedge_handle( cur_heh);
215 
216  for(unsigned int i=0; i<_face_texcoords.size(); ++i)
217  {
218  set_texcoord( cur_heh, _face_texcoords[i]);
219  cur_heh = mesh_.next_halfedge_handle( cur_heh);
220  }
221  }
222 
223  virtual void set_face_texindex( FaceHandle _fh, int _texId ) {
224  if ( mesh_.has_face_texture_index() ) {
225  mesh_.set_texture_index(_fh , _texId);
226  }
227  }
228 
229  virtual void add_texture_information( int _id , std::string _name ) {
231 
232  if ( !mesh_.get_property_handle(property,"TextureMapping") ) {
233  mesh_.add_property(property,"TextureMapping");
234  }
235 
236  if ( mesh_.property(property).find( _id ) == mesh_.property(property).end() )
237  mesh_.property(property)[_id] = _name;
238  }
239 
240  // low-level access to mesh
241 
242  virtual BaseKernel* kernel() { return &mesh_; }
243 
244  bool is_triangle_mesh() const
245  { return Mesh::is_triangles(); }
246 
247  void reserve(unsigned int nV, unsigned int nE, unsigned int nF)
248  {
249  mesh_.reserve(nV, nE, nF);
250  }
251 
252  // query number of faces, vertices, normals, texcoords
253  size_t n_vertices() const { return mesh_.n_vertices(); }
254  size_t n_faces() const { return mesh_.n_faces(); }
255  size_t n_edges() const { return mesh_.n_edges(); }
256 
257 
258  void prepare() { failed_faces_.clear(); }
259 
260 
261  void finish()
262  {
263  if (!failed_faces_.empty())
264  {
265  omerr() << failed_faces_.size()
266  << " faces failed, adding them as isolated faces\n";
267 
268  for (unsigned int i=0; i<failed_faces_.size(); ++i)
269  {
270  VHandles& vhandles = failed_faces_[i];
271 
272  // double vertices
273  for (unsigned int j=0; j<vhandles.size(); ++j)
274  {
275  Point p = mesh_.point(vhandles[j]);
276  vhandles[j] = mesh_.add_vertex(p);
277  // DO STORE p, reference may not work since vertex array
278  // may be relocated after adding a new vertex !
279 
280  // Mark vertices of failed face as non-manifold
281  if (mesh_.has_vertex_status()) {
282  mesh_.status(vhandles[j]).set_fixed_nonmanifold(true);
283  }
284  }
285 
286  // add face
287  FaceHandle fh = mesh_.add_face(vhandles);
288 
289  // Mark failed face as non-manifold
290  if (mesh_.has_face_status())
291  mesh_.status(fh).set_fixed_nonmanifold(true);
292 
293  // Mark edges of failed face as non-two-manifold
294  if (mesh_.has_edge_status()) {
295  typename Mesh::FaceEdgeIter fe_it = mesh_.fe_iter(fh);
296  for(; fe_it; ++fe_it) {
297  mesh_.status(fe_it).set_fixed_nonmanifold(true);
298  }
299  }
300  }
301 
302  failed_faces_.clear();
303  }
304  }
305 
306 
307 
308 private:
309 
310  Mesh& mesh_;
311  std::vector<VHandles> failed_faces_;
312 };
313 
314 
315 //=============================================================================
316 } // namespace IO
317 } // namespace OpenMesh
318 //=============================================================================
319 #endif
320 //=============================================================================

acg pic Project OpenMesh, ©  Computer Graphics Group, RWTH Aachen. Documentation generated using doxygen .