| OpenMesh

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
ImporterT.hh
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 * $Revision: 1258 $ *
45 * $Date: 2015-04-28 15:07:46 +0200 (Di, 28 Apr 2015) $ *
46 * *
47 \*===========================================================================*/
48
49
50 //=============================================================================
51 //
52 // Implements an importer module for arbitrary OpenMesh meshes
53 //
54 //=============================================================================
55
56
57 #ifndef __IMPORTERT_HH__
58 #define __IMPORTERT_HH__
59
60
61 //=== INCLUDES ================================================================
62
63
64 #include <OpenMesh/Core/IO/importer/BaseImporter.hh>
65 #include <OpenMesh/Core/Utils/vector_cast.hh>
66 #include <OpenMesh/Core/Utils/color_cast.hh>
67 #include <OpenMesh/Core/Mesh/Attributes.hh>
68 #include <OpenMesh/Core/System/omstream.hh>
69
70
71 //== NAMESPACES ===============================================================
72
73
74 namespace OpenMesh {
75 namespace IO {
76
77
78 //=== IMPLEMENTATION ==========================================================
79
80
84 template <class Mesh>
85 class ImporterT : public BaseImporter
86 {
87 public:
88
89 typedef typename Mesh::Point Point;
90 typedef typename Mesh::Normal Normal;
91 typedef typename Mesh::Color Color;
92 typedef typename Mesh::TexCoord2D TexCoord2D;
93 typedef std::vector<VertexHandle> VHandles;
94
95
96 ImporterT(Mesh& _mesh) : mesh_(_mesh), halfedgeNormals_() {}
97
98
99 virtual VertexHandle add_vertex(const Vec3f& _point)
100 {
101 return mesh_.add_vertex(vector_cast<Point>(_point));
102 }
103
104 virtual VertexHandle add_vertex()
105 {
106 return mesh_.new_vertex();
107 }
108
109 virtual FaceHandle add_face(const VHandles& _indices)
110 {
111 FaceHandle fh;
112
113 if (_indices.size() > 2)
114 {
115 VHandles::const_iterator it, it2, end(_indices.end());
116
117
118 // test for valid vertex indices
119 for (it=_indices.begin(); it!=end; ++it)
120 if (! mesh_.is_valid_handle(*it))
121 {
122 omerr() << "ImporterT: Face contains invalid vertex index\n";
123 return fh;
124 }
125
126
127 // don't allow double vertices
128 for (it=_indices.begin(); it!=end; ++it)
129 for (it2=it+1; it2!=end; ++it2)
130 if (*it == *it2)
131 {
132 omerr() << "ImporterT: Face has equal vertices\n";
133 failed_faces_.push_back(_indices);
134 return fh;
135 }
136
137
138 // try to add face
139 fh = mesh_.add_face(_indices);
140 if (!fh.is_valid())
141 {
142 failed_faces_.push_back(_indices);
143 return fh;
144 }
145
146 //write the half edge normals
147 if (mesh_.has_halfedge_normals())
148 {
149 //iterate over all incoming haldedges of the added face
150 for (typename Mesh::FaceHalfedgeIter fh_iter = mesh_.fh_begin(fh);
151 fh_iter != mesh_.fh_end(fh); ++fh_iter)
152 {
153 //and write the normals to it
154 typename Mesh::HalfedgeHandle heh = *fh_iter;
155 typename Mesh::VertexHandle vh = mesh_.to_vertex_handle(heh);
156 typename std::map<VertexHandle,Normal>::iterator it_heNs = halfedgeNormals_.find(vh);
157 if (it_heNs != halfedgeNormals_.end())
158 mesh_.set_normal(heh,it_heNs->second);
159 }
160 halfedgeNormals_.clear();
161 }
162 }
163 return fh;
164 }
165
166 // vertex attributes
167
168 virtual void set_point(VertexHandle _vh, const Vec3f& _point)
169 {
170 mesh_.set_point(_vh,vector_cast<Point>(_point));
171 }
172
173 virtual void set_normal(VertexHandle _vh, const Vec3f& _normal)
174 {
175 if (mesh_.has_vertex_normals())
176 mesh_.set_normal(_vh, vector_cast<Normal>(_normal));
177
178 //saves normals for half edges.
179 //they will be written, when the face is added
180 if (mesh_.has_halfedge_normals())
181 halfedgeNormals_[_vh] = vector_cast<Normal>(_normal);
182 }
183
184 virtual void set_color(VertexHandle _vh, const Vec4uc& _color)
185 {
186 if (mesh_.has_vertex_colors())
187 mesh_.set_color(_vh, color_cast<Color>(_color));
188 }
189
190 virtual void set_color(VertexHandle _vh, const Vec3uc& _color)
191 {
192 if (mesh_.has_vertex_colors())
193 mesh_.set_color(_vh, color_cast<Color>(_color));
194 }
195
196 virtual void set_color(VertexHandle _vh, const Vec4f& _color)
197 {
198 if (mesh_.has_vertex_colors())
199 mesh_.set_color(_vh, color_cast<Color>(_color));
200 }
201
202 virtual void set_color(VertexHandle _vh, const Vec3f& _color)
203 {
204 if (mesh_.has_vertex_colors())
205 mesh_.set_color(_vh, color_cast<Color>(_color));
206 }
207
208 virtual void set_texcoord(VertexHandle _vh, const Vec2f& _texcoord)
209 {
210 if (mesh_.has_vertex_texcoords2D())
211 mesh_.set_texcoord2D(_vh, vector_cast<TexCoord2D>(_texcoord));
212 }
213
214 virtual void set_texcoord(HalfedgeHandle _heh, const Vec2f& _texcoord)
215 {
216 if (mesh_.has_halfedge_texcoords2D())
217 mesh_.set_texcoord2D(_heh, vector_cast<TexCoord2D>(_texcoord));
218 }
219
220 // edge attributes
221
222 virtual void set_color(EdgeHandle _eh, const Vec4uc& _color)
223 {
224 if (mesh_.has_edge_colors())
225 mesh_.set_color(_eh, color_cast<Color>(_color));
226 }
227
228 virtual void set_color(EdgeHandle _eh, const Vec3uc& _color)
229 {
230 if (mesh_.has_edge_colors())
231 mesh_.set_color(_eh, color_cast<Color>(_color));
232 }
233
234 virtual void set_color(EdgeHandle _eh, const Vec4f& _color)
235 {
236 if (mesh_.has_edge_colors())
237 mesh_.set_color(_eh, color_cast<Color>(_color));
238 }
239
240 virtual void set_color(EdgeHandle _eh, const Vec3f& _color)
241 {
242 if (mesh_.has_edge_colors())
243 mesh_.set_color(_eh, color_cast<Color>(_color));
244 }
245
246 // face attributes
247
248 virtual void set_normal(FaceHandle _fh, const Vec3f& _normal)
249 {
250 if (mesh_.has_face_normals())
251 mesh_.set_normal(_fh, vector_cast<Normal>(_normal));
252 }
253
254 virtual void set_color(FaceHandle _fh, const Vec3uc& _color)
255 {
256 if (mesh_.has_face_colors())
257 mesh_.set_color(_fh, color_cast<Color>(_color));
258 }
259
260 virtual void set_color(FaceHandle _fh, const Vec4uc& _color)
261 {
262 if (mesh_.has_face_colors())
263 mesh_.set_color(_fh, color_cast<Color>(_color));
264 }
265
266 virtual void set_color(FaceHandle _fh, const Vec3f& _color)
267 {
268 if (mesh_.has_face_colors())
269 mesh_.set_color(_fh, color_cast<Color>(_color));
270 }
271
272 virtual void set_color(FaceHandle _fh, const Vec4f& _color)
273 {
274 if (mesh_.has_face_colors())
275 mesh_.set_color(_fh, color_cast<Color>(_color));
276 }
277
278 virtual void add_face_texcoords( FaceHandle _fh, VertexHandle _vh, const std::vector<Vec2f>& _face_texcoords)
279 {
280 // get first halfedge handle
281 HalfedgeHandle cur_heh = mesh_.halfedge_handle(_fh);
282 HalfedgeHandle end_heh = mesh_.prev_halfedge_handle(cur_heh);
283
284 // find start heh
285 while( mesh_.to_vertex_handle(cur_heh) != _vh && cur_heh != end_heh )
286 cur_heh = mesh_.next_halfedge_handle( cur_heh);
287
288 for(unsigned int i=0; i<_face_texcoords.size(); ++i)
289 {
290 set_texcoord( cur_heh, _face_texcoords[i]);
291 cur_heh = mesh_.next_halfedge_handle( cur_heh);
292 }
293 }
294
295 virtual void set_face_texindex( FaceHandle _fh, int _texId ) {
296 if ( mesh_.has_face_texture_index() ) {
297 mesh_.set_texture_index(_fh , _texId);
298 }
299 }
300
301 virtual void add_texture_information( int _id , std::string _name ) {
302 OpenMesh::MPropHandleT< std::map< int, std::string > > property;
303
304 if ( !mesh_.get_property_handle(property,"TextureMapping") ) {
305 mesh_.add_property(property,"TextureMapping");
306 }
307
308 if ( mesh_.property(property).find( _id ) == mesh_.property(property).end() )
309 mesh_.property(property)[_id] = _name;
310 }
311
312 // low-level access to mesh
313
314 virtual BaseKernel* kernel() { return &mesh_; }
315
316 bool is_triangle_mesh() const
317 { return Mesh::is_triangles(); }
318
319 void reserve(unsigned int nV, unsigned int nE, unsigned int nF)
320 {
321 mesh_.reserve(nV, nE, nF);
322 }
323
324 // query number of faces, vertices, normals, texcoords
325 size_t n_vertices() const { return mesh_.n_vertices(); }
326 size_t n_faces() const { return mesh_.n_faces(); }
327 size_t n_edges() const { return mesh_.n_edges(); }
328
329
330 void prepare() { failed_faces_.clear(); }
331
332
333 void finish()
334 {
335 if (!failed_faces_.empty())
336 {
337 omerr() << failed_faces_.size()
338 << " faces failed, adding them as isolated faces\n";
339
340 for (unsigned int i=0; i<failed_faces_.size(); ++i)
341 {
342 VHandles& vhandles = failed_faces_[i];
343
344 // double vertices
345 for (unsigned int j=0; j<vhandles.size(); ++j)
346 {
347 Point p = mesh_.point(vhandles[j]);
348 vhandles[j] = mesh_.add_vertex(p);
349 // DO STORE p, reference may not work since vertex array
350 // may be relocated after adding a new vertex !
351
352 // Mark vertices of failed face as non-manifold
353 if (mesh_.has_vertex_status()) {
354 mesh_.status(vhandles[j]).set_fixed_nonmanifold(true);
355 }
356 }
357
358 // add face
359 FaceHandle fh = mesh_.add_face(vhandles);
360
361 // Mark failed face as non-manifold
362 if (mesh_.has_face_status())
363 mesh_.status(fh).set_fixed_nonmanifold(true);
364
365 // Mark edges of failed face as non-two-manifold
366 if (mesh_.has_edge_status()) {
367 typename Mesh::FaceEdgeIter fe_it = mesh_.fe_iter(fh);
368 for(; fe_it.is_valid(); ++fe_it) {
369 mesh_.status(*fe_it).set_fixed_nonmanifold(true);
370 }
371 }
372 }
373
374 failed_faces_.clear();
375 }
376 }
377
378
379
380 private:
381
382 Mesh& mesh_;
383 std::vector<VHandles> failed_faces_;
384 // stores normals for halfedges of the next face
385 std::map<VertexHandle,Normal> halfedgeNormals_;
386 };
387
388
389 //=============================================================================
390 } // namespace IO
391 } // namespace OpenMesh
392 //=============================================================================
393 #endif
394 //=============================================================================
OpenMesh::BaseHandle::is_valid
bool is_valid() const
The handle is valid iff the index is not equal to -1.
Definition: Handles.hh:77
OpenMesh::IO::BaseImporter
Base class for importer modules.
Definition: BaseImporter.hh:88
OpenMesh
Contains all the mesh ingredients like the polygonal mesh, the triangle mesh, different mesh kernels ...
Definition: MeshItems.hh:64
OpenMesh::EdgeHandle
Handle for a edge entity.
Definition: Handles.hh:135
OpenMesh::FaceHandle
Handle for a face entity.
Definition: Handles.hh:142
OpenMesh::HalfedgeHandle
Handle for a halfedge entity.
Definition: Handles.hh:128
OpenMesh::Attributes::Color
Add colors to mesh item (vertices/faces/edges)
Definition: Attributes.hh:88
OpenMesh::Attributes::Normal
Add normals to mesh item (vertices/faces)
Definition: Attributes.hh:87
OpenMesh::VertexHandle
Handle for a vertex entity.
Definition: Handles.hh:121
OpenMesh::MPropHandleT
Handle representing a mesh property.
Definition: Property.hh:543
OpenMesh::IO::ImporterT
This class template provides an importer module for OpenMesh meshes.
Definition: ImporterT.hh:85
omstream.hh
This file provides the streams omlog, omout, and omerr.
Attributes.hh
This file provides some macros containing attribute usage.
OpenMesh::BaseKernel
This class provides the basic property management like adding/removing properties and access to prope...
Definition: BaseKernel.hh:99
OpenMesh::Attributes::TexCoord2D
Add 2D texture coordinates (vertices, halfedges)
Definition: Attributes.hh:92