Debian Codegraph @

tools/physicalModeling/mesh2faust/vega/libraries/minivector/mat3d.h

raw symbols SCIP
1 /*
2 * Copyright (c) 2008, Carnegie Mellon University
3 * All rights reserved.
4 *
5 * Code author: Jernej Barbic
6 * Research: Jernej Barbic, Doug L. James
7 * Funding: NSF, Link Foundation
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are met:
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * * Neither the name of Carnegie Mellon University, nor the
17 * names of its contributors may be used to endorse or promote products
18 * derived from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND ANY
21 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE* DISCLAIMED. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE FOR ANY
23 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30 This class implements a 3x3 matrix, with elementary algebra,
31 including the computation of eigenvalues and eigenvectors of a *symmetric* 3x3 matrix.
32 (using a public domain external routine; see mat3d.cpp and eig3.h)
33 */
34
35
36
37 #ifndef _MINIMAT3D_H_
38 #define _MINIMAT3D_H_
39
40 #include "vec3d.h"
41
42 class Mat3d {
43 public:
44
45 inline Mat3d() {}
46
47 /*
48 [ x0 x1 x2 ]
49 M = [ x3 x4 x5 ]
50 [ x6 x7 x8 ]
51 */
52 inline Mat3d(double x0, double x1, double x2,
53 double x3, double x4, double x5,
54 double x6, double x7, double x8);
55 inline Mat3d(const double mat[9]); // "mat" must be given in row-major order
56 inline Mat3d(const Vec3d rows[3]);
57 inline Mat3d(const Vec3d & row0, const Vec3d & row1, const Vec3d & row2);
58 inline Mat3d(double diag); // create a diagonal matrix with all entries "diag" (can create zero matrix by passing 0.0)
59
60 inline void set(double x0, double x1, double x2,
61 double x3, double x4, double x5,
62 double x6, double x7, double x8);
63 inline void set(double value); // set all elements to value
64
65 inline Mat3d & operator=(const Mat3d & source);
66
67 inline Mat3d operator+ (const Mat3d & ) const;
68 inline Mat3d & operator+= (const Mat3d & );
69
70 inline Mat3d operator- (const Mat3d & ) const;
71 inline Mat3d & operator-= (const Mat3d & );
72
73 inline Mat3d & operator*= (double scalar);
74 inline Mat3d & operator/= (double scalar);
75
76 inline bool operator== (const Mat3d &) const;
77 inline bool operator!= (const Mat3d &) const;
78
79 friend inline Mat3d operator* (double scalar, const Mat3d & mat2);
80 friend inline Mat3d operator/ (const Mat3d & mat2, double scalar);
81
82 inline Mat3d & multiplyDiagRight(Vec3d & v); // M = M * diag(v)
83 inline Mat3d & multiplyDiagLeft(Vec3d & v); // M = diag(v) * M
84
85 friend inline Mat3d tensorProduct(const Vec3d & vec1, const Vec3d & vec2);
86
87 friend inline Mat3d inv(const Mat3d &); // inverse matrix
88 friend inline double det(const Mat3d & mat); // determinant
89 friend inline Mat3d trans(const Mat3d & mat); // transpose
90
91 inline double maxAbsEntry() const;
92 inline bool hasNaN() const;
93 friend inline std::ostream &operator << (std::ostream &s, const Mat3d &v);
94 inline void print() const;
95
96 inline Vec3d & operator[] (int index); // M[i] returns i-th row
97 inline const Vec3d & operator[] (int index) const; // M[i] returns i-th row
98
99 // converts the matrix into a C array of length 9
100 inline void convertToArray(double * array) const; // in row-major order
101
102 // matrix-vector multiply
103 inline const Vec3d operator* (const Vec3d & vec) const;
104
105 // matrix-matrix multiply
106 inline const Mat3d operator* (const Mat3d & mat2) const;
107
108 // Computes eigenvalues and eigenvectors of a 3x3 matrix M
109 // Assumes symmetric matrix; contents of matrix "M" are not modified by the routine
110 // Eigenvalues are sorted in decreasing order (not decreasing absolute-value order)
111 // Returned eigenvectors are unit length
112 friend void eigen_sym(Mat3d & M, Vec3d & eig_val, Vec3d eig_vec[3]);
113
114 // NOTE: This particular routine is not publicly released, as its implementation
115 // was taken from Numerical Recipes, which is not free software.
116 // (you can use the eigen_sym routine above which is public domain)
117 // Computes eigenvalues and eigenvectors of a 3x3 matrix, using Jacobi Iteration
118 // Assumes symmetric matrix; contents of matrix "M" are overwritten (destroyed)
119 // Returns true if iteration succeeded in making the sum of abs values of non-diagonal values below epsilon, and false otherwise
120 // Default epsilon is machine precision (which always converged with our matrices)
121 // Eigenvalues are sorted in decreasing absolute order
122 // Returned eigenvectors are unit length
123 friend bool eigen_sym_NR(Mat3d & M, Vec3d & eig_val, Vec3d eig_vec[3], int maxIterations, double epsilon);
124
125 /*
126 Standard SVD (modifiedSVD == 0):
127
128 Given a 3x3 matrix M, decomposes it using SVD so
129 that M = U Sigma V^T where U is a 3x3 rotation,
130 V is 3x3 orthonormal (V^T V = V V^T = I), and
131 Sigma is a diagonal matrix with non-negative entries,
132 in descending order, Sigma[0] >= Sigma[1] >= Sigma[2] >= 0.
133 Note that the matrix V may have a determinant equaling
134 to -1 (i.e., reflection).
135
136 singularValue_eps is a threshold to determine
137 when a singular value is deemed zero, and special handling is then invoked
138 to improve numerical robustness.
139
140 Modified SVD (modifiedSVD == 1):
141
142 The SVD is modified so that it gives the
143 following properties (useful in solid mechanics applications) :
144
145 1) Not just the determinant of U, but also the determinant of V is 1
146 (i.e., both U and V are rotations, not reflections).
147
148 2) The smallest diagonal value Sigma[2] may be negative. We have:
149 Sigma[0] >= Sigma[1] >= abs(Sigma[2]) >= 0 .
150 */
151 friend int SVD(Mat3d & M, Mat3d & U, Vec3d & Sigma, Mat3d & V, double singularValue_eps, int modifiedSVD);
152
153 const static Mat3d Identity;
154 const static Mat3d Zero;
155 protected:
156 Vec3d elt[3]; // the three rows of the matrix
157 };
158
159 // Computes eigenvalues and eigenvectors of a 3x3 matrix M
160 // Assumes symmetric matrix; contents of matrix "M" are not modified by the routine
161 // Eigenvalues are sorted in decreasing order (not decreasing absolute-value order)
162 // Returned eigenvectors are unit length
163 void eigen_sym(Mat3d & M, Vec3d & eig_val, Vec3d eig_vec[3]=NULL);
164
165 // inverse of a 3x3 matrix (suitable when A is given by a pointer to the 9 entries)
166 // input: A (row-major)
167 // output: AInv
168 void inverse3x3(const double * A, double * AInv);
169
170 // these two declarations are repeated here, with default arguments, to conform to the C++ standard; this makes it possible to compile the code on Apple's clang compiler
171 bool eigen_sym_NR(Mat3d & M, Vec3d & eig_val, Vec3d eig_vec[3], int maxIterations=50, double epsilon=0.0);
172 int SVD(Mat3d & M, Mat3d & U, Vec3d & Sigma, Mat3d & V, double singularValue_eps=1e-8, int modifiedSVD=0);
173
174 // ===== below is the implementation =====
175
176 inline Mat3d::Mat3d(double x0_g, double x1_g, double x2_g,
177 double x3_g, double x4_g, double x5_g,
178 double x6_g, double x7_g, double x8_g)
179 {
180 elt[0] = Vec3d(x0_g,x1_g,x2_g);
181 elt[1] = Vec3d(x3_g,x4_g,x5_g);
182 elt[2] = Vec3d(x6_g,x7_g,x8_g);
183 }
184
185 inline Mat3d::Mat3d(const double mat[9])
186 {
187 elt[0] = Vec3d(mat[0],mat[1],mat[2]);
188 elt[1] = Vec3d(mat[3],mat[4],mat[5]);
189 elt[2] = Vec3d(mat[6],mat[7],mat[8]);
190 }
191
192 inline Mat3d::Mat3d(const Vec3d rows[3])
193 {
194 elt[0] = rows[0];
195 elt[1] = rows[1];
196 elt[2] = rows[2];
197 }
198
199 inline Mat3d::Mat3d(const Vec3d & row0, const Vec3d & row1, const Vec3d & row2)
200 {
201 elt[0] = row0;
202 elt[1] = row1;
203 elt[2] = row2;
204 }
205
206 inline Mat3d::Mat3d(double diag)
207 {
208 elt[0] = Vec3d(diag,0,0);
209 elt[1] = Vec3d(0,diag,0);
210 elt[2] = Vec3d(0,0,diag);
211 }
212
213 inline void Mat3d::set(double x0, double x1, double x2,
214 double x3, double x4, double x5,
215 double x6, double x7, double x8)
216 {
217 elt[0][0] = x0;
218 elt[0][1] = x1;
219 elt[0][2] = x2;
220
221 elt[1][0] = x3;
222 elt[1][1] = x4;
223 elt[1][2] = x5;
224
225 elt[2][0] = x6;
226 elt[2][1] = x7;
227 elt[2][2] = x8;
228 }
229
230 inline void Mat3d::set(double value)
231 {
232 elt[0][0] = elt[0][1] = elt[0][2] =
233 elt[1][0] = elt[1][1] = elt[1][2] =
234 elt[2][0] = elt[2][1] = elt[2][2] = value;
235 }
236
237 inline Mat3d & Mat3d::operator=(const Mat3d & source)
238 {
239 elt[0] = source.elt[0];
240 elt[1] = source.elt[1];
241 elt[2] = source.elt[2];
242
243 return *this;
244 }
245
246 inline Mat3d Mat3d::operator+ (const Mat3d & mat2) const
247 {
248 Mat3d sum = *this;
249 sum.elt[0] += mat2.elt[0];
250 sum.elt[1] += mat2.elt[1];
251 sum.elt[2] += mat2.elt[2];
252
253 return sum;
254 }
255
256 inline Mat3d & Mat3d::operator+= (const Mat3d & mat2)
257 {
258 elt[0] += mat2.elt[0];
259 elt[1] += mat2.elt[1];
260 elt[2] += mat2.elt[2];
261 return *this;
262 }
263
264 inline Mat3d Mat3d::operator- (const Mat3d & mat2) const
265 {
266 Mat3d sum = *this;
267 sum.elt[0] -= mat2.elt[0];
268 sum.elt[1] -= mat2.elt[1];
269 sum.elt[2] -= mat2.elt[2];
270
271 return sum;
272 }
273
274 inline Mat3d & Mat3d::operator-= (const Mat3d & mat2)
275 {
276 elt[0] -= mat2.elt[0];
277 elt[1] -= mat2.elt[1];
278 elt[2] -= mat2.elt[2];
279
280 return *this;
281 }
282
283 inline Vec3d & Mat3d::operator[] (int index)
284 {
285 return elt[index];
286 }
287
288 inline const Vec3d & Mat3d::operator[] (int index) const
289 {
290 return elt[index];
291 }
292
293 inline Mat3d & Mat3d::operator*= (double scalar)
294 {
295 elt[0] *= scalar;
296 elt[1] *= scalar;
297 elt[2] *= scalar;
298
299 return *this;
300 }
301
302 inline bool Mat3d::operator== (const Mat3d & mat2) const
303 {
304 return elt[0] == mat2.elt[0] && elt[1] == mat2.elt[1] && elt[2] == mat2.elt[2];
305 }
306
307 inline bool Mat3d::operator!= (const Mat3d & mat2) const
308 {
309 return elt[0] != mat2.elt[0] || elt[1] != mat2.elt[1] || elt[2] != mat2.elt[2];
310 }
311
312 inline Mat3d & Mat3d::multiplyDiagRight(Vec3d & v) // M = M * diag(v)
313 {
314 elt[0][0] *= v[0];
315 elt[1][0] *= v[0];
316 elt[2][0] *= v[0];
317
318 elt[0][1] *= v[1];
319 elt[1][1] *= v[1];
320 elt[2][1] *= v[1];
321
322 elt[0][2] *= v[2];
323 elt[1][2] *= v[2];
324 elt[2][2] *= v[2];
325
326 return *this;
327 }
328
329 inline Mat3d & Mat3d::multiplyDiagLeft(Vec3d & v) // M = diag(v) * M
330 {
331 elt[0] *= v[0];
332 elt[1] *= v[1];
333 elt[2] *= v[2];
334
335 return *this;
336 }
337
338 inline Mat3d operator* (double scalar, const Mat3d & mat2)
339 {
340 Mat3d result = mat2;
341 result.elt[0] *= scalar;
342 result.elt[1] *= scalar;
343 result.elt[2] *= scalar;
344
345 return result;
346 }
347
348 inline Mat3d operator/ (const Mat3d & mat2, double scalar)
349 {
350 Mat3d result = mat2;
351 result.elt[0] /= scalar;
352 result.elt[1] /= scalar;
353 result.elt[2] /= scalar;
354
355 return result;
356 }
357
358 inline Mat3d tensorProduct(const Vec3d & vecA, const Vec3d & vecB)
359 {
360 Mat3d result(vecA[0]*vecB[0],vecA[0]*vecB[1],vecA[0]*vecB[2],
361 vecA[1]*vecB[0],vecA[1]*vecB[1],vecA[1]*vecB[2],
362 vecA[2]*vecB[0],vecA[2]*vecB[1],vecA[2]*vecB[2]);
363
364 return result;
365 }
366
367 inline Mat3d & Mat3d::operator/= (double scalar)
368 {
369 elt[0] /= scalar;
370 elt[1] /= scalar;
371 elt[2] /= scalar;
372
373 return *this;
374 }
375
376 inline const Vec3d Mat3d::operator* (const Vec3d & vec) const
377 {
378 return(Vec3d(
379 dot(elt[0],vec),
380 dot(elt[1],vec),
381 dot(elt[2],vec)));
382 }
383
384 inline const Mat3d Mat3d::operator* (const Mat3d & mat2) const
385 {
386 return(Mat3d(
387 dot(elt[0],Vec3d(mat2.elt[0][0],mat2.elt[1][0],mat2.elt[2][0])),
388 dot(elt[0],Vec3d(mat2.elt[0][1],mat2.elt[1][1],mat2.elt[2][1])),
389 dot(elt[0],Vec3d(mat2.elt[0][2],mat2.elt[1][2],mat2.elt[2][2])),
390
391 dot(elt[1],Vec3d(mat2.elt[0][0],mat2.elt[1][0],mat2.elt[2][0])),
392 dot(elt[1],Vec3d(mat2.elt[0][1],mat2.elt[1][1],mat2.elt[2][1])),
393 dot(elt[1],Vec3d(mat2.elt[0][2],mat2.elt[1][2],mat2.elt[2][2])),
394
395 dot(elt[2],Vec3d(mat2.elt[0][0],mat2.elt[1][0],mat2.elt[2][0])),
396 dot(elt[2],Vec3d(mat2.elt[0][1],mat2.elt[1][1],mat2.elt[2][1])),
397 dot(elt[2],Vec3d(mat2.elt[0][2],mat2.elt[1][2],mat2.elt[2][2])) ));
398 }
399
400 inline Mat3d inv(const Mat3d & mat)
401 {
402 double A[9];
403 mat.convertToArray(A);
404 double AInv[9];
405 inverse3x3(A, AInv);
406
407 return Mat3d(AInv);
408 }
409
410 // inverse of a 3x3 matrix
411 // row-major format
412 inline void inverse3x3(const double * A, double * AInv)
413 {
414 // converted to C from Mathematica output
415 AInv[0] = -A[5] * A[7] + A[4] * A[8];
416 AInv[1] = A[2] * A[7] - A[1] * A[8];
417 AInv[2] = -A[2] * A[4] + A[1] * A[5];
418 AInv[3] = A[5] * A[6] - A[3] * A[8];
419 AInv[4] = -A[2] * A[6] + A[0] * A[8];
420 AInv[5] = A[2] * A[3] - A[0] * A[5];
421 AInv[6] = -A[4] * A[6] + A[3] * A[7];
422 AInv[7] = A[1] * A[6] - A[0] * A[7];
423 AInv[8] = -A[1] * A[3] + A[0] * A[4];
424
425 double invDet = 1.0 / (A[0] * AInv[0] + A[1] * AInv[3] + A[2] * AInv[6]);
426
427 for(int i=0; i<9; i++)
428 AInv[i] *= invDet;
429 }
430
431 inline double det(const Mat3d & mat)
432 {
433 return
434 (-mat[0][2] * mat[1][1] * mat[2][0] +
435 mat[0][1] * mat[1][2] * mat[2][0] +
436 mat[0][2] * mat[1][0] * mat[2][1] -
437 mat[0][0] * mat[1][2] * mat[2][1] -
438 mat[0][1] * mat[1][0] * mat[2][2] +
439 mat[0][0] * mat[1][1] * mat[2][2] );
440 }
441
442 inline Mat3d trans(const Mat3d & mat)
443 {
444 return
445 Mat3d( mat[0][0], mat[1][0], mat[2][0],
446 mat[0][1], mat[1][1], mat[2][1],
447 mat[0][2], mat[1][2], mat[2][2] );
448 }
449
450 // compute [ 0 -v2 v1 ]
451 // [ v2 0 -v0 ]
452 // [ -v1 v0 0 ]
453 inline Mat3d skewSymmetricMatrix(const Vec3d & vec)
454 {
455 return Mat3d(0, -vec[2], vec[1],
456 vec[2], 0, -vec[0],
457 -vec[1], vec[0], 0);
458 }
459
460 inline void Mat3d::convertToArray(double * array) const // in row-major order
461 {
462 array[0] = elt[0][0]; array[1] = elt[0][1]; array[2] = elt[0][2];
463 array[3] = elt[1][0]; array[4] = elt[1][1]; array[5] = elt[1][2];
464 array[6] = elt[2][0]; array[7] = elt[2][1]; array[8] = elt[2][2];
465 }
466
467 inline std::ostream &operator << (std::ostream &s, const Mat3d &v)
468 {
469 double a00 = v.elt[0][0]; double a01 = v.elt[0][1]; double a02 = v.elt[0][2];
470 double a10 = v.elt[1][0]; double a11 = v.elt[1][1]; double a12 = v.elt[1][2];
471 double a20 = v.elt[2][0]; double a21 = v.elt[2][1]; double a22 = v.elt[2][2];
472
473 return(
474 s << '[' << a00 << ' ' << a01 << ' ' << a02 << ']' << '\n'
475 << '[' << a10 << ' ' << a11 << ' ' << a12 << ']' << '\n'
476 << '[' << a20 << ' ' << a21 << ' ' << a22 << ']'
477 );
478 }
479
480 inline void Mat3d::print() const
481 {
482 double a00 = elt[0][0]; double a01 = elt[0][1]; double a02 = elt[0][2];
483 double a10 = elt[1][0]; double a11 = elt[1][1]; double a12 = elt[1][2];
484 double a20 = elt[2][0]; double a21 = elt[2][1]; double a22 = elt[2][2];
485
486 printf("[%G %G %G;\n", a00, a01, a02);
487 printf(" %G %G %G;\n", a10, a11, a12);
488 printf(" %G %G %G]\n", a20, a21, a22);
489 }
490
491 inline double Mat3d::maxAbsEntry() const
492 {
493 double maxAbsEntry = 0.0;
494 for(int i = 0; i < 3; i++)
495 for(int j = 0; j < 3; j++)
496 {
497 if (fabs(elt[i][j]) > maxAbsEntry)
498 maxAbsEntry = fabs(elt[i][j]);
499 }
500 return maxAbsEntry;
501 }
502
503 inline bool Mat3d::hasNaN() const
504 {
505 return elt[0].hasNaN() || elt[1].hasNaN() || elt[2].hasNaN();
506 }
507
508 #endif
509