summaryrefslogtreecommitdiff
path: root/indra/llmath/v2math.h
blob: 863318551e4f2dba9aa13f94a5bfe73252485e8f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/** 
 * @file v2math.h
 * @brief LLVector2 class header file.
 *
 * Copyright (c) 2000-$CurrentYear$, Linden Research, Inc.
 * $License$
 */

#ifndef LL_V2MATH_H
#define LL_V2MATH_H

#include <math.h>

#include "llmath.h"

class LLVector4;
class LLMatrix3;
class LLQuaternion;

//  Llvector2 = |x y z w|

static const U32 LENGTHOFVECTOR2 = 2;

class LLVector2
{
	public:
		F32 mV[LENGTHOFVECTOR2];

		static LLVector2 zero;

		LLVector2();							// Initializes LLVector2 to (0, 0)
		LLVector2(F32 x, F32 y);			    // Initializes LLVector2 to (x. y)
		LLVector2(const F32 *vec);				// Initializes LLVector2 to (vec[0]. vec[1])
		
		// Clears LLVector2 to (0, 0).  DEPRECATED - prefer zeroVec.
		void	clearVec();

		// Zero LLVector2 to (0, 0)
		void	zeroVec();

		void	setVec(F32 x, F32 y);	        // Sets LLVector2 to (x, y)
		void	setVec(const LLVector2 &vec);	// Sets LLVector2 to vec
		void	setVec(const F32 *vec);			// Sets LLVector2 to vec

		F32		magVec() const;				// Returns magnitude of LLVector2
		F32		magVecSquared() const;		// Returns magnitude squared of LLVector2
		F32		normVec();					// Normalizes and returns the magnitude of LLVector2

		BOOL		abs();						// sets all values to absolute value of original value (first octant), returns TRUE if changed

		const LLVector2&	scaleVec(const LLVector2& vec);				// scales per component by vec

		BOOL isNull();			// Returns TRUE if vector has a _very_small_ length
		BOOL isExactlyZero() const		{ return !mV[VX] && !mV[VY]; }

		F32 operator[](int idx) const { return mV[idx]; }
		F32 &operator[](int idx) { return mV[idx]; }
	
		friend bool operator<(const LLVector2 &a, const LLVector2 &b);	// For sorting. x is "more significant" than y
		friend LLVector2 operator+(const LLVector2 &a, const LLVector2 &b);	// Return vector a + b
		friend LLVector2 operator-(const LLVector2 &a, const LLVector2 &b);	// Return vector a minus b
		friend F32 operator*(const LLVector2 &a, const LLVector2 &b);		// Return a dot b
		friend LLVector2 operator%(const LLVector2 &a, const LLVector2 &b);	// Return a cross b
		friend LLVector2 operator/(const LLVector2 &a, F32 k);				// Return a divided by scaler k
		friend LLVector2 operator*(const LLVector2 &a, F32 k);				// Return a times scaler k
		friend LLVector2 operator*(F32 k, const LLVector2 &a);				// Return a times scaler k
		friend bool operator==(const LLVector2 &a, const LLVector2 &b);		// Return a == b
		friend bool operator!=(const LLVector2 &a, const LLVector2 &b);		// Return a != b

		friend const LLVector2& operator+=(LLVector2 &a, const LLVector2 &b);	// Return vector a + b
		friend const LLVector2& operator-=(LLVector2 &a, const LLVector2 &b);	// Return vector a minus b
		friend const LLVector2& operator%=(LLVector2 &a, const LLVector2 &b);	// Return a cross b
		friend const LLVector2& operator*=(LLVector2 &a, F32 k);				// Return a times scaler k
		friend const LLVector2& operator/=(LLVector2 &a, F32 k);				// Return a divided by scaler k

		friend LLVector2 operator-(const LLVector2 &a);					// Return vector -a

		friend std::ostream&	 operator<<(std::ostream& s, const LLVector2 &a);		// Stream a
};


// Non-member functions 

F32	angle_between(const LLVector2 &a, const LLVector2 &b);	// Returns angle (radians) between a and b
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
F32	dist_vec(const LLVector2 &a, const LLVector2 &b);		// Returns distance between a and b
F32	dist_vec_squared(const LLVector2 &a, const LLVector2 &b);// Returns distance sqaured between a and b
F32	dist_vec_squared2D(const LLVector2 &a, const LLVector2 &b);// Returns distance sqaured between a and b ignoring Z component
LLVector2 lerp(const LLVector2 &a, const LLVector2 &b, F32 u); // Returns a vector that is a linear interpolation between a and b

// Constructors

inline LLVector2::LLVector2(void)
{
	mV[VX] = 0.f;
	mV[VY] = 0.f;
}

inline LLVector2::LLVector2(F32 x, F32 y)
{
	mV[VX] = x;
	mV[VY] = y;
}

inline LLVector2::LLVector2(const F32 *vec)
{
	mV[VX] = vec[VX];
	mV[VY] = vec[VY];
}


// Clear and Assignment Functions

inline void	LLVector2::clearVec(void)
{
	mV[VX] = 0.f;
	mV[VY] = 0.f;
}

