Developer Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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
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  // get edge data
168 
169  Vec3uc color(EdgeHandle _eh) const
170  {
171  return (mesh_.has_edge_colors()
172  ? color_cast<Vec3uc>(mesh_.color(_eh))
173  : Vec3uc(0, 0, 0));
174  }
175 
176  Vec4uc colorA(EdgeHandle _eh) const
177  {
178  return (mesh_.has_edge_colors()
179  ? color_cast<Vec4uc>(mesh_.color(_eh))
180  : Vec4uc(0, 0, 0, 0));
181  }
182 
183  Vec3ui colori(EdgeHandle _eh) const
184  {
185  return (mesh_.has_edge_colors()
186  ? color_cast<Vec3ui>(mesh_.color(_eh))
187  : Vec3ui(0, 0, 0));
188  }
189 
190  Vec4ui colorAi(EdgeHandle _eh) const
191  {
192  return (mesh_.has_edge_colors()
193  ? color_cast<Vec4ui>(mesh_.color(_eh))
194  : Vec4ui(0, 0, 0, 0));
195  }
196 
197  Vec3f colorf(EdgeHandle _eh) const
198  {
199  return (mesh_.has_vertex_colors()
200  ? color_cast<Vec3f>(mesh_.color(_eh))
201  : Vec3f(0, 0, 0));
202  }
203 
204  Vec4f colorAf(EdgeHandle _eh) const
205  {
206  return (mesh_.has_vertex_colors()
207  ? color_cast<Vec4f>(mesh_.color(_eh))
208  : Vec4f(0, 0, 0, 0));
209  }
210 
211  // get face data
212 
213  unsigned int get_vhandles(FaceHandle _fh,
214  std::vector<VertexHandle>& _vhandles) const
215  {
216  unsigned int count(0);
217  _vhandles.clear();
218  for (typename Mesh::CFVIter fv_it=mesh_.cfv_iter(_fh); fv_it.is_valid(); ++fv_it)
219  {
220  _vhandles.push_back(*fv_it);
221  ++count;
222  }
223  return count;
224  }
225 
226  Vec3f normal(FaceHandle _fh) const
227  {
228  return (mesh_.has_face_normals()
229  ? vector_cast<Vec3f>(mesh_.normal(_fh))
230  : Vec3f(0.0f, 0.0f, 0.0f));
231  }
232 
233  Vec3uc color(FaceHandle _fh) const
234  {
235  return (mesh_.has_face_colors()
236  ? color_cast<Vec3uc>(mesh_.color(_fh))
237  : Vec3uc(0, 0, 0));
238  }
239 
240  Vec4uc colorA(FaceHandle _fh) const
241  {
242  return (mesh_.has_face_colors()
243  ? color_cast<Vec4uc>(mesh_.color(_fh))
244  : Vec4uc(0, 0, 0, 0));
245  }
246 
247  Vec3ui colori(FaceHandle _fh) const
248  {
249  return (mesh_.has_face_colors()
250  ? color_cast<Vec3ui>(mesh_.color(_fh))
251  : Vec3ui(0, 0, 0));
252  }
253 
254  Vec4ui colorAi(FaceHandle _fh) const
255  {
256  return (mesh_.has_face_colors()
257  ? color_cast<Vec4ui>(mesh_.color(_fh))
258  : Vec4ui(0, 0, 0, 0));
259  }
260 
261  Vec3f colorf(FaceHandle _fh) const
262  {
263  return (mesh_.has_vertex_colors()
264  ? color_cast<Vec3f>(mesh_.color(_fh))
265  : Vec3f(0, 0, 0));
266  }
267 
268  Vec4f colorAf(FaceHandle _fh) const
269  {
270  return (mesh_.has_vertex_colors()
271  ? color_cast<Vec4f>(mesh_.color(_fh))
272  : Vec4f(0, 0, 0, 0));
273  }
274 
275  virtual const BaseKernel* kernel() { return &mesh_; }
276 
277 
278  // query number of faces, vertices, normals, texcoords
279  size_t n_vertices() const { return mesh_.n_vertices(); }
280  size_t n_faces() const { return mesh_.n_faces(); }
281  size_t n_edges() const { return mesh_.n_edges(); }
282 
283 
284  // property information
285  bool is_triangle_mesh() const
286  { return Mesh::is_triangles(); }
287 
288  bool has_vertex_normals() const { return mesh_.has_vertex_normals(); }
289  bool has_vertex_colors() const { return mesh_.has_vertex_colors(); }
290  bool has_vertex_texcoords() const { return mesh_.has_vertex_texcoords2D(); }
291  bool has_edge_colors() const { return mesh_.has_edge_colors(); }
292  bool has_face_normals() const { return mesh_.has_face_normals(); }
293  bool has_face_colors() const { return mesh_.has_face_colors(); }
294 
295 private:
296 
297  const Mesh& mesh_;
298 };
299 
300 
301 //=============================================================================
302 } // namespace IO
303 } // namespace OpenMesh
304 //=============================================================================
305 #endif
306 //=============================================================================
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
VectorT< unsigned int, 3 > Vec3ui
Definition: Vector11T.hh:767
VectorT< float, 2 > Vec2f
Definition: Vector11T.hh:752
Handle for a vertex entity.
Definition: Handles.hh:125
Handle for a edge entity.
Definition: Handles.hh:139
Handle for a face entity.
Definition: Handles.hh:146
VectorT< unsigned char, 4 > Vec4uc
Definition: Vector11T.hh:778
VectorT< unsigned char, 3 > Vec3uc
Definition: Vector11T.hh:759
VectorT< unsigned int, 4 > Vec4ui
Definition: Vector11T.hh:786
VectorT< float, 3 > Vec3f
Definition: Vector11T.hh:769
VectorT< float, 4 > Vec4f
Definition: Vector11T.hh:788