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 #include <OpenMesh/Core/IO/OMFormat.hh>
69 
70 
71 //=== NAMESPACES ==============================================================
72 
73 namespace OpenMesh {
74 namespace IO {
75 
76 
77 //=== EXPORTER CLASS ==========================================================
78 
79 /**
80  *  This class template provides an exporter module for OpenMesh meshes.
81  */
82 template <class Mesh>
83 class ExporterT : public BaseExporter
84 {
85 public:
86 
87   // Constructor
ExporterT(const Mesh & _mesh)88   explicit ExporterT(const Mesh& _mesh) : mesh_(_mesh) {}
89 
90 
91   // get vertex data
92 
point(VertexHandle _vh) const93   Vec3f  point(VertexHandle _vh)    const override
94   {
95     return vector_cast<Vec3f>(mesh_.point(_vh));
96   }
97 
pointd(VertexHandle _vh) const98   Vec3d  pointd(VertexHandle _vh) const override
99   {
100     return vector_cast<Vec3d>(mesh_.point(_vh));
101   }
102 
is_point_double() const103   bool is_point_double() const override
104   {
105     return OMFormat::is_double(typename Mesh::Point()[0]);
106   }
107 
is_normal_double() const108   bool is_normal_double() const override
109   {
110     return OMFormat::is_double(typename Mesh::Normal()[0]);
111   }
112 
normal(VertexHandle _vh) const113   Vec3f  normal(VertexHandle _vh)   const override
114   {
115     return (mesh_.has_vertex_normals()
116       ? vector_cast<Vec3f>(mesh_.normal(_vh))
117       : Vec3f(0.0f, 0.0f, 0.0f));
118   }
119 
normald(VertexHandle _vh) const120   Vec3d  normald(VertexHandle _vh)   const override
121   {
122     return (mesh_.has_vertex_normals()
123       ? vector_cast<Vec3d>(mesh_.normal(_vh))
124       : Vec3d(0.0f, 0.0f, 0.0f));
125   }
126 
color(VertexHandle _vh) const127   Vec3uc color(VertexHandle _vh)    const override
128   {
129     return (mesh_.has_vertex_colors()
130 	    ? color_cast<Vec3uc>(mesh_.color(_vh))
131 	    : Vec3uc(0, 0, 0));
132   }
133 
colorA(VertexHandle _vh) const134   Vec4uc colorA(VertexHandle _vh)   const override
135   {
136     return (mesh_.has_vertex_colors()
137       ? color_cast<Vec4uc>(mesh_.color(_vh))
138       : Vec4uc(0, 0, 0, 0));
139   }
140 
colori(VertexHandle _vh) const141   Vec3ui colori(VertexHandle _vh)    const override
142   {
143     return (mesh_.has_vertex_colors()
144 	    ? color_cast<Vec3ui>(mesh_.color(_vh))
145 	    : Vec3ui(0, 0, 0));
146   }
147 
colorAi(VertexHandle _vh) const148   Vec4ui colorAi(VertexHandle _vh)   const override
149   {
150     return (mesh_.has_vertex_colors()
151       ? color_cast<Vec4ui>(mesh_.color(_vh))
152       : Vec4ui(0, 0, 0, 0));
153   }
154 
colorf(VertexHandle _vh) const155   Vec3f colorf(VertexHandle _vh)    const override
156   {
157     return (mesh_.has_vertex_colors()
158 	    ? color_cast<Vec3f>(mesh_.color(_vh))
159 	    : Vec3f(0, 0, 0));
160   }
161 
colorAf(VertexHandle _vh) const162   Vec4f colorAf(VertexHandle _vh)   const override
163   {
164     return (mesh_.has_vertex_colors()
165       ? color_cast<Vec4f>(mesh_.color(_vh))
166       : Vec4f(0, 0, 0, 0));
167   }
168 
texcoord(VertexHandle _vh) const169   Vec2f  texcoord(VertexHandle _vh) const override
170   {
171 #if defined(OM_CC_GCC) && (OM_CC_VERSION<30000)
172     // Workaround!
173     // gcc 2.95.3 exits with internal compiler error at the
174     // code below!??? **)
175     if (mesh_.has_vertex_texcoords2D())
176       return vector_cast<Vec2f>(mesh_.texcoord2D(_vh));
177     return Vec2f(0.0f, 0.0f);
178 #else // **)
179     return (mesh_.has_vertex_texcoords2D()
180 	    ? vector_cast<Vec2f>(mesh_.texcoord2D(_vh))
181 	    : Vec2f(0.0f, 0.0f));
182 #endif
183   }
184 
texcoord(HalfedgeHandle _heh) const185   Vec2f  texcoord(HalfedgeHandle _heh) const override
186   {
187     return (mesh_.has_halfedge_texcoords2D()
188         ? vector_cast<Vec2f>(mesh_.texcoord2D(_heh))
189         : Vec2f(0.0f, 0.0f));
190   }
191 
status(VertexHandle _vh) const192   OpenMesh::Attributes::StatusInfo  status(VertexHandle _vh) const override
193   {
194     if (mesh_.has_vertex_status())
195       return mesh_.status(_vh);
196     return OpenMesh::Attributes::StatusInfo();
197   }
198 
199   // get edge data
200 
color(EdgeHandle _eh) const201   Vec3uc color(EdgeHandle _eh)    const override
202   {
203       return (mesh_.has_edge_colors()
204       ? color_cast<Vec3uc>(mesh_.color(_eh))
205       : Vec3uc(0, 0, 0));
206   }
207 
colorA(EdgeHandle _eh) const208   Vec4uc colorA(EdgeHandle _eh)   const override
209   {
210       return (mesh_.has_edge_colors()
211       ? color_cast<Vec4uc>(mesh_.color(_eh))
212       : Vec4uc(0, 0, 0, 0));
213   }
214 
colori(EdgeHandle _eh) const215   Vec3ui colori(EdgeHandle _eh)    const override
216   {
217       return (mesh_.has_edge_colors()
218       ? color_cast<Vec3ui>(mesh_.color(_eh))
219       : Vec3ui(0, 0, 0));
220   }
221 
colorAi(EdgeHandle _eh) const222   Vec4ui colorAi(EdgeHandle _eh)   const override
223   {
224       return (mesh_.has_edge_colors()
225       ? color_cast<Vec4ui>(mesh_.color(_eh))
226       : Vec4ui(0, 0, 0, 0));
227   }
228 
colorf(EdgeHandle _eh) const229   Vec3f colorf(EdgeHandle _eh)    const override
230   {
231     return (mesh_.has_vertex_colors()
232 	    ? color_cast<Vec3f>(mesh_.color(_eh))
233 	    : Vec3f(0, 0, 0));
234   }
235 
colorAf(EdgeHandle _eh) const236   Vec4f colorAf(EdgeHandle _eh)   const override
237   {
238     return (mesh_.has_vertex_colors()
239       ? color_cast<Vec4f>(mesh_.color(_eh))
240       : Vec4f(0, 0, 0, 0));
241   }
242 
status(EdgeHandle _eh) const243   OpenMesh::Attributes::StatusInfo  status(EdgeHandle _eh) const override
244   {
245     if (mesh_.has_edge_status())
246       return mesh_.status(_eh);
247     return OpenMesh::Attributes::StatusInfo();
248   }
249 
250   // get halfedge data
251 
get_halfedge_id(VertexHandle _vh)252   int get_halfedge_id(VertexHandle _vh) override
253   {
254     return mesh_.halfedge_handle(_vh).idx();
255   }
256 
get_halfedge_id(FaceHandle _fh)257   int get_halfedge_id(FaceHandle _fh) override
258   {
259     return mesh_.halfedge_handle(_fh).idx();
260   }
261 
get_next_halfedge_id(HalfedgeHandle _heh)262   int get_next_halfedge_id(HalfedgeHandle _heh) override
263   {
264     return mesh_.next_halfedge_handle(_heh).idx();
265   }
266 
get_to_vertex_id(HalfedgeHandle _heh)267   int get_to_vertex_id(HalfedgeHandle _heh) override
268   {
269     return mesh_.to_vertex_handle(_heh).idx();
270   }
271 
get_face_id(HalfedgeHandle _heh)272   int get_face_id(HalfedgeHandle _heh) override
273   {
274     return mesh_.face_handle(_heh).idx();
275   }
276 
status(HalfedgeHandle _heh) const277   OpenMesh::Attributes::StatusInfo  status(HalfedgeHandle _heh) const override
278   {
279     if (mesh_.has_halfedge_status())
280       return mesh_.status(_heh);
281     return OpenMesh::Attributes::StatusInfo();
282   }
283 
284   // get face data
285 
get_vhandles(FaceHandle _fh,std::vector<VertexHandle> & _vhandles) const286   unsigned int get_vhandles(FaceHandle _fh,
287 			    std::vector<VertexHandle>& _vhandles) const override
288   {
289     unsigned int count(0);
290     _vhandles.clear();
291     for (typename Mesh::CFVIter fv_it=mesh_.cfv_iter(_fh); fv_it.is_valid(); ++fv_it)
292     {
293       _vhandles.push_back(*fv_it);
294       ++count;
295     }
296     return count;
297   }
298 
get_face_texcoords(std::vector<Vec2f> & _hehandles) const299   unsigned int get_face_texcoords(std::vector<Vec2f>& _hehandles) const override
300   {
301     unsigned int count(0);
302     _hehandles.clear();
303     for(typename Mesh::CHIter he_it=mesh_.halfedges_begin();
304         he_it != mesh_.halfedges_end(); ++he_it)
305     {
306       _hehandles.push_back(vector_cast<Vec2f>(mesh_.texcoord2D( *he_it)));
307       ++count;
308     }
309 
310     return count;
311   }
312 
getHeh(FaceHandle _fh,VertexHandle _vh) const313   HalfedgeHandle getHeh(FaceHandle _fh, VertexHandle _vh) const override
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 
normal(FaceHandle _fh) const324   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 
normald(FaceHandle _fh) const331   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 
color(FaceHandle _fh) const338   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 
colorA(FaceHandle _fh) const345   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 
colori(FaceHandle _fh) const352   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 
colorAi(FaceHandle _fh) const359   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 
colorf(FaceHandle _fh) const366   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 
colorAf(FaceHandle _fh) const373   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 
status(FaceHandle _fh) const380   OpenMesh::Attributes::StatusInfo  status(FaceHandle _fh) const override
381   {
382     if (mesh_.has_face_status())
383       return mesh_.status(_fh);
384     return OpenMesh::Attributes::StatusInfo();
385   }
386 
kernel()387   virtual const BaseKernel* kernel() override { return &mesh_; }
388 
389 
390   // query number of faces, vertices, normals, texcoords
n_vertices() const391   size_t n_vertices()  const override { return mesh_.n_vertices(); }
n_faces() const392   size_t n_faces()     const override { return mesh_.n_faces(); }
n_edges() const393   size_t n_edges()     const override { return mesh_.n_edges(); }
394 
395 
396   // property information
is_triangle_mesh() const397   bool is_triangle_mesh() const override
398   { return Mesh::is_triangles(); }
399 
has_vertex_normals() const400   bool has_vertex_normals()   const override { return mesh_.has_vertex_normals();   }
has_vertex_colors() const401   bool has_vertex_colors()    const override { return mesh_.has_vertex_colors();    }
has_vertex_texcoords() const402   bool has_vertex_texcoords() const override { return mesh_.has_vertex_texcoords2D(); }
has_vertex_status() const403   bool has_vertex_status()    const override { return mesh_.has_vertex_status();    }
has_edge_colors() const404   bool has_edge_colors()      const override { return mesh_.has_edge_colors();      }
has_edge_status() const405   bool has_edge_status()      const override { return mesh_.has_edge_status();      }
has_halfedge_status() const406   bool has_halfedge_status()  const override { return mesh_.has_halfedge_status();  }
has_face_normals() const407   bool has_face_normals()     const override { return mesh_.has_face_normals();     }
has_face_colors() const408   bool has_face_colors()      const override { return mesh_.has_face_colors();      }
has_face_status() const409   bool has_face_status()      const override { return mesh_.has_face_status();      }
410 
411 private:
412 
413    const Mesh& mesh_;
414 };
415 
416 
417 //=============================================================================
418 } // namespace IO
419 } // namespace OpenMesh
420 //=============================================================================
421 #endif
422 //=============================================================================
423