aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/linden/indra/llmath/v4math.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/v4math.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/v4math.h')
-rw-r--r--linden/indra/llmath/v4math.h404
1 files changed, 404 insertions, 0 deletions
diff --git a/linden/indra/llmath/v4math.h b/linden/indra/llmath/v4math.h
new file mode 100644
index 0000000..06ac777
--- /dev/null
+++ b/linden/indra/llmath/v4math.h
@@ -0,0 +1,404 @@
1/**
2 * @file v4math.h
3 * @brief LLVector4 class header file.
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_V4MATH_H
29#define LL_V4MATH_H
30
31#include "llerror.h"
32#include "llmath.h"
33#include "v3math.h"
34
35class LLMatrix3;
36class LLMatrix4;
37class LLQuaternion;
38
39// LLVector4 = |x y z w|
40
41static const U32 LENGTHOFVECTOR4 = 4;
42
43#if LL_WINDOWS
44__declspec( align(16) )
45#endif
46
47class LLVector4
48{
49 public:
50 F32 mV[LENGTHOFVECTOR4];
51 LLVector4(); // Initializes LLVector4 to (0, 0, 0, 1)
52 explicit LLVector4(const F32 *vec); // Initializes LLVector4 to (vec[0]. vec[1], vec[2], 1)
53 explicit LLVector4(const LLVector3 &vec); // Initializes LLVector4 to (vec, 1)
54 explicit LLVector4(const LLVector3 &vec, F32 w); // Initializes LLVector4 to (vec, w)
55 LLVector4(F32 x, F32 y, F32 z); // Initializes LLVector4 to (x. y, z, 1)
56 LLVector4(F32 x, F32 y, F32 z, F32 w);
57
58 LLSD getValue() const
59 {
60 LLSD ret;
61 ret[0] = mV[0];
62 ret[1] = mV[1];
63 ret[2] = mV[2];
64 ret[3] = mV[3];
65 return ret;
66 }
67
68 inline BOOL isFinite() const; // checks to see if all values of LLVector3 are finite
69
70 inline void clearVec(); // Clears LLVector4 to (0, 0, 0, 1)
71 inline void zeroVec(); // zero LLVector4 to (0, 0, 0, 0)
72 inline void setVec(F32 x, F32 y, F32 z); // Sets LLVector4 to (x, y, z, 1)
73 inline void setVec(F32 x, F32 y, F32 z, F32 w); // Sets LLVector4 to (x, y, z, w)
74 inline void setVec(const LLVector4 &vec); // Sets LLVector4 to vec
75 inline void setVec(const LLVector3 &vec, F32 w = 1.f); // Sets LLVector4 to LLVector3 vec
76 inline void setVec(const F32 *vec); // Sets LLVector4 to vec
77
78 F32 magVec() const; // Returns magnitude of LLVector4
79 F32 magVecSquared() const; // Returns magnitude squared of LLVector4
80 F32 normVec(); // Normalizes and returns the magnitude of LLVector4
81
82 // Sets all values to absolute value of their original values
83 // Returns TRUE if data changed
84 BOOL abs();
85
86 BOOL isExactlyClear() const { return (mV[VW] == 1.0f) && !mV[VX] && !mV[VY] && !mV[VZ]; }
87 BOOL isExactlyZero() const { return !mV[VW] && !mV[VX] && !mV[VY] && !mV[VZ]; }
88
89 const LLVector4& rotVec(F32 angle, const LLVector4 &vec); // Rotates about vec by angle radians
90 const LLVector4& rotVec(F32 angle, F32 x, F32 y, F32 z); // Rotates about x,y,z by angle radians
91 const LLVector4& rotVec(const LLMatrix4 &mat); // Rotates by MAT4 mat
92 const LLVector4& rotVec(const LLQuaternion &q); // Rotates by QUAT q
93
94 const LLVector4& scaleVec(const LLVector4& vec); // Scales component-wise by vec
95
96 F32 operator[](int idx) const { return mV[idx]; }
97 F32 &operator[](int idx) { return mV[idx]; }
98
99 friend std::ostream& operator<<(std::ostream& s, const LLVector4 &a); // Print a
100 friend LLVector4 operator+(const LLVector4 &a, const LLVector4 &b); // Return vector a + b
101 friend LLVector4 operator-(const LLVector4 &a, const LLVector4 &b); // Return vector a minus b
102 friend F32 operator*(const LLVector4 &a, const LLVector4 &b); // Return a dot b
103 friend LLVector4 operator%(const LLVector4 &a, const LLVector4 &b); // Return a cross b
104 friend LLVector4 operator/(const LLVector4 &a, F32 k); // Return a divided by scaler k
105 friend LLVector4 operator*(const LLVector4 &a, F32 k); // Return a times scaler k
106 friend LLVector4 operator*(F32 k, const LLVector4 &a); // Return a times scaler k
107 friend bool operator==(const LLVector4 &a, const LLVector4 &b); // Return a == b
108 friend bool operator!=(const LLVector4 &a, const LLVector4 &b); // Return a != b
109
110 friend const LLVector4& operator+=(LLVector4 &a, const LLVector4 &b); // Return vector a + b
111 friend const LLVector4& operator-=(LLVector4 &a, const LLVector4 &b); // Return vector a minus b
112 friend const LLVector4& operator%=(LLVector4 &a, const LLVector4 &b); // Return a cross b
113 friend const LLVector4& operator*=(LLVector4 &a, F32 k); // Return a times scaler k
114 friend const LLVector4& operator/=(LLVector4 &a, F32 k); // Return a divided by scaler k
115
116 friend LLVector4 operator-(const LLVector4 &a); // Return vector -a
117}
118#if LL_DARWIN
119__attribute__ ((aligned (16)))
120#endif
121;
122
123
124// Non-member functions
125F32 angle_between(const LLVector4 &a, const LLVector4 &b); // Returns angle (radians) between a and b
126BOOL are_parallel(const LLVector4 &a, const LLVector4 &b, F32 epsilon=F_APPROXIMATELY_ZERO); // Returns TRUE if a and b are very close to parallel
127F32 dist_vec(const LLVector4 &a, const LLVector4 &b); // Returns distance between a and b
128F32 dist_vec_squared(const LLVector4 &a, const LLVector4 &b); // Returns distance squared between a and b
129LLVector3 vec4to3(const LLVector4 &vec);
130LLVector4 vec3to4(const LLVector3 &vec);
131LLVector4 lerp(const LLVector4 &a, const LLVector4 &b, F32 u); // Returns a vector that is a linear interpolation between a and b
132
133// Constructors
134
135inline LLVector4::LLVector4(void)
136{
137 mV[VX] = 0.f;
138 mV[VY] = 0.f;
139 mV[VZ] = 0.f;
140 mV[VW] = 1.f;
141}
142
143inline LLVector4::LLVector4(F32 x, F32 y, F32 z)
144{
145 mV[VX] = x;
146 mV[VY] = y;
147 mV[VZ] = z;
148 mV[VW] = 1.f;
149}
150
151inline LLVector4::LLVector4(F32 x, F32 y, F32 z, F32 w)
152{
153 mV[VX] = x;
154 mV[VY] = y;
155 mV[VZ] = z;
156 mV[VW] = w;
157}
158
159inline LLVector4::LLVector4(const F32 *vec)
160{
161 mV[VX] = vec[VX];
162 mV[VY] = vec[VY];
163 mV[VZ] = vec[VZ];
164 mV[VW] = vec[VW];
165}
166
167inline LLVector4::LLVector4(const LLVector3 &vec)
168{
169 mV[VX] = vec.mV[VX];
170 mV[VY] = vec.mV[VY];
171 mV[VZ] = vec.mV[VZ];
172 mV[VW] = 1.f;
173}
174
175inline LLVector4::LLVector4(const LLVector3 &vec, F32 w)
176{
177 mV[VX] = vec.mV[VX];
178 mV[VY] = vec.mV[VY];
179 mV[VZ] = vec.mV[VZ];
180 mV[VW] = w;
181}
182
183
184inline BOOL LLVector4::isFinite() const
185{
186 return (llfinite(mV[VX]) && llfinite(mV[VY]) && llfinite(mV[VZ]) && llfinite(mV[VW]));
187}
188
189// Clear and Assignment Functions
190
191inline void LLVector4::clearVec(void)
192{
193 mV[VX] = 0.f;
194 mV[VY] = 0.f;
195 mV[VZ] = 0.f;
196 mV[VW] = 1.f;
197}
198
199inline void LLVector4::zeroVec(void)
200{
201 mV[VX] = 0.f;
202 mV[VY] = 0.f;
203 mV[VZ] = 0.f;
204 mV[VW] = 0.f;
205}
206
207inline void LLVector4::setVec(F32 x, F32 y, F32 z)
208{
209 mV[VX] = x;
210 mV[VY] = y;
211 mV[VZ] = z;
212 mV[VW] = 1.f;
213}
214
215inline void LLVector4::setVec(F32 x, F32 y, F32 z, F32 w)
216{
217 mV[VX] = x;
218 mV[VY] = y;
219 mV[VZ] = z;
220 mV[VW] = w;
221}
222
223inline void LLVector4::setVec(const LLVector4 &vec)
224{
225 mV[VX] = vec.mV[VX];
226 mV[VY] = vec.mV[VY];
227 mV[VZ] = vec.mV[VZ];
228 mV[VW] = vec.mV[VW];
229}
230
231inline void LLVector4::setVec(const LLVector3 &vec, F32 w)
232{
233 mV[VX] = vec.mV[VX];
234 mV[VY] = vec.mV[VY];
235 mV[VZ] = vec.mV[VZ];
236 mV[VW] = w;
237}
238
239inline void LLVector4::setVec(const F32 *vec)
240{
241 mV[VX] = vec[VX];
242 mV[VY] = vec[VY];
243 mV[VZ] = vec[VZ];
244 mV[VW] = vec[VW];
245}
246
247// LLVector4 Magnitude and Normalization Functions
248
249inline F32 LLVector4::magVec(void) const
250{
251 return fsqrtf(mV[VX]*mV[VX] + mV[VY]*mV[VY] + mV[VZ]*mV[VZ]);
252}
253
254inline F32 LLVector4::magVecSquared(void) const
255{
256 return mV[VX]*mV[VX] + mV[VY]*mV[VY] + mV[VZ]*mV[VZ];
257}
258
259// LLVector4 Operators
260
261inline LLVector4 operator+(const LLVector4 &a, const LLVector4 &b)
262{
263 LLVector4 c(a);
264 return c += b;
265}
266
267inline LLVector4 operator-(const LLVector4 &a, const LLVector4 &b)
268{
269 LLVector4 c(a);
270 return c -= b;
271}
272
273inline F32 operator*(const LLVector4 &a, const LLVector4 &b)
274{
275 return (a.mV[VX]*b.mV[VX] + a.mV[VY]*b.mV[VY] + a.mV[VZ]*b.mV[VZ]);
276}
277
278inline LLVector4 operator%(const LLVector4 &a, const LLVector4 &b)
279{
280 return LLVector4(a.mV[VY]*b.mV[VZ] - b.mV[VY]*a.mV[VZ], a.mV[VZ]*b.mV[VX] - b.mV[VZ]*a.mV[VX], a.mV[VX]*b.mV[VY] - b.mV[VX]*a.mV[VY]);
281}
282
283inline LLVector4 operator/(const LLVector4 &a, F32 k)
284{
285 F32 t = 1.f / k;
286 return LLVector4( a.mV[VX] * t, a.mV[VY] * t, a.mV[VZ] * t );
287}
288
289
290inline LLVector4 operator*(const LLVector4 &a, F32 k)
291{
292 return LLVector4( a.mV[VX] * k, a.mV[VY] * k, a.mV[VZ] * k );
293}
294
295inline LLVector4 operator*(F32 k, const LLVector4 &a)
296{
297 return LLVector4( a.mV[VX] * k, a.mV[VY] * k, a.mV[VZ] * k );
298}
299
300inline bool operator==(const LLVector4 &a, const LLVector4 &b)
301{
302 return ( (a.mV[VX] == b.mV[VX])
303 &&(a.mV[VY] == b.mV[VY])
304 &&(a.mV[VZ] == b.mV[VZ]));
305}
306
307inline bool operator!=(const LLVector4 &a, const LLVector4 &b)
308{
309 return ( (a.mV[VX] != b.mV[VX])
310 ||(a.mV[VY] != b.mV[VY])
311 ||(a.mV[VZ] != b.mV[VZ]));
312}
313
314inline const LLVector4& operator+=(LLVector4 &a, const LLVector4 &b)
315{
316 a.mV[VX] += b.mV[VX];
317 a.mV[VY] += b.mV[VY];
318 a.mV[VZ] += b.mV[VZ];
319 return a;
320}
321
322inline const LLVector4& operator-=(LLVector4 &a, const LLVector4 &b)
323{
324 a.mV[VX] -= b.mV[VX];
325 a.mV[VY] -= b.mV[VY];
326 a.mV[VZ] -= b.mV[VZ];
327 return a;
328}
329
330inline const LLVector4& operator%=(LLVector4 &a, const LLVector4 &b)
331{
332 LLVector4 ret(a.mV[VY]*b.mV[VZ] - b.mV[VY]*a.mV[VZ], a.mV[VZ]*b.mV[VX] - b.mV[VZ]*a.mV[VX], a.mV[VX]*b.mV[VY] - b.mV[VX]*a.mV[VY]);
333 a = ret;
334 return a;
335}
336
337inline const LLVector4& operator*=(LLVector4 &a, F32 k)
338{
339 a.mV[VX] *= k;
340 a.mV[VY] *= k;
341 a.mV[VZ] *= k;
342 return a;
343}
344
345inline const LLVector4& operator/=(LLVector4 &a, F32 k)
346{
347 F32 t = 1.f / k;
348 a.mV[VX] *= t;
349 a.mV[VY] *= t;
350 a.mV[VZ] *= t;
351 return a;
352}
353
354inline LLVector4 operator-(const LLVector4 &a)
355{
356 return LLVector4( -a.mV[VX], -a.mV[VY], -a.mV[VZ] );
357}
358
359inline F32 dist_vec(const LLVector4 &a, const LLVector4 &b)
360{
361 LLVector4 vec = a - b;
362 return (vec.magVec());
363}
364
365inline F32 dist_vec_squared(const LLVector4 &a, const LLVector4 &b)
366{
367 LLVector4 vec = a - b;
368 return (vec.magVecSquared());
369}
370
371inline LLVector4 lerp(const LLVector4 &a, const LLVector4 &b, F32 u)
372{
373 return LLVector4(
374 a.mV[VX] + (b.mV[VX] - a.mV[VX]) * u,
375 a.mV[VY] + (b.mV[VY] - a.mV[VY]) * u,
376 a.mV[VZ] + (b.mV[VZ] - a.mV[VZ]) * u,
377 a.mV[VW] + (b.mV[VW] - a.mV[VW]) * u);
378}
379
380inline F32 LLVector4::normVec(void)
381{
382 F32 mag = fsqrtf(mV[VX]*mV[VX] + mV[VY]*mV[VY] + mV[VZ]*mV[VZ]);
383 F32 oomag;
384
385 if (mag > FP_MAG_THRESHOLD)
386 {
387 oomag = 1.f/mag;
388 mV[VX] *= oomag;
389 mV[VY] *= oomag;
390 mV[VZ] *= oomag;
391 }
392 else
393 {
394 mV[0] = 0.f;
395 mV[1] = 0.f;
396 mV[2] = 0.f;
397 mag = 0;
398 }
399 return (mag);
400}
401
402
403#endif
404