From 21f20ce877a6ac3aa7483714c3cad50975b94a56 Mon Sep 17 00:00:00 2001 From: Loren Schwiebert Date: Tue, 14 May 2024 10:14:53 -0400 Subject: [PATCH 1/2] Extend and optimize implementation of the XYZ operators --- lib/BasicTypes.h | 63 +++++++++++++++++++++++------------------------- 1 file changed, 30 insertions(+), 33 deletions(-) diff --git a/lib/BasicTypes.h b/lib/BasicTypes.h index 2b9713e00..18097d20c 100644 --- a/lib/BasicTypes.h +++ b/lib/BasicTypes.h @@ -97,17 +97,18 @@ inline void record_debug(uint *x, uint len, std::string filename, //****************************************************************************** -typedef unsigned int uint; -typedef unsigned long int ulong; - -#define UNUSED(x) (void)(x) - -// single XYZ for use as a temporary and return type -struct XYZ { - double x, y, z; - +// single XYZ coordinate for use as a temporary and return type +class XYZ { +public: XYZ() : x(0.0), y(0.0), z(0.0) {} XYZ(double xVal, double yVal, double zVal) : x(xVal), y(yVal), z(zVal) {} + + friend inline std::ostream &operator<<(std::ostream &stream, const XYZ &p); + + inline double getX() const { return x; } + inline double getY() const { return y; } + inline double getZ() const { return z; } + void Reset() { x = y = z = 0.0; } XYZ &operator=(XYZ const &rhs) { x = rhs.x; @@ -115,26 +116,19 @@ struct XYZ { z = rhs.z; return *this; } - bool operator==(XYZ const &rhs) { - if (x == rhs.x && y == rhs.y && z == rhs.z) - return true; - return false; + inline bool operator==(XYZ const &rhs) const { + return (x == rhs.x && y == rhs.y && z == rhs.z); } - bool operator!=(XYZ const &rhs) { - if (x != rhs.x || y != rhs.y || z != rhs.z) - return true; - return false; + inline bool operator!=(XYZ const &rhs) const { + return (x != rhs.x || y != rhs.y || z != rhs.z); } - bool operator<(XYZ const &rhs) { - if (x < rhs.x && y < rhs.y && z < rhs.z) - return true; - return false; + inline bool operator<(XYZ const &rhs) const { + return (x < rhs.x && y < rhs.y && z < rhs.z); } - bool operator>(XYZ const &rhs) { - if (x > rhs.x || y > rhs.y || z > rhs.z) - return true; - return false; + inline bool operator>(XYZ const &rhs) const { + return (x > rhs.x || y > rhs.y || z > rhs.z); } + XYZ &operator+=(XYZ const &rhs) { x += rhs.x; y += rhs.y; @@ -167,14 +161,14 @@ struct XYZ { return *this; } - XYZ operator+(XYZ const &rhs) const { return XYZ(*this) += rhs; } - XYZ operator-(XYZ const &rhs) const { return XYZ(*this) -= rhs; } - XYZ operator*(XYZ const &rhs) const { return XYZ(*this) *= rhs; } - XYZ operator/(XYZ const &rhs) const { return XYZ(*this) /= rhs; } + XYZ operator+(XYZ const &rhs) const { return XYZ(x+rhs.x, y+rhs.y, z+rhs.z); } + XYZ operator-(XYZ const &rhs) const { return XYZ(x-rhs.x, y-rhs.y, z-rhs.z); } + XYZ operator*(XYZ const &rhs) const { return XYZ(x*rhs.x, y*rhs.y, z*rhs.z); } + XYZ operator/(XYZ const &rhs) const { return XYZ(x/rhs.x, y/rhs.y, z/rhs.z); } - XYZ operator*(const double a) const { return XYZ(*this) *= a; } + XYZ operator*(const double a) const { return XYZ(x*a, y*a, z*a); } - XYZ operator-() const { return XYZ(*this) * -1.0; } + XYZ operator-() const { return XYZ(-x, -y, -z); } void Inverse() { x = 1.0 / x; @@ -182,11 +176,11 @@ struct XYZ { z = 1.0 / z; } - double Length() const { return sqrt(LengthSq()); } double LengthSq() const { return x * x + y * y + z * z; } + double Length() const { return sqrt(LengthSq()); } XYZ &Normalize() { - *this *= (1 / Length()); + *this *= (1.0 / Length()); return *this; } @@ -207,6 +201,9 @@ struct XYZ { m = z; return m; } + +public: + double x, y, z; }; inline std::ostream &operator<<(std::ostream &stream, const XYZ &p) { From 8f05a7fb177eab5132f5fd798b21251b58ae0ab9 Mon Sep 17 00:00:00 2001 From: Loren Schwiebert Date: Mon, 28 Oct 2024 06:54:44 -0400 Subject: [PATCH 2/2] Explicitly call std library sqrt function --- lib/BasicTypes.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/BasicTypes.h b/lib/BasicTypes.h index 18097d20c..8907ec45d 100644 --- a/lib/BasicTypes.h +++ b/lib/BasicTypes.h @@ -177,7 +177,7 @@ class XYZ { } double LengthSq() const { return x * x + y * y + z * z; } - double Length() const { return sqrt(LengthSq()); } + double Length() const { return std::sqrt(LengthSq()); } XYZ &Normalize() { *this *= (1.0 / Length());