forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vtkSplineGraphEdges.cxx
445 lines (402 loc) · 11.1 KB
/
vtkSplineGraphEdges.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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
/*=========================================================================
Program: Visualization Toolkit
Module: vtkSplineGraphEdges.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 "vtkSplineGraphEdges.h"
#include "vtkCardinalSpline.h"
#include "vtkCommand.h"
#include "vtkGraph.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkMath.h"
#include "vtkObjectFactory.h"
vtkStandardNewMacro(vtkSplineGraphEdges);
vtkCxxSetObjectMacro(vtkSplineGraphEdges, Spline, vtkSpline);
namespace {
// N-function defined at:
// http://mathworld.wolfram.com/B-Spline.html
// optimized for j = 3.
double CubicSpline(vtkIdType i, double* k, double t)
{
if (t >= k[i] && t < k[i+1])
{
double denom = (k[i+3]-k[i])*(k[i+2]-k[i])*(k[i+1]-k[i]);
if (denom == 0.0) return 0.0;
double temp = t - k[i];
return temp*temp*temp/denom;
}
if (t >= k[i+1] && t < k[i+2])
{
double denom1 = (k[i+3]-k[i])*(k[i+2]-k[i])*(k[i+2]-k[i+1]);
double term1;
if (denom1 == 0.0)
{
term1 = 0.0;
}
else
{
term1 = (t-k[i])*(t-k[i])*(k[i+2]-t)/denom1;
}
double denom2 = (k[i+3]-k[i])*(k[i+3]-k[i+1])*(k[i+2]-k[i+1]);
double term2;
if (denom2 == 0.0)
{
term2 = 0.0;
}
else
{
term2 = (t-k[i])*(k[i+3]-t)*(t-k[i+1])/denom2;
}
double denom3 = (k[i+4]-k[i+1])*(k[i+3]-k[i+1])*(k[i+2]-k[i+1]);
double term3;
if (denom3 == 0.0)
{
term3 = 0.0;
}
else
{
term3 = (k[i+4]-t)*(t-k[i+1])*(t-k[i+1])/denom3;
}
return term1 + term2 + term3;
}
if (t >= k[i+2] && t < k[i+3])
{
double denom1 = (k[i+3]-k[i])*(k[i+3]-k[i+1])*(k[i+3]-k[i+2]);
double term1;
if (denom1 == 0.0)
{
term1 = 0.0;
}
else
{
term1 = (t-k[i])*(k[i+3]-t)*(k[i+3]-t)/denom1;
}
double denom2 = (k[i+4]-k[i+1])*(k[i+3]-k[i+1])*(k[i+3]-k[i+2]);
double term2;
if (denom2 == 0.0)
{
term2 = 0.0;
}
else
{
term2 = (k[i+4]-t)*(t-k[i+1])*(k[i+3]-t)/denom2;
}
double denom3 = (k[i+4]-k[i+1])*(k[i+4]-k[i+2])*(k[i+3]-k[i+2]);
double term3;
if (denom3 == 0.0)
{
term3 = 0.0;
}
else
{
term3 = (k[i+4]-t)*(k[i+4]-t)*(t-k[i+2])/denom3;
}
return term1 + term2 + term3;
}
if (t >= k[i+3] && t < k[i+4])
{
double denom = (k[i+4]-k[i+1])*(k[i+4]-k[i+2])*(k[i+4]-k[i+3]);
if (denom == 0.0) return 0.0;
double temp = k[i+4] - t;
return temp*temp*temp/denom;
}
return 0.0;
}
// Slow, recursive version of N-function defined:
// http://mathworld.wolfram.com/B-Spline.html
#if 0
double N(vtkIdType i, vtkIdType j, double* k, double t)
{
if (j < 0)
{
return 0.0;
}
if (j == 0)
{
if (t >= k[i] && t < k[i+1] && k[i] < k[i+1])
{
return 1.0;
}
return 0.0;
}
double term1 = 0.0;
if (k[i] < k[i+j])
{
term1 = (t-k[i])/(k[i+j]-k[i])*N(i, j-1, k, t);
}
double term2 = 0.0;
if (k[i+1] < k[i+j+1])
{
term2 = (k[i+j+1]-t)/(k[i+j+1]-k[i+1])*N(i+1, j-1, k, t);
}
return term1 + term2;
}
#endif
#if 0
double BCubic(vtkIdType i, double* k, double t)
{
if (t < k[i-2] || t >= k[i+2])
{
return 0.0;
}
double temp;
if (t >= k[i-2] && t < k[i-1])
{
temp = 2.0 + t;
return temp*temp*temp/6.0;
}
if (t >= k[i-1] && t < k[i])
{
return (4 - 6*t*t - 3*t*t*t)/6.0;
}
if (t >= k[i] && t < k[i+1])
{
return (4 - 6*t*t + 3*t*t*t)/6.0;
}
temp = 2.0 - t;
return temp*temp*temp;
}
#endif
}
vtkSplineGraphEdges::vtkSplineGraphEdges()
{
this->Spline = vtkCardinalSpline::New();
this->XSpline = 0;
this->YSpline = 0;
this->ZSpline = 0;
this->NumberOfSubdivisions = 20;
this->SplineType = CUSTOM;
}
vtkSplineGraphEdges::~vtkSplineGraphEdges()
{
if (this->Spline)
{
this->Spline->Delete();
this->Spline = 0;
}
}
unsigned long vtkSplineGraphEdges::GetMTime()
{
unsigned long mtime = this->Superclass::GetMTime();
if (this->Spline && this->Spline->GetMTime() > mtime)
{
mtime = this->Spline->GetMTime();
}
return mtime;
}
int vtkSplineGraphEdges::RequestData(
vtkInformation *vtkNotUsed(request),
vtkInformationVector **inputVector,
vtkInformationVector *outputVector)
{
if (!this->Spline)
{
vtkErrorMacro("Must have a valid spline.");
return 0;
}
// get the info objects
vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
vtkInformation *outInfo = outputVector->GetInformationObject(0);
// get the input and output
vtkGraph *input = vtkGraph::SafeDownCast(
inInfo->Get(vtkDataObject::DATA_OBJECT()));
vtkGraph *output = vtkGraph::SafeDownCast(
outInfo->Get(vtkDataObject::DATA_OBJECT()));
output->ShallowCopy(input);
output->DeepCopyEdgePoints(input);
if (this->SplineType == CUSTOM)
{
this->XSpline.TakeReference(this->Spline->NewInstance());
this->XSpline->DeepCopy(this->Spline);
this->YSpline.TakeReference(this->Spline->NewInstance());
this->YSpline->DeepCopy(this->Spline);
this->ZSpline.TakeReference(this->Spline->NewInstance());
this->ZSpline->DeepCopy(this->Spline);
}
for (vtkIdType i = 0; i < output->GetNumberOfEdges(); ++i)
{
if (this->SplineType == BSPLINE)
{
this->GenerateBSpline(output, i);
}
else
{
this->GeneratePoints(output, i);
}
if (i % 1000 == 0)
{
double progress = static_cast<double>(i)/
static_cast<double>(output->GetNumberOfEdges());
this->InvokeEvent(vtkCommand::ProgressEvent, &progress);
}
}
return 1;
}
void vtkSplineGraphEdges::GeneratePoints(vtkGraph* g, vtkIdType e)
{
// Initialize the splines
this->XSpline->RemoveAllPoints();
this->YSpline->RemoveAllPoints();
this->ZSpline->RemoveAllPoints();
vtkIdType numInternalPoints;
double* internalPoints;
g->GetEdgePoints(e, numInternalPoints, internalPoints);
vtkIdType numPoints = numInternalPoints + 2;
double* points = new double[3*static_cast<size_t>(numPoints)];
memcpy(points + 3, internalPoints, sizeof(double)*3
*static_cast<size_t>(numInternalPoints));
g->GetPoint(g->GetSourceVertex(e), points);
g->GetPoint(g->GetTargetVertex(e), points + 3*(numInternalPoints+1));
double* xPrev;
double* x;
double length = 0.0;
double* xEnd = points + 3*numPoints;
for (xPrev = points, x = points+3; x != xEnd; xPrev += 3, x += 3)
{
double len = sqrt(vtkMath::Distance2BetweenPoints(x,xPrev));
length += len;
}
if (length <= 0.0)
{
return;
}
// Now we insert points into the splines with the parametric coordinate
// based on length. We keep track of the parametric coordinates
// of the points for later point interpolation.
this->XSpline->AddPoint(0.0, points[0]);
this->YSpline->AddPoint(0.0, points[1]);
this->ZSpline->AddPoint(0.0, points[2]);
double len = 0.0;
for (xPrev = points, x = points+3; x != xEnd; xPrev += 3, x += 3)
{
double dist = sqrt(vtkMath::Distance2BetweenPoints(x, xPrev));
if (dist == 0)
{
continue;
}
len += dist;
double t = len/length;
this->XSpline->AddPoint(t, x[0]);
this->YSpline->AddPoint(t, x[1]);
this->ZSpline->AddPoint(t, x[2]);
}
// Now compute the new points
vtkIdType numNewPoints = this->NumberOfSubdivisions - 1;
double* newPoints = new double[3*numNewPoints];
vtkIdType i;
for (i = 0, x = newPoints; i < numNewPoints; i++, x += 3)
{
double t = static_cast<double>(i+1) /
static_cast<double>(this->NumberOfSubdivisions);
x[0] = this->XSpline->Evaluate(t);
x[1] = this->YSpline->Evaluate(t);
x[2] = this->ZSpline->Evaluate(t);
}
g->SetEdgePoints(e, numNewPoints, newPoints);
delete [] points;
delete [] newPoints;
}
void vtkSplineGraphEdges::GenerateBSpline(vtkGraph* g, vtkIdType e)
{
vtkIdType numInternalPoints;
double* internalPoints;
g->GetEdgePoints(e, numInternalPoints, internalPoints);
// Duplicate internal point if there is just one, so there are at least
// four points, required for B-spline.
bool repeat = false;
if (numInternalPoints == 1)
{
repeat = true;
numInternalPoints = 2;
}
vtkIdType numPoints = numInternalPoints + 2;
double* points = new double[3*numPoints];
if (repeat)
{
memcpy(points + 3, internalPoints, sizeof(double)*3);
memcpy(points + 6, internalPoints, sizeof(double)*3);
}
else
{
memcpy(points + 3, internalPoints, sizeof(double)*3
*static_cast<size_t>(numInternalPoints));
}
g->GetPoint(g->GetSourceVertex(e), points);
g->GetPoint(g->GetTargetVertex(e), points + 3*(numInternalPoints+1));
if (numPoints <= 3)
{
return;
}
// Compute the knot vector
vtkIdType numKnots = numPoints + 4;
double* knots = new double[numKnots];
knots[0] = 0.0;
knots[1] = 0.0;
knots[2] = 0.0;
knots[3] = 0.0;
knots[numKnots-4] = 1.0;
knots[numKnots-3] = 1.0;
knots[numKnots-2] = 1.0;
knots[numKnots-1] = 1.0;
vtkIdType i;
for (i = 4; i < numKnots-4; ++i)
{
knots[i] = static_cast<double>(i-3)/static_cast<double>(numKnots-7);
}
// Special case of 3 points, make symmetric
if (numPoints == 3)
{
knots[3] = 0.5;
}
// Now compute the new points
vtkIdType numNewPoints = this->NumberOfSubdivisions - 1;
double* newPoints = new double[3*numNewPoints];
double* xNew;
for (i = 0, xNew = newPoints; i < numNewPoints; i++, xNew += 3)
{
xNew[0] = 0.0;
xNew[1] = 0.0;
xNew[2] = 0.0;
double t = static_cast<double>(i+1) /
static_cast<double>(this->NumberOfSubdivisions);
double* x = points;
//double bsum = 0.0;
for (vtkIdType j = 0; j < numPoints; ++j, x += 3)
{
//double b = BCubic(j+2, knots, t);
//double b = N(j, 3, knots, t);
double b = CubicSpline(j, knots, t);
//bsum += b;
xNew[0] += x[0]*b;
xNew[1] += x[1]*b;
xNew[2] += x[2]*b;
}
//cerr << "bsum: " << bsum << endl;
}
g->SetEdgePoints(e, numNewPoints, newPoints);
delete [] points;
delete [] knots;
delete [] newPoints;
}
void vtkSplineGraphEdges::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
os << indent << "SplineType: " << this->SplineType << endl;
os << indent << "NumberOfSubdivisions: " << this->NumberOfSubdivisions << endl;
os << indent << "Spline: " << (this->Spline ? "" : "(none)") << endl;
if (this->Spline)
{
this->Spline->PrintSelf(os, indent.GetNextIndent());
}
}