Changeset View
Changeset View
Standalone View
Standalone View
source/blender/blenlib/BLI_math_base.hh
| /* SPDX-License-Identifier: GPL-2.0-or-later | /* SPDX-License-Identifier: GPL-2.0-or-later | ||||
| * Copyright 2022 Blender Foundation. */ | * Copyright 2022 Blender Foundation. */ | ||||
| #pragma once | #pragma once | ||||
| /** \file | /** \file | ||||
| * \ingroup bli | * \ingroup bli | ||||
| */ | */ | ||||
| #include <algorithm> | #include <algorithm> | ||||
| #include <cmath> | #include <cmath> | ||||
| #include <type_traits> | #include <type_traits> | ||||
| #include "BLI_math_base_safe.h" | #include "BLI_math_base_safe.h" | ||||
| #include "BLI_math_vec_types.hh" | |||||
| #include "BLI_utildefines.h" | #include "BLI_utildefines.h" | ||||
| #ifdef WITH_GMP | #ifdef WITH_GMP | ||||
| # include "BLI_math_mpq.hh" | # include "BLI_math_mpq.hh" | ||||
| #endif | #endif | ||||
| namespace blender::math { | namespace blender::math { | ||||
| template<typename T> | |||||
| inline constexpr bool is_math_float_type = (std::is_floating_point_v<T> | |||||
| #ifdef WITH_GMP | |||||
| || std::is_same_v<T, mpq_class> | |||||
| #endif | |||||
| ); | |||||
| template<typename T> inline constexpr bool is_math_integral_type = std::is_integral_v<T>; | |||||
| template<typename T> inline bool is_zero(const T &a) | template<typename T> inline bool is_zero(const T &a) | ||||
| { | { | ||||
| return a == T(0); | return a == T(0); | ||||
| } | } | ||||
| template<typename T> inline bool is_any_zero(const T &a) | template<typename T> inline bool is_any_zero(const T &a) | ||||
| { | { | ||||
| return is_zero(a); | return is_zero(a); | ||||
| ▲ Show 20 Lines • Show All 47 Lines • ▼ Show 20 Lines | template<typename T, BLI_ENABLE_IF((is_math_float_type<T>))> inline T floor(const T &a) | ||||
| return std::floor(a); | return std::floor(a); | ||||
| } | } | ||||
| template<typename T, BLI_ENABLE_IF((is_math_float_type<T>))> inline T ceil(const T &a) | template<typename T, BLI_ENABLE_IF((is_math_float_type<T>))> inline T ceil(const T &a) | ||||
| { | { | ||||
| return std::ceil(a); | return std::ceil(a); | ||||
| } | } | ||||
| template<typename T> inline T distance(const T &a, const T &b) | |||||
JacquesLucke: Doesn't seem to be used. | |||||
Not Done Inline ActionsAlso I would expect distance to return a scalar distance between two points. Maybe there is instances where this is useful for scalar types? fclem: Also I would expect `distance` to return a scalar distance between two points. Maybe there is… | |||||
Done Inline ActionsAh, I forgot to mention that in the patch description. I'm using this in D14382 so the same code can work for vector and scalar types. I know it makes less sense to have a distance function for scalars, but the consistency is very helpful. HooglyBoogly: Ah, I forgot to mention that in the patch description. I'm using this in D14382 so the same… | |||||
| { | |||||
| return std::abs(a - b); | |||||
| } | |||||
| template<typename T, BLI_ENABLE_IF((is_math_float_type<T>))> inline T fract(const T &a) | template<typename T, BLI_ENABLE_IF((is_math_float_type<T>))> inline T fract(const T &a) | ||||
| { | { | ||||
| return a - std::floor(a); | return a - std::floor(a); | ||||
| } | } | ||||
| template<typename T, BLI_ENABLE_IF((is_math_float_type<T>))> | template<typename T, typename FactorT, BLI_ENABLE_IF((is_math_float_type<FactorT>))> | ||||
| inline T interpolate(const T &a, const T &b, const T &t) | inline T interpolate(const T &a, const T &b, const FactorT &t) | ||||
| { | { | ||||
| return a * (1 - t) + b * t; | return a * (1 - t) + b * t; | ||||
| } | } | ||||
| template<typename T, BLI_ENABLE_IF((is_math_float_type<T>))> | template<typename T> inline T midpoint(const T &a, const T &b) | ||||
| inline T midpoint(const T &a, const T &b) | |||||
| { | { | ||||
| return (a + b) * T(0.5); | return (a + b) * T(0.5); | ||||
| } | } | ||||
| } // namespace blender::math | } // namespace blender::math | ||||
Doesn't seem to be used.