Skip to content

Commit

Permalink
Add VoxelArea:position, VoxelArea:iter and VoxelArea:iterp
Browse files Browse the repository at this point in the history
  • Loading branch information
kahrl committed Jul 11, 2013
1 parent 52beaff commit 6027c8d
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
41 changes: 41 additions & 0 deletions builtin/voxelarea.lua
Expand Up @@ -44,6 +44,22 @@ function VoxelArea:indexp(p)
return math.floor(i)
end

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

i = i - 1

p.z = math.floor(i / self.zstride) + self.MinEdge.z
i = i % self.zstride

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

p.x = math.floor(i) + self.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
Expand All @@ -60,3 +76,28 @@ function VoxelArea:containsi(i)
return (i >= 1) and (i <= self:getVolume())
end

function VoxelArea:iter(minx, miny, minz, maxx, maxy, maxz)
local i = self:index(minx, miny, minz) - 1
local last = self:index(maxx, maxy, maxz)
local ystride = self.ystride
local zstride = self.zstride
local yoff = (last+1) % ystride
local zoff = (last+1) % zstride
local ystridediff = (i - last) % ystride
local zstridediff = (i - last) % zstride
return function()
i = i + 1
if i % zstride == zoff then
i = i + zstridediff
elseif i % ystride == yoff then
i = i + ystridediff
end
if i <= last then
return i
end
end
end

function VoxelArea:iterp(minp, maxp)
return self:iter(minp.x, minp.y, minp.z, maxp.x, maxp.y, maxp.z)
end
7 changes: 7 additions & 0 deletions doc/lua_api.txt
Expand Up @@ -1676,6 +1676,13 @@ methods:
- index(x, y, z): returns the index of an absolute position in a flat array starting at 1
^ useful for things like VoxelManip, raw Schematic specifiers, PerlinNoiseMap:get2d/3dMap, and so on
- indexp(p): same as above, except takes a vector
- position(i): returns the absolute position vector corresponding to index i
- contains(x, y, z): check if (x,y,z) is inside area formed by MinEdge and MaxEdge
- containsp(p): same as above, except takes a vector
- containsi(i): same as above, except takes an index
- iter(minx, miny, minz, maxx, maxy, maxz): returns an iterator that returns indices
^ from (minx,miny,minz) to (maxx,maxy,maxz) in the order of [z [y [x]]]
- iterp(minp, maxp): same as above, except takes a vector

Mapgen objects
---------------
Expand Down

0 comments on commit 6027c8d

Please sign in to comment.