49 #include "view_controller_msgs/CameraPlacement.h"
51 #include <OGRE/OgreViewport.h>
52 #include <OGRE/OgreQuaternion.h>
53 #include <OGRE/OgreVector3.h>
54 #include <OGRE/OgreSceneNode.h>
55 #include <OGRE/OgreSceneManager.h>
56 #include <OGRE/OgreCamera.h>
57 #include <OGRE/OgreRenderWindow.h>
61 using namespace view_controller_msgs;
76 static inline Ogre::Vector3
vectorFromMsg(
const geometry_msgs::Point &m) {
return Ogre::Vector3(m.x, m.y, m.z); }
77 static inline Ogre::Vector3
vectorFromMsg(
const geometry_msgs::Vector3 &m) {
return Ogre::Vector3(m.x, m.y, m.z); }
80 geometry_msgs::Point m;
81 m.x = o.x; m.y = o.y; m.z = o.z;
84 static inline void pointOgreToMsg(
const Ogre::Vector3 &o, geometry_msgs::Point &m) { m.x = o.x; m.y = o.y; m.z = o.z; }
88 geometry_msgs::Vector3 m;
89 m.x = o.x; m.y = o.y; m.z = o.z;
92 static inline void vectorOgreToMsg(
const Ogre::Vector3 &o, geometry_msgs::Vector3 &m) { m.x = o.x; m.y = o.y; m.z = o.z; }
99 , cam_movements_buffer_(100)
102 , render_frame_by_frame_(false)
104 , rendered_frames_counter_(0)
105 , pause_animation_duration_(0.0)
110 "Enables mouse control of the camera.",
113 "Select the style of mouse interaction.",
120 "If enabled, the camera is not allowed to roll side-to-side.",
124 "TF frame the camera is attached to.",
127 "Position of the camera.",
this );
129 "Position of the focus/orbit point.",
this );
131 "The vector which maps to \"up\" in the camera image plane.",
this );
133 "The distance between the camera position and the focus point.",
137 "The default time to use for camera transitions.",
140 QString::fromStdString(ros::message_traits::datatype<view_controller_msgs::CameraPlacement>() ),
141 "Topic for CameraPlacement messages",
this, SLOT(
updateTopics()));
144 QString::fromStdString(
145 ros::message_traits::datatype<view_controller_msgs::CameraTrajectory>()),
146 "Topic for CameraTrajectory messages",
this,
153 "If enabled, publishes images of what the user sees in the visualization window during an animation.",
204 camera_->setProjectionType( Ogre::PT_PERSPECTIVE );
285 camera_->setFixedYawAxis(
false);
302 Ogre::Vector3 new_reference_position;
303 Ogre::Quaternion new_reference_orientation;
307 new_reference_position, new_reference_orientation ))
367 setStatus(
"<b>Mouse interaction is disabled. You can enable it by checking the \"Mouse Enabled\" check-box in the Views panel." );
370 else if ( event.
shift() )
372 setStatus(
"TODO: Fix me! <b>Left-Click:</b> Move X/Y. <b>Right-Click:</b>: Move Z." );
376 setStatus(
"TODO: Fix me! <b>Left-Click:</b> Move X/Y. <b>Right-Click:</b>: Move Z." );
380 setStatus(
"TODO: Fix me! <b>Left-Click:</b> Rotate. <b>Middle-Click:</b> Move X/Y. <b>Right-Click:</b>: Zoom. <b>Shift</b>: More options." );
388 if( event.
type == QEvent::MouseButtonPress )
395 else if( event.
type == QEvent::MouseButtonRelease )
403 diff_x =
event.x -
event.last_x;
404 diff_y =
event.y -
event.last_y;
420 float fovY =
camera_->getFOVy().valueRadians();
421 float fovX = 2.0f * atan( tan( fovY / 2.0
f ) *
camera_->getAspectRatio() );
423 int width =
camera_->getViewport()->getActualWidth();
424 int height =
camera_->getViewport()->getActualHeight();
427 ((
float)diff_y / (
float)height) * distance * tan( fovY / 2.0
f ) * 2.0
f,
435 else if( event.
right() )
445 move_eye( 0, 0, diff_y * 0.01 * distance );
455 int diff =
event.wheel_delta;
467 move_eye( 0, 0, -diff * 0.001 * distance );
487 geometry_msgs::Pose cam_pose;
488 cam_pose.position.x =
camera_->getPosition().x;
489 cam_pose.position.y =
camera_->getPosition().y;
490 cam_pose.position.z =
camera_->getPosition().z;
491 cam_pose.orientation.w =
camera_->getOrientation().w;
492 cam_pose.orientation.x =
camera_->getOrientation().x;
493 cam_pose.orientation.y =
camera_->getOrientation().y;
494 cam_pose.orientation.z =
camera_->getOrientation().z;
513 Ogre::Vector3 direction = source_camera->getOrientation() * Ogre::Vector3::NEGATIVE_UNIT_Z;
533 Ogre::Camera* source_camera = source_view->
getCamera();
534 Ogre::Vector3 position = source_camera->getPosition();
535 Ogre::Quaternion orientation = source_camera->getOrientation();
537 if( source_view->
getClassId() ==
"rviz/Orbit" )
571 const Ogre::Vector3 &focus,
572 const Ogre::Vector3 &up,
574 uint8_t interpolation_speed)
576 if(transition_duration.
toSec() < 0.0)
580 if(transition_duration.
isZero())
592 interpolation_speed)));
612 std_msgs::Bool finished_animation;
613 finished_animation.data = 1;
621 CameraPlacement cp = *cp_ptr;
626 if(cp.mouse_interaction_mode != cp.NO_CHANGE)
628 std::string name =
"";
629 if(cp.mouse_interaction_mode == cp.ORBIT) name =
MODE_ORBIT;
630 else if(cp.mouse_interaction_mode == cp.FPS) name =
MODE_FPS;
634 if(cp.target_frame !=
"")
640 if(cp.time_from_start.toSec() >= 0)
658 view_controller_msgs::CameraTrajectory ct = *ct_ptr;
660 if(ct.trajectory.empty())
666 if(ct.mouse_interaction_mode != view_controller_msgs::CameraTrajectory::NO_CHANGE)
668 std::string name =
"";
669 if(ct.mouse_interaction_mode == view_controller_msgs::CameraTrajectory::ORBIT)
671 else if(ct.mouse_interaction_mode == view_controller_msgs::CameraTrajectory::FPS)
676 if(ct.render_frame_by_frame > 0)
679 target_fps_ =
static_cast<int>(ct.frames_per_second);
683 for(
auto& cam_movement : ct.trajectory)
685 if(cam_movement.transition_duration.toSec() >= 0.0)
687 if(ct.target_frame !=
"")
698 Ogre::Vector3 focus =
vectorFromMsg(cam_movement.focus.point);
700 beginNewTransition(eye, focus, up, cam_movement.transition_duration, cam_movement.interpolation_speed);
704 ROS_WARN(
"Transition duration of camera movement is below zero. Skipping that movement.");
710 geometry_msgs::PointStamped& focus,
711 geometry_msgs::Vector3Stamped& up)
713 Ogre::Vector3 position_fixed_eye, position_fixed_focus, position_fixed_up;
714 Ogre::Quaternion rotation_fixed_eye, rotation_fixed_focus, rotation_fixed_up;
781 bool finished_current_movement =
false;
782 if(relative_progress_in_time >= 1.0)
784 relative_progress_in_time = 1.0;
785 finished_current_movement =
true;
789 goal->interpolation_speed);
791 Ogre::Vector3 new_position =
start->eye + relative_progress_in_space * (goal->eye -
start->eye);
792 Ogre::Vector3 new_focus =
start->focus + relative_progress_in_space * (goal->focus -
start->focus);
793 Ogre::Vector3 new_up =
start->up + relative_progress_in_space * (goal->up -
start->up);
811 if(finished_current_movement)
838 double relative_progress_in_time = 0.0;
847 relative_progress_in_time = duration_from_start.
toSec() / transition_duration.
toSec();
849 return relative_progress_in_time;
853 uint8_t interpolation_speed)
855 switch(interpolation_speed)
857 case view_controller_msgs::CameraMovement::RISING:
858 return 1.f -
static_cast<float>(cos(relative_progress_in_time * M_PI_2));
859 case view_controller_msgs::CameraMovement::DECLINING:
860 return static_cast<float>(-cos(relative_progress_in_time * M_PI_2 + M_PI_2));
861 case view_controller_msgs::CameraMovement::FULL:
862 return static_cast<float>(relative_progress_in_time);
863 case view_controller_msgs::CameraMovement::WAVE:
865 return 0.5f * (1.f -
static_cast<float>(cos(relative_progress_in_time * M_PI)));
873 std::shared_ptr<Ogre::PixelBox> pixel_box = std::make_shared<Ogre::PixelBox>();
876 sensor_msgs::ImagePtr image_msg = sensor_msgs::ImagePtr(
new sensor_msgs::Image());
881 delete[] (
unsigned char*)pixel_box->data;
891 const Ogre::PixelFormat pixel_format = Ogre::PF_BYTE_BGR;
892 const auto bytes_per_pixel = Ogre::PixelUtil::getNumElemBytes(pixel_format);
893 auto image_data =
new unsigned char[image_width * image_height * bytes_per_pixel];
894 Ogre::Box image_extents(0, 0, image_width, image_height);
895 pixel_box = std::make_shared<Ogre::PixelBox>(image_extents, pixel_format, image_data);
897 Ogre::RenderTarget::FB_AUTO);
901 sensor_msgs::ImagePtr output_image)
903 const auto bytes_per_pixel = Ogre::PixelUtil::getNumElemBytes(input_image->format);
904 const auto image_height = input_image->getHeight();
905 const auto image_width = input_image->getWidth();
909 output_image->height = image_height;
910 output_image->width = image_width;
912 output_image->is_bigendian =
false;
913 output_image->step =
static_cast<unsigned int>(image_width * bytes_per_pixel);
914 size_t size = image_width * image_height * bytes_per_pixel;
915 output_image->data.resize(size);
916 memcpy((
char*)(&output_image->data[0]), input_image->data, size);
936 Ogre::Quaternion old_camera_orientation =
camera_->getOrientation();
937 Ogre::Radian old_pitch = old_camera_orientation.getPitch(
false);
941 Ogre::Quaternion yaw_quat, pitch_quat, roll_quat;
942 yaw_quat.FromAngleAxis( Ogre::Radian( yaw ), Ogre::Vector3::UNIT_Y );
943 pitch_quat.FromAngleAxis( Ogre::Radian( pitch ), Ogre::Vector3::UNIT_X );
944 roll_quat.FromAngleAxis( Ogre::Radian( roll ), Ogre::Vector3::UNIT_Z );
945 Ogre::Quaternion orientation_change = yaw_quat * pitch_quat * roll_quat;
946 Ogre::Quaternion new_camera_orientation = old_camera_orientation * orientation_change;
947 Ogre::Radian new_pitch = new_camera_orientation.getPitch(
false);
952 orientation_change = yaw_quat * roll_quat;
953 new_camera_orientation = old_camera_orientation * orientation_change;
960 camera_->setOrientation( new_camera_orientation );
966 camera_->setPosition(new_eye_position);
978 return camera_->getOrientation();
983 Ogre::Vector3 translate( x, y, z );
990 Ogre::Vector3 translate( x, y, z );