VMTK
Loading...
Searching...
No Matches
vtkvmtkBoundaryReferenceSystems.cxx
Go to the documentation of this file.
1/*=========================================================================
2
3Program: VMTK
4Module: $RCSfile: vtkvmtkBoundaryReferenceSystems.cxx,v $
5Language: C++
6Date: $Date: 2006/04/06 16:46:43 $
7Version: $Revision: 1.6 $
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 "vtkPolyData.h"
25#include "vtkCellArray.h"
26#include "vtkDoubleArray.h"
27#include "vtkMath.h"
28#include "vtkPointData.h"
29#include "vtkPolyLine.h"
30#include "vtkvmtkConstants.h"
31#include "vtkInformation.h"
32#include "vtkInformationVector.h"
33#include "vtkObjectFactory.h"
34#include "vtkVersion.h"
35
36
38
46
48{
50 {
51 delete[] this->BoundaryRadiusArrayName;
52 this->BoundaryRadiusArrayName = NULL;
53 }
54
56 {
57 delete[] this->BoundaryNormalsArrayName;
58 this->BoundaryNormalsArrayName = NULL;
59 }
60
61 if (this->Point1ArrayName)
62 {
63 delete[] this->Point1ArrayName;
64 this->Point1ArrayName = NULL;
65 }
66
67 if (this->Point2ArrayName)
68 {
69 delete[] this->Point2ArrayName;
70 this->Point2ArrayName = NULL;
71 }
72}
73
74void vtkvmtkBoundaryReferenceSystems::ComputeBoundaryBarycenter(vtkPoints* points, double* barycenter)
75{
76 int numberOfPoints;
77 double weightSum, weight;
78 vtkIdType i;
79
80 // accounts for edge length
81
82 numberOfPoints = points->GetNumberOfPoints();
83
84 weightSum = 0.0;
85
86 barycenter[0] = 0.0;
87 barycenter[1] = 0.0;
88 barycenter[2] = 0.0;
89
90 double point0[3], point1[3];
91
92 for (i=0; i<numberOfPoints; i++)
93 {
94 points->GetPoint(i,point0);
95 points->GetPoint((i+1)%numberOfPoints,point1);
96 weight = sqrt(vtkMath::Distance2BetweenPoints(point0,point1));
97 if (weight < VTK_VMTK_DOUBLE_TOL)
98 {
99 continue;
100 }
101 weightSum += weight;
102
103 barycenter[0] += (point0[0] + point1[0])/2.0 * weight;
104 barycenter[1] += (point0[1] + point1[1])/2.0 * weight;
105 barycenter[2] += (point0[2] + point1[2])/2.0 * weight;
106 }
107
108 barycenter[0] /= weightSum;
109 barycenter[1] /= weightSum;
110 barycenter[2] /= weightSum;
111}
112
113double vtkvmtkBoundaryReferenceSystems::ComputeBoundaryMeanRadius(vtkPoints* points, double* barycenter)
114{
115 int numberOfPoints = points->GetNumberOfPoints();
116
117 // TODO: weight by edge length as for barycenter
118
119 double meanRadius = 0.0;
120
121 for (int i=0; i<numberOfPoints; i++)
122 {
123 meanRadius += sqrt(vtkMath::Distance2BetweenPoints(points->GetPoint(i),barycenter));
124 }
125
126 meanRadius /= numberOfPoints;
127
128 return meanRadius;
129}
130
131void vtkvmtkBoundaryReferenceSystems::ComputeBoundaryNormal(vtkPoints* points, double* barycenter, double* normal)
132{
133 int numberOfPoints;
134 double point[3], vector1[3], vector0[3], cross[3], sumCross[3] = {0.0f,0.0f,0.0f};
135 vtkIdType i, j;
136
137 // TODO: weight by edge length as for barycenter
138
139 numberOfPoints = points->GetNumberOfPoints();
140
141 points->GetPoint(numberOfPoints-1,point);
142 vector0[0] = point[0] - barycenter[0];
143 vector0[1] = point[1] - barycenter[1];
144 vector0[2] = point[2] - barycenter[2];
145
146 for (i=0; i<numberOfPoints; i++)
147 {
148 points->GetPoint(i,point);
149 vector1[0] = point[0] - barycenter[0];
150 vector1[1] = point[1] - barycenter[1];
151 vector1[2] = point[2] - barycenter[2];
152
153 vtkMath::Cross(vector1,vector0,cross);
154
155 for (j=0; j<3; j++)
156 {
157 vector0[j] = vector1[j];
158 sumCross[j] += cross[j];
159 }
160 }
161
162 vtkMath::Normalize(sumCross);
163
164 for (j=0; j<3; j++)
165 {
166 normal[j] = sumCross[j];
167 }
168}
169
170void vtkvmtkBoundaryReferenceSystems::OrientBoundaryNormalOutwards(vtkPolyData* surface, vtkPolyData* boundaries, vtkIdType boundaryCellId, double normal[3], double outwardNormal[3])
171{
172 vtkPolyLine* boundary = vtkPolyLine::SafeDownCast(boundaries->GetCell(boundaryCellId));
173
174 int k=0;
175 if (!boundary)
176 {
177 for (k=0; k<3; k++)
178 {
179 outwardNormal[k] = normal[k];
180 }
181 return;
182 }
183
184 surface->BuildCells();
185 surface->BuildLinks();
186
187 vtkIdType *cells;
188 vtkIdType npts;
189 const vtkIdType *pts;
190 vtkIdType ncells;
191
192 double boundaryPoint[3], neighborPoint[3];
193
194 int numberOfBoundaryPoints = boundary->GetNumberOfPoints();
195
196 int j;
197 vtkIdList* boundaryIds = vtkIdList::New();
198 for (j=0; j<numberOfBoundaryPoints; j++)
199 {
200 boundaryIds->InsertNextId(static_cast<vtkIdType>(vtkMath::Round(boundaries->GetPointData()->GetScalars()->GetComponent(boundary->GetPointId(j),0))));
201 }
202
203 double neighborsToBoundaryNormal[3];
204 neighborsToBoundaryNormal[0] = neighborsToBoundaryNormal[1] = neighborsToBoundaryNormal[2] = 0.0;
205 for (j=0; j<numberOfBoundaryPoints; j++)
206 {
207 surface->GetPointCells(boundaryIds->GetId(j),ncells,cells);
208 for (int c=0; c<ncells; c++)
209 {
210 surface->GetCellPoints(cells[c],npts,pts);
211 for (int p=0; p<npts; p++)
212 {
213 if (boundaryIds->IsId(pts[p]) == -1)
214 {
215 surface->GetPoint(boundaryIds->GetId(j),boundaryPoint);
216 surface->GetPoint(pts[p],neighborPoint);
217 for (k=0; k<3; k++)
218 {
219 neighborsToBoundaryNormal[k] += boundaryPoint[k] - neighborPoint[k];
220 }
221 }
222 }
223 }
224 }
225
226 for (k=0; k<3; k++)
227 {
228 outwardNormal[k] = normal[k];
229 }
230
231 vtkMath::Normalize(neighborsToBoundaryNormal);
232
233 if (vtkMath::Dot(normal,neighborsToBoundaryNormal) < 0.0)
234 {
235 for (k=0; k<3; k++)
236 {
237 outwardNormal[k] *= -1.0;
238 }
239 }
240
241 boundaryIds->Delete();
242}
243
244void vtkvmtkBoundaryReferenceSystems::ComputeReferenceSystemPoints(double origin[3], double normal[3], double radius, double point1[3], double point2[3])
245{
246 point1[0] = point1[1] = point1[2] = 0.0;
247 point2[0] = point2[1] = point2[2] = 0.0;
248
249 double x_p[3], y_p[3], x[3];
250
251 x[0] = 1.0;
252 x[1] = 0.0;
253 x[2] = 0.0;
254
255 vtkMath::Cross(normal,x,x_p);
256 vtkMath::Normalize(x_p);
257
258 vtkMath::Cross(normal,x_p,y_p);
259 vtkMath::Normalize(y_p);
260
261 for (int i=0; i<3; i++)
262 {
263 point1[i] = origin[i] + x_p[i] * radius;
264 point2[i] = origin[i] + y_p[i] * radius;
265 }
266}
267
269 vtkInformation *vtkNotUsed(request),
270 vtkInformationVector **inputVector,
271 vtkInformationVector *outputVector)
272{
273 // get the info objects
274 vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
275 vtkInformation *outInfo = outputVector->GetInformationObject(0);
276
277 // get the input and output
278 vtkPolyData *input = vtkPolyData::SafeDownCast(
279 inInfo->Get(vtkDataObject::DATA_OBJECT()));
280 vtkPolyData *output = vtkPolyData::SafeDownCast(
281 outInfo->Get(vtkDataObject::DATA_OBJECT()));
282
283 if (!this->BoundaryRadiusArrayName)
284 {
285 vtkErrorMacro(<< "BoundaryRadiusArrayName not specified.");
286 return 1;
287 }
288
289 if (!this->BoundaryNormalsArrayName)
290 {
291 vtkErrorMacro(<< "BoundaryNormalsArrayName not specified.");
292 return 1;
293 }
294
295 if (!this->Point1ArrayName)
296 {
297 vtkErrorMacro(<< "Point1ArrayName not specified.");
298 return 1;
299 }
300
301 if (!this->Point2ArrayName)
302 {
303 vtkErrorMacro(<< "Point2ArrayName not specified.");
304 return 1;
305 }
306
307 vtkPoints* outputPoints = vtkPoints::New();
308 vtkCellArray* outputVerts = vtkCellArray::New();
309 vtkDoubleArray* outputRadius = vtkDoubleArray::New();
310 outputRadius->SetName(this->BoundaryRadiusArrayName);
311 vtkDoubleArray* outputNormals = vtkDoubleArray::New();
312 outputNormals->SetName(this->BoundaryNormalsArrayName);
313 outputNormals->SetNumberOfComponents(3);
314 vtkDoubleArray* point1Array = vtkDoubleArray::New();
315 point1Array->SetName(this->Point1ArrayName);
316 point1Array->SetNumberOfComponents(3);
317 vtkDoubleArray* point2Array = vtkDoubleArray::New();
318 point2Array->SetName(this->Point2ArrayName);
319 point2Array->SetNumberOfComponents(3);
320
322 boundaryExtractor->SetInputData(input);
323
324 boundaryExtractor->Update();
325
326 vtkPolyData* boundaries = boundaryExtractor->GetOutput();
327
328 double barycenter[3], normal[3], outwardNormal[3], meanRadius;
329
330 for (int i=0; i<boundaries->GetNumberOfCells(); i++)
331 {
332 vtkPolyLine* boundary = vtkPolyLine::SafeDownCast(boundaries->GetCell(i));
333
334 if (!boundary)
335 {
336 vtkErrorMacro(<<"Boundary not a vtkPolyLine");
337 continue;
338 }
339
340 this->ComputeBoundaryBarycenter(boundary->GetPoints(),barycenter);
341 meanRadius = this->ComputeBoundaryMeanRadius(boundary->GetPoints(),barycenter);
342 this->ComputeBoundaryNormal(boundary->GetPoints(),barycenter,normal);
343 this->OrientBoundaryNormalOutwards(input,boundaries,i,normal,outwardNormal);
344 double point1[3], point2[3];
345 this->ComputeReferenceSystemPoints(barycenter,normal,meanRadius,point1,point2);
346
347 vtkIdType pointId = outputPoints->InsertNextPoint(barycenter);
348 outputVerts->InsertNextCell(1);
349 outputVerts->InsertCellPoint(pointId);
350 outputNormals->InsertNextTuple(outwardNormal);
351 outputRadius->InsertNextValue(meanRadius);
352 point1Array->InsertNextTuple(point1);
353 point2Array->InsertNextTuple(point2);
354 }
355
356 output->SetPoints(outputPoints);
357 output->SetVerts(outputVerts);
358 output->GetPointData()->AddArray(outputNormals);
359 output->GetPointData()->SetActiveNormals(this->BoundaryNormalsArrayName);
360 output->GetPointData()->AddArray(outputRadius);
361 output->GetPointData()->SetActiveScalars(this->BoundaryRadiusArrayName);
362 output->GetPointData()->AddArray(point1Array);
363 output->GetPointData()->AddArray(point2Array);
364
365 outputPoints->Delete();
366 outputVerts->Delete();
367 outputNormals->Delete();
368 outputRadius->Delete();
369 point1Array->Delete();
370 point2Array->Delete();
371 boundaryExtractor->Delete();
372
373 return 1;
374}
375
376void vtkvmtkBoundaryReferenceSystems::PrintSelf(std::ostream& os, vtkIndent indent)
377{
378 this->Superclass::PrintSelf(os,indent);
379}
Creates a reference system consisting of a radius and normals on top of boundaries.
virtual int RequestData(vtkInformation *, vtkInformationVector **, vtkInformationVector *) override
static void ComputeBoundaryBarycenter(vtkPoints *points, double barycenter[3])
static void OrientBoundaryNormalOutwards(vtkPolyData *surface, vtkPolyData *boundaries, vtkIdType boundaryCellId, double normal[3], double outwardNormal[3])
static void ComputeBoundaryNormal(vtkPoints *points, double barycenter[3], double normal[3])
static void ComputeReferenceSystemPoints(double origin[3], double normal[3], double radius, double point1[3], double point2[3])
static double ComputeBoundaryMeanRadius(vtkPoints *points, double barycenter[3])
void PrintSelf(std::ostream &os, vtkIndent indent) override
Extract boundary edges as poly lines.
static vtkvmtkPolyDataBoundaryExtractor * New()
vtkStandardNewMacro(vtkvmtkBoundaryReferenceSystems)
#define VTK_VMTK_DOUBLE_TOL