triangle_list_marker.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010, Willow Garage, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of the Willow Garage, Inc. nor the names of its
14  * contributors may be used to endorse or promote products derived from
15  * this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #include "triangle_list_marker.h"
31 
36 
37 #include "rviz/display_context.h"
38 #include "rviz/mesh_loader.h"
39 #include "marker_display.h"
40 
41 #include <OgreSceneNode.h>
42 #include <OgreSceneManager.h>
43 #include <OgreManualObject.h>
44 #include <OgreMaterialManager.h>
45 #include <OgreTextureManager.h>
46 #include <OgreTechnique.h>
47 
48 namespace rviz
49 {
51  DisplayContext* context,
52  Ogre::SceneNode* parent_node)
53  : MarkerBase(owner, context, parent_node), manual_object_(nullptr)
54 {
55 }
56 
58 {
59  if (manual_object_)
60  {
61  context_->getSceneManager()->destroyManualObject(manual_object_);
62  material_->unload();
63  Ogre::MaterialManager::getSingleton().remove(material_->getName());
64  }
65 }
66 
68  const MarkerConstPtr& new_message)
69 {
70  ROS_ASSERT(new_message->type == visualization_msgs::Marker::TRIANGLE_LIST);
71 
72  size_t num_points = new_message->points.size();
73  if ((num_points % 3) != 0 || num_points == 0)
74  {
75  scene_node_->setVisible(false);
76  return;
77  }
78  else
79  {
80  scene_node_->setVisible(true);
81  }
82 
83  if (!manual_object_)
84  {
85  static uint32_t count = 0;
87  ss << "Triangle List Marker" << count++;
88  manual_object_ = context_->getSceneManager()->createManualObject(ss.str());
89  scene_node_->attachObject(manual_object_);
90 
91  ss << "Material";
92  material_name_ = ss.str();
93  material_ = Ogre::MaterialManager::getSingleton().create(material_name_, ROS_PACKAGE_NAME);
94  material_->setReceiveShadows(false);
95  material_->getTechnique(0)->setLightingEnabled(true);
96  material_->setCullingMode(Ogre::CULL_NONE);
97 
98  handler_.reset(
99  new MarkerSelectionHandler(this, MarkerID(new_message->ns, new_message->id), context_));
100  }
101 
102  Ogre::Vector3 pos, scale;
103  Ogre::Quaternion orient;
104  if (!transform(new_message, pos, orient, scale))
105  {
106  ROS_DEBUG("Unable to transform marker message");
107  scene_node_->setVisible(false);
108  return;
109  }
110 
111  setPosition(pos);
112  setOrientation(orient);
113  scene_node_->setScale(scale);
114 
115  // If we have the same number of tris as previously, just update the object
116  if (old_message && num_points == old_message->points.size())
117  {
118  manual_object_->beginUpdate(0);
119  }
120  else // Otherwise clear it and begin anew
121  {
122  manual_object_->clear();
123  manual_object_->estimateVertexCount(num_points);
124  manual_object_->begin(material_name_, Ogre::RenderOperation::OT_TRIANGLE_LIST);
125  }
126 
127  bool has_vertex_colors = new_message->colors.size() == num_points;
128  bool has_face_colors = new_message->colors.size() == num_points / 3;
129  bool any_vertex_has_alpha = false;
130 
131  const std::vector<geometry_msgs::Point>& points = new_message->points;
132  for (size_t i = 0; i < num_points; i += 3)
133  {
134  std::vector<Ogre::Vector3> corners(3);
135  for (size_t c = 0; c < 3; c++)
136  {
137  corners[c] = Ogre::Vector3(points[i + c].x, points[i + c].y, points[i + c].z);
138  }
139  Ogre::Vector3 normal = (corners[1] - corners[0]).crossProduct(corners[2] - corners[0]);
140  normal.normalise();
141 
142  for (size_t c = 0; c < 3; c++)
143  {
144  manual_object_->position(corners[c]);
145  manual_object_->normal(normal);
146  if (has_vertex_colors)
147  {
148  any_vertex_has_alpha = any_vertex_has_alpha || (new_message->colors[i + c].a < 0.9998);
149  manual_object_->colour(new_message->colors[i + c].r, new_message->colors[i + c].g,
150  new_message->colors[i + c].b,
151  new_message->color.a * new_message->colors[i + c].a);
152  }
153  else if (has_face_colors)
154  {
155  any_vertex_has_alpha = any_vertex_has_alpha || (new_message->colors[i / 3].a < 0.9998);
156  manual_object_->colour(new_message->colors[i / 3].r, new_message->colors[i / 3].g,
157  new_message->colors[i / 3].b,
158  new_message->color.a * new_message->colors[i / 3].a);
159  }
160  }
161  }
162 
163  manual_object_->end();
164 
165  if (has_vertex_colors || has_face_colors)
166  {
167  material_->getTechnique(0)->setLightingEnabled(false);
168  }
169  else
170  {
171  material_->getTechnique(0)->setLightingEnabled(true);
172  float r, g, b, a;
173  r = new_message->color.r;
174  g = new_message->color.g;
175  b = new_message->color.b;
176  a = new_message->color.a;
177  material_->getTechnique(0)->setAmbient(r / 2, g / 2, b / 2);
178  material_->getTechnique(0)->setDiffuse(r, g, b, a);
179  }
180 
181  if ((!has_vertex_colors && new_message->color.a < 0.9998) ||
182  (has_vertex_colors && any_vertex_has_alpha))
183  {
184  material_->getTechnique(0)->setSceneBlending(Ogre::SBT_TRANSPARENT_ALPHA);
185  material_->getTechnique(0)->setDepthWriteEnabled(false);
186  }
187  else
188  {
189  material_->getTechnique(0)->setSceneBlending(Ogre::SBT_REPLACE);
190  material_->getTechnique(0)->setDepthWriteEnabled(true);
191  }
192 
193  handler_->addTrackedObject(manual_object_);
194 }
195 
197 {
198  S_MaterialPtr materials;
199  materials.insert(material_);
200  return materials;
201 }
202 
203 
204 } // namespace rviz
Ogre::SceneNode * scene_node_
Definition: marker_base.h:116
bool transform(const MarkerConstPtr &message, Ogre::Vector3 &pos, Ogre::Quaternion &orient, Ogre::Vector3 &scale)
Definition: marker_base.cpp:83
virtual void setPosition(const Ogre::Vector3 &position)
std::pair< std::string, int32_t > MarkerID
Ogre::ManualObject * manual_object_
std::stringstream subclass which defaults to the "C" locale, so serialization of numbers is uniform a...
Pure-virtual base class for objects which give Display subclasses context in which to work...
boost::shared_ptr< MarkerSelectionHandler > handler_
Definition: marker_base.h:122
DisplayContext * context_
Definition: marker_base.h:114
TriangleListMarker(MarkerDisplay *owner, DisplayContext *context, Ogre::SceneNode *parent_node)
visualization_msgs::Marker::ConstPtr MarkerConstPtr
Definition: marker_base.h:63
virtual Ogre::SceneManager * getSceneManager() const =0
Returns the Ogre::SceneManager used for the main RenderPanel.
S_MaterialPtr getMaterials() override
std::set< Ogre::MaterialPtr > S_MaterialPtr
Definition: marker_base.h:57
virtual void setOrientation(const Ogre::Quaternion &orientation)
#define ROS_ASSERT(cond)
r
Displays "markers" sent in by other ROS nodes on the "visualization_marker" topic.
void onNewMessage(const MarkerConstPtr &old_message, const MarkerConstPtr &new_message) override
#define ROS_DEBUG(...)


rviz
Author(s): Dave Hershberger, David Gossow, Josh Faust
autogenerated on Sat May 27 2023 02:06:25