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_render_client/camera.h"
00031 #include "rve_render_client/scene.h"
00032 #include "rve_render_client/perspective_projection_factory.h"
00033 
00034 #include <rve_interfaces/Camera.h>
00035 #include <rve_common/eigen_conversions.h>
00036 
00037 #include <boost/bind.hpp>
00038 
00039 #include <Eigen/Geometry>
00040 
00041 using namespace rve_common;
00042 
00043 namespace rve_render_client
00044 {
00045 
00046 CameraPtr createCamera(Scene* scene)
00047 {
00048   CameraPtr cam(new Camera, destroySceneObject);
00049   scene->addObject(cam.get());
00050   return cam;
00051 }
00052 
00053 Camera::Camera()
00054   : scene_(0)
00055 {
00056   orientation_.w = 1.0;
00057   proxy_index_ = addProxy("camera");
00058   setProjectionMatrixFactory( ProjectionMatrixFactoryPtr( new PerspectiveProjectionFactory() ));
00059 }
00060 
00061 Camera::~Camera()
00062 {
00063 
00064 }
00065 
00066 void Camera::doCreate(ContextInfo& context)
00067 {
00068   ROS_ASSERT(!scene_ || context.scene == scene_);
00069   scene_ = context.scene;
00070 
00071   rve_interfaces::CameraProxy* proxy = getProxy(context);
00072   const rve_common::UUID& scene_id = getSceneID(context.scene);
00073   proxy->create(scene_id, getID());
00074   proxy->setPosition(scene_id, getID(), position_);
00075   proxy->setOrientation(scene_id, getID(), orientation_);
00076   proxy->setProjectionMatrix(scene_id, getID(), projection_matrix_);
00077 }
00078 
00079 void Camera::doDestroy(ContextInfo& context)
00080 {
00081   rve_interfaces::CameraProxy* proxy = getProxy(context);
00082   proxy->destroyAsync(getSceneID(context.scene), getID());
00083 
00084   if (getContexts().empty())
00085   {
00086     scene_ = 0;
00087   }
00088 }
00089 
00090 rve_interfaces::CameraProxy* Camera::getProxy(const ContextInfo& context)
00091 {
00092   return SceneObject::getProxy<rve_interfaces::CameraProxy>(context, proxy_index_);
00093 }
00094 
00095 #define CAMERA_MULTIPLEX(func, ...) \
00096     multiplex<rve_interfaces::CameraProxy>(boost::bind(&rve_interfaces::CameraProxy::func, _1, _2, getID(), __VA_ARGS__), proxy_index_);
00097 
00098 void Camera::setPosition(const rve_msgs::Vector3& pos)
00099 {
00100   position_ = pos;
00101   CAMERA_MULTIPLEX(setPositionAsync, pos);
00102 }
00103 
00104 void Camera::setOrientation(const rve_msgs::Quaternion& orient)
00105 {
00106   orientation_ = orient;
00107   CAMERA_MULTIPLEX(setOrientationAsync, orient);
00108 }
00109 
00110 void Camera::setDirection(const rve_msgs::Vector3& d)
00111 {
00112   Eigen::Vector3f dir(msgToEigen(d));
00113   if (dir.isZero())
00114   {
00115     return;
00116   }
00117 
00118   Eigen::Quaternionf orient;
00119   orient.setFromTwoVectors(Eigen::Vector3f::UnitX(), dir);
00120   setOrientation(eigenToMsg(orient));
00121 }
00122 
00123 void Camera::lookAt(const rve_msgs::Vector3& point)
00124 {
00125   Eigen::Vector3f dir = msgToEigen(point) - msgToEigen(position_);
00126   setDirection(eigenToMsg(dir));
00127 }
00128 
00129 void Camera::move(const rve_msgs::Vector3& v)
00130 {
00131   Eigen::Vector3f vec(msgToEigen(v));
00132   Eigen::Vector3f pos(msgToEigen(position_));
00133 
00134   pos = pos + vec;
00135   setPosition(eigenToMsg(pos));
00136 }
00137 
00138 void Camera::moveRelative(const rve_msgs::Vector3& v)
00139 {
00140   Eigen::Vector3f vec(msgToEigen(v));
00141   Eigen::Vector3f pos(msgToEigen(position_));
00142   Eigen::Quaternionf orient(msgToEigen(orientation_));
00143 
00144   pos = pos + (orient * vec);
00145   setPosition(eigenToMsg(pos));
00146 }
00147 
00148 void Camera::rotate(const rve_msgs::Quaternion& q)
00149 {
00150   // Normalise the quat to avoid cumulative problems with precision
00151   Eigen::Quaternionf qnorm(msgToEigen(q));
00152   qnorm.normalize();
00153   Eigen::Quaternionf orient = msgToEigen(orientation_) * qnorm;
00154   setOrientation(eigenToMsg(orient));
00155 }
00156 
00157 void Camera::setTransform(const rve_msgs::Vector3& pos, const rve_msgs::Quaternion& orient)
00158 {
00159   setPosition(pos);
00160   setOrientation(orient);
00161 }
00162 
00163 void Camera::setProjectionMatrix(const rve_msgs::Matrix4& matrix)
00164 {
00165   projection_matrix_ = matrix;
00166   CAMERA_MULTIPLEX(setProjectionMatrixAsync, matrix);
00167 }
00168 
00169 void Camera::setProjectionMatrixFactory(const ProjectionMatrixFactoryPtr& factory)
00170 {
00171   projection_matrix_factory_ = factory;
00172   if( projection_matrix_factory_ )
00173   {
00174     projection_matrix_factory_->setAspectRatio( aspect_ratio_ );
00175     projection_matrix_factory_->setCamera( this );
00176   }
00177 }
00178 
00179 void Camera::setAspectRatio( float width_over_height )
00180 {
00181   aspect_ratio_ = width_over_height;
00182   if( projection_matrix_factory_ )
00183   {
00184     projection_matrix_factory_->setAspectRatio( aspect_ratio_ );
00185   }
00186 }
00187 
00188 }


rve_render_client
Author(s): Josh Faust
autogenerated on Wed Dec 11 2013 14:31:32