1 /*=========================================================================
2 
3   Program:   Visualization Toolkit
4   Module:    vtkConvertToPointCloud.cxx
5 
6   Copyright (c) Kitware, Inc.
7   All rights reserved.
8   See LICENSE file for details.
9 
10      This software is distributed WITHOUT ANY WARRANTY; without even
11      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12      PURPOSE.  See the above copyright notice for more information.
13 
14 =========================================================================*/
15 #include "vtkConvertToPointCloud.h"
16 
17 #include "vtkInformation.h"
18 #include "vtkPointData.h"
19 
20 #include <numeric>
21 
22 vtkStandardNewMacro(vtkConvertToPointCloud);
23 
24 //------------------------------------------------------------------------------
RequestData(vtkInformation * vtkNotUsed (request),vtkInformationVector ** inputVector,vtkInformationVector * outputVector)25 int vtkConvertToPointCloud::RequestData(vtkInformation* vtkNotUsed(request),
26   vtkInformationVector** inputVector, vtkInformationVector* outputVector)
27 {
28   // Recover input and output
29   vtkDataSet* dataset = vtkDataSet::GetData(inputVector[0]);
30   vtkPolyData* output = vtkPolyData::GetData(outputVector);
31 
32   // Copy the point and field data
33   output->GetPointData()->ShallowCopy(dataset->GetPointData());
34   output->GetFieldData()->ShallowCopy(dataset->GetFieldData());
35 
36   // Copy the points
37   vtkPointSet* pointSet = vtkPointSet::SafeDownCast(dataset);
38   if (pointSet)
39   {
40     // Input is a vtkPointSet, copy the points
41     vtkNew<vtkPoints> points;
42     points->ShallowCopy(pointSet->GetPoints());
43     output->SetPoints(points);
44   }
45   else
46   {
47     // Not a vtkPointSet, use a slower approach
48     vtkNew<vtkPoints> points;
49     points->SetNumberOfPoints(dataset->GetNumberOfPoints());
50     for (vtkIdType i = 0; i < dataset->GetNumberOfPoints(); i++)
51     {
52       double x[3];
53       dataset->GetPoint(i, x);
54       points->SetPoint(i, x);
55     }
56     output->SetPoints(points);
57   }
58 
59   switch (this->CellGenerationMode)
60   {
61     case (vtkConvertToPointCloud::POLYVERTEX_CELL):
62     {
63       // Create a polyvertex cell
64       std::vector<vtkIdType> polyVertex(dataset->GetNumberOfPoints());
65       std::iota(polyVertex.begin(), polyVertex.end(), 0);
66       vtkNew<vtkCellArray> verts;
67       verts->InsertNextCell(dataset->GetNumberOfPoints(), polyVertex.data());
68       output->SetVerts(verts);
69     }
70     break;
71     case (vtkConvertToPointCloud::VERTEX_CELLS):
72     {
73       // Create as many vertex cells as they are points
74       // Note: A faster implementation could be done using SetVoidArray
75       vtkNew<vtkCellArray> verts;
76       verts->SetNumberOfCells(dataset->GetNumberOfPoints());
77       for (vtkIdType i = 0; i < dataset->GetNumberOfPoints(); i++)
78       {
79         verts->InsertNextCell(1, &i);
80       }
81       output->SetVerts(verts);
82     }
83     break;
84     case (vtkConvertToPointCloud::NO_CELLS):
85     default:
86       break;
87   }
88   return 1;
89 }
90 
91 //------------------------------------------------------------------------------
FillInputPortInformation(int,vtkInformation * info)92 int vtkConvertToPointCloud::FillInputPortInformation(int, vtkInformation* info)
93 {
94   info->Set(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkDataSet");
95   return 1;
96 }
97 
98 //------------------------------------------------------------------------------
PrintSelf(ostream & os,vtkIndent indent)99 void vtkConvertToPointCloud::PrintSelf(ostream& os, vtkIndent indent)
100 {
101   this->Superclass::PrintSelf(os, indent);
102   os << indent << "Cell Generation Mode: " << this->CellGenerationMode << endl;
103 }
104