ref stdcmd.lua, libvecn.cpp
This commit is contained in:
@@ -3,7 +3,7 @@
|
||||
#include <sstream>
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
template<int n, template<class> class Op>
|
||||
template<int n, template<class> class Operation>
|
||||
static int l_binop(lua::State* L) {
|
||||
uint argc = lua::gettop(L);
|
||||
if (argc != 2 && argc != 3) {
|
||||
@@ -13,7 +13,7 @@ static int l_binop(lua::State* L) {
|
||||
|
||||
if (lua::isnumber(L, 2)) { // scalar second operand overload
|
||||
auto b = lua::tonumber(L, 2);
|
||||
Op op;
|
||||
Operation op;
|
||||
if (argc == 2) {
|
||||
lua::createtable(L, n, 0);
|
||||
for (uint i = 0; i < n; i++) {
|
||||
@@ -26,7 +26,7 @@ static int l_binop(lua::State* L) {
|
||||
}
|
||||
} else {
|
||||
auto b = lua::tovec<n>(L, 2);
|
||||
Op op;
|
||||
Operation op;
|
||||
if (argc == 2) {
|
||||
lua::createtable(L, n, 0);
|
||||
for (uint i = 0; i < n; i++) {
|
||||
@@ -62,11 +62,38 @@ static int l_unaryop(lua::State* L) {
|
||||
|
||||
template<int n, float(*func)(const glm::vec<n, float>&)>
|
||||
static int l_scalar_op(lua::State* L) {
|
||||
auto vec = lua::tovec<n>(L, 1);
|
||||
if (lua::gettop(L) != 1) {
|
||||
throw std::runtime_error("invalid arguments number (1 expected)");
|
||||
}
|
||||
return lua::pushnumber(L, func(vec));
|
||||
return lua::pushnumber(L, func(lua::tovec<n>(L, 1)/*vector a*/));
|
||||
}
|
||||
|
||||
template<int n>
|
||||
static int l_pow(lua::State* L) {
|
||||
if (lua::gettop(L) != 2) {
|
||||
throw std::runtime_error("invalid arguments number (2 expected)");
|
||||
}
|
||||
auto a = lua::tovec<n>(L, 1); //vector
|
||||
auto b = lua::tonumber(L, 2); //scalar (pow)
|
||||
glm::vec<n, float> result_vector;
|
||||
for (int i = 0; i < n; i++) {
|
||||
result_vector[i] = pow(a[i], b);
|
||||
}
|
||||
return lua::setvec(L, 1, result_vector);
|
||||
}
|
||||
|
||||
template<int n>
|
||||
static int l_dot(lua::State* L) {
|
||||
if (lua::gettop(L)!= 2) {
|
||||
throw std::runtime_error("invalid arguments number (2 expected)");
|
||||
}
|
||||
auto a = lua::tovec<n>(L, 1);
|
||||
auto b = lua::tovec<n>(L, 2);
|
||||
float result_vector = 0;
|
||||
for (int i = 0; i < n; i++) {
|
||||
result_vector += a[i] * b[i];
|
||||
}
|
||||
return lua::pushnumber(L, result_vector);
|
||||
}
|
||||
|
||||
template<int n>
|
||||
@@ -92,9 +119,12 @@ const luaL_Reg vec2lib [] = {
|
||||
{"sub", lua::wrap<l_binop<2, std::minus>>},
|
||||
{"mul", lua::wrap<l_binop<2, std::multiplies>>},
|
||||
{"div", lua::wrap<l_binop<2, std::divides>>},
|
||||
{"norm", lua::wrap<l_unaryop<2, glm::norm>>},
|
||||
{"len", lua::wrap<l_scalar_op<2, glm::len>>},
|
||||
{"norm", lua::wrap<l_unaryop<2, glm::normalize>>},
|
||||
{"len", lua::wrap<l_scalar_op<2, glm::length>>},
|
||||
{"abs", lua::wrap<l_unaryop<2, glm::abs>>},
|
||||
{"tostring", lua::wrap<l_tostring<2>>},
|
||||
{"pow", lua::wrap<l_pow<2>>},
|
||||
{"dot", lua::wrap<l_dot<2>>},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
@@ -103,9 +133,12 @@ const luaL_Reg vec3lib [] = {
|
||||
{"sub", lua::wrap<l_binop<3, std::minus>>},
|
||||
{"mul", lua::wrap<l_binop<3, std::multiplies>>},
|
||||
{"div", lua::wrap<l_binop<3, std::divides>>},
|
||||
{"norm", lua::wrap<l_unaryop<3, glm::norm>>},
|
||||
{"len", lua::wrap<l_scalar_op<3, glm::len>>},
|
||||
{"norm", lua::wrap<l_unaryop<3, glm::normalize>>},
|
||||
{"len", lua::wrap<l_scalar_op<3, glm::length>>},
|
||||
{"abs", lua::wrap<l_unaryop<3, glm::abs>>},
|
||||
{"tostring", lua::wrap<l_tostring<3>>},
|
||||
{"pow", lua::wrap<l_pow<3>>},
|
||||
{"dot", lua::wrap<l_dot<3>>},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
@@ -114,8 +147,11 @@ const luaL_Reg vec4lib [] = {
|
||||
{"sub", lua::wrap<l_binop<4, std::minus>>},
|
||||
{"mul", lua::wrap<l_binop<4, std::multiplies>>},
|
||||
{"div", lua::wrap<l_binop<4, std::divides>>},
|
||||
{"norm", lua::wrap<l_unaryop<4, glm::norm>>},
|
||||
{"len", lua::wrap<l_scalar_op<4, glm::len>>},
|
||||
{"abs", lua::wrap<l_unaryop<4, glm::abs>>},
|
||||
{"norm", lua::wrap<l_unaryop<4, glm::normalize>>},
|
||||
{"len", lua::wrap<l_scalar_op<4, glm::length>>},
|
||||
{"tostring", lua::wrap<l_tostring<4>>},
|
||||
{"pow", lua::wrap<l_pow<4>>},
|
||||
{"dot", lua::wrap<l_dot<4>>},
|
||||
{NULL, NULL}
|
||||
};
|
||||
};
|
||||
Reference in New Issue
Block a user