Skip to content

Commit 6027c8d

Browse files
committedJul 11, 2013
Add VoxelArea:position, VoxelArea:iter and VoxelArea:iterp
1 parent 52beaff commit 6027c8d

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed
 

‎builtin/voxelarea.lua

+41
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,22 @@ function VoxelArea:indexp(p)
4444
return math.floor(i)
4545
end
4646

47+
function VoxelArea:position(i)
48+
local p = {}
49+
50+
i = i - 1
51+
52+
p.z = math.floor(i / self.zstride) + self.MinEdge.z
53+
i = i % self.zstride
54+
55+
p.y = math.floor(i / self.ystride) + self.MinEdge.y
56+
i = i % self.ystride
57+
58+
p.x = math.floor(i) + self.MinEdge.x
59+
60+
return p
61+
end
62+
4763
function VoxelArea:contains(x, y, z)
4864
return (x >= self.MinEdge.x) and (x <= self.MaxEdge.x) and
4965
(y >= self.MinEdge.y) and (y <= self.MaxEdge.y) and
@@ -60,3 +76,28 @@ function VoxelArea:containsi(i)
6076
return (i >= 1) and (i <= self:getVolume())
6177
end
6278

79+
function VoxelArea:iter(minx, miny, minz, maxx, maxy, maxz)
80+
local i = self:index(minx, miny, minz) - 1
81+
local last = self:index(maxx, maxy, maxz)
82+
local ystride = self.ystride
83+
local zstride = self.zstride
84+
local yoff = (last+1) % ystride
85+
local zoff = (last+1) % zstride
86+
local ystridediff = (i - last) % ystride
87+
local zstridediff = (i - last) % zstride
88+
return function()
89+
i = i + 1
90+
if i % zstride == zoff then
91+
i = i + zstridediff
92+
elseif i % ystride == yoff then
93+
i = i + ystridediff
94+
end
95+
if i <= last then
96+
return i
97+
end
98+
end
99+
end
100+
101+
function VoxelArea:iterp(minp, maxp)
102+
return self:iter(minp.x, minp.y, minp.z, maxp.x, maxp.y, maxp.z)
103+
end

‎doc/lua_api.txt

+7
Original file line numberDiff line numberDiff line change
@@ -1676,6 +1676,13 @@ methods:
16761676
- index(x, y, z): returns the index of an absolute position in a flat array starting at 1
16771677
^ useful for things like VoxelManip, raw Schematic specifiers, PerlinNoiseMap:get2d/3dMap, and so on
16781678
- indexp(p): same as above, except takes a vector
1679+
- position(i): returns the absolute position vector corresponding to index i
1680+
- contains(x, y, z): check if (x,y,z) is inside area formed by MinEdge and MaxEdge
1681+
- containsp(p): same as above, except takes a vector
1682+
- containsi(i): same as above, except takes an index
1683+
- iter(minx, miny, minz, maxx, maxy, maxz): returns an iterator that returns indices
1684+
^ from (minx,miny,minz) to (maxx,maxy,maxz) in the order of [z [y [x]]]
1685+
- iterp(minp, maxp): same as above, except takes a vector
16791686

16801687
Mapgen objects
16811688
---------------

0 commit comments

Comments
 (0)
Please sign in to comment.