Improved VoxelArea variable locality, thus performance

Signed-off-by: Craig Robbins <kde.psych@gmail.com>
master
Wouters Dorian 2014-11-09 10:25:39 +01:00 committed by Craig Robbins
parent 874109c520
commit 92815ad54b
1 changed files with 24 additions and 18 deletions

View File

@ -18,10 +18,11 @@ function VoxelArea:new(o)
end end
function VoxelArea:getExtent() function VoxelArea:getExtent()
local MaxEdge, MinEdge = self.MaxEdge, self.MinEdge
return { return {
x = self.MaxEdge.x - self.MinEdge.x + 1, x = MaxEdge.x - MinEdge.x + 1,
y = self.MaxEdge.y - self.MinEdge.y + 1, y = MaxEdge.y - MinEdge.y + 1,
z = self.MaxEdge.z - self.MinEdge.z + 1, z = MaxEdge.z - MinEdge.z + 1,
} }
end end
@ -31,45 +32,50 @@ function VoxelArea:getVolume()
end end
function VoxelArea:index(x, y, z) function VoxelArea:index(x, y, z)
local i = (z - self.MinEdge.z) * self.zstride + local MinEdge = self.MinEdge
(y - self.MinEdge.y) * self.ystride + local i = (z - MinEdge.z) * self.zstride +
(x - self.MinEdge.x) + 1 (y - MinEdge.y) * self.ystride +
(x - MinEdge.x) + 1
return math.floor(i) return math.floor(i)
end end
function VoxelArea:indexp(p) function VoxelArea:indexp(p)
local i = (p.z - self.MinEdge.z) * self.zstride + local MinEdge = self.MinEdge
(p.y - self.MinEdge.y) * self.ystride + local i = (p.z - MinEdge.z) * self.zstride +
(p.x - self.MinEdge.x) + 1 (p.y - MinEdge.y) * self.ystride +
(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 p = {}
local MinEdge = self.MinEdge
i = i - 1 i = i - 1
p.z = math.floor(i / self.zstride) + self.MinEdge.z p.z = math.floor(i / self.zstride) + MinEdge.z
i = i % self.zstride i = i % self.zstride
p.y = math.floor(i / self.ystride) + self.MinEdge.y p.y = math.floor(i / self.ystride) + MinEdge.y
i = i % self.ystride i = i % self.ystride
p.x = math.floor(i) + self.MinEdge.x p.x = math.floor(i) + MinEdge.x
return p return p
end end
function VoxelArea:contains(x, y, z) function VoxelArea:contains(x, y, z)
return (x >= self.MinEdge.x) and (x <= self.MaxEdge.x) and local MaxEdge, MinEdge = self.MaxEdge, self.MinEdge
(y >= self.MinEdge.y) and (y <= self.MaxEdge.y) and return (x >= MinEdge.x) and (x <= MaxEdge.x) and
(z >= self.MinEdge.z) and (z <= self.MaxEdge.z) (y >= MinEdge.y) and (y <= MaxEdge.y) and
(z >= MinEdge.z) and (z <= MaxEdge.z)
end end
function VoxelArea:containsp(p) function VoxelArea:containsp(p)
return (p.x >= self.MinEdge.x) and (p.x <= self.MaxEdge.x) and local MaxEdge, MinEdge = self.MaxEdge, self.MinEdge
(p.y >= self.MinEdge.y) and (p.y <= self.MaxEdge.y) and return (p.x >= MinEdge.x) and (p.x <= MaxEdge.x) and
(p.z >= self.MinEdge.z) and (p.z <= self.MaxEdge.z) (p.y >= MinEdge.y) and (p.y <= MaxEdge.y) and
(p.z >= MinEdge.z) and (p.z <= MaxEdge.z)
end end
function VoxelArea:containsi(i) function VoxelArea:containsi(i)