VMTK
Loading...
Searching...
No Matches
vtkvmtkCenterlineUtilities.cxx
Go to the documentation of this file.
1/*=========================================================================
2
3Program: VMTK
4Module: $RCSfile: vtkvmtkCenterlineUtilities.cxx,v $
5Language: C++
6Date: $Date: 2006/10/17 15:16:16 $
7Version: $Revision: 1.5 $
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 "vtkPointData.h"
25#include "vtkCellData.h"
26#include "vtkIdList.h"
27#include "vtkPolyLine.h"
28#include "vtkMath.h"
29#include "vtkvmtkConstants.h"
30#include "vtkObjectFactory.h"
31
32
34
35vtkIdType vtkvmtkCenterlineUtilities::GetMaxGroupId(vtkPolyData* centerlines, const char* groupIdsArrayName)
36{
37 vtkDataArray* groupIdsArray = centerlines->GetCellData()->GetArray(groupIdsArrayName);
38
39 int numberOfCells = centerlines->GetNumberOfCells();
40 vtkIdType maxGroupId = -1;
41
42 int i;
43 for (i=0; i<numberOfCells; i++)
44 {
45 vtkIdType groupId = static_cast<int>(groupIdsArray->GetComponent(i,0));
46 if (groupId > maxGroupId)
47 {
48 maxGroupId = groupId;
49 }
50 }
51
52 return maxGroupId;
53}
54
55void vtkvmtkCenterlineUtilities::GetGroupsIdList(vtkPolyData* centerlines, const char* groupIdsArrayName, const char* blankingArrayName, int blanked, vtkIdList* groupIds)
56{
57 vtkDataArray* groupIdsArray = centerlines->GetCellData()->GetArray(groupIdsArrayName);
58
59 vtkDataArray* blankingArray = NULL;
60
61 if (blankingArrayName)
62 {
63 blankingArray = centerlines->GetCellData()->GetArray(blankingArrayName);
64 }
65
66 int numberOfCells = centerlines->GetNumberOfCells();
67
68 groupIds->Initialize();
69
70 int i;
71 for (i=0; i<numberOfCells; i++)
72 {
73 vtkIdType groupId = static_cast<int>(groupIdsArray->GetComponent(i,0));
74
75 if (blankingArray)
76 {
77 vtkIdType current_blanked = static_cast<int>(blankingArray->GetComponent(i,0));
78 if (blanked != current_blanked)
79 {
80 continue;
81 }
82 }
83 groupIds->InsertUniqueId(groupId);
84 }
85}
86
87void vtkvmtkCenterlineUtilities::GetGroupsIdList(vtkPolyData* centerlines, const char* groupIdsArrayName, vtkIdList* groupIds)
88{
89 vtkvmtkCenterlineUtilities::GetGroupsIdList(centerlines,groupIdsArrayName,NULL,0,groupIds);
90}
91
92void vtkvmtkCenterlineUtilities::GetNonBlankedGroupsIdList(vtkPolyData* centerlines, const char* groupIdsArrayName, const char* blankingArrayName, vtkIdList* groupIds)
93{
94 vtkvmtkCenterlineUtilities::GetGroupsIdList(centerlines,groupIdsArrayName,blankingArrayName,0,groupIds);
95}
96
97void vtkvmtkCenterlineUtilities::GetBlankedGroupsIdList(vtkPolyData* centerlines, const char* groupIdsArrayName, const char* blankingArrayName, vtkIdList* groupIds)
98{
99 vtkvmtkCenterlineUtilities::GetGroupsIdList(centerlines,groupIdsArrayName,blankingArrayName,1,groupIds);
100}
101
102void vtkvmtkCenterlineUtilities::GetGroupCellIds(vtkPolyData* centerlines, const char* groupIdsArrayName, vtkIdType groupId, vtkIdList* groupCellIds)
103{
104 vtkDataArray* groupIdsArray = centerlines->GetCellData()->GetArray(groupIdsArrayName);
105 int numberOfCells = centerlines->GetNumberOfCells();
106
107 groupCellIds->Initialize();
108
109 int i;
110 for (i=0; i<numberOfCells; i++)
111 {
112 vtkIdType current_groupId = static_cast<int>(groupIdsArray->GetComponent(i,0));
113 if (current_groupId != groupId)
114 {
115 continue;
116 }
117 groupCellIds->InsertNextId(i);
118 }
119}
120
121void vtkvmtkCenterlineUtilities::GetGroupUniqueCellIds(vtkPolyData* centerlines, const char* groupIdsArrayName, vtkIdType groupId, vtkIdList* groupCellIds)
122{
123 vtkDataArray* groupIdsArray = centerlines->GetCellData()->GetArray(groupIdsArrayName);
124 int numberOfCells = centerlines->GetNumberOfCells();
125
126 groupCellIds->Initialize();
127
128 int i;
129 for (i=0; i<numberOfCells; i++)
130 {
131 vtkIdType current_groupId = static_cast<int>(groupIdsArray->GetComponent(i,0));
132 if (current_groupId != groupId)
133 {
134 continue;
135 }
136 int currentNumberOfGroupCellIds = groupCellIds->GetNumberOfIds();
137 bool duplicate = false;
138 int j;
139 for (j=0; j<currentNumberOfGroupCellIds; j++)
140 {
141 double currentCellFirstPoint[3], currentCellLastPoint[3];
142 double cellFirstPoint[3], cellLastPoint[3];
143 vtkCell* cell = centerlines->GetCell(groupCellIds->GetId(j));
144 cell->GetPoints()->GetPoint(0,currentCellFirstPoint);
145 cell->GetPoints()->GetPoint(cell->GetNumberOfPoints()-1,currentCellLastPoint);
146 cell = centerlines->GetCell(i);
147 cell->GetPoints()->GetPoint(0,cellFirstPoint);
148 cell->GetPoints()->GetPoint(cell->GetNumberOfPoints()-1,cellLastPoint);
149 if ((vtkMath::Distance2BetweenPoints(currentCellFirstPoint,cellFirstPoint) < VTK_VMTK_DOUBLE_TOL) &&
150 (vtkMath::Distance2BetweenPoints(currentCellLastPoint,cellLastPoint) < VTK_VMTK_DOUBLE_TOL))
151 {
152 duplicate = true;
153 break;
154 }
155 }
156 if (duplicate)
157 {
158 continue;
159 }
160 groupCellIds->InsertNextId(i);
161 }
162}
163
164void vtkvmtkCenterlineUtilities::GetCenterlineCellIds(vtkPolyData* centerlines, const char* centerlineIdsArrayName, vtkIdType centerlineId, vtkIdList* centerlineCellIds)
165{
166 vtkDataArray* centerlineIdsArray = centerlines->GetCellData()->GetArray(centerlineIdsArrayName);
167 int numberOfCells = centerlines->GetNumberOfCells();
168
169 centerlineCellIds->Initialize();
170
171 int i;
172 for (i=0; i<numberOfCells; i++)
173 {
174 vtkIdType currentCenterlineId = static_cast<int>(centerlineIdsArray->GetComponent(i,0));
175 if (currentCenterlineId != centerlineId)
176 {
177 continue;
178 }
179 centerlineCellIds->InsertNextId(i);
180 }
181}
182
183void vtkvmtkCenterlineUtilities::GetCenterlineCellIds(vtkPolyData* centerlines, const char* centerlineIdsArrayName, const char* tractIdsArrayName, vtkIdType centerlineId, vtkIdList* centerlineCellIds)
184{
185 vtkDataArray* centerlineIdsArray = centerlines->GetCellData()->GetArray(centerlineIdsArrayName);
186 vtkDataArray* tractIdsArray = centerlines->GetCellData()->GetArray(tractIdsArrayName);
187 int numberOfCells = centerlines->GetNumberOfCells();
188
189 centerlineCellIds->Initialize();
190 vtkIdList* centerlineTractIds = vtkIdList::New();
191
192 int i;
193 for (i=0; i<numberOfCells; i++)
194 {
195 vtkIdType currentCenterlineId = static_cast<int>(centerlineIdsArray->GetComponent(i,0));
196 if (currentCenterlineId != centerlineId)
197 {
198 continue;
199 }
200 centerlineCellIds->InsertNextId(i);
201 vtkIdType currectTractId = static_cast<int>(tractIdsArray->GetComponent(i,0));
202 centerlineTractIds->InsertNextId(currectTractId);
203 }
204
205 int numberOfCenterlineCellIds = centerlineCellIds->GetNumberOfIds();
206
207 bool done = false;
208 while (!done)
209 {
210 done = true;
211 for (i=0; i<numberOfCenterlineCellIds-1; i++)
212 {
213 if (centerlineTractIds->GetId(i) > centerlineTractIds->GetId(i+1))
214 {
215 vtkIdType tmp = centerlineTractIds->GetId(i+1);
216 centerlineTractIds->SetId(i+1,centerlineTractIds->GetId(i));
217 centerlineTractIds->SetId(i,tmp);
218 tmp = centerlineCellIds->GetId(i+1);
219 centerlineCellIds->SetId(i+1,centerlineCellIds->GetId(i));
220 centerlineCellIds->SetId(i,tmp);
221 done = false;
222 }
223 }
224 }
225
226 centerlineTractIds->Delete();
227}
228
229int vtkvmtkCenterlineUtilities::IsGroupBlanked(vtkPolyData* centerlines, const char* groupIdsArrayName, const char* blankingArrayName, vtkIdType groupId)
230{
231 vtkDataArray* groupIdsArray = centerlines->GetCellData()->GetArray(groupIdsArrayName);
232 int numberOfCells = centerlines->GetNumberOfCells();
233
234 vtkIdType cellId = -1;
235 int i;
236 for (i=0; i<numberOfCells; i++)
237 {
238 vtkIdType current_groupId = static_cast<int>(groupIdsArray->GetComponent(i,0));
239 if (current_groupId != groupId)
240 {
241 continue;
242 }
243 cellId = i;
244 break;
245 }
246
247 if (cellId == -1)
248 {
249 return -1;
250 }
251
252 return vtkvmtkCenterlineUtilities::IsCellBlanked(centerlines,blankingArrayName,cellId);
253}
254
255int vtkvmtkCenterlineUtilities::IsCellBlanked(vtkPolyData* centerlines, const char* blankingArrayName, vtkIdType cellId)
256{
257 vtkDataArray* blankingArray = centerlines->GetCellData()->GetArray(blankingArrayName);
258 int blanking = static_cast<int>(blankingArray->GetComponent(cellId,0));
259 int isBlanked = blanking == 1 ? 1 : 0;
260 return isBlanked;
261}
262
263void vtkvmtkCenterlineUtilities::FindAdjacentCenterlineGroupIds(vtkPolyData* centerlines, const char* groupIdsArrayName, const char* centerlineIdsArrayName, const char* tractIdsArrayName, vtkIdType groupId, vtkIdList* upStreamGroupIds, vtkIdList* downStreamGroupIds)
264{
265 upStreamGroupIds->Initialize();
266 downStreamGroupIds->Initialize();
267
268 vtkDataArray* centerlineGroupIdsArray = centerlines->GetCellData()->GetArray(groupIdsArrayName);
269 vtkDataArray* centerlineIdsArray = centerlines->GetCellData()->GetArray(centerlineIdsArrayName);
270 vtkDataArray* centerlineTractIdsArray = centerlines->GetCellData()->GetArray(tractIdsArrayName);
271
272 int numberOfCenterlineCells = centerlines->GetNumberOfCells();
273
274 int i;
275 for (i=0; i<numberOfCenterlineCells; i++)
276 {
277 vtkIdType branchGroupId = static_cast<int>(centerlineGroupIdsArray->GetComponent(i,0));
278
279 if (branchGroupId != groupId)
280 {
281 continue;
282 }
283
284 vtkCell* centerlineBranch = centerlines->GetCell(i);
285 if (centerlineBranch->GetCellType() != VTK_LINE && centerlineBranch->GetCellType() != VTK_POLY_LINE)
286 {
287 continue;
288 }
289
290 vtkIdType branchCenterlineId = static_cast<int>(centerlineIdsArray->GetComponent(i,0));
291 vtkIdType branchCenterlineTractId = static_cast<int>(centerlineTractIdsArray->GetComponent(i,0));
292
293 int j;
294 for (j=0; j<numberOfCenterlineCells; j++)
295 {
296 vtkIdType currentBranchCenterlineGroupId = static_cast<int>(centerlineGroupIdsArray->GetComponent(j,0));
297 vtkIdType currentBranchCenterlineId = static_cast<int>(centerlineIdsArray->GetComponent(j,0));
298 vtkIdType currentBranchCenterlineTractId = static_cast<int>(centerlineTractIdsArray->GetComponent(j,0));
299
300 if (currentBranchCenterlineGroupId == branchGroupId)
301 {
302 continue;
303 }
304
305 if (currentBranchCenterlineId != branchCenterlineId)
306 {
307 continue;
308 }
309
310 //WARNING: this assumes that tract ids are adjacent
311 if (currentBranchCenterlineTractId == branchCenterlineTractId - 1)
312 {
313 upStreamGroupIds->InsertUniqueId(currentBranchCenterlineGroupId);
314 }
315
316 if (currentBranchCenterlineTractId == branchCenterlineTractId + 1)
317 {
318 downStreamGroupIds->InsertUniqueId(currentBranchCenterlineGroupId);
319 }
320 }
321 }
322}
323
324void vtkvmtkCenterlineUtilities::InterpolatePoint(vtkPolyData* centerlines, int cellId, int subId, double pcoord, double interpolatedPoint[3])
325{
326 double point0[3], point1[3];
327 vtkPoints* centerlineCellPoints = centerlines->GetCell(cellId)->GetPoints();
328 centerlineCellPoints->GetPoint(subId,point0);
329 centerlineCellPoints->GetPoint(subId+1,point1);
330
331 interpolatedPoint[0] = (1.0 - pcoord) * point0[0] + pcoord * point1[0];
332 interpolatedPoint[1] = (1.0 - pcoord) * point0[1] + pcoord * point1[1];
333 interpolatedPoint[2] = (1.0 - pcoord) * point0[2] + pcoord * point1[2];
334}
335
336void vtkvmtkCenterlineUtilities::InterpolateTuple(vtkPolyData* centerlines, const char* arrayName, int cellId, int subId, double pcoord, double* interpolatedTuple)
337{
338 vtkDataArray* array = centerlines->GetPointData()->GetArray(arrayName);
339 int numberOfComponents = array->GetNumberOfComponents();
340 vtkIdType pointId0 = centerlines->GetCell(cellId)->GetPointId(subId);
341 vtkIdType pointId1 = centerlines->GetCell(cellId)->GetPointId(subId+1);
342 double* tuple0 = array->GetTuple(pointId0);
343 double* tuple1 = array->GetTuple(pointId1);
344
345 int i;
346 for (i=0; i<numberOfComponents; i++)
347 {
348 interpolatedTuple[i] = (1.0 - pcoord) * tuple0[i] + pcoord * tuple1[i];
349 }
350}
351
352void vtkvmtkCenterlineUtilities::FindMergingPoints(vtkPolyData* centerlines, vtkPoints* mergingPoints, double tolerance=1E-8)
353{
354 mergingPoints->Initialize();
355 int numberOfCells = centerlines->GetNumberOfCells();
356 int i, j, k;
357 for (i=0; i<numberOfCells; i++)
358 {
359 vtkCell* centerline = centerlines->GetCell(i);
360 int numberOfCenterlinePoints = centerline->GetNumberOfPoints();
361 int previousMergeStatus = -1;
362 int mergeStatus = -1;
363 for (j=0; j<numberOfCenterlinePoints; j++)
364 {
365 double point[3];
366 centerline->GetPoints()->GetPoint(j,point);
367 for (k=i+1; k<numberOfCells; k++)
368 {
369 vtkCell* currentCenterline = centerlines->GetCell(k);
370 int numberOfCurrentCenterlinePoints = currentCenterline->GetNumberOfPoints();
371 double closestPoint[3];
372 int subId;
373 double pcoords[3];
374 double dist2;
375 double* weights = new double[numberOfCurrentCenterlinePoints];
376 currentCenterline->EvaluatePosition(point,closestPoint,subId,pcoords,dist2,weights);
377 if (dist2 < tolerance*tolerance)
378 {
379 mergeStatus = 1;
380 }
381 else
382 {
383 mergeStatus = 0;
384 }
385 if (j>0 && mergeStatus != previousMergeStatus)
386 {
387 mergingPoints->InsertNextPoint(point);
388 previousMergeStatus = mergeStatus;
389 }
390 delete[] weights;
391 }
392 }
393 }
394}
395
Provides a collection of functions used when working with split and grouped centerlines.
static void GetBlankedGroupsIdList(vtkPolyData *centerlines, const char *groupIdsArrayName, const char *blankingArrayName, vtkIdList *groupIds)
static void GetCenterlineCellIds(vtkPolyData *centerlines, const char *centerlineIdsArrayName, vtkIdType centerlineId, vtkIdList *centerlineCellIds)
static void InterpolatePoint(vtkPolyData *centerlines, int cellId, int subId, double pcoord, double interpolatedPoint[3])
static int IsGroupBlanked(vtkPolyData *centerlines, const char *groupIdsArrayName, const char *blankingArrayName, vtkIdType groupId)
static void GetGroupCellIds(vtkPolyData *centerlines, const char *groupIdsArrayName, vtkIdType groupId, vtkIdList *groupCellIds)
static void GetGroupUniqueCellIds(vtkPolyData *centerlines, const char *groupIdsArrayName, vtkIdType groupId, vtkIdList *groupCellIds)
static void InterpolateTuple(vtkPolyData *centerlines, const char *arrayName, int cellId, int subId, double pcoord, double *interpolatedTuple)
static void GetGroupsIdList(vtkPolyData *centerlines, const char *groupIdsArrayName, vtkIdList *groupIds)
static void FindMergingPoints(vtkPolyData *centerlines, vtkPoints *mergingPoints, double tolerance)
static void FindAdjacentCenterlineGroupIds(vtkPolyData *centerlines, const char *groupIdsArrayName, const char *centerlineIdsArrayName, const char *tractIdsArrayName, vtkIdType groupId, vtkIdList *upStreamGroupIds, vtkIdList *downStreamGroupIds)
static int IsCellBlanked(vtkPolyData *centerlines, const char *blankingArrayName, vtkIdType cellId)
static void GetNonBlankedGroupsIdList(vtkPolyData *centerlines, const char *groupIdsArrayName, const char *blankingArrayName, vtkIdList *groupIds)
static vtkIdType GetMaxGroupId(vtkPolyData *centerlines, const char *groupIdsArrayName)
vtkStandardNewMacro(vtkvmtkCenterlineUtilities)
#define VTK_VMTK_DOUBLE_TOL