From 97b0d99ce64fabbabe132e6ae935caa5754ac505 Mon Sep 17 00:00:00 2001 From: Cogi Asd Date: Mon, 8 Jul 2024 19:36:09 +0300 Subject: [PATCH] upd: docs for vectors, libvecn.cpp --- doc/ru/scripting/builtins/libvecn.md | 52 +++++++++++++++++++---- src/logic/scripting/lua/libvecn.cpp | 62 ++++++++-------------------- 2 files changed, 62 insertions(+), 52 deletions(-) diff --git a/doc/ru/scripting/builtins/libvecn.md b/doc/ru/scripting/builtins/libvecn.md index d72cbb96..ad0f61da 100644 --- a/doc/ru/scripting/builtins/libvecn.md +++ b/doc/ru/scripting/builtins/libvecn.md @@ -5,7 +5,7 @@ > [!WARNING] > -> vecn, где n |-> размерность вектора (2, 3, 4), т.е vec2, vec3, vec4 +> vecn, где n == размерность вектора (2, 3, 4), т.е vec2, vec3, vec4 > ## Типы данных @@ -110,12 +110,6 @@ vecn.pow(a: vector, b: number) vecn.dot(a: vector, b: vector) ``` -#### Векторное произведение - *vecn.cross(...)* -```lua --- возвращает векторное произведение векторов -vecn.cross(a: vector, b: vector) -``` - #### Поворот - *vecn.rot(...)* > [!WARNING] > Угол поворота (angle) указывается в радианах. @@ -140,8 +134,50 @@ vecn.tostring(a: vector) local v1_3d = {1, 2, 2} local v2_3d = {10, 20, 40} local v3_4d = {1, 2, 4, 1} -local v4_2d = {1, 1} +local v4_2d = {1, 0} local scal = 6 -- обычный скаляр +-- сложение векторов +local result_add = vec3.add(v1_3d, v2_3d) +print("add: " .. vec3.tostring(result_add)) -- {11, 22, 42} +-- вычитание векторов +local result_sub = vec3.sub(v2_3d, v1_3d) +print("sub: " .. vec3.tostring(result_sub)) -- {9, 18, 38} + +-- умножение векторов +local result_mul = vec3.mul(v1_3d, v2_3d) +print("mul: " .. vec3.tostring(result_mul)) -- {10, 40, 80} + +-- умножение вектора на скаляр +local result_mul_scal = vec3.mul(v1_3d, scal) +print("mul_scal: " .. vec3.tostring(result_mul_scal)) -- {6, 12, 12} + +-- нормализация вектора +local result_norm = vec3.norm(v1_3d) +print("norm: " .. vec3.tostring(result_norm)) -- {0.333, 0.667, 0.667} + +-- длина вектора +local result_len = vec3.len(v1_3d) +print("len: " .. result_len) -- 3 + +-- абсолютное значение вектора +local result_abs = vec3.abs(v1_3d) +print("abs: " .. vec3.tostring(result_abs)) -- {1, 2, 2} + +-- округление вектора +local result_round = vec3.round(v1_3d) +print("round: " .. vec3.tostring(result_round)) -- {1, 2, 2} + +-- степень вектора +local result_pow = vec3.pow(v1_3d, 2) +print("pow: " .. vec3.tostring(result_pow)) -- {1, 4, 4} + +-- скалярное произведение векторов +local result_dot = vec3.dot(v1_3d, v2_3d) +print("dot: " .. result_dot) -- 250 + +-- поворот вектора +local result_rot = vec2.rot(v4_2d, math.pi / 4) +print("rot: " .. vec2.tostring(result_rot)) -- {0.707107, 0.707107} ``` diff --git a/src/logic/scripting/lua/libvecn.cpp b/src/logic/scripting/lua/libvecn.cpp index 289b54f3..813aae9b 100644 --- a/src/logic/scripting/lua/libvecn.cpp +++ b/src/logic/scripting/lua/libvecn.cpp @@ -1,7 +1,9 @@ #include "api_lua.hpp" +#define GLM_ENABLE_EXPERIMENTAL #include #include +#include template class Operation> static int l_binop(lua::State* L) { @@ -87,9 +89,8 @@ static int l_dot(lua::State* L) { if (lua::gettop(L) != 2) { throw std::runtime_error("invalid arguments number (2 expected)"); } - return lua::pushnumber(L, glm::dot(lua::tovec(L, 1), // vector a - lua::tovec(L, 2) // vector b - )); + return lua::pushnumber(L, glm::dot(lua::tovec(L, 1), + lua::tovec(L, 2))); } template @@ -97,56 +98,34 @@ static int l_rotate(lua::State* L) { if (lua::gettop(L) != 2) { throw std::runtime_error("invalid arguments number (2 expected)"); } - const auto& vec = lua::tovec(L, 1); // vector - const auto& angle = lua::tonumber(L, 2); // scalar (in radians) + const auto& angle = lua::tonumber(L, 2); // scalar (angle) - const float _cos = std::cos(angle); - const float _sin = std::sin(angle); + const auto _cos = std::cos(angle); + const auto _sin = std::sin(angle); - glm::vec result_vector = vec; + glm::vec result_vector; - for (int i = 0; i < n - 1; ++i) { - float temp = result_vector[i] * _cos - result_vector[i + 1] * _sin; - result_vector[i + 1] = result_vector[i] * _sin + result_vector[i + 1] * _cos; - result_vector[i] = temp; + switch (n) { + case 2: + result_vector.x = vec.x * _cos - vec.y * _sin; + result_vector.y = vec.x * _sin + vec.y * _cos; + break; + // TODO } return lua::setvec(L, 1, result_vector); } template -static int l_cross(lua::State* L) { - if (lua::gettop(L)!= 2) { +static int l_inverse(lua::State* L) { + if (lua::gettop(L) != 1) { throw std::runtime_error("invalid arguments number (2 expected)"); } - const auto& a = lua::tovec(L, 1); - const auto& b = lua::tovec(L, 2); - - glm::vec result_vector; - - if (n == 2) { - result_vector.x = a.x * b.y - a.y * b.x; - return lua::pushnumber(L, result_vector.x); - } else { - for (int i = 0; i < n; ++i) { - int j = (i + 1) % n; - int k = (i + 2) % n; - result_vector[i] = a[j] * b[k] - a[k] * b[j]; - } - return lua::setvec(L, 1, result_vector); - } -} - -template -static int l_inverse(lua::State* L) { - if (lua::gettop(L)!= 1) { - throw std::runtime_error("invalid arguments number (1 expected)"); - } - const auto& vec = lua::tovec(L, 1); + const auto& _vector = lua::tovec(L, 1); //vector glm::vec result_vector; for (int i = 0; i < n; i++) { - result_vector[i] = -vec[i]; + result_vector[i] = -_vector[i]; } return lua::setvec(L, 1, result_vector); } @@ -180,7 +159,6 @@ const luaL_Reg vec2lib [] = { {"round", lua::wrap>}, {"tostring", lua::wrap>}, {"inv", lua::wrap>}, - {"cross", lua::wrap>}, {"rot", lua::wrap>}, {"pow", lua::wrap>}, {"dot", lua::wrap>}, @@ -198,8 +176,6 @@ const luaL_Reg vec3lib [] = { {"round", lua::wrap>}, {"tostring", lua::wrap>}, {"inv", lua::wrap>}, - {"cross", lua::wrap>}, - {"rot", lua::wrap>}, {"pow", lua::wrap>}, {"dot", lua::wrap>}, {NULL, NULL} @@ -216,8 +192,6 @@ const luaL_Reg vec4lib [] = { {"round", lua::wrap>}, {"tostring", lua::wrap>}, {"inv", lua::wrap>}, - {"cross", lua::wrap>}, - {"rot", lua::wrap>}, {"pow", lua::wrap>}, {"dot", lua::wrap>}, {NULL, NULL}