From fefe7ddf3998b9036710b530f837aa591a86a9ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cle=CC=81ment=20Foucault?= Date: Thu, 1 Dec 2022 21:45:53 +0100 Subject: [PATCH] BLI: Add math::orthogonal and math::compare Port of C BLI API. --- source/blender/blenlib/BLI_math_vector.hh | 39 +++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/source/blender/blenlib/BLI_math_vector.hh b/source/blender/blenlib/BLI_math_vector.hh index e8303aa858b..6ce033ea376 100644 --- a/source/blender/blenlib/BLI_math_vector.hh +++ b/source/blender/blenlib/BLI_math_vector.hh @@ -414,6 +414,45 @@ template inline int dominant_axis(const vec_base &a) return ((b.x > b.y) ? ((b.x > b.z) ? 0 : 2) : ((b.y > b.z) ? 1 : 2)); } +/** + * Calculates a perpendicular vector to \a v. + * \note Returned vector can be in any perpendicular direction. + * \note Returned vector might not the same length as \a v. + */ +template inline vec_base orthogonal(const vec_base &v) +{ + const int axis = dominant_axis(v); + switch (axis) { + case 0: + return {-v.y - v.z, v.x, v.x}; + case 1: + return {v.y, -v.x - v.z, v.y}; + case 2: + return {v.z, v.z, -v.x - v.y}; + } + return v; +} + +/** + * Calculates a perpendicular vector to \a v. + * \note Returned vector can be in any perpendicular direction. + */ +template inline vec_base orthogonal(const vec_base &v) +{ + return {-v.y, v.x}; +} + +template +inline bool compare(const vec_base &a, const vec_base &b, const T limit) +{ + for (int i = 0; i < Size; i++) { + if (std::abs(a[i] - b[i]) > limit) { + return false; + } + } + return true; +} + /** Intersections. */ template struct isect_result {