mirror of
https://github.com/enduro2d/enduro2d.git
synced 2025-12-16 14:08:59 +07:00
just variadic min and max
This commit is contained in:
@@ -570,21 +570,59 @@ namespace e2d::math
|
|||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
// min/max/minmax
|
// min
|
||||||
//
|
//
|
||||||
|
|
||||||
|
template < typename T >
|
||||||
|
std::enable_if_t<std::is_arithmetic_v<T>, T>
|
||||||
|
min(T v) noexcept {
|
||||||
|
return v;
|
||||||
|
}
|
||||||
|
|
||||||
template < typename T >
|
template < typename T >
|
||||||
std::enable_if_t<std::is_arithmetic_v<T>, T>
|
std::enable_if_t<std::is_arithmetic_v<T>, T>
|
||||||
min(T l, T r) noexcept {
|
min(T l, T r) noexcept {
|
||||||
return l < r ? l : r;
|
return l < r ? l : r;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template < typename T, typename... Ts >
|
||||||
|
std::enable_if_t<std::is_arithmetic_v<T>, T>
|
||||||
|
min(T a, T b, T c, Ts... ts) noexcept {
|
||||||
|
return min(a < b ? a : b, c, ts...);
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// max
|
||||||
|
//
|
||||||
|
|
||||||
|
template < typename T >
|
||||||
|
std::enable_if_t<std::is_arithmetic_v<T>, T>
|
||||||
|
max(T v) noexcept {
|
||||||
|
return v;
|
||||||
|
}
|
||||||
|
|
||||||
template < typename T >
|
template < typename T >
|
||||||
std::enable_if_t<std::is_arithmetic_v<T>, T>
|
std::enable_if_t<std::is_arithmetic_v<T>, T>
|
||||||
max(T l, T r) noexcept {
|
max(T l, T r) noexcept {
|
||||||
return l < r ? r : l;
|
return l < r ? r : l;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template < typename T, typename... Ts >
|
||||||
|
std::enable_if_t<std::is_arithmetic_v<T>, T>
|
||||||
|
max(T a, T b, T c, Ts... ts) noexcept {
|
||||||
|
return max(a < b ? b : a, c, ts...);
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// minmax
|
||||||
|
//
|
||||||
|
|
||||||
|
template < typename T >
|
||||||
|
std::enable_if_t<std::is_arithmetic_v<T>, std::pair<T,T>>
|
||||||
|
minmax(T v) noexcept {
|
||||||
|
return std::make_pair(v, v);
|
||||||
|
}
|
||||||
|
|
||||||
template < typename T >
|
template < typename T >
|
||||||
std::enable_if_t<std::is_arithmetic_v<T>, std::pair<T,T>>
|
std::enable_if_t<std::is_arithmetic_v<T>, std::pair<T,T>>
|
||||||
minmax(T l, T r) noexcept {
|
minmax(T l, T r) noexcept {
|
||||||
@@ -593,6 +631,14 @@ namespace e2d::math
|
|||||||
: std::make_pair(r, l);
|
: std::make_pair(r, l);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template < typename T, typename... Ts >
|
||||||
|
std::enable_if_t<std::is_arithmetic_v<T>, std::pair<T,T>>
|
||||||
|
minmax(T a, T b, T c, Ts... ts) noexcept {
|
||||||
|
return std::make_pair(
|
||||||
|
min(a, b, c, ts...),
|
||||||
|
max(a, b, c, ts...));
|
||||||
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
// clamp/saturate
|
// clamp/saturate
|
||||||
//
|
//
|
||||||
|
|||||||
@@ -522,12 +522,12 @@ namespace e2d::math
|
|||||||
|
|
||||||
template < typename T >
|
template < typename T >
|
||||||
T minimum(const vec3<T>& v) noexcept {
|
T minimum(const vec3<T>& v) noexcept {
|
||||||
return math::min(math::min(v.x, v.y), v.z);
|
return math::min(v.x, v.y, v.z);
|
||||||
}
|
}
|
||||||
|
|
||||||
template < typename T >
|
template < typename T >
|
||||||
T maximum(const vec3<T>& v) noexcept {
|
T maximum(const vec3<T>& v) noexcept {
|
||||||
return math::max(math::max(v.x, v.y), v.z);
|
return math::max(v.x, v.y, v.z);
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
|
|||||||
@@ -553,12 +553,12 @@ namespace e2d::math
|
|||||||
|
|
||||||
template < typename T >
|
template < typename T >
|
||||||
T minimum(const vec4<T>& v) noexcept {
|
T minimum(const vec4<T>& v) noexcept {
|
||||||
return math::min(math::min(math::min(v.x, v.y), v.z), v.w);
|
return math::min(v.x, v.y, v.z, v.w);
|
||||||
}
|
}
|
||||||
|
|
||||||
template < typename T >
|
template < typename T >
|
||||||
T maximum(const vec4<T>& v) noexcept {
|
T maximum(const vec4<T>& v) noexcept {
|
||||||
return math::max(math::max(math::max(v.x, v.y), v.z), v.w);
|
return math::max(v.x, v.y, v.z, v.w);
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
|
|||||||
@@ -271,11 +271,11 @@ namespace e2d::math
|
|||||||
//
|
//
|
||||||
|
|
||||||
f32 minimum(const color& c) noexcept {
|
f32 minimum(const color& c) noexcept {
|
||||||
return math::min(math::min(math::min(c.r, c.g), c.b), c.a);
|
return math::min(c.r, c.g, c.b, c.a);
|
||||||
}
|
}
|
||||||
|
|
||||||
f32 maximum(const color& c) noexcept {
|
f32 maximum(const color& c) noexcept {
|
||||||
return math::max(math::max(math::max(c.r, c.g), c.b), c.a);
|
return math::max(c.r, c.g, c.b, c.a);
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
@@ -322,17 +322,17 @@ namespace e2d::colors
|
|||||||
{
|
{
|
||||||
u32 pack_color(const color& c) noexcept {
|
u32 pack_color(const color& c) noexcept {
|
||||||
return
|
return
|
||||||
math::numeric_cast<u32>(math::round(math::saturate(c.a) * 255.f)) << 24 |
|
static_cast<u32>(math::saturate(c.a) * 255.f + 0.5f) << 24 |
|
||||||
math::numeric_cast<u32>(math::round(math::saturate(c.r) * 255.f)) << 16 |
|
static_cast<u32>(math::saturate(c.r) * 255.f + 0.5f) << 16 |
|
||||||
math::numeric_cast<u32>(math::round(math::saturate(c.g) * 255.f)) << 8 |
|
static_cast<u32>(math::saturate(c.g) * 255.f + 0.5f) << 8 |
|
||||||
math::numeric_cast<u32>(math::round(math::saturate(c.b) * 255.f)) << 0;
|
static_cast<u32>(math::saturate(c.b) * 255.f + 0.5f) << 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
color unpack_color(u32 argb) noexcept {
|
color unpack_color(u32 argb) noexcept {
|
||||||
return color(
|
return color(
|
||||||
math::numeric_cast<u8>((argb >> 16) & 0xFF) / 255.f,
|
static_cast<u8>((argb >> 16) & 0xFF) / 255.f,
|
||||||
math::numeric_cast<u8>((argb >> 8) & 0xFF) / 255.f,
|
static_cast<u8>((argb >> 8) & 0xFF) / 255.f,
|
||||||
math::numeric_cast<u8>((argb >> 0) & 0xFF) / 255.f,
|
static_cast<u8>((argb >> 0) & 0xFF) / 255.f,
|
||||||
math::numeric_cast<u8>((argb >> 24) & 0xFF) / 255.f);
|
static_cast<u8>((argb >> 24) & 0xFF) / 255.f);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -55,10 +55,10 @@ namespace e2d
|
|||||||
}
|
}
|
||||||
|
|
||||||
color32::color32(const color& other) noexcept
|
color32::color32(const color& other) noexcept
|
||||||
: r(u8(other.r * 255.0f + 0.5f))
|
: r(static_cast<u8>(math::saturate(other.r) * 255.f + 0.5f))
|
||||||
, g(u8(other.g * 255.0f + 0.5f))
|
, g(static_cast<u8>(math::saturate(other.g) * 255.f + 0.5f))
|
||||||
, b(u8(other.b * 255.0f + 0.5f))
|
, b(static_cast<u8>(math::saturate(other.b) * 255.f + 0.5f))
|
||||||
, a(u8(other.a * 255.0f + 0.5f)) {}
|
, a(static_cast<u8>(math::saturate(other.a) * 255.f + 0.5f)) {}
|
||||||
|
|
||||||
color32::color32(u8 nr, u8 ng, u8 nb, u8 na) noexcept
|
color32::color32(u8 nr, u8 ng, u8 nb, u8 na) noexcept
|
||||||
: r(nr)
|
: r(nr)
|
||||||
@@ -271,11 +271,11 @@ namespace e2d::math
|
|||||||
//
|
//
|
||||||
|
|
||||||
u8 minimum(const color32& c) noexcept {
|
u8 minimum(const color32& c) noexcept {
|
||||||
return math::min(math::min(math::min(c.r, c.g), c.b), c.a);
|
return math::min(c.r, c.g, c.b, c.a);
|
||||||
}
|
}
|
||||||
|
|
||||||
u8 maximum(const color32& c) noexcept {
|
u8 maximum(const color32& c) noexcept {
|
||||||
return math::max(math::max(math::max(c.r, c.g), c.b), c.a);
|
return math::max(c.r, c.g, c.b, c.a);
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
@@ -322,17 +322,17 @@ namespace e2d::colors
|
|||||||
{
|
{
|
||||||
u32 pack_color32(const color32& c) noexcept {
|
u32 pack_color32(const color32& c) noexcept {
|
||||||
return
|
return
|
||||||
math::numeric_cast<u32>(c.a) << 24 |
|
static_cast<u32>(c.a) << 24 |
|
||||||
math::numeric_cast<u32>(c.r) << 16 |
|
static_cast<u32>(c.r) << 16 |
|
||||||
math::numeric_cast<u32>(c.g) << 8 |
|
static_cast<u32>(c.g) << 8 |
|
||||||
math::numeric_cast<u32>(c.b) << 0;
|
static_cast<u32>(c.b) << 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
color32 unpack_color32(u32 argb) noexcept {
|
color32 unpack_color32(u32 argb) noexcept {
|
||||||
return color32(
|
return color32(
|
||||||
math::numeric_cast<u8>((argb >> 16) & 0xFF),
|
static_cast<u8>((argb >> 16) & 0xFF),
|
||||||
math::numeric_cast<u8>((argb >> 8) & 0xFF),
|
static_cast<u8>((argb >> 8) & 0xFF),
|
||||||
math::numeric_cast<u8>((argb >> 0) & 0xFF),
|
static_cast<u8>((argb >> 0) & 0xFF),
|
||||||
math::numeric_cast<u8>((argb >> 24) & 0xFF));
|
static_cast<u8>((argb >> 24) & 0xFF));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -462,17 +462,58 @@ TEST_CASE("math") {
|
|||||||
REQUIRE(math::approximately(math::round(-0.6f), -1.f));
|
REQUIRE(math::approximately(math::round(-0.6f), -1.f));
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
|
REQUIRE(math::min(1) == 1);
|
||||||
|
|
||||||
REQUIRE(math::min(1,1) == 1);
|
REQUIRE(math::min(1,1) == 1);
|
||||||
REQUIRE(math::min(1,2) == 1);
|
REQUIRE(math::min(1,2) == 1);
|
||||||
REQUIRE(math::min(2,1) == 1);
|
REQUIRE(math::min(2,1) == 1);
|
||||||
|
|
||||||
|
REQUIRE(math::min(1,1,1) == 1);
|
||||||
|
REQUIRE(math::min(2,1,1) == 1);
|
||||||
|
REQUIRE(math::min(1,2,1) == 1);
|
||||||
|
REQUIRE(math::min(1,1,2) == 1);
|
||||||
|
REQUIRE(math::min(2,3,4) == 2);
|
||||||
|
REQUIRE(math::min(2,4,3) == 2);
|
||||||
|
|
||||||
|
REQUIRE(math::min(2,3,4,5) == 2);
|
||||||
|
REQUIRE(math::min(2,4,3,5) == 2);
|
||||||
|
REQUIRE(math::min(5,3,4,2) == 2);
|
||||||
|
REQUIRE(math::min(5,4,3,2) == 2);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
|
||||||
|
REQUIRE(math::max(1) == 1);
|
||||||
|
|
||||||
REQUIRE(math::max(1,1) == 1);
|
REQUIRE(math::max(1,1) == 1);
|
||||||
REQUIRE(math::max(1,2) == 2);
|
REQUIRE(math::max(1,2) == 2);
|
||||||
REQUIRE(math::max(2,1) == 2);
|
REQUIRE(math::max(2,1) == 2);
|
||||||
|
|
||||||
|
REQUIRE(math::max(1,1,1) == 1);
|
||||||
|
REQUIRE(math::max(2,1,1) == 2);
|
||||||
|
REQUIRE(math::max(1,2,1) == 2);
|
||||||
|
REQUIRE(math::max(1,1,2) == 2);
|
||||||
|
REQUIRE(math::max(2,3,4) == 4);
|
||||||
|
|
||||||
|
REQUIRE(math::max(2,3,4,5) == 5);
|
||||||
|
REQUIRE(math::max(2,4,3,5) == 5);
|
||||||
|
REQUIRE(math::max(5,3,4,2) == 5);
|
||||||
|
REQUIRE(math::max(5,4,3,2) == 5);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
REQUIRE(math::minmax(1) == std::make_pair(1,1));
|
||||||
|
|
||||||
REQUIRE(math::minmax(1,2) == std::make_pair(1,2));
|
REQUIRE(math::minmax(1,2) == std::make_pair(1,2));
|
||||||
REQUIRE(math::minmax(2,1) == std::make_pair(1,2));
|
REQUIRE(math::minmax(2,1) == std::make_pair(1,2));
|
||||||
|
|
||||||
|
REQUIRE(math::minmax(1,2,3) == std::make_pair(1,3));
|
||||||
|
REQUIRE(math::minmax(3,2,1) == std::make_pair(1,3));
|
||||||
|
|
||||||
|
REQUIRE(math::minmax(1,2,3,4) == std::make_pair(1,4));
|
||||||
|
REQUIRE(math::minmax(1,3,2,4) == std::make_pair(1,4));
|
||||||
|
REQUIRE(math::minmax(4,2,3,1) == std::make_pair(1,4));
|
||||||
|
REQUIRE(math::minmax(4,3,2,1) == std::make_pair(1,4));
|
||||||
|
}
|
||||||
|
{
|
||||||
REQUIRE(math::clamp(2,1,3) == 2);
|
REQUIRE(math::clamp(2,1,3) == 2);
|
||||||
REQUIRE(math::clamp(3,1,3) == 3);
|
REQUIRE(math::clamp(3,1,3) == 3);
|
||||||
REQUIRE(math::clamp(4,1,3) == 3);
|
REQUIRE(math::clamp(4,1,3) == 3);
|
||||||
|
|||||||
@@ -135,5 +135,8 @@ TEST_CASE("color") {
|
|||||||
REQUIRE(colors::pack_color(color(color32(0x12,0x34,0x56,0x78))) == 0x78123456);
|
REQUIRE(colors::pack_color(color(color32(0x12,0x34,0x56,0x78))) == 0x78123456);
|
||||||
REQUIRE(colors::unpack_color(0x04010203) == color(color32(1,2,3,4)));
|
REQUIRE(colors::unpack_color(0x04010203) == color(color32(1,2,3,4)));
|
||||||
REQUIRE(colors::unpack_color(0x78123456) == color(color32(0x12,0x34,0x56,0x78)));
|
REQUIRE(colors::unpack_color(0x78123456) == color(color32(0x12,0x34,0x56,0x78)));
|
||||||
|
|
||||||
|
REQUIRE(colors::pack_color(color(0.001f,0.001f,0.001f,0.001f)) == 0x00000000);
|
||||||
|
REQUIRE(colors::pack_color(color(0.999f,0.999f,0.999f,0.999f)) == 0xFFFFFFFF);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -127,5 +127,8 @@ TEST_CASE("color32") {
|
|||||||
REQUIRE(colors::pack_color32(color32(0x12,0x34,0x56,0x78)) == 0x78123456);
|
REQUIRE(colors::pack_color32(color32(0x12,0x34,0x56,0x78)) == 0x78123456);
|
||||||
REQUIRE(colors::unpack_color32(0x04010203) == color32(1,2,3,4));
|
REQUIRE(colors::unpack_color32(0x04010203) == color32(1,2,3,4));
|
||||||
REQUIRE(colors::unpack_color32(0x78123456) == color32(0x12,0x34,0x56,0x78));
|
REQUIRE(colors::unpack_color32(0x78123456) == color32(0x12,0x34,0x56,0x78));
|
||||||
|
|
||||||
|
REQUIRE(colors::pack_color32(color32(0x01,0x01,0x01,0x01)) == 0x01010101);
|
||||||
|
REQUIRE(colors::pack_color32(color32(0xFE,0xFE,0xFE,0xFE)) == 0xFEFEFEFE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user