Coverage for geometry/tests/test_section.py: 100%
37 statements
« prev ^ index » next coverage.py v7.11.0, created at 2025-11-16 22:49 +1300
« prev ^ index » next coverage.py v7.11.0, created at 2025-11-16 22:49 +1300
1"""Define the unit tests for the :mod:`colour.geometry.section` module."""
3from __future__ import annotations
5import numpy as np
6import pytest
8from colour.constants import TOLERANCE_ABSOLUTE_TESTS
9from colour.geometry import hull_section, primitive_cube
10from colour.geometry.section import close_chord, edges_to_chord, unique_vertices
11from colour.utilities import is_trimesh_installed
13__author__ = "Colour Developers"
14__copyright__ = "Copyright 2013 Colour Developers"
15__license__ = "BSD-3-Clause - https://opensource.org/licenses/BSD-3-Clause"
16__maintainer__ = "Colour Developers"
17__email__ = "colour-developers@colour-science.org"
18__status__ = "Production"
20__all__ = [
21 "TestEdgesToChord",
22 "TestCloseChord",
23 "TestUniqueVertices",
24 "TestHullSection",
25]
28class TestEdgesToChord:
29 """
30 Define :func:`colour.geometry.section.edges_to_chord` definition unit
31 tests methods.
32 """
34 def test_edges_to_chord(self) -> None:
35 """Test :func:`colour.geometry.section.edges_to_chord` definition."""
37 edges = np.array(
38 [
39 [[0.0, -0.5, 0.0], [0.5, -0.5, 0.0]],
40 [[-0.5, -0.5, 0.0], [0.0, -0.5, 0.0]],
41 [[0.5, 0.5, 0.0], [0.0, 0.5, 0.0]],
42 [[0.0, 0.5, 0.0], [-0.5, 0.5, 0.0]],
43 [[-0.5, 0.0, 0.0], [-0.5, -0.5, 0.0]],
44 [[-0.5, 0.5, 0.0], [-0.5, 0.0, 0.0]],
45 [[0.5, -0.5, 0.0], [0.5, 0.0, 0.0]],
46 [[0.5, 0.0, 0.0], [0.5, 0.5, 0.0]],
47 ]
48 )
50 np.testing.assert_allclose(
51 edges_to_chord(edges),
52 np.array(
53 [
54 [0.0, -0.5, 0.0],
55 [0.5, -0.5, 0.0],
56 [0.5, -0.5, -0.0],
57 [0.5, 0.0, -0.0],
58 [0.5, 0.0, -0.0],
59 [0.5, 0.5, -0.0],
60 [0.5, 0.5, 0.0],
61 [0.0, 0.5, 0.0],
62 [0.0, 0.5, 0.0],
63 [-0.5, 0.5, 0.0],
64 [-0.5, 0.5, -0.0],
65 [-0.5, 0.0, -0.0],
66 [-0.5, 0.0, -0.0],
67 [-0.5, -0.5, -0.0],
68 [-0.5, -0.5, 0.0],
69 [0.0, -0.5, 0.0],
70 ]
71 ),
72 atol=TOLERANCE_ABSOLUTE_TESTS,
73 )
75 np.testing.assert_allclose(
76 edges_to_chord(edges, 5),
77 np.array(
78 [
79 [-0.5, 0.5, 0.0],
80 [-0.5, 0.0, 0.0],
81 [-0.5, 0.0, 0.0],
82 [-0.5, -0.5, 0.0],
83 [-0.5, -0.5, 0.0],
84 [0.0, -0.5, 0.0],
85 [0.0, -0.5, 0.0],
86 [0.5, -0.5, 0.0],
87 [0.5, -0.5, 0.0],
88 [0.5, 0.0, 0.0],
89 [0.5, 0.0, 0.0],
90 [0.5, 0.5, 0.0],
91 [0.5, 0.5, 0.0],
92 [0.0, 0.5, 0.0],
93 [0.0, 0.5, 0.0],
94 [-0.5, 0.5, 0.0],
95 ]
96 ),
97 atol=TOLERANCE_ABSOLUTE_TESTS,
98 )
101class TestCloseChord:
102 """
103 Define :func:`colour.geometry.section.close_chord` definition unit tests
104 methods.
105 """
107 def test_close_chord(self) -> None:
108 """Test :func:`colour.geometry.section.close_chord` definition."""
110 np.testing.assert_allclose(
111 close_chord(np.array([[0.0, 0.5, 0.0], [0.0, 0.0, 0.5]])),
112 np.array([[0.0, 0.5, 0.0], [0.0, 0.0, 0.5], [0.0, 0.5, 0.0]]),
113 atol=TOLERANCE_ABSOLUTE_TESTS,
114 )
117class TestUniqueVertices:
118 """
119 Define :func:`colour.geometry.section.unique_vertices` definition unit
120 tests methods.
121 """
123 def test_unique_vertices(self) -> None:
124 """Test :func:`colour.geometry.section.unique_vertices` definition."""
126 np.testing.assert_allclose(
127 unique_vertices(
128 np.array([[0.0, 0.5, 0.0], [0.0, 0.0, 0.5], [0.0, 0.5, 0.0]])
129 ),
130 np.array([[0.0, 0.5, 0.0], [0.0, 0.0, 0.5]]),
131 atol=TOLERANCE_ABSOLUTE_TESTS,
132 )
134 np.testing.assert_allclose(
135 unique_vertices(
136 np.array([[0.0, 0.51, 0.0], [0.0, 0.0, 0.51], [0.0, 0.52, 0.0]]),
137 1,
138 ),
139 np.array([[0.0, 0.5, 0.0], [0.0, 0.0, 0.5]]),
140 atol=TOLERANCE_ABSOLUTE_TESTS,
141 )
144class TestHullSection:
145 """
146 Define :func:`colour.geometry.section.hull_section` definition unit tests
147 methods.
148 """
150 def test_hull_section(self) -> None:
151 """Test :func:`colour.geometry.section.hull_section` definition."""
153 if not is_trimesh_installed(): # pragma: no cover
154 return
156 import trimesh # noqa: PLC0415
158 vertices, faces, _outline = primitive_cube(1, 1, 1, 2, 2, 2)
159 hull = trimesh.Trimesh(vertices["position"], faces, process=False)
161 np.testing.assert_allclose(
162 hull_section(hull, origin=0),
163 np.array(
164 [
165 [0.0, -0.5, 0.0],
166 [0.5, -0.5, 0.0],
167 [0.5, 0.0, 0.0],
168 [0.5, 0.5, 0.0],
169 [0.0, 0.5, 0.0],
170 [-0.5, 0.5, 0.0],
171 [-0.5, 0.0, 0.0],
172 [-0.5, -0.5, 0.0],
173 [0.0, -0.5, 0.0],
174 ]
175 ),
176 atol=TOLERANCE_ABSOLUTE_TESTS,
177 )
179 np.testing.assert_allclose(
180 hull_section(hull, axis="+x", origin=0),
181 np.array(
182 [
183 [0.0, 0.0, -0.5],
184 [0.0, 0.5, -0.5],
185 [0.0, 0.5, 0.0],
186 [0.0, 0.5, 0.5],
187 [0.0, 0.0, 0.5],
188 [0.0, -0.5, 0.5],
189 [0.0, -0.5, 0.0],
190 [0.0, -0.5, -0.5],
191 [0.0, 0.0, -0.5],
192 ]
193 ),
194 atol=TOLERANCE_ABSOLUTE_TESTS,
195 )
197 np.testing.assert_allclose(
198 hull_section(hull, axis="+y", origin=0),
199 np.array(
200 [
201 [0.0, 0.0, -0.5],
202 [-0.5, 0.0, -0.5],
203 [-0.5, 0.0, 0.0],
204 [-0.5, 0.0, 0.5],
205 [0.0, 0.0, 0.5],
206 [0.5, 0.0, 0.5],
207 [0.5, 0.0, 0.0],
208 [0.5, 0.0, -0.5],
209 [0.0, 0.0, -0.5],
210 ]
211 ),
212 atol=TOLERANCE_ABSOLUTE_TESTS,
213 )
215 hull.vertices = (hull.vertices + 0.5) * 2
216 np.testing.assert_allclose(
217 hull_section(hull, origin=0.5, normalise=True),
218 np.array(
219 [
220 [1.0, 0.0, 1.0],
221 [2.0, 0.0, 1.0],
222 [2.0, 1.0, 1.0],
223 [2.0, 2.0, 1.0],
224 [1.0, 2.0, 1.0],
225 [0.0, 2.0, 1.0],
226 [0.0, 1.0, 1.0],
227 [0.0, 0.0, 1.0],
228 [1.0, 0.0, 1.0],
229 ]
230 ),
231 atol=TOLERANCE_ABSOLUTE_TESTS,
232 )
234 pytest.raises(ValueError, hull_section, hull, origin=-1)