ImporterT.hh 13.9 KB
Newer Older
Jan Möbius's avatar
Jan Möbius committed
1
/* ========================================================================= *
Jan Möbius's avatar
Jan Möbius committed
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
Typo    
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                               *
Jan Möbius's avatar
Jan Möbius committed
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
 *   $Revision$                                                         *
 *   $Date$                   *
Jan Möbius's avatar
Jan Möbius committed
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
87
88
89
90
91
92
 *                                                                           *
\*===========================================================================*/


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


#ifndef __IMPORTERT_HH__
#define __IMPORTERT_HH__


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


#include <OpenMesh/Core/IO/importer/BaseImporter.hh>
#include <OpenMesh/Core/Utils/vector_cast.hh>
#include <OpenMesh/Core/Utils/color_cast.hh>
#include <OpenMesh/Core/Mesh/Attributes.hh>
#include <OpenMesh/Core/System/omstream.hh>


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


namespace OpenMesh {
namespace IO {


//=== IMPLEMENTATION ==========================================================


/**
 *  This class template provides an importer module for OpenMesh meshes.
 */
template <class Mesh>
class ImporterT : public BaseImporter
{
public:

  typedef typename Mesh::Point       Point;
  typedef typename Mesh::Normal      Normal;
  typedef typename Mesh::Color       Color;
  typedef typename Mesh::TexCoord2D  TexCoord2D;
93
  typedef typename Mesh::TexCoord3D  TexCoord3D;
Jan Möbius's avatar
Jan Möbius committed
94
95
96
  typedef std::vector<VertexHandle>  VHandles;


97
  ImporterT(Mesh& _mesh) : mesh_(_mesh), halfedgeNormals_() {}
Jan Möbius's avatar
Jan Möbius committed
98
99
100
101
102
103
104


  virtual VertexHandle add_vertex(const Vec3f& _point)
  {
    return mesh_.add_vertex(vector_cast<Point>(_point));
  }

105
106
107
108
  virtual VertexHandle add_vertex()
  {
    return mesh_.new_vertex();
  }
Jan Möbius's avatar
Jan Möbius committed
109
110
111
112
113
114
115
116
117

  virtual FaceHandle add_face(const VHandles& _indices)
  {
    FaceHandle fh;

    if (_indices.size() > 2)
    {
      VHandles::const_iterator it, it2, end(_indices.end());

118

Jan Möbius's avatar
Jan Möbius committed
119
120
      // test for valid vertex indices
      for (it=_indices.begin(); it!=end; ++it)
Jan Möbius's avatar
Jan Möbius committed
121
122
123
124
125
        if (! mesh_.is_valid_handle(*it))
        {
          omerr() << "ImporterT: Face contains invalid vertex index\n";
          return fh;
        }
Jan Möbius's avatar
Jan Möbius committed
126
127
128
129


      // don't allow double vertices
      for (it=_indices.begin(); it!=end; ++it)
Jan Möbius's avatar
Jan Möbius committed
130
131
132
133
134
135
136
        for (it2=it+1; it2!=end; ++it2)
          if (*it == *it2)
          {
            omerr() << "ImporterT: Face has equal vertices\n";
            failed_faces_.push_back(_indices);
            return fh;
          }
Jan Möbius's avatar
Jan Möbius committed
137
138
139
140
141
142


      // try to add face
      fh = mesh_.add_face(_indices);
      if (!fh.is_valid())
      {
Jan Möbius's avatar
Jan Möbius committed
143
144
        failed_faces_.push_back(_indices);
        return fh;
Jan Möbius's avatar
Jan Möbius committed
145
146
      }

147
148
149
150
151
152
153
154
      //write the half edge normals
      if (mesh_.has_halfedge_normals())
      {
        //iterate over all incoming haldedges of the added face
        for (typename Mesh::FaceHalfedgeIter fh_iter = mesh_.fh_begin(fh);
            fh_iter != mesh_.fh_end(fh); ++fh_iter)
        {
          //and write the normals to it
155
          typename Mesh::HalfedgeHandle heh = *fh_iter;
156
157
158
159
160
161
162
163
          typename Mesh::VertexHandle vh = mesh_.to_vertex_handle(heh);
          typename std::map<VertexHandle,Normal>::iterator it_heNs = halfedgeNormals_.find(vh);
          if (it_heNs != halfedgeNormals_.end())
            mesh_.set_normal(heh,it_heNs->second);
        }
        halfedgeNormals_.clear();
      }
    }
Jan Möbius's avatar
Jan Möbius committed
164
165
166
167
168
    return fh;
  }

