ExporterT.hh 12.2 KB
Newer Older
Jan Möbius's avatar
Jan Möbius committed
1
/* ========================================================================= *
2 3
 *                                                                           *
 *                               OpenMesh                                    *
Jan Möbius's avatar
Jan Möbius committed
4
 *           Copyright (c) 2001-2015, RWTH-Aachen University                 *
Jan Möbius's avatar
Jan Möbius committed
5
 *           Department of Computer Graphics and Multimedia                  *
Jan Möbius's avatar
Jan Möbius committed
6 7
 *                          All rights reserved.                             *
 *                            www.openmesh.org                               *
8
 *                                                                           *
9
 *---------------------------------------------------------------------------*
Jan Möbius's avatar
Jan Möbius committed
10 11
 * This file is part of OpenMesh.                                            *
 *---------------------------------------------------------------------------*
12
 *                                                                           *
Jan Möbius's avatar
Jan Möbius committed
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
 * Redistribution and use in source and binary forms, with or without        *
 * modification, are permitted provided that the following conditions        *
 * are met:                                                                  *
 *                                                                           *
 * 1. Redistributions of source code must retain the above copyright notice, *
 *    this list of conditions and the following disclaimer.                  *
 *                                                                           *
 * 2. Redistributions in binary form must reproduce the above copyright      *
 *    notice, this list of conditions and the following disclaimer in the    *
 *    documentation and/or other materials provided with the distribution.   *
 *                                                                           *
 * 3. Neither the name of the copyright holder nor the names of its          *
 *    contributors may be used to endorse or promote products derived from   *
 *    this software without specific prior written permission.               *
 *                                                                           *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS       *
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A           *
 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,  *
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,       *
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR        *
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF    *
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING      *
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS        *
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.              *
Jan Möbius's avatar
Jan Möbius committed
39 40
 *                                                                           *
 * ========================================================================= */
41

42

43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86


//=============================================================================
//
//  Implements an exporter module for arbitrary OpenMesh meshes
//
//=============================================================================


#ifndef __EXPORTERT_HH__
#define __EXPORTERT_HH__


//=== INCLUDES ================================================================

// C++
#include <vector>

// OpenMesh
#include <OpenMesh/Core/System/config.h>
#include <OpenMesh/Core/Geometry/VectorT.hh>
#include <OpenMesh/Core/Utils/GenProg.hh>
#include <OpenMesh/Core/Utils/vector_cast.hh>
#include <OpenMesh/Core/Utils/color_cast.hh>
#include <OpenMesh/Core/IO/exporter/BaseExporter.hh>


//=== NAMESPACES ==============================================================

namespace OpenMesh {
namespace IO {


//=== EXPORTER CLASS ==========================================================

/**
 *  This class template provides an exporter module for OpenMesh meshes.
 */
template <class Mesh>
class ExporterT : public BaseExporter
{
public:

  // Constructor
Jan Möbius's avatar
Jan Möbius committed
87
  explicit ExporterT(const Mesh& _mesh) : mesh_(_mesh) {}
88

89 90 91

