VoronoiSegmentVisual.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012, 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 <OGRE/OgreVector3.h>
31 #include <OGRE/OgreSceneNode.h>
32 #include <OGRE/OgreSceneManager.h>
33 
34 #include <rviz/ogre_helpers/line.h>
35 #include <tuw_multi_robot_msgs/Vertex.h>
37 
38 namespace tuw_multi_robot_rviz
39 {
40 
41 VoronoiSegmentVisual::VoronoiSegmentVisual(Ogre::SceneManager *scene_manager, Ogre::SceneNode *parent_node)
42 {
43  scene_manager_ = scene_manager;
44 
45  // Ogre::SceneNode s form a tree, with each node storing the
46  // transform (position and orientation) of itself relative to its
47  // parent. Ogre does the math of combining those transforms when it
48  // is time to render.
49  //
50  // Here we create a node to store the pose of the MarkerDetection's header frame
51  // relative to the RViz fixed frame.
52  frame_node_ = parent_node->createChildSceneNode();
53 
54  // initialize global variables
55  colorPath_ = Ogre::ColourValue(255, 0, 0);
56  scalePoint_ = 0.1;
57  scalePath_ = 1;
58 }
59 
61 {
62  // Destroy the frame node since we don't need it anymore.
63  scene_manager_->destroySceneNode(frame_node_);
64 }
65 
66 void VoronoiSegmentVisual::setMessage(const tuw_multi_robot_msgs::Graph::ConstPtr &msg)
67 {
68  static double timeOld_;
69  if (timeOld_ == msg->header.stamp.toSec())
70  {
71  return;
72  }
73  timeOld_ = msg->header.stamp.toSec();
74 
75  pathLine.resize(msg->vertices.size() * 4);
76  for (size_t i = 0; i < msg->vertices.size(); ++i)
77  {
78  tuw_multi_robot_msgs::Vertex seg = msg->vertices[i];
79  geometry_msgs::Point p1 = seg.path.front();
80  geometry_msgs::Point p2 = seg.path.back();
81 
82  // Ogre::Quaternion rotation = Ogre::Quaternion ( Ogre::Radian( (*spline_)(i / (double)pointsNrPath_ )(2) + atan2(v_y, v_x) ), Ogre::Vector3::UNIT_Z );
83  Line l = {
84  (float)((p1.x + 0.5) * msg->resolution + msg->origin.position.x),
85  (float)((p1.y + 0.5) * msg->resolution + msg->origin.position.y),
86  (float)((p2.x + 0.5) * msg->resolution + msg->origin.position.x),
87  (float)((p2.y + 0.5) * msg->resolution + msg->origin.position.y)};
88  float z1 = p1.z * msg->resolution + msg->origin.position.z;
89  float z2 = p2.z * msg->resolution + msg->origin.position.z;
90 
91  Line l1 = offsetLine(l, msg->resolution * (0.5 + seg.width / 2));
92  Line l2 = offsetLine(l, msg->resolution * (-0.5 - seg.width / 2));
93 
94  pathLine[i * 4].reset(new rviz::Line(scene_manager_, frame_node_));
95  pathLine[i * 4]->setColor(colorPath_);
96  pathLine[i * 4]->setPoints(Ogre::Vector3(l1.x0, l1.y0, z1), Ogre::Vector3(l1.x1, l1.y1, z2));
97  pathLine[i * 4]->setScale(Ogre::Vector3(scalePath_, scalePath_, scalePath_));
98 
99  pathLine[i * 4 + 1].reset(new rviz::Line(scene_manager_, frame_node_));
100  pathLine[i * 4 + 1]->setColor(colorPath_);
101  pathLine[i * 4 + 1]->setPoints(Ogre::Vector3(l2.x0, l2.y0, z1), Ogre::Vector3(l2.x1, l2.y1, z2));
102  pathLine[i * 4 + 1]->setScale(Ogre::Vector3(scalePath_, scalePath_, scalePath_));
103 
104  pathLine[i * 4 + 2].reset(new rviz::Line(scene_manager_, frame_node_));
105  pathLine[i * 4 + 2]->setColor(colorPath_);
106  pathLine[i * 4 + 2]->setPoints(Ogre::Vector3(l1.x0, l1.y0, z1), Ogre::Vector3(l2.x0, l2.y0, z2));
107  pathLine[i * 4 + 2]->setScale(Ogre::Vector3(scalePath_, scalePath_, scalePath_));
108 
109  pathLine[i * 4 + 3].reset(new rviz::Line(scene_manager_, frame_node_));
110  pathLine[i * 4 + 3]->setColor(colorPath_);
111  pathLine[i * 4 + 3]->setPoints(Ogre::Vector3(l2.x1, l2.y1, z1), Ogre::Vector3(l1.x1, l1.y1, z2));
112  pathLine[i * 4 + 3]->setScale(Ogre::Vector3(scalePath_, scalePath_, scalePath_));
113  }
114 }
115 
117 {
118  Line ret;
119  float dx = _l.x0 - _l.x1;
120  float dy = _l.y0 - _l.y1;
121  float l = std::sqrt(dx * dx + dy * dy);
122 
123  ret.x0 = _l.x0 + d * (-dy) / l;
124  ret.x1 = _l.x1 + d * (-dy) / l;
125  ret.y0 = _l.y0 + d * dx / l;
126  ret.y1 = _l.y1 + d * dx / l;
127 
128  return ret;
129 }
130 
131 // Position is passed through to the SceneNode.
132 void VoronoiSegmentVisual::setFramePosition(const Ogre::Vector3 &position)
133 {
134  frame_node_->setPosition(position);
135 }
136 
137 // Orientation is passed through to the SceneNode.
138 void VoronoiSegmentVisual::setFrameOrientation(const Ogre::Quaternion &orientation)
139 {
140  frame_node_->setOrientation(orientation);
141 }
142 
143 // Color is passed through to the Shape object.
144 void VoronoiSegmentVisual::setPathColor(Ogre::ColourValue color)
145 {
146  colorPath_ = color;
147  for (auto &pathXYi : pathLine)
148  {
149  pathXYi->setColor(colorPath_);
150  }
151 }
152 
154 {
155  scalePath_ = scale;
156  for (auto &pathThetai : pathLine)
157  {
158  pathThetai->setScale(Ogre::Vector3(scalePath_, scalePath_, scalePath_));
159  }
160 }
161 
162 } // namespace tuw_multi_robot_rviz
std::vector< boost::shared_ptr< rviz::Line > > pathLine
void setMessage(const tuw_multi_robot_msgs::Graph::ConstPtr &msg)
void setFrameOrientation(const Ogre::Quaternion &orientation)
void setFramePosition(const Ogre::Vector3 &position)
VoronoiSegmentVisual(Ogre::SceneManager *scene_manager, Ogre::SceneNode *parent_node)


tuw_multi_robot_rviz
Author(s): Benjamin Binder
autogenerated on Mon Jun 10 2019 15:42:40