ed_rviz_plugins
entity_visual.cpp
Go to the documentation of this file.
1 #include <boost/make_shared.hpp>
2 
3 #include <OGRE/OgreVector3.h>
4 #include <OGRE/OgreSceneNode.h>
5 #include <OGRE/OgreSceneManager.h>
6 
7 #include "entity_visual.h"
8 
9 namespace ed_rviz_plugins
10 {
11 
12 void getMinMax(const Ogre::Vector3& value, Ogre::Vector3& min_pt, Ogre::Vector3& max_pt)
13 {
14  if (value.x > max_pt.x) max_pt.x = value.x;
15  if (value.y > max_pt.y) max_pt.y = value.y;
16  if (value.z > max_pt.z) max_pt.z = value.z;
17 
18  if (value.x < min_pt.x) min_pt.x = value.x;
19  if (value.y < min_pt.y) min_pt.y = value.y;
20  if (value.z < min_pt.z) min_pt.z = value.z;
21 }
22 
23 EntityVisual::EntityVisual(Ogre::SceneManager* scene_manager, Ogre::SceneNode* parent_node) : visual_revision_(0), volumes_revision_(0)
24 {
25  scene_manager_ = scene_manager;
26 
27  frame_node_ = parent_node->createChildSceneNode();
28 
29  mesh_ = boost::make_shared<rviz::MeshShape>(scene_manager_, frame_node_);
30  convex_hull_ = boost::make_shared<rviz::BillboardLine>(scene_manager_, frame_node_);
31  label_ = boost::make_shared<rviz::MovableText>("unknown");
32 
33  frame_node_->attachObject(label_.get());
34 }
35 
37 {
38  // Destroy the frame node since we don't need it anymore.
39  scene_manager_->destroySceneNode(frame_node_);
40 }
41 
42 void EntityVisual::setEntityMeshAndVolumes(const ed_gui_server_msgs::EntityMeshAndVolumes& mesh_and_volumes)
43 {
44  mesh_->clear();
45 
46  // Set mesh revision
47  if (mesh_and_volumes.visual_revision != 0)
48  {
49  visual_revision_ = mesh_and_volumes.visual_revision;
50 
51  mesh_->estimateVertexCount(mesh_and_volumes.mesh.vertices.size() / 3);
52  mesh_->beginTriangles();
53 
54  // Set vertices
55  for (unsigned int i = 0; i < mesh_and_volumes.mesh.vertices.size() / 3 / 3; ++i)
56  {
57  unsigned int i9 = 9 * i;
58 
59  Ogre::Vector3 v1, v2, v3, n;
60 
61  v1.x = mesh_and_volumes.mesh.vertices[i9 + 0];
62  v1.y = mesh_and_volumes.mesh.vertices[i9 + 1];
63  v1.z = mesh_and_volumes.mesh.vertices[i9 + 2];
64 
65  v2.x = mesh_and_volumes.mesh.vertices[i9 + 3];
66  v2.y = mesh_and_volumes.mesh.vertices[i9 + 4];
67  v2.z = mesh_and_volumes.mesh.vertices[i9 + 5];
68 
69  v3.x = mesh_and_volumes.mesh.vertices[i9 + 6];
70  v3.y = mesh_and_volumes.mesh.vertices[i9 + 7];
71  v3.z = mesh_and_volumes.mesh.vertices[i9 + 8];
72 
73  n = (v1-v3).crossProduct(v2-v3).normalisedCopy();
74 
75  mesh_->addVertex(v1, n);
76  mesh_->addVertex(v2, n);
77  mesh_->addVertex(v3, n);
78  }
79 
80  mesh_->endTriangles();
81  }
82 
83  if (mesh_and_volumes.volumes_revision != 0)
84  {
85  volumes_revision_ = mesh_and_volumes.volumes_revision;
86 
89 
90  // Now set the areas
91  for (const ed_gui_server_msgs::Volume& v : mesh_and_volumes.volumes)
92  {
93  volume_labels_.push_back(boost::shared_ptr<rviz::MovableText>(new rviz::MovableText(v.name)));
94  boost::shared_ptr<rviz::MovableText> label = volume_labels_.back();
95  label->setTextAlignment(rviz::MovableText::H_CENTER, rviz::MovableText::V_CENTER);
96  label->setCharacterHeight(0.05);
97  label->setCaption(v.name);
98 
99  volume_meshes_.push_back(boost::shared_ptr<rviz::MeshShape>(new rviz::MeshShape(scene_manager_, frame_node_)));
100  boost::shared_ptr<rviz::MeshShape> mesh = volume_meshes_.back();
101 
102  mesh->estimateVertexCount(v.mesh.vertices.size() / 3);
103  mesh->beginTriangles();
104 
105  Ogre::Vector3 max_pt(-1e9, -1e9, -1e9);
106  Ogre::Vector3 min_pt(1e9, 1e9, 1e9);
107 
108  // Set vertices
109  for (unsigned int i = 0; i < v.mesh.vertices.size() / 3 / 3; ++i)
110  {
111  unsigned int i9 = 9 * i;
112 
113  Ogre::Vector3 v1, v2, v3, n;
114 
115  v1.x = v.mesh.vertices[i9 + 0];
116  v1.y = v.mesh.vertices[i9 + 1];
117  v1.z = v.mesh.vertices[i9 + 2];
118 
119  getMinMax(v1, min_pt, max_pt);
120 
121  v2.x = v.mesh.vertices[i9 + 3];
122  v2.y = v.mesh.vertices[i9 + 4];
123  v2.z = v.mesh.vertices[i9 + 5];
124 
125  getMinMax(v2, min_pt, max_pt);
126 
127  v3.x = v.mesh.vertices[i9 + 6];
128  v3.y = v.mesh.vertices[i9 + 7];
129  v3.z = v.mesh.vertices[i9 + 8];
130 
131  getMinMax(v3, min_pt, max_pt);
132 
133  n = (v1-v3).crossProduct(v2-v3).normalisedCopy();
134 
135  mesh->addVertex(v1, n);
136  mesh->addVertex(v2, n);
137  mesh->addVertex(v3, n);
138  }
139 
140  mesh->endTriangles();
141 
142  Ogre::Vector3 label_pos((max_pt.x + min_pt.x) / 2, (max_pt.y + min_pt.y) / 2, max_pt.z);
143 
144  frame_node_->createChildSceneNode(label_pos)->attachObject(label.get());
145  }
146  }
147 }
148 
149 void EntityVisual::setColor(Ogre::ColourValue c, double entity_label_opacity, double volume_opacity, double volume_label_opacity)
150 {
151  label_->setColor(Ogre::ColourValue(1.0f, 1.0f, 1.0f, entity_label_opacity));
152 
153  for (auto& label : volume_labels_)
154  label->setColor(Ogre::ColourValue(1.0f, 1.0f, 1.0f, volume_label_opacity));
155 
156  c.a = volume_opacity;
157  for (auto& mesh : volume_meshes_)
158  mesh->setColor(c);
159 
160  c.a = 1.0;
161  convex_hull_->setColor(c.r, c.g, c.b, c.a);
162  mesh_->setColor(c);
163 }
164 
165 void EntityVisual::setConvexHull(const ed_gui_server_msgs::Polygon& polygon)
166 {
167  convex_hull_->clear();
168  convex_hull_->setMaxPointsPerLine(2);
169  convex_hull_->setNumLines(polygon.xs.size() * 3);
170  convex_hull_->setLineWidth(0.01);
171 
172  for (unsigned int i = 0; i < polygon.xs.size(); ++i)
173  {
174  int j = (i + 1) % polygon.xs.size();
175 
176  float x1 = polygon.xs[i];
177  float x2 = polygon.xs[j];
178 
179  float y1 = polygon.ys[i];
180  float y2 = polygon.ys[j];
181 
182  // Low line
183  if (i != 0)
184  convex_hull_->newLine();
185  convex_hull_->addPoint(Ogre::Vector3(x1, y1, polygon.z_min));
186  convex_hull_->addPoint(Ogre::Vector3(x2, y2, polygon.z_min));
187 
188  // High line
189  convex_hull_->newLine();
190  convex_hull_->addPoint(Ogre::Vector3(x1, y1, polygon.z_max));
191  convex_hull_->addPoint(Ogre::Vector3(x2, y2, polygon.z_max));
192 
193  // Vertical line
194  convex_hull_->newLine();
195  convex_hull_->addPoint(Ogre::Vector3(x1, y1, polygon.z_min));
196  convex_hull_->addPoint(Ogre::Vector3(x1, y1, polygon.z_max));
197  }
198 }
199 
201 {
202  label_->setTextAlignment(rviz::MovableText::H_CENTER, rviz::MovableText::V_CENTER);
203  // You can't set an empty string as caption. But without setting a caption, its shows 'unknown'
204  // Hide this 'unknown' by setting height to zero.
205  if (label.empty())
206  label_->setCharacterHeight(0);
207  else
208  {
209  label_->setCharacterHeight(0.05);
210  label_->setCaption(label);
211  }
212 }
213 
214 // Position and orientation are passed through to the SceneNode.
215 void EntityVisual::setFramePosition(const Ogre::Vector3& position)
216 {
217  frame_node_->setPosition(position);
218 }
219 
220 void EntityVisual::setFrameOrientation(const Ogre::Quaternion& orientation)
221 {
222  frame_node_->setOrientation(orientation);
223 }
224 
225 } // end namespace rviz_plugins
226 
ed_rviz_plugins::EntityVisual::setColor
void setColor(Ogre::ColourValue c, double entity_label_opacity, double area_opacity, double area_label_opacity)
Definition: entity_visual.cpp:149
std::string
ed_rviz_plugins::EntityVisual::volume_meshes_
std::vector< boost::shared_ptr< rviz::MeshShape > > volume_meshes_
Definition: entity_visual.h:50
ed_rviz_plugins::EntityVisual::label_
boost::shared_ptr< rviz::MovableText > label_
Definition: entity_visual.h:57
ed_rviz_plugins::EntityVisual::scene_manager_
Ogre::SceneManager * scene_manager_
Definition: entity_visual.h:63
ed_rviz_plugins::EntityVisual::setFrameOrientation
void setFrameOrientation(const Ogre::Quaternion &orientation)
Definition: entity_visual.cpp:220
ed_rviz_plugins::EntityVisual::volume_labels_
std::vector< boost::shared_ptr< rviz::MovableText > > volume_labels_
Definition: entity_visual.h:51
ed_rviz_plugins::EntityVisual::mesh_
boost::shared_ptr< rviz::MeshShape > mesh_
Definition: entity_visual.h:49
std::vector::back
T back(T... args)
std::vector::clear
T clear(T... args)
ed_rviz_plugins::EntityVisual::EntityVisual
EntityVisual(Ogre::SceneManager *scene_manager, Ogre::SceneNode *parent_node)
Definition: entity_visual.cpp:23
std::vector::push_back
T push_back(T... args)
ed_rviz_plugins::EntityVisual::setLabel
void setLabel(const std::string &label)
Definition: entity_visual.cpp:200
ed_rviz_plugins::EntityVisual::setEntityMeshAndVolumes
void setEntityMeshAndVolumes(const ed_gui_server_msgs::EntityMeshAndVolumes &mesh_and_areas)
Definition: entity_visual.cpp:42
ed_rviz_plugins::EntityVisual::setConvexHull
void setConvexHull(const ed_gui_server_msgs::Polygon &polygon)
Definition: entity_visual.cpp:165
ed_rviz_plugins::EntityVisual::frame_node_
Ogre::SceneNode * frame_node_
Definition: entity_visual.h:61
ed_rviz_plugins::EntityVisual::~EntityVisual
virtual ~EntityVisual()
Definition: entity_visual.cpp:36
entity_visual.h
ed_rviz_plugins::EntityVisual::setFramePosition
void setFramePosition(const Ogre::Vector3 &position)
Definition: entity_visual.cpp:215
ed_rviz_plugins::EntityVisual::volumes_revision_
unsigned long volumes_revision_
Definition: entity_visual.h:53
ed_rviz_plugins
Definition: world_model_display.cpp:96
std::string::empty
T empty(T... args)
ed_rviz_plugins::EntityVisual::visual_revision_
unsigned long visual_revision_
Definition: entity_visual.h:52
ed_rviz_plugins::EntityVisual::convex_hull_
boost::shared_ptr< rviz::BillboardLine > convex_hull_
Definition: entity_visual.h:55
ed_rviz_plugins::getMinMax
void getMinMax(const Ogre::Vector3 &value, Ogre::Vector3 &min_pt, Ogre::Vector3 &max_pt)
Definition: entity_visual.cpp:12