some changes

This commit is contained in:
HybridDog 2015-04-25 09:54:07 +02:00
parent 02291fcb65
commit 1db996a040

112
init.lua

@ -1,6 +1,8 @@
local load_time_start = os.clock() local load_time_start = os.clock()
function vector.pos_to_string(pos) local funcs = {}
function funcs.pos_to_string(pos)
return "{x="..pos.x.."; y="..pos.y.."; z="..pos.z.."}" return "{x="..pos.x.."; y="..pos.y.."; z="..pos.z.."}"
end end
@ -118,7 +120,7 @@ local function return_fine_line(pos, dir, range, scale)
return ps2 return ps2
end end
function vector.fine_line(pos, dir, range, scale) function funcs.fine_line(pos, dir, range, scale)
--assert_vector(pos) --assert_vector(pos)
if not range then --dir = pos2 if not range then --dir = pos2
dir = vector.direction(pos, dir) dir = vector.direction(pos, dir)
@ -127,7 +129,7 @@ function vector.fine_line(pos, dir, range, scale)
return return_fine_line(pos, dir, range, scale) return return_fine_line(pos, dir, range, scale)
end end
function vector.line(pos, dir, range, alt) function funcs.line(pos, dir, range, alt)
--assert_vector(pos) --assert_vector(pos)
if alt then if alt then
if not range then --dir = pos2 if not range then --dir = pos2
@ -149,7 +151,7 @@ function vector.line(pos, dir, range, alt)
end end
local twolines = {} local twolines = {}
function vector.twoline(x, y) function funcs.twoline(x, y)
local pstr = x.." "..y local pstr = x.." "..y
local line = twolines[pstr] local line = twolines[pstr]
if line then if line then
@ -183,7 +185,7 @@ function vector.twoline(x, y)
end end
local threelines = {} local threelines = {}
function vector.threeline(x, y, z) function funcs.threeline(x, y, z)
local pstr = x.." "..y.." "..z local pstr = x.." "..y.." "..z
local line = threelines[pstr] local line = threelines[pstr]
if line then if line then
@ -218,7 +220,7 @@ function vector.threeline(x, y, z)
return line return line
end end
function vector.sort(ps, preferred_coords) function funcs.sort(ps, preferred_coords)
preferred_coords = preferred_coords or {"z", "y", "x"} preferred_coords = preferred_coords or {"z", "y", "x"}
local a,b,c = unpack(preferred_coords) local a,b,c = unpack(preferred_coords)
local function ps_sorting(p1, p2) local function ps_sorting(p1, p2)
@ -238,11 +240,11 @@ function vector.sort(ps, preferred_coords)
return ps return ps
end end
function vector.scalarproduct(v1, v2) function funcs.scalar(v1, v2)
return v1.x*v2.x + v1.y*v2.y + v1.z*v2.z return v1.x*v2.x + v1.y*v2.y + v1.z*v2.z
end end
function vector.crossproduct(v1, v2) function funcs.cross(v1, v2)
return { return {
x = v1.y*v2.z - v1.z*v2.y, x = v1.y*v2.z - v1.z*v2.y,
y = v1.z*v2.x - v1.x*v2.z, y = v1.z*v2.x - v1.x*v2.z,
@ -250,23 +252,10 @@ function vector.crossproduct(v1, v2)
} }
end end
function vector.mirror(pos, v, vb) --not optimized
if vb then
v = vector.normalize(vector.crossproduct(v, vb))
end
local dx,dy,dz = v.x,v.y,v.z
local x,y,z = pos.x,pos.y,pos.z
local dif = (dx*x+dy*y+dz*z)/(dx*dx+dy*dy+dz*dz)
if dif == 0 then
return pos
end
dif = -dif*2
return vector.add(pos, vector.multiply(v, dif))
end
--local areas = {} --local areas = {}
function vector.area(ps) function funcs.plane(ps)
-- sort positions and imagine the first one as vector.zero -- sort positions and imagine the first one (A) as vector.zero
ps = vector.sort(ps) ps = vector.sort(ps)
local pos = ps[1] local pos = ps[1]
local B = vector.subtract(ps[2], pos) local B = vector.subtract(ps[2], pos)
@ -279,69 +268,48 @@ function vector.area(ps)
cube_p1[i] = math.min(B[i], C[i], 0) cube_p1[i] = math.min(B[i], C[i], 0)
cube_p2[i] = math.max(B[i], C[i], 0) cube_p2[i] = math.max(B[i], C[i], 0)
end end
cube_p1 = vector.apply(cube_p1, math.floor)
cube_p2 = vector.apply(cube_p2, math.ceil)
local vn = vector.normalize(vector.crossproduct(B, C)) local vn = vector.normalize(vector.cross(B, C))
local nAB = vector.normalize(B) local nAB = vector.normalize(B)
local nAC = vector.normalize(C) local nAC = vector.normalize(C)
local angle_BAC = math.acos(vector.scalarproduct(nAB, nAC)) local angle_BAC = math.acos(vector.scalar(nAB, nAC))
local nBA = vector.multiply(nAB, -1) local nBA = vector.multiply(nAB, -1)
local nBC = vector.normalize(vector.subtract(C, B)) local nBC = vector.normalize(vector.subtract(C, B))
local angle_ABC = math.acos(vector.scalarproduct(nBA, nBC)) local angle_ABC = math.acos(vector.scalar(nBA, nBC))
local area = {}
for z = cube_p1.z, cube_p2.z do for z = cube_p1.z, cube_p2.z do
for y = cube_p1.y, cube_p2.y do for y = cube_p1.y, cube_p2.y do
for x = cube_p1.x, cube_p2.x do for x = cube_p1.x, cube_p2.x do
local p = {x=x, y=y, z=z} local p = {x=x, y=y, z=z}
local d = { local n = -vector.scalar(p, vn)/vector.scalar(vn, vn)
x = (p.y*vn.y+p.z*vn.z)/vn.x+p.x, if math.abs(n) <= 0.5 then
y = (p.x*vn.x+p.z*vn.z)/vn.y+p.y, local ep = vector.add(p, vector.multiply(vn, n))
z = (p.x*vn.x+p.y*vn.y)/vn.z+p.z,
}
local dmin = math.min(math.abs(d.x), math.abs(d.y), math.abs(d.z))
if dmin <= 0.5 then
--[[ep.x*vn.x+ep.y*vn.y+ep.z*vn.z = 0
ep.x = p.x+n*vn.x
ep.y = p.y+n*vn.y
ep.z = p.z+n*vn.z
(p.x+n*vn.x)*vn.x+(p.y+n*vn.y)*vn.y+(p.z+n*vn.z)*vn.z = 0
p.x*vn.x+n*vn.x*vn.x+p.y*vn.y+n*vn.y*vn.y+p.z*vn.z+n*vn.z*vn.z = 0
n*vn.x*vn.x+n*vn.y*vn.y+n*vn.z*vn.z = -(p.x*vn.x+p.y*vn.y+p.z*vn.z)
n*(vn.x*vn.x+vn.y*vn.y+vn.z*vn.z) = -(p.x*vn.x+p.y*vn.y+p.z*vn.z)--]]
n = -(p.x*vn.x+p.y*vn.y+p.z*vn.z)/(vn.x*vn.x+vn.y*vn.y+vn.z*vn.z)
local ep = vector.new(p)
ep = vector.add(ep, vector.multiply(vn, n))
--[[for n,i in pairs(d) do
if math.abs(i) == dmin then
ep[n] = ep[n]+i
break
end
end--]]
local nep = vector.normalize(ep) local nep = vector.normalize(ep)
local angle_BAep = math.acos(vector.scalarproduct(nAB, nep)) local angle_BAep = math.acos(vector.scalar(nAB, nep))
local angle_CAep = math.acos(vector.scalarproduct(nAC, nep)) local angle_CAep = math.acos(vector.scalar(nAC, nep))
local angldif = angle_BAC - (angle_BAep+angle_CAep) local angldif = angle_BAC - (angle_BAep+angle_CAep)
if math.abs(angldif) < 0.01 then if math.abs(angldif) < 0.001 then
ep = vector.subtract(ep, B) ep = vector.subtract(ep, B)
nep = vector.normalize(ep) nep = vector.normalize(ep)
local angle_ABep = math.acos(vector.scalarproduct(nBA, nep)) local angle_ABep = math.acos(vector.scalar(nBA, nep))
local angle_CBep = math.acos(vector.scalarproduct(nBC, nep)) local angle_CBep = math.acos(vector.scalar(nBC, nep))
local angldif = angle_ABC - (angle_ABep+angle_CBep) local angldif = angle_ABC - (angle_ABep+angle_CBep)
if math.abs(angldif) < 0.01 then if math.abs(angldif) < 0.001 then
table.insert(area, vector.add(pos, p)) table.insert(ps, vector.add(pos, p))
end end
end end
end end
end end
end end
end end
return area return ps
end end
function vector.straightdelay(s, v, a) function funcs.straightdelay(s, v, a)
if not a then if not a then
return s/v return s/v
end end
@ -350,7 +318,7 @@ end
vector.zero = {x=0, y=0, z=0} vector.zero = {x=0, y=0, z=0}
function vector.sun_dir(time) function funcs.sun_dir(time)
if not time then if not time then
time = minetest.get_timeofday() time = minetest.get_timeofday()
end end
@ -363,7 +331,7 @@ function vector.sun_dir(time)
return {x=tmp, y=math.sqrt(1-tmp*tmp), z=0} return {x=tmp, y=math.sqrt(1-tmp*tmp), z=0}
end end
function vector.inside(pos, minp, maxp) function funcs.inside(pos, minp, maxp)
for _,i in pairs({"x", "y", "z"}) do for _,i in pairs({"x", "y", "z"}) do
if pos[i] < minp[i] if pos[i] < minp[i]
or pos[i] > maxp[i] then or pos[i] > maxp[i] then
@ -373,7 +341,7 @@ function vector.inside(pos, minp, maxp)
return true return true
end end
function vector.minmax(p1, p2) function funcs.minmax(p1, p2)
local p1 = vector.new(p1) local p1 = vector.new(p1)
local p2 = vector.new(p2) local p2 = vector.new(p2)
for _,i in ipairs({"x", "y", "z"}) do for _,i in ipairs({"x", "y", "z"}) do
@ -384,7 +352,7 @@ function vector.minmax(p1, p2)
return p1, p2 return p1, p2
end end
function vector.move(p1, p2, s) function funcs.move(p1, p2, s)
return vector.round( return vector.round(
vector.add( vector.add(
vector.multiply( vector.multiply(
@ -400,7 +368,7 @@ function vector.move(p1, p2, s)
end end
local explosion_tables = {} local explosion_tables = {}
function vector.explosion_table(r) function funcs.explosion_table(r)
local table = explosion_tables[r] local table = explosion_tables[r]
if table then if table then
return table return table
@ -432,7 +400,7 @@ function vector.explosion_table(r)
end end
local circle_tables = {} local circle_tables = {}
function vector.circle(r) function funcs.circle(r)
local table = circle_tables[r] local table = circle_tables[r]
if table then if table then
return table return table
@ -455,7 +423,7 @@ function vector.circle(r)
end end
local ring_tables = {} local ring_tables = {}
function vector.ring(r) function funcs.ring(r)
local table = ring_tables[r] local table = ring_tables[r]
if table then if table then
return table return table
@ -496,10 +464,14 @@ function vector.ring(r)
return tab2 return tab2
end end
function vector.chunkcorner(pos) function funcs.chunkcorner(pos)
return {x=pos.x-pos.x%16, y=pos.y-pos.y%16, z=pos.z-pos.z%16} return {x=pos.x-pos.x%16, y=pos.y-pos.y%16, z=pos.z-pos.z%16}
end end
dofile(minetest.get_modpath("vector_extras").."/vector_meta.lua") dofile(minetest.get_modpath("vector_extras").."/vector_meta.lua")
for name,func in pairs(funcs) do
vector[name] = vector[name] or func
end
minetest.log("info", string.format("[vector_extras] loaded after ca. %.2fs", os.clock() - load_time_start)) minetest.log("info", string.format("[vector_extras] loaded after ca. %.2fs", os.clock() - load_time_start))