00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #include "fixed_orientation_ortho_view_controller.h"
00031 #include "rviz/viewport_mouse_event.h"
00032 #include "rviz/visualization_manager.h"
00033
00034 #include <OGRE/OgreCamera.h>
00035 #include <OGRE/OgreSceneManager.h>
00036 #include <OGRE/OgreSceneNode.h>
00037 #include <OGRE/OgreVector3.h>
00038 #include <OGRE/OgreQuaternion.h>
00039 #include <OGRE/OgreViewport.h>
00040
00041 #include <ogre_tools/shape.h>
00042 #include <ogre_tools/orthographic.h>
00043
00044 #include <stdint.h>
00045 #include <sstream>
00046
00047 namespace rviz
00048 {
00049
00050 FixedOrientationOrthoViewController::FixedOrientationOrthoViewController(VisualizationManager* manager, const std::string& name)
00051 : ViewController(manager, name)
00052 , scale_(10.0f)
00053 , orientation_(Ogre::Quaternion::IDENTITY)
00054 {
00055 }
00056
00057 FixedOrientationOrthoViewController::~FixedOrientationOrthoViewController()
00058 {
00059 }
00060
00061 void FixedOrientationOrthoViewController::handleMouseEvent(ViewportMouseEvent& event)
00062 {
00063 bool moved = false;
00064
00065 if ( event.event.Dragging() )
00066 {
00067 int32_t diff_x = event.event.GetX() - event.last_x;
00068 int32_t diff_y = event.event.GetY() - event.last_y;
00069
00070 if ( event.event.LeftIsDown() )
00071 {
00072 camera_->roll( Ogre::Radian( -diff_x * 0.005 ) );
00073 orientation_ = camera_->getOrientation();
00074 }
00075 else if ( event.event.MiddleIsDown() )
00076 {
00077 move( -diff_x / scale_, diff_y / scale_, 0.0f );
00078 }
00079 else if ( event.event.RightIsDown() )
00080 {
00081 scale_ *= 1.0 - diff_y * 0.01;
00082 }
00083
00084 moved = true;
00085 }
00086
00087 if ( event.event.GetWheelRotation() != 0 )
00088 {
00089 int diff = event.event.GetWheelRotation();
00090 scale_ *= 1.0 - (-diff) * 0.001;
00091
00092 moved = true;
00093 }
00094
00095 if (moved)
00096 {
00097 manager_->queueRender();
00098 }
00099 }
00100
00101 void FixedOrientationOrthoViewController::setOrientation(const Ogre::Quaternion& orientation)
00102 {
00103 orientation_ = orientation;
00104 }
00105
00106 void FixedOrientationOrthoViewController::onActivate()
00107 {
00108 camera_->setProjectionType(Ogre::PT_ORTHOGRAPHIC);
00109 camera_->setFixedYawAxis(false);
00110
00111 reference_node_->attachObject(camera_);
00112 }
00113
00114 void FixedOrientationOrthoViewController::onDeactivate()
00115 {
00116 reference_node_->detachObject(camera_);
00117 camera_->setCustomProjectionMatrix(false);
00118 }
00119
00120 void FixedOrientationOrthoViewController::onUpdate(float dt, float ros_dt)
00121 {
00122 updateCamera();
00123 }
00124
00125 void FixedOrientationOrthoViewController::lookAt( const Ogre::Vector3& point )
00126 {
00127 Ogre::Vector3 reference_point = reference_node_->getPosition() - point;
00128 Ogre::Vector3 current_pos = camera_->getPosition();
00129 current_pos.x = reference_point.x;
00130 current_pos.z = reference_point.z;
00131
00132 camera_->setPosition(current_pos);
00133 }
00134
00135 void FixedOrientationOrthoViewController::onReferenceFrameChanged(const Ogre::Vector3& old_reference_position, const Ogre::Quaternion& old_reference_orientation)
00136 {
00137 lookAt(reference_node_->getPosition());
00138 }
00139
00140 void FixedOrientationOrthoViewController::updateCamera()
00141 {
00142 camera_->setOrientation(orientation_);
00143
00144 float width = camera_->getViewport()->getActualWidth();
00145 float height = camera_->getViewport()->getActualHeight();
00146
00147 Ogre::Matrix4 proj;
00148 ogre_tools::buildScaledOrthoMatrix( proj, -width / scale_ / 2, width / scale_ / 2, -height / scale_ / 2, height / scale_ / 2,
00149 camera_->getNearClipDistance(), camera_->getFarClipDistance() );
00150 camera_->setCustomProjectionMatrix(true, proj);
00151 }
00152
00153 void FixedOrientationOrthoViewController::move( float x, float y, float z )
00154 {
00155 camera_->moveRelative( Ogre::Vector3( x, y, z ) );
00156 }
00157
00158 void FixedOrientationOrthoViewController::fromString(const std::string& str)
00159 {
00160 std::istringstream iss(str);
00161
00162 iss >> scale_;
00163 iss.ignore();
00164
00165 Ogre::Vector3 vec;
00166 iss >> vec.x;
00167 iss.ignore();
00168 iss >> vec.y;
00169 iss.ignore();
00170 iss >> vec.z;
00171 iss.ignore();
00172 camera_->setPosition(vec);
00173
00174 Ogre::Quaternion quat;
00175 iss >> quat.x;
00176 iss.ignore();
00177 iss >> quat.y;
00178 iss.ignore();
00179 iss >> quat.z;
00180 iss.ignore();
00181 iss >> quat.w;
00182 iss.ignore();
00183 orientation_ = quat;
00184 }
00185
00186 std::string FixedOrientationOrthoViewController::toString()
00187 {
00188 std::ostringstream oss;
00189 oss << scale_ << " " << camera_->getPosition().x << " " << camera_->getPosition().y << " " << camera_->getPosition().z
00190 << " " << camera_->getOrientation().x << " " << camera_->getOrientation().y << " " << camera_->getOrientation().z << " " << camera_->getOrientation().w;
00191
00192 return oss.str();
00193 }
00194
00195
00196 }