differt 0.9.0__tar.gz → 0.9.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {differt-0.9.0 → differt-0.9.1}/PKG-INFO +3 -3
- {differt-0.9.0 → differt-0.9.1}/README.md +1 -1
- {differt-0.9.0 → differt-0.9.1}/pyproject.toml +1 -1
- {differt-0.9.0 → differt-0.9.1}/src/differt/geometry/_paths.py +1 -6
- {differt-0.9.0 → differt-0.9.1}/src/differt/geometry/_triangle_mesh.py +3 -2
- {differt-0.9.0 → differt-0.9.1}/src/differt/geometry/_utils.py +143 -14
- {differt-0.9.0 → differt-0.9.1}/src/differt/plotting/_core.py +109 -11
- {differt-0.9.0 → differt-0.9.1}/src/differt/scene/_triangle_scene.py +512 -1
- {differt-0.9.0 → differt-0.9.1}/.gitignore +0 -0
- {differt-0.9.0 → differt-0.9.1}/LICENSE.md +0 -0
- {differt-0.9.0 → differt-0.9.1}/src/differt/__init__.py +0 -0
- {differt-0.9.0 → differt-0.9.1}/src/differt/_version.py +0 -0
- {differt-0.9.0 → differt-0.9.1}/src/differt/em/__init__.py +0 -0
- {differt-0.9.0 → differt-0.9.1}/src/differt/em/_antenna.py +0 -0
- {differt-0.9.0 → differt-0.9.1}/src/differt/em/_constants.py +0 -0
- {differt-0.9.0 → differt-0.9.1}/src/differt/em/_fresnel.py +0 -0
- {differt-0.9.0 → differt-0.9.1}/src/differt/em/_interaction_type.py +0 -0
- {differt-0.9.0 → differt-0.9.1}/src/differt/em/_material.py +0 -0
- {differt-0.9.0 → differt-0.9.1}/src/differt/em/_utd.py +0 -0
- {differt-0.9.0 → differt-0.9.1}/src/differt/em/_utils.py +0 -0
- {differt-0.9.0 → differt-0.9.1}/src/differt/geometry/__init__.py +0 -0
- {differt-0.9.0 → differt-0.9.1}/src/differt/plotting/__init__.py +0 -0
- {differt-0.9.0 → differt-0.9.1}/src/differt/plotting/_utils.py +0 -0
- {differt-0.9.0 → differt-0.9.1}/src/differt/plugins/__init__.py +0 -0
- {differt-0.9.0 → differt-0.9.1}/src/differt/plugins/_deepmimo_types.py +0 -0
- {differt-0.9.0 → differt-0.9.1}/src/differt/plugins/deepmimo.py +0 -0
- {differt-0.9.0 → differt-0.9.1}/src/differt/py.typed +0 -0
- {differt-0.9.0 → differt-0.9.1}/src/differt/rt/__init__.py +0 -0
- {differt-0.9.0 → differt-0.9.1}/src/differt/rt/_fermat.py +0 -0
- {differt-0.9.0 → differt-0.9.1}/src/differt/rt/_image_method.py +0 -0
- {differt-0.9.0 → differt-0.9.1}/src/differt/rt/_utils.py +0 -0
- {differt-0.9.0 → differt-0.9.1}/src/differt/scene/__init__.py +0 -0
- {differt-0.9.0 → differt-0.9.1}/src/differt/scene/_sionna.py +0 -0
- {differt-0.9.0 → differt-0.9.1}/src/differt/utils.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: DiffeRT
|
|
3
|
-
Version: 0.9.
|
|
3
|
+
Version: 0.9.1
|
|
4
4
|
Summary: Differentiable Ray Tracing Toolbox for Radio Propagation Simulations
|
|
5
5
|
Project-URL: Changelog, https://github.com/jeertmans/DiffeRT/releases
|
|
6
6
|
Project-URL: Documentation, https://eertmans.be/DiffeRT
|
|
@@ -20,7 +20,7 @@ Classifier: Programming Language :: Python :: 3.14
|
|
|
20
20
|
Classifier: Topic :: Scientific/Engineering
|
|
21
21
|
Requires-Python: >=3.11
|
|
22
22
|
Requires-Dist: chex>=0.1.86
|
|
23
|
-
Requires-Dist: differt-core==0.9.
|
|
23
|
+
Requires-Dist: differt-core==0.9.1
|
|
24
24
|
Requires-Dist: equinox>=0.13.7
|
|
25
25
|
Requires-Dist: filelock>=3.15.4
|
|
26
26
|
Requires-Dist: fpt-jax==0.1.0
|
|
@@ -156,7 +156,7 @@ or:
|
|
|
156
156
|
author = {Eertmans, Jérome},
|
|
157
157
|
url = {https://github.com/jeertmans/DiffeRT},
|
|
158
158
|
license = {MIT},
|
|
159
|
-
version = {v0.9.
|
|
159
|
+
version = {v0.9.1}
|
|
160
160
|
}
|
|
161
161
|
```
|
|
162
162
|
|
|
@@ -128,12 +128,7 @@ class Paths(eqx.Module, Generic[_M]):
|
|
|
128
128
|
|
|
129
129
|
@property
|
|
130
130
|
def shape(self) -> tuple[int, ...]:
|
|
131
|
-
"""
|
|
132
|
-
Return the batch shape of the paths.
|
|
133
|
-
|
|
134
|
-
Returns:
|
|
135
|
-
The shape of paths' batch dimensions.
|
|
136
|
-
"""
|
|
131
|
+
"""The batch shape of the paths."""
|
|
137
132
|
return self.vertices.shape[:-2]
|
|
138
133
|
|
|
139
134
|
def reshape(self, *batch: int) -> Self:
|
|
@@ -518,6 +518,7 @@ def _triangles_visible_func(
|
|
|
518
518
|
_WARP_MESHES_CACHE[mesh_id] = wp.Mesh(points=points, indices=indices)
|
|
519
519
|
|
|
520
520
|
epsilon = 1e-5
|
|
521
|
+
output_visible.fill_(False) # noqa: FBT003
|
|
521
522
|
|
|
522
523
|
wp.launch(
|
|
523
524
|
_triangles_visible_kernel,
|
|
@@ -581,7 +582,7 @@ def _triangles_visible_cpu_impl(
|
|
|
581
582
|
wp_ray_origins = wp.from_jax(jax_ray_origins, dtype=wp.vec3)
|
|
582
583
|
wp_ray_directions = wp.from_jax(jax_ray_directions, dtype=wp.vec3)
|
|
583
584
|
|
|
584
|
-
output_visible = wp.
|
|
585
|
+
output_visible = wp.empty(
|
|
585
586
|
total_batches * num_triangles, dtype=bool, device=wp_ray_origins.device
|
|
586
587
|
)
|
|
587
588
|
|
|
@@ -1473,7 +1474,7 @@ class TriangleMesh(eqx.Module):
|
|
|
1473
1474
|
# Using self.triangle_vertices is important because, e.g., as a result of using
|
|
1474
1475
|
# __getitem__, some vertices in 'self.vertices' may no longer be used by this mesh.
|
|
1475
1476
|
vertices = self.triangle_vertices
|
|
1476
|
-
where = self.mask[:, None] if self.mask is not None else None
|
|
1477
|
+
where = self.mask[:, None, None] if self.mask is not None else None
|
|
1477
1478
|
return jnp.vstack(
|
|
1478
1479
|
(
|
|
1479
1480
|
jnp.min(vertices, axis=(0, 1), initial=+jnp.inf, where=where),
|
|
@@ -418,17 +418,69 @@ def fibonacci_lattice(
|
|
|
418
418
|
msg = f"Unsupported dtype {dtype!r}, must be a floating dtype."
|
|
419
419
|
raise ValueError(msg)
|
|
420
420
|
|
|
421
|
-
phi = 1.618033988749895 # golden ratio
|
|
422
421
|
i = jnp.arange(0.0, n) # '0.0' forces floating point values
|
|
423
422
|
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
423
|
+
# Compute the fractional part of (i / phi) for the azimuthal angle.
|
|
424
|
+
#
|
|
425
|
+
# Naively computing ``(i / phi) % 1.0`` in float32 loses precision for
|
|
426
|
+
# large i (e.g. i >= 10_000_000), because the float32 mantissa only has
|
|
427
|
+
# ~7 decimal digits. At i = 10^7, the product ``i * inv_phi`` is ~6.18e6,
|
|
428
|
+
# and taking ``% 1.0`` amplifies the rounding error so severely that only
|
|
429
|
+
# a handful of unique fractional values remain in the last ~10k points,
|
|
430
|
+
# producing visible "hatching" artifacts in the generated lattice.
|
|
431
|
+
#
|
|
432
|
+
# To fix this we decompose i = q1*m1 + q2*m2 + r with m1 = 2^18 and
|
|
433
|
+
# m2 = 2^9 (chosen so that every intermediate product stays < 1000),
|
|
434
|
+
# and exploit the identity:
|
|
435
|
+
#
|
|
436
|
+
# (i * inv_phi) % 1 = (q1*(inv_phi*m1 % 1)
|
|
437
|
+
# + q2*(inv_phi*m2 % 1)
|
|
438
|
+
# + r * inv_phi ) % 1
|
|
439
|
+
#
|
|
440
|
+
# Each term is small enough to preserve full float32 precision.
|
|
441
|
+
inv_phi = 0.6180339887498949 # 1 / phi
|
|
442
|
+
m1 = 262144.0 # 2^18
|
|
443
|
+
m2 = 512.0 # 2^9
|
|
444
|
+
|
|
445
|
+
# Pre-compute the fractional parts of inv_phi * m1 and inv_phi * m2,
|
|
446
|
+
# which are constants independent of i.
|
|
447
|
+
inv_phi_m1 = (inv_phi * m1) % 1.0
|
|
448
|
+
inv_phi_m2 = (inv_phi * m2) % 1.0
|
|
449
|
+
|
|
450
|
+
# Decompose i into a two-stage quotient-remainder representation.
|
|
451
|
+
q1 = jnp.floor(i / m1)
|
|
452
|
+
rem = i - q1 * m1
|
|
453
|
+
q2 = jnp.floor(rem / m2)
|
|
454
|
+
r = rem - q2 * m2
|
|
455
|
+
|
|
456
|
+
# Reconstruct the fractional part with full precision.
|
|
457
|
+
frac = (q1 * inv_phi_m1 + q2 * inv_phi_m2 + r * inv_phi) % 1.0
|
|
428
458
|
|
|
429
459
|
if frustum is not None:
|
|
430
|
-
|
|
431
|
-
|
|
460
|
+
# When a viewing frustum is provided, distribute points uniformly
|
|
461
|
+
# in solid angle within the frustum's polar and azimuthal bounds.
|
|
462
|
+
p_min, a_min = frustum[0, -2:]
|
|
463
|
+
p_max, a_max = frustum[1, -2:]
|
|
464
|
+
|
|
465
|
+
# Uniform spacing in cos(polar) ensures equal solid-angle coverage
|
|
466
|
+
# (the Jacobian of the sphere is sin(p) dp da = -d(cos p) da).
|
|
467
|
+
cos_p_min = jnp.cos(p_min)
|
|
468
|
+
cos_p_max = jnp.cos(p_max)
|
|
469
|
+
denom = jnp.where(n > 1, n - 1, 1.0)
|
|
470
|
+
cos_lat = cos_p_min - (cos_p_min - cos_p_max) * (i / denom)
|
|
471
|
+
lat = jnp.arccos(cos_lat)
|
|
472
|
+
|
|
473
|
+
# Azimuthal angle uses the quasi-random fractional part to break
|
|
474
|
+
# lattice alignment, mapped into the frustum's azimuthal range.
|
|
475
|
+
a_width = a_max - a_min
|
|
476
|
+
lon = a_min + a_width * frac
|
|
477
|
+
pa = jnp.stack((lat, lon), axis=-1)
|
|
478
|
+
else:
|
|
479
|
+
# Full-sphere Fibonacci lattice: uniform in cos(polar) over [0, pi]
|
|
480
|
+
# and quasi-random in azimuth over [0, 2*pi].
|
|
481
|
+
lat = jnp.arccos(1 - 2 * i / n)
|
|
482
|
+
lon = 2 * jnp.pi * frac
|
|
483
|
+
pa = jnp.stack((lat, lon), axis=-1)
|
|
432
484
|
|
|
433
485
|
return spherical_to_cartesian(pa).astype(dtype)
|
|
434
486
|
|
|
@@ -724,9 +776,48 @@ def viewing_frustum(
|
|
|
724
776
|
... showlegend=False,
|
|
725
777
|
... ) # doctest: +SKIP
|
|
726
778
|
>>> fig # doctest: +SKIP
|
|
779
|
+
|
|
780
|
+
This fourth example shows how to launch rays in a frustum covering a loaded scene,
|
|
781
|
+
with the viewing vertex (tx) positioned at the center at height z=32.
|
|
782
|
+
|
|
783
|
+
.. plotly::
|
|
784
|
+
:context:
|
|
785
|
+
|
|
786
|
+
>>> from differt.scene import TriangleScene, get_sionna_scene
|
|
787
|
+
>>> from differt.plotting import draw_rays, reuse, draw_markers
|
|
788
|
+
>>> from differt.geometry import fibonacci_lattice, viewing_frustum
|
|
789
|
+
>>>
|
|
790
|
+
>>> # Load the simple street canyon scene
|
|
791
|
+
>>> scene_path = get_sionna_scene("simple_street_canyon")
|
|
792
|
+
>>> scene = TriangleScene.load_xml(scene_path)
|
|
793
|
+
>>> tx = jnp.array([0.0, 0.0, 32.0])
|
|
794
|
+
>>>
|
|
795
|
+
>>> with reuse("plotly") as fig:
|
|
796
|
+
... draw_markers(tx.reshape(-1, 3), labels=["tx"], showlegend=False)
|
|
797
|
+
... scene.mesh.plot(opacity=0.5)
|
|
798
|
+
...
|
|
799
|
+
... frustum = viewing_frustum(
|
|
800
|
+
... tx, scene.mesh.triangle_vertices.reshape(-1, 3)
|
|
801
|
+
... )
|
|
802
|
+
... frustum = frustum.at[1, 1].set(jnp.pi)
|
|
803
|
+
... ray_origins, ray_directions = jnp.broadcast_arrays(
|
|
804
|
+
... tx, fibonacci_lattice(200, frustum=frustum)
|
|
805
|
+
... )
|
|
806
|
+
... ray_origins += 0.5 * ray_directions
|
|
807
|
+
... ray_directions *= 40.0 # Scale rays length before plotting
|
|
808
|
+
... draw_rays(
|
|
809
|
+
... ray_origins,
|
|
810
|
+
... ray_directions,
|
|
811
|
+
... color="red",
|
|
812
|
+
... showlegend=False,
|
|
813
|
+
... ) # doctest: +SKIP
|
|
814
|
+
>>> fig # doctest: +SKIP
|
|
727
815
|
"""
|
|
728
816
|
world_vertices = jnp.asarray(world_vertices)
|
|
729
817
|
viewing_vertex = jnp.asarray(viewing_vertex)
|
|
818
|
+
|
|
819
|
+
# Convert all world vertices to spherical coordinates (r, polar, azimuth)
|
|
820
|
+
# relative to the viewing vertex.
|
|
730
821
|
xyz = world_vertices - viewing_vertex[..., None, :]
|
|
731
822
|
rpa = cartesian_to_spherical(xyz)
|
|
732
823
|
|
|
@@ -737,23 +828,38 @@ def viewing_frustum(
|
|
|
737
828
|
|
|
738
829
|
axis = None if reduce else -1
|
|
739
830
|
|
|
831
|
+
# ------------------------------------------------------------------
|
|
832
|
+
# Radial and polar bounds: straightforward min/max over active verts.
|
|
833
|
+
# ------------------------------------------------------------------
|
|
740
834
|
r_min = jnp.min(r, axis=axis, where=active_vertices, initial=jnp.inf)
|
|
741
835
|
r_max = jnp.max(r, axis=axis, where=active_vertices, initial=0)
|
|
742
836
|
p_min = jnp.min(p, axis=axis, where=active_vertices, initial=jnp.pi)
|
|
743
837
|
p_max = jnp.max(p, axis=axis, where=active_vertices, initial=0)
|
|
838
|
+
|
|
839
|
+
# ------------------------------------------------------------------
|
|
840
|
+
# Azimuthal bounds — handling the -pi / +pi discontinuity.
|
|
841
|
+
#
|
|
842
|
+
# cartesian_to_spherical returns azimuth in [-pi, pi). When geometry
|
|
843
|
+
# straddles the -pi/+pi boundary (e.g. angles {-170°, +170°}), a
|
|
844
|
+
# naive min/max would give [-170°, +170°] = 340° span, even though
|
|
845
|
+
# the actual span is only 20° across the boundary.
|
|
846
|
+
#
|
|
847
|
+
# Strategy: compute bounds in TWO domains, then keep the tighter one.
|
|
848
|
+
# ------------------------------------------------------------------
|
|
849
|
+
|
|
850
|
+
# Domain 1: native [-pi, pi) range.
|
|
744
851
|
a_min = jnp.min(a, axis=axis, where=active_vertices, initial=jnp.pi)
|
|
745
852
|
a_max = jnp.max(a, axis=axis, where=active_vertices, initial=-jnp.pi)
|
|
746
853
|
|
|
747
|
-
#
|
|
748
|
-
#
|
|
749
|
-
|
|
750
|
-
# We map azimuthal angles from [-pi;pi[ to [0;2pi[.
|
|
854
|
+
# Domain 2: shift to [0, 2*pi) — the discontinuity moves to 0/2*pi,
|
|
855
|
+
# which is harmless when geometry is near ±pi.
|
|
751
856
|
two_pi = 2 * jnp.pi
|
|
752
|
-
|
|
753
857
|
a_0 = (a + two_pi) % two_pi
|
|
754
858
|
a_0_min = jnp.min(a_0, axis=axis, where=active_vertices, initial=two_pi)
|
|
755
859
|
a_0_max = jnp.max(a_0, axis=axis, where=active_vertices, initial=0)
|
|
756
860
|
|
|
861
|
+
# Compare the angular widths in both domains and keep the narrower one,
|
|
862
|
+
# since that is the one that avoids the wrap-around discontinuity.
|
|
757
863
|
a_width = a_max - a_min
|
|
758
864
|
a_0_width = a_0_max - a_0_min
|
|
759
865
|
|
|
@@ -763,8 +869,31 @@ def viewing_frustum(
|
|
|
763
869
|
jnp.stack((a_min, a_max)),
|
|
764
870
|
)
|
|
765
871
|
|
|
766
|
-
#
|
|
767
|
-
#
|
|
872
|
+
# ------------------------------------------------------------------
|
|
873
|
+
# Full-circle fallback.
|
|
874
|
+
#
|
|
875
|
+
# When the geometry truly surrounds the viewing vertex (e.g. a TX
|
|
876
|
+
# placed inside a corridor that extends in all azimuthal directions),
|
|
877
|
+
# *both* domains produce a large span (> 270°). Neither domain can
|
|
878
|
+
# represent the coverage compactly, so we fall back to the full
|
|
879
|
+
# circle [-pi, pi] to avoid excluding valid angular regions.
|
|
880
|
+
# ------------------------------------------------------------------
|
|
881
|
+
min_width = jnp.minimum(a_width, a_0_width)
|
|
882
|
+
use_full_circle = min_width > 1.5 * jnp.pi # > 270°
|
|
883
|
+
a_min = jnp.where(use_full_circle, -jnp.pi, a_min)
|
|
884
|
+
a_max = jnp.where(use_full_circle, jnp.pi, a_max)
|
|
885
|
+
|
|
886
|
+
# ------------------------------------------------------------------
|
|
887
|
+
# Polar angle special case.
|
|
888
|
+
#
|
|
889
|
+
# When all vertices share the same polar angle (p_min == p_max),
|
|
890
|
+
# the frustum degenerates to a zero-width band. We expand it so
|
|
891
|
+
# that rays can still reach the geometry:
|
|
892
|
+
# - Option A: expand downward to 0 (keep max, widen upward).
|
|
893
|
+
# - Option B: expand upward to pi (keep min, widen downward).
|
|
894
|
+
# We pick whichever yields the smaller total width.
|
|
895
|
+
# TODO: improve this heuristic for more general degenerate cases.
|
|
896
|
+
# ------------------------------------------------------------------
|
|
768
897
|
p_0_min = p_min
|
|
769
898
|
p_0_max = p_max
|
|
770
899
|
|
|
@@ -616,8 +616,8 @@ def draw_image(
|
|
|
616
616
|
data: Real[ArrayLike, "rows cols"]
|
|
617
617
|
| Real[ArrayLike, "rows cols 3"]
|
|
618
618
|
| Real[ArrayLike, "rows cols 4"],
|
|
619
|
-
x: Real[ArrayLike, " cols"] | Real[ArrayLike, "rows cols
|
|
620
|
-
y: Real[ArrayLike, " rows"] | Real[ArrayLike, "rows cols
|
|
619
|
+
x: Real[ArrayLike, " cols"] | Real[ArrayLike, "rows cols"] | None = None,
|
|
620
|
+
y: Real[ArrayLike, " rows"] | Real[ArrayLike, "rows cols"] | None = None,
|
|
621
621
|
z0: float = 0.0,
|
|
622
622
|
**kwargs: Any,
|
|
623
623
|
) -> Canvas | MplFigure | Figure:
|
|
@@ -639,9 +639,15 @@ def draw_image(
|
|
|
639
639
|
kwargs: Keyword arguments passed to
|
|
640
640
|
:class:`Image<vispy.scene.visuals.Image>`,
|
|
641
641
|
:meth:`contourf<mpl_toolkits.mplot3d.axes3d.Axes3D.contourf>`,
|
|
642
|
-
or :class:`
|
|
642
|
+
or :class:`Surface<plotly.graph_objects.Surface>`, depending on the
|
|
643
643
|
backend.
|
|
644
644
|
|
|
645
|
+
.. important::
|
|
646
|
+
|
|
647
|
+
If data is RGB or RGBA, Plotly's :class:`Mesh3d<plotly.graph_objects.Mesh3d>`
|
|
648
|
+
is used instead of :class:`Surface<plotly.graph_objects.Surface>`
|
|
649
|
+
to display the image.
|
|
650
|
+
|
|
645
651
|
Returns:
|
|
646
652
|
The resulting plot output.
|
|
647
653
|
|
|
@@ -670,6 +676,26 @@ def draw_image(
|
|
|
670
676
|
>>> fig2 = draw_image(Z, x=x, y=y, backend="plotly")
|
|
671
677
|
>>> fig2 # doctest: +SKIP
|
|
672
678
|
|
|
679
|
+
The next example shows how to plot an RGBA image.
|
|
680
|
+
|
|
681
|
+
.. plotly::
|
|
682
|
+
|
|
683
|
+
>>> from differt.plotting import draw_image
|
|
684
|
+
>>>
|
|
685
|
+
>>> x = np.linspace(-1.0, +1.0, 100)
|
|
686
|
+
>>> y = np.linspace(-4.0, +4.0, 200)
|
|
687
|
+
>>> X, Y = np.meshgrid(x, y)
|
|
688
|
+
>>> Z = np.sin(X) * np.cos(Y)
|
|
689
|
+
>>> # Let's mask some values to draw a ring
|
|
690
|
+
>>> R = np.sqrt(16 * X * X + Y * Y)
|
|
691
|
+
>>> r = (X + 1.0) / 2.0
|
|
692
|
+
>>> g = (Y + 4.0) / 8.0
|
|
693
|
+
>>> b = (Z + 1.0) / 2.0
|
|
694
|
+
>>> a = np.where((R > 0.5) & (R < 1.5), 0.0, 0.5)
|
|
695
|
+
>>> rgba_image = np.stack([r, g, b, a], axis=-1)
|
|
696
|
+
>>> fig = draw_image(rgba_image, backend="plotly")
|
|
697
|
+
>>> fig # doctest: +SKIP
|
|
698
|
+
|
|
673
699
|
"""
|
|
674
700
|
raise NotImplementedError
|
|
675
701
|
|
|
@@ -679,8 +705,8 @@ def _(
|
|
|
679
705
|
data: Real[ArrayLike, "rows cols"]
|
|
680
706
|
| Real[ArrayLike, "rows cols 3"]
|
|
681
707
|
| Real[ArrayLike, "rows cols 4"],
|
|
682
|
-
x: Real[ArrayLike, " cols"] | Real[ArrayLike, "rows cols
|
|
683
|
-
y: Real[ArrayLike, " rows"] | Real[ArrayLike, "rows cols
|
|
708
|
+
x: Real[ArrayLike, " cols"] | Real[ArrayLike, "rows cols"] | None = None,
|
|
709
|
+
y: Real[ArrayLike, " rows"] | Real[ArrayLike, "rows cols"] | None = None,
|
|
684
710
|
z0: float = 0.0,
|
|
685
711
|
**kwargs: Any,
|
|
686
712
|
) -> Canvas:
|
|
@@ -727,8 +753,8 @@ def _(
|
|
|
727
753
|
data: Real[ArrayLike, "rows cols"]
|
|
728
754
|
| Real[ArrayLike, "rows cols 3"]
|
|
729
755
|
| Real[ArrayLike, "rows cols 4"],
|
|
730
|
-
x: Real[ArrayLike, " cols"] | Real[ArrayLike, "rows cols
|
|
731
|
-
y: Real[ArrayLike, " rows"] | Real[ArrayLike, "rows cols
|
|
756
|
+
x: Real[ArrayLike, " cols"] | Real[ArrayLike, "rows cols"] | None = None,
|
|
757
|
+
y: Real[ArrayLike, " rows"] | Real[ArrayLike, "rows cols"] | None = None,
|
|
732
758
|
z0: float = 0.0,
|
|
733
759
|
**kwargs: Any,
|
|
734
760
|
) -> MplFigure:
|
|
@@ -751,17 +777,89 @@ def _(
|
|
|
751
777
|
data: Real[ArrayLike, "rows cols"]
|
|
752
778
|
| Real[ArrayLike, "rows cols 3"]
|
|
753
779
|
| Real[ArrayLike, "rows cols 4"],
|
|
754
|
-
x: Real[ArrayLike, " cols"] | Real[ArrayLike, "rows cols
|
|
755
|
-
y: Real[ArrayLike, " rows"] | Real[ArrayLike, "rows cols
|
|
780
|
+
x: Real[ArrayLike, " cols"] | Real[ArrayLike, "rows cols"] | None = None,
|
|
781
|
+
y: Real[ArrayLike, " rows"] | Real[ArrayLike, "rows cols"] | None = None,
|
|
756
782
|
z0: float = 0.0,
|
|
757
783
|
**kwargs: Any,
|
|
758
784
|
) -> Figure:
|
|
759
785
|
fig = process_plotly_kwargs(kwargs)
|
|
760
786
|
|
|
761
787
|
data = np.asarray(data)
|
|
788
|
+
|
|
762
789
|
x = None if x is None else np.asarray(x)
|
|
763
790
|
y = None if y is None else np.asarray(y)
|
|
764
791
|
|
|
792
|
+
kwargs.setdefault("hoverinfo", "skip")
|
|
793
|
+
|
|
794
|
+
if data.ndim == 3 and data.shape[-1] in {3, 4}: # noqa: PLR2004
|
|
795
|
+
rows, cols, channels = data.shape
|
|
796
|
+
if x is not None and x.ndim == 1 and len(x) == cols:
|
|
797
|
+
dx = np.diff(x)
|
|
798
|
+
dx = np.append(dx, dx[-1]) if len(dx) > 0 else np.array([1.0])
|
|
799
|
+
x_edges = np.concatenate([
|
|
800
|
+
[x[0] - dx[0] / 2],
|
|
801
|
+
x[:-1] + dx[:-1] / 2,
|
|
802
|
+
[x[-1] + dx[-1] / 2],
|
|
803
|
+
])
|
|
804
|
+
else:
|
|
805
|
+
x_edges = np.arange(cols + 1)
|
|
806
|
+
|
|
807
|
+
if y is not None and y.ndim == 1 and len(y) == rows:
|
|
808
|
+
dy = np.diff(y)
|
|
809
|
+
dy = np.append(dy, dy[-1]) if len(dy) > 0 else np.array([1.0])
|
|
810
|
+
y_edges = np.concatenate([
|
|
811
|
+
[y[0] - dy[0] / 2],
|
|
812
|
+
y[:-1] + dy[:-1] / 2,
|
|
813
|
+
[y[-1] + dy[-1] / 2],
|
|
814
|
+
])
|
|
815
|
+
else:
|
|
816
|
+
y_edges = np.arange(rows + 1)
|
|
817
|
+
|
|
818
|
+
x, y = np.meshgrid(x_edges, y_edges)
|
|
819
|
+
x = x.ravel()
|
|
820
|
+
y = y.ravel()
|
|
821
|
+
z = np.full_like(x, z0)
|
|
822
|
+
|
|
823
|
+
# 2. Build Triangles (2 per pixel)
|
|
824
|
+
r = np.arange(rows)
|
|
825
|
+
c = np.arange(cols)
|
|
826
|
+
c, r = np.meshgrid(c, r)
|
|
827
|
+
c = c.ravel()
|
|
828
|
+
r = r.ravel()
|
|
829
|
+
|
|
830
|
+
v0 = r * (cols + 1) + c
|
|
831
|
+
v1 = r * (cols + 1) + (c + 1)
|
|
832
|
+
v2 = (r + 1) * (cols + 1) + c
|
|
833
|
+
v3 = (r + 1) * (cols + 1) + (c + 1)
|
|
834
|
+
|
|
835
|
+
# Triangle 1 (v0, v1, v2) and Triangle 2 (v1, v3, v2)
|
|
836
|
+
i = np.concatenate([v0, v1])
|
|
837
|
+
j = np.concatenate([v1, v3])
|
|
838
|
+
k = np.concatenate([v2, v2])
|
|
839
|
+
|
|
840
|
+
facecolors = data.reshape(-1, channels)
|
|
841
|
+
facecolors = np.concatenate(
|
|
842
|
+
[
|
|
843
|
+
facecolors,
|
|
844
|
+
facecolors,
|
|
845
|
+
],
|
|
846
|
+
axis=0,
|
|
847
|
+
)
|
|
848
|
+
|
|
849
|
+
kwargs.setdefault("flatshading", False)
|
|
850
|
+
kwargs.setdefault(
|
|
851
|
+
"lighting",
|
|
852
|
+
{
|
|
853
|
+
"ambient": 1.0,
|
|
854
|
+
"diffuse": 0.0,
|
|
855
|
+
"specular": 0.0,
|
|
856
|
+
"roughness": 1.0,
|
|
857
|
+
"fresnel": 0.0,
|
|
858
|
+
},
|
|
859
|
+
)
|
|
860
|
+
return fig.add_mesh3d(
|
|
861
|
+
x=x, y=y, z=z, i=i, j=j, k=k, facecolor=facecolors, **kwargs
|
|
862
|
+
)
|
|
765
863
|
return fig.add_surface(
|
|
766
864
|
x=x,
|
|
767
865
|
y=y,
|
|
@@ -774,8 +872,8 @@ def _(
|
|
|
774
872
|
@dispatch
|
|
775
873
|
def draw_contour(
|
|
776
874
|
data: Real[ArrayLike, "rows cols"],
|
|
777
|
-
x: Real[ArrayLike, " cols"] | Real[ArrayLike, "rows cols
|
|
778
|
-
y: Real[ArrayLike, " rows"] | Real[ArrayLike, "rows cols
|
|
875
|
+
x: Real[ArrayLike, " cols"] | Real[ArrayLike, "rows cols"] | None = None,
|
|
876
|
+
y: Real[ArrayLike, " rows"] | Real[ArrayLike, "rows cols"] | None = None,
|
|
779
877
|
z0: float = 0.0,
|
|
780
878
|
levels: int | Real[ArrayLike, " num_levels"] | None = None,
|
|
781
879
|
fill: bool = False,
|
|
@@ -2,13 +2,16 @@ import math
|
|
|
2
2
|
import typing
|
|
3
3
|
import warnings
|
|
4
4
|
from collections.abc import Iterator, Mapping
|
|
5
|
-
from
|
|
5
|
+
from functools import partial
|
|
6
|
+
from typing import TYPE_CHECKING, Any, Literal, cast, no_type_check, overload
|
|
6
7
|
|
|
7
8
|
import equinox as eqx
|
|
8
9
|
import jax
|
|
9
10
|
import jax.numpy as jnp
|
|
10
11
|
import numpy as np
|
|
12
|
+
import warp as wp
|
|
11
13
|
from jaxtyping import Array, ArrayLike, Bool, Float, Int
|
|
14
|
+
from jaxtyping import UInt as Uint
|
|
12
15
|
|
|
13
16
|
import differt_core.scene
|
|
14
17
|
from differt.geometry import (
|
|
@@ -509,6 +512,391 @@ def _compute_paths_sbr(
|
|
|
509
512
|
)
|
|
510
513
|
|
|
511
514
|
|
|
515
|
+
from differt.geometry._triangle_mesh import ( # noqa: E402
|
|
516
|
+
_WARP_MESHES_CACHE, # TODO: should we create a separate cache here?
|
|
517
|
+
)
|
|
518
|
+
|
|
519
|
+
|
|
520
|
+
@no_type_check
|
|
521
|
+
@wp.func
|
|
522
|
+
def combine_hashes(h1: wp.uint32, h2: wp.uint32) -> wp.uint32: # pragma: no cover
|
|
523
|
+
return h1 ^ (
|
|
524
|
+
h2 + wp.uint32(0x9E3779B9) + (h1 << wp.uint32(6)) + (h1 >> wp.uint32(2))
|
|
525
|
+
)
|
|
526
|
+
|
|
527
|
+
|
|
528
|
+
@no_type_check
|
|
529
|
+
@wp.func
|
|
530
|
+
def hash_int(x: wp.uint32) -> wp.uint32: # pragma: no cover
|
|
531
|
+
x = ((x >> wp.uint32(16)) ^ x) * wp.uint32(0x45D9F3B)
|
|
532
|
+
x = ((x >> wp.uint32(16)) ^ x) * wp.uint32(0x45D9F3B)
|
|
533
|
+
return (x >> wp.uint32(16)) ^ x
|
|
534
|
+
|
|
535
|
+
|
|
536
|
+
@no_type_check
|
|
537
|
+
@wp.kernel
|
|
538
|
+
def _compute_tx_mlm_kernel(
|
|
539
|
+
mesh_id: wp.uint64,
|
|
540
|
+
mesh_points: wp.array[wp.vec3],
|
|
541
|
+
mesh_indices: wp.array[wp.int32],
|
|
542
|
+
ray_origins: wp.array(dtype=wp.vec3, ndim=2),
|
|
543
|
+
ray_directions: wp.array(dtype=wp.vec3, ndim=2),
|
|
544
|
+
dim_x: int,
|
|
545
|
+
dim_y: int,
|
|
546
|
+
max_order: int,
|
|
547
|
+
min_order: int,
|
|
548
|
+
assume_quads: bool,
|
|
549
|
+
receiver_height: float,
|
|
550
|
+
min_x: float,
|
|
551
|
+
max_x: float,
|
|
552
|
+
min_y: float,
|
|
553
|
+
max_y: float,
|
|
554
|
+
output: wp.array3d[wp.uint32],
|
|
555
|
+
) -> None: # pragma: no cover
|
|
556
|
+
itx, iray = wp.tid()
|
|
557
|
+
|
|
558
|
+
current_origin = ray_origins[itx, iray]
|
|
559
|
+
current_direction = ray_directions[itx, iray]
|
|
560
|
+
ray_hash = wp.uint32(2166136261)
|
|
561
|
+
|
|
562
|
+
epsilon = wp.float32(1e-4)
|
|
563
|
+
dx = (wp.float32(max_x) - wp.float32(min_x)) / wp.float32(dim_x)
|
|
564
|
+
dy = (wp.float32(max_y) - wp.float32(min_y)) / wp.float32(dim_y)
|
|
565
|
+
|
|
566
|
+
for t in range(max_order + 1):
|
|
567
|
+
# Query closest hit along the ray
|
|
568
|
+
query_origin = current_origin
|
|
569
|
+
if t > 0:
|
|
570
|
+
query_origin = current_origin + current_direction * epsilon
|
|
571
|
+
|
|
572
|
+
res = wp.mesh_query_ray(mesh_id, query_origin, current_direction, wp.inf)
|
|
573
|
+
|
|
574
|
+
# Distance to closest triangle hit (if any)
|
|
575
|
+
t_hit = wp.inf
|
|
576
|
+
if res.result:
|
|
577
|
+
t_hit = res.t + epsilon if t > 0 else res.t
|
|
578
|
+
|
|
579
|
+
# Intersection with the receiver plane z = receiver_height
|
|
580
|
+
if wp.abs(current_direction[2]) > wp.float32(1e-6):
|
|
581
|
+
u = (wp.float32(receiver_height) - query_origin[2]) / current_direction[2]
|
|
582
|
+
|
|
583
|
+
# Intersection point P
|
|
584
|
+
P = query_origin + current_direction * u # noqa: N806
|
|
585
|
+
|
|
586
|
+
# Check if intersection is valid and unobstructed
|
|
587
|
+
if u > wp.float32(0.0) and u < t_hit: # noqa: SIM102
|
|
588
|
+
if t >= min_order and (
|
|
589
|
+
P[0] >= wp.float32(min_x)
|
|
590
|
+
and P[0] <= wp.float32(max_x)
|
|
591
|
+
and P[1] >= wp.float32(min_y)
|
|
592
|
+
and P[1] <= wp.float32(max_y)
|
|
593
|
+
):
|
|
594
|
+
# It hit the receiver grid!
|
|
595
|
+
ix = wp.int32(wp.floor((P[0] - wp.float32(min_x)) / dx))
|
|
596
|
+
iy = wp.int32(wp.floor((P[1] - wp.float32(min_y)) / dy))
|
|
597
|
+
|
|
598
|
+
# Clip to bounds
|
|
599
|
+
ix = wp.clamp(ix, wp.int32(0), wp.int32(dim_x - 1))
|
|
600
|
+
iy = wp.clamp(iy, wp.int32(0), wp.int32(dim_y - 1))
|
|
601
|
+
|
|
602
|
+
# Add path hash to cell
|
|
603
|
+
wp.atomic_or(output, itx, ix, iy, ray_hash)
|
|
604
|
+
|
|
605
|
+
# If the ray hit a triangle, we bounce it
|
|
606
|
+
if res.result:
|
|
607
|
+
# Update origin to hit point
|
|
608
|
+
current_origin = query_origin + current_direction * res.t
|
|
609
|
+
|
|
610
|
+
# TODO: maybe we should pre-calculate the face normals?
|
|
611
|
+
# Compute face normal
|
|
612
|
+
face_index = res.face
|
|
613
|
+
i0 = mesh_indices[face_index * 3 + 0]
|
|
614
|
+
i1 = mesh_indices[face_index * 3 + 1]
|
|
615
|
+
i2 = mesh_indices[face_index * 3 + 2]
|
|
616
|
+
v0 = mesh_points[i0]
|
|
617
|
+
v1 = mesh_points[i1]
|
|
618
|
+
v2 = mesh_points[i2]
|
|
619
|
+
|
|
620
|
+
# Normal vector
|
|
621
|
+
normal = wp.normalize(wp.cross(v1 - v0, v2 - v0))
|
|
622
|
+
|
|
623
|
+
# Reflected direction
|
|
624
|
+
current_direction = (
|
|
625
|
+
current_direction
|
|
626
|
+
- wp.float32(2.0) * wp.dot(current_direction, normal) * normal
|
|
627
|
+
)
|
|
628
|
+
|
|
629
|
+
# Update path hash
|
|
630
|
+
hash_face = face_index
|
|
631
|
+
if assume_quads:
|
|
632
|
+
hash_face = face_index // 2
|
|
633
|
+
ray_hash = combine_hashes(ray_hash, hash_int(wp.uint32(hash_face)))
|
|
634
|
+
else:
|
|
635
|
+
# No hit, ray goes to infinity
|
|
636
|
+
break
|
|
637
|
+
|
|
638
|
+
|
|
639
|
+
@no_type_check
|
|
640
|
+
def _compute_tx_mlm_func(
|
|
641
|
+
mesh_id: int,
|
|
642
|
+
mesh_points: wp.array[wp.vec3],
|
|
643
|
+
mesh_indices: wp.array[wp.int32],
|
|
644
|
+
ray_origins: wp.array(dtype=wp.vec3, ndim=2),
|
|
645
|
+
ray_directions: wp.array(dtype=wp.vec3, ndim=2),
|
|
646
|
+
dim_x: int,
|
|
647
|
+
dim_y: int,
|
|
648
|
+
num_rays: int,
|
|
649
|
+
max_order: int,
|
|
650
|
+
min_order: int,
|
|
651
|
+
assume_quads: bool,
|
|
652
|
+
receiver_height: float,
|
|
653
|
+
min_x: float,
|
|
654
|
+
max_x: float,
|
|
655
|
+
min_y: float,
|
|
656
|
+
max_y: float,
|
|
657
|
+
output: wp.array(dtype=wp.uint32, ndim=3),
|
|
658
|
+
) -> None:
|
|
659
|
+
if (wp_mesh := _WARP_MESHES_CACHE.get(mesh_id)) is None:
|
|
660
|
+
wp_mesh = wp.Mesh(points=mesh_points, indices=mesh_indices)
|
|
661
|
+
_WARP_MESHES_CACHE[mesh_id] = wp_mesh
|
|
662
|
+
|
|
663
|
+
output.fill_(0)
|
|
664
|
+
|
|
665
|
+
num_tx = ray_origins.shape[0]
|
|
666
|
+
|
|
667
|
+
wp.launch(
|
|
668
|
+
_compute_tx_mlm_kernel,
|
|
669
|
+
dim=(num_tx, num_rays),
|
|
670
|
+
inputs=[
|
|
671
|
+
wp_mesh.id,
|
|
672
|
+
mesh_points,
|
|
673
|
+
mesh_indices,
|
|
674
|
+
ray_origins,
|
|
675
|
+
ray_directions,
|
|
676
|
+
dim_x,
|
|
677
|
+
dim_y,
|
|
678
|
+
max_order,
|
|
679
|
+
min_order,
|
|
680
|
+
assume_quads,
|
|
681
|
+
receiver_height,
|
|
682
|
+
min_x,
|
|
683
|
+
max_x,
|
|
684
|
+
min_y,
|
|
685
|
+
max_y,
|
|
686
|
+
],
|
|
687
|
+
outputs=[output],
|
|
688
|
+
device=ray_origins.device,
|
|
689
|
+
)
|
|
690
|
+
|
|
691
|
+
|
|
692
|
+
@no_type_check
|
|
693
|
+
def _compute_tx_mlm_cuda_impl(
|
|
694
|
+
mesh_id: np.uint64,
|
|
695
|
+
vertices: Float[Array, "num_vertices 3"],
|
|
696
|
+
triangles: Int[Array, "num_triangles 3"],
|
|
697
|
+
ray_origins: Float[Array, "num_tx num_rays 3"],
|
|
698
|
+
ray_directions: Float[Array, "num_tx num_rays 3"],
|
|
699
|
+
*,
|
|
700
|
+
dim_x: int,
|
|
701
|
+
dim_y: int,
|
|
702
|
+
num_rays: int,
|
|
703
|
+
max_order: int,
|
|
704
|
+
min_order: int,
|
|
705
|
+
assume_quads: bool,
|
|
706
|
+
receiver_height: float,
|
|
707
|
+
min_x: float,
|
|
708
|
+
max_x: float,
|
|
709
|
+
min_y: float,
|
|
710
|
+
max_y: float,
|
|
711
|
+
) -> Uint[Array, "num_tx dim_x dim_y"]:
|
|
712
|
+
num_tx = ray_origins.shape[0]
|
|
713
|
+
return wp.jax_callable(
|
|
714
|
+
_compute_tx_mlm_func,
|
|
715
|
+
num_outputs=1,
|
|
716
|
+
output_dims=(num_tx, dim_x, dim_y),
|
|
717
|
+
graph_mode=wp.JaxCallableGraphMode.NONE,
|
|
718
|
+
)(
|
|
719
|
+
mesh_id,
|
|
720
|
+
vertices,
|
|
721
|
+
triangles.ravel(),
|
|
722
|
+
ray_origins,
|
|
723
|
+
ray_directions,
|
|
724
|
+
dim_x,
|
|
725
|
+
dim_y,
|
|
726
|
+
num_rays,
|
|
727
|
+
max_order,
|
|
728
|
+
min_order,
|
|
729
|
+
assume_quads,
|
|
730
|
+
receiver_height,
|
|
731
|
+
min_x,
|
|
732
|
+
max_x,
|
|
733
|
+
min_y,
|
|
734
|
+
max_y,
|
|
735
|
+
)[0]
|
|
736
|
+
|
|
737
|
+
|
|
738
|
+
@no_type_check
|
|
739
|
+
def _compute_tx_mlm_cpu_impl(
|
|
740
|
+
mesh_id: np.uint64,
|
|
741
|
+
vertices: Float[Array, "num_vertices 3"],
|
|
742
|
+
triangles: Int[Array, "num_triangles 3"],
|
|
743
|
+
ray_origins: Float[Array, "num_tx num_rays 3"],
|
|
744
|
+
ray_directions: Float[Array, "num_tx num_rays 3"],
|
|
745
|
+
*,
|
|
746
|
+
dim_x: int,
|
|
747
|
+
dim_y: int,
|
|
748
|
+
num_rays: int,
|
|
749
|
+
max_order: int,
|
|
750
|
+
min_order: int,
|
|
751
|
+
assume_quads: bool,
|
|
752
|
+
receiver_height: float,
|
|
753
|
+
min_x: float,
|
|
754
|
+
max_x: float,
|
|
755
|
+
min_y: float,
|
|
756
|
+
max_y: float,
|
|
757
|
+
) -> Uint[Array, "num_tx dim_x dim_y"]:
|
|
758
|
+
num_tx = ray_origins.shape[0]
|
|
759
|
+
|
|
760
|
+
def callback(
|
|
761
|
+
jax_vertices: Float[Array, "num_vertices 3"],
|
|
762
|
+
jax_triangles: Int[Array, "num_triangles 3"],
|
|
763
|
+
jax_ray_origins: Float[Array, "num_tx num_rays 3"],
|
|
764
|
+
jax_ray_directions: Float[Array, "num_tx num_rays 3"],
|
|
765
|
+
) -> Uint[Array, "num_tx dim_x dim_y"]:
|
|
766
|
+
wp_vertices = wp.from_jax(jax_vertices, dtype=wp.vec3)
|
|
767
|
+
wp_triangles = wp.from_jax(jax_triangles.ravel(), dtype=wp.int32)
|
|
768
|
+
wp_ray_origins = wp.from_jax(jax_ray_origins, dtype=wp.vec3)
|
|
769
|
+
wp_ray_directions = wp.from_jax(jax_ray_directions, dtype=wp.vec3)
|
|
770
|
+
|
|
771
|
+
output = wp.empty(
|
|
772
|
+
(num_tx, dim_x, dim_y), dtype=wp.uint32, device=wp_ray_origins.device
|
|
773
|
+
)
|
|
774
|
+
|
|
775
|
+
_compute_tx_mlm_func(
|
|
776
|
+
int(mesh_id),
|
|
777
|
+
wp_vertices,
|
|
778
|
+
wp_triangles,
|
|
779
|
+
wp_ray_origins,
|
|
780
|
+
wp_ray_directions,
|
|
781
|
+
dim_x,
|
|
782
|
+
dim_y,
|
|
783
|
+
num_rays,
|
|
784
|
+
max_order,
|
|
785
|
+
min_order,
|
|
786
|
+
assume_quads,
|
|
787
|
+
receiver_height,
|
|
788
|
+
min_x,
|
|
789
|
+
max_x,
|
|
790
|
+
min_y,
|
|
791
|
+
max_y,
|
|
792
|
+
output,
|
|
793
|
+
)
|
|
794
|
+
|
|
795
|
+
return wp.to_jax(output)
|
|
796
|
+
|
|
797
|
+
return jax.pure_callback(
|
|
798
|
+
callback,
|
|
799
|
+
jax.ShapeDtypeStruct((num_tx, dim_x, dim_y), jnp.uint32),
|
|
800
|
+
vertices,
|
|
801
|
+
triangles,
|
|
802
|
+
ray_origins,
|
|
803
|
+
ray_directions,
|
|
804
|
+
)
|
|
805
|
+
|
|
806
|
+
|
|
807
|
+
@eqx.filter_jit
|
|
808
|
+
@no_type_check
|
|
809
|
+
def _compute_tx_mlm(
|
|
810
|
+
tx: Float[Array, "num_tx 3"],
|
|
811
|
+
mesh: TriangleMesh,
|
|
812
|
+
max_order: int,
|
|
813
|
+
min_order: int,
|
|
814
|
+
assume_quads: bool,
|
|
815
|
+
dim_x: int,
|
|
816
|
+
dim_y: int,
|
|
817
|
+
num_rays: int,
|
|
818
|
+
receiver_height: float,
|
|
819
|
+
min_x: float,
|
|
820
|
+
max_x: float,
|
|
821
|
+
min_y: float,
|
|
822
|
+
max_y: float,
|
|
823
|
+
) -> Uint[Array, "num_tx dim_x dim_y"]:
|
|
824
|
+
# Prepare arrays
|
|
825
|
+
points = mesh.vertices
|
|
826
|
+
indices = mesh.triangles
|
|
827
|
+
|
|
828
|
+
world_vertices = mesh.triangle_vertices.reshape(-1, 3)
|
|
829
|
+
|
|
830
|
+
if mesh.mask is not None:
|
|
831
|
+
active_vertices = jnp.repeat(mesh.mask, 3, axis=0)
|
|
832
|
+
indices = jnp.where(mesh.mask[:, None], indices, 0)
|
|
833
|
+
else:
|
|
834
|
+
active_vertices = None
|
|
835
|
+
|
|
836
|
+
# Include the 4 corner points of the receiver plane to expand viewing frustum
|
|
837
|
+
corners = jnp.array([
|
|
838
|
+
[min_x, min_y, receiver_height],
|
|
839
|
+
[max_x, min_y, receiver_height],
|
|
840
|
+
[max_x, max_y, receiver_height],
|
|
841
|
+
[min_x, max_y, receiver_height],
|
|
842
|
+
])
|
|
843
|
+
world_vertices = jnp.concatenate((world_vertices, corners), axis=0)
|
|
844
|
+
if active_vertices is not None:
|
|
845
|
+
active_vertices = jnp.concatenate(
|
|
846
|
+
(active_vertices, jnp.ones(4, dtype=bool)), axis=0
|
|
847
|
+
)
|
|
848
|
+
|
|
849
|
+
def gen_rays(
|
|
850
|
+
t: Float[Array, "3"],
|
|
851
|
+
) -> tuple[Float[Array, "num_rays 3"], Float[Array, "num_rays 3"]]:
|
|
852
|
+
f = viewing_frustum(t, world_vertices, active_vertices=active_vertices)
|
|
853
|
+
f = f.at[1, 1].set(jnp.pi) # TODO: fixme
|
|
854
|
+
origins = jnp.repeat(t[None, :], num_rays, axis=0)
|
|
855
|
+
directions = fibonacci_lattice(num_rays, frustum=f)
|
|
856
|
+
return origins, directions
|
|
857
|
+
|
|
858
|
+
ray_origins, ray_directions = jax.vmap(gen_rays)(tx)
|
|
859
|
+
|
|
860
|
+
mesh_id = np.uint64(id(mesh))
|
|
861
|
+
|
|
862
|
+
return jax.lax.platform_dependent(
|
|
863
|
+
points,
|
|
864
|
+
indices,
|
|
865
|
+
ray_origins,
|
|
866
|
+
ray_directions,
|
|
867
|
+
cpu=partial(
|
|
868
|
+
_compute_tx_mlm_cpu_impl,
|
|
869
|
+
mesh_id,
|
|
870
|
+
dim_x=dim_x,
|
|
871
|
+
dim_y=dim_y,
|
|
872
|
+
num_rays=num_rays,
|
|
873
|
+
max_order=max_order,
|
|
874
|
+
min_order=min_order,
|
|
875
|
+
assume_quads=assume_quads,
|
|
876
|
+
receiver_height=receiver_height,
|
|
877
|
+
min_x=min_x,
|
|
878
|
+
max_x=max_x,
|
|
879
|
+
min_y=min_y,
|
|
880
|
+
max_y=max_y,
|
|
881
|
+
),
|
|
882
|
+
cuda=partial(
|
|
883
|
+
_compute_tx_mlm_cuda_impl,
|
|
884
|
+
mesh_id,
|
|
885
|
+
dim_x=dim_x,
|
|
886
|
+
dim_y=dim_y,
|
|
887
|
+
num_rays=num_rays,
|
|
888
|
+
max_order=max_order,
|
|
889
|
+
min_order=min_order,
|
|
890
|
+
assume_quads=assume_quads,
|
|
891
|
+
receiver_height=receiver_height,
|
|
892
|
+
min_x=min_x,
|
|
893
|
+
max_x=max_x,
|
|
894
|
+
min_y=min_y,
|
|
895
|
+
max_y=max_y,
|
|
896
|
+
),
|
|
897
|
+
)
|
|
898
|
+
|
|
899
|
+
|
|
512
900
|
class TriangleScene(eqx.Module):
|
|
513
901
|
"""A simple scene made of one or more triangle meshes, some transmitters and some receivers."""
|
|
514
902
|
|
|
@@ -1319,6 +1707,129 @@ class TriangleScene(eqx.Module):
|
|
|
1319
1707
|
batch_size=batch_size,
|
|
1320
1708
|
).reshape(*tx_batch, *rx_batch, path_candidates.shape[0])
|
|
1321
1709
|
|
|
1710
|
+
def compute_tx_mlm(
|
|
1711
|
+
self,
|
|
1712
|
+
max_order: int,
|
|
1713
|
+
dim_x: int,
|
|
1714
|
+
dim_y: int,
|
|
1715
|
+
num_rays: int = int(1e6),
|
|
1716
|
+
min_order: int = 0,
|
|
1717
|
+
height: float | None = None,
|
|
1718
|
+
) -> Uint[Array, "*transmitters_batch dim_x dim_y"]:
|
|
1719
|
+
"""
|
|
1720
|
+
Compute the Multipath Lifetime Map (MLM) from the transmitter(s) for a moving receiver on a 2D grid in the XY plane.
|
|
1721
|
+
|
|
1722
|
+
This method implements the MLM algorithm described in the paper
|
|
1723
|
+
*Comparing Differentiable and Dynamic Ray Tracing: Introducing the
|
|
1724
|
+
Multipath Lifetime Map* :cite:`mlm-eucap2025`.
|
|
1725
|
+
|
|
1726
|
+
Rather than performing exhaustive ray tracing for each grid receiver (which is
|
|
1727
|
+
computationally expensive and has a large memory footprint, as shown in the
|
|
1728
|
+
{ref}`multipath_lifetime_map` tutorial notebook), this function uses a
|
|
1729
|
+
**shooting and bouncing ray (SBR)** approach to efficiently sample paths from
|
|
1730
|
+
the transmitter and resolve which receiver cells they intersect.
|
|
1731
|
+
|
|
1732
|
+
Warning:
|
|
1733
|
+
Because this function relies on a stochastic SBR approach, there is a
|
|
1734
|
+
trade-off between grid density and ray count. When increasing the resolution
|
|
1735
|
+
of the grid (i.e., ``dim_x`` and ``dim_y``), you **must** increase ``num_rays``
|
|
1736
|
+
correspondingly. Otherwise, some grid cells will not be sampled by any rays,
|
|
1737
|
+
leading to "unreached" cells and visible noise/holes in the map.
|
|
1738
|
+
|
|
1739
|
+
Args:
|
|
1740
|
+
max_order: The maximum path order (number of bounces).
|
|
1741
|
+
dim_x: The number of grid cells along the X-axis.
|
|
1742
|
+
dim_y: The number of grid cells along the Y-axis.
|
|
1743
|
+
num_rays: The number of rays to launch from the transmitter.
|
|
1744
|
+
min_order: The minimum path order (number of bounces).
|
|
1745
|
+
height: The height (altitude) at which the MLM is computed. If None,
|
|
1746
|
+
defaults to the height of the first receiver, or 1.5 if no receivers.
|
|
1747
|
+
|
|
1748
|
+
Returns:
|
|
1749
|
+
A 2D array representing the path hashes for each grid cell.
|
|
1750
|
+
|
|
1751
|
+
Examples:
|
|
1752
|
+
The following example demonstrates how to compute and visualize a 3D MLM
|
|
1753
|
+
for a simple street canyon scene.
|
|
1754
|
+
|
|
1755
|
+
.. plotly::
|
|
1756
|
+
|
|
1757
|
+
>>> from differt.scene import TriangleScene, get_sionna_scene
|
|
1758
|
+
>>> from differt.plotting import draw_image
|
|
1759
|
+
>>> import equinox as eqx
|
|
1760
|
+
>>>
|
|
1761
|
+
>>> # Load the simple street canyon scene
|
|
1762
|
+
>>> scene_path = get_sionna_scene("simple_street_canyon")
|
|
1763
|
+
>>> scene = TriangleScene.load_xml(scene_path)
|
|
1764
|
+
>>> scene = eqx.tree_at(
|
|
1765
|
+
... lambda s: s.transmitters, scene, jnp.array([0.0, 0.0, 32.0])
|
|
1766
|
+
... )
|
|
1767
|
+
>>>
|
|
1768
|
+
>>> # Define grid limits and compute the MLM at height z=1.5
|
|
1769
|
+
>>> bbox = scene.mesh.bounding_box
|
|
1770
|
+
>>> x = jnp.linspace(bbox[0, 0], bbox[1, 0], 100)
|
|
1771
|
+
>>> y = jnp.linspace(bbox[0, 1], bbox[1, 1], 100)
|
|
1772
|
+
>>> mlm = scene.compute_tx_mlm(
|
|
1773
|
+
... max_order=2,
|
|
1774
|
+
... dim_x=100,
|
|
1775
|
+
... dim_y=100,
|
|
1776
|
+
... height=1.5,
|
|
1777
|
+
... )
|
|
1778
|
+
>>>
|
|
1779
|
+
>>> # Map hashes to random colors, masking out the background (hash=0)
|
|
1780
|
+
>>> mlm = mlm.T # Transpose to swap x and y axes
|
|
1781
|
+
>>> cell_colors = jnp.vectorize(
|
|
1782
|
+
... lambda h: jr.uniform(jr.key(h), shape=(4,)).at[3].set(1),
|
|
1783
|
+
... signature="()->(4)",
|
|
1784
|
+
... )(mlm)
|
|
1785
|
+
>>> cell_colors = jnp.where(mlm[..., None] == 0, 0, cell_colors)
|
|
1786
|
+
>>>
|
|
1787
|
+
>>> # Plot scene and overlay the computed MLM at the target height
|
|
1788
|
+
>>> fig = scene.plot(backend="plotly")
|
|
1789
|
+
>>> fig = draw_image(
|
|
1790
|
+
... cell_colors,
|
|
1791
|
+
... x=x,
|
|
1792
|
+
... y=y,
|
|
1793
|
+
... z0=1.5,
|
|
1794
|
+
... figure=fig,
|
|
1795
|
+
... backend="plotly",
|
|
1796
|
+
... ) # doctest: +SKIP
|
|
1797
|
+
>>> fig # doctest: +SKIP
|
|
1798
|
+
"""
|
|
1799
|
+
tx_shape = self.transmitters.shape[:-1]
|
|
1800
|
+
tx_flat = jax.lax.stop_gradient(self.transmitters).reshape(-1, 3)
|
|
1801
|
+
if height is not None:
|
|
1802
|
+
receiver_height = height
|
|
1803
|
+
elif self.receivers.size > 0:
|
|
1804
|
+
receiver_height = float(self.receivers.reshape(-1, 3)[0, 2])
|
|
1805
|
+
else:
|
|
1806
|
+
receiver_height = 1.5
|
|
1807
|
+
|
|
1808
|
+
bbox = self.mesh.bounding_box
|
|
1809
|
+
min_x = float(bbox[0, 0])
|
|
1810
|
+
max_x = float(bbox[1, 0])
|
|
1811
|
+
min_y = float(bbox[0, 1])
|
|
1812
|
+
max_y = float(bbox[1, 1])
|
|
1813
|
+
|
|
1814
|
+
out = _compute_tx_mlm(
|
|
1815
|
+
tx_flat,
|
|
1816
|
+
self.mesh,
|
|
1817
|
+
max_order=max_order,
|
|
1818
|
+
min_order=min_order,
|
|
1819
|
+
assume_quads=self.mesh.assume_quads,
|
|
1820
|
+
dim_x=dim_x,
|
|
1821
|
+
dim_y=dim_y,
|
|
1822
|
+
num_rays=num_rays,
|
|
1823
|
+
receiver_height=receiver_height,
|
|
1824
|
+
min_x=min_x,
|
|
1825
|
+
max_x=max_x,
|
|
1826
|
+
min_y=min_y,
|
|
1827
|
+
max_y=max_y,
|
|
1828
|
+
)
|
|
1829
|
+
|
|
1830
|
+
res = out.reshape(*tx_shape, dim_x, dim_y)
|
|
1831
|
+
return jax.lax.stop_gradient(res)
|
|
1832
|
+
|
|
1322
1833
|
def plot(
|
|
1323
1834
|
self,
|
|
1324
1835
|
tx_kwargs: Mapping[str, Any] | None = None,
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|