Skip to content

Commit

Permalink
Extend and optimize implementation of the XYZ operators
Browse files Browse the repository at this point in the history
  • Loading branch information
LSchwiebert committed May 14, 2024
1 parent ff3f67a commit 21f20ce
Showing 1 changed file with 30 additions and 33 deletions.
63 changes: 30 additions & 33 deletions lib/BasicTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,44 +97,38 @@ 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;
y = rhs.y;
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;
Expand Down Expand Up @@ -167,26 +161,26 @@ 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;
y = 1.0 / y;
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;
}

Expand All @@ -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) {
Expand Down

0 comments on commit 21f20ce

Please sign in to comment.