VMTK
Loading...
Searching...
No Matches
vtkvmtkPolyDataGeodesicRBFInterpolation.cxx
Go to the documentation of this file.
1/*=========================================================================
2
3 Program: VMTK
4 Module: $RCSfile: vtkvmtkPolyDataGeodesicRBFInterpolation.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 "vtkVersion.h"
29#include "vtkPointData.h"
30#include "vtkMath.h"
31#include "vtkInformation.h"
32#include "vtkInformationVector.h"
33#include "vtkObjectFactory.h"
34#include "vtkIOStream.h"
35#include <iostream>
36#include "vtkFloatArray.h"
37#include "vtkDoubleArray.h"
38
39#include <vector>
40
41#include "vtkvmtkConstants.h"
42
43#include "vtkDijkstraGraphGeodesicPath.h"
44
46
56
58{
59 if (this->InterpolatedArrayName)
60 {
61 delete[] this->InterpolatedArrayName;
62 this->InterpolatedArrayName = NULL;
63 }
64
65 if (this->SeedIds)
66 {
67 this->SeedIds->Delete();
68 this->SeedIds = NULL;
69 }
70
71 if (this->SeedValues)
72 {
73 this->SeedValues->Delete();
74 this->SeedValues = NULL;
75 }
76}
77
79{
80 if (this->RBFType == THIN_PLATE_SPLINE)
81 {
82 double r2 = r*r;
83 if (!r2)
84 {
85 return 0.0;
86 }
87 return r2 * log(sqrt(r2));
88 }
89 else if (this->RBFType == BIHARMONIC)
90 {
91 return r;
92 }
93 else if (this->RBFType == TRIHARMONIC)
94 {
95 return r*r*r;
96 }
97 else
98 {
99 vtkErrorMacro(<<"Error: Unsupported RBFType!");
100 return 0.0;
101 }
102}
103
104
106 vtkInformation *vtkNotUsed(request),
107 vtkInformationVector **inputVector,
108 vtkInformationVector *outputVector)
109{
110 vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
111 vtkInformation *outInfo = outputVector->GetInformationObject(0);
112
113 vtkPolyData *input = vtkPolyData::SafeDownCast(
114 inInfo->Get(vtkDataObject::DATA_OBJECT()));
115 vtkPolyData *output = vtkPolyData::SafeDownCast(
116 outInfo->Get(vtkDataObject::DATA_OBJECT()));
117
118 if (!this->InterpolatedArrayName)
119 {
120 vtkErrorMacro(<<"InterpolatedArrayName not set.");
121 return 1;
122 }
123
124 if (!this->SeedIds)
125 {
126 vtkErrorMacro(<<"SeedIds not set");
127 return 1;
128 }
129
130 if (!this->SeedValues)
131 {
132 vtkErrorMacro(<<"SeedValues not set");
133 return 1;
134 }
135
136
137 int numberOfSeeds = this->SeedIds->GetNumberOfIds();
138
139 int numberOfInputPoints = input->GetNumberOfPoints();
140
141 if ((numberOfSeeds != this->SeedValues->GetNumberOfTuples()) || (numberOfSeeds>numberOfInputPoints) || (numberOfSeeds<2))
142 {
143 vtkErrorMacro(<<"Incorrect number of seed values");
144 return 1;
145 }
146
147 output->DeepCopy(input);
148
149
150 vtkDataArray* interpolatedArray = output->GetPointData()->GetArray(this->InterpolatedArrayName);
151
152 bool createArray = !interpolatedArray;
153
154 if (createArray)
155 {
156 interpolatedArray = vtkDoubleArray::New();
157 interpolatedArray->SetName(this->InterpolatedArrayName);
158 interpolatedArray->SetNumberOfComponents(1);
159 interpolatedArray->SetNumberOfTuples(numberOfInputPoints);
160
161 output->GetPointData()->AddArray(interpolatedArray);
162 }
163
164
165
166 vtkDijkstraGraphGeodesicPath *dijkstraAlgo = vtkDijkstraGraphGeodesicPath::New();
167 dijkstraAlgo->SetInputData(input);
168 dijkstraAlgo->StopWhenEndReachedOff();
169 dijkstraAlgo->UseScalarWeightsOff();
170
171 std::vector<vtkDoubleArray *> geodesicDistances;
172
173
174 //Compute the geodesic distances
175 for (int i=0; i<numberOfSeeds; i++)
176 {
177 dijkstraAlgo->SetStartVertex(SeedIds->GetId(i));
178 dijkstraAlgo->Update();
179 vtkDoubleArray *seedDistances = vtkDoubleArray::New();
180 dijkstraAlgo->GetCumulativeWeights(seedDistances);
181 geodesicDistances.push_back(seedDistances);
182 }
183
184 //Compute the coefficients
185 double **A, *x;
186 x = new double[numberOfSeeds];
187 A = new double* [numberOfSeeds];
188
189 int i;
190 for (i=0; i<numberOfSeeds; i++)
191 {
192 A[i] = new double[numberOfSeeds];
193 x[i] = this->SeedValues->GetComponent(i,0);
194 }
195
196 int j;
197 for (i=0; i<numberOfSeeds; i++)
198 {
199 for (j=0; j<numberOfSeeds; j++)
200 {
201 double dist = geodesicDistances[i]->GetValue(this->SeedIds->GetId(j));
202 A[i][j] = this->EvaluateRBF(dist);
203 }
204 }
205
206 std::cout << "A " << std::endl;
207 for (int i=0;i<numberOfSeeds;i++) {
208 for (int j=0;j<numberOfSeeds;j++) {
209 std::cout << A[i][j] << " ";
210 }
211 std::cout << std::endl;
212 }
213
214 std::cout << "b " << std::endl;
215 for (int i=0;i<numberOfSeeds;i++) {
216 std::cout << x[i] << std::endl;
217 }
218
219 int ret = vtkMath::SolveLinearSystem(A,x,numberOfSeeds);
220
221 std::cout << "x " << std::endl;
222 for (int i=0;i<numberOfSeeds;i++) {
223 std::cout << x[i] << std::endl;
224 }
225
226 if (!ret)
227 {
228 vtkErrorMacro(<<"Cannot compute coefficients: error during linear system solve");
229 }
230
231
232 for (i=0; i<numberOfSeeds; i++)
233 {
234 delete[] A[i];
235 }
236 delete[] A;
237
238
239 //Interpolate the values at all points using the coefficients
240 for (int i=0;i<numberOfInputPoints;i++)
241 {
242 double rbfValue = 0.;
243 for (int j=0;j<numberOfSeeds;j++) {
244 double dist = geodesicDistances[j]->GetValue(i);
245 rbfValue += x[j]*this->EvaluateRBF(dist);
246 }
247 interpolatedArray->SetComponent(i,0,rbfValue);
248 }
249
250 delete[] x;
251 for (int i=0;i<numberOfSeeds;i++)
252 {
253 geodesicDistances[i]->Delete();
254 }
255
256 geodesicDistances.clear();
257
258
259 if (createArray) interpolatedArray->Delete();
260
261 return 1;
262}
Interpolate a point-based array over a surface using a radial basis function of the mesh geodesic dis...
virtual int RequestData(vtkInformation *, vtkInformationVector **, vtkInformationVector *) override
vtkStandardNewMacro(vtkvmtkPolyDataGeodesicRBFInterpolation)