LCOV - code coverage report
Current view: top level - source/maths - Vector4D.h (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 15 20 75.0 %
Date: 2026-12-03 01:19:39 Functions: 0 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             :  * Provides an interface for a vector in R4 and allows vector and
      20             :  * scalar operations on it
      21             :  */
      22             : 
      23             : #ifndef INCLUDED_VECTOR4D
      24             : #define INCLUDED_VECTOR4D
      25             : 
      26             : #include <cstddef>
      27             : #include <span>
      28             : 
      29             : class CVector4D
      30             : {
      31             : public:
      32          89 :     CVector4D() : X(0.0f), Y(0.0f), Z(0.0f), W(0.0f) { }
      33             : 
      34          99 :     CVector4D(float x, float y, float z, float w) : X(x), Y(y), Z(z), W(w) { }
      35             : 
      36           3 :     bool operator==(const CVector4D& t) const
      37             :     {
      38           3 :         return (X == t.X && Y == t.Y && Z == t.Z && W == t.W);
      39             :     }
      40             : 
      41             :     bool operator!=(const CVector4D& t) const
      42             :     {
      43             :         return !(*this == t);
      44             :     }
      45             : 
      46             :     CVector4D operator-() const
      47             :     {
      48             :         return CVector4D(-X, -Y, -Z, -W);
      49             :     }
      50             : 
      51          12 :     CVector4D operator+(const CVector4D& t) const
      52             :     {
      53          12 :         return CVector4D(X+t.X, Y+t.Y, Z+t.Z, W+t.W);
      54             :     }
      55             : 
      56             :     CVector4D operator-(const CVector4D& t) const
      57             :     {
      58             :         return CVector4D(X-t.X, Y-t.Y, Z-t.Z, W-t.W);
      59             :     }
      60             : 
      61             :     CVector4D operator*(const CVector4D& t) const
      62             :     {
      63             :         return CVector4D(X*t.X, Y*t.Y, Z*t.Z, W*t.W);
      64             :     }
      65             : 
      66          12 :     CVector4D operator*(float f) const
      67             :     {
      68          12 :         return CVector4D(X*f, Y*f, Z*f, W*f);
      69             :     }
      70             : 
      71             :     CVector4D operator/(float f) const
      72             :     {
      73             :         float inv_f = 1.0f / f;
      74             :         return CVector4D(X*inv_f, Y*inv_f, Z*inv_f, W*inv_f);
      75             :     }
      76             : 
      77             :     CVector4D& operator+=(const CVector4D& t)
      78             :     {
      79             :         X += t.X;
      80             :         Y += t.Y;
      81             :         Z += t.Z;
      82             :         W += t.W;
      83             :         return *this;
      84             :     }
      85             : 
      86             :     CVector4D& operator-=(const CVector4D& t)
      87             :     {
      88             :         X -= t.X;
      89             :         Y -= t.Y;
      90             :         Z -= t.Z;
      91             :         W -= t.W;
      92             :         return *this;
      93             :     }
      94             : 
      95             :     CVector4D& operator*=(const CVector4D& t)
      96             :     {
      97             :         X *= t.X;
      98             :         Y *= t.Y;
      99             :         Z *= t.Z;
     100             :         W *= t.W;
     101             :         return *this;
     102             :     }
     103             : 
     104             :     CVector4D& operator*=(float f)
     105             :     {
     106             :         X *= f;
     107             :         Y *= f;
     108             :         Z *= f;
     109             :         W *= f;
     110             :         return *this;
     111             :     }
     112             : 
     113          24 :     CVector4D& operator/=(float f)
     114             :     {
     115          24 :         float inv_f = 1.0f / f;
     116          24 :         X *= inv_f;
     117          24 :         Y *= inv_f;
     118          24 :         Z *= inv_f;
     119          24 :         W *= inv_f;
     120          12 :         return *this;
     121             :     }
     122             : 
     123           0 :     float Dot(const CVector4D& a) const
     124             :     {
     125           0 :         return X*a.X + Y*a.Y + Z*a.Z + W*a.W;
     126             :     }
     127             : 
     128             :     // Returns 4 element array of floats, e.g. for vec4 uniforms.
     129           0 :     std::span<const float, 4> AsFloatArray() const
     130             :     {
     131             :         // Additional check to prevent a weird compiler has a different
     132             :         // alignement for an array and a class members.
     133           0 :         static_assert(
     134             :             sizeof(CVector4D) == sizeof(float) * 4u &&
     135             :             offsetof(CVector4D, X) == 0 &&
     136             :             offsetof(CVector4D, W) == sizeof(float) * 3u,
     137             :             "CVector4D should be properly layouted to use AsFloatArray");
     138           0 :         return std::span<const float, 4>(&X, 4);
     139             :     }
     140             : 
     141             :     float X, Y, Z, W;
     142             : };
     143             : 
     144             : #endif // INCLUDED_VECTOR4D

Generated by: LCOV version 1.16