diff options
Diffstat (limited to 'indra/llmath/v3math.h')
-rw-r--r-- | indra/llmath/v3math.h | 181 |
1 files changed, 138 insertions, 43 deletions
diff --git a/indra/llmath/v3math.h b/indra/llmath/v3math.h index e18b20ddd0..dbd38c1c3f 100644 --- a/indra/llmath/v3math.h +++ b/indra/llmath/v3math.h @@ -2,30 +2,25 @@ * @file v3math.h * @brief LLVector3 class header file. * - * $LicenseInfo:firstyear=2000&license=viewergpl$ - * - * Copyright (c) 2000-2007, Linden Research, Inc. - * + * $LicenseInfo:firstyear=2000&license=viewerlgpl$ * Second Life Viewer Source Code - * The source code in this file ("Source Code") is provided by Linden Lab - * to you under the terms of the GNU General Public License, version 2.0 - * ("GPL"), unless you have obtained a separate licensing agreement - * ("Other License"), formally executed by you and Linden Lab. Terms of - * the GPL can be found in doc/GPL-license.txt in this distribution, or - * online at http://secondlife.com/developers/opensource/gplv2 + * Copyright (C) 2010, Linden Research, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; + * version 2.1 of the License only. * - * There are special exceptions to the terms and conditions of the GPL as - * it is applied to this Source Code. View the full text of the exception - * in the file doc/FLOSS-exception.txt in this software distribution, or - * online at http://secondlife.com/developers/opensource/flossexception + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. * - * By copying, modifying or distributing this software, you acknowledge - * that you have read and understood your obligations described above, - * and agree to abide by those obligations. + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * - * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO - * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY, - * COMPLETENESS OR PERFORMANCE. + * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA * $/LicenseInfo$ */ @@ -36,6 +31,7 @@ #include "llmath.h" #include "llsd.h" +class LLVector2; class LLVector4; class LLMatrix3; class LLVector3d; @@ -62,18 +58,18 @@ class LLVector3 inline LLVector3(); // Initializes LLVector3 to (0, 0, 0) inline LLVector3(const F32 x, const F32 y, const F32 z); // Initializes LLVector3 to (x. y, z) inline explicit LLVector3(const F32 *vec); // Initializes LLVector3 to (vec[0]. vec[1], vec[2]) + explicit LLVector3(const LLVector2 &vec); // Initializes LLVector3 to (vec[0]. vec[1], 0) explicit LLVector3(const LLVector3d &vec); // Initializes LLVector3 to (vec[0]. vec[1], vec[2]) explicit LLVector3(const LLVector4 &vec); // Initializes LLVector4 to (vec[0]. vec[1], vec[2]) - LLVector3(const LLSD& sd); + explicit LLVector3(const LLSD& sd); LLSD getValue() const; void setValue(const LLSD& sd); - const LLVector3& operator=(const LLSD& sd); - inline BOOL isFinite() const; // checks to see if all values of LLVector3 are finite BOOL clamp(F32 min, F32 max); // Clamps all values to (min,max), returns TRUE if data changed + BOOL clampLength( F32 length_limit ); // Scales vector to limit length to a value void quantize16(F32 lowerxy, F32 upperxy, F32 lowerz, F32 upperz); // changes the vector to reflect quatization void quantize8(F32 lowerxy, F32 upperxy, F32 lowerz, F32 upperz); // changes the vector to reflect quatization @@ -81,18 +77,33 @@ class LLVector3 BOOL abs(); // sets all values to absolute value of original value (first octant), returns TRUE if changed - inline void clearVec(); // Clears LLVector3 to (0, 0, 0, 1) - inline void zeroVec(); // Zero LLVector3 to (0, 0, 0, 0) - inline void setVec(F32 x, F32 y, F32 z); // Sets LLVector3 to (x, y, z, 1) - inline void setVec(const LLVector3 &vec); // Sets LLVector3 to vec - inline void setVec(const F32 *vec); // Sets LLVector3 to vec + inline void clear(); // Clears LLVector3 to (0, 0, 0) + inline void setZero(); // Clears LLVector3 to (0, 0, 0) + inline void clearVec(); // deprecated + inline void zeroVec(); // deprecated + + inline void set(F32 x, F32 y, F32 z); // Sets LLVector3 to (x, y, z, 1) + inline void set(const LLVector3 &vec); // Sets LLVector3 to vec + inline void set(const F32 *vec); // Sets LLVector3 to vec + const LLVector3& set(const LLVector4 &vec); + const LLVector3& set(const LLVector3d &vec);// Sets LLVector3 to vec - const LLVector3& setVec(const LLVector4 &vec); - const LLVector3& setVec(const LLVector3d &vec); // Sets LLVector3 to vec + inline void setVec(F32 x, F32 y, F32 z); // deprecated + inline void setVec(const LLVector3 &vec); // deprecated + inline void setVec(const F32 *vec); // deprecated - F32 magVec() const; // Returns magnitude of LLVector3 - F32 magVecSquared() const; // Returns magnitude squared of LLVector3 - inline F32 normVec(); // Normalizes and returns the magnitude of LLVector3 + const LLVector3& setVec(const LLVector4 &vec); // deprecated + const LLVector3& setVec(const LLVector3d &vec); // deprecated + + F32 length() const; // Returns magnitude of LLVector3 + F32 lengthSquared() const; // Returns magnitude squared of LLVector3 + F32 magVec() const; // deprecated + F32 magVecSquared() const; // deprecated + + inline F32 normalize(); // Normalizes and returns the magnitude of LLVector3 + inline F32 normVec(); // deprecated + + inline BOOL inRange( F32 min, F32 max ) const; // Returns true if all values of the vector are between min and max const LLVector3& rotVec(F32 angle, const LLVector3 &vec); // Rotates about vec by angle radians const LLVector3& rotVec(F32 angle, F32 x, F32 y, F32 z); // Rotates about x,y,z by angle radians @@ -132,7 +143,7 @@ class LLVector3 friend std::ostream& operator<<(std::ostream& s, const LLVector3 &a); // Stream a - static BOOL parseVector3(const char* buf, LLVector3* value); + static BOOL parseVector3(const std::string& buf, LLVector3* value); }; typedef LLVector3 LLSimLocalVec; @@ -142,8 +153,8 @@ typedef LLVector3 LLSimLocalVec; F32 angle_between(const LLVector3 &a, const LLVector3 &b); // Returns angle (radians) between a and b BOOL are_parallel(const LLVector3 &a, const LLVector3 &b, F32 epsilon=F_APPROXIMATELY_ZERO); // Returns TRUE if a and b are very close to parallel F32 dist_vec(const LLVector3 &a, const LLVector3 &b); // Returns distance between a and b -F32 dist_vec_squared(const LLVector3 &a, const LLVector3 &b);// Returns distance sqaured between a and b -F32 dist_vec_squared2D(const LLVector3 &a, const LLVector3 &b);// Returns distance sqaured between a and b ignoring Z component +F32 dist_vec_squared(const LLVector3 &a, const LLVector3 &b);// Returns distance squared between a and b +F32 dist_vec_squared2D(const LLVector3 &a, const LLVector3 &b);// Returns distance squared between a and b ignoring Z component LLVector3 projected_vec(const LLVector3 &a, const LLVector3 &b); // Returns vector a projected on vector b LLVector3 lerp(const LLVector3 &a, const LLVector3 &b, F32 u); // Returns a vector that is a linear interpolation between a and b @@ -188,6 +199,20 @@ inline BOOL LLVector3::isFinite() const // Clear and Assignment Functions +inline void LLVector3::clear(void) +{ + mV[0] = 0.f; + mV[1] = 0.f; + mV[2] = 0.f; +} + +inline void LLVector3::setZero(void) +{ + mV[0] = 0.f; + mV[1] = 0.f; + mV[2] = 0.f; +} + inline void LLVector3::clearVec(void) { mV[0] = 0.f; @@ -202,6 +227,28 @@ inline void LLVector3::zeroVec(void) mV[2] = 0.f; } +inline void LLVector3::set(F32 x, F32 y, F32 z) +{ + mV[VX] = x; + mV[VY] = y; + mV[VZ] = z; +} + +inline void LLVector3::set(const LLVector3 &vec) +{ + mV[0] = vec.mV[0]; + mV[1] = vec.mV[1]; + mV[2] = vec.mV[2]; +} + +inline void LLVector3::set(const F32 *vec) +{ + mV[0] = vec[0]; + mV[1] = vec[1]; + mV[2] = vec[2]; +} + +// deprecated inline void LLVector3::setVec(F32 x, F32 y, F32 z) { mV[VX] = x; @@ -209,6 +256,7 @@ inline void LLVector3::setVec(F32 x, F32 y, F32 z) mV[VZ] = z; } +// deprecated inline void LLVector3::setVec(const LLVector3 &vec) { mV[0] = vec.mV[0]; @@ -216,6 +264,7 @@ inline void LLVector3::setVec(const LLVector3 &vec) mV[2] = vec.mV[2]; } +// deprecated inline void LLVector3::setVec(const F32 *vec) { mV[0] = vec[0]; @@ -223,6 +272,29 @@ inline void LLVector3::setVec(const F32 *vec) mV[2] = vec[2]; } +inline F32 LLVector3::normalize(void) +{ + F32 mag = fsqrtf(mV[0]*mV[0] + mV[1]*mV[1] + mV[2]*mV[2]); + F32 oomag; + + if (mag > FP_MAG_THRESHOLD) + { + oomag = 1.f/mag; + mV[0] *= oomag; + mV[1] *= oomag; + mV[2] *= oomag; + } + else + { + mV[0] = 0.f; + mV[1] = 0.f; + mV[2] = 0.f; + mag = 0; + } + return (mag); +} + +// deprecated inline F32 LLVector3::normVec(void) { F32 mag = fsqrtf(mV[0]*mV[0] + mV[1]*mV[1] + mV[2]*mV[2]); @@ -247,6 +319,16 @@ inline F32 LLVector3::normVec(void) // LLVector3 Magnitude and Normalization Functions +inline F32 LLVector3::length(void) const +{ + return fsqrtf(mV[0]*mV[0] + mV[1]*mV[1] + mV[2]*mV[2]); +} + +inline F32 LLVector3::lengthSquared(void) const +{ + return mV[0]*mV[0] + mV[1]*mV[1] + mV[2]*mV[2]; +} + inline F32 LLVector3::magVec(void) const { return fsqrtf(mV[0]*mV[0] + mV[1]*mV[1] + mV[2]*mV[2]); @@ -257,6 +339,13 @@ inline F32 LLVector3::magVecSquared(void) const return mV[0]*mV[0] + mV[1]*mV[1] + mV[2]*mV[2]; } +inline BOOL LLVector3::inRange( F32 min, F32 max ) const +{ + return mV[0] >= min && mV[0] <= max && + mV[1] >= min && mV[1] <= max && + mV[2] >= min && mV[2] <= max; +} + inline LLVector3 operator+(const LLVector3 &a, const LLVector3 &b) { LLVector3 c(a); @@ -314,8 +403,8 @@ inline bool operator<(const LLVector3 &a, const LLVector3 &b) return (a.mV[0] < b.mV[0] || (a.mV[0] == b.mV[0] && (a.mV[1] < b.mV[1] - || (a.mV[1] == b.mV[1]) - && a.mV[2] < b.mV[2]))); + || ((a.mV[1] == b.mV[1]) + && a.mV[2] < b.mV[2])))); } inline const LLVector3& operator+=(LLVector3 &a, const LLVector3 &b) @@ -397,7 +486,7 @@ inline F32 dist_vec_squared2D(const LLVector3 &a, const LLVector3 &b) inline LLVector3 projected_vec(const LLVector3 &a, const LLVector3 &b) { LLVector3 project_axis = b; - project_axis.normVec(); + project_axis.normalize(); return project_axis * (a * project_axis); } @@ -438,8 +527,8 @@ inline F32 angle_between(const LLVector3& a, const LLVector3& b) { LLVector3 an = a; LLVector3 bn = b; - an.normVec(); - bn.normVec(); + an.normalize(); + bn.normalize(); F32 cosine = an * bn; F32 angle = (cosine >= 1.0f) ? 0.0f : (cosine <= -1.0f) ? F_PI : @@ -451,8 +540,8 @@ inline BOOL are_parallel(const LLVector3 &a, const LLVector3 &b, F32 epsilon) { LLVector3 an = a; LLVector3 bn = b; - an.normVec(); - bn.normVec(); + an.normalize(); + bn.normalize(); F32 dot = an * bn; if ( (1.0f - fabs(dot)) < epsilon) { @@ -461,4 +550,10 @@ inline BOOL are_parallel(const LLVector3 &a, const LLVector3 &b, F32 epsilon) return FALSE; } +inline std::ostream& operator<<(std::ostream& s, const LLVector3 &a) +{ + s << "{ " << a.mV[VX] << ", " << a.mV[VY] << ", " << a.mV[VZ] << " }"; + return s; +} + #endif |