diff options
author | Campbell Barton <ideasman42@gmail.com> | 2014-01-14 02:14:34 +0400 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2014-01-14 02:48:59 +0400 |
commit | 8cb9b42c9c3ec09e78d59ac80fd7db9d50342170 (patch) | |
tree | 2deb8fd3d12f0fb9a5f88ccdd636aceb0816a7e9 /source/blender/blenlib/intern/math_vector.c | |
parent | a02a97753b74b96971a94c9c979ea122c26a2870 (diff) |
Math Lib: minor optimization for angle functions
Diffstat (limited to 'source/blender/blenlib/intern/math_vector.c')
-rw-r--r-- | source/blender/blenlib/intern/math_vector.c | 31 |
1 files changed, 15 insertions, 16 deletions
diff --git a/source/blender/blenlib/intern/math_vector.c b/source/blender/blenlib/intern/math_vector.c index 48134cbf21d..6d3c74abc66 100644 --- a/source/blender/blenlib/intern/math_vector.c +++ b/source/blender/blenlib/intern/math_vector.c @@ -292,41 +292,40 @@ float angle_signed_v2v2(const float v1[2], const float v2[2]) float angle_normalized_v3v3(const float v1[3], const float v2[3]) { + const float len_sq = dot_v3v3(v1, v2); + /* double check they are normalized */ BLI_ASSERT_UNIT_V3(v1); BLI_ASSERT_UNIT_V3(v2); /* this is the same as acos(dot_v3v3(v1, v2)), but more accurate */ - if (dot_v3v3(v1, v2) < 0.0f) { + if (len_sq >= 0.0f) { + return 2.0f * saasin(sqrtf(len_sq) / 2.0f); + } + else { float vec[3]; - - vec[0] = -v2[0]; - vec[1] = -v2[1]; - vec[2] = -v2[2]; - - return (float)M_PI - 2.0f * (float)saasin(len_v3v3(vec, v1) / 2.0f); + negate_v3_v3(vec, v2); + return (float)M_PI - 2.0f * saasin(len_v3v3(vec, v1) / 2.0f); } - else - return 2.0f * (float)saasin(len_v3v3(v2, v1) / 2.0f); } float angle_normalized_v2v2(const float v1[2], const float v2[2]) { + const float len_sq = dot_v2v2(v1, v2); + /* double check they are normalized */ BLI_ASSERT_UNIT_V2(v1); BLI_ASSERT_UNIT_V2(v2); /* this is the same as acos(dot_v3v3(v1, v2)), but more accurate */ - if (dot_v2v2(v1, v2) < 0.0f) { + if (len_sq >= 0.0f) { + return 2.0f * saasin(sqrtf(len_sq) / 2.0f); + } + else { float vec[2]; - - vec[0] = -v2[0]; - vec[1] = -v2[1]; - + negate_v2_v2(vec, v2); return (float)M_PI - 2.0f * saasin(len_v2v2(vec, v1) / 2.0f); } - else - return 2.0f * (float)saasin(len_v2v2(v2, v1) / 2.0f); } /** |