VMTK
Loading...
Searching...
No Matches
vtkvmtkCenterlineInterpolateArray.cxx
Go to the documentation of this file.
1/*=========================================================================
2
3 Program: VMTK
4 Module: $RCSfile: vtkvmtkCenterlineInterpolateArray.cxx,v $
5 Language: C++
6 Date: $$
7 Version: $$
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 <algorithm>
29#include <vector>
30
31#include "vtkPointData.h"
32#include "vtkPolyLine.h"
33#include "vtkMath.h"
34#include "vtkBitArray.h"
35#include "vtkDoubleArray.h"
36#include "vtkInformation.h"
37#include "vtkInformationVector.h"
38#include "vtkObjectFactory.h"
39#include "vtkIOStream.h"
40#include "vtkVersion.h"
41
42#include "vtkvmtkConstants.h"
44
45
47
49{
50 this->InterpolatedArrayName = NULL;
51
52 this->DefaultValue = 0;
53 this->Values = NULL;
54 this->ValuesIds = NULL;
55}
56
58{
59 if (this->InterpolatedArrayName)
60 {
61 delete[] this->InterpolatedArrayName;
62 this->InterpolatedArrayName = NULL;
63 }
64
65 if (this->Values)
66 {
67 this->Values->Delete();
68 this->Values = NULL;
69 }
70
71 if (this->ValuesIds)
72 {
73 this->ValuesIds->Delete();
74 this->ValuesIds = NULL;
75 }
76}
77
79 vtkInformation *vtkNotUsed(request),
80 vtkInformationVector **inputVector,
81 vtkInformationVector *outputVector)
82{
83 vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
84 vtkInformation *outInfo = outputVector->GetInformationObject(0);
85
86 vtkPolyData *input = vtkPolyData::SafeDownCast(
87 inInfo->Get(vtkDataObject::DATA_OBJECT()));
88 vtkPolyData *output = vtkPolyData::SafeDownCast(
89 outInfo->Get(vtkDataObject::DATA_OBJECT()));
90
91 if (!this->InterpolatedArrayName)
92 {
93 vtkErrorMacro(<<"InterpolatedArrayName not set.");
94 return 1;
95 }
96
97 if (!this->Values)
98 {
99 vtkErrorMacro(<<"Values not set");
100 return 1;
101 }
102
103 if (!this->ValuesIds)
104 {
105 vtkErrorMacro(<<"ValuesIds not set");
106 return 1;
107 }
108
109 if (this->Values->GetNumberOfTuples() != this->ValuesIds->GetNumberOfIds())
110 {
111 vtkErrorMacro(<<"Inconsistent number of values/ids");
112 return 1;
113 }
114
115 int numberOfInputPoints = input->GetNumberOfPoints();
116
117 output->DeepCopy(input);
118
119
120 vtkDataArray* interpolatedArray = output->GetPointData()->GetArray(this->InterpolatedArrayName);
121
122 bool createArray = !interpolatedArray;
123
124 if (createArray)
125 {
126 interpolatedArray = vtkDoubleArray::New();
127 interpolatedArray->SetName(this->InterpolatedArrayName);
128 interpolatedArray->SetNumberOfComponents(this->Values->GetNumberOfComponents());
129 interpolatedArray->SetNumberOfTuples(numberOfInputPoints);
130 for (int i=0; i<interpolatedArray->GetNumberOfComponents(); i++)
131 {
132 interpolatedArray->FillComponent(i, this->DefaultValue);
133 }
134 output->GetPointData()->AddArray(interpolatedArray);
135 }
136
137 int numberOfComponents = std::min(interpolatedArray->GetNumberOfComponents(),this->Values->GetNumberOfComponents());
138
139 //Compute abscissas for the centerlines
141 attribFilter->SetInputData(input);
142 attribFilter->SetAbscissasArrayName("Abscissa");
143 attribFilter->SetParallelTransportNormalsArrayName("ParallelTransportNormals");
144 attribFilter->Update();
145 vtkDataArray *abscissaArray = attribFilter->GetOutput()->GetPointData()->GetArray("Abscissa");
146
147 //Indicates which point have values
148 vtkBitArray *hasValueArray = vtkBitArray::New();
149 hasValueArray->SetNumberOfComponents(1);
150 hasValueArray->SetNumberOfTuples(numberOfInputPoints);
151 hasValueArray->FillComponent(0,false);
152
153 for (int i=0; i<this->ValuesIds->GetNumberOfIds(); i++)
154 {
155 hasValueArray->SetValue(this->ValuesIds->GetId(i),true);
156 for (int j=0; j<numberOfComponents; j++)
157 {
158 interpolatedArray->SetComponent(this->ValuesIds->GetId(i),j,this->Values->GetComponent(i,j));
159 }
160 }
161
162 int numberOfInputCells = input->GetNumberOfCells();
163
164
165 for (int i=0; i<numberOfInputCells; i++)
166 {
167 vtkPolyLine* polyLine = vtkPolyLine::SafeDownCast(input->GetCell(i));
168 if (!polyLine)
169 {
170 continue;
171 }
172
173 int numberOfLinePoints = polyLine->GetNumberOfPoints();
174
175 bool foundValue = false;
176 int startInd = 0;
177 //Find the first point having a value
178 while ((startInd<numberOfLinePoints) && (!foundValue))
179 {
180 foundValue = hasValueArray->GetValue(polyLine->GetPointId(startInd++));
181 }
182
183 //If no point with a value has been found skip to next line
184 if ((startInd==numberOfLinePoints) && (!foundValue))
185 {
186 continue;
187 }
188
189 //Otherwise we have our first value fill the previous points with this value
190 startInd--;
191 std::vector<double> startVal(numberOfComponents);
192 for (int j=0; j<numberOfComponents; j++)
193 {
194 startVal[j] = interpolatedArray->GetComponent(polyLine->GetPointId(startInd),j);
195 }
196 double startAbs = abscissaArray->GetComponent(polyLine->GetPointId(startInd),0);
197
198 for (int j=0; j<startInd; j++)
199 {
200 for (int k=0; k<numberOfComponents; k++)
201 {
202 interpolatedArray->SetComponent(polyLine->GetPointId(j),k,startVal[k]);
203 }
204 hasValueArray->SetValue(polyLine->GetPointId(j),true);
205 }
206
207 int endInd = startInd + 1;
208 foundValue = false;
209 while (endInd<numberOfLinePoints)
210 {
211 foundValue = hasValueArray->GetValue(polyLine->GetPointId(endInd));
212 if (foundValue)
213 {
214 //Interpolate between the start and end values
215 std::vector<double> endVal(numberOfComponents);
216 for (int j=0; j<numberOfComponents; j++)
217 {
218 endVal[j] = interpolatedArray->GetComponent(polyLine->GetPointId(endInd),j);
219 }
220 double endAbs = abscissaArray->GetComponent(polyLine->GetPointId(endInd),0);
221 double absDiff = endAbs - startAbs;
222 if (absDiff!=0.)
223 {
224 float invAbsDiff = 1./absDiff;
225 for (int j=startInd+1;j<endInd;j++)
226 {
227 double currAbs = abscissaArray->GetComponent(polyLine->GetPointId(j),0);
228 for (int k=0; k<numberOfComponents; k++)
229 {
230 double newVal = startVal[k] + (currAbs - startAbs)*invAbsDiff*(endVal[k] - startVal[k]);
231 interpolatedArray->SetComponent(polyLine->GetPointId(j),k,newVal);
232 }
233 hasValueArray->SetValue(polyLine->GetPointId(j),true);
234 }
235 }
236 //Replace the end value by the start value
237 startInd = endInd;
238 startVal = endVal;
239 startAbs = endAbs;
240 }
241 endInd++;
242 }
243
244 //Fill the remaining values
245 for (int j=startInd+1; j<numberOfLinePoints; j++)
246 {
247 for (int k=0; k<numberOfComponents; k++)
248 {
249 interpolatedArray->SetComponent(polyLine->GetPointId(j),k,startVal[k]);
250 }
251 hasValueArray->SetValue(polyLine->GetPointId(j),true);
252 }
253 }
254
255 if (createArray) interpolatedArray->Delete();
256 attribFilter->Delete();
257 hasValueArray->Delete();
258
259 return 1;
260}
Computes the abscissa and parallel transport normal along a centerline.
virtual void SetAbscissasArrayName(const char *)
virtual void SetParallelTransportNormalsArrayName(const char *)
static vtkvmtkCenterlineAttributesFilter * New()
Interpolate a point-based array from a set of provided values.
virtual int RequestData(vtkInformation *, vtkInformationVector **, vtkInformationVector *) override
vtkStandardNewMacro(vtkvmtkCenterlineInterpolateArray)