LCOV - code coverage report
Current view: top level - source/maths - Matrix3D.h (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 63 90 70.0 %
Date: 2026-12-03 01:19:39 Functions: 4 7 57.1 %

          Line data    Source code
       1             : /* Copyright (C) 2025 Wildfire Games.
       2             :  * This file is part of 0 A.D.
       3             :  *
       4             :  * 0 A.D. is free software: you can redistribute it and/or modify
       5             :  * it under the terms of the GNU General Public License as published by
       6             :  * the Free Software Foundation, either version 2 of the License, or
       7             :  * (at your option) any later version.
       8             :  *
       9             :  * 0 A.D. is distributed in the hope that it will be useful,
      10             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      12             :  * GNU General Public License for more details.
      13             :  *
      14             :  * You should have received a copy of the GNU General Public License
      15             :  * along with 0 A.D.  If not, see <http://www.gnu.org/licenses/>.
      16             :  */
      17             : 
      18             : /*
      19             :  * A Matrix class used for holding and manipulating transformation
      20             :  * info.
      21             :  */
      22             : 
      23             : #ifndef INCLUDED_MATRIX3D
      24             : #define INCLUDED_MATRIX3D
      25             : 
      26             : #include "maths/Vector3D.h"
      27             : #include "maths/Vector4D.h"
      28             : 
      29             : #include <cstddef>
      30             : #include <span>
      31             : 
      32             : class CQuaternion;
      33             : 
      34             : // CMatrix3D: a 4x4 matrix class for common operations in 3D
      35             : class CMatrix3D
      36             : {
      37             : public:
      38             :     // the matrix data itself - accessible as either longhand names
      39             :     // or via a flat or 2d array
      40             :     // NOTE: _xy means row x, column y in the mathematical notation of this matrix, so don't be
      41             :     // fooled by the way they're listed below
      42             :     union
      43             :     {
      44             :         struct
      45             :         {
      46             :             float _11, _21, _31, _41;
      47             :             float _12, _22, _32, _42;
      48             :             float _13, _23, _33, _43;
      49             :             float _14, _24, _34, _44;
      50             :         };
      51             :         float _data[16];
      52             :         float _data2d[4][4];
      53             :         // (Be aware that m(0,2) == _data2d[2][0] == _13, etc. This is to be considered a feature.)
      54             :     };
      55             : 
      56             : public:
      57             :     // constructors
      58         851 :     CMatrix3D ()
      59         851 :     {
      60             :     }
      61             : 
      62          99 :     CMatrix3D(
      63             :         float a11, float a12, float a13, float a14,
      64             :         float a21, float a22, float a23, float a24,
      65             :         float a31, float a32, float a33, float a34,
      66          99 :         float a41, float a42, float a43, float a44) :
      67          99 :         _11(a11), _12(a12), _13(a13), _14(a14),
      68          99 :         _21(a21), _22(a22), _23(a23), _24(a24),
      69          99 :         _31(a31), _32(a32), _33(a33), _34(a34),
      70          69 :         _41(a41), _42(a42), _43(a43), _44(a44)
      71             :     {
      72             :     }
      73             : 
      74             :     CMatrix3D(float data[]) :
      75             :         _11(data[0]), _21(data[1]), _31(data[2]), _41(data[3]),
      76             :         _12(data[4]), _22(data[5]), _32(data[6]), _42(data[7]),
      77             :         _13(data[8]), _23(data[9]), _33(data[10]), _43(data[11]),
      78             :         _14(data[12]), _24(data[13]), _34(data[14]), _44(data[15])
      79             :     {
      80             :     }
      81             : 
      82           1 :     CMatrix3D(const float data[]) :
      83           8 :         _11(data[0]), _21(data[1]), _31(data[2]), _41(data[3]),
      84           8 :         _12(data[4]), _22(data[5]), _32(data[6]), _42(data[7]),
      85           8 :         _13(data[8]), _23(data[9]), _33(data[10]), _43(data[11]),
      86           6 :         _14(data[12]), _24(data[13]), _34(data[14]), _44(data[15])
      87             :     {
      88             :     }
      89             : 
      90             :     // accessors to individual elements of matrix
      91             :     // NOTE: in this function definition, 'col' and 'row' represent the column and row into the
      92             :     // internal element matrix which is the transposed of the mathematical notation, so the first
      93             :     // and second arguments here are actually the row and column into the mathematical notation.
      94         128 :     float& operator()(int col, int row)
      95             :     {
      96         128 :         return _data[row*4+col];
      97             :     }
      98           0 :     const float& operator()(int col, int row) const
      99             :     {
     100           0 :         return _data[row*4+col];
     101             :     }
     102             : 
     103             :     float& operator[](int idx)
     104             :     {
     105             :         return _data[idx];
     106             :     }
     107             :     const float& operator[](int idx) const
     108             :     {
     109             :         return _data[idx];
     110             :     }
     111             : 
     112             :     // matrix multiplication
     113          69 :     CMatrix3D operator*(const CMatrix3D &matrix) const
     114             :     {
     115          69 :         return CMatrix3D(
     116          69 :             _11*matrix._11 + _12*matrix._21 + _13*matrix._31 + _14*matrix._41,
     117          69 :             _11*matrix._12 + _12*matrix._22 + _13*matrix._32 + _14*matrix._42,
     118          69 :             _11*matrix._13 + _12*matrix._23 + _13*matrix._33 + _14*matrix._43,
     119          69 :             _11*matrix._14 + _12*matrix._24 + _13*matrix._34 + _14*matrix._44,
     120             : 
     121          69 :             _21*matrix._11 + _22*matrix._21 + _23*matrix._31 + _24*matrix._41,
     122          69 :             _21*matrix._12 + _22*matrix._22 + _23*matrix._32 + _24*matrix._42,
     123          69 :             _21*matrix._13 + _22*matrix._23 + _23*matrix._33 + _24*matrix._43,
     124          69 :             _21*matrix._14 + _22*matrix._24 + _23*matrix._34 + _24*matrix._44,
     125             : 
     126          69 :             _31*matrix._11 + _32*matrix._21 + _33*matrix._31 + _34*matrix._41,
     127          69 :             _31*matrix._12 + _32*matrix._22 + _33*matrix._32 + _34*matrix._42,
     128          69 :             _31*matrix._13 + _32*matrix._23 + _33*matrix._33 + _34*matrix._43,
     129          69 :             _31*matrix._14 + _32*matrix._24 + _33*matrix._34 + _34*matrix._44,
     130             : 
     131          69 :             _41*matrix._11 + _42*matrix._21 + _43*matrix._31 + _44*matrix._41,
     132          69 :             _41*matrix._12 + _42*matrix._22 + _43*matrix._32 + _44*matrix._42,
     133          69 :             _41*matrix._13 + _42*matrix._23 + _43*matrix._33 + _44*matrix._43,
     134          69 :             _41*matrix._14 + _42*matrix._24 + _43*matrix._34 + _44*matrix._44
     135          69 :         );
     136             :     }
     137             : 
     138             :     // matrix multiplication/assignment
     139           4 :     CMatrix3D& operator*=(const CMatrix3D &matrix)
     140             :     {
     141           4 :         *this = *this * matrix;
     142           4 :         return *this;
     143             :     }
     144             : 
     145             :     // matrix scaling
     146             :     CMatrix3D operator*(float f) const
     147             :     {
     148             :         return CMatrix3D(
     149             :             _11*f, _12*f, _13*f, _14*f,
     150             :             _21*f, _22*f, _23*f, _24*f,
     151             :             _31*f, _32*f, _33*f, _34*f,
     152             :             _41*f, _42*f, _43*f, _44*f
     153             :         );
     154             :     }
     155             : 
     156             :     // matrix addition
     157             :     CMatrix3D operator+(const CMatrix3D &m) const
     158             :     {
     159             :         return CMatrix3D(
     160             :             _11+m._11, _12+m._12, _13+m._13, _14+m._14,
     161             :             _21+m._21, _22+m._22, _23+m._23, _24+m._24,
     162             :             _31+m._31, _32+m._32, _33+m._33, _34+m._34,
     163             :             _41+m._41, _42+m._42, _43+m._43, _44+m._44
     164             :         );
     165             :     }
     166             : 
     167             :     // matrix addition/assignment
     168             :     CMatrix3D& operator+=(const CMatrix3D &m)
     169             :     {
     170             :         _11 += m._11; _21 += m._21; _31 += m._31; _41 += m._41;
     171             :         _12 += m._12; _22 += m._22; _32 += m._32; _42 += m._42;
     172             :         _13 += m._13; _23 += m._23; _33 += m._33; _43 += m._43;
     173             :         _14 += m._14; _24 += m._24; _34 += m._34; _44 += m._44;
     174             :         return *this;
     175             :     }
     176             : 
     177             :     // equality
     178           4 :     bool operator==(const CMatrix3D &m) const
     179             :     {
     180           2 :         return _11 == m._11 && _21 == m._21 && _31 == m._31 && _41 == m._41 &&
     181           2 :                  _12 == m._12 && _22 == m._22 && _32 == m._32 && _42 == m._42 &&
     182           2 :                  _13 == m._13 && _23 == m._23 && _33 == m._33 && _43 == m._43 &&
     183           6 :                  _14 == m._14 && _24 == m._24 && _34 == m._34 && _44 == m._44;
     184             :     }
     185             : 
     186             :     // inequality
     187             :     bool operator!=(const CMatrix3D& m) const
     188             :     {
     189             :         return !(*this == m);
     190             :     }
     191             : 
     192             :     // set this matrix to the identity matrix
     193             :     void SetIdentity();
     194             :     // set this matrix to the zero matrix
     195             :     void SetZero();
     196             :     // set this matrix to the orthogonal projection matrix
     197             :     void SetOrtho(float left, float right, float bottom, float top, float near, float far);
     198             :     // set this matrix to the perspective projection matrix
     199             :     void SetPerspective(float fov, float aspect, float near, float far);
     200             :     void SetPerspectiveTile(float fov, float aspect, float near, float far, int tiles, int tile_x, int tile_y);
     201             : 
     202             :     // concatenate arbitrary matrix onto this matrix
     203          15 :     void Concatenate(const CMatrix3D& m)
     204             :     {
     205          15 :         (*this) = m * (*this);
     206           0 :     }
     207             : 
     208             :     // blend matrix using only 4x3 subset
     209           0 :     void Blend(const CMatrix3D& m, float f)
     210             :     {
     211           0 :         _11 = m._11*f; _21 = m._21*f; _31 = m._31*f;
     212           0 :         _12 = m._12*f; _22 = m._22*f; _32 = m._32*f;
     213           0 :         _13 = m._13*f; _23 = m._23*f; _33 = m._33*f;
     214           0 :         _14 = m._14*f; _24 = m._24*f; _34 = m._34*f;
     215           0 :     }
     216             : 
     217             :     // blend matrix using only 4x3 and add onto existing blend
     218           0 :     void AddBlend(const CMatrix3D& m, float f)
     219             :     {
     220           0 :         _11 += m._11*f; _21 += m._21*f; _31 += m._31*f;
     221           0 :         _12 += m._12*f; _22 += m._22*f; _32 += m._32*f;
     222           0 :         _13 += m._13*f; _23 += m._23*f; _33 += m._33*f;
     223           0 :         _14 += m._14*f; _24 += m._24*f; _34 += m._34*f;
     224           0 :     }
     225             : 
     226             :     // set this matrix to a rotation matrix for a rotation about X axis of given angle
     227             :     void SetXRotation(float angle);
     228             :     // set this matrix to a rotation matrix for a rotation about Y axis of given angle
     229             :     void SetYRotation(float angle);
     230             :     // set this matrix to a rotation matrix for a rotation about Z axis of given angle
     231             :     void SetZRotation(float angle);
     232             :     // set this matrix to a rotation described by given quaternion
     233             :     void SetRotation(const CQuaternion& quat);
     234             : 
     235             :     // concatenate a rotation about the X axis onto this matrix
     236             :     void RotateX(float angle);
     237             :     // concatenate a rotation about the Y axis onto this matrix
     238             :     void RotateY(float angle);
     239             :     // concatenate a rotation about the Z axis onto this matrix
     240             :     void RotateZ(float angle);
     241             :     // concatenate a rotation described by given quaternion
     242             :     void Rotate(const CQuaternion& quat);
     243             : 
     244             :     // sets this matrix to the given translation matrix (any existing transformation will be overwritten)
     245             :     void SetTranslation(float x, float y, float z);
     246             :     void SetTranslation(const CVector3D& vector);
     247             : 
     248             :     // concatenate given translation onto this matrix. Assumes the current
     249             :     // matrix is an affine transformation (i.e. the bottom row is [0,0,0,1])
     250             :     // as an optimisation.
     251             :     void Translate(float x, float y, float z);
     252             :     void Translate(const CVector3D& vector);
     253             : 
     254             :     // apply translation after this matrix (M = M * T)
     255             :     void PostTranslate(float x, float y, float z);
     256             : 
     257             :     // set this matrix to the given scaling matrix
     258             :     void SetScaling(float x_scale, float y_scale, float z_scale);
     259             : 
     260             :     // concatenate given scaling matrix onto this matrix
     261             :     void Scale(float x_scale, float y_scale, float z_scale);
     262             : 
     263             :     // calculate the inverse of this matrix, store in dst
     264             :     void GetInverse(CMatrix3D& dst) const;
     265             : 
     266             :     // return the inverse of this matrix
     267             :     CMatrix3D GetInverse() const;
     268             : 
     269             :     // calculate the transpose of this matrix, store in dst
     270             :     CMatrix3D GetTranspose() const;
     271             : 
     272             :     // return the translation component of this matrix
     273             :     CVector3D GetTranslation() const;
     274             :     // return left vector, derived from rotation
     275             :     CVector3D GetLeft() const;
     276             :     // return up vector, derived from rotation
     277             :     CVector3D GetUp() const;
     278             :     // return forward vector, derived from rotation
     279             :     CVector3D GetIn() const;
     280             :     // return a quaternion representing the matrix's rotation
     281             :     CQuaternion GetRotation() const;
     282             :     // return the angle of rotation around the Y axis in range [-pi,pi]
     283             :     // (based on projecting the X axis onto the XZ plane)
     284             :     float GetYRotation() const;
     285             : 
     286             :     // transform a 3D vector by this matrix
     287          45 :     CVector3D Transform(const CVector3D &vector) const
     288             :     {
     289          45 :         CVector3D result;
     290          45 :         Transform(vector, result);
     291          45 :         return result;
     292             :     }
     293             : 
     294          75 :     void Transform(const CVector3D& vector, CVector3D& result) const
     295             :     {
     296          75 :         result.X = _11*vector.X + _12*vector.Y + _13*vector.Z + _14;
     297          75 :         result.Y = _21*vector.X + _22*vector.Y + _23*vector.Z + _24;
     298          75 :         result.Z = _31*vector.X + _32*vector.Y + _33*vector.Z + _34;
     299          75 :     }
     300             : 
     301             :     // transform a 4D vector by this matrix
     302         100 :     CVector4D Transform(const CVector4D &vector) const
     303             :     {
     304          88 :         CVector4D result;
     305          88 :         Transform(vector, result);
     306          88 :         return result;
     307             :     }
     308             : 
     309         100 :     void Transform(const CVector4D& vector, CVector4D& result) const
     310             :     {
     311         100 :         result.X = _11*vector.X + _12*vector.Y + _13*vector.Z + _14*vector.W;
     312         100 :         result.Y = _21*vector.X + _22*vector.Y + _23*vector.Z + _24*vector.W;
     313         100 :         result.Z = _31*vector.X + _32*vector.Y + _33*vector.Z + _34*vector.W;
     314         100 :         result.W = _41*vector.X + _42*vector.Y + _43*vector.Z + _44*vector.W;
     315         100 :     }
     316             : 
     317             :     // rotate a vector by this matrix
     318           0 :     CVector3D Rotate(const CVector3D& vector) const
     319             :     {
     320           0 :         CVector3D result;
     321           0 :         Rotate(vector, result);
     322           0 :         return result;
     323             :     }
     324             : 
     325           0 :     void Rotate(const CVector3D& vector, CVector3D& result) const
     326             :     {
     327           0 :         result.X = _11*vector.X + _12*vector.Y + _13*vector.Z;
     328           0 :         result.Y = _21*vector.X + _22*vector.Y + _23*vector.Z;
     329           0 :         result.Z = _31*vector.X + _32*vector.Y + _33*vector.Z;
     330           0 :     }
     331             : 
     332             :     // rotate a vector by the transpose of this matrix
     333             :     void RotateTransposed(const CVector3D& vector,CVector3D& result) const;
     334             :     CVector3D RotateTransposed(const CVector3D& vector) const;
     335             : 
     336             :     // Returns 16 element array of floats, e.g. for mat4 uniforms.
     337           0 :     std::span<const float, 16> AsFloatArray() const
     338             :     {
     339             :         // Additional check to prevent a weird compiler has a different
     340             :         // alignement for an array and a class members.
     341           0 :         static_assert(
     342             :             sizeof(CMatrix3D) == sizeof(float) * 16u &&
     343             :             offsetof(CMatrix3D, _data) == 0 &&
     344             :             offsetof(CMatrix3D, _11) == 0 &&
     345             :             offsetof(CMatrix3D, _44) == sizeof(float) * 15u,
     346             :             "CMatrix3D should be properly layouted to use AsFloatArray");
     347           0 :         return std::span<const float, 16>(_data);
     348             :     }
     349             : };
     350             : 
     351             : #endif // INCLUDED_MATRIX3D

Generated by: LCOV version 1.16