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 {
50 
51 TriangleListMarker::TriangleListMarker(MarkerDisplay* owner, DisplayContext* context, Ogre::SceneNode* parent_node)
52 : MarkerBase(owner, context, parent_node)
53 , manual_object_(0)
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 
67 void TriangleListMarker::onNewMessage(const MarkerConstPtr& old_message, const MarkerConstPtr& new_message)
68 {
69  ROS_ASSERT(new_message->type == visualization_msgs::Marker::TRIANGLE_LIST);
70 
71  size_t num_points = new_message->points.size();
72  if( (num_points % 3) != 0 || num_points == 0 )
73  {
74  scene_node_->setVisible( false );
75  return;
76  }
77  else
78  {
79  scene_node_->setVisible( true );
80  }
81 
82  if (!manual_object_)
83  {
84  static uint32_t count = 0;
86  ss << "Triangle List Marker" << count++;
87  manual_object_ = context_->getSceneManager()->createManualObject(ss.str());
88  scene_node_->attachObject(manual_object_);
89 
90  ss << "Material";
91  material_name_ = ss.str();
92  material_ = Ogre::MaterialManager::getSingleton().create( material_name_, ROS_PACKAGE_NAME );
93  material_->setReceiveShadows(false);
94  material_->getTechnique(0)->setLightingEnabled(true);
95  material_->setCullingMode(Ogre::CULL_NONE);
96 
97  handler_.reset( new MarkerSelectionHandler( this, MarkerID( new_message->ns, new_message->id ), context_ ));
98  }
99 
100  Ogre::Vector3 pos, scale;
101  Ogre::Quaternion orient;
102  if (!transform(new_message, pos, orient, scale))
103  {
104  ROS_DEBUG("Unable to transform marker message");
105  scene_node_->setVisible( false );
106  return;
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,
148  new_message->colors[i+c].g,
149  new_message->colors[i+c].b,
150  new_message->color.a * new_message->colors[i+c].a);
151  }
152  else if (has_face_colors)
153  {
154  any_vertex_has_alpha = any_vertex_has_alpha || (new_message->colors[i/3].a < 0.9998);
155  manual_object_->colour(new_message->colors[i/3].r,
156  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) || (has_vertex_colors && any_vertex_has_alpha))
182  {
183  material_->getTechnique(0)->setSceneBlending( Ogre::SBT_TRANSPARENT_ALPHA );
184  material_->getTechnique(0)->setDepthWriteEnabled( false );
185  }
186  else
187  {
188  material_->getTechnique(0)->setSceneBlending( Ogre::SBT_REPLACE );
189  material_->getTechnique(0)->setDepthWriteEnabled( true );
190  }
191 
192  handler_->addTrackedObject( manual_object_ );
193 }
194 
196 {
197  S_MaterialPtr materials;
198  materials.insert( material_ );
199  return materials;
200 }
201 
202 
203 }
204 
Ogre::SceneNode * scene_node_
Definition: marker_base.h:104
bool transform(const MarkerConstPtr &message, Ogre::Vector3 &pos, Ogre::Quaternion &orient, Ogre::Vector3 &scale)
Definition: marker_base.cpp:88
virtual void setPosition(const Ogre::Vector3 &position)
TFSIMD_FORCE_INLINE const tfScalar & y() const
std::pair< std::string, int32_t > MarkerID
Ogre::ManualObject * manual_object_
virtual void onNewMessage(const MarkerConstPtr &old_message, const MarkerConstPtr &new_message)
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:110
DisplayContext * context_
Definition: marker_base.h:102
TFSIMD_FORCE_INLINE const tfScalar & x() const
TFSIMD_FORCE_INLINE const tfScalar & z() const
TriangleListMarker(MarkerDisplay *owner, DisplayContext *context, Ogre::SceneNode *parent_node)
virtual S_MaterialPtr getMaterials()
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.
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.
#define ROS_DEBUG(...)


rviz
Author(s): Dave Hershberger, David Gossow, Josh Faust
autogenerated on Wed Aug 28 2019 04:01:51