From 188be593bf8d59c6a057754c71cd608fb1290433 Mon Sep 17 00:00:00 2001
From: Utkarsh Ayachit <utkarsh.ayachit@kitware.com>
Date: Wed, 5 Feb 2020 10:41:16 -0500
Subject: [PATCH] vtkExtractCells: shallow copy points, if possible

vtkExtractCells now shallow copies points, if possible thus avoiding
an expensive copy if input dataset is indeed a point-set.
---
 Filters/Extraction/vtkExtractCells.cxx | 34 ++++++++++++++++++++------
 1 file changed, 27 insertions(+), 7 deletions(-)

diff --git a/Filters/Extraction/vtkExtractCells.cxx b/Filters/Extraction/vtkExtractCells.cxx
index 384ba912d30..8996217ba50 100644
--- a/Filters/Extraction/vtkExtractCells.cxx
+++ b/Filters/Extraction/vtkExtractCells.cxx
@@ -22,6 +22,7 @@
 #include "vtkCell.h"
 #include "vtkCellArray.h"
 #include "vtkCellData.h"
+#include "vtkDoubleArray.h"
 #include "vtkIdTypeArray.h"
 #include "vtkInformation.h"
 #include "vtkNew.h"
@@ -35,6 +36,7 @@
 #include "vtkUnstructuredGrid.h"
 
 #include <algorithm>
+#include <iterator>
 #include <numeric>
 #include <vector>
 
@@ -368,16 +370,34 @@ void vtkExtractCells::Copy(vtkDataSet* input, vtkUnstructuredGrid* output)
 
   vtkIdType numPoints = input->GetNumberOfPoints();
   vtkIdType numCells = input->GetNumberOfCells();
-
   output->Allocate(numCells);
 
-  vtkNew<vtkPoints> pts;
-  pts->SetNumberOfPoints(numPoints);
-  output->SetPoints(pts);
-
-  for (vtkIdType i = 0; i < numPoints; i++)
+  if (auto inputPS = vtkPointSet::SafeDownCast(input))
+  {
+    vtkNew<vtkPoints> pts;
+    pts->ShallowCopy(inputPS->GetPoints());
+    output->SetPoints(pts);
+  }
+  else
   {
-    pts->SetPoint(i, input->GetPoint(i));
+    vtkNew<vtkPoints> pts;
+    pts->SetDataTypeToDouble();
+    pts->SetNumberOfPoints(numPoints);
+
+    double temp[3];
+    pts->GetPoint(0, temp);
+
+    auto array = vtkDoubleArray::SafeDownCast(pts->GetData());
+    assert(array && array->GetNumberOfTuples() == numPoints);
+    vtkSMPTools::For(0, numPoints, [&array, &input](vtkIdType first, vtkIdType last) {
+      double coords[3];
+      for (vtkIdType cc = first; cc < last; ++cc)
+      {
+        input->GetPoint(cc, coords);
+        array->SetTypedTuple(cc, coords);
+      }
+    });
+    output->SetPoints(pts);
   }
 
   vtkNew<vtkIdList> cellPoints;
-- 
GitLab