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 // Create vector from angle + length (2D; z is set to zero)
35 void Vector::SetAngleAndLength(double angle, double length)
37 x = cos(angle) * length;
38 y = sin(angle) * length;
43 Vector Vector::operator=(Vector const v)
45 x = v.x, y = v.y, z = v.z;
51 Vector Vector::operator+(Vector const v)
53 return Vector(x + v.x, y + v.y, z + v.z);
57 Vector Vector::operator-(Vector const v)
59 return Vector(x - v.x, y - v.y, z - v.z);
65 Vector Vector::operator-(void)
67 return Vector(-x, -y, -z);
73 Vector Vector::operator*(double const v)
75 return Vector(x * v, y * v, z * v);
81 Vector Vector::operator*(float const v)
83 return Vector(x * v, y * v, z * v);
89 Vector Vector::operator/(double const v)
91 return Vector(x / v, y / v, z / v);
97 Vector Vector::operator/(float const v)
99 return Vector(x / v, y / v, z / v);
103 // Vector (cross) product
105 Vector Vector::operator*(Vector const v)
107 // a x b = [a2b3 - a3b2, a3b1 - a1b3, a1b2 - a2b1]
108 return Vector((y * v.z) - (z * v.y), (z * v.x) - (x * v.z), (x * v.y) - (y * v.x));
114 double Vector::Dot(Vector const v)
116 return (x * v.x) + (y * v.y) + (z * v.z);
120 // Vector x constant, self assigned
122 Vector& Vector::operator*=(double const v)
124 x *= v, y *= v, z *= v;
130 // Vector / constant, self assigned
132 Vector& Vector::operator/=(double const v)
134 x /= v, y /= v, z /= v;
139 // Vector + vector, self assigned
141 Vector& Vector::operator+=(Vector const v)
143 x += v.x, y += v.y, z += v.z;
149 // Vector + constant, self assigned
151 Vector& Vector::operator+=(double const v)
153 x += v, y += v, z += v;
159 // Vector - vector, self assigned
161 Vector& Vector::operator-=(Vector const v)
163 x -= v.x, y -= v.y, z -= v.z;
169 // Vector - constant, self assigned
171 Vector& Vector::operator-=(double const v)
173 x -= v, y -= v, z -= v;
179 // Check for equality
180 bool Vector::operator==(Vector const v)
182 return (x == v.x && y == v.y && z == v.z ? true : false);
186 // Check for inequality
187 bool Vector::operator!=(Vector const v)
189 return (x != v.x || y != v.y || z != v.z ? true : false);
193 Vector Vector::Unit(void)
195 double mag = Magnitude();
197 // If the magnitude of the vector is zero, then the Unit vector is undefined...
199 return Vector(0, 0, 0);
201 return Vector(x / mag, y / mag, z / mag);
205 double Vector::Magnitude(void)
207 return sqrt(x * x + y * y + z * z);
211 double Vector::Angle(void)
213 // acos returns a value between zero and PI, which means we don't know which
214 // quadrant the angle is in... Though, if the y-coordinate of the vector is
215 // negative, that means that the angle is in quadrants III - IV.
216 double rawAngle = acos(Unit().x);
217 double correctedAngle = (y < 0 ? (2.0 * PI) - rawAngle : rawAngle);
219 return correctedAngle;
223 bool Vector::isZero(double epsilon/*= 1e-6*/)
225 return (fabs(x) < epsilon && fabs(y) < epsilon && fabs(z) < epsilon ? true : false);
231 /*static*/ double Vector::Dot(Vector v1, Vector v2)
233 return (v1.x * v2.x) + (v1.y * v2.y) + (v1.z * v2.z);
237 /*static*/ double Vector::Magnitude(Vector v1, Vector v2)
239 double xx = v1.x - v2.x;
240 double yy = v1.y - v2.y;
241 double zz = v1.z - v2.z;
242 return sqrt((xx * xx) + (yy * yy) + (zz * zz));
247 // Convenience function
249 /*static*/ double Vector::Angle(Point p1, Point p2)
251 return Vector(p1, p2).Angle();
255 // Returns the parameter of a point in space to this vector. If the parameter
256 // is between 0 and 1, the normal of the vector to the point is on the vector.
257 // Note: v1 is the tail, v2 is the head of the line (vector).
258 /*static*/ double Vector::Parameter(Vector tail, Vector head, Vector p)
260 // Geometric interpretation:
261 // The parameterized point on the vector lineSegment is where the normal of
262 // the lineSegment to the point intersects lineSegment. If the pp < 0, then
263 // the perpendicular lies beyond the 1st endpoint. If pp > 1, then the
264 // perpendicular lies beyond the 2nd endpoint.
266 Vector lineSegment = head - tail;
267 double magnitude = lineSegment.Magnitude();
268 Vector pointSegment = p - tail;
269 double t = lineSegment.Dot(pointSegment) / (magnitude * magnitude);
274 // Return the 2D normal to the linesegment formed by the passed in points.
275 // The normal thus calculated should rotate anti-clockwise.
276 /*static*/ Vector Vector::Normal(Vector tail, Vector head)
278 Vector v = (head - tail).Unit();
279 return Vector(-v.y, v.x);
283 /*static*/ double Vector::AngleBetween(Vector a, Vector b)
285 // This is done using the following formula:
286 // (a . b) = ||a|| ||b|| cos(theta)
287 // However, have to check for two degenerate cases, where a = cb:
288 // 1, if c > 0, theta = 0; 2, if c < 0, theta = 180°.
289 // Also, the vectors a & b have to be non-zero.
290 // Also, have to check using an epsilon because acos will not return an
291 // exact value if the vectors are orthogonal
292 if (a.isZero() || b.isZero())
295 return acos(a.Dot(b) / (a.Magnitude() * b.Magnitude()));