From e185900270be277c96d2a530344b4ade39d7b5e9 Mon Sep 17 00:00:00 2001 From: ozantonkal Date: Wed, 10 Jul 2013 15:34:19 +0200 Subject: [PATCH] grid widget implementation --- modules/viz/include/opencv2/viz/widgets.hpp | 7 +++++ modules/viz/src/simple_widgets.cpp | 32 +++++++++++++++++++++ modules/viz/test/test_viz3d.cpp | 15 +++++++--- 3 files changed, 50 insertions(+), 4 deletions(-) diff --git a/modules/viz/include/opencv2/viz/widgets.hpp b/modules/viz/include/opencv2/viz/widgets.hpp index cd1ed94533..85ebda0209 100644 --- a/modules/viz/include/opencv2/viz/widgets.hpp +++ b/modules/viz/include/opencv2/viz/widgets.hpp @@ -115,6 +115,12 @@ namespace temp_viz struct CopyImpl; }; + class CV_EXPORTS GridWidget : public Widget3D + { + public: + GridWidget(Vec2i dimensions, Vec2d spacing, const Color &color = Color::white()); + }; + class CV_EXPORTS TextWidget : public Widget2D { public: @@ -154,6 +160,7 @@ namespace temp_viz template<> CV_EXPORTS CubeWidget Widget::cast(); template<> CV_EXPORTS CoordinateSystemWidget Widget::cast(); template<> CV_EXPORTS PolyLineWidget Widget::cast(); + template<> CV_EXPORTS GridWidget 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 a9ed2ef8fc..f3ecdb9dc9 100644 --- a/modules/viz/src/simple_widgets.cpp +++ b/modules/viz/src/simple_widgets.cpp @@ -418,6 +418,38 @@ template<> temp_viz::PolyLineWidget temp_viz::Widget::cast(widget); } +/////////////////////////////////////////////////////////////////////////////////////////////// +/// grid widget implementation + +temp_viz::GridWidget::GridWidget(Vec2i dimensions, Vec2d spacing, const Color &color) +{ + // Create the grid using image data + vtkSmartPointer grid = vtkSmartPointer::New(); + + // Add 1 to dimensions because in ImageData dimensions is the number of lines + // - however here it means number of cells + grid->SetDimensions(dimensions[0]+1, dimensions[1]+1, 1); + grid->SetSpacing(spacing[0], spacing[1], 0.); + + // Set origin of the grid to be the middle of the grid + grid->SetOrigin(dimensions[0] * spacing[0] * (-0.5), dimensions[1] * spacing[1] * (-0.5), 0); + + vtkSmartPointer mapper = vtkSmartPointer::New(); + mapper->SetInput(grid); + vtkSmartPointer actor = vtkSmartPointer::New(); + actor->SetMapper(mapper); + + // Show it as wireframe + actor->GetProperty ()->SetRepresentationToWireframe (); + WidgetAccessor::setProp(*this, actor); +} + +template<> temp_viz::GridWidget 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 fb8a9f6dae..7a48bb9a98 100644 --- a/modules/viz/test/test_viz3d.cpp +++ b/modules/viz/test/test_viz3d.cpp @@ -136,8 +136,13 @@ TEST(Viz_viz3d, accuracy) points = points.reshape(0, 2); temp_viz::PolyLineWidget plw(points); - v.showWidget("polyline",plw); - lw = v.getWidget("polyline").cast(); +// v.showWidget("polyline",plw); +// lw = v.getWidget("polyline").cast(); + + 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(); +// float grid_x_angle = 0.0; while(!v.wasStopped()) { @@ -145,8 +150,8 @@ TEST(Viz_viz3d, accuracy) cv::Affine3f cloudPosition(angle_x, angle_y, angle_z, cv::Vec3f(pos_x, pos_y, pos_z)); cv::Affine3f cloudPosition2(angle_x, angle_y, angle_z, cv::Vec3f(pos_x+0.2, pos_y+0.2, pos_z+0.2)); -// lw2.setColor(temp_viz::Color(col_blue, col_green, col_red)); - lw.setLineWidth(pos_x * 10); + lw.setColor(temp_viz::Color(col_blue, col_green, col_red)); +// lw.setLineWidth(pos_x * 10); plw.setColor(temp_viz::Color(col_blue, col_green, col_red)); @@ -167,6 +172,8 @@ TEST(Viz_viz3d, accuracy) cnw.setColor(temp_viz::Color(col_blue, col_green, col_red)); pcw2.setColor(temp_viz::Color(col_blue, col_green, col_red)); + gw.updatePose(temp_viz::Affine3f(0.0, 0.1, 0.0, cv::Vec3f(0.0,0.0,0.0))); + angle_x += 0.1f; angle_y -= 0.1f; angle_z += 0.1f;