This repository has been archived by the owner on Feb 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
box.py
95 lines (74 loc) · 2.29 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
import numpy as np
class Box(object):
'''
An axis-aligned cuboid.
'''
def __init__(self, origin, shape):
'''
origin: The x, y, z coordinate of the origin.
shape: A sequence containing the width (dx), height (dy), and
depth (dz).
'''
from blmath.numerics import as_numeric_array
self.origin = as_numeric_array(origin, shape=(3,))
self.shape = as_numeric_array(shape, shape=(3,))
if any(np.less(self.shape, 0)):
raise ValueError('Shape should be zero or positive')
@property
def ranges(self):
'''
Return ranges for each coordinate axis as a 3x2 numpy array.
'''
ranges = np.array([self.origin, self.origin + self.shape]).T
# ranges is almost, but not quite what we want, since it might
# include mins which are greater than maxes, and vice versa.
return np.vstack([ranges.min(axis=1), ranges.max(axis=1)]).T
@property
def min_x(self):
return self.origin[0]
@property
def min_y(self):
return self.origin[1]
@property
def min_z(self):
return self.origin[2]
@property
def max_x(self):
return self.origin[0] + self.shape[0]
@property
def max_y(self):
return self.origin[1] + self.shape[1]
@property
def max_z(self):
return self.origin[2] + self.shape[2]
@property
def mid_x(self):
return self.origin[0] + self.shape[0] / 2
@property
def mid_y(self):
return self.origin[1] + self.shape[1] / 2
@property
def mid_z(self):
return self.origin[2] + self.shape[2] / 2
@property
def width(self):
return self.shape[0]
@property
def height(self):
return self.shape[1]
@property
def depth(self):
return self.shape[2]
@property
def center_point(self):
return self.origin + 0.5 * self.shape
@property
def floor_point(self):
return self.origin + [0.5, 0., 0.5] * self.shape
@property
def volume(self):
return np.prod(self.shape)
@property
def surface_area(self):
l, h, w = self.shape # self.shape is a np.ndarray, which is a sequence. pylint: disable=unpacking-non-sequence
return 2 * (w * l + h * l + h * w)