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 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738
|
//##########################################################################
//# #
//# CCLIB #
//# #
//# This program is free software; you can redistribute it and/or modify #
//# it under the terms of the GNU Library General Public License as #
//# published by the Free Software Foundation; version 2 or later of the #
//# License. #
//# #
//# This program is distributed in the hope that it will be useful, #
//# but WITHOUT ANY WARRANTY; without even the implied warranty of #
//# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
//# GNU General Public License for more details. #
//# #
//# COPYRIGHT: EDF R&D / TELECOM ParisTech (ENST-TSI) #
//# #
//##########################################################################
#ifndef SQUARE_MATRIX_HEADER
#define SQUARE_MATRIX_HEADER
//local
#include "CCGeom.h"
//system
#include <cassert>
#include <cstdio>
#include <cstring>
#include <vector>
namespace CCLib
{
//! Square matrix
/** Row-major ordered matrix (i.e. elements are accessed with 'values[row][column]')
**/
template <typename Scalar> class SquareMatrixTpl
{
public:
//! Default constructor
/** Warning: invalid matrix.
**/
SquareMatrixTpl() { init(0); }
//! Constructor with a given size
/** \param size the (square) matrix dimension
**/
SquareMatrixTpl(unsigned size) { init(size); }
//! Constructor from another matrix
/** \param mat matrix
**/
SquareMatrixTpl(const SquareMatrixTpl& mat)
{
if (init(mat.m_matrixSize))
{
*this = mat;
}
}
//! "From OpenGl" constructor (float version)
/** The matrix dimension is automatically set to 4.
It can be forced to 3 (size_3 = true). In this
case, only the rotation part will be 'imported'.
\param M16f a table of 16 floats (OpenGL float transformation matrix)
\param rotationOnly consider only the roation part (3x3 matrix)
**/
SquareMatrixTpl(const float M16f[], bool rotationOnly = false)
{
unsigned size = (rotationOnly ? 3 : 4);
if (init(size))
{
for (unsigned r = 0; r < size; r++)
for (unsigned c = 0; c < size; c++)
m_values[r][c] = static_cast<Scalar>(M16f[c * 4 + r]);
}
}
//! "From OpenGl" constructor (double version)
/** The matrix dimension is automatically set to 4.
It can be forced to 3 (size_3 = true). In this
case, only the rotation part will be 'imported'.
\param M16d a table of 16 floats (OpenGL double transformation matrix)
\param rotationOnly consider only the roation part (3x3 matrix)
**/
SquareMatrixTpl(const double M16d[], bool rotationOnly = false)
{
unsigned size = (rotationOnly ? 3 : 4);
if (init(size))
{
for (unsigned r = 0; r < size; r++)
for (unsigned c = 0; c < size; c++)
m_values[r][c] = static_cast<Scalar>(M16d[c * 4 + r]);
}
}
//! Default destructor
virtual ~SquareMatrixTpl()
{
invalidate();
}
//! Returns matrix size
inline unsigned size() const { return m_matrixSize; }
//! Returns matrix validity
/** Matrix is invalid if its size is 0!
**/
inline bool isValid() const { return (m_matrixSize != 0); }
//! Invalidates matrix
/** Size is reset to 0.
**/
void invalidate()
{
delete [] m_underlyingData;
m_underlyingData = nullptr;
delete [] m_values;
m_values = nullptr;
m_matrixSize = 0;
matrixSquareSize = 0;
}
//! The matrix rows
/** public for easy/fast access
**/
Scalar** m_values = nullptr;
//! Returns pointer to matrix row
inline Scalar* row(unsigned index) { return m_values[index]; }
//! Sets a particular matrix value
void inline setValue(unsigned row, unsigned column, Scalar value)
{
m_values[row][column] = value;
}
//! Returns a particular matrix value
Scalar inline getValue(unsigned row, unsigned column) const
{
return m_values[row][column];
}
//! Matrix copy operator
SquareMatrixTpl& operator = (const SquareMatrixTpl& B)
{
if (m_matrixSize != B.size())
{
invalidate();
init(B.size());
}
for (unsigned r = 0; r < m_matrixSize; r++)
for (unsigned c = 0; c < m_matrixSize; c++)
m_values[r][c] = B.m_values[r][c];
return *this;
}
//! Addition
SquareMatrixTpl operator + (const SquareMatrixTpl& B) const
{
SquareMatrixTpl C = *this;
C += B;
return C;
}
//! In-place addition
const SquareMatrixTpl& operator += (const SquareMatrixTpl& B)
{
assert(B.size() == m_matrixSize);
for (unsigned r = 0; r < m_matrixSize; r++)
for (unsigned c = 0; c < m_matrixSize; c++)
m_values[r][c] += B.m_values[r][c];
return *this;
}
//! Subtraction
SquareMatrixTpl operator - (const SquareMatrixTpl& B) const
{
SquareMatrixTpl C = *this;
C -= B;
return C;
}
//! In-place subtraction
const SquareMatrixTpl& operator -= (const SquareMatrixTpl& B)
{
assert(B.size() == m_matrixSize);
for (unsigned r = 0; r < m_matrixSize; r++)
for (unsigned c = 0; c < m_matrixSize; c++)
m_values[r][c] -= B.m_values[r][c];
return *this;
}
//! Multiplication (M = A*B)
SquareMatrixTpl operator * (const SquareMatrixTpl& B) const
{
assert(B.size() == m_matrixSize);
SquareMatrixTpl C(m_matrixSize);
for (unsigned r = 0; r < m_matrixSize; r++)
{
for (unsigned c = 0; c < m_matrixSize; c++)
{
Scalar sum = 0;
for (unsigned k = 0; k < m_matrixSize; k++)
sum += m_values[r][k] * B.m_values[k][c];
C.m_values[r][c] = sum;
}
}
return C;
}
//! Multiplication by a vector
inline CCVector3 operator * (const CCVector3& V) const
{
if (m_matrixSize == 3)
{
CCVector3 result;
apply(V.u, result.u);
return result;
}
else
{
return V;
}
}
//! In-place multiplication
inline const SquareMatrixTpl& operator *= (const SquareMatrixTpl& B)
{
*this = (*this) * B;
return *this;
}
//! Multiplication by a vector
/** Vec must have the same size as matrix.
Returns result = M.Vec.
**/
void apply(const Scalar Vec[], Scalar result[]) const
{
for (unsigned r = 0; r < m_matrixSize; r++)
{
Scalar sum = 0;
for (unsigned k = 0; k < m_matrixSize; k++)
sum += m_values[r][k] * static_cast<Scalar>(Vec[k]);
result[r] = sum;
}
}
//! In-place transpose
void transpose()
{
for (unsigned r = 0; r < m_matrixSize - 1; r++)
for (unsigned c = r + 1; c < m_matrixSize; c++)
std::swap(m_values[r][c], m_values[c][r]);
}
//! Returns the transposed version of this matrix
SquareMatrixTpl transposed() const
{
SquareMatrixTpl T(*this);
T.transpose();
return T;
}
//! Sets all elements to 0
void clear()
{
for (unsigned r = 0; r < m_matrixSize; ++r)
{
memset(m_values[r], 0, sizeof(Scalar)*m_matrixSize);
}
}
//! Returns inverse (Gauss)
SquareMatrixTpl inv() const
{
//we create the n by 2n matrix, composed of this matrix and the identity
Scalar** tempM = nullptr;
{
tempM = new Scalar*[m_matrixSize];
if (!tempM)
{
//not enough memory
return SquareMatrixTpl();
}
for (unsigned i = 0; i < m_matrixSize; i++)
{
tempM[i] = new Scalar[2 * m_matrixSize];
if (!tempM[i])
{
//not enough memory
for (unsigned j = 0; j < i; j++)
delete[] tempM[j];
delete[] tempM;
return SquareMatrixTpl();
}
}
}
//identity
{
for (unsigned i = 0; i < m_matrixSize; i++)
{
for (unsigned j = 0; j < m_matrixSize; j++)
{
tempM[i][j] = m_values[i][j];
if (i == j)
tempM[i][j + m_matrixSize] = 1;
else
tempM[i][j + m_matrixSize] = 0;
}
}
}
//Gauss pivot
{
for (unsigned i = 0; i < m_matrixSize; i++)
{
//we look for the pivot value (first non zero element)
unsigned j = i;
while (tempM[j][i] == 0)
{
if (++j >= m_matrixSize)
{
//non inversible matrix!
for (unsigned k = 0; k < m_matrixSize; ++k)
delete[] tempM[k];
delete[] tempM;
return SquareMatrixTpl();
}
}
//swap the 2 rows if they are different
//(we only start by the ith element (as all the others are zero!)
if (i != j)
for (unsigned k = i; k < 2 * m_matrixSize; k++)
std::swap(tempM[i][k], tempM[j][k]);
//we scale the matrix to make the pivot equal to 1
if (tempM[i][i] != 1.0)
{
const Scalar& tmpVal = tempM[i][i];
for (unsigned k = i; k < 2 * m_matrixSize; ++k)
tempM[i][k] /= tmpVal;
}
//after the pivot value, all elements are set to zero
for (unsigned j = i + 1; j < m_matrixSize; j++)
{
if (tempM[j][i] != 0)
{
const Scalar& tmpVal = tempM[j][i];
for (unsigned k = i; k < 2 * m_matrixSize; k++)
tempM[j][k] -= tempM[i][k] * tmpVal;
}
}
}
}
//reduction
{
for (unsigned i = m_matrixSize - 1; i > 0; i--)
{
for (unsigned j = 0; j < i; j++)
{
if (tempM[j][i] != 0)
{
const Scalar& tmpVal = tempM[j][i];
for (unsigned k = i; k < 2 * m_matrixSize; k++)
tempM[j][k] -= tempM[i][k] * tmpVal;
}
}
}
}
//result: second part or tempM
SquareMatrixTpl result(m_matrixSize);
{
for (unsigned i = 0; i < m_matrixSize; i++)
for (unsigned j = 0; j < m_matrixSize; j++)
result.m_values[i][j] = tempM[i][j + m_matrixSize];
}
//we release temp matrix from memory
{
for (unsigned i = 0; i < m_matrixSize; i++)
delete[] tempM[i];
delete[] tempM;
tempM = nullptr;
}
return result;
}
//! Prints out matrix to console or file
/** \param fp ASCII FILE handle (or 0 to print to console)
**/
void print(FILE* fp = nullptr) const
{
for (unsigned r = 0; r < m_matrixSize; r++)
{
for (unsigned c = 0; c < m_matrixSize; c++)
{
if (fp)
fprintf(fp, "%6.6f ", m_values[r][c]);
else
printf("%6.6f ", m_values[r][c]);
}
if (fp)
fprintf(fp, "\n");
else
printf("\n");
}
}
//! Sets matrix to identity
void toIdentity()
{
clear();
for (unsigned r = 0; r < m_matrixSize; r++)
m_values[r][r] = 1;
}
//! Scales matrix (all elements are multiplied by the same coef.)
void scale(Scalar coef)
{
for (unsigned r = 0; r < m_matrixSize; r++)
for (unsigned c = 0; c < m_matrixSize; c++)
m_values[r][c] *= coef;
}
//! Returns trace
Scalar trace() const
{
Scalar trace = 0;
for (unsigned r = 0; r < m_matrixSize; r++)
trace += m_values[r][r];
return trace;
}
//! Returns determinant
double computeDet() const
{
return computeSubDet(m_values, m_matrixSize);
}
//! Creates a rotation matrix from a quaternion (float version)
/** Shortcut to double version of initFromQuaternion)
\param q normalized quaternion (4 float values)
**/
void initFromQuaternion(const float q[])
{
double qd[4] = { static_cast<double>(q[0]),
static_cast<double>(q[1]),
static_cast<double>(q[2]),
static_cast<double>(q[3]) };
initFromQuaternion(qd);
}
//! Creates a rotation matrix from a quaternion (double version)
/** Quaternion is composed of 4 values: an angle (cos(alpha/2))
and an axis (sin(alpha/2)*unit vector).
\param q normalized quaternion (w,x,y,z)
**/
void initFromQuaternion(const double q[])
{
if (m_matrixSize == 0)
if (!init(3))
return;
assert(m_matrixSize == 3);
double q00 = q[0] * q[0];
double q11 = q[1] * q[1];
double q22 = q[2] * q[2];
double q33 = q[3] * q[3];
double q03 = q[0] * q[3];
double q13 = q[1] * q[3];
double q23 = q[2] * q[3];
double q02 = q[0] * q[2];
double q12 = q[1] * q[2];
double q01 = q[0] * q[1];
m_values[0][0] = static_cast<Scalar>(q00 + q11 - q22 - q33);
m_values[1][1] = static_cast<Scalar>(q00 - q11 + q22 - q33);
m_values[2][2] = static_cast<Scalar>(q00 - q11 - q22 + q33);
m_values[0][1] = static_cast<Scalar>(2.0*(q12 - q03));
m_values[1][0] = static_cast<Scalar>(2.0*(q12 + q03));
m_values[0][2] = static_cast<Scalar>(2.0*(q13 + q02));
m_values[2][0] = static_cast<Scalar>(2.0*(q13 - q02));
m_values[1][2] = static_cast<Scalar>(2.0*(q23 - q01));
m_values[2][1] = static_cast<Scalar>(2.0*(q23 + q01));
}
//! Converts rotation matrix to quaternion
/** Warning: for 3x3 matrix only!
From libE57 'best practices' (http://www.libe57.org/best.html)
\param q quaternion (w,x,y,z)
\return success
**/
bool toQuaternion(double q[/*4*/])
{
if (m_matrixSize != 3)
return false;
double dTrace = static_cast<double>(m_values[0][0])
+ static_cast<double>(m_values[1][1])
+ static_cast<double>(m_values[2][2])
+ 1.0;
double w, x, y, z; //quaternion
if (dTrace > 1.0e-6)
{
double S = 2.0 * sqrt(dTrace);
x = (m_values[2][1] - m_values[1][2]) / S;
y = (m_values[0][2] - m_values[2][0]) / S;
z = (m_values[1][0] - m_values[0][1]) / S;
w = 0.25 * S;
}
else if (m_values[0][0] > m_values[1][1] && m_values[0][0] > m_values[2][2])
{
double S = sqrt(1.0 + m_values[0][0] - m_values[1][1] - m_values[2][2]) * 2.0;
x = 0.25 * S;
y = (m_values[1][0] + m_values[0][1]) / S;
z = (m_values[0][2] + m_values[2][0]) / S;
w = (m_values[2][1] - m_values[1][2]) / S;
}
else if (m_values[1][1] > m_values[2][2])
{
double S = sqrt(1.0 + m_values[1][1] - m_values[0][0] - m_values[2][2]) * 2.0;
x = (m_values[1][0] + m_values[0][1]) / S;
y = 0.25 * S;
z = (m_values[2][1] + m_values[1][2]) / S;
w = (m_values[0][2] - m_values[2][0]) / S;
}
else
{
double S = sqrt(1.0 + m_values[2][2] - m_values[0][0] - m_values[1][1]) * 2.0;
x = (m_values[0][2] + m_values[2][0]) / S;
y = (m_values[2][1] + m_values[1][2]) / S;
z = 0.25 * S;
w = (m_values[1][0] - m_values[0][1]) / S;
}
// normalize the quaternion if the matrix is not a clean rigid body matrix or if it has scaler information.
double len = sqrt(w*w + x*x + y*y + z*z);
if (len != 0)
{
q[0] = w / len;
q[1] = x / len;
q[2] = y / len;
q[3] = z / len;
return true;
}
return false;
}
//! Returns Delta-determinant (see Kramer formula)
Scalar deltaDeterminant(unsigned column, Scalar* Vec) const
{
SquareMatrixTpl mat(m_matrixSize);
for (unsigned i = 0; i < m_matrixSize; i++)
{
if (column == i)
{
for (unsigned j = 0; j < m_matrixSize; j++)
{
mat.m_values[j][i] = static_cast<Scalar>(*Vec);
Vec++;
}
}
else
{
for (unsigned j = 0; j < m_matrixSize; j++)
mat.m_values[j][i] = m_values[j][i];
}
}
return mat.computeDet();
}
//! Converts a 3*3 or 4*4 matrix to an OpenGL-style float matrix (float[16])
void toGlMatrix(float M16f[]) const
{
assert(m_matrixSize == 3 || m_matrixSize == 4);
memset(M16f, 0, sizeof(float) * 16);
for (unsigned r = 0; r < 3; r++)
for (unsigned c = 0; c < 3; c++)
M16f[r + c * 4] = static_cast<float>(m_values[r][c]);
if (m_matrixSize == 4)
for (unsigned r = 0; r < 3; r++)
{
M16f[12 + r] = static_cast<float>(m_values[3][r]);
M16f[3 + r * 4] = static_cast<float>(m_values[r][3]);
}
M16f[15] = 1.0f;
}
//! Converts a 3*3 or 4*4 matrix to an OpenGL-style double matrix (double[16])
void toGlMatrix(double M16d[]) const
{
assert(m_matrixSize == 3 || m_matrixSize == 4);
memset(M16d, 0, sizeof(double) * 16);
for (unsigned r = 0; r < 3; r++)
for (unsigned c = 0; c < 3; c++)
M16d[r + c * 4] = static_cast<double>(m_values[r][c]);
if (m_matrixSize == 4)
{
for (unsigned r = 0; r < 3; r++)
{
M16d[12 + r] = static_cast<double>(m_values[3][r]);
M16d[3 + r * 4] = static_cast<double>(m_values[r][3]);
}
}
M16d[15] = 1.0;
}
private:
//! Internal initialization
/** \return initilization success
**/
bool init(unsigned size)
{
m_matrixSize = size;
matrixSquareSize = m_matrixSize*m_matrixSize;
if ( size == 0 )
{
return true;
}
m_values = new Scalar*[m_matrixSize]{};
m_underlyingData = new Scalar[matrixSquareSize]{};
if ( (m_values == nullptr) || (m_underlyingData == nullptr) )
{
return false;
}
for (unsigned i = 0; i < m_matrixSize; i++)
{
m_values[i] = m_underlyingData + (i * m_matrixSize);
}
return true;
}
//! Computes sub-matrix determinant
double computeSubDet(Scalar** mat, unsigned matSize) const
{
if (matSize == 2)
{
return static_cast<double>(mat[0][0] * mat[1][1] - mat[0][1] * mat[1][0]);
}
Scalar** subMat = new Scalar*[matSize - 1];
if (subMat)
{
double subDet = 0;
double sign = 1.0;
for (unsigned row = 0; row < matSize; row++)
{
unsigned k = 0;
for (unsigned i = 0; i < matSize; i++)
if (i != row)
subMat[k++] = mat[i] + 1;
subDet += static_cast<double>(mat[row][0]) * computeSubDet(subMat, matSize - 1) * sign;
sign = -sign;
}
delete[] subMat;
return subDet;
}
else
{
//not enough memory
return 0.0;
}
}
//! Matrix size
unsigned m_matrixSize;
//! Matrix square-size
unsigned matrixSquareSize;
//! Stores the actual data, indexed by m_values
Scalar *m_underlyingData = nullptr;
};
//! Default CC square matrix type (PointCoordinateType)
using SquareMatrix = SquareMatrixTpl<PointCoordinateType>;
//! Float square matrix type
using SquareMatrixf = SquareMatrixTpl<float>;
//! Double square matrix type
using SquareMatrixd = SquareMatrixTpl<double>;
} //namespace CCLib
#endif //SQUARE_MATRIX_HEADER
|