From c4b3b920461f9c7207d4c184a5bedf7608a51ed1 Mon Sep 17 00:00:00 2001 From: Maksim Shabunin Date: Sat, 30 May 2020 06:09:02 +0300 Subject: [PATCH 1/3] Added VTK 9 support --- modules/viz/CMakeLists.txt | 4 +++- modules/viz/src/precomp.hpp | 8 +++++++- modules/viz/src/types.cpp | 3 ++- modules/viz/src/vizimpl.cpp | 13 +++++++++++-- modules/viz/src/vtk/vtkOBJWriter.cpp | 14 +++++++++----- modules/viz/src/vtk/vtkXYZReader.cpp | 2 +- modules/viz/src/vtk/vtkXYZWriter.cpp | 2 +- 7 files changed, 34 insertions(+), 12 deletions(-) diff --git a/modules/viz/CMakeLists.txt b/modules/viz/CMakeLists.txt index 89a9c3e098..3426e1dd26 100644 --- a/modules/viz/CMakeLists.txt +++ b/modules/viz/CMakeLists.txt @@ -3,7 +3,9 @@ if(NOT HAVE_VTK) endif() set(the_description "Viz") -include(${VTK_USE_FILE}) +if(VTK_VERSION VERSION_LESS 8.90) + include(${VTK_USE_FILE}) +endif() if(NOT BUILD_SHARED_LIBS) # We observed conflict between builtin 3rdparty libraries and diff --git a/modules/viz/src/precomp.hpp b/modules/viz/src/precomp.hpp index f92fdb6ac2..4c4bf7c599 100644 --- a/modules/viz/src/precomp.hpp +++ b/modules/viz/src/precomp.hpp @@ -133,7 +133,8 @@ #include #include #include -#include "vtkCallbackCommand.h" +#include +#include #if !defined(_WIN32) || defined(__CYGWIN__) # include /* unlink */ @@ -149,6 +150,11 @@ #include "vtk/vtkTrajectorySource.h" #include "vtk/vtkImageMatSource.h" +#if VTK_MAJOR_VERSION >= 9 +typedef vtkIdType const * CellIterT; +#else +typedef vtkIdType * CellIterT; +#endif #include #include diff --git a/modules/viz/src/types.cpp b/modules/viz/src/types.cpp index 65571a192e..0e14477891 100644 --- a/modules/viz/src/types.cpp +++ b/modules/viz/src/types.cpp @@ -100,7 +100,8 @@ cv::viz::Mesh cv::viz::Mesh::load(const String& file, int type) int* poly_ptr = mesh.polygons.ptr(); polygons->InitTraversal(); - vtkIdType nr_cell_points, *cell_points; + vtkIdType nr_cell_points; + CellIterT cell_points; while (polygons->GetNextCell(nr_cell_points, cell_points)) { *poly_ptr++ = nr_cell_points; diff --git a/modules/viz/src/vizimpl.cpp b/modules/viz/src/vizimpl.cpp index 2c291c0569..2c7ce997a4 100644 --- a/modules/viz/src/vizimpl.cpp +++ b/modules/viz/src/vizimpl.cpp @@ -55,8 +55,17 @@ cv::viz::Viz3d::VizImpl::VizImpl(const String &name) : spin_once_state_(false), // Create render window window_ = vtkSmartPointer::New(); - cv::Vec2i window_size = cv::Vec2i(window_->GetScreenSize()) / 2; - window_->SetSize(window_size.val); + int * sz = window_->GetScreenSize(); + if (sz) + { + cv::Vec2i window_size = cv::Vec2i(sz) / 2; + window_->SetSize(window_size.val); + } + else + { + int new_sz[2] = { 640, 480 }; + window_->SetSize(new_sz); + } window_->AddRenderer(renderer_); // Create the interactor style diff --git a/modules/viz/src/vtk/vtkOBJWriter.cpp b/modules/viz/src/vtk/vtkOBJWriter.cpp index 296b6eb065..2e5764fc27 100644 --- a/modules/viz/src/vtk/vtkOBJWriter.cpp +++ b/modules/viz/src/vtk/vtkOBJWriter.cpp @@ -72,7 +72,7 @@ void cv::viz::vtkOBJWriter::WriteData() } vtkDebugMacro(<<"Opening vtk file for writing..."); - ostream *outfilep = new ofstream(this->FileName, ios::out); + std::ostream *outfilep = new std::ofstream(this->FileName, ios::out); if (outfilep->fail()) { vtkErrorMacro(<< "Unable to open file: "<< this->FileName); @@ -127,7 +127,8 @@ void cv::viz::vtkOBJWriter::WriteData() // write out verts if any if (input->GetNumberOfVerts() > 0) { - vtkIdType npts = 0, *index = 0; + vtkIdType npts = 0; + CellIterT index = 0; vtkCellArray *cells = input->GetVerts(); for (cells->InitTraversal(); cells->GetNextCell(npts, index); ) { @@ -141,7 +142,8 @@ void cv::viz::vtkOBJWriter::WriteData() // write out lines if any if (input->GetNumberOfLines() > 0) { - vtkIdType npts = 0, *index = 0; + vtkIdType npts = 0; + CellIterT index = 0; vtkCellArray *cells = input->GetLines(); for (cells->InitTraversal(); cells->GetNextCell(npts, index); ) { @@ -162,7 +164,8 @@ void cv::viz::vtkOBJWriter::WriteData() // write out polys if any if (input->GetNumberOfPolys() > 0) { - vtkIdType npts = 0, *index = 0; + vtkIdType npts = 0; + CellIterT index = 0; vtkCellArray *cells = input->GetPolys(); for (cells->InitTraversal(); cells->GetNextCell(npts, index); ) { @@ -191,7 +194,8 @@ void cv::viz::vtkOBJWriter::WriteData() // write out tstrips if any if (input->GetNumberOfStrips() > 0) { - vtkIdType npts = 0, *index = 0; + vtkIdType npts = 0; + CellIterT index = 0; vtkCellArray *cells = input->GetStrips(); for (cells->InitTraversal(); cells->GetNextCell(npts, index); ) { diff --git a/modules/viz/src/vtk/vtkXYZReader.cpp b/modules/viz/src/vtk/vtkXYZReader.cpp index 57726eae9b..3b9265fed6 100644 --- a/modules/viz/src/vtk/vtkXYZReader.cpp +++ b/modules/viz/src/vtk/vtkXYZReader.cpp @@ -77,7 +77,7 @@ int cv::viz::vtkXYZReader::RequestData(vtkInformation*, vtkInformationVector**, } // Open the input file. - ifstream fin(this->FileName); + std::ifstream fin(this->FileName); if(!fin) { vtkErrorMacro("Error opening file " << this->FileName); diff --git a/modules/viz/src/vtk/vtkXYZWriter.cpp b/modules/viz/src/vtk/vtkXYZWriter.cpp index cf95e3c6a0..56a26b38a0 100644 --- a/modules/viz/src/vtk/vtkXYZWriter.cpp +++ b/modules/viz/src/vtk/vtkXYZWriter.cpp @@ -69,7 +69,7 @@ void cv::viz::vtkXYZWriter::WriteData() } vtkDebugMacro(<<"Opening vtk file for writing..."); - ostream *outfilep = new ofstream(this->FileName, ios::out); + std::ostream *outfilep = new std::ofstream(this->FileName, ios::out); if (outfilep->fail()) { vtkErrorMacro(<< "Unable to open file: "<< this->FileName); From 4281df7fe0090c03b6a40db44292ba6268239cbb Mon Sep 17 00:00:00 2001 From: Maksim Shabunin Date: Tue, 2 Jun 2020 19:21:19 +0300 Subject: [PATCH 2/3] viz: tests are non-interactive now --- modules/viz/test/test_tutorial2.cpp | 4 +-- modules/viz/test/test_tutorial3.cpp | 2 +- modules/viz/test/test_viz3d.cpp | 2 +- modules/viz/test/tests_simple.cpp | 56 ++++++++++++++--------------- 4 files changed, 32 insertions(+), 32 deletions(-) diff --git a/modules/viz/test/test_tutorial2.cpp b/modules/viz/test/test_tutorial2.cpp index 6b2972f0af..a4b5b99582 100644 --- a/modules/viz/test/test_tutorial2.cpp +++ b/modules/viz/test/test_tutorial2.cpp @@ -28,7 +28,7 @@ static void tutorial2() /// Rodrigues vector Vec3d rot_vec = Vec3d::all(0); double translation_phase = 0.0, translation = 0.0; - while(!myWindow.wasStopped()) + for(unsigned num = 0; num < 50; ++num) { /* Rotation using rodrigues */ /// Rotate around (1,1,1) @@ -45,7 +45,7 @@ static void tutorial2() myWindow.setWidgetPose("Cube Widget", pose); - myWindow.spinOnce(1, true); + myWindow.spinOnce(100, true); } } diff --git a/modules/viz/test/test_tutorial3.cpp b/modules/viz/test/test_tutorial3.cpp index 232130f0a6..32e33b1902 100644 --- a/modules/viz/test/test_tutorial3.cpp +++ b/modules/viz/test/test_tutorial3.cpp @@ -48,7 +48,7 @@ static void tutorial3(bool camera_pov) myWindow.setViewerPose(camera_pose); /// Start event loop. - myWindow.spin(); + myWindow.spinOnce(500, true); } TEST(Viz, tutorial3_global_view) diff --git a/modules/viz/test/test_viz3d.cpp b/modules/viz/test/test_viz3d.cpp index cdf8a00ad7..4ab05c3e0a 100644 --- a/modules/viz/test/test_viz3d.cpp +++ b/modules/viz/test/test_viz3d.cpp @@ -59,7 +59,7 @@ TEST(Viz_viz3d, DISABLED_develop) //cv::Mat cloud = cv::viz::readCloud(get_dragon_ply_file_path()); //---->>>>> - viz.spin(); + viz.spinOnce(500, true); } }} // namespace diff --git a/modules/viz/test/tests_simple.cpp b/modules/viz/test/tests_simple.cpp index 12d696dfba..5584483f4f 100644 --- a/modules/viz/test/tests_simple.cpp +++ b/modules/viz/test/tests_simple.cpp @@ -56,7 +56,7 @@ TEST(Viz, show_cloud_bluberry) viz.showWidget("dragon", WCloud(dragon_cloud, Color::bluberry()), pose); viz.showWidget("text2d", WText("Bluberry cloud", Point(20, 20), 20, Color::green())); - viz.spin(); + viz.spinOnce(500, true); } TEST(Viz, show_cloud_random_color) @@ -73,7 +73,7 @@ TEST(Viz, show_cloud_random_color) viz.showWidget("coosys", WCoordinateSystem()); viz.showWidget("dragon", WCloud(dragon_cloud, colors), pose); viz.showWidget("text2d", WText("Random color cloud", Point(20, 20), 20, Color::green())); - viz.spin(); + viz.spinOnce(500, true); } TEST(Viz, show_cloud_masked) @@ -91,7 +91,7 @@ TEST(Viz, show_cloud_masked) viz.showWidget("coosys", WCoordinateSystem()); viz.showWidget("dragon", WCloud(dragon_cloud), pose); viz.showWidget("text2d", WText("Nan masked cloud", Point(20, 20), 20, Color::green())); - viz.spin(); + viz.spinOnce(500, true); } TEST(Viz, show_cloud_collection) @@ -109,7 +109,7 @@ TEST(Viz, show_cloud_collection) viz.showWidget("coosys", WCoordinateSystem()); viz.showWidget("ccol", ccol); viz.showWidget("text2d", WText("Cloud collection", Point(20, 20), 20, Color::green())); - viz.spin(); + viz.spinOnce(500, true); } TEST(Viz, show_painted_clouds) @@ -124,7 +124,7 @@ TEST(Viz, show_painted_clouds) viz.showWidget("cloud3", WPaintedCloud(cloud, Vec3d(0.0, 0.0, -1.0), Vec3d(0.0, 0.0, 1.0), Color::blue(), Color::red())); viz.showWidget("arrow", WArrow(Vec3d(0.0, 1.0, -1.0), Vec3d(0.0, 1.0, 1.0), 0.009, Color::raspberry())); viz.showWidget("text2d", WText("Painted clouds", Point(20, 20), 20, Color::green())); - viz.spin(); + viz.spinOnce(500, true); } TEST(Viz, show_mesh) @@ -137,7 +137,7 @@ TEST(Viz, show_mesh) viz.showWidget("coosys", WCoordinateSystem()); viz.showWidget("mesh", WMesh(mesh), pose); viz.showWidget("text2d", WText("Just mesh", Point(20, 20), 20, Color::green())); - viz.spin(); + viz.spinOnce(500, true); } TEST(Viz, show_mesh_random_colors) @@ -152,7 +152,7 @@ TEST(Viz, show_mesh_random_colors) viz.showWidget("mesh", WMesh(mesh), pose); viz.setRenderingProperty("mesh", SHADING, SHADING_PHONG); viz.showWidget("text2d", WText("Random color mesh", Point(20, 20), 20, Color::green())); - viz.spin(); + viz.spinOnce(500, true); } TEST(Viz, show_widget_merger) @@ -173,7 +173,7 @@ TEST(Viz, show_widget_merger) viz.showWidget("coo", WCoordinateSystem()); viz.showWidget("merger", merger); viz.showWidget("text2d", WText("Widget merger", Point(20, 20), 20, Color::green())); - viz.spin(); + viz.spinOnce(500, true); } TEST(Viz, show_textured_mesh) @@ -210,7 +210,7 @@ TEST(Viz, show_textured_mesh) viz.showWidget("mesh", WMesh(mesh)); viz.setRenderingProperty("mesh", SHADING, SHADING_PHONG); viz.showWidget("text2d", WText("Textured mesh", Point(20, 20), 20, Color::green())); - viz.spin(); + viz.spinOnce(500, true); } TEST(Viz, show_polyline) @@ -229,7 +229,7 @@ TEST(Viz, show_polyline) viz.showWidget("polyline", WPolyLine(polyline, colors)); viz.showWidget("coosys", WCoordinateSystem()); viz.showWidget("text2d", WText("Polyline", Point(20, 20), 20, Color::green())); - viz.spin(); + viz.spinOnce(500, true); } TEST(Viz, show_sampled_normals) @@ -244,7 +244,7 @@ TEST(Viz, show_sampled_normals) viz.showWidget("normals", WCloudNormals(mesh.cloud, mesh.normals, 30, 0.1f, Color::green()), pose); viz.setRenderingProperty("normals", LINE_WIDTH, 2.0); viz.showWidget("text2d", WText("Cloud or mesh normals", Point(20, 20), 20, Color::green())); - viz.spin(); + viz.spinOnce(500, true); } TEST(Viz, show_cloud_shaded_by_normals) @@ -260,7 +260,7 @@ TEST(Viz, show_cloud_shaded_by_normals) Viz3d viz("show_cloud_shaded_by_normals"); viz.showWidget("cloud", cloud, pose); viz.showWidget("text2d", WText("Cloud shaded by normals", Point(20, 20), 20, Color::green())); - viz.spin(); + viz.spinOnce(500, true); } TEST(Viz, show_trajectories) @@ -287,15 +287,15 @@ TEST(Viz, show_trajectories) viz.showWidget("text2d", WText("Different kinds of supported trajectories", Point(20, 20), 20, Color::green())); int i = 0; - while(!viz.wasStopped()) + for(unsigned num = 0; num < 50; ++num) { double a = --i % 360; Vec3d pose(sin(a * CV_PI/180), 0.7, cos(a * CV_PI/180)); viz.setViewerPose(makeCameraPose(pose * 7.5, Vec3d(0.0, 0.5, 0.0), Vec3d(0.0, 0.1, 0.0))); - viz.spinOnce(20, true); + viz.spinOnce(100, true); } viz.resetCamera(); - viz.spin(); + viz.spinOnce(500, true); } TEST(Viz, show_trajectory_reposition) @@ -306,7 +306,7 @@ TEST(Viz, show_trajectory_reposition) viz.showWidget("coos", WCoordinateSystem()); viz.showWidget("sub3", WTrajectory(Mat(path).rowRange(0, (int)path.size()/3), WTrajectory::BOTH, 0.2, Color::brown()), path.front().inv()); viz.showWidget("text2d", WText("Trajectory resposition to origin", Point(20, 20), 20, Color::green())); - viz.spin(); + viz.spinOnce(500, true); } TEST(Viz, show_camera_positions) @@ -330,7 +330,7 @@ TEST(Viz, show_camera_positions) viz.showWidget("pos3", WCameraPosition(0.75), poses[1]); viz.showWidget("pos4", WCameraPosition(K, gray, 3, Color::indigo()), poses[1]); viz.showWidget("text2d", WText("Camera positions with images", Point(20, 20), 20, Color::green())); - viz.spin(); + viz.spinOnce(500, true); } TEST(Viz, show_overlay_image) @@ -353,16 +353,16 @@ TEST(Viz, show_overlay_image) viz.showWidget("text2d", WText("Overlay images", Point(20, 20), 20, Color::green())); int i = 0; - while(!viz.wasStopped()) + for(unsigned num = 0; num < 50; ++num) { double a = ++i % 360; Vec3d pose(sin(a * CV_PI/180), 0.7, cos(a * CV_PI/180)); viz.setViewerPose(makeCameraPose(pose * 3, Vec3d(0.0, 0.5, 0.0), Vec3d(0.0, 0.1, 0.0))); viz.getWidget("img1").cast().setImage(lena * pow(sin(i*10*CV_PI/180) * 0.5 + 0.5, 1.0)); - viz.spinOnce(1, true); + viz.spinOnce(100, true); } viz.showWidget("text2d", WText("Overlay images (stopped)", Point(20, 20), 20, Color::green())); - viz.spin(); + viz.spinOnce(500, true); } @@ -376,7 +376,7 @@ TEST(Viz, show_image_method) viz.showImage(lena, lena.size()); viz.spinOnce(1500, true); - cv::viz::imshow("show_image_method", make_gray(lena)).spin(); + cv::viz::imshow("show_image_method", make_gray(lena)).spinOnce(500, true); } TEST(Viz, show_image_3d) @@ -398,13 +398,13 @@ TEST(Viz, show_image_3d) viz.showWidget("text2d", WText("Images in 3D", Point(20, 20), 20, Color::green())); int i = 0; - while(!viz.wasStopped()) + for(unsigned num = 0; num < 50; ++num) { viz.getWidget("img0").cast().setImage(lena * pow(sin(i++*7.5*CV_PI/180) * 0.5 + 0.5, 1.0)); - viz.spinOnce(1, true); + viz.spinOnce(100, true); } viz.showWidget("text2d", WText("Images in 3D (stopped)", Point(20, 20), 20, Color::green())); - viz.spin(); + viz.spinOnce(500, true); } TEST(Viz, show_simple_widgets) @@ -431,10 +431,10 @@ TEST(Viz, show_simple_widgets) viz.showWidget("grid1", WGrid(Vec2i(7,7), Vec2d::all(0.75), Color::gray()), Affine3d().translate(Vec3d(0.0, 0.0, -1.0))); - viz.spin(); + viz.spinOnce(500, true); viz.getWidget("text2d").cast().setText("Different simple widgets (updated)"); viz.getWidget("text3d").cast().setText("Updated text 3D"); - viz.spin(); + viz.spinOnce(500, true); } TEST(Viz, show_follower) @@ -446,9 +446,9 @@ TEST(Viz, show_follower) viz.showWidget("t3d_2", WText3D("Simple 3D follower", Point3d(-0.5, -0.5, 0.5), 0.125, true, Color::green())); viz.showWidget("text2d", WText("Follower: text always facing camera", Point(20, 20), 20, Color::green())); viz.setBackgroundMeshLab(); - viz.spin(); + viz.spinOnce(500, true); viz.getWidget("t3d_2").cast().setText("Updated follower 3D"); - viz.spin(); + viz.spinOnce(500, true); } }} // namespace From f46c6cadbe751b2dbf60b34e85aaf575511e9794 Mon Sep 17 00:00:00 2001 From: Maksim Shabunin Date: Tue, 2 Jun 2020 22:46:53 +0300 Subject: [PATCH 3/3] fixup! Added VTK 9 support --- modules/viz/CMakeLists.txt | 12 ++++++++---- modules/viz/src/types.cpp | 1 + 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/modules/viz/CMakeLists.txt b/modules/viz/CMakeLists.txt index 3426e1dd26..cd225960ce 100644 --- a/modules/viz/CMakeLists.txt +++ b/modules/viz/CMakeLists.txt @@ -3,9 +3,6 @@ if(NOT HAVE_VTK) endif() set(the_description "Viz") -if(VTK_VERSION VERSION_LESS 8.90) - include(${VTK_USE_FILE}) -endif() if(NOT BUILD_SHARED_LIBS) # We observed conflict between builtin 3rdparty libraries and @@ -37,7 +34,14 @@ ocv_add_accuracy_tests() ocv_add_perf_tests() ocv_add_samples(opencv_imgproc opencv_calib3d opencv_features2d opencv_flann) -ocv_target_link_libraries(${the_module} PRIVATE ${VTK_LIBRARIES}) + +if (VTK_VERSION VERSION_LESS "8.90.0") + include(${VTK_USE_FILE}) + ocv_target_link_libraries(${the_module} PRIVATE ${VTK_LIBRARIES}) +else () + ocv_target_link_libraries(${the_module} PRIVATE ${VTK_LIBRARIES}) + vtk_module_autoinit(TARGETS ${the_module} MODULES ${VTK_LIBRARIES}) +endif() if(APPLE AND BUILD_opencv_viz) ocv_target_link_libraries(${the_module} PRIVATE "-framework Cocoa") diff --git a/modules/viz/src/types.cpp b/modules/viz/src/types.cpp index 0e14477891..e9a470cf83 100644 --- a/modules/viz/src/types.cpp +++ b/modules/viz/src/types.cpp @@ -97,6 +97,7 @@ cv::viz::Mesh cv::viz::Mesh::load(const String& file, int type) // Now handle the polygons vtkSmartPointer polygons = polydata->GetPolys(); mesh.polygons.create(1, polygons->GetSize(), CV_32SC1); + mesh.polygons = 0; int* poly_ptr = mesh.polygons.ptr(); polygons->InitTraversal();