2022-07-09 22:32:33 +02:00
|
|
|
local math_floor = math.floor
|
|
|
|
local vector_new = vector.new
|
|
|
|
|
2013-06-28 16:28:15 +02:00
|
|
|
VoxelArea = {
|
2022-07-09 22:32:33 +02:00
|
|
|
MinEdge = vector_new(1, 1, 1),
|
|
|
|
MaxEdge = vector_new(0, 0, 0),
|
2013-06-28 16:28:15 +02:00
|
|
|
ystride = 0,
|
|
|
|
zstride = 0,
|
|
|
|
}
|
|
|
|
|
2022-10-22 14:05:45 +02:00
|
|
|
local class_metatable = {}
|
|
|
|
setmetatable(VoxelArea, class_metatable)
|
|
|
|
|
|
|
|
local function new(self, o)
|
2013-06-28 16:28:15 +02:00
|
|
|
o = o or {}
|
|
|
|
setmetatable(o, self)
|
|
|
|
self.__index = self
|
|
|
|
|
|
|
|
local e = o:getExtent()
|
|
|
|
o.ystride = e.x
|
|
|
|
o.zstride = e.x * e.y
|
|
|
|
|
|
|
|
return o
|
|
|
|
end
|
|
|
|
|
2022-10-22 14:05:45 +02:00
|
|
|
function class_metatable:__call(MinEdge, MaxEdge)
|
|
|
|
return new(self, {MinEdge = MinEdge, MaxEdge = MaxEdge})
|
|
|
|
end
|
|
|
|
|
|
|
|
VoxelArea.new = new
|
|
|
|
|
2013-06-28 16:28:15 +02:00
|
|
|
function VoxelArea:getExtent()
|
2014-11-09 10:25:39 +01:00
|
|
|
local MaxEdge, MinEdge = self.MaxEdge, self.MinEdge
|
2022-07-09 22:32:33 +02:00
|
|
|
return vector_new(
|
2021-06-04 21:22:33 +02:00
|
|
|
MaxEdge.x - MinEdge.x + 1,
|
|
|
|
MaxEdge.y - MinEdge.y + 1,
|
|
|
|
MaxEdge.z - MinEdge.z + 1
|
|
|
|
)
|
2013-06-28 16:28:15 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function VoxelArea:getVolume()
|
|
|
|
local e = self:getExtent()
|
|
|
|
return e.x * e.y * e.z
|
|
|
|
end
|
|
|
|
|
|
|
|
function VoxelArea:index(x, y, z)
|
2014-11-09 10:25:39 +01:00
|
|
|
local MinEdge = self.MinEdge
|
|
|
|
local i = (z - MinEdge.z) * self.zstride +
|
|
|
|
(y - MinEdge.y) * self.ystride +
|
|
|
|
(x - MinEdge.x) + 1
|
2022-07-09 22:32:33 +02:00
|
|
|
return math_floor(i)
|
2013-06-28 16:28:15 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function VoxelArea:indexp(p)
|
2014-11-09 10:25:39 +01:00
|
|
|
local MinEdge = self.MinEdge
|
|
|
|
local i = (p.z - MinEdge.z) * self.zstride +
|
|
|
|
(p.y - MinEdge.y) * self.ystride +
|
|
|
|
(p.x - MinEdge.x) + 1
|
2022-07-09 22:32:33 +02:00
|
|
|
return math_floor(i)
|
2013-06-28 16:28:15 +02:00
|
|
|
end
|
|
|
|
|
2013-07-09 16:26:52 +02:00
|
|
|
function VoxelArea:position(i)
|
2014-11-09 10:25:39 +01:00
|
|
|
local MinEdge = self.MinEdge
|
2016-10-08 14:42:17 +02:00
|
|
|
|
2013-07-09 16:26:52 +02:00
|
|
|
i = i - 1
|
|
|
|
|
2022-07-09 22:32:33 +02:00
|
|
|
local z = math_floor(i / self.zstride) + MinEdge.z
|
2013-07-09 16:26:52 +02:00
|
|
|
i = i % self.zstride
|
|
|
|
|
2022-07-09 22:32:33 +02:00
|
|
|
local y = math_floor(i / self.ystride) + MinEdge.y
|
2013-07-09 16:26:52 +02:00
|
|
|
i = i % self.ystride
|
|
|
|
|
2022-07-09 22:32:33 +02:00
|
|
|
local x = math_floor(i) + MinEdge.x
|
2013-07-09 16:26:52 +02:00
|
|
|
|
2022-07-09 22:32:33 +02:00
|
|
|
return vector_new(x, y, z)
|
2013-07-09 16:26:52 +02:00
|
|
|
end
|
|
|
|
|
2013-06-29 04:52:13 +02:00
|
|
|
function VoxelArea:contains(x, y, z)
|
2014-11-09 10:25:39 +01:00
|
|
|
local MaxEdge, MinEdge = self.MaxEdge, self.MinEdge
|
|
|
|
return (x >= MinEdge.x) and (x <= MaxEdge.x) and
|
|
|
|
(y >= MinEdge.y) and (y <= MaxEdge.y) and
|
|
|
|
(z >= MinEdge.z) and (z <= MaxEdge.z)
|
2013-06-29 04:52:13 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function VoxelArea:containsp(p)
|
2014-11-09 10:25:39 +01:00
|
|
|
local MaxEdge, MinEdge = self.MaxEdge, self.MinEdge
|
|
|
|
return (p.x >= MinEdge.x) and (p.x <= MaxEdge.x) and
|
|
|
|
(p.y >= MinEdge.y) and (p.y <= MaxEdge.y) and
|
|
|
|
(p.z >= MinEdge.z) and (p.z <= MaxEdge.z)
|
2013-06-29 04:52:13 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function VoxelArea:containsi(i)
|
|
|
|
return (i >= 1) and (i <= self:getVolume())
|
|
|
|
end
|
|
|
|
|
2013-07-09 16:26:52 +02:00
|
|
|
function VoxelArea:iter(minx, miny, minz, maxx, maxy, maxz)
|
|
|
|
local i = self:index(minx, miny, minz) - 1
|
2016-10-08 14:42:17 +02:00
|
|
|
local xrange = maxx - minx + 1
|
|
|
|
local nextaction = i + 1 + xrange
|
|
|
|
|
|
|
|
local y = 0
|
|
|
|
local yrange = maxy - miny + 1
|
|
|
|
local yreqstride = self.ystride - xrange
|
|
|
|
|
|
|
|
local z = 0
|
|
|
|
local zrange = maxz - minz + 1
|
|
|
|
local multistride = self.zstride - ((yrange - 1) * self.ystride + xrange)
|
|
|
|
|
2013-07-09 16:26:52 +02:00
|
|
|
return function()
|
2016-10-08 14:42:17 +02:00
|
|
|
-- continue i until it needs to jump
|
2013-07-09 16:26:52 +02:00
|
|
|
i = i + 1
|
2016-10-08 14:42:17 +02:00
|
|
|
if i ~= nextaction then
|
|
|
|
return i
|
2013-07-09 16:26:52 +02:00
|
|
|
end
|
2016-10-08 14:42:17 +02:00
|
|
|
|
|
|
|
-- continue y until maxy is exceeded
|
|
|
|
y = y + 1
|
|
|
|
if y ~= yrange then
|
|
|
|
-- set i to index(minx, miny + y, minz + z) - 1
|
|
|
|
i = i + yreqstride
|
|
|
|
nextaction = i + xrange
|
2013-07-09 16:26:52 +02:00
|
|
|
return i
|
|
|
|
end
|
2016-10-08 14:42:17 +02:00
|
|
|
|
|
|
|
-- continue z until maxz is exceeded
|
|
|
|
z = z + 1
|
|
|
|
if z == zrange then
|
|
|
|
-- cuboid finished, return nil
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
-- set i to index(minx, miny, minz + z) - 1
|
|
|
|
i = i + multistride
|
|
|
|
|
|
|
|
y = 0
|
|
|
|
nextaction = i + xrange
|
|
|
|
return i
|
2013-07-09 16:26:52 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function VoxelArea:iterp(minp, maxp)
|
|
|
|
return self:iter(minp.x, minp.y, minp.z, maxp.x, maxp.y, maxp.z)
|
|
|
|
end
|