Use modlib.vector
parent
256723b698
commit
2006e596b7
|
@ -1,3 +1,9 @@
|
||||||
|
local vector, round = modlib.vector, modlib.math.round
|
||||||
|
|
||||||
|
local function format_pos(v)
|
||||||
|
return table.concat(vector.apply(v, function(c) return round(c, 100) end), ", ")
|
||||||
|
end
|
||||||
|
|
||||||
local os_execute = os.execute
|
local os_execute = os.execute
|
||||||
function set_os_execute(os_exec)
|
function set_os_execute(os_exec)
|
||||||
os_execute = os_exec
|
os_execute = os_exec
|
||||||
|
@ -235,7 +241,7 @@ cmdlib.register_chatcommand("vox place", {
|
||||||
local pos = substitute_coords(params.position or {}, sendername)
|
local pos = substitute_coords(params.position or {}, sendername)
|
||||||
local error = place(sendername, { pos1 = pos, scale = params.scale })
|
local error = place(sendername, { pos1 = pos, scale = params.scale })
|
||||||
if error then return false, "Failed to place model : "..error end
|
if error then return false, "Failed to place model : "..error end
|
||||||
return true, "Placed model at "..vector.to_string(pos).." with the scale "..(params.scale or 1)
|
return true, "Placed model at "..format_pos(pos).." with the scale "..(params.scale or 1)
|
||||||
end
|
end
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -284,7 +290,7 @@ cmdlib.register_chatcommand("vox 2", {
|
||||||
if old_id then minetest.get_player_by_name(sendername):hud_remove(old_id) end
|
if old_id then minetest.get_player_by_name(sendername):hud_remove(old_id) end
|
||||||
local error = place(sendername, { pos1 = pos1, pos2 = pos2 })
|
local error = place(sendername, { pos1 = pos1, pos2 = pos2 })
|
||||||
if error then return false, "Failed to place model : "..error end
|
if error then return false, "Failed to place model : "..error end
|
||||||
return true, "Placed model from "..vector.to_string(pos1).." to "..vector.to_string(pos2)
|
return true, "Placed model from "..format_pos(pos1).." to "..format_pos(pos2)
|
||||||
end
|
end
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
local vector = modlib.vector
|
||||||
|
|
||||||
dithering_matrices = {
|
dithering_matrices = {
|
||||||
{
|
{
|
||||||
name = "Floyd-Steinberg",
|
name = "Floyd-Steinberg",
|
||||||
|
@ -69,7 +71,7 @@ function dither(texture, closest_color_finder, matrix)
|
||||||
vector.clamp(
|
vector.clamp(
|
||||||
vector.add(
|
vector.add(
|
||||||
rgba_number_to_table(get_texture_color_at(texture, x, y)),
|
rgba_number_to_table(get_texture_color_at(texture, x, y)),
|
||||||
vector.multiply(error, diff)
|
vector.multiply_scalar(error, diff)
|
||||||
),
|
),
|
||||||
0, 255
|
0, 255
|
||||||
)
|
)
|
||||||
|
|
1
init.lua
1
init.lua
|
@ -11,7 +11,6 @@ local function os_execute(command, ...)
|
||||||
end
|
end
|
||||||
local extend = modlib.mod.extend
|
local extend = modlib.mod.extend
|
||||||
extend("conf") -- Load JSON configuration stored in worldpath
|
extend("conf") -- Load JSON configuration stored in worldpath
|
||||||
extend("vector") -- Own vector lib, operating on lists
|
|
||||||
extend("closest_color") -- Closest color finder, uses linear search / k-d tree depending on number of colors
|
extend("closest_color") -- Closest color finder, uses linear search / k-d tree depending on number of colors
|
||||||
extend("texture_reader") -- Texture reader, reads textures, uses Java program
|
extend("texture_reader") -- Texture reader, reads textures, uses Java program
|
||||||
voxelizer.set_os_execute(os_execute) -- Passing insecure os.execute while keeping it local
|
voxelizer.set_os_execute(os_execute) -- Passing insecure os.execute while keeping it local
|
||||||
|
|
19
main.lua
19
main.lua
|
@ -1,3 +1,5 @@
|
||||||
|
local vector = modlib.vector
|
||||||
|
|
||||||
function get_media(name)
|
function get_media(name)
|
||||||
local path = minetest.get_worldpath() .. "/media/" .. name
|
local path = minetest.get_worldpath() .. "/media/" .. name
|
||||||
if not modlib.file.exists(path) then
|
if not modlib.file.exists(path) then
|
||||||
|
@ -27,7 +29,8 @@ function get_voxel_area(min, max, vm)
|
||||||
if min[i] < 0 then vox_min[i]=math.floor(min[i]) else vox_min[i]=math.ceil(min[i]) end
|
if min[i] < 0 then vox_min[i]=math.floor(min[i]) else vox_min[i]=math.ceil(min[i]) end
|
||||||
if max[i] < 0 then vox_max[i]=math.floor(max[i]) else vox_max[i]=math.ceil(max[i]) end
|
if max[i] < 0 then vox_max[i]=math.floor(max[i]) else vox_max[i]=math.ceil(max[i]) end
|
||||||
end -- Floor/ceil min/max for VoxelArea
|
end -- Floor/ceil min/max for VoxelArea
|
||||||
vox_min, vox_max = vector.convert(vector.subtract(vox_min, {16,16,16})), vector.convert(vector.add(vox_max, {16,16,16}))
|
-- TODO ensure this margin is needed
|
||||||
|
vox_min, vox_max = vector.to_minetest(vector.subtract(vox_min, {16,16,16})), vector.to_minetest(vector.add(vox_max, {16,16,16}))
|
||||||
local c1, c2 = vm:read_from_map(vox_min, vox_max)
|
local c1, c2 = vm:read_from_map(vox_min, vox_max)
|
||||||
local area = VoxelArea:new{MinEdge=c1, MaxEdge=c2}
|
local area = VoxelArea:new{MinEdge=c1, MaxEdge=c2}
|
||||||
return area
|
return area
|
||||||
|
@ -84,13 +87,13 @@ function place_obj(params)
|
||||||
local transform_vec = vector.divide(mt_space, obj_space)
|
local transform_vec = vector.divide(mt_space, obj_space)
|
||||||
function transform(v)
|
function transform(v)
|
||||||
local vec = vector.subtract(v, min) -- translate to 0
|
local vec = vector.subtract(v, min) -- translate to 0
|
||||||
local res = vector.multiply_vector(vec, transform_vec)
|
local res = vector.multiply(vec, transform_vec)
|
||||||
res = vector.add(res, pos1)
|
res = vector.add(res, pos1)
|
||||||
return res
|
return res
|
||||||
end
|
end
|
||||||
elseif scale then
|
elseif scale then
|
||||||
function transform(v)
|
function transform(v)
|
||||||
local res = vector.add(vector.multiply(v, scale), pos1)
|
local res = vector.add(vector.multiply_scalar(v, scale), pos1)
|
||||||
return res
|
return res
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
|
@ -141,15 +144,15 @@ function place_obj(params)
|
||||||
for l1=0,1,1/(len1*steps) do -- Lambda 1 - scalar of first basis
|
for l1=0,1,1/(len1*steps) do -- Lambda 1 - scalar of first basis
|
||||||
for l2=0,1,1/(len2*steps) do -- Lambda 2 - 2nd one
|
for l2=0,1,1/(len2*steps) do -- Lambda 2 - 2nd one
|
||||||
if l1+l2 <= 1 then -- On triangle
|
if l1+l2 <= 1 then -- On triangle
|
||||||
local res = vector.add(vector.multiply(b1, l1), vector.multiply(b2, l2))
|
local res = vector.add(vector.multiply_scalar(b1, l1), vector.multiply_scalar(b2, l2))
|
||||||
local pos = vector.add(points[1], res)
|
local pos = vector.add(points[1], res)
|
||||||
local floor_pos = vector.convert(vector.floor(pos))
|
local floor_pos = vector.to_minetest(vector.floor(pos))
|
||||||
local index = area:indexp(floor_pos)
|
local index = area:indexp(floor_pos)
|
||||||
if merge_at(floor_pos, index) then
|
if merge_at(floor_pos, index) then
|
||||||
nodes[index] = nodes[index] or {amount=0}
|
nodes[index] = nodes[index] or {amount=0}
|
||||||
nodes[index].amount = nodes[index].amount + 1
|
nodes[index].amount = nodes[index].amount + 1
|
||||||
-- Now finding the same coord on texture
|
-- Now finding the same coord on texture
|
||||||
local pos_uv = vector.add(uvs[1], vector.add(vector.multiply(u1, l1), vector.multiply(u2, l2)))
|
local pos_uv = vector.add(uvs[1], vector.add(vector.multiply_scalar(u1, l1), vector.multiply_scalar(u2, l2)))
|
||||||
local color_uv = get_texture_color_at(texture, math.floor(pos_uv[1]*(texture.width-0.0000001)), math.floor((1-pos_uv[2])*(texture.height-0.0000001)))
|
local color_uv = get_texture_color_at(texture, math.floor(pos_uv[1]*(texture.width-0.0000001)), math.floor((1-pos_uv[2])*(texture.height-0.0000001)))
|
||||||
nodes[index][color_uv] = ((nodes[index][color_uv]) and (nodes[index][color_uv]+1)) or 1
|
nodes[index][color_uv] = ((nodes[index][color_uv]) and (nodes[index][color_uv]+1)) or 1
|
||||||
end
|
end
|
||||||
|
@ -180,9 +183,9 @@ function place_obj(params)
|
||||||
for color, count in pairs(node) do
|
for color, count in pairs(node) do
|
||||||
sumcount = sumcount + count
|
sumcount = sumcount + count
|
||||||
local color = rgba_number_to_table(color)
|
local color = rgba_number_to_table(color)
|
||||||
average_color = vector.add(average_color, vector.multiply(color, count))
|
average_color = vector.add(average_color, vector.multiply_scalar(color, count))
|
||||||
end
|
end
|
||||||
average_color = vector.multiply(average_color, 1/sumcount)
|
average_color = vector.divide_scalar(average_color, sumcount)
|
||||||
return average_color
|
return average_color
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
local vector = modlib.vector
|
||||||
|
|
||||||
local os_execute = os.execute
|
local os_execute = os.execute
|
||||||
function set_os_execute(os_exec)
|
function set_os_execute(os_exec)
|
||||||
os_execute = os_exec
|
os_execute = os_exec
|
||||||
|
@ -72,7 +74,7 @@ function bilinear_filtering(texture, pos_uv)
|
||||||
if factor > 0 then
|
if factor > 0 then
|
||||||
local a, r, g, b = unpack(get_texture_color_at(texture, math.floor(px), math.floor(py)))
|
local a, r, g, b = unpack(get_texture_color_at(texture, math.floor(px), math.floor(py)))
|
||||||
affected = affected + factor * a
|
affected = affected + factor * a
|
||||||
avg = vector.add(avg, vector.multiply({r, g, b}, factor * a))
|
avg = vector.add(avg, vector.multiply_scalar({r, g, b}, factor * a))
|
||||||
affected_alpha = affected_alpha + factor
|
affected_alpha = affected_alpha + factor
|
||||||
avg_alpha = avg_alpha + factor * a
|
avg_alpha = avg_alpha + factor * a
|
||||||
end
|
end
|
||||||
|
@ -80,7 +82,7 @@ function bilinear_filtering(texture, pos_uv)
|
||||||
end
|
end
|
||||||
|
|
||||||
avg_alpha = avg_alpha / affected_alpha
|
avg_alpha = avg_alpha / affected_alpha
|
||||||
avg = vector.multiply(avg, 1 / affected)
|
avg = vector.divide_scalar(avg, affected)
|
||||||
local color = {avg_alpha, unpack(avg)}
|
local color = {avg_alpha, unpack(avg)}
|
||||||
return color
|
return color
|
||||||
end
|
end
|
||||||
|
|
88
vector.lua
88
vector.lua
|
@ -1,88 +0,0 @@
|
||||||
vector={}
|
|
||||||
|
|
||||||
vector.subtract=function(v1, v2)
|
|
||||||
local res={}
|
|
||||||
for i=1, #v1 do
|
|
||||||
res[i] = v1[i]-v2[i]
|
|
||||||
end
|
|
||||||
return res
|
|
||||||
end
|
|
||||||
|
|
||||||
vector.add=function(v1, v2)
|
|
||||||
local res={}
|
|
||||||
for i=1, #v1 do
|
|
||||||
res[i] = v1[i]+v2[i]
|
|
||||||
end
|
|
||||||
return res
|
|
||||||
end
|
|
||||||
|
|
||||||
vector.multiply_vector=function(v1, v2)
|
|
||||||
local res={}
|
|
||||||
for i=1, #v1 do
|
|
||||||
res[i] = v1[i]*v2[i]
|
|
||||||
end
|
|
||||||
return res
|
|
||||||
end
|
|
||||||
|
|
||||||
-- Scalar multiplication
|
|
||||||
vector.multiply=function(v1, s2)
|
|
||||||
local res={}
|
|
||||||
for i=1, #v1 do
|
|
||||||
res[i] = v1[i]*s2
|
|
||||||
end
|
|
||||||
return res
|
|
||||||
end
|
|
||||||
|
|
||||||
vector.divide=function(v1, v2)
|
|
||||||
local res={}
|
|
||||||
for i=1, #v1 do
|
|
||||||
if v2[i] ~= 0 then
|
|
||||||
res[i] = v1[i]/v2[i]
|
|
||||||
else
|
|
||||||
res[i] = 0
|
|
||||||
end
|
|
||||||
end
|
|
||||||
return res
|
|
||||||
end
|
|
||||||
|
|
||||||
vector.length=function(v)
|
|
||||||
local res=0
|
|
||||||
for i=1, #v do
|
|
||||||
res = res + (v[i] * v[i])
|
|
||||||
end
|
|
||||||
return math.sqrt(res)
|
|
||||||
end
|
|
||||||
|
|
||||||
vector.floor=function(v)
|
|
||||||
local res={}
|
|
||||||
for i=1, #v do
|
|
||||||
res[i] = math.floor(v[i])
|
|
||||||
end
|
|
||||||
return res
|
|
||||||
end
|
|
||||||
|
|
||||||
vector.convert=function(v)
|
|
||||||
return {x=v[1], y=v[2], z=v[3]}
|
|
||||||
end
|
|
||||||
|
|
||||||
vector.to_minetest=vector.convert
|
|
||||||
|
|
||||||
vector.from_minetest = function(v)
|
|
||||||
return {v.x, v.y, v.z}
|
|
||||||
end
|
|
||||||
|
|
||||||
vector.clamp=function(v, min, max)
|
|
||||||
local res={}
|
|
||||||
for i=1, #v do
|
|
||||||
res[i] = math.max(min, math.min(v[i], max))
|
|
||||||
end
|
|
||||||
return res
|
|
||||||
end
|
|
||||||
|
|
||||||
vector.to_string = function(v)
|
|
||||||
local c = {}
|
|
||||||
for i=1, #v do
|
|
||||||
c[i] = modlib.number.round(v[i], 100)
|
|
||||||
end
|
|
||||||
return table.concat(c, ", ")
|
|
||||||
end
|
|
Loading…
Reference in New Issue