camera_following_camera.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2010, Willow Garage, Inc.
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions are met:
00007  *
00008  *     * Redistributions of source code must retain the above copyright
00009  *       notice, this list of conditions and the following disclaimer.
00010  *     * Redistributions in binary form must reproduce the above copyright
00011  *       notice, this list of conditions and the following disclaimer in the
00012  *       documentation and/or other materials provided with the distribution.
00013  *     * Neither the name of the Willow Garage, Inc. nor the names of its
00014  *       contributors may be used to endorse or promote products derived from
00015  *       this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00018  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00021  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00022  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00023  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00024  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00025  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00026  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00027  * POSSIBILITY OF SUCH DAMAGE.
00028  */
00029 
00030 #include <rve_common_transformers/camera_following_camera.h>
00031 #include <rve_transformer/transformer_manager.h>
00032 #include <rve_transformer/frame_manager.h>
00033 #include <rve_render_client/camera.h>
00034 #include <rve_render_client/camera_info_projection_factory.h>
00035 #include <rve_properties/property_node.h>
00036 
00037 #include <rve_msgs/MouseEvent.h>
00038 
00039 #include <rve_common/eigen_conversions.h>
00040 #include <geometry_msgs/TransformStamped.h>
00041 #include <ros/callback_queue.h>
00042 #include <geometry_msgs/Pose.h>
00043 
00044 #include <sys/time.h>
00045 
00046 namespace rve_common_transformers
00047 {
00048 
00049 CameraFollowingCamera::CameraFollowingCamera()
00050 : orientation_(Eigen::Quaternionf::Identity())
00051 , position_(0.0, 0.0, 0.0)
00052 , camera_projection_( new rve_render_client::CameraInfoProjectionFactory() )
00053 , camera_info_changed_( false )
00054 {
00055 }
00056 
00057 CameraFollowingCamera::~CameraFollowingCamera()
00058 {
00059 }
00060 
00061 void CameraFollowingCamera::onInit()
00062 {
00063   addSubscriber("camera_info", 1, &CameraFollowingCamera::cameraInfoCallback, this);
00064 }
00065 
00066 void CameraFollowingCamera::cameraInfoCallback(const sensor_msgs::CameraInfoConstPtr& camera_info)
00067 {
00068 //  boost::mutex::scoped_lock lock(caminfo_mutex_);
00069   camera_info_ = camera_info;
00070   camera_info_changed_ = true;
00071 }
00072 
00073 void CameraFollowingCamera::onUpdate()
00074 {
00075   updateCamera();
00076 }
00077 
00078 void CameraFollowingCamera::onCameraSet()
00079 {
00080   getCamera()->setPosition(rve_common::eigenToMsg(position_));
00081   getCamera()->setOrientation(rve_common::eigenToMsg(orientation_));
00082   getCamera()->setProjectionMatrixFactory( camera_projection_ );
00083 }
00084 
00085 void CameraFollowingCamera::updateCamera()
00086 {
00087   sensor_msgs::CameraInfo info;
00088   {
00089 //    boost::mutex::scoped_lock lock(caminfo_mutex_);
00090     if( !camera_info_changed_ || !camera_info_ )
00091     {
00092       return;
00093     }
00094     info = *camera_info_;
00095     camera_info_changed_ = false;
00096   }
00097 
00098   // Read the target frame relative to the fixed frame.
00099   geometry_msgs::TransformStamped target = getManager()->getFrameManager()->lookupTransform( info.header.frame_id, ros::Time());
00100 
00101   geometry_msgs::Vector3 p = target.transform.translation;
00102   position_ = Eigen::Vector3f(p.x, p.y, p.z);
00103 
00104   geometry_msgs::Quaternion q = target.transform.rotation;
00105   orientation_ = Eigen::Quaternionf(q.w, q.x, q.y, q.z);
00106 
00107   // convert vision (Z-forward, X-right) frame to rve scene frame (Z-up, X-forward)
00108   orientation_ = orientation_ *
00109     Eigen::AngleAxisf(-M_PI/2, Eigen::Vector3f::UnitZ()) *
00110     Eigen::AngleAxisf(-M_PI/2, Eigen::Vector3f::UnitY()) *
00111     Eigen::AngleAxisf(M_PI, Eigen::Vector3f::UnitX());
00112   
00113   float width = info.width;
00114   float height = info.height;
00115 
00116   // If the image width is 0 due to a malformed caminfo, try to grab the width from the image.
00117   if( width == 0 )
00118   {
00119 // Not actually listening for images just yet...
00120 //    width = texture_.getWidth();
00121   }
00122 
00123   if( height == 0 )
00124   {
00125 // Not actually listening for images just yet...
00126 //    height = texture_.getHeight();
00127   }
00128 
00129   if( height == 0 || width == 0 )
00130   {
00131     ROS_ERROR( "CameraInfo: bad size: width = %f, height = %f.", width, height );
00132     return;
00133   }
00134 
00135   double fx = info.P[0];
00136   double fy = info.P[5];
00137 
00138   // Add the camera's translation relative to the left camera (from P[3]);
00139   double tx = -1 * (info.P[3] / fx);
00140   Eigen::Vector3f right = orientation_ * Eigen::Vector3f::UnitX();
00141   position_ = position_ + (right * tx);
00142 
00143   double ty = -1 * (info.P[7] / fy);
00144   Eigen::Vector3f down = orientation_ * Eigen::Vector3f::UnitY();
00145   position_ = position_ + (down * ty);
00146 
00147   getCamera()->setPosition( rve_common::eigenToMsg( position_ ));
00148   getCamera()->setOrientation( rve_common::eigenToMsg( orientation_ ));
00149 
00150   double cx = info.P[2];
00151   double cy = info.P[6];
00152   camera_projection_->setCameraInfo( cx, cy, fx, fy, width, height );
00153 }
00154 
00155 } // namespace rve_common_transformers


rve_common_transformers
Author(s): Josh Faust
autogenerated on Wed Dec 11 2013 14:31:58