text widget implementation
authorozantonkal <ozantonkal@gmail.com>
Mon, 8 Jul 2013 08:41:42 +0000 (10:41 +0200)
committerozantonkal <ozantonkal@gmail.com>
Mon, 8 Jul 2013 08:41:42 +0000 (10:41 +0200)
modules/viz/include/opencv2/viz/types.hpp
modules/viz/include/opencv2/viz/widget_accessor.hpp
modules/viz/include/opencv2/viz/widgets.hpp
modules/viz/src/simple_widgets.cpp
modules/viz/src/viz3d_impl.cpp
modules/viz/src/widget.cpp
modules/viz/test/test_viz3d.cpp

index 92b2778..d64debc 100644 (file)
@@ -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;
index 4b1c5ef..0cc39f7 100644 (file)
@@ -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<vtkLODActor> getActor(const Widget &widget);
+        static vtkSmartPointer<vtkProp> getActor(const Widget &widget);
     };
 }
index cc70d26..eaf9e62 100644 (file)
@@ -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
+    };
 }
index bcc8bea..7ae8225 100644 (file)
@@ -12,7 +12,7 @@ temp_viz::LineWidget::LineWidget(const Point3f &pt1, const Point3f &pt2, const C
     vtkSmartPointer<vtkDataSetMapper> mapper = vtkSmartPointer<vtkDataSetMapper>::New ();
     mapper->SetInput(line->GetOutput ());
 
-    vtkSmartPointer<vtkLODActor> actor = WidgetAccessor::getActor(*this);
+    vtkSmartPointer<vtkLODActor> 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<vtkLODActor> actor = WidgetAccessor::getActor(*this);
+    vtkSmartPointer<vtkLODActor> actor = vtkLODActor::SafeDownCast(WidgetAccessor::getActor(*this));
     actor->GetProperty()->SetLineWidth(line_width);
 }
 
 float temp_viz::LineWidget::getLineWidth()
 {
-    vtkSmartPointer<vtkLODActor> actor = WidgetAccessor::getActor(*this);
+    vtkSmartPointer<vtkLODActor> 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<vtkDataSetMapper> mapper = vtkSmartPointer<vtkDataSetMapper>::New ();
     mapper->SetInput(plane->GetOutput ());
     
-    vtkSmartPointer<vtkLODActor> actor = WidgetAccessor::getActor(*this);
+    vtkSmartPointer<vtkLODActor> 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<vtkDataSetMapper> mapper = vtkSmartPointer<vtkDataSetMapper>::New ();
     mapper->SetInput(plane->GetOutput ());
 
-    vtkSmartPointer<vtkLODActor> actor = WidgetAccessor::getActor(*this);
+    vtkSmartPointer<vtkLODActor> actor = vtkLODActor::SafeDownCast(WidgetAccessor::getActor(*this));
     actor->SetMapper(mapper);
     actor->SetScale(size);
 
@@ -87,7 +87,7 @@ temp_viz::SphereWidget::SphereWidget(const cv::Point3f &center, float radius, in
     vtkSmartPointer<vtkDataSetMapper> mapper = vtkSmartPointer<vtkDataSetMapper>::New ();
     mapper->SetInput(sphere->GetOutput ());
 
-    vtkSmartPointer<vtkLODActor> actor = WidgetAccessor::getActor(*this);
+    vtkSmartPointer<vtkLODActor> 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<vtkDataSetMapper> mapper = vtkSmartPointer<vtkDataSetMapper>::New ();
     mapper->SetInput(transformPD->GetOutput ());
 
-    vtkSmartPointer<vtkLODActor> actor = WidgetAccessor::getActor(*this);
+    vtkSmartPointer<vtkLODActor> 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<vtkDataSetMapper> mapper = vtkSmartPointer<vtkDataSetMapper>::New ();
     mapper->SetInput(tf->GetOutput ());
 
-    vtkSmartPointer<vtkLODActor> actor = WidgetAccessor::getActor(*this);
+    vtkSmartPointer<vtkLODActor> 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<vtkDataSetMapper> mapper = vtkSmartPointer<vtkDataSetMapper>::New ();
     mapper->SetInput(tuber->GetOutput ());
 
-    vtkSmartPointer<vtkLODActor> actor = WidgetAccessor::getActor(*this);
+    vtkSmartPointer<vtkLODActor> 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<vtkDataSetMapper> mapper = vtkSmartPointer<vtkDataSetMapper>::New ();
     mapper->SetInput(cube->GetOutput ());
 
-    vtkSmartPointer<vtkLODActor> actor = WidgetAccessor::getActor(*this);
+    vtkSmartPointer<vtkLODActor> 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<vtkLODActor> actor = WidgetAccessor::getActor(*this);
+    vtkSmartPointer<vtkLODActor> 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<vtkTextActor> actor = vtkTextActor::SafeDownCast(WidgetAccessor::getActor(*this));
+    actor->SetPosition (pos.x, pos.y);
+    actor->SetInput (text.c_str ());
+
+    vtkSmartPointer<vtkTextProperty> tprop = actor->GetTextProperty ();
+    tprop->SetFontSize (font_size);
+    tprop->SetFontFamilyToArial ();
+    tprop->SetJustificationToLeft ();
+    tprop->BoldOn ();
+
+    Color c = vtkcolor(color);
+    tprop->SetColor (c.val);
+}
index 07a25ab..0eb0bd0 100644 (file)
@@ -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<vtkLODActor> actor = vtkLODActor::SafeDownCast(WidgetAccessor::getActor(widget));
-    vtkSmartPointer<vtkMatrix4x4> matrix = convertToVtkMatrix(pose.matrix);
-    actor->SetUserMatrix (matrix);
-    actor->Modified();
-    renderer_->AddActor(actor);
-    (*widget_actor_map_)[id].actor = actor;
+    vtkSmartPointer<vtkLODActor> actor;
+    if (actor = vtkLODActor::SafeDownCast(WidgetAccessor::getActor(widget)))
+    {
+        vtkSmartPointer<vtkMatrix4x4> 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)
index d35b10d..e51f867 100644 (file)
@@ -3,34 +3,45 @@
 class temp_viz::Widget::Impl
 {
 public:
-    vtkSmartPointer<vtkLODActor> actor;
+    vtkSmartPointer<vtkProp> actor;
     int ref_counter;
 
     Impl() : actor(vtkSmartPointer<vtkLODActor>::New()) {}
+    
+    Impl(bool text_widget) 
+    {
+        if (text_widget)
+            actor = vtkSmartPointer<vtkTextActor>::New();
+        else
+            actor = vtkSmartPointer<vtkLeaderActor2D>::New();
+    }
 
     void setColor(const Color& color)
     {
+        vtkSmartPointer<vtkLODActor> 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<vtkLODActor> lod_actor = vtkLODActor::SafeDownCast(actor);
         vtkSmartPointer<vtkMatrix4x4> matrix = convertToVtkMatrix(pose.matrix);
-        actor->SetUserMatrix (matrix);
-        actor->Modified ();
+        lod_actor->SetUserMatrix (matrix);
+        lod_actor->Modified ();
     }
 
     void updatePose(const Affine3f& pose)
     {
-        vtkSmartPointer<vtkMatrix4x4> matrix = actor->GetUserMatrix();
+        vtkSmartPointer<vtkLODActor> lod_actor = vtkLODActor::SafeDownCast(actor);
+        vtkSmartPointer<vtkMatrix4x4> 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<vtkMatrix4x4> matrix = actor->GetUserMatrix();
+        vtkSmartPointer<vtkLODActor> lod_actor = vtkLODActor::SafeDownCast(actor);
+        vtkSmartPointer<vtkMatrix4x4> matrix = lod_actor->GetUserMatrix();
         Matx44f matrix_cv = convertToMatx(matrix);
         return Affine3f(matrix_cv);
     }
@@ -77,7 +89,7 @@ protected:
 ///////////////////////////////////////////////////////////////////////////////////////////////
 /// stream accessor implementaion
 
-vtkSmartPointer<vtkLODActor> temp_viz::WidgetAccessor::getActor(const Widget& widget)
+vtkSmartPointer<vtkProp> 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;
+}
+
index e574449..4c58652 100644 (file)
@@ -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;