diff options
Diffstat (limited to 'indra/llmath/v3math.h')
-rw-r--r-- | indra/llmath/v3math.h | 150 |
1 files changed, 103 insertions, 47 deletions
diff --git a/indra/llmath/v3math.h b/indra/llmath/v3math.h index 098ca5218c..551c7df6c9 100644 --- a/indra/llmath/v3math.h +++ b/indra/llmath/v3math.h @@ -112,24 +112,24 @@ class 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 + 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 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 + 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 - const LLVector3& rotVec(const LLMatrix3 &mat); // Rotates by LLMatrix4 mat - const LLVector3& rotVec(const LLQuaternion &q); // Rotates by LLQuaternion q - const LLVector3& transVec(const LLMatrix4& mat); // Transforms by LLMatrix4 mat (mat * v) + 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 + const LLVector3& rotVec(const LLMatrix3 &mat); // Rotates by LLMatrix4 mat + const LLVector3& rotVec(const LLQuaternion &q); // Rotates by LLQuaternion q + const LLVector3& transVec(const LLMatrix4& mat); // Transforms by LLMatrix4 mat (mat * v) - const LLVector3& scaleVec(const LLVector3& vec); // scales per component by vec - LLVector3 scaledVec(const LLVector3& vec) const; // get a copy of this vector scaled by vec + const LLVector3& scaleVec(const LLVector3& vec); // scales per component by vec + LLVector3 scaledVec(const LLVector3& vec) const; // get a copy of this vector scaled by vec bool isNull() const; // Returns true if vector has a _very_small_ length bool isExactlyZero() const { return !mV[VX] && !mV[VY] && !mV[VZ]; } @@ -183,17 +183,23 @@ bool box_valid_and_non_zero(const LLVector3* box); inline LLVector3::LLVector3() { - clear(); + mV[VX] = 0.f; + mV[VY] = 0.f; + mV[VZ] = 0.f; } inline LLVector3::LLVector3(const F32 x, const F32 y, const F32 z) { - set(x, y, z); + mV[VX] = x; + mV[VY] = y; + mV[VZ] = z; } inline LLVector3::LLVector3(const F32 *vec) { - set(vec); + mV[VX] = vec[VX]; + mV[VY] = vec[VY]; + mV[VZ] = vec[VZ]; } inline LLVector3::LLVector3(const glm::vec3& vec) @@ -224,7 +230,7 @@ inline LLVector3::LLVector3(const LLVector3 ©) // checker inline bool LLVector3::isFinite() const { - return llfinite(mV[VX]) && llfinite(mV[VY]) && llfinite(mV[VZ]); + return (llfinite(mV[VX]) && llfinite(mV[VY]) && llfinite(mV[VZ])); } @@ -232,22 +238,30 @@ inline bool LLVector3::isFinite() const inline void LLVector3::clear() { - set(0.f, 0.f, 0.f); + mV[VX] = 0.f; + mV[VY] = 0.f; + mV[VZ] = 0.f; } inline void LLVector3::setZero() { - clear(); + mV[VX] = 0.f; + mV[VY] = 0.f; + mV[VZ] = 0.f; } inline void LLVector3::clearVec() { - clear(); + mV[VX] = 0.f; + mV[VY] = 0.f; + mV[VZ] = 0.f; } inline void LLVector3::zeroVec() { - clear(); + mV[VX] = 0.f; + mV[VY] = 0.f; + mV[VZ] = 0.f; } inline void LLVector3::set(F32 x, F32 y, F32 z) @@ -259,12 +273,16 @@ inline void LLVector3::set(F32 x, F32 y, F32 z) inline void LLVector3::set(const LLVector3& vec) { - set(vec.mV[VX], vec.mV[VY], vec.mV[VZ]); + mV[VX] = vec.mV[VX]; + mV[VY] = vec.mV[VY]; + mV[VZ] = vec.mV[VZ]; } inline void LLVector3::set(const F32* vec) { - set(vec[VX], vec[VY], vec[VZ]); + mV[VX] = vec[VX]; + mV[VY] = vec[VY]; + mV[VZ] = vec[VZ]; } inline void LLVector3::set(const glm::vec4& vec) @@ -284,66 +302,95 @@ inline void LLVector3::set(const glm::vec3& vec) // deprecated inline void LLVector3::setVec(F32 x, F32 y, F32 z) { - set(x, y, z); + mV[VX] = x; + mV[VY] = y; + mV[VZ] = z; } // deprecated inline void LLVector3::setVec(const LLVector3& vec) { - set(vec); + mV[VX] = vec.mV[VX]; + mV[VY] = vec.mV[VY]; + mV[VZ] = vec.mV[VZ]; } // deprecated inline void LLVector3::setVec(const F32* vec) { - set(vec); + mV[VX] = vec[0]; + mV[VY] = vec[1]; + mV[VZ] = vec[2]; } inline F32 LLVector3::normalize() { F32 mag = (F32) sqrt(mV[VX]*mV[VX] + mV[VY]*mV[VY] + mV[VZ]*mV[VZ]); + F32 oomag; if (mag > FP_MAG_THRESHOLD) { - *this /= mag; + oomag = 1.f/mag; + mV[VX] *= oomag; + mV[VY] *= oomag; + mV[VZ] *= oomag; } else { - clear(); + mV[VX] = 0.f; + mV[VY] = 0.f; + mV[VZ] = 0.f; mag = 0; } - return mag; + return (mag); } // deprecated inline F32 LLVector3::normVec() { - return normalize(); + F32 mag = sqrt(mV[VX]*mV[VX] + mV[VY]*mV[VY] + mV[VZ]*mV[VZ]); + F32 oomag; + + if (mag > FP_MAG_THRESHOLD) + { + oomag = 1.f/mag; + mV[VX] *= oomag; + mV[VY] *= oomag; + mV[VZ] *= oomag; + } + else + { + mV[VX] = 0.f; + mV[VY] = 0.f; + mV[VZ] = 0.f; + mag = 0; + } + return (mag); } // LLVector3 Magnitude and Normalization Functions -inline F32 LLVector3::length() const +inline F32 LLVector3::length() const { - return sqrt(lengthSquared()); + return sqrt(mV[VX]*mV[VX] + mV[VY]*mV[VY] + mV[VZ]*mV[VZ]); } -inline F32 LLVector3::lengthSquared() const +inline F32 LLVector3::lengthSquared() const { return mV[VX]*mV[VX] + mV[VY]*mV[VY] + mV[VZ]*mV[VZ]; } -inline F32 LLVector3::magVec() const +inline F32 LLVector3::magVec() const { - return length(); + return sqrt(mV[VX]*mV[VX] + mV[VY]*mV[VY] + mV[VZ]*mV[VZ]); } -inline F32 LLVector3::magVecSquared() const +inline F32 LLVector3::magVecSquared() const { - return lengthSquared(); + return mV[VX]*mV[VX] + mV[VY]*mV[VY] + mV[VZ]*mV[VZ]; } -inline bool LLVector3::inRange(F32 min, F32 max) const +inline bool LLVector3::inRange( F32 min, F32 max ) const { return mV[VX] >= min && mV[VX] <= max && mV[VY] >= min && mV[VY] <= max && @@ -369,7 +416,7 @@ inline F32 operator*(const LLVector3& a, const LLVector3& b) inline LLVector3 operator%(const LLVector3& a, const LLVector3& b) { - return LLVector3(a.mV[VY]*b.mV[VZ] - b.mV[VY]*a.mV[VZ], a.mV[VZ]*b.mV[VX] - b.mV[VZ]*a.mV[VX], a.mV[VX]*b.mV[VY] - b.mV[VX]*a.mV[VY]); + return LLVector3( a.mV[VY]*b.mV[VZ] - b.mV[VY]*a.mV[VZ], a.mV[VZ]*b.mV[VX] - b.mV[VZ]*a.mV[VX], a.mV[VX]*b.mV[VY] - b.mV[VX]*a.mV[VY] ); } inline LLVector3 operator/(const LLVector3& a, F32 k) @@ -429,7 +476,7 @@ inline const LLVector3& operator-=(LLVector3& a, const LLVector3& b) inline const LLVector3& operator%=(LLVector3& a, const LLVector3& b) { - LLVector3 ret(a.mV[VY]*b.mV[VZ] - b.mV[VY]*a.mV[VZ], a.mV[VZ]*b.mV[VX] - b.mV[VZ]*a.mV[VX], a.mV[VX]*b.mV[VY] - b.mV[VX]*a.mV[VY]); + LLVector3 ret( a.mV[VY]*b.mV[VZ] - b.mV[VY]*a.mV[VZ], a.mV[VZ]*b.mV[VX] - b.mV[VZ]*a.mV[VX], a.mV[VX]*b.mV[VY] - b.mV[VX]*a.mV[VY]); a = ret; return a; } @@ -452,7 +499,9 @@ inline const LLVector3& operator*=(LLVector3& a, const LLVector3& b) inline const LLVector3& operator/=(LLVector3& a, F32 k) { - a *= 1.f / k; + a.mV[VX] /= k; + a.mV[VY] /= k; + a.mV[VZ] /= k; return a; } @@ -477,7 +526,7 @@ inline F32 dist_vec(const LLVector3& a, const LLVector3& b) F32 x = a.mV[VX] - b.mV[VX]; F32 y = a.mV[VY] - b.mV[VY]; F32 z = a.mV[VZ] - b.mV[VZ]; - return sqrt(x*x + y*y + z*z); + return sqrt( x*x + y*y + z*z ); } inline F32 dist_vec_squared(const LLVector3& a, const LLVector3& b) @@ -502,7 +551,10 @@ inline LLVector3 projected_vec(const LLVector3& a, const LLVector3& b) { return ((a * b) / bb) * b; } - return b.zero; + else + { + return b.zero; + } } inline LLVector3 inverse_projected_vec(const LLVector3& a, const LLVector3& b) @@ -539,7 +591,11 @@ inline LLVector3 lerp(const LLVector3& a, const LLVector3& b, F32 u) inline bool LLVector3::isNull() const { - return F_APPROXIMATELY_ZERO > mV[VX]*mV[VX] + mV[VY]*mV[VY] + mV[VZ]*mV[VZ]; + if ( F_APPROXIMATELY_ZERO > mV[VX]*mV[VX] + mV[VY]*mV[VY] + mV[VZ]*mV[VZ] ) + { + return true; + } + return false; } inline void update_min_max(LLVector3& min, LLVector3& max, const LLVector3& pos) @@ -580,7 +636,7 @@ inline F32 angle_between(const LLVector3& a, const LLVector3& b) ab = 0.0f; // get rid of negative zero } LLVector3 c = a % b; // crossproduct - return atan2f(c.length(), ab); // return the angle + return atan2f(sqrtf(c * c), ab); // return the angle } inline bool are_parallel(const LLVector3& a, const LLVector3& b, F32 epsilon) @@ -590,7 +646,7 @@ inline bool are_parallel(const LLVector3& a, const LLVector3& b, F32 epsilon) an.normalize(); bn.normalize(); F32 dot = an * bn; - if (1.0f - fabs(dot) < epsilon) + if ( (1.0f - fabs(dot)) < epsilon) { return true; } |