pose_with_covariance_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 <ros/ros.h>
31 
32 #include <OGRE/OgreVector3.h>
33 #include <OGRE/OgreMatrix3.h>
34 #include <OGRE/OgreSceneNode.h>
35 #include <OGRE/OgreSceneManager.h>
36 
38 
39 namespace tuw_pose_rviz_plugin {
40 
41 PoseWithCovarianceVisual::PoseWithCovarianceVisual ( Ogre::SceneManager* scene_manager, Ogre::SceneNode* parent_node ) {
42  scene_manager_ = scene_manager;
43 
44  // Ogre::SceneNode s form a tree, with each node storing the
45  // transform (position and orientation) of itself relative to its
46  // parent. Ogre does the math of combining those transforms when it
47  // is time to render.
48  //
49  // Here we create a node to store the pose of the MarkerDetection's header frame
50  // relative to the RViz fixed frame.
51  frame_node_ = parent_node->createChildSceneNode();
52 
53  // We create the visual objects within the frame node so that we can
54  // set thier position and direction relative to their header frame.
55  pose_.reset ( new rviz::Arrow( scene_manager_, frame_node_ ) );
57 }
58 
60  // Destroy the frame node since we don't need it anymore.
61  scene_manager_->destroySceneNode ( frame_node_ );
62 }
63 
64 void PoseWithCovarianceVisual::setMessage ( const geometry_msgs::PoseWithCovarianceStamped::ConstPtr& msg ) {
65  Ogre::Vector3 position = Ogre::Vector3 ( msg->pose.pose.position.x, msg->pose.pose.position.y, msg->pose.pose.position.z );
66  Ogre::Quaternion orientation = Ogre::Quaternion ( msg->pose.pose.orientation.x, msg->pose.pose.orientation.y, msg->pose.pose.orientation.z, msg->pose.pose.orientation.w );
67 
68  // Arrow points in -Z direction, so rotate the orientation before display.
69  // TODO: is it safe to change Arrow to point in +X direction?
70  Ogre::Quaternion rotation1 = Ogre::Quaternion ( Ogre::Degree( -90 ), Ogre::Vector3::UNIT_Y );
71  Ogre::Quaternion rotation2 = Ogre::Quaternion ( Ogre::Degree( -180 ), Ogre::Vector3::UNIT_X );
72  orientation = rotation2 * rotation1 * orientation;
73 
74  pose_->setPosition( position );
75  pose_->setOrientation( orientation );
76 
77  Ogre::Matrix3 C = Ogre::Matrix3 ( msg->pose.covariance[6*0 + 0], msg->pose.covariance[6*0 + 1], msg->pose.covariance[6*0 + 5],
78  msg->pose.covariance[6*1 + 0], msg->pose.covariance[6*1 + 1], msg->pose.covariance[6*1 + 5],
79  msg->pose.covariance[6*5 + 0], msg->pose.covariance[6*5 + 1], msg->pose.covariance[6*5 + 5] );
80  Ogre::Real eigenvalues[3];
81  Ogre::Vector3 eigenvectors[3];
82  C.EigenSolveSymmetric(eigenvalues, eigenvectors);
83  if ( eigenvalues[0] < 0 ) {
84  ROS_WARN ( "[PoseWithCovarianceVisual setMessage] eigenvalue[0]: %f < 0 ", eigenvalues[0] );
85  eigenvalues[0] = 0;
86  }
87  if ( eigenvalues[1] < 0 ) {
88  ROS_WARN ( "[PoseWithCovarianceVisual setMessage] eigenvalue[1]: %f < 0 ", eigenvalues[1] );
89  eigenvalues[1] = 0;
90  }
91  if ( eigenvalues[2] < 0 ) {
92  ROS_WARN ( "[PoseWithCovarianceVisual setMessage] eigenvalue[2]: %f < 0 ", eigenvalues[2] );
93  eigenvalues[2] = 0;
94  }
95 
96  variance_->setColor ( color_variance_ );
97  variance_->setPosition ( position );
98  variance_->setOrientation ( Ogre::Quaternion ( eigenvectors[0], eigenvectors[1], eigenvectors[2] ) );
99  variance_->setScale ( Ogre::Vector3 ( 2*sqrt(eigenvalues[0]), 2*sqrt(eigenvalues[1]), 2*sqrt(eigenvalues[2]) ) );
100 }
101 
102 // Position is passed through to the SceneNode.
103 void PoseWithCovarianceVisual::setFramePosition ( const Ogre::Vector3& position ) {
104  frame_node_->setPosition ( position );
105 }
106 
107 // Orientation is passed through to the SceneNode.
108 void PoseWithCovarianceVisual::setFrameOrientation ( const Ogre::Quaternion& orientation ) {
109  frame_node_->setOrientation ( orientation );
110 }
111 
112 // Scale is passed through to the pose Shape object.
114  pose_->setScale ( Ogre::Vector3 ( scale, scale, scale ) );
115  scale_pose_ = scale;
116 }
117 
118 // Color is passed through to the pose Shape object.
119 void PoseWithCovarianceVisual::setColorPose ( Ogre::ColourValue color ) {
120  pose_->setColor ( color );
121  color_pose_ = color;
122 }
123 
124 // Color is passed through to the variance Shape object.
125 void PoseWithCovarianceVisual::setColorVariance ( Ogre::ColourValue color ) {
126  variance_->setColor ( color );
127  color_variance_ = color;
128 }
129 
130 } // end namespace marker_rviz_plugin
PoseWithCovarianceVisual(Ogre::SceneManager *scene_manager, Ogre::SceneNode *parent_node)
void setMessage(const geometry_msgs::PoseWithCovarianceStamped::ConstPtr &msg)
#define ROS_WARN(...)
void setFrameOrientation(const Ogre::Quaternion &orientation)


tuw_pose_rviz_plugin
Author(s):
autogenerated on Mon Aug 1 2016 04:05:13