示例#1
0
    def test_mask_2(self):
        a = ChunkGrid(2, bool, False)
        a[0, 0, 0] = True
        a.ensure_chunk_at_index((1, 1, 1)).set_fill(True)
        a.set_value((2, 1, 1), True)

        b = a.astype(np.int8)

        adense = a.to_dense()
        bdense = b.to_dense()
        self.assertEqual(adense.shape, bdense.shape)
        self.assertEqual(str(adense.astype(np.int8)), str(bdense))

        mask = ChunkGrid(2, bool, False)
        mask.ensure_chunk_at_index((1, 0, 0)).set_fill(True)
        mask.ensure_chunk_at_index((0, 1, 0)).set_fill(True)
        mask.ensure_chunk_at_index((0, 0, 1))[0, 0, 0] = True

        b[mask] = 2
        bdense = b.to_dense()

        expected = np.zeros((4, 4, 4), dtype=np.int8)
        expected[0, 0, 0] = 1
        expected[2:4, 2:4, 2:4] = 1
        expected[2, 1, 1] = 1
        expected[2:4, 0:2, 0:2] = 2
        expected[0:2, 2:4, 0:2] = 2
        expected[0, 0, 2] = 2

        self.assertEqual(expected.shape, bdense.shape)
        self.assertEqual(str(expected), str(bdense))
示例#2
0
    def test_eq_3(self):
        a = ChunkGrid(1, bool, False)
        b = ChunkGrid(1, bool, False)

        x = a == b
        self.assertEqual(0, len(x.chunks))
        self.assertTrue(x.fill_value)
示例#3
0
    def test_eq_1(self):
        a = ChunkGrid(2, bool, False)
        b = ChunkGrid(2, bool, False)
        a.set_value((0, 0, 0), True)
        result = (a == b).to_dense()
        self.assertIsInstance((a == b), ChunkGrid)

        expected = np.ones((2, 2, 2), dtype=bool)
        expected[0, 0, 0] = False

        assert result.shape == expected.shape
        self.assertEqual(str(expected), str(result))
示例#4
0
 def grid_segments(self) -> Tuple[ChunkGrid[np.bool8], ChunkGrid[np.bool8]]:
     to_voxel = MinCut.to_voxel
     segments = self.segments()
     if self._grid_segment0 is None:
         self._grid_segment0 = ChunkGrid(self.crust.chunk_size, np.bool8, False)
         self._grid_segment0[[p for node, s in zip(self.nodes, segments) if s == False
                              for p in to_voxel(node)]] = True
     if self._grid_segment1 is None:
         self._grid_segment1 = ChunkGrid(self.crust.chunk_size, np.bool8, False)
         self._grid_segment1[[p for node, s in zip(self.nodes, segments) if s == True
                              for p in to_voxel(node)]] = True
     return self._grid_segment0, self._grid_segment1
示例#5
0
    def test_mask_1(self):
        a = ChunkGrid(2, int, 0)

        mask = ChunkGrid(2, bool, False)
        mask[1:3, 1:3, 1:3] = True
        self.assertEqual((4, 4, 4), mask.to_dense().shape)
        a[mask] = 3

        expected = np.zeros((4, 4, 4), dtype=int)
        expected[1:3, 1:3, 1:3] = 3

        self.assertEqual(str(expected), str(a.to_dense()))

        tmp = a == 3
        self.assertEqual(str(mask.to_dense()), str(tmp.to_dense()))
