// // Created by orange on 07.03.2026. // #ifdef OMATH_ENABLE_LUA #include "omath/lua/lua.hpp" #include #include #include namespace { template std::size_t checked_index(const int index) { if (index < 0 || index >= static_cast(Limit)) throw std::out_of_range("matrix index is out of range"); return static_cast(index); } template omath::Mat mat_from_rows(const sol::table& rows) { omath::Mat result; for (std::size_t row = 0; row < Rows; ++row) { const auto row_table = rows[row + 1].get>(); if (!row_table) throw std::invalid_argument("matrix rows must be tables"); for (std::size_t column = 0; column < Columns; ++column) { const auto value = (*row_table)[column + 1].get>(); if (!value) throw std::invalid_argument("matrix row has missing value"); result.at(row, column) = *value; } } return result; } template sol::table mat_as_table(const omath::Mat& mat, sol::this_state state) { sol::state_view lua(state); sol::table rows = lua.create_table(); for (std::size_t row = 0; row < Rows; ++row) { sol::table row_table = lua.create_table(); for (std::size_t column = 0; column < Columns; ++column) row_table[column + 1] = mat.at(row, column); rows[row + 1] = row_table; } return rows; } template void register_square_mat(sol::table& omath_table, const char* name) { using MatType = omath::Mat; auto type = omath_table.new_usertype( name, sol::constructors(), "from_rows", &mat_from_rows, "row_count", []() { return Size; }, "columns_count", []() { return Size; }, "at", [](const MatType& mat, const int row, const int column) { return mat.at(checked_index(row), checked_index(column)); }, "set_at", [](MatType& mat, const int row, const int column, const Type value) { mat.at(checked_index(row), checked_index(column)) = value; return mat; }, sol::meta_function::multiplication, sol::overload( [](const MatType& lhs, const MatType& rhs) { return lhs * rhs; }, [](const MatType& mat, const Type scalar) { return mat * scalar; }, [](const Type scalar, const MatType& mat) { return mat * scalar; }), sol::meta_function::division, [](const MatType& mat, const Type scalar) { return mat / scalar; }, sol::meta_function::equal_to, &MatType::operator==, sol::meta_function::to_string, &MatType::to_string, "transposed", &MatType::transposed, "determinant", &MatType::determinant, "sum", &MatType::sum, "clear", &MatType::clear, "set", &MatType::set, "inverted", [](const MatType& mat) -> sol::optional { auto result = mat.inverted(); if (!result) return sol::nullopt; return *result; }, "as_table", &mat_as_table); if constexpr (RegisterMat4Helpers) { type["to_screen_mat"] = [](const Type screen_width, const Type screen_height) { return MatType{ {screen_width / Type{2}, Type{0}, Type{0}, Type{0}}, {Type{0}, -screen_height / Type{2}, Type{0}, Type{0}}, {Type{0}, Type{0}, Type{1}, Type{0}}, {screen_width / Type{2}, screen_height / Type{2}, Type{0}, Type{1}}, }; }; type["translation"] = &omath::mat_translation; type["scale"] = &omath::mat_scale; type["look_at_left_handed"] = &omath::mat_look_at_left_handed; type["look_at_right_handed"] = &omath::mat_look_at_right_handed; type["perspective_left_handed_vertical_fov"] = &omath::mat_perspective_left_handed_vertical_fov; type["perspective_right_handed_vertical_fov"] = &omath::mat_perspective_right_handed_vertical_fov; } } } // namespace namespace omath::lua { void LuaInterpreter::register_matrices(sol::table& omath_table) { register_square_mat<3, float, omath::MatStoreType::ROW_MAJOR>(omath_table, "Mat3"); register_square_mat<4, float, omath::MatStoreType::ROW_MAJOR, true>(omath_table, "Mat4"); register_square_mat<4, float, omath::MatStoreType::COLUMN_MAJOR, true>(omath_table, "Mat4ColumnMajor"); register_square_mat<4, double, omath::MatStoreType::ROW_MAJOR>(omath_table, "Mat4d"); } } // namespace omath::lua #endif