PageRenderTime 37ms CodeModel.GetById 11ms app.highlight 22ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/llmath/v2math.h

https://bitbucket.org/lindenlab/viewer-beta/
C++ Header | 435 lines | 313 code | 80 blank | 42 comment | 14 complexity | c87aeae4c8150f31619f0dfa5cbca6a9 MD5 | raw file
  1/** 
  2 * @file v2math.h
  3 * @brief LLVector2 class header file.
  4 *
  5 * $LicenseInfo:firstyear=2000&license=viewerlgpl$
  6 * Second Life Viewer Source Code
  7 * Copyright (C) 2010, Linden Research, Inc.
  8 * 
  9 * This library is free software; you can redistribute it and/or
 10 * modify it under the terms of the GNU Lesser General Public
 11 * License as published by the Free Software Foundation;
 12 * version 2.1 of the License only.
 13 * 
 14 * This library is distributed in the hope that it will be useful,
 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 17 * Lesser General Public License for more details.
 18 * 
 19 * You should have received a copy of the GNU Lesser General Public
 20 * License along with this library; if not, write to the Free Software
 21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 22 * 
 23 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 24 * $/LicenseInfo$
 25 */
 26
 27#ifndef LL_V2MATH_H
 28#define LL_V2MATH_H
 29
 30#include "llmath.h"
 31#include "v3math.h"
 32
 33class LLVector4;
 34class LLMatrix3;
 35class LLQuaternion;
 36
 37//  Llvector2 = |x y z w|
 38
 39static const U32 LENGTHOFVECTOR2 = 2;
 40
 41class LLVector2
 42{
 43	public:
 44		F32 mV[LENGTHOFVECTOR2];
 45
 46		static LLVector2 zero;
 47
 48		LLVector2();							  // Initializes LLVector2 to (0, 0)
 49		LLVector2(F32 x, F32 y);			      // Initializes LLVector2 to (x. y)
 50		LLVector2(const F32 *vec);				  // Initializes LLVector2 to (vec[0]. vec[1])
 51        explicit LLVector2(const LLVector3 &vec); // Initializes LLVector2 to (vec[0]. vec[1])
 52		
 53		// Clears LLVector2 to (0, 0).  DEPRECATED - prefer zeroVec.
 54		void	clear();
 55		void	setZero();
 56		void	clearVec();	// deprecated
 57		void	zeroVec();	// deprecated
 58
 59		void	set(F32 x, F32 y);	        // Sets LLVector2 to (x, y)
 60		void	set(const LLVector2 &vec);	// Sets LLVector2 to vec
 61		void	set(const F32 *vec);			// Sets LLVector2 to vec
 62
 63		LLSD	getValue() const;
 64		void	setValue(LLSD& sd);
 65
 66		void	setVec(F32 x, F32 y);	        // deprecated
 67		void	setVec(const LLVector2 &vec);	// deprecated
 68		void	setVec(const F32 *vec);			// deprecated
 69
 70		inline bool isFinite() const; // checks to see if all values of LLVector2 are finite
 71
 72		F32		length() const;				// Returns magnitude of LLVector2
 73		F32		lengthSquared() const;		// Returns magnitude squared of LLVector2
 74		F32		normalize();					// Normalizes and returns the magnitude of LLVector2
 75
 76		F32		magVec() const;				// deprecated
 77		F32		magVecSquared() const;		// deprecated
 78		F32		normVec();					// deprecated
 79
 80		BOOL		abs();						// sets all values to absolute value of original value (first octant), returns TRUE if changed
 81
 82		const LLVector2&	scaleVec(const LLVector2& vec);				// scales per component by vec
 83
 84		BOOL isNull();			// Returns TRUE if vector has a _very_small_ length
 85		BOOL isExactlyZero() const		{ return !mV[VX] && !mV[VY]; }
 86
 87		F32 operator[](int idx) const { return mV[idx]; }
 88		F32 &operator[](int idx) { return mV[idx]; }
 89	
 90		friend bool operator<(const LLVector2 &a, const LLVector2 &b);	// For sorting. x is "more significant" than y
 91		friend LLVector2 operator+(const LLVector2 &a, const LLVector2 &b);	// Return vector a + b
 92		friend LLVector2 operator-(const LLVector2 &a, const LLVector2 &b);	// Return vector a minus b
 93		friend F32 operator*(const LLVector2 &a, const LLVector2 &b);		// Return a dot b
 94		friend LLVector2 operator%(const LLVector2 &a, const LLVector2 &b);	// Return a cross b
 95		friend LLVector2 operator/(const LLVector2 &a, F32 k);				// Return a divided by scaler k
 96		friend LLVector2 operator*(const LLVector2 &a, F32 k);				// Return a times scaler k
 97		friend LLVector2 operator*(F32 k, const LLVector2 &a);				// Return a times scaler k
 98		friend bool operator==(const LLVector2 &a, const LLVector2 &b);		// Return a == b
 99		friend bool operator!=(const LLVector2 &a, const LLVector2 &b);		// Return a != b
100
101		friend const LLVector2& operator+=(LLVector2 &a, const LLVector2 &b);	// Return vector a + b
102		friend const LLVector2& operator-=(LLVector2 &a, const LLVector2 &b);	// Return vector a minus b
103		friend const LLVector2& operator%=(LLVector2 &a, const LLVector2 &b);	// Return a cross b
104		friend const LLVector2& operator*=(LLVector2 &a, F32 k);				// Return a times scaler k
105		friend const LLVector2& operator/=(LLVector2 &a, F32 k);				// Return a divided by scaler k
106
107		friend LLVector2 operator-(const LLVector2 &a);					// Return vector -a
108
109		friend std::ostream&	 operator<<(std::ostream& s, const LLVector2 &a);		// Stream a
110};
111
112
113// Non-member functions 
114
115F32	angle_between(const LLVector2 &a, const LLVector2 &b);	// Returns angle (radians) between a and b
116BOOL are_parallel(const LLVector2 &a, const LLVector2 &b, F32 epsilon=F_APPROXIMATELY_ZERO);	// Returns TRUE if a and b are very close to parallel
117F32	dist_vec(const LLVector2 &a, const LLVector2 &b);		// Returns distance between a and b
118F32	dist_vec_squared(const LLVector2 &a, const LLVector2 &b);// Returns distance squared between a and b
119F32	dist_vec_squared2D(const LLVector2 &a, const LLVector2 &b);// Returns distance squared between a and b ignoring Z component
120LLVector2 lerp(const LLVector2 &a, const LLVector2 &b, F32 u); // Returns a vector that is a linear interpolation between a and b
121
122// Constructors
123
124inline LLVector2::LLVector2(void)
125{
126	mV[VX] = 0.f;
127	mV[VY] = 0.f;
128}
129
130inline LLVector2::LLVector2(F32 x, F32 y)
131{
132	mV[VX] = x;
133	mV[VY] = y;
134}
135
136inline LLVector2::LLVector2(const F32 *vec)
137{
138	mV[VX] = vec[VX];
139	mV[VY] = vec[VY];
140}
141
142inline LLVector2::LLVector2(const LLVector3 &vec)
143{
144	mV[VX] = vec.mV[VX];
145	mV[VY] = vec.mV[VY];
146}
147
148
149// Clear and Assignment Functions
150
151inline void	LLVector2::clear(void)
152{
153	mV[VX] = 0.f;
154	mV[VY] = 0.f;
155}
156
157inline void	LLVector2::setZero(void)
158{
159	mV[VX] = 0.f;
160	mV[VY] = 0.f;
161}
162
163// deprecated
164inline void	LLVector2::clearVec(void)
165{
166	mV[VX] = 0.f;
167	mV[VY] = 0.f;
168}
169
170// deprecated
171inline void	LLVector2::zeroVec(void)
172{
173	mV[VX] = 0.f;
174	mV[VY] = 0.f;
175}
176
177inline void	LLVector2::set(F32 x, F32 y)
178{
179	mV[VX] = x;
180	mV[VY] = y;
181}
182
183inline void	LLVector2::set(const LLVector2 &vec)
184{
185	mV[VX] = vec.mV[VX];
186	mV[VY] = vec.mV[VY];
187}
188
189inline void	LLVector2::set(const F32 *vec)
190{
191	mV[VX] = vec[VX];
192	mV[VY] = vec[VY];
193}
194
195
196// deprecated
197inline void	LLVector2::setVec(F32 x, F32 y)
198{
199	mV[VX] = x;
200	mV[VY] = y;
201}
202
203// deprecated
204inline void	LLVector2::setVec(const LLVector2 &vec)
205{
206	mV[VX] = vec.mV[VX];
207	mV[VY] = vec.mV[VY];
208}
209
210// deprecated
211inline void	LLVector2::setVec(const F32 *vec)
212{
213	mV[VX] = vec[VX];
214	mV[VY] = vec[VY];
215}
216
217
218// LLVector2 Magnitude and Normalization Functions
219
220inline F32 LLVector2::length(void) const
221{
222	return (F32) sqrt(mV[0]*mV[0] + mV[1]*mV[1]);
223}
224
225inline F32 LLVector2::lengthSquared(void) const
226{
227	return mV[0]*mV[0] + mV[1]*mV[1];
228}
229
230inline F32		LLVector2::normalize(void)
231{
232	F32 mag = (F32) sqrt(mV[0]*mV[0] + mV[1]*mV[1]);
233	F32 oomag;
234
235	if (mag > FP_MAG_THRESHOLD)
236	{
237		oomag = 1.f/mag;
238		mV[0] *= oomag;
239		mV[1] *= oomag;
240	}
241	else
242	{
243		mV[0] = 0.f;
244		mV[1] = 0.f;
245		mag = 0;
246	}
247	return (mag);
248}
249
250// checker
251inline bool LLVector2::isFinite() const
252{
253	return (llfinite(mV[VX]) && llfinite(mV[VY]));
254}
255
256// deprecated
257inline F32		LLVector2::magVec(void) const
258{
259	return (F32) sqrt(mV[0]*mV[0] + mV[1]*mV[1]);
260}
261
262// deprecated
263inline F32		LLVector2::magVecSquared(void) const
264{
265	return mV[0]*mV[0] + mV[1]*mV[1];
266}
267
268// deprecated
269inline F32		LLVector2::normVec(void)
270{
271	F32 mag = (F32) sqrt(mV[0]*mV[0] + mV[1]*mV[1]);
272	F32 oomag;
273
274	if (mag > FP_MAG_THRESHOLD)
275	{
276		oomag = 1.f/mag;
277		mV[0] *= oomag;
278		mV[1] *= oomag;
279	}
280	else
281	{
282		mV[0] = 0.f;
283		mV[1] = 0.f;
284		mag = 0;
285	}
286	return (mag);
287}
288
289inline const LLVector2&	LLVector2::scaleVec(const LLVector2& vec)
290{
291	mV[VX] *= vec.mV[VX];
292	mV[VY] *= vec.mV[VY];
293
294	return *this;
295}
296
297inline BOOL	LLVector2::isNull()
298{
299	if ( F_APPROXIMATELY_ZERO > mV[VX]*mV[VX] + mV[VY]*mV[VY] )
300	{
301		return TRUE;
302	}
303	return FALSE;
304}
305
306
307// LLVector2 Operators
308
309// For sorting. By convention, x is "more significant" than y.
310inline bool operator<(const LLVector2 &a, const LLVector2 &b)	
311{
312	if( a.mV[VX] == b.mV[VX] )
313	{
314		return a.mV[VY] < b.mV[VY];
315	}
316	else
317	{
318		return a.mV[VX] < b.mV[VX];
319	}
320}
321
322
323inline LLVector2 operator+(const LLVector2 &a, const LLVector2 &b)
324{
325	LLVector2 c(a);
326	return c += b;
327}
328
329inline LLVector2 operator-(const LLVector2 &a, const LLVector2 &b)
330{
331	LLVector2 c(a);
332	return c -= b;
333}
334
335inline F32  operator*(const LLVector2 &a, const LLVector2 &b)
336{
337	return (a.mV[0]*b.mV[0] + a.mV[1]*b.mV[1]);
338}
339
340inline LLVector2 operator%(const LLVector2 &a, const LLVector2 &b)
341{
342	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]);
343}
344
345inline LLVector2 operator/(const LLVector2 &a, F32 k)
346{
347	F32 t = 1.f / k;
348	return LLVector2( a.mV[0] * t, a.mV[1] * t );
349}
350
351inline LLVector2 operator*(const LLVector2 &a, F32 k)
352{
353	return LLVector2( a.mV[0] * k, a.mV[1] * k );
354}
355
356inline LLVector2 operator*(F32 k, const LLVector2 &a)
357{
358	return LLVector2( a.mV[0] * k, a.mV[1] * k );
359}
360
361inline bool operator==(const LLVector2 &a, const LLVector2 &b)
362{
363	return (  (a.mV[0] == b.mV[0])
364			&&(a.mV[1] == b.mV[1]));
365}
366
367inline bool operator!=(const LLVector2 &a, const LLVector2 &b)
368{
369	return (  (a.mV[0] != b.mV[0])
370			||(a.mV[1] != b.mV[1]));
371}
372
373inline const LLVector2& operator+=(LLVector2 &a, const LLVector2 &b)
374{
375	a.mV[0] += b.mV[0];
376	a.mV[1] += b.mV[1];
377	return a;
378}
379
380inline const LLVector2& operator-=(LLVector2 &a, const LLVector2 &b)
381{
382	a.mV[0] -= b.mV[0];
383	a.mV[1] -= b.mV[1];
384	return a;
385}
386
387inline const LLVector2& operator%=(LLVector2 &a, const LLVector2 &b)
388{
389	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]);
390	a = ret;
391	return a;
392}
393
394inline const LLVector2& operator*=(LLVector2 &a, F32 k)
395{
396	a.mV[0] *= k;
397	a.mV[1] *= k;
398	return a;
399}
400
401inline const LLVector2& operator/=(LLVector2 &a, F32 k)
402{
403	F32 t = 1.f / k;
404	a.mV[0] *= t;
405	a.mV[1] *= t;
406	return a;
407}
408
409inline LLVector2 operator-(const LLVector2 &a)
410{
411	return LLVector2( -a.mV[0], -a.mV[1] );
412}
413
414inline void update_min_max(LLVector2& min, LLVector2& max, const LLVector2& pos)
415{
416	for (U32 i = 0; i < 2; i++)
417	{
418		if (min.mV[i] > pos.mV[i])
419		{
420			min.mV[i] = pos.mV[i];
421		}
422		if (max.mV[i] < pos.mV[i])
423		{
424			max.mV[i] = pos.mV[i];
425		}
426	}
427}
428
429inline std::ostream& operator<<(std::ostream& s, const LLVector2 &a) 
430{
431	s << "{ " << a.mV[VX] << ", " << a.mV[VY] << " }";
432	return s;
433}
434
435#endif