voxelarea.lua 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. local math_floor = math.floor
  2. local vector_new = vector.new
  3. VoxelArea = {
  4. MinEdge = vector_new(1, 1, 1),
  5. MaxEdge = vector_new(0, 0, 0),
  6. ystride = 0,
  7. zstride = 0,
  8. }
  9. local class_metatable = {}
  10. setmetatable(VoxelArea, class_metatable)
  11. local function new(self, o)
  12. o = o or {}
  13. setmetatable(o, self)
  14. self.__index = self
  15. local e = o:getExtent()
  16. o.ystride = e.x
  17. o.zstride = e.x * e.y
  18. return o
  19. end
  20. function class_metatable:__call(MinEdge, MaxEdge)
  21. return new(self, {MinEdge = MinEdge, MaxEdge = MaxEdge})
  22. end
  23. VoxelArea.new = new
  24. function VoxelArea:getExtent()
  25. local MaxEdge, MinEdge = self.MaxEdge, self.MinEdge
  26. return vector_new(
  27. MaxEdge.x - MinEdge.x + 1,
  28. MaxEdge.y - MinEdge.y + 1,
  29. MaxEdge.z - MinEdge.z + 1
  30. )
  31. end
  32. function VoxelArea:getVolume()
  33. local e = self:getExtent()
  34. return e.x * e.y * e.z
  35. end
  36. function VoxelArea:index(x, y, z)
  37. local MinEdge = self.MinEdge
  38. local i = (z - MinEdge.z) * self.zstride +
  39. (y - MinEdge.y) * self.ystride +
  40. (x - MinEdge.x) + 1
  41. return math_floor(i)
  42. end
  43. function VoxelArea:indexp(p)
  44. local MinEdge = self.MinEdge
  45. local i = (p.z - MinEdge.z) * self.zstride +
  46. (p.y - MinEdge.y) * self.ystride +
  47. (p.x - MinEdge.x) + 1
  48. return math_floor(i)
  49. end
  50. function VoxelArea:position(i)
  51. local MinEdge = self.MinEdge
  52. i = i - 1
  53. local z = math_floor(i / self.zstride) + MinEdge.z
  54. i = i % self.zstride
  55. local y = math_floor(i / self.ystride) + MinEdge.y
  56. i = i % self.ystride
  57. local x = math_floor(i) + MinEdge.x
  58. return vector_new(x, y, z)
  59. end
  60. function VoxelArea:contains(x, y, z)
  61. local MaxEdge, MinEdge = self.MaxEdge, self.MinEdge
  62. return (x >= MinEdge.x) and (x <= MaxEdge.x) and
  63. (y >= MinEdge.y) and (y <= MaxEdge.y) and
  64. (z >= MinEdge.z) and (z <= MaxEdge.z)
  65. end
  66. function VoxelArea:containsp(p)
  67. local MaxEdge, MinEdge = self.MaxEdge, self.MinEdge
  68. return (p.x >= MinEdge.x) and (p.x <= MaxEdge.x) and
  69. (p.y >= MinEdge.y) and (p.y <= MaxEdge.y) and
  70. (p.z >= MinEdge.z) and (p.z <= MaxEdge.z)
  71. end
  72. function VoxelArea:containsi(i)
  73. return (i >= 1) and (i <= self:getVolume())
  74. end
  75. function VoxelArea:iter(minx, miny, minz, maxx, maxy, maxz)
  76. local i = self:index(minx, miny, minz) - 1
  77. local xrange = maxx - minx + 1
  78. local nextaction = i + 1 + xrange
  79. local y = 0
  80. local yrange = maxy - miny + 1
  81. local yreqstride = self.ystride - xrange
  82. local z = 0
  83. local zrange = maxz - minz + 1
  84. local multistride = self.zstride - ((yrange - 1) * self.ystride + xrange)
  85. return function()
  86. -- continue i until it needs to jump
  87. i = i + 1
  88. if i ~= nextaction then
  89. return i
  90. end
  91. -- continue y until maxy is exceeded
  92. y = y + 1
  93. if y ~= yrange then
  94. -- set i to index(minx, miny + y, minz + z) - 1
  95. i = i + yreqstride
  96. nextaction = i + xrange
  97. return i
  98. end
  99. -- continue z until maxz is exceeded
  100. z = z + 1
  101. if z == zrange then
  102. -- cuboid finished, return nil
  103. return
  104. end
  105. -- set i to index(minx, miny, minz + z) - 1
  106. i = i + multistride
  107. y = 0
  108. nextaction = i + xrange
  109. return i
  110. end
  111. end
  112. function VoxelArea:iterp(minp, maxp)
  113. return self:iter(minp.x, minp.y, minp.z, maxp.x, maxp.y, maxp.z)
  114. end