effort_visual.cpp
Go to the documentation of this file.
2 #include <OgreSceneNode.h>
3 #include <OgreSceneManager.h>
4 
7 
8 #include <ros/ros.h>
9 
10 #include <utility>
11 
12 #include "effort_visual.h"
13 
14 namespace rviz
15 {
16 EffortVisual::EffortVisual(Ogre::SceneManager* scene_manager, Ogre::SceneNode* parent_node)
17  : scene_manager_(scene_manager), parent_node_(parent_node)
18 {
19 }
20 
22 {
23  // Delete the arrow to make it disappear.
24  for (auto& pair : effort_circle_)
25  delete pair.second;
26 
27  for (auto& pair : effort_arrow_)
28  delete pair.second;
29 }
30 
31 void EffortVisual::getRainbowColor(float value, Ogre::ColourValue& color)
32 {
33  value = std::min(value, 1.0f);
34  value = std::max(value, 0.0f);
35 
36  float h = value * 5.0f + 1.0f;
37  int i = floor(h);
38  float f = h - i;
39  if (!(i & 1))
40  f = 1 - f; // if i is even
41  float n = 1 - f;
42 
43  if (i <= 1)
44  color[0] = n, color[1] = 0, color[2] = 1;
45  else if (i == 2)
46  color[0] = 0, color[1] = n, color[2] = 1;
47  else if (i == 3)
48  color[0] = 0, color[1] = 1, color[2] = n;
49  else if (i == 4)
50  color[0] = n, color[1] = 1, color[2] = 0;
51  else if (i >= 5)
52  color[0] = 1, color[1] = n, color[2] = 0;
53 }
54 
55 void EffortVisual::setEffort(const std::string& joint_name, double effort, double max_effort)
56 {
57  bool enabled = effort_enabled_.insert(std::make_pair(joint_name, true)).first->second;
58 
59  // enable or disable draw
60  if (effort_circle_.find(joint_name) != effort_circle_.end() && !enabled) // enable->disable
61  {
62  delete (effort_circle_[joint_name]);
63  delete (effort_arrow_[joint_name]);
64  effort_circle_.erase(joint_name);
65  effort_arrow_.erase(joint_name);
66  }
67  if (effort_circle_.find(joint_name) == effort_circle_.end() && enabled) // disable -> enable
68  {
71  }
72 
73  if (!enabled)
74  return;
75 
76  double effort_value;
77 
78  if (max_effort != 0.0)
79  {
80  effort_value = std::min(fabs(effort) / max_effort, 1.0) + 0.05;
81  }
82  else
83  {
84  effort_value = fabs(effort) + 0.05;
85  }
86 
87  effort_arrow_[joint_name]->set(0, width_ * 2, width_ * 2 * 1.0, width_ * 2 * 2.0);
88  if (effort > 0)
89  {
90  effort_arrow_[joint_name]->setDirection(orientation_[joint_name] * Ogre::Vector3(-1, 0, 0));
91  }
92  else
93  {
94  effort_arrow_[joint_name]->setDirection(orientation_[joint_name] * Ogre::Vector3(1, 0, 0));
95  }
96  effort_arrow_[joint_name]->setPosition(orientation_[joint_name] *
97  Ogre::Vector3(0, 0.05 + effort_value * scale_ * 0.5, 0) +
98  position_[joint_name]);
99  effort_circle_[joint_name]->clear();
100  effort_circle_[joint_name]->setLineWidth(width_);
101  for (int i = 0; i < 30; i++)
102  {
103  Ogre::Vector3 point =
104  Ogre::Vector3((0.05 + effort_value * scale_ * 0.5) * sin(i * 2 * M_PI / 32),
105  (0.05 + effort_value * scale_ * 0.5) * cos(i * 2 * M_PI / 32), 0);
106  if (effort < 0)
107  point.x = -point.x;
108  effort_circle_[joint_name]->addPoint(orientation_[joint_name] * point + position_[joint_name]);
109  }
110  Ogre::ColourValue color;
111  getRainbowColor(effort_value, color);
112  effort_arrow_[joint_name]->setColor(color.r, color.g, color.b, color.a);
113  effort_circle_[joint_name]->setColor(color.r, color.g, color.b, color.a);
114 }
115 
116 void EffortVisual::setFrameEnabled(const std::string& joint_name, const bool e)
117 {
118  effort_enabled_[joint_name] = e;
119 }
120 
121 // Position and orientation are passed through to the SceneNode.
122 void EffortVisual::setFramePosition(const std::string& joint_name, const Ogre::Vector3& position)
123 {
124  position_[joint_name] = position;
125 }
126 
127 void EffortVisual::setFrameOrientation(const std::string& joint_name, const Ogre::Quaternion& orientation)
128 {
129  orientation_[joint_name] = orientation;
130 }
131 
133 {
134  width_ = w;
135 }
136 
138 {
139  scale_ = s;
140 }
141 
142 } // end namespace rviz
rviz::BillboardLine
An object that displays a multi-segment line strip rendered as billboards.
Definition: billboard_line.h:58
rviz::EffortVisual::setFrameOrientation
void setFrameOrientation(const std::string &joint_name, const Ogre::Quaternion &orientation)
Definition: effort_visual.cpp:127
rviz::EffortVisual::orientation_
std::map< std::string, Ogre::Quaternion > orientation_
Definition: effort_visual.h:52
rviz::EffortVisual::setFrameEnabled
void setFrameEnabled(const std::string &joint_name, const bool e)
Definition: effort_visual.cpp:116
rviz::Arrow
An arrow consisting of a cylinder and a cone.
Definition: arrow.h:57
rviz::EffortVisual::getRainbowColor
void getRainbowColor(float value, Ogre::ColourValue &color)
Definition: effort_visual.cpp:31
s
XmlRpcServer s
ros.h
rviz::EffortVisual::position_
std::map< std::string, Ogre::Vector3 > position_
Definition: effort_visual.h:51
rviz::EffortVisual::effort_circle_
std::map< std::string, rviz::BillboardLine * > effort_circle_
Definition: effort_visual.h:44
f
f
rviz
Definition: add_display_dialog.cpp:54
rviz::EffortVisual::setFramePosition
void setFramePosition(const std::string &joint_name, const Ogre::Vector3 &position)
Definition: effort_visual.cpp:122
arrow.h
ogre_vector.h
rviz::EffortVisual::scale_
float scale_
Definition: effort_visual.h:54
rviz::EffortVisual::effort_arrow_
std::map< std::string, rviz::Arrow * > effort_arrow_
Definition: effort_visual.h:45
rviz::EffortVisual::setWidth
void setWidth(float w)
Definition: effort_visual.cpp:132
rviz::EffortVisual::parent_node_
Ogre::SceneNode * parent_node_
Definition: effort_visual.h:49
rviz::EffortVisual::width_
float width_
Definition: effort_visual.h:54
billboard_line.h
rviz::EffortVisual::setScale
void setScale(float s)
Definition: effort_visual.cpp:137
rviz::EffortVisual::scene_manager_
Ogre::SceneManager * scene_manager_
Definition: effort_visual.h:48
rviz::EffortVisual::setEffort
void setEffort(const std::string &joint_name, double effort, double max_effort)
Definition: effort_visual.cpp:55
rviz::EffortVisual::~EffortVisual
virtual ~EffortVisual()
Definition: effort_visual.cpp:21
rviz::EffortVisual::effort_enabled_
std::map< std::string, bool > effort_enabled_
Definition: effort_visual.h:46
effort_visual.h
rviz::EffortVisual::EffortVisual
EffortVisual(Ogre::SceneManager *scene_manager, Ogre::SceneNode *parent_node)
Definition: effort_visual.cpp:16


rviz
Author(s): Dave Hershberger, David Gossow, Josh Faust, William Woodall
autogenerated on Fri Aug 2 2024 08:43:09