diff --git a/modules/viz/src/precomp.hpp b/modules/viz/src/precomp.hpp index d57e677c5..155612724 100644 --- a/modules/viz/src/precomp.hpp +++ b/modules/viz/src/precomp.hpp @@ -188,7 +188,7 @@ namespace cv struct VtkUtils { template - static inline void SetInputData(vtkSmartPointer filter, vtkPolyData *polydata) + static void SetInputData(vtkSmartPointer filter, vtkPolyData *polydata) { #if VTK_MAJOR_VERSION <= 5 filter->SetInput(polydata); @@ -198,7 +198,7 @@ namespace cv } template - static inline void AddInputData(vtkSmartPointer filter, vtkPolyData *polydata) + static void AddInputData(vtkSmartPointer filter, vtkPolyData *polydata) { #if VTK_MAJOR_VERSION <= 5 filter->AddInput(polydata); @@ -206,6 +206,20 @@ namespace cv filter->AddInputData(polydata); #endif } + + static vtkSmartPointer FillScalars(size_t size, const Color& color) + { + Vec3b rgb = Vec3d(color[2], color[1], color[0]); + Vec3b* color_data = new Vec3b[size]; + std::fill(color_data, color_data + size, rgb); + + vtkSmartPointer scalars = vtkSmartPointer::New(); + scalars->SetName("Colors"); + scalars->SetNumberOfComponents(3); + scalars->SetNumberOfTuples(size); + scalars->SetArray(color_data->val, size * 3, 0); + return scalars; + } }; } } diff --git a/modules/viz/src/shapes.cpp b/modules/viz/src/shapes.cpp index cd649ad23..a4401869e 100644 --- a/modules/viz/src/shapes.cpp +++ b/modules/viz/src/shapes.cpp @@ -400,15 +400,7 @@ cv::viz::WPolyLine::WPolyLine(InputArray _points, const Color &color) for(size_t i = 0; i < total; ++i) cell_array->InsertCellPoint(i); - Vec3b rgb = Vec3d(color[2], color[1], color[0]); - Vec3b* color_data = new Vec3b[total]; - std::fill(color_data, color_data + total, rgb); - - vtkSmartPointer scalars = vtkSmartPointer::New(); - scalars->SetName("Colors"); - scalars->SetNumberOfComponents(3); - scalars->SetNumberOfTuples(total); - scalars->SetArray(color_data->val, total * 3, 0); + vtkSmartPointer scalars = VtkUtils::FillScalars(total, color); vtkSmartPointer polydata = vtkSmartPointer::New(); polydata->SetPoints(points); @@ -913,9 +905,9 @@ namespace cv { namespace viz { namespace // Create frustum camera->SetViewAngle(fovy); - camera->SetPosition(0.0,0.0,0.0); - camera->SetViewUp(0.0,1.0,0.0); - camera->SetFocalPoint(0.0,0.0,1.0); + camera->SetPosition(0.0, 0.0, 0.0); + camera->SetViewUp(0.0, 1.0, 0.0); + camera->SetFocalPoint(0.0, 0.0, 1.0); camera->SetClippingRange(0.01, scale); double planesArray[24]; @@ -955,39 +947,10 @@ namespace cv { namespace viz { namespace cv::viz::WCameraPosition::WCameraPosition(double scale) { - vtkSmartPointer axes = vtkSmartPointer::New(); - axes->SetOrigin(0, 0, 0); - axes->SetScaleFactor(scale); - - vtkSmartPointer axes_colors = vtkSmartPointer::New(); - axes_colors->Allocate(6); - axes_colors->InsertNextValue(0.0); - axes_colors->InsertNextValue(0.0); - axes_colors->InsertNextValue(0.5); - axes_colors->InsertNextValue(0.5); - axes_colors->InsertNextValue(1.0); - axes_colors->InsertNextValue(1.0); - - vtkSmartPointer axes_data = axes->GetOutput(); -#if VTK_MAJOR_VERSION <= 5 - axes_data->Update(); -#else - axes->Update(); -#endif - axes_data->GetPointData()->SetScalars(axes_colors); - - vtkSmartPointer axes_tubes = vtkSmartPointer::New(); -#if VTK_MAJOR_VERSION <= 5 - axes_tubes->SetInput(axes_data); -#else - axes_tubes->SetInputData(axes_data); -#endif - axes_tubes->SetRadius(axes->GetScaleFactor() / 50.0); - axes_tubes->SetNumberOfSides(6); - vtkSmartPointer mapper = vtkSmartPointer::New(); + + VtkUtils::SetInputData(mapper, getPolyData(WCoordinateSystem(scale))); mapper->SetScalarModeToUsePointData(); - mapper->SetInputConnection(axes_tubes->GetOutputPort()); vtkSmartPointer actor = vtkSmartPointer::New(); actor->SetMapper(mapper); @@ -1035,7 +998,6 @@ cv::viz::WCameraPosition::WCameraPosition(const Matx33d &K, double scale, const setColor(color); } - cv::viz::WCameraPosition::WCameraPosition(const Vec2d &fov, double scale, const Color &color) { vtkSmartPointer camera = vtkSmartPointer::New(); @@ -1299,28 +1261,19 @@ cv::viz::WTrajectorySpheres::WTrajectorySpheres(const std::vector &pat line_scalars->InsertNextTuple3(line_color[2], line_color[1], line_color[0]); // Create color array for sphere - vtkSphereSource * dummy_sphere = vtkSphereSource::New(); + vtkSmartPointer dummy_sphere = vtkSmartPointer::New(); // Create the array for big sphere dummy_sphere->SetRadius(init_sphere_radius); dummy_sphere->Update(); vtkIdType nr_points = dummy_sphere->GetOutput()->GetNumberOfCells(); - vtkSmartPointer sphere_scalars_init = vtkSmartPointer::New(); - sphere_scalars_init->SetNumberOfComponents(3); - sphere_scalars_init->SetNumberOfTuples(nr_points); - sphere_scalars_init->FillComponent(0, sphere_color[2]); - sphere_scalars_init->FillComponent(1, sphere_color[1]); - sphere_scalars_init->FillComponent(2, sphere_color[0]); + vtkSmartPointer sphere_scalars_init = VtkUtils::FillScalars(nr_points, sphere_color); + // Create the array for small sphere dummy_sphere->SetRadius(sphere_radius); dummy_sphere->Update(); nr_points = dummy_sphere->GetOutput()->GetNumberOfCells(); - vtkSmartPointer sphere_scalars = vtkSmartPointer::New(); - sphere_scalars->SetNumberOfComponents(3); - sphere_scalars->SetNumberOfTuples(nr_points); - sphere_scalars->FillComponent(0, sphere_color[2]); - sphere_scalars->FillComponent(1, sphere_color[1]); - sphere_scalars->FillComponent(2, sphere_color[0]); - dummy_sphere->Delete(); + vtkSmartPointer sphere_scalars = VtkUtils::FillScalars(nr_points, sphere_color); + for (vtkIdType i = 0; i < nr_poses; ++i) {