PageRenderTime 23ms CodeModel.GetById 14ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/llmath/v2math.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 126 lines | 76 code | 20 blank | 30 comment | 3 complexity | 89109fcf64fea7eee0b46a5511af5f8f MD5 | raw file
  1/** 
  2 * @file v2math.cpp
  3 * @brief LLVector2 class implementation.
  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#include "linden_common.h"
 28
 29//#include "vmath.h"
 30#include "v2math.h"
 31#include "v3math.h"
 32#include "v4math.h"
 33#include "m4math.h"
 34#include "m3math.h"
 35#include "llquaternion.h"
 36
 37// LLVector2
 38
 39LLVector2 LLVector2::zero(0,0);
 40
 41
 42// Non-member functions
 43
 44// Sets all values to absolute value of their original values
 45// Returns TRUE if data changed
 46BOOL LLVector2::abs()
 47{
 48	BOOL ret = FALSE;
 49
 50	if (mV[0] < 0.f) { mV[0] = -mV[0]; ret = TRUE; }
 51	if (mV[1] < 0.f) { mV[1] = -mV[1]; ret = TRUE; }
 52	
 53	return ret;
 54}
 55
 56
 57F32 angle_between(const LLVector2& a, const LLVector2& b)
 58{
 59	LLVector2 an = a;
 60	LLVector2 bn = b;
 61	an.normVec();
 62	bn.normVec();
 63	F32 cosine = an * bn;
 64	F32 angle = (cosine >= 1.0f) ? 0.0f :
 65				(cosine <= -1.0f) ? F_PI :
 66				acos(cosine);
 67	return angle;
 68}
 69
 70BOOL are_parallel(const LLVector2 &a, const LLVector2 &b, float epsilon)
 71{
 72	LLVector2 an = a;
 73	LLVector2 bn = b;
 74	an.normVec();
 75	bn.normVec();
 76	F32 dot = an * bn;
 77	if ( (1.0f - fabs(dot)) < epsilon)
 78	{
 79		return TRUE;
 80	}
 81	return FALSE;
 82}
 83
 84
 85F32	dist_vec(const LLVector2 &a, const LLVector2 &b)
 86{
 87	F32 x = a.mV[0] - b.mV[0];
 88	F32 y = a.mV[1] - b.mV[1];
 89	return (F32) sqrt( x*x + y*y );
 90}
 91
 92F32	dist_vec_squared(const LLVector2 &a, const LLVector2 &b)
 93{
 94	F32 x = a.mV[0] - b.mV[0];
 95	F32 y = a.mV[1] - b.mV[1];
 96	return x*x + y*y;
 97}
 98
 99F32	dist_vec_squared2D(const LLVector2 &a, const LLVector2 &b)
100{
101	F32 x = a.mV[0] - b.mV[0];
102	F32 y = a.mV[1] - b.mV[1];
103	return x*x + y*y;
104}
105
106LLVector2 lerp(const LLVector2 &a, const LLVector2 &b, F32 u)
107{
108	return LLVector2(
109		a.mV[VX] + (b.mV[VX] - a.mV[VX]) * u,
110		a.mV[VY] + (b.mV[VY] - a.mV[VY]) * u );
111}
112
113LLSD LLVector2::getValue() const
114{
115	LLSD ret;
116	ret[0] = mV[0];
117	ret[1] = mV[1];
118	return ret;
119}
120
121void LLVector2::setValue(LLSD& sd)
122{
123	mV[0] = (F32) sd[0].asReal();
124	mV[1] = (F32) sd[1].asReal();
125}
126