moveit2
The MoveIt Motion Planning Framework for ROS 2.
gl_mesh.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2013, Willow Garage, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of Willow Garage nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  *********************************************************************/
34 
35 /* Author: Suat Gedikli */
36 
38 #include <geometric_shapes/shapes.h>
39 #include <stdexcept>
40 #include <Eigen/Eigen>
41 
42 using namespace std;
43 using namespace Eigen;
44 using shapes::Mesh;
45 
46 mesh_filter::GLMesh::GLMesh(const Mesh& mesh, unsigned int mesh_label)
47 {
48  if (!mesh.vertex_normals)
49  throw std::runtime_error("Vertex normals are not computed for input mesh. Call computeVertexNormals() before "
50  "passing as input to mesh_filter.");
51 
52  mesh_label_ = mesh_label;
53  list_ = glGenLists(1);
54  glNewList(list_, GL_COMPILE);
55  glBegin(GL_TRIANGLES);
56  glColor4ubv((GLubyte*)&mesh_label_);
57  for (unsigned t_idx = 0; t_idx < mesh.triangle_count; ++t_idx)
58  {
59  unsigned v1 = 3 * mesh.triangles[3 * t_idx];
60  unsigned v2 = 3 * mesh.triangles[3 * t_idx + 1];
61  unsigned v3 = 3 * mesh.triangles[3 * t_idx + 2];
62 
63  glNormal3f(mesh.vertex_normals[v1], mesh.vertex_normals[v1 + 1], mesh.vertex_normals[v1 + 2]);
64  glVertex3f(mesh.vertices[v1], mesh.vertices[v1 + 1], mesh.vertices[v1 + 2]);
65 
66  glNormal3f(mesh.vertex_normals[v2], mesh.vertex_normals[v2 + 1], mesh.vertex_normals[v2 + 2]);
67  glVertex3f(mesh.vertices[v2], mesh.vertices[v2 + 1], mesh.vertices[v2 + 2]);
68 
69  glNormal3f(mesh.vertex_normals[v3], mesh.vertex_normals[v3 + 1], mesh.vertex_normals[v3 + 2]);
70  glVertex3f(mesh.vertices[v3], mesh.vertices[v3 + 1], mesh.vertices[v3 + 2]);
71  }
72  glEnd();
73  glEndList();
74 }
75 
77 {
78  glDeleteLists(list_, 1);
79 }
80 
81 void mesh_filter::GLMesh::render(const Isometry3d& transform) const
82 {
83  glMatrixMode(GL_MODELVIEW);
84  glPushMatrix();
85  if (!(transform.matrix().Flags & RowMajorBit))
86  glMultMatrixd(transform.matrix().data());
87  else
88  glMultTransposeMatrixd(transform.matrix().data());
89  glCallList(list_);
90  glPopMatrix();
91 }
void render(const Eigen::Isometry3d &transform) const
renders the mesh in current OpenGL frame buffer (context)
Definition: gl_mesh.cpp:81
GLMesh(const shapes::Mesh &mesh, unsigned int mesh_label)
Constructs a GLMesh object for given mesh and label.
Definition: gl_mesh.cpp:46
~GLMesh()
Destructor.
Definition: gl_mesh.cpp:76