]> Shamusworld >> Repos - ttedit/blob - src/vector.cpp
Minor fixes to polygon adding, added Tau constants to math package.
[ttedit] / src / vector.cpp
1 #if 0
2
3 //
4 // VECTOR.H - vector class definition
5 //
6 // by James L. Hammons
7 // (C) 2004 Underground Software
8 //
9 // JLH = James L. Hammons <jlhamm@acm.org>
10 //
11 // Who  When        What
12 // ---  ----------  -----------------------------------------------------------
13 // JLH  ??/??/2003  Created original implementation
14 // JLH  05/14/2004  Separated header from implementation, added operator-
15 //                  function
16 // JLH  05/15/2004  Added operator+ function
17 //
18
19 #include "vector.h"
20 #include <math.h>
21
22 vector::vector(double a1/*= 0.0*/, double b1/*= 0.0*/, double c1/*= 0.0*/,
23         double a2/*= 0.0*/, double b2/*= 0.0*/, double c2/*= 0.0*/):
24         x(a1 - a2), y(b1 - b2), z(c1 - c2)
25 {
26 }
27
28 vector::vector(const vector &v1, const vector &v2):
29         x(v1.x - v2.x), y(v1.y - v2.y), z(v1.z - v2.z)
30 {
31 }
32
33 vector& vector::operator=(const vector &v)
34 {
35         x = v.x, y = v.y, z = v.z;
36         return *this;
37 }
38
39 bool vector::operator==(const vector &v)
40 {
41         if ((x == v.x) && (y == v.y) && (z == v.z))
42                 return true;
43
44         return false;
45 }
46
47 void vector::unitize(void)
48 {
49         double dist = sqrt(x*x + y*y + z*z);
50
51         if (dist != 0.0)
52                 x /= dist, y /= dist, z /= dist;
53
54         if (x == -0.0)
55                 x = +0.0;
56
57         if (y == -0.0)
58                 y = +0.0;
59
60         if (z == -0.0)
61                 z = +0.0;
62 }
63
64 vector vector::operator*(const vector &v)               // Cross product: "this" x "v"
65 {
66         vector r;
67
68         r.x = (y * v.z) - (v.y * z);
69         r.y = -((x * v.z) - (v.x * z));
70         r.z = (x * v.y) - (v.x * y);
71
72         return r;
73 }
74
75 vector vector::operator+(const vector &v)
76 {
77         return vector(x + v.x, y + v.y, z + v.z);
78 }
79
80 vector vector::operator-(const vector &v)
81 {
82         return vector(x, y, z, v.x, v.y, v.z);
83 }
84
85 double vector::dot(const vector &v1, const vector &v2)
86 {
87         return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;
88 }
89
90 double vector::dot(const vector &v)
91 {
92         return x * v.x + y * v.y + z * v.z;
93 }
94
95 double vector::distance(const vector &v)                // Pythagoras extended to 3 dimensions
96 {
97         double a = x - v.x, b = y - v.y, c = z - v.z;
98
99         return sqrt(a * a + b * b + c * c);
100 }
101
102 double vector::length(void)
103 {
104         return sqrt(x * x + y * y + z * z);
105 }
106
107 void vector::operator*=(const double &d)
108 {
109         x *= d, y *= d, z *= d;
110 }
111
112 void vector::operator/=(const double &d)
113 {
114         if (d != 0.0)
115                 x /= d, y /= d, z /= d;
116 }
117
118 void vector::operator+=(const vector &v)
119 {
120         x += v.x, y += v.y, z += v.z;
121 }
122
123 void vector::operator-=(const vector &v)
124 {
125         x -= v.x, y -= v.y, z -= v.z;
126 }
127
128 vector vector::operator*(const double &d)               // Scale vector by amount
129 {
130         return vector(x * d, y * d, z * d);
131 }
132
133 void vector::zero(const double epsilon/*= 1.0e-6*/)
134 {
135         if (fabs(x) < epsilon)
136                 x = 0.0;
137
138         if (fabs(y) < epsilon)
139                 y = 0.0;
140
141         if (fabs(z) < epsilon)
142                 z = 0.0;
143 }
144
145 #else
146
147 //
148 // vector.cpp: Various structures used for 3 dimensional imaging
149 //
150 // by James Hammons
151 // (C) 2006 Underground Software
152 //
153 // JLH = James L. Hammons <jlhamm@acm.org>
154 //
155 // WHO  WHEN        WHAT
156 // ---  ----------  ----------------------------------------------------------
157 // JLH  09/19/2006  Created this file
158 // JLH  03/22/2011  Moved implementation of constructor from header to here
159 // JLH  04/02/2011  Fixed divide-by-zero bug in Unit(), added Angle() function
160 //
161
162 #include "vector.h"
163
164 #include <math.h>                                                               // For sqrt()
165 #include "mathconstants.h"
166
167
168 // Vector implementation
169
170 Vector::Vector(double x1/*= 0*/, double y1/*= 0*/, double z1/*= 0*/,
171         double x2/*= 0*/, double y2/*= 0*/, double z2/*= 0*/):
172         x(x1 - x2), y(y1 - y2), z(z1 - z2)
173 {
174 }
175
176
177 Vector::Vector(Vector head, Vector tail): x(head.x - tail.x), y(head.y - tail.y), z(head.z - tail.z)
178 {
179 }
180
181
182 Vector Vector::operator=(Vector const v)
183 {
184         x = v.x, y = v.y, z = v.z;
185
186         return *this;
187 }
188
189
190 Vector Vector::operator+(Vector const v)
191 {
192         return Vector(x + v.x, y + v.y, z + v.z);
193 }
194
195
196 Vector Vector::operator-(Vector const v)
197 {
198         return Vector(x - v.x, y - v.y, z - v.z);
199 }
200
201
202 // Unary negation
203
204 Vector Vector::operator-(void)
205 {
206         return Vector(-x, -y, -z);
207 }
208
209
210 // Vector x constant
211
212 Vector Vector::operator*(double const v)
213 {
214         return Vector(x * v, y * v, z * v);
215 }
216
217
218 // Vector x constant
219
220 Vector Vector::operator*(float const v)
221 {
222         return Vector(x * v, y * v, z * v);
223 }
224
225
226 // Vector / constant
227
228 Vector Vector::operator/(double const v)
229 {
230         return Vector(x / v, y / v, z / v);
231 }
232
233
234 // Vector / constant
235
236 Vector Vector::operator/(float const v)
237 {
238         return Vector(x / v, y / v, z / v);
239 }
240
241
242 // Vector (cross) product
243
244 Vector Vector::operator*(Vector const v)
245 {
246         // a x b = [a2b3 - a3b2, a3b1 - a1b3, a1b2 - a2b1]
247         return Vector((y * v.z) - (z * v.y), (z * v.x) - (x * v.z), (x * v.y) - (y * v.x));
248 }
249
250
251 // Dot product
252
253 double Vector::Dot(Vector const v)
254 {
255         return (x * v.x) + (y * v.y) + (z * v.z);
256 }
257
258
259 // Vector x constant, self assigned
260
261 Vector& Vector::operator*=(double const v)
262 {
263         x *= v, y *= v, z *= v;
264
265         return *this;
266 }
267
268
269 // Vector / constant, self assigned
270
271 Vector& Vector::operator/=(double const v)
272 {
273         x /= v, y /= v, z /= v;
274
275         return *this;
276 }
277
278
279 // Vector + vector, self assigned
280
281 Vector& Vector::operator+=(Vector const v)
282 {
283         x += v.x, y += v.y, z += v.z;
284
285         return *this;
286 }
287
288
289 // Vector + constant, self assigned
290
291 Vector& Vector::operator+=(double const v)
292 {
293         x += v, y += v, z += v;
294
295         return *this;
296 }
297
298
299 // Vector - vector, self assigned
300
301 Vector& Vector::operator-=(Vector const v)
302 {
303         x -= v.x, y -= v.y, z -= v.z;
304
305         return *this;
306 }
307
308
309 // Vector - constant, self assigned
310
311 Vector& Vector::operator-=(double const v)
312 {
313         x -= v, y -= v, z -= v;
314
315         return *this;
316 }
317
318
319 // Check for equality
320 bool Vector::operator==(Vector const v)
321 {
322         return ((x == v.x) && (y == v.y) && (z == v.z) ? true : false);
323 }
324
325
326 // Check for inequality
327 bool Vector::operator!=(Vector const v)
328 {
329         return ((x != v.x) || (y != v.y) || (z != v.z) ? true : false);
330 }
331
332
333 Vector Vector::Unit(void)
334 {
335         double mag = Magnitude();
336
337         // If the magnitude of the vector is zero, then the Unit vector is undefined...
338         if (mag == 0)
339                 return Vector(0, 0, 0);
340
341         return Vector(x / mag, y / mag, z / mag);
342 }
343
344
345 double Vector::Magnitude(void)
346 {
347         return sqrt((x * x) + (y * y) + (z * z));
348 }
349
350
351 double Vector::Angle(void)
352 {
353         // acos returns a value between zero and PI, which means we don't know
354         // which quadrant the angle is in... Though, if the y-coordinate of the
355         // vector is negative, that means that the angle is in quadrants III - IV.
356         double rawAngle = acos(Unit().x);
357         double correctedAngle = (y < 0 ? TAU - rawAngle : rawAngle);
358
359         return correctedAngle;
360 }
361
362
363 //
364 // Returns the smallest angle between these two vectors
365 //
366 double Vector::Angle(Vector v)
367 {
368 // seems that something relies on this bad behavior... :-P
369 #if 0
370         // Discard the sign from the subtraction
371         double angle = fabs(Angle() - v.Angle());
372
373         // Return the complementary angle if greater than 180⁰
374         return (angle <= 180.0 ? angle : 360.0 - angle);
375 #else
376         return Angle() - v.Angle();
377 #endif
378 }
379
380
381 bool Vector::isZero(double epsilon/*= 1e-6*/)
382 {
383         return ((fabs(x) < epsilon) && (fabs(y) < epsilon) && (fabs(z) < epsilon) ? true : false);
384 }
385
386
387 // Class methods
388
389 double Vector::Dot(Vector v1, Vector v2)
390 {
391         return (v1.x * v2.x) + (v1.y * v2.y) + (v1.z * v2.z);
392 }
393
394
395 double Vector::Magnitude(Vector v1, Vector v2)
396 {
397         double xx = v1.x - v2.x;
398         double yy = v1.y - v2.y;
399         double zz = v1.z - v2.z;
400         return sqrt((xx * xx) + (yy * yy) + (zz * zz));
401 }
402
403 #endif