VMTK
Loading...
Searching...
No Matches
vtkvmtkCenterlineBranchGeometry.cxx
Go to the documentation of this file.
1/*=========================================================================
2
3Program: VMTK
4Module: $RCSfile: vtkvmtkCenterlineBranchGeometry.cxx,v $
5Language: C++
6Date: $Date: 2006/07/17 09:52:56 $
7Version: $Revision: 1.12 $
8
9 Copyright (c) Luca Antiga, David Steinman. All rights reserved.
10 See LICENSE file for details.
11
12 Portions of this code are covered under the VTK copyright.
13 See VTKCopyright.txt or http://www.kitware.com/VTKCopyright.htm
14 for details.
15
16 This software is distributed WITHOUT ANY WARRANTY; without even
17 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
18 PURPOSE. See the above copyright notices for more information.
19
20=========================================================================*/
21
26#include "vtkPolyData.h"
27#include "vtkPointData.h"
28#include "vtkCellData.h"
29#include "vtkCellArray.h"
30#include "vtkDoubleArray.h"
31#include "vtkMath.h"
32#include "vtkvmtkMath.h"
33#include "vtkInformation.h"
34#include "vtkInformationVector.h"
35#include "vtkObjectFactory.h"
36
38
39// #define SPIRAL_TEST
40
41
43
45{
46 this->RadiusArrayName = NULL;
47 this->GroupIdsArrayName = NULL;
48 this->BlankingArrayName = NULL;
49
50 this->LengthArrayName = NULL;
51 this->CurvatureArrayName = NULL;
52 this->TorsionArrayName = NULL;
53 this->TortuosityArrayName = NULL;
54
55 this->MinSubsamplingSpacing = 0.1;
56 this->SmoothingFactor = 0.01;
58
59 this->LineSubsampling = 1;
60 this->SphereSubsampling = 1;
61 this->LineSmoothing = 0;
62}
63
65{
66 if (this->RadiusArrayName)
67 {
68 delete[] this->RadiusArrayName;
69 this->RadiusArrayName = NULL;
70 }
71
72 if (this->GroupIdsArrayName)
73 {
74 delete[] this->GroupIdsArrayName;
75 this->GroupIdsArrayName = NULL;
76 }
77
78 if (this->BlankingArrayName)
79 {
80 delete[] this->BlankingArrayName;
81 this->BlankingArrayName = NULL;
82 }
83
84 if (this->LengthArrayName)
85 {
86 delete[] this->LengthArrayName;
87 this->LengthArrayName = NULL;
88 }
89
90 if (this->CurvatureArrayName)
91 {
92 delete[] this->CurvatureArrayName;
93 this->CurvatureArrayName = NULL;
94 }
95
96 if (this->TorsionArrayName)
97 {
98 delete[] this->TorsionArrayName;
99 this->TorsionArrayName = NULL;
100 }
101
102 if (this->TortuosityArrayName)
103 {
104 delete[] this->TortuosityArrayName;
105 this->TortuosityArrayName = NULL;
106 }
107}
108
110 vtkInformation *vtkNotUsed(request),
111 vtkInformationVector **inputVector,
112 vtkInformationVector *outputVector)
113{
114 vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
115 vtkInformation *outInfo = outputVector->GetInformationObject(0);
116
117 vtkPolyData *input = vtkPolyData::SafeDownCast(
118 inInfo->Get(vtkDataObject::DATA_OBJECT()));
119 vtkPolyData *output = vtkPolyData::SafeDownCast(
120 outInfo->Get(vtkDataObject::DATA_OBJECT()));
121
122 if (!this->RadiusArrayName)
123 {
124 vtkErrorMacro(<<"RadiusArrayName not specified");
125 return 1;
126 }
127
128 vtkDataArray* radiusArray = input->GetPointData()->GetArray(this->RadiusArrayName);
129
130 if (!radiusArray)
131 {
132 vtkErrorMacro(<<"RadiusArray with name specified does not exist");
133 return 1;
134 }
135
136 if (!this->GroupIdsArrayName)
137 {
138 vtkErrorMacro(<<"GroupIdsArrayName not specified");
139 return 1;
140 }
141
142 vtkDataArray* groupIdsArray = input->GetCellData()->GetArray(this->GroupIdsArrayName);
143
144 if (!groupIdsArray)
145 {
146 vtkErrorMacro(<<"GroupIdsArray with name specified does not exist");
147 return 1;
148 }
149
150 if (!this->BlankingArrayName)
151 {
152 vtkErrorMacro(<<"BlankingArrayName not specified");
153 return 1;
154 }
155
156 vtkDataArray* blankingArray = input->GetCellData()->GetArray(this->BlankingArrayName);
157
158 if (!blankingArray)
159 {
160 vtkErrorMacro(<<"BlankingArray with name specified does not exist");
161 return 1;
162 }
163
164 if (!this->LengthArrayName)
165 {
166 vtkErrorMacro(<<"LengthArrayName not specified");
167 return 1;
168 }
169
170 if (!this->CurvatureArrayName)
171 {
172 vtkErrorMacro(<<"CurvatureArrayName not specified");
173 return 1;
174 }
175
176 if (!this->TorsionArrayName)
177 {
178 vtkErrorMacro(<<"TorsionArrayName not specified");
179 return 1;
180 }
181
182 if (!this->TortuosityArrayName)
183 {
184 vtkErrorMacro(<<"TortuosityArrayName not specified");
185 return 1;
186 }
187
188 vtkDoubleArray* lengthArray = vtkDoubleArray::New();
189 lengthArray->SetName(this->LengthArrayName);
190
191 vtkDoubleArray* curvatureArray = vtkDoubleArray::New();
192 curvatureArray->SetName(this->CurvatureArrayName);
193
194 vtkDoubleArray* torsionArray = vtkDoubleArray::New();
195 torsionArray->SetName(this->TorsionArrayName);
196
197 vtkDoubleArray* tortuosityArray = vtkDoubleArray::New();
198 tortuosityArray->SetName(this->TortuosityArrayName);
199
200 vtkDoubleArray* branchGroupIdsArray = vtkDoubleArray::New();
201 branchGroupIdsArray->SetName(this->GroupIdsArrayName);
202
203 output->GetPointData()->AddArray(lengthArray);
204 output->GetPointData()->AddArray(curvatureArray);
205 output->GetPointData()->AddArray(torsionArray);
206 output->GetPointData()->AddArray(tortuosityArray);
207 output->GetPointData()->AddArray(branchGroupIdsArray);
208
209 vtkPoints* outputPoints = vtkPoints::New();
210 vtkCellArray* outputVerts = vtkCellArray::New();
211
212 output->SetPoints(outputPoints);
213 output->SetVerts(outputVerts);
214
215 vtkIdList* nonBlankedGroupIds = vtkIdList::New();
217 for (int i=0; i<nonBlankedGroupIds->GetNumberOfIds(); i++)
218 {
219 vtkIdType groupId = nonBlankedGroupIds->GetId(i);
220
221 double groupLength = this->ComputeGroupLength(input,groupId);
222 double groupCurvature = this->ComputeGroupCurvature(input,groupId);
223 double groupTorsion = this->ComputeGroupTorsion(input,groupId);
224 double groupTortuosity = this->ComputeGroupTortuosity(input,groupId,groupLength);
225
226 branchGroupIdsArray->InsertNextTuple1(groupId);
227 lengthArray->InsertNextTuple1(groupLength);
228 curvatureArray->InsertNextTuple1(groupCurvature);
229 torsionArray->InsertNextTuple1(groupTorsion);
230 tortuosityArray->InsertNextTuple1(groupTortuosity);
231
232 double zeroPoint[3];
233 zeroPoint[0] = zeroPoint[1] = zeroPoint[2] = 0.0;
234 vtkIdType pointId = outputPoints->InsertNextPoint(zeroPoint);
235 outputVerts->InsertNextCell(1);
236 outputVerts->InsertCellPoint(pointId);
237 }
238 nonBlankedGroupIds->Delete();
239
240 outputPoints->Delete();
241 outputVerts->Delete();
242
243 lengthArray->Delete();
244 curvatureArray->Delete();
245 torsionArray->Delete();
246 tortuosityArray->Delete();
247 branchGroupIdsArray->Delete();
248
249 return 1;
250}
251
252double vtkvmtkCenterlineBranchGeometry::ComputeGroupLength(vtkPolyData* input, int branchGroupId)
253{
254 double groupLength = 0.0;
255
256 vtkDataArray* groupIdsArray = input->GetCellData()->GetArray(this->GroupIdsArrayName);
257
258 int numberOfCells = input->GetNumberOfCells();
259
260 double lengthWeightSum = 0.0;
261
262 for (int i=0; i<numberOfCells; i++)
263 {
264 int groupId = static_cast<int>(groupIdsArray->GetComponent(i,0));
265
266 if (groupId != branchGroupId)
267 {
268 continue;
269 }
270
271 int numberOfCellPoints = input->GetCell(i)->GetNumberOfPoints();
272
273 double length = 0.0;
274
275 double point0[3], point1[3];
276 for (int j=0; j<numberOfCellPoints-1; j++)
277 {
278 input->GetCell(i)->GetPoints()->GetPoint(j,point0);
279 input->GetCell(i)->GetPoints()->GetPoint(j+1,point1);
280
281 length += sqrt(vtkMath::Distance2BetweenPoints(point0,point1));
282 }
283
284 groupLength += length;
285 lengthWeightSum += 1.0;
286 }
287
288 groupLength /= lengthWeightSum;
289
290 return groupLength;
291}
292
293double vtkvmtkCenterlineBranchGeometry::ComputeGroupCurvature(vtkPolyData* input, int branchGroupId)
294{
295 double groupCurvature = 0.0;
296
297 vtkDataArray* groupIdsArray = input->GetCellData()->GetArray(this->GroupIdsArrayName);
298
299 int numberOfCells = input->GetNumberOfCells();
300
301 double groupCurvatureWeightSum = 0.0;
302
303 for (int i=0; i<numberOfCells; i++)
304 {
305 int groupId = static_cast<int>(groupIdsArray->GetComponent(i,0));
306
307 if (groupId != branchGroupId)
308 {
309 continue;
310 }
311
312 vtkPoints* linePoints = vtkPoints::New();
313#ifndef SPIRAL_TEST
314 linePoints->DeepCopy(input->GetCell(i)->GetPoints());
315#else
316 int numberOfSectors = 5;
317 for (int j=0; j<numberOfSectors; j++)
318 {
319 double point[3];
320 point[0] = cos(j*2.0*vtkMath::Pi()/numberOfSectors);
321 point[1] = sin(j*2.0*vtkMath::Pi()/numberOfSectors);
322 point[2] = j*(2.0*vtkMath::Pi()/numberOfSectors);
323 linePoints->InsertNextPoint(point);
324 }
325#endif
326
327 if (this->LineSmoothing)
328 {
329 vtkPoints* smoothLinePoints = vtkPoints::New();
331 linePoints->DeepCopy(smoothLinePoints);
332 smoothLinePoints->Delete();
333 }
334
335 if (this->LineSubsampling)
336 {
337 vtkPoints* subsampledLinePoints = vtkPoints::New();
338 if (this->SphereSubsampling)
339 {
340 this->SphereSubsampleLine(input,i,subsampledLinePoints);
341 }
342 else
343 {
344 this->SubsampleLine(linePoints,subsampledLinePoints,this->MinSubsamplingSpacing);
345 }
346 linePoints->DeepCopy(subsampledLinePoints);
347 subsampledLinePoints->Delete();
348 }
349
350 vtkDoubleArray* curvatureArray = vtkDoubleArray::New();
351 double curvature = vtkvmtkCenterlineGeometry::ComputeLineCurvature(linePoints,curvatureArray);
352
353 curvatureArray->Delete();
354 linePoints->Delete();
355
356 groupCurvature += curvature;
357 groupCurvatureWeightSum += 1.0;
358 }
359
360 groupCurvature /= groupCurvatureWeightSum;
361
362// cout<<"Group curvature:"<<groupCurvature<<endl;
363
364 return groupCurvature;
365}
366
367double vtkvmtkCenterlineBranchGeometry::ComputeGroupTorsion(vtkPolyData* input, int branchGroupId)
368{
369 double groupTorsion = 0.0;
370
371 vtkDataArray* groupIdsArray = input->GetCellData()->GetArray(this->GroupIdsArrayName);
372
373 int numberOfCells = input->GetNumberOfCells();
374
375 double groupTorsionWeightSum = 0.0;
376
377 for (int i=0; i<numberOfCells; i++)
378 {
379 int groupId = static_cast<int>(groupIdsArray->GetComponent(i,0));
380
381 if (groupId != branchGroupId)
382 {
383 continue;
384 }
385
386 vtkPoints* linePoints = vtkPoints::New();
387#ifndef SPIRAL_TEST
388 linePoints->DeepCopy(input->GetCell(i)->GetPoints());
389#else
390 int numberOfSectors = 5;
391 for (int j=0; j<numberOfSectors; j++)
392 {
393 double point[3];
394 point[0] = cos(j*2.0*vtkMath::Pi()/numberOfSectors);
395 point[1] = sin(j*2.0*vtkMath::Pi()/numberOfSectors);
396 point[2] = j*(2.0*vtkMath::Pi()/numberOfSectors);
397 linePoints->InsertNextPoint(point);
398 }
399#endif
400
401 if (this->LineSmoothing)
402 {
403 vtkPoints* smoothLinePoints = vtkPoints::New();
405 linePoints->DeepCopy(smoothLinePoints);
406 smoothLinePoints->Delete();
407 }
408
409 if (this->LineSubsampling)
410 {
411 vtkPoints* subsampledLinePoints = vtkPoints::New();
412 if (this->SphereSubsampling)
413 {
414 this->SphereSubsampleLine(input,i,subsampledLinePoints);
415 }
416 else
417 {
418 this->SubsampleLine(linePoints,subsampledLinePoints,this->MinSubsamplingSpacing);
419 }
420 linePoints->DeepCopy(subsampledLinePoints);
421 subsampledLinePoints->Delete();
422 }
423
424 vtkDoubleArray* torsionArray = vtkDoubleArray::New();
425 double torsion = vtkvmtkCenterlineGeometry::ComputeLineTorsion(linePoints,torsionArray);
426
427 torsionArray->Delete();
428
429 linePoints->Delete();
430
431 groupTorsion += torsion;
432 groupTorsionWeightSum += 1.0;
433 }
434
435 groupTorsion /= groupTorsionWeightSum;
436
437// cout<<"Group torsion:"<<groupTorsion<<endl;
438
439 return groupTorsion;
440}
441
442
443void vtkvmtkCenterlineBranchGeometry::SubsampleLine(vtkPoints* linePoints, vtkPoints* subsampledLinePoints, double minSpacing)
444{
445 int numberOfPoints = linePoints->GetNumberOfPoints();
446
447 double point0[3];
448 double point1[3];
449
450 double spacing = 0.0;
451 linePoints->GetPoint(0,point0);
452 subsampledLinePoints->InsertNextPoint(point0);
453 for (int j=1; j<numberOfPoints-1; j++)
454 {
455 linePoints->GetPoint(j,point1);
456
457 spacing += sqrt(vtkMath::Distance2BetweenPoints(point0,point1));
458
459 if (spacing < minSpacing)
460 {
461 continue;
462 }
463
464 subsampledLinePoints->InsertNextPoint(point1);
465 linePoints->GetPoint(j,point0);
466 spacing = 0.0;
467 }
468
469 linePoints->GetPoint(numberOfPoints-1,point1);
470
471 spacing = sqrt(vtkMath::Distance2BetweenPoints(point0,point1));
472
473 if (spacing<minSpacing)
474 {
475 int numberOfSubsampledPoints = subsampledLinePoints->GetNumberOfPoints();
476 subsampledLinePoints->InsertPoint(numberOfSubsampledPoints-1,point1);
477 }
478 else
479 {
480 subsampledLinePoints->InsertNextPoint(point1);
481 }
482}
483
484void vtkvmtkCenterlineBranchGeometry::SphereSubsampleLine(vtkPolyData* input, vtkIdType cellId, vtkPoints* subsampledLinePoints)
485{
486 vtkIdType subId = 0;
487 double pcoord = 0.0;
488
489 vtkIdType touchingSubId = 0;
490 double touchingPCoord = 0.0;
491
492 double point[3];
493 double point0[3];
494 double point1[3];
495
496 subsampledLinePoints->InsertNextPoint(input->GetCell(cellId)->GetPoints()->GetPoint(0));
497
498 while (touchingSubId != -1)
499 {
500 vtkvmtkCenterlineSphereDistance::FindTouchingSphereCenter(input, this->RadiusArrayName, cellId, subId, pcoord, touchingSubId, touchingPCoord, false);
501
502 if (touchingSubId == -1)
503 {
504 break;
505 }
506
507 input->GetCell(cellId)->GetPoints()->GetPoint(touchingSubId,point0);
508 input->GetCell(cellId)->GetPoints()->GetPoint(touchingSubId+1,point1);
509
510 point[0] = (1.0 - touchingPCoord) * point0[0] + touchingPCoord * point1[0];
511 point[1] = (1.0 - touchingPCoord) * point0[1] + touchingPCoord * point1[1];
512 point[2] = (1.0 - touchingPCoord) * point0[2] + touchingPCoord * point1[2];
513
514 subsampledLinePoints->InsertNextPoint(point);
515
516 subId = touchingSubId;
517 pcoord = touchingPCoord;
518 }
519
520 subsampledLinePoints->InsertNextPoint(input->GetCell(cellId)->GetPoints()->GetPoint(input->GetCell(cellId)->GetNumberOfPoints()-1));
521
522}
523
524double vtkvmtkCenterlineBranchGeometry::ComputeGroupTortuosity(vtkPolyData* input, int branchGroupId, double groupLength)
525{
526 double groupTortuosity = 0.0;
527
528 vtkDataArray* groupIdsArray = input->GetCellData()->GetArray(this->GroupIdsArrayName);
529 vtkDataArray* radiusArray = input->GetPointData()->GetArray(this->RadiusArrayName);
530
531 int numberOfCells = input->GetNumberOfCells();
532
533 double averageFirstPoint[3], averageLastPoint[3];
534 averageFirstPoint[0] = averageFirstPoint[1] = averageFirstPoint[2] = 0.0;
535 averageLastPoint[0] = averageLastPoint[1] = averageLastPoint[2] = 0.0;
536 double firstPointWeightSum = 0.0;
537 double lastPointWeightSum = 0.0;
538
539 for (int i=0; i<numberOfCells; i++)
540 {
541 int groupId = static_cast<int>(groupIdsArray->GetComponent(i,0));
542
543 if (groupId != branchGroupId)
544 {
545 continue;
546 }
547
548 int numberOfCellPoints = input->GetCell(i)->GetNumberOfPoints();
549
550 double firstPoint[3], lastPoint[3];
551
552 input->GetCell(i)->GetPoints()->GetPoint(0,firstPoint);
553 input->GetCell(i)->GetPoints()->GetPoint(numberOfCellPoints-1,lastPoint);
554
555 double firstPointRadius = radiusArray->GetComponent(input->GetCell(i)->GetPointId(0),0);
556 double lastPointRadius = radiusArray->GetComponent(input->GetCell(i)->GetPointId(numberOfCellPoints-1),0);
557
558 averageFirstPoint[0] += firstPointRadius * firstPointRadius * firstPoint[0];
559 averageFirstPoint[1] += firstPointRadius * firstPointRadius * firstPoint[1];
560 averageFirstPoint[2] += firstPointRadius * firstPointRadius * firstPoint[2];
561
562 firstPointWeightSum += firstPointRadius * firstPointRadius;
563
564 averageLastPoint[0] += lastPointRadius * lastPointRadius * lastPoint[0];
565 averageLastPoint[1] += lastPointRadius * lastPointRadius * lastPoint[1];
566 averageLastPoint[2] += lastPointRadius * lastPointRadius * lastPoint[2];
567
568 lastPointWeightSum += lastPointRadius * lastPointRadius;
569 }
570
571 averageFirstPoint[0] /= firstPointWeightSum;
572 averageFirstPoint[1] /= firstPointWeightSum;
573 averageFirstPoint[2] /= firstPointWeightSum;
574
575 averageLastPoint[0] /= lastPointWeightSum;
576 averageLastPoint[1] /= lastPointWeightSum;
577 averageLastPoint[2] /= lastPointWeightSum;
578
579 groupTortuosity = groupLength / sqrt(vtkMath::Distance2BetweenPoints(averageFirstPoint,averageLastPoint)) - 1.0;
580
581 return groupTortuosity;
582}
583
584void vtkvmtkCenterlineBranchGeometry::PrintSelf(std::ostream& os, vtkIndent indent)
585{
586 this->Superclass::PrintSelf(os,indent);
587}
588
Computes the length, curvature, torsion, and tortuosity for each branch within a split centerline.
void SphereSubsampleLine(vtkPolyData *input, vtkIdType cellId, vtkPoints *subsampledLinePoints)
virtual int RequestData(vtkInformation *, vtkInformationVector **, vtkInformationVector *) override
double ComputeGroupLength(vtkPolyData *input, int branchGroupId)
static void SubsampleLine(vtkPoints *linePoints, vtkPoints *subsampledLinePoints, double minSpacing=0.01)
double ComputeGroupTortuosity(vtkPolyData *input, int branchGroupId, double groupLength)
double ComputeGroupCurvature(vtkPolyData *input, int branchGroupId)
double ComputeGroupTorsion(vtkPolyData *input, int branchGroupId)
void PrintSelf(std::ostream &os, vtkIndent indent) override
static double ComputeLineTorsion(vtkPoints *linePoints, vtkDoubleArray *torsionArray)
static double ComputeLineCurvature(vtkPoints *linePoints, vtkDoubleArray *curvatureArray)
static void SmoothLine(vtkPoints *linePoints, vtkPoints *smoothLinePoints, int numberOfIterations=10, double relaxation=0.1)
static void FindTouchingSphereCenter(vtkPolyData *centerlines, const char *radiusArrayName, vtkIdType cellId, vtkIdType subId, double pcoord, vtkIdType &touchingSubId, double &touchingPCoord, bool forward=true)
static void GetNonBlankedGroupsIdList(vtkPolyData *centerlines, const char *groupIdsArrayName, const char *blankingArrayName, vtkIdList *groupIds)
vtkStandardNewMacro(vtkvmtkCenterlineBranchGeometry)