forked from mcedit/pymclevel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
box.py
229 lines (167 loc) · 6.46 KB
/
box.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import itertools
class BoundingBox (object):
type = int
def __init__(self, origin=(0, 0, 0), size=(0, 0, 0)):
if isinstance(origin, BoundingBox):
self._origin = list(origin._origin)
self._size = list(origin._size)
else:
self._origin, self._size = list(map(self.type, origin)), list(map(self.type, size))
def chunkBox(self, level):
"""Returns this box extended to the chunk boundaries of the given level"""
box = self
return BoundingBox((box.mincx << 4, 0, box.mincz << 4), (box.maxcx - box.mincx << 4, level.Height, box.maxcz - box.mincz << 4))
def getMinx(self):
return self.origin[0]
def getMiny(self):
return self.origin[1]
def getMinz(self):
return self.origin[2]
def getMaxx(self):
return self.origin[0] + self.size[0]
def getMaxy(self):
return self.origin[1] + self.size[1]
def getMaxz(self):
return self.origin[2] + self.size[2]
def setMinx(self, x):
self.size[0] -= x - self.origin[0]
self.origin[0] = x
def setMiny(self, y):
self.size[1] -= y - self.origin[1]
self.origin[1] = y
def setMinz(self, z):
self.size[2] -= z - self.origin[2]
self.origin[2] = z
def setMaxx(self, x):
if x < self.origin[0]:
x = self.origin[0]
self.size[0] = x - self.origin[0]
def setMaxy(self, y):
if y < self.origin[1]:
y = self.origin[1]
self.size[1] = y - self.origin[1]
def setMaxz(self, z):
if z < self.origin[2]:
z = self.origin[2]
self.size[2] = z - self.origin[2]
minx = property(getMinx, setMinx)
miny = property(getMiny, setMiny)
minz = property(getMinz, setMinz)
maxx = property(getMaxx, setMaxx)
maxy = property(getMaxy, setMaxy)
maxz = property(getMaxz, setMaxz)
def getMincx(self):
return self.origin[0] >> 4
def getMincz(self):
return self.origin[2] >> 4
def getMaxcx(self):
return ((self.origin[0] + self.size[0] - 1) >> 4) + 1
def getMaxcz(self):
return ((self.origin[2] + self.size[2] - 1) >> 4) + 1
mincx = property(getMincx, None, None, "The smallest chunk position contained in this box")
mincz = property(getMincz, None, None, "The smallest chunk position contained in this box")
maxcx = property(getMaxcx, None, None, "The largest chunk position contained in this box")
maxcz = property(getMaxcz, None, None, "The largest chunk position contained in this box")
def getOrigin(self):
return self._origin
def setOrigin(self, o):
self._origin = list(o)
def getSize(self):
return self._size
def setSize(self, s):
self._size = list(s)
origin = property(getOrigin, setOrigin)
size = property(getSize, setSize)
def getWidth(self):
return self._size[0]
def getHeight(self):
return self._size[1]
def getLength(self):
return self._size[2]
def setWidth(self, w):
self.size[0] = max(0, self.type(w))
def setHeight(self, h):
self.size[1] = max(0, self.type(h))
def setLength(self, l):
self.size[2] = max(0, self.type(l))
width = property(getWidth, setWidth, None, "The dimension along the X axis")
height = property(getHeight, setHeight, None, "The dimension along the Y axis")
length = property(getLength, setLength, None, "The dimension along the Z axis")
def getMaximum(self):
return map(lambda a, b: a + b, self._origin, self._size)
maximum = property(getMaximum, None, None, "The endpoint of the box; origin plus size.")
def getVolume(self):
return reduce(lambda a, b: a * b, self.size)
volume = property(getVolume, None, None, "The volume of the box in blocks")
@property
def chunkPositions(self):
#iterate through all of the chunk positions within this selection box
return itertools.product(xrange(self.mincx, self.maxcx), xrange(self.mincz, self.maxcz))
@property
def positions(self):
"""iterate through all of the positions within this selection box"""
return itertools.product(
xrange(self.minx, self.maxx),
xrange(self.miny, self.maxy),
xrange(self.minz, self.maxz)
)
@property
def chunkCount(self):
return (self.maxcx - self.mincx) * (self.maxcz - self.mincz)
@property
def isChunkAligned(self):
return (self.origin[0] & 0xf == 0) and (self.origin[2] & 0xf == 0)
def intersect(self, box):
""" return a box containing the area self and box have in common"""
newbox = BoundingBox()
if self.minx > box.maxx or self.maxx < box.minx:
return BoundingBox()
newbox.minx = max(self.minx, box.minx)
newbox.maxx = min(self.maxx, box.maxx)
if self.miny > box.maxy or self.maxy < box.miny:
return BoundingBox()
newbox.miny = max(self.miny, box.miny)
newbox.maxy = min(self.maxy, box.maxy)
if self.minz > box.maxz or self.maxz < box.minz:
return BoundingBox()
newbox.minz = max(self.minz, box.minz)
newbox.maxz = min(self.maxz, box.maxz)
#print "Intersect of {0} and {1}: {2}".format(self, box, newbox)
return newbox
def union(self, box):
newbox = BoundingBox()
newbox.minx = min(self.minx, box.minx)
newbox.maxx = max(self.maxx, box.maxx)
newbox.miny = min(self.miny, box.miny)
newbox.maxy = max(self.maxy, box.maxy)
newbox.minz = min(self.minz, box.minz)
newbox.maxz = max(self.maxz, box.maxz)
return newbox
def expand(self, dx, dy=None, dz=None):
if dz is None:
dz = dx
if dy is None:
dy = dx
box = BoundingBox(self)
box.minx -= dx
box.miny -= dy
box.minz -= dz
box.maxx += dx
box.maxy += dy
box.maxz += dz
return box
def __contains__(self, pos):
x, y, z = pos
if x < self.minx or x >= self.maxx:
return False
if y < self.miny or y >= self.maxy:
return False
if z < self.minz or z >= self.maxz:
return False
return True
def __cmp__(self, b):
return cmp((self.origin, self.size), (b.origin, b.size))
def __repr__(self):
return "BoundingBox({0}, {1})".format(self.origin, self.size)
class FloatBox (BoundingBox):
type = float