VMTK
Loading...
Searching...
No Matches
vtkvmtkCenterlineBifurcationReferenceSystems.cxx
Go to the documentation of this file.
1/*=========================================================================
2
3Program: VMTK
4Module: $RCSfile: vtkvmtkCenterlineBifurcationReferenceSystems.cxx,v $
5Language: C++
6Date: $Date: 2006/04/06 16:46:43 $
7Version: $Revision: 1.8 $
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
23#include "vtkPolyData.h"
24#include "vtkDoubleArray.h"
25#include "vtkIntArray.h"
26#include "vtkPointData.h"
27#include "vtkCellData.h"
28#include "vtkPolyLine.h"
29#include "vtkPoints.h"
30#include "vtkTriangle.h"
31#include "vtkvmtkConstants.h"
32#include "vtkvmtkMath.h"
33#include "vtkMath.h"
34#include "vtkCellArray.h"
35#include "vtkInformation.h"
36#include "vtkInformationVector.h"
37#include "vtkObjectFactory.h"
38
40
41
43
53
55{
56 if (this->RadiusArrayName)
57 {
58 delete[] this->RadiusArrayName;
59 this->RadiusArrayName = NULL;
60 }
61
62 if (this->GroupIdsArrayName)
63 {
64 delete[] this->GroupIdsArrayName;
65 this->GroupIdsArrayName = NULL;
66 }
67
68 if (this->BlankingArrayName)
69 {
70 delete[] this->BlankingArrayName;
71 this->BlankingArrayName = NULL;
72 }
73
74 if (this->NormalArrayName)
75 {
76 delete[] this->NormalArrayName;
77 this->NormalArrayName = NULL;
78 }
79
80 if (this->UpNormalArrayName)
81 {
82 delete[] this->UpNormalArrayName;
83 this->UpNormalArrayName = NULL;
84 }
85}
86
88 vtkInformation *vtkNotUsed(request),
89 vtkInformationVector **inputVector,
90 vtkInformationVector *outputVector)
91{
92 vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
93 vtkInformation *outInfo = outputVector->GetInformationObject(0);
94
95 vtkPolyData *input = vtkPolyData::SafeDownCast(
96 inInfo->Get(vtkDataObject::DATA_OBJECT()));
97 vtkPolyData *output = vtkPolyData::SafeDownCast(
98 outInfo->Get(vtkDataObject::DATA_OBJECT()));
99
100 vtkDataArray* radiusArray;
101 vtkDataArray* groupIdsArray;
102 vtkDataArray* blankingArray;
103
104 if (!this->RadiusArrayName)
105 {
106 vtkErrorMacro(<<"RadiusArrayName not specified");
107 return 1;
108 }
109
110 radiusArray = input->GetPointData()->GetArray(this->RadiusArrayName);
111
112 if (!radiusArray)
113 {
114 vtkErrorMacro(<<"RadiusArray with name specified does not exist");
115 return 1;
116 }
117
118 if (!this->GroupIdsArrayName)
119 {
120 vtkErrorMacro(<<"GroupIdsArrayName not specified");
121 return 1;
122 }
123
124 groupIdsArray = input->GetCellData()->GetArray(this->GroupIdsArrayName);
125
126 if (!input->GetCellData()->GetArray(this->GroupIdsArrayName))
127 {
128 vtkErrorMacro(<<"GroupIdsArray with name specified does not exist");
129 return 1;
130 }
131
132 if (!this->BlankingArrayName)
133 {
134 vtkErrorMacro(<<"BlankingArrayName not specified");
135 return 1;
136 }
137
138 blankingArray = input->GetCellData()->GetArray(this->BlankingArrayName);
139
140 if (!input->GetCellData()->GetArray(this->BlankingArrayName))
141 {
142 vtkErrorMacro(<<"BlankingArray with name specified does not exist");
143 return 1;
144 }
145
146 if (!this->NormalArrayName)
147 {
148 vtkErrorMacro(<<"NormalArrayName not specified");
149 return 1;
150 }
151
152 if (!this->UpNormalArrayName)
153 {
154 vtkErrorMacro(<<"UpNormalArrayName not specified");
155 return 1;
156 }
157
158 vtkPoints* outputPoints = vtkPoints::New();
159
160 vtkDoubleArray* normalArray = vtkDoubleArray::New();
161 normalArray->SetName(this->NormalArrayName);
162 normalArray->SetNumberOfComponents(3);
163
164 vtkDoubleArray* upNormalArray = vtkDoubleArray::New();
165 upNormalArray->SetName(this->UpNormalArrayName);
166 upNormalArray->SetNumberOfComponents(3);
167
168 vtkIntArray* referenceGroupIdsArray = vtkIntArray::New();
169 referenceGroupIdsArray->SetName(this->GroupIdsArrayName);
170 vtkIdList* blankedGroupIds = vtkIdList::New();
172 int i;
173 for (i=0; i<blankedGroupIds->GetNumberOfIds(); i++)
174 {
175 vtkIdType groupId = blankedGroupIds->GetId(i);
176 this->ComputeGroupReferenceSystem(input,groupId,outputPoints,normalArray,upNormalArray,referenceGroupIdsArray);
177 }
178 blankedGroupIds->Delete();
179
180 vtkCellArray* outputVertices = vtkCellArray::New();
181
182 int numberOfOutputPoints = outputPoints->GetNumberOfPoints();
183 for (i=0; i<numberOfOutputPoints; i++)
184 {
185 outputVertices->InsertNextCell(1);
186 outputVertices->InsertCellPoint(i);
187 }
188
189 output->SetPoints(outputPoints);
190 output->SetVerts(outputVertices);
191 output->GetPointData()->AddArray(normalArray);
192 output->GetPointData()->AddArray(upNormalArray);
193 output->GetPointData()->AddArray(referenceGroupIdsArray);
194
195 outputPoints->Delete();
196 outputVertices->Delete();
197 normalArray->Delete();
198 upNormalArray->Delete();
199 referenceGroupIdsArray->Delete();
200
201 return 1;
202}
203
204void vtkvmtkCenterlineBifurcationReferenceSystems::ComputeGroupReferenceSystem(vtkPolyData* input, int referenceGroupId, vtkPoints* outputPoints, vtkDoubleArray* normalArray, vtkDoubleArray* upNormalArray, vtkIntArray* referenceGroupIdsArray)
205{
206 vtkDataArray* radiusArray;
207 vtkDataArray* groupIdsArray;
208 vtkDataArray* blankingArray;
209
210 radiusArray = input->GetPointData()->GetArray(this->RadiusArrayName);
211 groupIdsArray = input->GetCellData()->GetArray(this->GroupIdsArrayName);
212 blankingArray = input->GetCellData()->GetArray(this->BlankingArrayName);
213
214 vtkIdType point0Id, point1Id;
215
216 double point0[3];
217 double point1[3];
218
219 double radius0;
220 double radius1;
221
222 vtkPoints* bifurcationPoints = vtkPoints::New();
223 vtkDoubleArray* bifurcationRadii = vtkDoubleArray::New();
224
225 vtkIdList* groupCellIds = vtkIdList::New();
226// vtkvmtkCenterlineUtilities::GetGroupCellIds(input,this->GroupIdsArrayName,referenceGroupId,groupCellIds);
227 vtkvmtkCenterlineUtilities::GetGroupUniqueCellIds(input,this->GroupIdsArrayName,referenceGroupId,groupCellIds);
228 int i;
229 for (i=0; i<groupCellIds->GetNumberOfIds(); i++)
230 {
231 vtkIdType cellId = groupCellIds->GetId(i);
232 vtkCell* polyLine = input->GetCell(cellId);
233
234 if (polyLine->GetCellType() != VTK_LINE && polyLine->GetCellType() != VTK_POLY_LINE)
235 {
236 continue;
237 }
238
239 point0Id = polyLine->GetPointId(0);
240 input->GetPoint(point0Id,point0);
241 point1Id = polyLine->GetPointId(polyLine->GetNumberOfPoints()-1);
242 input->GetPoint(point1Id,point1);
243
244 bool skip = false;
245
246 for (int j=0; j<bifurcationPoints->GetNumberOfPoints(); j+=2)
247 {
248 if (vtkMath::Distance2BetweenPoints(point0,bifurcationPoints->GetPoint(j)) < VTK_VMTK_DOUBLE_TOL &&
249 vtkMath::Distance2BetweenPoints(point1,bifurcationPoints->GetPoint(j+1)) < VTK_VMTK_DOUBLE_TOL)
250 {
251 skip = true;
252 break;
253 }
254 }
255
256 if (skip)
257 {
258 continue;
259 }
260
261 radius0 = radiusArray->GetComponent(point0Id,0);
262 bifurcationPoints->InsertNextPoint(point0);
263 bifurcationRadii->InsertNextTuple1(radius0);
264
265 radius1 = radiusArray->GetComponent(point1Id,0);
266 bifurcationPoints->InsertNextPoint(point1);
267 bifurcationRadii->InsertNextTuple1(radius1);
268 }
269 groupCellIds->Delete();
270
271 int numberOfBifurcationPoints = bifurcationPoints->GetNumberOfPoints();
272
273 double bifurcationOrigin[3];
274 double bifurcationNormal[3];
275 double bifurcationUpNormal[3];
276
277 double point[3];
278 double radius;
279 double weight;
280 double weightSum;
281
282 bifurcationOrigin[0] = bifurcationOrigin[1] = bifurcationOrigin[2] = 0.0;
283 weightSum = 0.0;
284
285 for (i=0; i<numberOfBifurcationPoints; i++)
286 {
287 bifurcationPoints->GetPoint(i,point);
288 radius = bifurcationRadii->GetComponent(i,0);
289
290 weight = radius*radius;
291
292 bifurcationOrigin[0] += weight * point[0];
293 bifurcationOrigin[1] += weight * point[1];
294 bifurcationOrigin[2] += weight * point[2];
295
296 weightSum += weight;
297 }
298
299 bifurcationOrigin[0] /= weightSum;
300 bifurcationOrigin[1] /= weightSum;
301 bifurcationOrigin[2] /= weightSum;
302
303 vtkDoubleArray* bifurcationNormals = vtkDoubleArray::New();
304 bifurcationNormals->SetNumberOfComponents(3);
305
306 vtkPoints* vertexPoints = vtkPoints::New();
307
308 for (i=0; i<numberOfBifurcationPoints; i+=2)
309 {
310 for (int j=i+2; j<numberOfBifurcationPoints; j+=2)
311 {
312 if (j==i)
313 {
314 continue;
315 }
316
317 vertexPoints->Initialize();
318
319 bifurcationPoints->GetPoint(i,point0);
320 bifurcationPoints->GetPoint(j,point1);
321 if (vtkMath::Distance2BetweenPoints(point0,point1)<VTK_VMTK_DOUBLE_TOL)
322 {
323 vertexPoints->InsertPoint(0,bifurcationPoints->GetPoint(i));
324 vertexPoints->InsertPoint(1,bifurcationPoints->GetPoint(i+1));
325 vertexPoints->InsertPoint(2,bifurcationPoints->GetPoint(j+1));
326 }
327 else
328 {
329 vertexPoints->InsertPoint(0,bifurcationPoints->GetPoint(i));
330 vertexPoints->InsertPoint(1,bifurcationPoints->GetPoint(i+1));
331 vertexPoints->InsertPoint(2,bifurcationPoints->GetPoint(j+1));
332 vertexPoints->InsertPoint(3,bifurcationPoints->GetPoint(j));
333 }
334
335 int numberOfPolygonPoints = vertexPoints->GetNumberOfPoints();
336 double vertexPoint0[3], vertexPoint1[3], vertexPoint2[3];
337 double vertexNormal[3];
338 double cotangent0, cotangent1;
339
340 double polygonNormal[3];
341 polygonNormal[0] = polygonNormal[1] = polygonNormal[2] = 0.0;
342
343 for (int n=0; n<numberOfPolygonPoints; n++)
344 {
345 vtkIdType vertexId0 = (n-1+numberOfPolygonPoints) % numberOfPolygonPoints;
346 vtkIdType vertexId1 = n;
347 vtkIdType vertexId2 = (n+1) % numberOfPolygonPoints;
348 vertexPoints->GetPoint(vertexId0,vertexPoint0);
349 vertexPoints->GetPoint(vertexId1,vertexPoint1);
350 vertexPoints->GetPoint(vertexId2,vertexPoint2);
351
352 vtkTriangle::ComputeNormal(vertexPoint0,vertexPoint1,vertexPoint2,vertexNormal);
353
354 cotangent0 = vtkvmtkMath::Cotangent(vertexPoint0,vertexPoint1,bifurcationOrigin);
355 cotangent1 = vtkvmtkMath::Cotangent(bifurcationOrigin,vertexPoint1,vertexPoint2);
356
357 weight = (cotangent0 + cotangent1) / vtkMath::Distance2BetweenPoints(bifurcationOrigin,vertexPoint1);
358
359 for (int k=0; k<3; k++)
360 {
361 polygonNormal[k] += weight * vertexNormal[k];
362 }
363 }
364
365 vtkMath::Normalize(polygonNormal);
366
367 bifurcationNormals->InsertNextTuple(polygonNormal);
368 }
369 }
370
371 if (bifurcationNormals->GetNumberOfTuples() == 0)
372 {
373 if (bifurcationPoints->GetNumberOfPoints() > 1)
374 {
375 bifurcationPoints->GetPoint(1,point1);
376 bifurcationPoints->GetPoint(0,point0);
377 double lineUpNormal[3];
378 lineUpNormal[0] = point1[0] - point0[0];
379 lineUpNormal[1] = point1[1] - point0[1];
380 lineUpNormal[2] = point1[2] - point0[2];
381 vtkMath::Normalize(lineUpNormal);
382
383 double lineNormal[3], dummy[3];
384 vtkMath::Perpendiculars(lineUpNormal,lineNormal,dummy,0.0);
385
386 outputPoints->InsertNextPoint(bifurcationOrigin);
387 normalArray->InsertNextTuple(lineNormal);
388 upNormalArray->InsertNextTuple(lineUpNormal);
389 referenceGroupIdsArray->InsertNextTuple1(referenceGroupId);
390 }
391
392 vertexPoints->Delete();
393 bifurcationPoints->Delete();
394 bifurcationRadii->Delete();
395 bifurcationNormals->Delete();
396
397 return;
398 }
399
400 int numberOfNormals = bifurcationNormals->GetNumberOfTuples();
401
402 double positiveNormal[3], orientedNormal[3];
403 bifurcationNormals->GetTuple(0,positiveNormal);
404
405 for (i=1; i<numberOfNormals; i++)
406 {
407 bifurcationNormals->GetTuple(i,orientedNormal);
408
409 if (vtkMath::Dot(positiveNormal,orientedNormal) < 0.0)
410 {
411 orientedNormal[0] *= -1.0;
412 orientedNormal[1] *= -1.0;
413 orientedNormal[2] *= -1.0;
414 bifurcationNormals->SetTuple(i,orientedNormal);
415 }
416 }
417
418 bifurcationNormal[0] = bifurcationNormal[1] = bifurcationNormal[2] = 0.0;
419
420 for (i=0; i<numberOfNormals; i++)
421 {
422 bifurcationNormals->GetTuple(i,orientedNormal);
423 for (int k=0; k<3; k++)
424 {
425 bifurcationNormal[k] += orientedNormal[k];
426 }
427 }
428
429 vtkMath::Normalize(bifurcationNormal);
430
431 bifurcationUpNormal[0] = bifurcationUpNormal[1] = bifurcationUpNormal[2] = 0.0;
432 double upVector[3], projectedUpVector[3];
433
434 weightSum = 0.0;
435 for (i=0; i<numberOfBifurcationPoints; i+=2)
436 {
437 bifurcationPoints->GetPoint(i+1,point1);
438 bifurcationPoints->GetPoint(i,point0);
439 radius0 = bifurcationRadii->GetTuple1(i);
440 radius1 = bifurcationRadii->GetTuple1(i+1);
441 //weight upVectors with squared sphere radii
442 //weight = radius0 * radius0 + radius1 * radius1;
443 //weight upVectors with squared sphere radii of downstream branch
444 weight = radius1 * radius1;
445 int k;
446 for (k=0; k<3; k++)
447 {
448 upVector[k] = point1[k] - point0[k];
449 }
450 double dot = vtkMath::Dot(upVector,bifurcationNormal);
451 for (k=0; k<3; k++)
452 {
453 projectedUpVector[k] = upVector[k] - dot * bifurcationNormal[k];
454 }
455 vtkMath::Normalize(projectedUpVector);
456 for (k=0; k<3; k++)
457 {
458 bifurcationUpNormal[k] += projectedUpVector[k] * weight;
459 }
460 weightSum += weight;
461 }
462
463 vtkMath::Normalize(bifurcationUpNormal);
464
465 outputPoints->InsertNextPoint(bifurcationOrigin);
466 normalArray->InsertNextTuple(bifurcationNormal);
467 upNormalArray->InsertNextTuple(bifurcationUpNormal);
468 referenceGroupIdsArray->InsertNextTuple1(referenceGroupId);
469
470 vertexPoints->Delete();
471 bifurcationPoints->Delete();
472 bifurcationRadii->Delete();
473 bifurcationNormals->Delete();
474}
475
476void vtkvmtkCenterlineBifurcationReferenceSystems::PrintSelf(std::ostream& os, vtkIndent indent)
477{
478 this->Superclass::PrintSelf(os,indent);
479}
Generates a normal and upnormal reference system for each bifurcation point of a split centerline.
virtual int RequestData(vtkInformation *, vtkInformationVector **, vtkInformationVector *) override
void ComputeGroupReferenceSystem(vtkPolyData *input, int referenceGroupId, vtkPoints *outputPoints, vtkDoubleArray *normalArray, vtkDoubleArray *upNormalArray, vtkIntArray *referenceGroupIdsArray)
static void GetBlankedGroupsIdList(vtkPolyData *centerlines, const char *groupIdsArrayName, const char *blankingArrayName, vtkIdList *groupIds)
static void GetGroupUniqueCellIds(vtkPolyData *centerlines, const char *groupIdsArrayName, vtkIdType groupId, vtkIdList *groupCellIds)
static double Cotangent(double point0[3], double point1[3], double point2[3])
vtkStandardNewMacro(vtkvmtkCenterlineBifurcationReferenceSystems)
#define VTK_VMTK_DOUBLE_TOL