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


tuw_spline_rviz_plugin
Author(s):
autogenerated on Mon Aug 1 2016 04:05:16