From ede9d27437da965dbffcfa6c5931516a67bbc54e Mon Sep 17 00:00:00 2001 From: uramer Date: Thu, 10 Feb 2022 19:43:27 +0000 Subject: [PATCH] Element-wise multiplication and division of Lua vectors --- .../lua/test_utilpackage.cpp | 12 ++++++ components/lua/utilpackage.cpp | 41 ++++++++++-------- files/lua_api/openmw/util.lua | 43 +++++++++++++++++++ 3 files changed, 78 insertions(+), 18 deletions(-) diff --git a/apps/openmw_test_suite/lua/test_utilpackage.cpp b/apps/openmw_test_suite/lua/test_utilpackage.cpp index ba19cca383..5e27b36916 100644 --- a/apps/openmw_test_suite/lua/test_utilpackage.cpp +++ b/apps/openmw_test_suite/lua/test_utilpackage.cpp @@ -39,6 +39,10 @@ namespace EXPECT_TRUE(get(lua, "v2 == util.vector2(3/5, 4/5)")); lua.safe_script("_, len = util.vector2(0, 0):normalize()"); EXPECT_FLOAT_EQ(get(lua, "len"), 0); + lua.safe_script("ediv0 = util.vector2(1, 0):ediv(util.vector2(0, 0))"); + EXPECT_TRUE(get(lua, "ediv0.x == math.huge and ediv0.y ~= ediv0.y")); + EXPECT_TRUE(get(lua, "util.vector2(1, 2):emul(util.vector2(3, 4)) == util.vector2(3, 8)")); + EXPECT_TRUE(get(lua, "util.vector2(4, 6):ediv(util.vector2(2, 3)) == util.vector2(2, 2)")); } TEST(LuaUtilPackageTest, Vector3) @@ -68,6 +72,10 @@ namespace EXPECT_TRUE(get(lua, "v2 == util.vector3(3/5, 4/5, 0)")); lua.safe_script("_, len = util.vector3(0, 0, 0):normalize()"); EXPECT_FLOAT_EQ(get(lua, "len"), 0); + lua.safe_script("ediv0 = util.vector3(1, 1, 1):ediv(util.vector3(0, 0, 0))"); + EXPECT_TRUE(get(lua, "ediv0.z == math.huge")); + EXPECT_TRUE(get(lua, "util.vector3(1, 2, 3):emul(util.vector3(3, 4, 5)) == util.vector3(3, 8, 15)")); + EXPECT_TRUE(get(lua, "util.vector3(4, 6, 8):ediv(util.vector3(2, 3, 4)) == util.vector3(2, 2, 2)")); } TEST(LuaUtilPackageTest, Vector4) @@ -95,6 +103,10 @@ namespace EXPECT_TRUE(get(lua, "v2 == util.vector4(3/5, 0, 0, 4/5)")); lua.safe_script("_, len = util.vector4(0, 0, 0, 0):normalize()"); EXPECT_FLOAT_EQ(get(lua, "len"), 0); + lua.safe_script("ediv0 = util.vector4(1, 1, 1, -1):ediv(util.vector4(0, 0, 0, 0))"); + EXPECT_TRUE(get(lua, "ediv0.w == -math.huge")); + EXPECT_TRUE(get(lua, "util.vector4(1, 2, 3, 4):emul(util.vector4(3, 4, 5, 6)) == util.vector4(3, 8, 15, 24)")); + EXPECT_TRUE(get(lua, "util.vector4(4, 6, 8, 9):ediv(util.vector4(2, 3, 4, 3)) == util.vector4(2, 2, 2, 3)")); } TEST(LuaUtilPackageTest, Color) diff --git a/components/lua/utilpackage.cpp b/components/lua/utilpackage.cpp index c51b00a7d5..d31a8259be 100644 --- a/components/lua/utilpackage.cpp +++ b/components/lua/utilpackage.cpp @@ -55,6 +55,29 @@ namespace LuaUtil else return std::make_tuple(v * (1.f / len), len); }; + vectorType["emul"] = [](const T& a, const T& b) + { + T result; + for (int i = 0; i < T::num_components; ++i) + result[i] = a[i] * b[i]; + return result; + }; + vectorType["ediv"] = [](const T& a, const T& b) + { + T result; + for (int i = 0; i < T::num_components; ++i) + result[i] = a[i] / b[i]; + return result; + }; + vectorType[sol::meta_function::to_string] = [](const T& v) + { + std::stringstream ss; + ss << "(" << v[0]; + for (int i = 1; i < T::num_components; ++i) + ss << ", " << v[i]; + ss << ")"; + return ss.str(); + }; } } @@ -67,12 +90,6 @@ namespace LuaUtil sol::usertype vec2Type = lua.new_usertype("Vec2"); vec2Type["x"] = sol::readonly_property([](const Vec2& v) -> float { return v.x(); } ); vec2Type["y"] = sol::readonly_property([](const Vec2& v) -> float { return v.y(); } ); - vec2Type[sol::meta_function::to_string] = [](const Vec2& v) - { - std::stringstream ss; - ss << "(" << v.x() << ", " << v.y() << ")"; - return ss.str(); - }; addVectorMethods(vec2Type); vec2Type["rotate"] = &Misc::rotateVec2f; @@ -82,12 +99,6 @@ namespace LuaUtil vec3Type["x"] = sol::readonly_property([](const Vec3& v) -> float { return v.x(); } ); vec3Type["y"] = sol::readonly_property([](const Vec3& v) -> float { return v.y(); } ); vec3Type["z"] = sol::readonly_property([](const Vec3& v) -> float { return v.z(); } ); - vec3Type[sol::meta_function::to_string] = [](const Vec3& v) - { - std::stringstream ss; - ss << "(" << v.x() << ", " << v.y() << ", " << v.z() << ")"; - return ss.str(); - }; addVectorMethods(vec3Type); vec3Type[sol::meta_function::involution] = [](const Vec3& a, const Vec3& b) { return a ^ b; }; vec3Type["cross"] = [](const Vec3& a, const Vec3& b) { return a ^ b; }; @@ -100,12 +111,6 @@ namespace LuaUtil vec4Type["y"] = sol::readonly_property([](const Vec4& v) -> float { return v.y(); }); vec4Type["z"] = sol::readonly_property([](const Vec4& v) -> float { return v.z(); }); vec4Type["w"] = sol::readonly_property([](const Vec4& v) -> float { return v.w(); }); - vec4Type[sol::meta_function::to_string] = [](const Vec4& v) - { - std::stringstream ss; - ss << "(" << v.x() << ", " << v.y() << ", " << v.z() << ", " << v.w() << ")"; - return ss.str(); - }; addVectorMethods(vec4Type); // Lua bindings for Color diff --git a/files/lua_api/openmw/util.lua b/files/lua_api/openmw/util.lua index dca1683d45..4ea0ee4b0d 100644 --- a/files/lua_api/openmw/util.lua +++ b/files/lua_api/openmw/util.lua @@ -87,6 +87,20 @@ -- @param #Vector2 v -- @return #number +--- +-- Element-wise multiplication +-- @function [parent=#Vector2] emul +-- @param self +-- @param #Vector2 v +-- @return #Vector2 + +--- +-- Element-wise division +-- @function [parent=#Vector2] ediv +-- @param self +-- @param #Vector2 v +-- @return #Vector2 + --- -- Immutable 3D vector @@ -152,6 +166,20 @@ -- @param #Vector3 v -- @return #Vector3 +--- +-- Element-wise multiplication +-- @function [parent=#Vector3] emul +-- @param self +-- @param #Vector3 v +-- @return #Vector3 + +--- +-- Element-wise division +-- @function [parent=#Vector3] ediv +-- @param self +-- @param #Vector3 v +-- @return #Vector3 + --- -- Immutable 4D vector. @@ -210,6 +238,21 @@ -- @param #Vector4 v -- @return #number +--- +-- Element-wise multiplication +-- @function [parent=#Vector4] emul +-- @param self +-- @param #Vector4 v +-- @return #Vector4 + +--- +-- Element-wise division +-- @function [parent=#Vector4] ediv +-- @param self +-- @param #Vector4 v +-- @return #Vector4 + + --- -- Color in RGBA format. All of the component values are in the range [0, 1]. -- @type Color