wrench_visual.cpp
Go to the documentation of this file.
1 #include <OgreVector3.h>
2 #include <OgreSceneNode.h>
3 #include <OgreSceneManager.h>
4 
7 
8 #include <ros/ros.h>
9 
10 #include "wrench_visual.h"
11 
12 namespace rviz
13 {
14 WrenchVisual::WrenchVisual(Ogre::SceneManager* scene_manager, Ogre::SceneNode* parent_node)
15 {
16  scene_manager_ = scene_manager;
17 
18  // Ogre::SceneNode s form a tree, with each node storing the
19  // transform (position and orientation) of itself relative to its
20  // parent. Ogre does the math of combining those transforms when it
21  // is time to render.
22  //
23  // Here we create a node to store the pose of the WrenchStamped's header frame
24  // relative to the RViz fixed frame.
25  frame_node_ = parent_node->createChildSceneNode();
26  force_node_ = frame_node_->createChildSceneNode();
27  torque_node_ = frame_node_->createChildSceneNode();
28  hide_small_values_ = true;
29 
30  // We create the arrow object within the frame node so that we can
31  // set its position and direction relative to its header frame.
36 }
37 
39 {
40  // Delete the arrow to make it disappear.
41  delete arrow_force_;
42  delete arrow_torque_;
43  delete circle_torque_;
44  delete circle_arrow_torque_;
45 
46  // Destroy the frame node since we don't need it anymore.
47  scene_manager_->destroySceneNode(frame_node_);
48 }
49 
50 
51 void WrenchVisual::setWrench(const geometry_msgs::Wrench& wrench)
52 {
53  Ogre::Vector3 force(wrench.force.x, wrench.force.y, wrench.force.z);
54  Ogre::Vector3 torque(wrench.torque.x, wrench.torque.y, wrench.torque.z);
55  setWrench(force, torque);
56 }
57 
58 void WrenchVisual::setWrench(const Ogre::Vector3& force, const Ogre::Vector3& torque)
59 {
60  double force_length = force.length() * force_scale_;
61  double torque_length = torque.length() * torque_scale_;
62  // hide markers if they get too short and hide_small_values_ is activated
63  // "too short" is defined as "force_length > width_"
64  bool show_force = (force_length > width_) || !hide_small_values_;
65  bool show_torque = (torque_length > width_) || !hide_small_values_;
66 
67  if (show_force)
68  {
69  arrow_force_->setScale(Ogre::Vector3(force_length, width_, width_));
70  arrow_force_->setDirection(force);
71  }
72  force_node_->setVisible(show_force);
73 
74  if (show_torque)
75  {
76  arrow_torque_->setScale(Ogre::Vector3(torque_length, width_, width_));
77  arrow_torque_->setDirection(torque);
78  Ogre::Vector3 axis_z(0, 0, 1);
79  Ogre::Quaternion orientation = axis_z.getRotationTo(torque);
80  if (std::isnan(orientation.x) || std::isnan(orientation.y) || std::isnan(orientation.z))
81  orientation = Ogre::Quaternion::IDENTITY;
82  // circle_arrow_torque_->setScale(Ogre::Vector3(width_, width_, 0.05));
83  circle_arrow_torque_->set(0, width_ * 0.1, width_ * 0.1 * 1.0, width_ * 0.1 * 2.0);
84  circle_arrow_torque_->setDirection(orientation * Ogre::Vector3(0, 1, 0));
85  circle_arrow_torque_->setPosition(orientation *
86  Ogre::Vector3(torque_length / 4, 0, torque_length / 2));
89  for (int i = 4; i <= 32; i++)
90  {
91  Ogre::Vector3 point =
92  Ogre::Vector3((torque_length / 4) * cos(i * 2 * M_PI / 32),
93  (torque_length / 4) * sin(i * 2 * M_PI / 32), torque_length / 2);
94  circle_torque_->addPoint(orientation * point);
95  }
96  }
97  torque_node_->setVisible(show_torque);
98 }
99 
100 // Position and orientation are passed through to the SceneNode.
101 void WrenchVisual::setFramePosition(const Ogre::Vector3& position)
102 {
103  frame_node_->setPosition(position);
104 }
105 
106 void WrenchVisual::setFrameOrientation(const Ogre::Quaternion& orientation)
107 {
108  frame_node_->setOrientation(orientation);
109 }
110 
111 // Color is passed through to the rviz object.
112 void WrenchVisual::setForceColor(float r, float g, float b, float a)
113 {
114  arrow_force_->setColor(r, g, b, a);
115 }
116 // Color is passed through to the rviz object.
117 void WrenchVisual::setTorqueColor(float r, float g, float b, float a)
118 {
119  arrow_torque_->setColor(r, g, b, a);
120  circle_torque_->setColor(r, g, b, a);
121  circle_arrow_torque_->setColor(r, g, b, a);
122 }
123 
125 {
126  force_scale_ = s;
127 }
128 
130 {
131  torque_scale_ = s;
132 }
133 
135 {
136  width_ = w;
137 }
138 
140 {
141  hide_small_values_ = h;
142 }
143 
144 
145 void WrenchVisual::setVisible(bool visible)
146 {
147  frame_node_->setVisible(visible);
148 }
149 
150 } // end namespace rviz
rviz::BillboardLine * circle_torque_
Definition: wrench_visual.h:61
void setFrameOrientation(const Ogre::Quaternion &orientation)
void setTorqueScale(float s)
rviz::Arrow * circle_arrow_torque_
Definition: wrench_visual.h:62
Ogre::SceneManager * scene_manager_
Definition: wrench_visual.h:75
void addPoint(const Ogre::Vector3 &point)
Ogre::SceneNode * frame_node_
Definition: wrench_visual.h:68
An object that displays a multi-segment line strip rendered as billboards.
void setWidth(float w)
void setDirection(const Ogre::Vector3 &direction)
Set the direction of the arrow.
Definition: arrow.cpp:126
void setPosition(const Ogre::Vector3 &position) override
Set the position of the base of the arrow.
Definition: arrow.cpp:114
void setTorqueColor(float r, float g, float b, float a)
void setFramePosition(const Ogre::Vector3 &position)
Ogre::SceneNode * force_node_
Definition: wrench_visual.h:70
void set(float shaft_length, float shaft_diameter, float head_length, float head_diameter)
Set the parameters for this arrow.
Definition: arrow.cpp:74
Ogre::SceneNode * torque_node_
Definition: wrench_visual.h:71
void setWrench(const Ogre::Vector3 &force, const Ogre::Vector3 &torque)
void setForceScale(float s)
void setVisible(bool visible)
void setColor(float r, float g, float b, float a) override
Set the color of the object. Values are in the range [0, 1].
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
rviz::Arrow * arrow_torque_
Definition: wrench_visual.h:60
void setForceColor(float r, float g, float b, float a)
rviz::Arrow * arrow_force_
Definition: wrench_visual.h:59
An arrow consisting of a cylinder and a cone.
Definition: arrow.h:59
void setHideSmallValues(bool h)
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
INLINE Rall1d< T, V, S > cos(const Rall1d< T, V, S > &arg)
WrenchVisual(Ogre::SceneManager *scene_manager, Ogre::SceneNode *parent_node)
INLINE Rall1d< T, V, S > sin(const Rall1d< T, V, S > &arg)
void setLineWidth(float width)


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