VMTK
Loading...
Searching...
No Matches
vtkvmtkCenterlineBranchExtractor.cxx
Go to the documentation of this file.
1/*=========================================================================
2
3Program: VMTK
4Module: $RCSfile: vtkvmtkCenterlineBranchExtractor.cxx,v $
5Language: C++
6Date: $Date: 2006/07/07 10:46:19 $
7Version: $Revision: 1.10 $
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
24#include "vtkvmtkPolyBallLine.h"
25#include "vtkPolyData.h"
26#include "vtkPolyLine.h"
27#include "vtkPointData.h"
28#include "vtkIdList.h"
29#include "vtkCellData.h"
30#include "vtkDoubleArray.h"
31#include "vtkMath.h"
32#include "vtkObjectFactory.h"
33
34
36
40
44
45void vtkvmtkCenterlineBranchExtractor::ComputeCenterlineSplitting(vtkPolyData* input, vtkIdType cellId)
46{
48
49 if (this->SubIds)
50 {
51 delete[] this->SubIds;
52 this->SubIds = NULL;
53 }
54
55 if (this->PCoords)
56 {
57 delete[] this->PCoords;
58 this->PCoords = NULL;
59 }
60
61 if (this->TractBlanking)
62 {
63 delete[] this->TractBlanking;
64 this->TractBlanking = NULL;
65 }
66
67 if (!this->RadiusArrayName)
68 {
69 return;
70 }
71
72 if (input->GetCell(cellId)->GetCellType() != VTK_LINE && input->GetCell(cellId)->GetCellType() != VTK_POLY_LINE)
73 {
74 return;
75 }
76
77 vtkDataArray* radiusArray = input->GetPointData()->GetArray(this->RadiusArrayName);
78
79 if (!radiusArray)
80 {
81 return;
82 }
83
84 // TODO: remove assumption: centerlines organized in a tree, parent branches as sources and children as targets
85
87
88 tube->SetInput(input);
90
91 vtkIdList* tubeCellIds = vtkIdList::New();
92
93 vtkIdList* intersectionSubIds = vtkIdList::New();
94 vtkDoubleArray* intersectionPCoords = vtkDoubleArray::New();
95
96 vtkIdType numberOfCells = input->GetNumberOfCells();
97
98 //for every other cell than cellId, find intersection of cellId with the tube function of the other cell
99 int j;
100 for (j=0; j<numberOfCells; j++)
101 {
102 if (j==cellId)
103 {
104 continue;
105 }
106
107 if (input->GetCell(j)->GetCellType() != VTK_LINE && input->GetCell(j)->GetCellType() != VTK_POLY_LINE)
108 {
109 continue;
110 }
111
112 tubeCellIds->Initialize();
113 tubeCellIds->InsertNextId(j);
114 tube->SetInputCellIds(tubeCellIds);
115
116 int numberOfCellPoints = input->GetCell(cellId)->GetNumberOfPoints();
117
118 double point0[3], point1[3];
119 double tubeValue0, tubeValue1;
120 for (int k=0; k<numberOfCellPoints-1; k++)
121 {
122 input->GetCell(cellId)->GetPoints()->GetPoint(k,point0);
123 input->GetCell(cellId)->GetPoints()->GetPoint(k+1,point1);
124 tubeValue0 = tube->EvaluateFunction(point0);
125 tubeValue1 = tube->EvaluateFunction(point1);
126
127// if (tubeValue0 * tubeValue1 <= 0.0)
128 if ((tubeValue0 <= 0.0) && (tubeValue1 > 0.0)) // this is for divergent networks
129 {
130 double radius0, radius1;
131 radius0 = radiusArray->GetComponent(input->GetCell(cellId)->GetPointId(k),0);
132 radius1 = radiusArray->GetComponent(input->GetCell(cellId)->GetPointId(k+1),0);
133
134 //TODO: use Newton iterations
135 double segmentLength, stepSize, pcoordStepSize;
136 int numberOfSteps;
137 double subPoint0[3], subPoint1[3];
138 double subTubeValue0, subTubeValue1;
139 segmentLength = vtkMath::Distance2BetweenPoints(point0,point1);
140 stepSize = 1E-2 * (radius0 + radius1) / 2.0;
141 numberOfSteps = (int)ceil(segmentLength / stepSize);
142 stepSize = segmentLength / numberOfSteps;
143 pcoordStepSize = 1.0 / (double)numberOfSteps;
144
145 double pcoord = 0.0;
146 int s;
147 for (s=0; s<numberOfSteps; s++)
148 {
149 for (int d=0; d<3; d++)
150 {
151 subPoint0[d] = point0[d] + pcoord * (point1[d] - point0[d]);
152 subPoint1[d] = point0[d] + (pcoord + pcoordStepSize) * (point1[d] - point0[d]);
153 }
154
155 subTubeValue0 = tube->EvaluateFunction(subPoint0);
156 subTubeValue1 = tube->EvaluateFunction(subPoint1);
157
158// if (subTubeValue0 * subTubeValue1 <= 0.0)
159 if ((subTubeValue0 <= 0.0) && (subTubeValue1 > 0.0)) // this is for divergent networks
160 {
161 pcoord += pcoordStepSize;
162 break;
163 }
164
165 pcoord += pcoordStepSize;
166 }
167
168 //since we are considering each other cell separately, we need to decide where to put the intersection point in order to have them sorted along cellId
169 int numberOfIntersections = intersectionSubIds->GetNumberOfIds();
170 int insertLocation = 0;
171 for (s=0; s<numberOfIntersections; s++)
172 {
173 bool afterThis = (k > intersectionSubIds->GetId(s)) || (k==intersectionSubIds->GetId(s) && pcoord > intersectionPCoords->GetValue(s));
174 bool beforeNext = true;
175 if (s<numberOfIntersections-1)
176 {
177 beforeNext = (k < intersectionSubIds->GetId(s+1)) || (k==intersectionSubIds->GetId(s+1) && pcoord <= intersectionPCoords->GetValue(s+1));
178 }
179 if (afterThis && beforeNext)
180 {
181 insertLocation = s+1;
182 break;
183 }
184 }
185 for (s=numberOfIntersections-1; s>=insertLocation; s--)
186 {
187 intersectionSubIds->InsertId(s+1,intersectionSubIds->GetId(s));
188 intersectionPCoords->InsertValue(s+1,intersectionPCoords->GetValue(s));
189 }
190 intersectionSubIds->InsertId(insertLocation,k);
191 intersectionPCoords->InsertValue(insertLocation,pcoord);
192 }
193 }
194 }
195
196 // For each branch, find point at one-sphere distance upstream (i.e. touching forward).
197
198 vtkIdType touchingSubId;
199 double touchingPCoord;
200
201 const int numberOfGapSpheres = 1;
202 const bool forward = true;
203
204 vtkIdList* touchingSubIds = vtkIdList::New();
205 vtkDoubleArray* touchingPCoords = vtkDoubleArray::New();
206
207 int i;
208 for (i=0; i<intersectionSubIds->GetNumberOfIds(); i++)
209 {
210 vtkvmtkCenterlineSphereDistance::FindNTouchingSphereCenter(input,this->RadiusArrayName,cellId,intersectionSubIds->GetId(i),intersectionPCoords->GetValue(i),numberOfGapSpheres,touchingSubId,touchingPCoord,forward);
211 if (touchingSubId == -1)
212 {
213 touchingSubId = 0;
214 touchingPCoord = 0.0;
215 touchingSubIds->InsertNextId(touchingSubId);
216 touchingPCoords->InsertNextValue(touchingPCoord);
217 continue;
218 }
219
220 touchingSubIds->InsertNextId(touchingSubId);
221 touchingPCoords->InsertNextValue(touchingPCoord);
222 }
223
224 vtkIdList* splittingSubIds = vtkIdList::New();
225 vtkDoubleArray* splittingPCoords = vtkDoubleArray::New();
226 vtkIdList* blankingFlags = vtkIdList::New();
227
228 blankingFlags->InsertNextId(0);
229/*
230 for (i=0; i<intersectionSubIds->GetNumberOfIds(); i++)
231 {
232 // put one intersection every touching. Blank out between touching and intersection.
233 if (i==0)
234 {
235 splittingSubIds->InsertNextId(touchingSubIds->GetId(i));
236 splittingPCoords->InsertNextValue(touchingPCoords->GetValue(i));
237 blankingFlags->InsertNextId(1);
238 }
239 else
240 {
241 // check if this touching is before previous intersection. If it is, skip the touching.
242 if (!((touchingSubIds->GetId(i) < intersectionSubIds->GetId(i-1)) ||
243 ((touchingSubIds->GetId(i) == intersectionSubIds->GetId(i-1)) && (touchingPCoords->GetValue(i) <= intersectionPCoords->GetValue(i-1)))))
244 {
245 splittingSubIds->InsertNextId(touchingSubIds->GetId(i));
246 splittingPCoords->InsertNextValue(touchingPCoords->GetValue(i));
247 blankingFlags->InsertNextId(1);
248 }
249 }
250
251 if (i==intersectionSubIds->GetNumberOfIds()-1)
252 {
253 splittingSubIds->InsertNextId(intersectionSubIds->GetId(i));
254 splittingPCoords->InsertNextValue(intersectionPCoords->GetValue(i));
255 blankingFlags->InsertNextId(0);
256 }
257 else
258 {
259 // check if subsequent touching is before this intersection. If it is, and if subsequent intersection is after this intersection, skip the intersection.
260 if (!
261 (((touchingSubIds->GetId(i+1) < intersectionSubIds->GetId(i)) ||
262 ((touchingSubIds->GetId(i+1) == intersectionSubIds->GetId(i)) && (touchingPCoords->GetValue(i+1) <= intersectionPCoords->GetValue(i))))) &&
263 (((intersectionSubIds->GetId(i+1) > intersectionSubIds->GetId(i)) ||
264 ((intersectionSubIds->GetId(i+1) == intersectionSubIds->GetId(i)) && (intersectionPCoords->GetValue(i+1) > intersectionPCoords->GetValue(i))))) )
265 {
266 splittingSubIds->InsertNextId(intersectionSubIds->GetId(i));
267 splittingPCoords->InsertNextValue(intersectionPCoords->GetValue(i));
268 blankingFlags->InsertNextId(0);
269 }
270 }
271 }
272*/
273
274 //for every touching, put one intersection, and blank in between. If a subsequent touching falls between a previous touching and the corresponding intersection, take the farthest intersection
275 vtkIdType prevIntersectionId = -1;
276 for (i=0; i<touchingSubIds->GetNumberOfIds(); i++)
277 {
278 if (i > 0)
279 {
280 if ((touchingSubIds->GetId(i) < intersectionSubIds->GetId(prevIntersectionId)) ||
281 ((touchingSubIds->GetId(i) == intersectionSubIds->GetId(prevIntersectionId)) && (touchingPCoords->GetValue(i) <= intersectionPCoords->GetValue(prevIntersectionId))))
282 {
283 continue;
284 }
285 }
286
287 splittingSubIds->InsertNextId(touchingSubIds->GetId(i));
288 splittingPCoords->InsertNextValue(touchingPCoords->GetValue(i));
289 blankingFlags->InsertNextId(1);
290
291 vtkIdType maxIntersectionId = i;
292
293 if (i < touchingSubIds->GetNumberOfIds()-1)
294 {
295 for (j=i+1; j<touchingSubIds->GetNumberOfIds(); j++)
296 {
297 if ((touchingSubIds->GetId(j) < intersectionSubIds->GetId(maxIntersectionId)) ||
298 ((touchingSubIds->GetId(j) == intersectionSubIds->GetId(maxIntersectionId)) && (touchingPCoords->GetValue(j) <= intersectionPCoords->GetValue(maxIntersectionId))))
299 {
300 if ((intersectionSubIds->GetId(j) > intersectionSubIds->GetId(maxIntersectionId)) ||
301 ((intersectionSubIds->GetId(j) == intersectionSubIds->GetId(maxIntersectionId)) && (intersectionPCoords->GetValue(j) >= intersectionPCoords->GetValue(maxIntersectionId))))
302 {
303 maxIntersectionId = j;
304 }
305 }
306 else
307 {
308 break;
309 }
310 }
311 }
312
313 splittingSubIds->InsertNextId(intersectionSubIds->GetId(maxIntersectionId));
314 splittingPCoords->InsertNextValue(intersectionPCoords->GetValue(maxIntersectionId));
315 blankingFlags->InsertNextId(0);
316
317 prevIntersectionId = maxIntersectionId;
318 }
319
320 this->NumberOfSplittingPoints = splittingSubIds->GetNumberOfIds();
321
322 this->SubIds = new vtkIdType[this->NumberOfSplittingPoints];
323 this->PCoords = new double[this->NumberOfSplittingPoints];
324
325 for (i=0; i<splittingSubIds->GetNumberOfIds(); i++)
326 {
327 this->SubIds[i] = splittingSubIds->GetId(i);
328 this->PCoords[i] = splittingPCoords->GetValue(i);
329 }
330
331 this->TractBlanking = new int[this->NumberOfSplittingPoints+1];
332
333 for (i=0; i<this->NumberOfSplittingPoints+1; i++)
334 {
335 this->TractBlanking[i] = blankingFlags->GetId(i);
336 }
337
338 tube->Delete();
339 tubeCellIds->Delete();
340
341 intersectionSubIds->Delete();
342 intersectionPCoords->Delete();
343
344 touchingSubIds->Delete();
345 touchingPCoords->Delete();
346
347 splittingSubIds->Delete();
348 splittingPCoords->Delete();
349 blankingFlags->Delete();
350}
351
352void vtkvmtkCenterlineBranchExtractor::GroupTracts(vtkPolyData* input, vtkPolyData* centerlineTracts)
353{
354 Superclass::GroupTracts(input,centerlineTracts);
355
356 //New ideas:
357 // loop over group ids, if blanked group, if same centerlineId as another tract in same group, make it another group. And what about the rest? No, better: assume net is a tree. For every group, look at which tracts of each centerline are downstream (via TractIdsArray) and group them in a bifurcation. In order to relax the assumption on the tree, for every group, for every direction, look for all the groups to which the next next tracts belong (tractId + 2 or -2).
358}
359
360void vtkvmtkCenterlineBranchExtractor::PrintSelf(std::ostream& os, vtkIndent indent)
361{
362 this->Superclass::PrintSelf(os,indent);
363}
virtual void GroupTracts(vtkPolyData *input, vtkPolyData *centerlineTracts) override
void PrintSelf(std::ostream &os, vtkIndent indent) override
virtual void ComputeCenterlineSplitting(vtkPolyData *input, vtkIdType cellId) override
static void FindNTouchingSphereCenter(vtkPolyData *centerlines, const char *radiusArrayName, vtkIdType cellId, vtkIdType subId, double pcoord, int numberOfTouchingSpheres, vtkIdType &touchingSubId, double &touchingPCoord, bool forward=true)
void PrintSelf(std::ostream &os, vtkIndent indent) override
virtual void GroupTracts(vtkPolyData *input, vtkPolyData *centerlineTracts)
Implements vtkImplicitFunction to evaluate the minimum sphere function for an envelope created by an ...
virtual void SetInput(vtkPolyData *)
virtual void SetPolyBallRadiusArrayName(const char *)
virtual void SetInputCellIds(vtkIdList *)
static vtkvmtkPolyBallLine * New()
double EvaluateFunction(double x[3]) override
vtkStandardNewMacro(vtkvmtkCenterlineBranchExtractor)