arrow_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 <OgreVector3.h>
31 #include <OgreQuaternion.h>
32 #include <OgreSceneNode.h>
33 #include <OgreSceneManager.h>
34 #include <OgreEntity.h>
35 
38 #include "rviz/display_context.h"
42 
44 
45 namespace rviz
46 {
47 ArrowMarker::ArrowMarker(MarkerDisplay* owner, DisplayContext* context, Ogre::SceneNode* parent_node)
48  : MarkerBase(owner, context, parent_node), arrow_(nullptr), last_arrow_set_from_points_(false)
49 {
50  child_scene_node_ = scene_node_->createChildSceneNode();
51 }
52 
54 {
55  delete arrow_;
56  context_->getSceneManager()->destroySceneNode(child_scene_node_);
57 }
58 
60 {
61  arrow_->set(0.77, 1.0, 0.23, 2.0);
62 }
63 
64 void ArrowMarker::onNewMessage(const MarkerConstPtr& /*old_message*/, const MarkerConstPtr& new_message)
65 {
66  ROS_ASSERT(new_message->type == visualization_msgs::Marker::ARROW);
67  ROS_ASSERT(new_message->points.empty() || new_message->points.size() >= 2);
68 
69  if (!arrow_)
70  {
73  handler_.reset(
74  new MarkerSelectionHandler(this, MarkerID(new_message->ns, new_message->id), context_));
75  handler_->addTrackedObjects(arrow_->getSceneNode());
76  }
77 
78  Ogre::Vector3 pos, scale;
79  Ogre::Quaternion orient;
80  transform(new_message, pos, orient, scale);
81  setPosition(pos);
82  setOrientation(orient);
83 
84  arrow_->setColor(new_message->color.r, new_message->color.g, new_message->color.b,
85  new_message->color.a);
86 
87  // compute translation & rotation from the two points
88  if (new_message->points.size() == 2)
89  {
91 
92  Ogre::Vector3 point1(new_message->points[0].x, new_message->points[0].y, new_message->points[0].z);
93  Ogre::Vector3 point2(new_message->points[1].x, new_message->points[1].y, new_message->points[1].z);
94 
95  Ogre::Vector3 direction = point2 - point1;
96  float distance = direction.length();
97 
98  float head_length_proportion =
99  0.23; // Seems to be a good value based on default in arrow.h of shaft:head ratio of 1:0.3
100  float head_length = head_length_proportion * distance;
101  if (new_message->scale.z != 0.0)
102  {
103  float length = new_message->scale.z;
104  head_length = std::max<double>(0.0, std::min<double>(length, distance)); // clamp
105  }
106  float shaft_length = distance - head_length;
107 
108  arrow_->set(shaft_length, new_message->scale.x, head_length, new_message->scale.y);
109 
110  direction.normalise();
111 
112  // for some reason the arrow goes into the y direction by default
113  Ogre::Quaternion orient = Ogre::Vector3::NEGATIVE_UNIT_Z.getRotationTo(direction);
114 
115  arrow_->setPosition(point1);
116  arrow_->setOrientation(orient);
117  }
118  else
119  {
121  {
122  // Reset arrow to default proportions if we previously set it from points
125  }
126  arrow_->setScale(scale);
127 
128  Ogre::Quaternion orient = Ogre::Vector3::NEGATIVE_UNIT_Z.getRotationTo(Ogre::Vector3(1, 0, 0));
129  arrow_->setOrientation(orient);
130  }
131 }
132 
134 {
135  S_MaterialPtr materials;
136  extractMaterials(arrow_->getHead()->getEntity(), materials);
137  extractMaterials(arrow_->getShaft()->getEntity(), materials);
138  return materials;
139 }
140 
141 } // 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 setPosition(const Ogre::Vector3 &position) override
Set the position of the base of the arrow.
Definition: arrow.cpp:114
virtual void setPosition(const Ogre::Vector3 &position)
void set(float shaft_length, float shaft_diameter, float head_length, float head_diameter)
Set the parameters for this arrow.
Definition: arrow.cpp:74
Shape * getHead()
Definition: arrow.h:163
void extractMaterials(Ogre::Entity *entity, S_MaterialPtr &materials)
Ogre::SceneNode * getSceneNode()
Get the scene node associated with this arrow.
Definition: arrow.h:149
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
S_MaterialPtr getMaterials() override
void onNewMessage(const MarkerConstPtr &old_message, const MarkerConstPtr &new_message) override
DisplayContext * context_
Definition: marker_base.h:114
bool last_arrow_set_from_points_
Definition: arrow_marker.h:59
void setScale(const Ogre::Vector3 &scale) override
Set the scale of the object. Always relative to the identity orientation of the object.
Definition: arrow.cpp:134
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.
~ArrowMarker() override
Ogre::SceneNode * child_scene_node_
Definition: arrow_marker.h:57
std::set< Ogre::MaterialPtr > S_MaterialPtr
Definition: marker_base.h:57
An arrow consisting of a cylinder and a cone.
Definition: arrow.h:59
virtual void setOrientation(const Ogre::Quaternion &orientation)
virtual void setDefaultProportions()
void setOrientation(const Ogre::Quaternion &orientation) override
Set the orientation.
Definition: arrow.cpp:119
TFSIMD_FORCE_INLINE tfScalar length(const Quaternion &q)
Shape * getShaft()
Definition: arrow.h:159
void setColor(float r, float g, float b, float a) override
Set the color of this arrow. Sets both the head and shaft color to the same value. Values are in the range [0, 1].
Definition: arrow.cpp:89
#define ROS_ASSERT(cond)
Displays "markers" sent in by other ROS nodes on the "visualization_marker" topic.
ArrowMarker(MarkerDisplay *owner, DisplayContext *context, Ogre::SceneNode *parent_node)
Ogre::Entity * getEntity()
Definition: shape.h:110


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