1
0
mirror of https://gitlab.com/OpenMW/openmw.git synced 2025-01-27 03:35:27 +00:00

Merge branch 'lua_vector_elementwise' into 'master'

Element-wise multiplication and division of Lua vectors

See merge request OpenMW/openmw!1639
This commit is contained in:
Petr Mikheev 2022-02-10 19:43:27 +00:00
commit 6afdf1cb8c
3 changed files with 78 additions and 18 deletions

View File

@ -39,6 +39,10 @@ namespace
EXPECT_TRUE(get<bool>(lua, "v2 == util.vector2(3/5, 4/5)"));
lua.safe_script("_, len = util.vector2(0, 0):normalize()");
EXPECT_FLOAT_EQ(get<float>(lua, "len"), 0);
lua.safe_script("ediv0 = util.vector2(1, 0):ediv(util.vector2(0, 0))");
EXPECT_TRUE(get<bool>(lua, "ediv0.x == math.huge and ediv0.y ~= ediv0.y"));
EXPECT_TRUE(get<bool>(lua, "util.vector2(1, 2):emul(util.vector2(3, 4)) == util.vector2(3, 8)"));
EXPECT_TRUE(get<bool>(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<bool>(lua, "v2 == util.vector3(3/5, 4/5, 0)"));
lua.safe_script("_, len = util.vector3(0, 0, 0):normalize()");
EXPECT_FLOAT_EQ(get<float>(lua, "len"), 0);
lua.safe_script("ediv0 = util.vector3(1, 1, 1):ediv(util.vector3(0, 0, 0))");
EXPECT_TRUE(get<bool>(lua, "ediv0.z == math.huge"));
EXPECT_TRUE(get<bool>(lua, "util.vector3(1, 2, 3):emul(util.vector3(3, 4, 5)) == util.vector3(3, 8, 15)"));
EXPECT_TRUE(get<bool>(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<bool>(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<float>(lua, "len"), 0);
lua.safe_script("ediv0 = util.vector4(1, 1, 1, -1):ediv(util.vector4(0, 0, 0, 0))");
EXPECT_TRUE(get<bool>(lua, "ediv0.w == -math.huge"));
EXPECT_TRUE(get<bool>(lua, "util.vector4(1, 2, 3, 4):emul(util.vector4(3, 4, 5, 6)) == util.vector4(3, 8, 15, 24)"));
EXPECT_TRUE(get<bool>(lua, "util.vector4(4, 6, 8, 9):ediv(util.vector4(2, 3, 4, 3)) == util.vector4(2, 2, 2, 3)"));
}
TEST(LuaUtilPackageTest, Color)

View File

@ -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<Vec2> vec2Type = lua.new_usertype<Vec2>("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<Vec2>(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<Vec3>(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<Vec4>(vec4Type);
// Lua bindings for Color

View File

@ -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