83{
84 vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
85 vtkInformation *outInfo = outputVector->GetInformationObject(0);
86
87 vtkPolyData *input = vtkPolyData::SafeDownCast(
88 inInfo->Get(vtkDataObject::DATA_OBJECT()));
89 vtkPolyData *output = vtkPolyData::SafeDownCast(
90 outInfo->Get(vtkDataObject::DATA_OBJECT()));
91
92 if ( input->GetNumberOfPoints() < 1 )
93 {
94 return 1;
95 }
96
97 input->BuildCells();
98 input->BuildLinks();
99
100 vtkPoints* newPoints = vtkPoints::New();
101 newPoints->DeepCopy(input->GetPoints());
102
103 vtkCellArray* newPolys = vtkCellArray::New();
104 newPolys->DeepCopy(input->GetPolys());
105
106
107 vtkIdTypeArray* cellEntityIdsArray = NULL;
109 if (markCells)
110 {
111 cellEntityIdsArray = vtkIdTypeArray::New();
113 if (input->GetCellData()->GetArray(this->CellEntityIdsArrayName))
114 {
115 cellEntityIdsArray->DeepCopy(input->GetCellData()->GetArray(this->CellEntityIdsArrayName));
116 }
117 else
118 {
119 cellEntityIdsArray->SetNumberOfTuples(newPolys->GetNumberOfCells());
121 }
122 }
123
124
125
126
127 vtkNew<vtkPolyData> boundaries;
128 vtkNew<vtkIdList> boundaryLabels;
131
132 int boundaryId;
133 for (boundaryId=0; boundaryId<boundaries->GetNumberOfCells(); boundaryId++)
134 {
135
136
137 vtkIdType capBoundaryId = useBoundaryLabels ? boundaryLabels->GetId(boundaryId) : boundaryId;
139 {
141 {
142 continue;
143 }
144 }
145
146 vtkPolyLine* boundaryCell = vtkPolyLine::SafeDownCast(boundaries->GetCell(boundaryId));
147
148 int numberOfBoundaryPoints = boundaryCell->GetNumberOfPoints();
149 double boundaryDiagonal = sqrt(boundaryCell->GetLength2());
150
151 vtkIdList* boundaryPointIds = vtkIdList::New();
152 int i;
153 for (i=0; i<numberOfBoundaryPoints; i++)
154 {
155 vtkIdType boundaryPointId = static_cast<vtkIdType>(boundaries->GetPointData()->GetScalars()->GetComponent(boundaryCell->GetPointId(i),0));
156 boundaryPointIds->InsertNextId(boundaryPointId);
157 }
158
159 vtkPoints* outerRingPoints = vtkPoints::New();
160 vtkIdList* pointCells = vtkIdList::New();
161 vtkIdList* cellPoints = vtkIdList::New();
162 for (i=0; i<numberOfBoundaryPoints; i++)
163 {
164 vtkIdType boundaryPointId = boundaryPointIds->GetId(i);
165 pointCells->Initialize();
166 input->GetPointCells(boundaryPointId,pointCells);
167 double outerRingPoint[3];
168 outerRingPoint[0] = outerRingPoint[1] = outerRingPoint[2] = 0.0;
169 double areaSum = 0.0;
170 int j;
171 for (j=0; j<pointCells->GetNumberOfIds(); j++)
172 {
173 vtkIdType cellId = pointCells->GetId(j);
174 input->GetCellPoints(cellId,cellPoints);
175
176 double point0[3], point1[3], point2[3];
177 input->GetPoint(cellPoints->GetId(0),point0);
178 input->GetPoint(cellPoints->GetId(1),point1);
179 input->GetPoint(cellPoints->GetId(2),point2);
180 double triangleCenter[3];
181 vtkTriangle::TriangleCenter(point0,point1,point2,triangleCenter);
182 double triangleArea = vtkTriangle::TriangleArea(point0,point1,point2);
183 outerRingPoint[0] += triangleArea * triangleCenter[0];
184 outerRingPoint[1] += triangleArea * triangleCenter[1];
185 outerRingPoint[2] += triangleArea * triangleCenter[2];
186 areaSum += triangleArea;
187 }
188 outerRingPoint[0] /= areaSum;
189 outerRingPoint[1] /= areaSum;
190 outerRingPoint[2] /= areaSum;
191 double boundaryPoint[3];
192 input->GetPoint(boundaryPointId,boundaryPoint);
193 double boundaryVector[3];
194 boundaryVector[0] = outerRingPoint[0] - boundaryPoint[0];
195 boundaryVector[1] = outerRingPoint[1] - boundaryPoint[1];
196 boundaryVector[2] = outerRingPoint[2] - boundaryPoint[2];
197 vtkMath::Normalize(boundaryVector);
199 outerRingPoint[0] = distance * boundaryVector[0] + boundaryPoint[0];
200 outerRingPoint[1] = distance * boundaryVector[1] + boundaryPoint[1];
201 outerRingPoint[2] = distance * boundaryVector[2] + boundaryPoint[2];
202 outerRingPoints->InsertNextPoint(outerRingPoint);
203 }
204
205 pointCells->Delete();
206 cellPoints->Delete();
207
208 vtkThinPlateSplineTransform* thinPlateSplineTransform = vtkThinPlateSplineTransform::New();
209 thinPlateSplineTransform->SetBasisToR2LogR();
210 thinPlateSplineTransform->SetSigma(1.0);
211 vtkPoints* sourceLandmarks = vtkPoints::New();
212 vtkPoints* targetLandmarks = vtkPoints::New();
213
214 for (i=0; i<numberOfBoundaryPoints; i++)
215 {
216 double angle = (double)i / (double)numberOfBoundaryPoints * 2.0*vtkMath::Pi();
217 double sourcePoint[3];
218 sourcePoint[0] = cos(angle);
219 sourcePoint[1] = sin(angle);
220 sourcePoint[2] = 0.0;
221 sourceLandmarks->InsertNextPoint(sourcePoint);
222 targetLandmarks->InsertNextPoint(boundaryCell->GetPoints()->GetPoint(i));
223 double outerRingRadius = 1.25;
224 sourcePoint[0] = outerRingRadius * cos(angle);
225 sourcePoint[1] = outerRingRadius * sin(angle);
226 sourcePoint[2] = 0.0;
227 sourceLandmarks->InsertNextPoint(sourcePoint);
228 targetLandmarks->InsertNextPoint(outerRingPoints->GetPoint(i));
229 }
230
231 thinPlateSplineTransform->SetSourceLandmarks(sourceLandmarks);
232 thinPlateSplineTransform->SetTargetLandmarks(targetLandmarks);
233
234 outerRingPoints->Delete();
235
236 vtkIdList* circlePointIds = vtkIdList::New();
237 vtkIdList* previousCirclePointIds = vtkIdList::New();
238
239 circlePointIds->DeepCopy(boundaryPointIds);
240
241 int k;
243 {
244 previousCirclePointIds->DeepCopy(circlePointIds);
245 circlePointIds->Initialize();
246 for (i=0; i<numberOfBoundaryPoints; i++)
247 {
248 double angle = (double)i / (double)numberOfBoundaryPoints * 2.0*vtkMath::Pi();
250 double sourcePoint[3];
251 sourcePoint[0] = radius * cos(angle);
252 sourcePoint[1] = radius * sin(angle);
253 sourcePoint[2] = 0.0;
254 double transformedPoint[3];
255 thinPlateSplineTransform->TransformPoint(sourcePoint,transformedPoint);
256 vtkIdType circlePointId = newPoints->InsertNextPoint(transformedPoint);
257 circlePointIds->InsertNextId(circlePointId);
258 }
259 for (i=0; i<numberOfBoundaryPoints; i++)
260 {
261
262
263
264
265
266
267 vtkIdType previousId = previousCirclePointIds->GetId(i);
268 vtkIdType previousNextId = previousCirclePointIds->GetId((i+1)%numberOfBoundaryPoints);
269 vtkIdType currentId = circlePointIds->GetId(i);
270 vtkIdType currentNextId = circlePointIds->GetId((i+1)%numberOfBoundaryPoints);
271
272 vtkIdType firstTriangle[3] = {previousId, previousNextId, currentId};
273 vtkIdType secondTriangle[3] = {previousNextId, currentNextId, currentId};
274 newPolys->InsertNextCell(3,firstTriangle);
275 newPolys->InsertNextCell(3,secondTriangle);
276 if (markCells)
277 {
278 vtkIdType capCellEntityId = this->
CapCellEntityId(boundaryId,capBoundaryId,useBoundaryLabels);
279 cellEntityIdsArray->InsertNextValue(capCellEntityId);
280 cellEntityIdsArray->InsertNextValue(capCellEntityId);
281 }
282 }
283 }
284
285
286
287
288
289 double tipCentre[3] = {0.0, 0.0, 0.0};
290 vtkIdType numberOfTipPoints = circlePointIds->GetNumberOfIds();
291 for (i=0; i<numberOfTipPoints; i++)
292 {
293 double tipPoint[3];
294 newPoints->GetPoint(circlePointIds->GetId(i),tipPoint);
295 for (int component=0; component<3; component++)
296 {
297 tipCentre[component] += tipPoint[component];
298 }
299 }
300 for (int component=0; component<3; component++)
301 {
302 tipCentre[component] /= static_cast<double>(numberOfTipPoints);
303 }
304 vtkIdType tipCentreId = newPoints->InsertNextPoint(tipCentre);
305 for (i=0; i<numberOfTipPoints; i++)
306 {
307 vtkIdType tipTriangle[3] = {circlePointIds->GetId(i),
308 circlePointIds->GetId((i+1)%numberOfTipPoints),
309 tipCentreId};
310 newPolys->InsertNextCell(3,tipTriangle);
311 if (markCells)
312 {
313 cellEntityIdsArray->InsertNextValue(this->
CapCellEntityId(boundaryId,capBoundaryId,useBoundaryLabels));
314 }
315 }
316
317 boundaryPointIds->Delete();
318
319 circlePointIds->Delete();
320 previousCirclePointIds->Delete();
321
322 thinPlateSplineTransform->Delete();
323 sourceLandmarks->Delete();
324 targetLandmarks->Delete();
325 }
326
327 output->SetPoints(newPoints);
328 output->SetPolys(newPolys);
329
330 if (markCells)
331 {
332 output->GetCellData()->AddArray(cellEntityIdsArray);
333 cellEntityIdsArray->Delete();
334 }
335
336 newPoints->Delete();
337 newPolys->Delete();
338
339 return 1;
340}
static bool GetOrExtractBoundaries(vtkPolyData *surface, const char *boundaryLabelsArrayName, const char *boundaryPointOrderArrayName, vtkPolyData *boundaries, vtkIdList *boundaryLabels, vtkObject *warningSource=nullptr)
vtkIdType CapCellEntityId(vtkIdType boundaryIndex, vtkIdType boundaryId, bool useBoundaryLabels)