diff --git a/headers/enduro2d/math/trs2.hpp b/headers/enduro2d/math/trs2.hpp index 0814e919..45d737ee 100644 --- a/headers/enduro2d/math/trs2.hpp +++ b/headers/enduro2d/math/trs2.hpp @@ -26,17 +26,18 @@ namespace e2d rad rotation = rad(0); vec2 scale = vec2::unit(); public: - static const trs2& zero() noexcept; - static const trs2& identity() noexcept; + static constexpr trs2 zero() noexcept; + static constexpr trs2 identity() noexcept; public: - trs2() noexcept = default; - trs2(const trs2& other) noexcept = default; - trs2& operator=(const trs2& other) noexcept = default; + constexpr trs2() noexcept = default; + constexpr trs2(const trs2& other) noexcept = default; + constexpr trs2& operator=(const trs2& other) noexcept = default; template < typename AngleTag > - trs2(const vec2& t, - const unit& r, - const vec2& s) noexcept; + constexpr trs2( + const vec2& t, + const unit& r, + const vec2& s) noexcept; template < typename To > trs2 cast_to() const noexcept; @@ -46,26 +47,24 @@ namespace e2d namespace e2d { template < typename T > - const trs2& trs2::zero() noexcept { - static const trs2 zero{ + constexpr trs2 trs2::zero() noexcept { + return { vec2::zero(), rad(0), vec2::zero()}; - return zero; } template < typename T > - const trs2& trs2::identity() noexcept { - static const trs2 identity{ + constexpr trs2 trs2::identity() noexcept { + return { vec2::zero(), rad(0), vec2::unit()}; - return identity; } template < typename T > template < typename AngleTag > - trs2::trs2( + constexpr trs2::trs2( const vec2& t, const unit& r, const vec2& s) noexcept @@ -90,7 +89,7 @@ namespace e2d // template < typename T, typename AngleTag > - trs2 make_trs2( + constexpr trs2 make_trs2( const vec2& t, const unit& r, const vec2& s) noexcept diff --git a/headers/enduro2d/math/trs3.hpp b/headers/enduro2d/math/trs3.hpp index 487f1a31..3f300540 100644 --- a/headers/enduro2d/math/trs3.hpp +++ b/headers/enduro2d/math/trs3.hpp @@ -25,16 +25,17 @@ namespace e2d quat rotation = quat::identity(); vec3 scale = vec3::unit(); public: - static const trs3& zero() noexcept; - static const trs3& identity() noexcept; + static constexpr trs3 zero() noexcept; + static constexpr trs3 identity() noexcept; public: - trs3() noexcept = default; - trs3(const trs3& other) noexcept = default; - trs3& operator=(const trs3& other) noexcept = default; + constexpr trs3() noexcept = default; + constexpr trs3(const trs3& other) noexcept = default; + constexpr trs3& operator=(const trs3& other) noexcept = default; - trs3(const vec3& t, - const quat& r, - const vec3& s) noexcept; + constexpr trs3( + const vec3& t, + const quat& r, + const vec3& s) noexcept; template < typename To > trs3 cast_to() const noexcept; @@ -44,25 +45,23 @@ namespace e2d namespace e2d { template < typename T > - const trs3& trs3::zero() noexcept { - static const trs3 zero{ + constexpr trs3 trs3::zero() noexcept { + return { vec3::zero(), quat::zero(), vec3::zero()}; - return zero; } template < typename T > - const trs3& trs3::identity() noexcept { - static const trs3 identity{ + constexpr trs3 trs3::identity() noexcept { + return { vec3::zero(), quat::identity(), vec3::unit()}; - return identity; } template < typename T > - trs3::trs3( + constexpr trs3::trs3( const vec3& t, const quat& r, const vec3& s) noexcept @@ -87,7 +86,7 @@ namespace e2d // template < typename T > - trs3 make_trs3( + constexpr trs3 make_trs3( const vec3& t, const quat& r, const vec3& s) noexcept diff --git a/sources/enduro2d/high/bindings/math_binds.cpp b/sources/enduro2d/high/bindings/math_binds.cpp index 099b8d86..dacbf108 100644 --- a/sources/enduro2d/high/bindings/math_binds.cpp +++ b/sources/enduro2d/high/bindings/math_binds.cpp @@ -603,6 +603,59 @@ namespace "contains_nan", sol::resolve&)>(&math::contains_nan)); } + + template < typename T > + void bind_trs2(const str& name, sol::state& l) { + l.new_usertype>(name, + sol::constructors< + trs2(), + trs2(trs2), + trs2(vec2,deg,vec2), + trs2(vec2,rad,vec2)>(), + + "zero", &trs2::zero, + "unit", &trs2::identity, + + "translation", &trs2::translation, + "rotation", &trs2::rotation, + "scale", &trs2::scale, + + sol::meta_function::equal_to, sol::resolve&, const trs2&)>(::operator==), + + "make_translation_trs2", sol::resolve(const vec2&)>(&math::make_translation_trs2), + "make_rotation_trs2", sol::overload( + sol::resolve(const deg&)>(&math::make_rotation_trs2), + sol::resolve(const rad&)>(&math::make_rotation_trs2)), + "make_scale_trs2", sol::resolve(const vec2&)>(&math::make_scale_trs2), + + "approximately", [](const trs2& l, const trs2& r){ return math::approximately(l,r); }, + "contains_nan", sol::resolve&)>(&math::contains_nan)); + } + + template < typename T > + void bind_trs3(const str& name, sol::state& l) { + l.new_usertype>(name, + sol::constructors< + trs3(), + trs3(trs3), + trs3(vec3,quat,vec3)>(), + + "zero", &trs3::zero, + "unit", &trs3::identity, + + "translation", &trs3::translation, + "rotation", &trs3::rotation, + "scale", &trs3::scale, + + sol::meta_function::equal_to, sol::resolve&, const trs3&)>(::operator==), + + "make_translation_trs3", sol::resolve(const vec3&)>(&math::make_translation_trs3), + "make_rotation_trs3", sol::resolve(const quat&)>(&math::make_rotation_trs3), + "make_scale_trs3", sol::resolve(const vec3&)>(&math::make_scale_trs3), + + "approximately", [](const trs3& l, const trs3& r){ return math::approximately(l,r); }, + "contains_nan", sol::resolve&)>(&math::contains_nan)); + } } namespace e2d::bindings @@ -619,5 +672,7 @@ namespace e2d::bindings bind_mat4("m4f", l); bind_rect("b2f", l); bind_aabb("b3f", l); + bind_trs2("t2f", l); + bind_trs3("t3f", l); } } diff --git a/untests/sources/untests_high/luasol.cpp b/untests/sources/untests_high/luasol.cpp index fa47ba5c..b29d41c7 100644 --- a/untests/sources/untests_high/luasol.cpp +++ b/untests/sources/untests_high/luasol.cpp @@ -89,4 +89,17 @@ TEST_CASE("luasol") { )lua"); REQUIRE(r1); } + + SECTION("trs2/trs3") { + radf r0 = l.lua().script(R"lua( + local t = t2f.make_rotation_trs2(degf.new(45)) + return t.rotation + )lua"); + REQUIRE(r0 == math::to_rad(degf(45.f))); + v3f r1 = l.lua().script(R"lua( + local t = t3f.make_translation_trs3(v3f.new(1,2,3)) + return t.translation + )lua"); + REQUIRE(r1 == v3f(1,2,3)); + } }