Changeset View
Changeset View
Standalone View
Standalone View
source/blender/blenlib/BLI_math_vector.hh
| Show All 11 Lines | |||||
| #include "BLI_math_base.hh" | #include "BLI_math_base.hh" | ||||
| #include "BLI_math_vec_types.hh" | #include "BLI_math_vec_types.hh" | ||||
| #include "BLI_span.hh" | #include "BLI_span.hh" | ||||
| #include "BLI_utildefines.h" | #include "BLI_utildefines.h" | ||||
| namespace blender::math { | namespace blender::math { | ||||
| #ifndef NDEBUG | |||||
| # define BLI_ASSERT_UNIT(v) \ | |||||
| { \ | |||||
| const float _test_unit = length_squared(v); \ | |||||
| BLI_assert(!(std::abs(_test_unit - 1.0f) >= BLI_ASSERT_UNIT_EPSILON) || \ | |||||
| !(std::abs(_test_unit) >= BLI_ASSERT_UNIT_EPSILON)); \ | |||||
| } \ | |||||
| (void)0 | |||||
| #else | |||||
| # define BLI_ASSERT_UNIT(v) (void)(v) | |||||
| #endif | |||||
| template<typename T, int Size> inline bool is_zero(const vec_base<T, Size> &a) | template<typename T, int Size> inline bool is_zero(const vec_base<T, Size> &a) | ||||
| { | { | ||||
| for (int i = 0; i < Size; i++) { | for (int i = 0; i < Size; i++) { | ||||
| if (a[i] != T(0)) { | if (a[i] != T(0)) { | ||||
| return false; | return false; | ||||
| } | } | ||||
| } | } | ||||
| return true; | return true; | ||||
| ▲ Show 20 Lines • Show All 92 Lines • ▼ Show 20 Lines | inline vec_base<T, Size> mod(const vec_base<T, Size> &a, const T &b) | ||||
| vec_base<T, Size> result; | vec_base<T, Size> result; | ||||
| for (int i = 0; i < Size; i++) { | for (int i = 0; i < Size; i++) { | ||||
| result[i] = std::fmod(a[i], b); | result[i] = std::fmod(a[i], b); | ||||
| } | } | ||||
| return result; | return result; | ||||
| } | } | ||||
| template<typename T, int Size, BLI_ENABLE_IF((is_math_float_type<T>))> | template<typename T, int Size, BLI_ENABLE_IF((is_math_float_type<T>))> | ||||
| inline T safe_mod(const vec_base<T, Size> &a, const vec_base<T, Size> &b) | inline vec_base<T, Size> safe_mod(const vec_base<T, Size> &a, const vec_base<T, Size> &b) | ||||
| { | { | ||||
| vec_base<T, Size> result; | vec_base<T, Size> result; | ||||
| for (int i = 0; i < Size; i++) { | for (int i = 0; i < Size; i++) { | ||||
| result[i] = (b[i] != 0) ? std::fmod(a[i], b[i]) : 0; | result[i] = (b[i] != 0) ? std::fmod(a[i], b[i]) : 0; | ||||
| } | } | ||||
| return result; | return result; | ||||
| } | } | ||||
| template<typename T, int Size, BLI_ENABLE_IF((is_math_float_type<T>))> | template<typename T, int Size, BLI_ENABLE_IF((is_math_float_type<T>))> | ||||
| inline T safe_mod(const vec_base<T, Size> &a, const T &b) | inline vec_base<T, Size> safe_mod(const vec_base<T, Size> &a, const T &b) | ||||
| { | { | ||||
| if (b == 0) { | if (b == 0) { | ||||
| return vec_base<T, Size>(0); | return vec_base<T, Size>(0); | ||||
| } | } | ||||
| vec_base<T, Size> result; | vec_base<T, Size> result; | ||||
| for (int i = 0; i < Size; i++) { | for (int i = 0; i < Size; i++) { | ||||
| result[i] = std::fmod(a[i], b); | result[i] = std::fmod(a[i], b); | ||||
| } | } | ||||
| ▲ Show 20 Lines • Show All 114 Lines • ▼ Show 20 Lines | |||||
| } | } | ||||
| template<typename T, int Size, BLI_ENABLE_IF((is_math_float_type<T>))> | template<typename T, int Size, BLI_ENABLE_IF((is_math_float_type<T>))> | ||||
| inline T length(const vec_base<T, Size> &a) | inline T length(const vec_base<T, Size> &a) | ||||
| { | { | ||||
| return std::sqrt(length_squared(a)); | return std::sqrt(length_squared(a)); | ||||
| } | } | ||||
| template<typename T, int Size> inline bool is_unit_scale(const vec_base<T, Size> &v) | |||||
| { | |||||
| /* Checks are flipped so NAN doesn't assert because we're making sure the value was | |||||
| * normalized and in the case we don't want NAN to be raising asserts since there | |||||
| * is nothing to be done in that case. */ | |||||
| const T test_unit = math::length_squared(v); | |||||
| return (!(std::abs(test_unit - T(1)) >= AssertUnitEpsilon<T>::value) || | |||||
HooglyBoogly: Comments inside of functions don't have to use doxygen format. Better to avoid the whitespace… | |||||
| !(std::abs(test_unit) >= AssertUnitEpsilon<T>::value)); | |||||
| } | |||||
| template<typename T, int Size, BLI_ENABLE_IF((is_math_float_type<T>))> | template<typename T, int Size, BLI_ENABLE_IF((is_math_float_type<T>))> | ||||
| inline T distance_manhattan(const vec_base<T, Size> &a, const vec_base<T, Size> &b) | inline T distance_manhattan(const vec_base<T, Size> &a, const vec_base<T, Size> &b) | ||||
| { | { | ||||
| return length_manhattan(a - b); | return length_manhattan(a - b); | ||||
| } | } | ||||
| template<typename T, int Size, BLI_ENABLE_IF((is_math_float_type<T>))> | template<typename T, int Size, BLI_ENABLE_IF((is_math_float_type<T>))> | ||||
| inline T distance_squared(const vec_base<T, Size> &a, const vec_base<T, Size> &b) | inline T distance_squared(const vec_base<T, Size> &a, const vec_base<T, Size> &b) | ||||
| { | { | ||||
| return length_squared(a - b); | return length_squared(a - b); | ||||
| } | } | ||||
| template<typename T, int Size, BLI_ENABLE_IF((is_math_float_type<T>))> | template<typename T, int Size, BLI_ENABLE_IF((is_math_float_type<T>))> | ||||
| inline T distance(const vec_base<T, Size> &a, const vec_base<T, Size> &b) | inline T distance(const vec_base<T, Size> &a, const vec_base<T, Size> &b) | ||||
| { | { | ||||
| return length(a - b); | return length(a - b); | ||||
| } | } | ||||
| template<typename T, int Size, BLI_ENABLE_IF((is_math_float_type<T>))> | template<typename T, int Size, BLI_ENABLE_IF((is_math_float_type<T>))> | ||||
| inline vec_base<T, Size> reflect(const vec_base<T, Size> &incident, | inline vec_base<T, Size> reflect(const vec_base<T, Size> &incident, | ||||
| const vec_base<T, Size> &normal) | const vec_base<T, Size> &normal) | ||||
| { | { | ||||
| BLI_ASSERT_UNIT(normal); | BLI_assert(is_unit_scale(normal)); | ||||
| return incident - 2.0 * dot(normal, incident) * normal; | return incident - 2.0 * dot(normal, incident) * normal; | ||||
| } | } | ||||
| template<typename T, int Size, BLI_ENABLE_IF((is_math_float_type<T>))> | template<typename T, int Size, BLI_ENABLE_IF((is_math_float_type<T>))> | ||||
| inline vec_base<T, Size> refract(const vec_base<T, Size> &incident, | inline vec_base<T, Size> refract(const vec_base<T, Size> &incident, | ||||
| const vec_base<T, Size> &normal, | const vec_base<T, Size> &normal, | ||||
| const T &eta) | const T &eta) | ||||
| { | { | ||||
| ▲ Show 20 Lines • Show All 83 Lines • ▼ Show 20 Lines | |||||
| } | } | ||||
| template<typename T, int Size, BLI_ENABLE_IF((is_math_float_type<T>))> | template<typename T, int Size, BLI_ENABLE_IF((is_math_float_type<T>))> | ||||
| inline vec_base<T, Size> midpoint(const vec_base<T, Size> &a, const vec_base<T, Size> &b) | inline vec_base<T, Size> midpoint(const vec_base<T, Size> &a, const vec_base<T, Size> &b) | ||||
| { | { | ||||
| return (a + b) * 0.5; | return (a + b) * 0.5; | ||||
| } | } | ||||
| /** | |||||
| * Return `vector` if `incident` and `reference` are pointing in the same direction. | |||||
| */ | |||||
| template<typename T, int Size, BLI_ENABLE_IF((is_math_float_type<T>))> | template<typename T, int Size, BLI_ENABLE_IF((is_math_float_type<T>))> | ||||
| inline vec_base<T, Size> faceforward(const vec_base<T, Size> &vector, | inline vec_base<T, Size> faceforward(const vec_base<T, Size> &vector, | ||||
| const vec_base<T, Size> &incident, | const vec_base<T, Size> &incident, | ||||
| const vec_base<T, Size> &reference) | const vec_base<T, Size> &reference) | ||||
| { | { | ||||
| return (dot(reference, incident) < 0) ? vector : -vector; | return (dot(reference, incident) < 0) ? vector : -vector; | ||||
| } | } | ||||
| ▲ Show 20 Lines • Show All 64 Lines • Show Last 20 Lines | |||||
Comments inside of functions don't have to use doxygen format. Better to avoid the whitespace and write them a little differently.
The \note often just adds noise anyway, and could be replaced by a more standard description of what the function does.
The same comment applies in other places in this patch. Searching for /** \note finds most of them.