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 
40 #include <OgreSceneNode.h>
41 #include <OgreSceneManager.h>
42 #include <OgreManualObject.h>
43 #include <OgreMaterialManager.h>
44 #include <OgreTextureManager.h>
45 #include <OgreTechnique.h>
46 
47 namespace rviz
48 {
50  DisplayContext* context,
51  Ogre::SceneNode* parent_node)
52  : MarkerBase(owner, context, parent_node), manual_object_(nullptr)
53 {
54 }
55 
57 {
58  if (manual_object_)
59  {
60  context_->getSceneManager()->destroyManualObject(manual_object_);
61  Ogre::MaterialManager::getSingleton().remove(material_->getName());
62  }
63 }
64 
66  const MarkerConstPtr& new_message)
67 {
68  ROS_ASSERT(new_message->type == visualization_msgs::Marker::TRIANGLE_LIST);
69 
70  Ogre::Vector3 pos, scale;
71  Ogre::Quaternion orient;
72  if (!transform(new_message, pos, orient, scale))
73  {
74  scene_node_->setVisible(false);
75  return;
76  }
77 
78  size_t num_points = new_message->points.size();
79  if ((num_points % 3) != 0 || num_points == 0)
80  {
81  scene_node_->setVisible(false);
82  return;
83  }
84  else
85  {
86  scene_node_->setVisible(true);
87  }
88 
89  if (!manual_object_)
90  {
91  static uint32_t count = 0;
93  ss << "Triangle List Marker" << count++;
94  manual_object_ = context_->getSceneManager()->createManualObject(ss.str());
95  scene_node_->attachObject(manual_object_);
96 
97  ss << "Material";
98  material_name_ = ss.str();
99  material_ = Ogre::MaterialManager::getSingleton().create(
100  material_name_, Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
101  material_->setReceiveShadows(false);
102  material_->getTechnique(0)->setLightingEnabled(true);
103  material_->setCullingMode(Ogre::CULL_NONE);
104 
105  handler_.reset(
106  new MarkerSelectionHandler(this, MarkerID(new_message->ns, new_message->id), context_));
107  }
108 
109  setPosition(pos);
110  setOrientation(orient);
111  scene_node_->setScale(scale);
112 
113  // If we have the same number of tris as previously, just update the object
114  if (old_message && num_points == old_message->points.size())
115  {
116  manual_object_->beginUpdate(0);
117  }
118  else // Otherwise clear it and begin anew
119  {
120  manual_object_->clear();
121  manual_object_->estimateVertexCount(num_points);
122  manual_object_->begin(material_name_, Ogre::RenderOperation::OT_TRIANGLE_LIST);
123  }
124 
125  bool has_vertex_colors = new_message->colors.size() == num_points;
126  bool has_face_colors = new_message->colors.size() == num_points / 3;
127  bool any_vertex_has_alpha = false;
128 
129  const std::vector<geometry_msgs::Point>& points = new_message->points;
130  for (size_t i = 0; i < num_points; i += 3)
131  {
132  std::vector<Ogre::Vector3> corners(3);
133  for (size_t c = 0; c < 3; c++)
134  {
135  corners[c] = Ogre::Vector3(points[i + c].x, points[i + c].y, points[i + c].z);
136  }
137  Ogre::Vector3 normal = (corners[1] - corners[0]).crossProduct(corners[2] - corners[0]);
138  normal.normalise();
139 
140  for (size_t c = 0; c < 3; c++)
141  {
142  manual_object_->position(corners[c]);
143  manual_object_->normal(normal);
144  if (has_vertex_colors)
145  {
146  any_vertex_has_alpha = any_vertex_has_alpha || (new_message->colors[i + c].a < 0.9998);
147  manual_object_->colour(new_message->colors[i + c].r, new_message->colors[i + c].g,
148  new_message->colors[i + c].b,
149  new_message->color.a * new_message->colors[i + c].a);
150  }
151  else if (has_face_colors)
152  {
153  any_vertex_has_alpha = any_vertex_has_alpha || (new_message->colors[i / 3].a < 0.9998);
154  manual_object_->colour(new_message->colors[i / 3].r, new_message->colors[i / 3].g,
155  new_message->colors[i / 3].b,
156  new_message->color.a * new_message->colors[i / 3].a);
157  }
158  }
159  }
160 
161  manual_object_->end();
162 
163  if (has_vertex_colors || has_face_colors)
164  {
165  material_->getTechnique(0)->setLightingEnabled(false);
166  }
167  else
168  {
169  material_->getTechnique(0)->setLightingEnabled(true);
170  float r, g, b, a;
171  r = new_message->color.r;
172  g = new_message->color.g;
173  b = new_message->color.b;
174  a = new_message->color.a;
175  material_->getTechnique(0)->setAmbient(r / 2, g / 2, b / 2);
176  material_->getTechnique(0)->setDiffuse(r, g, b, a);
177  }
178 
179  if ((!has_vertex_colors && new_message->color.a < 0.9998) ||
180  (has_vertex_colors && any_vertex_has_alpha))
181  {
182  material_->getTechnique(0)->setSceneBlending(Ogre::SBT_TRANSPARENT_ALPHA);
183  material_->getTechnique(0)->setDepthWriteEnabled(false);
184  }
185  else
186  {
187  material_->getTechnique(0)->setSceneBlending(Ogre::SBT_REPLACE);
188  material_->getTechnique(0)->setDepthWriteEnabled(true);
189  }
190 
191  handler_->addTrackedObject(manual_object_);
192 }
193 
195 {
196  S_MaterialPtr materials;
197  materials.insert(material_);
198  return materials;
199 }
200 
201 
202 } // namespace rviz
rviz::MarkerBase::context_
DisplayContext * context_
Definition: marker_base.h:108
rviz::S_MaterialPtr
std::set< Ogre::MaterialPtr > S_MaterialPtr
Definition: marker_base.h:51
rviz::TriangleListMarker::getMaterials
S_MaterialPtr getMaterials() override
Definition: triangle_list_marker.cpp:194
rviz::MarkerDisplay
Displays "markers" sent in by other ROS nodes on the "visualization_marker" topic.
Definition: marker_display.h:70
marker_selection_handler.h
triangle_list_marker.h
rviz::MarkerBase::setPosition
virtual void setPosition(const Ogre::Vector3 &position)
Definition: marker_base.cpp:124
rviz::MarkerBase
Definition: marker_base.h:53
marker_display.h
rviz::MarkerBase::scene_node_
Ogre::SceneNode * scene_node_
Definition: marker_base.h:110
rviz::MarkerBase::setOrientation
virtual void setOrientation(const Ogre::Quaternion &orientation)
Definition: marker_base.cpp:129
rviz::MarkerBase::handler_
boost::shared_ptr< MarkerSelectionHandler > handler_
Definition: marker_base.h:116
selection_manager.h
rviz::UniformStringStream
std::stringstream subclass which defaults to the "C" locale, so serialization of numbers is uniform a...
Definition: uniform_string_stream.h:44
rviz::DisplayContext::getSceneManager
virtual Ogre::SceneManager * getSceneManager() const =0
Returns the Ogre::SceneManager used for the main RenderPanel.
rviz::TriangleListMarker::onNewMessage
void onNewMessage(const MarkerConstPtr &old_message, const MarkerConstPtr &new_message) override
Definition: triangle_list_marker.cpp:65
rviz::MarkerBase::transform
bool transform(const MarkerConstPtr &message, Ogre::Vector3 &pos, Ogre::Quaternion &orient, Ogre::Vector3 &scale)
Definition: marker_base.cpp:87
rviz
Definition: add_display_dialog.cpp:54
rviz::TriangleListMarker::~TriangleListMarker
~TriangleListMarker() override
Definition: triangle_list_marker.cpp:56
rviz::MarkerSelectionHandler
Definition: marker_selection_handler.h:45
mesh_loader.h
rviz::DisplayContext
Pure-virtual base class for objects which give Display subclasses context in which to work.
Definition: display_context.h:81
rviz::TriangleListMarker::material_name_
std::string material_name_
Definition: triangle_list_marker.h:59
rviz::MarkerID
std::pair< std::string, int32_t > MarkerID
Definition: interactive_marker_display.h:58
uniform_string_stream.h
rviz::TriangleListMarker::manual_object_
Ogre::ManualObject * manual_object_
Definition: triangle_list_marker.h:57
display_context.h
rviz::TriangleListMarker::TriangleListMarker
TriangleListMarker(MarkerDisplay *owner, DisplayContext *context, Ogre::SceneNode *parent_node)
Definition: triangle_list_marker.cpp:49
ROS_ASSERT
#define ROS_ASSERT(cond)
rviz::MarkerBase::MarkerConstPtr
visualization_msgs::Marker::ConstPtr MarkerConstPtr
Definition: marker_base.h:57
rviz::TriangleListMarker::material_
Ogre::MaterialPtr material_
Definition: triangle_list_marker.h:58


rviz
Author(s): Dave Hershberger, David Gossow, Josh Faust, William Woodall
autogenerated on Sat Jun 1 2024 02:31:53