LCOV - code coverage report
Current view: top level - src - Vector.cpp (source / functions) Coverage Total Hit
Test: coverage.info Lines: 100.0 % 167 167
Test Date: 2026-04-03 02:26:39 Functions: 100.0 % 24 24

            Line data    Source code
       1              : #include "Vector.h"
       2              : 
       3              : //------------------------------------------------------------------------------
       4         2524 : Vector::Vector() {
       5         1262 :   x = y = z = 0.0f;
       6         2524 : }
       7              : 
       8              : //------------------------------------------------------------------------------
       9          214 : Vector::Vector(const float &tx, const float &ty, const float &tz) {
      10          107 :   x = tx; y = ty; z = tz;
      11          214 : }
      12              : 
      13              : //------------------------------------------------------------------------------
      14         1049 : void Vector::setVector(const float &tx, const float &ty, const float &tz) {
      15         1049 :   x = tx; y = ty; z = tz;
      16         1049 : }
      17              : 
      18              : //------------------------------------------------------------------------------
      19           21 : void Vector::normalize() {
      20           21 :   float len = sqrtf((x * x) + (y * y) + (z * z));
      21              : 
      22           21 :   if (len != 0.0f) {
      23           20 :     x /= len;
      24           20 :     y /= len;
      25           20 :     z /= len;
      26           20 :   }
      27           21 : }
      28              : 
      29              : //------------------------------------------------------------------------------
      30            2 : float Vector::dotProduct(const Vector &rhs) {
      31            2 :   return (((x * rhs.x) + (y * rhs.y) + (z * rhs.z)));
      32              : }
      33              : 
      34              : //------------------------------------------------------------------------------
      35           27 : void Vector::operator =(const Vector &rhs) {
      36           27 :   x = rhs.x; y = rhs.y; z = rhs.z;
      37           27 : }
      38              : 
      39              : //------------------------------------------------------------------------------
      40           10 : void Vector::rotateVector(const Matrix &M, const Vector &V) {
      41           10 :   x = (M.data[0] * V.x) + (M.data[4] * V.y) + (M.data[8] * V.z);
      42           10 :   y = (M.data[1] * V.x) + (M.data[5] * V.y) + (M.data[9] * V.z);
      43           10 :   z = (M.data[2] * V.x) + (M.data[6] * V.y) + (M.data[10] * V.z);
      44           10 : }
      45              : 
      46              : //------------------------------------------------------------------------------
      47            8 : void Vector::crossProduct(const Vector &d1, const Vector &d2) {
      48            8 :   x = d1.y * d2.z - d1.z * d2.y;
      49            8 :   y = d1.z * d2.x - d1.x * d2.z;
      50            8 :   z = d1.x * d2.y - d1.y * d2.x;
      51            8 : }
      52              : 
      53              : //------------------------------------------------------------------------------
      54            1 : void Vector::calcNorm(const Vector &p1, const Vector &p2, const Vector &p3) {
      55              :   // calculate the surface normal of the triangle formed by p1, p2 and p3
      56            1 :   Vector t1;
      57            1 :   Vector t2;
      58              : 
      59            1 :   t1.setVector(p2.x - p1.x, p2.y - p1.y, p2.z - p1.z);
      60            1 :   t2.setVector(p3.x - p1.x, p3.y - p1.y, p3.z - p1.z);
      61              : 
      62            1 :   crossProduct(t1, t2);
      63            1 :   normalize();
      64            1 : }
      65              : 
      66              : //------------------------------------------------------------------------------
      67            1 : void Vector::transformVector(const Matrix &m, const Vector &v) {
      68              :   float h;
      69              : 
      70            1 :   x = (v.x * m.data[0]) + (v.y * m.data[4]) + (v.z * m.data[8]) + m.data[12];
      71            1 :   y = (v.x * m.data[1]) + (v.y * m.data[5]) + (v.z * m.data[9]) + m.data[13];
      72            1 :   z = (v.x * m.data[2]) + (v.y * m.data[6]) + (v.z * m.data[10]) + m.data[14];
      73            1 :   h = (v.x * m.data[3]) + (v.y * m.data[7]) + (v.z * m.data[11]) + m.data[15];
      74              : 
      75              :   // x /= h;
      76              :   // y /= h;
      77              :   // z /= h;
      78            1 : }
      79              : 
      80              : //------------------------------------------------------------------------------
      81            1 : void Vector::average(const Vector &a, const Vector &b) {
      82            1 :   x = a.x + b.x;
      83            1 :   y = a.y + b.y;
      84            1 :   z = a.z + b.z;
      85              : 
      86            1 :   normalize();
      87            1 : }
      88              : 
      89              : //------------------------------------------------------------------------------
      90            1 : void Vector::scale(const float &m) {
      91            1 :   x *= m; y *= m; z *= m;
      92            1 : }
      93              : 
      94              : //------------------------------------------------------------------------------
      95            1 : float Vector::getDistance(const Vector &t) {
      96            1 :   float dx = t.x - x;
      97            1 :   float dy = t.y - y;
      98            1 :   float dz = t.z - z;
      99              : 
     100            1 :   return sqrtf(dx * dx + dy * dy + dz * dz);
     101              : }
     102              : 
     103              : //------------------------------------------------------------------------------
     104            1 : float Vector::getSum(void) {
     105            1 :   return (x + y + z);
     106              : }
     107              : 
     108              : //------------------------------------------------------------------------------
     109            2 : float Vector::getScaler(const Vector &vector) {
     110            2 :   Vector t;
     111            2 :   t.x = x * vector.x;
     112            2 :   t.y = y * vector.y;
     113            2 :   t.z = z * vector.z;
     114              : 
     115            2 :   return (t.x + t.y + t.z);
     116              : }
     117              : 
     118              : //------------------------------------------------------------------------------
     119           23 : bool Vector::intersectBox(
     120              :   const Vector &rayPosition, Vector collisionBox[],
     121              :   float extend, Vector &hitPoint
     122              : ) {
     123           23 :   if (x == 0.0f && y == 0.0f && z == 0.0f)
     124            1 :     return false;
     125              : 
     126           27 :   if (rayPosition.x >= collisionBox[0].x - extend && rayPosition.x <= collisionBox[1].x + extend &&
     127           12 :       rayPosition.y >= collisionBox[0].y - extend && rayPosition.y <= collisionBox[1].y + extend &&
     128            7 :       rayPosition.z >= collisionBox[0].z - extend && rayPosition.z <= collisionBox[1].z + extend) {
     129            3 :     hitPoint = rayPosition;
     130            3 :     return true;
     131              :   }
     132              : 
     133           19 :   bool hit = false;
     134           19 :   bool nearFaceFound = false;
     135           19 :   float tNear = 0.0f;
     136              : 
     137              :   // test planes with fixed x value //
     138           19 :   if (x > 0) {
     139            6 :     if (rayPosition.x <= collisionBox[0].x - extend) {
     140            5 :       tNear = (collisionBox[0].x - extend - rayPosition.x) / x;
     141            5 :       nearFaceFound = true;
     142            5 :     }
     143            6 :   }
     144           13 :   else if (x < 0) {
     145            2 :     if (rayPosition.x >= collisionBox[1].x + extend) {
     146            2 :       tNear = (collisionBox[1].x + extend - rayPosition.x) / x;
     147            2 :       nearFaceFound = true;
     148            2 :     }
     149            2 :   }
     150              : 
     151           19 :   if (nearFaceFound) {
     152            7 :     hitPoint.x = rayPosition.x + x * tNear;
     153            7 :     hitPoint.y = rayPosition.y + y * tNear;
     154            7 :     hitPoint.z = rayPosition.z + z * tNear;
     155              : 
     156           12 :     if (hitPoint.y <= collisionBox[1].y + extend && hitPoint.y >= collisionBox[0].y - extend &&
     157            5 :         hitPoint.z <= collisionBox[1].z + extend && hitPoint.z >= collisionBox[0].z - extend) {
     158            5 :       hit = true;
     159            5 :     }
     160            7 :   }
     161              :   //////////////////////////////////////
     162              : 
     163              :   // if no hit on x planes test planes with fixed y value //
     164           19 :   if (!hit) {
     165           14 :     nearFaceFound = false;
     166              : 
     167           14 :     if (y > 0) {
     168            4 :       if (rayPosition.y <= collisionBox[0].y - extend) {
     169            4 :         tNear = (collisionBox[0].y - extend - rayPosition.y) / y;
     170            4 :         nearFaceFound = true;
     171            4 :       }
     172            4 :     }
     173           10 :     else if (y < 0) {
     174            2 :       if (rayPosition.y >= collisionBox[1].y + extend) {
     175            2 :         tNear = (collisionBox[1].y + extend - rayPosition.y) / y;
     176            2 :         nearFaceFound = true;
     177            2 :       }
     178            2 :     }
     179              : 
     180           14 :     if (nearFaceFound) {
     181            6 :       hitPoint.x = rayPosition.x + x * tNear;
     182            6 :       hitPoint.y = rayPosition.y + y * tNear;
     183            6 :       hitPoint.z = rayPosition.z + z * tNear;
     184              : 
     185           10 :       if (hitPoint.x <= collisionBox[1].x + extend && hitPoint.x >= collisionBox[0].x - extend &&
     186            5 :           hitPoint.z <= collisionBox[1].z + extend && hitPoint.z >= collisionBox[0].z - extend) {
     187            4 :         hit = true;
     188            4 :       }
     189            6 :     }
     190           14 :   }
     191              :   ////////////////////////////////////////////////////////////
     192              : 
     193              :   // if no hit on x or y planes test planes with fixed z value //
     194           19 :   if (!hit) {
     195           10 :     nearFaceFound = false;
     196              : 
     197           10 :     if (z > 0) {
     198            3 :       if (rayPosition.z <= collisionBox[0].z - extend) {
     199            3 :         tNear = (collisionBox[0].z - extend - rayPosition.z) / z;
     200            3 :         nearFaceFound = true;
     201            3 :       }
     202            3 :     }
     203            7 :     else if (z < 0) {
     204            2 :       if (rayPosition.z >= collisionBox[1].z + extend) {
     205            2 :         tNear = (collisionBox[1].z + extend - rayPosition.z) / z;
     206            2 :         nearFaceFound = true;
     207            2 :       }
     208            2 :     }
     209              : 
     210           10 :     if (nearFaceFound) {
     211            5 :       hitPoint.x = rayPosition.x + x * tNear;
     212            5 :       hitPoint.y = rayPosition.y + y * tNear;
     213            5 :       hitPoint.z = rayPosition.z + z * tNear;
     214              : 
     215            9 :       if (hitPoint.x <= collisionBox[1].x + extend && hitPoint.x >= collisionBox[0].x - extend &&
     216            4 :           hitPoint.y <= collisionBox[1].y + extend && hitPoint.y >= collisionBox[0].y - extend) {
     217            4 :         hit = true;
     218            4 :       }
     219            5 :     }
     220           10 :   }
     221              :   ///////////////////////////////////////////////////////////////////
     222              : 
     223           19 :   return hit;
     224           23 : }
     225              : 
     226              : //------------------------------------------------------------------------------
     227           16 : bool Vector::intersectBox(const Vector &rayPosition, Vector collisionBox[], float extend) {
     228           16 :   Vector hitPoint;
     229           16 :   return intersectBox(rayPosition, collisionBox, extend, hitPoint);
     230              : }
     231              : 
     232              : //------------------------------------------------------------------------------
     233            1 : Vector Vector::operator +(const Vector &a) {
     234            1 :   Vector t;
     235            1 :   t.setVector(x + a.x, y + a.y, z + a.z);
     236            1 :   return t;
     237              : }
     238              : 
     239              : //------------------------------------------------------------------------------
     240            1 : Vector Vector::operator *(float a) {
     241            1 :   return Vector(a * x, a * y, a * z);
     242              : }
     243              : 
     244              : //------------------------------------------------------------------------------
     245            1 : void Vector::operator +=(const Vector &a) {
     246            1 :   x += (a.x); y += (a.y); z += (a.z);
     247            1 : }
     248              : 
     249              : //------------------------------------------------------------------------------
     250            1 : void Vector::operator -=(const Vector &a) {
     251            1 :   x -= (a.x); y -= (a.y); z -= (a.z);
     252            1 : }
     253              : 
     254              : //------------------------------------------------------------------------------
     255            1 : float Vector::getLength(void) {
     256            1 :   return (sqrtf((x * x) + (y * y) + (z * z)));
     257              : }
        

Generated by: LCOV version 2.4-0