Coverage for volume/mesh.py: 22%
18 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"""
2Mesh Volume Computation Helpers
3===============================
5Define helper objects for computing volumes of three-dimensional meshes
6and polyhedra using Delaunay triangulation and related computational
7geometry methods.
8"""
10from __future__ import annotations
12import typing
14import numpy as np
16from colour.constants import EPSILON
17from colour.utilities import as_float_array, required
19if typing.TYPE_CHECKING:
20 from colour.hints import ArrayLike, NDArrayFloat
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"
29__all__ = [
30 "is_within_mesh_volume",
31]
34@required("SciPy")
35def 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.
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.
51 Returns
52 -------
53 :class:`numpy.ndarray`
54 Boolean array indicating whether specified points are within
55 specified mesh volume.
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 """
75 from scipy.spatial import Delaunay # noqa: PLC0415
77 triangulation = Delaunay(as_float_array(mesh))
79 simplex = triangulation.find_simplex(as_float_array(points), tol=tolerance)
81 return np.where(simplex >= 0, True, False)