Developer Documentation
ImporterT.hh
1 /* ========================================================================= *
2  * *
3  * OpenMesh *
4  * Copyright (c) 2001-2015, RWTH-Aachen University *
5  * Department of Computer Graphics and Multimedia *
6  * All rights reserved. *
7  * www.openmesh.org *
8  * *
9  *---------------------------------------------------------------------------*
10  * This file is part of OpenMesh. *
11  *---------------------------------------------------------------------------*
12  * *
13  * Redistribution and use in source and binary forms, with or without *
14  * modification, are permitted provided that the following conditions *
15  * are met: *
16  * *
17  * 1. Redistributions of source code must retain the above copyright notice, *
18  * this list of conditions and the following disclaimer. *
19  * *
20  * 2. Redistributions in binary form must reproduce the above copyright *
21  * notice, this list of conditions and the following disclaimer in the *
22  * documentation and/or other materials provided with the distribution. *
23  * *
24  * 3. Neither the name of the copyright holder nor the names of its *
25  * contributors may be used to endorse or promote products derived from *
26  * this software without specific prior written permission. *
27  * *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39  * *
40  * ========================================================================= */
41 
42 
43 
44 
45 //=============================================================================
46 //
47 // Implements an importer module for arbitrary OpenMesh meshes
48 //
49 //=============================================================================
50 
51 
52 #ifndef __IMPORTERT_HH__
53 #define __IMPORTERT_HH__
54 
55 
56 //=== INCLUDES ================================================================
57 
58 
59 #include <OpenMesh/Core/IO/importer/BaseImporter.hh>
60 #include <OpenMesh/Core/Utils/vector_cast.hh>
61 #include <OpenMesh/Core/Utils/color_cast.hh>
64 
65 
66 //== NAMESPACES ===============================================================
67 
68 
69 namespace OpenMesh {
70 namespace IO {
71 
72 
73 //=== IMPLEMENTATION ==========================================================
74 
75 
79 template <class Mesh>
80 class ImporterT : public BaseImporter
81 {
82 public:
83 
84  typedef typename Mesh::Point Point;
85  typedef typename Mesh::Normal Normal;
86  typedef typename Mesh::Color Color;
87  typedef typename Mesh::TexCoord2D TexCoord2D;
88  typedef typename Mesh::TexCoord3D TexCoord3D;
89  typedef std::vector<VertexHandle> VHandles;
90 
91 
92  explicit ImporterT(Mesh& _mesh) : mesh_(_mesh), halfedgeNormals_() {}
93 
94 
95  virtual VertexHandle add_vertex(const Vec3f& _point) override
96  {
97  return mesh_.add_vertex(vector_cast<Point>(_point));
98  }
99 
100  virtual VertexHandle add_vertex() override
101  {
102  return mesh_.new_vertex();
103  }
104 
105  virtual HalfedgeHandle add_edge(VertexHandle _vh0, VertexHandle _vh1) override
106  {
107  return mesh_.new_edge(_vh0, _vh1);
108  }
109 
110  virtual FaceHandle add_face(const VHandles& _indices) override
111  {
112  FaceHandle fh;
113 
114  if (_indices.size() > 2)
115  {
116  VHandles::const_iterator it, it2, end(_indices.end());
117 
118 
119  // test for valid vertex indices
120  for (it=_indices.begin(); it!=end; ++it)
121  if (! mesh_.is_valid_handle(*it))
122  {
123  omerr() << "ImporterT: Face contains invalid vertex index\n";
124  return fh;
125  }
126 
127 
128  // don't allow double vertices
129  for (it=_indices.begin(); it!=end; ++it)
130  for (it2=it+1; it2!=end; ++it2)
131  if (*it == *it2)
132  {
133  omerr() << "ImporterT: Face has equal vertices\n";
134  return fh;
135  }
136 
137 
138  // try to add face
139  fh = mesh_.add_face(_indices);
140  // separate non-manifold faces and mark them
141  if (!fh.is_valid())
142  {
143  VHandles vhandles(_indices.size());
144 
145  // double vertices
146  for (unsigned int j=0; j<_indices.size(); ++j)
147  {
148  // DO STORE p, reference may not work since vertex array
149  // may be relocated after adding a new vertex !
150  Point p = mesh_.point(_indices[j]);
151  vhandles[j] = mesh_.add_vertex(p);
152 
153  // Mark vertices of failed face as non-manifold
154  if (mesh_.has_vertex_status()) {
155  mesh_.status(vhandles[j]).set_fixed_nonmanifold(true);
156  }
157  }
158 
159  // add face
160  fh = mesh_.add_face(vhandles);
161 
162  // Mark failed face as non-manifold
163  if (mesh_.has_face_status())
164  mesh_.status(fh).set_fixed_nonmanifold(true);
165 
166  // Mark edges of failed face as non-two-manifold
167  if (mesh_.has_edge_status()) {
168  typename Mesh::FaceEdgeIter fe_it = mesh_.fe_iter(fh);
169  for(; fe_it.is_valid(); ++fe_it) {
170  mesh_.status(*fe_it).set_fixed_nonmanifold(true);
171  }
172  }
173  }
174 
175  //write the half edge normals
176  if (mesh_.has_halfedge_normals())
177  {
178  //iterate over all incoming haldedges of the added face
179  for (typename Mesh::FaceHalfedgeIter fh_iter = mesh_.fh_begin(fh);
180  fh_iter != mesh_.fh_end(fh); ++fh_iter)
181  {
182  //and write the normals to it
183  typename Mesh::HalfedgeHandle heh = *fh_iter;
184  typename Mesh::VertexHandle vh = mesh_.to_vertex_handle(heh);
185  typename std::map<VertexHandle,Normal>::iterator it_heNs = halfedgeNormals_.find(vh);
186  if (it_heNs != halfedgeNormals_.end())
187  mesh_.set_normal(heh,it_heNs->second);
188  }
189  halfedgeNormals_.clear();
190  }
191  }
192  return fh;
193  }
194 
195  virtual FaceHandle add_face(HalfedgeHandle _heh) override
196  {
197  auto fh = mesh_.new_face();
198  mesh_.set_halfedge_handle(fh, _heh);
199  return fh;
200  }
201 
202  // vertex attributes
203 
204  virtual void set_point(VertexHandle _vh, const Vec3f& _point) override
205  {
206  mesh_.set_point(_vh,vector_cast<Point>(_point));
207  }
208 
209  virtual void set_halfedge(VertexHandle _vh, HalfedgeHandle _heh) override
210  {
211  mesh_.set_halfedge_handle(_vh, _heh);
212  }
213 
214  virtual void set_normal(VertexHandle _vh, const Vec3f& _normal) override
215  {
216  if (mesh_.has_vertex_normals())
217  mesh_.set_normal(_vh, vector_cast<Normal>(_normal));
218 
219  //saves normals for half edges.
220  //they will be written, when the face is added
221  if (mesh_.has_halfedge_normals())
222  halfedgeNormals_[_vh] = vector_cast<Normal>(_normal);
223  }
224 
225  virtual void set_color(VertexHandle _vh, const Vec4uc& _color) override
226  {
227  if (mesh_.has_vertex_colors())
228  mesh_.set_color(_vh, color_cast<Color>(_color));
229  }
230 
231  virtual void set_color(VertexHandle _vh, const Vec3uc& _color) override
232  {
233  if (mesh_.has_vertex_colors())
234  mesh_.set_color(_vh, color_cast<Color>(_color));
235  }
236 
237  virtual void set_color(VertexHandle _vh, const Vec4f& _color) override
238  {
239  if (mesh_.has_vertex_colors())
240  mesh_.set_color(_vh, color_cast<Color>(_color));
241  }
242 
243  virtual void set_color(VertexHandle _vh, const Vec3f& _color) override
244  {
245  if (mesh_.has_vertex_colors())
246  mesh_.set_color(_vh, color_cast<Color>(_color));
247  }
248 
249  virtual void set_texcoord(VertexHandle _vh, const Vec2f& _texcoord) override
250  {
251  if (mesh_.has_vertex_texcoords2D())
252  mesh_.set_texcoord2D(_vh, vector_cast<TexCoord2D>(_texcoord));
253  }
254 
255  virtual void set_status(VertexHandle _vh, const OpenMesh::Attributes::StatusInfo& _status) override
256  {
257  if (!mesh_.has_vertex_status())
258  mesh_.request_vertex_status();
259  mesh_.status(_vh) = _status;
260  }
261 
262  virtual void set_next(HalfedgeHandle _heh, HalfedgeHandle _next) override
263  {
264  mesh_.set_next_halfedge_handle(_heh, _next);
265  }
266 
267  virtual void set_face(HalfedgeHandle _heh, FaceHandle _fh) override
268  {
269  mesh_.set_face_handle(_heh, _fh);
270  }
271 
272 
273  virtual void set_texcoord(HalfedgeHandle _heh, const Vec2f& _texcoord) override
274  {
275  if (mesh_.has_halfedge_texcoords2D())
276  mesh_.set_texcoord2D(_heh, vector_cast<TexCoord2D>(_texcoord));
277  }
278 
279  virtual void set_texcoord(VertexHandle _vh, const Vec3f& _texcoord) override
280  {
281  if (mesh_.has_vertex_texcoords3D())
282  mesh_.set_texcoord3D(_vh, vector_cast<TexCoord3D>(_texcoord));
283  }
284 
285  virtual void set_texcoord(HalfedgeHandle _heh, const Vec3f& _texcoord) override
286  {
287  if (mesh_.has_halfedge_texcoords3D())
288  mesh_.set_texcoord3D(_heh, vector_cast<TexCoord3D>(_texcoord));
289  }
290 
291  virtual void set_status(HalfedgeHandle _heh, const OpenMesh::Attributes::StatusInfo& _status) override
292  {
293  if (!mesh_.has_halfedge_status())
294  mesh_.request_halfedge_status();
295  mesh_.status(_heh) = _status;
296  }
297 
298  // edge attributes
299 
300  virtual void set_color(EdgeHandle _eh, const Vec4uc& _color) override
301  {
302  if (mesh_.has_edge_colors())
303  mesh_.set_color(_eh, color_cast<Color>(_color));
304  }
305 
306  virtual void set_color(EdgeHandle _eh, const Vec3uc& _color) override
307  {
308  if (mesh_.has_edge_colors())
309  mesh_.set_color(_eh, color_cast<Color>(_color));
310  }
311 
312  virtual void set_color(EdgeHandle _eh, const Vec4f& _color) override
313  {
314  if (mesh_.has_edge_colors())
315  mesh_.set_color(_eh, color_cast<Color>(_color));
316  }
317 
318  virtual void set_color(EdgeHandle _eh, const Vec3f& _color) override
319  {
320  if (mesh_.has_edge_colors())
321  mesh_.set_color(_eh, color_cast<Color>(_color));
322  }
323 
324  virtual void set_status(EdgeHandle _eh, const OpenMesh::Attributes::StatusInfo& _status) override
325  {
326  if (!mesh_.has_edge_status())
327  mesh_.request_edge_status();
328  mesh_.status(_eh) = _status;
329  }
330 
331  // face attributes
332 
333  virtual void set_normal(FaceHandle _fh, const Vec3f& _normal) override
334  {
335  if (mesh_.has_face_normals())
336  mesh_.set_normal(_fh, vector_cast<Normal>(_normal));
337  }
338 
339  virtual void set_color(FaceHandle _fh, const Vec3uc& _color) override
340  {
341  if (mesh_.has_face_colors())
342  mesh_.set_color(_fh, color_cast<Color>(_color));
343  }
344 
345  virtual void set_color(FaceHandle _fh, const Vec4uc& _color) override
346  {
347  if (mesh_.has_face_colors())
348  mesh_.set_color(_fh, color_cast<Color>(_color));
349  }
350 
351  virtual void set_color(FaceHandle _fh, const Vec3f& _color) override
352  {
353  if (mesh_.has_face_colors())
354  mesh_.set_color(_fh, color_cast<Color>(_color));
355  }
356 
357  virtual void set_color(FaceHandle _fh, const Vec4f& _color) override
358  {
359  if (mesh_.has_face_colors())
360  mesh_.set_color(_fh, color_cast<Color>(_color));
361  }
362 
363  virtual void set_status(FaceHandle _fh, const OpenMesh::Attributes::StatusInfo& _status) override
364  {
365  if (!mesh_.has_face_status())
366  mesh_.request_face_status();
367  mesh_.status(_fh) = _status;
368  }
369 
370  virtual void add_face_texcoords( FaceHandle _fh, VertexHandle _vh, const std::vector<Vec2f>& _face_texcoords) override
371  {
372  // get first halfedge handle
373  HalfedgeHandle cur_heh = mesh_.halfedge_handle(_fh);
374  HalfedgeHandle end_heh = mesh_.prev_halfedge_handle(cur_heh);
375 
376  // find start heh
377  while( mesh_.to_vertex_handle(cur_heh) != _vh && cur_heh != end_heh )
378  cur_heh = mesh_.next_halfedge_handle( cur_heh);
379 
380  for(unsigned int i=0; i<_face_texcoords.size(); ++i)
381  {
382  set_texcoord( cur_heh, _face_texcoords[i]);
383  cur_heh = mesh_.next_halfedge_handle( cur_heh);
384  }
385  }
386 
387  virtual void add_face_texcoords( FaceHandle _fh, VertexHandle _vh, const std::vector<Vec3f>& _face_texcoords) override
388  {
389  // get first halfedge handle
390  HalfedgeHandle cur_heh = mesh_.halfedge_handle(_fh);
391  HalfedgeHandle end_heh = mesh_.prev_halfedge_handle(cur_heh);
392 
393  // find start heh
394  while( mesh_.to_vertex_handle(cur_heh) != _vh && cur_heh != end_heh )
395  cur_heh = mesh_.next_halfedge_handle( cur_heh);
396 
397  for(unsigned int i=0; i<_face_texcoords.size(); ++i)
398  {
399  set_texcoord( cur_heh, _face_texcoords[i]);
400  cur_heh = mesh_.next_halfedge_handle( cur_heh);
401  }
402  }
403 
404  virtual void set_face_texindex( FaceHandle _fh, int _texId ) override
405  {
406  if ( mesh_.has_face_texture_index() ) {
407  mesh_.set_texture_index(_fh , _texId);
408  }
409  }
410 
411  virtual void add_texture_information( int _id , std::string _name ) override
412  {
414 
415  if ( !mesh_.get_property_handle(property,"TextureMapping") ) {
416  mesh_.add_property(property,"TextureMapping");
417  }
418 
419  if ( mesh_.property(property).find( _id ) == mesh_.property(property).end() )
420  mesh_.property(property)[_id] = _name;
421  }
422 
423  // low-level access to mesh
424 
425  virtual BaseKernel* kernel() override { return &mesh_; }
426 
427  bool is_triangle_mesh() const override
428  { return Mesh::is_triangles(); }
429 
430  void reserve(unsigned int nV, unsigned int nE, unsigned int nF) override
431  {
432  mesh_.reserve(nV, nE, nF);
433  }
434 
435  // query number of faces, vertices, normals, texcoords
436  size_t n_vertices() const override { return mesh_.n_vertices(); }
437  size_t n_faces() const override { return mesh_.n_faces(); }
438  size_t n_edges() const override { return mesh_.n_edges(); }
439 
440 
441  void prepare() override{ }
442 
443 
444  void finish() override { }
445 
446 
447 private:
448 
449  Mesh& mesh_;
450  // stores normals for halfedges of the next face
451  std::map<VertexHandle,Normal> halfedgeNormals_;
452 };
453 
454 
455 //=============================================================================
456 } // namespace IO
457 } // namespace OpenMesh
458 //=============================================================================
459 #endif
460 //=============================================================================
Handle for a edge entity.
Definition: Handles.hh:134
Handle for a face entity.
Definition: Handles.hh:141
Kernel::Color Color
Color type.
Definition: PolyMeshT.hh:116
Kernel::FaceEdgeIter FaceEdgeIter
Circulator.
Definition: PolyMeshT.hh:169
Kernel::Normal Normal
Normal type.
Definition: PolyMeshT.hh:114
Handle for a halfedge entity.
Definition: Handles.hh:127
Kernel::Point Point
Coordinate type.
Definition: PolyMeshT.hh:112
Handle for a vertex entity.
Definition: Handles.hh:120
bool is_valid() const
The handle is valid iff the index is not negative.
Definition: Handles.hh:72
Kernel::TexCoord3D TexCoord3D
TexCoord3D type.
Definition: PolyMeshT.hh:122
Kernel::TexCoord2D TexCoord2D
TexCoord2D type.
Definition: PolyMeshT.hh:120
SmartVertexHandle add_vertex(const Point &_p)
Alias for new_vertex(const Point&).
Definition: PolyMeshT.hh:235
Kernel::FaceHalfedgeIter FaceHalfedgeIter
Circulator.
Definition: PolyMeshT.hh:168
Kernel::VertexHandle VertexHandle
Handle for referencing the corresponding item.
Definition: PolyMeshT.hh:136
SmartVertexHandle new_vertex()
Adds a new default-initialized vertex.
Definition: PolyMeshT.hh:201