X-Git-Url: http://shamusworld.gotdns.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;ds=sidebyside;f=src%2Fvector.cpp;h=1f4c29cc5ae5b69eedd639efaa5fa0a6bc56883c;hb=cf3ec188764cdf34ff3472ee9806aba3a772d2df;hp=32fe4e1cb7f4e47046cf163827ad1260f195b9e4;hpb=0cdf0ebfb4b788156b8eb2c2acadd5f95fe5be26;p=ttedit diff --git a/src/vector.cpp b/src/vector.cpp index 32fe4e1..1f4c29c 100755 --- a/src/vector.cpp +++ b/src/vector.cpp @@ -1,3 +1,5 @@ +#if 0 + // // VECTOR.H - vector class definition // @@ -14,8 +16,8 @@ // JLH 05/15/2004 Added operator+ function // -#include #include "vector.h" +#include vector::vector(double a1/*= 0.0*/, double b1/*= 0.0*/, double c1/*= 0.0*/, double a2/*= 0.0*/, double b2/*= 0.0*/, double c2/*= 0.0*/): @@ -139,3 +141,258 @@ void vector::zero(const double epsilon/*= 1.0e-6*/) if (fabs(z) < epsilon) z = 0.0; } + +#else + +// +// vector.cpp: Various structures used for 3 dimensional imaging +// +// by James Hammons +// (C) 2006 Underground Software +// +// JLH = James L. Hammons +// +// WHO WHEN WHAT +// --- ---------- ------------------------------------------------------------ +// JLH 09/19/2006 Created this file +// JLH 03/22/2011 Moved implementation of constructor from header to here +// JLH 04/02/2011 Fixed divide-by-zero bug in Unit(), added Angle() function +// + +#include "vector.h" + +#include // For sqrt() +//#include "mathconstants.h" + +#define PI 3.14159265358979323846264338327 +#define RADIANS_TO_DEGREES (180.0 / PI) +#define DEGREES_TO_RADIANS (PI / 180.0) + + +// Vector implementation + +Vector::Vector(double x1/*= 0*/, double y1/*= 0*/, double z1/*= 0*/, + double x2/*= 0*/, double y2/*= 0*/, double z2/*= 0*/): + x(x1 - x2), y(y1 - y2), z(z1 - z2) +{ +} + + +Vector::Vector(Vector head, Vector tail): x(head.x - tail.x), y(head.y - tail.y), z(head.z - tail.z) +{ +} + + +Vector Vector::operator=(Vector const v) +{ + x = v.x, y = v.y, z = v.z; + + return *this; +} + + +Vector Vector::operator+(Vector const v) +{ + return Vector(x + v.x, y + v.y, z + v.z); +} + + +Vector Vector::operator-(Vector const v) +{ + return Vector(x - v.x, y - v.y, z - v.z); +} + + +// Unary negation + +Vector Vector::operator-(void) +{ + return Vector(-x, -y, -z); +} + + +// Vector x constant + +Vector Vector::operator*(double const v) +{ + return Vector(x * v, y * v, z * v); +} + + +// Vector x constant + +Vector Vector::operator*(float const v) +{ + return Vector(x * v, y * v, z * v); +} + + +// Vector / constant + +Vector Vector::operator/(double const v) +{ + return Vector(x / v, y / v, z / v); +} + + +// Vector / constant + +Vector Vector::operator/(float const v) +{ + return Vector(x / v, y / v, z / v); +} + + +// Vector (cross) product + +Vector Vector::operator*(Vector const v) +{ + // a x b = [a2b3 - a3b2, a3b1 - a1b3, a1b2 - a2b1] + return Vector((y * v.z) - (z * v.y), (z * v.x) - (x * v.z), (x * v.y) - (y * v.x)); +} + + +// Dot product + +double Vector::Dot(Vector const v) +{ + return (x * v.x) + (y * v.y) + (z * v.z); +} + + +// Vector x constant, self assigned + +Vector& Vector::operator*=(double const v) +{ + x *= v, y *= v, z *= v; + + return *this; +} + + +// Vector / constant, self assigned + +Vector& Vector::operator/=(double const v) +{ + x /= v, y /= v, z /= v; + + return *this; +} + + +// Vector + vector, self assigned + +Vector& Vector::operator+=(Vector const v) +{ + x += v.x, y += v.y, z += v.z; + + return *this; +} + + +// Vector + constant, self assigned + +Vector& Vector::operator+=(double const v) +{ + x += v, y += v, z += v; + + return *this; +} + + +// Vector - vector, self assigned + +Vector& Vector::operator-=(Vector const v) +{ + x -= v.x, y -= v.y, z -= v.z; + + return *this; +} + + +// Vector - constant, self assigned + +Vector& Vector::operator-=(double const v) +{ + x -= v, y -= v, z -= v; + + return *this; +} + + +// Check for equality +bool Vector::operator==(Vector const v) +{ + return (x == v.x && y == v.y && z == v.z ? true : false); +} + + +// Check for inequality +bool Vector::operator!=(Vector const v) +{ + return (x != v.x || y != v.y || z != v.z ? true : false); +} + + +Vector Vector::Unit(void) +{ + double mag = Magnitude(); + + // If the magnitude of the vector is zero, then the Unit vector is undefined... + if (mag == 0) + return Vector(0, 0, 0); + + return Vector(x / mag, y / mag, z / mag); +} + + +double Vector::Magnitude(void) +{ + return sqrt(x * x + y * y + z * z); +} + + +double Vector::Angle(void) +{ + // acos returns a value between zero and PI, which means we don't know which + // quadrant the angle is in... Though, if the y-coordinate of the vector is + // negative, that means that the angle is in quadrants III - IV. + double rawAngle = acos(Unit().x); + double correctedAngle = (y < 0 ? (2.0 * PI) - rawAngle : rawAngle); + + return correctedAngle; +} + + +// +// Angle between these two vectors +// +double Vector::Angle(Vector v) +{ + return Angle() - v.Angle(); +} + + +bool Vector::isZero(double epsilon/*= 1e-6*/) +{ + return (fabs(x) < epsilon && fabs(y) < epsilon && fabs(z) < epsilon ? true : false); +} + + +// Class methods + +double Vector::Dot(Vector v1, Vector v2) +{ + return (v1.x * v2.x) + (v1.y * v2.y) + (v1.z * v2.z); +} + + +double Vector::Magnitude(Vector v1, Vector v2) +{ + double xx = v1.x - v2.x; + double yy = v1.y - v2.y; + double zz = v1.z - v2.z; + return sqrt(xx * xx + yy * yy + zz * zz); +} + +#endif