示例#6
0
    def test_padding_2(self):
        a = ChunkGrid(2, int, 0)
        expected = np.zeros((6, 6, 6), dtype=int)
        for n, i in enumerate(np.ndindex(6, 6, 6)):
            pos = np.array(i, dtype=int) - 2
            a.set_value(pos, n + 1)
            s = np.sum(pos < 0) + np.sum(2 <= pos)
            if s >= 2:
                continue
            expected[i] = n + 1

        data = a.padding_at((0, 0, 0), 2, corners=False, edges=False)
        self.assertEqual(expected.shape, data.shape)
        self.assertEqual(str(expected), str(data))

        expected2 = expected[1:-1, 1:-1, 1:-1]
        data2 = a.padding_at((0, 0, 0), 1, corners=False, edges=False)
        self.assertEqual(expected2.shape, data2.shape)
        self.assertEqual(str(expected2), str(data2))

        expected3_tmp = np.zeros((6, 6, 6), dtype=int)
        for n, i in enumerate(np.ndindex(6, 6, 6)):
            expected3_tmp[i] = n + 1
        expected3 = np.zeros((8, 8, 8), dtype=int)
        expected3[1:-1, 1:-1, 1:-1] = expected3_tmp
        data3 = a.padding_at((0, 0, 0), 3, corners=False, edges=False)
        self.assertEqual(expected3.shape, data3.shape)
        self.assertEqual(str(expected3), str(data3))
示例#7
0
    def test_padding_vec3_1(self):
        dtype = np.dtype((int, (3,)))
        a = ChunkGrid(2, dtype, np.zeros(3))
        a.set_value((0, 0, 2), np.ones(3) * 1)
        a.set_value((0, 2, 0), np.ones(3) * 2)
        a.set_value((2, 0, 0), np.ones(3) * 3)
        a.set_value((1, 1, 1), np.ones(3) * 4)

        a.set_value((2, 2, 2), np.ones(3) * -1)
        a.set_value((-1, -1, -1), np.ones(3) * -2)

        a.set_value((2, -1, -1), np.ones(3) * -3)
        a.set_value((-1, 2, -1), np.ones(3) * -4)
        a.set_value((-1, -1, 2), np.ones(3) * -5)

        a.set_value((2, 2, -1), np.ones(3) * -6)
        a.set_value((2, -1, 2), np.ones(3) * -7)
        a.set_value((-1, 2, 2), np.ones(3) * -8)

        pad = a.padding_at((0, 0, 0), 1, corners=False)

        expected = np.zeros((4, 4, 4, 3), dtype=int)
        expected[1, 1, 3] = 1
        expected[1, 3, 1] = 2
        expected[3, 1, 1] = 3
        expected[2, 2, 2] = 4

        self.assertEqual(expected.shape, pad.shape)
        self.assertEqual(str(expected), str(pad))
示例#8
0
    def test_padding_vec3_4(self):
        dtype = np.dtype((int, (3,)))
        a = ChunkGrid(2, dtype, np.zeros(3))
        expected = np.zeros((6, 6, 6, 3), dtype=int)

        for n, i in enumerate(np.ndindex(6, 6, 6)):
            pos = np.array(i, dtype=int) - 2
            value = np.array([1, 2, 3]) + (n + 1) * 3
            a.set_value(pos, value)
            s = np.sum(pos < 0) + np.sum(2 <= pos)
            if s >= 2:
                continue
            expected[i] = value

        data = a.padding_at((0, 0, 0), 2, corners=False, edges=False)
        self.assertEqual(expected.shape, data.shape)
        self.assertEqual(str(expected), str(data))

        expected2 = expected[1:-1, 1:-1, 1:-1]
        data2 = a.padding_at((0, 0, 0), 1, corners=False, edges=False)
        self.assertEqual(expected2.shape, data2.shape)
        self.assertEqual(str(expected2), str(data2))

        expected3_tmp = np.zeros((6, 6, 6, 3), dtype=int)
        for n, i in enumerate(np.ndindex(6, 6, 6)):
            value = np.array([1, 2, 3]) + (n + 1) * 3
            expected3_tmp[i] = value
        expected3 = np.zeros((8, 8, 8, 3), dtype=int)
        expected3[1:-1, 1:-1, 1:-1] = expected3_tmp
        data3 = a.padding_at((0, 0, 0), 3, corners=False, edges=False)
        self.assertEqual(expected3.shape, data3.shape)
        self.assertEqual(str(expected3), str(data3))
