Make `VoxelArea:position()` return a vector with metatable (#12521)

master
AFCMS 2022-07-09 22:32:33 +02:00 committed by GitHub
parent e51f474613
commit fae8726a76
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 12 additions and 10 deletions

View File

@ -1,6 +1,9 @@
local math_floor = math.floor
local vector_new = vector.new
VoxelArea = { VoxelArea = {
MinEdge = vector.new(1, 1, 1), MinEdge = vector_new(1, 1, 1),
MaxEdge = vector.new(0, 0, 0), MaxEdge = vector_new(0, 0, 0),
ystride = 0, ystride = 0,
zstride = 0, zstride = 0,
} }
@ -19,7 +22,7 @@ end
function VoxelArea:getExtent() function VoxelArea:getExtent()
local MaxEdge, MinEdge = self.MaxEdge, self.MinEdge local MaxEdge, MinEdge = self.MaxEdge, self.MinEdge
return vector.new( return vector_new(
MaxEdge.x - MinEdge.x + 1, MaxEdge.x - MinEdge.x + 1,
MaxEdge.y - MinEdge.y + 1, MaxEdge.y - MinEdge.y + 1,
MaxEdge.z - MinEdge.z + 1 MaxEdge.z - MinEdge.z + 1
@ -36,7 +39,7 @@ function VoxelArea:index(x, y, z)
local i = (z - MinEdge.z) * self.zstride + local i = (z - MinEdge.z) * self.zstride +
(y - MinEdge.y) * self.ystride + (y - MinEdge.y) * self.ystride +
(x - MinEdge.x) + 1 (x - MinEdge.x) + 1
return math.floor(i) return math_floor(i)
end end
function VoxelArea:indexp(p) function VoxelArea:indexp(p)
@ -44,24 +47,23 @@ function VoxelArea:indexp(p)
local i = (p.z - MinEdge.z) * self.zstride + local i = (p.z - MinEdge.z) * self.zstride +
(p.y - MinEdge.y) * self.ystride + (p.y - MinEdge.y) * self.ystride +
(p.x - MinEdge.x) + 1 (p.x - MinEdge.x) + 1
return math.floor(i) return math_floor(i)
end end
function VoxelArea:position(i) function VoxelArea:position(i)
local p = {}
local MinEdge = self.MinEdge local MinEdge = self.MinEdge
i = i - 1 i = i - 1
p.z = math.floor(i / self.zstride) + MinEdge.z local z = math_floor(i / self.zstride) + MinEdge.z
i = i % self.zstride i = i % self.zstride
p.y = math.floor(i / self.ystride) + MinEdge.y local y = math_floor(i / self.ystride) + MinEdge.y
i = i % self.ystride i = i % self.ystride
p.x = math.floor(i) + MinEdge.x local x = math_floor(i) + MinEdge.x
return p return vector_new(x, y, z)
end end
function VoxelArea:contains(x, y, z) function VoxelArea:contains(x, y, z)