diff --git a/modules/viz/include/opencv2/viz/types.hpp b/modules/viz/include/opencv2/viz/types.hpp index 92b277857..d64debc81 100644 --- a/modules/viz/include/opencv2/viz/types.hpp +++ b/modules/viz/include/opencv2/viz/types.hpp @@ -22,6 +22,7 @@ namespace temp_viz typedef cv::Matx33d Matx33d; typedef cv::Affine3f Affine3f; typedef cv::Affine3d Affine3d; + typedef cv::Point2i Point2i; typedef cv::Point3f Point3f; typedef cv::Point3d Point3d; typedef cv::Matx44d Matx44d; diff --git a/modules/viz/include/opencv2/viz/widget_accessor.hpp b/modules/viz/include/opencv2/viz/widget_accessor.hpp index 4b1c5ef15..0cc39f7ff 100644 --- a/modules/viz/include/opencv2/viz/widget_accessor.hpp +++ b/modules/viz/include/opencv2/viz/widget_accessor.hpp @@ -12,6 +12,6 @@ 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 vtkSmartPointer getActor(const Widget &widget); }; } diff --git a/modules/viz/include/opencv2/viz/widgets.hpp b/modules/viz/include/opencv2/viz/widgets.hpp index cc70d268a..eaf9e6286 100644 --- a/modules/viz/include/opencv2/viz/widgets.hpp +++ b/modules/viz/include/opencv2/viz/widgets.hpp @@ -22,13 +22,17 @@ namespace temp_viz void setPose(const Affine3f &pose); void updatePose(const Affine3f &pose); Affine3f getPose() const; - + + protected: + Widget(bool text_widget); + private: class Impl; Impl* impl_; void create(); void release(); + void create(bool text_widget); friend struct WidgetAccessor; }; @@ -85,5 +89,12 @@ namespace temp_viz public: CoordinateSystemWidget(double scale, const Affine3f& affine); }; - + + class CV_EXPORTS TextWidget : public Widget + { + 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 + }; } diff --git a/modules/viz/src/simple_widgets.cpp b/modules/viz/src/simple_widgets.cpp index bcc8beaf1..7ae822515 100644 --- a/modules/viz/src/simple_widgets.cpp +++ b/modules/viz/src/simple_widgets.cpp @@ -12,7 +12,7 @@ temp_viz::LineWidget::LineWidget(const Point3f &pt1, const Point3f &pt2, const C vtkSmartPointer mapper = vtkSmartPointer::New (); mapper->SetInput(line->GetOutput ()); - vtkSmartPointer actor = WidgetAccessor::getActor(*this); + vtkSmartPointer actor = vtkLODActor::SafeDownCast(WidgetAccessor::getActor(*this)); actor->SetMapper(mapper); setColor(color); @@ -20,13 +20,13 @@ temp_viz::LineWidget::LineWidget(const Point3f &pt1, const Point3f &pt2, const C void temp_viz::LineWidget::setLineWidth(float line_width) { - vtkSmartPointer actor = WidgetAccessor::getActor(*this); + vtkSmartPointer actor = vtkLODActor::SafeDownCast(WidgetAccessor::getActor(*this)); actor->GetProperty()->SetLineWidth(line_width); } float temp_viz::LineWidget::getLineWidth() { - vtkSmartPointer actor = WidgetAccessor::getActor(*this); + vtkSmartPointer actor = vtkLODActor::SafeDownCast(WidgetAccessor::getActor(*this)); return actor->GetProperty()->GetLineWidth(); } @@ -43,7 +43,7 @@ temp_viz::PlaneWidget::PlaneWidget(const Vec4f& coefs, double size, const Color vtkSmartPointer mapper = vtkSmartPointer::New (); mapper->SetInput(plane->GetOutput ()); - vtkSmartPointer actor = WidgetAccessor::getActor(*this); + vtkSmartPointer actor = vtkLODActor::SafeDownCast(WidgetAccessor::getActor(*this)); actor->SetMapper(mapper); actor->SetScale(size); @@ -64,7 +64,7 @@ temp_viz::PlaneWidget::PlaneWidget(const Vec4f& coefs, const Point3f& pt, double vtkSmartPointer mapper = vtkSmartPointer::New (); mapper->SetInput(plane->GetOutput ()); - vtkSmartPointer actor = WidgetAccessor::getActor(*this); + vtkSmartPointer actor = vtkLODActor::SafeDownCast(WidgetAccessor::getActor(*this)); actor->SetMapper(mapper); actor->SetScale(size); @@ -87,7 +87,7 @@ temp_viz::SphereWidget::SphereWidget(const cv::Point3f ¢er, float radius, in vtkSmartPointer mapper = vtkSmartPointer::New (); mapper->SetInput(sphere->GetOutput ()); - vtkSmartPointer actor = WidgetAccessor::getActor(*this); + vtkSmartPointer actor = vtkLODActor::SafeDownCast(WidgetAccessor::getActor(*this)); actor->SetMapper(mapper); setColor(color); @@ -149,7 +149,7 @@ temp_viz::ArrowWidget::ArrowWidget(const Point3f& pt1, const Point3f& pt2, const vtkSmartPointer mapper = vtkSmartPointer::New (); mapper->SetInput(transformPD->GetOutput ()); - vtkSmartPointer actor = WidgetAccessor::getActor(*this); + vtkSmartPointer actor = vtkLODActor::SafeDownCast(WidgetAccessor::getActor(*this)); actor->SetMapper(mapper); setColor(color); @@ -178,7 +178,7 @@ temp_viz::CircleWidget::CircleWidget(const temp_viz::Point3f& pt, double radius, vtkSmartPointer mapper = vtkSmartPointer::New (); mapper->SetInput(tf->GetOutput ()); - vtkSmartPointer actor = WidgetAccessor::getActor(*this); + vtkSmartPointer actor = vtkLODActor::SafeDownCast(WidgetAccessor::getActor(*this)); actor->SetMapper(mapper); setColor(color); @@ -202,7 +202,7 @@ temp_viz::CylinderWidget::CylinderWidget(const Point3f& pt_on_axis, const Point3 vtkSmartPointer mapper = vtkSmartPointer::New (); mapper->SetInput(tuber->GetOutput ()); - vtkSmartPointer actor = WidgetAccessor::getActor(*this); + vtkSmartPointer actor = vtkLODActor::SafeDownCast(WidgetAccessor::getActor(*this)); actor->SetMapper(mapper); setColor(color); @@ -219,7 +219,7 @@ temp_viz::CubeWidget::CubeWidget(const Point3f& pt_min, const Point3f& pt_max, c vtkSmartPointer mapper = vtkSmartPointer::New (); mapper->SetInput(cube->GetOutput ()); - vtkSmartPointer actor = WidgetAccessor::getActor(*this); + vtkSmartPointer actor = vtkLODActor::SafeDownCast(WidgetAccessor::getActor(*this)); actor->SetMapper(mapper); setColor(color); @@ -256,7 +256,7 @@ temp_viz::CoordinateSystemWidget::CoordinateSystemWidget(double scale, const Aff mapper->SetScalarModeToUsePointData (); mapper->SetInput(axes_tubes->GetOutput ()); - vtkSmartPointer actor = WidgetAccessor::getActor(*this); + vtkSmartPointer actor = vtkLODActor::SafeDownCast(WidgetAccessor::getActor(*this)); actor->SetMapper(mapper); cv::Vec3d t = affine.translation(); @@ -273,3 +273,19 @@ 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]); } + +temp_viz::TextWidget::TextWidget(const String &text, const Point2i &pos, int font_size, const Color &color) : Widget(true) +{ + vtkSmartPointer actor = vtkTextActor::SafeDownCast(WidgetAccessor::getActor(*this)); + actor->SetPosition (pos.x, pos.y); + actor->SetInput (text.c_str ()); + + vtkSmartPointer tprop = actor->GetTextProperty (); + tprop->SetFontSize (font_size); + tprop->SetFontFamilyToArial (); + tprop->SetJustificationToLeft (); + tprop->BoldOn (); + + Color c = vtkcolor(color); + tprop->SetColor (c.val); +} diff --git a/modules/viz/src/viz3d_impl.cpp b/modules/viz/src/viz3d_impl.cpp index 07a25abc7..0eb0bd082 100644 --- a/modules/viz/src/viz3d_impl.cpp +++ b/modules/viz/src/viz3d_impl.cpp @@ -874,12 +874,15 @@ 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 - vtkSmartPointer actor = vtkLODActor::SafeDownCast(WidgetAccessor::getActor(widget)); - vtkSmartPointer matrix = convertToVtkMatrix(pose.matrix); - actor->SetUserMatrix (matrix); - actor->Modified(); - renderer_->AddActor(actor); - (*widget_actor_map_)[id].actor = actor; + vtkSmartPointer actor; + if (actor = vtkLODActor::SafeDownCast(WidgetAccessor::getActor(widget))) + { + vtkSmartPointer matrix = convertToVtkMatrix(pose.matrix); + actor->SetUserMatrix (matrix); + actor->Modified(); + } + renderer_->AddActor(WidgetAccessor::getActor(widget)); + (*widget_actor_map_)[id].actor = WidgetAccessor::getActor(widget); } bool temp_viz::Viz3d::VizImpl::removeWidget(const String &id) diff --git a/modules/viz/src/widget.cpp b/modules/viz/src/widget.cpp index d35b10d70..e51f86770 100644 --- a/modules/viz/src/widget.cpp +++ b/modules/viz/src/widget.cpp @@ -3,34 +3,45 @@ class temp_viz::Widget::Impl { public: - vtkSmartPointer actor; + 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) { + vtkSmartPointer lod_actor = vtkLODActor::SafeDownCast(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 (); + 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) { + vtkSmartPointer lod_actor = vtkLODActor::SafeDownCast(actor); vtkSmartPointer matrix = convertToVtkMatrix(pose.matrix); - actor->SetUserMatrix (matrix); - actor->Modified (); + lod_actor->SetUserMatrix (matrix); + lod_actor->Modified (); } void updatePose(const Affine3f& pose) { - vtkSmartPointer matrix = actor->GetUserMatrix(); + vtkSmartPointer lod_actor = vtkLODActor::SafeDownCast(actor); + vtkSmartPointer matrix = lod_actor->GetUserMatrix(); if (!matrix) { setPose(pose); @@ -41,13 +52,14 @@ public: Affine3f updated_pose = pose * Affine3f(matrix_cv); matrix = convertToVtkMatrix(updated_pose.matrix); - actor->SetUserMatrix (matrix); - actor->Modified (); + lod_actor->SetUserMatrix (matrix); + lod_actor->Modified (); } Affine3f getPose() const { - vtkSmartPointer matrix = actor->GetUserMatrix(); + vtkSmartPointer lod_actor = vtkLODActor::SafeDownCast(actor); + vtkSmartPointer matrix = lod_actor->GetUserMatrix(); Matx44f matrix_cv = convertToMatx(matrix); return Affine3f(matrix_cv); } @@ -77,7 +89,7 @@ protected: /////////////////////////////////////////////////////////////////////////////////////////////// /// stream accessor implementaion -vtkSmartPointer temp_viz::WidgetAccessor::getActor(const Widget& widget) +vtkSmartPointer temp_viz::WidgetAccessor::getActor(const Widget& widget) { return widget.impl_->actor; } @@ -90,6 +102,11 @@ temp_viz::Widget::Widget() : impl_(0) 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_) @@ -140,3 +157,11 @@ void temp_viz::Widget::release() } } +void temp_viz::Widget::create(bool text_widget) +{ + if (impl_) + release(); + impl_ = new Impl(text_widget); + impl_->ref_counter = 1; +} + diff --git a/modules/viz/test/test_viz3d.cpp b/modules/viz/test/test_viz3d.cpp index e574449cf..4c586524f 100644 --- a/modules/viz/test/test_viz3d.cpp +++ b/modules/viz/test/test_viz3d.cpp @@ -98,6 +98,7 @@ TEST(Viz_viz3d, accuracy) temp_viz::CylinderWidget cyw(cv::Point3f(0,0,0), cv::Point3f(-1,-1,-1), 0.5, 30, temp_viz::Color(0,255,0)); temp_viz::CubeWidget cuw(cv::Point3f(-2,-2,-2), cv::Point3f(-1,-1,-1), temp_viz::Color(0,0,255)); temp_viz::CoordinateSystemWidget csw(1.0f, cv::Affine3f::Identity()); + temp_viz::TextWidget tw("TEST", cv::Point2i(100,100), 20); // v.showWidget("line", lw); v.showWidget("plane", pw); @@ -107,6 +108,7 @@ TEST(Viz_viz3d, accuracy) // v.showWidget("cylinder", cyw); // v.showWidget("cube", cuw); v.showWidget("coordinateSystem", csw); + v.showWidget("text",tw); temp_viz::LineWidget lw2 = lw;