87{
88 vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
89 vtkInformation *outInfo = outputVector->GetInformationObject(0);
90
91 vtkPolyData *input = vtkPolyData::SafeDownCast(inInfo->Get(vtkDataObject::DATA_OBJECT()));
92 vtkPolyData *output = vtkPolyData::SafeDownCast(outInfo->Get(vtkDataObject::DATA_OBJECT()));
93
95 {
96 vtkErrorMacro(<<"RadiusArrayName not specified");
97 return 1;
98 }
99
100 vtkDataArray* radiusArray = input->GetPointData()->GetArray(this->
RadiusArrayName);
101
102 if (!radiusArray)
103 {
104 vtkErrorMacro(<<"RadiusArray with name specified does not exist");
105 return 1;
106 }
107
109 {
110 vtkErrorMacro(<<"GroupIdsArrayName not specified");
111 return 1;
112 }
113
114 vtkDataArray* groupIdsArray = input->GetCellData()->GetArray(this->
GroupIdsArrayName);
115
116 if (!groupIdsArray)
117 {
118 vtkErrorMacro(<<"GroupIdsArray with name specified does not exist");
119 return 1;
120 }
121
123 {
124 vtkErrorMacro(<<"CenterlineIdsArrayName not specified");
125 return 1;
126 }
127
129
130 if (!centerlineIdsArray)
131 {
132 vtkErrorMacro(<<"CenterlineIdsArray with name specified does not exist");
133 return 1;
134 }
135
137 {
138 vtkErrorMacro(<<"TractIdsArrayName not specified");
139 return 1;
140 }
141
142 vtkDataArray* tractIdsArray = input->GetCellData()->GetArray(this->
TractIdsArrayName);
143
144 if (!tractIdsArray)
145 {
146 vtkErrorMacro(<<"TractIdsArray with name specified does not exist");
147 return 1;
148 }
149
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 vtkCleanPolyData* cleaner = vtkCleanPolyData::New();
165 cleaner->SetInputData(input);
166 cleaner->Update();
167
169 {
171 }
172
173 vtkSplineFilter* resampler = vtkSplineFilter::New();
174 resampler->SetInputConnection(cleaner->GetOutputPort());
175 resampler->SetSubdivideToLength();
177 resampler->Update();
178
179 vtkPolyData* resampledCenterlines = vtkPolyData::New();
180 resampledCenterlines->DeepCopy(resampler->GetOutput());
181
182 resampler->Delete();
183 cleaner->Delete();
184
185 radiusArray = resampledCenterlines->GetPointData()->GetArray(this->
RadiusArrayName);
186
187 vtkPoints* outputPoints = vtkPoints::New();
188 vtkCellArray* outputLines = vtkCellArray::New();
189
190 output->SetPoints(outputPoints);
191 output->SetLines(outputLines);
192
193 output->GetPointData()->CopyAllocate(resampledCenterlines->GetPointData());
194 output->GetCellData()->CopyAllocate(resampledCenterlines->GetCellData());
195
196 vtkIdList* nonBlankedGroupIds = vtkIdList::New();
198
199 vtkIdList* groupIdsToMergedCells = vtkIdList::New();
201 groupIdsToMergedCells->SetNumberOfIds(maxGroupId);
202 int i;
203 for (i=0; i<maxGroupId; i++)
204 {
205 groupIdsToMergedCells->SetId(i,-1);
206 }
207
208 vtkIdTypeArray* cellEndPointIds = vtkIdTypeArray::New();
209 cellEndPointIds->SetNumberOfComponents(2);
210
211 for (i=0; i<nonBlankedGroupIds->GetNumberOfIds(); i++)
212 {
213 vtkIdType groupId = nonBlankedGroupIds->GetId(i);
214 vtkIdList* groupUniqueCellIds = vtkIdList::New();
215
217
218 int numberOfMergedCellPoints = 0;
219 int j;
220 for (j=0; j<groupUniqueCellIds->GetNumberOfIds(); j++)
221 {
222 vtkIdType cellId = groupUniqueCellIds->GetId(j);
223 int numberOfCellPoints = resampledCenterlines->GetCell(cellId)->GetNumberOfPoints();
224 if ((j==0) || (numberOfCellPoints < numberOfMergedCellPoints))
225 {
226 numberOfMergedCellPoints = numberOfCellPoints;
227 }
228 }
229
230 vtkIdType mergedCellId = outputLines->InsertNextCell(numberOfMergedCellPoints);
231 groupIdsToMergedCells->InsertId(groupId,mergedCellId);
232 int k;
233 for (k=0; k<numberOfMergedCellPoints; k++)
234 {
235 double mergedPoint[3];
236 mergedPoint[0] = 0.0;
237 mergedPoint[1] = 0.0;
238 mergedPoint[2] = 0.0;
239 double weightSum = 0.0;
240 for (j=0; j<groupUniqueCellIds->GetNumberOfIds(); j++)
241 {
242 vtkIdType cellId = groupUniqueCellIds->GetId(j);
243 double point[3];
244 vtkIdType pointId = resampledCenterlines->GetCell(cellId)->GetPointId(k);
245 resampledCenterlines->GetPoint(pointId,point);
246 double radius = radiusArray->GetTuple1(pointId);
247 double weight = radius*radius;
248 resampledCenterlines->GetCell(cellId)->GetPoints()->GetPoint(k,point);
249 mergedPoint[0] += weight * point[0];
250 mergedPoint[1] += weight * point[1];
251 mergedPoint[2] += weight * point[2];
252 weightSum += weight;
253 }
254 mergedPoint[0] /= weightSum;
255 mergedPoint[1] /= weightSum;
256 mergedPoint[2] /= weightSum;
257 vtkIdType mergedPointId = outputPoints->InsertNextPoint(mergedPoint);
258 outputLines->InsertCellPoint(mergedPointId);
259 vtkIdType cellId = groupUniqueCellIds->GetId(0);
260 vtkIdType pointId = resampledCenterlines->GetCell(cellId)->GetPointId(k);
261 output->GetPointData()->CopyData(resampledCenterlines->GetPointData(),pointId,mergedPointId);
262 if (k==0 || k==numberOfMergedCellPoints-1)
263 {
264 cellEndPointIds->InsertNextValue(mergedPointId);
265 }
266 }
267
268 vtkIdType cellId = groupUniqueCellIds->GetId(0);
269 output->GetCellData()->CopyData(resampledCenterlines->GetCellData(),cellId,mergedCellId);
270
271 groupUniqueCellIds->Delete();
272 }
273
275 {
276 nonBlankedGroupIds->Delete();
277 resampledCenterlines->Delete();
278 outputPoints->Delete();
279 outputLines->Delete();
280 groupIdsToMergedCells->Delete();
281 cellEndPointIds->Delete();
282 return 1;
283 }
284
286 referenceSystemsFilter->SetInputData(resampledCenterlines);
292 referenceSystemsFilter->Update();
293
294 vtkPolyData* referenceSystems = referenceSystemsFilter->GetOutput();
295
296 int numberOfMergedCells = outputLines->GetNumberOfCells();
297
298 vtkIdList* blankedGroupIds = vtkIdList::New();
300 vtkIdList* groupIdsToBifurcationPointIds = vtkIdList::New();
301 vtkIdTypeArray* cellAdditionalEndPointIds = vtkIdTypeArray::New();
302 cellAdditionalEndPointIds->SetNumberOfComponents(2);
303 cellAdditionalEndPointIds->SetNumberOfTuples(numberOfMergedCells);
304 vtkIdType tupleValue[2];
305 tupleValue[0] = tupleValue[1] = -1;
306 for (i=0; i<numberOfMergedCells; i++)
307 {
308 cellAdditionalEndPointIds->SetTypedTuple(i, tupleValue);
309 }
310
311 for (i=0; i<blankedGroupIds->GetNumberOfIds(); i++)
312 {
313 vtkIdType groupId = blankedGroupIds->GetId(i);
314
316
317 vtkIdList* upStreamGroupIds = vtkIdList::New();
318 vtkIdList* downStreamGroupIds = vtkIdList::New();
319
321
322 double bifurcationPoint[3];
323 referenceSystems->GetPoint(referenceSystemPointId,bifurcationPoint);
324
325 vtkIdType bifurcationPointId = outputPoints->InsertNextPoint(bifurcationPoint);
326
327 vtkIdType sourcePointId = -1;
328 int j;
329 for (j=0; j<upStreamGroupIds->GetNumberOfIds(); j++)
330 {
331 vtkIdType mergedCellId = groupIdsToMergedCells->GetId(upStreamGroupIds->GetId(j));
332 if (mergedCellId == -1)
333 {
334 continue;
335 }
336 if (sourcePointId == -1)
337 {
338 vtkIdList* groupUniqueCellIds = vtkIdList::New();
340 vtkCell* resampledCell = resampledCenterlines->GetCell(groupUniqueCellIds->GetId(0));
341 sourcePointId = resampledCell->GetPointId(resampledCell->GetNumberOfPoints()-1);
342 groupUniqueCellIds->Delete();
343 }
344 vtkIdType tupleValue[2];
345 cellAdditionalEndPointIds->GetTypedTuple(mergedCellId,tupleValue);
346 tupleValue[1] = bifurcationPointId;
347 cellAdditionalEndPointIds->SetTypedTuple(mergedCellId, tupleValue);
348 }
349
350 for (j=0; j<downStreamGroupIds->GetNumberOfIds(); j++)
351 {
352 vtkIdType mergedCellId = groupIdsToMergedCells->GetId(downStreamGroupIds->GetId(j));
353 if (mergedCellId == -1)
354 {
355 continue;
356 }
357 if (sourcePointId == -1)
358 {
359 vtkIdList* groupUniqueCellIds = vtkIdList::New();
361 vtkCell* resampledCell = resampledCenterlines->GetCell(groupUniqueCellIds->GetId(0));
362 sourcePointId = resampledCell->GetPointId(0);
363 groupUniqueCellIds->Delete();
364 }
365 vtkIdType tupleValue[2];
366 cellAdditionalEndPointIds->GetTypedTuple(mergedCellId, tupleValue);
367 tupleValue[0] = bifurcationPointId;
368 cellAdditionalEndPointIds->SetTypedTuple(mergedCellId, tupleValue);
369 }
370 if (sourcePointId == -1)
371 {
372 upStreamGroupIds->Delete();
373 downStreamGroupIds->Delete();
374 continue;
375 }
376
377
378 output->GetPointData()->CopyData(resampledCenterlines->GetPointData(),sourcePointId,bifurcationPointId);
379
380 upStreamGroupIds->Delete();
381 downStreamGroupIds->Delete();
382 }
383
384 vtkCellArray* extendedOutputLines = vtkCellArray::New();
385 outputLines->InitTraversal();
386 for (i=0; i<numberOfMergedCells; i++)
387 {
388 vtkIdType npts;
389 const vtkIdType *pts;
390 npts = 0;
391 pts = NULL;
392 outputLines->GetNextCell(npts,pts);
393 vtkIdType tupleValue[2];
394 cellAdditionalEndPointIds->GetTypedTuple(i, tupleValue);
395 vtkIdType extendedNpts = npts;
396 if (tupleValue[0] != -1)
397 {
398 extendedNpts += 1;
399 }
400 if (tupleValue[1] != -1)
401 {
402 extendedNpts += 1;
403 }
404 extendedOutputLines->InsertNextCell(extendedNpts);
405 if (tupleValue[0] != -1)
406 {
407 extendedOutputLines->InsertCellPoint(tupleValue[0]);
408 }
409 int j;
410 for (j=0; j<npts; j++)
411 {
412 extendedOutputLines->InsertCellPoint(pts[j]);
413 }
414 if (tupleValue[1] != -1)
415 {
416 extendedOutputLines->InsertCellPoint(tupleValue[1]);
417 }
418 }
419
420 output->SetLines(extendedOutputLines);
421
422 nonBlankedGroupIds->Delete();
423 resampledCenterlines->Delete();
424 outputPoints->Delete();
425 outputLines->Delete();
426 groupIdsToMergedCells->Delete();
427 cellEndPointIds->Delete();
428
429 referenceSystemsFilter->Delete();
430 blankedGroupIds->Delete();
431 groupIdsToBifurcationPointIds->Delete();
432 cellAdditionalEndPointIds->Delete();
433 extendedOutputLines->Delete();
434
435 return 1;
436}
virtual void SetNormalArrayName(const char *)
virtual void SetGroupIdsArrayName(const char *)
static vtkvmtkCenterlineBifurcationReferenceSystems * New()
virtual void SetUpNormalArrayName(const char *)
virtual void SetRadiusArrayName(const char *)
virtual void SetBlankingArrayName(const char *)
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 void FindAdjacentCenterlineGroupIds(vtkPolyData *centerlines, const char *groupIdsArrayName, const char *centerlineIdsArrayName, const char *tractIdsArrayName, vtkIdType groupId, vtkIdList *upStreamGroupIds, vtkIdList *downStreamGroupIds)
static void GetNonBlankedGroupsIdList(vtkPolyData *centerlines, const char *groupIdsArrayName, const char *blankingArrayName, vtkIdList *groupIds)
static vtkIdType GetMaxGroupId(vtkPolyData *centerlines, const char *groupIdsArrayName)
static vtkIdType GetReferenceSystemPointId(vtkPolyData *referenceSystems, const char *groupIdsArrayName, vtkIdType groupId)