Coverage for volume/tests/test_mesh.py: 100%

35 statements  

« 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.volume.mesh` module.""" 

2 

3from __future__ import annotations 

4 

5from itertools import product 

6 

7import numpy as np 

8 

9from colour.constants import TOLERANCE_ABSOLUTE_TESTS 

10from colour.utilities import ignore_numpy_errors, is_scipy_installed 

11from colour.volume import is_within_mesh_volume 

12 

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" 

19 

20__all__ = [ 

21 "TestIsWithinMeshVolume", 

22] 

23 

24 

25class TestIsWithinMeshVolume: 

26 """ 

27 Define :func:`colour.volume.mesh.is_within_mesh_volume` definition unit 

28 tests methods. 

29 """ 

30 

31 def setup_method(self) -> None: 

32 """Initialise the common tests attributes.""" 

33 

34 self._mesh = np.array( 

35 [ 

36 [-1.0, -1.0, 1.0], 

37 [1.0, -1.0, 1.0], 

38 [1.0, -1.0, -1.0], 

39 [-1.0, -1.0, -1.0], 

40 [0.0, 1.0, 0.0], 

41 ] 

42 ) 

43 

44 def test_is_within_mesh_volume(self) -> None: 

45 """Test :func:`colour.volume.mesh.is_within_mesh_volume` definition.""" 

46 

47 if not is_scipy_installed(): # pragma: no cover 

48 return 

49 

50 assert is_within_mesh_volume(np.array([0.0005, 0.0031, 0.0010]), self._mesh) 

51 

52 assert not is_within_mesh_volume(np.array([0.3205, 0.4131, 0.5100]), self._mesh) 

53 

54 assert is_within_mesh_volume(np.array([0.0025, 0.0088, 0.0340]), self._mesh) 

55 

56 assert not is_within_mesh_volume(np.array([0.4325, 0.3788, 0.1034]), self._mesh) 

57 

58 def test_n_dimensional_is_within_mesh_volume(self) -> None: 

59 """ 

60 Test :func:`colour.volume.mesh.is_within_mesh_volume` definition 

61 n-dimensional arrays support. 

62 """ 

63 

64 if not is_scipy_installed(): # pragma: no cover 

65 return 

66 

67 a = np.array([0.0005, 0.0031, 0.0010]) 

68 b = is_within_mesh_volume(a, self._mesh) 

69 

70 a = np.tile(a, (6, 1)) 

71 b = np.tile(b, 6) 

72 np.testing.assert_allclose( 

73 is_within_mesh_volume(a, self._mesh), 

74 b, 

75 atol=TOLERANCE_ABSOLUTE_TESTS, 

76 ) 

77 

78 a = np.reshape(a, (2, 3, 3)) 

79 b = np.reshape(b, (2, 3)) 

80 np.testing.assert_allclose( 

81 is_within_mesh_volume(a, self._mesh), 

82 b, 

83 atol=TOLERANCE_ABSOLUTE_TESTS, 

84 ) 

85 

86 @ignore_numpy_errors 

87 def test_nan_is_within_mesh_volume(self) -> None: 

88 """ 

89 Test :func:`colour.volume.mesh.is_within_mesh_volume` definition nan 

90 support. 

91 """ 

92 

93 if not is_scipy_installed(): # pragma: no cover 

94 return 

95 

96 cases = [-1.0, 0.0, 1.0, -np.inf, np.inf, np.nan] 

97 cases = np.array(list(set(product(cases, repeat=3)))) 

98 is_within_mesh_volume(cases, self._mesh)