def __init__( self, client: Client, position: Vec3, cube: Union[Items, np.ndarray], anchor: Anchor3 = Anchor3.BOTTOM_NW, render: bool = True, ) -> None: self._client = client self.anchor = anchor if isinstance(cube, np.ndarray): self.ncube = cube else: self.ncube = np.array(cube, dtype=Item) if self.ncube.ndim != 3: raise ValueError("invalid cube specificaton") self.volume = Volume.from_anchor( position, Vec3(*self.ncube.shape), self.anchor) self._solid: Any = self.ncube != Item.AIR if render: self._render()
def move_to(self, position: Vec3, clear: bool = True) -> None: """ moves the cubiod to position and redraws it """ old_volume = self.volume self.volume = Volume.from_anchor( position, Vec3(*self.ncube.shape), self.anchor) self._render() if clear: old_volume.fill(self._client)
def move(self, vector: Vec3, clear: bool = True) -> None: """ moves the cubiod by vector and redraws it """ old_start = self.volume.start self.volume = Volume.from_anchor( self.volume.position + vector, Vec3(*self.ncube.shape), self.anchor ) self._render() if clear: self._unrender(vector, old_start)
def test_anchors_even_size(self): size = Vec3(4, 4, 4) position = Vec3(0, 0, 0) v = Volume.from_anchor(position, size, anchor=Anchor3.MIDDLE) self.assertTrue(v.position == position) self.assertTrue(v.start == Vec3(-2, -2, -2)) self.assertTrue(v.end == Vec3(1, 1, 1)) self.assertTrue(v.size == size) v = Volume.from_anchor(position, size, anchor=Anchor3.BOTTOM_NW) self.assertTrue(v.position == position) self.assertTrue(v.start == Vec3(0, 0, 0)) self.assertTrue(v.end == Vec3(3, 3, 3)) self.assertTrue(v.size == size) v = Volume.from_anchor(position, size, anchor=Anchor3.BOTTOM_NE) self.assertTrue(v.position == position) self.assertTrue(v.start == Vec3(-3, 0, 0)) self.assertTrue(v.end == Vec3(0, 3, 3)) self.assertTrue(v.size == size)
def test_bounds_odd_size(self): size = Vec3(3, 3, 3) position = Vec3(-1, -1, -1) for x, y, z in product([1, -1], [1, -1], [1, -1]): start = Vec3(x, y, z) opposite = Vec3(-x, -y, -z) print(start, opposite) v = Volume.from_corners(start, end=opposite) print(v.start, v.end, v.position, v.size) self.assertTrue(v.position == position) self.assertTrue(v.size == size)
def rotate(self, plane: Planes3d, steps: int = 1, clear=True) -> None: """ rotate the blocks in place """ self.ncube = np.rot90(self.ncube, k=steps, axes=plane.value) if clear: # TODO implement unrender for rotated blocks (challenging?) self.volume.fill(self._client) self._solid = self.ncube != Item.AIR self.volume = Volume.from_anchor( self.volume.position, Vec3(*self.ncube.shape), self.anchor ) self._render()
def test_anchors_odd_size(self): size = Vec3(3, 3, 3) position = Vec3(0, 0, 0) v = Volume.from_anchor(position, size, anchor=Anchor3.MIDDLE) self.assertTrue(v.position == position) self.assertTrue(v.start == Vec3(-1, -1, -1)) self.assertTrue(v.end == Vec3(1, 1, 1)) self.assertTrue(v.size == size) v = Volume.from_anchor(position, size, anchor=Anchor3.BOTTOM_NW) self.assertTrue(v.position == position) self.assertTrue(v.start == Vec3(0, 0, 0)) self.assertTrue(v.end == Vec3(2, 2, 2)) self.assertTrue(v.size == size) v = Volume.from_anchor(position, size, anchor=Anchor3.BOTTOM_NE) self.assertTrue(v.position == position) self.assertTrue(v.start == Vec3(-2, 0, 0)) self.assertTrue(v.end == Vec3(0, 2, 2)) self.assertTrue(v.size == size) v = Volume.from_anchor(position, size, anchor=Anchor3.BOTTOM_SE) self.assertTrue(v.position == position) self.assertTrue(v.start == Vec3(-2, 0, -2)) self.assertTrue(v.end == Vec3(0, 2, 0)) self.assertTrue(v.size == size) v = Volume.from_anchor(position, size, anchor=Anchor3.BOTTOM_SW) self.assertTrue(v.position == position) self.assertTrue(v.start == Vec3(0, 0, -2)) self.assertTrue(v.end == Vec3(2, 2, 0)) self.assertTrue(v.size == size) v = Volume.from_anchor(position, size, anchor=Anchor3.BOTTOM_MIDDLE) self.assertTrue(v.position == position) self.assertTrue(v.start == Vec3(-1, 0, -1)) self.assertTrue(v.end == Vec3(1, 2, 1)) self.assertTrue(v.size == size) v = Volume.from_anchor(position, size, anchor=Anchor3.TOP_NW) self.assertTrue(v.position == position) self.assertTrue(v.start == Vec3(0, -2, 0)) self.assertTrue(v.end == Vec3(2, 0, 2)) self.assertTrue(v.size == size) v = Volume.from_anchor(position, size, anchor=Anchor3.TOP_NE) self.assertTrue(v.position == position) self.assertTrue(v.start == Vec3(-2, -2, 0)) self.assertTrue(v.end == Vec3(0, 0, 2)) self.assertTrue(v.size == size) v = Volume.from_anchor(position, size, anchor=Anchor3.TOP_SE) self.assertTrue(v.position == position) self.assertTrue(v.start == Vec3(-2, -2, -2)) self.assertTrue(v.end == Vec3(0, 0, 0)) self.assertTrue(v.size == size) v = Volume.from_anchor(position, size, anchor=Anchor3.TOP_SW) self.assertTrue(v.position == position) self.assertTrue(v.start == Vec3(0, -2, -2)) self.assertTrue(v.end == Vec3(2, 0, 0)) self.assertTrue(v.size == size) v = Volume.from_anchor(position, size, anchor=Anchor3.TOP_MIDDLE) self.assertTrue(v.position == position) self.assertTrue(v.start == Vec3(-1, -2, -1)) self.assertTrue(v.end == Vec3(1, 0, 1)) self.assertTrue(v.size == size)