-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathvtkAddonMathUtilities.cxx
390 lines (350 loc) · 11.8 KB
/
vtkAddonMathUtilities.cxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/*=auto==============================================================================
Program: 3D Slicer
See COPYRIGHT.txt
or http://www.slicer.org/copyright/copyright.txt for details.
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
===============================================================================auto=*/
// std includes
#include <sstream>
#include <vector>
#include <cstdlib>
#include <cmath>
#include <algorithm> // VTK 8.2.0 has bug for C++17 "clamp" function (algorithm must be included before vtMath.h)
// VTK includes
#include "vtk_eigen.h"
#include VTK_EIGEN(Dense)
#include <vtkMath.h>
#include <vtkMatrix3x3.h>
#include <vtkMatrix4x4.h>
#include <vtkObjectFactory.h>
#include <vtkPlane.h>
#include <vtkPoints.h>
#include <vtksys/RegularExpression.hxx>
#include <vtkLoggingMacros.h>
// vtkAddon includes
#include <vtkAddonMathUtilities.h>
//----------------------------------------------------------------------------
vtkStandardNewMacro(vtkAddonMathUtilities);
//----------------------------------------------------------------------------
vtkAddonMathUtilities::vtkAddonMathUtilities()
= default;
//----------------------------------------------------------------------------
vtkAddonMathUtilities::~vtkAddonMathUtilities()
= default;
//----------------------------------------------------------------------------
void vtkAddonMathUtilities::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
}
//----------------------------------------------------------------------------
bool vtkAddonMathUtilities::MatrixAreEqual(const vtkMatrix4x4* m1,
const vtkMatrix4x4* m2,
double tolerance)
{
if (!m1 || !m2)
{
vtkGenericWarningMacro("vtkAddonMathUtilities::MatrixAreEqual: invalid input matrix");
return false;
}
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
if ( fabs(m1->GetElement(i, j) - m2->GetElement(i, j)) > tolerance )
{
return false;
}
}
}
return true;
}
//----------------------------------------------------------------------------
bool vtkAddonMathUtilities::MatrixAreEqual(const vtkMatrix4x4 *m1,
const vtkMatrix3x3 *m2,
double tolerance)
{
if (!m1 || !m2)
{
vtkGenericWarningMacro("vtkAddonMathUtilities::MatrixAreEqual: invalid input matrix");
return false;
}
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
if ( fabs(m1->GetElement(i, j) - m2->GetElement(i, j)) > tolerance )
{
return false;
}
}
}
return true;
}
//----------------------------------------------------------------------------
bool vtkAddonMathUtilities::MatrixAreEqual(const vtkMatrix3x3 *m1,
const vtkMatrix4x4 *m2,
double tolerance)
{
return MatrixAreEqual(m2, m1, tolerance);
}
//----------------------------------------------------------------------------
bool vtkAddonMathUtilities::MatrixAreEqual(const vtkMatrix3x3 *m1,
const vtkMatrix3x3 *m2,
double tolerance)
{
if (!m1 || !m2)
{
vtkGenericWarningMacro("vtkAddonMathUtilities::MatrixAreEqual: invalid input matrix");
return false;
}
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
if ( fabs(m1->GetElement(i, j) - m2->GetElement(i, j)) > tolerance )
{
return false;
}
}
}
return true;
}
//----------------------------------------------------------------------------
void vtkAddonMathUtilities::GetOrientationMatrixColumn(vtkMatrix4x4* m, int columnIndex,
double columnVector[3])
{
if (!m)
{
vtkGenericWarningMacro("vtkAddonMathUtilities::GetMatrixColumn: invalid matrix");
return;
}
for (int rowIndex = 0; rowIndex < 3; ++rowIndex)
{
columnVector[rowIndex] = m->GetElement(rowIndex, columnIndex);
}
}
//----------------------------------------------------------------------------
void vtkAddonMathUtilities::SetOrientationMatrixColumn(vtkMatrix4x4* m, int columnIndex,
double columnVector[3])
{
if (!m)
{
vtkGenericWarningMacro("vtkAddonMathUtilities::GetMatrixColumn: invalid matrix");
return;
}
for (int rowIndex = 0; rowIndex < 3; ++rowIndex)
{
m->SetElement(rowIndex, columnIndex, columnVector[rowIndex]);
}
}
//----------------------------------------------------------------------------
void vtkAddonMathUtilities::GetOrientationMatrix(vtkMatrix4x4* source,
vtkMatrix3x3* dest)
{
if (!source || !dest)
{
vtkGenericWarningMacro("vtkAddonMathUtilities::GetOrientationMatrix: invalid source or destination matrix");
return;
}
for (int rowIndex = 0; rowIndex < 3; ++rowIndex)
{
for (int columnIndex = 0; columnIndex < 3; ++columnIndex)
{
dest->SetElement(rowIndex, columnIndex, source->GetElement(rowIndex, columnIndex));
}
}
}
//----------------------------------------------------------------------------
void vtkAddonMathUtilities::SetOrientationMatrix(vtkMatrix3x3* source,
vtkMatrix4x4* dest)
{
if (!source || !dest)
{
vtkGenericWarningMacro("vtkAddonMathUtilities::SetOrientationMatrix: invalid source or destination matrix");
return;
}
for (int rowIndex = 0; rowIndex < 3; ++rowIndex)
{
for (int columnIndex = 0; columnIndex < 3; ++columnIndex)
{
dest->SetElement(rowIndex, columnIndex, source->GetElement(rowIndex, columnIndex));
}
}
}
//----------------------------------------------------------------------------
void vtkAddonMathUtilities::NormalizeColumns(vtkMatrix3x3 *m, double scale[3])
{
if (!m)
{
vtkGenericWarningMacro("vtkAddonMathUtilities::NormalizeColumns: invalid input matrix");
return;
}
for (int col = 0; col < 3; col++)
{
double len = 0;
for (int row = 0; row < 3; row++)
{
len += m->GetElement(row, col) * m->GetElement(row, col);
}
len = sqrt(len);
scale[col] = len;
for (int row = 0; row < 3; row++)
{
m->SetElement(row, col, m->GetElement(row, col)/len);
}
}
}
//----------------------------------------------------------------------------
void vtkAddonMathUtilities::NormalizeOrientationMatrixColumns(vtkMatrix4x4 *m, double scale[3])
{
if (!m)
{
vtkGenericWarningMacro("vtkAddonMathUtilities::NormalizeOrientationMatrixColumns: invalid input matrix");
return;
}
for (int col = 0; col < 3; col++)
{
double len = 0;
for (int row = 0; row < 3; row++)
{
len += m->GetElement(row, col) * m->GetElement(row, col);
}
len = sqrt(len);
scale[col] = len;
for (int row = 0; row < 3; row++)
{
m->SetElement(row, col, m->GetElement(row, col)/len);
}
}
}
//----------------------------------------------------------------------------
std::string vtkAddonMathUtilities::ToString(const vtkMatrix4x4* mat, const std::string delimiter, const std::string rowDelimiter)
{
if (!mat)
{
return "";
}
std::stringstream ss;
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
ss << mat->GetElement(i, j);
ss << delimiter;
}
ss << rowDelimiter;
}
return ss.str();
}
//----------------------------------------------------------------------------
bool vtkAddonMathUtilities::FromString(vtkMatrix4x4* mat, const std::string& str, const std::string delimiterExp)
{
if (!mat)
{
return false;
}
// Parse the string using the regular expression
vtksys::RegularExpression delimiterRegex( delimiterExp );
// Convert each string token into a double and put into vector
char* end;
std::string remainString = str;
std::vector<double> elements;
while(!remainString.empty())
{
bool separatorFound = delimiterRegex.find(remainString);
std::string::size_type tokenStartIndex = remainString.length();
std::string::size_type tokenEndIndex = remainString.length();
if (separatorFound)
{
tokenStartIndex = delimiterRegex.start(0);
tokenEndIndex = delimiterRegex.end(0);
}
std::string valString = remainString.substr(0, tokenStartIndex);
remainString = remainString.substr(tokenEndIndex);
if (valString.empty()) // Handle back-to-back delimiters
{
continue;
}
// strtod is much faster (about 2x on some computers) than string stream
// based string->number conversion
double val = std::strtod(valString.c_str(), &end);
if (*end != 0) // Parsing failed due to non-numeric character
{
return false;
}
elements.push_back(val);
}
// Ensure the matrix is 1x1, 2x2, 3x3, or 4x4
if (elements.size() != 1
&& elements.size() != 4
&& elements.size() != 9
&& elements.size() != 16)
{
return false;
}
int dimension = std::sqrt(elements.size()) + 0.5; // Since conversion to int just truncates
// Put into matrix
int linearIndex = 0;
for (int row = 0; row < dimension; row++)
{
for (int col = 0; col < dimension; col++)
{
mat->SetElement(row, col, elements.at(linearIndex));
linearIndex++;
}
}
return true;
}
//---------------------------------------------------------------------------
bool vtkAddonMathUtilities::FitPlaneToPoints(vtkPoints* points, vtkPlane* bestFitPlane)
{
if (!points || !bestFitPlane || points->GetNumberOfPoints() < 3)
{
return false;
}
vtkNew<vtkMatrix4x4> transformToBestFitPlane;
if (!vtkAddonMathUtilities::FitPlaneToPoints(points, transformToBestFitPlane))
{
return false;
}
bestFitPlane->SetOrigin(transformToBestFitPlane->GetElement(0, 3), transformToBestFitPlane->GetElement(1, 3), transformToBestFitPlane->GetElement(2, 3));
bestFitPlane->SetNormal(transformToBestFitPlane->GetElement(0, 2), transformToBestFitPlane->GetElement(1, 2), transformToBestFitPlane->GetElement(2, 2));
return true;
}
//---------------------------------------------------------------------------
bool vtkAddonMathUtilities::FitPlaneToPoints(vtkPoints* points, vtkMatrix4x4* transformToBestFitPlane)
{
if (!points || !transformToBestFitPlane || points->GetNumberOfPoints() < 3)
{
return false;
}
vtkIdType numberOfPoints = points->GetNumberOfPoints();
Eigen::MatrixXd pointCoords(3, numberOfPoints);
double point[3] = { 0.0 };
for (vtkIdType pointIndex = 0; pointIndex < numberOfPoints; ++pointIndex)
{
points->GetPoint(pointIndex, point);
pointCoords(0, pointIndex) = point[0];
pointCoords(1, pointIndex) = point[1];
pointCoords(2, pointIndex) = point[2];
}
// Subtract centroid
Eigen::Vector3d centroid(pointCoords.row(0).mean(), pointCoords.row(1).mean(), pointCoords.row(2).mean());
pointCoords.row(0).array() -= centroid(0);
pointCoords.row(1).array() -= centroid(1);
pointCoords.row(2).array() -= centroid(2);
Eigen::BDCSVD<Eigen::MatrixXd> svd(pointCoords, Eigen::ComputeFullU);
transformToBestFitPlane->Identity();
for (int row = 0; row < 3; row++)
{
transformToBestFitPlane->SetElement(row, 0, svd.matrixU()(row, 0));
transformToBestFitPlane->SetElement(row, 1, svd.matrixU()(row, 1));
transformToBestFitPlane->SetElement(row, 2, svd.matrixU()(row, 2));
transformToBestFitPlane->SetElement(row, 3, centroid(row));
}
return true;
}