1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #pragma once19 20 #include "../MathBuildConfig.h"21 #include "[SSEMath.h]"22 #include "[float4.h]"23 24 #ifdef MATH_ENABLE_STL_SUPPORT25 #include "[myassert.h]"26 #endif27 #include "../MathGeoLibFwd.h"28 #include "[MatrixProxy.h]"29 30 #ifdef MATH_OGRE_INTEROP31 #include <OgreMatrix4.h>32 #endif33 #ifdef MATH_QT_INTEROP34 #include <QMatrix4x4>35 #endif36 #ifdef MATH_URHO3D_INTEROP37 #include <Urho3D/Math/Matrix4.h>38 #endif39 40 [MATH_BEGIN_NAMESPACE]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 class [ALIGN_MAT] [float4x4]72 {73 public:74 75 enum { Rows = 4 };76 77 78 enum { Cols = 4 };79 80 81 82 #if defined(MATH_SIMD)83 union84 {85 #endif86 float v[Rows][Cols];87 #ifdef MATH_AVX88 __m256 row2[2];89 #endif90 #if defined(MATH_SIMD)91 simd4f row[4];92 };93 #endif94 95 96 static const [float4x4] [zero];97 98 99 100 101 102 103 104 105 static const [float4x4] [identity];106 107 108 109 110 111 static const [float4x4] [nan];112 113 114 115 [float4x4]() {}116 117 #ifdef MATH_EXPLICIT_COPYCTORS118 119 120 [float4x4](const [float4x4] &rhs) { Set(rhs); }121 #endif122 123 124 125 126 [float4x4](float _00, float _01, float _02, float _03,127 float _10, float _11, float _12, float _13,128 float _20, float _21, float _22, float _23,129 float _30, float _31, float _32, float _33);130 131 132 133 [float4x4](const [float3x3] &other);134 135 136 137 [float4x4](const [float3x4] &other);138 139 140 141 142 143 144 145 146 147 148 [float4x4](const [float4] &col0, const [float4] &col1, const [float4] &col2, const [float4] &col3);149 150 151 explicit [float4x4](const [Quat] &orientation);152 153 154 155 [float4x4](const [Quat] &orientation, const [float3] &translation);156 157 158 static [TranslateOp] Translate(float tx, float ty, float tz);159 static [TranslateOp] Translate(const [float3] &offset);160 161 162 163 164 165 166 167 static [float4x4] RotateX(float angleRadians, const [float3] &pointOnAxis);168 static [float4x4] RotateX(float angleRadians);169 170 static [float4x4] RotateY(float angleRadians);171 172 static [float4x4] RotateY(float angleRadians, const [float3] &pointOnAxis);173 174 static [float4x4] RotateZ(float angleRadians);175 176 static [float4x4] RotateZ(float angleRadians, const [float3] &pointOnAxis);177 178 179 180 181 182 183 static [float4x4] RotateAxisAngle(const [float3] &axisDirection, float angleRadians, const [float3] &pointOnAxis);184 static [float4x4] RotateAxisAngle(const [float3] &axisDirection, float angleRadians);185 186 187 188 189 190 191 192 193 194 static [float4x4] RotateFromTo(const [float3] &sourceDirection, const [float3] &targetDirection, const [float3] ¢erPoint);195 static [float4x4] RotateFromTo(const [float3] &sourceDirection, const [float3] &targetDirection);196 197 198 199 200 static [float4x4] RandomGeneral([LCG] &lcg, float minElem, float maxElem);201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 static [float4x4] RotateFromTo(const [float3] &sourceDirection, const [float3] &targetDirection,216 const [float3] &sourceDirection2, const [float3] &targetDirection2, const [float3] ¢erPoint);217 static [float4x4] RotateFromTo(const [float3] &sourceDirection, const [float3] &targetDirection,218 const [float3] &sourceDirection2, const [float3] &targetDirection2);219 220 221 static [float4x4] FromQuat(const [Quat] &orientation);222 223 static [float4x4] FromQuat(const [Quat] &orientation, const [float3] &pointOnAxis);224 225 226 227 228 229 230 static [float4x4] FromTRS(const [float3] &translate, const [Quat] &rotate, const [float3] &scale);231 static [float4x4] FromTRS(const [float3] &translate, const [float3x3] &rotate, const [float3] &scale);232 static [float4x4] FromTRS(const [float3] &translate, const [float3x4] &rotate, const [float3] &scale);233 static [float4x4] FromTRS(const [float3] &translate, const [float4x4] &rotate, const [float3] &scale);234 235 236 237 238 static [float4x4] FromEulerXYX(float x2, float y, float x);239 static [float4x4] FromEulerXZX(float x2, float z, float x); 240 static [float4x4] FromEulerYXY(float y2, float x, float y); 241 static [float4x4] FromEulerYZY(float y2, float z, float y); 242 static [float4x4] FromEulerZXZ(float z2, float x, float z); 243 static [float4x4] FromEulerZYZ(float z2, float y, float z); 244 static [float4x4] FromEulerXYZ(float x, float y, float z); 245 static [float4x4] FromEulerXZY(float x, float z, float y); 246 static [float4x4] FromEulerYXZ(float y, float x, float z); 247 static [float4x4] FromEulerYZX(float y, float z, float x); 248 static [float4x4] FromEulerZXY(float z, float x, float y); 249 static [float4x4] FromEulerZYX(float z, float y, float x); 250 251 252 253 static [ScaleOp] Scale(float sx, float sy, float sz);254 static [ScaleOp] Scale(const [float3] &scale);255 256 257 258 259 260 static [float4x4] Scale(const [float3] &scale, const [float3] &scaleCenter);261 262 263 264 265 266 static [float4x4] ScaleAlongAxis(const [float3] &axis, float scalingFactor, const [float3] &scaleCenter);267 static [float4x4] ScaleAlongAxis(const [float3] &axis, float scalingFactor);268 269 270 static [ScaleOp] UniformScale(float uniformScale);271 static [float4x4] UniformScale(float uniformScale, const [float3] &scaleCenter);272 273 274 275 276 [float3] GetScale() const;277 278 279 280 281 static [float4x4] ShearX(float yFactor, float zFactor);282 static [float4x4] ShearY(float xFactor, float zFactor); 283 static [float4x4] ShearZ(float xFactor, float yFactor); 284 285 286 287 288 static [float4x4] Mirror(const [Plane] &p);289 290 291 292 293 static [float4x4] D3DOrthoProjLH(float nearPlaneDistance, float farPlaneDistance, float horizontalViewportSize, float verticalViewportSize);294 295 296 297 static [float4x4] D3DOrthoProjRH(float nearPlaneDistance, float farPlaneDistance, float horizontalViewportSize, float verticalViewportSize);298 299 300 301 302 static [float4x4] D3DPerspProjLH(float nearPlaneDistance, float farPlaneDistance, float horizontalViewportSize, float verticalViewportSize);303 304 305 306 static [float4x4] D3DPerspProjRH(float nearPlaneDistance, float farPlaneDistance, float horizontalViewportSize, float verticalViewportSize);307 308 309 310 static [float4x4] OpenGLOrthoProjLH(float nearPlaneDistance, float farPlaneDistance, float horizontalViewportSize, float verticalViewportSize);311 312 313 static [float4x4] OpenGLOrthoProjRH(float nearPlaneDistance, float farPlaneDistance, float horizontalViewportSize, float verticalViewportSize);314 315 316 317 static [float4x4] OpenGLPerspProjLH(float nearPlaneDistance, float farPlaneDistance, float horizontalViewportSize, float verticalViewportSize);318 319 320 321 static [float4x4] OpenGLPerspProjRH(float nearPlaneDistance, float farPlaneDistance, float horizontalViewportSize, float verticalViewportSize);322 323 324 static [float4x4] OrthographicProjection(const [Plane] &target);325 static [float4x4] OrthographicProjectionYZ(); 326 static [float4x4] OrthographicProjectionXZ(); 327 static [float4x4] OrthographicProjectionXY(); 328 329 330 331 332 [float4x4] ComplementaryProjection() const;333 334 335 336 337 338 339 340 341 342 [MatrixProxy<Cols>] &operator[](int row);343 const [MatrixProxy<Cols>] &operator[](int row) const;344 345 346 347 348 349 float &At(int row, int col);350 [CONST_WIN32] float At(int row, int col) const;351 352 353 354 [float4] &Row(int row);355 const [float4] &Row(int row) const;356 357 [float3] &Row3(int row); 358 const [float3] &Row3(int row) const;359 360 361 362 [CONST_WIN32] [float4] Col(int col) const;363 364 [CONST_WIN32] [float3] Col3(int col) const;365 366 367 368 [CONST_WIN32] [float4] Diagonal() const;369 370 [CONST_WIN32] [float3] Diagonal3() const;371 372 373 void ScaleRow3(int row, float scalar);374 375 376 void ScaleRow(int row, float scalar);377 378 379 void ScaleCol3(int col, float scalar);380 381 382 void ScaleCol(int col, float scalar);383 384 385 [CONST_WIN32] [float3x3] Float3x3Part() const;386 387 388 389 [float3x4] &Float3x4Part();390 const [float3x4] &Float3x4Part() const;391 392 393 394 395 396 397 398 [CONST_WIN32] [float3] TranslatePart() const;399 400 401 [CONST_WIN32] [float3x3] RotatePart() const;402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 [float3] WorldX() const;424 425 426 [float3] WorldY() const;427 428 429 [float3] WorldZ() const;430 431 432 433 434 435 [FORCE_INLINE] float *[ptr]() { return &v[0][0]; }436 [FORCE_INLINE] const float *[ptr]() const { return &v[0][0]; }437 438 439 440 441 void SetRow3(int row, const float *data);442 void SetRow3(int row, const [float3] &rowVector);443 void SetRow3(int row, float m_r0, float m_r1, float m_r2);444 445 446 447 448 void SetRow(int row, const float *data);449 void SetRow(int row, const [float3] &rowVector, float m_r3);450 void SetRow(int row, const [float4] &rowVector);451 void SetRow(int row, float m_r0, float m_r1, float m_r2, float m_r3);452 453 454 455 456 void SetCol3(int column, const float *data);457 void SetCol3(int column, const [float3] &columnVector);458 void SetCol3(int column, float m_0c, float m_1c, float m_2c);459 460 461 462 463 void SetCol(int column, const float *data);464 void SetCol(int column, const [float3] &columnVector, float m_3c);465 void SetCol(int column, const [float4] &columnVector);466 void SetCol(int column, float m_0c, float m_1c, float m_2c, float m_3c);467 468 469 void Set(float _00, float _01, float _02, float _03,470 float _10, float _11, float _12, float _13,471 float _20, float _21, float _22, float _23,472 float _30, float _31, float _32, float _33);473 474 475 void Set(const [float4x4] &rhs);476 477 478 479 480 void Set(const float *values);481 482 483 484 485 486 void Set(int row, int col, float value);487 488 void Set3x3Part(const [float3x3] &rotation);489 void Set3x4Part(const [float3x4] &rotateTranslate);490 491 492 void SetIdentity();493 494 495 void SwapColumns(int col1, int col2);496 497 void SwapColumns3(int col1, int col2);498 499 500 void SwapRows(int row1, int row2);501 502 void SwapRows3(int row1, int row2);503 504 505 506 507 void SetTranslatePart(float tx, float ty, float tz);508 void SetTranslatePart(const [float3] &offset);509 void SetTranslatePart(const [float4] &offset);510 511 512 513 void SetRotatePartX(float angleRadians);514 515 516 void SetRotatePartY(float angleRadians);517 518 519 void SetRotatePartZ(float angleRadians);520 521 522 523 void SetRotatePart(const [float3] &axisDirection, float angleRadians);524 525 526 void SetRotatePart(const [Quat] &orientation);527 528 529 530 531 void [SetRotatePart](const [float3x3] &rotation) { Set3x3Part(rotation); }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 static [float4x4] LookAt(const [float3] &localForward, const [float3] &targetDirection, const [float3] &localUp, const [float3] &worldUp);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 static [float4x4] LookAt(const [float3] &eyePos, const [float3] &targetPos, const [float3] &localForward,599 const [float3] &localUp, const [float3] &worldUp);600 601 602 603 [float4x4] &operator =(const [float3x3] &rhs);604 605 606 607 [float4x4] &operator =(const [float3x4] &rhs);608 609 [float4x4] &operator =(const [float4x4] &rhs);610 611 [float4x4] &operator =(const [TranslateOp] &rhs);612 613 614 615 [float4x4] &operator =(const [Quat] &rhs);616 617 618 float Determinant3() const;619 620 621 622 623 624 625 626 float Determinant4() const;627 628 629 [float3x3] SubMatrix(int i, int j) const;630 631 632 float Minor(int i, int j) const;633 634 635 [float4x4] Adjugate() const;636 637 638 639 640 bool CholeskyDecompose([float4x4] &outL) const;641 642 643 644 645 bool LUDecompose([float4x4] &outLower, [float4x4] &outUpper) const;646 647 648 649 bool Inverse(float [epsilon] = 1[e]-6f);650 651 652 653 654 [float4x4] Inverted() const;655 656 657 658 659 660 661 662 663 664 665 666 667 bool InverseColOrthogonal();668 669 670 671 672 673 674 675 676 677 678 bool InverseOrthogonalUniformScale();679 680 681 682 683 684 685 686 687 void InverseOrthonormal();688 689 690 691 void Transpose();692 693 694 [float4x4] Transposed() const;695 696 697 698 bool InverseTranspose();699 700 701 702 [float4x4] InverseTransposed() const;703 704 705 float Trace() const;706 707 708 709 710 711 712 713 void Orthogonalize3(int firstColumn, int secondColumn, int thirdColumn);714 void [Orthogonalize3]() { [Orthogonalize3](0, 1, 2); }715 716 717 718 719 720 721 722 void Orthonormalize3(int firstColumn, int secondColumn, int thirdColumn);723 void [Orthonormalize3]() { [Orthonormalize3](0, 1, 2); }724 725 726 727 728 729 730 void RemoveScale();731 732 733 void Pivot();734 735 736 737 738 [float3] TransformPos(const [float3] &pointVector) const;739 [float3] TransformPos(float x, float y, float z) const;740 [float4] [TransformPos](const [float4] &vector) const { return Transform(vector); }741 742 743 744 745 [float3] TransformDir(const [float3] &directionVector) const;746 [float3] TransformDir(float x, float y, float z) const;747 [float4] [TransformDir](const [float4] &vector) const { return Transform(vector); }748 749 750 751 752 [float4] Transform(const [float4] &vector) const;753 754 755 756 757 758 759 void TransformPos([float3] *pointArray, int numPoints) const;760 void TransformPos([float3] *pointArray, int numPoints, int strideBytes) const;761 762 763 764 765 766 767 void TransformDir([float3] *dirArray, int numVectors) const;768 void TransformDir([float3] *dirArray, int numVectors, int strideBytes) const;769 770 771 772 773 void Transform([float4] *vectorArray, int numVectors) const;774 void Transform([float4] *vectorArray, int numVectors, int strideBytes) const;775 776 777 [float4x4] [operator *](const [float3x3] &rhs) const;778 779 780 [float4x4] [operator *](const [float3x4] &rhs) const;781 782 783 [float4x4] [operator *](const [float4x4] &rhs) const;784 785 786 [float4x4] [operator *](const [Quat] &rhs) const;787 788 789 [float4] [operator *](const [float4] &rhs) const;790 791 [float4x4] [operator *](float scalar) const;792 [float4x4] operator /(float scalar) const;793 [float4x4] [operator +](const [float4x4] &rhs) const;794 [float4x4] [operator -](const [float4x4] &rhs) const;795 [float4x4] [operator -]() const;796 797 798 [float4x4] [operator +]() const { return *this; }799 800 [float4x4] &[operator *=](float scalar);801 [float4x4] &operator /=(float scalar);802 [float4x4] &[operator +=](const [float4x4] &rhs);803 [float4x4] &[operator -=](const [float4x4] &rhs);804 805 806 807 bool [IsFinite]() const;808 809 810 811 bool IsIdentity(float [epsilon] = 1[e]-3f) const;812 813 814 815 bool IsLowerTriangular(float [epsilon] = 1[e]-3f) const;816 817 818 819 bool IsUpperTriangular(float [epsilon] = 1[e]-3f) const;820 821 822 823 bool IsInvertible(float [epsilon] = 1[e]-3f) const;824 825 826 827 bool IsSymmetric(float [epsilon] = 1[e]-3f) const;828 829 830 831 832 bool IsSkewSymmetric(float [epsilon] = 1[e]-3f) const;833 834 835 836 bool IsIdempotent(float [epsilon] = 1[e]-3f) const;837 838 839 840 841 842 843 844 bool HasUnitaryScale(float [epsilon] = 1[e]-3f) const;845 846 847 848 849 850 851 bool HasNegativeScale() const;852 853 854 855 856 857 bool HasUniformScale(float [epsilon] = 1[e]-3f) const;858 859 860 bool IsRowOrthogonal3(float [epsilon] = 1[e]-3f) const;861 862 863 bool IsColOrthogonal3(float [epsilon] = 1[e]-3f) const;864 865 866 867 868 869 870 bool IsOrthonormal3(float [epsilon] = 1[e]-3f) const;871 872 873 bool Equals(const [float4x4] &other, float [epsilon] = 1[e]-3f) const;874 875 876 877 bool ContainsProjection(float [epsilon] = 1[e]-3f) const;878 879 #ifdef MATH_ENABLE_STL_SUPPORT880 881 std::string ToString() const;882 std::string SerializeToString() const;883 884 std::string ToString2() const;885 #endif886 887 888 889 890 891 892 [float3] ToEulerXYX() const;893 [float3] ToEulerXZX() const; 894 [float3] ToEulerYXY() const; 895 [float3] ToEulerYZY() const; 896 [float3] ToEulerZXZ() const; 897 [float3] ToEulerZYZ() const; 898 [float3] ToEulerXYZ() const; 899 [float3] ToEulerXZY() const; 900 [float3] ToEulerYXZ() const; 901 [float3] ToEulerYZX() const; 902 [float3] ToEulerZXY() const; 903 [float3] ToEulerZYX() const; 904 905 906 907 908 909 910 911 912 913 [float3] ExtractScale() const;914 915 916 917 918 919 920 921 922 923 924 925 926 void Decompose([float3] &translate, [Quat] &rotate, [float3] &scale) const;927 void Decompose([float3] &translate, [float3x3] &rotate, [float3] &scale) const;928 void Decompose([float3] &translate, [float3x4] &rotate, [float3] &scale) const;929 void Decompose([float3] &translate, [float4x4] &rotate, [float3] &scale) const;930 931 932 [float4x4] [Abs]() const;933 934 #ifdef MATH_OGRE_INTEROP935 [float4x4](const Ogre::Matrix4 &m) { Set(&m[0][0]); }936 operator Ogre::Matrix4() { return Ogre::Matrix4(v[0][0], v[0][1], v[0][2], v[0][3], v[1][0], v[1][1], v[1][2], v[1][3], v[2][0], v[2][1], v[2][2], v[2][3], v[3][0], v[3][1], v[3][2], v[3][3]); }937 #endif938 #ifdef MATH_QT_INTEROP939 [float4x4](const QMatrix4x4 &m) { Set(m(0,0), m(0,1), m(0,2), m(0,3), m(1,0), m(1,1), m(1,2), m(1,3), m(2,0), m(2,1), m(2,2), m(2,3), m(3,0), m(3,1), m(3,2), m(3,3)); }940 operator QMatrix4x4() const { return QMatrix4x4(v[0][0], v[0][1], v[0][2], v[0][3], v[1][0], v[1][1], v[1][2], v[1][3], v[2][0], v[2][1], v[2][2], v[2][3], v[3][0], v[3][1], v[3][2], v[3][3]); }941 operator QString() const { return toString(); }942 QString toString() const { return ToString2().c_str(); }943 QMatrix4x4 ToQMatrix4x4() const { return (QMatrix4x4)*this; }944 static [float4x4] FromQMatrix4x4(const QMatrix4x4 &m) { return ([float4x4])m; }945 #endif946 #ifdef MATH_URHO3D_INTEROP947 [float4x4](const Urho3D::Matrix4 &m) { Set(m.m00_, m.m01_, m.m02_, m.m03_, m.m10_, m.m11_, m.m12_, m.m13_, m.m20_, m.m21_, m.m22_, m.m23_, m.m30_, m.m31_, m.m32_, m.m33_); }948 operator Urho3D::Matrix4() { return Urho3D::Matrix4(ptr()); }949 #endif950 951 [float4x4] [Mul](const [float3x3] &rhs) const;952 [float4x4] [Mul](const [float3x4] &rhs) const;953 [float4x4] [Mul](const [float4x4] &rhs) const;954 [float4x4] [Mul](const [Quat] &rhs) const;955 [float3] MulPos(const [float3] &pointVector) const;956 inline [float4] [MulPos](const [float4] &pointVector) const { [assume](![EqualAbs](pointVector.[w], 0.f)); return [Mul](pointVector); }957 [float3] MulDir(const [float3] &directionVector) const;958 inline [float4] [MulDir](const [float4] &directionVector) const { [assume]([EqualAbs](directionVector.[w], 0.f)); return [Mul](directionVector); }959 [float4] [Mul](const [float4] &vector) const;960 };961 962 #ifdef MATH_ENABLE_STL_SUPPORT963 964 std::ostream &[operator <<](std::ostream &out, const [float4x4] &rhs);965 #endif966 967 968 [float4x4] [operator *](const [Quat] &lhs, const [float4x4] &rhs);969 [float4x4] [operator *](const [float3x4] &lhs, const [float4x4] &rhs);970 [float4x4] [operator *](const [float3x3] &lhs, const [float4x4] &rhs);971 972 973 974 975 [float4] [operator *](const [float4] &lhs, const [float4x4] &rhs);976 977 #ifdef MATH_QT_INTEROP978 Q_DECLARE_METATYPE([float4x4])979 Q_DECLARE_METATYPE([float4x4]*)980 #endif981 982 #ifdef MATH_SIMD983 template<>984 inline [float4x4] *AlignedNew<float4x4>(size_t numElements) { return AlignedNew<float4x4>(numElements, MAT_ALIGNMENT); }985 #endif986 987 [MATH_END_NAMESPACE] Go back to previous page