From 6035cf05bf1c794c35e89fbf467cfc35901d81e3 Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Fri, 8 Jul 2016 09:52:29 +1000 Subject: BLI_math: add normalize functions which fit to a length Convenient since its common to normalize then scale, since these are inlined, use for regular normalize w/ 1.0 length. --- source/blender/blenlib/BLI_math_vector.h | 6 ++++ source/blender/blenlib/intern/math_vector_inline.c | 34 ++++++++++++++++++---- 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/source/blender/blenlib/BLI_math_vector.h b/source/blender/blenlib/BLI_math_vector.h index 8a36b047bad..4d546abe201 100644 --- a/source/blender/blenlib/BLI_math_vector.h +++ b/source/blender/blenlib/BLI_math_vector.h @@ -191,6 +191,12 @@ MINLINE float len_manhattan_v3v3(const float a[3], const float b[3]) ATTR_WARN_U MINLINE float len_v3(const float a[3]) ATTR_WARN_UNUSED_RESULT; MINLINE float len_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT; +MINLINE float normalize_v2_length(float r[2], const float unit_scale); +MINLINE float normalize_v2_v2_length(float r[2], const float a[2], const float unit_scale); +MINLINE float normalize_v3_length(float r[3], const float unit_scale); +MINLINE float normalize_v3_v3_length(float r[3], const float a[3], const float unit_scale); +MINLINE double normalize_v3_length_d(double n[3], const double unit_scale); + MINLINE float normalize_v2(float r[2]); MINLINE float normalize_v2_v2(float r[2], const float a[2]); MINLINE float normalize_v3(float r[3]); diff --git a/source/blender/blenlib/intern/math_vector_inline.c b/source/blender/blenlib/intern/math_vector_inline.c index fd9f3d5ff99..e9fb77f6302 100644 --- a/source/blender/blenlib/intern/math_vector_inline.c +++ b/source/blender/blenlib/intern/math_vector_inline.c @@ -859,13 +859,13 @@ MINLINE float len_v3v3(const float a[3], const float b[3]) return len_v3(d); } -MINLINE float normalize_v2_v2(float r[2], const float a[2]) +MINLINE float normalize_v2_v2_length(float r[2], const float a[2], const float unit_length) { float d = dot_v2v2(a, a); if (d > 1.0e-35f) { d = sqrtf(d); - mul_v2_v2fl(r, a, 1.0f / d); + mul_v2_v2fl(r, a, unit_length / d); } else { zero_v2(r); @@ -874,13 +874,22 @@ MINLINE float normalize_v2_v2(float r[2], const float a[2]) return d; } +MINLINE float normalize_v2_v2(float r[2], const float a[2]) +{ + return normalize_v2_v2_length(r, a, 1.0f); +} MINLINE float normalize_v2(float n[2]) { return normalize_v2_v2(n, n); } -MINLINE float normalize_v3_v3(float r[3], const float a[3]) +MINLINE float normalize_v2_length(float n[2], const float unit_length) +{ + return normalize_v2_v2_length(n, n, unit_length); +} + +MINLINE float normalize_v3_v3_length(float r[3], const float a[3], const float unit_length) { float d = dot_v3v3(a, a); @@ -888,7 +897,7 @@ MINLINE float normalize_v3_v3(float r[3], const float a[3]) * scaled down models with camera extreme close */ if (d > 1.0e-35f) { d = sqrtf(d); - mul_v3_v3fl(r, a, 1.0f / d); + mul_v3_v3fl(r, a, unit_length / d); } else { zero_v3(r); @@ -897,8 +906,12 @@ MINLINE float normalize_v3_v3(float r[3], const float a[3]) return d; } +MINLINE float normalize_v3_v3(float r[3], const float a[3]) +{ + return normalize_v3_v3_length(r, a, 1.0f); +} -MINLINE double normalize_v3_d(double n[3]) +MINLINE double normalize_v3_length_d(double n[3], const double unit_length) { double d = n[0] * n[0] + n[1] * n[1] + n[2] * n[2]; @@ -908,7 +921,7 @@ MINLINE double normalize_v3_d(double n[3]) double mul; d = sqrt(d); - mul = 1.0 / d; + mul = unit_length / d; n[0] *= mul; n[1] *= mul; @@ -921,6 +934,15 @@ MINLINE double normalize_v3_d(double n[3]) return d; } +MINLINE double normalize_v3_d(double n[3]) +{ + return normalize_v3_length_d(n, 1.0); +} + +MINLINE float normalize_v3_length(float n[3], const float unit_length) +{ + return normalize_v3_v3_length(n, n, unit_length); +} MINLINE float normalize_v3(float n[3]) { -- cgit v1.2.3