  // get vertex data

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 100
  {
    return (mesh_.has_vertex_normals()
	    ? vector_cast<Vec3f>(mesh_.normal(_vh))
101 102 103
	    : Vec3f(0.0f, 0.0f, 0.0f));
  }

104
  Vec3uc color(VertexHandle _vh)    const override
105
  {
106 107
    return (mesh_.has_vertex_colors()
	    ? color_cast<Vec3uc>(mesh_.color(_vh))
108 109 110
	    : Vec3uc(0, 0, 0));
  }

111
  Vec4uc colorA(VertexHandle _vh)   const override
112
  {
113 114
    return (mesh_.has_vertex_colors()
      ? color_cast<Vec4uc>(mesh_.color(_vh))
115 116 117
      : Vec4uc(0, 0, 0, 0));
  }

118
  Vec3ui colori(VertexHandle _vh)    const override
119 120 121 122 123 124
  {
    return (mesh_.has_vertex_colors()
	    ? color_cast<Vec3ui>(mesh_.color(_vh))
	    : Vec3ui(0, 0, 0));
  }

125
  Vec4ui colorAi(VertexHandle _vh)   const override
126 127 128 129 130 131
  {
    return (mesh_.has_vertex_colors()
      ? color_cast<Vec4ui>(mesh_.color(_vh))
      : Vec4ui(0, 0, 0, 0));
  }

132
  Vec3f colorf(VertexHandle _vh)    const override
133 134 135 136 137 138
  {
    return (mesh_.has_vertex_colors()
	    ? color_cast<Vec3f>(mesh_.color(_vh))
	    : Vec3f(0, 0, 0));
  }

139
  Vec4f colorAf(VertexHandle _vh)   const override
140 141 142 143 144 145
  {
    return (mesh_.has_vertex_colors()
      ? color_cast<Vec4f>(mesh_.color(_vh))
      : Vec4f(0, 0, 0, 0));
  }

146
  Vec2f  texcoord(VertexHandle _vh) const override
147 148
  {
#if defined(OM_CC_GCC) && (OM_CC_VERSION<30000)
149
    // Workaround!
150 151 152 153 154 155
    // gcc 2.95.3 exits with internal compiler error at the
    // code below!??? **)
    if (mesh_.has_vertex_texcoords2D())
      return vector_cast<Vec2f>(mesh_.texcoord2D(_vh));
    return Vec2f(0.0f, 0.0f);
#else // **)
156 157
    return (mesh_.has_vertex_texcoords2D()
	    ? vector_cast<Vec2f>(mesh_.texcoord2D(_vh))
158 159 160
	    : Vec2f(0.0f, 0.0f));
#endif
  }
161

162
  Vec2f  texcoord(HalfedgeHandle _heh) const override
163 164 165 166 167 168
  {
    return (mesh_.has_halfedge_texcoords2D()
        ? vector_cast<Vec2f>(mesh_.texcoord2D(_heh))
        : Vec2f(0.0f, 0.0f));
  }

169
  OpenMesh::Attributes::StatusInfo  status(VertexHandle _vh) const override
170 171 172 173 174 175
  {
    if (mesh_.has_vertex_status())
      return mesh_.status(_vh);
    return OpenMesh::Attributes::StatusInfo();
  }

176
  // get edge data
177

178
  Vec3uc color(EdgeHandle _eh)    const override
179
  {
180 181
      return (mesh_.has_edge_colors()
      ? color_cast<Vec3uc>(mesh_.color(_eh))
182 183
      : Vec3uc(0, 0, 0));
  }
184

185
  Vec4uc colorA(EdgeHandle _eh)   const override
186
  {
187 188
      return (mesh_.has_edge_colors()
      ? color_cast<Vec4uc>(mesh_.color(_eh))
189 190
      : Vec4uc(0, 0, 0, 0));
  }
191

192
  Vec3ui colori(EdgeHandle _eh)    const override
193 194 195 196 197 198
  {
      return (mesh_.has_edge_colors()
      ? color_cast<Vec3ui>(mesh_.color(_eh))
      : Vec3ui(0, 0, 0));
  }

199
  Vec4ui colorAi(EdgeHandle _eh)   const override
200 201 202 203 204 205
  {
      return (mesh_.has_edge_colors()
      ? color_cast<Vec4ui>(mesh_.color(_eh))
      : Vec4ui(0, 0, 0, 0));
  }

206
  Vec3f colorf(EdgeHandle _eh)    const override
207 208 209 210 211 212
  {
    return (mesh_.has_vertex_colors()
	    ? color_cast<Vec3f>(mesh_.color(_eh))
	    : Vec3f(0, 0, 0));
  }

213
  Vec4f colorAf(EdgeHandle _eh)   const override
214 215 216 217 218 219
  {
    return (mesh_.has_vertex_colors()
      ? color_cast<Vec4f>(mesh_.color(_eh))
      : Vec4f(0, 0, 0, 0));
  }

220
  OpenMesh::Attributes::StatusInfo  status(EdgeHandle _eh) const override
221
  {
222 223 224
    if (mesh_.has_edge_status())
      return mesh_.status(_eh);
    return OpenMesh::Attributes::StatusInfo();
225 226
  }

227 228
  // get halfedge data

229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
  int get_halfedge_id(VertexHandle _vh) override
  {
    return mesh_.halfedge_handle(_vh).idx();
  }

  int get_halfedge_id(FaceHandle _fh) override
  {
    return mesh_.halfedge_handle(_fh).idx();
  }

  int get_next_halfedge_id(HalfedgeHandle _heh) override
  {
    return mesh_.next_halfedge_handle(_heh).idx();
  }

  int get_to_vertex_id(HalfedgeHandle _heh) override
  {
    return mesh_.to_vertex_handle(_heh).idx();
  }

  int get_face_id(HalfedgeHandle _heh) override
  {
    return mesh_.face_handle(_heh).idx();
  }

254
  OpenMesh::Attributes::StatusInfo  status(HalfedgeHandle _heh) const override
255 256 257 258 259 260 261 262 263
  {
    if (mesh_.has_halfedge_status())
      return mesh_.status(_heh);
    return OpenMesh::Attributes::StatusInfo();
  }

  // get face data