示例#9
0
    def test_padding(self):
        grid = ChunkGrid(2, int, -1)
        grid.ensure_chunk_at_index((0, 0, 0)).set_fill(1)
        grid.ensure_chunk_at_index((0, 0, 1)).set_fill(2)
        grid.ensure_chunk_at_index((0, 1, 0)).set_fill(3)
        grid.ensure_chunk_at_index((0, 1, 1)).set_fill(4)

        t = grid.chunks[(0, 0, 1)]
        t.set_array(np.array([
            [(111, 112), (121, 122)],
            [(211, 212), (221, 222)]
        ]))
        expected1 = t.to_array()[:, :, 0]

        c = grid.chunks.get((0, 0, 0))
        pad = c.padding(grid, 1)
        actual = pad

        expected = np.ones((4, 4, 4), int) * -1
        expected[1:3, 1:3, 1:3] = 1
        expected[1:-1, 1:-1, -1] = expected1
        expected[1:-1, -1, 1:-1] = 3

        self.assertEqual(expected.shape, actual.shape)
        # self.assertTrue(np.all(actual == expected), f"Failure! \n{actual}\n-------\n{expected}")
        self.assertEqual(str(expected), str(actual))
示例#10
0
    def test_padding_1(self):
        a = ChunkGrid(2, int, 0)
        expected = np.zeros((6, 6, 6), dtype=int)
        for n, i in enumerate(np.ndindex(6, 6, 6)):
            pos = np.array(i, dtype=int) - 2
            a.set_value(pos, n + 1)
            expected[i] = n + 1

        data = a.padding_at((0, 0, 0), 2)
        self.assertEqual(expected.shape, data.shape)
        self.assertEqual(str(expected), str(data))

        expected2 = expected[1:-1, 1:-1, 1:-1]
        data2 = a.padding_at((0, 0, 0), 1)
        self.assertEqual(expected2.shape, data2.shape)
        self.assertEqual(str(expected2), str(data2))

        expected3_tmp = np.zeros((6, 6, 6), dtype=int)
        for n, i in enumerate(np.ndindex(6, 6, 6)):
            expected3_tmp[i] = n + 1
        expected3 = np.zeros((8, 8, 8), dtype=int)
        expected3[1:-1, 1:-1, 1:-1] = expected3_tmp
        data3 = a.padding_at((0, 0, 0), 3)
        self.assertEqual(expected3.shape, data3.shape)
        self.assertEqual(str(expected3), str(data3))
示例#11
0
def flood_fill_at(position: Vec3i,
                  mask: ChunkGrid,
                  max_steps: Optional[int] = None,
                  verbose=False,
                  **kwargs) -> ChunkGrid[b8]:
    image = ChunkGrid(mask.chunk_size, b8, False)
    image[position] = True
    return flood_fill(image, mask, max_steps, verbose=verbose, **kwargs)
示例#12
0
def grid_normals(surface: ChunkGrid[np.bool8], outer: ChunkGrid[np.bool8], normal_kernel: Optional[np.ndarray] = None) \
        -> ChunkGrid[Vec3f]:
    normal_pos, normal_val = detect_normals(surface, outer, normal_kernel)
    normals: ChunkGrid[np.float32] = ChunkGrid(surface.chunk_size,
                                               np.dtype((np.float32, (3, ))),
                                               0.0)
    normals[normal_pos] = normal_val
    return normals
示例#13
0
    def test_eq_2(self):
        a = ChunkGrid(2, bool, False)
        b = ChunkGrid(2, bool, False)

        a.set_value((0, 0, 0), True)
        a.set_value((2, 2, 2), True)

        comp = a == b
        self.assertIsInstance(comp, ChunkGrid)
        self.assertIs(comp.dtype.type, np.bool8)

        expected = np.ones((4, 4, 4), dtype=bool)
        expected[0, 0, 0] = False
        expected[2, 2, 2] = False

        result = comp.to_dense()
        assert result.shape == expected.shape
        self.assertEqual(str(expected), str(result))
示例#14
0
    def test_get_block_1(self):
        a = ChunkGrid(2, int, 0)
        expected = np.zeros((6, 6, 6), dtype=int)
        for n, i in enumerate(np.ndindex(6, 6, 6)):
            pos = np.array(i, dtype=int) - 2
            a.set_value(pos, n + 1)
            expected[i] = n + 1

        block = a.get_block_at((0, 0, 0), (3, 3, 3), corners=True, edges=True)
        self.assertEqual(str(expected), str(a.block_to_array(block)))
