Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
145 changes: 68 additions & 77 deletions Shared/sdk/CVector.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,53 +26,64 @@
class CVector
{
public:
float fX, fY, fZ;
float fX = 0.0f;
float fY = 0.0f;
float fZ = 0.0f;

CVector()
constexpr CVector() = default;

constexpr CVector(float x, float y, float z) :
fX(x),
fY(y),
fZ(z)
{
this->fX = 0;
this->fY = 0;
this->fZ = 0;
};
}

CVector(float fX, float fY, float fZ)
constexpr CVector(const CVector4D& vec) noexcept :
fX(vec.fX),
fY(vec.fY),
fZ(vec.fZ)
{
this->fX = fX;
this->fY = fY;
this->fZ = fZ;
}

CVector(const CVector4D& vec)
constexpr CVector& operator=(const CVector4D& vec) noexcept
{
this->fX = vec.fX;
this->fY = vec.fY;
this->fZ = vec.fZ;
fX = vec.fX;
fY = vec.fY;
fZ = vec.fZ;
return *this;
}


constexpr CVector Clone() const { return *this; }

// Normalize returns the normalized length of the vector.
float Normalize()
{
float t = sqrt(fX * fX + fY * fY + fZ * fZ);
const float t = Length();
if (t > FLOAT_EPSILON)
{
float fRcpt = 1 / t;
fX *= fRcpt;
fY *= fRcpt;
fZ *= fRcpt;
fX /= t;
fY /= t;
fZ /= t;

return t;
}
else
t = 0;
return t;
return 0;
}

float Length() const { return sqrt((fX * fX) + (fY * fY) + (fZ * fZ)); }
inline float Length() const { return sqrt((fX * fX) + (fY * fY) + (fZ * fZ)); }

float LengthSquared() const { return (fX * fX) + (fY * fY) + (fZ * fZ); }
// LengthSquared returns Length() without sqrt applied (i.e. returns x*x* + y*y + z*z).
// This can be useful if you only want to compare lengths.
inline float LengthSquared() const { return (fX * fX) + (fY * fY) + (fZ * fZ); }

float DotProduct(const CVector* param) const { return fX * param->fX + fY * param->fY + fZ * param->fZ; }
inline float DotProduct(const CVector* param) const { return fX * param->fX + fY * param->fY + fZ * param->fZ; }

