Minetest-WorldEditAdditions/worldeditadditions_core/utils/chaikin.lua

63 lines
2.1 KiB
Lua
Raw Normal View History

local wea_c = worldeditadditions_core
2023-08-03 13:16:14 +00:00
---
-- @module worldeditadditions_core
--- Interpolates between the 2 given points
2023-08-03 13:16:14 +00:00
--
-- TODO: refactor numbers.lua and move this function into there instead
-- @param a Vector3|number The starting point.
-- @param b Vector3|number The ending point.
-- @param time number The percentage between 0 and 1 to interpolate to.
-- @returns Vector3|number The interpolated point.
local function linear_interpolate(a, b, time)
return ((b - a) * time) + a
end
--- Chaikin curve smoothing implementation.
-- This algorithm works by taking a list of points that define a segmented line,
-- and then interpolating between them to smooth the line.
-- @source Ported from https://git.starbeamrainbowlabs.com/sbrl-GitLab/Chaikin-Generator/src/branch/master/ChaikinGenerator/ChaikinCurve.cs
-- @param arr_pos Vector3[] A list of points to interpolate.
-- @param steps number The number of interpolatioon passes to do.
-- @returns Vector3[] A (longer) list of interpolated points.
local function chaikin(arr_pos, steps)
2022-09-25 16:07:37 +00:00
-- print("DEBUG:chaikin START", wea_c.inspect(arr_pos))
local result = wea_c.table.shallowcopy(arr_pos)
2022-09-25 14:30:16 +00:00
local pos_start = result[1]
local pos_end = result[#result]
for pass = 1, steps do
2022-09-25 14:30:16 +00:00
local result_nextpass = {}
-- local offset = 0
for i = 1,#result-1,1 do
local pos_cur = result[i]
local pos_next = result[i+1]
2022-09-25 16:07:37 +00:00
-- print("DEBUG:chaikin SUBSTEP i", i, "pos_cur", pos_cur, "pos_next", pos_next)
2022-09-25 14:30:16 +00:00
table.insert(result_nextpass, linear_interpolate(pos_cur, pos_next, 0.25))
table.insert(result_nextpass, linear_interpolate(pos_cur, pos_next, 0.75))
-- result[i+offset] = linear_interpolate(pos_cur, pos_next, 0.25)
-- table.insert(result, i+1+offset, linear_interpolate(pos_cur, pos_next, 0.75))
-- offset = offset + 1
end
2022-09-25 14:30:16 +00:00
-- table.remove(result, #result-1) -- In the original, but I don't know why
2022-09-25 14:30:16 +00:00
result = result_nextpass
-- Keep the starting & ending positions the same
result[1] = pos_start
result[#result] = pos_end
2022-09-25 14:30:16 +00:00
2022-09-25 16:07:37 +00:00
-- print("DEBUG:chakin STEP", wea_c.inspect(result))
end
return result
end
return {
chaikin = chaikin,
linear_interpolate = linear_interpolate
}