forked from Kitware/VTK
/
vtkAssignCoordinates.cxx
198 lines (169 loc) · 5.58 KB
/
vtkAssignCoordinates.cxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*=========================================================================
Program: Visualization Toolkit
Module: vtkAssignCoordinates.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
/*-------------------------------------------------------------------------
Copyright 2008 Sandia Corporation.
Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
the U.S. Government retains certain rights in this software.
-------------------------------------------------------------------------*/
#include "vtkAssignCoordinates.h"
#include "vtkGraph.h"
#include "vtkIdTypeArray.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkMath.h"
#include "vtkObjectFactory.h"
#include "vtkPointData.h"
#include "vtkPointSet.h"
#include "vtkPoints.h"
vtkStandardNewMacro(vtkAssignCoordinates);
vtkAssignCoordinates::vtkAssignCoordinates()
{
this->XCoordArrayName = nullptr;
this->YCoordArrayName = nullptr;
this->ZCoordArrayName = nullptr;
this->Jitter = false;
}
vtkAssignCoordinates::~vtkAssignCoordinates()
{
delete[] this->XCoordArrayName;
delete[] this->YCoordArrayName;
delete[] this->ZCoordArrayName;
}
int vtkAssignCoordinates::RequestData(vtkInformation* vtkNotUsed(request),
vtkInformationVector** inputVector, vtkInformationVector* outputVector)
{
// get the info objects
vtkInformation* inInfo = inputVector[0]->GetInformationObject(0);
vtkInformation* outInfo = outputVector->GetInformationObject(0);
// get the input and output
vtkDataObject* input = inInfo->Get(vtkDataObject::DATA_OBJECT());
vtkDataObject* output = outInfo->Get(vtkDataObject::DATA_OBJECT());
// Do a shallow copy of the input to the output
output->ShallowCopy(input);
// Create new points on the output
vtkDataSetAttributes* data = nullptr;
vtkPoints* pts = vtkPoints::New();
if (vtkPointSet::SafeDownCast(input))
{
vtkPointSet* psInput = vtkPointSet::SafeDownCast(input);
vtkPointSet* psOutput = vtkPointSet::SafeDownCast(output);
pts->DeepCopy(psInput->GetPoints());
psOutput->SetPoints(pts);
pts->Delete();
data = psOutput->GetPointData();
}
else if (vtkGraph::SafeDownCast(input))
{
vtkGraph* graphInput = vtkGraph::SafeDownCast(input);
vtkGraph* graphOutput = vtkGraph::SafeDownCast(output);
pts->DeepCopy(graphInput->GetPoints());
graphOutput->SetPoints(pts);
pts->Delete();
data = graphOutput->GetVertexData();
}
else
{
vtkErrorMacro(<< "Input must be graph or point set.");
return 0;
}
// I need at least one coordinate array
if (!this->XCoordArrayName || strlen(XCoordArrayName) == 0)
{
return 0;
}
// Okay now check for coordinate arrays
vtkDataArray* XArray = data->GetArray(this->XCoordArrayName);
// Does the array exist at all?
if (XArray == nullptr)
{
vtkErrorMacro("Could not find array named " << this->XCoordArrayName);
return 0;
}
// Y coordinate array
vtkDataArray* YArray = nullptr;
if (this->YCoordArrayName && strlen(YCoordArrayName) > 0)
{
YArray = data->GetArray(this->YCoordArrayName);
// Does the array exist at all?
if (YArray == nullptr)
{
vtkErrorMacro("Could not find array named " << this->YCoordArrayName);
return 0;
}
}
// Z coordinate array
vtkDataArray* ZArray = nullptr;
if (this->ZCoordArrayName && strlen(ZCoordArrayName) > 0)
{
ZArray = data->GetArray(this->ZCoordArrayName);
// Does the array exist at all?
if (ZArray == nullptr)
{
vtkErrorMacro("Could not find array named " << this->ZCoordArrayName);
return 0;
}
}
// Generate the points, either x,0,0 or x,y,0 or x,y,z
int numPts = pts->GetNumberOfPoints();
for (int i = 0; i < numPts; i++)
{
double rx, ry, rz;
if (Jitter)
{
rx = vtkMath::Random() - .5;
ry = vtkMath::Random() - .5;
rz = vtkMath::Random() - .5;
rx *= .02;
ry *= .02;
rz *= .02;
}
else
{
rx = ry = rz = 0;
}
if (YArray)
{
if (ZArray)
{
pts->SetPoint(
i, XArray->GetTuple1(i) + rx, YArray->GetTuple1(i) + ry, ZArray->GetTuple1(i) + rz);
}
else
{
pts->SetPoint(i, XArray->GetTuple1(i) + rx, YArray->GetTuple1(i) + ry, 0);
}
}
else
{
pts->SetPoint(i, XArray->GetTuple1(i) + rx, 0, 0);
}
}
return 1;
}
int vtkAssignCoordinates::FillInputPortInformation(int vtkNotUsed(port), vtkInformation* info)
{
// This algorithm may accept a vtkPointSet or vtkGraph.
info->Remove(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE());
info->Append(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkPointSet");
info->Append(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkGraph");
return 1;
}
void vtkAssignCoordinates::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
os << indent << "XCoordArrayName: " << (this->XCoordArrayName ? this->XCoordArrayName : "(none)")
<< endl;
os << indent << "YCoordArrayName: " << (this->YCoordArrayName ? this->YCoordArrayName : "(none)")
<< endl;
os << indent << "ZCoordArrayName: " << (this->ZCoordArrayName ? this->ZCoordArrayName : "(none)")
<< endl;
os << indent << "Jitter: " << (this->Jitter ? "True" : "False") << endl;
}