diff --git a/modules/viz/include/opencv2/viz/viz3d.hpp b/modules/viz/include/opencv2/viz/viz3d.hpp index 65065bc7f..a0181cb71 100644 --- a/modules/viz/include/opencv2/viz/viz3d.hpp +++ b/modules/viz/include/opencv2/viz/viz3d.hpp @@ -47,10 +47,11 @@ namespace temp_viz bool wasStopped() const; void showWidget(const String &id, const Widget &widget, const Affine3f &pose = Affine3f::Identity()); - bool removeWidget(const String &id); + void removeWidget(const String &id); + Widget getWidget(const String &id) const; - bool setWidgetPose(const String &id, const Affine3f &pose); - bool updateWidgetPose(const String &id, const Affine3f &pose); + void setWidgetPose(const String &id, const Affine3f &pose); + void updateWidgetPose(const String &id, const Affine3f &pose); Affine3f getWidgetPose(const String &id) const; private: Viz3d(const Viz3d&); diff --git a/modules/viz/include/opencv2/viz/widgets.hpp b/modules/viz/include/opencv2/viz/widgets.hpp index cc53e734f..9aea9725a 100644 --- a/modules/viz/include/opencv2/viz/widgets.hpp +++ b/modules/viz/include/opencv2/viz/widgets.hpp @@ -31,6 +31,8 @@ namespace temp_viz { public: Widget3D() {} + Widget3D(const Widget& other); + Widget3D& operator =(const Widget &other); void setPose(const Affine3f &pose); void updatePose(const Affine3f &pose); @@ -49,6 +51,8 @@ namespace temp_viz { public: Widget2D() {} + Widget2D(const Widget &other); + Widget2D& operator=(const Widget &other); }; @@ -56,6 +60,8 @@ namespace temp_viz { public: LineWidget(const Point3f &pt1, const Point3f &pt2, const Color &color = Color::white()); + LineWidget(const Widget &other) : Widget3D(other) {} + LineWidget& operator=(const Widget &other); void setLineWidth(float line_width); float getLineWidth(); @@ -66,48 +72,64 @@ namespace temp_viz public: PlaneWidget(const Vec4f& coefs, double size = 1.0, const Color &color = Color::white()); PlaneWidget(const Vec4f& coefs, const Point3f& pt, double size = 1.0, const Color &color = Color::white()); + PlaneWidget(const Widget& other) : Widget3D(other) {} + PlaneWidget& operator=(const Widget& other); }; class CV_EXPORTS SphereWidget : public Widget3D { public: SphereWidget(const cv::Point3f ¢er, float radius, int sphere_resolution = 10, const Color &color = Color::white()); + SphereWidget(const Widget &other) : Widget3D(other) {} + SphereWidget& operator=(const Widget &other); }; class CV_EXPORTS ArrowWidget : public Widget3D { public: ArrowWidget(const Point3f& pt1, const Point3f& pt2, const Color &color = Color::white()); + ArrowWidget(const Widget &other) : Widget3D(other) {} + ArrowWidget& operator=(const Widget &other); }; class CV_EXPORTS CircleWidget : public Widget3D { public: CircleWidget(const Point3f& pt, double radius, double thickness = 0.01, const Color &color = Color::white()); + CircleWidget(const Widget& other) : Widget3D(other) {} + CircleWidget& operator=(const Widget &other); }; class CV_EXPORTS CylinderWidget : public Widget3D { public: CylinderWidget(const Point3f& pt_on_axis, const Point3f& axis_direction, double radius, int numsides = 30, const Color &color = Color::white()); + CylinderWidget(const Widget& other) : Widget3D(other) {} + CylinderWidget& operator=(const Widget &other); }; class CV_EXPORTS CubeWidget : public Widget3D { public: CubeWidget(const Point3f& pt_min, const Point3f& pt_max, bool wire_frame = true, const Color &color = Color::white()); + CubeWidget(const Widget& other) : Widget3D(other) {} + CubeWidget& operator=(const Widget &other); }; class CV_EXPORTS CoordinateSystemWidget : public Widget3D { public: CoordinateSystemWidget(double scale, const Affine3f& affine); + CoordinateSystemWidget(const Widget &other) : Widget3D(other) {} + CoordinateSystemWidget& operator=(const Widget &other); }; class CV_EXPORTS TextWidget : public Widget2D { public: TextWidget(const String &text, const Point2i &pos, int font_size = 10, const Color &color = Color::white()); + TextWidget(const Widget& other) : Widget2D(other) {} + TextWidget& operator=(const Widget &other); }; class CV_EXPORTS CloudWidget : public Widget3D @@ -115,6 +137,9 @@ namespace temp_viz public: CloudWidget(InputArray _cloud, InputArray _colors); CloudWidget(InputArray _cloud, const Color &color = Color::white()); + CloudWidget(const Widget &other) : Widget3D(other) {} + CloudWidget& operator=(const Widget &other); + private: struct CreateCloudWidget; }; @@ -123,6 +148,9 @@ namespace temp_viz { public: CloudNormalsWidget(InputArray _cloud, InputArray _normals, int level = 100, float scale = 0.02f, const Color &color = Color::white()); + CloudNormalsWidget(const Widget &other) : Widget3D(other) {} + CloudNormalsWidget& operator=(const Widget &other); + private: struct ApplyCloudNormals; }; diff --git a/modules/viz/src/q/viz3d_impl.hpp b/modules/viz/src/q/viz3d_impl.hpp index 6595416bb..a49bb4922 100644 --- a/modules/viz/src/q/viz3d_impl.hpp +++ b/modules/viz/src/q/viz3d_impl.hpp @@ -200,10 +200,11 @@ public: void setSize (int xw, int yw); void showWidget(const String &id, const Widget &widget, const Affine3f &pose = Affine3f::Identity()); - bool removeWidget(const String &id); + void removeWidget(const String &id); + Widget getWidget(const String &id) const; - bool setWidgetPose(const String &id, const Affine3f &pose); - bool updateWidgetPose(const String &id, const Affine3f &pose); + void setWidgetPose(const String &id, const Affine3f &pose); + void updateWidgetPose(const String &id, const Affine3f &pose); Affine3f getWidgetPose(const String &id) const; void all_data(); diff --git a/modules/viz/src/simple_widgets.cpp b/modules/viz/src/simple_widgets.cpp index 8cb77ee44..82a529272 100644 --- a/modules/viz/src/simple_widgets.cpp +++ b/modules/viz/src/simple_widgets.cpp @@ -25,6 +25,12 @@ temp_viz::LineWidget::LineWidget(const Point3f &pt1, const Point3f &pt2, const C setColor(color); } +temp_viz::LineWidget& temp_viz::LineWidget::operator=(const Widget &other) +{ + Widget3D::operator=(other); + return *this; +} + void temp_viz::LineWidget::setLineWidth(float line_width) { vtkLODActor *actor = vtkLODActor::SafeDownCast(WidgetAccessor::getActor(*this)); @@ -82,6 +88,12 @@ temp_viz::PlaneWidget::PlaneWidget(const Vec4f& coefs, const Point3f& pt, double setColor(color); } +temp_viz::PlaneWidget& temp_viz::PlaneWidget::operator=(const Widget& other) +{ + Widget3D::operator=(other); + return *this; +} + /////////////////////////////////////////////////////////////////////////////////////////////// /// sphere widget implementation @@ -106,6 +118,12 @@ temp_viz::SphereWidget::SphereWidget(const cv::Point3f ¢er, float radius, in setColor(color); } +temp_viz::SphereWidget& temp_viz::SphereWidget::operator=(const Widget &other) +{ + Widget3D::operator=(other); + return *this; +} + /////////////////////////////////////////////////////////////////////////////////////////////// /// arrow widget implementation @@ -170,6 +188,12 @@ temp_viz::ArrowWidget::ArrowWidget(const Point3f& pt1, const Point3f& pt2, const setColor(color); } +temp_viz::ArrowWidget& temp_viz::ArrowWidget::operator=(const Widget &other) +{ + Widget3D::operator=(other); + return *this; +} + /////////////////////////////////////////////////////////////////////////////////////////////// /// circle widget implementation @@ -201,6 +225,12 @@ temp_viz::CircleWidget::CircleWidget(const temp_viz::Point3f& pt, double radius, setColor(color); } +temp_viz::CircleWidget& temp_viz::CircleWidget::operator=(const Widget &other) +{ + Widget3D::operator=(other); + return *this; +} + /////////////////////////////////////////////////////////////////////////////////////////////// /// cylinder widget implementation @@ -227,6 +257,12 @@ temp_viz::CylinderWidget::CylinderWidget(const Point3f& pt_on_axis, const Point3 setColor(color); } +temp_viz::CylinderWidget& temp_viz::CylinderWidget::operator=(const Widget &other) +{ + Widget3D::operator=(other); + return *this; +} + /////////////////////////////////////////////////////////////////////////////////////////////// /// cylinder widget implementation @@ -249,6 +285,12 @@ temp_viz::CubeWidget::CubeWidget(const Point3f& pt_min, const Point3f& pt_max, b setColor(color); } +temp_viz::CubeWidget& temp_viz::CubeWidget::operator=(const Widget &other) +{ + Widget3D::operator=(other); + return *this; +} + /////////////////////////////////////////////////////////////////////////////////////////////// /// coordinate system widget implementation @@ -300,6 +342,12 @@ temp_viz::CoordinateSystemWidget::CoordinateSystemWidget(double scale, const Aff actor->RotateWXYZ(r_angle*180/CV_PI,rvec[0], rvec[1], rvec[2]); } +temp_viz::CoordinateSystemWidget& temp_viz::CoordinateSystemWidget::operator=(const Widget &other) +{ + Widget3D::operator=(other); + return *this; +} + /////////////////////////////////////////////////////////////////////////////////////////////// /// text widget implementation @@ -321,6 +369,12 @@ temp_viz::TextWidget::TextWidget(const String &text, const Point2i &pos, int fon tprop->SetColor (c.val); } +temp_viz::TextWidget& temp_viz::TextWidget::operator=(const Widget &other) +{ + Widget2D::operator=(other); + return *this; +} + /////////////////////////////////////////////////////////////////////////////////////////////// /// point cloud widget implementation @@ -487,6 +541,12 @@ temp_viz::CloudWidget::CloudWidget(InputArray _cloud, const Color &color) setColor(color); } +temp_viz::CloudWidget& temp_viz::CloudWidget::operator=(const Widget &other) +{ + Widget3D::operator=(other); + return *this; +} + /////////////////////////////////////////////////////////////////////////////////////////////// /// cloud normals widget implementation @@ -618,4 +678,10 @@ temp_viz::CloudNormalsWidget::CloudNormalsWidget(InputArray _cloud, InputArray _ vtkLODActor * actor = vtkLODActor::SafeDownCast(WidgetAccessor::getActor(*this)); actor->SetMapper(mapper); setColor(color); +} + +temp_viz::CloudNormalsWidget& temp_viz::CloudNormalsWidget::operator=(const Widget &other) +{ + Widget3D::operator=(other); + return *this; } \ No newline at end of file diff --git a/modules/viz/src/viz3d.cpp b/modules/viz/src/viz3d.cpp index ed9405e91..0460630ba 100644 --- a/modules/viz/src/viz3d.cpp +++ b/modules/viz/src/viz3d.cpp @@ -84,19 +84,24 @@ void temp_viz::Viz3d::showWidget(const String &id, const Widget &widget, const A impl_->showWidget(id, widget, pose); } -bool temp_viz::Viz3d::removeWidget(const String &id) +void temp_viz::Viz3d::removeWidget(const String &id) { - return impl_->removeWidget(id); + impl_->removeWidget(id); } -bool temp_viz::Viz3d::setWidgetPose(const String &id, const Affine3f &pose) +temp_viz::Widget temp_viz::Viz3d::getWidget(const String &id) const { - return impl_->setWidgetPose(id, pose); + return impl_->getWidget(id); } -bool temp_viz::Viz3d::updateWidgetPose(const String &id, const Affine3f &pose) +void temp_viz::Viz3d::setWidgetPose(const String &id, const Affine3f &pose) { - return impl_->updateWidgetPose(id, pose); + impl_->setWidgetPose(id, pose); +} + +void temp_viz::Viz3d::updateWidgetPose(const String &id, const Affine3f &pose) +{ + impl_->updateWidgetPose(id, pose); } temp_viz::Affine3f temp_viz::Viz3d::getWidgetPose(const String &id) const diff --git a/modules/viz/src/viz3d_impl.cpp b/modules/viz/src/viz3d_impl.cpp index 11982b574..ae7190afc 100644 --- a/modules/viz/src/viz3d_impl.cpp +++ b/modules/viz/src/viz3d_impl.cpp @@ -874,9 +874,10 @@ void temp_viz::Viz3d::VizImpl::showWidget(const String &id, const Widget &widget removeActorFromRenderer(wam_itr->second.actor); } // Get the actor and set the user matrix - vtkLODActor *actor; - if (actor = vtkLODActor::SafeDownCast(WidgetAccessor::getActor(widget))) + vtkProp3D *actor = vtkProp3D::SafeDownCast(WidgetAccessor::getActor(widget)); + if (actor) { + // If the actor is 3D, apply pose vtkSmartPointer matrix = convertToVtkMatrix(pose.matrix); actor->SetUserMatrix (matrix); actor->Modified(); @@ -885,61 +886,61 @@ void temp_viz::Viz3d::VizImpl::showWidget(const String &id, const Widget &widget (*widget_actor_map_)[id].actor = WidgetAccessor::getActor(widget); } -bool temp_viz::Viz3d::VizImpl::removeWidget(const String &id) +void temp_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); - - if (!removeActorFromRenderer (wam_itr->second.actor)) - return false; - + CV_Assert(removeActorFromRenderer (wam_itr->second.actor)); widget_actor_map_->erase(wam_itr); - return true; } -bool temp_viz::Viz3d::VizImpl::setWidgetPose(const String &id, const Affine3f &pose) +temp_viz::Widget temp_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::setVtkProp(widget, wam_itr->second.actor); + return widget; +} + +void temp_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); - vtkLODActor *actor; - if ((actor = vtkLODActor::SafeDownCast(wam_itr->second.actor))) - { - vtkSmartPointer matrix = convertToVtkMatrix(pose.matrix); - actor->SetUserMatrix (matrix); - actor->Modified (); - return true; - } - return false; + vtkProp3D *actor = vtkProp3D::SafeDownCast(wam_itr->second.actor); + CV_Assert(actor); + + vtkSmartPointer matrix = convertToVtkMatrix(pose.matrix); + actor->SetUserMatrix (matrix); + actor->Modified (); } -bool temp_viz::Viz3d::VizImpl::updateWidgetPose(const String &id, const Affine3f &pose) +void temp_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); - vtkLODActor *actor; - if ((actor = vtkLODActor::SafeDownCast(wam_itr->second.actor))) + vtkProp3D *actor = vtkProp3D::SafeDownCast(wam_itr->second.actor); + CV_Assert(actor); + + vtkSmartPointer matrix = actor->GetUserMatrix(); + if (!matrix) { - vtkSmartPointer matrix = actor->GetUserMatrix(); - if (!matrix) - { - setWidgetPose(id, pose); - return true; - } - Matx44f matrix_cv = convertToMatx(matrix); - - Affine3f updated_pose = pose * Affine3f(matrix_cv); - matrix = convertToVtkMatrix(updated_pose.matrix); - - actor->SetUserMatrix (matrix); - actor->Modified (); - return true; + setWidgetPose(id, pose); + return ; } - return false; + Matx44f matrix_cv = convertToMatx(matrix); + Affine3f updated_pose = pose * Affine3f(matrix_cv); + matrix = convertToVtkMatrix(updated_pose.matrix); + + actor->SetUserMatrix (matrix); + actor->Modified (); } temp_viz::Affine3f temp_viz::Viz3d::VizImpl::getWidgetPose(const String &id) const @@ -948,12 +949,10 @@ temp_viz::Affine3f temp_viz::Viz3d::VizImpl::getWidgetPose(const String &id) con bool exists = wam_itr != widget_actor_map_->end(); CV_Assert(exists); - vtkLODActor *actor; - if ((actor = vtkLODActor::SafeDownCast(wam_itr->second.actor))) - { - vtkSmartPointer matrix = actor->GetUserMatrix(); - Matx44f matrix_cv = convertToMatx(matrix); - return Affine3f(matrix_cv); - } - return Affine3f(); + vtkProp3D *actor = vtkProp3D::SafeDownCast(wam_itr->second.actor); + CV_Assert(actor); + + vtkSmartPointer matrix = actor->GetUserMatrix(); + Matx44f matrix_cv = convertToMatx(matrix); + return Affine3f(matrix_cv); } \ No newline at end of file diff --git a/modules/viz/src/widget.cpp b/modules/viz/src/widget.cpp index 93dd2a68e..419cd350a 100644 --- a/modules/viz/src/widget.cpp +++ b/modules/viz/src/widget.cpp @@ -22,7 +22,7 @@ temp_viz::Widget::Widget(const Widget &other) : impl_(other.impl_) if (impl_) CV_XADD(&impl_->ref_counter, 1); } -temp_viz::Widget& temp_viz::Widget::operator =(const Widget &other) +temp_viz::Widget& temp_viz::Widget::operator=(const Widget &other) { if (this != &other) { @@ -91,6 +91,23 @@ struct temp_viz::Widget3D::MatrixConverter } }; +temp_viz::Widget3D::Widget3D(const Widget& other) : Widget(other) +{ + // Check if other's actor is castable to vtkProp3D + vtkProp3D *actor = vtkProp3D::SafeDownCast(WidgetAccessor::getActor(other)); + CV_Assert(actor); +} + +temp_viz::Widget3D& temp_viz::Widget3D::operator =(const Widget &other) +{ + // Check if other's actor is castable to vtkProp3D + vtkProp3D *actor = vtkProp3D::SafeDownCast(WidgetAccessor::getActor(other)); + CV_Assert(actor); + + Widget::operator=(other); + return *this; +} + void temp_viz::Widget3D::setPose(const Affine3f &pose) { vtkProp3D *actor = vtkProp3D::SafeDownCast(WidgetAccessor::getActor(*this)); @@ -147,3 +164,22 @@ void temp_viz::Widget3D::setColor(const Color &color) actor->GetProperty ()->SetLighting (0); actor->Modified (); } + +/////////////////////////////////////////////////////////////////////////////////////////////// +/// widget2D implementation + +temp_viz::Widget2D::Widget2D(const Widget &other) : Widget(other) +{ + // Check if other's actor is castable to vtkActor2D + vtkActor2D *actor = vtkActor2D::SafeDownCast(WidgetAccessor::getActor(other)); + CV_Assert(actor); +} + +temp_viz::Widget2D& temp_viz::Widget2D::operator=(const Widget &other) +{ + // Check if other's actor is castable to vtkActor2D + vtkActor2D *actor = vtkActor2D::SafeDownCast(WidgetAccessor::getActor(other)); + CV_Assert(actor); + Widget::operator=(other); + return *this; +} \ No newline at end of file