示例#15
0
    def test__getitem_empty(self):
        a = ChunkGrid(2, int, -1)
        a.set_or_fill((0, 0, 0), 1)

        res = a[-1:1, -1:1, -1:1]

        expected = np.full((2, 2, 2), -1)
        expected[1, 1, 1] = 1

        self.assertEqual(str(expected), str(res))
示例#16
0
    def _test_operator2_int(self, op, b0=0, dtype: Type = int, inplace=False):
        a = ChunkGrid(2, dtype, 0)
        b = ChunkGrid(2, dtype, 1)

        a.set_value((0, 0, 0), 1)
        a.set_value((0, 0, 1), 1)

        b.set_value((0, 0, 0), 1 + b0)
        b.set_value((0, 1, 0), 0 + b0)
        b.set_value((0, 1, 1), 0 + b0)

        a0 = a.copy()
        b0 = b.copy()
        expected = op(a.to_dense(), b.to_dense())
        res = op(a, b)
        self.assertIsInstance(res, ChunkGrid)
        result = res.to_dense()
        assert result.shape == expected.shape
        self.assertTrue(np.all(result == expected), f"Failure {op}! \n{result}\n-------\n{expected}")
        self._test_inplace_modified(op, a, a0, b, b0, inplace)
示例#17
0
    def test_reflected(self):
        grid = ChunkGrid(2, float, -1.0)
        grid.set_value((0, 0, 0), 0.5)
        grid.set_value((0, 0, 1), 1.0)

        result = (0 < grid) & (grid < 1.0)
        actual = result.to_dense()

        expected = np.zeros((2, 2, 2), dtype=bool)
        expected[0, 0, 0] = True

        self.assertEqual(str(expected), str(actual))
示例#18
0
    def _test_operator1_int(self, op):
        a = ChunkGrid(2, int, -1)

        a.set_value((0, 0, 0), 0)
        a.set_value((0, 0, 1), 2)
        a.set_value((0, 1, 0), -2)

        expected = op(a.to_dense())
        res = op(a)
        self.assertIsInstance(res, ChunkGrid)
        result = res.to_dense()
        assert result.shape == expected.shape
        self.assertTrue(np.all(result == expected), f"Failure {op}! \n{result}\n-------\n{expected}")
示例#19
0
    def test_get_block_2(self):
        a = ChunkGrid(2, int, 0)
        expected = np.zeros((6, 6, 6), dtype=int)
        for n, i in enumerate(np.ndindex(6, 6, 6)):
            pos = np.array(i, dtype=int) - 2
            a.set_value(pos, n + 1)
            s = np.sum(pos < 0) + np.sum(2 <= pos)
            if s >= 2:
                continue
            expected[i] = n + 1

        block = a.get_block_at((0, 0, 0), (3, 3, 3), corners=False, edges=False)
        self.assertEqual(str(expected), str(a.block_to_array(block)))
示例#20
0
 def fill_at_pos(self,
                 position: Vec3i,
                 max_steps: Optional[int] = None) -> ChunkGrid[bool]:
     """
     Start the flood fill at a position
     :param position: the starting point
     :param max_steps: maximum propagation steps between chunks
     :return:
     """
     image: ChunkGrid[b8] = ChunkGrid(self.mask.chunk_size,
                                      dtype=b8,
                                      fill_value=b8(False))
     image.set_value(position, True)
     return self.fill(image, max_steps=max_steps)
示例#21
0
    def test_special_dtype(self):

        t = np.dtype((np.int, (3,)))
        a = ChunkGrid(2, dtype=t, fill_value=np.zeros(3))

        a.set_value((0, 0, 0), np.ones(3))
        a.set_value((2, 2, 2), np.ones(3))
        dense = a.to_dense()

        expected = np.zeros((4, 4, 4), dtype=t)
        expected[0, 0, 0] = 1
        expected[2, 2, 2] = 1

        self.assertEqual(expected.shape, dense.shape)
        self.assertEqual(str(expected), str(dense))
