| 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 |
#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 |
|
| 49 |
|
| 50 |
|
| 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]); |
| 56 |
inline Mat3d(const Vec3d rows[3]); |
| 57 |
inline Mat3d(const Vec3d & row0, const Vec3d & row1, const Vec3d & row2); |
| 58 |
inline Mat3d(double diag); |
| 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); |
| 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); |
| 83 |
inline Mat3d & multiplyDiagLeft(Vec3d & v); |
| 84 |
|
| 85 |
friend inline Mat3d tensorProduct(const Vec3d & vec1, const Vec3d & vec2); |
| 86 |
|
| 87 |
friend inline Mat3d inv(const Mat3d &); |
| 88 |
friend inline double det(const Mat3d & mat); |
| 89 |
friend inline Mat3d trans(const Mat3d & mat); |
| 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); |
| 97 |
inline const Vec3d & operator[] (int index) const; |
| 98 |
|
| 99 |
|
| 100 |
inline void convertToArray(double * array) const; |
| 101 |
|
| 102 |
|
| 103 |
inline const Vec3d operator* (const Vec3d & vec) const; |
| 104 |
|
| 105 |
|
| 106 |
inline const Mat3d operator* (const Mat3d & mat2) const; |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
friend void eigen_sym(Mat3d & M, Vec3d & eig_val, Vec3d eig_vec[3]); |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
friend bool eigen_sym_NR(Mat3d & M, Vec3d & eig_val, Vec3d eig_vec[3], int maxIterations, double epsilon); |
| 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 |
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]; |
| 157 |
}; |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
void eigen_sym(Mat3d & M, Vec3d & eig_val, Vec3d eig_vec[3]=NULL); |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
void inverse3x3(const double * A, double * AInv); |
| 169 |
|
| 170 |
|
| 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 |
|
| 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) |
| 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) |
| 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 |
|
| 411 |
|
| 412 |
inline void inverse3x3(const double * A, double * AInv) |
| 413 |
{ |
| 414 |
|
| 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 |
|
| 451 |
|
| 452 |
|
| 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 |
| 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 |
|