diff --git a/modules/viz/include/opencv2/viz/types.hpp b/modules/viz/include/opencv2/viz/types.hpp index 3698d556b3..27fc074267 100644 --- a/modules/viz/include/opencv2/viz/types.hpp +++ b/modules/viz/include/opencv2/viz/types.hpp @@ -118,17 +118,22 @@ namespace temp_viz { 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; }; + + class LineWidget : public Widget + { + public: + LineWidget(const Point3f &pt1, const Point3f &pt2, const Color &color); + }; } diff --git a/modules/viz/include/opencv2/viz/viz3d.hpp b/modules/viz/include/opencv2/viz/viz3d.hpp index 46d0be04c3..a610e07ade 100644 --- a/modules/viz/include/opencv2/viz/viz3d.hpp +++ b/modules/viz/include/opencv2/viz/viz3d.hpp @@ -69,6 +69,8 @@ namespace temp_viz void registerMouseCallback(void (*callback)(const MouseEvent&, void*), void* cookie = 0); bool wasStopped() const; + + void showWidget(const String &id, const Widget &widget); private: Viz3d(const Viz3d&); Viz3d& operator=(const Viz3d&); diff --git a/modules/viz/include/opencv2/viz/widget_accessor.hpp b/modules/viz/include/opencv2/viz/widget_accessor.hpp index 2c0437c5a8..58907e14b1 100644 --- a/modules/viz/include/opencv2/viz/widget_accessor.hpp +++ b/modules/viz/include/opencv2/viz/widget_accessor.hpp @@ -1,12 +1,11 @@ #pragma once -#include "precomp.hpp" -#include "types.hpp" +#include namespace temp_viz { struct WidgetAccessor { - static CV_EXPORTS vtkSmartPointer getActor(const Widget &widget); + static CV_EXPORTS vtkSmartPointer getActor(const Widget &widget); }; } \ No newline at end of file diff --git a/modules/viz/src/q/viz3d_impl.hpp b/modules/viz/src/q/viz3d_impl.hpp index d80881c924..9544a31421 100644 --- a/modules/viz/src/q/viz3d_impl.hpp +++ b/modules/viz/src/q/viz3d_impl.hpp @@ -245,6 +245,9 @@ public: void setPosition (int x, int y); void setSize (int xw, int yw); + + void showWidget(const String &id, const Widget &widget); + void all_data(); private: vtkSmartPointer interactor_; @@ -311,6 +314,9 @@ private: /** \brief Internal list with actor pointers and name IDs for shapes. */ cv::Ptr shape_actor_map_; + + /** \brief Internal list with actor pointers and name IDs for all widget actors */ + cv::Ptr widget_actor_map_; /** \brief Boolean that holds whether or not the camera parameters were manually initialized*/ bool camera_set_; diff --git a/modules/viz/src/q/viz_types.h b/modules/viz/src/q/viz_types.h index 1acfa4b1ad..3dbb44f18f 100644 --- a/modules/viz/src/q/viz_types.h +++ b/modules/viz/src/q/viz_types.h @@ -15,8 +15,17 @@ namespace temp_viz /** \brief Internal cell array. Used for optimizing updatePointCloud. */ vtkSmartPointer cells; }; + + // TODO This will be used to contain both cloud and shape actors + struct CV_EXPORTS WidgetActor + { + vtkSmartPointer actor; + vtkSmartPointer viewpoint_transformation_; + vtkSmartPointer cells; + }; typedef std::map CloudActorMap; typedef std::map > ShapeActorMap; + typedef std::map WidgetActorMap; } diff --git a/modules/viz/src/types.cpp b/modules/viz/src/types.cpp index d717c305e9..5670f759a6 100644 --- a/modules/viz/src/types.cpp +++ b/modules/viz/src/types.cpp @@ -24,28 +24,18 @@ class temp_viz::Widget::Impl { public: String id; - vtkSmartPointer actor; + vtkSmartPointer actor; Impl() { - actor = vtkSmartPointer:: New(); + actor = vtkSmartPointer::New (); } - vtkSmartPointer getActor() + 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); @@ -85,11 +75,22 @@ public: return Affine3f(matrix_cv); } + void setActorMapperInput(const vtkSmartPointer &data) + { + vtkSmartPointer mapper = reinterpret_cast(actor->GetMapper ()); + if (mapper == 0) + { + mapper = vtkSmartPointer::New (); + actor->SetMapper(mapper); + } + mapper->SetInput (data); + } + protected: vtkSmartPointer convertToVtkMatrix (const cv::Matx44f &m) const { - vtkSmartPointer vtk_matrix = vtkSmartPointer::New(); + 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)); @@ -101,20 +102,12 @@ protected: 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) @@ -122,11 +115,6 @@ 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); @@ -147,11 +135,18 @@ 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) +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 3df43fab68..dcd0bf7f18 100644 --- a/modules/viz/src/viz3d.cpp +++ b/modules/viz/src/viz3d.cpp @@ -139,3 +139,7 @@ void temp_viz::Viz3d::registerMouseCallback(void (*callback)(const MouseEvent&, bool temp_viz::Viz3d::wasStopped() const { return impl_->wasStopped(); } +void temp_viz::Viz3d::showWidget(const String &id, const Widget &widget) +{ + impl_->showWidget(id, widget); +} diff --git a/modules/viz/src/viz3d_impl.cpp b/modules/viz/src/viz3d_impl.cpp index 7c97b20d7e..e25bca0098 100644 --- a/modules/viz/src/viz3d_impl.cpp +++ b/modules/viz/src/viz3d_impl.cpp @@ -1168,3 +1168,18 @@ bool temp_viz::Viz3d::VizImpl::addPolygon (const cv::Mat& cloud, const Color& co return (true); } + +#include "opencv2/viz/widget_accessor.hpp" + +void temp_viz::Viz3d::VizImpl::showWidget(const String &id, const Widget &widget) +{ + WidgetActorMap::iterator wam_itr = widget_actor_map_->find(id); + bool exists = !(wam_itr == widget_actor_map_->end()); + if (exists) + { + // Remove it if it exists and add it again + removeActorFromRenderer(wam_itr->second.actor); + } + renderer_->AddActor(WidgetAccessor::getActor(widget)); + (*widget_actor_map_)[id].actor = WidgetAccessor::getActor(widget); +} diff --git a/modules/viz/src/viz_main.cpp b/modules/viz/src/viz_main.cpp index 4552790abc..eeb72c4150 100644 --- a/modules/viz/src/viz_main.cpp +++ b/modules/viz/src/viz_main.cpp @@ -17,6 +17,7 @@ temp_viz::Viz3d::VizImpl::VizImpl (const std::string &name) : style_ (vtkSmartPointer::New ()) , cloud_actor_map_ (new CloudActorMap) , shape_actor_map_ (new ShapeActorMap) + , widget_actor_map_ (new WidgetActorMap) , s_lastDone_(0.0) { renderer_ = vtkSmartPointer::New (); diff --git a/modules/viz/test/test_viz3d.cpp b/modules/viz/test/test_viz3d.cpp index c80845f1f1..31b85f9e09 100644 --- a/modules/viz/test/test_viz3d.cpp +++ b/modules/viz/test/test_viz3d.cpp @@ -94,6 +94,8 @@ TEST(Viz_viz3d, accuracy) v.showCircle("circle1", cv::Point3f(0,0,0), 1.0, temp_viz::Color(0,255,0)); v.showSphere("sphere1", cv::Point3f(0,0,0), 0.5, temp_viz::Color(0,0,255)); v.showArrow("arrow1", cv::Point3f(0,0,0), cv::Point3f(1,1,1), temp_viz::Color(255,0,0)); + temp_viz::LineWidget lw(cv::Point3f(0.0,0.0,0.0), cv::Point3f(1.0,1.0,1.0), temp_viz::Color(0,255,0)); + v.showWidget("line", lw); while(!v.wasStopped()) { @@ -110,6 +112,7 @@ TEST(Viz_viz3d, accuracy) v.setShapePose("circle1", cloudPosition); v.setShapePose("sphere1", cloudPosition); v.setShapePose("arrow1", cloudPosition); + lw.setColor(temp_viz::Color(col_blue, col_green, col_red)); angle_x += 0.1f; angle_y -= 0.1f;