  // vertex attributes

169
170
171
172
173
  virtual void set_point(VertexHandle _vh, const Vec3f& _point)
  {
    mesh_.set_point(_vh,vector_cast<Point>(_point));
  }

Jan Möbius's avatar
Jan Möbius committed
174
175
176
177
  virtual void set_normal(VertexHandle _vh, const Vec3f& _normal)
  {
    if (mesh_.has_vertex_normals())
      mesh_.set_normal(_vh, vector_cast<Normal>(_normal));
178
179
180
181

    //saves normals for half edges.
    //they will be written, when the face is added
    if (mesh_.has_halfedge_normals())
182
      halfedgeNormals_[_vh] = vector_cast<Normal>(_normal);
Jan Möbius's avatar
Jan Möbius committed
183
184
185
186
187
188
189
190
191
192
193
194
195
196
  }

  virtual void set_color(VertexHandle _vh, const Vec4uc& _color)
  {
    if (mesh_.has_vertex_colors())
      mesh_.set_color(_vh, color_cast<Color>(_color));
  }

  virtual void set_color(VertexHandle _vh, const Vec3uc& _color)
  {
    if (mesh_.has_vertex_colors())
      mesh_.set_color(_vh, color_cast<Color>(_color));
  }

197
198
199
200
201
202
203
204
205
206
207
  virtual void set_color(VertexHandle _vh, const Vec4f& _color)
  {
    if (mesh_.has_vertex_colors())
      mesh_.set_color(_vh, color_cast<Color>(_color));
  }

  virtual void set_color(VertexHandle _vh, const Vec3f& _color)
  {
    if (mesh_.has_vertex_colors())
      mesh_.set_color(_vh, color_cast<Color>(_color));
  }
Jan Möbius's avatar
Jan Möbius committed
208
209
210
211

  virtual void set_texcoord(VertexHandle _vh, const Vec2f& _texcoord)
  {
    if (mesh_.has_vertex_texcoords2D())
Jan Möbius's avatar
Jan Möbius committed
212
      mesh_.set_texcoord2D(_vh, vector_cast<TexCoord2D>(_texcoord));
213
214
215
216
217
218
  }

  virtual void set_texcoord(HalfedgeHandle _heh, const Vec2f& _texcoord)
  {
    if (mesh_.has_halfedge_texcoords2D())
      mesh_.set_texcoord2D(_heh, vector_cast<TexCoord2D>(_texcoord));
Jan Möbius's avatar
Jan Möbius committed
219
220
  }

221
222
223
224
225
226
227
228
229
230
231
232
233
  virtual void set_texcoord(VertexHandle _vh, const Vec3f& _texcoord)
  {
    if (mesh_.has_vertex_texcoords3D())
      mesh_.set_texcoord3D(_vh, vector_cast<TexCoord3D>(_texcoord));
  }

  virtual void set_texcoord(HalfedgeHandle _heh, const Vec3f& _texcoord)
  {
    if (mesh_.has_halfedge_texcoords3D())
      mesh_.set_texcoord3D(_heh, vector_cast<TexCoord3D>(_texcoord));
  }


234
  // edge attributes
235

236
237
238
239
240
  virtual void set_color(EdgeHandle _eh, const Vec4uc& _color)
  {
      if (mesh_.has_edge_colors())
          mesh_.set_color(_eh, color_cast<Color>(_color));
  }
241

242
243
244
245
246
  virtual void set_color(EdgeHandle _eh, const Vec3uc& _color)
  {
      if (mesh_.has_edge_colors())
          mesh_.set_color(_eh, color_cast<Color>(_color));
  }
Jan Möbius's avatar
Jan Möbius committed
247

248
249
250
251
252
253
254
255
256
257
258
259
  virtual void set_color(EdgeHandle _eh, const Vec4f& _color)
  {
      if (mesh_.has_edge_colors())
          mesh_.set_color(_eh, color_cast<Color>(_color));
  }

  virtual void set_color(EdgeHandle _eh, const Vec3f& _color)
  {
      if (mesh_.has_edge_colors())
          mesh_.set_color(_eh, color_cast<Color>(_color));
  }

Jan Möbius's avatar
Jan Möbius committed
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
  // face attributes

  virtual void set_normal(FaceHandle _fh, const Vec3f& _normal)
  {
    if (mesh_.has_face_normals())
      mesh_.set_normal(_fh, vector_cast<Normal>(_normal));
  }

  virtual void set_color(FaceHandle _fh, const Vec3uc& _color)
  {
    if (mesh_.has_face_colors())
      mesh_.set_color(_fh, color_cast<Color>(_color));
  }

  virtual void set_color(FaceHandle _fh, const Vec4uc& _color)
  {
    if (mesh_.has_face_colors())
      mesh_.set_color(_fh, color_cast<Color>(_color));
  }

280
281
282
283
284
285
286
287
288
289
290
291
  virtual void set_color(FaceHandle _fh, const Vec3f& _color)
  {
    if (mesh_.has_face_colors())
      mesh_.set_color(_fh, color_cast<Color>(_color));
  }

