diff --git a/modules/viz/include/opencv2/viz/viz3d.hpp b/modules/viz/include/opencv2/viz/viz3d.hpp index 89e1841dbb..a80fd37486 100644 --- a/modules/viz/include/opencv2/viz/viz3d.hpp +++ b/modules/viz/include/opencv2/viz/viz3d.hpp @@ -60,6 +60,9 @@ namespace cv void setRenderingProperty(int property, double value, const String &id); double getRenderingProperty(int property, const String &id); + void setDesiredUpdateRate(double time); + double getDesiredUpdateRate(); + void setRepresentationToSurface(); void setRepresentationToWireframe(); void setRepresentationToPoints(); diff --git a/modules/viz/src/viz3d.cpp b/modules/viz/src/viz3d.cpp index 5a2ae0531c..96483f43aa 100644 --- a/modules/viz/src/viz3d.cpp +++ b/modules/viz/src/viz3d.cpp @@ -80,6 +80,9 @@ void cv::viz::Viz3d::setBackgroundColor(const Color& color) { impl_->setBackgrou void cv::viz::Viz3d::setRenderingProperty(int property, double value, const String &id) { getWidget(id).setRenderingProperty(property, value); } double cv::viz::Viz3d::getRenderingProperty(int property, const String &id) { return getWidget(id).getRenderingProperty(property); } +void cv::viz::Viz3d::setDesiredUpdateRate(double time) { impl_->setDesiredUpdateRate(time); } +double cv::viz::Viz3d::getDesiredUpdateRate() { return impl_->getDesiredUpdateRate(); } + void cv::viz::Viz3d::setRepresentationToSurface() { impl_->setRepresentationToSurface(); } void cv::viz::Viz3d::setRepresentationToWireframe() { impl_->setRepresentationToWireframe(); } -void cv::viz::Viz3d::setRepresentationToPoints() { impl_->setRepresentationToPoints(); } +void cv::viz::Viz3d::setRepresentationToPoints() { impl_->setRepresentationToPoints(); } \ No newline at end of file diff --git a/modules/viz/src/viz3d_impl.cpp b/modules/viz/src/viz3d_impl.cpp index f6d560da97..f0157331ec 100644 --- a/modules/viz/src/viz3d_impl.cpp +++ b/modules/viz/src/viz3d_impl.cpp @@ -84,6 +84,127 @@ cv::viz::Viz3d::VizImpl::~VizImpl () if (renderer_) renderer_->Clear(); } +///////////////////////////////////////////////////////////////////////////////////////////// +void cv::viz::Viz3d::VizImpl::showWidget(const String &id, const Widget &widget, const Affine3f &pose) +{ + WidgetActorMap::iterator wam_itr = widget_actor_map_->find(id); + bool exists = wam_itr != widget_actor_map_->end(); + if (exists) + { + // Remove it if it exists and add it again + removeActorFromRenderer(wam_itr->second); + } + // Get the actor and set the user matrix + vtkProp3D *actor = vtkProp3D::SafeDownCast(WidgetAccessor::getProp(widget)); + if (actor) + { + // If the actor is 3D, apply pose + vtkSmartPointer matrix = convertToVtkMatrix(pose.matrix); + actor->SetUserMatrix (matrix); + actor->Modified(); + } + // If the actor is a vtkFollower, then it should always face the camera + vtkFollower *follower = vtkFollower::SafeDownCast(actor); + if (follower) + { + follower->SetCamera(renderer_->GetActiveCamera()); + } + + renderer_->AddActor(WidgetAccessor::getProp(widget)); + (*widget_actor_map_)[id] = WidgetAccessor::getProp(widget); +} + +///////////////////////////////////////////////////////////////////////////////////////////// +void cv::viz::Viz3d::VizImpl::removeWidget(const String &id) +{ + WidgetActorMap::iterator wam_itr = widget_actor_map_->find(id); + bool exists = wam_itr != widget_actor_map_->end(); + CV_Assert(exists); + CV_Assert(removeActorFromRenderer (wam_itr->second)); + widget_actor_map_->erase(wam_itr); +} + +///////////////////////////////////////////////////////////////////////////////////////////// +cv::viz::Widget cv::viz::Viz3d::VizImpl::getWidget(const String &id) const +{ + WidgetActorMap::const_iterator wam_itr = widget_actor_map_->find(id); + bool exists = wam_itr != widget_actor_map_->end(); + CV_Assert(exists); + + Widget widget; + WidgetAccessor::setProp(widget, wam_itr->second); + return widget; +} + +///////////////////////////////////////////////////////////////////////////////////////////// +void cv::viz::Viz3d::VizImpl::setWidgetPose(const String &id, const Affine3f &pose) +{ + WidgetActorMap::iterator wam_itr = widget_actor_map_->find(id); + bool exists = wam_itr != widget_actor_map_->end(); + CV_Assert(exists); + + vtkProp3D *actor = vtkProp3D::SafeDownCast(wam_itr->second); + CV_Assert(actor); + + vtkSmartPointer matrix = convertToVtkMatrix(pose.matrix); + actor->SetUserMatrix (matrix); + actor->Modified (); +} + +///////////////////////////////////////////////////////////////////////////////////////////// +void cv::viz::Viz3d::VizImpl::updateWidgetPose(const String &id, const Affine3f &pose) +{ + WidgetActorMap::iterator wam_itr = widget_actor_map_->find(id); + bool exists = wam_itr != widget_actor_map_->end(); + CV_Assert(exists); + + vtkProp3D *actor = vtkProp3D::SafeDownCast(wam_itr->second); + CV_Assert(actor); + + vtkSmartPointer matrix = actor->GetUserMatrix(); + if (!matrix) + { + setWidgetPose(id, pose); + return ; + } + Matx44f matrix_cv = convertToMatx(matrix); + Affine3f updated_pose = pose * Affine3f(matrix_cv); + matrix = convertToVtkMatrix(updated_pose.matrix); + + actor->SetUserMatrix (matrix); + actor->Modified (); +} + +///////////////////////////////////////////////////////////////////////////////////////////// +cv::Affine3f cv::viz::Viz3d::VizImpl::getWidgetPose(const String &id) const +{ + WidgetActorMap::const_iterator wam_itr = widget_actor_map_->find(id); + bool exists = wam_itr != widget_actor_map_->end(); + CV_Assert(exists); + + vtkProp3D *actor = vtkProp3D::SafeDownCast(wam_itr->second); + CV_Assert(actor); + + vtkSmartPointer matrix = actor->GetUserMatrix(); + Matx44f matrix_cv = convertToMatx(matrix); + return Affine3f(matrix_cv); +} + +///////////////////////////////////////////////////////////////////////////////////////////// +void cv::viz::Viz3d::VizImpl::setDesiredUpdateRate(double time) +{ + if (interactor_) + interactor_->SetDesiredUpdateRate(time); +} + +///////////////////////////////////////////////////////////////////////////////////////////// +double cv::viz::Viz3d::VizImpl::getDesiredUpdateRate() +{ + if (interactor_) + return interactor_->GetDesiredUpdateRate(); + return 0.0; +} + ///////////////////////////////////////////////////////////////////////////////////////////// void cv::viz::Viz3d::VizImpl::saveScreenshot (const std::string &file) { style_->saveScreenshot (file); } @@ -492,103 +613,3 @@ cv::String cv::viz::Viz3d::VizImpl::getWindowName() const void cv::viz::Viz3d::VizImpl::setWindowPosition (int x, int y) { window_->SetPosition (x, y); } void cv::viz::Viz3d::VizImpl::setWindowSize (int xw, int yw) { window_->SetSize (xw, yw); } cv::Size cv::viz::Viz3d::VizImpl::getWindowSize() const { return Size(window_->GetSize()[0], window_->GetSize()[1]); } - -void cv::viz::Viz3d::VizImpl::showWidget(const String &id, const Widget &widget, const Affine3f &pose) -{ - WidgetActorMap::iterator wam_itr = widget_actor_map_->find(id); - bool exists = wam_itr != widget_actor_map_->end(); - if (exists) - { - // Remove it if it exists and add it again - removeActorFromRenderer(wam_itr->second); - } - // Get the actor and set the user matrix - vtkProp3D *actor = vtkProp3D::SafeDownCast(WidgetAccessor::getProp(widget)); - if (actor) - { - // If the actor is 3D, apply pose - vtkSmartPointer matrix = convertToVtkMatrix(pose.matrix); - actor->SetUserMatrix (matrix); - actor->Modified(); - } - // If the actor is a vtkFollower, then it should always face the camera - vtkFollower *follower = vtkFollower::SafeDownCast(actor); - if (follower) - { - follower->SetCamera(renderer_->GetActiveCamera()); - } - - renderer_->AddActor(WidgetAccessor::getProp(widget)); - (*widget_actor_map_)[id] = WidgetAccessor::getProp(widget); -} - -void cv::viz::Viz3d::VizImpl::removeWidget(const String &id) -{ - WidgetActorMap::iterator wam_itr = widget_actor_map_->find(id); - bool exists = wam_itr != widget_actor_map_->end(); - CV_Assert(exists); - CV_Assert(removeActorFromRenderer (wam_itr->second)); - widget_actor_map_->erase(wam_itr); -} - -cv::viz::Widget cv::viz::Viz3d::VizImpl::getWidget(const String &id) const -{ - WidgetActorMap::const_iterator wam_itr = widget_actor_map_->find(id); - bool exists = wam_itr != widget_actor_map_->end(); - CV_Assert(exists); - - Widget widget; - WidgetAccessor::setProp(widget, wam_itr->second); - return widget; -} - -void cv::viz::Viz3d::VizImpl::setWidgetPose(const String &id, const Affine3f &pose) -{ - WidgetActorMap::iterator wam_itr = widget_actor_map_->find(id); - bool exists = wam_itr != widget_actor_map_->end(); - CV_Assert(exists); - - vtkProp3D *actor = vtkProp3D::SafeDownCast(wam_itr->second); - CV_Assert(actor); - - vtkSmartPointer matrix = convertToVtkMatrix(pose.matrix); - actor->SetUserMatrix (matrix); - actor->Modified (); -} - -void cv::viz::Viz3d::VizImpl::updateWidgetPose(const String &id, const Affine3f &pose) -{ - WidgetActorMap::iterator wam_itr = widget_actor_map_->find(id); - bool exists = wam_itr != widget_actor_map_->end(); - CV_Assert(exists); - - vtkProp3D *actor = vtkProp3D::SafeDownCast(wam_itr->second); - CV_Assert(actor); - - vtkSmartPointer matrix = actor->GetUserMatrix(); - if (!matrix) - { - setWidgetPose(id, pose); - return ; - } - Matx44f matrix_cv = convertToMatx(matrix); - Affine3f updated_pose = pose * Affine3f(matrix_cv); - matrix = convertToVtkMatrix(updated_pose.matrix); - - actor->SetUserMatrix (matrix); - actor->Modified (); -} - -cv::Affine3f cv::viz::Viz3d::VizImpl::getWidgetPose(const String &id) const -{ - WidgetActorMap::const_iterator wam_itr = widget_actor_map_->find(id); - bool exists = wam_itr != widget_actor_map_->end(); - CV_Assert(exists); - - vtkProp3D *actor = vtkProp3D::SafeDownCast(wam_itr->second); - CV_Assert(actor); - - vtkSmartPointer matrix = actor->GetUserMatrix(); - Matx44f matrix_cv = convertToMatx(matrix); - return Affine3f(matrix_cv); -} diff --git a/modules/viz/src/viz3d_impl.hpp b/modules/viz/src/viz3d_impl.hpp index 5dcd4bf857..4d24fb86e6 100644 --- a/modules/viz/src/viz3d_impl.hpp +++ b/modules/viz/src/viz3d_impl.hpp @@ -26,8 +26,8 @@ public: void updateWidgetPose(const String &id, const Affine3f &pose); Affine3f getWidgetPose(const String &id) const; - void setRenderingProperty(int property, double value, const String &id); - double getRenderingProperty(int property, const String &id); + void setDesiredUpdateRate(double time); + double getDesiredUpdateRate(); /** \brief Returns true when the user tried to close the window */ bool wasStopped () const { if (interactor_ != NULL) return (stopped_); else return true; }