OpenMesh
ExporterT.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  * $Revision$ *
45  * $Date$ *
46  * *
47 \*===========================================================================*/
48 
49 
50 //=============================================================================
51 //
52 // Implements an exporter module for arbitrary OpenMesh meshes
53 //
54 //=============================================================================
55 
56 
57 #ifndef __EXPORTERT_HH__
58 #define __EXPORTERT_HH__
59 
60 
61 //=== INCLUDES ================================================================
62 
63 // C++
64 #include <vector>
65 
66 // OpenMesh
67 #include <OpenMesh/Core/System/config.h>
68 #include <OpenMesh/Core/Geometry/VectorT.hh>
69 #include <OpenMesh/Core/Utils/GenProg.hh>
70 #include <OpenMesh/Core/Utils/vector_cast.hh>
71 #include <OpenMesh/Core/Utils/color_cast.hh>
72 #include <OpenMesh/Core/IO/exporter/BaseExporter.hh>
73 
74 
75 //=== NAMESPACES ==============================================================
76 
77 namespace OpenMesh {
78 namespace IO {
79 
80 
81 //=== EXPORTER CLASS ==========================================================
82 
86 template <class Mesh>
87 class ExporterT : public BaseExporter
88 {
89 public:
90 
91  // Constructor
92  ExporterT(const Mesh& _mesh) : mesh_(_mesh) {}
93 
94 
95  // get vertex data
96 
97  Vec3f point(VertexHandle _vh) const
98  {
99  return vector_cast<Vec3f>(mesh_.point(_vh));
100  }
101 
102  Vec3f normal(VertexHandle _vh) const
103  {
104  return (mesh_.has_vertex_normals()
105  ? vector_cast<Vec3f>(mesh_.normal(_vh))
106  : Vec3f(0.0f, 0.0f, 0.0f));
107  }
108 
109  Vec3uc color(VertexHandle _vh) const
110  {
111  return (mesh_.has_vertex_colors()
112  ? color_cast<Vec3uc>(mesh_.color(_vh))
113  : Vec3uc(0, 0, 0));
114  }
115 
116  Vec4uc colorA(VertexHandle _vh) const
117  {
118  return (mesh_.has_vertex_colors()
119  ? color_cast<Vec4uc>(mesh_.color(_vh))
120  : Vec4uc(0, 0, 0, 0));
121  }
122 
123  Vec3ui colori(VertexHandle _vh) const
124  {
125  return (mesh_.has_vertex_colors()
126  ? color_cast<Vec3ui>(mesh_.color(_vh))
127  : Vec3ui(0, 0, 0));
128  }
129 
130  Vec4ui colorAi(VertexHandle _vh) const
131  {
132  return (mesh_.has_vertex_colors()
133  ? color_cast<Vec4ui>(mesh_.color(_vh))
134  : Vec4ui(0, 0, 0, 0));
135  }
136 
137  Vec3f colorf(VertexHandle _vh) const
138  {
139  return (mesh_.has_vertex_colors()
140  ? color_cast<Vec3f>(mesh_.color(_vh))
141  : Vec3f(0, 0, 0));
142  }
143 
144  Vec4f colorAf(VertexHandle _vh) const
145  {
146  return (mesh_.has_vertex_colors()
147  ? color_cast<Vec4f>(mesh_.color(_vh))
148  : Vec4f(0, 0, 0, 0));
149  }
150 
151  Vec2f texcoord(VertexHandle _vh) const
152  {
153 #if defined(OM_CC_GCC) && (OM_CC_VERSION<30000)
154  // Workaround!
155  // gcc 2.95.3 exits with internal compiler error at the
156  // code below!??? **)
157  if (mesh_.has_vertex_texcoords2D())
158  return vector_cast<Vec2f>(mesh_.texcoord2D(_vh));
159  return Vec2f(0.0f, 0.0f);
160 #else // **)
161  return (mesh_.has_vertex_texcoords2D()
162  ? vector_cast<Vec2f>(mesh_.texcoord2D(_vh))
163  : Vec2f(0.0f, 0.0f));
164 #endif
165  }
166 
167  Vec2f texcoord(HalfedgeHandle _heh) const
168  {
169  return (mesh_.has_halfedge_texcoords2D()
170  ? vector_cast<Vec2f>(mesh_.texcoord2D(_heh))
171  : Vec2f(0.0f, 0.0f));
172  }
173 
174  // get edge data
175 
176  Vec3uc color(EdgeHandle _eh) const
177  {
178  return (mesh_.has_edge_colors()
179  ? color_cast<Vec3uc>(mesh_.color(_eh))
180  : Vec3uc(0, 0, 0));
181  }
182 
183  Vec4uc colorA(EdgeHandle _eh) const
184  {
185  return (mesh_.has_edge_colors()
186  ? color_cast<Vec4uc>(mesh_.color(_eh))
187  : Vec4uc(0, 0, 0, 0));
188  }
189 
190  Vec3ui colori(EdgeHandle _eh) const
191  {
192  return (mesh_.has_edge_colors()
193  ? color_cast<Vec3ui>(mesh_.color(_eh))
194  : Vec3ui(0, 0, 0));
195  }
196 
197  Vec4ui colorAi(EdgeHandle _eh) const
198  {
199  return (mesh_.has_edge_colors()
200  ? color_cast<Vec4ui>(mesh_.color(_eh))
201  : Vec4ui(0, 0, 0, 0));
202  }
203 
204  Vec3f colorf(EdgeHandle _eh) const
205  {
206  return (mesh_.has_vertex_colors()
207  ? color_cast<Vec3f>(mesh_.color(_eh))
208  : Vec3f(0, 0, 0));
209  }
210 
211  Vec4f colorAf(EdgeHandle _eh) const
212  {
213  return (mesh_.has_vertex_colors()
214  ? color_cast<Vec4f>(mesh_.color(_eh))
215  : Vec4f(0, 0, 0, 0));
216  }
217 
218  // get face data
219 
220  unsigned int get_vhandles(FaceHandle _fh,
221  std::vector<VertexHandle>& _vhandles) const
222  {
223  unsigned int count(0);
224  _vhandles.clear();
225  for (typename Mesh::CFVIter fv_it=mesh_.cfv_iter(_fh); fv_it.is_valid(); ++fv_it)
226  {
227  _vhandles.push_back(*fv_it);
228  ++count;
229  }
230  return count;
231  }
232 
233  unsigned int get_face_texcoords(std::vector<Vec2f>& _hehandles) const
234  {
235  unsigned int count(0);
236  _hehandles.clear();
237  for(typename Mesh::CHIter he_it=mesh_.halfedges_begin();
238  he_it != mesh_.halfedges_end(); ++he_it)
239  {
240  _hehandles.push_back(vector_cast<Vec2f>(mesh_.texcoord2D( *he_it)));
241  ++count;
242  }
243 
244  return count;
245  }
246 
248  {
249  typename Mesh::ConstFaceHalfedgeIter fh_it;
250  for(fh_it = mesh_.cfh_iter(_fh); fh_it.is_valid();++fh_it)
251  {
252  if(mesh_.to_vertex_handle(*fh_it) == _vh)
253  return *fh_it;
254  }
255  return *fh_it;
256  }
257 
258  Vec3f normal(FaceHandle _fh) const
259  {
260  return (mesh_.has_face_normals()
261  ? vector_cast<Vec3f>(mesh_.normal(_fh))
262  : Vec3f(0.0f, 0.0f, 0.0f));
263  }
264 
265  Vec3uc color(FaceHandle _fh) const
266  {
267  return (mesh_.has_face_colors()
268  ? color_cast<Vec3uc>(mesh_.color(_fh))
269  : Vec3uc(0, 0, 0));
270  }
271 
272  Vec4uc colorA(FaceHandle _fh) const
273  {
274  return (mesh_.has_face_colors()
275  ? color_cast<Vec4uc>(mesh_.color(_fh))
276  : Vec4uc(0, 0, 0, 0));
277  }
278 
279  Vec3ui colori(FaceHandle _fh) const
280  {
281  return (mesh_.has_face_colors()
282  ? color_cast<Vec3ui>(mesh_.color(_fh))
283  : Vec3ui(0, 0, 0));
284  }
285 
286  Vec4ui colorAi(FaceHandle _fh) const
287  {
288  return (mesh_.has_face_colors()
289  ? color_cast<Vec4ui>(mesh_.color(_fh))
290  : Vec4ui(0, 0, 0, 0));
291  }
292 
293  Vec3f colorf(FaceHandle _fh) const
294  {
295  return (mesh_.has_vertex_colors()
296  ? color_cast<Vec3f>(mesh_.color(_fh))
297  : Vec3f(0, 0, 0));
298  }
299 
300  Vec4f colorAf(FaceHandle _fh) const
301  {
302  return (mesh_.has_vertex_colors()
303  ? color_cast<Vec4f>(mesh_.color(_fh))
304  : Vec4f(0, 0, 0, 0));
305  }
306 
307  virtual const BaseKernel* kernel() { return &mesh_; }
308 
309 
310  // query number of faces, vertices, normals, texcoords
311  size_t n_vertices() const { return mesh_.n_vertices(); }
312  size_t n_faces() const { return mesh_.n_faces(); }
313  size_t n_edges() const { return mesh_.n_edges(); }
314 
315 
316  // property information
317  bool is_triangle_mesh() const
318  { return Mesh::is_triangles(); }
319 
320  bool has_vertex_normals() const { return mesh_.has_vertex_normals(); }
321  bool has_vertex_colors() const { return mesh_.has_vertex_colors(); }
322  bool has_vertex_texcoords() const { return mesh_.has_vertex_texcoords2D(); }
323  bool has_edge_colors() const { return mesh_.has_edge_colors(); }
324  bool has_face_normals() const { return mesh_.has_face_normals(); }
325  bool has_face_colors() const { return mesh_.has_face_colors(); }
326 
327 private:
328 
329  const Mesh& mesh_;
330 };
331 
332 
333 //=============================================================================
334 } // namespace IO
335 } // namespace OpenMesh
336 //=============================================================================
337 #endif
338 //=============================================================================
Handle for a halfedge entity.
Definition: Handles.hh:132
This class provides low-level property management like adding/removing properties and access to prope...
Definition: BaseKernel.hh:102
VectorT< unsigned char, 4 > Vec4uc
4-byte unsigned vector
Definition: Vector11T.hh:820
VectorT< float, 3 > Vec3f
3-float vector
Definition: Vector11T.hh:811
Handle for a face entity.
Definition: Handles.hh:146
VectorT< unsigned char, 3 > Vec3uc
3-byte unsigned vector
Definition: Vector11T.hh:801
Contains all the mesh ingredients like the polygonal mesh, the triangle mesh, different mesh kernels ...
Definition: MeshItems.hh:64
VectorT< unsigned int, 4 > Vec4ui
4-int unsigned vector
Definition: Vector11T.hh:828
VectorT< unsigned int, 3 > Vec3ui
3-int unsigned vector
Definition: Vector11T.hh:809
void vector_cast(const src_t &_src, dst_t &_dst, GenProg::Int2Type< n >)
Cast vector type to another vector type by copying the vector elements.
Definition: vector_cast.hh:86
Handle for a vertex entity.
Definition: Handles.hh:125
VectorT< float, 4 > Vec4f
4-float vector
Definition: Vector11T.hh:830
Base class for exporter modules.
Definition: BaseExporter.hh:89
HalfedgeHandle getHeh(FaceHandle _fh, VertexHandle _vh) const
getHeh returns the HalfEdgeHandle that belongs to the face specified by _fh and has a toVertexHandle ...
Definition: ExporterT.hh:247
VectorT< float, 2 > Vec2f
2-float vector
Definition: Vector11T.hh:794
Handle for a edge entity.
Definition: Handles.hh:139
This class template provides an exporter module for OpenMesh meshes.
Definition: ExporterT.hh:87

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