From d324c03b6ac419730b30dead788420ffbc095ade Mon Sep 17 00:00:00 2001 From: ozantonkal Date: Tue, 9 Jul 2013 10:21:57 +0200 Subject: [PATCH 01/12] initial redesigning widgets --- .../include/opencv2/viz/widget_accessor.hpp | 1 + modules/viz/include/opencv2/viz/widgets.hpp | 64 ++-- modules/viz/src/simple_widgets.cpp | 28 +- modules/viz/src/widget.cpp | 345 +++++++++++------- 4 files changed, 272 insertions(+), 166 deletions(-) diff --git a/modules/viz/include/opencv2/viz/widget_accessor.hpp b/modules/viz/include/opencv2/viz/widget_accessor.hpp index 0cc39f7ff5..ec2a020309 100644 --- a/modules/viz/include/opencv2/viz/widget_accessor.hpp +++ b/modules/viz/include/opencv2/viz/widget_accessor.hpp @@ -13,5 +13,6 @@ namespace temp_viz struct CV_EXPORTS WidgetAccessor { static vtkSmartPointer getActor(const Widget &widget); + static void setVtkProp(Widget &widget, vtkSmartPointer actor); }; } diff --git a/modules/viz/include/opencv2/viz/widgets.hpp b/modules/viz/include/opencv2/viz/widgets.hpp index b5b9b65cd9..b22be40405 100644 --- a/modules/viz/include/opencv2/viz/widgets.hpp +++ b/modules/viz/include/opencv2/viz/widgets.hpp @@ -13,32 +13,44 @@ namespace temp_viz Widget(); Widget(const Widget &other); Widget& operator =(const Widget &other); - + ~Widget(); - void copyTo(Widget &dst); - - void setColor(const Color &color); + private: + class Impl; + Impl *impl_; + friend struct WidgetAccessor; + + }; + + ///////////////////////////////////////////////////////////////////////////// + /// The base class for all 3D widgets + class CV_EXPORTS Widget3D : public Widget + { + public: + Widget3D(); + void setPose(const Affine3f &pose); void updatePose(const Affine3f &pose); Affine3f getPose() const; - protected: - Widget(bool text_widget); + void setColor(const Color &color); private: - class Impl; - Impl* impl_; + struct MatrixConverter; - void create(); - void release(); - void create(bool text_widget); - - friend struct WidgetAccessor; }; + + ///////////////////////////////////////////////////////////////////////////// + /// The base class for all 2D widgets + class CV_EXPORTS Widget2D : public Widget + { + public: + Widget2D(); + }; + - - class CV_EXPORTS LineWidget : public Widget + class CV_EXPORTS LineWidget : public Widget3D { public: LineWidget(const Point3f &pt1, const Point3f &pt2, const Color &color = Color::white()); @@ -47,58 +59,56 @@ namespace temp_viz float getLineWidth(); }; - class CV_EXPORTS PlaneWidget : public Widget + class CV_EXPORTS PlaneWidget : public Widget3D { 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()); }; - class CV_EXPORTS SphereWidget : public Widget + class CV_EXPORTS SphereWidget : public Widget3D { public: SphereWidget(const cv::Point3f ¢er, float radius, int sphere_resolution = 10, const Color &color = Color::white()); }; - class CV_EXPORTS ArrowWidget : public Widget + class CV_EXPORTS ArrowWidget : public Widget3D { public: ArrowWidget(const Point3f& pt1, const Point3f& pt2, const Color &color = Color::white()); }; - class CV_EXPORTS CircleWidget : public Widget + class CV_EXPORTS CircleWidget : public Widget3D { public: CircleWidget(const Point3f& pt, double radius, double thickness = 0.01, const Color &color = Color::white()); }; - class CV_EXPORTS CylinderWidget : public Widget + 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()); }; - class CV_EXPORTS CubeWidget : public Widget + 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()); }; - class CV_EXPORTS CoordinateSystemWidget : public Widget + class CV_EXPORTS CoordinateSystemWidget : public Widget3D { public: CoordinateSystemWidget(double scale, const Affine3f& affine); }; - class CV_EXPORTS TextWidget : public Widget + class CV_EXPORTS TextWidget : public Widget2D { public: TextWidget(const String &text, const Point2i &pos, int font_size = 10, const Color &color = Color::white()); - - // TODO Overload setColor method, and hide setPose, updatePose, getPose methods }; - class CV_EXPORTS CloudWidget : public Widget + class CV_EXPORTS CloudWidget : public Widget3D { public: CloudWidget(InputArray _cloud, InputArray _colors); @@ -107,7 +117,7 @@ namespace temp_viz struct CreateCloudWidget; }; - class CV_EXPORTS CloudNormalsWidget : public Widget + class CV_EXPORTS CloudNormalsWidget : public Widget3D { public: CloudNormalsWidget(InputArray _cloud, InputArray _normals, int level = 100, float scale = 0.02f, const Color &color = Color::white()); diff --git a/modules/viz/src/simple_widgets.cpp b/modules/viz/src/simple_widgets.cpp index 640e85c98f..8cb77ee441 100644 --- a/modules/viz/src/simple_widgets.cpp +++ b/modules/viz/src/simple_widgets.cpp @@ -9,6 +9,8 @@ namespace temp_viz /// line widget implementation temp_viz::LineWidget::LineWidget(const Point3f &pt1, const Point3f &pt2, const Color &color) { + WidgetAccessor::setVtkProp(*this, vtkSmartPointer::New()); + vtkSmartPointer line = vtkSmartPointer::New(); line->SetPoint1 (pt1.x, pt1.y, pt1.z); line->SetPoint2 (pt2.x, pt2.y, pt2.z); @@ -40,6 +42,8 @@ float temp_viz::LineWidget::getLineWidth() temp_viz::PlaneWidget::PlaneWidget(const Vec4f& coefs, double size, const Color &color) { + WidgetAccessor::setVtkProp(*this, vtkSmartPointer::New()); + vtkSmartPointer plane = vtkSmartPointer::New (); plane->SetNormal (coefs[0], coefs[1], coefs[2]); double norm = cv::norm(cv::Vec3f(coefs.val)); @@ -57,6 +61,8 @@ temp_viz::PlaneWidget::PlaneWidget(const Vec4f& coefs, double size, const Color temp_viz::PlaneWidget::PlaneWidget(const Vec4f& coefs, const Point3f& pt, double size, const Color &color) { + WidgetAccessor::setVtkProp(*this, vtkSmartPointer::New()); + vtkSmartPointer plane = vtkSmartPointer::New (); cv::Point3f coefs3(coefs[0], coefs[1], coefs[2]); double norm_sqr = 1.0 / coefs3.dot (coefs3); @@ -81,6 +87,8 @@ temp_viz::PlaneWidget::PlaneWidget(const Vec4f& coefs, const Point3f& pt, double temp_viz::SphereWidget::SphereWidget(const cv::Point3f ¢er, float radius, int sphere_resolution, const Color &color) { + WidgetAccessor::setVtkProp(*this, vtkSmartPointer::New()); + vtkSmartPointer sphere = vtkSmartPointer::New (); sphere->SetRadius (radius); sphere->SetCenter (center.x, center.y, center.z); @@ -103,6 +111,8 @@ temp_viz::SphereWidget::SphereWidget(const cv::Point3f ¢er, float radius, in temp_viz::ArrowWidget::ArrowWidget(const Point3f& pt1, const Point3f& pt2, const Color &color) { + WidgetAccessor::setVtkProp(*this, vtkSmartPointer::New()); + vtkSmartPointer arrowSource = vtkSmartPointer::New (); float startPoint[3], endPoint[3]; @@ -165,6 +175,8 @@ temp_viz::ArrowWidget::ArrowWidget(const Point3f& pt1, const Point3f& pt2, const temp_viz::CircleWidget::CircleWidget(const temp_viz::Point3f& pt, double radius, double thickness, const temp_viz::Color& color) { + WidgetAccessor::setVtkProp(*this, vtkSmartPointer::New()); + vtkSmartPointer disk = vtkSmartPointer::New (); // Maybe the resolution should be lower e.g. 50 or 25 disk->SetCircumferentialResolution (50); @@ -194,6 +206,8 @@ temp_viz::CircleWidget::CircleWidget(const temp_viz::Point3f& pt, double radius, temp_viz::CylinderWidget::CylinderWidget(const Point3f& pt_on_axis, const Point3f& axis_direction, double radius, int numsides, const Color &color) { + WidgetAccessor::setVtkProp(*this, vtkSmartPointer::New()); + const cv::Point3f pt2 = pt_on_axis + axis_direction; vtkSmartPointer line = vtkSmartPointer::New (); line->SetPoint1 (pt_on_axis.x, pt_on_axis.y, pt_on_axis.z); @@ -218,6 +232,8 @@ temp_viz::CylinderWidget::CylinderWidget(const Point3f& pt_on_axis, const Point3 temp_viz::CubeWidget::CubeWidget(const Point3f& pt_min, const Point3f& pt_max, bool wire_frame, const Color &color) { + WidgetAccessor::setVtkProp(*this, vtkSmartPointer::New()); + vtkSmartPointer cube = vtkSmartPointer::New (); cube->SetBounds (pt_min.x, pt_max.x, pt_min.y, pt_max.y, pt_min.z, pt_max.z); @@ -238,6 +254,8 @@ temp_viz::CubeWidget::CubeWidget(const Point3f& pt_min, const Point3f& pt_max, b temp_viz::CoordinateSystemWidget::CoordinateSystemWidget(double scale, const Affine3f& affine) { + WidgetAccessor::setVtkProp(*this, vtkSmartPointer::New()); + vtkSmartPointer axes = vtkSmartPointer::New (); axes->SetOrigin (0, 0, 0); axes->SetScaleFactor (scale); @@ -285,8 +303,10 @@ temp_viz::CoordinateSystemWidget::CoordinateSystemWidget(double scale, const Aff /////////////////////////////////////////////////////////////////////////////////////////////// /// text widget implementation -temp_viz::TextWidget::TextWidget(const String &text, const Point2i &pos, int font_size, const Color &color) : Widget(true) +temp_viz::TextWidget::TextWidget(const String &text, const Point2i &pos, int font_size, const Color &color) { + WidgetAccessor::setVtkProp(*this, vtkSmartPointer::New()); + vtkTextActor *actor = vtkTextActor::SafeDownCast(WidgetAccessor::getActor(*this)); actor->SetPosition (pos.x, pos.y); actor->SetInput (text.c_str ()); @@ -400,6 +420,8 @@ temp_viz::CloudWidget::CloudWidget(InputArray _cloud, InputArray _colors) CV_Assert(cloud.type() == CV_32FC3 || cloud.type() == CV_64FC3 || cloud.type() == CV_32FC4 || cloud.type() == CV_64FC4); CV_Assert(colors.type() == CV_8UC3 && cloud.size() == colors.size()); + WidgetAccessor::setVtkProp(*this, vtkSmartPointer::New()); + vtkLODActor * actor = vtkLODActor::SafeDownCast(WidgetAccessor::getActor(*this)); vtkIdType nr_points; vtkSmartPointer polydata = CreateCloudWidget::create(cloud, nr_points); @@ -441,6 +463,8 @@ temp_viz::CloudWidget::CloudWidget(InputArray _cloud, const Color &color) Mat cloud = _cloud.getMat(); CV_Assert(cloud.type() == CV_32FC3 || cloud.type() == CV_64FC3 || cloud.type() == CV_32FC4 || cloud.type() == CV_64FC4); + WidgetAccessor::setVtkProp(*this, vtkSmartPointer::New()); + vtkLODActor * actor = vtkLODActor::SafeDownCast(WidgetAccessor::getActor(*this)); vtkIdType nr_points; vtkSmartPointer polydata = CreateCloudWidget::create(cloud, nr_points); @@ -551,6 +575,8 @@ temp_viz::CloudNormalsWidget::CloudNormalsWidget(InputArray _cloud, InputArray _ CV_Assert(cloud.type() == CV_32FC3 || cloud.type() == CV_64FC3 || cloud.type() == CV_32FC4 || cloud.type() == CV_64FC4); CV_Assert(cloud.size() == normals.size() && cloud.type() == normals.type()); + WidgetAccessor::setVtkProp(*this, vtkSmartPointer::New()); + vtkSmartPointer points = vtkSmartPointer::New(); vtkSmartPointer lines = vtkSmartPointer::New(); vtkIdType nr_normals = 0; diff --git a/modules/viz/src/widget.cpp b/modules/viz/src/widget.cpp index f75827f8ff..4fed46d1cd 100644 --- a/modules/viz/src/widget.cpp +++ b/modules/viz/src/widget.cpp @@ -1,90 +1,132 @@ #include "precomp.hpp" +/////////////////////////////////////////////////////////////////////////////////////////////// +/// widget implementation + class temp_viz::Widget::Impl { public: vtkSmartPointer actor; - int ref_counter; - - Impl() : actor(vtkSmartPointer::New()) {} - Impl(bool text_widget) - { - if (text_widget) - actor = vtkSmartPointer::New(); - else - actor = vtkSmartPointer::New(); - } - - void setColor(const Color& color) - { - vtkLODActor *lod_actor = vtkLODActor::SafeDownCast(actor); - Color c = vtkcolor(color); - lod_actor->GetMapper ()->ScalarVisibilityOff (); - lod_actor->GetProperty ()->SetColor (c.val); - lod_actor->GetProperty ()->SetEdgeColor (c.val); - lod_actor->GetProperty ()->SetAmbient (0.8); - lod_actor->GetProperty ()->SetDiffuse (0.8); - lod_actor->GetProperty ()->SetSpecular (0.8); - lod_actor->GetProperty ()->SetLighting (0); - lod_actor->Modified (); - } - - void setPose(const Affine3f& pose) - { - vtkLODActor *lod_actor = vtkLODActor::SafeDownCast(actor); - vtkSmartPointer matrix = convertToVtkMatrix(pose.matrix); - lod_actor->SetUserMatrix (matrix); - lod_actor->Modified (); - } - - void updatePose(const Affine3f& pose) - { - vtkLODActor *lod_actor = vtkLODActor::SafeDownCast(actor); - vtkSmartPointer matrix = lod_actor->GetUserMatrix(); - if (!matrix) - { - setPose(pose); - return ; - } - Matx44f matrix_cv = convertToMatx(matrix); - - Affine3f updated_pose = pose * Affine3f(matrix_cv); - matrix = convertToVtkMatrix(updated_pose.matrix); - - lod_actor->SetUserMatrix (matrix); - lod_actor->Modified (); - } - - Affine3f getPose() const - { - vtkLODActor *lod_actor = vtkLODActor::SafeDownCast(actor); - vtkSmartPointer matrix = lod_actor->GetUserMatrix(); - Matx44f matrix_cv = convertToMatx(matrix); - return Affine3f(matrix_cv); - } - -protected: - - static vtkSmartPointer convertToVtkMatrix (const cv::Matx44f& m) - { - vtkSmartPointer vtk_matrix = vtkSmartPointer::New (); - for (int i = 0; i < 4; i++) - for (int k = 0; k < 4; k++) - vtk_matrix->SetElement(i, k, m(i, k)); - return vtk_matrix; - } - - static cv::Matx44f convertToMatx(const vtkSmartPointer& vtk_matrix) - { - cv::Matx44f m; - for (int i = 0; i < 4; i++) - for (int k = 0; k < 4; k++) - m(i, k) = vtk_matrix->GetElement (i, k); - return m; - } + Impl() : actor(0) {} }; +temp_viz::Widget::Widget() : impl_(0) +{ + impl_ = new Impl(); +} + +temp_viz::Widget::Widget(const Widget &other) : impl_(other.impl_) +{ + +} + +temp_viz::Widget& temp_viz::Widget::operator =(const Widget &other) +{ + if (this != &other) + { + delete impl_; + impl_ = other.impl_; + } + return *this; +} + +temp_viz::Widget::~Widget() +{ + if (impl_) + { + delete impl_; + impl_ = 0; + } +} + + + +// class temp_viz::Widget::Impl +// { +// public: +// vtkSmartPointer actor; +// int ref_counter; +// +// Impl() : actor(vtkSmartPointer::New()) {} +// +// Impl(bool text_widget) +// { +// if (text_widget) +// actor = vtkSmartPointer::New(); +// else +// actor = vtkSmartPointer::New(); +// } +// +// void setColor(const Color& color) +// { +// vtkLODActor *lod_actor = vtkLODActor::SafeDownCast(actor); +// Color c = vtkcolor(color); +// lod_actor->GetMapper ()->ScalarVisibilityOff (); +// lod_actor->GetProperty ()->SetColor (c.val); +// lod_actor->GetProperty ()->SetEdgeColor (c.val); +// lod_actor->GetProperty ()->SetAmbient (0.8); +// lod_actor->GetProperty ()->SetDiffuse (0.8); +// lod_actor->GetProperty ()->SetSpecular (0.8); +// lod_actor->GetProperty ()->SetLighting (0); +// lod_actor->Modified (); +// } +// +// void setPose(const Affine3f& pose) +// { +// vtkLODActor *lod_actor = vtkLODActor::SafeDownCast(actor); +// vtkSmartPointer matrix = convertToVtkMatrix(pose.matrix); +// lod_actor->SetUserMatrix (matrix); +// lod_actor->Modified (); +// } +// +// void updatePose(const Affine3f& pose) +// { +// vtkLODActor *lod_actor = vtkLODActor::SafeDownCast(actor); +// vtkSmartPointer matrix = lod_actor->GetUserMatrix(); +// if (!matrix) +// { +// setPose(pose); +// return ; +// } +// Matx44f matrix_cv = convertToMatx(matrix); +// +// Affine3f updated_pose = pose * Affine3f(matrix_cv); +// matrix = convertToVtkMatrix(updated_pose.matrix); +// +// lod_actor->SetUserMatrix (matrix); +// lod_actor->Modified (); +// } +// +// Affine3f getPose() const +// { +// vtkLODActor *lod_actor = vtkLODActor::SafeDownCast(actor); +// vtkSmartPointer matrix = lod_actor->GetUserMatrix(); +// Matx44f matrix_cv = convertToMatx(matrix); +// return Affine3f(matrix_cv); +// } +// +// protected: +// +// static vtkSmartPointer convertToVtkMatrix (const cv::Matx44f& m) +// { +// vtkSmartPointer vtk_matrix = vtkSmartPointer::New (); +// for (int i = 0; i < 4; i++) +// for (int k = 0; k < 4; k++) +// vtk_matrix->SetElement(i, k, m(i, k)); +// return vtk_matrix; +// } +// +// static cv::Matx44f convertToMatx(const vtkSmartPointer& vtk_matrix) +// { +// cv::Matx44f m; +// for (int i = 0; i < 4; i++) +// for (int k = 0; k < 4; k++) +// m(i, k) = vtk_matrix->GetElement (i, k); +// return m; +// } +// }; + /////////////////////////////////////////////////////////////////////////////////////////////// /// stream accessor implementaion @@ -94,74 +136,101 @@ vtkSmartPointer temp_viz::WidgetAccessor::getActor(const Widget& widget return widget.impl_->actor; } +void temp_viz::WidgetAccessor::setVtkProp(Widget& widget, vtkSmartPointer actor) +{ + widget.impl_->actor = actor; +} + /////////////////////////////////////////////////////////////////////////////////////////////// -/// widget implementaion +/// widget3D implementation -temp_viz::Widget::Widget() : impl_(0) +struct temp_viz::Widget3D::MatrixConverter { - create(); -} - -temp_viz::Widget::Widget(bool text_widget) : impl_(0) -{ - create(text_widget); -} - -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) -{ - if (this != &other) + static cv::Matx44f convertToMatx(const vtkSmartPointer& vtk_matrix) { - release(); - impl_ = other.impl_; - if (impl_) - CV_XADD(&impl_->ref_counter, 1); + cv::Matx44f m; + for (int i = 0; i < 4; i++) + for (int k = 0; k < 4; k++) + m(i, k) = vtk_matrix->GetElement (i, k); + return m; } - return *this; -} - -temp_viz::Widget::~Widget() -{ - release(); -} - -void temp_viz::Widget::copyTo(Widget& /*dst*/) -{ - // TODO Deep copy the data if there is any -} - -void temp_viz::Widget::setColor(const Color& color) { impl_->setColor(color); } -void temp_viz::Widget::setPose(const Affine3f& pose) { impl_->setPose(pose); } -void temp_viz::Widget::updatePose(const Affine3f& pose) { impl_->updatePose(pose); } -temp_viz::Affine3f temp_viz::Widget::getPose() const { return impl_->getPose(); } - -void temp_viz::Widget::create() -{ - if (impl_) - release(); - impl_ = new Impl(); - impl_->ref_counter = 1; -} - -void temp_viz::Widget::release() -{ - if (impl_ && CV_XADD(&impl_->ref_counter, -1) == 1) + + static vtkSmartPointer convertToVtkMatrix (const cv::Matx44f& m) { - delete impl_; - impl_ = 0; + vtkSmartPointer vtk_matrix = vtkSmartPointer::New (); + for (int i = 0; i < 4; i++) + for (int k = 0; k < 4; k++) + vtk_matrix->SetElement(i, k, m(i, k)); + return vtk_matrix; } -} +}; -void temp_viz::Widget::create(bool text_widget) +temp_viz::Widget3D::Widget3D() { - if (impl_) - release(); - impl_ = new Impl(text_widget); - impl_->ref_counter = 1; + } +void temp_viz::Widget3D::setPose(const Affine3f &pose) +{ + vtkProp3D *actor = vtkProp3D::SafeDownCast(WidgetAccessor::getActor(*this)); + CV_Assert(actor); + + vtkSmartPointer matrix = convertToVtkMatrix(pose.matrix); + actor->SetUserMatrix (matrix); + actor->Modified (); +} + +void temp_viz::Widget3D::updatePose(const Affine3f &pose) +{ + vtkProp3D *actor = vtkProp3D::SafeDownCast(WidgetAccessor::getActor(*this)); + CV_Assert(actor); + + vtkSmartPointer matrix = actor->GetUserMatrix(); + if (!matrix) + { + setPose(pose); + return ; + } + Matx44f matrix_cv = MatrixConverter::convertToMatx(matrix); + + Affine3f updated_pose = pose * Affine3f(matrix_cv); + matrix = MatrixConverter::convertToVtkMatrix(updated_pose.matrix); + + actor->SetUserMatrix (matrix); + actor->Modified (); +} + +temp_viz::Affine3f temp_viz::Widget3D::getPose() const +{ + vtkProp3D *actor = vtkProp3D::SafeDownCast(WidgetAccessor::getActor(*this)); + CV_Assert(actor); + + vtkSmartPointer matrix = actor->GetUserMatrix(); + Matx44f matrix_cv = MatrixConverter::convertToMatx(matrix); + return Affine3f(matrix_cv); +} + +void temp_viz::Widget3D::setColor(const Color &color) +{ + // Cast to actor instead of prop3d since prop3d doesn't provide getproperty + vtkActor *actor = vtkActor::SafeDownCast(WidgetAccessor::getActor(*this)); + CV_Assert(actor); + + Color c = vtkcolor(color); + actor->GetMapper ()->ScalarVisibilityOff (); + actor->GetProperty ()->SetColor (c.val); + actor->GetProperty ()->SetEdgeColor (c.val); + actor->GetProperty ()->SetAmbient (0.8); + actor->GetProperty ()->SetDiffuse (0.8); + actor->GetProperty ()->SetSpecular (0.8); + actor->GetProperty ()->SetLighting (0); + actor->Modified (); +} + +/////////////////////////////////////////////////////////////////////////////////////////////// +/// widget2D implementation + +temp_viz::Widget2D::Widget2D() +{ + +} From 195d60f4f886df758bcd608401d346d0a107af00 Mon Sep 17 00:00:00 2001 From: ozantonkal Date: Tue, 9 Jul 2013 11:07:21 +0200 Subject: [PATCH 02/12] fix widget delete bug --- modules/viz/include/opencv2/viz/widgets.hpp | 6 +- modules/viz/src/widget.cpp | 127 +++----------------- 2 files changed, 24 insertions(+), 109 deletions(-) diff --git a/modules/viz/include/opencv2/viz/widgets.hpp b/modules/viz/include/opencv2/viz/widgets.hpp index b22be40405..cc53e734f0 100644 --- a/modules/viz/include/opencv2/viz/widgets.hpp +++ b/modules/viz/include/opencv2/viz/widgets.hpp @@ -21,6 +21,8 @@ namespace temp_viz Impl *impl_; friend struct WidgetAccessor; + void create(); + void release(); }; ///////////////////////////////////////////////////////////////////////////// @@ -28,7 +30,7 @@ namespace temp_viz class CV_EXPORTS Widget3D : public Widget { public: - Widget3D(); + Widget3D() {} void setPose(const Affine3f &pose); void updatePose(const Affine3f &pose); @@ -46,7 +48,7 @@ namespace temp_viz class CV_EXPORTS Widget2D : public Widget { public: - Widget2D(); + Widget2D() {} }; diff --git a/modules/viz/src/widget.cpp b/modules/viz/src/widget.cpp index 4fed46d1cd..93dd2a68e7 100644 --- a/modules/viz/src/widget.cpp +++ b/modules/viz/src/widget.cpp @@ -7,129 +7,55 @@ class temp_viz::Widget::Impl { public: vtkSmartPointer actor; + int ref_counter; Impl() : actor(0) {} }; temp_viz::Widget::Widget() : impl_(0) { - impl_ = new Impl(); + create(); } -temp_viz::Widget::Widget(const Widget &other) : impl_(other.impl_) +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) { if (this != &other) { - delete impl_; + release(); impl_ = other.impl_; + if (impl_) CV_XADD(&impl_->ref_counter, 1); } return *this; } temp_viz::Widget::~Widget() { - if (impl_) + release(); +} + +void temp_viz::Widget::create() +{ + if (impl_) release(); + impl_ = new Impl(); + impl_->ref_counter = 1; +} + +void temp_viz::Widget::release() +{ + if (impl_ && CV_XADD(&impl_->ref_counter, -1) == 1) { delete impl_; impl_ = 0; } } - - -// class temp_viz::Widget::Impl -// { -// public: -// vtkSmartPointer actor; -// int ref_counter; -// -// Impl() : actor(vtkSmartPointer::New()) {} -// -// Impl(bool text_widget) -// { -// if (text_widget) -// actor = vtkSmartPointer::New(); -// else -// actor = vtkSmartPointer::New(); -// } -// -// void setColor(const Color& color) -// { -// vtkLODActor *lod_actor = vtkLODActor::SafeDownCast(actor); -// Color c = vtkcolor(color); -// lod_actor->GetMapper ()->ScalarVisibilityOff (); -// lod_actor->GetProperty ()->SetColor (c.val); -// lod_actor->GetProperty ()->SetEdgeColor (c.val); -// lod_actor->GetProperty ()->SetAmbient (0.8); -// lod_actor->GetProperty ()->SetDiffuse (0.8); -// lod_actor->GetProperty ()->SetSpecular (0.8); -// lod_actor->GetProperty ()->SetLighting (0); -// lod_actor->Modified (); -// } -// -// void setPose(const Affine3f& pose) -// { -// vtkLODActor *lod_actor = vtkLODActor::SafeDownCast(actor); -// vtkSmartPointer matrix = convertToVtkMatrix(pose.matrix); -// lod_actor->SetUserMatrix (matrix); -// lod_actor->Modified (); -// } -// -// void updatePose(const Affine3f& pose) -// { -// vtkLODActor *lod_actor = vtkLODActor::SafeDownCast(actor); -// vtkSmartPointer matrix = lod_actor->GetUserMatrix(); -// if (!matrix) -// { -// setPose(pose); -// return ; -// } -// Matx44f matrix_cv = convertToMatx(matrix); -// -// Affine3f updated_pose = pose * Affine3f(matrix_cv); -// matrix = convertToVtkMatrix(updated_pose.matrix); -// -// lod_actor->SetUserMatrix (matrix); -// lod_actor->Modified (); -// } -// -// Affine3f getPose() const -// { -// vtkLODActor *lod_actor = vtkLODActor::SafeDownCast(actor); -// vtkSmartPointer matrix = lod_actor->GetUserMatrix(); -// Matx44f matrix_cv = convertToMatx(matrix); -// return Affine3f(matrix_cv); -// } -// -// protected: -// -// static vtkSmartPointer convertToVtkMatrix (const cv::Matx44f& m) -// { -// vtkSmartPointer vtk_matrix = vtkSmartPointer::New (); -// for (int i = 0; i < 4; i++) -// for (int k = 0; k < 4; k++) -// vtk_matrix->SetElement(i, k, m(i, k)); -// return vtk_matrix; -// } -// -// static cv::Matx44f convertToMatx(const vtkSmartPointer& vtk_matrix) -// { -// cv::Matx44f m; -// for (int i = 0; i < 4; i++) -// for (int k = 0; k < 4; k++) -// m(i, k) = vtk_matrix->GetElement (i, k); -// return m; -// } -// }; - - /////////////////////////////////////////////////////////////////////////////////////////////// -/// stream accessor implementaion +/// widget accessor implementaion vtkSmartPointer temp_viz::WidgetAccessor::getActor(const Widget& widget) { @@ -165,11 +91,6 @@ struct temp_viz::Widget3D::MatrixConverter } }; -temp_viz::Widget3D::Widget3D() -{ - -} - void temp_viz::Widget3D::setPose(const Affine3f &pose) { vtkProp3D *actor = vtkProp3D::SafeDownCast(WidgetAccessor::getActor(*this)); @@ -226,11 +147,3 @@ void temp_viz::Widget3D::setColor(const Color &color) actor->GetProperty ()->SetLighting (0); actor->Modified (); } - -/////////////////////////////////////////////////////////////////////////////////////////////// -/// widget2D implementation - -temp_viz::Widget2D::Widget2D() -{ - -} From 318b1c00a9fd07c049907dc4b59d8c0979eb3067 Mon Sep 17 00:00:00 2001 From: ozantonkal Date: Tue, 9 Jul 2013 14:12:49 +0200 Subject: [PATCH 03/12] implement assignment operator and copy constructor for all widgets existing --- modules/viz/include/opencv2/viz/viz3d.hpp | 7 +- modules/viz/include/opencv2/viz/widgets.hpp | 28 +++++++ modules/viz/src/q/viz3d_impl.hpp | 7 +- modules/viz/src/simple_widgets.cpp | 66 ++++++++++++++++ modules/viz/src/viz3d.cpp | 17 ++-- modules/viz/src/viz3d_impl.cpp | 87 ++++++++++----------- modules/viz/src/widget.cpp | 38 ++++++++- 7 files changed, 193 insertions(+), 57 deletions(-) diff --git a/modules/viz/include/opencv2/viz/viz3d.hpp b/modules/viz/include/opencv2/viz/viz3d.hpp index 65065bc7f9..a0181cb713 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 cc53e734f0..9aea9725a6 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 6595416bbb..a49bb49227 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 8cb77ee441..82a5292720 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 ed9405e913..0460630baa 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 11982b5740..ae7190afc3 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 93dd2a68e7..419cd350a6 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 From 10d955f18b7c6af94f9556aa8bb292dd6bd3dff3 Mon Sep 17 00:00:00 2001 From: ozantonkal Date: Tue, 9 Jul 2013 14:28:55 +0200 Subject: [PATCH 04/12] widget2d setcolor implementation --- modules/viz/include/opencv2/viz/widgets.hpp | 2 ++ modules/viz/src/widget.cpp | 9 +++++++++ 2 files changed, 11 insertions(+) diff --git a/modules/viz/include/opencv2/viz/widgets.hpp b/modules/viz/include/opencv2/viz/widgets.hpp index 9aea9725a6..d70b763402 100644 --- a/modules/viz/include/opencv2/viz/widgets.hpp +++ b/modules/viz/include/opencv2/viz/widgets.hpp @@ -53,6 +53,8 @@ namespace temp_viz Widget2D() {} Widget2D(const Widget &other); Widget2D& operator=(const Widget &other); + + void setColor(const Color &color); }; diff --git a/modules/viz/src/widget.cpp b/modules/viz/src/widget.cpp index 419cd350a6..e7f346df4e 100644 --- a/modules/viz/src/widget.cpp +++ b/modules/viz/src/widget.cpp @@ -182,4 +182,13 @@ temp_viz::Widget2D& temp_viz::Widget2D::operator=(const Widget &other) CV_Assert(actor); Widget::operator=(other); return *this; +} + +void temp_viz::Widget2D::setColor(const Color &color) +{ + vtkActor2D *actor = vtkActor2D::SafeDownCast(WidgetAccessor::getActor(*this)); + CV_Assert(actor); + Color c = vtkcolor(color); + actor->GetProperty ()->SetColor (c.val); + actor->Modified (); } \ No newline at end of file From 200b254badd774337d969264f16616759e9f30eb Mon Sep 17 00:00:00 2001 From: ozantonkal Date: Tue, 9 Jul 2013 15:18:44 +0200 Subject: [PATCH 05/12] rearrange widget constructors --- .../include/opencv2/viz/widget_accessor.hpp | 4 +- modules/viz/include/opencv2/viz/widgets.hpp | 3 + modules/viz/src/simple_widgets.cpp | 98 ++++++++++--------- modules/viz/src/viz3d_impl.cpp | 8 +- modules/viz/src/widget.cpp | 22 ++--- 5 files changed, 72 insertions(+), 63 deletions(-) diff --git a/modules/viz/include/opencv2/viz/widget_accessor.hpp b/modules/viz/include/opencv2/viz/widget_accessor.hpp index ec2a020309..ddfd61b1c5 100644 --- a/modules/viz/include/opencv2/viz/widget_accessor.hpp +++ b/modules/viz/include/opencv2/viz/widget_accessor.hpp @@ -12,7 +12,7 @@ namespace temp_viz //It is indended for those users who want to develop own widgets system using VTK library API. struct CV_EXPORTS WidgetAccessor { - static vtkSmartPointer getActor(const Widget &widget); - static void setVtkProp(Widget &widget, vtkSmartPointer actor); + static vtkSmartPointer getProp(const Widget &widget); + static void setProp(Widget &widget, vtkSmartPointer actor); }; } diff --git a/modules/viz/include/opencv2/viz/widgets.hpp b/modules/viz/include/opencv2/viz/widgets.hpp index d70b763402..23eb7b71ca 100644 --- a/modules/viz/include/opencv2/viz/widgets.hpp +++ b/modules/viz/include/opencv2/viz/widgets.hpp @@ -132,6 +132,9 @@ namespace temp_viz 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); + + void setText(const String &text); + String getText() const; }; class CV_EXPORTS CloudWidget : public Widget3D diff --git a/modules/viz/src/simple_widgets.cpp b/modules/viz/src/simple_widgets.cpp index 82a5292720..d1fab75d21 100644 --- a/modules/viz/src/simple_widgets.cpp +++ b/modules/viz/src/simple_widgets.cpp @@ -8,9 +8,7 @@ namespace temp_viz /////////////////////////////////////////////////////////////////////////////////////////////// /// line widget implementation temp_viz::LineWidget::LineWidget(const Point3f &pt1, const Point3f &pt2, const Color &color) -{ - WidgetAccessor::setVtkProp(*this, vtkSmartPointer::New()); - +{ vtkSmartPointer line = vtkSmartPointer::New(); line->SetPoint1 (pt1.x, pt1.y, pt1.z); line->SetPoint2 (pt2.x, pt2.y, pt2.z); @@ -19,9 +17,10 @@ temp_viz::LineWidget::LineWidget(const Point3f &pt1, const Point3f &pt2, const C vtkSmartPointer mapper = vtkSmartPointer::New (); mapper->SetInput(line->GetOutput ()); - vtkLODActor *actor = vtkLODActor::SafeDownCast(WidgetAccessor::getActor(*this)); + vtkSmartPointer actor = vtkSmartPointer::New(); actor->SetMapper(mapper); + WidgetAccessor::setProp(*this, actor); setColor(color); } @@ -33,13 +32,15 @@ temp_viz::LineWidget& temp_viz::LineWidget::operator=(const Widget &other) void temp_viz::LineWidget::setLineWidth(float line_width) { - vtkLODActor *actor = vtkLODActor::SafeDownCast(WidgetAccessor::getActor(*this)); + vtkLODActor *actor = vtkLODActor::SafeDownCast(WidgetAccessor::getProp(*this)); + CV_Assert(actor); actor->GetProperty()->SetLineWidth(line_width); } float temp_viz::LineWidget::getLineWidth() { - vtkLODActor *actor = vtkLODActor::SafeDownCast(WidgetAccessor::getActor(*this)); + vtkLODActor *actor = vtkLODActor::SafeDownCast(WidgetAccessor::getProp(*this)); + CV_Assert(actor); return actor->GetProperty()->GetLineWidth(); } @@ -47,9 +48,7 @@ float temp_viz::LineWidget::getLineWidth() /// plane widget implementation temp_viz::PlaneWidget::PlaneWidget(const Vec4f& coefs, double size, const Color &color) -{ - WidgetAccessor::setVtkProp(*this, vtkSmartPointer::New()); - +{ vtkSmartPointer plane = vtkSmartPointer::New (); plane->SetNormal (coefs[0], coefs[1], coefs[2]); double norm = cv::norm(cv::Vec3f(coefs.val)); @@ -58,17 +57,16 @@ temp_viz::PlaneWidget::PlaneWidget(const Vec4f& coefs, double size, const Color vtkSmartPointer mapper = vtkSmartPointer::New (); mapper->SetInput(plane->GetOutput ()); - vtkLODActor *actor = vtkLODActor::SafeDownCast(WidgetAccessor::getActor(*this)); + vtkSmartPointer actor = vtkSmartPointer::New(); actor->SetMapper(mapper); actor->SetScale(size); + WidgetAccessor::setProp(*this, actor); setColor(color); } temp_viz::PlaneWidget::PlaneWidget(const Vec4f& coefs, const Point3f& pt, double size, const Color &color) { - WidgetAccessor::setVtkProp(*this, vtkSmartPointer::New()); - vtkSmartPointer plane = vtkSmartPointer::New (); cv::Point3f coefs3(coefs[0], coefs[1], coefs[2]); double norm_sqr = 1.0 / coefs3.dot (coefs3); @@ -81,10 +79,11 @@ temp_viz::PlaneWidget::PlaneWidget(const Vec4f& coefs, const Point3f& pt, double vtkSmartPointer mapper = vtkSmartPointer::New (); mapper->SetInput(plane->GetOutput ()); - vtkLODActor *actor = vtkLODActor::SafeDownCast(WidgetAccessor::getActor(*this)); + vtkSmartPointer actor = vtkSmartPointer::New(); actor->SetMapper(mapper); actor->SetScale(size); - + + WidgetAccessor::setProp(*this, actor); setColor(color); } @@ -99,8 +98,6 @@ temp_viz::PlaneWidget& temp_viz::PlaneWidget::operator=(const Widget& other) temp_viz::SphereWidget::SphereWidget(const cv::Point3f ¢er, float radius, int sphere_resolution, const Color &color) { - WidgetAccessor::setVtkProp(*this, vtkSmartPointer::New()); - vtkSmartPointer sphere = vtkSmartPointer::New (); sphere->SetRadius (radius); sphere->SetCenter (center.x, center.y, center.z); @@ -112,9 +109,10 @@ temp_viz::SphereWidget::SphereWidget(const cv::Point3f ¢er, float radius, in vtkSmartPointer mapper = vtkSmartPointer::New (); mapper->SetInput(sphere->GetOutput ()); - vtkLODActor *actor = vtkLODActor::SafeDownCast(WidgetAccessor::getActor(*this)); + vtkSmartPointer actor = vtkSmartPointer::New(); actor->SetMapper(mapper); + WidgetAccessor::setProp(*this, actor); setColor(color); } @@ -129,8 +127,6 @@ temp_viz::SphereWidget& temp_viz::SphereWidget::operator=(const Widget &other) temp_viz::ArrowWidget::ArrowWidget(const Point3f& pt1, const Point3f& pt2, const Color &color) { - WidgetAccessor::setVtkProp(*this, vtkSmartPointer::New()); - vtkSmartPointer arrowSource = vtkSmartPointer::New (); float startPoint[3], endPoint[3]; @@ -182,9 +178,10 @@ temp_viz::ArrowWidget::ArrowWidget(const Point3f& pt1, const Point3f& pt2, const vtkSmartPointer mapper = vtkSmartPointer::New (); mapper->SetInput(transformPD->GetOutput ()); - vtkLODActor *actor = vtkLODActor::SafeDownCast(WidgetAccessor::getActor(*this)); + vtkSmartPointer actor = vtkSmartPointer::New(); actor->SetMapper(mapper); + WidgetAccessor::setProp(*this, actor); setColor(color); } @@ -199,8 +196,6 @@ temp_viz::ArrowWidget& temp_viz::ArrowWidget::operator=(const Widget &other) temp_viz::CircleWidget::CircleWidget(const temp_viz::Point3f& pt, double radius, double thickness, const temp_viz::Color& color) { - WidgetAccessor::setVtkProp(*this, vtkSmartPointer::New()); - vtkSmartPointer disk = vtkSmartPointer::New (); // Maybe the resolution should be lower e.g. 50 or 25 disk->SetCircumferentialResolution (50); @@ -219,9 +214,10 @@ temp_viz::CircleWidget::CircleWidget(const temp_viz::Point3f& pt, double radius, vtkSmartPointer mapper = vtkSmartPointer::New (); mapper->SetInput(tf->GetOutput ()); - vtkLODActor *actor = vtkLODActor::SafeDownCast(WidgetAccessor::getActor(*this)); + vtkSmartPointer actor = vtkSmartPointer::New(); actor->SetMapper(mapper); + WidgetAccessor::setProp(*this, actor); setColor(color); } @@ -235,9 +231,7 @@ temp_viz::CircleWidget& temp_viz::CircleWidget::operator=(const Widget &other) /// cylinder widget implementation temp_viz::CylinderWidget::CylinderWidget(const Point3f& pt_on_axis, const Point3f& axis_direction, double radius, int numsides, const Color &color) -{ - WidgetAccessor::setVtkProp(*this, vtkSmartPointer::New()); - +{ const cv::Point3f pt2 = pt_on_axis + axis_direction; vtkSmartPointer line = vtkSmartPointer::New (); line->SetPoint1 (pt_on_axis.x, pt_on_axis.y, pt_on_axis.z); @@ -251,9 +245,10 @@ temp_viz::CylinderWidget::CylinderWidget(const Point3f& pt_on_axis, const Point3 vtkSmartPointer mapper = vtkSmartPointer::New (); mapper->SetInput(tuber->GetOutput ()); - vtkLODActor *actor = vtkLODActor::SafeDownCast(WidgetAccessor::getActor(*this)); + vtkSmartPointer actor = vtkSmartPointer::New (); actor->SetMapper(mapper); + WidgetAccessor::setProp(*this, actor); setColor(color); } @@ -268,20 +263,19 @@ temp_viz::CylinderWidget& temp_viz::CylinderWidget::operator=(const Widget &othe temp_viz::CubeWidget::CubeWidget(const Point3f& pt_min, const Point3f& pt_max, bool wire_frame, const Color &color) { - WidgetAccessor::setVtkProp(*this, vtkSmartPointer::New()); - vtkSmartPointer cube = vtkSmartPointer::New (); cube->SetBounds (pt_min.x, pt_max.x, pt_min.y, pt_max.y, pt_min.z, pt_max.z); vtkSmartPointer mapper = vtkSmartPointer::New (); mapper->SetInput(cube->GetOutput ()); - vtkLODActor *actor = vtkLODActor::SafeDownCast(WidgetAccessor::getActor(*this)); + vtkSmartPointer actor = vtkSmartPointer::New(); actor->SetMapper(mapper); if (wire_frame) actor->GetProperty ()->SetRepresentationToWireframe (); + WidgetAccessor::setProp(*this, actor); setColor(color); } @@ -296,8 +290,6 @@ temp_viz::CubeWidget& temp_viz::CubeWidget::operator=(const Widget &other) temp_viz::CoordinateSystemWidget::CoordinateSystemWidget(double scale, const Affine3f& affine) { - WidgetAccessor::setVtkProp(*this, vtkSmartPointer::New()); - vtkSmartPointer axes = vtkSmartPointer::New (); axes->SetOrigin (0, 0, 0); axes->SetScaleFactor (scale); @@ -324,7 +316,7 @@ temp_viz::CoordinateSystemWidget::CoordinateSystemWidget(double scale, const Aff mapper->SetScalarModeToUsePointData (); mapper->SetInput(axes_tubes->GetOutput ()); - vtkLODActor *actor = vtkLODActor::SafeDownCast(WidgetAccessor::getActor(*this)); + vtkSmartPointer actor = vtkSmartPointer::New(); actor->SetMapper(mapper); cv::Vec3d t = affine.translation(); @@ -340,6 +332,8 @@ temp_viz::CoordinateSystemWidget::CoordinateSystemWidget(double scale, const Aff actor->SetOrientation(0,0,0); actor->RotateWXYZ(r_angle*180/CV_PI,rvec[0], rvec[1], rvec[2]); + + WidgetAccessor::setProp(*this, actor); } temp_viz::CoordinateSystemWidget& temp_viz::CoordinateSystemWidget::operator=(const Widget &other) @@ -353,9 +347,7 @@ temp_viz::CoordinateSystemWidget& temp_viz::CoordinateSystemWidget::operator=(co temp_viz::TextWidget::TextWidget(const String &text, const Point2i &pos, int font_size, const Color &color) { - WidgetAccessor::setVtkProp(*this, vtkSmartPointer::New()); - - vtkTextActor *actor = vtkTextActor::SafeDownCast(WidgetAccessor::getActor(*this)); + vtkSmartPointer actor = vtkSmartPointer::New(); actor->SetPosition (pos.x, pos.y); actor->SetInput (text.c_str ()); @@ -367,6 +359,8 @@ temp_viz::TextWidget::TextWidget(const String &text, const Point2i &pos, int fon Color c = vtkcolor(color); tprop->SetColor (c.val); + + WidgetAccessor::setProp(*this, actor); } temp_viz::TextWidget& temp_viz::TextWidget::operator=(const Widget &other) @@ -375,6 +369,20 @@ temp_viz::TextWidget& temp_viz::TextWidget::operator=(const Widget &other) return *this; } +void temp_viz::TextWidget::setText(const String &text) +{ + vtkTextActor *actor = vtkTextActor::SafeDownCast(WidgetAccessor::getProp(*this)); + CV_Assert(actor); + actor->SetInput(text.c_str()); +} + +temp_viz::String temp_viz::TextWidget::getText() const +{ + vtkTextActor *actor = vtkTextActor::SafeDownCast(WidgetAccessor::getProp(*this)); + CV_Assert(actor); + return actor->GetInput(); +} + /////////////////////////////////////////////////////////////////////////////////////////////// /// point cloud widget implementation @@ -474,9 +482,7 @@ temp_viz::CloudWidget::CloudWidget(InputArray _cloud, InputArray _colors) CV_Assert(cloud.type() == CV_32FC3 || cloud.type() == CV_64FC3 || cloud.type() == CV_32FC4 || cloud.type() == CV_64FC4); CV_Assert(colors.type() == CV_8UC3 && cloud.size() == colors.size()); - WidgetAccessor::setVtkProp(*this, vtkSmartPointer::New()); - - vtkLODActor * actor = vtkLODActor::SafeDownCast(WidgetAccessor::getActor(*this)); + vtkSmartPointer actor = vtkSmartPointer::New(); vtkIdType nr_points; vtkSmartPointer polydata = CreateCloudWidget::create(cloud, nr_points); @@ -510,6 +516,8 @@ temp_viz::CloudWidget::CloudWidget(InputArray _cloud, InputArray _colors) actor->GetProperty ()->SetInterpolationToFlat (); actor->GetProperty ()->BackfaceCullingOn (); actor->SetMapper (mapper); + + WidgetAccessor::setProp(*this, actor); } temp_viz::CloudWidget::CloudWidget(InputArray _cloud, const Color &color) @@ -517,9 +525,7 @@ temp_viz::CloudWidget::CloudWidget(InputArray _cloud, const Color &color) Mat cloud = _cloud.getMat(); CV_Assert(cloud.type() == CV_32FC3 || cloud.type() == CV_64FC3 || cloud.type() == CV_32FC4 || cloud.type() == CV_64FC4); - WidgetAccessor::setVtkProp(*this, vtkSmartPointer::New()); - - vtkLODActor * actor = vtkLODActor::SafeDownCast(WidgetAccessor::getActor(*this)); + vtkSmartPointer actor = vtkSmartPointer::New(); vtkIdType nr_points; vtkSmartPointer polydata = CreateCloudWidget::create(cloud, nr_points); @@ -538,6 +544,7 @@ temp_viz::CloudWidget::CloudWidget(InputArray _cloud, const Color &color) actor->GetProperty ()->BackfaceCullingOn (); actor->SetMapper (mapper); + WidgetAccessor::setProp(*this, actor); setColor(color); } @@ -634,9 +641,7 @@ temp_viz::CloudNormalsWidget::CloudNormalsWidget(InputArray _cloud, InputArray _ Mat normals = _normals.getMat(); CV_Assert(cloud.type() == CV_32FC3 || cloud.type() == CV_64FC3 || cloud.type() == CV_32FC4 || cloud.type() == CV_64FC4); CV_Assert(cloud.size() == normals.size() && cloud.type() == normals.type()); - - WidgetAccessor::setVtkProp(*this, vtkSmartPointer::New()); - + vtkSmartPointer points = vtkSmartPointer::New(); vtkSmartPointer lines = vtkSmartPointer::New(); vtkIdType nr_normals = 0; @@ -675,8 +680,9 @@ temp_viz::CloudNormalsWidget::CloudNormalsWidget(InputArray _cloud, InputArray _ mapper->SetColorModeToMapScalars(); mapper->SetScalarModeToUsePointData(); - vtkLODActor * actor = vtkLODActor::SafeDownCast(WidgetAccessor::getActor(*this)); + vtkSmartPointer actor = vtkSmartPointer::New(); actor->SetMapper(mapper); + WidgetAccessor::setProp(*this, actor); setColor(color); } diff --git a/modules/viz/src/viz3d_impl.cpp b/modules/viz/src/viz3d_impl.cpp index ae7190afc3..dd94dc343d 100644 --- a/modules/viz/src/viz3d_impl.cpp +++ b/modules/viz/src/viz3d_impl.cpp @@ -874,7 +874,7 @@ 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 - vtkProp3D *actor = vtkProp3D::SafeDownCast(WidgetAccessor::getActor(widget)); + vtkProp3D *actor = vtkProp3D::SafeDownCast(WidgetAccessor::getProp(widget)); if (actor) { // If the actor is 3D, apply pose @@ -882,8 +882,8 @@ void temp_viz::Viz3d::VizImpl::showWidget(const String &id, const Widget &widget actor->SetUserMatrix (matrix); actor->Modified(); } - renderer_->AddActor(WidgetAccessor::getActor(widget)); - (*widget_actor_map_)[id].actor = WidgetAccessor::getActor(widget); + renderer_->AddActor(WidgetAccessor::getProp(widget)); + (*widget_actor_map_)[id].actor = WidgetAccessor::getProp(widget); } void temp_viz::Viz3d::VizImpl::removeWidget(const String &id) @@ -902,7 +902,7 @@ temp_viz::Widget temp_viz::Viz3d::VizImpl::getWidget(const String &id) const CV_Assert(exists); Widget widget; - WidgetAccessor::setVtkProp(widget, wam_itr->second.actor); + WidgetAccessor::setProp(widget, wam_itr->second.actor); return widget; } diff --git a/modules/viz/src/widget.cpp b/modules/viz/src/widget.cpp index e7f346df4e..b832d15df3 100644 --- a/modules/viz/src/widget.cpp +++ b/modules/viz/src/widget.cpp @@ -57,12 +57,12 @@ void temp_viz::Widget::release() /////////////////////////////////////////////////////////////////////////////////////////////// /// widget accessor implementaion -vtkSmartPointer temp_viz::WidgetAccessor::getActor(const Widget& widget) +vtkSmartPointer temp_viz::WidgetAccessor::getProp(const Widget& widget) { return widget.impl_->actor; } -void temp_viz::WidgetAccessor::setVtkProp(Widget& widget, vtkSmartPointer actor) +void temp_viz::WidgetAccessor::setProp(Widget& widget, vtkSmartPointer actor) { widget.impl_->actor = actor; } @@ -94,14 +94,14 @@ 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)); + vtkProp3D *actor = vtkProp3D::SafeDownCast(WidgetAccessor::getProp(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)); + vtkProp3D *actor = vtkProp3D::SafeDownCast(WidgetAccessor::getProp(other)); CV_Assert(actor); Widget::operator=(other); @@ -110,7 +110,7 @@ temp_viz::Widget3D& temp_viz::Widget3D::operator =(const Widget &other) void temp_viz::Widget3D::setPose(const Affine3f &pose) { - vtkProp3D *actor = vtkProp3D::SafeDownCast(WidgetAccessor::getActor(*this)); + vtkProp3D *actor = vtkProp3D::SafeDownCast(WidgetAccessor::getProp(*this)); CV_Assert(actor); vtkSmartPointer matrix = convertToVtkMatrix(pose.matrix); @@ -120,7 +120,7 @@ void temp_viz::Widget3D::setPose(const Affine3f &pose) void temp_viz::Widget3D::updatePose(const Affine3f &pose) { - vtkProp3D *actor = vtkProp3D::SafeDownCast(WidgetAccessor::getActor(*this)); + vtkProp3D *actor = vtkProp3D::SafeDownCast(WidgetAccessor::getProp(*this)); CV_Assert(actor); vtkSmartPointer matrix = actor->GetUserMatrix(); @@ -140,7 +140,7 @@ void temp_viz::Widget3D::updatePose(const Affine3f &pose) temp_viz::Affine3f temp_viz::Widget3D::getPose() const { - vtkProp3D *actor = vtkProp3D::SafeDownCast(WidgetAccessor::getActor(*this)); + vtkProp3D *actor = vtkProp3D::SafeDownCast(WidgetAccessor::getProp(*this)); CV_Assert(actor); vtkSmartPointer matrix = actor->GetUserMatrix(); @@ -151,7 +151,7 @@ temp_viz::Affine3f temp_viz::Widget3D::getPose() const void temp_viz::Widget3D::setColor(const Color &color) { // Cast to actor instead of prop3d since prop3d doesn't provide getproperty - vtkActor *actor = vtkActor::SafeDownCast(WidgetAccessor::getActor(*this)); + vtkActor *actor = vtkActor::SafeDownCast(WidgetAccessor::getProp(*this)); CV_Assert(actor); Color c = vtkcolor(color); @@ -171,14 +171,14 @@ void temp_viz::Widget3D::setColor(const Color &color) 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)); + vtkActor2D *actor = vtkActor2D::SafeDownCast(WidgetAccessor::getProp(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)); + vtkActor2D *actor = vtkActor2D::SafeDownCast(WidgetAccessor::getProp(other)); CV_Assert(actor); Widget::operator=(other); return *this; @@ -186,7 +186,7 @@ temp_viz::Widget2D& temp_viz::Widget2D::operator=(const Widget &other) void temp_viz::Widget2D::setColor(const Color &color) { - vtkActor2D *actor = vtkActor2D::SafeDownCast(WidgetAccessor::getActor(*this)); + vtkActor2D *actor = vtkActor2D::SafeDownCast(WidgetAccessor::getProp(*this)); CV_Assert(actor); Color c = vtkcolor(color); actor->GetProperty ()->SetColor (c.val); From 509a93c7ce73cb9697b7c19d9aca6ee02e210ea8 Mon Sep 17 00:00:00 2001 From: Anatoly Baksheev Date: Tue, 9 Jul 2013 19:35:26 +0400 Subject: [PATCH 06/12] minor naming --- modules/viz/include/opencv2/viz/widget_accessor.hpp | 4 ++-- modules/viz/include/opencv2/viz/widgets.hpp | 1 - modules/viz/src/widget.cpp | 12 ++++++------ 3 files changed, 8 insertions(+), 9 deletions(-) diff --git a/modules/viz/include/opencv2/viz/widget_accessor.hpp b/modules/viz/include/opencv2/viz/widget_accessor.hpp index ddfd61b1c5..a114c3a263 100644 --- a/modules/viz/include/opencv2/viz/widget_accessor.hpp +++ b/modules/viz/include/opencv2/viz/widget_accessor.hpp @@ -2,7 +2,7 @@ #include #include -#include +#include namespace temp_viz { @@ -13,6 +13,6 @@ namespace temp_viz struct CV_EXPORTS WidgetAccessor { static vtkSmartPointer getProp(const Widget &widget); - static void setProp(Widget &widget, vtkSmartPointer actor); + static void setProp(Widget &widget, vtkSmartPointer prop); }; } diff --git a/modules/viz/include/opencv2/viz/widgets.hpp b/modules/viz/include/opencv2/viz/widgets.hpp index 23eb7b71ca..2114838c56 100644 --- a/modules/viz/include/opencv2/viz/widgets.hpp +++ b/modules/viz/include/opencv2/viz/widgets.hpp @@ -15,7 +15,6 @@ namespace temp_viz Widget& operator =(const Widget &other); ~Widget(); - private: class Impl; Impl *impl_; diff --git a/modules/viz/src/widget.cpp b/modules/viz/src/widget.cpp index b832d15df3..450e766339 100644 --- a/modules/viz/src/widget.cpp +++ b/modules/viz/src/widget.cpp @@ -6,10 +6,10 @@ class temp_viz::Widget::Impl { public: - vtkSmartPointer actor; + vtkSmartPointer prop; int ref_counter; - Impl() : actor(0) {} + Impl() : prop(0) {} }; temp_viz::Widget::Widget() : impl_(0) @@ -59,12 +59,12 @@ void temp_viz::Widget::release() vtkSmartPointer temp_viz::WidgetAccessor::getProp(const Widget& widget) { - return widget.impl_->actor; + return widget.impl_->prop; } -void temp_viz::WidgetAccessor::setProp(Widget& widget, vtkSmartPointer actor) +void temp_viz::WidgetAccessor::setProp(Widget& widget, vtkSmartPointer prop) { - widget.impl_->actor = actor; + widget.impl_->prop = prop; } /////////////////////////////////////////////////////////////////////////////////////////////// @@ -191,4 +191,4 @@ void temp_viz::Widget2D::setColor(const Color &color) Color c = vtkcolor(color); actor->GetProperty ()->SetColor (c.val); actor->Modified (); -} \ No newline at end of file +} From e4c3416f5885d44ae69e2e022279ffbc96c636ae Mon Sep 17 00:00:00 2001 From: Anatoly Baksheev Date: Tue, 9 Jul 2013 20:02:43 +0400 Subject: [PATCH 07/12] experimental widget casting functionality --- modules/viz/include/opencv2/viz/widgets.hpp | 13 +++++++++---- modules/viz/src/simple_widgets.cpp | 14 +++++++------- modules/viz/src/widget.cpp | 10 ++++++++++ 3 files changed, 26 insertions(+), 11 deletions(-) diff --git a/modules/viz/include/opencv2/viz/widgets.hpp b/modules/viz/include/opencv2/viz/widgets.hpp index 2114838c56..c61b5a745f 100644 --- a/modules/viz/include/opencv2/viz/widgets.hpp +++ b/modules/viz/include/opencv2/viz/widgets.hpp @@ -15,6 +15,8 @@ namespace temp_viz Widget& operator =(const Widget &other); ~Widget(); + + template _W cast(); private: class Impl; Impl *impl_; @@ -56,18 +58,15 @@ namespace temp_viz void setColor(const Color &color); }; - class CV_EXPORTS LineWidget : public Widget3D { 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(); }; - + class CV_EXPORTS PlaneWidget : public Widget3D { public: @@ -158,4 +157,10 @@ namespace temp_viz private: struct ApplyCloudNormals; }; + + template<> CV_EXPORTS Widget3D Widget::cast(); + template<> CV_EXPORTS LineWidget Widget::cast(); } + + + diff --git a/modules/viz/src/simple_widgets.cpp b/modules/viz/src/simple_widgets.cpp index d1fab75d21..1d5b0a9e98 100644 --- a/modules/viz/src/simple_widgets.cpp +++ b/modules/viz/src/simple_widgets.cpp @@ -24,12 +24,6 @@ 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::getProp(*this)); @@ -44,6 +38,12 @@ float temp_viz::LineWidget::getLineWidth() return actor->GetProperty()->GetLineWidth(); } +template<> temp_viz::LineWidget temp_viz::Widget::cast() +{ + Widget3D widget = this->cast(); + return static_cast(widget); +} + /////////////////////////////////////////////////////////////////////////////////////////////// /// plane widget implementation @@ -690,4 +690,4 @@ temp_viz::CloudNormalsWidget& temp_viz::CloudNormalsWidget::operator=(const Widg { Widget3D::operator=(other); return *this; -} \ No newline at end of file +} diff --git a/modules/viz/src/widget.cpp b/modules/viz/src/widget.cpp index 450e766339..cb415ce827 100644 --- a/modules/viz/src/widget.cpp +++ b/modules/viz/src/widget.cpp @@ -165,6 +165,16 @@ void temp_viz::Widget3D::setColor(const Color &color) actor->Modified (); } +template<> temp_viz::Widget3D temp_viz::Widget::cast() +{ + vtkProp3D *actor = vtkProp3D::SafeDownCast(WidgetAccessor::getProp(*this)); + CV_Assert(actor); + + Widget3D widget; + WidgetAccessor::setProp(widget, actor); + return widget; +} + /////////////////////////////////////////////////////////////////////////////////////////////// /// widget2D implementation From d3dee3a2fe541bd0424f08cd2aa62e454ef82333 Mon Sep 17 00:00:00 2001 From: ozantonkal Date: Wed, 10 Jul 2013 09:55:42 +0200 Subject: [PATCH 08/12] explicit casting among widgets, removed implicit casting --- modules/viz/include/opencv2/viz/widgets.hpp | 35 ++++-------- modules/viz/src/simple_widgets.cpp | 60 ++++++++++----------- modules/viz/src/widget.cpp | 43 ++++----------- 3 files changed, 51 insertions(+), 87 deletions(-) diff --git a/modules/viz/include/opencv2/viz/widgets.hpp b/modules/viz/include/opencv2/viz/widgets.hpp index c61b5a745f..cf265ac483 100644 --- a/modules/viz/include/opencv2/viz/widgets.hpp +++ b/modules/viz/include/opencv2/viz/widgets.hpp @@ -32,8 +32,6 @@ namespace temp_viz { public: Widget3D() {} - Widget3D(const Widget& other); - Widget3D& operator =(const Widget &other); void setPose(const Affine3f &pose); void updatePose(const Affine3f &pose); @@ -52,8 +50,6 @@ namespace temp_viz { public: Widget2D() {} - Widget2D(const Widget &other); - Widget2D& operator=(const Widget &other); void setColor(const Color &color); }; @@ -72,64 +68,48 @@ 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); void setText(const String &text); String getText() const; @@ -140,8 +120,6 @@ 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; @@ -151,15 +129,24 @@ 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; }; + template<> CV_EXPORTS Widget2D Widget::cast(); template<> CV_EXPORTS Widget3D Widget::cast(); template<> CV_EXPORTS LineWidget Widget::cast(); + template<> CV_EXPORTS PlaneWidget Widget::cast(); + template<> CV_EXPORTS SphereWidget Widget::cast(); + template<> CV_EXPORTS CylinderWidget Widget::cast(); + template<> CV_EXPORTS ArrowWidget Widget::cast(); + template<> CV_EXPORTS CircleWidget Widget::cast(); + template<> CV_EXPORTS CubeWidget Widget::cast(); + template<> CV_EXPORTS CoordinateSystemWidget Widget::cast(); + template<> CV_EXPORTS TextWidget Widget::cast(); + template<> CV_EXPORTS CloudWidget Widget::cast(); + template<> CV_EXPORTS CloudNormalsWidget Widget::cast(); } diff --git a/modules/viz/src/simple_widgets.cpp b/modules/viz/src/simple_widgets.cpp index 1d5b0a9e98..db27884e87 100644 --- a/modules/viz/src/simple_widgets.cpp +++ b/modules/viz/src/simple_widgets.cpp @@ -87,10 +87,10 @@ temp_viz::PlaneWidget::PlaneWidget(const Vec4f& coefs, const Point3f& pt, double setColor(color); } -temp_viz::PlaneWidget& temp_viz::PlaneWidget::operator=(const Widget& other) +template<> temp_viz::PlaneWidget temp_viz::Widget::cast() { - Widget3D::operator=(other); - return *this; + Widget3D widget = this->cast(); + return static_cast(widget); } /////////////////////////////////////////////////////////////////////////////////////////////// @@ -116,10 +116,10 @@ temp_viz::SphereWidget::SphereWidget(const cv::Point3f ¢er, float radius, in setColor(color); } -temp_viz::SphereWidget& temp_viz::SphereWidget::operator=(const Widget &other) +template<> temp_viz::SphereWidget temp_viz::Widget::cast() { - Widget3D::operator=(other); - return *this; + Widget3D widget = this->cast(); + return static_cast(widget); } /////////////////////////////////////////////////////////////////////////////////////////////// @@ -185,10 +185,10 @@ temp_viz::ArrowWidget::ArrowWidget(const Point3f& pt1, const Point3f& pt2, const setColor(color); } -temp_viz::ArrowWidget& temp_viz::ArrowWidget::operator=(const Widget &other) +template<> temp_viz::ArrowWidget temp_viz::Widget::cast() { - Widget3D::operator=(other); - return *this; + Widget3D widget = this->cast(); + return static_cast(widget); } /////////////////////////////////////////////////////////////////////////////////////////////// @@ -221,10 +221,10 @@ temp_viz::CircleWidget::CircleWidget(const temp_viz::Point3f& pt, double radius, setColor(color); } -temp_viz::CircleWidget& temp_viz::CircleWidget::operator=(const Widget &other) +template<> temp_viz::CircleWidget temp_viz::Widget::cast() { - Widget3D::operator=(other); - return *this; + Widget3D widget = this->cast(); + return static_cast(widget); } /////////////////////////////////////////////////////////////////////////////////////////////// @@ -252,10 +252,10 @@ temp_viz::CylinderWidget::CylinderWidget(const Point3f& pt_on_axis, const Point3 setColor(color); } -temp_viz::CylinderWidget& temp_viz::CylinderWidget::operator=(const Widget &other) +template<> temp_viz::CylinderWidget temp_viz::Widget::cast() { - Widget3D::operator=(other); - return *this; + Widget3D widget = this->cast(); + return static_cast(widget); } /////////////////////////////////////////////////////////////////////////////////////////////// @@ -279,10 +279,10 @@ 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) +template<> temp_viz::CubeWidget temp_viz::Widget::cast() { - Widget3D::operator=(other); - return *this; + Widget3D widget = this->cast(); + return static_cast(widget); } /////////////////////////////////////////////////////////////////////////////////////////////// @@ -336,10 +336,10 @@ temp_viz::CoordinateSystemWidget::CoordinateSystemWidget(double scale, const Aff WidgetAccessor::setProp(*this, actor); } -temp_viz::CoordinateSystemWidget& temp_viz::CoordinateSystemWidget::operator=(const Widget &other) +template<> temp_viz::CoordinateSystemWidget temp_viz::Widget::cast() { - Widget3D::operator=(other); - return *this; + Widget3D widget = this->cast(); + return static_cast(widget); } /////////////////////////////////////////////////////////////////////////////////////////////// @@ -363,10 +363,10 @@ temp_viz::TextWidget::TextWidget(const String &text, const Point2i &pos, int fon WidgetAccessor::setProp(*this, actor); } -temp_viz::TextWidget& temp_viz::TextWidget::operator=(const Widget &other) +template<> temp_viz::TextWidget temp_viz::Widget::cast() { - Widget2D::operator=(other); - return *this; + Widget2D widget = this->cast(); + return static_cast(widget); } void temp_viz::TextWidget::setText(const String &text) @@ -548,10 +548,10 @@ temp_viz::CloudWidget::CloudWidget(InputArray _cloud, const Color &color) setColor(color); } -temp_viz::CloudWidget& temp_viz::CloudWidget::operator=(const Widget &other) +template<> temp_viz::CloudWidget temp_viz::Widget::cast() { - Widget3D::operator=(other); - return *this; + Widget3D widget = this->cast(); + return static_cast(widget); } /////////////////////////////////////////////////////////////////////////////////////////////// @@ -686,8 +686,8 @@ temp_viz::CloudNormalsWidget::CloudNormalsWidget(InputArray _cloud, InputArray _ setColor(color); } -temp_viz::CloudNormalsWidget& temp_viz::CloudNormalsWidget::operator=(const Widget &other) +template<> temp_viz::CloudNormalsWidget temp_viz::Widget::cast() { - Widget3D::operator=(other); - return *this; + Widget3D widget = this->cast(); + return static_cast(widget); } diff --git a/modules/viz/src/widget.cpp b/modules/viz/src/widget.cpp index cb415ce827..7978d7a5b9 100644 --- a/modules/viz/src/widget.cpp +++ b/modules/viz/src/widget.cpp @@ -91,23 +91,6 @@ 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::getProp(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::getProp(other)); - CV_Assert(actor); - - Widget::operator=(other); - return *this; -} - void temp_viz::Widget3D::setPose(const Affine3f &pose) { vtkProp3D *actor = vtkProp3D::SafeDownCast(WidgetAccessor::getProp(*this)); @@ -178,22 +161,6 @@ template<> temp_viz::Widget3D temp_viz::Widget::cast() /////////////////////////////////////////////////////////////////////////////////////////////// /// 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::getProp(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::getProp(other)); - CV_Assert(actor); - Widget::operator=(other); - return *this; -} - void temp_viz::Widget2D::setColor(const Color &color) { vtkActor2D *actor = vtkActor2D::SafeDownCast(WidgetAccessor::getProp(*this)); @@ -202,3 +169,13 @@ void temp_viz::Widget2D::setColor(const Color &color) actor->GetProperty ()->SetColor (c.val); actor->Modified (); } + +template<> temp_viz::Widget2D temp_viz::Widget::cast() +{ + vtkActor2D *actor = vtkActor2D::SafeDownCast(WidgetAccessor::getProp(*this)); + CV_Assert(actor); + + Widget2D widget; + WidgetAccessor::setProp(widget, actor); + return widget; +} From 71c76aecc9bffefce9660008a9ac7d2e50bb7b79 Mon Sep 17 00:00:00 2001 From: ozantonkal Date: Wed, 10 Jul 2013 12:51:17 +0200 Subject: [PATCH 09/12] PolyLine widget implementation --- modules/viz/include/opencv2/viz/widgets.hpp | 10 +++ modules/viz/src/simple_widgets.cpp | 82 ++++++++++++++++++++- modules/viz/test/test_viz3d.cpp | 34 ++++++--- 3 files changed, 113 insertions(+), 13 deletions(-) diff --git a/modules/viz/include/opencv2/viz/widgets.hpp b/modules/viz/include/opencv2/viz/widgets.hpp index cf265ac483..cd1ed94533 100644 --- a/modules/viz/include/opencv2/viz/widgets.hpp +++ b/modules/viz/include/opencv2/viz/widgets.hpp @@ -106,6 +106,15 @@ namespace temp_viz CoordinateSystemWidget(double scale, const Affine3f& affine); }; + class CV_EXPORTS PolyLineWidget : public Widget3D + { + public: + PolyLineWidget(InputArray _points, const Color &color = Color::white()); + + private: + struct CopyImpl; + }; + class CV_EXPORTS TextWidget : public Widget2D { public: @@ -144,6 +153,7 @@ namespace temp_viz template<> CV_EXPORTS CircleWidget Widget::cast(); template<> CV_EXPORTS CubeWidget Widget::cast(); template<> CV_EXPORTS CoordinateSystemWidget Widget::cast(); + template<> CV_EXPORTS PolyLineWidget Widget::cast(); template<> CV_EXPORTS TextWidget Widget::cast(); template<> CV_EXPORTS CloudWidget Widget::cast(); template<> CV_EXPORTS CloudNormalsWidget Widget::cast(); diff --git a/modules/viz/src/simple_widgets.cpp b/modules/viz/src/simple_widgets.cpp index db27884e87..a9ed2ef8fc 100644 --- a/modules/viz/src/simple_widgets.cpp +++ b/modules/viz/src/simple_widgets.cpp @@ -26,14 +26,14 @@ temp_viz::LineWidget::LineWidget(const Point3f &pt1, const Point3f &pt2, const C void temp_viz::LineWidget::setLineWidth(float line_width) { - vtkLODActor *actor = vtkLODActor::SafeDownCast(WidgetAccessor::getProp(*this)); + vtkActor *actor = vtkActor::SafeDownCast(WidgetAccessor::getProp(*this)); CV_Assert(actor); actor->GetProperty()->SetLineWidth(line_width); } float temp_viz::LineWidget::getLineWidth() { - vtkLODActor *actor = vtkLODActor::SafeDownCast(WidgetAccessor::getProp(*this)); + vtkActor *actor = vtkActor::SafeDownCast(WidgetAccessor::getProp(*this)); CV_Assert(actor); return actor->GetProperty()->GetLineWidth(); } @@ -342,6 +342,82 @@ template<> temp_viz::CoordinateSystemWidget temp_viz::Widget::cast(widget); } +/////////////////////////////////////////////////////////////////////////////////////////////// +/// polyline widget implementation + +struct temp_viz::PolyLineWidget::CopyImpl +{ + template + static void copy(const Mat& source, Vec<_Tp, 3> *output, vtkSmartPointer polyLine) + { + int s_chs = source.channels(); + + for(int y = 0, id = 0; y < source.rows; ++y) + { + const _Tp* srow = source.ptr<_Tp>(y); + + for(int x = 0; x < source.cols; ++x, srow += s_chs, ++id) + { + *output++ = Vec<_Tp, 3>(srow); + polyLine->GetPointIds()->SetId(id,id); + } + } + } +}; + +temp_viz::PolyLineWidget::PolyLineWidget(InputArray _pointData, const Color &color) +{ + Mat pointData = _pointData.getMat(); + CV_Assert(pointData.type() == CV_32FC3 || pointData.type() == CV_32FC4 || pointData.type() == CV_64FC3 || pointData.type() == CV_64FC4); + vtkIdType nr_points = pointData.total(); + + vtkSmartPointer points = vtkSmartPointer::New (); + vtkSmartPointer polyData = vtkSmartPointer::New (); + vtkSmartPointer polyLine = vtkSmartPointer::New (); + + if (pointData.depth() == CV_32F) + points->SetDataTypeToFloat(); + else + points->SetDataTypeToDouble(); + + points->SetNumberOfPoints(nr_points); + polyLine->GetPointIds()->SetNumberOfIds(nr_points); + + if (pointData.depth() == CV_32F) + { + // Get a pointer to the beginning of the data array + Vec3f *data_beg = vtkpoints_data(points); + CopyImpl::copy(pointData, data_beg, polyLine); + } + else if (pointData.depth() == CV_64F) + { + // Get a pointer to the beginning of the data array + Vec3d *data_beg = vtkpoints_data(points); + CopyImpl::copy(pointData, data_beg, polyLine); + } + + vtkSmartPointer cells = vtkSmartPointer::New(); + cells->InsertNextCell(polyLine); + + polyData->SetPoints(points); + polyData->SetLines(cells); + + vtkSmartPointer mapper = vtkSmartPointer::New(); + mapper->SetInput(polyData); + + vtkSmartPointer actor = vtkSmartPointer::New(); + actor->SetMapper(mapper); + + WidgetAccessor::setProp(*this, actor); + setColor(color); +} + +template<> temp_viz::PolyLineWidget temp_viz::Widget::cast() +{ + Widget3D widget = this->cast(); + return static_cast(widget); +} + /////////////////////////////////////////////////////////////////////////////////////////////// /// text widget implementation @@ -398,8 +474,6 @@ struct temp_viz::CloudWidget::CreateCloudWidget vtkSmartPointer points = polydata->GetPoints(); vtkSmartPointer initcells; nr_points = cloud.total(); - - points = polydata->GetPoints (); if (!points) { diff --git a/modules/viz/test/test_viz3d.cpp b/modules/viz/test/test_viz3d.cpp index 76993d160d..fb8a9f6dae 100644 --- a/modules/viz/test/test_viz3d.cpp +++ b/modules/viz/test/test_viz3d.cpp @@ -112,16 +112,32 @@ TEST(Viz_viz3d, accuracy) v.showWidget("coordinateSystem", csw); // v.showWidget("text",tw); // v.showWidget("pcw",pcw); - v.showWidget("pcw2",pcw2); +// v.showWidget("pcw2",pcw2); - temp_viz::LineWidget lw2 = lw; +// temp_viz::LineWidget lw2 = lw; // v.showPointCloud("cld",cloud, colors); cv::Mat normals(cloud.size(), cloud.type(), cv::Scalar(0, 10, 0)); // v.addPointCloudNormals(cloud, normals, 100, 0.02, "n"); temp_viz::CloudNormalsWidget cnw(cloud, normals); - v.showWidget("n", cnw); +// v.showWidget("n", cnw); + +// lw = v.getWidget("n").cast(); +// pw = v.getWidget("n").cast(); + + cv::Mat points(1, 4, CV_64FC4); + + cv::Vec4d* data = points.ptr(); + data[0] = cv::Vec4d(0.0,0.0,0.0,0.0); + data[1] = cv::Vec4d(1.0,1.0,1.0,1.0); + data[2] = cv::Vec4d(0.0,2.0,0.0,0.0); + data[3] = cv::Vec4d(3.0,4.0,1.0,1.0); + points = points.reshape(0, 2); + + temp_viz::PolyLineWidget plw(points); + v.showWidget("polyline",plw); + lw = v.getWidget("polyline").cast(); while(!v.wasStopped()) { @@ -129,25 +145,25 @@ TEST(Viz_viz3d, accuracy) cv::Affine3f cloudPosition(angle_x, angle_y, angle_z, cv::Vec3f(pos_x, pos_y, pos_z)); cv::Affine3f cloudPosition2(angle_x, angle_y, angle_z, cv::Vec3f(pos_x+0.2, pos_y+0.2, pos_z+0.2)); - lw2.setColor(temp_viz::Color(col_blue, col_green, col_red)); - lw.setLineWidth(lw.getLineWidth()+pos_x * 10); +// lw2.setColor(temp_viz::Color(col_blue, col_green, col_red)); + lw.setLineWidth(pos_x * 10); - pw.setColor(temp_viz::Color(col_blue, col_green, col_red)); + plw.setColor(temp_viz::Color(col_blue, col_green, col_red)); sw.setPose(cloudPosition); // pw.setPose(cloudPosition); aw.setPose(cloudPosition); cw.setPose(cloudPosition); cyw.setPose(cloudPosition); - lw.setPose(cloudPosition); +// lw.setPose(cloudPosition); cuw.setPose(cloudPosition); // cnw.setPose(cloudPosition); // v.showWidget("pcw",pcw, cloudPosition); // v.showWidget("pcw2",pcw2, cloudPosition2); // v.showWidget("plane", pw, cloudPosition); - v.setWidgetPose("n",cloudPosition); - v.setWidgetPose("pcw2", cloudPosition); +// v.setWidgetPose("n",cloudPosition); +// v.setWidgetPose("pcw2", cloudPosition); cnw.setColor(temp_viz::Color(col_blue, col_green, col_red)); pcw2.setColor(temp_viz::Color(col_blue, col_green, col_red)); From e185900270be277c96d2a530344b4ade39d7b5e9 Mon Sep 17 00:00:00 2001 From: ozantonkal Date: Wed, 10 Jul 2013 15:34:19 +0200 Subject: [PATCH 10/12] grid widget implementation --- modules/viz/include/opencv2/viz/widgets.hpp | 7 +++++ modules/viz/src/simple_widgets.cpp | 32 +++++++++++++++++++++ modules/viz/test/test_viz3d.cpp | 15 +++++++--- 3 files changed, 50 insertions(+), 4 deletions(-) diff --git a/modules/viz/include/opencv2/viz/widgets.hpp b/modules/viz/include/opencv2/viz/widgets.hpp index cd1ed94533..85ebda0209 100644 --- a/modules/viz/include/opencv2/viz/widgets.hpp +++ b/modules/viz/include/opencv2/viz/widgets.hpp @@ -115,6 +115,12 @@ namespace temp_viz struct CopyImpl; }; + class CV_EXPORTS GridWidget : public Widget3D + { + public: + GridWidget(Vec2i dimensions, Vec2d spacing, const Color &color = Color::white()); + }; + class CV_EXPORTS TextWidget : public Widget2D { public: @@ -154,6 +160,7 @@ namespace temp_viz template<> CV_EXPORTS CubeWidget Widget::cast(); template<> CV_EXPORTS CoordinateSystemWidget Widget::cast(); template<> CV_EXPORTS PolyLineWidget Widget::cast(); + template<> CV_EXPORTS GridWidget Widget::cast(); template<> CV_EXPORTS TextWidget Widget::cast(); template<> CV_EXPORTS CloudWidget Widget::cast(); template<> CV_EXPORTS CloudNormalsWidget Widget::cast(); diff --git a/modules/viz/src/simple_widgets.cpp b/modules/viz/src/simple_widgets.cpp index a9ed2ef8fc..f3ecdb9dc9 100644 --- a/modules/viz/src/simple_widgets.cpp +++ b/modules/viz/src/simple_widgets.cpp @@ -418,6 +418,38 @@ template<> temp_viz::PolyLineWidget temp_viz::Widget::cast(widget); } +/////////////////////////////////////////////////////////////////////////////////////////////// +/// grid widget implementation + +temp_viz::GridWidget::GridWidget(Vec2i dimensions, Vec2d spacing, const Color &color) +{ + // Create the grid using image data + vtkSmartPointer grid = vtkSmartPointer::New(); + + // Add 1 to dimensions because in ImageData dimensions is the number of lines + // - however here it means number of cells + grid->SetDimensions(dimensions[0]+1, dimensions[1]+1, 1); + grid->SetSpacing(spacing[0], spacing[1], 0.); + + // Set origin of the grid to be the middle of the grid + grid->SetOrigin(dimensions[0] * spacing[0] * (-0.5), dimensions[1] * spacing[1] * (-0.5), 0); + + vtkSmartPointer mapper = vtkSmartPointer::New(); + mapper->SetInput(grid); + vtkSmartPointer actor = vtkSmartPointer::New(); + actor->SetMapper(mapper); + + // Show it as wireframe + actor->GetProperty ()->SetRepresentationToWireframe (); + WidgetAccessor::setProp(*this, actor); +} + +template<> temp_viz::GridWidget temp_viz::Widget::cast() +{ + Widget3D widget = this->cast(); + return static_cast(widget); +} + /////////////////////////////////////////////////////////////////////////////////////////////// /// text widget implementation diff --git a/modules/viz/test/test_viz3d.cpp b/modules/viz/test/test_viz3d.cpp index fb8a9f6dae..7a48bb9a98 100644 --- a/modules/viz/test/test_viz3d.cpp +++ b/modules/viz/test/test_viz3d.cpp @@ -136,8 +136,13 @@ TEST(Viz_viz3d, accuracy) points = points.reshape(0, 2); temp_viz::PolyLineWidget plw(points); - v.showWidget("polyline",plw); - lw = v.getWidget("polyline").cast(); +// v.showWidget("polyline",plw); +// lw = v.getWidget("polyline").cast(); + + temp_viz::GridWidget gw(temp_viz::Vec2i(10,10), temp_viz::Vec2d(0.1,0.1)); + v.showWidget("grid", gw); + lw = v.getWidget("grid").cast(); +// float grid_x_angle = 0.0; while(!v.wasStopped()) { @@ -145,8 +150,8 @@ TEST(Viz_viz3d, accuracy) cv::Affine3f cloudPosition(angle_x, angle_y, angle_z, cv::Vec3f(pos_x, pos_y, pos_z)); cv::Affine3f cloudPosition2(angle_x, angle_y, angle_z, cv::Vec3f(pos_x+0.2, pos_y+0.2, pos_z+0.2)); -// lw2.setColor(temp_viz::Color(col_blue, col_green, col_red)); - lw.setLineWidth(pos_x * 10); + lw.setColor(temp_viz::Color(col_blue, col_green, col_red)); +// lw.setLineWidth(pos_x * 10); plw.setColor(temp_viz::Color(col_blue, col_green, col_red)); @@ -167,6 +172,8 @@ TEST(Viz_viz3d, accuracy) cnw.setColor(temp_viz::Color(col_blue, col_green, col_red)); pcw2.setColor(temp_viz::Color(col_blue, col_green, col_red)); + gw.updatePose(temp_viz::Affine3f(0.0, 0.1, 0.0, cv::Vec3f(0.0,0.0,0.0))); + angle_x += 0.1f; angle_y -= 0.1f; angle_z += 0.1f; From 5813a3a99d7da8cbc060d8ac60486da91c0af2cd Mon Sep 17 00:00:00 2001 From: ozantonkal Date: Wed, 10 Jul 2013 16:34:47 +0200 Subject: [PATCH 11/12] text3D widget implementation --- modules/viz/include/opencv2/viz/widgets.hpp | 10 ++++ modules/viz/src/simple_widgets.cpp | 53 +++++++++++++++++++++ modules/viz/test/test_viz3d.cpp | 3 ++ 3 files changed, 66 insertions(+) diff --git a/modules/viz/include/opencv2/viz/widgets.hpp b/modules/viz/include/opencv2/viz/widgets.hpp index 85ebda0209..06750238ee 100644 --- a/modules/viz/include/opencv2/viz/widgets.hpp +++ b/modules/viz/include/opencv2/viz/widgets.hpp @@ -121,6 +121,15 @@ namespace temp_viz GridWidget(Vec2i dimensions, Vec2d spacing, const Color &color = Color::white()); }; + class CV_EXPORTS Text3DWidget : public Widget3D + { + public: + Text3DWidget(const String &text, const Point3f &position, double text_scale = 1.0, const Color &color = Color::white()); + + void setText(const String &text); + String getText() const; + }; + class CV_EXPORTS TextWidget : public Widget2D { public: @@ -161,6 +170,7 @@ namespace temp_viz template<> CV_EXPORTS CoordinateSystemWidget Widget::cast(); template<> CV_EXPORTS PolyLineWidget Widget::cast(); template<> CV_EXPORTS GridWidget Widget::cast(); + template<> CV_EXPORTS Text3DWidget Widget::cast(); template<> CV_EXPORTS TextWidget Widget::cast(); template<> CV_EXPORTS CloudWidget Widget::cast(); template<> CV_EXPORTS CloudNormalsWidget Widget::cast(); diff --git a/modules/viz/src/simple_widgets.cpp b/modules/viz/src/simple_widgets.cpp index f3ecdb9dc9..49e9f68111 100644 --- a/modules/viz/src/simple_widgets.cpp +++ b/modules/viz/src/simple_widgets.cpp @@ -450,6 +450,59 @@ template<> temp_viz::GridWidget temp_viz::Widget::cast() return static_cast(widget); } +/////////////////////////////////////////////////////////////////////////////////////////////// +/// text3D widget implementation + +temp_viz::Text3DWidget::Text3DWidget(const String &text, const Point3f &position, double text_scale, const Color &color) +{ + vtkSmartPointer textSource = vtkSmartPointer::New (); + textSource->SetText (text.c_str()); + textSource->Update (); + + vtkSmartPointer mapper = vtkSmartPointer::New (); + mapper->SetInputConnection (textSource->GetOutputPort ()); + + vtkSmartPointer actor = vtkSmartPointer::New (); + actor->SetMapper (mapper); + actor->SetPosition (position.x, position.y, position.z); + actor->SetScale (text_scale); + + WidgetAccessor::setProp(*this, actor); + setColor(color); +} + +void temp_viz::Text3DWidget::setText(const String &text) +{ + vtkFollower *actor = vtkFollower::SafeDownCast(WidgetAccessor::getProp(*this)); + CV_Assert(actor); + + // Update text source + vtkPolyDataMapper *mapper = vtkPolyDataMapper::SafeDownCast(actor->GetMapper()); + vtkVectorText * textSource = vtkVectorText::SafeDownCast(mapper->GetInputConnection(0,0)->GetProducer()); + CV_Assert(textSource); + + textSource->SetText(text.c_str()); + textSource->Update(); +} + +temp_viz::String temp_viz::Text3DWidget::getText() const +{ + vtkFollower *actor = vtkFollower::SafeDownCast(WidgetAccessor::getProp(*this)); + CV_Assert(actor); + + vtkPolyDataMapper *mapper = vtkPolyDataMapper::SafeDownCast(actor->GetMapper()); + vtkVectorText * textSource = vtkVectorText::SafeDownCast(mapper->GetInputConnection(0,0)->GetProducer()); + CV_Assert(textSource); + + return textSource->GetText(); +} + +template<> temp_viz::Text3DWidget temp_viz::Widget::cast() +{ + Widget3D widget = this->cast(); + return static_cast(widget); +} + /////////////////////////////////////////////////////////////////////////////////////////////// /// text widget implementation diff --git a/modules/viz/test/test_viz3d.cpp b/modules/viz/test/test_viz3d.cpp index 7a48bb9a98..090501eb58 100644 --- a/modules/viz/test/test_viz3d.cpp +++ b/modules/viz/test/test_viz3d.cpp @@ -142,6 +142,9 @@ TEST(Viz_viz3d, accuracy) temp_viz::GridWidget gw(temp_viz::Vec2i(10,10), temp_viz::Vec2d(0.1,0.1)); v.showWidget("grid", gw); lw = v.getWidget("grid").cast(); + + temp_viz::Text3DWidget t3w("OpenCV", cv::Point3f(0.0, 2.0, 0.0), 1.0, temp_viz::Color(255,255,0)); + v.showWidget("txt3d", t3w); // float grid_x_angle = 0.0; while(!v.wasStopped()) From eef8195569869189ed8abd991f81b503145a1381 Mon Sep 17 00:00:00 2001 From: ozantonkal Date: Thu, 11 Jul 2013 09:30:52 +0200 Subject: [PATCH 12/12] clean unnecessary methods, make text3d face camera --- modules/viz/include/opencv2/viz/viz3d.hpp | 5 - modules/viz/src/q/viz3d_impl.hpp | 21 -- modules/viz/src/viz3d.cpp | 20 -- modules/viz/src/viz3d_impl.cpp | 392 +--------------------- modules/viz/src/viz_main.cpp | 113 ------- 5 files changed, 7 insertions(+), 544 deletions(-) diff --git a/modules/viz/include/opencv2/viz/viz3d.hpp b/modules/viz/include/opencv2/viz/viz3d.hpp index a0181cb713..f16d26d420 100644 --- a/modules/viz/include/opencv2/viz/viz3d.hpp +++ b/modules/viz/include/opencv2/viz/viz3d.hpp @@ -26,16 +26,11 @@ namespace temp_viz void setBackgroundColor(const Color& color = Color::black()); - void showPointCloud(const String& id, InputArray cloud, InputArray colors, const Affine3f& pose = Affine3f::Identity()); - void showPointCloud(const String& id, InputArray cloud, const Color& color, const Affine3f& pose = Affine3f::Identity()); - - bool addPointCloudNormals (const Mat &cloud, const Mat& normals, int level = 100, float scale = 0.02f, const String& id = "cloud"); bool addPolygonMesh (const Mesh3d& mesh, const String& id = "polygon"); bool updatePolygonMesh (const Mesh3d& mesh, const String& id = "polygon"); bool addPolylineFromPolygonMesh (const Mesh3d& mesh, const String& id = "polyline"); - bool addText (const String &text, int xpos, int ypos, const Color& color, int fontsize = 10, const String& id = ""); bool addPolygon(const Mat& cloud, const Color& color, const String& id = "polygon"); void spin (); diff --git a/modules/viz/src/q/viz3d_impl.hpp b/modules/viz/src/q/viz3d_impl.hpp index a49bb49227..b028812fbc 100644 --- a/modules/viz/src/q/viz3d_impl.hpp +++ b/modules/viz/src/q/viz3d_impl.hpp @@ -52,36 +52,15 @@ public: void setBackgroundColor (const Color& color); - bool addText (const String &text, int xpos, int ypos, const Color& color, int fontsize = 10, const String& id = ""); - bool updateText (const String &text, int xpos, int ypos, const Color& color, int fontsize = 10, const String& id = ""); - - /** \brief Set the pose of an existing shape. Returns false if the shape doesn't exist, true if the pose was succesfully updated. */ - bool updateShapePose (const String& id, const Affine3f& pose); - - bool addText3D (const String &text, const Point3f &position, const Color& color, double textScale = 1.0, const String& id = ""); - - bool addPointCloudNormals (const cv::Mat &cloud, const cv::Mat& normals, int level = 100, float scale = 0.02f, const String& id = "cloud"); - - /** \brief If the id exists, updates the point cloud; otherwise, adds a new point cloud to the scene - * \param[in] id a variable to identify the point cloud - * \param[in] cloud cloud input in x,y,z coordinates - * \param[in] colors color input in the same order of the points or single uniform color - * \param[in] pose transform to be applied on the point cloud - */ - void showPointCloud(const String& id, InputArray cloud, InputArray colors, const Affine3f& pose = Affine3f::Identity()); - void showPointCloud(const String& id, InputArray cloud, const Color& color, const Affine3f& pose = Affine3f::Identity()); - bool addPolygonMesh (const Mesh3d& mesh, const cv::Mat& mask, const String& id = "polygon"); bool updatePolygonMesh (const Mesh3d& mesh, const cv::Mat& mask, const String& id = "polygon"); bool addPolylineFromPolygonMesh (const Mesh3d& mesh, const String& id = "polyline"); - void setPointCloudColor (const Color& color, const String& id = "cloud"); bool setPointCloudRenderingProperties (int property, double value, const String& id = "cloud"); bool getPointCloudRenderingProperties (int property, double &value, const String& id = "cloud"); bool setShapeRenderingProperties (int property, double value, const String& id); - void setShapeColor (const Color& color, const String& id); /** \brief Set whether the point cloud is selected or not * \param[in] selected whether the cloud is selected or not (true = selected) diff --git a/modules/viz/src/viz3d.cpp b/modules/viz/src/viz3d.cpp index 0460630baa..4b9357391d 100644 --- a/modules/viz/src/viz3d.cpp +++ b/modules/viz/src/viz3d.cpp @@ -17,21 +17,6 @@ void temp_viz::Viz3d::setBackgroundColor(const Color& color) impl_->setBackgroundColor(color); } -void temp_viz::Viz3d::showPointCloud(const String& id, InputArray cloud, InputArray colors, const Affine3f& pose) -{ - impl_->showPointCloud(id, cloud, colors, pose); -} - -void temp_viz::Viz3d::showPointCloud(const String& id, InputArray cloud, const Color& color, const Affine3f& pose) -{ - impl_->showPointCloud(id, cloud, color, pose); -} - -bool temp_viz::Viz3d::addPointCloudNormals (const Mat &cloud, const Mat& normals, int level, float scale, const String& id) -{ - return impl_->addPointCloudNormals(cloud, normals, level, scale, id); -} - bool temp_viz::Viz3d::addPolygonMesh (const Mesh3d& mesh, const String& id) { return impl_->addPolygonMesh(mesh, Mat(), id); @@ -47,11 +32,6 @@ bool temp_viz::Viz3d::addPolylineFromPolygonMesh (const Mesh3d& mesh, const Stri return impl_->addPolylineFromPolygonMesh(mesh, id); } -bool temp_viz::Viz3d::addText (const String &text, int xpos, int ypos, const Color& color, int fontsize, const String& id) -{ - return impl_->addText(text, xpos, ypos, color, fontsize, id); -} - bool temp_viz::Viz3d::addPolygon(const Mat& cloud, const Color& color, const String& id) { return impl_->addPolygon(cloud, color, id); diff --git a/modules/viz/src/viz3d_impl.cpp b/modules/viz/src/viz3d_impl.cpp index dd94dc343d..1a1fca699b 100644 --- a/modules/viz/src/viz3d_impl.cpp +++ b/modules/viz/src/viz3d_impl.cpp @@ -36,347 +36,6 @@ void temp_viz::Viz3d::VizImpl::setWindowName (const std::string &name) void temp_viz::Viz3d::VizImpl::setPosition (int x, int y) { window_->SetPosition (x, y); } void temp_viz::Viz3d::VizImpl::setSize (int xw, int yw) { window_->SetSize (xw, yw); } -void temp_viz::Viz3d::VizImpl::showPointCloud(const String& id, InputArray _cloud, InputArray _colors, const Affine3f& pose) -{ - Mat cloud = _cloud.getMat(); - Mat colors = _colors.getMat(); - CV_Assert(cloud.type() == CV_32FC3 || cloud.type() == CV_64FC3 || cloud.type() == CV_32FC4 || cloud.type() == CV_64FC4); - CV_Assert(colors.type() == CV_8UC3 && cloud.size() == colors.size()); - - vtkSmartPointer polydata; - vtkSmartPointer vertices; - vtkSmartPointer points; - vtkSmartPointer initcells; - vtkIdType nr_points = cloud.total(); - - // If the cloud already exists, update otherwise create new one - CloudActorMap::iterator am_it = cloud_actor_map_->find (id); - bool exist = am_it == cloud_actor_map_->end(); - if (exist) - { - // Add as new cloud - allocVtkPolyData(polydata); - //polydata = vtkSmartPointer::New (); - vertices = vtkSmartPointer::New (); - polydata->SetVerts (vertices); - - points = polydata->GetPoints (); - - if (!points) - { - points = vtkSmartPointer::New (); - if (cloud.depth() == CV_32F) - points->SetDataTypeToFloat(); - else if (cloud.depth() == CV_64F) - points->SetDataTypeToDouble(); - polydata->SetPoints (points); - } - points->SetNumberOfPoints (nr_points); - } - else - { - // Update the cloud - // Get the current poly data - polydata = reinterpret_cast(am_it->second.actor->GetMapper ())->GetInput (); - vertices = polydata->GetVerts (); - points = polydata->GetPoints (); - // Update the point data type based on the cloud - if (cloud.depth() == CV_32F) - points->SetDataTypeToFloat (); - else if (cloud.depth() == CV_64F) - points->SetDataTypeToDouble (); - - points->SetNumberOfPoints (nr_points); - } - - if (cloud.depth() == CV_32F) - { - // Get a pointer to the beginning of the data array - Vec3f *data_beg = vtkpoints_data(points); - Vec3f *data_end = NanFilter::copy(cloud, data_beg, cloud); - std::transform(data_beg, data_end, data_beg, ApplyAffine(pose)); - nr_points = data_end - data_beg; - - } - else if (cloud.depth() == CV_64F) - { - // Get a pointer to the beginning of the data array - Vec3d *data_beg = vtkpoints_data(points); - Vec3d *data_end = NanFilter::copy(cloud, data_beg, cloud); - std::transform(data_beg, data_end, data_beg, ApplyAffine(pose)); - nr_points = data_end - data_beg; - } - - points->SetNumberOfPoints (nr_points); - - vtkSmartPointer cells = vertices->GetData (); - - if (exist) - updateCells (cells, initcells, nr_points); - else - updateCells (cells, am_it->second.cells, nr_points); - - // Set the cells and the vertices - vertices->SetCells (nr_points, cells); - - // Get a random color - Vec3b* colors_data = new Vec3b[nr_points]; - NanFilter::copy(colors, colors_data, cloud); - - vtkSmartPointer scalars = vtkSmartPointer::New (); - scalars->SetNumberOfComponents (3); - scalars->SetNumberOfTuples (nr_points); - scalars->SetArray (colors_data->val, 3 * nr_points, 0); - - // Assign the colors - Vec2d minmax; - polydata->GetPointData ()->SetScalars (scalars); - scalars->GetRange (minmax.val); - - // If this is the new point cloud, a new actor is created - if (exist) - { - vtkSmartPointer actor; - createActorFromVTKDataSet (polydata, actor); - - actor->GetMapper ()->SetScalarRange (minmax.val); - - // Add it to all renderers - renderer_->AddActor (actor); - - // Save the pointer/ID pair to the global actor map - (*cloud_actor_map_)[id].actor = actor; - (*cloud_actor_map_)[id].cells = initcells; - - const Eigen::Vector4f sensor_origin = Eigen::Vector4f::Zero (); - const Eigen::Quaternionf sensor_orientation = Eigen::Quaternionf::Identity (); - - // Save the viewpoint transformation matrix to the global actor map - vtkSmartPointer transformation = vtkSmartPointer::New(); - convertToVtkMatrix (sensor_origin, sensor_orientation, transformation); - - (*cloud_actor_map_)[id].viewpoint_transformation_ = transformation; - } - else - { - // Update the mapper - reinterpret_cast(am_it->second.actor->GetMapper ())->SetInput (polydata); - am_it->second.actor->GetMapper ()->ScalarVisibilityOn(); - am_it->second.actor->Modified (); - } -} - -void temp_viz::Viz3d::VizImpl::showPointCloud(const String& id, InputArray _cloud, const Color& color, const Affine3f& pose) -{ - Mat cloud = _cloud.getMat(); - CV_Assert(cloud.type() == CV_32FC3 || cloud.type() == CV_64FC3 || cloud.type() == CV_32FC4 || cloud.type() == CV_64FC4); - - vtkSmartPointer polydata; - vtkSmartPointer vertices; - vtkSmartPointer points; - vtkSmartPointer initcells; - vtkIdType nr_points = cloud.total(); - - // If the cloud already exists, update otherwise create new one - CloudActorMap::iterator am_it = cloud_actor_map_->find (id); - bool exist = am_it == cloud_actor_map_->end(); - if (exist) - { - // Add as new cloud - allocVtkPolyData(polydata); - //polydata = vtkSmartPointer::New (); - vertices = vtkSmartPointer::New (); - polydata->SetVerts (vertices); - - points = polydata->GetPoints (); - - if (!points) - { - points = vtkSmartPointer::New (); - if (cloud.depth() == CV_32F) - points->SetDataTypeToFloat(); - else if (cloud.depth() == CV_64F) - points->SetDataTypeToDouble(); - polydata->SetPoints (points); - } - points->SetNumberOfPoints (nr_points); - } - else - { - // Update the cloud - // Get the current poly data - polydata = reinterpret_cast(am_it->second.actor->GetMapper ())->GetInput (); - vertices = polydata->GetVerts (); - points = polydata->GetPoints (); - // Update the point data type based on the cloud - if (cloud.depth() == CV_32F) - points->SetDataTypeToFloat (); - else if (cloud.depth() == CV_64F) - points->SetDataTypeToDouble (); - - points->SetNumberOfPoints (nr_points); - } - - if (cloud.depth() == CV_32F) - { - // Get a pointer to the beginning of the data array - Vec3f *data_beg = vtkpoints_data(points); - Vec3f *data_end = NanFilter::copy(cloud, data_beg, cloud); - std::transform(data_beg, data_end, data_beg, ApplyAffine(pose)); - nr_points = data_end - data_beg; - - } - else if (cloud.depth() == CV_64F) - { - // Get a pointer to the beginning of the data array - Vec3d *data_beg = vtkpoints_data(points); - Vec3d *data_end = NanFilter::copy(cloud, data_beg, cloud); - std::transform(data_beg, data_end, data_beg, ApplyAffine(pose)); - nr_points = data_end - data_beg; - } - - points->SetNumberOfPoints (nr_points); - - vtkSmartPointer cells = vertices->GetData (); - - if (exist) - updateCells (cells, initcells, nr_points); - else - updateCells (cells, am_it->second.cells, nr_points); - - // Set the cells and the vertices - vertices->SetCells (nr_points, cells); - - // Get a random color - Color c = vtkcolor(color); - polydata->GetPointData ()->SetScalars (0); - - // If this is the new point cloud, a new actor is created - if (exist) - { - vtkSmartPointer actor; - createActorFromVTKDataSet (polydata, actor, false); - - actor->GetProperty ()->SetColor(c.val); - - // Add it to all renderers - renderer_->AddActor (actor); - - // Save the pointer/ID pair to the global actor map - (*cloud_actor_map_)[id].actor = actor; - (*cloud_actor_map_)[id].cells = initcells; - - const Eigen::Vector4f sensor_origin = Eigen::Vector4f::Zero (); - const Eigen::Quaternionf sensor_orientation = Eigen::Quaternionf::Identity (); - - // Save the viewpoint transformation matrix to the global actor map - vtkSmartPointer transformation = vtkSmartPointer::New(); - convertToVtkMatrix (sensor_origin, sensor_orientation, transformation); - - (*cloud_actor_map_)[id].viewpoint_transformation_ = transformation; - } - else - { - // Update the mapper - reinterpret_cast(am_it->second.actor->GetMapper ())->SetInput (polydata); - am_it->second.actor->GetProperty ()->SetColor(c.val); - am_it->second.actor->GetMapper ()->ScalarVisibilityOff(); - am_it->second.actor->Modified (); - } -} - -bool temp_viz::Viz3d::VizImpl::addPointCloudNormals (const cv::Mat &cloud, const cv::Mat& normals, int level, float scale, const std::string &id) -{ - CV_Assert(cloud.size() == normals.size() && cloud.type() == CV_32FC3 && normals.type() == CV_32FC3); - - if (cloud_actor_map_->find (id) != cloud_actor_map_->end ()) - return false; - - vtkSmartPointer points = vtkSmartPointer::New(); - vtkSmartPointer lines = vtkSmartPointer::New(); - - points->SetDataTypeToFloat (); - vtkSmartPointer data = vtkSmartPointer::New (); - data->SetNumberOfComponents (3); - - vtkIdType nr_normals = 0; - float* pts = 0; - - // If the cloud is organized, then distribute the normal step in both directions - if (cloud.cols > 1 && cloud.rows > 1) - { - vtkIdType point_step = static_cast (sqrt (double (level))); - nr_normals = (static_cast ((cloud.cols - 1)/ point_step) + 1) * - (static_cast ((cloud.rows - 1) / point_step) + 1); - pts = new float[2 * nr_normals * 3]; - - vtkIdType cell_count = 0; - for (vtkIdType y = 0; y < cloud.rows; y += point_step) - for (vtkIdType x = 0; x < cloud.cols; x += point_step) - { - cv::Point3f p = cloud.at(y, x); - cv::Point3f n = normals.at(y, x) * scale; - - pts[2 * cell_count * 3 + 0] = p.x; - pts[2 * cell_count * 3 + 1] = p.y; - pts[2 * cell_count * 3 + 2] = p.z; - pts[2 * cell_count * 3 + 3] = p.x + n.x; - pts[2 * cell_count * 3 + 4] = p.y + n.y; - pts[2 * cell_count * 3 + 5] = p.z + n.z; - - lines->InsertNextCell (2); - lines->InsertCellPoint (2 * cell_count); - lines->InsertCellPoint (2 * cell_count + 1); - cell_count++; - } - } - else - { - nr_normals = (cloud.size().area() - 1) / level + 1 ; - pts = new float[2 * nr_normals * 3]; - - for (vtkIdType i = 0, j = 0; j < nr_normals; j++, i = j * level) - { - cv::Point3f p = cloud.ptr()[i]; - cv::Point3f n = normals.ptr()[i] * scale; - - pts[2 * j * 3 + 0] = p.x; - pts[2 * j * 3 + 1] = p.y; - pts[2 * j * 3 + 2] = p.z; - pts[2 * j * 3 + 3] = p.x + n.x; - pts[2 * j * 3 + 4] = p.y + n.y; - pts[2 * j * 3 + 5] = p.z + n.z; - - lines->InsertNextCell (2); - lines->InsertCellPoint (2 * j); - lines->InsertCellPoint (2 * j + 1); - } - } - - data->SetArray (&pts[0], 2 * nr_normals * 3, 0); - points->SetData (data); - - vtkSmartPointer polyData = vtkSmartPointer::New(); - polyData->SetPoints (points); - polyData->SetLines (lines); - - vtkSmartPointer mapper = vtkSmartPointer::New (); - mapper->SetInput (polyData); - mapper->SetColorModeToMapScalars(); - mapper->SetScalarModeToUsePointData(); - - // create actor - vtkSmartPointer actor = vtkSmartPointer::New (); - actor->SetMapper (mapper); - - // Add it to all renderers - renderer_->AddActor (actor); - - // Save the pointer/ID pair to the global actor map - (*cloud_actor_map_)[id].actor = actor; - return (true); -} - bool temp_viz::Viz3d::VizImpl::addPolygonMesh (const Mesh3d& mesh, const Mat& mask, const std::string &id) { CV_Assert(mesh.cloud.type() == CV_32FC3 && mesh.cloud.rows == 1 && !mesh.polygons.empty ()); @@ -742,50 +401,6 @@ bool temp_viz::Viz3d::VizImpl::addArrow (const cv::Point3f &p1, const cv::Point3 return (true); } -////////////////////////////////////////////////// -bool temp_viz::Viz3d::VizImpl::addText3D (const std::string &text, const cv::Point3f& position, const Color& color, double textScale, const std::string &id) -{ - std::string tid; - if (id.empty ()) - tid = text; - else - tid = id; - - // Check to see if this ID entry already exists (has it been already added to the visualizer?) - ShapeActorMap::iterator am_it = shape_actor_map_->find (tid); - if (am_it != shape_actor_map_->end ()) - return std::cout << "[addText3d] A text with id <" << tid << "> already exists! Please choose a different id and retry." << std::endl, false; - - vtkSmartPointer textSource = vtkSmartPointer::New (); - textSource->SetText (text.c_str()); - textSource->Update (); - - vtkSmartPointer textMapper = vtkSmartPointer::New (); - textMapper->SetInputConnection (textSource->GetOutputPort ()); - - // Since each follower may follow a different camera, we need different followers - vtkRenderer* renderer = renderer_; - - vtkSmartPointer textActor = vtkSmartPointer::New (); - textActor->SetMapper (textMapper); - textActor->SetPosition (position.x, position.y, position.z); - textActor->SetScale (textScale); - - Color c = vtkcolor(color); - textActor->GetProperty ()->SetColor (c.val); - textActor->SetCamera (renderer->GetActiveCamera ()); - - renderer->AddActor (textActor); - renderer->Render (); - - // Save the pointer/ID pair to the global actor map. If we are saving multiple vtkFollowers - // for multiple viewport - (*shape_actor_map_)[tid] = textActor; - - - return (true); -} - bool temp_viz::Viz3d::VizImpl::addPolygon (const cv::Mat& cloud, const Color& color, const std::string &id) { CV_Assert(cloud.type() == CV_32FC3 && cloud.rows == 1); @@ -882,6 +497,13 @@ void temp_viz::Viz3d::VizImpl::showWidget(const String &id, const Widget &widget 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].actor = WidgetAccessor::getProp(widget); } diff --git a/modules/viz/src/viz_main.cpp b/modules/viz/src/viz_main.cpp index 0d56fe0a56..442a5ff8e7 100644 --- a/modules/viz/src/viz_main.cpp +++ b/modules/viz/src/viz_main.cpp @@ -350,21 +350,6 @@ void temp_viz::Viz3d::VizImpl::setBackgroundColor (const Color& color) renderer_->SetBackground (c.val); } -///////////////////////////////////////////////////////////////////////////////////////////// -void temp_viz::Viz3d::VizImpl::setPointCloudColor (const Color& color, const std::string &id) -{ - CloudActorMap::iterator am_it = cloud_actor_map_->find (id); - if (am_it != cloud_actor_map_->end ()) - { - vtkLODActor* actor = vtkLODActor::SafeDownCast (am_it->second.actor); - - Color c = vtkcolor(color); - actor->GetProperty ()->SetColor (c.val); - actor->GetMapper ()->ScalarVisibilityOff (); - actor->Modified (); - } -} - ///////////////////////////////////////////////////////////////////////////////////////////// bool temp_viz::Viz3d::VizImpl::getPointCloudRenderingProperties (int property, double &value, const std::string &id) { @@ -470,26 +455,6 @@ bool temp_viz::Viz3d::VizImpl::setPointCloudSelected (const bool selected, const return true; } -///////////////////////////////////////////////////////////////////////////////////////////// -void temp_viz::Viz3d::VizImpl::setShapeColor (const Color& color, const std::string &id) -{ - ShapeActorMap::iterator am_it = shape_actor_map_->find (id); - if (am_it != shape_actor_map_->end ()) - { - vtkActor* actor = vtkActor::SafeDownCast (am_it->second); - - Color c = vtkcolor(color); - actor->GetMapper ()->ScalarVisibilityOff (); - actor->GetProperty ()->SetColor (c.val); - actor->GetProperty ()->SetEdgeColor (c.val); - actor->GetProperty ()->SetAmbient (0.8); - actor->GetProperty ()->SetDiffuse (0.8); - actor->GetProperty ()->SetSpecular (0.8); - actor->GetProperty ()->SetLighting (0); - actor->Modified (); - } -} - ///////////////////////////////////////////////////////////////////////////////////////////// bool temp_viz::Viz3d::VizImpl::setShapeRenderingProperties (int property, double value, const std::string &id) { @@ -618,28 +583,6 @@ void temp_viz::Viz3d::VizImpl::updateCamera () renderer_->Render (); } -///////////////////////////////////////////////////////////////////////////////////////////// -bool temp_viz::Viz3d::VizImpl::updateShapePose (const std::string &id, const cv::Affine3f& pose) -{ - ShapeActorMap::iterator am_it = shape_actor_map_->find (id); - - vtkLODActor* actor; - - if (am_it == shape_actor_map_->end ()) - return (false); - else - actor = vtkLODActor::SafeDownCast (am_it->second); - - vtkSmartPointer matrix = vtkSmartPointer::New (); - - convertToVtkMatrix (pose.matrix, matrix); - - actor->SetUserMatrix (matrix); - actor->Modified (); - - return (true); -} - ///////////////////////////////////////////////////////////////////////////////////////////// void temp_viz::Viz3d::VizImpl::getCameras (temp_viz::Camera& camera) { @@ -916,62 +859,6 @@ bool temp_viz::Viz3d::VizImpl::addModelFromPLYFile (const std::string &filename, return (true); } -///////////////////////////////////////////////////////////////////////////////////////////// -bool temp_viz::Viz3d::VizImpl::addText (const std::string &text, int xpos, int ypos, const Color& color, int fontsize, const std::string &id) -{ - std::string tid = id.empty() ? text : id; - - // Check to see if this ID entry already exists (has it been already added to the visualizer?) - ShapeActorMap::iterator am_it = shape_actor_map_->find (tid); - if (am_it != shape_actor_map_->end ()) - return std::cout << "[addText] A text with id <"< already exists! Please choose a different id and retry.\n" << std::endl, false; - - // Create an Actor - vtkSmartPointer actor = vtkSmartPointer::New (); - actor->SetPosition (xpos, ypos); - actor->SetInput (text.c_str ()); - - vtkSmartPointer tprop = actor->GetTextProperty (); - tprop->SetFontSize (fontsize); - tprop->SetFontFamilyToArial (); - tprop->SetJustificationToLeft (); - tprop->BoldOn (); - - Color c = vtkcolor(color); - tprop->SetColor (c.val); - renderer_->AddActor(actor); - - // Save the pointer/ID pair to the global actor map - (*shape_actor_map_)[tid] = actor; - return (true); -} - -////////////////////////////////////////////////////////////////////////////////////////// -bool temp_viz::Viz3d::VizImpl::updateText (const std::string &text, int xpos, int ypos, const Color& color, int fontsize, const std::string &id) -{ - std::string tid = id.empty() ? text : id; - - ShapeActorMap::iterator am_it = shape_actor_map_->find (tid); - if (am_it == shape_actor_map_->end ()) - return false; - - // Retrieve the Actor - vtkTextActor *actor = vtkTextActor::SafeDownCast (am_it->second); - - actor->SetPosition (xpos, ypos); - actor->SetInput (text.c_str ()); - - vtkTextProperty* tprop = actor->GetTextProperty (); - tprop->SetFontSize (fontsize); - - Color c = vtkcolor(color); - tprop->SetColor (c.val); - - actor->Modified (); - - return (true); -} - bool temp_viz::Viz3d::VizImpl::addPolylineFromPolygonMesh (const Mesh3d& mesh, const std::string &id) { CV_Assert(mesh.cloud.rows == 1 && mesh.cloud.type() == CV_32FC3);