File pcl-vtk-9_0-compat.patch of Package pcl

From 92d28d9689e761c3184ca1cab65ab59ff2850344 Mon Sep 17 00:00:00 2001
From: Matt Middleton <mmiddleton@wynright.com>
Date: Thu, 14 May 2020 11:34:14 +1200
Subject: [PATCH 01/17] API Changes to Support vtkCellArray V2

Version 2 of vtkCellArray deprecates the write pointer and requires const pointers in in some api calls that return pointers to shared memory.  If the VTK_CELL_ARRAY_V2 macro is defined, this code uses the appropriate const pointers and uses alternative methods to insert cells into the cell array.  This commit also adds some missing vtk headers that were causing incomplete type errors.
---
 apps/src/render_views_tesselated_sphere.cpp   | 23 +++++++-
 io/src/vtk_lib_io.cpp                         |  4 ++
 surface/src/vtk_smoothing/vtk_utils.cpp       |  5 ++
 tools/mesh_sampling.cpp                       | 11 +++-
 .../pcl/visualization/impl/pcl_visualizer.hpp | 58 ++++++++++++++++++-
 .../pcl/visualization/interactor_style.h      |  2 +
 visualization/src/pcl_visualizer.cpp          | 49 ++++++++++++++--
 7 files changed, 142 insertions(+), 10 deletions(-)

diff --git a/apps/src/render_views_tesselated_sphere.cpp b/apps/src/render_views_tesselated_sphere.cpp
index e329551b69..57487a1205 100644
--- a/apps/src/render_views_tesselated_sphere.cpp
+++ b/apps/src/render_views_tesselated_sphere.cpp
@@ -26,6 +26,7 @@
 #include <vtkTransformFilter.h>
 #include <vtkTriangle.h>
 #include <vtkWorldPointPicker.h>
+#include <vtkIdTypeArray.h>
 
 #include <array>
 
