shape_marker.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2009, 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 "shape_marker.h"
33 
34 #include "rviz/display_context.h"
36 
38 
39 #include <OgreSceneNode.h>
40 #include <OgreMatrix3.h>
41 
42 namespace rviz
43 {
44 ShapeMarker::ShapeMarker(MarkerDisplay* owner, DisplayContext* context, Ogre::SceneNode* parent_node)
45  : MarkerBase(owner, context, parent_node), shape_(nullptr)
46 {
47 }
48 
50 {
51  delete shape_;
52 }
53 
54 void ShapeMarker::onNewMessage(const MarkerConstPtr& old_message, const MarkerConstPtr& new_message)
55 {
56  if (!shape_ || old_message->type != new_message->type)
57  {
58  delete shape_;
59  shape_ = nullptr;
60 
61  Shape::Type shape_type = Shape::Cube;
62  switch (new_message->type)
63  {
64  case visualization_msgs::Marker::CUBE:
65  shape_type = Shape::Cube;
66  break;
67  case visualization_msgs::Marker::CYLINDER:
68  shape_type = Shape::Cylinder;
69  break;
70  case visualization_msgs::Marker::SPHERE:
71  shape_type = Shape::Sphere;
72  break;
73  default:
74  ROS_BREAK();
75  break;
76  }
77  shape_ = new Shape(shape_type, context_->getSceneManager(), scene_node_);
78 
79  handler_.reset(
80  new MarkerSelectionHandler(this, MarkerID(new_message->ns, new_message->id), context_));
81  handler_->addTrackedObjects(shape_->getRootNode());
82  }
83 
84  Ogre::Vector3 pos, scale, scale_correct;
85  Ogre::Quaternion orient;
86  if (!transform(new_message, pos, orient, scale))
87  return;
88 
89  setPosition(pos);
90  setOrientation(orient * Ogre::Quaternion(Ogre::Degree(90), Ogre::Vector3(1, 0, 0)));
91 
92  scale_correct = Ogre::Quaternion(Ogre::Degree(90), Ogre::Vector3(1, 0, 0)) * scale;
93 
94  shape_->setScale(scale_correct);
95 
96  shape_->setColor(new_message->color.r, new_message->color.g, new_message->color.b,
97  new_message->color.a);
98 }
99 
101 {
102  S_MaterialPtr materials;
103  extractMaterials(shape_->getEntity(), materials);
104  return materials;
105 }
106 
107 } // 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
void setScale(const Ogre::Vector3 &scale) override
Set the scale of the object. Always relative to the identity orientation of the object.
Definition: shape.cpp:160
virtual void setPosition(const Ogre::Vector3 &position)
void setColor(float r, float g, float b, float a) override
Set the color of the object. Values are in the range [0, 1].
Definition: shape.cpp:140
ShapeMarker(MarkerDisplay *owner, DisplayContext *context, Ogre::SceneNode *parent_node)
void extractMaterials(Ogre::Entity *entity, S_MaterialPtr &materials)
S_MaterialPtr getMaterials() override
std::pair< std::string, int32_t > MarkerID
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
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)
Ogre::SceneNode * getRootNode()
Get the root scene node (pivot node) for this object.
Definition: shape.h:100
#define ROS_BREAK()
~ShapeMarker() override
Displays "markers" sent in by other ROS nodes on the "visualization_marker" topic.
Ogre::Entity * getEntity()
Definition: shape.h:110
void onNewMessage(const MarkerConstPtr &old_message, const MarkerConstPtr &new_message) override


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