Developer Documentation

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