differt.geometry.fermat_path_on_linear_objects

differt.geometry.fermat_path_on_linear_objects#

fermat_path_on_linear_objects(from_vertex, to_vertex, object_origins, object_vectors, *, interaction_types=None, steps=None, max_steps=None, rtol=None, atol=None, unroll=1, linesearch_steps=1, unroll_linesearch=1, implicit_diff=True, use_image_method=True)[source]#

Return the ray path between a pair of vertices, that reflects or diffracts on a given list of objects in between.

Linear objects are defined by a linear combination of zero or more (possibly orthogonal) vectors, plus a point in space (i.e., the origin).

E.g., a straight line can be defined by a point and a direction vector, while a plane can be defined by a point and two orthogonal direction vectors.

Because the size of object_vectors is determined by the object with the most dimensions, objects with fewer dimensions should have the extra vectors set to zero.

Based on the Fermat principle, this method finds the ray path between the from and to vertices, that minimizes the total length of the path.

While the method assumes that the objects are infinite, as for the image method, choosing an appropriate origin can be important as it will be used as the initial point of the minimization procedure.

Important

The current implementation uses the fpt-jax library [4], which defines jax.custom_vjp functions for more efficient gradient computations, see the paper for more details. The implementation is not guaranteed to converge for all configurations of vertices and objects, and the user may need to tune the number of steps to achieve convergence.

By default, a fixed number of steps (a jax.lax.scan) is used rather than the adaptive, max_steps-bounded jax.lax.while_loop alternative (see max_steps/rtol/atol below): under the batched (jax.vmap-based) evaluation used throughout DiffeRT, every batch element runs in lockstep, so the while-loop only stops once its slowest element converges and does not actually save time in practice, while being harder to keep numerically stable and to differentiate through. The default steps=15 (with use_image_method=True, so only diffracting edges are actually iterated on) was chosen so that path coordinates converge to well below millimeter accuracy, including for hard (grazing/near-degenerate) configurations.

Parameters:
  • from_vertex (Float[ArrayLike, "*#batch 3"]) – from vertex, i.e., vertex from which the ray path starts. In a radio communications context, this is usually the transmitter position.

  • to_vertex (Float[ArrayLike, "*#batch 3"]) – to vertex, i.e., vertex to which the ray path ends. In a radio communications context, this is usually the receiver position.

  • object_origins (Float[ArrayLike, "*#batch num_objects 3"]) – Object origins, used as the initial guess of the minimization procedure.

  • object_vectors (Float[ArrayLike, "*#batch num_objects num_dims 3"]) – Base vector(s) describing each object.

  • interaction_types (Int[ArrayLike, "*#batch num_objects"] | None) –

    Optional InteractionType of each object, used to disambiguate a diffracting edge that happens to be numerically planar. If unset, planarity is inferred from object_vectors alone, which is sufficient as long as diffracting edges are passed with their second vector row set to zero, as documented above.

    Only REFLECTION, DIFFRACTION, and SCATTERING describe a linear object that this solver can handle (the latter is solved identically to REFLECTION, as both bounce off the same planar object); any other value raises at runtime (via equinox.error_if, works under jax.jit too).

  • steps (int | None) – The number of optimization steps to perform, see jax.lax.scan. Mutually exclusive with max_steps.

  • max_steps (int | None) – The maximum number of optimization steps to perform, using an adaptive jax.lax.while_loop with Cauchy termination (see rtol/atol) instead of a fixed-length jax.lax.scan. Mutually exclusive with steps, and requires implicit_diff=True.

  • rtol (float | None) – Relative tolerance for the Cauchy termination criterion. Required if max_steps is set, ignored otherwise.

  • atol (float | None) – Absolute tolerance for the Cauchy termination criterion. Required if max_steps is set, ignored otherwise.

  • unroll (int | bool) – Whether to unroll the optimization loop. Can be a boolean or an integer specifying the number of iterations to unroll, see jax.lax.scan.

  • linesearch_steps (int) – The number of line search steps to perform at each iteration.

  • unroll_linesearch (int | bool) – Whether to unroll the line search loop. Can be a boolean or an integer specifying the number of iterations to unroll, see jax.lax.scan.

  • implicit_diff (bool) – Whether to use implicit differentiation for computing the gradient. See [4] and its GitHub page for more details.

  • use_image_method (bool) – Whether to inline the image method inside the Fermat solver to directly solve planar specular reflections and reduce the number of unknowns to edge diffraction points.

Return type:

Float[Array, "*batch num_objects 3"]

Returns:

Intermediate ray path vertices obtained using Fermat’s principle.

Note

The paths do not contain the starting and ending vertices.

You can easily create the complete ray paths using assemble_path:

path = fermat_path_on_linear_objects(...)

full_path = assemble_path(
    from_vertex,
    path,
    to_vertex,
)

Examples

The following example shows how to use this method to find the ray paths undergoing a diffraction on the edge of a wall, then a reflection on a mirror, before reaching the receiver.

>>> from differt.geometry import (
...     Mesh,
...     assemble_path,
...     fermat_path_on_linear_objects,
...     normalize,
... )
>>> from differt.plotting import draw_markers, draw_paths, reuse
>>>
>>> from_vertex = jnp.array([-2.0, 0.0, 0.0])
>>> to_vertex = jnp.array([0.0, 0.0, 0.0])
>>> wall = Mesh.plane(
...     jnp.array([-1.0, 0.0, 0.0]), normal=jnp.array([1.0, 0.0, 0.0])
... )
>>> mirror = Mesh.plane(
...     jnp.array([1.0, 0.0, 0.0]), normal=jnp.array([1.0, 0.0, 0.0])
... )
>>> object_origins = jnp.array([[-1.0, -0.5, 0.5], [1.0, 0.0, 0.0]])
>>> object_vectors = jnp.array([
...     [[0.0, 1.0, 0.0], [0.0, 0.0, 0.0]],  # Edges only need one vector
...     [[0.0, 1.0, 0.0], [0.0, 0.0, 1.0]],  # Planes need two vectors
... ])
>>> path = fermat_path_on_linear_objects(
...     from_vertex,
...     to_vertex,
...     object_origins,
...     object_vectors,
... )
>>> with reuse(backend="plotly") as fig:
...     wall.plot(color="blue")
...     mirror.plot(color="red")
...     draw_paths(
...         wall.triangle_edges[0, 1, ...],
...         line_color="yellow",
...         line_width=5,
...         name="Edge",
...     )
...
...     full_path = assemble_path(
...         from_vertex,
...         path,
...         to_vertex,
...     )
...     draw_paths(full_path, marker={"color": "green"}, name="Final path")
...     markers = jnp.vstack((from_vertex, to_vertex))
...     draw_markers(
...         markers,
...         labels=["BS", "UE"],
...         marker={"color": "black"},
...         name="BS/UE",
...     )
...     fig.update_layout(scene_aspectmode="data")
>>> fig