VMTK
Loading...
Searching...
No Matches
vtkvmtkMergeCenterlines.cxx
Go to the documentation of this file.
1/*=========================================================================
2
3Program: VMTK
4Module: $RCSfile: vtkvmtkMergeCenterlines.cxx,v $
5Language: C++
6Date: $Date: 2006/04/06 16:46:43 $
7Version: $Revision: 1.4 $
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 "vtkSplineFilter.h"
27#include "vtkCleanPolyData.h"
28#include "vtkDoubleArray.h"
29#include "vtkPointData.h"
30#include "vtkCellData.h"
31#include "vtkCellArray.h"
32#include "vtkIdList.h"
33#include "vtkIdTypeArray.h"
34#include "vtkInformation.h"
35#include "vtkInformationVector.h"
36#include "vtkObjectFactory.h"
37#include "vtkVersion.h"
38
39
41
43{
44 this->RadiusArrayName = NULL;
45 this->GroupIdsArrayName = NULL;
46 this->CenterlineIdsArrayName = NULL;
47 this->TractIdsArrayName = NULL;
48 this->BlankingArrayName = NULL;
49 this->ResamplingStepLength = 0.0;
50 this->MergeBlanked = 1;
51}
52
54{
55 if (this->RadiusArrayName)
56 {
57 delete[] this->RadiusArrayName;
58 this->RadiusArrayName = NULL;
59 }
60
61 if (this->GroupIdsArrayName)
62 {
63 delete[] this->GroupIdsArrayName;
64 this->GroupIdsArrayName = NULL;
65 }
66
67 if (this->CenterlineIdsArrayName)
68 {
69 delete[] this->CenterlineIdsArrayName;
70 this->CenterlineIdsArrayName = NULL;
71 }
72
73 if (this->TractIdsArrayName)
74 {
75 delete[] this->TractIdsArrayName;
76 this->TractIdsArrayName = NULL;
77 }
78
79 if (this->BlankingArrayName)
80 {
81 delete[] this->BlankingArrayName;
82 this->BlankingArrayName = NULL;
83 }
84}
85
86int vtkvmtkMergeCenterlines::RequestData(vtkInformation *vtkNotUsed(request), vtkInformationVector **inputVector, vtkInformationVector *outputVector)
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
94 if (!this->RadiusArrayName)
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
108 if (!this->GroupIdsArrayName)
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
122 if (!this->CenterlineIdsArrayName)
123 {
124 vtkErrorMacro(<<"CenterlineIdsArrayName not specified");
125 return 1;
126 }
127
128 vtkDataArray* centerlineIdsArray = input->GetCellData()->GetArray(this->CenterlineIdsArrayName);
129
130 if (!centerlineIdsArray)
131 {
132 vtkErrorMacro(<<"CenterlineIdsArray with name specified does not exist");
133 return 1;
134 }
135
136 if (!this->TractIdsArrayName)
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
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 vtkCleanPolyData* cleaner = vtkCleanPolyData::New();
165 cleaner->SetInputData(input);
166 cleaner->Update();
167
168 if (this->ResamplingStepLength < 1E-12)
169 {
170 this->ResamplingStepLength = 0.01 * cleaner->GetOutput()->GetLength();
171 }
172
173 vtkSplineFilter* resampler = vtkSplineFilter::New();
174 resampler->SetInputConnection(cleaner->GetOutputPort());
175 resampler->SetSubdivideToLength();
176 resampler->SetLength(this->ResamplingStepLength);
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();
197 vtkvmtkCenterlineUtilities::GetNonBlankedGroupsIdList(resampledCenterlines,this->GroupIdsArrayName,this->BlankingArrayName,nonBlankedGroupIds);
198
199 vtkIdList* groupIdsToMergedCells = vtkIdList::New();
200 vtkIdType maxGroupId = vtkvmtkCenterlineUtilities::GetMaxGroupId(resampledCenterlines,this->GroupIdsArrayName);
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
216 vtkvmtkCenterlineUtilities::GetGroupUniqueCellIds(resampledCenterlines,this->GroupIdsArrayName,groupId,groupUniqueCellIds);
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
274 if (!this->MergeBlanked)
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);
287 referenceSystemsFilter->SetRadiusArrayName(this->RadiusArrayName);
288 referenceSystemsFilter->SetGroupIdsArrayName(this->GroupIdsArrayName);
289 referenceSystemsFilter->SetBlankingArrayName(this->BlankingArrayName);
290 referenceSystemsFilter->SetNormalArrayName("Normal");
291 referenceSystemsFilter->SetUpNormalArrayName("UpNormal");
292 referenceSystemsFilter->Update();
293
294 vtkPolyData* referenceSystems = referenceSystemsFilter->GetOutput();
295
296 int numberOfMergedCells = outputLines->GetNumberOfCells();
297
298 vtkIdList* blankedGroupIds = vtkIdList::New();
299 vtkvmtkCenterlineUtilities::GetBlankedGroupsIdList(resampledCenterlines,this->GroupIdsArrayName,this->BlankingArrayName,blankedGroupIds);
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
315 vtkIdType referenceSystemPointId = vtkvmtkReferenceSystemUtilities::GetReferenceSystemPointId(referenceSystems,this->GroupIdsArrayName,groupId);
316
317 vtkIdList* upStreamGroupIds = vtkIdList::New();
318 vtkIdList* downStreamGroupIds = vtkIdList::New();
319
320 vtkvmtkCenterlineUtilities::FindAdjacentCenterlineGroupIds(resampledCenterlines,this->GroupIdsArrayName,this->CenterlineIdsArrayName,this->TractIdsArrayName,groupId,upStreamGroupIds,downStreamGroupIds);
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();
339 vtkvmtkCenterlineUtilities::GetGroupUniqueCellIds(resampledCenterlines,this->GroupIdsArrayName,upStreamGroupIds->GetId(j),groupUniqueCellIds);
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();
360 vtkvmtkCenterlineUtilities::GetGroupUniqueCellIds(resampledCenterlines,this->GroupIdsArrayName,downStreamGroupIds->GetId(j),groupUniqueCellIds);
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 // TODO: interpolate point data instead of copying from first upstream point - good enough for now
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}
437
438void vtkvmtkMergeCenterlines::PrintSelf(std::ostream& os, vtkIndent indent)
439{
440 this->Superclass::PrintSelf(os,indent);
441}
Generates a normal and upnormal reference system for each bifurcation point of a split centerline.
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)
Combine multiple centerlines which lie within the same branch of a split and grouped centerline.
virtual int RequestData(vtkInformation *, vtkInformationVector **, vtkInformationVector *) override
void PrintSelf(std::ostream &os, vtkIndent indent) override
static vtkIdType GetReferenceSystemPointId(vtkPolyData *referenceSystems, const char *groupIdsArrayName, vtkIdType groupId)
vtkStandardNewMacro(vtkvmtkMergeCenterlines)