OpenMesh
ExporterT.hh
1/* ========================================================================= *
2 * *
3 * OpenMesh *
4 * Copyright (c) 2001-2025, 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/Utils/Predicates.hh>
68#include <OpenMesh/Core/IO/exporter/BaseExporter.hh>
69#include <OpenMesh/Core/IO/OMFormat.hh>
70
71
72//=== NAMESPACES ==============================================================
73
74namespace OpenMesh {
75namespace IO {
76
77
78//=== EXPORTER CLASS ==========================================================
79
83template <class Mesh>
84class ExporterT : public BaseExporter
85{
86public:
87
88 // Constructor
89 explicit ExporterT(const Mesh& _mesh) : mesh_(_mesh) {}
90
91
92 // get vertex data
93
94 Vec3f point(VertexHandle _vh) const override
95 {
96 return vector_cast<Vec3f>(mesh_.point(_vh));
97 }
98
99 Vec3d pointd(VertexHandle _vh) const override
100 {
101 return vector_cast<Vec3d>(mesh_.point(_vh));
102 }
103
104 bool is_point_double() const override
105 {
106 return OMFormat::is_double(typename Mesh::Point()[0]);
107 }
108
109 bool is_normal_double() const override
110 {
111 return OMFormat::is_double(typename Mesh::Normal()[0]);
112 }
113
114 Vec3f normal(VertexHandle _vh) const override
115 {
116 return (mesh_.has_vertex_normals()
117 ? vector_cast<Vec3f>(mesh_.normal(_vh))
118 : Vec3f(0.0f, 0.0f, 0.0f));
119 }
120
121 Vec3d normald(VertexHandle _vh) const override
122 {
123 return (mesh_.has_vertex_normals()
124 ? vector_cast<Vec3d>(mesh_.normal(_vh))
125 : Vec3d(0.0f, 0.0f, 0.0f));
126 }
127
128 Vec3uc color(VertexHandle _vh) const override
129 {
130 return (mesh_.has_vertex_colors()
131 ? color_cast<Vec3uc>(mesh_.color(_vh))
132 : Vec3uc(0, 0, 0));
133 }
134
135 Vec4uc colorA(VertexHandle _vh) const override
136 {
137 return (mesh_.has_vertex_colors()
138 ? color_cast<Vec4uc>(mesh_.color(_vh))
139 : Vec4uc(0, 0, 0, 0));
140 }
141
142 Vec3ui colori(VertexHandle _vh) const override
143 {
144 return (mesh_.has_vertex_colors()
145 ? color_cast<Vec3ui>(mesh_.color(_vh))
146 : Vec3ui(0, 0, 0));
147 }
148
149 Vec4ui colorAi(VertexHandle _vh) const override
150 {
151 return (mesh_.has_vertex_colors()
152 ? color_cast<Vec4ui>(mesh_.color(_vh))
153 : Vec4ui(0, 0, 0, 0));
154 }
155
156 Vec3f colorf(VertexHandle _vh) const override
157 {
158 return (mesh_.has_vertex_colors()
159 ? color_cast<Vec3f>(mesh_.color(_vh))
160 : Vec3f(0, 0, 0));
161 }
162
163 Vec4f colorAf(VertexHandle _vh) const override
164 {
165 return (mesh_.has_vertex_colors()
166 ? color_cast<Vec4f>(mesh_.color(_vh))
167 : Vec4f(0, 0, 0, 0));
168 }
169
170 Vec2f texcoord(VertexHandle _vh) const override
171 {
172#if defined(OM_CC_GCC) && (OM_CC_VERSION<30000)
173 // Workaround!
174 // gcc 2.95.3 exits with internal compiler error at the
175 // code below!??? **)
176 if (mesh_.has_vertex_texcoords2D())
177 return vector_cast<Vec2f>(mesh_.texcoord2D(_vh));
178 return Vec2f(0.0f, 0.0f);
179#else // **)
180 return (mesh_.has_vertex_texcoords2D()
181 ? vector_cast<Vec2f>(mesh_.texcoord2D(_vh))
182 : Vec2f(0.0f, 0.0f));
183#endif
184 }
185
186 Vec2f texcoord(HalfedgeHandle _heh) const override
187 {
188 return (mesh_.has_halfedge_texcoords2D()
189 ? vector_cast<Vec2f>(mesh_.texcoord2D(_heh))
190 : Vec2f(0.0f, 0.0f));
191 }
192
193 OpenMesh::Attributes::StatusInfo status(VertexHandle _vh) const override
194 {
195 if (mesh_.has_vertex_status())
196 return mesh_.status(_vh);
198 }
199
200 // get edge data
201
202 Vec3uc color(EdgeHandle _eh) const override
203 {
204 return (mesh_.has_edge_colors()
205 ? color_cast<Vec3uc>(mesh_.color(_eh))
206 : Vec3uc(0, 0, 0));
207 }
208
209 Vec4uc colorA(EdgeHandle _eh) const override
210 {
211 return (mesh_.has_edge_colors()
212 ? color_cast<Vec4uc>(mesh_.color(_eh))
213 : Vec4uc(0, 0, 0, 0));
214 }
215
216 Vec3ui colori(EdgeHandle _eh) const override
217 {
218 return (mesh_.has_edge_colors()
219 ? color_cast<Vec3ui>(mesh_.color(_eh))
220 : Vec3ui(0, 0, 0));
221 }
222
223 Vec4ui colorAi(EdgeHandle _eh) const override
224 {
225 return (mesh_.has_edge_colors()
226 ? color_cast<Vec4ui>(mesh_.color(_eh))
227 : Vec4ui(0, 0, 0, 0));
228 }
229
230 Vec3f colorf(EdgeHandle _eh) const override
231 {
232 return (mesh_.has_vertex_colors()
233 ? color_cast<Vec3f>(mesh_.color(_eh))
234 : Vec3f(0, 0, 0));
235 }
236
237 Vec4f colorAf(EdgeHandle _eh) const override
238 {
239 return (mesh_.has_vertex_colors()
240 ? color_cast<Vec4f>(mesh_.color(_eh))
241 : Vec4f(0, 0, 0, 0));
242 }
243
244 OpenMesh::Attributes::StatusInfo status(EdgeHandle _eh) const override
245 {
246 if (mesh_.has_edge_status())
247 return mesh_.status(_eh);
249 }
250
251 // get halfedge data
252
253 int get_halfedge_id(VertexHandle _vh) override
254 {
255 return mesh_.halfedge_handle(_vh).idx();
256 }
257
258 int get_halfedge_id(FaceHandle _fh) override
259 {
260 return mesh_.halfedge_handle(_fh).idx();
261 }
262
263 int get_next_halfedge_id(HalfedgeHandle _heh) override
264 {
265 return mesh_.next_halfedge_handle(_heh).idx();
266 }
267
268 int get_to_vertex_id(HalfedgeHandle _heh) override
269 {
270 return mesh_.to_vertex_handle(_heh).idx();
271 }
272
273 int get_face_id(HalfedgeHandle _heh) override
274 {
275 return mesh_.face_handle(_heh).idx();
276 }
277
278 OpenMesh::Attributes::StatusInfo status(HalfedgeHandle _heh) const override
279 {
280 if (mesh_.has_halfedge_status())
281 return mesh_.status(_heh);
283 }
284
285 // get face data
286
287 unsigned int get_vhandles(FaceHandle _fh,
288 std::vector<VertexHandle>& _vhandles) const override
289 {
290 unsigned int count(0);
291 _vhandles.clear();
292 for (typename Mesh::CFVIter fv_it=mesh_.cfv_iter(_fh); fv_it.is_valid(); ++fv_it)
293 {
294 _vhandles.push_back(*fv_it);
295 ++count;
296 }
297 return count;
298 }
299
300 unsigned int get_face_texcoords(std::vector<Vec2f>& _hehandles) const override
301 {
302 unsigned int count(0);
303 _hehandles.clear();
304 for(auto heh: mesh_.halfedges().filtered(!OpenMesh::Predicates::Boundary()))
305 {
306 _hehandles.push_back(vector_cast<Vec2f>(mesh_.texcoord2D(heh)));
307 ++count;
308 }
309
310 return count;
311 }
312
314 {
315 typename Mesh::ConstFaceHalfedgeIter fh_it;
316 for(fh_it = mesh_.cfh_iter(_fh); fh_it.is_valid();++fh_it)
317 {
318 if(mesh_.to_vertex_handle(*fh_it) == _vh)
319 return *fh_it;
320 }
321 return *fh_it;
322 }
323
324 Vec3f normal(FaceHandle _fh) const override
325 {
326 return (mesh_.has_face_normals()
327 ? vector_cast<Vec3f>(mesh_.normal(_fh))
328 : Vec3f(0.0f, 0.0f, 0.0f));
329 }
330
331 Vec3d normald(FaceHandle _fh) const override
332 {
333 return (mesh_.has_face_normals()
334 ? vector_cast<Vec3d>(mesh_.normal(_fh))
335 : Vec3d(0.0, 0.0, 0.0));
336 }
337
338 Vec3uc color(FaceHandle _fh) const override
339 {
340 return (mesh_.has_face_colors()
341 ? color_cast<Vec3uc>(mesh_.color(_fh))
342 : Vec3uc(0, 0, 0));
343 }
344
345 Vec4uc colorA(FaceHandle _fh) const override
346 {
347 return (mesh_.has_face_colors()
348 ? color_cast<Vec4uc>(mesh_.color(_fh))
349 : Vec4uc(0, 0, 0, 0));
350 }
351
352 Vec3ui colori(FaceHandle _fh) const override
353 {
354 return (mesh_.has_face_colors()
355 ? color_cast<Vec3ui>(mesh_.color(_fh))
356 : Vec3ui(0, 0, 0));
357 }
358
359 Vec4ui colorAi(FaceHandle _fh) const override
360 {
361 return (mesh_.has_face_colors()
362 ? color_cast<Vec4ui>(mesh_.color(_fh))
363 : Vec4ui(0, 0, 0, 0));
364 }
365
366 Vec3f colorf(FaceHandle _fh) const override
367 {
368 return (mesh_.has_face_colors()
369 ? color_cast<Vec3f>(mesh_.color(_fh))
370 : Vec3f(0, 0, 0));
371 }
372
373 Vec4f colorAf(FaceHandle _fh) const override
374 {
375 return (mesh_.has_face_colors()
376 ? color_cast<Vec4f>(mesh_.color(_fh))
377 : Vec4f(0, 0, 0, 0));
378 }
379
380 OpenMesh::Attributes::StatusInfo status(FaceHandle _fh) const override
381 {
382 if (mesh_.has_face_status())
383 return mesh_.status(_fh);
385 }
386
387 virtual const BaseKernel* kernel() override { return &mesh_; }
388
389
390 // query number of faces, vertices, normals, texcoords
391 size_t n_vertices() const override { return mesh_.n_vertices(); }
392 size_t n_faces() const override { return mesh_.n_faces(); }
393 size_t n_edges() const override { return mesh_.n_edges(); }
394
395
396 // property information
397 bool is_triangle_mesh() const override
398 { return Mesh::is_triangles(); }
399
400 bool has_vertex_normals() const override { return mesh_.has_vertex_normals(); }
401 bool has_vertex_colors() const override { return mesh_.has_vertex_colors(); }
402 bool has_vertex_texcoords() const override { return mesh_.has_vertex_texcoords2D(); }
403 bool has_vertex_status() const override { return mesh_.has_vertex_status(); }
404 bool has_edge_colors() const override { return mesh_.has_edge_colors(); }
405 bool has_edge_status() const override { return mesh_.has_edge_status(); }
406 bool has_halfedge_status() const override { return mesh_.has_halfedge_status(); }
407 bool has_face_normals() const override { return mesh_.has_face_normals(); }
408 bool has_face_colors() const override { return mesh_.has_face_colors(); }
409 bool has_face_status() const override { return mesh_.has_face_status(); }
410
411private:
412
413 const Mesh& mesh_;
414};
415
416
417//=============================================================================
418} // namespace IO
419} // namespace OpenMesh
420//=============================================================================
421#endif
422//=============================================================================
Contains all the mesh ingredients like the polygonal mesh, the triangle mesh, different mesh kernels ...
Definition: MeshItems.hh:59
VectorT< float, 2 > Vec2f
2-float vector
Definition: Vector11T.hh:834
VectorT< unsigned int, 4 > Vec4ui
4-int unsigned vector
Definition: Vector11T.hh:868
VectorT< unsigned char, 3 > Vec3uc
3-byte unsigned vector
Definition: Vector11T.hh:841
VectorT< float, 4 > Vec4f
4-float vector
Definition: Vector11T.hh:870
VectorT< unsigned char, 4 > Vec4uc
4-byte unsigned vector
Definition: Vector11T.hh:860
VectorT< unsigned int, 3 > Vec3ui
3-int unsigned vector
Definition: Vector11T.hh:849
VectorT< float, 3 > Vec3f
3-float vector
Definition: Vector11T.hh:851
Base class for exporter modules.
Definition: BaseExporter.hh:85
This class template provides an exporter module for OpenMesh meshes.
Definition: ExporterT.hh:85
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:313
Handle for a vertex entity.
Definition: Handles.hh:121
Handle for a halfedge entity.
Definition: Handles.hh:128
Handle for a edge entity.
Definition: Handles.hh:135
Handle for a face entity.
Definition: Handles.hh:142
Polygonal mesh based on the ArrayKernel.
Definition: PolyMesh_ArrayKernelT.hh:96
Kernel::ConstFaceHalfedgeIter ConstFaceHalfedgeIter
Circulator.
Definition: PolyMeshT.hh:178
Kernel::Normal Normal
Normal type.
Definition: PolyMeshT.hh:114
Kernel::Point Point
Coordinate type.
Definition: PolyMeshT.hh:112
Add status information to a base class.
Definition: Status.hh:95
Definition: Predicates.hh:239

Project OpenMesh, ©  Visual Computing Institute, RWTH Aachen. Documentation generated using doxygen .