void CrossProduct(const CVector* param)
{
float _fX = fX, _fY = fY, _fZ = fZ;
const float _fX = fX, _fY = fY, _fZ = fZ;
fX = _fY * param->fZ - param->fY * _fZ;
fY = _fZ * param->fX - param->fZ * _fX;
fZ = _fX * param->fY - param->fX * _fY;
Expand All @@ -83,7 +94,7 @@ class CVector
{
CVector vecRotation;
vecRotation.fZ = atan2(fY, fX);
CVector vecTemp(sqrt(fX * fX + fY * fY), fZ, 0);
CVector vecTemp(std::hypotf(fX, fY), fZ, 0);
vecTemp.Normalize();
vecRotation.fY = atan2(vecTemp.fX, vecTemp.fY) - PI / 2;
return vecRotation;
Expand All @@ -97,25 +108,16 @@ class CVector
vecResult = CVector(fZ, 0, -fX);
else
vecResult = CVector(0, -fZ, fY);
vecResult.Normalize();
return vecResult;
}

CVector Clone() const
{
CVector vecResult;
vecResult.fX = fX;
vecResult.fY = fY;
vecResult.fZ = fZ;
vecResult.Normalize();
return vecResult;
}

// Intersections code based on https://github.com/juj/MathGeoLib/blob/master/src/Geometry/Plane.h
bool IntesectsLinePlane(const CVector& vecRay, const CVector& vecNormal, const CVector& vecPosition, float* fOutDist) const
bool IntesectsLinePlane(const CVector& vecRay, const CVector& vecNormal, const CVector& vecPosition, float* fOutDist) const noexcept
{
bool bIntersects = false;
const float fDenom = vecNormal.DotProduct(&vecRay);

float fDenom = vecNormal.DotProduct(&vecRay);
if (fabs(fDenom) > 1e-4f)
{
*fOutDist = (vecPosition.Length() - vecNormal.DotProduct(this)) / fDenom;
Expand All @@ -132,22 +134,23 @@ class CVector
return fabs(vecNormal.DotProduct(this) - vecPosition.Length()) < 1e-3f;;
}

bool IntersectsSegmentPlane(const CVector& vecSegment, const CVector& vecNormal, const CVector& vecPosition, CVector* outVec) const
bool IntersectsSegmentPlane(const CVector& vecSegment, const CVector& vecNormal, const CVector& vecPosition, CVector* outVec) const noexcept
{
float fDist;
CVector vecRay = vecSegment;
vecRay.Normalize();
bool bIntersects = IntesectsLinePlane(vecRay, vecNormal, vecPosition, &fDist);

const float fSegLength = vecSegment.Length();

*outVec = *this + vecRay * fDist;
return bIntersects && fDist >= 0 && (fDist <= fSegLength);
}

// https://en.wikipedia.org/wiki/M%C3%B6ller%E2%80%93Trumbore_intersection_algorithm
bool IntersectsSegmentTriangle(const CVector& vecSegment, const CVector& vecVert1, const CVector& vecVert2, const CVector& vecVert3, CVector* outVec) const
bool IntersectsSegmentTriangle(const CVector& vecSegment, const CVector& vecVert1, const CVector& vecVert2, const CVector& vecVert3, CVector* outVec) const noexcept
{
const float fEpsilon = 1e-6f;
constexpr float fEpsilon = 1e-6f;

CVector vecEdge1, vecEdge2, h, s;
float a, f, u, v;
Expand Down Expand Up @@ -192,96 +195,84 @@ class CVector
return false;
}

CVector operator+(const CVector& vecRight) const { return CVector(fX + vecRight.fX, fY + vecRight.fY, fZ + vecRight.fZ); }
constexpr CVector operator+(const CVector& vecRight) const noexcept { return CVector(fX + vecRight.fX, fY + vecRight.fY, fZ + vecRight.fZ); }

CVector operator-(const CVector& vecRight) const { return CVector(fX - vecRight.fX, fY - vecRight.fY, fZ - vecRight.fZ); }

CVector operator*(const CVector& vecRight) const { return CVector(fX * vecRight.fX, fY * vecRight.fY, fZ * vecRight.fZ); }
constexpr CVector operator-(const CVector& vecRight) const noexcept { return CVector(fX - vecRight.fX, fY - vecRight.fY, fZ - vecRight.fZ); }

CVector operator*(float fRight) const { return CVector(fX * fRight, fY * fRight, fZ * fRight); }
constexpr CVector operator-() const noexcept { return CVector(-fX, -fY, -fZ); }

CVector operator/(const CVector& vecRight) const { return CVector(fX / vecRight.fX, fY / vecRight.fY, fZ / vecRight.fZ); }

CVector operator/(float fRight) const
{
float fRcpValue = 1 / fRight;
return CVector(fX * fRcpValue, fY * fRcpValue, fZ * fRcpValue);
}
constexpr CVector operator*(const CVector& vecRight) const noexcept { return CVector(fX * vecRight.fX, fY * vecRight.fY, fZ * vecRight.fZ); }

constexpr CVector operator*(const float fRight) const noexcept { return CVector(fX * fRight, fY * fRight, fZ * fRight); }


CVector operator-() const { return CVector(-fX, -fY, -fZ); }
constexpr CVector operator/(const CVector& vecRight) const noexcept { return CVector(fX / vecRight.fX, fY / vecRight.fY, fZ / vecRight.fZ); }

void operator+=(float fRight)
constexpr CVector operator/(const float fRight) const noexcept { return CVector(fX / fRight, fY / fRight, fZ / fRight); }

constexpr void operator+=(const float fRight) noexcept
{
fX += fRight;
fY += fRight;
fZ += fRight;
}

void operator+=(const CVector& vecRight)
constexpr void operator+=(const CVector& vecRight) noexcept
{
fX += vecRight.fX;
fY += vecRight.fY;
fZ += vecRight.fZ;
}

void operator-=(float fRight)
constexpr void operator-=(const float fRight) noexcept
{
fX -= fRight;
fY -= fRight;
fZ -= fRight;
}

void operator-=(const CVector& vecRight)
constexpr void operator-=(const CVector& vecRight) noexcept
{
fX -= vecRight.fX;
fY -= vecRight.fY;
fZ -= vecRight.fZ;
}

void operator*=(float fRight)
constexpr void operator*=(const float fRight) noexcept
{
fX *= fRight;
fY *= fRight;
fZ *= fRight;
}

void operator*=(const CVector& vecRight)
constexpr void operator*=(const CVector& vecRight) noexcept
{
fX *= vecRight.fX;
fY *= vecRight.fY;
fZ *= vecRight.fZ;
}

void operator/=(float fRight)
constexpr void operator/=(const float fRight) noexcept
{
float fRcpValue = 1 / fRight;
fX *= fRcpValue;
fY *= fRcpValue;
fZ *= fRcpValue;
fX /= fRight;
fY /= fRight;
fZ /= fRight;
}

void operator/=(const CVector& vecRight)
constexpr void operator/=(const CVector& vecRight) noexcept
{
fX /= vecRight.fX;
fY /= vecRight.fY;
fZ /= vecRight.fZ;
}

bool operator==(const CVector& param) const
{
return ((fabs(fX - param.fX) < FLOAT_EPSILON) && (fabs(fY - param.fY) < FLOAT_EPSILON) && (fabs(fZ - param.fZ) < FLOAT_EPSILON));
}

bool operator!=(const CVector& param) const
inline bool operator==(const CVector& param) const noexcept
{
return ((fabs(fX - param.fX) >= FLOAT_EPSILON) || (fabs(fY - param.fY) >= FLOAT_EPSILON) || (fabs(fZ - param.fZ) >= FLOAT_EPSILON));
return ((fabs(fX - param.fX) < FLOAT_EPSILON) && (fabs(fY - param.fY) < FLOAT_EPSILON) && (fabs(fZ - param.fZ) < FLOAT_EPSILON));
}

CVector& operator=(const CVector4D& vec)
{
fX = vec.fX;
fY = vec.fY;
fZ = vec.fZ;
return *this;
}
inline bool operator!=(const CVector& param) const noexcept { return !(*this == param); }
};
Loading