SplineVisual.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 <Spline/SplineVisual.h>
38 #include <eigen3/unsupported/Eigen/Splines>
39 
40 namespace tuw_nav_rviz_plugin {
41 
42 SplineVisual::SplineVisual ( 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  pointsNrPath_ = 100;
61  pointsNrOrient_ = 50;
62 }
63 
65  // Destroy the frame node since we don't need it anymore.
66  scene_manager_->destroySceneNode ( frame_node_ );
67 }
68 
69 void SplineVisual::setMessage ( const tuw_nav_msgs::Spline::ConstPtr& msg ) {
70  static double timeOld_;
71  if( timeOld_ == msg->header.stamp.toSec() ){ return; }
72  timeOld_ = msg->header.stamp.toSec();
73 
74  Eigen::MatrixXd vKnots(1, msg->knots.size() );
75  Eigen::MatrixXd mCtrls(msg->ctrls.size(), msg->ctrls[0].val.size() );
76  for( int i = 0; i < vKnots.cols(); ++i) { vKnots(i) = msg->knots[i]; }
77  for( int i = 0; i < mCtrls.rows(); ++i) { for( int j = 0; j < mCtrls.cols(); ++j) { mCtrls(i,j) = msg->ctrls[i].val[j]; } }
78  spline_ = boost::shared_ptr<Eigen::Spline3d>( new Eigen::Spline3d(vKnots, mCtrls) );
79 
80  splinePtsXY_ .resize ( pointsNrPath_ + 1 );
81  for( size_t i = 0; i <= pointsNrPath_; ++i) {
82  double p_x = (*spline_)(i / (double)pointsNrPath_ )(0);
83  double p_y = (*spline_)(i / (double)pointsNrPath_ )(1);
84  Eigen::SplineTraits< Eigen::Spline3d >::DerivativeType diff = spline_->derivatives(i / (double)pointsNrPath_, 1);
85  double v_x = diff(0,1);
86  double v_y = diff(1,1);
87 // double p_z = (*spline_)(i / (double)pointsNrPath_ )(2);
88 
89  Ogre::Quaternion rotation = Ogre::Quaternion ( Ogre::Radian( (*spline_)(i / (double)pointsNrPath_ )(2) + atan2(v_y, v_x) ), Ogre::Vector3::UNIT_Z );
90  Ogre::Quaternion rotation2 = Ogre::Quaternion ( Ogre::Radian( -Ogre::Math::PI/2.), Ogre::Vector3::UNIT_Y );
92  splinePtsXY_[i]->setColor ( colorPath_ );
93  splinePtsXY_[i]->setPosition ( Ogre::Vector3 ( p_x, p_y, 0 ) );
94  splinePtsXY_[i]->setOrientation ( rotation*rotation2 );
95  splinePtsXY_[i]->setScale ( Ogre::Vector3 ( scalePath_, scalePath_, scalePath_ ) );
96  }
97 
98  splinePtsTheta_.resize ( pointsNrOrient_ + 1 );
99 // if( !plotArrows_ ) { splinePtsTheta_.resize ( 0 ); return; }
100  for( size_t i = 0; i <= pointsNrOrient_; ++i) {
101  double p_x = (*spline_)(i / (double)pointsNrOrient_ )(0);
102  double p_y = (*spline_)(i / (double)pointsNrOrient_ )(1);
103  Eigen::SplineTraits< Eigen::Spline3d >::DerivativeType diff = spline_->derivatives(i / (double)pointsNrOrient_, 1);
104  double v_x = diff(0,1);
105  double v_y = diff(1,1);
106 // double p_z = (*spline_)(i / (double)pointsNrOrient_ )(2);
107 
108  Ogre::Quaternion rotation = Ogre::Quaternion ( Ogre::Radian( (*spline_)(i / (double)pointsNrOrient_ )(2) + atan2(v_y, v_x) ), Ogre::Vector3::UNIT_Z );
109  Ogre::Quaternion rotation2 = Ogre::Quaternion ( Ogre::Radian( -Ogre::Math::PI/2.), Ogre::Vector3::UNIT_Y );
111  splinePtsTheta_[i]->setColor ( colorOrient_ );
112  splinePtsTheta_[i]->setPosition ( Ogre::Vector3 ( p_x, p_y, 0 ) );
113  splinePtsTheta_[i]->setOrientation ( rotation*rotation2 );
114  splinePtsTheta_[i]->setScale ( Ogre::Vector3 ( scaleOrient_, scaleOrient_, scaleOrient_ ) );
115  }
116 }
117 
118 // Position is passed through to the SceneNode.
119 void SplineVisual::setFramePosition ( const Ogre::Vector3& position ) {
120  frame_node_->setPosition ( position );
121 }
122 
123 // Orientation is passed through to the SceneNode.
124 void SplineVisual::setFrameOrientation ( const Ogre::Quaternion& orientation ) {
125  frame_node_->setOrientation ( orientation );
126 }
127 
128 // Color is passed through to the Shape object.
129 void SplineVisual::setPathColor ( Ogre::ColourValue color ) {
130  colorPath_ = color;
131  for ( auto& splineXYi : splinePtsXY_ ) { splineXYi ->setColor ( colorPath_ ); }
132 }
133 
134 // Color is passed through to the Shape object.
135 void SplineVisual::setOrientColor ( Ogre::ColourValue color ) {
136  colorOrient_ = color;
137  for ( auto& splineThetai: splinePtsTheta_ ) { splineThetai->setColor ( colorOrient_ ); }
138 }
139 
140 // Shape type is passed through to the Shape object.
142  shape_type_ = shape_type;
143  for ( auto& splineXYi: splinePtsXY_ ) {
144  Ogre::Vector3 posOld = splineXYi->getPosition();
145  Ogre::Quaternion orientOld = splineXYi->getOrientation();
146  splineXYi.reset ( new rviz::Shape ( shape_type_, scene_manager_, frame_node_ ) );
147  splineXYi->setColor ( colorPath_ );
148  splineXYi->setPosition ( posOld );
149  splineXYi->setOrientation ( orientOld );
150  splineXYi->setScale ( Ogre::Vector3 ( scalePath_, scalePath_, scalePath_ ) );
151  }
152 }
153 
154 // Scale is passed through to the Shape object.
155 void SplineVisual::setPathScale ( float scale ) {
156  scalePath_ = scale;
157  for ( auto& splineXYi : splinePtsXY_ ) { splineXYi ->setScale ( Ogre::Vector3 ( scalePath_, scalePath_, scalePath_ ) ); }
158 }
159 
160 // Scale is passed through to the Shape object.
161 void SplineVisual::setOrientScale ( float scale ) {
162  scaleOrient_ = scale;
163  for ( auto& splineThetai: splinePtsTheta_ ) { splineThetai->setScale ( Ogre::Vector3 ( scaleOrient_, scaleOrient_, scaleOrient_ ) ); }
164 }
165 
166 
167 void SplineVisual::setPathPointsNr ( int pointsNr ) {
168  pointsNrPath_ = pointsNr;
169  if(!spline_){ return; }
170 
171  splinePtsXY_.resize ( pointsNrPath_ + 1 );
172  for( size_t i = 0; i <= pointsNrPath_; ++i) {
173  auto splineEval = spline_->derivatives(i / (double)pointsNrPath_, 1);
174  double p_x = splineEval(0,0), p_y = splineEval(1,0);
175  double v_x = splineEval(0,1), v_y = splineEval(1,1);
176 
177  Ogre::Quaternion rotation = Ogre::Quaternion ( Ogre::Radian( splineEval(2,0) + atan2(v_y, v_x) ), Ogre::Vector3::UNIT_Z );
178  Ogre::Quaternion rotation2 = Ogre::Quaternion ( Ogre::Radian( -Ogre::Math::PI/2.), Ogre::Vector3::UNIT_Y );
179 
181  splinePtsXY_[i]->setColor ( colorPath_ );
182  splinePtsXY_[i]->setPosition ( Ogre::Vector3 ( p_x, p_y, 0 ) );
183  splinePtsXY_[i]->setOrientation ( rotation*rotation2 );
184  splinePtsXY_[i]->setScale ( Ogre::Vector3 ( scalePath_, scalePath_, scalePath_ ) );
185  }
186 }
187 
188 void SplineVisual::setOrientPointsNr ( int pointsNr ) {
189  pointsNrOrient_ = pointsNr;
190  if(!spline_){ return; }
191 
192  splinePtsTheta_.resize ( pointsNrOrient_ + 1 );
193  for( size_t i = 0; i <= pointsNrOrient_; ++i) {
194  auto splineEval = spline_->derivatives(i / (double)pointsNrOrient_, 1);
195  double p_x = splineEval(0,0), p_y = splineEval(1,0);
196  double v_x = splineEval(0,1), v_y = splineEval(1,1);
197 
198  Ogre::Quaternion rotation = Ogre::Quaternion ( Ogre::Radian( splineEval(2,0) + atan2(v_y, v_x) ), Ogre::Vector3::UNIT_Z );
199  Ogre::Quaternion rotation2 = Ogre::Quaternion ( Ogre::Radian( -Ogre::Math::PI/2.), Ogre::Vector3::UNIT_Y );
200 
202  splinePtsTheta_[i]->setColor ( colorOrient_ );
203  splinePtsTheta_[i]->setPosition ( Ogre::Vector3 ( p_x, p_y, 0 ) );
204  splinePtsTheta_[i]->setOrientation ( rotation*rotation2 );
205  splinePtsTheta_[i]->setScale ( Ogre::Vector3 ( scaleOrient_, scaleOrient_, scaleOrient_ ) );
206  }
207 }
208 
209 
210 } // end namespace tuw_nav_rviz_plugin
211 
void setFrameOrientation(const Ogre::Quaternion &orientation)
void setOrientPointsNr(int pointsNr)
void setShape(rviz::Shape::Type shape_type)
void setPathColor(Ogre::ColourValue color)
void setMessage(const tuw_nav_msgs::Spline::ConstPtr &msg)
std::vector< boost::shared_ptr< rviz::Arrow > > splinePtsTheta_
Definition: SplineVisual.h:98
boost::shared_ptr< Eigen::Spline3d > spline_
Definition: SplineVisual.h:100
Ogre::SceneManager * scene_manager_
Definition: SplineVisual.h:108
void setOrientColor(Ogre::ColourValue color)
SplineVisual(Ogre::SceneManager *scene_manager, Ogre::SceneNode *parent_node)
std::vector< boost::shared_ptr< rviz::Shape > > splinePtsXY_
Definition: SplineVisual.h:97
void setFramePosition(const Ogre::Vector3 &position)


tuw_nav_rviz_plugin
Author(s):
autogenerated on Sun Aug 28 2016 03:53:55