示例#22
0
    def test_eq_and_combined(self):
        a = ChunkGrid(2, bool, False)
        a.set_value((0, 0, 0), True)
        a.set_value((0, 0, 1), True)
        a.set_value((0, 1, 1), True)

        b = ChunkGrid(2, int, 0)
        b.set_value((0, 0, 1), 1)
        b.set_value((0, 1, 1), 2)
        b.set_value((1, 1, 1), 1)

        tmp = (b == 1)
        self.assertEqual(1, len(tmp.chunks))
        self.assertFalse(next(iter(tmp.chunks)).is_filled())

        result = tmp & a
        self.assertEqual(1, len(result.chunks))
        self.assertFalse(next(iter(result.chunks)).is_filled())

        expected_tmp = np.zeros((2, 2, 2), dtype=bool)
        expected_tmp[0, 0, 1] = True
        expected_tmp[1, 1, 1] = True

        self.assertEqual(str(expected_tmp), str(tmp.to_dense()))

        expected = np.zeros((2, 2, 2), dtype=bool)
        expected[0, 0, 1] = True
        self.assertEqual(str(expected), str(result.to_dense()))

        values = list(tmp.items(mask=a))
        self.assertEqual(3, len(values))
        self.assertEqual(str([
            (np.array([0, 0, 0]), False),
            (np.array([0, 0, 1]), True),
            (np.array([0, 1, 1]), False)
        ]), str(values))
示例#23
0
    def test_eq_int(self):
        a = ChunkGrid(2, int, 0)
        a.set_value((1, 1, 1), 1)
        a.ensure_chunk_at_index((1, 1, 1))

        result = a == 0
        dense = result.to_dense()

        expected = np.ones((4, 4, 4), dtype=bool)
        expected[1, 1, 1] = False

        self.assertEqual(expected.shape, dense.shape)
        self.assertEqual(str(expected), str(dense))
        self.assertTrue(result.any())
        self.assertFalse(result.all())
示例#24
0
    def test_set_pos(self):
        a = ChunkGrid(2, bool, False)
        a.set_value((0, 0, 0), True)
        a.set_value((2, 2, 2), True)

        self.assertFalse(a.chunks[0, 0, 0].is_filled())

        result = a.to_dense()

        expected = np.zeros((4, 4, 4), dtype=bool)
        expected[0, 0, 0] = True
        expected[2, 2, 2] = True

        assert result.shape == expected.shape
        self.assertEqual(str(expected), str(result))
示例#25
0
    def test_filled(self):
        a = ChunkGrid(2, bool, False)
        a.ensure_chunk_at_index((0, 0, 0)).set_fill(True)

        res = dilate(a, steps=1)
        self.assertEqual(7, len(res.chunks))
        c0 = res.chunks.get((0, 0, 0))
        self.assertIsNotNone(c0)
        self.assertTrue(c0.is_filled())
        self.assertTrue(c0.value)

        expected = np.zeros((6, 6, 6), dtype=bool)
        expected[2:-2, 2:-2, 1:-1] = True
        expected[2:-2, 1:-1, 2:-2] = True
        expected[1:-1, 2:-2, 2:-2] = True
        self.assertEqual(str(expected), str(res.to_dense()))
示例#26
0
    def test_single_negative(self):
        a = ChunkGrid(2, bool, False)
        a.set_value((0, 0, 0), True)

        res = dilate(a, steps=1)
        self.assertEqual(4, len(res.chunks))
        c0 = res.chunks.get((0, 0, 0))
        self.assertIsNotNone(c0)
        self.assertFalse(c0.is_filled())

        expected = np.zeros((4, 4, 4), dtype=bool)
        expected[2, 2, 1:4] = True
        expected[2, 1:4, 2] = True
        expected[1:4, 2, 2] = True

        self.assertEqual(str(expected), str(res.to_dense()))
