Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Improved VoxelArea variable locality, thus performance
Signed-off-by: Craig Robbins <kde.psych@gmail.com>
  • Loading branch information
Wouters Dorian authored and Zeno- committed Nov 13, 2014
1 parent 874109c commit 92815ad
Showing 1 changed file with 24 additions and 18 deletions.
42 changes: 24 additions & 18 deletions builtin/game/voxelarea.lua
Expand Up @@ -18,10 +18,11 @@ function VoxelArea:new(o)
end

function VoxelArea:getExtent()
local MaxEdge, MinEdge = self.MaxEdge, self.MinEdge
return {
x = self.MaxEdge.x - self.MinEdge.x + 1,
y = self.MaxEdge.y - self.MinEdge.y + 1,
z = self.MaxEdge.z - self.MinEdge.z + 1,
x = MaxEdge.x - MinEdge.x + 1,
y = MaxEdge.y - MinEdge.y + 1,
z = MaxEdge.z - MinEdge.z + 1,
}
end

Expand All @@ -31,45 +32,50 @@ function VoxelArea:getVolume()
end

function VoxelArea:index(x, y, z)
local i = (z - self.MinEdge.z) * self.zstride +
(y - self.MinEdge.y) * self.ystride +
(x - self.MinEdge.x) + 1
local MinEdge = self.MinEdge
local i = (z - MinEdge.z) * self.zstride +
(y - MinEdge.y) * self.ystride +
(x - MinEdge.x) + 1
return math.floor(i)
end

function VoxelArea:indexp(p)
local i = (p.z - self.MinEdge.z) * self.zstride +
(p.y - self.MinEdge.y) * self.ystride +
(p.x - self.MinEdge.x) + 1
local MinEdge = self.MinEdge
local i = (p.z - MinEdge.z) * self.zstride +
(p.y - MinEdge.y) * self.ystride +
(p.x - MinEdge.x) + 1
return math.floor(i)
end

function VoxelArea:position(i)
local p = {}
local MinEdge = self.MinEdge

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

p.y = math.floor(i / self.ystride) + self.MinEdge.y
p.y = math.floor(i / self.ystride) + MinEdge.y
i = i % self.ystride

p.x = math.floor(i) + self.MinEdge.x
p.x = math.floor(i) + MinEdge.x

return p
end

function VoxelArea:contains(x, y, z)
return (x >= self.MinEdge.x) and (x <= self.MaxEdge.x) and
(y >= self.MinEdge.y) and (y <= self.MaxEdge.y) and
(z >= self.MinEdge.z) and (z <= self.MaxEdge.z)
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)
end

function VoxelArea:containsp(p)
return (p.x >= self.MinEdge.x) and (p.x <= self.MaxEdge.x) and
(p.y >= self.MinEdge.y) and (p.y <= self.MaxEdge.y) and
(p.z >= self.MinEdge.z) and (p.z <= self.MaxEdge.z)
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)
end

function VoxelArea:containsi(i)
Expand Down

0 comments on commit 92815ad

Please sign in to comment.