ExporterT.hh 9.29 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-2012 by Computer Graphics Group, RWTH Aachen      *
Jan Möbius's avatar
Jan Möbius committed
5 6
 *                           www.openmesh.org                                *
 *                                                                           *
7
 *---------------------------------------------------------------------------*
8
 *  This file is part of OpenMesh.                                           *
Jan Möbius's avatar
Jan Möbius committed
9
 *                                                                           *
10
 *  OpenMesh is free software: you can redistribute it and/or modify         *
11 12 13 14
 *  it under the terms of the GNU Lesser General Public License as           *
 *  published by the Free Software Foundation, either version 3 of           *
 *  the License, or (at your option) any later version with the              *
 *  following exceptions:                                                    *
Jan Möbius's avatar
Jan Möbius committed
15
 *                                                                           *
16 17 18 19 20 21 22
 *  If other files instantiate templates or use macros                       *
 *  or inline functions from this file, or you compile this file and         *
 *  link it with other files to produce an executable, this file does        *
 *  not by itself cause the resulting executable to be covered by the        *
 *  GNU Lesser General Public License. This exception does not however       *
 *  invalidate any other reasons why the executable file might be            *
 *  covered by the GNU Lesser General Public License.                        *
Jan Möbius's avatar
Jan Möbius committed
23
 *                                                                           *
24 25 26 27
 *  OpenMesh is distributed in the hope that it will be useful,              *
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of           *
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            *
 *  GNU Lesser General Public License for more details.                      *
Jan Möbius's avatar
Jan Möbius committed
28
 *                                                                           *
29 30 31 32
 *  You should have received a copy of the GNU LesserGeneral Public          *
 *  License along with OpenMesh.  If not,                                    *
 *  see <http://www.gnu.org/licenses/>.                                      *
 *                                                                           *
33
\*===========================================================================*/
34 35

/*===========================================================================*\
36
 *                                                                           *
37 38
 *   $Revision$                                                         *
 *   $Date$                   *
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
 *                                                                           *
\*===========================================================================*/


//=============================================================================
//
//  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
  ExporterT(const Mesh& _mesh) : mesh_(_mesh) {}
86

Jan Möbius's avatar
Jan Möbius committed
87 88 89

  // get vertex data

90 91 92
  Vec3f  point(VertexHandle _vh)    const
  {
    return vector_cast<Vec3f>(mesh_.point(_vh));
Jan Möbius's avatar
Jan Möbius committed
93 94
  }

95 96 97 98
  Vec3f  normal(VertexHandle _vh)   const
  {
    return (mesh_.has_vertex_normals()
	    ? vector_cast<Vec3f>(mesh_.normal(_vh))
Jan Möbius's avatar
Jan Möbius committed
99 100 101 102 103
	    : Vec3f(0.0f, 0.0f, 0.0f));
  }

  Vec3uc color(VertexHandle _vh)    const
  {
104 105
    return (mesh_.has_vertex_colors()
	    ? color_cast<Vec3uc>(mesh_.color(_vh))
Jan Möbius's avatar
Jan Möbius committed
106 107 108 109 110
	    : Vec3uc(0, 0, 0));
  }

  Vec4uc colorA(VertexHandle _vh)   const
  {
111 112
    return (mesh_.has_vertex_colors()
      ? color_cast<Vec4uc>(mesh_.color(_vh))
Jan Möbius's avatar
Jan Möbius committed
113 114 115
      : Vec4uc(0, 0, 0, 0));
  }

116 117 118 119 120 121 122 123 124 125 126 127 128 129
  Vec3ui colori(VertexHandle _vh)    const
  {
    return (mesh_.has_vertex_colors()
	    ? color_cast<Vec3ui>(mesh_.color(_vh))
	    : Vec3ui(0, 0, 0));
  }

  Vec4ui colorAi(VertexHandle _vh)   const
  {
    return (mesh_.has_vertex_colors()
      ? color_cast<Vec4ui>(mesh_.color(_vh))
      : Vec4ui(0, 0, 0, 0));
  }

130 131 132 133 134 135 136 137 138 139 140 141 142 143
  Vec3f colorf(VertexHandle _vh)    const
  {
    return (mesh_.has_vertex_colors()
	    ? color_cast<Vec3f>(mesh_.color(_vh))
	    : Vec3f(0, 0, 0));
  }

  Vec4f colorAf(VertexHandle _vh)   const
  {
    return (mesh_.has_vertex_colors()
      ? color_cast<Vec4f>(mesh_.color(_vh))
      : Vec4f(0, 0, 0, 0));
  }

Jan Möbius's avatar
Jan Möbius committed
144 145 146
  Vec2f  texcoord(VertexHandle _vh) const
  {
#if defined(OM_CC_GCC) && (OM_CC_VERSION<30000)
147
    // Workaround!
Jan Möbius's avatar
Jan Möbius committed
148 149 150 151 152 153
    // 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 // **)
154 155
    return (mesh_.has_vertex_texcoords2D()
	    ? vector_cast<Vec2f>(mesh_.texcoord2D(_vh))
Jan Möbius's avatar
Jan Möbius committed
156 157 158
	    : Vec2f(0.0f, 0.0f));
#endif
  }
159

160
  // get edge data
161

162 163
  Vec3uc color(EdgeHandle _eh)    const
  {
164 165
      return (mesh_.has_edge_colors()
      ? color_cast<Vec3uc>(mesh_.color(_eh))
166 167
      : Vec3uc(0, 0, 0));
  }
