mirror of
https://github.com/enduro2d/enduro2d.git
synced 2025-12-15 00:11:55 +07:00
add rect and aabb bindings
This commit is contained in:
@@ -23,18 +23,18 @@ namespace e2d
|
||||
vec3<T> position;
|
||||
vec3<T> size;
|
||||
public:
|
||||
static const aabb& zero() noexcept;
|
||||
static const aabb& unit() noexcept;
|
||||
static constexpr aabb zero() noexcept;
|
||||
static constexpr aabb unit() noexcept;
|
||||
public:
|
||||
aabb() noexcept = default;
|
||||
aabb(const aabb& other) noexcept = default;
|
||||
aabb& operator=(const aabb& other) noexcept = default;
|
||||
constexpr aabb() noexcept = default;
|
||||
constexpr aabb(const aabb& other) noexcept = default;
|
||||
constexpr aabb& operator=(const aabb& other) noexcept = default;
|
||||
|
||||
aabb(T w, T h, T l) noexcept;
|
||||
aabb(T x, T y, T z, T w, T h, T l) noexcept;
|
||||
constexpr aabb(T w, T h, T l) noexcept;
|
||||
constexpr aabb(T x, T y, T z, T w, T h, T l) noexcept;
|
||||
|
||||
aabb(const vec3<T>& nsize) noexcept;
|
||||
aabb(const vec3<T>& nposition, const vec3<T>& nsize) noexcept;
|
||||
constexpr aabb(const vec3<T>& nsize) noexcept;
|
||||
constexpr aabb(const vec3<T>& nposition, const vec3<T>& nsize) noexcept;
|
||||
|
||||
template < typename To >
|
||||
aabb<To> cast_to() const noexcept;
|
||||
@@ -60,32 +60,30 @@ namespace e2d
|
||||
namespace e2d
|
||||
{
|
||||
template < typename T >
|
||||
const aabb<T>& aabb<T>::zero() noexcept {
|
||||
static const aabb<T> zero{0, 0, 0, 0, 0, 0};
|
||||
return zero;
|
||||
constexpr aabb<T> aabb<T>::zero() noexcept {
|
||||
return {0, 0, 0, 0, 0, 0};
|
||||
}
|
||||
|
||||
template < typename T >
|
||||
const aabb<T>& aabb<T>::unit() noexcept {
|
||||
static const aabb<T> unit{0, 0, 0, 1, 1, 1};
|
||||
return unit;
|
||||
constexpr aabb<T> aabb<T>::unit() noexcept {
|
||||
return {0, 0, 0, 1, 1, 1};
|
||||
}
|
||||
|
||||
template < typename T >
|
||||
aabb<T>::aabb(T w, T h, T l) noexcept
|
||||
constexpr aabb<T>::aabb(T w, T h, T l) noexcept
|
||||
: size(w, h, l) {}
|
||||
|
||||
template < typename T >
|
||||
aabb<T>::aabb(T x, T y, T z, T w, T h, T l) noexcept
|
||||
constexpr aabb<T>::aabb(T x, T y, T z, T w, T h, T l) noexcept
|
||||
: position(x, y, z)
|
||||
, size(w, h, l) {}
|
||||
|
||||
template < typename T >
|
||||
aabb<T>::aabb(const vec3<T>& nsize) noexcept
|
||||
constexpr aabb<T>::aabb(const vec3<T>& nsize) noexcept
|
||||
: size(nsize) {}
|
||||
|
||||
template < typename T >
|
||||
aabb<T>::aabb(const vec3<T>& nposition, const vec3<T>& nsize) noexcept
|
||||
constexpr aabb<T>::aabb(const vec3<T>& nposition, const vec3<T>& nsize) noexcept
|
||||
: position(nposition)
|
||||
, size(nsize) {}
|
||||
|
||||
@@ -175,22 +173,22 @@ namespace e2d
|
||||
//
|
||||
|
||||
template < typename T >
|
||||
aabb<T> make_aabb(T w, T h, T l) noexcept {
|
||||
constexpr aabb<T> make_aabb(T w, T h, T l) noexcept {
|
||||
return {w, h, l};
|
||||
}
|
||||
|
||||
template < typename T >
|
||||
aabb<T> make_aabb(T x, T y, T z, T w, T h, T l) noexcept {
|
||||
constexpr aabb<T> make_aabb(T x, T y, T z, T w, T h, T l) noexcept {
|
||||
return {x, y, z, w, h, l};
|
||||
}
|
||||
|
||||
template < typename T >
|
||||
aabb<T> make_aabb(const vec3<T>& size) noexcept {
|
||||
constexpr aabb<T> make_aabb(const vec3<T>& size) noexcept {
|
||||
return {size};
|
||||
}
|
||||
|
||||
template < typename T >
|
||||
aabb<T> make_aabb(const vec3<T>& position, const vec3<T>& size) noexcept {
|
||||
constexpr aabb<T> make_aabb(const vec3<T>& position, const vec3<T>& size) noexcept {
|
||||
return {position, size};
|
||||
}
|
||||
|
||||
|
||||
@@ -23,18 +23,18 @@ namespace e2d
|
||||
vec2<T> position;
|
||||
vec2<T> size;
|
||||
public:
|
||||
static const rect& zero() noexcept;
|
||||
static const rect& unit() noexcept;
|
||||
static constexpr rect zero() noexcept;
|
||||
static constexpr rect unit() noexcept;
|
||||
public:
|
||||
rect() noexcept = default;
|
||||
rect(const rect& other) noexcept = default;
|
||||
rect& operator=(const rect& other) noexcept = default;
|
||||
constexpr rect() noexcept = default;
|
||||
constexpr rect(const rect& other) noexcept = default;
|
||||
constexpr rect& operator=(const rect& other) noexcept = default;
|
||||
|
||||
rect(T w, T h) noexcept;
|
||||
rect(T x, T y, T w, T h) noexcept;
|
||||
constexpr rect(T w, T h) noexcept;
|
||||
constexpr rect(T x, T y, T w, T h) noexcept;
|
||||
|
||||
rect(const vec2<T>& nsize) noexcept;
|
||||
rect(const vec2<T>& nposition, const vec2<T>& nsize) noexcept;
|
||||
constexpr rect(const vec2<T>& nsize) noexcept;
|
||||
constexpr rect(const vec2<T>& nposition, const vec2<T>& nsize) noexcept;
|
||||
|
||||
template < typename To >
|
||||
rect<To> cast_to() const noexcept;
|
||||
@@ -60,32 +60,30 @@ namespace e2d
|
||||
namespace e2d
|
||||
{
|
||||
template < typename T >
|
||||
const rect<T>& rect<T>::zero() noexcept {
|
||||
static const rect<T> zero{0, 0, 0, 0};
|
||||
return zero;
|
||||
constexpr rect<T> rect<T>::zero() noexcept {
|
||||
return {0, 0, 0, 0};
|
||||
}
|
||||
|
||||
template < typename T >
|
||||
const rect<T>& rect<T>::unit() noexcept {
|
||||
static const rect<T> unit{0, 0, 1, 1};
|
||||
return unit;
|
||||
constexpr rect<T> rect<T>::unit() noexcept {
|
||||
return {0, 0, 1, 1};
|
||||
}
|
||||
|
||||
template < typename T >
|
||||
rect<T>::rect(T w, T h) noexcept
|
||||
constexpr rect<T>::rect(T w, T h) noexcept
|
||||
: size(w, h) {}
|
||||
|
||||
template < typename T >
|
||||
rect<T>::rect(T x, T y, T w, T h) noexcept
|
||||
constexpr rect<T>::rect(T x, T y, T w, T h) noexcept
|
||||
: position(x, y)
|
||||
, size(w, h) {}
|
||||
|
||||
template < typename T >
|
||||
rect<T>::rect(const vec2<T>& nsize) noexcept
|
||||
constexpr rect<T>::rect(const vec2<T>& nsize) noexcept
|
||||
: size(nsize) {}
|
||||
|
||||
template < typename T >
|
||||
rect<T>::rect(const vec2<T>& nposition, const vec2<T>& nsize) noexcept
|
||||
constexpr rect<T>::rect(const vec2<T>& nposition, const vec2<T>& nsize) noexcept
|
||||
: position(nposition)
|
||||
, size(nsize) {}
|
||||
|
||||
@@ -175,22 +173,22 @@ namespace e2d
|
||||
//
|
||||
|
||||
template < typename T >
|
||||
rect<T> make_rect(T w, T h) noexcept {
|
||||
constexpr rect<T> make_rect(T w, T h) noexcept {
|
||||
return {w, h};
|
||||
}
|
||||
|
||||
template < typename T >
|
||||
rect<T> make_rect(T x, T y, T w, T h) noexcept {
|
||||
constexpr rect<T> make_rect(T x, T y, T w, T h) noexcept {
|
||||
return {x, y, w, h};
|
||||
}
|
||||
|
||||
template < typename T >
|
||||
rect<T> make_rect(const vec2<T>& size) noexcept {
|
||||
constexpr rect<T> make_rect(const vec2<T>& size) noexcept {
|
||||
return {size};
|
||||
}
|
||||
|
||||
template < typename T >
|
||||
rect<T> make_rect(const vec2<T>& position, const vec2<T>& size) noexcept {
|
||||
constexpr rect<T> make_rect(const vec2<T>& position, const vec2<T>& size) noexcept {
|
||||
return {position, size};
|
||||
}
|
||||
|
||||
|
||||
@@ -483,6 +483,126 @@ namespace
|
||||
|
||||
"contains_nan", sol::resolve<bool(const mat4<T>&)>(&math::contains_nan));
|
||||
}
|
||||
|
||||
template < typename T >
|
||||
void bind_rect(const str& name, sol::state& l) {
|
||||
l.new_usertype<rect<T>>(name,
|
||||
sol::constructors<
|
||||
rect<T>(),
|
||||
rect<T>(rect<T>),
|
||||
rect<T>(T,T),
|
||||
rect<T>(T,T,T,T),
|
||||
rect<T>(vec2<T>),
|
||||
rect<T>(vec2<T>,vec2<T>)>(),
|
||||
|
||||
"zero", &rect<T>::zero,
|
||||
"unit", &rect<T>::unit,
|
||||
|
||||
"position", &rect<T>::position,
|
||||
"size", &rect<T>::size,
|
||||
|
||||
sol::meta_function::equal_to, sol::resolve<bool(const rect<T>&, const rect<T>&)>(::operator==),
|
||||
sol::meta_function::less_than, sol::resolve<bool(const rect<T>&, const rect<T>&)>(::operator<),
|
||||
sol::meta_function::less_than_or_equal_to, sol::resolve<bool(const rect<T>&, const rect<T>&)>(::operator<=),
|
||||
|
||||
sol::meta_function::addition, sol::overload(
|
||||
sol::resolve<rect<T>(const rect<T>&, T)>(::operator+),
|
||||
sol::resolve<rect<T>(const rect<T>&, const vec2<T>&)>(::operator+)),
|
||||
|
||||
sol::meta_function::subtraction, sol::overload(
|
||||
sol::resolve<rect<T>(const rect<T>&, T)>(::operator-),
|
||||
sol::resolve<rect<T>(const rect<T>&, const vec2<T>&)>(::operator-)),
|
||||
|
||||
sol::meta_function::multiplication, sol::overload(
|
||||
sol::resolve<rect<T>(const rect<T>&, T)>(::operator*),
|
||||
sol::resolve<rect<T>(const rect<T>&, const vec2<T>&)>(::operator*)),
|
||||
|
||||
sol::meta_function::division, sol::overload(
|
||||
sol::resolve<rect<T>(const rect<T>&, T)>(::operator/),
|
||||
sol::resolve<rect<T>(const rect<T>&, const vec2<T>&)>(::operator/)),
|
||||
|
||||
"make_minmax_rect", sol::overload(
|
||||
sol::resolve<rect<T>(T,T,T,T)>(&math::make_minmax_rect),
|
||||
sol::resolve<rect<T>(const vec2<T>&,const vec2<T>&)>(&math::make_minmax_rect),
|
||||
sol::resolve<rect<T>(const rect<T>&)>(&math::make_minmax_rect)),
|
||||
|
||||
"approximately", [](const rect<T>& l, const rect<T>& r){ return math::approximately(l,r); },
|
||||
|
||||
"minimum", sol::resolve<vec2<T>(const rect<T>&)>(&math::minimum),
|
||||
"maximum", sol::resolve<vec2<T>(const rect<T>&)>(&math::maximum),
|
||||
|
||||
"area", sol::resolve<T(const rect<T>&)>(&math::area),
|
||||
"abs_area", sol::resolve<T(const rect<T>&)>(&math::abs_area),
|
||||
|
||||
"merged", sol::resolve<rect<T>(const rect<T>&,const rect<T>&)>(&math::merged),
|
||||
"inside", sol::resolve<bool(const rect<T>&,const vec2<T>&)>(&math::inside),
|
||||
"overlaps", sol::resolve<bool(const rect<T>&,const rect<T>&)>(&math::overlaps),
|
||||
|
||||
"normalized_to_point", sol::resolve<vec2<T>(const rect<T>&,const vec2<T>&)>(&math::normalized_to_point),
|
||||
"point_to_normalized", sol::resolve<vec2<T>(const rect<T>&,const vec2<T>&)>(&math::point_to_normalized),
|
||||
|
||||
"contains_nan", sol::resolve<bool(const rect<T>&)>(&math::contains_nan));
|
||||
}
|
||||
|
||||
template < typename T >
|
||||
void bind_aabb(const str& name, sol::state& l) {
|
||||
l.new_usertype<aabb<T>>(name,
|
||||
sol::constructors<
|
||||
aabb<T>(),
|
||||
aabb<T>(aabb<T>),
|
||||
aabb<T>(T,T,T),
|
||||
aabb<T>(T,T,T,T,T,T),
|
||||
aabb<T>(vec3<T>),
|
||||
aabb<T>(vec3<T>,vec3<T>)>(),
|
||||
|
||||
"zero", &aabb<T>::zero,
|
||||
"unit", &aabb<T>::unit,
|
||||
|
||||
"position", &aabb<T>::position,
|
||||
"size", &aabb<T>::size,
|
||||
|
||||
sol::meta_function::equal_to, sol::resolve<bool(const aabb<T>&, const aabb<T>&)>(::operator==),
|
||||
sol::meta_function::less_than, sol::resolve<bool(const aabb<T>&, const aabb<T>&)>(::operator<),
|
||||
sol::meta_function::less_than_or_equal_to, sol::resolve<bool(const aabb<T>&, const aabb<T>&)>(::operator<=),
|
||||
|
||||
sol::meta_function::addition, sol::overload(
|
||||
sol::resolve<aabb<T>(const aabb<T>&, T)>(::operator+),
|
||||
sol::resolve<aabb<T>(const aabb<T>&, const vec3<T>&)>(::operator+)),
|
||||
|
||||
sol::meta_function::subtraction, sol::overload(
|
||||
sol::resolve<aabb<T>(const aabb<T>&, T)>(::operator-),
|
||||
sol::resolve<aabb<T>(const aabb<T>&, const vec3<T>&)>(::operator-)),
|
||||
|
||||
sol::meta_function::multiplication, sol::overload(
|
||||
sol::resolve<aabb<T>(const aabb<T>&, T)>(::operator*),
|
||||
sol::resolve<aabb<T>(const aabb<T>&, const vec3<T>&)>(::operator*)),
|
||||
|
||||
sol::meta_function::division, sol::overload(
|
||||
sol::resolve<aabb<T>(const aabb<T>&, T)>(::operator/),
|
||||
sol::resolve<aabb<T>(const aabb<T>&, const vec3<T>&)>(::operator/)),
|
||||
|
||||
"make_minmax_rect", sol::overload(
|
||||
sol::resolve<aabb<T>(T,T,T,T,T,T)>(&math::make_minmax_aabb),
|
||||
sol::resolve<aabb<T>(const vec3<T>&,const vec3<T>&)>(&math::make_minmax_aabb),
|
||||
sol::resolve<aabb<T>(const aabb<T>&)>(&math::make_minmax_aabb)),
|
||||
|
||||
"approximately", [](const aabb<T>& l, const aabb<T>& r){ return math::approximately(l,r); },
|
||||
|
||||
"minimum", sol::resolve<vec3<T>(const aabb<T>&)>(&math::minimum),
|
||||
"maximum", sol::resolve<vec3<T>(const aabb<T>&)>(&math::maximum),
|
||||
|
||||
"volume", sol::resolve<T(const aabb<T>&)>(&math::volume),
|
||||
"abs_volume", sol::resolve<T(const aabb<T>&)>(&math::abs_volume),
|
||||
|
||||
"merged", sol::resolve<aabb<T>(const aabb<T>&,const aabb<T>&)>(&math::merged),
|
||||
"inside", sol::resolve<bool(const aabb<T>&,const vec3<T>&)>(&math::inside),
|
||||
"overlaps", sol::resolve<bool(const aabb<T>&,const aabb<T>&)>(&math::overlaps),
|
||||
|
||||
"normalized_to_point", sol::resolve<vec3<T>(const aabb<T>&,const vec3<T>&)>(&math::normalized_to_point),
|
||||
"point_to_normalized", sol::resolve<vec3<T>(const aabb<T>&,const vec3<T>&)>(&math::point_to_normalized),
|
||||
|
||||
"contains_nan", sol::resolve<bool(const aabb<T>&)>(&math::contains_nan));
|
||||
}
|
||||
}
|
||||
|
||||
namespace e2d::bindings
|
||||
@@ -497,5 +617,7 @@ namespace e2d::bindings
|
||||
bind_mat2<f32>("m2f", l);
|
||||
bind_mat3<f32>("m3f", l);
|
||||
bind_mat4<f32>("m4f", l);
|
||||
bind_rect<f32>("b2f", l);
|
||||
bind_aabb<f32>("b3f", l);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -76,4 +76,17 @@ TEST_CASE("luasol") {
|
||||
REQUIRE(r2.second);
|
||||
REQUIRE(r2.first == math::inversed(math::make_translation_matrix4(2.f,3.f,4.f)).first);
|
||||
}
|
||||
|
||||
SECTION("rect/aabb") {
|
||||
bool r0 = l.lua().script(R"lua(
|
||||
local b = b2f.unit() * 2
|
||||
return b:inside(v2f.new(1.5,1.5))
|
||||
)lua");
|
||||
REQUIRE(r0);
|
||||
bool r1 = l.lua().script(R"lua(
|
||||
local b = b3f.unit() * 2
|
||||
return b:overlaps(b3f.new(1.5,1.5,1.5,2,2,2))
|
||||
)lua");
|
||||
REQUIRE(r1);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user