forked from mcedit/pymclevel
-
Notifications
You must be signed in to change notification settings - Fork 1
/
box.py
236 lines (185 loc) · 6.5 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
230
231
232
233
234
235
236
from collections import namedtuple
import itertools
import math
_Vector = namedtuple("_Vector", ("x", "y", "z"))
class Vector(_Vector):
__slots__ = ()
def __add__(self, other):
return Vector(self[0] + other[0], self[1] + other[1], self[2] + other[2])
def __sub__(self, other):
return Vector(self[0] - other[0], self[1] - other[1], self[2] - other[2])
def __mul__(self, other):
if isinstance(other, (int, float)):
return Vector(self[0] * other, self[1] * other, self[2] * other)
return Vector(self[0] * other[0], self[1] * other[1], self[2] * other[2])
def __truediv__(self, other):
if isinstance(other, (int, float)):
return Vector(self[0] / other, self[1] / other, self[2] / other)
return Vector(self[0] / other[0], self[1] / other[1], self[2] / other[2])
__div__ = __truediv__
def length(self):
return math.sqrt(self[0] * self[0] + self[1] * self[1] + self[2] * self[2])
def normalize(self):
l = self.length()
if l == 0: return self
return self / l
def intfloor(self):
return Vector(*[int(math.floor(p)) for p in self])
class BoundingBox (object):
type = int
def __init__(self, origin=(0, 0, 0), size=(0, 0, 0)):
if isinstance(origin, BoundingBox):
self._origin = origin._origin
self._size = origin._size
else:
self._origin, self._size = Vector(*(self.type(a) for a in origin)), Vector(*(self.type(a) for a in size))
def __repr__(self):
return "BoundingBox({0}, {1})".format(self.origin, self.size)
@property
def origin(self):
"The smallest position in the box"
return self._origin
@property
def size(self):
"The size of the box"
return self._size
@property
def width(self):
"The dimension along the X axis"
return self._size.x
@property
def height(self):
"The dimension along the Y axis"
return self._size.y
@property
def length(self):
"The dimension along the Z axis"
return self._size.z
@property
def minx(self):
return self.origin.x
@property
def miny(self):
return self.origin.y
@property
def minz(self):
return self.origin.z
@property
def maxx(self):
return self.origin.x + self.size.x
@property
def maxy(self):
return self.origin.y + self.size.y
@property
def maxz(self):
return self.origin.z + self.size.z
@property
def maximum(self):
"The largest point of the box; origin plus size."
return self._origin + self._size
@property
def volume(self):
"The volume of the box in blocks"
return self.size.x * self.size.y * self.size.z
@property
def positions(self):
"""iterate through all of the positions within this selection box"""
return itertools.product(
range(self.minx, self.maxx),
range(self.miny, self.maxy),
range(self.minz, self.maxz)
)
def intersect(self, box):
"""
Return a box containing the area self and box have in common. Box will have zero volume
if there is no common area.
"""
if (self.minx > box.maxx or self.maxx < box.minx or
self.miny > box.maxy or self.maxy < box.miny or
self.minz > box.maxz or self.maxz < box.minz):
#Zero size intersection.
return BoundingBox()
origin = Vector(
max(self.minx, box.minx),
max(self.miny, box.miny),
max(self.minz, box.minz),
)
maximum = Vector(
min(self.maxx, box.maxx),
min(self.maxy, box.maxy),
min(self.maxz, box.maxz),
)
#print "Intersect of {0} and {1}: {2}".format(self, box, newbox)
return BoundingBox(origin, maximum - origin)
def union(self, box):
"""
Return a box large enough to contain both self and box.
"""
origin = Vector(
min(self.minx, box.minx),
min(self.miny, box.miny),
min(self.minz, box.minz),
)
maximum = Vector(
max(self.maxx, box.maxx),
max(self.maxy, box.maxy),
max(self.maxz, box.maxz),
)
return BoundingBox(origin, maximum - origin)
def expand(self, dx, dy=None, dz=None):
"""
Return a new box with boundaries expanded by dx, dy, dz.
If only dx is passed, expands by dx in all dimensions.
"""
if dz is None:
dz = dx
if dy is None:
dy = dx
origin = self.origin - (dx, dy, dz)
size = self.size + (dx * 2, dy * 2, dz * 2)
return BoundingBox(origin, size)
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))
# --- Chunk positions ---
@property
def mincx(self):
"The smallest chunk position contained in this box"
return self.origin.x >> 4
@property
def mincz(self):
"The smallest chunk position contained in this box"
return self.origin.z >> 4
@property
def maxcx(self):
"The largest chunk position contained in this box"
return ((self.origin.x + self.size.x - 1) >> 4) + 1
@property
def maxcz(self):
"The largest chunk position contained in this box"
return ((self.origin.z + self.size.z - 1) >> 4) + 1
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))
@property
def chunkPositions(self):
#iterate through all of the chunk positions within this selection box
return itertools.product(range(self.mincx, self.maxcx), range(self.mincz, self.maxcz))
@property
def chunkCount(self):
return (self.maxcx - self.mincx) * (self.maxcz - self.mincz)
@property
def isChunkAligned(self):
return (self.origin.x & 0xf == 0) and (self.origin.z & 0xf == 0)
class FloatBox (BoundingBox):
type = float