PathVisual.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Software License Agreement (BSD License) *
3  * Copyright (C) 2016 by George Todoran <george.todoran@tuwien.ac.at> *
4  * *
5  * Redistribution and use in source and binary forms, with or without *
6  * modification, are permitted provided that the following conditions *
7  * are met: *
8  * *
9  * 1. Redistributions of source code must retain the above copyright *
10  * notice, this list of conditions and the following disclaimer. *
11  * 2. Redistributions in binary form must reproduce the above copyright *
12  * notice, this list of conditions and the following disclaimer in *
13  * the documentation and/or other materials provided with the *
14  * distribution. *
15  * 3. Neither the name of the copyright holder nor the names of its *
16  * contributors may be used to endorse or promote products derived *
17  * from this software without specific prior written permission. *
18  * *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT *
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS *
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE *
23  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, *
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, *
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; *
26  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER *
27  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT *
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY *
29  * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE *
30  * POSSIBILITY OF SUCH DAMAGE. *
31  ***************************************************************************/
32 
33 #include <OGRE/OgreVector3.h>
34 #include <OGRE/OgreSceneNode.h>
35 #include <OGRE/OgreSceneManager.h>
36 
37 #include <Path/PathVisual.h>
38 #include <eigen3/unsupported/Eigen/Splines>
39 
40 namespace tuw_nav_rviz {
41 
42 PathVisual::PathVisual ( Ogre::SceneManager* scene_manager, Ogre::SceneNode* parent_node ) {
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  colorOrient_ = Ogre::ColourValue ( 255, 255, 0 );
58  scalePath_ = 0.01;
59  scaleOrient_ = 0.1;
60  deltaSOrient_ = 0.1;
61 }
62 
64  // Destroy the frame node since we don't need it anymore.
65  scene_manager_->destroySceneNode ( frame_node_ );
66 }
67 
68 void PathVisual::setMessage ( const nav_msgs::Path::ConstPtr& msg ) {
69  static double timeOld_;
70  if( timeOld_ == msg->header.stamp.toSec() ){ return; }
71  timeOld_ = msg->header.stamp.toSec();
72 
73  pathPtsXY_ .resize ( msg->poses.size() );
74  for( size_t i = 0; i < pathPtsXY_.size(); ++i) {
75  double p_x = msg->poses[i].pose.position.x;
76  double p_y = msg->poses[i].pose.position.y;
77  double p_z = msg->poses[i].pose.position.z;
78 
79 // Ogre::Quaternion rotation = Ogre::Quaternion ( Ogre::Radian( (*spline_)(i / (double)pointsNrPath_ )(2) + atan2(v_y, v_x) ), Ogre::Vector3::UNIT_Z );
80 
81 
82  Ogre::Quaternion rotation;
83  rotation.x = msg->poses[i].pose.orientation.x;
84  rotation.y = msg->poses[i].pose.orientation.y;
85  rotation.z = msg->poses[i].pose.orientation.z;
86  rotation.w = msg->poses[i].pose.orientation.w;
87  Ogre::Quaternion rotation2 = Ogre::Quaternion ( Ogre::Radian( -Ogre::Math::PI/2.), Ogre::Vector3::UNIT_Y );
88 
90  pathPtsXY_[i]->setColor ( colorPath_ );
91  pathPtsXY_[i]->setPosition ( Ogre::Vector3 ( p_x, p_y, p_z ) );
92  pathPtsXY_[i]->setOrientation ( rotation *rotation2 );
93  pathPtsXY_[i]->setScale ( Ogre::Vector3 ( scalePath_, scalePath_, scalePath_ ) );
94  }
95 
96  pathPtsTheta_.clear();
97 
98 
99  if(pathPtsXY_.size() > 0) {
100  pathPtsTheta_.reserve ( pathPtsXY_.size() );
101  double d = 0;
103  pathPtsTheta_.back().reset ( new rviz::Arrow ( scene_manager_, frame_node_ ) );
104  pathPtsTheta_.back()->setColor ( colorOrient_ );
105  pathPtsTheta_.back()->setPosition ( pathPtsXY_[0]->getPosition () );
106  pathPtsTheta_.back()->setOrientation ( pathPtsXY_[0]->getOrientation() );
107  pathPtsTheta_.back()->setScale ( Ogre::Vector3 ( scaleOrient_, scaleOrient_, scaleOrient_ ) );
108  for( size_t i = 1; i < pathPtsXY_.size(); ++i) {
109 
110  d += pathPtsXY_[i]->getPosition().distance(pathPtsXY_[i-1]->getPosition());
111  if ( d < deltaSOrient_ * (pathPtsTheta_.size() /*+ 1*/) ) { continue; }
112 // d = d - (int)(d / deltaSOrient_) * deltaSOrient_;
113 
115  pathPtsTheta_.back().reset ( new rviz::Arrow ( scene_manager_, frame_node_ ) );
116  pathPtsTheta_.back()->setColor ( colorOrient_ );
117  pathPtsTheta_.back()->setPosition ( pathPtsXY_[i]->getPosition () );
118  pathPtsTheta_.back()->setOrientation ( pathPtsXY_[i]->getOrientation() );
119  pathPtsTheta_.back()->setScale ( Ogre::Vector3 ( scaleOrient_, scaleOrient_, scaleOrient_ ) );
120  }
122  pathPtsTheta_.back().reset ( new rviz::Arrow ( scene_manager_, frame_node_ ) );
123  pathPtsTheta_.back()->setColor ( colorOrient_ );
124  pathPtsTheta_.back()->setPosition ( pathPtsXY_.back()->getPosition () );
125  pathPtsTheta_.back()->setOrientation ( pathPtsXY_.back()->getOrientation() );
126  pathPtsTheta_.back()->setScale ( Ogre::Vector3 ( scaleOrient_, scaleOrient_, scaleOrient_ ) );
127  }
128 }
129 
130 // Position is passed through to the SceneNode.
131 void PathVisual::setFramePosition ( const Ogre::Vector3& position ) {
132  frame_node_->setPosition ( position );
133 }
134 
135 // Orientation is passed through to the SceneNode.
136 void PathVisual::setFrameOrientation ( const Ogre::Quaternion& orientation ) {
137  frame_node_->setOrientation ( orientation );
138 }
139 
140 // Color is passed through to the Shape object.
141 void PathVisual::setPathColor ( Ogre::ColourValue color ) {
142  colorPath_ = color;
143  for ( auto& pathXYi : pathPtsXY_ ) { pathXYi ->setColor ( colorPath_ ); }
144 }
145 
146 // Color is passed through to the Shape object.
147 void PathVisual::setOrientColor ( Ogre::ColourValue color ) {
148  colorOrient_ = color;
149  for ( auto& pathThetai: pathPtsTheta_ ) { pathThetai->setColor ( colorOrient_ ); }
150 }
151 
152 // Shape type is passed through to the Shape object.
154  shape_type_ = shape_type;
155  for ( auto& splineXYi: pathPtsXY_ ) {
156  Ogre::Vector3 posOld = splineXYi->getPosition();
157  Ogre::Quaternion orientOld = splineXYi->getOrientation();
158  splineXYi.reset ( new rviz::Shape ( shape_type_, scene_manager_, frame_node_ ) );
159  splineXYi->setColor ( colorPath_ );
160  splineXYi->setPosition ( posOld );
161  splineXYi->setOrientation ( orientOld );
162  splineXYi->setScale ( Ogre::Vector3 ( scalePath_, scalePath_, scalePath_ ) );
163  }
164 }
165 
166 // Scale is passed through to the Shape object.
167 void PathVisual::setPathScale ( float scale ) {
168  scalePath_ = scale;
169  for ( auto& pathXYi : pathPtsXY_ ) { pathXYi ->setScale ( Ogre::Vector3 ( scalePath_, scalePath_, scalePath_ ) ); }
170 }
171 
172 // Scale is passed through to the Shape object.
173 void PathVisual::setOrientScale ( float scale ) {
174  scaleOrient_ = scale;
175  for ( auto& pathThetai: pathPtsTheta_ ) { pathThetai->setScale ( Ogre::Vector3 ( scaleOrient_, scaleOrient_, scaleOrient_ ) ); }
176 }
177 
178 void PathVisual::setOrientDeltaS ( double deltaS ) {
179  deltaSOrient_ = fmax(1e-5, deltaS);
180 
181  pathPtsTheta_.clear();
182  if(pathPtsXY_.size() > 0) {
183  pathPtsTheta_.reserve ( pathPtsXY_.size() );
184  double d = 0;
186  pathPtsTheta_.back().reset ( new rviz::Arrow ( scene_manager_, frame_node_ ) );
187  pathPtsTheta_.back()->setColor ( colorOrient_ );
188  pathPtsTheta_.back()->setPosition ( pathPtsXY_[0]->getPosition () );
189  pathPtsTheta_.back()->setOrientation ( pathPtsXY_[0]->getOrientation() );
190  pathPtsTheta_.back()->setScale ( Ogre::Vector3 ( scaleOrient_, scaleOrient_, scaleOrient_ ) );
191  for( size_t i = 1; i < pathPtsXY_.size(); ++i) {
192 
193  d += pathPtsXY_[i]->getPosition().distance(pathPtsXY_[i-1]->getPosition());
194  if ( d < deltaSOrient_ * (pathPtsTheta_.size() /*+ 1*/) ) { continue; }
195 // d = d - (int)(d / deltaSOrient_) * deltaSOrient_;
196 
198  pathPtsTheta_.back().reset ( new rviz::Arrow ( scene_manager_, frame_node_ ) );
199  pathPtsTheta_.back()->setColor ( colorOrient_ );
200  pathPtsTheta_.back()->setPosition ( pathPtsXY_[i]->getPosition () );
201  pathPtsTheta_.back()->setOrientation ( pathPtsXY_[i]->getOrientation() );
202  pathPtsTheta_.back()->setScale ( Ogre::Vector3 ( scaleOrient_, scaleOrient_, scaleOrient_ ) );
203  }
205  pathPtsTheta_.back().reset ( new rviz::Arrow ( scene_manager_, frame_node_ ) );
206  pathPtsTheta_.back()->setColor ( colorOrient_ );
207  pathPtsTheta_.back()->setPosition ( pathPtsXY_.back()->getPosition () );
208  pathPtsTheta_.back()->setOrientation ( pathPtsXY_.back()->getOrientation() );
209  pathPtsTheta_.back()->setScale ( Ogre::Vector3 ( scaleOrient_, scaleOrient_, scaleOrient_ ) );
210  }
211 }
212 
213 
214 } // end namespace tuw_nav_rviz
215 
d
void setFramePosition(const Ogre::Vector3 &position)
Definition: PathVisual.cpp:131
PathVisual(Ogre::SceneManager *scene_manager, Ogre::SceneNode *parent_node)
Definition: PathVisual.cpp:42
void setMessage(const nav_msgs::Path::ConstPtr &msg)
Definition: PathVisual.cpp:68
void setShape(rviz::Shape::Type shape_type)
Definition: PathVisual.cpp:153
Ogre::ColourValue colorPath_
Definition: PathVisual.h:106
void setOrientDeltaS(double deltaS)
Definition: PathVisual.cpp:178
rviz::Shape::Type shape_type_
Definition: PathVisual.h:110
void setOrientScale(float scale)
Definition: PathVisual.cpp:173
Ogre::SceneNode * frame_node_
Definition: PathVisual.h:99
std::vector< boost::shared_ptr< rviz::Shape > > pathPtsXY_
Definition: PathVisual.h:94
std::vector< boost::shared_ptr< rviz::Arrow > > pathPtsTheta_
Definition: PathVisual.h:95
void setPathColor(Ogre::ColourValue color)
Definition: PathVisual.cpp:141
void setFrameOrientation(const Ogre::Quaternion &orientation)
Definition: PathVisual.cpp:136
Ogre::ColourValue colorOrient_
Definition: PathVisual.h:107
void setOrientColor(Ogre::ColourValue color)
Definition: PathVisual.cpp:147
Ogre::SceneManager * scene_manager_
Definition: PathVisual.h:103
void setPathScale(float scale)
Definition: PathVisual.cpp:167


tuw_nav_rviz
Author(s):
autogenerated on Mon Jun 10 2019 15:40:12