diff options
author | Jacek Antonelli | 2008-08-15 23:44:46 -0500 |
---|---|---|
committer | Jacek Antonelli | 2008-08-15 23:44:46 -0500 |
commit | 38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4 (patch) | |
tree | adca584755d22ca041a2dbfc35d4eca01f70b32c /linden/indra/llmath/v2math.h | |
parent | README.txt (diff) | |
download | meta-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/v2math.h')
-rw-r--r-- | linden/indra/llmath/v2math.h | 326 |
1 files changed, 326 insertions, 0 deletions
diff --git a/linden/indra/llmath/v2math.h b/linden/indra/llmath/v2math.h new file mode 100644 index 0000000..1832403 --- /dev/null +++ b/linden/indra/llmath/v2math.h | |||
@@ -0,0 +1,326 @@ | |||
1 | /** | ||
2 | * @file v2math.h | ||
3 | * @brief LLVector2 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_V2MATH_H | ||
29 | #define LL_V2MATH_H | ||
30 | |||
31 | #include <math.h> | ||
32 | |||
33 | #include "llmath.h" | ||
34 | |||
35 | class LLVector4; | ||
36 | class LLMatrix3; | ||
37 | class LLQuaternion; | ||
38 | |||
39 | // Llvector2 = |x y z w| | ||
40 | |||
41 | static const U32 LENGTHOFVECTOR2 = 2; | ||
42 | |||
43 | class LLVector2 | ||
44 | { | ||
45 | public: | ||
46 | F32 mV[LENGTHOFVECTOR2]; | ||
47 | |||
48 | static LLVector2 zero; | ||
49 | |||
50 | LLVector2(); // Initializes LLVector2 to (0, 0) | ||
51 | LLVector2(F32 x, F32 y); // Initializes LLVector2 to (x. y) | ||
52 | LLVector2(const F32 *vec); // Initializes LLVector2 to (vec[0]. vec[1]) | ||
53 | |||
54 | // Clears LLVector2 to (0, 0). DEPRECATED - prefer zeroVec. | ||
55 | void clearVec(); | ||
56 | |||
57 | // Zero LLVector2 to (0, 0) | ||
58 | void zeroVec(); | ||
59 | |||
60 | void setVec(F32 x, F32 y); // Sets LLVector2 to (x, y) | ||
61 | void setVec(const LLVector2 &vec); // Sets LLVector2 to vec | ||
62 | void setVec(const F32 *vec); // Sets LLVector2 to vec | ||
63 | |||
64 | F32 magVec() const; // Returns magnitude of LLVector2 | ||
65 | F32 magVecSquared() const; // Returns magnitude squared of LLVector2 | ||
66 | F32 normVec(); // Normalizes and returns the magnitude of LLVector2 | ||
67 | |||
68 | BOOL abs(); // sets all values to absolute value of original value (first octant), returns TRUE if changed | ||
69 | |||
70 | const LLVector2& scaleVec(const LLVector2& vec); // scales per component by vec | ||
71 | |||
72 | BOOL isNull(); // Returns TRUE if vector has a _very_small_ length | ||
73 | BOOL isExactlyZero() const { return !mV[VX] && !mV[VY]; } | ||
74 | |||
75 | F32 operator[](int idx) const { return mV[idx]; } | ||
76 | F32 &operator[](int idx) { return mV[idx]; } | ||
77 | |||
78 | friend bool operator<(const LLVector2 &a, const LLVector2 &b); // For sorting. x is "more significant" than y | ||
79 | friend LLVector2 operator+(const LLVector2 &a, const LLVector2 &b); // Return vector a + b | ||
80 | friend LLVector2 operator-(const LLVector2 &a, const LLVector2 &b); // Return vector a minus b | ||
81 | friend F32 operator*(const LLVector2 &a, const LLVector2 &b); // Return a dot b | ||
82 | friend LLVector2 operator%(const LLVector2 &a, const LLVector2 &b); // Return a cross b | ||
83 | friend LLVector2 operator/(const LLVector2 &a, F32 k); // Return a divided by scaler k | ||
84 | friend LLVector2 operator*(const LLVector2 &a, F32 k); // Return a times scaler k | ||
85 | friend LLVector2 operator*(F32 k, const LLVector2 &a); // Return a times scaler k | ||
86 | friend bool operator==(const LLVector2 &a, const LLVector2 &b); // Return a == b | ||
87 | friend bool operator!=(const LLVector2 &a, const LLVector2 &b); // Return a != b | ||
88 | |||
89 | friend const LLVector2& operator+=(LLVector2 &a, const LLVector2 &b); // Return vector a + b | ||
90 | friend const LLVector2& operator-=(LLVector2 &a, const LLVector2 &b); // Return vector a minus b | ||
91 | friend const LLVector2& operator%=(LLVector2 &a, const LLVector2 &b); // Return a cross b | ||
92 | friend const LLVector2& operator*=(LLVector2 &a, F32 k); // Return a times scaler k | ||
93 | friend const LLVector2& operator/=(LLVector2 &a, F32 k); // Return a divided by scaler k | ||
94 | |||
95 | friend LLVector2 operator-(const LLVector2 &a); // Return vector -a | ||
96 | |||
97 | friend std::ostream& operator<<(std::ostream& s, const LLVector2 &a); // Stream a | ||
98 | }; | ||
99 | |||
100 | |||
101 | // Non-member functions | ||
102 | |||
103 | F32 angle_between(const LLVector2 &a, const LLVector2 &b); // Returns angle (radians) between a and b | ||
104 | BOOL are_parallel(const LLVector2 &a, const LLVector2 &b, F32 epsilon=F_APPROXIMATELY_ZERO); // Returns TRUE if a and b are very close to parallel | ||
105 | F32 dist_vec(const LLVector2 &a, const LLVector2 &b); // Returns distance between a and b | ||
106 | F32 dist_vec_squared(const LLVector2 &a, const LLVector2 &b);// Returns distance sqaured between a and b | ||
107 | F32 dist_vec_squared2D(const LLVector2 &a, const LLVector2 &b);// Returns distance sqaured between a and b ignoring Z component | ||
108 | LLVector2 lerp(const LLVector2 &a, const LLVector2 &b, F32 u); // Returns a vector that is a linear interpolation between a and b | ||
109 | |||
110 | // Constructors | ||
111 | |||
112 | inline LLVector2::LLVector2(void) | ||
113 | { | ||
114 | mV[VX] = 0.f; | ||
115 | mV[VY] = 0.f; | ||
116 | } | ||
117 | |||
118 | inline LLVector2::LLVector2(F32 x, F32 y) | ||
119 | { | ||
120 | mV[VX] = x; | ||
121 | mV[VY] = y; | ||
122 | } | ||
123 | |||
124 | inline LLVector2::LLVector2(const F32 *vec) | ||
125 | { | ||
126 | mV[VX] = vec[VX]; | ||
127 | mV[VY] = vec[VY]; | ||
128 | } | ||
129 | |||
130 | |||
131 | // Clear and Assignment Functions | ||
132 | |||
133 | inline void LLVector2::clearVec(void) | ||
134 | { | ||
135 | mV[VX] = 0.f; | ||
136 | mV[VY] = 0.f; | ||
137 | } | ||
138 | |||
139 | inline void LLVector2::zeroVec(void) | ||
140 | { | ||
141 | mV[VX] = 0.f; | ||
142 | mV[VY] = 0.f; | ||
143 | } | ||
144 | |||
145 | inline void LLVector2::setVec(F32 x, F32 y) | ||
146 | { | ||
147 | mV[VX] = x; | ||
148 | mV[VY] = y; | ||
149 | } | ||
150 | |||
151 | inline void LLVector2::setVec(const LLVector2 &vec) | ||
152 | { | ||
153 | mV[VX] = vec.mV[VX]; | ||
154 | mV[VY] = vec.mV[VY]; | ||
155 | } | ||
156 | |||
157 | inline void LLVector2::setVec(const F32 *vec) | ||
158 | { | ||
159 | mV[VX] = vec[VX]; | ||
160 | mV[VY] = vec[VY]; | ||
161 | } | ||
162 | |||
163 | // LLVector2 Magnitude and Normalization Functions | ||
164 | |||
165 | inline F32 LLVector2::magVec(void) const | ||
166 | { | ||
167 | return fsqrtf(mV[0]*mV[0] + mV[1]*mV[1]); | ||
168 | } | ||
169 | |||
170 | inline F32 LLVector2::magVecSquared(void) const | ||
171 | { | ||
172 | return mV[0]*mV[0] + mV[1]*mV[1]; | ||
173 | } | ||
174 | |||
175 | inline F32 LLVector2::normVec(void) | ||
176 | { | ||
177 | F32 mag = fsqrtf(mV[0]*mV[0] + mV[1]*mV[1]); | ||
178 | F32 oomag; | ||
179 | |||
180 | if (mag > FP_MAG_THRESHOLD) | ||
181 | { | ||
182 | oomag = 1.f/mag; | ||
183 | mV[0] *= oomag; | ||
184 | mV[1] *= oomag; | ||
185 | } | ||
186 | else | ||
187 | { | ||
188 | mV[0] = 0.f; | ||
189 | mV[1] = 0.f; | ||
190 | mag = 0; | ||
191 | } | ||
192 | return (mag); | ||
193 | } | ||
194 | |||
195 | inline const LLVector2& LLVector2::scaleVec(const LLVector2& vec) | ||
196 | { | ||
197 | mV[VX] *= vec.mV[VX]; | ||
198 | mV[VY] *= vec.mV[VY]; | ||
199 | |||
200 | return *this; | ||
201 | } | ||
202 | |||
203 | inline BOOL LLVector2::isNull() | ||
204 | { | ||
205 | if ( F_APPROXIMATELY_ZERO > mV[VX]*mV[VX] + mV[VY]*mV[VY] ) | ||
206 | { | ||
207 | return TRUE; | ||
208 | } | ||
209 | return FALSE; | ||
210 | } | ||
211 | |||
212 | |||
213 | // LLVector2 Operators | ||
214 | |||
215 | // For sorting. By convention, x is "more significant" than y. | ||
216 | inline bool operator<(const LLVector2 &a, const LLVector2 &b) | ||
217 | { | ||
218 | if( a.mV[VX] == b.mV[VX] ) | ||
219 | { | ||
220 | return a.mV[VY] < b.mV[VY]; | ||
221 | } | ||
222 | else | ||
223 | { | ||
224 | return a.mV[VX] < b.mV[VX]; | ||
225 | } | ||
226 | } | ||
227 | |||
228 | |||
229 | inline LLVector2 operator+(const LLVector2 &a, const LLVector2 &b) | ||
230 | { | ||
231 | LLVector2 c(a); | ||
232 | return c += b; | ||
233 | } | ||
234 | |||
235 | inline LLVector2 operator-(const LLVector2 &a, const LLVector2 &b) | ||
236 | { | ||
237 | LLVector2 c(a); | ||
238 | return c -= b; | ||
239 | } | ||
240 | |||
241 | inline F32 operator*(const LLVector2 &a, const LLVector2 &b) | ||
242 | { | ||
243 | return (a.mV[0]*b.mV[0] + a.mV[1]*b.mV[1]); | ||
244 | } | ||
245 | |||
246 | inline LLVector2 operator%(const LLVector2 &a, const LLVector2 &b) | ||
247 | { | ||
248 | return LLVector2(a.mV[0]*b.mV[1] - b.mV[0]*a.mV[1], a.mV[1]*b.mV[0] - b.mV[1]*a.mV[0]); | ||
249 | } | ||
250 | |||
251 | inline LLVector2 operator/(const LLVector2 &a, F32 k) | ||
252 | { | ||
253 | F32 t = 1.f / k; | ||
254 | return LLVector2( a.mV[0] * t, a.mV[1] * t ); | ||
255 | } | ||
256 | |||
257 | inline LLVector2 operator*(const LLVector2 &a, F32 k) | ||
258 | { | ||
259 | return LLVector2( a.mV[0] * k, a.mV[1] * k ); | ||
260 | } | ||
261 | |||
262 | inline LLVector2 operator*(F32 k, const LLVector2 &a) | ||
263 | { | ||
264 | return LLVector2( a.mV[0] * k, a.mV[1] * k ); | ||
265 | } | ||
266 | |||
267 | inline bool operator==(const LLVector2 &a, const LLVector2 &b) | ||
268 | { | ||
269 | return ( (a.mV[0] == b.mV[0]) | ||
270 | &&(a.mV[1] == b.mV[1])); | ||
271 | } | ||
272 | |||
273 | inline bool operator!=(const LLVector2 &a, const LLVector2 &b) | ||
274 | { | ||
275 | return ( (a.mV[0] != b.mV[0]) | ||
276 | ||(a.mV[1] != b.mV[1])); | ||
277 | } | ||
278 | |||
279 | inline const LLVector2& operator+=(LLVector2 &a, const LLVector2 &b) | ||
280 | { | ||
281 | a.mV[0] += b.mV[0]; | ||
282 | a.mV[1] += b.mV[1]; | ||
283 | return a; | ||
284 | } | ||
285 | |||
286 | inline const LLVector2& operator-=(LLVector2 &a, const LLVector2 &b) | ||
287 | { | ||
288 | a.mV[0] -= b.mV[0]; | ||
289 | a.mV[1] -= b.mV[1]; | ||
290 | return a; | ||
291 | } | ||
292 | |||
293 | inline const LLVector2& operator%=(LLVector2 &a, const LLVector2 &b) | ||
294 | { | ||
295 | LLVector2 ret(a.mV[0]*b.mV[1] - b.mV[0]*a.mV[1], a.mV[1]*b.mV[0] - b.mV[1]*a.mV[0]); | ||
296 | a = ret; | ||
297 | return a; | ||
298 | } | ||
299 | |||
300 | inline const LLVector2& operator*=(LLVector2 &a, F32 k) | ||
301 | { | ||
302 | a.mV[0] *= k; | ||
303 | a.mV[1] *= k; | ||
304 | return a; | ||
305 | } | ||
306 | |||
307 | inline const LLVector2& operator/=(LLVector2 &a, F32 k) | ||
308 | { | ||
309 | F32 t = 1.f / k; | ||
310 | a.mV[0] *= t; | ||
311 | a.mV[1] *= t; | ||
312 | return a; | ||
313 | } | ||
314 | |||
315 | inline LLVector2 operator-(const LLVector2 &a) | ||
316 | { | ||
317 | return LLVector2( -a.mV[0], -a.mV[1] ); | ||
318 | } | ||
319 | |||
320 | inline std::ostream& operator<<(std::ostream& s, const LLVector2 &a) | ||
321 | { | ||
322 | s << "{ " << a.mV[VX] << ", " << a.mV[VY] << " }"; | ||
323 | return s; | ||
324 | } | ||
325 | |||
326 | #endif | ||