/* SPDX-License-Identifier: GPL-2.0-or-later * Copyright 2022 Blender Foundation. */ #pragma once /** \file * \ingroup bli */ #include #include #include #include "BLI_math_base_safe.h" #include "BLI_utildefines.h" namespace blender::math { template inline constexpr bool is_math_float_type = std::is_floating_point_v; template inline constexpr bool is_math_integral_type = std::is_integral_v; template inline bool is_zero(const T &a) { return a == T(0); } template inline bool is_any_zero(const T &a) { return is_zero(a); } template inline T abs(const T &a) { return std::abs(a); } template inline T min(const T &a, const T &b) { return std::min(a, b); } template inline T max(const T &a, const T &b) { return std::max(a, b); } template inline void max_inplace(T &a, const T &b) { a = math::max(a, b); } template inline void min_inplace(T &a, const T &b) { a = math::min(a, b); } template inline T clamp(const T &a, const T &min, const T &max) { return std::clamp(a, min, max); } template))> inline T mod(const T &a, const T &b) { return std::fmod(a, b); } template))> inline T safe_mod(const T &a, const T &b) { return (b != 0) ? std::fmod(a, b) : 0; } template inline void min_max(const T &value, T &min, T &max) { min = math::min(value, min); max = math::max(value, max); } template))> inline T safe_divide(const T &a, const T &b) { return (b != 0) ? a / b : T(0.0f); } template))> inline T floor(const T &a) { return std::floor(a); } template))> inline T ceil(const T &a) { return std::ceil(a); } template inline T distance(const T &a, const T &b) { return std::abs(a - b); } template))> inline T fract(const T &a) { return a - std::floor(a); } template)), BLI_ENABLE_IF((is_math_float_type))> inline T interpolate(const T &a, const T &b, const FactorT &t) { auto result = a * (1 - t) + b * t; if constexpr (std::is_integral_v && std::is_floating_point_v) { result = std::round(result); } return result; } template inline T midpoint(const T &a, const T &b) { auto result = (a + b) * T(0.5); if constexpr (std::is_integral_v) { result = std::round(result); } return result; } } // namespace blender::math