From 681305e9bf725872af906acb707627b26b3c8e67 Mon Sep 17 00:00:00 2001 From: MartinRJ Fayray Date: Thu, 25 Oct 2012 07:36:33 +0200 Subject: BUG-540: Math updates by Moon Metty. Reviewed by Chieron Tenk. --- indra/llmath/v3math.h | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) (limited to 'indra/llmath/v3math.h') diff --git a/indra/llmath/v3math.h b/indra/llmath/v3math.h index 0432aeba4c..a269ed1b79 100644 --- a/indra/llmath/v3math.h +++ b/indra/llmath/v3math.h @@ -1,4 +1,4 @@ -/** +/** * @file v3math.h * @brief LLVector3 class header file. * @@ -490,9 +490,15 @@ 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.normalize(); - return project_axis * (a * project_axis); + F32 bb = b * b; + if (bb > FP_MAG_THRESHOLD * FP_MAG_THRESHOLD) + { + return ((a * b) / bb) * b; + } + else + { + return b.zero; + } } inline LLVector3 parallel_component(const LLVector3 &a, const LLVector3 &b) @@ -556,15 +562,13 @@ inline void update_min_max(LLVector3& min, LLVector3& max, const F32* pos) inline F32 angle_between(const LLVector3& a, const LLVector3& b) { - LLVector3 an = a; - LLVector3 bn = b; - an.normalize(); - bn.normalize(); - F32 cosine = an * bn; - F32 angle = (cosine >= 1.0f) ? 0.0f : - (cosine <= -1.0f) ? F_PI : - (F32)acos(cosine); - return angle; + F32 ab = a * b; // dotproduct + if (ab == -0.0f) + { + ab = 0.0f; // get rid of negative zero + } + LLVector3 c = a % b; // crossproduct + return atan2f(sqrtf(c * c), ab); // return the angle } inline BOOL are_parallel(const LLVector3 &a, const LLVector3 &b, F32 epsilon) -- cgit v1.2.3 From a060dcfff790f16dd4a0097e0eca4ff4b7f3d0de Mon Sep 17 00:00:00 2001 From: Ricky Curtice Date: Tue, 18 Feb 2014 23:03:13 -0800 Subject: STORM-1920: Math cleanup from Richard Linden. --- indra/llmath/v3math.h | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'indra/llmath/v3math.h') diff --git a/indra/llmath/v3math.h b/indra/llmath/v3math.h index 0432aeba4c..1461cd57b6 100755 --- a/indra/llmath/v3math.h +++ b/indra/llmath/v3math.h @@ -159,6 +159,7 @@ F32 dist_vec(const LLVector3 &a, const LLVector3 &b); // Returns distance betwe 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 inverse_projected_vec(const LLVector3 &a, const LLVector3 &b); // Returns vector a scaled such that projected_vec(inverse_projected_vec(a, b), b) == b; LLVector3 parallel_component(const LLVector3 &a, const LLVector3 &b); // Returns vector a projected on vector b (same as projected_vec) LLVector3 orthogonal_component(const LLVector3 &a, const LLVector3 &b); // Returns component of vector a not parallel to vector b (same as projected_vec) LLVector3 lerp(const LLVector3 &a, const LLVector3 &b, F32 u); // Returns a vector that is a linear interpolation between a and b @@ -495,6 +496,18 @@ inline LLVector3 projected_vec(const LLVector3 &a, const LLVector3 &b) return project_axis * (a * project_axis); } +inline LLVector3 inverse_projected_vec(const LLVector3& a, const LLVector3& b) +{ + LLVector3 normalized_a = a; + normalized_a.normalize(); + LLVector3 normalized_b = b; + F32 b_length = normalized_b.normalize(); + + F32 dot_product = normalized_a * normalized_b; + //NB: if a _|_ b, then returns an infinite vector + return normalized_a * (b_length / dot_product); +} + inline LLVector3 parallel_component(const LLVector3 &a, const LLVector3 &b) { return projected_vec(a, b); -- cgit v1.2.3