OpenMesh
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
ImporterT.hh
1 /*===========================================================================*\
2  * *
3  * OpenMesh *
4  * Copyright (C) 2001-2014 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: 990 $ *
38  * $Date: 2014-02-05 10:01:07 +0100 (Mi, 05 Feb 2014) $ *
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), halfedgeNormals_() {}
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  //write the half edge normals
136  if (mesh_.has_halfedge_normals())
137  {
138  //iterate over all incoming haldedges of the added face
139  for (typename Mesh::FaceHalfedgeIter fh_iter = mesh_.fh_begin(fh);
140  fh_iter != mesh_.fh_end(fh); ++fh_iter)
141  {
142  //and write the normals to it
143  typename Mesh::HalfedgeHandle heh = *fh_iter;
144  typename Mesh::VertexHandle vh = mesh_.to_vertex_handle(heh);
145  typename std::map<VertexHandle,Normal>::iterator it_heNs = halfedgeNormals_.find(vh);
146  if (it_heNs != halfedgeNormals_.end())
147  mesh_.set_normal(heh,it_heNs->second);
148  }
149  halfedgeNormals_.clear();
150  }
151  }
152  return fh;
153  }
154 
155  // vertex attributes
156 
157  virtual void set_normal(VertexHandle _vh, const Vec3f& _normal)
158  {
159  if (mesh_.has_vertex_normals())
160  mesh_.set_normal(_vh, vector_cast<Normal>(_normal));
161 
162  //saves normals for half edges.
163  //they will be written, when the face is added
164  if (mesh_.has_halfedge_normals())
165  halfedgeNormals_[_vh] = vector_cast<Normal>(_normal);
166  }
167 
168  virtual void set_color(VertexHandle _vh, const Vec4uc& _color)
169  {
170  if (mesh_.has_vertex_colors())
171  mesh_.set_color(_vh, color_cast<Color>(_color));
172  }
173 
174  virtual void set_color(VertexHandle _vh, const Vec3uc& _color)
175  {
176  if (mesh_.has_vertex_colors())
177  mesh_.set_color(_vh, color_cast<Color>(_color));
178  }
179 
180  virtual void set_color(VertexHandle _vh, const Vec4f& _color)
181  {
182  if (mesh_.has_vertex_colors())
183  mesh_.set_color(_vh, color_cast<Color>(_color));
184  }
185 
186  virtual void set_color(VertexHandle _vh, const Vec3f& _color)
187  {
188  if (mesh_.has_vertex_colors())
189  mesh_.set_color(_vh, color_cast<Color>(_color));
190  }
191 
192  virtual void set_texcoord(VertexHandle _vh, const Vec2f& _texcoord)
193  {
194  if (mesh_.has_vertex_texcoords2D())
195  mesh_.set_texcoord2D(_vh, vector_cast<TexCoord2D>(_texcoord));
196  }
197 
198  virtual void set_texcoord(HalfedgeHandle _heh, const Vec2f& _texcoord)
199  {
200  if (mesh_.has_halfedge_texcoords2D())
201  mesh_.set_texcoord2D(_heh, vector_cast<TexCoord2D>(_texcoord));
202  }
203 
204  // edge attributes
205 
206  virtual void set_color(EdgeHandle _eh, const Vec4uc& _color)
207  {
208  if (mesh_.has_edge_colors())
209  mesh_.set_color(_eh, color_cast<Color>(_color));
210  }
211 
212  virtual void set_color(EdgeHandle _eh, const Vec3uc& _color)
213  {
214  if (mesh_.has_edge_colors())
215  mesh_.set_color(_eh, color_cast<Color>(_color));
216  }
217 
218  virtual void set_color(EdgeHandle _eh, const Vec4f& _color)
219  {
220  if (mesh_.has_edge_colors())
221  mesh_.set_color(_eh, color_cast<Color>(_color));
222  }
223 
224  virtual void set_color(EdgeHandle _eh, const Vec3f& _color)
225  {
226  if (mesh_.has_edge_colors())
227  mesh_.set_color(_eh, color_cast<Color>(_color));
228  }
229 
230  // face attributes
231 
232  virtual void set_normal(FaceHandle _fh, const Vec3f& _normal)
233  {
234  if (mesh_.has_face_normals())
235  mesh_.set_normal(_fh, vector_cast<Normal>(_normal));
236  }
237 
238  virtual void set_color(FaceHandle _fh, const Vec3uc& _color)
239  {
240  if (mesh_.has_face_colors())
241  mesh_.set_color(_fh, color_cast<Color>(_color));
242  }
243 
244  virtual void set_color(FaceHandle _fh, const Vec4uc& _color)
245  {
246  if (mesh_.has_face_colors())
247  mesh_.set_color(_fh, color_cast<Color>(_color));
248  }
249 
250  virtual void set_color(FaceHandle _fh, const Vec3f& _color)
251  {
252  if (mesh_.has_face_colors())
253  mesh_.set_color(_fh, color_cast<Color>(_color));
254  }
255 
256  virtual void set_color(FaceHandle _fh, const Vec4f& _color)
257  {
258  if (mesh_.has_face_colors())
259  mesh_.set_color(_fh, color_cast<Color>(_color));
260  }
261 
262  virtual void add_face_texcoords( FaceHandle _fh, VertexHandle _vh, const std::vector<Vec2f>& _face_texcoords)
263  {
264  // get first halfedge handle
265  HalfedgeHandle cur_heh = mesh_.halfedge_handle(_fh);
266  HalfedgeHandle end_heh = mesh_.prev_halfedge_handle(cur_heh);
267 
268  // find start heh
269  while( mesh_.to_vertex_handle(cur_heh) != _vh && cur_heh != end_heh )
270  cur_heh = mesh_.next_halfedge_handle( cur_heh);
271 
272  for(unsigned int i=0; i<_face_texcoords.size(); ++i)
273  {
274  set_texcoord( cur_heh, _face_texcoords[i]);
275  cur_heh = mesh_.next_halfedge_handle( cur_heh);
276  }
277  }
278 
279  virtual void set_face_texindex( FaceHandle _fh, int _texId ) {
280  if ( mesh_.has_face_texture_index() ) {
281  mesh_.set_texture_index(_fh , _texId);
282  }
283  }
284 
285  virtual void add_texture_information( int _id , std::string _name ) {
287 
288  if ( !mesh_.get_property_handle(property,"TextureMapping") ) {
289  mesh_.add_property(property,"TextureMapping");
290  }
291 
292  if ( mesh_.property(property).find( _id ) == mesh_.property(property).end() )
293  mesh_.property(property)[_id] = _name;
294  }
295 
296  // low-level access to mesh
297 
298  virtual BaseKernel* kernel() { return &mesh_; }
299 
300  bool is_triangle_mesh() const
301  { return Mesh::is_triangles(); }
302 
303  void reserve(unsigned int nV, unsigned int nE, unsigned int nF)
304  {
305  mesh_.reserve(nV, nE, nF);
306  }
307 
308  // query number of faces, vertices, normals, texcoords
309  size_t n_vertices() const { return mesh_.n_vertices(); }
310  size_t n_faces() const { return mesh_.n_faces(); }
311  size_t n_edges() const { return mesh_.n_edges(); }
312 
313 
314  void prepare() { failed_faces_.clear(); }
315 
316 
317  void finish()
318  {
319  if (!failed_faces_.empty())
320  {
321  omerr() << failed_faces_.size()
322  << " faces failed, adding them as isolated faces\n";
323 
324  for (unsigned int i=0; i<failed_faces_.size(); ++i)
325  {
326  VHandles& vhandles = failed_faces_[i];
327 
328  // double vertices
329  for (unsigned int j=0; j<vhandles.size(); ++j)
330  {
331  Point p = mesh_.point(vhandles[j]);
332  vhandles[j] = mesh_.add_vertex(p);
333  // DO STORE p, reference may not work since vertex array
334  // may be relocated after adding a new vertex !
335 
336  // Mark vertices of failed face as non-manifold
337  if (mesh_.has_vertex_status()) {
338  mesh_.status(vhandles[j]).set_fixed_nonmanifold(true);
339  }
340  }
341 
342  // add face
343  FaceHandle fh = mesh_.add_face(vhandles);
344 
345  // Mark failed face as non-manifold
346  if (mesh_.has_face_status())
347  mesh_.status(fh).set_fixed_nonmanifold(true);
348 
349  // Mark edges of failed face as non-two-manifold
350  if (mesh_.has_edge_status()) {
351  typename Mesh::FaceEdgeIter fe_it = mesh_.fe_iter(fh);
352  for(; fe_it.is_valid(); ++fe_it) {
353  mesh_.status(*fe_it).set_fixed_nonmanifold(true);
354  }
355  }
356  }
357 
358  failed_faces_.clear();
359  }
360  }
361 
362 
363 
364 private:
365 
366  Mesh& mesh_;
367  std::vector<VHandles> failed_faces_;
368  // stores normals for halfedges of the next face
369  std::map<VertexHandle,Normal> halfedgeNormals_;
370 };
371 
372 
373 //=============================================================================
374 } // namespace IO
375 } // namespace OpenMesh
376 //=============================================================================
377 #endif
378 //=============================================================================
Add colors to mesh item (vertices/faces/edges)
Definition: Attributes.hh:81
bool is_valid() const
The handle is valid iff the index is not equal to -1.
Definition: Handles.hh:70
Base class for importer modules.
Definition: BaseImporter.hh:81
Add 2D texture coordinates (vertices, halfedges)
Definition: Attributes.hh:85
Contains all the mesh ingredients like the polygonal mesh, the triangle mesh, different mesh kernels ...
Definition: MeshItems.hh:56
Handle for a halfedge entity.
Definition: Handles.hh:121
This class template provides an importer module for OpenMesh meshes.
Definition: ImporterT.hh:78
This file provides some macros containing attribute usage.
Handle for a edge entity.
Definition: Handles.hh:128
Handle representing a mesh property.
Definition: Property.hh:532
Handle for a face entity.
Definition: Handles.hh:135
This class provides the basic property management like adding/removing properties and access to prope...
Definition: BaseKernel.hh:91
This file provides the streams omlog, omout, and omerr.
Handle for a vertex entity.
Definition: Handles.hh:114
Add normals to mesh item (vertices/faces)
Definition: Attributes.hh:80

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