VMTK
Loading...
Searching...
No Matches
vtkvmtkPolyDataStretchMappingFilter.cxx
Go to the documentation of this file.
1/*=========================================================================
2
3 Program: VMTK
4 Module: $RCSfile: vtkvmtkPolyDataStretchMappingFilter.cxx,v $
5 Language: C++
6 Date: $Date: 2006/04/06 16:46:43 $
7 Version: $Revision: 1.11 $
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
23#include "vtkPolyData.h"
24#include "vtkPointData.h"
25#include "vtkCellData.h"
26#include "vtkDoubleArray.h"
27#include "vtkCellArray.h"
28#include "vtkContourFilter.h"
29#include "vtkStripper.h"
30// #include "vtkCardinalSpline.h"
31#include "vtkPiecewiseFunction.h"
32#include "vtkPolyLine.h"
33#include "vtkCleanPolyData.h"
34#include "vtkMath.h"
36#include "vtkvmtkConstants.h"
37#include "vtkInformation.h"
38#include "vtkInformationVector.h"
39#include "vtkObjectFactory.h"
40#include "vtkVersion.h"
41
43
44
46
48{
49 this->StretchedMappingArrayName = NULL;
50
51 this->HarmonicMappingArrayName = NULL;
52 this->GroupIdsArrayName = NULL;
53
54 this->MetricArrayName = NULL;
55 this->BoundaryMetricArrayName = NULL;
56
57 this->UseBoundaryMetric = 0;
58
59 this->MetricBoundsGapFactor = 2.0;
60}
61
63{
65 {
66 delete[] this->StretchedMappingArrayName;
67 this->StretchedMappingArrayName = NULL;
68 }
69
71 {
72 delete[] this->HarmonicMappingArrayName;
73 this->HarmonicMappingArrayName = NULL;
74 }
75
76 if (this->GroupIdsArrayName)
77 {
78 delete[] this->GroupIdsArrayName;
79 this->GroupIdsArrayName = NULL;
80 }
81
82 if (this->MetricArrayName)
83 {
84 delete[] this->MetricArrayName;
85 this->MetricArrayName = NULL;
86 }
87
89 {
90 delete[] this->BoundaryMetricArrayName;
91 this->BoundaryMetricArrayName = NULL;
92 }
93}
94
96 vtkInformation *vtkNotUsed(request),
97 vtkInformationVector **inputVector,
98 vtkInformationVector *outputVector)
99{
100 vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
101 vtkInformation *outInfo = outputVector->GetInformationObject(0);
102
103 vtkPolyData *input = vtkPolyData::SafeDownCast(
104 inInfo->Get(vtkDataObject::DATA_OBJECT()));
105 vtkPolyData *output = vtkPolyData::SafeDownCast(
106 outInfo->Get(vtkDataObject::DATA_OBJECT()));
107
108 if (!this->StretchedMappingArrayName)
109 {
110 vtkErrorMacro(<<"StretchedMappingArrayName not set.");
111 return 1;
112 }
113
114 if (!this->HarmonicMappingArrayName)
115 {
116 vtkErrorMacro(<<"HarmonicMappingArrayName not set.");
117 return 1;
118 }
119
120 vtkDataArray* harmonicMappingArray = input->GetPointData()->GetArray(this->HarmonicMappingArrayName);
121
122 if (!harmonicMappingArray)
123 {
124 vtkErrorMacro(<<"HarmonicMappingArrayName with name specified does not exist.");
125 return 1;
126 }
127
128 if (!this->GroupIdsArrayName)
129 {
130 vtkErrorMacro(<<"GroupIdsArrayName not set.");
131 return 1;
132 }
133
134 vtkDataArray* groupIdsArray = input->GetPointData()->GetArray(this->GroupIdsArrayName);
135
136 if (!groupIdsArray)
137 {
138 vtkErrorMacro(<<"GroupIdsArray with name specified does not exist.");
139 return 1;
140 }
141
142 if (!this->MetricArrayName)
143 {
144 vtkErrorMacro(<<"MetricArrayName not set.");
145 return 1;
146 }
147
148 vtkDataArray* metricArray = input->GetPointData()->GetArray(this->MetricArrayName);
149
150 if (!metricArray)
151 {
152 vtkErrorMacro(<<"MetricArrayName with name specified does not exist.");
153 return 1;
154 }
155
156 vtkDataArray* boundaryMetricArray = NULL;
157 if (this->UseBoundaryMetric)
158 {
159 if (!this->BoundaryMetricArrayName)
160 {
161 vtkErrorMacro(<<"BoundaryMetricArrayName not set.");
162 return 1;
163 }
164
165 boundaryMetricArray = input->GetPointData()->GetArray(this->BoundaryMetricArrayName);
166
167 if (!boundaryMetricArray)
168 {
169 vtkErrorMacro(<<"BoundaryMetricArrayName with name specified does not exist.");
170 return 1;
171 }
172 }
173
174 int numberOfInputPoints = input->GetNumberOfPoints();
175
176 output->DeepCopy(input);
177
178 vtkDoubleArray* stretchedMapping = vtkDoubleArray::New();
179 stretchedMapping->SetName(this->StretchedMappingArrayName);
180 stretchedMapping->SetNumberOfComponents(1);
181 stretchedMapping->SetNumberOfTuples(numberOfInputPoints);
182
183 output->GetPointData()->AddArray(stretchedMapping);
184
185 vtkIdList* groupIds = vtkIdList::New();
187
188 int i, j, k;
189 for (i=0; i<groupIds->GetNumberOfIds(); i++)
190 {
191 vtkIdType groupId = groupIds->GetId(i);
192 vtkPolyData* cylinder = vtkPolyData::New();
193 vtkvmtkPolyDataBranchUtilities::ExtractGroup(input,this->GroupIdsArrayName,groupId,true,cylinder);
194 cylinder->GetPointData()->SetActiveScalars(this->HarmonicMappingArrayName);
195
196 // before contouring, extract boundaries and look for boundary values there if UseBoundaryValues is 1
197
198 vtkDataArray* cylinderHarmonicMappingArray = cylinder->GetPointData()->GetArray(this->HarmonicMappingArrayName);
199 vtkDataArray* cylinderMetricArray = cylinder->GetPointData()->GetArray(this->MetricArrayName);
200 vtkDataArray* cylinderBoundaryMetricArray = cylinder->GetPointData()->GetArray(this->BoundaryMetricArrayName);
201
202 double boundaryMappings[2];
203 double boundaryMetrics[2];
204 double boundaryMetricUsefulBounds[2];
205
206 // extract boundaries and look at values there.
208 boundaryExtractor->SetInputData(cylinder);
209 boundaryExtractor->Update();
210
211 int numberOfBoundaries = boundaryExtractor->GetOutput()->GetNumberOfCells();
212 if (numberOfBoundaries != 2)
213 {
214 vtkErrorMacro(<<"Branch not topologically a cylinder.");
215 return 1;
216 }
217
218 double contourMetricBounds[2][2];
219 contourMetricBounds[0][0] = VTK_DOUBLE_MAX;
220 contourMetricBounds[0][1] = VTK_DOUBLE_MIN;
221 contourMetricBounds[1][0] = VTK_DOUBLE_MAX;
222 contourMetricBounds[1][1] = VTK_DOUBLE_MIN;
223 for (j=0; j<numberOfBoundaries; j++)
224 {
225 vtkCell* boundary = boundaryExtractor->GetOutput()->GetCell(j);
226 if (boundary->GetNumberOfPoints() == 0)
227 {
228 vtkErrorMacro(<<"Degenerate branch found.");
229 return 1;
230 }
231 vtkDataArray* boundaryPointIds = boundaryExtractor->GetOutput()->GetPointData()->GetScalars();
232 vtkIdType boundaryPointId = static_cast<int>(boundaryPointIds->GetComponent(boundary->GetPointId(0),0));
233 boundaryMappings[j] = cylinderHarmonicMappingArray->GetComponent(boundaryPointId,0);
235 {
236 boundaryMetrics[j] = cylinderBoundaryMetricArray->GetComponent(boundaryPointId,0);
237 }
238
239 int numberOfBoundaryPoints = boundary->GetNumberOfPoints();
240 for (k=0; k<numberOfBoundaryPoints; k++)
241 {
242 boundaryPointId = static_cast<int>(boundaryPointIds->GetComponent(boundary->GetPointId(k),0));
243 double metricValue = cylinderMetricArray->GetComponent(boundaryPointId,0);
244 if (metricValue < contourMetricBounds[j][0])
245 {
246 contourMetricBounds[j][0] = metricValue;
247 }
248 if (metricValue > contourMetricBounds[j][1])
249 {
250 contourMetricBounds[j][1] = metricValue;
251 }
252 }
253 }
254
255 if (boundaryMetrics[0] > boundaryMetrics[1])
256 {
257 double temp;
258 temp = boundaryMappings[1];
259 boundaryMappings[1] = boundaryMappings[0];
260 boundaryMappings[0] = temp;
261 temp = boundaryMetrics[1];
262 boundaryMetrics[1] = boundaryMetrics[0];
263 boundaryMetrics[0] = temp;
264 temp = contourMetricBounds[1][0];
265 contourMetricBounds[1][0] = contourMetricBounds[0][0];
266 contourMetricBounds[0][0] = temp;
267 temp = contourMetricBounds[1][1];
268 contourMetricBounds[1][1] = contourMetricBounds[0][1];
269 contourMetricBounds[0][1] = temp;
270 }
271
272// boundaryMetricUsefulBounds[0] = contourMetricBounds[0][1];
273// boundaryMetricUsefulBounds[1] = contourMetricBounds[1][0];
274
275 boundaryMetricUsefulBounds[0] = contourMetricBounds[0][0] + this->MetricBoundsGapFactor * (contourMetricBounds[0][1] - contourMetricBounds[0][0]);
276 boundaryMetricUsefulBounds[1] = contourMetricBounds[1][1] - this->MetricBoundsGapFactor * (contourMetricBounds[1][1] - contourMetricBounds[1][0]);
277
278 if (!this->UseBoundaryMetric)
279 {
280 boundaryMetrics[0] = contourMetricBounds[0][0];
281 boundaryMetrics[1] = contourMetricBounds[1][1];
282 }
283
284 boundaryExtractor->Delete();
285
286 vtkContourFilter* contourFilter = vtkContourFilter::New();
287 contourFilter->SetInputData(cylinder);
288 contourFilter->SetValue(0,0.5);
289 contourFilter->Update();
290
291 int numberOfContours = static_cast<int>(10.0 * fabs(boundaryMetrics[1] - boundaryMetrics[0]) / (contourFilter->GetOutput()->GetLength()/2.0));
292
293 double interval = 1.0/static_cast<double>(numberOfContours);
294 for (j=0; j<=numberOfContours; j++)
295 {
296 contourFilter->SetValue(j,static_cast<double>(j) * interval);
297 }
298 contourFilter->Update();
299
300 vtkStripper* contourStripper = vtkStripper::New();
301 contourStripper->SetInputConnection(contourFilter->GetOutputPort());
302 contourStripper->Update();
303
304 vtkPolyData* contours = contourStripper->GetOutput();
305
306// vtkCardinalSpline* stretchFunction = vtkCardinalSpline::New();
307 vtkPiecewiseFunction* stretchFunction = vtkPiecewiseFunction::New();
308
309 int numberOfComputedContours = contours->GetNumberOfCells();
310
311 vtkDataArray* contourMetricArray = contours->GetPointData()->GetArray(this->MetricArrayName);
312
313 for (j=0; j<numberOfComputedContours; j++)
314 {
315 vtkPolyLine* contour = vtkPolyLine::SafeDownCast(contours->GetCell(j));
316
317 if (!contour)
318 {
319 continue;
320 }
321
322 double harmonicMappingValue = contours->GetPointData()->GetScalars()->GetComponent(contour->GetPointId(0),0);
323
324 int numberOfContourPoints = contour->GetNumberOfPoints();
325
326 double contourLength = 0.0;
327 double metricIntegral = 0.0;
328
329 for (k=0; k<numberOfContourPoints; k++)
330 {
331 int pointId0 = contour->GetPointId(k);
332 int pointId1 = contour->GetPointId((k+1)%numberOfContourPoints);
333
334 double point0[3];
335 double point1[3];
336
337 contours->GetPoint(pointId0,point0);
338 contours->GetPoint(pointId1,point1);
339
340 double length = sqrt(vtkMath::Distance2BetweenPoints(point0,point1));
341
342 if (length < VTK_VMTK_DOUBLE_TOL)
343 {
344 continue;
345 }
346
347 contourLength += length;
348
349 double metricValue0 = contourMetricArray->GetComponent(pointId0,0);
350 double metricValue1 = contourMetricArray->GetComponent(pointId1,0);
351
352 metricIntegral += length * 0.5 * (metricValue0 + metricValue1);
353 }
354
355 if (contourLength < VTK_VMTK_DOUBLE_TOL)
356 {
357 continue;
358 }
359
360 double metricMean = metricIntegral / contourLength;
361
362// if (metricMean < boundaryMetricUsefulBounds[0] || metricMean > boundaryMetricUsefulBounds[1])
363// {
364// continue;
365// }
366
367 if (metricMean < boundaryMetricUsefulBounds[0] || metricMean > boundaryMetricUsefulBounds[1])
368 {
369 continue;
370 }
371
372 stretchFunction->AddPoint(harmonicMappingValue, metricMean);
373 }
374
375 stretchFunction->AddPoint(boundaryMappings[0], boundaryMetrics[0]);
376 stretchFunction->AddPoint(boundaryMappings[1], boundaryMetrics[1]);
377
378// derivative = (stretchFunction->Evaluate(blendingSkip) - stretchFunction->Evaluate(0.0)) / blendingSkip;
379// stretchFunction->AddPoint(-blendingSkip, stretchFunction->Evaluate(0.0) - derivative * blendingSkip);
380
381// derivative = (stretchFunction->Evaluate(1.0) - stretchFunction->Evaluate(1.0-interval)) / interval;
382// stretchFunction->AddPoint(1.0+blendingSkip, stretchFunction->Evaluate(1.0) + derivative * blendingSkip);
383
384 for (j=0; j<numberOfInputPoints; j++)
385 {
386 vtkIdType currentGroupId = static_cast<int>(groupIdsArray->GetComponent(j,0));
387
388 if (currentGroupId != groupId)
389 {
390 continue;
391 }
392
393 double harmonicMappingValue = harmonicMappingArray->GetComponent(j,0);
394
395// double stretchedMappingValue = stretchFunction->Evaluate(harmonicMappingValue);
396 double stretchedMappingValue = stretchFunction->GetValue(harmonicMappingValue);
397
398 stretchedMapping->SetComponent(j,0,stretchedMappingValue);
399 }
400
401 stretchFunction->Delete();
402 contourFilter->Delete();
403 contourStripper->Delete();
404 cylinder->Delete();
405 }
406
407 groupIds->Delete();
408 stretchedMapping->Delete();
409
410 return 1;
411}
Extract boundary edges as poly lines.
static vtkvmtkPolyDataBoundaryExtractor * New()
static void ExtractGroup(vtkPolyData *surface, const char *groupIdsArrayName, vtkIdType groupId, bool cleanGroupSurface, vtkPolyData *groupSurface)
static void GetGroupsIdList(vtkPolyData *surface, const char *groupIdsArrayName, vtkIdList *groupIds)
Reparametrize a harmonic longitudinal mapping (0 to 1 per branch) into a "stretched" mapping proporti...
virtual int RequestData(vtkInformation *, vtkInformationVector **, vtkInformationVector *) override
#define VTK_VMTK_DOUBLE_TOL
vtkStandardNewMacro(vtkvmtkPolyDataStretchMappingFilter)