168

169 170
  Vec4uc colorA(EdgeHandle _eh)   const
  {
171 172
      return (mesh_.has_edge_colors()
      ? color_cast<Vec4uc>(mesh_.color(_eh))
173 174
      : Vec4uc(0, 0, 0, 0));
  }
Jan Möbius's avatar
Jan Möbius committed
175

176 177 178 179 180 181 182 183 184 185 186 187 188 189
  Vec3ui colori(EdgeHandle _eh)    const
  {
      return (mesh_.has_edge_colors()
      ? color_cast<Vec3ui>(mesh_.color(_eh))
      : Vec3ui(0, 0, 0));
  }

  Vec4ui colorAi(EdgeHandle _eh)   const
  {
      return (mesh_.has_edge_colors()
      ? color_cast<Vec4ui>(mesh_.color(_eh))
      : Vec4ui(0, 0, 0, 0));
  }

190 191 192 193 194 195 196 197 198 199 200 201 202 203
  Vec3f colorf(EdgeHandle _eh)    const
  {
    return (mesh_.has_vertex_colors()
	    ? color_cast<Vec3f>(mesh_.color(_eh))
	    : Vec3f(0, 0, 0));
  }

  Vec4f colorAf(EdgeHandle _eh)   const
  {
    return (mesh_.has_vertex_colors()
      ? color_cast<Vec4f>(mesh_.color(_eh))
      : Vec4f(0, 0, 0, 0));
  }

Jan Möbius's avatar
Jan Möbius committed
204 205
  // get face data

206
  unsigned int get_vhandles(FaceHandle _fh,
Jan Möbius's avatar
Jan Möbius committed
207 208 209 210 211 212 213 214 215 216 217 218
			    std::vector<VertexHandle>& _vhandles) const
  {
    unsigned int count(0);
    _vhandles.clear();
    for (typename Mesh::CFVIter fv_it=mesh_.cfv_iter(_fh); fv_it; ++fv_it)
    {
      _vhandles.push_back(fv_it.handle());
      ++count;
    }
    return count;
  }

219 220 221 222
  Vec3f  normal(FaceHandle _fh)   const
  {
    return (mesh_.has_face_normals()
            ? vector_cast<Vec3f>(mesh_.normal(_fh))
Jan Möbius's avatar
Jan Möbius committed
223 224 225
            : Vec3f(0.0f, 0.0f, 0.0f));
  }

226 227 228 229
  Vec3uc  color(FaceHandle _fh)   const
  {
    return (mesh_.has_face_colors()
            ? color_cast<Vec3uc>(mesh_.color(_fh))
Jan Möbius's avatar
Jan Möbius committed
230 231 232
            : Vec3uc(0, 0, 0));
  }

233 234 235 236
  Vec4uc  colorA(FaceHandle _fh)   const
  {
    return (mesh_.has_face_colors()
            ? color_cast<Vec4uc>(mesh_.color(_fh))
Jan Möbius's avatar
Jan Möbius committed
237 238 239
            : Vec4uc(0, 0, 0, 0));
  }

240 241 242 243 244 245 246 247 248 249 250 251 252 253
  Vec3ui  colori(FaceHandle _fh)   const
  {
    return (mesh_.has_face_colors()
            ? color_cast<Vec3ui>(mesh_.color(_fh))
            : Vec3ui(0, 0, 0));
  }

  Vec4ui  colorAi(FaceHandle _fh)   const
  {
    return (mesh_.has_face_colors()
            ? color_cast<Vec4ui>(mesh_.color(_fh))
            : Vec4ui(0, 0, 0, 0));
  }

254 255 256 257 258 259 260 261 262 263 264 265 266 267
  Vec3f colorf(FaceHandle _fh)    const
  {
    return (mesh_.has_vertex_colors()
	    ? color_cast<Vec3f>(mesh_.color(_fh))
	    : Vec3f(0, 0, 0));
  }

  Vec4f colorAf(FaceHandle _fh)   const
  {
    return (mesh_.has_vertex_colors()
      ? color_cast<Vec4f>(mesh_.color(_fh))
      : Vec4f(0, 0, 0, 0));
  }

Jan Möbius's avatar
Jan Möbius committed
268 269 270 271
  virtual const BaseKernel* kernel() { return &mesh_; }


  // query number of faces, vertices, normals, texcoords
272
  size_t n_vertices()  const { return mesh_.n_vertices(); }
Jan Möbius's avatar
Jan Möbius committed
273 274 275 276 277 278 279
  size_t n_faces()     const { return mesh_.n_faces(); }
  size_t n_edges()     const { return mesh_.n_edges(); }


  // property information
  bool is_triangle_mesh() const
  { return Mesh::is_triangles(); }
280

Jan Möbius's avatar
Jan Möbius committed
281 282 283
  bool has_vertex_normals()   const { return mesh_.has_vertex_normals();   }
  bool has_vertex_colors()    const { return mesh_.has_vertex_colors();    }
  bool has_vertex_texcoords() const { return mesh_.has_vertex_texcoords2D(); }
284
  bool has_edge_colors()      const { return mesh_.has_edge_colors();      }
Jan Möbius's avatar
Jan Möbius committed
285 286 287 288
  bool has_face_normals()     const { return mesh_.has_face_normals();     }
  bool has_face_colors()      const { return mesh_.has_face_colors();      }

private:
289

Jan Möbius's avatar
Jan Möbius committed
290 291 292 293 294 295 296 297 298 299
   const Mesh& mesh_;
};


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