inline void	LLVector2::zeroVec(void)
{
	mV[VX] = 0.f;
	mV[VY] = 0.f;
}

inline void	LLVector2::setVec(F32 x, F32 y)
{
	mV[VX] = x;
	mV[VY] = y;
}

inline void	LLVector2::setVec(const LLVector2 &vec)
{
	mV[VX] = vec.mV[VX];
	mV[VY] = vec.mV[VY];
}

inline void	LLVector2::setVec(const F32 *vec)
{
	mV[VX] = vec[VX];
	mV[VY] = vec[VY];
}

// LLVector2 Magnitude and Normalization Functions

inline F32		LLVector2::magVec(void) const
{
	return fsqrtf(mV[0]*mV[0] + mV[1]*mV[1]);
}

inline F32		LLVector2::magVecSquared(void) const
{
	return mV[0]*mV[0] + mV[1]*mV[1];
}

inline F32		LLVector2::normVec(void)
{
	F32 mag = fsqrtf(mV[0]*mV[0] + mV[1]*mV[1]);
	F32 oomag;

	if (mag > FP_MAG_THRESHOLD)
	{
		oomag = 1.f/mag;
		mV[0] *= oomag;
		mV[1] *= oomag;
	}
	else
	{
		mV[0] = 0.f;
		mV[1] = 0.f;
		mag = 0;
	}
	return (mag);
}

inline const LLVector2&	LLVector2::scaleVec(const LLVector2& vec)
{
	mV[VX] *= vec.mV[VX];
	mV[VY] *= vec.mV[VY];

	return *this;
}

inline BOOL	LLVector2::isNull()
{
	if ( F_APPROXIMATELY_ZERO > mV[VX]*mV[VX] + mV[VY]*mV[VY] )
	{
		return TRUE;
	}
	return FALSE;
}


// LLVector2 Operators

// For sorting. By convention, x is "more significant" than y.
inline bool operator<(const LLVector2 &a, const LLVector2 &b)	
{
	if( a.mV[VX] == b.mV[VX] )
	{
		return a.mV[VY] < b.mV[VY];
	}
	else
	{
		return a.mV[VX] < b.mV[VX];
	}
}


inline LLVector2 operator+(const LLVector2 &a, const LLVector2 &b)
{
	LLVector2 c(a);
	return c += b;
}

inline LLVector2 operator-(const LLVector2 &a, const LLVector2 &b)
{
	LLVector2 c(a);
	return c -= b;
}

inline F32  operator*(const LLVector2 &a, const LLVector2 &b)
{
	return (a.mV[0]*b.mV[0] + a.mV[1]*b.mV[1]);
}

inline LLVector2 operator%(const LLVector2 &a, const LLVector2 &b)
{
	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]);
}

inline LLVector2 operator/(const LLVector2 &a, F32 k)
{
	F32 t = 1.f / k;
	return LLVector2( a.mV[0] * t, a.mV[1] * t );
}

inline LLVector2 operator*(const LLVector2 &a, F32 k)
{
	return LLVector2( a.mV[0] * k, a.mV[1] * k );
}

inline LLVector2 operator*(F32 k, const LLVector2 &a)
{
	return LLVector2( a.mV[0] * k, a.mV[1] * k );
}

inline bool operator==(const LLVector2 &a, const LLVector2 &b)
{
	return (  (a.mV[0] == b.mV[0])
			&&(a.mV[1] == b.mV[1]));
}

inline bool operator!=(const LLVector2 &a, const LLVector2 &b)
{
	return (  (a.mV[0] != b.mV[0])
			||(a.mV[1] != b.mV[1]));
}

inline const LLVector2& operator+=(LLVector2 &a, const LLVector2 &b)
{
	a.mV[0] += b.mV[0];
	a.mV[1] += b.mV[1];
	return a;
}

inline const LLVector2& operator-=(LLVector2 &a, const LLVector2 &b)
{
	a.mV[0] -= b.mV[0];
	a.mV[1] -= b.mV[1];
	return a;
}

inline const LLVector2& operator%=(LLVector2 &a, const LLVector2 &b)
{
	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]);
	a = ret;
	return a;
}

inline const LLVector2& operator*=(LLVector2 &a, F32 k)
{
	a.mV[0] *= k;
	a.mV[1] *= k;
	return a;
}

inline const LLVector2& operator/=(LLVector2 &a, F32 k)
{
	F32 t = 1.f / k;
	a.mV[0] *= t;
	a.mV[1] *= t;
	return a;
}

inline LLVector2 operator-(const LLVector2 &a)
{
	return LLVector2( -a.mV[0], -a.mV[1] );
}

inline void update_min_max(LLVector2& min, LLVector2& max, const LLVector2& pos)
{
	for (U32 i = 0; i < 2; i++)
	{
		if (min.mV[i] > pos.mV[i])
		{
			min.mV[i] = pos.mV[i];
		}
		if (max.mV[i] < pos.mV[i])
		{
			max.mV[i] = pos.mV[i];
		}
	}
}

inline std::ostream& operator<<(std::ostream& s, const LLVector2 &a) 
{
	s << "{ " << a.mV[VX] << ", " << a.mV[VY] << " }";
	return s;
}

#endif