VMTK
Loading...
Searching...
No Matches
vtkvmtkUnstructuredGridCenterlineSections.cxx
Go to the documentation of this file.
1/*=========================================================================
2
3Program: VMTK
4Module: $RCSfile: vtkvmtkUnstructuredGridCenterlineSections.cxx,v $
5Language: C++
6Date: $Date: 2006/10/17 15:16:16 $
7Version: $Revision: 1.1 $
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 "vtkUnstructuredGrid.h"
24#include "vtkPolyLine.h"
25#include "vtkPointData.h"
26#include "vtkCellData.h"
27#include "vtkCellArray.h"
28#include "vtkPlane.h"
29#include "vtkCutter.h"
30#include "vtkStripper.h"
31#include "vtkPolyDataConnectivityFilter.h"
32#include "vtkMath.h"
33#include "vtkCleanPolyData.h"
34#include "vtkAppendPolyData.h"
35#include "vtkTransform.h"
36#include "vtkTransformPolyDataFilter.h"
37#include "vtkDoubleArray.h"
38#include "vtkvmtkMath.h"
40#include "vtkProbeFilter.h"
41#include "vtkIdTypeArray.h"
42#include "vtkInformation.h"
43#include "vtkInformationVector.h"
44#include "vtkObjectFactory.h"
45#include "vtkVersion.h"
46
49
50
52
54{
55 this->Centerlines = NULL;
56 this->SectionSource = NULL;
57 this->SectionPointsPolyData = NULL;
58 this->SectionUpNormalsArrayName = NULL;
59 this->SectionNormalsArrayName = NULL;
60 this->AdditionalNormalsArrayName = NULL;
61 this->AdditionalScalarsArrayName = NULL;
62 this->SectionIdsArrayName = NULL;
63 this->TransformSections = 0;
64 this->UseSectionSource = 0;
65 this->SourceScaling = 0;
66 this->OriginOffset[0] = this->OriginOffset[1] = this->OriginOffset[2] = 0.0;
67 this->VectorsArrayName = NULL;
68}
69
71{
72 if (this->Centerlines)
73 {
74 this->Centerlines->Delete();
75 this->Centerlines = NULL;
76 }
77 if (this->SectionSource)
78 {
79 this->SectionSource->Delete();
80 this->SectionSource = NULL;
81 }
82 if (this->SectionPointsPolyData)
83 {
84 this->SectionPointsPolyData->Delete();
85 this->SectionPointsPolyData = NULL;
86 }
88 {
89 delete[] this->SectionUpNormalsArrayName;
90 this->SectionUpNormalsArrayName = NULL;
91 }
93 {
94 delete[] this->SectionNormalsArrayName;
95 this->SectionNormalsArrayName = NULL;
96 }
98 {
99 delete[] this->AdditionalNormalsArrayName;
100 this->AdditionalNormalsArrayName = NULL;
101 }
103 {
104 delete[] this->AdditionalScalarsArrayName;
105 this->AdditionalScalarsArrayName = NULL;
106 }
107 if (this->VectorsArrayName)
108 {
109 delete[] this->VectorsArrayName;
110 this->VectorsArrayName = NULL;
111 }
112 if (this->SectionIdsArrayName)
113 {
114 delete[] this->SectionIdsArrayName;
115 this->SectionIdsArrayName = NULL;
116 }
117}
118
120{
121 info->Set(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkUnstructuredGrid");
122 return 1;
123}
124
125double vtkvmtkUnstructuredGridCenterlineSections::ComputeAngle(double vector0[3], double vector1[3])
126{
127 double normal0[3], normal1[3];
128 normal0[0] = vector0[0];
129 normal0[1] = vector0[1];
130 normal0[2] = vector0[2];
131 normal1[0] = vector1[0];
132 normal1[1] = vector1[1];
133 normal1[2] = vector1[2];
134 vtkMath::Normalize(normal0);
135 vtkMath::Normalize(normal1);
136 double sum[3], difference[3];
137 sum[0] = normal0[0] + normal1[0];
138 sum[1] = normal0[1] + normal1[1];
139 sum[2] = normal0[2] + normal1[2];
140 difference[0] = normal0[0] - normal1[0];
141 difference[1] = normal0[1] - normal1[1];
142 difference[2] = normal0[2] - normal1[2];
143 double sumNorm = vtkMath::Norm(sum);
144 double differenceNorm = vtkMath::Norm(difference);
145 double angle = 2.0 * atan2(differenceNorm,sumNorm);
146 return angle;
147}
148
149void vtkvmtkUnstructuredGridCenterlineSections::CreateTransform(vtkTransform* transform, double currentOrigin[3], double currentNormal[3], double currentUpNormal[3], double targetOrigin[3], double targetNormal[3], double targetUpNormal[3])
150{
151 transform->PostMultiply();
152
153 double translation0[3];
154 translation0[0] = 0.0 - currentOrigin[0];
155 translation0[1] = 0.0 - currentOrigin[1];
156 translation0[2] = 0.0 - currentOrigin[2];
157
158 double translation1[3];
159 translation1[0] = targetOrigin[0] - 0.0;
160 translation1[1] = targetOrigin[1] - 0.0;
161 translation1[2] = targetOrigin[2] - 0.0;
162
163 transform->Translate(translation0);
164
165 double cross0[3];
166 vtkMath::Cross(currentNormal,targetNormal,cross0);
167 vtkMath::Normalize(cross0);
168 double angle0 = this->ComputeAngle(currentNormal,targetNormal);
169 angle0 = angle0 / (2.0*vtkMath::Pi()) * 360.0;
170 transform->RotateWXYZ(angle0,cross0);
171
172 double dot = vtkMath::Dot(currentUpNormal,currentNormal);
173 double currentProjectedUpNormal[3];
174 currentProjectedUpNormal[0] = currentUpNormal[0] - dot * currentNormal[0];
175 currentProjectedUpNormal[1] = currentUpNormal[1] - dot * currentNormal[1];
176 currentProjectedUpNormal[2] = currentUpNormal[2] - dot * currentNormal[2];
177 vtkMath::Normalize(currentProjectedUpNormal);
178
179 double transformedUpNormal[3];
180 transform->TransformNormal(currentProjectedUpNormal,transformedUpNormal);
181
182 double cross1[3];
183 vtkMath::Cross(transformedUpNormal,targetUpNormal,cross1);
184 vtkMath::Normalize(cross1);
185 double angle1 = this->ComputeAngle(transformedUpNormal,targetUpNormal);
186 angle1 = angle1 / (2.0*vtkMath::Pi()) * 360.0;
187 transform->RotateWXYZ(angle1,cross1);
188 transform->Translate(translation1);
189}
190
192 vtkInformation *vtkNotUsed(request),
193 vtkInformationVector **inputVector,
194 vtkInformationVector *outputVector)
195{
196 vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
197 vtkInformation *outInfo = outputVector->GetInformationObject(0);
198
199 vtkUnstructuredGrid *input = vtkUnstructuredGrid::SafeDownCast(
200 inInfo->Get(vtkDataObject::DATA_OBJECT()));
201 vtkPolyData *output = vtkPolyData::SafeDownCast(
202 outInfo->Get(vtkDataObject::DATA_OBJECT()));
203
204 if (!this->Centerlines)
205 {
206 vtkErrorMacro(<<"Centerlines not set");
207 return 1;
208 }
209
210 if (this->UseSectionSource && !this->SectionSource)
211 {
212 vtkErrorMacro(<<"SectionSource not set");
213 return 1;
214 }
215
216 vtkDataArray* upNormalsArray = NULL;
217
219 {
220 vtkErrorMacro(<<"TransformSection is On and no SectionUpNormalsArrayName specified!");
221 return 1;
222 }
223
225 {
226 upNormalsArray = this->Centerlines->GetPointData()->GetArray(this->SectionUpNormalsArrayName);
227 if (!upNormalsArray)
228 {
229 vtkErrorMacro(<<"UpNormalsArray with name specified does not exist!");
230 return 1;
231 }
232 if (upNormalsArray->GetNumberOfComponents() != 3)
233 {
234 vtkErrorMacro(<<"UpNormalsArray does not have 3 components!");
235 return 1;
236 }
237 }
238
239 if (!this->SectionNormalsArrayName)
240 {
241 vtkErrorMacro(<<"SectionNormalsArrayName not specified!");
242 return 1;
243 }
244
245 if (!this->SectionIdsArrayName)
246 {
247 vtkErrorMacro(<<"SectionIdsArrayName not specified!");
248 return 1;
249 }
250
251 vtkDataArray* additionalNormalsArray = NULL;
252 vtkDataArray* additionalScalarsArray = NULL;
254 {
255 additionalNormalsArray = this->Centerlines->GetPointData()->GetArray(this->AdditionalNormalsArrayName);
256 if (!additionalNormalsArray)
257 {
258 vtkErrorMacro(<<"AdditionalNormalsArray with name specified does not exist!");
259 }
260 if (additionalNormalsArray->GetNumberOfComponents() != 3)
261 {
262 vtkErrorMacro(<<"AdditionalNormalsArray does not have 3 components!");
263 additionalNormalsArray = NULL;
264 }
265 }
266
268 {
269 additionalScalarsArray = this->Centerlines->GetPointData()->GetArray(this->AdditionalScalarsArrayName);
270 if (!additionalScalarsArray)
271 {
272 vtkErrorMacro(<<"AdditionalScalarsArray with name specified does not exist!");
273 }
274 }
275
276 if (this->SectionPointsPolyData)
277 {
278 this->SectionPointsPolyData->Delete();
279 this->SectionPointsPolyData = NULL;
280 }
281
282 this->SectionPointsPolyData = vtkPolyData::New();
283 vtkPoints* sectionPointsPoints = vtkPoints::New();
284 vtkCellArray* sectionPointsVerts = vtkCellArray::New();
285 vtkDoubleArray* sectionPointsUpNormals = vtkDoubleArray::New();
286 sectionPointsUpNormals->SetNumberOfComponents(3);
287 sectionPointsUpNormals->SetName(this->SectionUpNormalsArrayName);
288 vtkDoubleArray* sectionPointsNormals = vtkDoubleArray::New();
289 sectionPointsNormals->SetNumberOfComponents(3);
290 sectionPointsNormals->SetName(this->SectionNormalsArrayName);
291 vtkDoubleArray* sectionPointsAdditionalNormals = vtkDoubleArray::New();
292 sectionPointsAdditionalNormals->SetNumberOfComponents(3);
293 sectionPointsAdditionalNormals->SetName(this->AdditionalNormalsArrayName);
294 vtkDoubleArray* sectionPointsAdditionalScalars = vtkDoubleArray::New();
295 sectionPointsAdditionalScalars->SetNumberOfComponents(1);
296 sectionPointsAdditionalScalars->SetName(this->AdditionalScalarsArrayName);
297 this->SectionPointsPolyData->SetPoints(sectionPointsPoints);
298 this->SectionPointsPolyData->SetVerts(sectionPointsVerts);
299 if (upNormalsArray)
300 {
301 this->SectionPointsPolyData->GetPointData()->AddArray(sectionPointsUpNormals);
302 }
303 this->SectionPointsPolyData->GetPointData()->AddArray(sectionPointsNormals);
304 if (additionalNormalsArray)
305 {
306 this->SectionPointsPolyData->GetPointData()->AddArray(sectionPointsAdditionalNormals);
307 }
308 if (additionalScalarsArray)
309 {
310 this->SectionPointsPolyData->GetPointData()->AddArray(sectionPointsAdditionalScalars);
311 }
312
313 vtkIdType sectionId = 0;
314 vtkAppendPolyData* appendFilter = vtkAppendPolyData::New();
315
316 int numberOfCenterlineCells = this->Centerlines->GetNumberOfCells();
317 int i;
318 for (i=0; i<numberOfCenterlineCells; i++)
319 {
320 vtkCell* centerlineCell = this->Centerlines->GetCell(i);
321 vtkPoints* centerlineCellPoints = centerlineCell->GetPoints();
322 int numberOfCellPoints = centerlineCellPoints->GetNumberOfPoints();
323
324 int j;
325 for (j=0; j<numberOfCellPoints; j++)
326 {
327 double point[3];
328 centerlineCellPoints->GetPoint(j,point);
329
330 double tangent[3];
331 tangent[0] = tangent[1] = tangent[2] = 0.0;
332
333 double upNormal[3];
334 upNormal[0] = upNormal[1] = upNormal[2] = 0.0;
335
336 double weightSum = 0.0;
337 if (j>0)
338 {
339 double point0[3], point1[3];
340 centerlineCellPoints->GetPoint(j-1,point0);
341 centerlineCellPoints->GetPoint(j,point1);
342 double distance = sqrt(vtkMath::Distance2BetweenPoints(point0,point1));
343 tangent[0] += (point1[0] - point0[0]) / distance;
344 tangent[1] += (point1[1] - point0[1]) / distance;
345 tangent[2] += (point1[2] - point0[2]) / distance;
346 weightSum += 1.0;
347 }
348
349 if (j<numberOfCellPoints-1)
350 {
351 double point0[3], point1[3];
352 centerlineCellPoints->GetPoint(j,point0);
353 centerlineCellPoints->GetPoint(j+1,point1);
354 double distance = sqrt(vtkMath::Distance2BetweenPoints(point0,point1));
355 tangent[0] += (point1[0] - point0[0]) / distance;
356 tangent[1] += (point1[1] - point0[1]) / distance;
357 tangent[2] += (point1[2] - point0[2]) / distance;
358 weightSum += 1.0;
359 }
360
361 tangent[0] /= weightSum;
362 tangent[1] /= weightSum;
363 tangent[2] /= weightSum;
364
365 vtkMath::Normalize(tangent);
366
367 if (upNormalsArray)
368 {
369 upNormalsArray->GetTuple(centerlineCell->GetPointId(j),upNormal);
370 vtkMath::Normalize(upNormal);
371 }
372
373 vtkPolyData* section = vtkPolyData::New();
374
375 if (!this->UseSectionSource)
376 {
377 vtkPlane* plane = vtkPlane::New();
378 plane->SetOrigin(point);
379 plane->SetNormal(tangent);
380
381 vtkCutter* cutter = vtkCutter::New();
382 cutter->SetInputData(input);
383 cutter->SetCutFunction(plane);
384 cutter->GenerateCutScalarsOff();
385 cutter->SetValue(0,0.0);
386 cutter->Update();
387
388 vtkCleanPolyData* cleaner = vtkCleanPolyData::New();
389 cleaner->SetInputConnection(cutter->GetOutputPort());
390 cleaner->Update();
391
392 if (cleaner->GetOutput()->GetNumberOfPoints() == 0)
393 {
394 plane->Delete();
395 cutter->Delete();
396 cleaner->Delete();
397 continue;
398 }
399
400 vtkPolyDataConnectivityFilter* connectivityFilter = vtkPolyDataConnectivityFilter::New();
401 connectivityFilter->SetInputConnection(cleaner->GetOutputPort());
402 connectivityFilter->SetExtractionModeToClosestPointRegion();
403 connectivityFilter->SetClosestPoint(point);
404 connectivityFilter->Update();
405
406 section->DeepCopy(connectivityFilter->GetOutput());
407 section->GetPointData()->SetActiveVectors(this->VectorsArrayName);
408
409 plane->Delete();
410 cutter->Delete();
411 cleaner->Delete();
412 connectivityFilter->Delete();
413 }
414 else
415 {
416 double currentOrigin[3];
417 currentOrigin[0] = 0.0;
418 currentOrigin[1] = 0.0;
419 currentOrigin[2] = 0.0;
420
421 double currentNormal[3];
422 currentNormal[0] = 0.0;
423 currentNormal[1] = 0.0;
424 currentNormal[2] = 1.0;
425
426 double currentUpNormal[3];
427 currentUpNormal[0] = 0.0;
428 currentUpNormal[1] = 1.0;
429 currentUpNormal[2] = 0.0;
430
431 double targetOrigin[3];
432 targetOrigin[0] = point[0];
433 targetOrigin[1] = point[1];
434 targetOrigin[2] = point[2];
435
436 double targetNormal[3];
437 targetNormal[0] = tangent[0];
438 targetNormal[1] = tangent[1];
439 targetNormal[2] = tangent[2];
440
441 double targetUpNormal[3];
442 targetUpNormal[0] = upNormal[0];
443 targetUpNormal[1] = upNormal[1];
444 targetUpNormal[2] = upNormal[2];
445
446 vtkTransform* transform = vtkTransform::New();
447 this->CreateTransform(transform,currentOrigin,currentNormal,currentUpNormal,targetOrigin,targetNormal,targetUpNormal);
448
449 vtkTransformPolyDataFilter* transformFilter = vtkTransformPolyDataFilter::New();
450 transformFilter->SetInputData(this->SectionSource);
451 transformFilter->SetTransform(transform);
452 transformFilter->Update();
453
454 vtkProbeFilter* probeFilter = vtkProbeFilter::New();
455 probeFilter->SetInputConnection(transformFilter->GetOutputPort());
456 probeFilter->SetSourceData(input);
457 probeFilter->Update();
458
459 section->DeepCopy(probeFilter->GetOutput());
460 section->GetPointData()->SetActiveVectors(this->VectorsArrayName);
461
462 probeFilter->Delete();
463 transformFilter->Delete();
464 transform->Delete();
465 }
466
467 vtkIdType numberOfSectionPoints = section->GetNumberOfPoints();
468 vtkIdTypeArray* sectionIdsArray = vtkIdTypeArray::New();
469 sectionIdsArray->SetName(this->SectionIdsArrayName);
470 sectionIdsArray->SetNumberOfTuples(numberOfSectionPoints);
471 vtkIdType pointId;
472 for (pointId=0; pointId<numberOfSectionPoints; pointId++)
473 {
474 sectionIdsArray->SetValue(pointId,sectionId);
475 }
476 sectionId += 1;
477 section->GetPointData()->AddArray(sectionIdsArray);
478
479 if (!this->TransformSections)
480 {
481 appendFilter->AddInputData(section);
482
483 vtkIdType pointId = sectionPointsPoints->InsertNextPoint(point);
484 sectionPointsVerts->InsertNextCell(1);
485 sectionPointsVerts->InsertCellPoint(pointId);
486 sectionPointsNormals->InsertNextTuple(tangent);
487 if (upNormalsArray)
488 {
489 sectionPointsUpNormals->InsertNextTuple(upNormal);
490 }
491 if (additionalNormalsArray)
492 {
493 double additionalNormal[3];
494 additionalNormalsArray->GetTuple(centerlineCell->GetPointId(j),additionalNormal);
495 sectionPointsAdditionalNormals->InsertNextTuple(additionalNormal);
496 }
497 if (additionalScalarsArray)
498 {
499 double scalar = additionalScalarsArray->GetTuple1(centerlineCell->GetPointId(j));
500 sectionPointsAdditionalScalars->InsertNextTuple1(scalar);
501 }
502 }
503 else
504 {
505 double currentOrigin[3];
506 currentOrigin[0] = point[0];
507 currentOrigin[1] = point[1];
508 currentOrigin[2] = point[2];
509
510 double currentNormal[3];
511 currentNormal[0] = tangent[0];
512 currentNormal[1] = tangent[1];
513 currentNormal[2] = tangent[2];
514
515 double currentUpNormal[3];
516 currentUpNormal[0] = upNormal[0];
517 currentUpNormal[1] = upNormal[1];
518 currentUpNormal[2] = upNormal[2];
519
520 double targetOrigin[3];
521 targetOrigin[0] = j * this->OriginOffset[0];
522 targetOrigin[1] = j * this->OriginOffset[1];
523 targetOrigin[2] = j * this->OriginOffset[2];
524
525 double targetNormal[3];
526 targetNormal[0] = 0.0;
527 targetNormal[1] = 0.0;
528 targetNormal[2] = 1.0;
529
530 double targetUpNormal[3];
531 targetUpNormal[0] = 0.0;
532 targetUpNormal[1] = 1.0;
533 targetUpNormal[2] = 0.0;
534
535 vtkTransform* transform = vtkTransform::New();
536 this->CreateTransform(transform,currentOrigin,currentNormal,currentUpNormal,targetOrigin,targetNormal,targetUpNormal);
537
538 vtkTransformPolyDataFilter* transformFilter = vtkTransformPolyDataFilter::New();
539 transformFilter->SetInputData(section);
540 transformFilter->SetTransform(transform);
541 transformFilter->Update();
542
543 appendFilter->AddInputConnection(transformFilter->GetOutputPort());
544
545 vtkIdType pointId = sectionPointsPoints->InsertNextPoint(targetOrigin);
546 sectionPointsVerts->InsertNextCell(1);
547 sectionPointsVerts->InsertCellPoint(pointId);
548 sectionPointsNormals->InsertNextTuple(targetNormal);
549 sectionPointsUpNormals->InsertNextTuple(targetUpNormal);
550 if (additionalNormalsArray)
551 {
552 double additionalNormal[3], projectedAdditionalNormal[3], transformedAdditionalNormal[3];
553 additionalNormalsArray->GetTuple(centerlineCell->GetPointId(j),additionalNormal);
554 double dot = vtkMath::Dot(additionalNormal,currentNormal);
555 projectedAdditionalNormal[0] = additionalNormal[0] - dot * currentNormal[0];
556 projectedAdditionalNormal[1] = additionalNormal[1] - dot * currentNormal[1];
557 projectedAdditionalNormal[2] = additionalNormal[2] - dot * currentNormal[2];
558 vtkMath::Normalize(projectedAdditionalNormal);
559 transform->TransformNormal(projectedAdditionalNormal,transformedAdditionalNormal);
560 sectionPointsAdditionalNormals->InsertNextTuple(transformedAdditionalNormal);
561 }
562 if (additionalScalarsArray)
563 {
564 double scalar = additionalScalarsArray->GetTuple1(centerlineCell->GetPointId(j));
565 sectionPointsAdditionalScalars->InsertNextTuple1(scalar);
566 }
567
568 transform->Delete();
569 transformFilter->Delete();
570 }
571
572 section->Delete();
573 }
574 }
575
576 appendFilter->Update();
577
578 output->DeepCopy(appendFilter->GetOutput());
579
580 appendFilter->Delete();
581 sectionPointsPoints->Delete();
582 sectionPointsVerts->Delete();
583 sectionPointsAdditionalNormals->Delete();
584 sectionPointsAdditionalScalars->Delete();
585
586 return 1;
587}
588
589void vtkvmtkUnstructuredGridCenterlineSections::PrintSelf(std::ostream& os, vtkIndent indent)
590{
591 this->Superclass::PrintSelf(os,indent);
592}
Obtain sections of an unstructured grid mesh that lie along a centerline path.
virtual int RequestData(vtkInformation *, vtkInformationVector **, vtkInformationVector *) override
void CreateTransform(vtkTransform *transform, double currentOrigin[3], double currentNormal[3], double currentUpNormal[3], double targetOrigin[3], double targetNormal[3], double targetUpNormal[3])
void PrintSelf(std::ostream &os, vtkIndent indent) override
vtkStandardNewMacro(vtkvmtkUnstructuredGridCenterlineSections)