parent
694fdf8e2b
commit
fcde73c1ef
After Width: | Height: | Size: 1.0 MiB |
@ -0,0 +1,40 @@ |
||||
use rlua::{Context, Table, Error}; |
||||
use crate::object::{SWAP_XY, SWAP_YZ, SWAP_XZ, scale_xyz, scale, scale_x, scale_y, scale_z}; |
||||
use crate::structs::{Mat3, I3}; |
||||
|
||||
pub fn transform<'lua>(ctx: Context<'lua>, _env: Table<'lua>) -> rlua::Result<Table<'lua>> { |
||||
let module = ctx.create_table()?; |
||||
|
||||
module.set("SWAPXY", SWAP_XY)?; |
||||
module.set("SWAPXZ", SWAP_XZ)?; |
||||
module.set("SWAPYZ", SWAP_YZ)?; |
||||
|
||||
module.set("scalexyz", ctx.create_function( |
||||
|ctx, (x, y, z)| Ok(scale_xyz(x, y, z)) |
||||
)?)?; |
||||
module.set("scale", ctx.create_function( |
||||
|ctx, k| Ok(scale(k)) |
||||
)?)?; |
||||
|
||||
module.set("scalex", ctx.create_function( |
||||
|ctx, k| Ok(scale_x(k)) |
||||
)?)?; |
||||
module.set("scaley", ctx.create_function( |
||||
|ctx, k| Ok(scale_y(k)) |
||||
)?)?; |
||||
module.set("scalez", ctx.create_function( |
||||
|ctx, k| Ok(scale_z(k)) |
||||
)?)?; |
||||
|
||||
module.set("stack", ctx.create_function( |
||||
|ctx, transforms: Vec<Mat3>| { |
||||
let mut acc = I3; |
||||
for trans in transforms.iter().rev().cloned() { |
||||
acc = trans * I3; |
||||
} |
||||
Ok(acc) |
||||
} |
||||
)?)?; |
||||
|
||||
Ok(module) |
||||
} |
@ -0,0 +1,71 @@ |
||||
local env = ... |
||||
local obj = env.obj |
||||
local vec3 = env.vec3 |
||||
local mat3 = env.mat3 |
||||
local transform = env.transform |
||||
|
||||
local M = {} |
||||
|
||||
-- technically a binary fold |
||||
local function binstack(objs, fn) |
||||
local n = #objs |
||||
if n == 1 then return objs[1] end |
||||
|
||||
local mid = n//2 |
||||
local left, right = {}, {} |
||||
for i=1, mid do |
||||
left[i] = objs[i] |
||||
end |
||||
for i=mid+1, n do |
||||
right[i-mid] = objs[i] |
||||
end |
||||
return fn(binstack(left, fn), binstack(right, fn)) |
||||
end |
||||
|
||||
-- technically a fold |
||||
local function linstack(objs, fn) |
||||
local head = objs[1] |
||||
for i=2, #objs do |
||||
head = fn(head, objs[i]) |
||||
end |
||||
return head |
||||
end |
||||
|
||||
-- random functions |
||||
local SEED = os.getenv "SEED" or os.time() |
||||
function M.seed() |
||||
math.randomseed(SEED) |
||||
end |
||||
function M.randab(a, b) |
||||
return math.random() * (b-a) + a |
||||
end |
||||
function M.choice(list) |
||||
return list[math.random(1, #list)] |
||||
end |
||||
|
||||
-- multiple object composition |
||||
function M.union(objs) |
||||
return binstack(objs, obj.union) |
||||
end |
||||
function M.intersection(objs) |
||||
return binstack(objs, obj.intersection) |
||||
end |
||||
function M.smoothunion(objs, k) |
||||
return linstack(objs, function(a, b) return obj.smoothunion(a, b, k) end) |
||||
end |
||||
function M.smoothintersection(objs, k) |
||||
return linstack(objs, function(a, b) return obj.smoothintersection(a, b, k) end) |
||||
end |
||||
|
||||
-- simpler transformations |
||||
function M.scale(object, k) |
||||
return obj.affinetransform(object, transform.scale(k), vec3.O) |
||||
end |
||||
function M.transform(object, mat) |
||||
return obj.affinetransform(object, mat, vec3.O) |
||||
end |
||||
function M.translate(object, vec) |
||||
return obj.affinetransform(object, mat3.O, vec) |
||||
end |
||||
|
||||
return M |
@ -0,0 +1,5 @@ |
||||
use rlua::{Context, Table}; |
||||
|
||||
pub fn util<'lua>(ctx: Context<'lua>, env: Table<'lua>) -> rlua::Result<Table<'lua>> { |
||||
ctx.load(include_str!("util.lua")).call(env) |
||||
} |
Loading…
Reference in new issue