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

/indra/llmath/v3dmath.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 147 lines | 87 code | 24 blank | 36 comment | 18 complexity | 2aa9bf9ddd3a16d10a91392ffe7c7acc MD5 | raw file
  1/** 
  2 * @file v3dmath.cpp
  3 * @brief LLVector3d 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 <sstream>	// gcc 2.95.2 doesn't support sstream 
 30
 31#include "v3dmath.h"
 32
 33//#include "vmath.h"
 34#include "v4math.h"
 35#include "m4math.h"
 36#include "m3math.h"
 37#include "llquaternion.h"
 38#include "llquantize.h"
 39
 40// LLVector3d
 41// WARNING: Don't use these for global const definitions!
 42// For example: 
 43//		const LLQuaternion(0.5f * F_PI, LLVector3d::zero);
 44// at the top of a *.cpp file might not give you what you think.
 45const LLVector3d LLVector3d::zero(0,0,0);
 46const LLVector3d LLVector3d::x_axis(1, 0, 0);
 47const LLVector3d LLVector3d::y_axis(0, 1, 0);
 48const LLVector3d LLVector3d::z_axis(0, 0, 1);
 49const LLVector3d LLVector3d::x_axis_neg(-1, 0, 0);
 50const LLVector3d LLVector3d::y_axis_neg(0, -1, 0);
 51const LLVector3d LLVector3d::z_axis_neg(0, 0, -1);
 52
 53
 54// Clamps each values to range (min,max).
 55// Returns TRUE if data changed.
 56BOOL LLVector3d::clamp(F64 min, F64 max)
 57{
 58	BOOL ret = FALSE;
 59
 60	if (mdV[0] < min) { mdV[0] = min; ret = TRUE; }
 61	if (mdV[1] < min) { mdV[1] = min; ret = TRUE; }
 62	if (mdV[2] < min) { mdV[2] = min; ret = TRUE; }
 63
 64	if (mdV[0] > max) { mdV[0] = max; ret = TRUE; }
 65	if (mdV[1] > max) { mdV[1] = max; ret = TRUE; }
 66	if (mdV[2] > max) { mdV[2] = max; ret = TRUE; }
 67
 68	return ret;
 69}
 70
 71// Sets all values to absolute value of their original values
 72// Returns TRUE if data changed
 73BOOL LLVector3d::abs()
 74{
 75	BOOL ret = FALSE;
 76
 77	if (mdV[0] < 0.0) { mdV[0] = -mdV[0]; ret = TRUE; }
 78	if (mdV[1] < 0.0) { mdV[1] = -mdV[1]; ret = TRUE; }
 79	if (mdV[2] < 0.0) { mdV[2] = -mdV[2]; ret = TRUE; }
 80
 81	return ret;
 82}
 83
 84std::ostream& operator<<(std::ostream& s, const LLVector3d &a) 
 85{
 86	s << "{ " << a.mdV[VX] << ", " << a.mdV[VY] << ", " << a.mdV[VZ] << " }";
 87	return s;
 88}
 89
 90const LLVector3d& LLVector3d::operator=(const LLVector4 &a) 
 91{
 92	mdV[0] = a.mV[0];
 93	mdV[1] = a.mV[1];
 94	mdV[2] = a.mV[2];
 95	return *this;
 96}
 97
 98const LLVector3d&	LLVector3d::rotVec(const LLMatrix3 &mat)
 99{
100	*this = *this * mat;
101	return *this;
102}
103
104const LLVector3d&	LLVector3d::rotVec(const LLQuaternion &q)
105{
106	*this = *this * q;
107	return *this;
108}
109
110const LLVector3d&	LLVector3d::rotVec(F64 angle, const LLVector3d &vec)
111{
112	if ( !vec.isExactlyZero() && angle )
113	{
114		*this = *this * LLMatrix3((F32)angle, vec);
115	}
116	return *this;
117}
118
119const LLVector3d&	LLVector3d::rotVec(F64 angle, F64 x, F64 y, F64 z)
120{
121	LLVector3d vec(x, y, z);
122	if ( !vec.isExactlyZero() && angle )
123	{
124		*this = *this * LLMatrix3((F32)angle, vec);
125	}
126	return *this;
127}
128
129
130BOOL LLVector3d::parseVector3d(const std::string& buf, LLVector3d* value)
131{
132	if( buf.empty() || value == NULL)
133	{
134		return FALSE;
135	}
136
137	LLVector3d v;
138	S32 count = sscanf( buf.c_str(), "%lf %lf %lf", v.mdV + 0, v.mdV + 1, v.mdV + 2 );
139	if( 3 == count )
140	{
141		value->setVec( v );
142		return TRUE;
143	}
144
145	return FALSE;
146}
147