]> Shamusworld >> Repos - architektonas/blob - src/vector.cpp
Misc. fixes & additions
[architektonas] / src / vector.cpp
1 //
2 // vector.cpp: Various structures used for 3 dimensional imaging
3 //
4 // by James Hammons
5 // (C) 2006 Underground Software
6 //
7 // JLH = James L. Hammons <jlhamm@acm.org>
8 //
9 // WHO  WHEN        WHAT
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
15 //
16
17 #include "vector.h"
18
19 #include <math.h>                                                               // For sqrt()
20 #include "mathconstants.h"
21
22 // Vector implementation
23
24 Vector::Vector(double xx/*= 0*/, double yy/*= 0*/, double zz/*= 0*/): x(xx), y(yy), z(zz)
25 {
26 }
27
28
29 Vector::Vector(Vector tail, Vector head): x(head.x - tail.x), y(head.y - tail.y), z(head.z - tail.z)
30 {
31 }
32
33
34 Vector Vector::operator=(Vector const v)
35 {
36         x = v.x, y = v.y, z = v.z;
37
38         return *this;
39 }
40
41
42 Vector Vector::operator+(Vector const v)
43 {
44         return Vector(x + v.x, y + v.y, z + v.z);
45 }
46
47
48 Vector Vector::operator-(Vector const v)
49 {
50         return Vector(x - v.x, y - v.y, z - v.z);
51 }
52
53
54 // Unary negation
55
56 Vector Vector::operator-(void)
57 {
58         return Vector(-x, -y, -z);
59 }
60
61
62 // Vector x constant
63
64 Vector Vector::operator*(double const v)
65 {
66         return Vector(x * v, y * v, z * v);
67 }
68
69
70 // Vector x constant
71
72 Vector Vector::operator*(float const v)
73 {
74         return Vector(x * v, y * v, z * v);
75 }
76
77
78 // Vector / constant
79
80 Vector Vector::operator/(double const v)
81 {
82         return Vector(x / v, y / v, z / v);
83 }
84
85
86 // Vector / constant
87
88 Vector Vector::operator/(float const v)
89 {
90         return Vector(x / v, y / v, z / v);
91 }
92
93
94 // Vector (cross) product
95
96 Vector Vector::operator*(Vector const v)
97 {
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));
100 }
101
102
103 // Dot product
104
105 double Vector::Dot(Vector const v)
106 {
107         return (x * v.x) + (y * v.y) + (z * v.z);
108 }
109
110
111 // Vector x constant, self assigned
112
113 Vector& Vector::operator*=(double const v)
114 {
115         x *= v, y *= v, z *= v;
116
117         return *this;
118 }
119
120
121 // Vector / constant, self assigned
122
123 Vector& Vector::operator/=(double const v)
124 {
125         x /= v, y /= v, z /= v;
126
127         return *this;
128 }
129
130 // Vector + vector, self assigned
131
132 Vector& Vector::operator+=(Vector const v)
133 {
134         x += v.x, y += v.y, z += v.z;
135
136         return *this;
137 }
138
139
140 // Vector + constant, self assigned
141
142 Vector& Vector::operator+=(double const v)
143 {
144         x += v, y += v, z += v;
145
146         return *this;
147 }
148
149
150 // Vector - vector, self assigned
151
152 Vector& Vector::operator-=(Vector const v)
153 {
154         x -= v.x, y -= v.y, z -= v.z;
155
156         return *this;
157 }
158
159
160 // Vector - constant, self assigned
161
162 Vector& Vector::operator-=(double const v)
163 {
164         x -= v, y -= v, z -= v;
165
166         return *this;
167 }
168
169
170 // Check for equality
171 bool Vector::operator==(Vector const v)
172 {
173         return (x == v.x && y == v.y && z == v.z ? true : false);
174 }
175
176
177 // Check for inequality
178 bool Vector::operator!=(Vector const v)
179 {
180         return (x != v.x || y != v.y || z != v.z ? true : false);
181 }
182
183
184 Vector Vector::Unit(void)
185 {
186         double mag = Magnitude();
187
188         // If the magnitude of the vector is zero, then the Unit vector is undefined...
189         if (mag == 0)
190                 return Vector(0, 0, 0);
191
192         return Vector(x / mag, y / mag, z / mag);
193 }
194
195
196 double Vector::Magnitude(void)
197 {
198         return sqrt(x * x + y * y + z * z);
199 }
200
201
202 double Vector::Angle(void)
203 {
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);
209
210         return correctedAngle;
211 }
212
213
214 bool Vector::isZero(double epsilon/*= 1e-6*/)
215 {
216         return (fabs(x) < epsilon && fabs(y) < epsilon && fabs(z) < epsilon ? true : false);
217 }
218
219
220 // Class methods
221
222 double Vector::Dot(Vector v1, Vector v2)
223 {
224         return (v1.x * v2.x) + (v1.y * v2.y) + (v1.z * v2.z);
225 }
226
227
228 double Vector::Magnitude(Vector v1, Vector v2)
229 {
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));
234 }
235
236
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)
241 {
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.
247
248         Vector lineSegment = head - tail;
249         double magnitude = lineSegment.Magnitude();
250         Vector pointSegment = p - tail;
251         double t = lineSegment.Dot(pointSegment) / (magnitude * magnitude);
252         return t;
253 }
254
255
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)
262 {
263 //      Vector v = (v1 - v2).Unit();
264         Vector v = (head - tail).Unit();
265         return Vector(-v.y, v.x);
266 }
267