diff --git a/modules/viz/include/opencv2/viz.hpp b/modules/viz/include/opencv2/viz.hpp index 3e1663df56..2ec8fbb86d 100644 --- a/modules/viz/include/opencv2/viz.hpp +++ b/modules/viz/include/opencv2/viz.hpp @@ -1,3 +1,6 @@ #pragma once +#include +#include #include + diff --git a/modules/viz/include/opencv2/viz/types.hpp b/modules/viz/include/opencv2/viz/types.hpp index 39432e2e52..b28996a9e7 100644 --- a/modules/viz/include/opencv2/viz/types.hpp +++ b/modules/viz/include/opencv2/viz/types.hpp @@ -36,8 +36,6 @@ namespace temp_viz using cv::DataType; - - struct CV_EXPORTS ModelCoefficients { std::vector values; @@ -81,6 +79,8 @@ namespace temp_viz std::vector polygons; }; + ///////////////////////////////////////////////////////////////////////////// + /// Utility functions inline Color vtkcolor(const Color& color) { @@ -90,11 +90,7 @@ namespace temp_viz } inline Vec3d vtkpoint(const Point3f& point) { return Vec3d(point.x, point.y, point.z); } - - - template inline _Tp normalized(const _Tp& v) { return v * 1/cv::norm(v); } - inline bool isNan(float x) { @@ -113,30 +109,4 @@ 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 Widget &other); - Widget& operator =(const Widget &other); - - void copyTo(Widget &dst); - - 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; - }; - - class LineWidget : public Widget - { - public: - LineWidget(const Point3f &pt1, const Point3f &pt2, const Color &color = Color(255,255,255)); - }; } diff --git a/modules/viz/include/opencv2/viz/viz3d.hpp b/modules/viz/include/opencv2/viz/viz3d.hpp index a610e07ade..5c88f431ef 100644 --- a/modules/viz/include/opencv2/viz/viz3d.hpp +++ b/modules/viz/include/opencv2/viz/viz3d.hpp @@ -10,6 +10,7 @@ #include #include +#include #include namespace temp_viz @@ -33,8 +34,8 @@ namespace temp_viz bool addPointCloudNormals (const Mat &cloud, const Mat& normals, int level = 100, float scale = 0.02f, const String& id = "cloud"); void showLine(const String& id, const Point3f& pt1, const Point3f& pt2, const Color& color = Color::white()); - void showPlane(const String& id, const Vec4f& coefs, const Color& color = Color::white()); - void showPlane(const String& id, const Vec4f& coefs, const Point3f& pt, const Color& color = Color::white()); + void showPlane(const String& id, const Vec4f& coeffs, const Color& color = Color::white()); + void showPlane(const String& id, const Vec4f& coeffs, const Point3f& pt, const Color& color = Color::white()); void showCube(const String& id, const Point3f& pt1, const Point3f& pt2, const Color& color = Color::white()); void showCylinder(const String& id, const Point3f& pt_on_axis, const Point3f& axis_direction, double radius, int num_sides, const Color& color = Color::white()); void showCircle(const String& id, const Point3f& pt, double radius, const Color& color = Color::white()); diff --git a/modules/viz/include/opencv2/viz/widget_accessor.hpp b/modules/viz/include/opencv2/viz/widget_accessor.hpp index 58907e14b1..3df7773ed3 100644 --- a/modules/viz/include/opencv2/viz/widget_accessor.hpp +++ b/modules/viz/include/opencv2/viz/widget_accessor.hpp @@ -1,11 +1,15 @@ #pragma once -#include +#include +#include +#include namespace temp_viz { - struct WidgetAccessor + //The class is only that depends on VTK in its interface. + //It is indended for those user who want to develop own widgets system using VTK library API. + struct CV_EXPORTS WidgetAccessor { - static CV_EXPORTS vtkSmartPointer getActor(const Widget &widget); + static vtkSmartPointer getActor(const Widget &widget); }; -} \ No newline at end of file +} diff --git a/modules/viz/include/opencv2/viz/widgets.hpp b/modules/viz/include/opencv2/viz/widgets.hpp new file mode 100644 index 0000000000..b668533107 --- /dev/null +++ b/modules/viz/include/opencv2/viz/widgets.hpp @@ -0,0 +1,38 @@ +#pragma once + +#include + + +namespace temp_viz +{ + ///////////////////////////////////////////////////////////////////////////// + /// brief The base class for all widgets + class CV_EXPORTS Widget + { + public: + Widget(); + Widget(const Widget &other); + Widget& operator =(const Widget &other); + + void copyTo(Widget &dst); + + 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; + }; + + + class CV_EXPORTS LineWidget : public Widget + { + public: + LineWidget(const Point3f &pt1, const Point3f &pt2, const Color &color = Color::white()); + }; + + +} diff --git a/modules/viz/src/precomp.hpp b/modules/viz/src/precomp.hpp index bb59ca949f..f6779d3ce1 100644 --- a/modules/viz/src/precomp.hpp +++ b/modules/viz/src/precomp.hpp @@ -154,3 +154,5 @@ #include +#include "opencv2/viz/widget_accessor.hpp" +#include diff --git a/modules/viz/src/simple_widgets.cpp b/modules/viz/src/simple_widgets.cpp new file mode 100644 index 0000000000..0fedd8c4c8 --- /dev/null +++ b/modules/viz/src/simple_widgets.cpp @@ -0,0 +1,18 @@ +#include "precomp.hpp" + + +temp_viz::LineWidget::LineWidget(const Point3f &pt1, const Point3f &pt2, const Color &color) +{ + vtkSmartPointer line = vtkSmartPointer::New(); + line->SetPoint1 (pt1.x, pt1.y, pt1.z); + line->SetPoint2 (pt2.x, pt2.y, pt2.z); + line->Update (); + + vtkSmartPointer mapper = vtkSmartPointer::New (); + mapper->SetInput(line->GetOutput ()); + + vtkSmartPointer actor = WidgetAccessor::getActor(*this); + actor->SetMapper(mapper); + + setColor(color); +} diff --git a/modules/viz/src/types.cpp b/modules/viz/src/types.cpp index 8500d2afe4..ebebf2cb58 100644 --- a/modules/viz/src/types.cpp +++ b/modules/viz/src/types.cpp @@ -20,134 +20,3 @@ 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 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(); -} - -temp_viz::Widget::Widget(const Widget &other) -{ - impl_ = other.impl_; -} - -temp_viz::Widget& temp_viz::Widget::operator =(const Widget &other) -{ - if (this != &other) - impl_ = other.impl_; - return *this; -} - -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(); -} - -#include "opencv2/viz/widget_accessor.hpp" - -vtkSmartPointer temp_viz::WidgetAccessor::getActor(const temp_viz::Widget &widget) -{ - return widget.impl_->actor; -} - -temp_viz::LineWidget::LineWidget(const Point3f &pt1, const Point3f &pt2, const Color &color) : Widget() -{ - // Create the line and set actor's data - vtkSmartPointer mapper = vtkSmartPointer::New (); - mapper->SetInput(createLine(pt1,pt2)); - temp_viz::WidgetAccessor::getActor(*this)->SetMapper(mapper); - setColor(color); -} \ No newline at end of file diff --git a/modules/viz/src/viz3d.cpp b/modules/viz/src/viz3d.cpp index dcd0bf7f18..76e7459eb9 100644 --- a/modules/viz/src/viz3d.cpp +++ b/modules/viz/src/viz3d.cpp @@ -77,14 +77,14 @@ void temp_viz::Viz3d::showLine(const String& id, const Point3f& pt1, const Point impl_->showLine(id, pt1, pt2, color); } -void temp_viz::Viz3d::showPlane(const String& id, const Vec4f &coefs, const Color& color) +void temp_viz::Viz3d::showPlane(const String& id, const Vec4f &coeffs, const Color& color) { - impl_->showPlane(id, coefs, color); + impl_->showPlane(id, coeffs, color); } -void temp_viz::Viz3d::showPlane(const String& id, const Vec4f &coefs, const Point3f& pt, const Color& color) +void temp_viz::Viz3d::showPlane(const String& id, const Vec4f &coeffs, const Point3f& pt, const Color& color) { - impl_->showPlane(id, coefs, pt, color); + impl_->showPlane(id, coeffs, pt, color); } void temp_viz::Viz3d::showCube(const String& id, const Point3f& pt1, const Point3f& pt2, const Color& color) diff --git a/modules/viz/src/viz3d_impl.cpp b/modules/viz/src/viz3d_impl.cpp index e25bca0098..627ec48d92 100644 --- a/modules/viz/src/viz3d_impl.cpp +++ b/modules/viz/src/viz3d_impl.cpp @@ -416,7 +416,7 @@ void temp_viz::Viz3d::VizImpl::showLine (const String& id, const Point3f& pt1, c } } -void temp_viz::Viz3d::VizImpl::showPlane (const String& id, const cv::Vec4f &coefs, const Color& color) +void temp_viz::Viz3d::VizImpl::showPlane (const String& id, const cv::Vec4f &coeffs, const Color& color) { // Check if this Id already exists ShapeActorMap::iterator am_it = shape_actor_map_->find (id); @@ -426,7 +426,7 @@ void temp_viz::Viz3d::VizImpl::showPlane (const String& id, const cv::Vec4f &coe if (exists) { vtkSmartPointer actor = vtkLODActor::SafeDownCast (am_it->second); - reinterpret_cast(actor->GetMapper ())->SetInput(createPlane(coefs)); + reinterpret_cast(actor->GetMapper ())->SetInput(createPlane(coeffs)); actor->GetProperty ()->SetColor (c.val); actor->GetMapper ()->ScalarVisibilityOff (); actor->Modified (); @@ -434,7 +434,7 @@ void temp_viz::Viz3d::VizImpl::showPlane (const String& id, const cv::Vec4f &coe else { // Create a plane - vtkSmartPointer data = createPlane (coefs); + vtkSmartPointer data = createPlane (coeffs); // Create an Actor vtkSmartPointer actor; @@ -451,7 +451,7 @@ void temp_viz::Viz3d::VizImpl::showPlane (const String& id, const cv::Vec4f &coe } } -void temp_viz::Viz3d::VizImpl::showPlane (const String& id ,const cv::Vec4f &coefs, const Point3f& pt, const Color& color) +void temp_viz::Viz3d::VizImpl::showPlane (const String& id ,const cv::Vec4f &coeffs, const Point3f& pt, const Color& color) { // Check if this Id already exists ShapeActorMap::iterator am_it = shape_actor_map_->find (id); @@ -461,7 +461,7 @@ void temp_viz::Viz3d::VizImpl::showPlane (const String& id ,const cv::Vec4f &coe if (exists) { vtkSmartPointer actor = vtkLODActor::SafeDownCast (am_it->second); - reinterpret_cast(actor->GetMapper ())->SetInput(createPlane(coefs, pt)); + reinterpret_cast(actor->GetMapper ())->SetInput(createPlane(coeffs, pt)); actor->GetProperty ()->SetColor (c.val); actor->GetMapper ()->ScalarVisibilityOff (); actor->Modified (); @@ -469,7 +469,7 @@ void temp_viz::Viz3d::VizImpl::showPlane (const String& id ,const cv::Vec4f &coe else { // Create a plane - vtkSmartPointer data = createPlane (coefs, pt); + vtkSmartPointer data = createPlane (coeffs, pt); // Create an Actor vtkSmartPointer actor; diff --git a/modules/viz/src/widget.cpp b/modules/viz/src/widget.cpp new file mode 100644 index 0000000000..cbc18d1190 --- /dev/null +++ b/modules/viz/src/widget.cpp @@ -0,0 +1,110 @@ +#include "precomp.hpp" + +class temp_viz::Widget::Impl +{ +public: + vtkSmartPointer actor; + + Impl() : actor(vtkSmartPointer::New()) {} + + 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 = convertToMatx(matrix); + + Affine3f updated_pose = pose * Affine3f(matrix_cv); + matrix = convertToVtkMatrix(updated_pose.matrix); + + actor->SetUserMatrix (matrix); + actor->Modified (); + } + + Affine3f getPose() const + { + vtkSmartPointer matrix = 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 + +vtkSmartPointer temp_viz::WidgetAccessor::getActor(const Widget& widget) +{ + return widget.impl_->actor; +} + +/////////////////////////////////////////////////////////////////////////////////////////////// +/// widget implementaion + +temp_viz::Widget::Widget() +{ + 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) + impl_ = other.impl_; + return *this; +} + +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(); } + + + diff --git a/modules/viz/test/test_viz3d.cpp b/modules/viz/test/test_viz3d.cpp index 1bce1a356d..8c7ecb17c3 100644 --- a/modules/viz/test/test_viz3d.cpp +++ b/modules/viz/test/test_viz3d.cpp @@ -47,7 +47,7 @@ #include #include -#include +#include #include cv::Mat cvcloud_load()