| 4 | from typing import Any, Dict, List, Optional, Tuple |
| 5 | |
| 6 | class BoundingBox(): |
| 7 | def __init__(self, data: Any = None, scale=1.0): |
| 8 | self.scale = scale |
| 9 | if data: |
| 10 | self.load(data) |
| 11 | |
| 12 | def load(self, data: Any ) -> None: |
| 13 | if type(data) is dict: |
| 14 | position = data['position'] |
| 15 | rotation = data['rotation'] |
| 16 | dimension = data['dimension'] |
| 17 | elif type(data) is str: |
| 18 | f = json.load(open(data, 'r')) |
| 19 | |
| 20 | position = f['frames'][0]['items'][0]['position'] |
| 21 | rotation = f['frames'][0]['items'][0]['rotation'] |
| 22 | dimension = f['frames'][0]['items'][0]['dimension'] |
| 23 | else: |
| 24 | raise NotImplementedError |
| 25 | |
| 26 | self.center = np.array([position['x'], |
| 27 | position['y'], |
| 28 | position['z']]) * self.scale |
| 29 | |
| 30 | self.sizes = np.array([dimension['x'], |
| 31 | dimension['y'], |
| 32 | dimension['z']]) * self.scale |
| 33 | |
| 34 | self.rotation = np.array([rotation['x'], |
| 35 | rotation['y'], |
| 36 | rotation['z'], |
| 37 | ]) |
| 38 | |
| 39 | def volume(self) -> float: |
| 40 | return np.prod(self.sizes) |
| 41 | |
| 42 | def get_transformation_matrix(self) -> np.ndarray: |
| 43 | rotation = self.rotation |
| 44 | position = self.center |
| 45 | |
| 46 | Rx = np.array([[1, 0, 0, 0], |
| 47 | [0, np.cos(rotation[0]), -np.sin(rotation[0]), 0], |
| 48 | [0, np.sin(rotation[0]), np.cos(rotation[0]), 0], |
| 49 | [0, 0, 0, 1], |
| 50 | ]) |
| 51 | |
| 52 | Ry = np.array([[ np.cos(rotation[1]), 0, np.sin(rotation[1]), 0], |
| 53 | [0, 1, 0, 0], |
| 54 | [-np.sin(rotation[1]), 0, np.cos(rotation[1]), 0], |
| 55 | [0, 0, 0, 1], |
| 56 | ]) |
| 57 | |
| 58 | Rz = np.array([[np.cos(rotation[2]), -np.sin(rotation[2]), 0, 0], |
| 59 | [np.sin(rotation[2]), np.cos(rotation[2]), 0, 0], |
| 60 | [0, 0, 1, 0], |
| 61 | [0, 0, 0, 1], |
| 62 | ]) |
| 63 |
no outgoing calls
no test coverage detected