1 /*M/////////////////////////////////////////////////////////////////////////////////////// 2 // 3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. 4 // 5 // By downloading, copying, installing or using the software you agree to this license. 6 // If you do not agree to this license, do not download, install, 7 // copy or use the software. 8 // 9 // 10 // License Agreement 11 // For Open Source Computer Vision Library 12 // 13 // Copyright (C) 2013, OpenCV Foundation, all rights reserved. 14 // Third party copyrights are property of their respective owners. 15 // 16 // Redistribution and use in source and binary forms, with or without modification, 17 // are permitted provided that the following conditions are met: 18 // 19 // * Redistribution's of source code must retain the above copyright notice, 20 // this list of conditions and the following disclaimer. 21 // 22 // * Redistribution's in binary form must reproduce the above copyright notice, 23 // this list of conditions and the following disclaimer in the documentation 24 // and/or other materials provided with the distribution. 25 // 26 // * The name of the copyright holders may not be used to endorse or promote products 27 // derived from this software without specific prior written permission. 28 // 29 // This software is provided by the copyright holders and contributors "as is" and 30 // any express or implied warranties, including, but not limited to, the implied 31 // warranties of merchantability and fitness for a particular purpose are disclaimed. 32 // In no event shall the Intel Corporation or contributors be liable for any direct, 33 // indirect, incidental, special, exemplary, or consequential damages 34 // (including, but not limited to, procurement of substitute goods or services; 35 // loss of use, data, or profits; or business interruption) however caused 36 // and on any theory of liability, whether in contract, strict liability, 37 // or tort (including negligence or otherwise) arising in any way out of 38 // the use of this software, even if advised of the possibility of such damage. 39 // 40 // Authors: 41 // * Ozan Tonkal, ozantonkal (at) gmail.com 42 // * Anatoly Baksheev, Itseez Inc. myname.mysurname <> mycompany.com 43 // 44 //M*/ 45 46 #include "precomp.hpp" 47 48 49 ///////////////////////////////////////////////////////////////////////////////////////////// 50 cv::viz::Viz3d::VizImpl::VizImpl(const String &name) : spin_once_state_(false), 51 window_position_(Vec2i(std::numeric_limits<int>::min())), widget_actor_map_(new WidgetActorMap) 52 { 53 renderer_ = vtkSmartPointer<vtkRenderer>::New(); 54 window_name_ = VizStorage::generateWindowName(name); 55 56 // Create render window 57 window_ = vtkSmartPointer<vtkRenderWindow>::New(); 58 cv::Vec2i window_size = cv::Vec2i(window_->GetScreenSize()) / 2; 59 window_->SetSize(window_size.val); 60 window_->AddRenderer(renderer_); 61 62 // Create the interactor style 63 style_ = vtkSmartPointer<vtkVizInteractorStyle>::New(); 64 style_->setWidgetActorMap(widget_actor_map_); 65 style_->UseTimersOn(); 66 67 timer_callback_ = vtkSmartPointer<TimerCallback>::New(); 68 exit_callback_ = vtkSmartPointer<ExitCallback>::New(); 69 exit_callback_->viz = this; 70 71 setBackgroundMeshLab(); 72 } 73 74 cv::viz::Viz3d::VizImpl::~VizImpl() { close(); } 75 76 ///////////////////////////////////////////////////////////////////////////////////////////// 77 void cv::viz::Viz3d::VizImpl::TimerCallback::Execute(vtkObject* caller, unsigned long event_id, void* cookie) 78 { 79 if (event_id == vtkCommand::TimerEvent && timer_id == *reinterpret_cast<int*>(cookie)) 80 { 81 vtkSmartPointer<vtkRenderWindowInteractor> interactor = vtkRenderWindowInteractor::SafeDownCast(caller); 82 interactor->TerminateApp(); 83 } 84 } 85 86 void cv::viz::Viz3d::VizImpl::ExitCallback::Execute(vtkObject*, unsigned long event_id, void*) 87 { 88 if (event_id == vtkCommand::ExitEvent && viz->interactor_) 89 { 90 viz->interactor_->TerminateApp(); 91 viz->interactor_ = 0; 92 } 93 } 94 95 ///////////////////////////////////////////////////////////////////////////////////////////// 96 97 bool cv::viz::Viz3d::VizImpl::wasStopped() const 98 { 99 bool stopped = spin_once_state_ ? interactor_ == 0 : false; 100 spin_once_state_ &= !stopped; 101 return stopped; 102 } 103 104 void cv::viz::Viz3d::VizImpl::close() 105 { 106 if (!interactor_) 107 return; 108 interactor_->GetRenderWindow()->Finalize(); 109 interactor_->TerminateApp(); // This tends to close the window... 110 interactor_ = 0; 111 } 112 113 void cv::viz::Viz3d::VizImpl::recreateRenderWindow() 114 { 115 #if !defined _MSC_VER && !defined __APPLE__ 116 //recreating is workaround for Ubuntu -- a crash in x-server 117 Vec2i window_size(window_->GetSize()); 118 int fullscreen = window_->GetFullScreen(); 119 120 window_->Finalize(); 121 window_ = vtkSmartPointer<vtkRenderWindow>::New(); 122 if (window_position_[0] != std::numeric_limits<int>::min()) //also workaround 123 window_->SetPosition(window_position_.val); 124 125 window_->SetSize(window_size.val); 126 window_->SetFullScreen(fullscreen); 127 window_->AddRenderer(renderer_); 128 #endif 129 } 130 131 ///////////////////////////////////////////////////////////////////////////////////////////// 132 void cv::viz::Viz3d::VizImpl::spin() 133 { 134 recreateRenderWindow(); 135 #if defined __APPLE__ 136 interactor_ = vtkCocoaRenderWindowInteractorNew(); 137 #else 138 interactor_ = vtkSmartPointer<vtkRenderWindowInteractor>::New(); 139 #endif 140 interactor_->SetRenderWindow(window_); 141 interactor_->SetInteractorStyle(style_); 142 window_->AlphaBitPlanesOff(); 143 window_->PointSmoothingOff(); 144 window_->LineSmoothingOff(); 145 window_->PolygonSmoothingOff(); 146 window_->SwapBuffersOn(); 147 window_->SetStereoTypeToAnaglyph(); 148 window_->Render(); 149 window_->SetWindowName(window_name_.c_str()); 150 interactor_->Start(); 151 interactor_ = 0; 152 } 153 154 ///////////////////////////////////////////////////////////////////////////////////////////// 155 void cv::viz::Viz3d::VizImpl::spinOnce(int time, bool force_redraw) 156 { 157 if (interactor_ == 0) 158 { 159 spin_once_state_ = true; 160 recreateRenderWindow(); 161 #if defined __APPLE__ 162 interactor_ = vtkCocoaRenderWindowInteractorNew(); 163 #else 164 interactor_ = vtkSmartPointer<vtkRenderWindowInteractor>::New(); 165 #endif 166 interactor_->SetRenderWindow(window_); 167 interactor_->SetInteractorStyle(style_); 168 interactor_->AddObserver(vtkCommand::TimerEvent, timer_callback_); 169 interactor_->AddObserver(vtkCommand::ExitEvent, exit_callback_); 170 window_->AlphaBitPlanesOff(); 171 window_->PointSmoothingOff(); 172 window_->LineSmoothingOff(); 173 window_->PolygonSmoothingOff(); 174 window_->SwapBuffersOn(); 175 window_->SetStereoTypeToAnaglyph(); 176 window_->Render(); 177 window_->SetWindowName(window_name_.c_str()); 178 } 179 180 vtkSmartPointer<vtkRenderWindowInteractor> local = interactor_; 181 182 if (force_redraw) 183 local->Render(); 184 185 timer_callback_->timer_id = local->CreateRepeatingTimer(std::max(1, time)); 186 local->Start(); 187 local->DestroyTimer(timer_callback_->timer_id); 188 } 189 190 ///////////////////////////////////////////////////////////////////////////////////////////// 191 void cv::viz::Viz3d::VizImpl::showWidget(const String &id, const Widget &widget, const Affine3d &pose) 192 { 193 WidgetActorMap::iterator wam_itr = widget_actor_map_->find(id); 194 bool exists = wam_itr != widget_actor_map_->end(); 195 if (exists) 196 { 197 // Remove it if it exists and add it again 198 removeActorFromRenderer(wam_itr->second); 199 } 200 // Get the actor and set the user matrix 201 vtkProp3D *actor = vtkProp3D::SafeDownCast(WidgetAccessor::getProp(widget)); 202 if (actor) 203 { 204 // If the actor is 3D, apply pose 205 vtkSmartPointer<vtkMatrix4x4> matrix = vtkmatrix(pose.matrix); 206 actor->SetUserMatrix(matrix); 207 actor->Modified(); 208 } 209 // If the actor is a vtkFollower, then it should always face the camera 210 vtkFollower *follower = vtkFollower::SafeDownCast(actor); 211 if (follower) 212 { 213 follower->SetCamera(renderer_->GetActiveCamera()); 214 } 215 216 renderer_->AddActor(WidgetAccessor::getProp(widget)); 217 (*widget_actor_map_)[id] = WidgetAccessor::getProp(widget); 218 } 219 220 ///////////////////////////////////////////////////////////////////////////////////////////// 221 void cv::viz::Viz3d::VizImpl::removeWidget(const String &id) 222 { 223 WidgetActorMap::iterator wam_itr = widget_actor_map_->find(id); 224 bool exists = wam_itr != widget_actor_map_->end(); 225 CV_Assert("Widget does not exist." && exists); 226 CV_Assert("Widget could not be removed." && removeActorFromRenderer(wam_itr->second)); 227 widget_actor_map_->erase(wam_itr); 228 } 229 230 ///////////////////////////////////////////////////////////////////////////////////////////// 231 cv::viz::Widget cv::viz::Viz3d::VizImpl::getWidget(const String &id) const 232 { 233 WidgetActorMap::const_iterator wam_itr = widget_actor_map_->find(id); 234 bool exists = wam_itr != widget_actor_map_->end(); 235 CV_Assert("Widget does not exist." && exists); 236 237 Widget widget; 238 WidgetAccessor::setProp(widget, wam_itr->second); 239 return widget; 240 } 241 242 ///////////////////////////////////////////////////////////////////////////////////////////// 243 void cv::viz::Viz3d::VizImpl::setWidgetPose(const String &id, const Affine3d &pose) 244 { 245 WidgetActorMap::iterator wam_itr = widget_actor_map_->find(id); 246 bool exists = wam_itr != widget_actor_map_->end(); 247 CV_Assert("Widget does not exist." && exists); 248 249 vtkProp3D *actor = vtkProp3D::SafeDownCast(wam_itr->second); 250 CV_Assert("Widget is not 3D." && actor); 251 252 vtkSmartPointer<vtkMatrix4x4> matrix = vtkmatrix(pose.matrix); 253 actor->SetUserMatrix(matrix); 254 actor->Modified(); 255 } 256 257 ///////////////////////////////////////////////////////////////////////////////////////////// 258 void cv::viz::Viz3d::VizImpl::updateWidgetPose(const String &id, const Affine3d &pose) 259 { 260 WidgetActorMap::iterator wam_itr = widget_actor_map_->find(id); 261 bool exists = wam_itr != widget_actor_map_->end(); 262 CV_Assert("Widget does not exist." && exists); 263 264 vtkProp3D *actor = vtkProp3D::SafeDownCast(wam_itr->second); 265 CV_Assert("Widget is not 3D." && actor); 266 267 vtkSmartPointer<vtkMatrix4x4> matrix = actor->GetUserMatrix(); 268 if (!matrix) 269 { 270 setWidgetPose(id, pose); 271 return ; 272 } 273 Affine3d updated_pose = pose * Affine3d(*matrix->Element); 274 matrix = vtkmatrix(updated_pose.matrix); 275 276 actor->SetUserMatrix(matrix); 277 actor->Modified(); 278 } 279 280 ///////////////////////////////////////////////////////////////////////////////////////////// 281 cv::Affine3d cv::viz::Viz3d::VizImpl::getWidgetPose(const String &id) const 282 { 283 WidgetActorMap::const_iterator wam_itr = widget_actor_map_->find(id); 284 bool exists = wam_itr != widget_actor_map_->end(); 285 CV_Assert("Widget does not exist." && exists); 286 287 vtkProp3D *actor = vtkProp3D::SafeDownCast(wam_itr->second); 288 CV_Assert("Widget is not 3D." && actor); 289 290 return Affine3d(*actor->GetUserMatrix()->Element); 291 } 292 293 ///////////////////////////////////////////////////////////////////////////////////////////// 294 void cv::viz::Viz3d::VizImpl::saveScreenshot(const String &file) { style_->saveScreenshot(file.c_str()); } 295 296 ///////////////////////////////////////////////////////////////////////////////////////////// 297 void cv::viz::Viz3d::VizImpl::registerMouseCallback(MouseCallback callback, void* cookie) 298 { style_->registerMouseCallback(callback, cookie); } 299 300 void cv::viz::Viz3d::VizImpl::registerKeyboardCallback(KeyboardCallback callback, void* cookie) 301 { style_->registerKeyboardCallback(callback, cookie); } 302 303 304 ////////////////////////////////////////////////////////////////////////////////////////// 305 void cv::viz::Viz3d::VizImpl::removeAllWidgets() 306 { 307 widget_actor_map_->clear(); 308 renderer_->RemoveAllViewProps(); 309 } 310 ///////////////////////////////////////////////////////////////////////////////////////////// 311 void cv::viz::Viz3d::VizImpl::showImage(InputArray image, const Size& window_size) 312 { 313 removeAllWidgets(); 314 if (window_size.width > 0 && window_size.height > 0) 315 setWindowSize(window_size); 316 317 showWidget("showImage", WImageOverlay(image, Rect(Point(0,0), getWindowSize()))); 318 } 319 320 ///////////////////////////////////////////////////////////////////////////////////////////// 321 bool cv::viz::Viz3d::VizImpl::removeActorFromRenderer(vtkSmartPointer<vtkProp> actor) 322 { 323 vtkPropCollection* actors = renderer_->GetViewProps(); 324 actors->InitTraversal(); 325 vtkProp* current_actor = NULL; 326 while ((current_actor = actors->GetNextProp()) != NULL) 327 if (current_actor == actor) 328 { 329 renderer_->RemoveActor(actor); 330 return true; 331 } 332 return false; 333 } 334 335 ////////////////////////////////////////////////////////////////////////////////////////////// 336 void cv::viz::Viz3d::VizImpl::setBackgroundColor(const Color& color, const Color& color2) 337 { 338 Color c = vtkcolor(color), c2 = vtkcolor(color2); 339 bool gradient = color2[0] >= 0 && color2[1] >= 0 && color2[2] >= 0; 340 341 if (gradient) 342 { 343 renderer_->SetBackground(c2.val); 344 renderer_->SetBackground2(c.val); 345 renderer_->GradientBackgroundOn(); 346 } 347 else 348 { 349 renderer_->SetBackground(c.val); 350 renderer_->GradientBackgroundOff(); 351 } 352 } 353 354 void cv::viz::Viz3d::VizImpl::setBackgroundMeshLab() 355 { setBackgroundColor(Color(2, 1, 1), Color(240, 120, 120)); } 356 357 ////////////////////////////////////////////////////////////////////////////////////////////// 358 void cv::viz::Viz3d::VizImpl::setBackgroundTexture(InputArray image) 359 { 360 if (image.empty()) 361 { 362 renderer_->SetBackgroundTexture(0); 363 renderer_->TexturedBackgroundOff(); 364 return; 365 } 366 367 vtkSmartPointer<vtkImageMatSource> source = vtkSmartPointer<vtkImageMatSource>::New(); 368 source->SetImage(image); 369 370 vtkSmartPointer<vtkImageFlip> image_flip = vtkSmartPointer<vtkImageFlip>::New(); 371 image_flip->SetFilteredAxis(1); // Vertical flip 372 image_flip->SetInputConnection(source->GetOutputPort()); 373 374 vtkSmartPointer<vtkTexture> texture = vtkSmartPointer<vtkTexture>::New(); 375 texture->SetInputConnection(image_flip->GetOutputPort()); 376 //texture->Update(); 377 378 renderer_->SetBackgroundTexture(texture); 379 renderer_->TexturedBackgroundOn(); 380 } 381 382 ///////////////////////////////////////////////////////////////////////////////////////////// 383 void cv::viz::Viz3d::VizImpl::setCamera(const Camera &camera) 384 { 385 vtkSmartPointer<vtkCamera> active_camera = renderer_->GetActiveCamera(); 386 387 // Set the intrinsic parameters of the camera 388 window_->SetSize(camera.getWindowSize().width, camera.getWindowSize().height); 389 double aspect_ratio = static_cast<double>(camera.getWindowSize().width)/static_cast<double>(camera.getWindowSize().height); 390 391 Matx44d proj_mat; 392 camera.computeProjectionMatrix(proj_mat); 393 394 // Use the intrinsic parameters of the camera to simulate more realistically 395 vtkSmartPointer<vtkMatrix4x4> vtk_matrix = active_camera->GetProjectionTransformMatrix(aspect_ratio, -1.0, 1.0); 396 Matx44d old_proj_mat(*vtk_matrix->Element); 397 398 // This is a hack around not being able to set Projection Matrix 399 vtkSmartPointer<vtkTransform> transform = vtkSmartPointer<vtkTransform>::New(); 400 transform->SetMatrix(vtkmatrix(proj_mat * old_proj_mat.inv())); 401 active_camera->SetUserTransform(transform); 402 403 renderer_->ResetCameraClippingRange(); 404 renderer_->Render(); 405 } 406 407 ///////////////////////////////////////////////////////////////////////////////////////////// 408 cv::viz::Camera cv::viz::Viz3d::VizImpl::getCamera() const 409 { 410 vtkSmartPointer<vtkCamera> active_camera = renderer_->GetActiveCamera(); 411 412 Size window_size(renderer_->GetRenderWindow()->GetSize()[0], 413 renderer_->GetRenderWindow()->GetSize()[1]); 414 double aspect_ratio = window_size.width / (double)window_size.height; 415 416 vtkSmartPointer<vtkMatrix4x4> proj_matrix = active_camera->GetProjectionTransformMatrix(aspect_ratio, -1.0f, 1.0f); 417 return Camera(Matx44d(*proj_matrix->Element), window_size); 418 } 419 420 ///////////////////////////////////////////////////////////////////////////////////////////// 421 void cv::viz::Viz3d::VizImpl::setViewerPose(const Affine3d &pose) 422 { 423 vtkCamera& camera = *renderer_->GetActiveCamera(); 424 425 // Position = extrinsic translation 426 cv::Vec3d pos_vec = pose.translation(); 427 428 // Rotate the view vector 429 cv::Matx33d rotation = pose.rotation(); 430 cv::Vec3d y_axis(0.0, -1.0, 0.0); // In Computer Vision Camera Y-axis is oriented down 431 cv::Vec3d up_vec(rotation * y_axis); 432 433 // Compute the new focal point 434 cv::Vec3d z_axis(0.0, 0.0, 1.0); 435 cv::Vec3d focal_vec = pose * z_axis; 436 437 camera.SetPosition(pos_vec.val); 438 camera.SetFocalPoint(focal_vec.val); 439 camera.SetViewUp(up_vec.val); 440 441 renderer_->ResetCameraClippingRange(); 442 } 443 444 ///////////////////////////////////////////////////////////////////////////////////////////// 445 cv::Affine3d cv::viz::Viz3d::VizImpl::getViewerPose() 446 { 447 vtkCamera& camera = *renderer_->GetActiveCamera(); 448 449 Vec3d pos(camera.GetPosition()); 450 Vec3d view_up(camera.GetViewUp()); 451 Vec3d focal(camera.GetFocalPoint()); 452 453 Vec3d y_axis = normalized(-view_up); // In Computer Vision Camera Y-axis is oriented down 454 Vec3d z_axis = normalized(focal - pos); 455 Vec3d x_axis = normalized(y_axis.cross(z_axis)); 456 457 return makeTransformToGlobal(x_axis, y_axis, z_axis, pos); 458 } 459 460 ///////////////////////////////////////////////////////////////////////////////////////////// 461 void cv::viz::Viz3d::VizImpl::convertToWindowCoordinates(const Point3d &pt, Point3d &window_coord) 462 { 463 Vec3d window_pt; 464 vtkInteractorObserver::ComputeWorldToDisplay(renderer_, pt.x, pt.y, pt.z, window_pt.val); 465 window_coord = window_pt; 466 } 467 468 ///////////////////////////////////////////////////////////////////////////////////////////// 469 void cv::viz::Viz3d::VizImpl::converTo3DRay(const Point3d &window_coord, Point3d &origin, Vec3d &direction) 470 { 471 Vec4d world_pt; 472 vtkInteractorObserver::ComputeDisplayToWorld(renderer_, window_coord.x, window_coord.y, window_coord.z, world_pt.val); 473 Vec3d cam_pos(renderer_->GetActiveCamera()->GetPosition()); 474 origin = cam_pos; 475 direction = normalize(Vec3d(world_pt.val) - cam_pos); 476 } 477 478 ///////////////////////////////////////////////////////////////////////////////////////////// 479 void cv::viz::Viz3d::VizImpl::resetCameraViewpoint(const String &id) 480 { 481 vtkSmartPointer<vtkMatrix4x4> camera_pose; 482 static WidgetActorMap::iterator it = widget_actor_map_->find(id); 483 if (it != widget_actor_map_->end()) 484 { 485 vtkProp3D *actor = vtkProp3D::SafeDownCast(it->second); 486 CV_Assert("Widget is not 3D." && actor); 487 camera_pose = actor->GetUserMatrix(); 488 } 489 else 490 return; 491 492 // Prevent a segfault 493 if (!camera_pose) return; 494 495 vtkSmartPointer<vtkCamera> cam = renderer_->GetActiveCamera(); 496 cam->SetPosition(camera_pose->GetElement(0, 3), 497 camera_pose->GetElement(1, 3), 498 camera_pose->GetElement(2, 3)); 499 500 cam->SetFocalPoint(camera_pose->GetElement(0, 3) - camera_pose->GetElement(0, 2), 501 camera_pose->GetElement(1, 3) - camera_pose->GetElement(1, 2), 502 camera_pose->GetElement(2, 3) - camera_pose->GetElement(2, 2)); 503 504 cam->SetViewUp(camera_pose->GetElement(0, 1), 505 camera_pose->GetElement(1, 1), 506 camera_pose->GetElement(2, 1)); 507 508 renderer_->SetActiveCamera(cam); 509 renderer_->ResetCameraClippingRange(); 510 renderer_->Render(); 511 } 512 513 /////////////////////////////////////////////////////////////////////////////////// 514 void cv::viz::Viz3d::VizImpl::resetCamera() 515 { 516 renderer_->ResetCamera(); 517 } 518 519 /////////////////////////////////////////////////////////////////////////////////// 520 void cv::viz::Viz3d::VizImpl::setRepresentation(int representation) 521 { 522 vtkActorCollection * actors = renderer_->GetActors(); 523 actors->InitTraversal(); 524 vtkActor * actor; 525 switch (representation) 526 { 527 case REPRESENTATION_POINTS: 528 { 529 while ((actor = actors->GetNextActor()) != NULL) 530 actor->GetProperty()->SetRepresentationToPoints(); 531 break; 532 } 533 case REPRESENTATION_SURFACE: 534 { 535 while ((actor = actors->GetNextActor()) != NULL) 536 actor->GetProperty()->SetRepresentationToSurface(); 537 break; 538 } 539 case REPRESENTATION_WIREFRAME: 540 { 541 while ((actor = actors->GetNextActor()) != NULL) 542 actor->GetProperty()->SetRepresentationToWireframe(); 543 break; 544 } 545 } 546 } 547 548 ////////////////////////////////////////////////////////////////////////////////////////////// 549 cv::String cv::viz::Viz3d::VizImpl::getWindowName() const { return window_name_; } 550 void cv::viz::Viz3d::VizImpl::setFullScreen(bool mode) { window_->SetFullScreen(mode); } 551 void cv::viz::Viz3d::VizImpl::setWindowPosition(const Point& position) { window_position_ = position; window_->SetPosition(position.x, position.y); } 552 void cv::viz::Viz3d::VizImpl::setWindowSize(const Size& window_size) { window_->SetSize(window_size.width, window_size.height); } 553 cv::Size cv::viz::Viz3d::VizImpl::getWindowSize() const { return Size(Point(Vec2i(window_->GetSize()))); } 554