  unsigned int get_vhandles(FaceHandle _fh,
264
			    std::vector<VertexHandle>& _vhandles) const override
265 266 267 268 269 270 271 272 273 274 275
  {
    unsigned int count(0);
    _vhandles.clear();
    for (typename Mesh::CFVIter fv_it=mesh_.cfv_iter(_fh); fv_it.is_valid(); ++fv_it)
    {
      _vhandles.push_back(*fv_it);
      ++count;
    }
    return count;
  }

276
  unsigned int get_face_texcoords(std::vector<Vec2f>& _hehandles) const override
277 278 279 280 281 282 283 284 285 286 287 288 289
  {
    unsigned int count(0);
    _hehandles.clear();
    for(typename Mesh::CHIter he_it=mesh_.halfedges_begin();
        he_it != mesh_.halfedges_end(); ++he_it)
    {
      _hehandles.push_back(vector_cast<Vec2f>(mesh_.texcoord2D( *he_it)));
      ++count;
    }

    return count;
  }

290
  HalfedgeHandle getHeh(FaceHandle _fh, VertexHandle _vh) const override
291 292 293 294 295 296 297 298 299 300
  {
    typename Mesh::ConstFaceHalfedgeIter fh_it;
    for(fh_it = mesh_.cfh_iter(_fh); fh_it.is_valid();++fh_it)
    {
      if(mesh_.to_vertex_handle(*fh_it) == _vh)
        return *fh_it;
    }
    return *fh_it;
  }

301
  Vec3f  normal(FaceHandle _fh)   const override
302 303 304
  {
    return (mesh_.has_face_normals()
            ? vector_cast<Vec3f>(mesh_.normal(_fh))
305 306 307
            : Vec3f(0.0f, 0.0f, 0.0f));
  }

308
  Vec3uc  color(FaceHandle _fh)   const override
309 310 311
  {
    return (mesh_.has_face_colors()
            ? color_cast<Vec3uc>(mesh_.color(_fh))
312 313 314
            : Vec3uc(0, 0, 0));
  }

315
  Vec4uc  colorA(FaceHandle _fh)   const override
316 317 318
  {
    return (mesh_.has_face_colors()
            ? color_cast<Vec4uc>(mesh_.color(_fh))
319 320 321
            : Vec4uc(0, 0, 0, 0));
  }

322
  Vec3ui  colori(FaceHandle _fh)   const override
323 324 325 326 327 328
  {
    return (mesh_.has_face_colors()
            ? color_cast<Vec3ui>(mesh_.color(_fh))
            : Vec3ui(0, 0, 0));
  }

329
  Vec4ui  colorAi(FaceHandle _fh)   const override
330 331 332 333 334 335
  {
    return (mesh_.has_face_colors()
            ? color_cast<Vec4ui>(mesh_.color(_fh))
            : Vec4ui(0, 0, 0, 0));
  }

336
  Vec3f colorf(FaceHandle _fh)    const override
337
  {
338
    return (mesh_.has_face_colors()
339 340 341 342
	    ? color_cast<Vec3f>(mesh_.color(_fh))
	    : Vec3f(0, 0, 0));
  }

343
  Vec4f colorAf(FaceHandle _fh)   const override
344
  {
345
    return (mesh_.has_face_colors()
346 347 348 349
      ? color_cast<Vec4f>(mesh_.color(_fh))
      : Vec4f(0, 0, 0, 0));
  }

350
  OpenMesh::Attributes::StatusInfo  status(FaceHandle _fh) const override
351 352 353 354 355 356
  {
    if (mesh_.has_face_status())
      return mesh_.status(_fh);
    return OpenMesh::Attributes::StatusInfo();
  }

357
  virtual const BaseKernel* kernel() override { return &mesh_; }
358 359 360


  // query number of faces, vertices, normals, texcoords
361 362 363
  size_t n_vertices()  const override { return mesh_.n_vertices(); }
  size_t n_faces()     const override { return mesh_.n_faces(); }
  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 371 372 373 374 375 376 377 378 379
  bool has_vertex_normals()   const override { return mesh_.has_vertex_normals();   }
  bool has_vertex_colors()    const override { return mesh_.has_vertex_colors();    }
  bool has_vertex_texcoords() const override { return mesh_.has_vertex_texcoords2D(); }
  bool has_vertex_status()    const override { return mesh_.has_vertex_status();    }
  bool has_edge_colors()      const override { return mesh_.has_edge_colors();      }
  bool has_edge_status()      const override { return mesh_.has_edge_status();      }
  bool has_halfedge_status()  const override { return mesh_.has_halfedge_status();  }
  bool has_face_normals()     const override { return mesh_.has_face_normals();     }
  bool has_face_colors()      const override { return mesh_.has_face_colors();      }
  bool has_face_status()      const override { return mesh_.has_face_status();      }
380 381

private:
382

383 384 385 386 387 388 389 390 391 392
   const Mesh& mesh_;
};


//=============================================================================
} // namespace IO
} // namespace OpenMesh
//=============================================================================
#endif
//=============================================================================