vec3:min; vec3:max;

This commit is contained in:
bjorn 2019-02-08 12:39:10 -08:00
parent f34e6f86ea
commit 50e86579c2
5 changed files with 682 additions and 600 deletions

View File

@ -1,6 +1,7 @@
local ffi = type(jit) == 'table' and jit.status() and require 'ffi'
if not ffi then return end
local min, max = math.min, math.max
local math, Pool = ...
local C, new, cast, typeof, istype = ffi.C, ffi.new, ffi.cast, ffi.typeof, ffi.istype
@ -184,6 +185,20 @@ local vec3 = {
return v
end,
min = function(v, u, t)
checkvec3(v)
checkvec3(u, 1)
v.x, v.y, v.z = min(v.x, u.x), min(v.y, u.y), min(v.z, u.z)
return v
end,
max = function(v, u, t)
checkvec3(v)
checkvec3(u, 1)
v.x, v.y, v.z = max(v.x, u.x), max(v.y, u.y), max(v.z, u.z)
return v
end,
__add = function(v, u)
checkvec3(v, 1)
checkvec3(u, 2)

File diff suppressed because it is too large Load Diff

View File

@ -147,6 +147,22 @@ static int l_lovrVec3Lerp(lua_State* L) {
return 1;
}
static int l_lovrVec3Min(lua_State* L) {
vec3 v = luax_checkmathtype(L, 1, MATH_VEC3, NULL);
vec3 u = luax_checkmathtype(L, 2, MATH_VEC3, NULL);
vec3_min(v, u);
lua_settop(L, 1);
return 1;
}
static int l_lovrVec3Max(lua_State* L) {
vec3 v = luax_checkmathtype(L, 1, MATH_VEC3, NULL);
vec3 u = luax_checkmathtype(L, 2, MATH_VEC3, NULL);
vec3_max(v, u);
lua_settop(L, 1);
return 1;
}
static int l_lovrVec3__add(lua_State* L) {
vec3 v = luax_checkmathtype(L, 1, MATH_VEC3, NULL);
vec3 u = luax_checkmathtype(L, 2, MATH_VEC3, NULL);
@ -227,6 +243,8 @@ const luaL_Reg lovrVec3[] = {
{ "dot", l_lovrVec3Dot },
{ "cross", l_lovrVec3Cross },
{ "lerp", l_lovrVec3Lerp },
{ "min", l_lovrVec3Min },
{ "max", l_lovrVec3Max },
{ "__add", l_lovrVec3__add },
{ "__sub", l_lovrVec3__sub },
{ "__mul", l_lovrVec3__mul },

View File

@ -6,6 +6,14 @@
#include <xmmintrin.h>
#endif
#ifndef MIN
#define MIN(a, b) (a < b ? a : b)
#endif
#ifndef MAX
#define MAX(a, b) (a > b ? a : b)
#endif
// vec3
vec3 vec3_init(vec3 v, vec3 u) {
@ -75,6 +83,20 @@ vec3 vec3_lerp(vec3 v, vec3 u, float t) {
return v;
}
vec3 vec3_min(vec3 v, vec3 u) {
v[0] = MIN(v[0], u[0]);
v[1] = MIN(v[1], u[1]);
v[2] = MIN(v[2], u[2]);
return v;
}
vec3 vec3_max(vec3 v, vec3 u) {
v[0] = MAX(v[0], u[0]);
v[1] = MAX(v[1], u[1]);
v[2] = MAX(v[2], u[2]);
return v;
}
// quat
quat quat_init(quat q, quat r) {
@ -103,9 +125,6 @@ quat quat_fromAngleAxis(quat q, float angle, float ax, float ay, float az) {
return q;
}
#ifndef MAX
#define MAX(a, b) (a > b ? a : b)
#endif
quat quat_fromMat4(quat q, mat4 m) {
float x = sqrtf(MAX(0.f, 1.f + m[0] - m[5] - m[10])) / 2.f;
float y = sqrtf(MAX(0.f, 1.f - m[0] + m[5] - m[10])) / 2.f;

View File

@ -24,6 +24,8 @@ MAF_EXPORT float vec3_distance(vec3 v, vec3 u);
MAF_EXPORT float vec3_dot(vec3 v, vec3 u);
MAF_EXPORT vec3 vec3_cross(vec3 v, vec3 u);
MAF_EXPORT vec3 vec3_lerp(vec3 v, vec3 u, float t);
MAF_EXPORT vec3 vec3_min(vec3 v, vec3 u);
MAF_EXPORT vec3 vec3_max(vec3 v, vec3 u);
// quat
MAF_EXPORT quat quat_init(quat q, quat r);