OpenMesh

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

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
ImporterT.hh
1 /*===========================================================================*\
2 * *
3 * OpenMesh *
4 * Copyright (C) 2001-2012 by Computer Graphics Group, RWTH Aachen *
5 * www.openmesh.org *
6 * *
7 *---------------------------------------------------------------------------*
8 * This file is part of OpenMesh. *
9 * *
10 * OpenMesh is free software: you can redistribute it and/or modify *
11 * it under the terms of the GNU Lesser General Public License as *
12 * published by the Free Software Foundation, either version 3 of *
13 * the License, or (at your option) any later version with the *
14 * following exceptions: *
15 * *
16 * If other files instantiate templates or use macros *
17 * or inline functions from this file, or you compile this file and *
18 * link it with other files to produce an executable, this file does *
19 * not by itself cause the resulting executable to be covered by the *
20 * GNU Lesser General Public License. This exception does not however *
21 * invalidate any other reasons why the executable file might be *
22 * covered by the GNU Lesser General Public License. *
23 * *
24 * OpenMesh is distributed in the hope that it will be useful, *
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
27 * GNU Lesser General Public License for more details. *
28 * *
29 * You should have received a copy of the GNU LesserGeneral Public *
30 * License along with OpenMesh. If not, *
31 * see <http://www.gnu.org/licenses/>. *
32 * *
33 \*===========================================================================*/
34
35 /*===========================================================================*\
36 * *
37 * $Revision: 776 $ *
38 * $Date: 2012-12-10 16:22:00 +0100 (Mo, 10 Dez 2012) $ *
39 * *
40 \*===========================================================================*/
41
42
43 //=============================================================================
44 //
45 // Implements an importer module for arbitrary OpenMesh meshes
46 //
47 //=============================================================================
48
49
50 #ifndef __IMPORTERT_HH__
51 #define __IMPORTERT_HH__
52
53
54 //=== INCLUDES ================================================================
55
56
57 #include <OpenMesh/Core/IO/importer/BaseImporter.hh>
58 #include <OpenMesh/Core/Utils/vector_cast.hh>
59 #include <OpenMesh/Core/Utils/color_cast.hh>
60 #include <OpenMesh/Core/Mesh/Attributes.hh>
61 #include <OpenMesh/Core/System/omstream.hh>
62
63
64 //== NAMESPACES ===============================================================
65
66
67 namespace OpenMesh {
68 namespace IO {
69
70
71 //=== IMPLEMENTATION ==========================================================
72
73
77 template <class Mesh>
78 class ImporterT : public BaseImporter
79 {
80 public:
81
82 typedef typename Mesh::Point Point;
83 typedef typename Mesh::Normal Normal;
84 typedef typename Mesh::Color Color;
85 typedef typename Mesh::TexCoord2D TexCoord2D;
86 typedef std::vector<VertexHandle> VHandles;
87
88
89 ImporterT(Mesh& _mesh) : mesh_(_mesh), halfedgeNormals_() {}
90
91
92 virtual VertexHandle add_vertex(const Vec3f& _point)
93 {
94 return mesh_.add_vertex(vector_cast<Point>(_point));
95 }
96
97
98 virtual FaceHandle add_face(const VHandles& _indices)
99 {
100 FaceHandle fh;
101
102 if (_indices.size() > 2)
103 {
104 VHandles::const_iterator it, it2, end(_indices.end());
105
106
107 // test for valid vertex indices
108 for (it=_indices.begin(); it!=end; ++it)
109 if (! mesh_.is_valid_handle(*it))
110 {
111 omerr() << "ImporterT: Face contains invalid vertex index\n";
112 return fh;
113 }
114
115
116 // don't allow double vertices
117 for (it=_indices.begin(); it!=end; ++it)
118 for (it2=it+1; it2!=end; ++it2)
119 if (*it == *it2)
120 {
121 omerr() << "ImporterT: Face has equal vertices\n";
122 failed_faces_.push_back(_indices);
123 return fh;
124 }
125
126
127 // try to add face
128 fh = mesh_.add_face(_indices);
129 if (!fh.is_valid())
130 {
131 failed_faces_.push_back(_indices);
132 return fh;
133 }
134
135 //write the half edge normals
136 if (mesh_.has_halfedge_normals())
137 {
138 //iterate over all incoming haldedges of the added face
139 for (typename Mesh::FaceHalfedgeIter fh_iter = mesh_.fh_begin(fh);
140 fh_iter != mesh_.fh_end(fh); ++fh_iter)
141 {
142 //and write the normals to it
143 typename Mesh::HalfedgeHandle heh = fh_iter.current_halfedge_handle();
144 typename Mesh::VertexHandle vh = mesh_.to_vertex_handle(heh);
145 typename std::map<VertexHandle,Normal>::iterator it_heNs = halfedgeNormals_.find(vh);
146 if (it_heNs != halfedgeNormals_.end())
147 mesh_.set_normal(heh,it_heNs->second);
148 }
149 halfedgeNormals_.clear();
150 }
151 }
152 return fh;
153 }
154
155 // vertex attributes
156
157 virtual void set_normal(VertexHandle _vh, const Vec3f& _normal)
158 {
159 if (mesh_.has_vertex_normals())
160 mesh_.set_normal(_vh, vector_cast<Normal>(_normal));
161
162 //saves normals for half edges.
163 //they will be written, when the face is added
164 if (mesh_.has_halfedge_normals())
165 halfedgeNormals_[_vh] = vector_cast<Normal>(_normal);
166 }
167
168 virtual void set_color(VertexHandle _vh, const Vec4uc& _color)
169 {
170 if (mesh_.has_vertex_colors())
171 mesh_.set_color(_vh, color_cast<Color>(_color));
172 }
173
174 virtual void set_color(VertexHandle _vh, const Vec3uc& _color)
175 {
176 if (mesh_.has_vertex_colors())
177 mesh_.set_color(_vh, color_cast<Color>(_color));
178 }
179
180
181 virtual void set_texcoord(VertexHandle _vh, const Vec2f& _texcoord)
182 {
183 if (mesh_.has_vertex_texcoords2D())
184 mesh_.set_texcoord2D(_vh, vector_cast<TexCoord2D>(_texcoord));
185 }
186
187 virtual void set_texcoord(HalfedgeHandle _heh, const Vec2f& _texcoord)
188 {
189 if (mesh_.has_halfedge_texcoords2D())
190 mesh_.set_texcoord2D(_heh, vector_cast<TexCoord2D>(_texcoord));
191 }
192
193 // edge attributes
194
195 virtual void set_color(EdgeHandle _eh, const Vec4uc& _color)
196 {
197 if (mesh_.has_edge_colors())
198 mesh_.set_color(_eh, color_cast<Color>(_color));
199 }
200
201 virtual void set_color(EdgeHandle _eh, const Vec3uc& _color)
202 {
203 if (mesh_.has_edge_colors())
204 mesh_.set_color(_eh, color_cast<Color>(_color));
205 }
206
207 // face attributes
208
209 virtual void set_normal(FaceHandle _fh, const Vec3f& _normal)
210 {
211 if (mesh_.has_face_normals())
212 mesh_.set_normal(_fh, vector_cast<Normal>(_normal));
213 }
214
215 virtual void set_color(FaceHandle _fh, const Vec3uc& _color)
216 {
217 if (mesh_.has_face_colors())
218 mesh_.set_color(_fh, color_cast<Color>(_color));
219 }
220
221 virtual void set_color(FaceHandle _fh, const Vec4uc& _color)
222 {
223 if (mesh_.has_face_colors())
224 mesh_.set_color(_fh, color_cast<Color>(_color));
225 }
226
227 virtual void add_face_texcoords( FaceHandle _fh, VertexHandle _vh, const std::vector<Vec2f>& _face_texcoords)
228 {
229 // get first halfedge handle
230 HalfedgeHandle cur_heh = mesh_.halfedge_handle(_fh);
231 HalfedgeHandle end_heh = mesh_.prev_halfedge_handle(cur_heh);
232
233 // find start heh
234 while( mesh_.to_vertex_handle(cur_heh) != _vh && cur_heh != end_heh )
235 cur_heh = mesh_.next_halfedge_handle( cur_heh);
236
237 for(unsigned int i=0; i<_face_texcoords.size(); ++i)
238 {
239 set_texcoord( cur_heh, _face_texcoords[i]);
240 cur_heh = mesh_.next_halfedge_handle( cur_heh);
241 }
242 }
243
244 virtual void set_face_texindex( FaceHandle _fh, int _texId ) {
245 if ( mesh_.has_face_texture_index() ) {
246 mesh_.set_texture_index(_fh , _texId);
247 }
248 }
249
250 virtual void add_texture_information( int _id , std::string _name ) {
251 OpenMesh::MPropHandleT< std::map< int, std::string > > property;
252
253 if ( !mesh_.get_property_handle(property,"TextureMapping") ) {
254 mesh_.add_property(property,"TextureMapping");
255 }
256
257 if ( mesh_.property(property).find( _id ) == mesh_.property(property).end() )
258 mesh_.property(property)[_id] = _name;
259 }
260
261 // low-level access to mesh
262
263 virtual BaseKernel* kernel() { return &mesh_; }
264
265 bool is_triangle_mesh() const
266 { return Mesh::is_triangles(); }
267
268 void reserve(unsigned int nV, unsigned int nE, unsigned int nF)
269 {
270 mesh_.reserve(nV, nE, nF);
271 }
272
273 // query number of faces, vertices, normals, texcoords
274 size_t n_vertices() const { return mesh_.n_vertices(); }
275 size_t n_faces() const { return mesh_.n_faces(); }
276 size_t n_edges() const { return mesh_.n_edges(); }
277
278
279 void prepare() { failed_faces_.clear(); }
280
281
282 void finish()
283 {
284 if (!failed_faces_.empty())
285 {
286 omerr() << failed_faces_.size()
287 << " faces failed, adding them as isolated faces\n";
288
289 for (unsigned int i=0; i<failed_faces_.size(); ++i)
290 {
291 VHandles& vhandles = failed_faces_[i];
292
293 // double vertices
294 for (unsigned int j=0; j<vhandles.size(); ++j)
295 {
296 Point p = mesh_.point(vhandles[j]);
297 vhandles[j] = mesh_.add_vertex(p);
298 // DO STORE p, reference may not work since vertex array
299 // may be relocated after adding a new vertex !
300
301 // Mark vertices of failed face as non-manifold
302 if (mesh_.has_vertex_status()) {
303 mesh_.status(vhandles[j]).set_fixed_nonmanifold(true);
304 }
305 }
306
307 // add face
308 FaceHandle fh = mesh_.add_face(vhandles);
309
310 // Mark failed face as non-manifold
311 if (mesh_.has_face_status())
312 mesh_.status(fh).set_fixed_nonmanifold(true);
313
314 // Mark edges of failed face as non-two-manifold
315 if (mesh_.has_edge_status()) {
316 typename Mesh::FaceEdgeIter fe_it = mesh_.fe_iter(fh);
317 for(; fe_it; ++fe_it) {
318 mesh_.status(fe_it).set_fixed_nonmanifold(true);
319 }
320 }
321 }
322
323 failed_faces_.clear();
324 }
325 }
326
327
328
329 private:
330
331 Mesh& mesh_;
332 std::vector<VHandles> failed_faces_;
333 // stores normals for halfedges of the next face
334 std::map<VertexHandle,Normal> halfedgeNormals_;
335 };
336
337
338 //=============================================================================
339 } // namespace IO
340 } // namespace OpenMesh
341 //=============================================================================
342 #endif
343 //=============================================================================