aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/linden/indra/llmath/v3dmath.h
diff options
context:
space:
mode:
authorJacek Antonelli2008-08-15 23:44:46 -0500
committerJacek Antonelli2008-08-15 23:44:46 -0500
commit38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4 (patch)
treeadca584755d22ca041a2dbfc35d4eca01f70b32c /linden/indra/llmath/v3dmath.h
parentREADME.txt (diff)
downloadmeta-impy-38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4.zip
meta-impy-38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4.tar.gz
meta-impy-38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4.tar.bz2
meta-impy-38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4.tar.xz
Second Life viewer sources 1.13.2.12
Diffstat (limited to 'linden/indra/llmath/v3dmath.h')
-rw-r--r--linden/indra/llmath/v3dmath.h437
1 files changed, 437 insertions, 0 deletions
diff --git a/linden/indra/llmath/v3dmath.h b/linden/indra/llmath/v3dmath.h
new file mode 100644
index 0000000..da13cc3
--- /dev/null
+++ b/linden/indra/llmath/v3dmath.h
@@ -0,0 +1,437 @@
1/**
2 * @file v3dmath.h
3 * @brief High precision 3 dimensional vector.
4 *
5 * Copyright (c) 2000-2007, Linden Research, Inc.
6 *
7 * The source code in this file ("Source Code") is provided by Linden Lab
8 * to you under the terms of the GNU General Public License, version 2.0
9 * ("GPL"), unless you have obtained a separate licensing agreement
10 * ("Other License"), formally executed by you and Linden Lab. Terms of
11 * the GPL can be found in doc/GPL-license.txt in this distribution, or
12 * online at http://secondlife.com/developers/opensource/gplv2
13 *
14 * There are special exceptions to the terms and conditions of the GPL as
15 * it is applied to this Source Code. View the full text of the exception
16 * in the file doc/FLOSS-exception.txt in this software distribution, or
17 * online at http://secondlife.com/developers/opensource/flossexception
18 *
19 * By copying, modifying or distributing this software, you acknowledge
20 * that you have read and understood your obligations described above,
21 * and agree to abide by those obligations.
22 *
23 * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
24 * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
25 * COMPLETENESS OR PERFORMANCE.
26 */
27
28#ifndef LL_V3DMATH_H
29#define LL_V3DMATH_H
30
31#include "llerror.h"
32#include "v3math.h"
33
34class LLVector3d
35{
36 public:
37 F64 mdV[3];
38
39 const static LLVector3d zero;
40 const static LLVector3d x_axis;
41 const static LLVector3d y_axis;
42 const static LLVector3d z_axis;
43 const static LLVector3d x_axis_neg;
44 const static LLVector3d y_axis_neg;
45 const static LLVector3d z_axis_neg;
46
47 inline LLVector3d(); // Initializes LLVector3d to (0, 0, 0)
48 inline LLVector3d(const F64 x, const F64 y, const F64 z); // Initializes LLVector3d to (x. y, z)
49 inline explicit LLVector3d(const F64 *vec); // Initializes LLVector3d to (vec[0]. vec[1], vec[2])
50 inline explicit LLVector3d(const LLVector3 &vec);
51 LLVector3d(const LLSD& sd)
52 {
53 setValue(sd);
54 }
55
56 void setValue(const LLSD& sd)
57 {
58 mdV[0] = sd[0].asReal();
59 mdV[1] = sd[1].asReal();
60 mdV[2] = sd[2].asReal();
61 }
62
63 const LLVector3d& operator=(const LLSD& sd)
64 {
65 setValue(sd);
66 return *this;
67 }
68
69 LLSD getValue() const
70 {
71 LLSD ret;
72 ret[0] = mdV[0];
73 ret[1] = mdV[1];
74 ret[2] = mdV[2];
75 return ret;
76 }
77
78 inline BOOL isFinite() const; // checks to see if all values of LLVector3d are finite
79 BOOL clamp(const F64 min, const F64 max); // Clamps all values to (min,max), returns TRUE if data changed
80 BOOL abs(); // sets all values to absolute value of original value (first octant), returns TRUE if changed
81
82 inline const LLVector3d& clearVec(); // Clears LLVector3d to (0, 0, 0, 1)
83 inline const LLVector3d& zeroVec(); // Zero LLVector3d to (0, 0, 0, 0)
84 inline const LLVector3d& setVec(const F64 x, const F64 y, const F64 z); // Sets LLVector3d to (x, y, z, 1)
85 inline const LLVector3d& setVec(const LLVector3d &vec); // Sets LLVector3d to vec
86 inline const LLVector3d& setVec(const F64 *vec); // Sets LLVector3d to vec
87 inline const LLVector3d& setVec(const LLVector3 &vec);
88
89 F64 magVec() const; // Returns magnitude of LLVector3d
90 F64 magVecSquared() const; // Returns magnitude squared of LLVector3d
91 inline F64 normVec(); // Normalizes and returns the magnitude of LLVector3d
92
93 const LLVector3d& rotVec(const F64 angle, const LLVector3d &vec); // Rotates about vec by angle radians
94 const LLVector3d& rotVec(const F64 angle, const F64 x, const F64 y, const F64 z); // Rotates about x,y,z by angle radians
95 const LLVector3d& rotVec(const LLMatrix3 &mat); // Rotates by LLMatrix4 mat
96 const LLVector3d& rotVec(const LLQuaternion &q); // Rotates by LLQuaternion q
97
98 BOOL isNull() const; // Returns TRUE if vector has a _very_small_ length
99 BOOL isExactlyZero() const { return !mdV[VX] && !mdV[VY] && !mdV[VZ]; }
100
101 const LLVector3d& operator=(const LLVector4 &a);
102
103 F64 operator[](int idx) const { return mdV[idx]; }
104 F64 &operator[](int idx) { return mdV[idx]; }
105
106 friend LLVector3d operator+(const LLVector3d &a, const LLVector3d &b); // Return vector a + b
107 friend LLVector3d operator-(const LLVector3d &a, const LLVector3d &b); // Return vector a minus b
108 friend F64 operator*(const LLVector3d &a, const LLVector3d &b); // Return a dot b
109 friend LLVector3d operator%(const LLVector3d &a, const LLVector3d &b); // Return a cross b
110 friend LLVector3d operator*(const LLVector3d &a, const F64 k); // Return a times scaler k
111 friend LLVector3d operator/(const LLVector3d &a, const F64 k); // Return a divided by scaler k
112 friend LLVector3d operator*(const F64 k, const LLVector3d &a); // Return a times scaler k
113 friend bool operator==(const LLVector3d &a, const LLVector3d &b); // Return a == b
114 friend bool operator!=(const LLVector3d &a, const LLVector3d &b); // Return a != b
115
116 friend const LLVector3d& operator+=(LLVector3d &a, const LLVector3d &b); // Return vector a + b
117 friend const LLVector3d& operator-=(LLVector3d &a, const LLVector3d &b); // Return vector a minus b
118 friend const LLVector3d& operator%=(LLVector3d &a, const LLVector3d &b); // Return a cross b
119 friend const LLVector3d& operator*=(LLVector3d &a, const F64 k); // Return a times scaler k
120 friend const LLVector3d& operator/=(LLVector3d &a, const F64 k); // Return a divided by scaler k
121
122 friend LLVector3d operator-(const LLVector3d &a); // Return vector -a
123
124 friend std::ostream& operator<<(std::ostream& s, const LLVector3d &a); // Stream a
125
126 static BOOL parseVector3d(const char* buf, LLVector3d* value);
127
128};
129
130typedef LLVector3d LLGlobalVec;
131
132const LLVector3d &LLVector3d::setVec(const LLVector3 &vec)
133{
134 mdV[0] = vec.mV[0];
135 mdV[1] = vec.mV[1];
136 mdV[2] = vec.mV[2];
137 return *this;
138}
139
140
141inline LLVector3d::LLVector3d(void)
142{
143 mdV[0] = 0.f;
144 mdV[1] = 0.f;
145 mdV[2] = 0.f;
146}
147
148inline LLVector3d::LLVector3d(const F64 x, const F64 y, const F64 z)
149{
150 mdV[VX] = x;
151 mdV[VY] = y;
152 mdV[VZ] = z;
153}
154
155inline LLVector3d::LLVector3d(const F64 *vec)
156{
157 mdV[VX] = vec[VX];
158 mdV[VY] = vec[VY];
159 mdV[VZ] = vec[VZ];
160}
161
162inline LLVector3d::LLVector3d(const LLVector3 &vec)
163{
164 mdV[VX] = vec.mV[VX];
165 mdV[VY] = vec.mV[VY];
166 mdV[VZ] = vec.mV[VZ];
167}
168
169/*
170inline LLVector3d::LLVector3d(const LLVector3d &copy)
171{
172 mdV[VX] = copy.mdV[VX];
173 mdV[VY] = copy.mdV[VY];
174 mdV[VZ] = copy.mdV[VZ];
175}
176*/
177
178// Destructors
179
180// checker
181inline BOOL LLVector3d::isFinite() const
182{
183 return (llfinite(mdV[VX]) && llfinite(mdV[VY]) && llfinite(mdV[VZ]));
184}
185
186
187// Clear and Assignment Functions
188
189inline const LLVector3d& LLVector3d::clearVec(void)
190{
191 mdV[0] = 0.f;
192 mdV[1] = 0.f;
193 mdV[2]= 0.f;
194 return (*this);
195}
196
197inline const LLVector3d& LLVector3d::zeroVec(void)
198{
199 mdV[0] = 0.f;
200 mdV[1] = 0.f;
201 mdV[2] = 0.f;
202 return (*this);
203}
204
205inline const LLVector3d& LLVector3d::setVec(const F64 x, const F64 y, const F64 z)
206{
207 mdV[VX] = x;
208 mdV[VY] = y;
209 mdV[VZ] = z;
210 return (*this);
211}
212
213inline const LLVector3d& LLVector3d::setVec(const LLVector3d &vec)
214{
215 mdV[0] = vec.mdV[0];
216 mdV[1] = vec.mdV[1];
217 mdV[2] = vec.mdV[2];
218 return (*this);
219}
220
221inline const LLVector3d& LLVector3d::setVec(const F64 *vec)
222{
223 mdV[0] = vec[0];
224 mdV[1] = vec[1];
225 mdV[2] = vec[2];
226 return (*this);
227}
228
229inline F64 LLVector3d::normVec(void)
230{
231 F64 mag = fsqrtf(mdV[0]*mdV[0] + mdV[1]*mdV[1] + mdV[2]*mdV[2]);
232 F64 oomag;
233
234 if (mag > FP_MAG_THRESHOLD)
235 {
236 oomag = 1.f/mag;
237 mdV[0] *= oomag;
238 mdV[1] *= oomag;
239 mdV[2] *= oomag;
240 }
241 else
242 {
243 mdV[0] = 0.f;
244 mdV[1] = 0.f;
245 mdV[2] = 0.f;
246 mag = 0;
247 }
248 return (mag);
249}
250
251// LLVector3d Magnitude and Normalization Functions
252
253inline F64 LLVector3d::magVec(void) const
254{
255 return fsqrtf(mdV[0]*mdV[0] + mdV[1]*mdV[1] + mdV[2]*mdV[2]);
256}
257
258inline F64 LLVector3d::magVecSquared(void) const
259{
260 return mdV[0]*mdV[0] + mdV[1]*mdV[1] + mdV[2]*mdV[2];
261}
262
263inline LLVector3d operator+(const LLVector3d &a, const LLVector3d &b)
264{
265 LLVector3d c(a);
266 return c += b;
267}
268
269inline LLVector3d operator-(const LLVector3d &a, const LLVector3d &b)
270{
271 LLVector3d c(a);
272 return c -= b;
273}
274
275inline F64 operator*(const LLVector3d &a, const LLVector3d &b)
276{
277 return (a.mdV[0]*b.mdV[0] + a.mdV[1]*b.mdV[1] + a.mdV[2]*b.mdV[2]);
278}
279
280inline LLVector3d operator%(const LLVector3d &a, const LLVector3d &b)
281{
282 return LLVector3d( a.mdV[1]*b.mdV[2] - b.mdV[1]*a.mdV[2], a.mdV[2]*b.mdV[0] - b.mdV[2]*a.mdV[0], a.mdV[0]*b.mdV[1] - b.mdV[0]*a.mdV[1] );
283}
284
285inline LLVector3d operator/(const LLVector3d &a, const F64 k)
286{
287 F64 t = 1.f / k;
288 return LLVector3d( a.mdV[0] * t, a.mdV[1] * t, a.mdV[2] * t );
289}
290
291inline LLVector3d operator*(const LLVector3d &a, const F64 k)
292{
293 return LLVector3d( a.mdV[0] * k, a.mdV[1] * k, a.mdV[2] * k );
294}
295
296inline LLVector3d operator*(F64 k, const LLVector3d &a)
297{
298 return LLVector3d( a.mdV[0] * k, a.mdV[1] * k, a.mdV[2] * k );
299}
300
301inline bool operator==(const LLVector3d &a, const LLVector3d &b)
302{
303 return ( (a.mdV[0] == b.mdV[0])
304 &&(a.mdV[1] == b.mdV[1])
305 &&(a.mdV[2] == b.mdV[2]));
306}
307
308inline bool operator!=(const LLVector3d &a, const LLVector3d &b)
309{
310 return ( (a.mdV[0] != b.mdV[0])
311 ||(a.mdV[1] != b.mdV[1])
312 ||(a.mdV[2] != b.mdV[2]));
313}
314
315inline const LLVector3d& operator+=(LLVector3d &a, const LLVector3d &b)
316{
317 a.mdV[0] += b.mdV[0];
318 a.mdV[1] += b.mdV[1];
319 a.mdV[2] += b.mdV[2];
320 return a;
321}
322
323inline const LLVector3d& operator-=(LLVector3d &a, const LLVector3d &b)
324{
325 a.mdV[0] -= b.mdV[0];
326 a.mdV[1] -= b.mdV[1];
327 a.mdV[2] -= b.mdV[2];
328 return a;
329}
330
331inline const LLVector3d& operator%=(LLVector3d &a, const LLVector3d &b)
332{
333 LLVector3d ret( a.mdV[1]*b.mdV[2] - b.mdV[1]*a.mdV[2], a.mdV[2]*b.mdV[0] - b.mdV[2]*a.mdV[0], a.mdV[0]*b.mdV[1] - b.mdV[0]*a.mdV[1]);
334 a = ret;
335 return a;
336}
337
338inline const LLVector3d& operator*=(LLVector3d &a, const F64 k)
339{
340 a.mdV[0] *= k;
341 a.mdV[1] *= k;
342 a.mdV[2] *= k;
343 return a;
344}
345
346inline const LLVector3d& operator/=(LLVector3d &a, const F64 k)
347{
348 F64 t = 1.f / k;
349 a.mdV[0] *= t;
350 a.mdV[1] *= t;
351 a.mdV[2] *= t;
352 return a;
353}
354
355inline LLVector3d operator-(const LLVector3d &a)
356{
357 return LLVector3d( -a.mdV[0], -a.mdV[1], -a.mdV[2] );
358}
359
360inline F64 dist_vec(const LLVector3d &a, const LLVector3d &b)
361{
362 F64 x = a.mdV[0] - b.mdV[0];
363 F64 y = a.mdV[1] - b.mdV[1];
364 F64 z = a.mdV[2] - b.mdV[2];
365 return fsqrtf( x*x + y*y + z*z );
366}
367
368inline F64 dist_vec_squared(const LLVector3d &a, const LLVector3d &b)
369{
370 F64 x = a.mdV[0] - b.mdV[0];
371 F64 y = a.mdV[1] - b.mdV[1];
372 F64 z = a.mdV[2] - b.mdV[2];
373 return x*x + y*y + z*z;
374}
375
376inline F64 dist_vec_squared2D(const LLVector3d &a, const LLVector3d &b)
377{
378 F64 x = a.mdV[0] - b.mdV[0];
379 F64 y = a.mdV[1] - b.mdV[1];
380 return x*x + y*y;
381}
382
383inline LLVector3d lerp(const LLVector3d &a, const LLVector3d &b, const F64 u)
384{
385 return LLVector3d(
386 a.mdV[VX] + (b.mdV[VX] - a.mdV[VX]) * u,
387 a.mdV[VY] + (b.mdV[VY] - a.mdV[VY]) * u,
388 a.mdV[VZ] + (b.mdV[VZ] - a.mdV[VZ]) * u);
389}
390
391
392inline BOOL LLVector3d::isNull() const
393{
394 if ( F_APPROXIMATELY_ZERO > mdV[VX]*mdV[VX] + mdV[VY]*mdV[VY] + mdV[VZ]*mdV[VZ] )
395 {
396 return TRUE;
397 }
398 return FALSE;
399}
400
401
402inline F64 angle_between(const LLVector3d& a, const LLVector3d& b)
403{
404 LLVector3d an = a;
405 LLVector3d bn = b;
406 an.normVec();
407 bn.normVec();
408 F64 cosine = an * bn;
409 F64 angle = (cosine >= 1.0f) ? 0.0f :
410 (cosine <= -1.0f) ? F_PI :
411 acos(cosine);
412 return angle;
413}
414
415inline BOOL are_parallel(const LLVector3d &a, const LLVector3d &b, const F64 epsilon)
416{
417 LLVector3d an = a;
418 LLVector3d bn = b;
419 an.normVec();
420 bn.normVec();
421 F64 dot = an * bn;
422 if ( (1.0f - fabs(dot)) < epsilon)
423 {
424 return TRUE;
425 }
426 return FALSE;
427
428}
429
430inline LLVector3d projected_vec(const LLVector3d &a, const LLVector3d &b)
431{
432 LLVector3d project_axis = b;
433 project_axis.normVec();
434 return project_axis * (a * project_axis);
435}
436
437#endif // LL_V3DMATH_H