1 /*=========================================================================
2 
3   Program:   Visualization Toolkit
4   Module:    vtkPolyLineSource.cxx
5 
6   Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7   All rights reserved.
8   See Copyright.txt or http://www.kitware.com/Copyright.htm 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 "vtkPolyLineSource.h"
16 
17 #include "vtkCellArray.h"
18 #include "vtkIdList.h"
19 #include "vtkInformation.h"
20 #include "vtkInformationVector.h"
21 #include "vtkObjectFactory.h"
22 #include "vtkSmartPointer.h"
23 
24 //----------------------------------------------------------------------------
25 vtkStandardNewMacro(vtkPolyLineSource);
26 
27 //----------------------------------------------------------------------------
vtkPolyLineSource()28 vtkPolyLineSource::vtkPolyLineSource()
29 {
30   this->Points = nullptr;
31   this->Closed = 0;
32 
33   this->SetNumberOfInputPorts(0);
34 }
35 
36 //----------------------------------------------------------------------------
~vtkPolyLineSource()37 vtkPolyLineSource::~vtkPolyLineSource()
38 {
39   if (this->Points)
40   {
41     this->Points->Delete();
42   }
43 }
44 
45 //----------------------------------------------------------------------------
SetNumberOfPoints(vtkIdType numPoints)46 void vtkPolyLineSource::SetNumberOfPoints(vtkIdType numPoints)
47 {
48   if (!this->Points)
49   {
50     vtkPoints* pts = vtkPoints::New(VTK_DOUBLE);
51     this->SetPoints(pts);
52     this->Points = pts;
53     pts->Delete();
54   }
55 
56   if (numPoints != this->GetNumberOfPoints())
57   {
58     this->Points->SetNumberOfPoints(numPoints);
59     this->Modified();
60   }
61 }
62 
63 //----------------------------------------------------------------------------
GetNumberOfPoints()64 vtkIdType vtkPolyLineSource::GetNumberOfPoints()
65 {
66   if (this->Points)
67   {
68     return this->Points->GetNumberOfPoints();
69   }
70 
71   return 0;
72 }
73 
74 //----------------------------------------------------------------------------
Resize(vtkIdType numPoints)75 void vtkPolyLineSource::Resize(vtkIdType numPoints)
76 {
77   if (!this->Points)
78   {
79     this->SetNumberOfPoints(numPoints);
80   }
81 
82   if (numPoints != this->GetNumberOfPoints())
83   {
84     this->Points->Resize(numPoints);
85     this->Modified();
86   }
87 }
88 
89 //----------------------------------------------------------------------------
SetPoint(vtkIdType id,double x,double y,double z)90 void vtkPolyLineSource::SetPoint(vtkIdType id, double x, double y, double z)
91 {
92   if (!this->Points)
93   {
94     return;
95   }
96 
97   if (id >= this->Points->GetNumberOfPoints())
98   {
99     vtkErrorMacro(<< "point id " << id << " is larger than the number of points");
100     return;
101   }
102 
103   this->Points->SetPoint(id, x, y, z);
104   this->Modified();
105 }
106 
107 //----------------------------------------------------------------------------
SetPoints(vtkPoints * points)108 void vtkPolyLineSource::SetPoints(vtkPoints* points)
109 {
110   if ( points != this->Points )
111   {
112     if ( this->Points != nullptr )
113     {
114       this->Points->Delete();
115     }
116     this->Points = points;
117     if ( this->Points != nullptr )
118     {
119       this->Points->Register(this);
120     }
121     this->Modified();
122   }
123 }
124 
125 //----------------------------------------------------------------------------
RequestData(vtkInformation * vtkNotUsed (request),vtkInformationVector ** vtkNotUsed (inputVector),vtkInformationVector * outputVector)126 int vtkPolyLineSource::RequestData(
127   vtkInformation *vtkNotUsed(request),
128   vtkInformationVector **vtkNotUsed(inputVector),
129   vtkInformationVector *outputVector)
130 {
131   // get the info object
132   vtkInformation *outInfo = outputVector->GetInformationObject(0);
133 
134   // get the output
135   vtkPolyData *output = vtkPolyData::SafeDownCast(
136     outInfo->Get(vtkDataObject::DATA_OBJECT()));
137 
138   vtkIdType numPoints = this->GetNumberOfPoints();
139   vtkSmartPointer<vtkIdList> pointIds = vtkSmartPointer<vtkIdList>::New();
140   pointIds->SetNumberOfIds(this->Closed ? numPoints + 1 : numPoints);
141   for (vtkIdType i = 0; i < numPoints; ++i)
142   {
143     pointIds->SetId(i, i);
144   }
145   if (this->Closed)
146   {
147     pointIds->SetId(numPoints, 0);
148   }
149 
150   vtkSmartPointer<vtkCellArray> polyLine = vtkSmartPointer<vtkCellArray>::New();
151   polyLine->InsertNextCell(pointIds);
152 
153   output->SetPoints(this->Points);
154   output->SetLines(polyLine);
155 
156   return 1;
157 }
158 
159 //----------------------------------------------------------------------------
PrintSelf(ostream & os,vtkIndent indent)160 void vtkPolyLineSource::PrintSelf(ostream& os, vtkIndent indent)
161 {
162   this->Superclass::PrintSelf(os, indent);
163 
164   os << indent << "Points: " << this->Points << "\n";
165   os << indent << "Closed: " << this->Closed << "\n";
166 }
167