-
Notifications
You must be signed in to change notification settings - Fork 25
/
core_box.py
78 lines (69 loc) · 2.12 KB
/
core_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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = ['Benjamin Dillenburger','Demetris Shammas','Mathias Bernhard']
__copyright__ = 'Copyright 2019 / Digital Building Technologies DBT / ETH Zurich'
__license__ = 'MIT License'
__email__ = ['<[email protected]>']
import math
from mola.core_vertex import Vertex
class Box:
"""A `Box` is defined by by two opposite corners with x,y,z coordinates.
Mostly used for getting the bounding box of a set of points.
Attributes
----------
x1, y1, z1 : float
The coordinates of the bottom left front corner.
x2, y2, z2 : float
The coordinates of the top right back corner.
"""
def __init__(self, x1=float('inf'), y1=float('inf'), z1=float('inf'), x2=-float('inf'), y2=-float('inf'), z2=-float('inf')):
self.x1 = x1
self.y1 = y1
self.z1 = z1
self.x2 = x2
self.y2 = y2
self.z2 = z2
def dim_x(self):
"""
Returns the Box's extent in X direction.
"""
return self.x2 - self.x1
def dim_y(self):
"""
Returns the Box's extent in Y direction.
"""
return self.y2 - self.y1
def dim_z(self):
"""
Returns the Box's extent in Z direction.
"""
return self.z2 - self.z1
def dim_max(self):
"""
Returns the Box's maximum extent.
"""
x = self.dim_x()
y = self.dim_y()
z = self.dim_z()
if (x >= y) and (x >= z):
return x
elif (y >= x) and (y >= z):
return y
else:
return z
def center(self):
"""
returns the Box's center as a Vertex() object
"""
return Vertex((self.x2+self.x1)/2.0,(self.y2+self.y1)/2.0,(self.z2+self.z1)/2.0)
def add_point(self,x,y,z):
"""
adds a point to the bounding box,
increases the box's size if the point is outside.
"""
self.x1 = min(x,self.x1)
self.y1 = min(y,self.y1)
self.z1 = min(z,self.z1)
self.x2 = max(x,self.x2)
self.y2 = max(y,self.y2)
self.z2 = max(z,self.z2)