mirror of
https://github.com/enduro2d/enduro2d.git
synced 2025-12-14 16:09:06 +07:00
math: string format and binds for ray2 and ray3
This commit is contained in:
@@ -181,6 +181,96 @@ namespace e2d::strings
|
||||
}
|
||||
};
|
||||
|
||||
//
|
||||
// ray2
|
||||
//
|
||||
|
||||
template < typename T >
|
||||
class format_arg<ray2<T>, std::enable_if_t<std::is_integral_v<T>>> {
|
||||
ray2<T> value_;
|
||||
u8 width_;
|
||||
public:
|
||||
template < typename U >
|
||||
explicit format_arg(U&& value, u8 width = 0) noexcept
|
||||
: value_(std::forward<U>(value)), width_(width) {}
|
||||
|
||||
std::ptrdiff_t write(char* dst, size_t size) const {
|
||||
return math::numeric_cast<std::ptrdiff_t>(
|
||||
format(dst, size, "(%0,%1,%2,%3)",
|
||||
make_format_arg(value_.origin.x, width_),
|
||||
make_format_arg(value_.origin.y, width_),
|
||||
make_format_arg(value_.direction.x, width_),
|
||||
make_format_arg(value_.direction.y, width_)));
|
||||
}
|
||||
};
|
||||
|
||||
template < typename T >
|
||||
class format_arg<ray2<T>, std::enable_if_t<std::is_floating_point_v<T>>> {
|
||||
ray2<T> value_;
|
||||
u8 width_;
|
||||
u8 precision_;
|
||||
public:
|
||||
template < typename U >
|
||||
explicit format_arg(U&& value, u8 width = 0, u8 precision = 6) noexcept
|
||||
: value_(std::forward<U>(value)), width_(width), precision_(precision) {}
|
||||
|
||||
std::ptrdiff_t write(char* dst, size_t size) const {
|
||||
return math::numeric_cast<std::ptrdiff_t>(
|
||||
format(dst, size, "(%0,%1,%2,%3)",
|
||||
make_format_arg(value_.origin.x, width_, precision_),
|
||||
make_format_arg(value_.origin.y, width_, precision_),
|
||||
make_format_arg(value_.direction.x, width_, precision_),
|
||||
make_format_arg(value_.direction.y, width_, precision_)));
|
||||
}
|
||||
};
|
||||
|
||||
//
|
||||
// ray3
|
||||
//
|
||||
|
||||
template < typename T >
|
||||
class format_arg<ray3<T>, std::enable_if_t<std::is_integral_v<T>>> {
|
||||
ray3<T> value_;
|
||||
u8 width_;
|
||||
public:
|
||||
template < typename U >
|
||||
explicit format_arg(U&& value, u8 width = 0) noexcept
|
||||
: value_(std::forward<U>(value)), width_(width) {}
|
||||
|
||||
std::ptrdiff_t write(char* dst, size_t size) const {
|
||||
return math::numeric_cast<std::ptrdiff_t>(
|
||||
format(dst, size, "(%0,%1,%2,%3,%4,%5)",
|
||||
make_format_arg(value_.origin.x, width_),
|
||||
make_format_arg(value_.origin.y, width_),
|
||||
make_format_arg(value_.origin.z, width_),
|
||||
make_format_arg(value_.direction.x, width_),
|
||||
make_format_arg(value_.direction.y, width_),
|
||||
make_format_arg(value_.direction.z, width_)));
|
||||
}
|
||||
};
|
||||
|
||||
template < typename T >
|
||||
class format_arg<ray3<T>, std::enable_if_t<std::is_floating_point_v<T>>> {
|
||||
ray3<T> value_;
|
||||
u8 width_;
|
||||
u8 precision_;
|
||||
public:
|
||||
template < typename U >
|
||||
explicit format_arg(U&& value, u8 width = 0, u8 precision = 6) noexcept
|
||||
: value_(std::forward<U>(value)), width_(width), precision_(precision) {}
|
||||
|
||||
std::ptrdiff_t write(char* dst, size_t size) const {
|
||||
return math::numeric_cast<std::ptrdiff_t>(
|
||||
format(dst, size, "(%0,%1,%2,%3,%4,%5)",
|
||||
make_format_arg(value_.origin.x, width_, precision_),
|
||||
make_format_arg(value_.origin.y, width_, precision_),
|
||||
make_format_arg(value_.origin.z, width_, precision_),
|
||||
make_format_arg(value_.direction.x, width_, precision_),
|
||||
make_format_arg(value_.direction.y, width_, precision_),
|
||||
make_format_arg(value_.direction.z, width_, precision_)));
|
||||
}
|
||||
};
|
||||
|
||||
//
|
||||
// rect
|
||||
//
|
||||
|
||||
38
samples/bin/library/scripts/emmy/math/ray2.lua
Normal file
38
samples/bin/library/scripts/emmy/math/ray2.lua
Normal file
@@ -0,0 +1,38 @@
|
||||
---@class ray2
|
||||
local ray2 = {
|
||||
---@type v2f
|
||||
origin = v2f.zero(),
|
||||
|
||||
---@type v2f
|
||||
direction = v2f.zero()
|
||||
}
|
||||
|
||||
---@overload fun(): ray2
|
||||
---@overload fun(r: ray2): ray2
|
||||
---@overload fun(dx: number, dy: number): ray2
|
||||
---@overload fun(ox: number, oy: number, dx: number, dy: number): ray2
|
||||
---@overload fun(d: v2f): ray2
|
||||
---@overload fun(o: v2f, d: v2f): ray2
|
||||
---@return ray2
|
||||
function ray2.new(...) end
|
||||
|
||||
---@return ray2
|
||||
function ray2.zero() end
|
||||
|
||||
---@return ray2
|
||||
function ray2.unit_x() end
|
||||
|
||||
---@return ray2
|
||||
function ray2.unit_y() end
|
||||
|
||||
---@param l ray2
|
||||
---@param r ray2
|
||||
---@return boolean
|
||||
function ray2.approximately(l, r) end
|
||||
|
||||
---@param r ray2
|
||||
---@return boolean
|
||||
function ray2.contains_nan(r) end
|
||||
|
||||
---@type ray2
|
||||
_G.ray2 = _G.ray2 or ray2
|
||||
41
samples/bin/library/scripts/emmy/math/ray3.lua
Normal file
41
samples/bin/library/scripts/emmy/math/ray3.lua
Normal file
@@ -0,0 +1,41 @@
|
||||
---@class ray3
|
||||
local ray3 = {
|
||||
---@type v3f
|
||||
origin = v3f.zero(),
|
||||
|
||||
---@type v3f
|
||||
direction = v3f.zero()
|
||||
}
|
||||
|
||||
---@overload fun(): ray3
|
||||
---@overload fun(r: ray3): ray3
|
||||
---@overload fun(dx: number, dy: number, dz: number): ray2
|
||||
---@overload fun(ox: number, oy: number, oz: number, dx: number, dy: number, dz: number): ray2
|
||||
---@overload fun(d: v3f): ray3
|
||||
---@overload fun(o: v3f, d: v3f): ray3
|
||||
---@return ray3
|
||||
function ray3.new(...) end
|
||||
|
||||
---@return ray3
|
||||
function ray3.zero() end
|
||||
|
||||
---@return ray3
|
||||
function ray3.unit_x() end
|
||||
|
||||
---@return ray3
|
||||
function ray3.unit_y() end
|
||||
|
||||
---@return ray3
|
||||
function ray3.unit_z() end
|
||||
|
||||
---@param l ray3
|
||||
---@param r ray3
|
||||
---@return boolean
|
||||
function ray3.approximately(l, r) end
|
||||
|
||||
---@param r ray3
|
||||
---@return boolean
|
||||
function ray3.contains_nan(r) end
|
||||
|
||||
---@type ray3
|
||||
_G.ray3 = _G.ray3 or ray3
|
||||
@@ -20,6 +20,9 @@ namespace e2d::bindings::math
|
||||
|
||||
void bind_quat(sol::state& l);
|
||||
|
||||
void bind_ray2(sol::state& l);
|
||||
void bind_ray3(sol::state& l);
|
||||
|
||||
void bind_rect(sol::state& l);
|
||||
void bind_aabb(sol::state& l);
|
||||
|
||||
@@ -40,6 +43,9 @@ namespace e2d::bindings
|
||||
|
||||
math::bind_quat(l);
|
||||
|
||||
math::bind_ray2(l);
|
||||
math::bind_ray3(l);
|
||||
|
||||
math::bind_rect(l);
|
||||
math::bind_aabb(l);
|
||||
|
||||
|
||||
64
sources/enduro2d/high/bindings/math_binds/ray2_binds.cpp
Normal file
64
sources/enduro2d/high/bindings/math_binds/ray2_binds.cpp
Normal file
@@ -0,0 +1,64 @@
|
||||
/*******************************************************************************
|
||||
* This file is part of the "Enduro2D"
|
||||
* For conditions of distribution and use, see copyright notice in LICENSE.md
|
||||
* Copyright (C) 2018-2019, by Matvey Cherevko (blackmatov@gmail.com)
|
||||
******************************************************************************/
|
||||
|
||||
#include "_math_binds.hpp"
|
||||
|
||||
namespace
|
||||
{
|
||||
using namespace e2d;
|
||||
|
||||
template < typename T >
|
||||
void bind_ray2_t(const str& name, sol::state& l) {
|
||||
l.new_usertype<ray2<T>>(name,
|
||||
sol::constructors<
|
||||
ray2<T>(),
|
||||
ray2<T>(ray2<T>),
|
||||
ray2<T>(T,T),
|
||||
ray2<T>(T,T,T,T),
|
||||
ray2<T>(vec2<T>),
|
||||
ray2<T>(vec2<T>,vec2<T>)>(),
|
||||
|
||||
"zero", &ray2<T>::zero,
|
||||
"unit_x", &ray2<T>::unit_x,
|
||||
"unit_y", &ray2<T>::unit_y,
|
||||
|
||||
"origin", &ray2<T>::origin,
|
||||
"direction", &ray2<T>::direction,
|
||||
|
||||
sol::meta_function::to_string, [](const ray2<T>& v){
|
||||
return strings::rformat("%0", v);
|
||||
},
|
||||
|
||||
sol::meta_function::equal_to, sol::resolve<bool(const ray2<T>&, const ray2<T>&)>(::operator==),
|
||||
|
||||
sol::meta_function::addition, sol::overload(
|
||||
sol::resolve<ray2<T>(const ray2<T>&, T)>(::operator+),
|
||||
sol::resolve<ray2<T>(const ray2<T>&, const vec2<T>&)>(::operator+)),
|
||||
|
||||
sol::meta_function::subtraction, sol::overload(
|
||||
sol::resolve<ray2<T>(const ray2<T>&, T)>(::operator-),
|
||||
sol::resolve<ray2<T>(const ray2<T>&, const vec2<T>&)>(::operator-)),
|
||||
|
||||
sol::meta_function::multiplication, sol::overload(
|
||||
sol::resolve<ray2<T>(const ray2<T>&, T)>(::operator*),
|
||||
sol::resolve<ray2<T>(const ray2<T>&, const vec2<T>&)>(::operator*)),
|
||||
|
||||
sol::meta_function::division, sol::overload(
|
||||
sol::resolve<ray2<T>(const ray2<T>&, T)>(::operator/),
|
||||
sol::resolve<ray2<T>(const ray2<T>&, const vec2<T>&)>(::operator/)),
|
||||
|
||||
"approximately", [](const ray2<T>& l, const ray2<T>& r){ return math::approximately(l,r); },
|
||||
|
||||
"contains_nan", sol::resolve<bool(const ray2<T>&)>(&math::contains_nan));
|
||||
}
|
||||
}
|
||||
|
||||
namespace e2d::bindings::math
|
||||
{
|
||||
void bind_ray2(sol::state& l) {
|
||||
bind_ray2_t<f32>("r2f", l);
|
||||
}
|
||||
}
|
||||
65
sources/enduro2d/high/bindings/math_binds/ray3_binds.cpp
Normal file
65
sources/enduro2d/high/bindings/math_binds/ray3_binds.cpp
Normal file
@@ -0,0 +1,65 @@
|
||||
/*******************************************************************************
|
||||
* This file is part of the "Enduro2D"
|
||||
* For conditions of distribution and use, see copyright notice in LICENSE.md
|
||||
* Copyright (C) 2018-2019, by Matvey Cherevko (blackmatov@gmail.com)
|
||||
******************************************************************************/
|
||||
|
||||
#include "_math_binds.hpp"
|
||||
|
||||
namespace
|
||||
{
|
||||
using namespace e2d;
|
||||
|
||||
template < typename T >
|
||||
void bind_ray3_t(const str& name, sol::state& l) {
|
||||
l.new_usertype<ray3<T>>(name,
|
||||
sol::constructors<
|
||||
ray3<T>(),
|
||||
ray3<T>(ray3<T>),
|
||||
ray3<T>(T,T,T),
|
||||
ray3<T>(T,T,T,T,T,T),
|
||||
ray3<T>(vec3<T>),
|
||||
ray3<T>(vec3<T>,vec3<T>)>(),
|
||||
|
||||
"zero", &ray3<T>::zero,
|
||||
"unit", &ray3<T>::unit_x,
|
||||
"unit_y", &ray3<T>::unit_y,
|
||||
"unit_z", &ray3<T>::unit_z,
|
||||
|
||||
"origin", &ray3<T>::origin,
|
||||
"direction", &ray3<T>::direction,
|
||||
|
||||
sol::meta_function::to_string, [](const ray3<T>& v){
|
||||
return strings::rformat("%0", v);
|
||||
},
|
||||
|
||||
sol::meta_function::equal_to, sol::resolve<bool(const ray3<T>&, const ray3<T>&)>(::operator==),
|
||||
|
||||
sol::meta_function::addition, sol::overload(
|
||||
sol::resolve<ray3<T>(const ray3<T>&, T)>(::operator+),
|
||||
sol::resolve<ray3<T>(const ray3<T>&, const vec3<T>&)>(::operator+)),
|
||||
|
||||
sol::meta_function::subtraction, sol::overload(
|
||||
sol::resolve<ray3<T>(const ray3<T>&, T)>(::operator-),
|
||||
sol::resolve<ray3<T>(const ray3<T>&, const vec3<T>&)>(::operator-)),
|
||||
|
||||
sol::meta_function::multiplication, sol::overload(
|
||||
sol::resolve<ray3<T>(const ray3<T>&, T)>(::operator*),
|
||||
sol::resolve<ray3<T>(const ray3<T>&, const vec3<T>&)>(::operator*)),
|
||||
|
||||
sol::meta_function::division, sol::overload(
|
||||
sol::resolve<ray3<T>(const ray3<T>&, T)>(::operator/),
|
||||
sol::resolve<ray3<T>(const ray3<T>&, const vec3<T>&)>(::operator/)),
|
||||
|
||||
"approximately", [](const ray3<T>& l, const ray3<T>& r){ return math::approximately(l,r); },
|
||||
|
||||
"contains_nan", sol::resolve<bool(const ray3<T>&)>(&math::contains_nan));
|
||||
}
|
||||
}
|
||||
|
||||
namespace e2d::bindings::math
|
||||
{
|
||||
void bind_ray3(sol::state& l) {
|
||||
bind_ray3_t<f32>("r3f", l);
|
||||
}
|
||||
}
|
||||
@@ -47,8 +47,8 @@ TEST_CASE("ray2") {
|
||||
REQUIRE(r2i(1,2).origin == v2i(0,0));
|
||||
REQUIRE(r2i(1,2).direction == v2i(1,2));
|
||||
|
||||
REQUIRE(r2i({1,2}).origin == v2i(0,0));
|
||||
REQUIRE(r2i({1,2}).direction == v2i(1,2));
|
||||
REQUIRE(r2i(v2i{1,2}).origin == v2i(0,0));
|
||||
REQUIRE(r2i(v2i{1,2}).direction == v2i(1,2));
|
||||
|
||||
REQUIRE(r2i(1,2,3,4).origin == v2i(1,2));
|
||||
REQUIRE(r2i(1,2,3,4).direction == v2i(3,4));
|
||||
@@ -140,7 +140,7 @@ TEST_CASE("ray2") {
|
||||
{
|
||||
REQUIRE_FALSE(math::contains_nan(r2i({1,2},{3,4})));
|
||||
REQUIRE_FALSE(math::contains_nan(r2f({1.f,2.f},{3.f,4.f})));
|
||||
REQUIRE(math::contains_nan(r2f({1.f,std::numeric_limits<f32>::quiet_NaN()})));
|
||||
REQUIRE(math::contains_nan(r2f({std::numeric_limits<f32>::infinity(), 1.f})));
|
||||
REQUIRE(math::contains_nan(r2f(1.f,std::numeric_limits<f32>::quiet_NaN())));
|
||||
REQUIRE(math::contains_nan(r2f(std::numeric_limits<f32>::infinity(), 1.f)));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -48,8 +48,8 @@ TEST_CASE("ray3") {
|
||||
REQUIRE(r3i(1,2,3).origin == v3i(0,0,0));
|
||||
REQUIRE(r3i(1,2,3).direction == v3i(1,2,3));
|
||||
|
||||
REQUIRE(r3i({1,2,3}).origin == v3i(0,0,0));
|
||||
REQUIRE(r3i({1,2,3}).direction == v3i(1,2,3));
|
||||
REQUIRE(r3i(v3i{1,2,3}).origin == v3i(0,0,0));
|
||||
REQUIRE(r3i(v3i{1,2,3}).direction == v3i(1,2,3));
|
||||
|
||||
REQUIRE(r3i(1,2,3,3,4,5).origin == v3i(1,2,3));
|
||||
REQUIRE(r3i(1,2,3,3,4,5).direction == v3i(3,4,5));
|
||||
@@ -149,7 +149,7 @@ TEST_CASE("ray3") {
|
||||
{
|
||||
REQUIRE_FALSE(math::contains_nan(r3i({1,2,3},{3,4,5})));
|
||||
REQUIRE_FALSE(math::contains_nan(r3f({1.f,2.f,3.f},{3.f,4.f,5.f})));
|
||||
REQUIRE(math::contains_nan(r3f({1.f,2.f,std::numeric_limits<f32>::quiet_NaN()})));
|
||||
REQUIRE(math::contains_nan(r3f({std::numeric_limits<f32>::infinity(), 1.f,2.f})));
|
||||
REQUIRE(math::contains_nan(r3f(1.f,2.f,std::numeric_limits<f32>::quiet_NaN())));
|
||||
REQUIRE(math::contains_nan(r3f(std::numeric_limits<f32>::infinity(), 1.f,2.f)));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -156,10 +156,20 @@ TEST_CASE("strfmts") {
|
||||
" 4.00us");
|
||||
}
|
||||
{
|
||||
REQUIRE(strings::rformat("%0", make_ray2(v2i{1,2},v2i{3,4})) == "(1,2,3,4)");
|
||||
REQUIRE(strings::rformat("%0", make_ray3(v3i{1,2,3},v3i{4,5,6})) == "(1,2,3,4,5,6)");
|
||||
|
||||
REQUIRE(strings::rformat("%0", make_rect(1,2,3,4)) == "(1,2,3,4)");
|
||||
REQUIRE(strings::rformat("%0", make_quat(1,2,3,4)) == "(1,2,3,4)");
|
||||
REQUIRE(strings::rformat("%0", make_aabb(1,2,3,4,5,6)) == "(1,2,3,4,5,6)");
|
||||
|
||||
REQUIRE(strings::rformat(
|
||||
"%0",
|
||||
strings::make_format_arg(make_ray2(v2f{1.f,2.f},v2f{3.f,4.f}), u8(5), u8(2))) == "( 1.00, 2.00, 3.00, 4.00)");
|
||||
REQUIRE(strings::rformat(
|
||||
"%0",
|
||||
strings::make_format_arg(make_ray3(v3f{1.f,2.f,3.f},v3f{4.f,5.f,6.f}), u8(5), u8(2))) == "( 1.00, 2.00, 3.00, 4.00, 5.00, 6.00)");
|
||||
|
||||
REQUIRE(strings::rformat(
|
||||
"%0",
|
||||
strings::make_format_arg(make_rect(1.f,2.f,3.f,4.f), u8(5), u8(2))) == "( 1.00, 2.00, 3.00, 4.00)");
|
||||
|
||||
Reference in New Issue
Block a user