From 5813a3a99d7da8cbc060d8ac60486da91c0af2cd Mon Sep 17 00:00:00 2001 From: ozantonkal Date: Wed, 10 Jul 2013 16:34:47 +0200 Subject: [PATCH] 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())