LCOV - code coverage report
Current view: top level - source/maths - Matrix3D.cpp (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 235 274 85.8 %
Date: 2026-12-03 01:19:39 Functions: 22 31 71.0 %

          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             : #include "precompiled.h"
      24             : 
      25             : #include "Matrix3D.h"
      26             : 
      27             : #include "maths/Quaternion.h"
      28             : 
      29             : #include <cmath>
      30             : 
      31             : //Sets the identity matrix
      32         742 : void CMatrix3D::SetIdentity ()
      33             : {
      34         742 :     _11=1.0f; _12=0.0f; _13=0.0f; _14=0.0f;
      35         742 :     _21=0.0f; _22=1.0f; _23=0.0f; _24=0.0f;
      36         742 :     _31=0.0f; _32=0.0f; _33=1.0f; _34=0.0f;
      37         742 :     _41=0.0f; _42=0.0f; _43=0.0f; _44=1.0f;
      38         742 : }
      39             : 
      40             : //Sets the zero matrix
      41          15 : void CMatrix3D::SetZero()
      42             : {
      43          15 :     _11=0.0f; _12=0.0f; _13=0.0f; _14=0.0f;
      44          15 :     _21=0.0f; _22=0.0f; _23=0.0f; _24=0.0f;
      45          15 :     _31=0.0f; _32=0.0f; _33=0.0f; _34=0.0f;
      46          15 :     _41=0.0f; _42=0.0f; _43=0.0f; _44=0.0f;
      47           1 : }
      48             : 
      49           6 : void CMatrix3D::SetOrtho(float left, float right, float bottom, float top, float near, float far)
      50             : {
      51             :     // Based on OpenGL spec
      52           6 :     SetZero();
      53           6 :     _11 = 2 / (right - left);
      54           6 :     _22 = 2 / (top - bottom);
      55           6 :     _33 = -2 / (far - near);
      56           6 :     _44 = 1;
      57             : 
      58           6 :     _14 = -(right + left) / (right - left);
      59           6 :     _24 = -(top + bottom) / (top - bottom);
      60           6 :     _34 = -(far + near) / (far - near);
      61           6 : }
      62             : 
      63           8 : void CMatrix3D::SetPerspective(float fov, float aspect, float near, float far)
      64             : {
      65           8 :     const float f = 1.f / tanf(fov / 2.f);
      66             : 
      67           8 :     SetZero();
      68           8 :     _11 = f / aspect;
      69           8 :     _22 = f;
      70           8 :     _33 = -(far + near) / (near - far);
      71           8 :     _34 = 2 * far * near / (near - far);
      72           8 :     _43 = 1;
      73           8 : }
      74             : 
      75           0 : void CMatrix3D::SetPerspectiveTile(float fov, float aspect, float near, float far, int tiles, int tile_x, int tile_y)
      76             : {
      77           0 :     const float f = 1.f / tanf(fov / 2.f);
      78             : 
      79           0 :     SetPerspective(fov, aspect, near, far);
      80           0 :     _11 = tiles * f / aspect;
      81           0 :     _22 = tiles * f;
      82           0 :     _13 = -(1 - tiles + 2 * tile_x);
      83           0 :     _23 = -(1 - tiles + 2 * tile_y);
      84           0 : }
      85             : 
      86             : //The following clear the matrix and set the
      87             : //rotation of each of the 3 axes
      88             : 
      89          16 : void CMatrix3D::SetXRotation (float angle)
      90             : {
      91          16 :     const float Cos = cosf (angle);
      92          16 :     const float Sin = sinf (angle);
      93             : 
      94          16 :     _11=1.0f; _12=0.0f; _13=0.0f; _14=0.0f;
      95          16 :     _21=0.0f; _22=Cos;  _23=-Sin; _24=0.0f;
      96          16 :     _31=0.0f; _32=Sin;  _33=Cos;  _34=0.0f;
      97          16 :     _41=0.0f; _42=0.0f; _43=0.0f; _44=1.0f;
      98          16 : }
      99             : 
     100          19 : void CMatrix3D::SetYRotation (float angle)
     101             : {
     102          19 :     const float Cos = cosf (angle);
     103          19 :     const float Sin = sinf (angle);
     104             : 
     105          19 :     _11=Cos;  _12=0.0f; _13=Sin;  _14=0.0f;
     106          19 :     _21=0.0f; _22=1.0f; _23=0.0f; _24=0.0f;
     107          19 :     _31=-Sin; _32=0.0f; _33=Cos;  _34=0.0f;
     108          19 :     _41=0.0f; _42=0.0f; _43=0.0f; _44=1.0f;
     109          19 : }
     110             : 
     111           3 : void CMatrix3D::SetZRotation (float angle)
     112             : {
     113           3 :     const float Cos = cosf (angle);
     114           3 :     const float Sin = sinf (angle);
     115             : 
     116           3 :     _11=Cos;  _12=-Sin; _13=0.0f; _14=0.0f;
     117           3 :     _21=Sin;  _22=Cos;  _23=0.0f; _24=0.0f;
     118           3 :     _31=0.0f; _32=0.0f; _33=1.0f; _34=0.0f;
     119           3 :     _41=0.0f; _42=0.0f; _43=0.0f; _44=1.0f;
     120           3 : }
     121             : 
     122             : //The following apply a rotation to the matrix
     123             : //about each of the axes;
     124             : 
     125           1 : void CMatrix3D::RotateX (float angle)
     126             : {
     127           1 :     const float Cos = cosf (angle);
     128           1 :     const float Sin = sinf (angle);
     129           1 :     const float tmp_21 = _21;
     130           1 :     const float tmp_22 = _22;
     131           1 :     const float tmp_23 = _23;
     132           1 :     const float tmp_24 = _24;
     133             : 
     134           1 :     _21 = Cos * _21 - Sin * _31;
     135           1 :     _22 = Cos * _22 - Sin * _32;
     136           1 :     _23 = Cos * _23 - Sin * _33;
     137           1 :     _24 = Cos * _24 - Sin * _34;
     138             : 
     139           1 :     _31 = Sin * tmp_21 + Cos * _31;
     140           1 :     _32 = Sin * tmp_22 + Cos * _32;
     141           1 :     _33 = Sin * tmp_23 + Cos * _33;
     142           1 :     _34 = Sin * tmp_24 + Cos * _34;
     143           1 : }
     144             : 
     145          16 : void CMatrix3D::RotateY (float angle)
     146             : {
     147          16 :     const float Cos = cosf (angle);
     148          16 :     const float Sin = sinf (angle);
     149          16 :     const float tmp_11 = _11;
     150          16 :     const float tmp_12 = _12;
     151          16 :     const float tmp_13 = _13;
     152          16 :     const float tmp_14 = _14;
     153             : 
     154          16 :     _11 = Cos * _11 + Sin * _31;
     155          16 :     _12 = Cos * _12 + Sin * _32;
     156          16 :     _13 = Cos * _13 + Sin * _33;
     157          16 :     _14 = Cos * _14 + Sin * _34;
     158             : 
     159          16 :     _31 = -Sin * tmp_11 + Cos * _31;
     160          16 :     _32 = -Sin * tmp_12 + Cos * _32;
     161          16 :     _33 = -Sin * tmp_13 + Cos * _33;
     162          16 :     _34 = -Sin * tmp_14 + Cos * _34;
     163          16 : }
     164             : 
     165          16 : void CMatrix3D::RotateZ (float angle)
     166             : {
     167          16 :     const float Cos = cosf (angle);
     168          16 :     const float Sin = sinf (angle);
     169          16 :     const float tmp_11 = _11;
     170          16 :     const float tmp_12 = _12;
     171          16 :     const float tmp_13 = _13;
     172          16 :     const float tmp_14 = _14;
     173             : 
     174          16 :     _11 = Cos * _11 - Sin * _21;
     175          16 :     _12 = Cos * _12 - Sin * _22;
     176          16 :     _13 = Cos * _13 - Sin * _23;
     177          16 :     _14 = Cos * _14 - Sin * _24;
     178             : 
     179          16 :     _21 = Sin * tmp_11 + Cos * _21;
     180          16 :     _22 = Sin * tmp_12 + Cos * _22;
     181          16 :     _23 = Sin * tmp_13 + Cos * _23;
     182          16 :     _24 = Sin * tmp_14 + Cos * _24;
     183          16 : }
     184             : 
     185             : //Sets the translation of the matrix
     186           3 : void CMatrix3D::SetTranslation (float x, float y, float z)
     187             : {
     188           3 :     _11=1.0f; _12=0.0f; _13=0.0f; _14=x;
     189           3 :     _21=0.0f; _22=1.0f; _23=0.0f; _24=y;
     190           3 :     _31=0.0f; _32=0.0f; _33=1.0f; _34=z;
     191           3 :     _41=0.0f; _42=0.0f; _43=0.0f; _44=1.0f;
     192           2 : }
     193             : 
     194           1 : void CMatrix3D::SetTranslation(const CVector3D& vector)
     195             : {
     196           1 :     SetTranslation(vector.X, vector.Y, vector.Z);
     197           1 : }
     198             : 
     199             : //Applies a translation to the matrix
     200           0 : void CMatrix3D::Translate(float x, float y, float z)
     201             : {
     202           0 :     _14 += x;
     203           0 :     _24 += y;
     204           0 :     _34 += z;
     205           0 : }
     206             : 
     207          21 : void CMatrix3D::Translate(const CVector3D &vector)
     208             : {
     209          21 :     _14 += vector.X;
     210          21 :     _24 += vector.Y;
     211          21 :     _34 += vector.Z;
     212          21 : }
     213             : 
     214           0 : void CMatrix3D::PostTranslate(float x, float y, float z)
     215             : {
     216             :     // Equivalent to "m.SetTranslation(x, y, z); *this = *this * m;"
     217           0 :     _14 += _11*x + _12*y + _13*z;
     218           0 :     _24 += _21*x + _22*y + _23*z;
     219           0 :     _34 += _31*x + _32*y + _33*z;
     220           0 :     _44 += _41*x + _42*y + _43*z;
     221           0 : }
     222             : 
     223          34 : CVector3D CMatrix3D::GetTranslation() const
     224             : {
     225          34 :     return CVector3D(_14, _24, _34);
     226             : }
     227             : 
     228             : //Clears and sets the scaling of the matrix
     229           2 : void CMatrix3D::SetScaling (float x_scale, float y_scale, float z_scale)
     230             : {
     231           2 :     _11=x_scale; _12=0.0f;    _13=0.0f;    _14=0.0f;
     232           2 :     _21=0.0f;    _22=y_scale; _23=0.0f;    _24=0.0f;
     233           2 :     _31=0.0f;    _32=0.0f;    _33=z_scale; _34=0.0f;
     234           2 :     _41=0.0f;    _42=0.0f;    _43=0.0f;    _44=1.0f;
     235           2 : }
     236             : 
     237             : //Scales the matrix
     238           1 : void CMatrix3D::Scale (float x_scale, float y_scale, float z_scale)
     239             : {
     240           1 :     _11 *= x_scale;
     241           1 :     _12 *= x_scale;
     242           1 :     _13 *= x_scale;
     243           1 :     _14 *= x_scale;
     244             : 
     245           1 :     _21 *= y_scale;
     246           1 :     _22 *= y_scale;
     247           1 :     _23 *= y_scale;
     248           1 :     _24 *= y_scale;
     249             : 
     250           1 :     _31 *= z_scale;
     251           1 :     _32 *= z_scale;
     252           1 :     _33 *= z_scale;
     253           1 :     _34 *= z_scale;
     254           1 : }
     255             : 
     256             : //Returns the transpose of the matrix. For orthonormal
     257             : //matrices, this is the same is the inverse matrix
     258           0 : CMatrix3D CMatrix3D::GetTranspose() const
     259             : {
     260           0 :     return CMatrix3D(
     261           0 :         _11, _21, _31, _41,
     262           0 :         _12, _22, _32, _42,
     263           0 :         _13, _23, _33, _43,
     264           0 :         _14, _24, _34, _44);
     265             : }
     266             : 
     267             : 
     268             : //Get a vector which points to the left of the matrix
     269          20 : CVector3D CMatrix3D::GetLeft() const
     270             : {
     271          20 :     return CVector3D(-_11, -_21, -_31);
     272             : }
     273             : 
     274             : //Get a vector which points up from the matrix
     275           0 : CVector3D CMatrix3D::GetUp() const
     276             : {
     277           0 :     return CVector3D(_12, _22, _32);
     278             : }
     279             : 
     280             : //Get a vector which points to front of the matrix
     281          13 : CVector3D CMatrix3D::GetIn() const
     282             : {
     283          13 :     return CVector3D(_13, _23, _33);
     284             : }
     285             : 
     286             : ///////////////////////////////////////////////////////////////////////////////
     287             : // RotateTransposed: rotate a vector by the transpose of this matrix
     288           0 : CVector3D CMatrix3D::RotateTransposed(const CVector3D& vector) const
     289             : {
     290           0 :     CVector3D result;
     291           0 :     RotateTransposed(vector,result);
     292           0 :     return result;
     293             : }
     294             : 
     295             : ///////////////////////////////////////////////////////////////////////////////
     296             : // RotateTransposed: rotate a vector by the transpose of this matrix
     297           0 : void CMatrix3D::RotateTransposed(const CVector3D& vector,CVector3D& result) const
     298             : {
     299           0 :     result.X = _11*vector.X + _21*vector.Y + _31*vector.Z;
     300           0 :     result.Y = _12*vector.X + _22*vector.Y + _32*vector.Z;
     301           0 :     result.Z = _13*vector.X + _23*vector.Y + _33*vector.Z;
     302           0 : }
     303             : 
     304             : 
     305          25 : void CMatrix3D::GetInverse(CMatrix3D& dst) const
     306             : {
     307          25 :     float tmp[12];  // temp array for pairs
     308          25 :     float src[16];  // array of transpose source matrix
     309          25 :     float det;      // determinant
     310             : 
     311             :     // transpose matrix
     312         125 :     for (int i = 0; i < 4; ++i) {
     313         100 :         src[i] = _data[i*4];
     314         100 :         src[i + 4] = _data[i*4 + 1];
     315         100 :         src[i + 8] = _data[i*4 + 2];
     316         100 :         src[i + 12] = _data[i*4 + 3];
     317             :     }
     318             : 
     319             :     // calculate pairs for first 8 elements (cofactors)
     320          25 :     tmp[0] = src[10] * src[15];
     321          25 :     tmp[1] = src[11] * src[14];
     322          25 :     tmp[2] = src[9] * src[15];
     323          25 :     tmp[3] = src[11] * src[13];
     324          25 :     tmp[4] = src[9] * src[14];
     325          25 :     tmp[5] = src[10] * src[13];
     326          25 :     tmp[6] = src[8] * src[15];
     327          25 :     tmp[7] = src[11] * src[12];
     328          25 :     tmp[8] = src[8] * src[14];
     329          25 :     tmp[9] = src[10] * src[12];
     330          25 :     tmp[10] = src[8] * src[13];
     331          25 :     tmp[11] = src[9] * src[12];
     332             : 
     333             :     // calculate first 8 elements (cofactors)
     334          25 :     dst._data[0] = (tmp[0]-tmp[1])*src[5] + (tmp[3]-tmp[2])*src[6] + (tmp[4]-tmp[5])*src[7];
     335          25 :     dst._data[1] = (tmp[1]-tmp[0])*src[4] + (tmp[6]-tmp[7])*src[6] + (tmp[9]-tmp[8])*src[7];
     336          25 :     dst._data[2] = (tmp[2]-tmp[3])*src[4] + (tmp[7]-tmp[6])*src[5] + (tmp[10]-tmp[11])*src[7];
     337          25 :     dst._data[3] = (tmp[5]-tmp[4])*src[4] + (tmp[8]-tmp[9])*src[5] + (tmp[11]-tmp[10])*src[6];
     338          25 :     dst._data[4] = (tmp[1]-tmp[0])*src[1] + (tmp[2]-tmp[3])*src[2] + (tmp[5]-tmp[4])*src[3];
     339          25 :     dst._data[5] = (tmp[0]-tmp[1])*src[0] + (tmp[7]-tmp[6])*src[2] + (tmp[8]-tmp[9])*src[3];
     340          25 :     dst._data[6] = (tmp[3]-tmp[2])*src[0] + (tmp[6]-tmp[7])*src[1] + (tmp[11]-tmp[10])*src[3];
     341          25 :     dst._data[7] = (tmp[4]-tmp[5])*src[0] + (tmp[9]-tmp[8])*src[1] + (tmp[10]-tmp[11])*src[2];
     342             : 
     343             :     // calculate pairs for second 8 elements (cofactors)
     344          25 :     tmp[0] = src[2]*src[7];
     345          25 :     tmp[1] = src[3]*src[6];
     346          25 :     tmp[2] = src[1]*src[7];
     347          25 :     tmp[3] = src[3]*src[5];
     348          25 :     tmp[4] = src[1]*src[6];
     349          25 :     tmp[5] = src[2]*src[5];
     350          25 :     tmp[6] = src[0]*src[7];
     351          25 :     tmp[7] = src[3]*src[4];
     352          25 :     tmp[8] = src[0]*src[6];
     353          25 :     tmp[9] = src[2]*src[4];
     354          25 :     tmp[10] = src[0]*src[5];
     355          25 :     tmp[11] = src[1]*src[4];
     356             : 
     357             :     // calculate second 8 elements (cofactors)
     358          25 :     dst._data[8] = (tmp[0]-tmp[1])*src[13] + (tmp[3]-tmp[2])*src[14] + (tmp[4]-tmp[5])*src[15];
     359          25 :     dst._data[9] = (tmp[1]-tmp[0])*src[12] + (tmp[6]-tmp[7])*src[14] + (tmp[9]-tmp[8])*src[15];
     360          25 :     dst._data[10] = (tmp[2]-tmp[3])*src[12] + (tmp[7]-tmp[6])*src[13] + (tmp[10]-tmp[11])*src[15];
     361          25 :     dst._data[11] = (tmp[5]-tmp[4])*src[12] + (tmp[8]-tmp[9])*src[13] + (tmp[11]-tmp[10])*src[14];
     362          25 :     dst._data[12] = (tmp[2]-tmp[3])*src[10] + (tmp[5]-tmp[4])*src[11] + (tmp[1]-tmp[0])*src[9];
     363          25 :     dst._data[13] = (tmp[7]-tmp[6])*src[10] + (tmp[8]-tmp[9])*src[11] + (tmp[0]-tmp[1])*src[8];
     364          25 :     dst._data[14] = (tmp[6]-tmp[7])*src[9] + (tmp[11]-tmp[10])*src[11] + (tmp[3]-tmp[2])*src[8];
     365          25 :     dst._data[15] = (tmp[10]-tmp[11])*src[10] + (tmp[4]-tmp[5])*src[8] + (tmp[9]-tmp[8])*src[9];
     366             : 
     367             :     // calculate matrix inverse
     368          25 :     det=src[0]*dst._data[0]+src[1]*dst._data[1]+src[2]*dst._data[2]+src[3]*dst._data[3];
     369          25 :     det = 1/det;
     370         425 :     for ( int j = 0; j < 16; j++) {
     371         400 :         dst._data[j] *= det;
     372             :     }
     373          25 : }
     374             : 
     375          13 : CMatrix3D CMatrix3D::GetInverse() const
     376             : {
     377          13 :     CMatrix3D r;
     378          13 :     GetInverse(r);
     379          13 :     return r;
     380             : }
     381             : 
     382           4 : void CMatrix3D::Rotate(const CQuaternion& quat)
     383             : {
     384           4 :     CMatrix3D rotationMatrix=quat.ToMatrix();
     385           4 :     Concatenate(rotationMatrix);
     386           4 : }
     387             : 
     388           4 : CQuaternion CMatrix3D::GetRotation() const
     389             : {
     390           4 :     float tr = _data2d[0][0] + _data2d[1][1] + _data2d[2][2];
     391             : 
     392           4 :     int next[] = { 1, 2, 0 };
     393             : 
     394           4 :     float quat[4];
     395             : 
     396           4 :     if (tr > 0.f)
     397             :     {
     398           2 :         float s = sqrtf(tr + 1.f);
     399           2 :         quat[3] = s * 0.5f;
     400           2 :         s = 0.5f / s;
     401           2 :         quat[0] = (_data2d[1][2] - _data2d[2][1]) * s;
     402           2 :         quat[1] = (_data2d[2][0] - _data2d[0][2]) * s;
     403           2 :         quat[2] = (_data2d[0][1] - _data2d[1][0]) * s;
     404             :     }
     405             :     else
     406             :     {
     407           2 :         int i = 0;
     408           2 :         if (_data2d[1][1] > _data2d[0][0]) i = 1;
     409           2 :         if (_data2d[2][2] > _data2d[i][i]) i = 2;
     410           2 :         int j = next[i];
     411           2 :         int k = next[j];
     412             : 
     413           2 :         float s = sqrtf((_data2d[i][i] - (_data2d[j][j] + _data2d[k][k])) + 1.f);
     414           2 :         quat[i] = s * 0.5f;
     415             : 
     416           2 :         if (s != 0.f) s = 0.5f / s;
     417             : 
     418           2 :         quat[3] = (_data2d[j][k] - _data2d[k][j]) * s;
     419           2 :         quat[j] = (_data2d[i][j] + _data2d[j][i]) * s;
     420           2 :         quat[k] = (_data2d[i][k] + _data2d[k][i]) * s;
     421             :     }
     422             : 
     423           4 :     return CQuaternion(quat[0], quat[1], quat[2], quat[3]);
     424             : }
     425             : 
     426           0 : void CMatrix3D::SetRotation(const CQuaternion& quat)
     427             : {
     428           0 :     quat.ToMatrix(*this);
     429           0 : }
     430             : 
     431          20 : float CMatrix3D::GetYRotation() const
     432             : {
     433             :     // Project the X axis vector onto the XZ plane
     434          20 :     CVector3D axis = -GetLeft();
     435          20 :     axis.Y = 0;
     436             : 
     437             :     // Normalise projected vector
     438             : 
     439          20 :     float len = axis.Length();
     440          20 :     if (len < 0.0001f)
     441             :         return 0.f;
     442          17 :     axis *= 1.0f/len;
     443             : 
     444             :     // Negate the return angle to match the SetYRotation convention
     445          17 :     return -atan2(axis.Z, axis.X);
     446             : }

Generated by: LCOV version 1.16