diff --git a/modules/viz/include/opencv2/viz/types.hpp b/modules/viz/include/opencv2/viz/types.hpp index 2328dc073c..3698d556b3 100644 --- a/modules/viz/include/opencv2/viz/types.hpp +++ b/modules/viz/include/opencv2/viz/types.hpp @@ -113,4 +113,22 @@ namespace temp_viz template inline bool isNan(const Point3_<_Tp>& p) { return isNan(p.x) || isNan(p.y) || isNan(p.z); } + + class Widget + { + public: + Widget(); + Widget(const String &id); + Widget(const Widget &other); + + void setId(const String &id); + void setColor(const Color &color); + void setPose(const Affine3f &pose); + void updatePose(const Affine3f &pose); + Affine3f getPose() const; + private: + class Impl; + cv::Ptr impl_; + friend struct WidgetAccessor; + }; } diff --git a/modules/viz/include/opencv2/viz/widget_accessor.hpp b/modules/viz/include/opencv2/viz/widget_accessor.hpp new file mode 100644 index 0000000000..2c0437c5a8 --- /dev/null +++ b/modules/viz/include/opencv2/viz/widget_accessor.hpp @@ -0,0 +1,12 @@ +#pragma once + +#include "precomp.hpp" +#include "types.hpp" + +namespace temp_viz +{ + struct WidgetAccessor + { + static CV_EXPORTS vtkSmartPointer getActor(const Widget &widget); + }; +} \ No newline at end of file diff --git a/modules/viz/src/types.cpp b/modules/viz/src/types.cpp index 0baed3a727..d717c305e9 100644 --- a/modules/viz/src/types.cpp +++ b/modules/viz/src/types.cpp @@ -1,7 +1,5 @@ #include - - ////////////////////////////////////////////////////////////////////////////////////////////////////// /// cv::Color @@ -22,3 +20,138 @@ temp_viz::Color temp_viz::Color::white() { return Color(255, 255, 255); } temp_viz::Color temp_viz::Color::gray() { return Color(128, 128, 128); } +class temp_viz::Widget::Impl +{ +public: + String id; + vtkSmartPointer actor; + + Impl() + { + actor = vtkSmartPointer:: New(); + } + + vtkSmartPointer getActor() + { + return actor; + } + + void setId(const String &id) + { + this->id = id; + } + + const temp_viz::String & getString() const + { + return id; + } + + void setColor(const Color & color) + { + 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 (); + } + + void setPose(const Affine3f &pose) + { + vtkSmartPointer matrix = convertToVtkMatrix(pose.matrix); + actor->SetUserMatrix (matrix); + actor->Modified (); + } + + void updatePose(const Affine3f &pose) + { + vtkSmartPointer matrix = actor->GetUserMatrix(); + Matx44f matrix_cv; + convertToCvMatrix(matrix, matrix_cv); + matrix = convertToVtkMatrix ((pose * Affine3f(matrix_cv)).matrix); + + actor->SetUserMatrix (matrix); + actor->Modified (); + } + + Affine3f getPose() const + { + vtkSmartPointer matrix = actor->GetUserMatrix(); + Matx44f matrix_cv; + convertToCvMatrix(matrix, matrix_cv); + return Affine3f(matrix_cv); + } + +protected: + + vtkSmartPointer convertToVtkMatrix (const cv::Matx44f &m) const + { + 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 convertToCvMatrix (const vtkSmartPointer &vtk_matrix, cv::Matx44f &m) const + { + for (int i = 0; i < 4; i++) + for (int k = 0; k < 4; k++) + m(i,k) = vtk_matrix->GetElement (i, k); + } + +}; + +temp_viz::Widget::Widget() +{ + impl_ = new Impl(); + impl_->setId("id"); +} + +temp_viz::Widget::Widget(const String &id) +{ + impl_ = new Impl(); + impl_->setId("id"); +} + +temp_viz::Widget::Widget(const Widget &other) +{ + impl_ = other.impl_; +} + +void temp_viz::Widget::setId(const String &id) +{ + impl_->setId(id); +} + +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(); +} + +// TODO Check if HAVE_VTK +#include "opencv2/viz/widget_accessor.hpp" + +vtkSmartPointer temp_viz::WidgetAccessor::getActor(const temp_viz::Widget &widget) +{ + return widget.impl_->actor; +} + diff --git a/modules/viz/test/test_viz3d.cpp b/modules/viz/test/test_viz3d.cpp index 8a7472c514..c80845f1f1 100644 --- a/modules/viz/test/test_viz3d.cpp +++ b/modules/viz/test/test_viz3d.cpp @@ -50,7 +50,6 @@ #include #include - cv::Mat cvcloud_load() { cv::Mat cloud(1, 20000, CV_32FC3);