VMTK
Loading...
Searching...
No Matches
vtkvmtkSurfaceProjectCellArray.cxx
Go to the documentation of this file.
1/*=========================================================================
2
3Program: VMTK
4Module: $RCSfile: vtkvmtkSurfaceProjectCellArray.cxx,v $
5Language: C++
6Date: $$
7Version: $$
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 Note: this class was contributed by
21 Tangui Morvan
22 Kalkulo AS
23 Simula Research Laboratory
24=========================================================================*/
25
27
28#include "vtkCellLocator.h"
29#include "vtkDoubleArray.h"
30#include "vtkGenericCell.h"
31#include "vtkPolyData.h"
32#include "vtkCellData.h"
33#include "vtkMath.h"
34#include "vtkInformation.h"
35#include "vtkInformationVector.h"
36#include "vtkObjectFactory.h"
37
38#include "vtkvmtkConstants.h"
39
40#include <algorithm>
41
43
51
53{
54 if (this->ReferenceSurface)
55 {
56 this->ReferenceSurface->Delete();
57 this->ReferenceSurface = NULL;
58 }
59
60 if (this->ProjectedArrayName)
61 {
62 delete[] this->ProjectedArrayName;
63 this->ProjectedArrayName = NULL;
64 }
65}
66
68 vtkInformation *vtkNotUsed(request),
69 vtkInformationVector **inputVector,
70 vtkInformationVector *outputVector)
71{
72 vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
73 vtkInformation *outInfo = outputVector->GetInformationObject(0);
74
75 vtkPolyData *input = vtkPolyData::SafeDownCast(
76 inInfo->Get(vtkDataObject::DATA_OBJECT()));
77 vtkPolyData *output = vtkPolyData::SafeDownCast(
78 outInfo->Get(vtkDataObject::DATA_OBJECT()));
79
80
81 vtkCellLocator *locator;
82 vtkGenericCell *genericCell;
83
84 if (!this->ReferenceSurface)
85 {
86 vtkErrorMacro(<<"No reference surface!");
87 return 1;
88 }
89
90 vtkDataArray *referenceArray = this->ReferenceSurface->GetCellData()->GetArray(this->ProjectedArrayName);
91 if (!referenceArray)
92 {
93 vtkErrorMacro(<<"No reference array!");
94 return 1;
95 }
96
97 int numberOfCells = input->GetNumberOfCells();
98
99 output->DeepCopy(input);
100
101 vtkDataArray *projectedArray = output->GetCellData()->GetArray(this->ProjectedArrayName);
102
103 if (!projectedArray)
104 {
105 projectedArray = vtkDoubleArray::New();
106 projectedArray->SetName(this->ProjectedArrayName);
107 projectedArray->SetNumberOfTuples(numberOfCells);
108 projectedArray->SetNumberOfComponents(referenceArray->GetNumberOfComponents());
109 output->GetCellData()->AddArray(projectedArray);
110 }
111
112 int numberOfComponents = std::min(referenceArray->GetNumberOfComponents(), projectedArray->GetNumberOfComponents());
113
114 for (int i=0; i<numberOfComponents; i++)
115 {
116 projectedArray->FillComponent(i, DefaultValue);
117 }
118
119
120 this->ReferenceSurface->BuildCells();
121
122 locator = vtkCellLocator::New();
123 genericCell = vtkGenericCell::New();
124
125 locator->SetDataSet(this->ReferenceSurface);
126 locator->BuildLocator();
127
128 vtkIdList *cellPtsIds = vtkIdList::New();
129
130 double point[3], closestPoint[3];
131 double distance2;
132 double distanceTolerance2 = this->DistanceTolerance*this->DistanceTolerance;
133 vtkIdType cellId;
134 int subId;
135
136 for (int i=0; i<numberOfCells; i++)
137 {
138 input->GetCellPoints(i,cellPtsIds);
139 bool skipCell = false;
140 double minDistance = VTK_VMTK_LARGE_DOUBLE;
141 vtkIdType minId = 0;
142
143 //Find the cell with the smallest minimum distance to the vertices
144 for (int j=0; j<cellPtsIds->GetNumberOfIds(); j++)
145 {
146 input->GetPoint(cellPtsIds->GetId(j), point);
147 locator->FindClosestPoint(point,closestPoint,genericCell,cellId,subId,distance2);
148 if (distance2>distanceTolerance2)
149 {
150 skipCell = true;
151 break;
152 }
153 if ((j==0) || (distance2 < minDistance))
154 {
155 minDistance = distance2;
156 minId = cellId;
157 }
158 }
159
160 if (!skipCell)
161 {
162 for (int j=0; j<numberOfComponents; j++)
163 {
164 projectedArray->SetComponent(i,j,referenceArray->GetComponent(minId,j));
165 }
166 }
167 }
168
169 locator->Delete();
170 genericCell->Delete();
171 cellPtsIds->Delete();
172
173 return 1;
174}
175
176void vtkvmtkSurfaceProjectCellArray::PrintSelf(std::ostream& os, vtkIndent indent)
177{
178 this->Superclass::PrintSelf(os,indent);
179}
Projects a cell array from a reference surface.
virtual int RequestData(vtkInformation *, vtkInformationVector **, vtkInformationVector *) override
void PrintSelf(std::ostream &os, vtkIndent indent) override
#define VTK_VMTK_LARGE_DOUBLE
vtkStandardNewMacro(vtkvmtkSurfaceProjectCellArray)