pysdic.fem.cast_rays#

cast_rays(vertices_coordinates, connectivity_array, element_type, ray_origins, ray_directions, *, n_subdivisions=None, max_iteration=20, tolerance=1e-10, retry_neighbors=False)[source]#

Compute the first intersection of rays with a 3D surface mesh (\(K = 2\), \(E = 3\)) of any element type.

  • triangle_3: the intersections are computed by pysdic.fem.triangle_3_cast_rays() (exact).

  • Other element types: the surface \(X(\Xi)\) of an element is not flat, so the intersections are computed in two (or three) steps:

    1. Each element is subdivided into triangles (n_subdivisions segments per edge in the reference element) and the rays are intersected with these triangles (pysdic.fem.triangle_3_cast_rays()). This gives the element and a first approximation \((\Xi_0, t_0)\) of each intersection.

    2. The intersection with the exact surface of the element is refined with Newton iterations on \(X(\Xi) - (O + t\,d) = 0\) (3 equations, unknowns \(\xi, \eta, t\)).

    3. Optional (retry_neighbors=True): the rays rejected by the second step (not converged, out of the element or behind the origin) are searched again in the elements at a distance \(\leq 1\) of the element of the first approximation (the element itself and the elements sharing a vertex with it, see pysdic.fem.assemble_elements_adjacency_graph()), with Newton iterations from the center of the reference element and the distance \(t_0\). The valid intersection with the smallest distance \(t\) is kept.

A ray is unlocated (element index -1) if it intersects no element, if the Newton iterations do not converge (e.g. for a ray tangent to the surface), or if the refined intersection leaves the element (by more than tolerance in natural coordinates). The last case may happen for a ray passing very close to an edge of a curved mesh: the subdivided surface (chords of the curved edges) is intersected on the side of an element, while the exact intersection is in its neighbor. The Newton iterations do not change the element, so the ray is rejected. Increase n_subdivisions to reduce this band, or set retry_neighbors=True to search the intersection in the neighboring elements.

Note

The third step only concerns the rejected rays, so its cost is usually small. The first intersection along the ray is not guaranteed for a ray recovered in a neighbor: it is the closest valid intersection among the neighbors.

Note

  • Inputs vertices_coordinates, ray_origins and ray_directions will be converted to numpy.float64.

  • Input connectivity_array will be converted to numpy.int64.

Important

Requires the optional package open3d (pip install open3d).

Parameters:
  • vertices_coordinates (ArrayLike) – An array of shape \((N_v, 3)\) with the finite coordinates of the vertices.

  • connectivity_array (ArrayLike) – An array of shape \((N_e, N_{vpe})\) with the vertex indices of each element.

  • element_type (str) – The type of the elements (case insensitive), a surface element type.

  • ray_origins (ArrayLike) – An array of shape \((N_r, 3)\) with the finite origins of the rays.

  • ray_directions (ArrayLike) – An array of shape \((N_r, 3)\) with the finite non-zero directions of the rays (not necessarily normalized).

  • n_subdivisions (Optional[Integral], optional (default: None)) – The number of segments per edge of the reference element for the first approximation (\(\geq 1\)). By default, 1 for quadrangle_4 (2 triangles per element) and 2 for the quadratic elements. Ignored for triangle_3.

  • max_iteration (Integral, optional (default: 20)) – The maximum number of Newton iterations (\(\geq 1\)). Ignored for triangle_3.

  • tolerance (Real, optional (default: 1e-10)) – The convergence tolerance of the Newton iterations on the natural coordinates, also used to check that the intersection is inside the element. Ignored for triangle_3.

  • retry_neighbors (bool, optional (default: False)) – If True, the rays rejected by the Newton iterations in the element of their first approximation are searched again in the elements at a distance \(\leq 1\) (the element itself, restarted from its center, and the elements sharing a vertex). Ignored for triangle_3 (exact intersections).

Returns:

  • natural_coordinates (numpy.ndarray) – An array of shape \((N_r, 2)\) with the natural coordinates \((\xi, \eta)\) of the intersections (NaN for the unlocated rays).

  • element_indices (numpy.ndarray) – An array of shape \((N_r,)\) with the index of the intersected element (-1 for the unlocated rays).

Return type:

Tuple[ndarray, ndarray]

Examples

Intersect a warped quadrangle with a vertical ray.

 1import numpy
 2from pysdic.fem import cast_rays
 3
 4vertices_coordinates = numpy.array([[-1, -1, 0], [1, -1, 0], [1, 1, 1], [-1, 1, 0]])
 5connectivity_array = numpy.array([[0, 1, 2, 3]])
 6
 7natural_coordinates, element_indices = cast_rays(
 8    vertices_coordinates, connectivity_array, "quadrangle_4",
 9    ray_origins=[[0.5, 0.5, 5.0]], ray_directions=[[0.0, 0.0, -1.0]],
10)
11print(natural_coordinates, element_indices)
[[0.5 0.5]] [0]