14 #ifndef _OM_GEOMETRY_HXX_
15 #define _OM_GEOMETRY_HXX_
27 OpenMesh::Utils::Timer timer;
30 using namespace OMToolkit;
33 if (LoadVertices(mesh))
35 MDS_LOG_NOTE(
"Vertices successfully loaded into OSG...");
39 MDS_LOG_NOTE(
"Problem with copying vertices into OSG, aborting...");
48 MDS_LOG_NOTE(
"Faces successfully loaded into OSG...");
52 MDS_LOG_NOTE(
"Problem with copying faces into OSG, aborting...");
62 if (LoadNormals(mesh, Geometry::BIND_PER_PRIMITIVE))
64 MDS_LOG_NOTE(
"Normals successfully loaded into OSG, bound per face...");
68 MDS_LOG_NOTE(
"Normals bound per face failed to load, aborting...");
74 if (LoadNormals(mesh, Geometry::BIND_PER_VERTEX))
76 MDS_LOG_NOTE(
"Normals successfully loaded into OSG, bound per vertex...");
80 MDS_LOG_NOTE(
"Normals bound per vertex failed to load, aborting...");
91 if (LoadColor(mesh, defaultColor, Geometry::BIND_PER_VERTEX))
93 MDS_LOG_NOTE(
"Colors successfully loaded into OSG, bound per vertex...");
97 MDS_LOG_NOTE(
"Colors bound per vertex failed to load, aborting...");
103 if (LoadColor(mesh, defaultColor, Geometry::BIND_PER_PRIMITIVE))
105 MDS_LOG_NOTE(
"Colors successfully loaded into OSG, bound per face...");
109 MDS_LOG_NOTE(
"Colors bound per face failed to load, aborting...");
116 MDS_LOG_NOTE(
"Loading done in: " << timer.as_string(OpenMesh::Utils::Timer::MSeconds));
124 template <
class Mesh>
135 template <
class Mesh>
139 ref_ptr<Vec3Array> vertices =
new osg::Vec3Array;
140 OpenMesh::IO::ExporterT<Mesh> exporter(mesh);
141 Mesh::VertexIter vertexIt;
142 Mesh::VertexIter vertexEnd = mesh.vertices_end();
143 OpenMesh::Vec3f vertex;
146 for (vertexIt = mesh.vertices_begin(); vertexIt != vertexEnd; ++vertexIt)
148 OpenMesh::Vec3f vertex = exporter.point(vertexIt);
149 vertices->push_back(Vec3( vertex[0],
155 if (vertices->size() != mesh.n_vertices())
return false;
156 setVertexArray(vertices);
165 template <
class Mesh>
166 bool OMGeometry<Mesh>::LoadFaces(Mesh mesh)
169 ref_ptr<DrawElementsUInt> face;
170 OpenMesh::IO::ExporterT<Mesh> exporter(mesh);
171 Mesh::FaceIter faceIt;
172 Mesh::FaceIter faceEnd = mesh.faces_end();
175 if (exporter.is_triangle_mesh())
177 face =
new DrawElementsUInt(osg::PrimitiveSet::TRIANGLES, 0);
178 std::vector<Mesh::VertexHandle> faceVertices(3);
181 for (faceIt = mesh.faces_begin(); faceIt != faceEnd; ++faceIt)
183 exporter.get_vhandles(faceIt, faceVertices);
184 face->push_back(faceVertices[0].idx());
185 face->push_back(faceVertices[1].idx());
186 face->push_back(faceVertices[2].idx());
188 if (face->size() != mesh.n_faces()*3)
return false;
189 addPrimitiveSet(face);
195 std::vector<Mesh::VertexHandle> faceVertices;
196 std::vector<Mesh::VertexHandle>::iterator it, end;
199 for (faceIt = mesh.faces_begin(); faceIt != faceEnd; ++faceIt)
201 face =
new DrawElementsUInt(osg::PrimitiveSet::POLYGON, 0);
202 exporter.get_vhandles(faceIt, faceVertices);
203 end = faceVertices.end();
205 for (it = faceVertices.begin(); it != end; ++it)
207 face->push_back(it->idx());
210 addPrimitiveSet(face);
212 if (getPrimitiveSetList().size() != mesh.n_faces())
return false;
226 template <
class Mesh>
227 bool OMGeometry<Mesh>::LoadColor(Mesh mesh, osg::Vec4 &defaultColor = osg::Vec4(1.0, 1.0, 1.0, 1.0), Geometry::AttributeBinding binding = Geometry::BIND_PER_VERTEX)
230 OpenMesh::IO::ExporterT<Mesh> exporter(mesh);
231 ref_ptr<osg::Vec4Array> colors =
new osg::Vec4Array;
232 OpenMesh::Vec4uc color;
233 OpenMesh::Vec4uc zeros(1,0,0,255);
236 if (binding == Geometry::BIND_PER_VERTEX)
239 if (!exporter.has_vertex_colors())
241 colors->resize(exporter.n_vertices(), defaultColor);
246 Mesh::VertexIter vertex;
247 Mesh::VertexIter end = mesh.vertices_end();
261 for (vertex = mesh.vertices_begin(); vertex != end; ++vertex)
263 color = exporter.colorA(vertex);
264 colors->push_back(Vec4( (
float)color[0]/255.0f,
265 (
float)color[1]/255.0f,
266 (
float)color[2]/255.0f,
267 (
float)color[3]/255.0f));
276 if (colors->size() != mesh.n_vertices())
return false;
279 else if (binding == Geometry::BIND_PER_PRIMITIVE)
282 if (!exporter.has_face_colors())
284 colors->resize(exporter.n_vertices(), defaultColor);
290 Mesh::FaceIter end = mesh.faces_end();
304 for (face = mesh.faces_begin(); face != end; ++face)
306 color = exporter.colorA(face);
307 colors->push_back(Vec4( (
float)color[0]/255.0f,
308 (
float)color[1]/255.0f,
309 (
float)color[2]/255.0f,
310 (
float)color[3]/255.0f));
314 if (colors->size() != mesh.n_faces())
return false;
320 setColorArray(colors);
321 setColorBinding(binding);
332 template <
class Mesh>
333 bool OMGeometry<Mesh>::LoadNormals(Mesh mesh, Geometry::AttributeBinding binding = Geometry::BIND_PER_PRIMITIVE)
336 OpenMesh::IO::ExporterT<Mesh> exporter(mesh);
337 ref_ptr<Vec3Array> normals =
new Vec3Array();
340 if (binding == Geometry::BIND_PER_PRIMITIVE)
343 Mesh::FaceIter end = mesh.faces_end();
344 OpenMesh::Vec3f normal;
347 if (!exporter.has_face_normals())
349 mesh.request_face_normals();
350 for (face = mesh.faces_begin(); face != end; ++face)
352 mesh.set_normal(face, mesh.calc_face_normal(face));
353 normal = exporter.normal(face);
354 normals->push_back(Vec3( normal[0],
358 mesh.release_face_normals();
363 for (face = mesh.faces_begin(); face != end; ++face)
365 normal = exporter.normal(face);
366 normals->push_back(Vec3( normal[0],
373 else if (binding == Geometry::BIND_PER_VERTEX)
375 Mesh::VertexIter vertex;
376 Mesh::VertexIter end = mesh.vertices_end();
377 OpenMesh::Vec3f normal;
380 if (!exporter.has_vertex_normals())
382 bool faceNormalsDelete =
false;
385 if (!exporter.has_face_normals())
387 faceNormalsDelete =
true;
389 mesh.request_face_normals();
391 Mesh::FaceIter end = mesh.faces_end();
392 for (face = mesh.faces_begin(); face != end; ++face)
394 mesh.set_normal(face, mesh.calc_face_normal(face));
398 mesh.request_vertex_normals();
399 for (vertex = mesh.vertices_begin(); vertex != end; ++vertex)
401 mesh.set_normal(vertex, mesh.calc_vertex_normal(vertex));
402 normal = exporter.normal(vertex);
403 normals->push_back(Vec3( normal[0],
409 mesh.release_vertex_normals();
410 if (faceNormalsDelete) mesh.release_face_normals();
415 for (vertex = mesh.vertices_begin(); vertex != end; ++vertex)
417 normal = exporter.normal(vertex);
418 normals->push_back(Vec3( normal[0],
428 setNormalArray(normals);
429 setNormalBinding(binding);