|
| 1 | +import cccorelib |
| 2 | +import numpy as np |
| 3 | +import pycc |
| 4 | +import pytest |
| 5 | + |
| 6 | +# Vertices of the cube |
| 7 | +VERTICES = np.array([ |
| 8 | + [-0.5, -0.5, -0.5], |
| 9 | + [-0.5, -0.5, 0.5], |
| 10 | + [-0.5, 0.5, -0.5], |
| 11 | + [-0.5, 0.5, 0.5], |
| 12 | + [0.5, -0.5, -0.5], |
| 13 | + [0.5, -0.5, 0.5], |
| 14 | + [0.5, 0.5, -0.5], |
| 15 | + [0.5, 0.5, 0.5] |
| 16 | +]) |
| 17 | + |
| 18 | +# Indices of the cube |
| 19 | +INDICES = np.array([ |
| 20 | + [0, 1, 3], |
| 21 | + [0, 3, 2], |
| 22 | + [0, 2, 4], |
| 23 | + [2, 6, 4], |
| 24 | + [0, 4, 1], |
| 25 | + [1, 4, 5], |
| 26 | + [2, 3, 6], |
| 27 | + [3, 7, 6], |
| 28 | + [4, 6, 5], |
| 29 | + [5, 6, 7], |
| 30 | + [1, 5, 7], |
| 31 | + [1, 7, 3] |
| 32 | +]) |
| 33 | + |
| 34 | +def assert_ccvector_are_equal(a: cccorelib.CCVector3, b: cccorelib.CCVector3): |
| 35 | + assert a.x == b.x |
| 36 | + assert a.y == b.y |
| 37 | + assert a.z == b.z |
| 38 | + |
| 39 | +def expected_points_of_triangle(triangle_index): |
| 40 | + x = cccorelib.CCVector3( |
| 41 | + VERTICES[INDICES[triangle_index, 0], 0], |
| 42 | + VERTICES[INDICES[triangle_index, 0], 1], |
| 43 | + VERTICES[INDICES[triangle_index, 0], 2] |
| 44 | + ) |
| 45 | + y = cccorelib.CCVector3( |
| 46 | + VERTICES[INDICES[triangle_index, 1], 0], |
| 47 | + VERTICES[INDICES[triangle_index, 1], 1], |
| 48 | + VERTICES[INDICES[triangle_index, 1], 2] |
| 49 | + ) |
| 50 | + z = cccorelib.CCVector3( |
| 51 | + VERTICES[INDICES[triangle_index, 2], 0], |
| 52 | + VERTICES[INDICES[triangle_index, 2], 1], |
| 53 | + VERTICES[INDICES[triangle_index, 2], 2] |
| 54 | + ) |
| 55 | + return x, y, z |
| 56 | + |
| 57 | + |
| 58 | +def build_cube_mesh(): |
| 59 | + vertices = pycc.ccPointCloud(VERTICES[:, 0], VERTICES[:, 1], VERTICES[:, 2]) |
| 60 | + mesh = pycc.ccMesh(vertices) |
| 61 | + for (i1, i2, i3) in INDICES: |
| 62 | + mesh.addTriangle(i1, i2, i3) |
| 63 | + |
| 64 | + return mesh, vertices |
| 65 | + |
| 66 | + |
| 67 | +def test_building_mesh(): |
| 68 | + mesh, vertices = build_cube_mesh() |
| 69 | + assert vertices.size() == len(VERTICES) |
| 70 | + assert mesh.size() == len(INDICES) |
| 71 | + assert vertices == mesh.getAssociatedCloud() |
| 72 | + |
| 73 | + |
| 74 | +def test_mesh_lifetime_1(): |
| 75 | + mesh, vertices = build_cube_mesh() |
| 76 | + assert vertices == mesh.getAssociatedCloud() |
| 77 | + del mesh |
| 78 | + # If mesh is deleted, vertices should still be alive |
| 79 | + assert vertices.size() == len(VERTICES) |
| 80 | + |
| 81 | + |
| 82 | +def test_mesh_lifetime_2(): |
| 83 | + mesh, vertices = build_cube_mesh() |
| 84 | + assert vertices == mesh.getAssociatedCloud() |
| 85 | + del vertices |
| 86 | + # If vertices is deleted, then actually the mesh still have access to it |
| 87 | + # i.e. the mesh keeps it alive. |
| 88 | + # In CPP API, there is a HOjbect dependency between the two, and so when |
| 89 | + # the vertices are deleted, the mesh is notified of it and empties himself, so it is actually safe |
| 90 | + # to delete the vertices, but as lifetime is not manual in Python, it's better to keep it alive |
| 91 | + assert mesh.size() == len(INDICES) |
| 92 | + assert mesh.getAssociatedCloud().size() == len(VERTICES) |
| 93 | + |
| 94 | + |
| 95 | +def test_getTriangleVertIndexes(): |
| 96 | + x, y, z = cccorelib.CCVector3(), cccorelib.CCVector3(), cccorelib.CCVector3() |
| 97 | + mesh, vertices = build_cube_mesh() |
| 98 | + |
| 99 | + for i in range(mesh.size()): |
| 100 | + vert_indices = mesh.getTriangleVertIndexes(i) |
| 101 | + assert vert_indices.i1 == INDICES[i, 0] |
| 102 | + assert vert_indices.i2 == INDICES[i, 1] |
| 103 | + assert vert_indices.i3 == INDICES[i, 2] |
| 104 | + |
| 105 | + for j in range(3): |
| 106 | + assert vert_indices[j] == INDICES[i, j] |
| 107 | + |
| 108 | + vertices.getPoint(vert_indices.i1, x) |
| 109 | + vertices.getPoint(vert_indices.i2, y) |
| 110 | + vertices.getPoint(vert_indices.i3, z) |
| 111 | + ex, ey, ez = expected_points_of_triangle(i) |
| 112 | + assert_ccvector_are_equal(x, ex) |
| 113 | + assert_ccvector_are_equal(y, ey) |
| 114 | + assert_ccvector_are_equal(z, ez) |
| 115 | + |
| 116 | + with pytest.raises(IndexError): |
| 117 | + mesh.getTriangleVertIndexes(mesh.size()) |
| 118 | + |
| 119 | + with pytest.raises(IndexError): |
| 120 | + mesh.getTriangleVertIndexes(mesh.size() + 1) |
| 121 | + |
| 122 | + |
| 123 | +def test_getTriangleVertices(): |
| 124 | + x, y, z = cccorelib.CCVector3(), cccorelib.CCVector3(), cccorelib.CCVector3() |
| 125 | + mesh, _ = build_cube_mesh() |
| 126 | + |
| 127 | + for i in range(mesh.size()): |
| 128 | + mesh.getTriangleVertices(i, x, y, z) |
| 129 | + ex, ey, ez = expected_points_of_triangle(i) |
| 130 | + assert_ccvector_are_equal(x, ex) |
| 131 | + assert_ccvector_are_equal(y, ey) |
| 132 | + assert_ccvector_are_equal(z, ez) |
| 133 | + |
| 134 | + with pytest.raises(IndexError): |
| 135 | + mesh.getTriangleVertices(mesh.size(), x, y, z) |
| 136 | + |
| 137 | + with pytest.raises(IndexError): |
| 138 | + mesh.getTriangleVertices(mesh.size() + 1, x, y, z) |
0 commit comments