  virtual void set_color(FaceHandle _fh, const Vec4f& _color)
  {
    if (mesh_.has_face_colors())
      mesh_.set_color(_fh, color_cast<Color>(_color));
  }

292
293
294
295
296
297
298
299
300
301
302
  virtual void add_face_texcoords( FaceHandle _fh, VertexHandle _vh, const std::vector<Vec2f>& _face_texcoords)
  {
    // get first halfedge handle
    HalfedgeHandle cur_heh   = mesh_.halfedge_handle(_fh);
    HalfedgeHandle end_heh   = mesh_.prev_halfedge_handle(cur_heh);

    // find start heh
    while( mesh_.to_vertex_handle(cur_heh) != _vh && cur_heh != end_heh )
      cur_heh = mesh_.next_halfedge_handle( cur_heh);

    for(unsigned int i=0; i<_face_texcoords.size(); ++i)
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
    {
      set_texcoord( cur_heh, _face_texcoords[i]);
      cur_heh = mesh_.next_halfedge_handle( cur_heh);
    }
  }

  virtual void add_face_texcoords( FaceHandle _fh, VertexHandle _vh, const std::vector<Vec3f>& _face_texcoords)
  {
    // get first halfedge handle
    HalfedgeHandle cur_heh   = mesh_.halfedge_handle(_fh);
    HalfedgeHandle end_heh   = mesh_.prev_halfedge_handle(cur_heh);

    // find start heh
    while( mesh_.to_vertex_handle(cur_heh) != _vh && cur_heh != end_heh )
      cur_heh = mesh_.next_halfedge_handle( cur_heh);

    for(unsigned int i=0; i<_face_texcoords.size(); ++i)
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
    {
      set_texcoord( cur_heh, _face_texcoords[i]);
      cur_heh = mesh_.next_halfedge_handle( cur_heh);
    }
  }

  virtual void set_face_texindex( FaceHandle _fh, int _texId ) {
    if ( mesh_.has_face_texture_index() ) {
      mesh_.set_texture_index(_fh , _texId);
    }
  }

  virtual void add_texture_information( int _id , std::string _name ) {
    OpenMesh::MPropHandleT< std::map< int, std::string > > property;

    if ( !mesh_.get_property_handle(property,"TextureMapping") ) {
      mesh_.add_property(property,"TextureMapping");
    }

    if ( mesh_.property(property).find( _id ) == mesh_.property(property).end() )
      mesh_.property(property)[_id] = _name;
  }

Jan Möbius's avatar
Jan Möbius committed
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
  // low-level access to mesh

  virtual BaseKernel* kernel() { return &mesh_; }

  bool is_triangle_mesh() const
  { return Mesh::is_triangles(); }

  void reserve(unsigned int nV, unsigned int nE, unsigned int nF)
  {
    mesh_.reserve(nV, nE, nF);
  }

  // query number of faces, vertices, normals, texcoords
  size_t n_vertices()  const { return mesh_.n_vertices(); }
  size_t n_faces()     const { return mesh_.n_faces(); }
  size_t n_edges()     const { return mesh_.n_edges(); }


  void prepare() { failed_faces_.clear(); }


  void finish()
  {
    if (!failed_faces_.empty())
    {
      omerr() << failed_faces_.size()
	    << " faces failed, adding them as isolated faces\n";

      for (unsigned int i=0; i<failed_faces_.size(); ++i)
      {
373
374
375
376
377
378
379
380
381
        VHandles&  vhandles = failed_faces_[i];

        // double vertices
        for (unsigned int j=0; j<vhandles.size(); ++j)
        {
          Point p = mesh_.point(vhandles[j]);
          vhandles[j] = mesh_.add_vertex(p);
          // DO STORE p, reference may not work since vertex array
          // may be relocated after adding a new vertex !
382

383
384
385
386
387
388
389
390
          // Mark vertices of failed face as non-manifold
          if (mesh_.has_vertex_status()) {
              mesh_.status(vhandles[j]).set_fixed_nonmanifold(true);
          }
        }

        // add face
        FaceHandle fh = mesh_.add_face(vhandles);
391

392
393
394
        // Mark failed face as non-manifold
        if (mesh_.has_face_status())
            mesh_.status(fh).set_fixed_nonmanifold(true);
395

396
397
398
        // Mark edges of failed face as non-two-manifold
        if (mesh_.has_edge_status()) {
            typename Mesh::FaceEdgeIter fe_it = mesh_.fe_iter(fh);
Jan Möbius's avatar
Jan Möbius committed
399
400
            for(; fe_it.is_valid(); ++fe_it) {
                mesh_.status(*fe_it).set_fixed_nonmanifold(true);
401
402
            }
        }
Jan Möbius's avatar
Jan Möbius committed
403
404
405
406
407
408
409
410
411
412
413
414
      }

      failed_faces_.clear();
    }
  }



private:

  Mesh& mesh_;
  std::vector<VHandles>  failed_faces_;
415
416
  // stores normals for halfedges of the next face
  std::map<VertexHandle,Normal> halfedgeNormals_;
Jan Möbius's avatar
Jan Möbius committed
417
418
419
420
421
422
423
424
425
};


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