示例#27
0
    def test_padding_vec3_2(self):
        a = ChunkGrid(2, int, 0)
        expected = np.zeros((6, 6, 6), dtype=int)

        for n, i in enumerate(np.ndindex(6, 6, 6)):
            pos = np.array(i, dtype=int) - 2
            value = (n + 1)
            a.set_value(pos, value)
            expected[i] = value

        data = a.padding_at((0, 0, 0), 2)
        self.assertEqual(expected.shape, data.shape)
        self.assertEqual(str(expected), str(data))

        expected2 = expected[1:-1, 1:-1, 1:-1]
        data2 = a.padding_at((0, 0, 0), 1)
        self.assertEqual(expected2.shape, data2.shape)
        self.assertEqual(str(expected2), str(data2))
示例#28
0
    def test_getitem(self):
        CS = 2
        shape = (CS * 3, CS * 3, CS * 3)
        soll = np.arange(shape[0] * shape[1] * shape[2]).reshape(shape)

        a = ChunkGrid(CS, int, -1)
        for u in range(shape[0] // CS):
            for v in range(shape[1] // CS):
                for w in range(shape[2] // CS):
                    x, y, z = u * CS, v * CS, w * CS
                    index = (u, v, w)
                    a.ensure_chunk_at_index(index).set_array(soll[x:x + CS, y:y + CS, z:z + CS])

        dense = a.to_dense()
        self.assertEqual(soll.shape, dense.shape)
        self.assertEqual(str(soll), str(dense))

        self.assertEqual(str(soll[1: shape[0] - 1, 1: shape[1] - 1, 1: shape[2] - 1]),
                         str(a[1: shape[0] - 1, 1: shape[1] - 1, 1: shape[2] - 1]))
def diffuse(model: ChunkGrid[bool], repeat=1):
    """
    Diffuse the voxels in model to their neighboring voxels
    :param model: the model to diffuse
    :param repeat: number of diffusion steps
    :return: diffused model
    """
    kernel = np.zeros((3, 3, 3), dtype=float)
    kernel[1] = 1
    kernel[:, 1] = 1
    kernel[:, :, 1] = 1
    kernel /= np.sum(kernel)

    result = ChunkGrid(model.chunk_size, dtype=float, fill_value=1.0)
    result[model] = 0.0
    result.pad_chunks(repeat // result.chunk_size + 1)

    for r in range(repeat):
        tmp = result.copy(empty=True)
        for chunk in result.chunks:
            padded = chunk.padding(result, 1)
            ndimage.convolve(padded,
                             kernel,
                             output=padded,
                             mode='constant',
                             cval=1.0)
            conv = padded[1:-1, 1:-1, 1:-1]
            m = model.ensure_chunk_at_index(chunk.index, insert=False)
            if m.is_filled():
                if m.value:
                    tmp.ensure_chunk_at_index(chunk.index).set_fill(0.0)
                    continue
            else:
                conv[m.to_array()] = 0.0
            tmp.ensure_chunk_at_index(chunk.index).set_array(conv)
            # Expand chunks
            for f, i in ChunkGrid.iter_neighbors_indices(chunk.index):
                tmp.ensure_chunk_at_index(i)

        result = tmp

    result.cleanup(remove=True)
    return result
示例#30
0
    def test_padding_no_corners_edges(self):
        a = ChunkGrid(2, int, -1)
        expected = np.zeros((6, 6, 6), int)

        for n, i in enumerate(np.ndindex(6, 6, 6)):
            a.set_value(i, n)
            expected[i] = n

        self.assertEqual(expected.shape, tuple(a.size()))
        self.assertEqual(str(expected), str(a.to_dense()))

        padded1 = a.padding_at((1, 1, 1), 1, corners=True, edges=True)
        expected1 = expected[1:-1, 1:-1, 1:-1]
        self.assertEqual(str(expected1), str(padded1))

        padded2 = a.padding_at((1, 1, 1), 1, corners=False, edges=False)
        expected2 = np.full((4, 4, 4), -1, int)
        expected2[1:-1, 1:-1, :] = expected[2:-2, 2:-2, 1:-1]
        expected2[1:-1, :, 1:-1] = expected[2:-2, 1:-1, 2:-2]
        expected2[:, 1:-1, 1:-1] = expected[1:-1, 2:-2, 2:-2]
        self.assertEqual(str(expected2), str(padded2))