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 19 78.9 %
Date: 2023-01-19 00:18:29 Functions: 6 8 75.0 %

          Line data    Source code
       1             : /* Copyright (C) 2022 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 "ps/containers/Span.h"
      27             : 
      28             : #include <math.h>
      29             : 
      30             : class CVector4D
      31             : {
      32             : public:
      33         101 :     CVector4D() : X(0.0f), Y(0.0f), Z(0.0f), W(0.0f) { }
      34             : 
      35         157 :     CVector4D(float x, float y, float z, float w) : X(x), Y(y), Z(z), W(w) { }
      36             : 
      37           3 :     bool operator==(const CVector4D& t) const
      38             :     {
      39           3 :         return (X == t.X && Y == t.Y && Z == t.Z && W == t.W);
      40             :     }
      41             : 
      42             :     bool operator!=(const CVector4D& t) const
      43             :     {
      44             :         return !(*this == t);
      45             :     }
      46             : 
      47             :     CVector4D operator-() const
      48             :     {
      49             :         return CVector4D(-X, -Y, -Z, -W);
      50             :     }
      51             : 
      52          12 :     CVector4D operator+(const CVector4D& t) const
      53             :     {
      54          12 :         return CVector4D(X+t.X, Y+t.Y, Z+t.Z, W+t.W);
      55             :     }
      56             : 
      57             :     CVector4D operator-(const CVector4D& t) const
      58             :     {
      59             :         return CVector4D(X-t.X, Y-t.Y, Z-t.Z, W-t.W);
      60             :     }
      61             : 
      62             :     CVector4D operator*(const CVector4D& t) const
      63             :     {
      64             :         return CVector4D(X*t.X, Y*t.Y, Z*t.Z, W*t.W);
      65             :     }
      66             : 
      67          24 :     CVector4D operator*(float f) const
      68             :     {
      69          24 :         return CVector4D(X*f, Y*f, Z*f, W*f);
      70             :     }
      71             : 
      72             :     CVector4D operator/(float f) const
      73             :     {
      74             :         float inv_f = 1.0f / f;
      75             :         return CVector4D(X*inv_f, Y*inv_f, Z*inv_f, W*inv_f);
      76             :     }
      77             : 
      78             :     CVector4D& operator+=(const CVector4D& t)
      79             :     {
      80             :         X += t.X;
      81             :         Y += t.Y;
      82             :         Z += t.Z;
      83             :         W += t.W;
      84             :         return *this;
      85             :     }
      86             : 
      87             :     CVector4D& operator-=(const CVector4D& t)
      88             :     {
      89             :         X -= t.X;
      90             :         Y -= t.Y;
      91             :         Z -= t.Z;
      92             :         W -= t.W;
      93             :         return *this;
      94             :     }
      95             : 
      96             :     CVector4D& operator*=(const CVector4D& t)
      97             :     {
      98             :         X *= t.X;
      99             :         Y *= t.Y;
     100             :         Z *= t.Z;
     101             :         W *= t.W;
     102             :         return *this;
     103             :     }
     104             : 
     105             :     CVector4D& operator*=(float f)
     106             :     {
     107             :         X *= f;
     108             :         Y *= f;
     109             :         Z *= f;
     110             :         W *= f;
     111             :         return *this;
     112             :     }
     113             : 
     114          24 :     CVector4D& operator/=(float f)
     115             :     {
     116          24 :         float inv_f = 1.0f / f;
     117          24 :         X *= inv_f;
     118          24 :         Y *= inv_f;
     119          24 :         Z *= inv_f;
     120          24 :         W *= inv_f;
     121          24 :         return *this;
     122             :     }
     123             : 
     124           0 :     float Dot(const CVector4D& a) const
     125             :     {
     126           0 :         return X*a.X + Y*a.Y + Z*a.Z + W*a.W;
     127             :     }
     128             : 
     129             :     // Returns 4 element array of floats, e.g. for vec4 uniforms.
     130           0 :     PS::span<const float> AsFloatArray() const
     131             :     {
     132             :         // Additional check to prevent a weird compiler has a different
     133             :         // alignement for an array and a class members.
     134             :         static_assert(
     135             :             sizeof(CVector4D) == sizeof(float) * 4u &&
     136             :             offsetof(CVector4D, X) == 0 &&
     137             :             offsetof(CVector4D, W) == sizeof(float) * 3u,
     138             :             "CVector4D should be properly layouted to use AsFloatArray");
     139           0 :         return PS::span<const float>(&X, 4);
     140             :     }
     141             : 
     142             :     float X, Y, Z, W;
     143             : };
     144             : 
     145             : #endif // INCLUDED_VECTOR4D

Generated by: LCOV version 1.13