]> Shamusworld >> Repos - architektonas/blobdiff - src/vector.cpp
Misc. fixes & additions
[architektonas] / src / vector.cpp
index ac5a2058191e4f7e11d24218ba8085e93d7372d8..11ddb6494962b724e8166b05502baa27612c0295 100644 (file)
-//\r
-// vector.cpp: Various structures used for 3 dimensional imaging\r
-//\r
-// by James Hammons\r
-// (C) 2006 Underground Software\r
-//\r
-// JLH = James L. Hammons <jlhamm@acm.org>\r
-//\r
-// WHO  WHEN        WHAT\r
-// ---  ----------  ------------------------------------------------------------\r
-// JLH  09/19/2006  Created this file\r
-// JLH  03/22/2011  Moved implementation of constructor from header to here\r
-// JLH  04/02/2011  Fixed divide-by-zero bug in Unit(), added Angle() function\r
-// JLH  08/04/2013  Added Parameter() function\r
-//\r
-\r
-#include "vector.h"\r
-\r
-#include <math.h>                                                              // For sqrt()\r
-#include "mathconstants.h"\r
-\r
-// Vector implementation\r
-\r
-Vector::Vector(double xx/*= 0*/, double yy/*= 0*/, double zz/*= 0*/): x(xx), y(yy), z(zz)\r
-{\r
-}\r
-\r
-\r
-Vector::Vector(Vector head, Vector tail): x(head.x - tail.x), y(head.y - tail.y), z(head.z - tail.z)\r
-{\r
-}\r
-\r
-\r
-Vector Vector::operator=(Vector const v)\r
-{\r
-       x = v.x, y = v.y, z = v.z;\r
-\r
-       return *this;\r
-}\r
-\r
-\r
-Vector Vector::operator+(Vector const v)\r
-{\r
-       return Vector(x + v.x, y + v.y, z + v.z);\r
-}\r
-\r
-\r
-Vector Vector::operator-(Vector const v)\r
-{\r
-       return Vector(x - v.x, y - v.y, z - v.z);\r
-}\r
-\r
-\r
-// Unary negation\r
-\r
-Vector Vector::operator-(void)\r
-{\r
-       return Vector(-x, -y, -z);\r
-}\r
-\r
-\r
-// Vector x constant\r
-\r
-Vector Vector::operator*(double const v)\r
-{\r
-       return Vector(x * v, y * v, z * v);\r
-}\r
-\r
-\r
-// Vector x constant\r
-\r
-Vector Vector::operator*(float const v)\r
-{\r
-       return Vector(x * v, y * v, z * v);\r
-}\r
-\r
-\r
-// Vector / constant\r
-\r
-Vector Vector::operator/(double const v)\r
-{\r
-       return Vector(x / v, y / v, z / v);\r
-}\r
-\r
-\r
-// Vector / constant\r
-\r
-Vector Vector::operator/(float const v)\r
-{\r
-       return Vector(x / v, y / v, z / v);\r
-}\r
-\r
-\r
-// Vector (cross) product\r
-\r
-Vector Vector::operator*(Vector const v)\r
-{\r
-       // a x b = [a2b3 - a3b2, a3b1 - a1b3, a1b2 - a2b1]\r
-       return Vector((y * v.z) - (z * v.y), (z * v.x) - (x * v.z), (x * v.y) - (y * v.x));\r
-}\r
-\r
-\r
-// Dot product\r
-\r
-double Vector::Dot(Vector const v)\r
-{\r
-       return (x * v.x) + (y * v.y) + (z * v.z);\r
-}\r
-\r
-\r
-// Vector x constant, self assigned\r
-\r
-Vector& Vector::operator*=(double const v)\r
-{\r
-       x *= v, y *= v, z *= v;\r
-\r
-       return *this;\r
-}\r
-\r
-\r
-// Vector / constant, self assigned\r
-\r
-Vector& Vector::operator/=(double const v)\r
-{\r
-       x /= v, y /= v, z /= v;\r
-\r
-       return *this;\r
-}\r
-\r
-// Vector + vector, self assigned\r
-\r
-Vector& Vector::operator+=(Vector const v)\r
-{\r
-       x += v.x, y += v.y, z += v.z;\r
-\r
-       return *this;\r
-}\r
-\r
-\r
-// Vector + constant, self assigned\r
-\r
-Vector& Vector::operator+=(double const v)\r
-{\r
-       x += v, y += v, z += v;\r
-\r
-       return *this;\r
-}\r
-\r
-\r
-// Vector - vector, self assigned\r
-\r
-Vector& Vector::operator-=(Vector const v)\r
-{\r
-       x -= v.x, y -= v.y, z -= v.z;\r
-\r
-       return *this;\r
-}\r
-\r
-\r
-// Vector - constant, self assigned\r
-\r
-Vector& Vector::operator-=(double const v)\r
-{\r
-       x -= v, y -= v, z -= v;\r
-\r
-       return *this;\r
-}\r
-\r
-\r
-// Check for equality\r
-bool Vector::operator==(Vector const v)\r
-{\r
-       return (x == v.x && y == v.y && z == v.z ? true : false);\r
-}\r
-\r
-\r
-// Check for inequality\r
-bool Vector::operator!=(Vector const v)\r
-{\r
-       return (x != v.x || y != v.y || z != v.z ? true : false);\r
-}\r
-\r
-\r
-Vector Vector::Unit(void)\r
-{\r
-       double mag = Magnitude();\r
-\r
-       // If the magnitude of the vector is zero, then the Unit vector is undefined...\r
-       if (mag == 0)\r
-               return Vector(0, 0, 0);\r
-\r
-       return Vector(x / mag, y / mag, z / mag);\r
-}\r
-\r
-\r
-double Vector::Magnitude(void)\r
-{\r
-       return sqrt(x * x + y * y + z * z);\r
-}\r
-\r
-\r
-double Vector::Angle(void)\r
-{\r
-       // acos returns a value between zero and PI, which means we don't know which\r
-       // quadrant the angle is in... Though, if the y-coordinate of the vector is\r
-       // negative, that means that the angle is in quadrants III - IV.\r
-       double rawAngle = acos(Unit().x);\r
-       double correctedAngle = (y < 0 ? (2.0 * PI) - rawAngle : rawAngle);\r
-\r
-       return correctedAngle;\r
-}\r
-\r
-\r
-bool Vector::isZero(double epsilon/*= 1e-6*/)\r
-{\r
-       return (fabs(x) < epsilon && fabs(y) < epsilon && fabs(z) < epsilon ? true : false);\r
-}\r
-\r
-\r
-// Class methods\r
-\r
-double Vector::Dot(Vector v1, Vector v2)\r
-{\r
-       return (v1.x * v2.x) + (v1.y * v2.y) + (v1.z * v2.z);\r
-}\r
-\r
-\r
-double Vector::Magnitude(Vector v1, Vector v2)\r
-{\r
-       double xx = v1.x - v2.x;\r
-       double yy = v1.y - v2.y;\r
-       double zz = v1.z - v2.z;\r
-       return sqrt(xx * xx + yy * yy + zz * zz);\r
-}\r
-\r
-\r
-// Returns the parameter of a point in space to this vector. If the parameter\r
-// is between 0 and 1, the normal of the vector to the point is on the vector.\r
-double Vector::Parameter(Vector v1, Vector v2, Vector p)\r
-{\r
-       // Geometric interpretation:\r
-       // The parameterized point on the vector lineSegment is where the normal of\r
-       // the lineSegment to the point intersects lineSegment. If the pp < 0, then\r
-       // the perpendicular lies beyond the 1st endpoint. If pp > 1, then the\r
-       // perpendicular lies beyond the 2nd endpoint.\r
-\r
-       Vector lineSegment = v1 - v2;\r
-       double magnitude = lineSegment.Magnitude();\r
-       Vector pointSegment = p - v2;\r
-       double t = lineSegment.Dot(pointSegment) / (magnitude * magnitude);\r
-       return t;\r
-}\r
-\r
-\r
-// Return the normal to the linesegment formed by the passed in points.\r
-// (Not sure which is head or tail, or which hand the normal lies)\r
-/*static*/ Vector Vector::Normal(Vector v1, Vector v2)\r
-{\r
-       Vector v = (v1 - v2).Unit();\r
-       return Vector(-v.y, v.x);\r
-}\r
-\r
+//
+// vector.cpp: Various structures used for 3 dimensional imaging
+//
+// by James Hammons
+// (C) 2006 Underground Software
+//
+// JLH = James L. Hammons <jlhamm@acm.org>
+//
+// 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
+// JLH  08/04/2013  Added Parameter() function
+//
+
+#include "vector.h"
+
+#include <math.h>                                                              // For sqrt()
+#include "mathconstants.h"
+
+// Vector implementation
+
+Vector::Vector(double xx/*= 0*/, double yy/*= 0*/, double zz/*= 0*/): x(xx), y(yy), z(zz)
+{
+}
+
+
+Vector::Vector(Vector tail, Vector head): 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;
+}
+
+
+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));
+}
+
+
+// Returns the parameter of a point in space to this vector. If the parameter
+// is between 0 and 1, the normal of the vector to the point is on the vector.
+// Note: v1 is the tail, v2 is the head of the line (vector).
+double Vector::Parameter(Vector tail, Vector head, Vector p)
+{
+       // Geometric interpretation:
+       // The parameterized point on the vector lineSegment is where the normal of
+       // the lineSegment to the point intersects lineSegment. If the pp < 0, then
+       // the perpendicular lies beyond the 1st endpoint. If pp > 1, then the
+       // perpendicular lies beyond the 2nd endpoint.
+
+       Vector lineSegment = head - tail;
+       double magnitude = lineSegment.Magnitude();
+       Vector pointSegment = p - tail;
+       double t = lineSegment.Dot(pointSegment) / (magnitude * magnitude);
+       return t;
+}
+
+
+// Return the normal to the linesegment formed by the passed in points.
+// (Not sure which is head or tail, or which hand the normal lies)
+// [v1 should be the tail, v2 should be the head, in which case the normal should
+//  rotate anti-clockwise.]
+///*static*/ Vector Vector::Normal(Vector v1, Vector v2)
+/*static*/ Vector Vector::Normal(Vector tail, Vector head)
+{
+//     Vector v = (v1 - v2).Unit();
+       Vector v = (head - tail).Unit();
+       return Vector(-v.y, v.x);
+}
+