PageRenderTime 22ms CodeModel.GetById 13ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/llmath/v4math.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 142 lines | 69 code | 22 blank | 51 comment | 5 complexity | d39650cf663e46ccf0d587cda1de0d2a MD5 | raw file
  1/** 
  2 * @file v4math.cpp
  3 * @brief LLVector4 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 "v3math.h"
 31#include "v4math.h"
 32#include "m4math.h"
 33#include "m3math.h"
 34#include "llquaternion.h"
 35
 36// LLVector4
 37
 38// Axis-Angle rotations
 39
 40/*
 41const LLVector4&	LLVector4::rotVec(F32 angle, const LLVector4 &vec)
 42{
 43	if ( !vec.isExactlyZero() && angle )
 44	{
 45		*this = *this * LLMatrix4(angle, vec);
 46	}
 47	return *this;
 48}
 49
 50const LLVector4&	LLVector4::rotVec(F32 angle, F32 x, F32 y, F32 z)
 51{
 52	LLVector3 vec(x, y, z);
 53	if ( !vec.isExactlyZero() && angle )
 54	{
 55		*this = *this * LLMatrix4(angle, vec);
 56	}
 57	return *this;
 58}
 59*/
 60
 61const LLVector4&	LLVector4::rotVec(const LLMatrix4 &mat)
 62{
 63	*this = *this * mat;
 64	return *this;
 65}
 66
 67const LLVector4&	LLVector4::rotVec(const LLQuaternion &q)
 68{
 69	*this = *this * q;
 70	return *this;
 71}
 72
 73const LLVector4&	LLVector4::scaleVec(const LLVector4& vec)
 74{
 75	mV[VX] *= vec.mV[VX];
 76	mV[VY] *= vec.mV[VY];
 77	mV[VZ] *= vec.mV[VZ];
 78	mV[VW] *= vec.mV[VW];
 79
 80	return *this;
 81}
 82
 83// Sets all values to absolute value of their original values
 84// Returns TRUE if data changed
 85BOOL LLVector4::abs()
 86{
 87	BOOL ret = FALSE;
 88
 89	if (mV[0] < 0.f) { mV[0] = -mV[0]; ret = TRUE; }
 90	if (mV[1] < 0.f) { mV[1] = -mV[1]; ret = TRUE; }
 91	if (mV[2] < 0.f) { mV[2] = -mV[2]; ret = TRUE; }
 92	if (mV[3] < 0.f) { mV[3] = -mV[3]; ret = TRUE; }
 93
 94	return ret;
 95}
 96
 97
 98std::ostream& operator<<(std::ostream& s, const LLVector4 &a) 
 99{
100	s << "{ " << a.mV[VX] << ", " << a.mV[VY] << ", " << a.mV[VZ] << ", " << a.mV[VW] << " }";
101	return s;
102}
103
104
105// Non-member functions
106
107F32 angle_between( const LLVector4& a, const LLVector4& b )
108{
109	LLVector4 an = a;
110	LLVector4 bn = b;
111	an.normalize();
112	bn.normalize();
113	F32 cosine = an * bn;
114	F32 angle = (cosine >= 1.0f) ? 0.0f :
115		(cosine <= -1.0f) ? F_PI :
116		acos(cosine);
117	return angle;
118}
119
120BOOL are_parallel(const LLVector4 &a, const LLVector4 &b, F32 epsilon)
121{
122	LLVector4 an = a;
123	LLVector4 bn = b;
124	an.normalize();
125	bn.normalize();
126	F32 dot = an * bn;
127	if ( (1.0f - fabs(dot)) < epsilon)
128		return TRUE;
129	return FALSE;
130}
131
132
133LLVector3 vec4to3(const LLVector4 &vec)
134{
135	return LLVector3( vec.mV[VX], vec.mV[VY], vec.mV[VZ] );
136}
137
138LLVector4 vec3to4(const LLVector3 &vec)
139{
140	return LLVector4(vec.mV[VX], vec.mV[VY], vec.mV[VZ]);
141}
142