@@ -34,7 +35,13 @@ pcl::apps::RenderViewsTesselatedSphere::generateViews()
 {
   // center object
   double CoM[3];
-  vtkIdType npts_com = 0, *ptIds_com = nullptr;
+  vtkIdType npts_com = 0;
+#ifdef VTK_CELL_ARRAY_V2
+  vtkIdType const *ptIds_com = nullptr;
+#else
+  vtkIdType *ptIds_com = nullptr;
+#endif
+
   vtkSmartPointer<vtkCellArray> cells_com = polydata_->GetPolys();
 
   double center[3], p1_com[3], p2_com[3], p3_com[3], totalArea_com = 0;
@@ -95,7 +102,12 @@ pcl::apps::RenderViewsTesselatedSphere::generateViews()
   // * Compute area of the mesh
   //////////////////////////////
   vtkSmartPointer<vtkCellArray> cells = mapper->GetInput()->GetPolys();
-  vtkIdType npts = 0, *ptIds = nullptr;
+  vtkIdType npts = 0;
+#ifdef VTK_CELL_ARRAY_V2
+  vtkIdType const *ptIds = nullptr;
+#else
+  vtkIdType *ptIds = nullptr;
+#endif
 
   double p1[3], p2[3], p3[3], totalArea = 0;
   for (cells->InitTraversal(); cells->GetNextCell(npts, ptIds);) {
@@ -363,7 +375,12 @@ pcl::apps::RenderViewsTesselatedSphere::generateViews()
       polydata->BuildCells();
 
       vtkSmartPointer<vtkCellArray> cells = polydata->GetPolys();
-      vtkIdType npts = 0, *ptIds = nullptr;
+      vtkIdType npts = 0;
+#ifdef VTK_CELL_ARRAY_V2
+      vtkIdType const *ptIds = nullptr;
+#else
+      vtkIdType *ptIds = nullptr;
+#endif
 
       double p1[3], p2[3], p3[3], area, totalArea = 0;
       for (cells->InitTraversal(); cells->GetNextCell(npts, ptIds);) {
diff --git a/io/src/vtk_lib_io.cpp b/io/src/vtk_lib_io.cpp
index 37e9df4cc7..35b42d9b72 100644
--- a/io/src/vtk_lib_io.cpp
+++ b/io/src/vtk_lib_io.cpp
@@ -343,7 +343,11 @@ pcl::io::vtk2mesh (const vtkSmartPointer<vtkPolyData>& poly_data, pcl::PolygonMe
 
   // Now handle the polygons
   mesh.polygons.resize (nr_polygons);
+#ifdef VTK_CELL_ARRAY_V2
+  vtkIdType const *cell_points;
+#else
   vtkIdType* cell_points;
+#endif
   vtkIdType nr_cell_points;
   vtkCellArray * mesh_polygons = poly_data->GetPolys ();
   mesh_polygons->InitTraversal ();
diff --git a/surface/src/vtk_smoothing/vtk_utils.cpp b/surface/src/vtk_smoothing/vtk_utils.cpp
index cbd8059358..756b8cce1a 100644
--- a/surface/src/vtk_smoothing/vtk_utils.cpp
+++ b/surface/src/vtk_smoothing/vtk_utils.cpp
@@ -48,6 +48,7 @@
 #include <vtkPolyData.h>
 #include <vtkPointData.h>
 #include <vtkFloatArray.h>
+#include <vtkUnsignedCharArray.h>
 
 // Support for VTK 7.1 upwards
 #ifdef vtkGenericDataArray_h
@@ -154,7 +155,11 @@ pcl::VTKUtils::vtk2mesh (const vtkSmartPointer<vtkPolyData>& poly_data, pcl::Pol
   }
 
   mesh.polygons.resize (nr_polygons);
+#ifdef VTK_CELL_ARRAY_V2
+  vtkIdType const *cell_points;
+#else
   vtkIdType* cell_points;
+#endif
   vtkIdType nr_cell_points;
   vtkCellArray * mesh_polygons = poly_data->GetPolys ();
   mesh_polygons->InitTraversal ();
diff --git a/tools/mesh_sampling.cpp b/tools/mesh_sampling.cpp
index 23f98d7c1d..d427c180cc 100644
--- a/tools/mesh_sampling.cpp
+++ b/tools/mesh_sampling.cpp
@@ -87,7 +87,11 @@ randPSurface (vtkPolyData * polydata, std::vector<double> * cumulativeAreas, dou
 
   double A[3], B[3], C[3];
   vtkIdType npts = 0;
+#ifdef VTK_CELL_ARRAY_V2
+  vtkIdType const *ptIds = nullptr;
+#else
   vtkIdType *ptIds = nullptr;
+#endif
   polydata->GetCellPoints (el, npts, ptIds);
   polydata->GetPoint (ptIds[0], A);
   polydata->GetPoint (ptIds[1], B);
@@ -138,7 +142,12 @@ uniform_sampling (vtkSmartPointer<vtkPolyData> polydata, std::size_t n_samples,
 
   double p1[3], p2[3], p3[3], totalArea = 0;
   std::vector<double> cumulativeAreas (cells->GetNumberOfCells (), 0);
-  vtkIdType npts = 0, *ptIds = nullptr;
+  vtkIdType npts = 0;
+#ifdef VTK_CELL_ARRAY_V2
+  vtkIdType const *ptIds = nullptr;
+#else
+  vtkIdType *ptIds = nullptr;
+#endif
   std::size_t cellId = 0;
   for (cells->InitTraversal (); cells->GetNextCell (npts, ptIds); cellId++)
   {
diff --git a/visualization/include/pcl/visualization/impl/pcl_visualizer.hpp b/visualization/include/pcl/visualization/impl/pcl_visualizer.hpp
index 4b4900e380..bf8e3d3230 100644
--- a/visualization/include/pcl/visualization/impl/pcl_visualizer.hpp
+++ b/visualization/include/pcl/visualization/impl/pcl_visualizer.hpp
@@ -1701,8 +1701,32 @@ pcl::visualization::PCLVisualizer::addPolygonMesh (
   {
     // Create polys from polyMesh.polygons
     vtkSmartPointer<vtkCellArray> cell_array = vtkSmartPointer<vtkCellArray>::New ();
-    vtkIdType *cell = cell_array->WritePointer (vertices.size (), vertices.size () * (max_size_of_polygon + 1));
     int idx = 0;
+
+#ifdef VTK_CELL_ARRAY_V2
+    if (!lookup.empty ())
+    {
+      for (std::size_t i = 0; i < vertices.size (); ++i, ++idx)
+      {
+        std::size_t n_points = vertices[i].vertices.size ();
+        cell_array->InsertNextCell (n_points);
+        for (std::size_t j = 0; j < n_points; j++, ++idx)
+          cell_array->InsertCellPoint (lookup[vertices[i].vertices[j]]);
+      }
+    }
+    else
+    {
+      for (std::size_t i = 0; i < vertices.size (); ++i, ++idx)
+      {
+        std::size_t n_points = vertices[i].vertices.size ();
+        cell_array->InsertNextCell (n_points);
+        for (std::size_t j = 0; j < n_points; j++, ++idx)
+          cell_array->InsertCellPoint (vertices[i].vertices[j]);
+      }
+    }
+#else
+    vtkIdType *cell = cell_array->WritePointer (vertices.size (), vertices.size () * (max_size_of_polygon + 1));
+
     if (!lookup.empty ())
     {
       for (std::size_t i = 0; i < vertices.size (); ++i, ++idx)
@@ -1727,6 +1751,7 @@ pcl::visualization::PCLVisualizer::addPolygonMesh (
           //cell_array->InsertCellPoint (vertices[i].vertices[j]);
       }
     }
+#endif
     vtkSmartPointer<vtkPolyData> polydata;
     allocVtkPolyData (polydata);
     cell_array->GetData ()->SetNumberOfValues (idx);
@@ -1878,8 +1903,36 @@ pcl::visualization::PCLVisualizer::updatePolygonMesh (
 
   // Update the cells
   cells = vtkSmartPointer<vtkCellArray>::New ();
-  vtkIdType *cell = cells->WritePointer (verts.size (), verts.size () * (max_size_of_polygon + 1));
   int idx = 0;
+
+#ifdef VTK_CELL_ARRAY_V2
+  if (!lookup.empty ())
+  {
+    for (std::size_t i = 0; i < verts.size (); ++i, ++idx)
+    {
+      std::size_t n_points = verts[i].vertices.size ();
+      cells->InsertNextCell (n_points);
+      for (std::size_t j = 0; j < n_points; j++, ++idx)
+      {
+        cells->InsertCellPoint (lookup[verts[i].vertices[j]]);
+
+      }
+    }
+  }
+  else
+  {
+    for (std::size_t i = 0; i < verts.size (); ++i, ++idx)
+    {
+      std::size_t n_points = verts[i].vertices.size ();
+      cells->InsertNextCell (n_points);
+      for (std::size_t j = 0; j < n_points; j++, ++idx)
+      {
+        cells->InsertCellPoint(verts[i].vertices[j]);
+      }
+    }
+  }
+#else
+  vtkIdType *cell = cells->WritePointer (verts.size (), verts.size () * (max_size_of_polygon + 1));
   if (!lookup.empty ())
   {
     for (std::size_t i = 0; i < verts.size (); ++i, ++idx)
@@ -1900,6 +1953,7 @@ pcl::visualization::PCLVisualizer::updatePolygonMesh (
         *cell = verts[i].vertices[j];
     }
   }
+#endif
   cells->GetData ()->SetNumberOfValues (idx);
   cells->Squeeze ();
   // Set the the vertices
diff --git a/visualization/include/pcl/visualization/interactor_style.h b/visualization/include/pcl/visualization/interactor_style.h
index 0f166b75dd..a1f17238e0 100644
--- a/visualization/include/pcl/visualization/interactor_style.h
+++ b/visualization/include/pcl/visualization/interactor_style.h
@@ -51,6 +51,8 @@
 #include <boost/signals2/signal.hpp>
 #endif
 #include <vtkInteractorStyleRubberBandPick.h>
+#include <vtkRendererCollection.h>
+#include <vtkRenderWindow.h>
 
 class vtkRendererCollection;
 class vtkLegendScaleActor;
diff --git a/visualization/src/pcl_visualizer.cpp b/visualization/src/pcl_visualizer.cpp
index 0d6f9a4406..45bd2956c3 100644
--- a/visualization/src/pcl_visualizer.cpp
+++ b/visualization/src/pcl_visualizer.cpp
@@ -3160,8 +3160,33 @@ pcl::visualization::PCLVisualizer::updatePolygonMesh (
 
   // Update the cells
   cells = vtkSmartPointer<vtkCellArray>::New ();
-  vtkIdType *cell = cells->WritePointer (verts.size (), verts.size () * (max_size_of_polygon + 1));
   int idx = 0;
+
+#ifdef VTK_CELL_ARRAY_V2
+  if (!lookup.empty ())
+  {
+    for (std::size_t i = 0; i < verts.size (); ++i, ++idx)
+    {
+      std::size_t n_points = verts[i].vertices.size ();
+      cells->InsertNextCell(n_points);
+      for (std::size_t j = 0; j < n_points; j++, ++idx)
+        cells->InsertCellPoint(lookup[verts[i].vertices[j]]);
+    }
+  }
+  else
+  {
+    for (std::size_t i = 0; i < verts.size (); ++i, ++idx)
+    {
+      std::size_t n_points = verts[i].vertices.size ();
+      cells->InsertNextCell(n_points);
+      for (std::size_t j = 0; j < n_points; j++, ++idx)
+      {
+        cells->InsertCellPoint(verts[i].vertices[j]);
+      }
+    }
+  }
+#else
+  vtkIdType *cell = cells->WritePointer (verts.size (), verts.size () * (max_size_of_polygon + 1));
   if (!lookup.empty ())
   {
     for (std::size_t i = 0; i < verts.size (); ++i, ++idx)
@@ -3182,6 +3207,7 @@ pcl::visualization::PCLVisualizer::updatePolygonMesh (
         *cell = verts[i].vertices[j];
     }
   }
+#endif
   cells->GetData ()->SetNumberOfValues (idx);
   cells->Squeeze ();
   // Set the the vertices
@@ -3552,7 +3578,12 @@ pcl::visualization::PCLVisualizer::renderViewTesselatedSphere (
 
   //center object
   double CoM[3];
-  vtkIdType npts_com = 0, *ptIds_com = nullptr;
+  vtkIdType npts_com = 0;
+#ifdef VTK_CELL_ARRAY_V2
+  vtkIdType const *ptIds_com = nullptr;
+#else
+  vtkIdType *ptIds_com = nullptr;
+#endif
   vtkSmartPointer<vtkCellArray> cells_com = polydata->GetPolys ();
 
   double center[3], p1_com[3], p2_com[3], p3_com[3], totalArea_com = 0;
@@ -3611,7 +3642,12 @@ pcl::visualization::PCLVisualizer::renderViewTesselatedSphere (
   // * Compute area of the mesh
   //////////////////////////////
   vtkSmartPointer<vtkCellArray> cells = mapper->GetInput ()->GetPolys ();
-  vtkIdType npts = 0, *ptIds = nullptr;
+  vtkIdType npts = 0;
+#ifdef VTK_CELL_ARRAY_V2
+  vtkIdType const *ptIds = nullptr;
+#else
+  vtkIdType *ptIds = nullptr;
+#endif
 
   double p1[3], p2[3], p3[3], totalArea = 0;
   for (cells->InitTraversal (); cells->GetNextCell (npts, ptIds);)
@@ -3830,7 +3866,12 @@ pcl::visualization::PCLVisualizer::renderViewTesselatedSphere (
     polydata->BuildCells ();
 
     vtkSmartPointer<vtkCellArray> cells = polydata->GetPolys ();
-    vtkIdType npts = 0, *ptIds = nullptr;
+    vtkIdType npts = 0;
+#ifdef VTK_CELL_ARRAY_V2
+    vtkIdType const *ptIds = nullptr;
+#else
+    vtkIdType *ptIds = nullptr;
+#endif
 
     double p1[3], p2[3], p3[3], area, totalArea = 0;
     for (cells->InitTraversal (); cells->GetNextCell (npts, ptIds);)

From f807763f2742c70533d87f1e0348e612f4cfa83f Mon Sep 17 00:00:00 2001
From: Matt Middleton <mmiddleton@wynright.com>
Date: Thu, 14 May 2020 11:38:00 +1200
Subject: [PATCH 02/17] Add missing header and namespace specifier

---
 visualization/src/interactor_style.cpp | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/visualization/src/interactor_style.cpp b/visualization/src/interactor_style.cpp
index 4740577cfb..f2efbdfd96 100644
--- a/visualization/src/interactor_style.cpp
+++ b/visualization/src/interactor_style.cpp
@@ -36,6 +36,7 @@
  *
  */
 
+#include <fstream>
 #include <list>
 #include <pcl/common/angles.h>
 #include <pcl/visualization/common/io.h>
@@ -137,7 +138,7 @@ pcl::visualization::PCLVisualizerInteractorStyle::saveCameraParameters (const st
 {
   FindPokedRenderer (Interactor->GetEventPosition ()[0], Interactor->GetEventPosition ()[1]);
 
-  ofstream ofs_cam (file.c_str ());
+  std::ofstream ofs_cam (file.c_str ());
   if (!ofs_cam.is_open ())
   {
     return (false);

From cd819744f2b4ab5091aeee5faee747269bfae4a6 Mon Sep 17 00:00:00 2001
From: Matt Middleton <mmiddleton@wynright.com>
Date: Thu, 14 May 2020 14:50:06 +1200
Subject: [PATCH 03/17] Add VTK9 module auto initialization to visualizer cmake

---
 visualization/CMakeLists.txt | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/visualization/CMakeLists.txt b/visualization/CMakeLists.txt
index 990a5f5b2b..7f312baabc 100644
--- a/visualization/CMakeLists.txt
+++ b/visualization/CMakeLists.txt
@@ -171,6 +171,11 @@ PCL_ADD_INCLUDES("${SUBSYS_NAME}" "${SUBSYS_NAME}/impl" ${impl_incs})
 PCL_ADD_INCLUDES("${SUBSYS_NAME}" "${SUBSYS_NAME}/common/impl" ${common_impl_incs})
 PCL_ADD_INCLUDES("${SUBSYS_NAME}" "${SUBSYS_NAME}/vtk" ${vtk_incs})
 
+if(${VTK_VERSION} VERSION_GREATER_EQUAL 9.0)
+  vtk_module_autoinit(TARGETS "${LIB_NAME}" MODULES VTK::RenderingOpenGL2 VTK::RenderingFreeType)
+endif()
+
+
 if(BUILD_TESTS)
   add_subdirectory(test)
 endif()

From 8230bc613e27cfc7d2fbbcf5d0258d4d6135ff08 Mon Sep 17 00:00:00 2001
From: Matt Middleton <mmiddleton@wynright.com>
Date: Thu, 14 May 2020 14:51:03 +1200
Subject: [PATCH 04/17] Update main project Cmake to support VTK9 conventions

---
 CMakeLists.txt | 57 ++++++++++++++++++++++++++++++--------------------
 1 file changed, 34 insertions(+), 23 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 9422d97ba9..addd4c5a43 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -367,27 +367,29 @@ endif()
 option(WITH_VTK "Build VTK-Visualizations" TRUE)
 if(WITH_VTK AND NOT ANDROID)
   set(PCL_VTK_COMPONENTS
-    vtkChartsCore
-    vtkCommonCore
-    vtkCommonDataModel
-    vtkCommonExecutionModel
-    vtkFiltersCore
-    vtkFiltersExtraction
-    vtkFiltersModeling
-    vtkImagingCore
-    vtkImagingSources
-    vtkInteractionStyle
-    vtkInteractionWidgets
-    vtkIOCore
-    vtkIOGeometry
-    vtkIOImage
-    vtkIOLegacy
-    vtkIOPLY
-    vtkRenderingAnnotation
-    vtkRenderingLOD
-    vtkViewsContext2D
+    ChartsCore
+    CommonCore
+    CommonDataModel
+    CommonExecutionModel
+    FiltersCore
+    FiltersExtraction
+    FiltersGeometry
+    FiltersModeling
+    ImagingCore
+    ImagingSources
+    InteractionStyle
+    InteractionWidgets
+    IOCore
+    IOGeometry
+    IOImage
+    IOLegacy
+    IOPLY
+    RenderingAnnotation
+    RenderingLOD
+    ViewsContext2D
   )
-  find_package(VTK COMPONENTS ${PCL_VTK_COMPONENTS})
+
+  find_package(VTK COMPONENTS)
   if(VTK_FOUND AND ("${VTK_VERSION}" VERSION_LESS 6.2))
     message(WARNING "The minimum required version of VTK is 6.2, but found ${VTK_VERSION}")
     set(VTK_FOUND FALSE)
@@ -397,19 +399,27 @@ if(WITH_VTK AND NOT ANDROID)
     if(NOT DEFINED VTK_RENDERING_BACKEND)
       # On old VTK versions this variable does not exist. In this case it is
       # safe to assume OpenGL backend
-      set(VTK_RENDERING_BACKEND "OpenGL")
+      if(${VTK_VERSION} VERSION_LESS 9.0)
+        set(VTK_RENDERING_BACKEND "OpenGL")
+        else()
+        set(VTK_RENDERING_BACKEND "OpenGL2")
+      endif()
     endif()
-    list(APPEND PCL_VTK_COMPONENTS vtkRenderingContext${VTK_RENDERING_BACKEND})
+    list(APPEND PCL_VTK_COMPONENTS RenderingContext${VTK_RENDERING_BACKEND})
 
     if(WITH_QT)
       if(";${VTK_MODULES_ENABLED};" MATCHES ";vtkGUISupportQt;" AND ";${VTK_MODULES_ENABLED};" MATCHES ";vtkRenderingQt;")
         set(QVTK_FOUND ON)
-        list(APPEND PCL_VTK_COMPONENTS vtkRenderingQt vtkGUISupportQt)
+        list(APPEND PCL_VTK_COMPONENTS RenderingQt GUISupportQt)
       else()
         unset(QVTK_FOUND)
       endif()
     endif()
 
+    if(${VTK_VERSION} VERSION_LESS 9.0)
+      list(TRANSFORM PCL_VTK_COMPONENTS PREPEND vtk PCL_VTK_COMPONENTS)
+    endif()
+
     find_package(VTK COMPONENTS ${PCL_VTK_COMPONENTS})
 
     message(STATUS "VTK_MAJOR_VERSION ${VTK_MAJOR_VERSION}, rendering backend: ${VTK_RENDERING_BACKEND}")
@@ -430,6 +440,7 @@ if(WITH_VTK AND NOT ANDROID)
       elseif(${VTK_RENDERING_BACKEND} STREQUAL "OpenGL2")
         set(VTK_RENDERING_BACKEND_OPENGL_VERSION "2")
       endif()
+
     else()
       set(VTK_FOUND OFF)
       message("Warning: You are to build PCL in STATIC but VTK is SHARED!")

From 4ad0cfcffacdf4d88bce41eff66c56dbbdde6726 Mon Sep 17 00:00:00 2001
From: Larshg <larshg@gmail.com>
Date: Sat, 16 May 2020 22:35:01 +0200
Subject: [PATCH 05/17] Move findVTK to a module file.

Reorganise to hopefully support pre 9.0.
---
 2d/CMakeLists.txt           |  2 -
 CMakeLists.txt              | 82 +------------------------------
 cmake/Modules/FindVTK.cmake | 96 +++++++++++++++++++++++++++++++++++++
 people/CMakeLists.txt       |  2 -
 4 files changed, 97 insertions(+), 85 deletions(-)
 create mode 100644 cmake/Modules/FindVTK.cmake

diff --git a/2d/CMakeLists.txt b/2d/CMakeLists.txt
index 8bd5411e44..437d23fb47 100644
--- a/2d/CMakeLists.txt
+++ b/2d/CMakeLists.txt
@@ -31,8 +31,6 @@ set(impl_incs
 )
 
 if(${VTK_FOUND})
-  set(VTK_USE_FILE "${VTK_USE_FILE}" CACHE INTERNAL "VTK_USE_FILE")
-  include("${VTK_USE_FILE}")
   set(VTK_IO_TARGET_LINK_LIBRARIES vtkCommon vtkWidgets vtkIO vtkImaging)
 endif()
 
diff --git a/CMakeLists.txt b/CMakeLists.txt
index addd4c5a43..eb945c90ea 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -366,87 +366,7 @@ endif()
 # Find VTK
 option(WITH_VTK "Build VTK-Visualizations" TRUE)
 if(WITH_VTK AND NOT ANDROID)
-  set(PCL_VTK_COMPONENTS
-    ChartsCore
-    CommonCore
-    CommonDataModel
-    CommonExecutionModel
-    FiltersCore
-    FiltersExtraction
-    FiltersGeometry
-    FiltersModeling
-    ImagingCore
-    ImagingSources
-    InteractionStyle
-    InteractionWidgets
-    IOCore
-    IOGeometry
-    IOImage
-    IOLegacy
-    IOPLY
-    RenderingAnnotation
-    RenderingLOD
-    ViewsContext2D
-  )
-
-  find_package(VTK COMPONENTS)
-  if(VTK_FOUND AND ("${VTK_VERSION}" VERSION_LESS 6.2))
-    message(WARNING "The minimum required version of VTK is 6.2, but found ${VTK_VERSION}")
-    set(VTK_FOUND FALSE)
-  endif()
-
-  if(VTK_FOUND)
-    if(NOT DEFINED VTK_RENDERING_BACKEND)
-      # On old VTK versions this variable does not exist. In this case it is
-      # safe to assume OpenGL backend
-      if(${VTK_VERSION} VERSION_LESS 9.0)
-        set(VTK_RENDERING_BACKEND "OpenGL")
-        else()
-        set(VTK_RENDERING_BACKEND "OpenGL2")
-      endif()
-    endif()
-    list(APPEND PCL_VTK_COMPONENTS RenderingContext${VTK_RENDERING_BACKEND})
-
-    if(WITH_QT)
-      if(";${VTK_MODULES_ENABLED};" MATCHES ";vtkGUISupportQt;" AND ";${VTK_MODULES_ENABLED};" MATCHES ";vtkRenderingQt;")
-        set(QVTK_FOUND ON)
-        list(APPEND PCL_VTK_COMPONENTS RenderingQt GUISupportQt)
-      else()
-        unset(QVTK_FOUND)
-      endif()
-    endif()
-
-    if(${VTK_VERSION} VERSION_LESS 9.0)
-      list(TRANSFORM PCL_VTK_COMPONENTS PREPEND vtk PCL_VTK_COMPONENTS)
-    endif()
-
-    find_package(VTK COMPONENTS ${PCL_VTK_COMPONENTS})
-
-    message(STATUS "VTK_MAJOR_VERSION ${VTK_MAJOR_VERSION}, rendering backend: ${VTK_RENDERING_BACKEND}")
-    if(PCL_SHARED_LIBS OR (NOT (PCL_SHARED_LIBS) AND NOT (VTK_BUILD_SHARED_LIBS)))
-      if(VTK_USE_FILE)
-        include(${VTK_USE_FILE})
-      endif()
-      message(STATUS "VTK found (include: ${VTK_INCLUDE_DIRS}, libs: ${VTK_LIBRARIES}")
-      if(APPLE)
-        option(VTK_USE_COCOA "Use Cocoa for VTK render windows" ON)
-        mark_as_advanced(VTK_USE_COCOA)
-      endif()
-      if(${VTK_RENDERING_BACKEND} STREQUAL "OpenGL")
-        set(VTK_RENDERING_BACKEND_OPENGL_VERSION "1")
-        message(DEPRECATION "The rendering backend OpenGL is deprecated and not available anymore since VTK 8.2."
-                            "Please switch to the OpenGL2 backend instead, which is available since VTK 6.2."
-                            "Support of the deprecated backend will be dropped with PCL 1.13.")
-      elseif(${VTK_RENDERING_BACKEND} STREQUAL "OpenGL2")
-        set(VTK_RENDERING_BACKEND_OPENGL_VERSION "2")
-      endif()
-
-    else()
-      set(VTK_FOUND OFF)
-      message("Warning: You are to build PCL in STATIC but VTK is SHARED!")
-      message("Warning: VTK disabled!")
-    endif()
-  endif()
+  find_package(VTK)
 else()
   set(VTK_FOUND OFF)
 endif()
diff --git a/cmake/Modules/FindVTK.cmake b/cmake/Modules/FindVTK.cmake
new file mode 100644
index 0000000000..ab28117839
--- /dev/null
+++ b/cmake/Modules/FindVTK.cmake
@@ -0,0 +1,96 @@
+#Search for VTK
+find_package(VTK NO_MODULE)
+
+if(NOT VTK_FOUND)
+    return()
+endif()
+
+#Exit if version is below
+if("${VTK_VERSION}" VERSION_LESS 6.2)
+  message(WARNING "The minimum required version of VTK is 6.2, but found ${VTK_VERSION}")
+  set(VTK_FOUND FALSE)
+  return()
+endif()
+
+# assume VTK version >= 9.0
+set(PCL_VTK_COMPONENTS
+    CommonColor
+    ChartsCore
+    CommonCore
+    CommonDataModel
+    CommonExecutionModel
+    FiltersCore
+    FiltersExtraction
+    FiltersGeometry
+    FiltersModeling
+    ImagingCore
+    ImagingSources
+    InteractionStyle
+    InteractionWidgets
+    IOCore
+    IOGeometry
+    IOImage
+    IOLegacy
+    IOPLY
+    RenderingAnnotation
+    RenderingLOD
+    ViewsContext2D
+  )
+
+  # If not prepend vtk to names
+  if(${VTK_VERSION} VERSION_LESS 9.0)
+    list(TRANSFORM PCL_VTK_COMPONENTS PREPEND vtk PCL_VTK_COMPONENTS)
+  endif()
+
+if(NOT DEFINED VTK_RENDERING_BACKEND)
+# Use OpenGL backend pre 8.1, else use OpenGL2
+if(${VTK_VERSION} VERSION_LESS 8.1)
+    set(VTK_RENDERING_BACKEND "OpenGL")
+  else()
+    set(VTK_RENDERING_BACKEND "OpenGL2")
+  endif()
+endif()
+
+list(APPEND PCL_VTK_COMPONENTS RenderingContext${VTK_RENDERING_BACKEND})
+
+if(WITH_QT)
+  if(";${VTK_MODULES_ENABLED};" MATCHES ";vtkGUISupportQt;" AND ";${VTK_MODULES_ENABLED};" MATCHES ";vtkRenderingQt;")
+    set(QVTK_FOUND ON)
+    list(APPEND PCL_VTK_COMPONENTS RenderingQt GUISupportQt)
+  else()
+    unset(QVTK_FOUND)
+  endif()
+endif()
+
+find_package(VTK NO_MODULE COMPONENTS ${PCL_VTK_COMPONENTS})
+
+message(STATUS "VTK_MAJOR_VERSION ${VTK_MAJOR_VERSION}, rendering backend: ${VTK_RENDERING_BACKEND}")
+
+if(PCL_SHARED_LIBS OR (NOT (PCL_SHARED_LIBS) AND NOT (VTK_BUILD_SHARED_LIBS)))
+  if(${VTK_VERSION} VERSION_LESS 9.0)
+    if(VTK_USE_FILE)
+      include(${VTK_USE_FILE})
+    endif()
+    
+    message(STATUS "VTK found (include: ${VTK_INCLUDE_DIRS}, libs: ${VTK_LIBRARIES}")
+  endif()
+  
+  if(APPLE)
+    option(VTK_USE_COCOA "Use Cocoa for VTK render windows" ON)
+    mark_as_advanced(VTK_USE_COCOA)
+  endif()
+  
+  if(${VTK_RENDERING_BACKEND} STREQUAL "OpenGL")
+    set(VTK_RENDERING_BACKEND_OPENGL_VERSION "1")
+    message(DEPRECATION "The rendering backend OpenGL is deprecated and not available anymore since VTK 8.2."
+                        "Please switch to the OpenGL2 backend instead, which is available since VTK 6.2."
+                        "Support of the deprecated backend will be dropped with PCL 1.13.")
+  elseif(${VTK_RENDERING_BACKEND} STREQUAL "OpenGL2")
+    set(VTK_RENDERING_BACKEND_OPENGL_VERSION "2")
+  endif()
+
+else()
+  set(VTK_FOUND OFF)
+  message("Warning: You are to build PCL in STATIC but VTK is SHARED!")
+  message("Warning: VTK disabled!")
+endif()
diff --git a/people/CMakeLists.txt b/people/CMakeLists.txt
index 4778e2f1c5..66cfede896 100644
--- a/people/CMakeLists.txt
+++ b/people/CMakeLists.txt
@@ -8,8 +8,6 @@ if(NOT VTK_FOUND)
 else()
   set(DEFAULT TRUE)
   set(REASON)
-  set(VTK_USE_FILE "${VTK_USE_FILE}" CACHE INTERNAL "VTK_USE_FILE")
-  include("${VTK_USE_FILE}")
   include_directories("${CMAKE_CURRENT_SOURCE_DIR}/include")
 endif()
 

From e548e780012cf8d924485cfccab749fa7c5a0e57 Mon Sep 17 00:00:00 2001
From: Larshg <larshg@gmail.com>
Date: Sat, 16 May 2020 22:35:13 +0200
Subject: [PATCH 06/17] Added missing includes.

---
 visualization/include/pcl/visualization/common/actor_map.h | 1 +
 visualization/src/cloud_viewer.cpp                         | 3 +++
 2 files changed, 4 insertions(+)

diff --git a/visualization/include/pcl/visualization/common/actor_map.h b/visualization/include/pcl/visualization/common/actor_map.h
index 8434d67093..e03aa1805b 100644
--- a/visualization/include/pcl/visualization/common/actor_map.h
+++ b/visualization/include/pcl/visualization/common/actor_map.h
@@ -43,6 +43,7 @@
 
 #include <vtkLODActor.h>
 #include <vtkSmartPointer.h>
+#include <vtkIdTypeArray.h>
 
 #include <map>
 #include <unordered_map>
diff --git a/visualization/src/cloud_viewer.cpp b/visualization/src/cloud_viewer.cpp
index b0b32a1af2..83fa99bbaf 100644
--- a/visualization/src/cloud_viewer.cpp
+++ b/visualization/src/cloud_viewer.cpp
@@ -40,6 +40,9 @@
 #include <pcl/visualization/boost.h>
 #include <pcl/memory.h>
 
+#include <vtkOrientationMarkerWidget.h>
+#include <vtkRenderWindowInteractor.h>
+
 #include <mutex>
 #include <thread>
 

From 9c0ef4389a8fbb3723f5e791eab2ae2be6f9081e Mon Sep 17 00:00:00 2001
From: Larshg <larshg@gmail.com>
Date: Sun, 17 May 2020 22:21:18 +0200
Subject: [PATCH 07/17] Link only to necessary vtk modules.

---
 cmake/Modules/FindVTK.cmake  | 13 ++++++++++++-
 io/CMakeLists.txt            | 17 +++++++++++++++--
 surface/CMakeLists.txt       | 24 ++++++++++++++++++------
 visualization/CMakeLists.txt | 34 +++++++++++++++++++++++++++++-----
 4 files changed, 74 insertions(+), 14 deletions(-)

diff --git a/cmake/Modules/FindVTK.cmake b/cmake/Modules/FindVTK.cmake
index ab28117839..b16c24ee89 100644
--- a/cmake/Modules/FindVTK.cmake
+++ b/cmake/Modules/FindVTK.cmake
@@ -14,15 +14,20 @@ endif()
 
 # assume VTK version >= 9.0
 set(PCL_VTK_COMPONENTS
-    CommonColor
     ChartsCore
+    CommonColor
     CommonCore
     CommonDataModel
     CommonExecutionModel
+    CommonMath
+    CommonMisc
+    CommonTransforms
     FiltersCore
     FiltersExtraction
+    FiltersGeneral
     FiltersGeometry
     FiltersModeling
+    FiltersSources
     ImagingCore
     ImagingSources
     InteractionStyle
@@ -33,7 +38,13 @@ set(PCL_VTK_COMPONENTS
     IOLegacy
     IOPLY
     RenderingAnnotation
+    RenderingCore
+    RenderingContext2D
     RenderingLOD
+    RenderingFreeType
+    RenderingOpenGL2
+    RenderingUI
+    ViewsCore
     ViewsContext2D
   )
 
diff --git a/io/CMakeLists.txt b/io/CMakeLists.txt
index f234971002..dd1f7a9b46 100644
--- a/io/CMakeLists.txt
+++ b/io/CMakeLists.txt
@@ -331,10 +331,23 @@ endif()
 set(LIB_NAME "pcl_${SUBSYS_NAME}")
 
 add_definitions(${VTK_DEFINES})
+
 PCL_ADD_LIBRARY(${LIB_NAME} COMPONENT ${SUBSYS_NAME} SOURCES ${srcs} ${incs} ${compression_incs} ${impl_incs} ${OPENNI_INCLUDES} ${OPENNI2_INCLUDES})
+
 target_include_directories(${LIB_NAME} PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include")
-link_directories(${VTK_LINK_DIRECTORIES})
-target_link_libraries("${LIB_NAME}" pcl_common pcl_io_ply ${VTK_LIBRARIES})
+
+target_link_libraries("${LIB_NAME}" pcl_common pcl_io_ply)
+
+if(${VTK_VERSION} VERSION_LESS 9.0)
+  link_directories(${VTK_LINK_DIRECTORIES})
+  target_link_libraries("${LIB_NAME}" ${VTK_LIBRARIES})
+else()
+  target_link_libraries("${LIB_NAME}" 
+                        VTK::IOImage
+                        VTK::IOGeometry
+                        VTK::IOPLY)
+endif()
+
 if(PNG_FOUND)
   target_link_libraries("${LIB_NAME}" ${PNG_LIBRARIES})
 endif()
diff --git a/surface/CMakeLists.txt b/surface/CMakeLists.txt
index e5e4231138..447f23552d 100644
--- a/surface/CMakeLists.txt
+++ b/surface/CMakeLists.txt
@@ -4,7 +4,7 @@ set(SUBSYS_DEPS common search kdtree octree)
 
 set(build TRUE)
 PCL_SUBSYS_OPTION(build "${SUBSYS_NAME}" "${SUBSYS_DESC}" ON)
-PCL_SUBSYS_DEPEND(build "${SUBSYS_NAME}" DEPS ${SUBSYS_DEPS} OPT_DEPS qhull)
+PCL_SUBSYS_DEPEND(build "${SUBSYS_NAME}" DEPS ${SUBSYS_DEPS} EXT_DEPS vtk OPT_DEPS qhull)
 
 PCL_ADD_DOC("${SUBSYS_NAME}")
 
@@ -149,19 +149,30 @@ set(impl_incs
 )
 
 set(LIB_NAME "pcl_${SUBSYS_NAME}")
+
 include_directories(
   "${CMAKE_CURRENT_SOURCE_DIR}/include"
   "${CMAKE_CURRENT_SOURCE_DIR}"
 )
-include_directories(SYSTEM
-  ${VTK_INCLUDE_DIRS}
-)
-link_directories(${VTK_LIBRARY_DIRS})
 PCL_ADD_LIBRARY(${LIB_NAME} COMPONENT ${SUBSYS_NAME} SOURCES ${srcs} ${incs} ${impl_incs} ${VTK_SMOOTHING_INCLUDES} ${POISSON_INCLUDES} ${OPENNURBS_INCLUDES} ${ON_NURBS_INCLUDES})
-target_link_libraries("${LIB_NAME}" pcl_common pcl_search pcl_kdtree pcl_octree ${VTK_LIBRARIES} ${ON_NURBS_LIBRARIES})
+
+target_link_libraries("${LIB_NAME}" pcl_common pcl_search pcl_kdtree pcl_octree ${ON_NURBS_LIBRARIES})
+
+if(${VTK_VERSION} VERSION_LESS 9.0)
+  include_directories(SYSTEM ${VTK_INCLUDE_DIRS})
+  link_directories(${VTK_LIBRARY_DIRS})
+  target_link_libraries("${LIB_NAME}" ${VTK_LIBRARIES})
+else()
+  target_link_libraries("${LIB_NAME}" VTK::CommonDataModel
+                                      VTK::CommonExecutionModel
+                                      VTK::FiltersModeling
+                                      VTK::FiltersCore)
+endif()
+
 if(QHULL_FOUND)
   target_link_libraries("${LIB_NAME}" ${QHULL_LIBRARIES})
 endif()
+
 PCL_MAKE_PKGCONFIG(${LIB_NAME} COMPONENT ${SUBSYS_NAME} DESC ${SUBSYS_DESC} PCL_DEPS ${SUBSYS_DEPS})
 
 # Install include files
@@ -178,6 +189,7 @@ endif()
 if(VTK_FOUND AND NOT ANDROID)
   PCL_ADD_INCLUDES("${SUBSYS_NAME}" "${SUBSYS_NAME}/vtk_smoothing" ${VTK_SMOOTHING_INCLUDES})
 endif()
+
 if(WIN32)
   target_link_libraries("${LIB_NAME}" Rpcrt4.lib)
 endif()
diff --git a/visualization/CMakeLists.txt b/visualization/CMakeLists.txt
index 7f312baabc..9131d871e3 100644
--- a/visualization/CMakeLists.txt
+++ b/visualization/CMakeLists.txt
@@ -134,14 +134,37 @@ endif()
 set(LIB_NAME "pcl_${SUBSYS_NAME}")
 PCL_ADD_LIBRARY(${LIB_NAME} COMPONENT ${SUBSYS_NAME} SOURCES ${srcs} ${incs} ${common_incs} ${impl_incs} ${common_impl_incs} ${vtk_incs})
 
-target_include_directories("${LIB_NAME}" SYSTEM PUBLIC ${VTK_INCLUDE_DIRS})
-
 # apple workaround (continued)
 if(APPLE)
   target_link_libraries("${LIB_NAME}" "-framework Cocoa")
 endif()
 
-target_link_libraries("${LIB_NAME}" pcl_common pcl_io pcl_kdtree ${VTK_LIBRARIES} ${OPENGL_LIBRARIES})
+target_link_libraries("${LIB_NAME}" pcl_common pcl_io pcl_kdtree ${OPENGL_LIBRARIES})
+
+if(${VTK_VERSION} VERSION_LESS 9.0)
+  target_include_directories("${LIB_NAME}" SYSTEM PUBLIC ${VTK_INCLUDE_DIRS})
+  target_link_libraries("${LIB_NAME}" ${VTK_LIBRARIES})
+else()
+  #Some libs are referenced through depending on IO
+  target_link_libraries("${LIB_NAME}"
+                        VTK::ChartsCore
+                        VTK::CommonColor
+                        VTK::CommonDataModel
+                        VTK::FiltersExtraction
+                        VTK::FiltersGeneral
+                        VTK::FiltersModeling
+                        VTK::FiltersSources
+                        VTK::IOImage
+                        VTK::IOPLY
+                        VTK::ImagingSources
+                        VTK::InteractionStyle
+                        VTK::RenderingAnnotation
+                        VTK::RenderingContext2D
+                        VTK::RenderingFreeType
+                        VTK::RenderingLOD
+                        VTK::RenderingOpenGL2
+                        VTK::ViewsContext2D)
+endif()
 
 set(EXT_DEPS "")
 if(WITH_OPENNI)
@@ -172,10 +195,11 @@ PCL_ADD_INCLUDES("${SUBSYS_NAME}" "${SUBSYS_NAME}/common/impl" ${common_impl_inc
 PCL_ADD_INCLUDES("${SUBSYS_NAME}" "${SUBSYS_NAME}/vtk" ${vtk_incs})
 
 if(${VTK_VERSION} VERSION_GREATER_EQUAL 9.0)
-  vtk_module_autoinit(TARGETS "${LIB_NAME}" MODULES VTK::RenderingOpenGL2 VTK::RenderingFreeType)
+  vtk_module_autoinit(TARGETS "${LIB_NAME}" 
+                      MODULES VTK::RenderingOpenGL2
+                              VTK::RenderingFreeType)
 endif()
 
-
 if(BUILD_TESTS)
   add_subdirectory(test)
 endif()

From 382ef310ea344b9d3324fe2a249ab44dfbaec03a Mon Sep 17 00:00:00 2001
From: Larshg <larshg@gmail.com>
Date: Thu, 21 May 2020 22:42:57 +0200
Subject: [PATCH 08/17] Fix list transform with appending VTK.

Move RenderingUI as it is not available vtk < 9.0
---
 cmake/Modules/FindVTK.cmake | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

diff --git a/cmake/Modules/FindVTK.cmake b/cmake/Modules/FindVTK.cmake
index b16c24ee89..9a92a50be4 100644
--- a/cmake/Modules/FindVTK.cmake
+++ b/cmake/Modules/FindVTK.cmake
@@ -43,14 +43,13 @@ set(PCL_VTK_COMPONENTS
     RenderingLOD
     RenderingFreeType
     RenderingOpenGL2
-    RenderingUI
     ViewsCore
     ViewsContext2D
   )
 
   # If not prepend vtk to names
   if(${VTK_VERSION} VERSION_LESS 9.0)
-    list(TRANSFORM PCL_VTK_COMPONENTS PREPEND vtk PCL_VTK_COMPONENTS)
+    list(TRANSFORM PCL_VTK_COMPONENTS PREPEND "vtk")
   endif()
 
 if(NOT DEFINED VTK_RENDERING_BACKEND)
@@ -62,7 +61,12 @@ if(${VTK_VERSION} VERSION_LESS 8.1)
   endif()
 endif()
 
-list(APPEND PCL_VTK_COMPONENTS RenderingContext${VTK_RENDERING_BACKEND})
+if(${VTK_VERSION} VERSION_LESS 9.0)
+  list(APPEND PCL_VTK_COMPONENTS vtkRenderingContext${VTK_RENDERING_BACKEND})
+else()
+  list(APPEND PCL_VTK_COMPONENTS RenderingUI) # not available < 9.0 so append here.
+  list(APPEND PCL_VTK_COMPONENTS RenderingContext${VTK_RENDERING_BACKEND})
+endif()
 
 if(WITH_QT)
   if(";${VTK_MODULES_ENABLED};" MATCHES ";vtkGUISupportQt;" AND ";${VTK_MODULES_ENABLED};" MATCHES ";vtkRenderingQt;")

From a3cef59a1faedfce49fd90cf0eadc1a69d38b98a Mon Sep 17 00:00:00 2001
From: Larshg <larshg@gmail.com>
Date: Fri, 22 May 2020 20:56:54 +0200
Subject: [PATCH 09/17] Don't include VTK_USE_FILE if vtk > 9.0

---
 apps/3d_rec_framework/CMakeLists.txt       |  4 +++-
 apps/CMakeLists.txt                        |  4 +++-
 apps/cloud_composer/CMakeLists.txt         |  4 +++-
 apps/modeler/CMakeLists.txt                |  6 ++++--
 examples/keypoints/CMakeLists.txt          | 10 ++++++----
 examples/outofcore/CMakeLists.txt          |  4 +++-
 examples/segmentation/CMakeLists.txt       |  5 ++++-
 examples/stereo/CMakeLists.txt             |  4 +++-
 examples/surface/CMakeLists.txt            |  4 +++-
 gpu/kinfu/tools/CMakeLists.txt             |  6 ++++--
 gpu/kinfu_large_scale/tools/CMakeLists.txt |  6 ++++--
 gpu/people/tools/CMakeLists.txt            |  6 ++++--
 outofcore/tools/CMakeLists.txt             |  6 ++++--
 13 files changed, 48 insertions(+), 21 deletions(-)

diff --git a/apps/3d_rec_framework/CMakeLists.txt b/apps/3d_rec_framework/CMakeLists.txt
index ec6c56d8fa..aea22fb66b 100644
--- a/apps/3d_rec_framework/CMakeLists.txt
+++ b/apps/3d_rec_framework/CMakeLists.txt
@@ -9,7 +9,9 @@ if(NOT VTK_FOUND)
 else()
   set(DEFAULT TRUE)
   set(REASON)
-  include("${VTK_USE_FILE}")
+  if(${VTK_VERSION} VERSION_LESS 9.0)
+    include(${VTK_USE_FILE})
+  endif()
 endif()
 
 # OpenNI found?
diff --git a/apps/CMakeLists.txt b/apps/CMakeLists.txt
index c4ef60c6f9..1a55968784 100644
--- a/apps/CMakeLists.txt
+++ b/apps/CMakeLists.txt
@@ -34,7 +34,9 @@ if(LIBUSB_1_FOUND)
 endif()
 
 if(VTK_FOUND)
-  include("${VTK_USE_FILE}")
+  if(${VTK_VERSION} VERSION_LESS 9.0)
+    include(${VTK_USE_FILE})
+  endif()
 
   set(incs "include/pcl/${SUBSYS_NAME}/render_views_tesselated_sphere.h")
   set(srcs "src/render_views_tesselated_sphere.cpp")
diff --git a/apps/cloud_composer/CMakeLists.txt b/apps/cloud_composer/CMakeLists.txt
index d70e4bb543..0dfdedb1e9 100644
--- a/apps/cloud_composer/CMakeLists.txt
+++ b/apps/cloud_composer/CMakeLists.txt
@@ -14,7 +14,9 @@ if(NOT VTK_FOUND)
 else()
   set(DEFAULT TRUE)
   set(REASON)
-  include("${VTK_USE_FILE}")
+  if(${VTK_VERSION} VERSION_LESS 9.0)
+    include(${VTK_USE_FILE})
+  endif()
 endif()
 
 # QT5 Found?
diff --git a/apps/modeler/CMakeLists.txt b/apps/modeler/CMakeLists.txt
index 7b1d14b580..1cc01c9851 100644
--- a/apps/modeler/CMakeLists.txt
+++ b/apps/modeler/CMakeLists.txt
@@ -10,8 +10,10 @@ if(NOT VTK_FOUND)
 else()
   set(DEFAULT TRUE)
   set(REASON)
-  set(VTK_USE_FILE "${VTK_USE_FILE}" CACHE INTERNAL "VTK_USE_FILE")
-  include("${VTK_USE_FILE}")
+  if(${VTK_VERSION} VERSION_LESS 9.0)
+    set(VTK_USE_FILE "${VTK_USE_FILE}" CACHE INTERNAL "VTK_USE_FILE")
+    include("${VTK_USE_FILE}")
+  endif()
 endif()
 
 # QT5 Found?
diff --git a/examples/keypoints/CMakeLists.txt b/examples/keypoints/CMakeLists.txt
index 133bfed82a..a7ac3c6849 100644
--- a/examples/keypoints/CMakeLists.txt
+++ b/examples/keypoints/CMakeLists.txt
@@ -6,12 +6,14 @@ PCL_SUBSYS_DEPEND(build ${SUBSYS_NAME} DEPS visualization)
 
 ## Find VTK
 if(NOT VTK_FOUND)
-    set(DEFAULT FALSE)
-    set(REASON "VTK was not found.")
+  set(DEFAULT FALSE)
+  set(REASON "VTK was not found.")
 else()
-    set(DEFAULT TRUE)
-    set(REASON)
+  set(DEFAULT TRUE)
+  set(REASON)
+  if(${VTK_VERSION} VERSION_LESS 9.0)
     include(${VTK_USE_FILE})
+  endif()
 endif()
 
 PCL_ADD_EXAMPLE(pcl_example_sift_keypoint_estimation FILES example_sift_keypoint_estimation.cpp
diff --git a/examples/outofcore/CMakeLists.txt b/examples/outofcore/CMakeLists.txt
index aed932e9b5..7fb93b42e9 100644
--- a/examples/outofcore/CMakeLists.txt
+++ b/examples/outofcore/CMakeLists.txt
@@ -9,7 +9,9 @@ if(NOT VTK_FOUND)
 else()
   set(DEFAULT TRUE)
   set(REASON)
-  include(${VTK_USE_FILE})
+  if(${VTK_VERSION} VERSION_LESS 9.0)
+    include(${VTK_USE_FILE})
+  endif()
 endif()
 
 PCL_SUBSYS_DEPEND (build ${SUBSYS_NAME} DEPS outofcore io common octree filters visualization EXT_DEPS vtk)
diff --git a/examples/segmentation/CMakeLists.txt b/examples/segmentation/CMakeLists.txt
index 5e672fd821..ca35a832c2 100644
--- a/examples/segmentation/CMakeLists.txt
+++ b/examples/segmentation/CMakeLists.txt
@@ -11,7 +11,10 @@ PCL_ADD_EXAMPLE(pcl_example_region_growing FILES example_region_growing.cpp
 
 ## Find VTK
 if(VTK_FOUND)
-  include(${VTK_USE_FILE})
+  if(${VTK_VERSION} VERSION_LESS 9.0)
+    include(${VTK_USE_FILE})
+  endif()
+  
   PCL_ADD_EXAMPLE(pcl_example_supervoxels FILES example_supervoxels.cpp
                   LINK_WITH pcl_common pcl_features pcl_segmentation pcl_octree pcl_kdtree pcl_visualization)
   PCL_ADD_EXAMPLE(pcl_example_lccp_segmentation FILES example_lccp_segmentation.cpp
diff --git a/examples/stereo/CMakeLists.txt b/examples/stereo/CMakeLists.txt
index 518d104942..853090b74c 100644
--- a/examples/stereo/CMakeLists.txt
+++ b/examples/stereo/CMakeLists.txt
@@ -11,7 +11,9 @@ if(NOT VTK_FOUND)
 else()
   set(DEFAULT TRUE)
   set(REASON)
-  include(${VTK_USE_FILE})
+  if(${VTK_VERSION} VERSION_LESS 9.0)
+    include(${VTK_USE_FILE})
+  endif()
 endif()
 
 PCL_ADD_EXAMPLE(pcl_example_stereo_baseline FILES example_stereo_baseline.cpp
diff --git a/examples/surface/CMakeLists.txt b/examples/surface/CMakeLists.txt
index ec9f030d61..0ff87a34dd 100644
--- a/examples/surface/CMakeLists.txt
+++ b/examples/surface/CMakeLists.txt
@@ -11,7 +11,9 @@ if(NOT VTK_FOUND)
 else()
   set(DEFAULT TRUE)
   set(REASON)
-  include(${VTK_USE_FILE})
+  if(${VTK_VERSION} VERSION_LESS 9.0)
+    include(${VTK_USE_FILE})
+  endif()
 endif()
 
 PCL_ADD_EXAMPLE(pcl_test_nurbs_fitting_surface
diff --git a/gpu/kinfu/tools/CMakeLists.txt b/gpu/kinfu/tools/CMakeLists.txt
index 8ef3d920df..5733516170 100644
--- a/gpu/kinfu/tools/CMakeLists.txt
+++ b/gpu/kinfu/tools/CMakeLists.txt
@@ -8,8 +8,10 @@ if(NOT VTK_FOUND)
 else()
   set(DEFAULT TRUE)
   set(REASON)
-  set(VTK_USE_FILE "${VTK_USE_FILE}" CACHE INTERNAL "VTK_USE_FILE")
-  include("${VTK_USE_FILE}")
+  if(${VTK_VERSION} VERSION_LESS 9.0)
+    set(VTK_USE_FILE ${VTK_USE_FILE} CACHE INTERNAL "VTK_USE_FILE")
+    include(${VTK_USE_FILE})
+  endif()
   include_directories("${CMAKE_CURRENT_SOURCE_DIR}/include")
 endif()
 
diff --git a/gpu/kinfu_large_scale/tools/CMakeLists.txt b/gpu/kinfu_large_scale/tools/CMakeLists.txt
index 9df2535a5c..4e10ce821a 100644
--- a/gpu/kinfu_large_scale/tools/CMakeLists.txt
+++ b/gpu/kinfu_large_scale/tools/CMakeLists.txt
@@ -8,8 +8,10 @@ if(NOT VTK_FOUND)
 else()
   set(DEFAULT TRUE)
   set(REASON)
-  set(VTK_USE_FILE "${VTK_USE_FILE}" CACHE INTERNAL "VTK_USE_FILE")
-  include("${VTK_USE_FILE}")
+  if(${VTK_VERSION} VERSION_LESS 9.0)
+    set(VTK_USE_FILE ${VTK_USE_FILE} CACHE INTERNAL "VTK_USE_FILE")
+    include(${VTK_USE_FILE})
+  endif()
   include_directories("${CMAKE_CURRENT_SOURCE_DIR}/include")
 endif()
 
diff --git a/gpu/people/tools/CMakeLists.txt b/gpu/people/tools/CMakeLists.txt
index 3d45dbc42a..d47fb301c5 100644
--- a/gpu/people/tools/CMakeLists.txt
+++ b/gpu/people/tools/CMakeLists.txt
@@ -6,8 +6,10 @@ if(NOT VTK_FOUND)
 else()
   set(DEFAULT TRUE)
   set(REASON)
-  set(VTK_USE_FILE "${VTK_USE_FILE}" CACHE INTERNAL "VTK_USE_FILE")
-  include("${VTK_USE_FILE}")
+  if(${VTK_VERSION} VERSION_LESS 9.0)
+    set(VTK_USE_FILE ${VTK_USE_FILE} CACHE INTERNAL "VTK_USE_FILE")
+    include(${VTK_USE_FILE})
+  endif()
   include_directories("${CMAKE_CURRENT_SOURCE_DIR}/include")
 endif()
 
diff --git a/outofcore/tools/CMakeLists.txt b/outofcore/tools/CMakeLists.txt
index 53c63e3940..37ca2079bf 100644
--- a/outofcore/tools/CMakeLists.txt
+++ b/outofcore/tools/CMakeLists.txt
@@ -13,8 +13,10 @@ if(NOT VTK_FOUND)
 else()
   set(DEFAULT TRUE)
   set(REASON)
-  set(VTK_USE_FILE "${VTK_USE_FILE}" CACHE INTERNAL "VTK_USE_FILE")
-  include("${VTK_USE_FILE}")
+  if(${VTK_VERSION} VERSION_LESS 9.0)
+    set(VTK_USE_FILE "${VTK_USE_FILE}" CACHE INTERNAL "VTK_USE_FILE")
+    include("${VTK_USE_FILE}")
+  endif()
   include_directories("${CMAKE_CURRENT_SOURCE_DIR}/include")
 
   set(srcs outofcore_viewer.cpp

From e85ab545ea03930a126c521a5c47deab4e23b089 Mon Sep 17 00:00:00 2001
From: Larshg <larshg@gmail.com>
Date: Sat, 23 May 2020 23:44:16 +0200
Subject: [PATCH 10/17] Fix Tools

---
 tools/CMakeLists.txt | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/tools/CMakeLists.txt b/tools/CMakeLists.txt
index 1911f7b9f4..fc5b92f14d 100644
--- a/tools/CMakeLists.txt
+++ b/tools/CMakeLists.txt
@@ -200,12 +200,18 @@ else()
 
   PCL_ADD_EXECUTABLE(pcl_obj2pcd COMPONENT ${SUBSYS_NAME} SOURCES obj2pcd.cpp)
   target_link_libraries(pcl_obj2pcd pcl_common pcl_io)
+  if(${VTK_VERSION} GREATER_EQUAL 9.0)
+    target_link_libraries(pcl_obj2pcd VTK::FiltersCore)
+  endif()
 
   PCL_ADD_EXECUTABLE(pcl_obj2ply COMPONENT ${SUBSYS_NAME} SOURCES obj2ply.cpp)
   target_link_libraries(pcl_obj2ply pcl_common pcl_io)
 
   PCL_ADD_EXECUTABLE(pcl_vtk2pcd COMPONENT ${SUBSYS_NAME} SOURCES vtk2pcd.cpp)
   target_link_libraries(pcl_vtk2pcd pcl_common pcl_io)
+  if(${VTK_VERSION} GREATER_EQUAL 9.0)
+    target_link_libraries(pcl_vtk2pcd VTK::FiltersCore)
+  endif()
 
   if(BUILD_visualization)
     PCL_ADD_EXECUTABLE(pcl_obj_rec_ransac_model_opps COMPONENT ${SUBSYS_NAME} SOURCES obj_rec_ransac_model_opps.cpp)

From a32a9e5bf91fe2fd2d29c3bb696e311b467c88b0 Mon Sep 17 00:00:00 2001
From: Lars Glud <larshg@gmail.com>
Date: Sun, 24 May 2020 21:08:53 +0200
Subject: [PATCH 11/17] Use support cmake cmds.

---
 cmake/Modules/FindVTK.cmake  | 95 +++++++++++++++++++++---------------
 tools/CMakeLists.txt         |  4 +-
 visualization/CMakeLists.txt |  2 +-
 3 files changed, 60 insertions(+), 41 deletions(-)

diff --git a/cmake/Modules/FindVTK.cmake b/cmake/Modules/FindVTK.cmake
index 9a92a50be4..8db34ee979 100644
--- a/cmake/Modules/FindVTK.cmake
+++ b/cmake/Modules/FindVTK.cmake
@@ -12,45 +12,64 @@ if("${VTK_VERSION}" VERSION_LESS 6.2)
   return()
 endif()
 
-# assume VTK version >= 9.0
+#Change to use list Transform when cmake is >= 3.12
+if(NOT (${VTK_VERSION} VERSION_LESS 9.0))
 set(PCL_VTK_COMPONENTS
-    ChartsCore
-    CommonColor
-    CommonCore
-    CommonDataModel
-    CommonExecutionModel
-    CommonMath
-    CommonMisc
-    CommonTransforms
-    FiltersCore
-    FiltersExtraction
-    FiltersGeneral
-    FiltersGeometry
-    FiltersModeling
-    FiltersSources
-    ImagingCore
-    ImagingSources
-    InteractionStyle
-    InteractionWidgets
-    IOCore
-    IOGeometry
-    IOImage
-    IOLegacy
-    IOPLY
-    RenderingAnnotation
-    RenderingCore
-    RenderingContext2D
-    RenderingLOD
-    RenderingFreeType
-    RenderingOpenGL2
-    ViewsCore
-    ViewsContext2D
-  )
-
-  # If not prepend vtk to names
-  if(${VTK_VERSION} VERSION_LESS 9.0)
-    list(TRANSFORM PCL_VTK_COMPONENTS PREPEND "vtk")
-  endif()
+  ChartsCore
+  CommonColor
+  CommonCore
+  CommonDataModel
+  CommonExecutionModel
+  CommonMath
+  CommonMisc
+  CommonTransforms
+  FiltersCore
+  FiltersExtraction
+  FiltersGeneral
+  FiltersGeometry
+  FiltersModeling
+  FiltersSources
+  ImagingCore
+  ImagingSources
+  InteractionStyle
+  InteractionWidgets
+  IOCore
+  IOGeometry
+  IOImage
+  IOLegacy
+  IOPLY
+  RenderingAnnotation
+  RenderingCore
+  RenderingContext2D
+  RenderingLOD
+  RenderingFreeType
+  RenderingOpenGL2
+  ViewsCore
+  ViewsContext2D
+)
+else()
+set(PCL_VTK_COMPONENTS
+  vtkChartsCore
+  vtkCommonCore
+  vtkCommonDataModel
+  vtkCommonExecutionModel
+  vtkFiltersCore
+  vtkFiltersExtraction
+  vtkFiltersModeling
+  vtkImagingCore
+  vtkImagingSources
+  vtkInteractionStyle
+  vtkInteractionWidgets
+  vtkIOCore
+  vtkIOGeometry
+  vtkIOImage
+  vtkIOLegacy
+  vtkIOPLY
+  vtkRenderingAnnotation
+  vtkRenderingLOD
+  vtkViewsContext2D
+)
+endif()
 
 if(NOT DEFINED VTK_RENDERING_BACKEND)
 # Use OpenGL backend pre 8.1, else use OpenGL2
diff --git a/tools/CMakeLists.txt b/tools/CMakeLists.txt
index fc5b92f14d..5118dc85f5 100644
--- a/tools/CMakeLists.txt
+++ b/tools/CMakeLists.txt
@@ -200,7 +200,7 @@ else()
 
   PCL_ADD_EXECUTABLE(pcl_obj2pcd COMPONENT ${SUBSYS_NAME} SOURCES obj2pcd.cpp)
   target_link_libraries(pcl_obj2pcd pcl_common pcl_io)
-  if(${VTK_VERSION} GREATER_EQUAL 9.0)
+  if(NOT (${VTK_VERSION} VERSION_LESS 9.0))
     target_link_libraries(pcl_obj2pcd VTK::FiltersCore)
   endif()
 
@@ -209,7 +209,7 @@ else()
 
   PCL_ADD_EXECUTABLE(pcl_vtk2pcd COMPONENT ${SUBSYS_NAME} SOURCES vtk2pcd.cpp)
   target_link_libraries(pcl_vtk2pcd pcl_common pcl_io)
-  if(${VTK_VERSION} GREATER_EQUAL 9.0)
+  if(NOT (${VTK_VERSION} VERSION_LESS 9.0))
     target_link_libraries(pcl_vtk2pcd VTK::FiltersCore)
   endif()
 
diff --git a/visualization/CMakeLists.txt b/visualization/CMakeLists.txt
index 9131d871e3..1849cdcc8f 100644
--- a/visualization/CMakeLists.txt
+++ b/visualization/CMakeLists.txt
@@ -194,7 +194,7 @@ PCL_ADD_INCLUDES("${SUBSYS_NAME}" "${SUBSYS_NAME}/impl" ${impl_incs})
 PCL_ADD_INCLUDES("${SUBSYS_NAME}" "${SUBSYS_NAME}/common/impl" ${common_impl_incs})
 PCL_ADD_INCLUDES("${SUBSYS_NAME}" "${SUBSYS_NAME}/vtk" ${vtk_incs})
 
-if(${VTK_VERSION} VERSION_GREATER_EQUAL 9.0)
+if(NOT (${VTK_VERSION} VERSION_LESS 9.0))
   vtk_module_autoinit(TARGETS "${LIB_NAME}" 
                       MODULES VTK::RenderingOpenGL2
                               VTK::RenderingFreeType)

From e3fb9fc4246cfec2b422b1707feff7f774f863cf Mon Sep 17 00:00:00 2001
From: Lars Glud <larshg@gmail.com>
Date: Sun, 24 May 2020 21:21:23 +0200
Subject: [PATCH 12/17] Fix qtmodules

---
 cmake/Modules/FindVTK.cmake | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/cmake/Modules/FindVTK.cmake b/cmake/Modules/FindVTK.cmake
index 8db34ee979..d5ec766e10 100644
--- a/cmake/Modules/FindVTK.cmake
+++ b/cmake/Modules/FindVTK.cmake
@@ -90,7 +90,11 @@ endif()
 if(WITH_QT)
   if(";${VTK_MODULES_ENABLED};" MATCHES ";vtkGUISupportQt;" AND ";${VTK_MODULES_ENABLED};" MATCHES ";vtkRenderingQt;")
     set(QVTK_FOUND ON)
-    list(APPEND PCL_VTK_COMPONENTS RenderingQt GUISupportQt)
+    if(NOT (${VTK_VERSION} VERSION_LESS 9.0))
+      list(APPEND PCL_VTK_COMPONENTS RenderingQt GUISupportQt)
+    else()
+      list(APPEND PCL_VTK_COMPONENTS vtkRenderingQt vtkGUISupportQt)
+    endif()
   else()
     unset(QVTK_FOUND)
   endif()

From 965e2f17e6d01217e88bc756d11d13d27e1395a0 Mon Sep 17 00:00:00 2001
From: Larshg <larshg@gmail.com>
Date: Sun, 24 May 2020 21:39:11 +0200
Subject: [PATCH 14/17] Check if VTK is present

---
 io/CMakeLists.txt | 19 ++++++++++---------
 1 file changed, 10 insertions(+), 9 deletions(-)

diff --git a/io/CMakeLists.txt b/io/CMakeLists.txt
index dd1f7a9b46..9163eff7f8 100644
--- a/io/CMakeLists.txt
+++ b/io/CMakeLists.txt
@@ -337,15 +337,16 @@ PCL_ADD_LIBRARY(${LIB_NAME} COMPONENT ${SUBSYS_NAME} SOURCES ${srcs} ${incs} ${c
 target_include_directories(${LIB_NAME} PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include")
 
 target_link_libraries("${LIB_NAME}" pcl_common pcl_io_ply)
-
-if(${VTK_VERSION} VERSION_LESS 9.0)
-  link_directories(${VTK_LINK_DIRECTORIES})
-  target_link_libraries("${LIB_NAME}" ${VTK_LIBRARIES})
-else()
-  target_link_libraries("${LIB_NAME}" 
-                        VTK::IOImage
-                        VTK::IOGeometry
-                        VTK::IOPLY)
+if(VTK_FOUND)
+  if(${VTK_VERSION} VERSION_LESS 9.0)
+    link_directories(${VTK_LINK_DIRECTORIES})
+    target_link_libraries("${LIB_NAME}" ${VTK_LIBRARIES})
+  else()
+    target_link_libraries("${LIB_NAME}" 
+                          VTK::IOImage
+                          VTK::IOGeometry
+                          VTK::IOPLY)
+  endif()
 endif()
 
 if(PNG_FOUND)

From 3a522f4140b6528ff1f45568de7c784402e457f8 Mon Sep 17 00:00:00 2001
From: Larshg <larshg@gmail.com>
Date: Sun, 24 May 2020 22:00:32 +0200
Subject: [PATCH 15/17] Check if VTK found in surface.

---
 surface/CMakeLists.txt | 20 +++++++++++---------
 1 file changed, 11 insertions(+), 9 deletions(-)

diff --git a/surface/CMakeLists.txt b/surface/CMakeLists.txt
index 447f23552d..6ab1917f08 100644
--- a/surface/CMakeLists.txt
+++ b/surface/CMakeLists.txt
@@ -158,15 +158,17 @@ PCL_ADD_LIBRARY(${LIB_NAME} COMPONENT ${SUBSYS_NAME} SOURCES ${srcs} ${incs} ${i
 
 target_link_libraries("${LIB_NAME}" pcl_common pcl_search pcl_kdtree pcl_octree ${ON_NURBS_LIBRARIES})
 
-if(${VTK_VERSION} VERSION_LESS 9.0)
-  include_directories(SYSTEM ${VTK_INCLUDE_DIRS})
-  link_directories(${VTK_LIBRARY_DIRS})
-  target_link_libraries("${LIB_NAME}" ${VTK_LIBRARIES})
-else()
-  target_link_libraries("${LIB_NAME}" VTK::CommonDataModel
-                                      VTK::CommonExecutionModel
-                                      VTK::FiltersModeling
-                                      VTK::FiltersCore)
+if(VTK_FOUND)
+  if(${VTK_VERSION} VERSION_LESS 9.0)
+    include_directories(SYSTEM ${VTK_INCLUDE_DIRS})
+    link_directories(${VTK_LIBRARY_DIRS})
+    target_link_libraries("${LIB_NAME}" ${VTK_LIBRARIES})
+  else()
+    target_link_libraries("${LIB_NAME}" VTK::CommonDataModel
+                                        VTK::CommonExecutionModel
+                                        VTK::FiltersModeling
+                                        VTK::FiltersCore)
+  endif()
 endif()
 
 if(QHULL_FOUND)

From 2b68fcfe1cd6ad1041267565742a13faf87da770 Mon Sep 17 00:00:00 2001
From: Lars Glud <larshg@gmail.com>
Date: Tue, 26 May 2020 20:51:29 +0200
Subject: [PATCH 16/17] Define PCL_SHARED_LIBS for findVTK script to work

---
 PCLConfig.cmake.in | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/PCLConfig.cmake.in b/PCLConfig.cmake.in
index 349ad7c4c5..2c83bdfa2f 100644
--- a/PCLConfig.cmake.in
+++ b/PCLConfig.cmake.in
@@ -429,6 +429,8 @@ else()
 endif()
 
 set(PCL_INCLUDE_DIRS "${PCL_CONF_INCLUDE_DIR}")
+#Used in FindVTK to avoid linking static PCL with shared VTK.
+set(PCL_SHARED_LIBS @PCL_SHARED_LIBS@)
 
 #set a suffix for debug libraries
 set(PCL_DEBUG_SUFFIX "@CMAKE_DEBUG_POSTFIX@")

From 5b8544e332336531742a606b7277d9204b0175f5 Mon Sep 17 00:00:00 2001
From: Larshg <larshg@gmail.com>
Date: Wed, 27 May 2020 14:47:25 +0200
Subject: [PATCH 17/17] Remove FindVTK and move it to PCL_only_find so it
 doesn't get shipped.

---
 CMakeLists.txt                                      | 2 +-
 PCLConfig.cmake.in                                  | 2 --
 cmake/{Modules/FindVTK.cmake => pcl_find_vtk.cmake} | 2 ++
 3 files changed, 3 insertions(+), 3 deletions(-)
 rename cmake/{Modules/FindVTK.cmake => pcl_find_vtk.cmake} (97%)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index eb945c90ea..e847142108 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -366,7 +366,7 @@ endif()
 # Find VTK
 option(WITH_VTK "Build VTK-Visualizations" TRUE)
 if(WITH_VTK AND NOT ANDROID)
-  find_package(VTK)
+  include("${PCL_SOURCE_DIR}/cmake/pcl_find_vtk.cmake")
 else()
   set(VTK_FOUND OFF)
 endif()
diff --git a/PCLConfig.cmake.in b/PCLConfig.cmake.in
index 2c83bdfa2f..349ad7c4c5 100644
--- a/PCLConfig.cmake.in
+++ b/PCLConfig.cmake.in
@@ -429,8 +429,6 @@ else()
 endif()
 
 set(PCL_INCLUDE_DIRS "${PCL_CONF_INCLUDE_DIR}")
-#Used in FindVTK to avoid linking static PCL with shared VTK.
-set(PCL_SHARED_LIBS @PCL_SHARED_LIBS@)
 
 #set a suffix for debug libraries
 set(PCL_DEBUG_SUFFIX "@CMAKE_DEBUG_POSTFIX@")
diff --git a/cmake/Modules/FindVTK.cmake b/cmake/pcl_find_vtk.cmake
similarity index 97%
rename from cmake/Modules/FindVTK.cmake
rename to cmake/pcl_find_vtk.cmake
index d5ec766e10..6e5813069c 100644
--- a/cmake/Modules/FindVTK.cmake
+++ b/cmake/pcl_find_vtk.cmake
@@ -87,8 +87,11 @@ else()
   list(APPEND PCL_VTK_COMPONENTS RenderingContext${VTK_RENDERING_BACKEND})
 endif()
 
+message("WITH_QT is:${WITH_QT}")
 if(WITH_QT)
-  if(";${VTK_MODULES_ENABLED};" MATCHES ";vtkGUISupportQt;" AND ";${VTK_MODULES_ENABLED};" MATCHES ";vtkRenderingQt;")
+  message("VTK_MODULES_ENABLED is: ${VTK_MODULES_ENABLED}|${VTK_AVAILABLE_COMPONENTS}")
+  if((";${VTK_MODULES_ENABLED};" MATCHES ";vtkGUISupportQt;" AND ";${VTK_MODULES_ENABLED};" MATCHES ";vtkRenderingQt;")
+     OR (";${VTK_AVAILABLE_COMPONENTS};" MATCHES ";GUISupportQt;" AND ";${VTK_AVAILABLE_COMPONENTS};" MATCHES ";RenderingQt;"))
     set(QVTK_FOUND ON)
     if(NOT (${VTK_VERSION} VERSION_LESS 9.0))
       list(APPEND PCL_VTK_COMPONENTS RenderingQt GUISupportQt)
openSUSE Build Service is sponsored by