Coverage for volume/mesh.py: 22%

1"""

2 Mesh Volume Computation Helpers

3===============================

4

5 Define helper objects for computing volumes of three-dimensional meshes

6 and polyhedra using Delaunay triangulation and related computational

7 geometry methods.

8"""

9

10 from __future__ import annotations

11

12 import typing

13

14 import numpy as np

15

16 from colour.constants import EPSILON

17 from colour.utilities import as_float_array, required

18

19 if typing.TYPE_CHECKING:

20 from colour.hints import ArrayLike, NDArrayFloat

21

22 __author__ = "Colour Developers"

23 __copyright__ = "Copyright 2013 Colour Developers"

24 __license__ = "BSD-3-Clause - https://opensource.org/licenses/BSD-3-Clause"

25 __maintainer__ = "Colour Developers"

26 __email__ = "colour-developers@colour-science.org"

27 __status__ = "Production"

28

29 __all__ = [

30 "is_within_mesh_volume",

31]

32

33

34@required("SciPy")

35 def is_within_mesh_volume(

36 points: ArrayLike, mesh: ArrayLike, tolerance: float = 100 * EPSILON

37) -> NDArrayFloat:

38 """

39 Determine whether the specified points are within the volume defined by a mesh

40 using Delaunay triangulation.

41

42 Parameters

43 ----------

44 points

45 Points to check if they are within ``mesh`` volume.

46 mesh

47 Points of the volume used to generate the Delaunay triangulation.

48 tolerance

49 Tolerance allowed in the inside-triangle check.

50

51 Returns

52 -------

53 :class:`numpy.ndarray`

54 Boolean array indicating whether specified points are within

55 specified mesh volume.

56

57 Examples

58 --------

59 >>> mesh = np.array(

60 ... [

61 ... [-1.0, -1.0, 1.0],

62 ... [1.0, -1.0, 1.0],

63 ... [1.0, -1.0, -1.0],

64 ... [-1.0, -1.0, -1.0],

65 ... [0.0, 1.0, 0.0],

66 ... ]

67 ... )

68 >>> is_within_mesh_volume(np.array([0.0005, 0.0031, 0.0010]), mesh)

69 array(True, dtype=bool)

70 >>> a = np.array([[0.0005, 0.0031, 0.0010], [0.3205, 0.4131, 0.5100]])

71 >>> is_within_mesh_volume(a, mesh)

72 array([ True, False], dtype=bool)

73 """

74

75 from scipy.spatial import Delaunay # noqa: PLC0415

76

77 triangulation = Delaunay(as_float_array(mesh))

78

79 simplex = triangulation.find_simplex(as_float_array(points), tol=tolerance)

80

81 return np.where(simplex >= 0, True, False)