2 // vector.cpp: Various structures used for 3 dimensional imaging
5 // (C) 2006 Underground Software
7 // JLH = James L. Hammons <jlhamm@acm.org>
10 // --- ---------- ------------------------------------------------------------
11 // JLH 09/19/2006 Created this file
12 // JLH 03/22/2011 Moved implementation of constructor from header to here
13 // JLH 04/02/2011 Fixed divide-by-zero bug in Unit(), added Angle() function
14 // JLH 08/04/2013 Added Parameter() function
19 #include <math.h> // For sqrt()
20 #include "mathconstants.h"
22 // Vector implementation
24 Vector::Vector(double xx/*= 0*/, double yy/*= 0*/, double zz/*= 0*/): x(xx), y(yy), z(zz)
29 Vector::Vector(Vector tail, Vector head): x(head.x - tail.x), y(head.y - tail.y), z(head.z - tail.z)
34 Vector Vector::operator=(Vector const v)
36 x = v.x, y = v.y, z = v.z;
42 Vector Vector::operator+(Vector const v)
44 return Vector(x + v.x, y + v.y, z + v.z);
48 Vector Vector::operator-(Vector const v)
50 return Vector(x - v.x, y - v.y, z - v.z);
56 Vector Vector::operator-(void)
58 return Vector(-x, -y, -z);
64 Vector Vector::operator*(double const v)
66 return Vector(x * v, y * v, z * v);
72 Vector Vector::operator*(float const v)
74 return Vector(x * v, y * v, z * v);
80 Vector Vector::operator/(double const v)
82 return Vector(x / v, y / v, z / v);
88 Vector Vector::operator/(float const v)
90 return Vector(x / v, y / v, z / v);
94 // Vector (cross) product
96 Vector Vector::operator*(Vector const v)
98 // a x b = [a2b3 - a3b2, a3b1 - a1b3, a1b2 - a2b1]
99 return Vector((y * v.z) - (z * v.y), (z * v.x) - (x * v.z), (x * v.y) - (y * v.x));
105 double Vector::Dot(Vector const v)
107 return (x * v.x) + (y * v.y) + (z * v.z);
111 // Vector x constant, self assigned
113 Vector& Vector::operator*=(double const v)
115 x *= v, y *= v, z *= v;
121 // Vector / constant, self assigned
123 Vector& Vector::operator/=(double const v)
125 x /= v, y /= v, z /= v;
130 // Vector + vector, self assigned
132 Vector& Vector::operator+=(Vector const v)
134 x += v.x, y += v.y, z += v.z;
140 // Vector + constant, self assigned
142 Vector& Vector::operator+=(double const v)
144 x += v, y += v, z += v;
150 // Vector - vector, self assigned
152 Vector& Vector::operator-=(Vector const v)
154 x -= v.x, y -= v.y, z -= v.z;
160 // Vector - constant, self assigned
162 Vector& Vector::operator-=(double const v)
164 x -= v, y -= v, z -= v;
170 // Check for equality
171 bool Vector::operator==(Vector const v)
173 return (x == v.x && y == v.y && z == v.z ? true : false);
177 // Check for inequality
178 bool Vector::operator!=(Vector const v)
180 return (x != v.x || y != v.y || z != v.z ? true : false);
184 Vector Vector::Unit(void)
186 double mag = Magnitude();
188 // If the magnitude of the vector is zero, then the Unit vector is undefined...
190 return Vector(0, 0, 0);
192 return Vector(x / mag, y / mag, z / mag);
196 double Vector::Magnitude(void)
198 return sqrt(x * x + y * y + z * z);
202 double Vector::Angle(void)
204 // acos returns a value between zero and PI, which means we don't know which
205 // quadrant the angle is in... Though, if the y-coordinate of the vector is
206 // negative, that means that the angle is in quadrants III - IV.
207 double rawAngle = acos(Unit().x);
208 double correctedAngle = (y < 0 ? (2.0 * PI) - rawAngle : rawAngle);
210 return correctedAngle;
214 bool Vector::isZero(double epsilon/*= 1e-6*/)
216 return (fabs(x) < epsilon && fabs(y) < epsilon && fabs(z) < epsilon ? true : false);
222 double Vector::Dot(Vector v1, Vector v2)
224 return (v1.x * v2.x) + (v1.y * v2.y) + (v1.z * v2.z);
228 double Vector::Magnitude(Vector v1, Vector v2)
230 double xx = v1.x - v2.x;
231 double yy = v1.y - v2.y;
232 double zz = v1.z - v2.z;
233 return sqrt((xx * xx) + (yy * yy) + (zz * zz));
237 // Returns the parameter of a point in space to this vector. If the parameter
238 // is between 0 and 1, the normal of the vector to the point is on the vector.
239 // Note: v1 is the tail, v2 is the head of the line (vector).
240 double Vector::Parameter(Vector tail, Vector head, Vector p)
242 // Geometric interpretation:
243 // The parameterized point on the vector lineSegment is where the normal of
244 // the lineSegment to the point intersects lineSegment. If the pp < 0, then
245 // the perpendicular lies beyond the 1st endpoint. If pp > 1, then the
246 // perpendicular lies beyond the 2nd endpoint.
248 Vector lineSegment = head - tail;
249 double magnitude = lineSegment.Magnitude();
250 Vector pointSegment = p - tail;
251 double t = lineSegment.Dot(pointSegment) / (magnitude * magnitude);
256 // Return the normal to the linesegment formed by the passed in points.
257 // (Not sure which is head or tail, or which hand the normal lies)
258 // [v1 should be the tail, v2 should be the head, in which case the normal should
259 // rotate anti-clockwise.]
260 ///*static*/ Vector Vector::Normal(Vector v1, Vector v2)
261 /*static*/ Vector Vector::Normal(Vector tail, Vector head)
263 // Vector v = (v1 - v2).Unit();
264 Vector v = (head - tail).Unit();
265 return Vector(-v.y, v.x);