From 7531c7636f306e8969b6daa4af3ade7eaa53050d Mon Sep 17 00:00:00 2001 From: Pirulax Date: Sun, 31 May 2020 14:40:37 +0200 Subject: [PATCH 1/7] Refactor CVector to use constexpr + noexcept everywhere possible --- Shared/sdk/CVector.h | 148 +++++++++++++++++++++---------------------- 1 file changed, 71 insertions(+), 77 deletions(-) diff --git a/Shared/sdk/CVector.h b/Shared/sdk/CVector.h index d4e938ae926..3fdf5accaac 100644 --- a/Shared/sdk/CVector.h +++ b/Shared/sdk/CVector.h @@ -26,53 +26,63 @@ 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; } + + // returns the length of the vector and normalizes it 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); } + // returns just the squared length(eg.: x*x* + y*y + z*z) + 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; @@ -83,7 +93,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; @@ -97,25 +107,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; @@ -132,12 +133,13 @@ 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; @@ -145,9 +147,9 @@ class CVector } // 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; @@ -192,96 +194,88 @@ 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); } - CVector operator-() const { return CVector(-fX, -fY, -fZ); } + constexpr CVector operator*(const float fRight) const noexcept { return CVector(fX * fRight, fY * fRight, fZ * fRight); } - void operator+=(float fRight) + + 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); } + + + 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); } }; From 029c23ab9300733033c4f6e1f1ece4cac1a00596 Mon Sep 17 00:00:00 2001 From: Pirulax Date: Sun, 31 May 2020 14:41:42 +0200 Subject: [PATCH 2/7] Refactor CVector4D to use constexpr + noexcept everywhere possible --- Shared/sdk/CVector4D.h | 106 +++++++++++++++-------------------------- 1 file changed, 39 insertions(+), 67 deletions(-) diff --git a/Shared/sdk/CVector4D.h b/Shared/sdk/CVector4D.h index 798d6bc3a41..5635a5cef94 100644 --- a/Shared/sdk/CVector4D.h +++ b/Shared/sdk/CVector4D.h @@ -20,48 +20,37 @@ class CVector4D { public: - CVector4D() - { - fX = 0; - fY = 0; - fZ = 0; - fW = 0; - } + float fX = 0.0f; + float fY = 0.0f; + float fZ = 0.0f; + float fW = 0.0f; - CVector4D(float _fX, float _fY, float _fZ, float _fW) - { - fX = _fX; - fY = _fY; - fZ = _fZ; - fW = _fW; - } + constexpr CVector4D() noexcept = default; - CVector4D(const CVector4D& vec) - { - fX = vec.fX; - fY = vec.fY; - fZ = vec.fZ; - fW = vec.fW; - } + constexpr CVector4D(const CVector4D&) noexcept = default; + + constexpr CVector4D& operator=(const CVector4D&) noexcept = default; - CVector4D& operator=(const CVector4D& vec) + constexpr CVector4D(float x, float y, float z, float w) noexcept : + fX(x), + fY(y), + fZ(z), + fW(w) { - fX = vec.fX; - fY = vec.fY; - fZ = vec.fZ; - fW = vec.fW; - return *this; } - float DotProduct(CVector4D& other) const { return fX * other.fX + fY * other.fY + fZ * other.fZ; } + // Warning, this function is returning the wrong value(fW is missing), its kept because nothing uses it, only + // CLuaVector4DDefs. + constexpr float DotProduct(const CVector4D& other) const noexcept { return fX * other.fX + fY * other.fY + fZ * other.fZ; } - float Length() const { return sqrt(fX * fX + fY * fY + fZ * fZ + fW * fW); } + float Length() const noexcept { return sqrt(fX * fX + fY * fY + fZ * fZ + fW * fW); } - float LengthSquared() const { return (fX * fX) + (fY * fY) + (fZ * fZ) + (fW * fW); } + // returns just the squared length(eg.: x*x* + y*y + z*z + w*w) + constexpr float LengthSquared() const noexcept { return (fX * fX) + (fY * fY) + (fZ * fZ) + (fW * fW); } - void Normalize() + void Normalize() noexcept { - float fLength = Length(); + const float fLength = Length(); if (fLength > 0.0f) { fX /= fLength; @@ -71,23 +60,22 @@ class CVector4D } } - CVector4D operator*(float fRight) const { return CVector4D(fX * fRight, fY * fRight, fZ * fRight, fW * fRight); } + constexpr CVector4D operator*(const CVector4D& vecRight) const { return CVector4D(fX * vecRight.fX, fY * vecRight.fY, fZ * vecRight.fZ, fW * vecRight.fW); } - CVector4D operator/(float fRight) const - { - float fRcpValue = 1 / fRight; - return CVector4D(fX * fRcpValue, fY * fRcpValue, fZ * fRcpValue, fW * fRcpValue); - } + constexpr CVector4D operator*(const float fRight) const noexcept { return CVector4D(fX * fRight, fY * fRight, fZ * fRight, fW * fRight); } - CVector4D operator+(const CVector4D& vecRight) const { return CVector4D(fX + vecRight.fX, fY + vecRight.fY, fZ + vecRight.fZ, fW + vecRight.fW); } - CVector4D operator-(const CVector4D& vecRight) const { return CVector4D(fX - vecRight.fX, fY - vecRight.fY, fZ - vecRight.fZ, fW - vecRight.fW); } + constexpr CVector4D operator+(const CVector4D& vecRight) const noexcept { return CVector4D(fX + vecRight.fX, fY + vecRight.fY, fZ + vecRight.fZ, fW + vecRight.fW); } - CVector4D operator*(const CVector4D& vecRight) const { return CVector4D(fX * vecRight.fX, fY * vecRight.fY, fZ * vecRight.fZ, fW * vecRight.fW); } + constexpr CVector4D operator-(const CVector4D& vecRight) const noexcept { return CVector4D(fX - vecRight.fX, fY - vecRight.fY, fZ - vecRight.fZ, fW - vecRight.fW); } - CVector4D operator/(const CVector4D& vecRight) const { return CVector4D(fX / vecRight.fX, fY / vecRight.fY, fZ / vecRight.fZ, fW / vecRight.fW); } - void operator+=(float fRight) + constexpr CVector4D operator/(const CVector4D& vecRight) const noexcept { return CVector4D(fX / vecRight.fX, fY / vecRight.fY, fZ / vecRight.fZ, fW / vecRight.fW); } + + constexpr CVector4D operator/(const float fRight) const noexcept { return CVector4D(fX / fRight, fY / fRight, fZ / fRight, fW / fRight); } + + + constexpr void operator+=(const float fRight) noexcept { fX += fRight; fY += fRight; @@ -95,7 +83,7 @@ class CVector4D fW += fRight; } - void operator+=(const CVector4D& vecRight) + constexpr void operator+=(const CVector4D& vecRight) noexcept { fX += vecRight.fX; fY += vecRight.fY; @@ -103,7 +91,7 @@ class CVector4D fW += vecRight.fW; } - void operator-=(float fRight) + constexpr void operator-=(const float fRight) noexcept { fX -= fRight; fY -= fRight; @@ -111,7 +99,7 @@ class CVector4D fW -= fRight; } - void operator-=(const CVector4D& vecRight) + constexpr void operator-=(const CVector4D& vecRight) noexcept { fX -= vecRight.fX; fY -= vecRight.fY; @@ -119,7 +107,7 @@ class CVector4D fW -= vecRight.fW; } - void operator*=(float fRight) + constexpr void operator*=(const float fRight) noexcept { fX *= fRight; fY *= fRight; @@ -127,15 +115,8 @@ class CVector4D fW *= fRight; } - void operator*=(const CVector4D& vecRight) - { - fX *= vecRight.fX; - fY *= vecRight.fY; - fZ *= vecRight.fZ; - fW *= vecRight.fW; - } - void operator/=(float fRight) + constexpr void operator/=(const float fRight) noexcept { fX /= fRight; fY /= fRight; @@ -143,7 +124,7 @@ class CVector4D fW /= fRight; } - void operator/=(const CVector4D& vecRight) + constexpr void operator/=(const CVector4D& vecRight) noexcept { fX /= vecRight.fX; fY /= vecRight.fY; @@ -151,20 +132,11 @@ class CVector4D fW /= vecRight.fW; } - bool operator==(const CVector4D& param) const + bool operator==(const CVector4D& param) const noexcept { return ((fabs(fX - param.fX) < FLOAT_EPSILON) && (fabs(fY - param.fY) < FLOAT_EPSILON) && (fabs(fZ - param.fZ) < FLOAT_EPSILON) && (fabs(fW - param.fW) < FLOAT_EPSILON)); } - bool operator!=(const CVector4D& param) const - { - return ((fabs(fX - param.fX) >= FLOAT_EPSILON) || (fabs(fY - param.fY) >= FLOAT_EPSILON) || (fabs(fZ - param.fZ) >= FLOAT_EPSILON) || - (fabs(fW - param.fW) >= FLOAT_EPSILON)); - } - - float fX; - float fY; - float fZ; - float fW; + bool operator!=(const CVector4D& param) const noexcept { return !(*this == param); } }; From 27e75c2d71b59cf15d8537ed3f1964d3043d5c54 Mon Sep 17 00:00:00 2001 From: Pirulax Date: Sun, 31 May 2020 14:41:52 +0200 Subject: [PATCH 3/7] Refactor CVector2D to use constexpr + noexcept everywhere possible --- Shared/sdk/CVector2D.h | 82 ++++++++++++++++++++---------------------- 1 file changed, 38 insertions(+), 44 deletions(-) diff --git a/Shared/sdk/CVector2D.h b/Shared/sdk/CVector2D.h index ec5573593b0..64929eb127e 100644 --- a/Shared/sdk/CVector2D.h +++ b/Shared/sdk/CVector2D.h @@ -20,53 +20,54 @@ class CVector2D { public: - CVector2D() - { - fX = 0; - fY = 0; - } + float fX = 0.0f; + float fY = 0.0f; + + constexpr CVector2D() noexcept = default; - CVector2D(float _fX, float _fY) + constexpr CVector2D(float x, float y) noexcept : + fX(x), + fY(y) { - fX = _fX; - fY = _fY; } - CVector2D(const CVector& vec) + constexpr CVector2D(const CVector& vec) noexcept : + fX(vec.fX), + fY(vec.fY) { - fX = vec.fX; - fY = vec.fY; } - CVector2D(const CVector4D& vec) + constexpr CVector2D(const CVector4D& vec) noexcept : + fX(vec.fX), + fY(vec.fY) { - fX = vec.fX; - fY = vec.fY; } - CVector2D& operator=(const CVector& vec) + + constexpr CVector2D& operator=(const CVector& vec) noexcept { fX = vec.fX; fY = vec.fY; return *this; } - CVector2D& operator=(const CVector4D& vec) + constexpr CVector2D& operator=(const CVector4D& vec) noexcept { fX = vec.fX; fY = vec.fY; return *this; } - float DotProduct(CVector2D& other) const { return fX * other.fX + fY * other.fY; } + constexpr float DotProduct(CVector2D& other) const { return fX * other.fX + fY * other.fY; } - float Length() const { return sqrt(fX * fX + fY * fY); } + float Length() const { return std::hypotf(fX, fY); } - float LengthSquared() const { return (fX * fX) + (fY * fY); } + // returns just the squared length(eg.: x*x* + y*y) + constexpr float LengthSquared() const noexcept { return (fX * fX) + (fY * fY); } - void Normalize() + inline void Normalize() noexcept { - float fLength = Length(); + const float fLength = Length(); if (fLength > 0.0f) { fX /= fLength; @@ -74,74 +75,67 @@ class CVector2D } } - CVector2D operator*(float fRight) const { return CVector2D(fX * fRight, fY * fRight); } + constexpr CVector2D operator*(float fRight) const noexcept { return CVector2D(fX * fRight, fY * fRight); } - CVector2D operator/(float fRight) const - { - float fRcpValue = 1 / fRight; - return CVector2D(fX * fRcpValue, fY * fRcpValue); - } + constexpr CVector2D operator/(float fRight) const noexcept { return CVector2D(fX / fRight, fY / fRight); } - CVector2D operator+(const CVector2D& vecRight) const { return CVector2D(fX + vecRight.fX, fY + vecRight.fY); } + constexpr CVector2D operator+(const CVector2D& vecRight) const noexcept { return CVector2D(fX + vecRight.fX, fY + vecRight.fY); } - CVector2D operator-(const CVector2D& vecRight) const { return CVector2D(fX - vecRight.fX, fY - vecRight.fY); } + constexpr CVector2D operator-(const CVector2D& vecRight) const noexcept { return CVector2D(fX - vecRight.fX, fY - vecRight.fY); } - CVector2D operator*(const CVector2D& vecRight) const { return CVector2D(fX * vecRight.fX, fY * vecRight.fY); } + constexpr CVector2D operator*(const CVector2D& vecRight) const noexcept { return CVector2D(fX * vecRight.fX, fY * vecRight.fY); } - CVector2D operator/(const CVector2D& vecRight) const { return CVector2D(fX / vecRight.fX, fY / vecRight.fY); } + constexpr CVector2D operator/(const CVector2D& vecRight) const noexcept { return CVector2D(fX / vecRight.fX, fY / vecRight.fY); } - void operator+=(float fRight) + constexpr void operator+=(float fRight) noexcept { fX += fRight; fY += fRight; } - void operator+=(const CVector2D& vecRight) + constexpr void operator+=(const CVector2D& vecRight) noexcept { fX += vecRight.fX; fY += vecRight.fY; } - void operator-=(float fRight) + constexpr void operator-=(float fRight) noexcept { fX -= fRight; fY -= fRight; } - void operator-=(const CVector2D& vecRight) + constexpr void operator-=(const CVector2D& vecRight) noexcept { fX -= vecRight.fX; fY -= vecRight.fY; } - void operator*=(float fRight) + constexpr void operator*=(float fRight) noexcept { fX *= fRight; fY *= fRight; } - void operator*=(const CVector2D& vecRight) + constexpr void operator*=(const CVector2D& vecRight) noexcept { fX *= vecRight.fX; fY *= vecRight.fY; } - void operator/=(float fRight) + constexpr void operator/=(float fRight) noexcept { fX /= fRight; fY /= fRight; } - void operator/=(const CVector2D& vecRight) + constexpr void operator/=(const CVector2D& vecRight) noexcept { fX /= vecRight.fX; fY /= vecRight.fY; } - bool operator==(const CVector2D& param) const { return ((fabs(fX - param.fX) < FLOAT_EPSILON) && (fabs(fY - param.fY) < FLOAT_EPSILON)); } - - bool operator!=(const CVector2D& param) const { return ((fabs(fX - param.fX) >= FLOAT_EPSILON) || (fabs(fY - param.fY) >= FLOAT_EPSILON)); } + bool operator==(const CVector2D& param) const noexcept { return ((fabs(fX - param.fX) < FLOAT_EPSILON) && (fabs(fY - param.fY) < FLOAT_EPSILON)); } - float fX; - float fY; + bool operator!=(const CVector2D& param) const noexcept { return !(*this == param); } }; From b93a57d5a9ec5e5bb37fe940cd73d7b7ed4888b6 Mon Sep 17 00:00:00 2001 From: Pirulax Date: Sun, 31 May 2020 21:04:47 +0200 Subject: [PATCH 4/7] Apply comment fixing suggestions CVector --- Shared/sdk/CVector.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Shared/sdk/CVector.h b/Shared/sdk/CVector.h index 3fdf5accaac..dd74472f084 100644 --- a/Shared/sdk/CVector.h +++ b/Shared/sdk/CVector.h @@ -75,7 +75,8 @@ class CVector inline float Length() const { return sqrt((fX * fX) + (fY * fY) + (fZ * fZ)); } - // returns just the squared length(eg.: x*x* + y*y + z*z) + // LengthSquared returns Length() without sqrt applied (i.e. returns x*x* + y*y + z*z) + // For more info see CMaterialLine3DBatcher::Flush() inline float LengthSquared() const { return (fX * fX) + (fY * fY) + (fZ * fZ); } inline float DotProduct(const CVector* param) const { return fX * param->fX + fY * param->fY + fZ * param->fZ; } From 82d051ea40ccc82fe9042f000f92e0baac55d09d Mon Sep 17 00:00:00 2001 From: Pirulax Date: Sun, 31 May 2020 21:05:06 +0200 Subject: [PATCH 5/7] Apply comment fixing suggestions CVector4D --- Shared/sdk/CVector4D.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Shared/sdk/CVector4D.h b/Shared/sdk/CVector4D.h index 5635a5cef94..d3b8f1f325c 100644 --- a/Shared/sdk/CVector4D.h +++ b/Shared/sdk/CVector4D.h @@ -45,7 +45,8 @@ class CVector4D float Length() const noexcept { return sqrt(fX * fX + fY * fY + fZ * fZ + fW * fW); } - // returns just the squared length(eg.: x*x* + y*y + z*z + w*w) + // LengthSquared returns Length() without sqrt applied (i.e. returns x*x* + y*y + z*z + w*w) + // For more info see CMaterialLine3DBatcher::Flush() constexpr float LengthSquared() const noexcept { return (fX * fX) + (fY * fY) + (fZ * fZ) + (fW * fW); } void Normalize() noexcept From 6985c85281349725eb2e3d0d4b0d10323bab6ca0 Mon Sep 17 00:00:00 2001 From: Pirulax Date: Sun, 31 May 2020 21:05:12 +0200 Subject: [PATCH 6/7] Apply comment fixing suggestions CVector2D --- Shared/sdk/CVector2D.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Shared/sdk/CVector2D.h b/Shared/sdk/CVector2D.h index 64929eb127e..213a78619f8 100644 --- a/Shared/sdk/CVector2D.h +++ b/Shared/sdk/CVector2D.h @@ -62,7 +62,8 @@ class CVector2D float Length() const { return std::hypotf(fX, fY); } - // returns just the squared length(eg.: x*x* + y*y) + // LengthSquared returns Length() without sqrt applied (i.e. returns x*x* + y*y) + // For more info see CMaterialLine3DBatcher::Flush() constexpr float LengthSquared() const noexcept { return (fX * fX) + (fY * fY); } inline void Normalize() noexcept From 2d2e16c234bf0ee666f774f310fb1d0d3a14e438 Mon Sep 17 00:00:00 2001 From: Qais Patankar Date: Mon, 8 Jun 2020 16:50:29 +0100 Subject: [PATCH 7/7] Apply suggestions from code review --- Shared/sdk/CVector.h | 10 +++------- Shared/sdk/CVector2D.h | 4 ++-- Shared/sdk/CVector4D.h | 5 ++--- 3 files changed, 7 insertions(+), 12 deletions(-) diff --git a/Shared/sdk/CVector.h b/Shared/sdk/CVector.h index dd74472f084..8fe136935bf 100644 --- a/Shared/sdk/CVector.h +++ b/Shared/sdk/CVector.h @@ -57,7 +57,7 @@ class CVector constexpr CVector Clone() const { return *this; } - // returns the length of the vector and normalizes it + // Normalize returns the normalized length of the vector. float Normalize() { const float t = Length(); @@ -75,8 +75,8 @@ class CVector inline float Length() const { return sqrt((fX * fX) + (fY * fY) + (fZ * fZ)); } - // LengthSquared returns Length() without sqrt applied (i.e. returns x*x* + y*y + z*z) - // For more info see CMaterialLine3DBatcher::Flush() + // 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); } inline float DotProduct(const CVector* param) const { return fX * param->fX + fY * param->fY + fZ * param->fZ; } @@ -212,7 +212,6 @@ class CVector 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; @@ -227,7 +226,6 @@ class CVector fZ += vecRight.fZ; } - constexpr void operator-=(const float fRight) noexcept { fX -= fRight; @@ -242,7 +240,6 @@ class CVector fZ -= vecRight.fZ; } - constexpr void operator*=(const float fRight) noexcept { fX *= fRight; @@ -257,7 +254,6 @@ class CVector fZ *= vecRight.fZ; } - constexpr void operator/=(const float fRight) noexcept { fX /= fRight; diff --git a/Shared/sdk/CVector2D.h b/Shared/sdk/CVector2D.h index 213a78619f8..f875a7b7161 100644 --- a/Shared/sdk/CVector2D.h +++ b/Shared/sdk/CVector2D.h @@ -62,8 +62,8 @@ class CVector2D float Length() const { return std::hypotf(fX, fY); } - // LengthSquared returns Length() without sqrt applied (i.e. returns x*x* + y*y) - // For more info see CMaterialLine3DBatcher::Flush() + // LengthSquared returns Length() without sqrt applied (i.e. returns x*x* + y*y). + // This can be useful if you only want to compare lengths. constexpr float LengthSquared() const noexcept { return (fX * fX) + (fY * fY); } inline void Normalize() noexcept diff --git a/Shared/sdk/CVector4D.h b/Shared/sdk/CVector4D.h index d3b8f1f325c..25b2dc35a3c 100644 --- a/Shared/sdk/CVector4D.h +++ b/Shared/sdk/CVector4D.h @@ -45,8 +45,8 @@ class CVector4D float Length() const noexcept { return sqrt(fX * fX + fY * fY + fZ * fZ + fW * fW); } - // LengthSquared returns Length() without sqrt applied (i.e. returns x*x* + y*y + z*z + w*w) - // For more info see CMaterialLine3DBatcher::Flush() + // LengthSquared returns Length() without sqrt applied (i.e. returns x*x* + y*y + z*z + w*w). + // This can be useful if you only want to compare lengths. constexpr float LengthSquared() const noexcept { return (fX * fX) + (fY * fY) + (fZ * fZ) + (fW * fW); } void Normalize() noexcept @@ -75,7 +75,6 @@ class CVector4D constexpr CVector4D operator/(const float fRight) const noexcept { return CVector4D(fX / fRight, fY / fRight, fZ / fRight, fW / fRight); } - constexpr void operator+=(const float fRight) noexcept { fX += fRight;