File: test_binvox.py

package info (click to toggle)
trimesh 4.5.1-3
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 33,416 kB
  • sloc: python: 35,596; makefile: 96; javascript: 85; sh: 38
file content (53 lines) | stat: -rw-r--r-- 1,861 bytes parent folder | download
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
try:
    from . import generic as g
except BaseException:
    import generic as g

from io import BytesIO

from trimesh import voxel as v
from trimesh.exchange import binvox
from trimesh.voxel import runlength as rl


class BinvoxTest(g.unittest.TestCase):
    def test_load_save_invariance(self):
        np = g.np
        n = 4
        dense = np.random.uniform(size=(n,) * 3) > 0.8
        dense[0, 0, 0] = dense[-1, -1, -1] = 1  # ensure extent test works
        shape = dense.shape
        rl_data = rl.dense_to_rle(dense.flatten(), dtype=np.uint8)
        translate = np.array([2, 5, 10], dtype=np.float32)
        scale = 5.0
        base = binvox.voxel_from_binvox(
            rl_data, shape, translate, scale, axis_order="xzy"
        )
        s = scale / (n - 1)
        np.testing.assert_equal(
            base.transform,
            np.array([[s, 0, 0, 2], [0, s, 0, 5], [0, 0, s, 10], [0, 0, 0, 1]]),
        )
        dense = dense.transpose((0, 2, 1))
        bound_min = translate - 0.5 * s
        bound_max = translate + scale + 0.5 * s
        np.testing.assert_allclose(base.bounds, [bound_min, bound_max])
        np.testing.assert_equal(base.encoding.dense, dense)

        if binvox.binvox_encoder is None:
            g.log.warning("No binvox encoder found, skipping binvox export tests")
            return

        file_obj = BytesIO(binvox.export_binvox(base))
        file_obj.seek(0)
        loaded = binvox.load_binvox(file_obj)
        np.testing.assert_equal(loaded.encoding.dense, base.encoding.dense)
        self.assertTrue(isinstance(base, v.VoxelGrid))
        self.assertTrue(isinstance(loaded, v.VoxelGrid))
        np.testing.assert_equal(base.transform, loaded.transform)
        np.testing.assert_equal(base.shape, loaded.shape)


if __name__ == "__main__":
    g.trimesh.util.attach_to_log()
    g.unittest.main()