forked from Kitware/VTK
/
vtkPerturbCoincidentVertices.cxx
315 lines (268 loc) · 10.8 KB
/
vtkPerturbCoincidentVertices.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/*=========================================================================
Program: Visualization Toolkit
Module: vtkPerturbCoincidentVertices.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 2009 Sandia Corporation.
Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
the U.S. Government retains certain rights in this software.
-------------------------------------------------------------------------*/
#include "vtkPerturbCoincidentVertices.h"
#include "vtkBitArray.h"
#include "vtkCoincidentPoints.h"
#include "vtkDataSetAttributes.h"
#include "vtkEdgeListIterator.h"
#include "vtkFloatArray.h"
#include "vtkGraph.h"
#include "vtkIdList.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkMath.h"
#include "vtkObjectFactory.h"
#include "vtkPoints.h"
#include "vtkSmartPointer.h"
#include "vtkTimerLog.h"
#include <vector>
vtkStandardNewMacro(vtkPerturbCoincidentVertices);
//----------------------------------------------------------------------------
vtkPerturbCoincidentVertices::vtkPerturbCoincidentVertices()
{
PerturbFactor = 1.0;
}
//----------------------------------------------------------------------------
vtkPerturbCoincidentVertices::~vtkPerturbCoincidentVertices() = default;
//----------------------------------------------------------------------------
void vtkPerturbCoincidentVertices::SpiralPerturbation(vtkGraph* input, vtkGraph* output)
{
// The points will be deep copied because they
// will be modified (perturbed).
output->ShallowCopy(input);
output->GetPoints()->DeepCopy(input->GetPoints());
vtkPoints* points = output->GetPoints();
int numPoints = points->GetNumberOfPoints();
double bounds[6]; // xmin, xmax, ymin, ymax, zmin, zmax
points->ComputeBounds();
points->GetBounds(bounds);
// double point1[3] = { bounds[0], bounds[2], bounds[4] };
// double point2[3] = { bounds[1], bounds[3], bounds[5] };
double vertEdge1[3], vertEdge2[3], boundingDims[3];
int numCoincidentPoints = 0, i = 0, j = 0;
vtkSmartPointer<vtkCoincidentPoints> coincidentPoints =
vtkSmartPointer<vtkCoincidentPoints>::New();
for (i = 0; i < numPoints; ++i)
{
coincidentPoints->AddPoint(i, points->GetPoint(i));
}
coincidentPoints->RemoveNonCoincidentPoints();
coincidentPoints->InitTraversal();
vtkIdType Id = 0;
vtkIdType vertId = 0;
vtkIdType vertInDegree = 0;
vtkIdType vertOutDegree = 0;
double edgeLength = VTK_DOUBLE_MAX;
double shortestEdge = VTK_DOUBLE_MAX;
vtkInEdgeType inEdge;
vtkOutEdgeType outEdge;
// Here we compute 2 metrics, the length of the shortest edge connected to any coincident point or
// the average point distance assuming the points are uniformly distributed. The smallest of these
// two metrics will be used to scale the spiral.
// Compute shortest edge coming to/from the coincident points.
vtkIdList* coincidentPointsList = coincidentPoints->GetNextCoincidentPointIds();
while (coincidentPointsList != nullptr)
{
numCoincidentPoints = coincidentPointsList->GetNumberOfIds();
for (i = 0; i < numCoincidentPoints; ++i)
{
vertId = coincidentPointsList->GetId(i);
vertInDegree = input->GetInDegree(vertId);
vertOutDegree = input->GetOutDegree(vertId);
points->GetPoint(vertId, vertEdge1);
for (j = 0; j < vertInDegree; ++j)
{
inEdge = input->GetInEdge(vertId, j);
points->GetPoint(inEdge.Source, vertEdge2);
if (vertEdge1[0] != vertEdge2[0] || vertEdge1[1] != vertEdge2[1] ||
vertEdge1[2] != vertEdge2[2])
{
edgeLength = vtkMath::Distance2BetweenPoints(vertEdge1, vertEdge2);
}
shortestEdge = edgeLength < shortestEdge ? edgeLength : shortestEdge;
}
for (j = 0; j < vertOutDegree; ++j)
{
outEdge = input->GetOutEdge(vertId, j);
points->GetPoint(outEdge.Target, vertEdge2);
if (vertEdge1[0] != vertEdge2[0] || vertEdge1[1] != vertEdge2[1] ||
vertEdge1[2] != vertEdge2[2])
{
edgeLength = vtkMath::Distance2BetweenPoints(vertEdge1, vertEdge2);
}
shortestEdge = edgeLength < shortestEdge ? edgeLength : shortestEdge;
}
}
coincidentPointsList = coincidentPoints->GetNextCoincidentPointIds();
}
shortestEdge = sqrt(shortestEdge);
// Compute the average distance assuming all the points are uniformly dispersed
// through the bounding box.
boundingDims[0] = bounds[1] - bounds[0];
boundingDims[1] = bounds[3] - bounds[2];
boundingDims[2] = bounds[5] - bounds[4];
double averageDistance = 0.0;
if (boundingDims[2] == 0.0)
{
averageDistance = sqrt((boundingDims[0] * boundingDims[1]) / numPoints);
}
else
{
averageDistance =
pow((boundingDims[0] * boundingDims[1] * boundingDims[2]) / static_cast<double>(numPoints),
1.0 / 3.0);
}
double spiralPoint[3];
double point[3];
// use the smallest metric to scale the spiral vertices.
double scale = shortestEdge < averageDistance ? shortestEdge / 4 : averageDistance / 4;
vtkSmartPointer<vtkPoints> offsets = vtkSmartPointer<vtkPoints>::New();
coincidentPoints->InitTraversal();
coincidentPointsList = coincidentPoints->GetNextCoincidentPointIds();
// Iterate over each coordinate that may have a set of coincident point ids.
while (coincidentPointsList != nullptr)
{
// Iterate over all coincident point ids and perturb them
numCoincidentPoints = coincidentPointsList->GetNumberOfIds();
vtkCoincidentPoints::SpiralPoints(numCoincidentPoints + 1, offsets);
for (i = 0; i < numCoincidentPoints; ++i)
{
Id = coincidentPointsList->GetId(i);
points->GetPoint(Id, point);
offsets->GetPoint(i + 1, spiralPoint);
points->SetPoint(
Id, point[0] + spiralPoint[0] * scale, point[1] + spiralPoint[1] * scale, point[2]);
}
coincidentPointsList = coincidentPoints->GetNextCoincidentPointIds();
}
}
// Temp datastructure
struct Coord
{
double coord[2];
Coord() = default;
Coord(const double src[3])
{
this->coord[0] = src[0];
this->coord[1] = src[1];
}
static double distance(Coord x, Coord y)
{
return ((x.coord[0] - y.coord[0]) * (x.coord[0] - y.coord[0]) +
(x.coord[1] - y.coord[1]) * (x.coord[1] - y.coord[1]));
}
};
//----------------------------------------------------------------------------
void vtkPerturbCoincidentVertices::SimpleSpiralPerturbation(
vtkGraph* input, vtkGraph* output, float perturbFactor)
{
// The points will be deep copied because they
// will be modified (perturbed).
output->ShallowCopy(input);
output->GetPoints()->DeepCopy(input->GetPoints());
vtkPoints* points = output->GetPoints();
int numPoints = points->GetNumberOfPoints();
// Temporary abort as this perturbation method
// calculates N^2 distances which doesn't scale well.
if (numPoints > 1000)
{
return;
}
int numCoincidentPoints = 0;
double spiralOffsets[3];
double currentPoint[3];
vtkIdType index;
vtkSmartPointer<vtkTimerLog> timer = vtkSmartPointer<vtkTimerLog>::New();
// Collect the coincident points into a nice list
vtkSmartPointer<vtkCoincidentPoints> coincidentPoints =
vtkSmartPointer<vtkCoincidentPoints>::New();
for (int i = 0; i < numPoints; ++i)
{
coincidentPoints->AddPoint(i, points->GetPoint(i));
}
// Note: We're not going to remove the non-coincident
// points until after computing the distance from all
// the points that have distinct coordinates.
coincidentPoints->InitTraversal();
std::vector<Coord> coincidentFoci;
vtkIdList* coincidentPointsList = coincidentPoints->GetNextCoincidentPointIds();
while (coincidentPointsList != nullptr)
{
// Just grabbing the first vertex of each coincident foci
vtkIdType vertexIndex = coincidentPointsList->GetId(0);
points->GetPoint(vertexIndex, currentPoint);
coincidentFoci.emplace_back(currentPoint);
// Get next coincident point list
coincidentPointsList = coincidentPoints->GetNextCoincidentPointIds();
}
// Compute the shortest intra-distance between coincident point foci
double shortestDistance = VTK_DOUBLE_MAX;
int numberOfFoci = static_cast<int>(coincidentFoci.size());
if (numberOfFoci > 1)
{
for (int i = 0; i < numberOfFoci; ++i)
{
for (int j = i + 1; j < numberOfFoci; ++j)
{
double distance = Coord::distance(coincidentFoci[i], coincidentFoci[j]);
shortestDistance = distance < shortestDistance ? distance : shortestDistance;
}
}
}
else
{
shortestDistance = 0.;
}
// Set the offset distance to be the shortest distance /4 * user setting (perturbFactor)
double offsetDistance = sqrt(shortestDistance) / 4.0 * perturbFactor;
// These store the offsets for a spiral with a certain number of points
vtkSmartPointer<vtkPoints> offsets = vtkSmartPointer<vtkPoints>::New();
// Removing the coincident points and re-initializing the iterator.
coincidentPoints->RemoveNonCoincidentPoints();
coincidentPoints->InitTraversal();
coincidentPointsList = coincidentPoints->GetNextCoincidentPointIds();
// Iterate over each coordinate that may have a set of coincident point ids.
while (coincidentPointsList != nullptr)
{
// Iterate over all coincident point ids and perturb them
numCoincidentPoints = coincidentPointsList->GetNumberOfIds();
vtkCoincidentPoints::SpiralPoints(numCoincidentPoints + 1, offsets);
for (int i = 0; i < numCoincidentPoints; ++i)
{
index = coincidentPointsList->GetId(i);
points->GetPoint(index, currentPoint);
offsets->GetPoint(i + 1, spiralOffsets);
points->SetPoint(index, currentPoint[0] + spiralOffsets[0] * offsetDistance,
currentPoint[1] + spiralOffsets[1] * offsetDistance, currentPoint[2]);
}
coincidentPointsList = coincidentPoints->GetNextCoincidentPointIds();
}
}
//----------------------------------------------------------------------------
int vtkPerturbCoincidentVertices::RequestData(vtkInformation* vtkNotUsed(request),
vtkInformationVector** inputVector, vtkInformationVector* outputVector)
{
vtkGraph* input = vtkGraph::GetData(inputVector[0]);
vtkGraph* output = vtkGraph::GetData(outputVector);
this->SimpleSpiralPerturbation(input, output, 1.0);
return 1;
}
//----------------------------------------------------------------------------
void vtkPerturbCoincidentVertices::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
os << indent << "PerturbFactor: " << this->PerturbFactor << "\n";
}