differt 0.8.0__tar.gz → 0.8.2__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.8.0 → differt-0.8.2}/PKG-INFO +4 -4
- {differt-0.8.0 → differt-0.8.2}/README.md +1 -1
- {differt-0.8.0 → differt-0.8.2}/pyproject.toml +2 -2
- {differt-0.8.0 → differt-0.8.2}/src/differt/em/_antenna.py +5 -3
- {differt-0.8.0 → differt-0.8.2}/src/differt/geometry/_paths.py +1 -1
- {differt-0.8.0 → differt-0.8.2}/src/differt/geometry/_triangle_mesh.py +472 -5
- {differt-0.8.0 → differt-0.8.2}/src/differt/plotting/_core.py +43 -20
- {differt-0.8.0 → differt-0.8.2}/src/differt/plotting/_utils.py +23 -32
- {differt-0.8.0 → differt-0.8.2}/src/differt/plugins/deepmimo.py +12 -2
- {differt-0.8.0 → differt-0.8.2}/src/differt/rt/_fermat.py +16 -12
- {differt-0.8.0 → differt-0.8.2}/src/differt/rt/_image_method.py +0 -2
- {differt-0.8.0 → differt-0.8.2}/src/differt/scene/_triangle_scene.py +10 -5
- {differt-0.8.0 → differt-0.8.2}/.gitignore +0 -0
- {differt-0.8.0 → differt-0.8.2}/LICENSE.md +0 -0
- {differt-0.8.0 → differt-0.8.2}/src/differt/__init__.py +0 -0
- {differt-0.8.0 → differt-0.8.2}/src/differt/_version.py +0 -0
- {differt-0.8.0 → differt-0.8.2}/src/differt/em/__init__.py +0 -0
- {differt-0.8.0 → differt-0.8.2}/src/differt/em/_constants.py +0 -0
- {differt-0.8.0 → differt-0.8.2}/src/differt/em/_fresnel.py +0 -0
- {differt-0.8.0 → differt-0.8.2}/src/differt/em/_interaction_type.py +0 -0
- {differt-0.8.0 → differt-0.8.2}/src/differt/em/_material.py +0 -0
- {differt-0.8.0 → differt-0.8.2}/src/differt/em/_utd.py +0 -0
- {differt-0.8.0 → differt-0.8.2}/src/differt/em/_utils.py +0 -0
- {differt-0.8.0 → differt-0.8.2}/src/differt/geometry/__init__.py +0 -0
- {differt-0.8.0 → differt-0.8.2}/src/differt/geometry/_utils.py +0 -0
- {differt-0.8.0 → differt-0.8.2}/src/differt/plotting/__init__.py +0 -0
- {differt-0.8.0 → differt-0.8.2}/src/differt/plugins/__init__.py +0 -0
- {differt-0.8.0 → differt-0.8.2}/src/differt/plugins/_deepmimo_types.py +0 -0
- {differt-0.8.0 → differt-0.8.2}/src/differt/py.typed +0 -0
- {differt-0.8.0 → differt-0.8.2}/src/differt/rt/__init__.py +0 -0
- {differt-0.8.0 → differt-0.8.2}/src/differt/rt/_utils.py +0 -0
- {differt-0.8.0 → differt-0.8.2}/src/differt/scene/__init__.py +0 -0
- {differt-0.8.0 → differt-0.8.2}/src/differt/scene/_sionna.py +0 -0
- {differt-0.8.0 → differt-0.8.2}/src/differt/utils.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: DiffeRT
|
|
3
|
-
Version: 0.8.
|
|
3
|
+
Version: 0.8.2
|
|
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
|
|
@@ -21,8 +21,8 @@ Classifier: Programming Language :: Python :: Free Threading
|
|
|
21
21
|
Classifier: Topic :: Scientific/Engineering
|
|
22
22
|
Requires-Python: >=3.11
|
|
23
23
|
Requires-Dist: chex>=0.1.86
|
|
24
|
-
Requires-Dist: differt-core==0.8.
|
|
25
|
-
Requires-Dist: equinox>=0.13.
|
|
24
|
+
Requires-Dist: differt-core==0.8.2
|
|
25
|
+
Requires-Dist: equinox>=0.13.7
|
|
26
26
|
Requires-Dist: filelock>=3.15.4
|
|
27
27
|
Requires-Dist: fpt-jax==0.1.0
|
|
28
28
|
Requires-Dist: jax>=0.8.1
|
|
@@ -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.8.
|
|
159
|
+
version = {v0.8.2}
|
|
160
160
|
}
|
|
161
161
|
```
|
|
162
162
|
|
|
@@ -645,11 +645,13 @@ class RadiationPattern(BaseAntenna):
|
|
|
645
645
|
|
|
646
646
|
r = self.center + distance * jnp.stack((x, y, z), axis=-1)
|
|
647
647
|
|
|
648
|
-
s = self.
|
|
648
|
+
s, p = self.polarization_vectors(r)
|
|
649
649
|
|
|
650
|
-
|
|
650
|
+
power = jnp.sum(s * s, axis=-1, keepdims=True) + jnp.sum(
|
|
651
|
+
p * p, axis=-1, keepdims=True
|
|
652
|
+
)
|
|
651
653
|
|
|
652
|
-
gain =
|
|
654
|
+
gain = power / power.max()
|
|
653
655
|
|
|
654
656
|
r *= gain
|
|
655
657
|
gain = jnp.squeeze(gain, axis=-1)
|
|
@@ -350,7 +350,7 @@ class Paths(eqx.Module, Generic[_M]):
|
|
|
350
350
|
|
|
351
351
|
if not _is_masked_paths(paths):
|
|
352
352
|
# ty is unable to infer that _M & ~Array implies None
|
|
353
|
-
return paths # type: ignore[invalid-return-type]
|
|
353
|
+
return paths # type: ignore[ty:invalid-return-type]
|
|
354
354
|
|
|
355
355
|
mask = paths.mask
|
|
356
356
|
if mask.dtype == jnp.bool_:
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import typing
|
|
2
|
+
import warnings
|
|
2
3
|
from collections.abc import Callable, Iterator, Mapping, Sequence
|
|
3
4
|
from dataclasses import replace
|
|
4
5
|
from typing import (
|
|
@@ -770,6 +771,18 @@ class TriangleMesh(eqx.Module):
|
|
|
770
771
|
if self.mask is None:
|
|
771
772
|
return jax.tree.map(lambda m: m, self)
|
|
772
773
|
|
|
774
|
+
triangles = self.triangles[self.mask, :]
|
|
775
|
+
# We will need to re-index the vertices, so we first get the unique vertex indices used by the active triangles,
|
|
776
|
+
used_indices = jnp.unique(triangles.reshape(-1))
|
|
777
|
+
# Then we create a lookup table to re-index the triangles, where the indices of the used vertices are replaced by their new indices.
|
|
778
|
+
lookup = jnp.empty((self.vertices.shape[0],), dtype=triangles.dtype)
|
|
779
|
+
lookup = lookup.at[used_indices].set(
|
|
780
|
+
jnp.arange(used_indices.shape[0], dtype=triangles.dtype)
|
|
781
|
+
)
|
|
782
|
+
# Keep unique vertices
|
|
783
|
+
vertices = self.vertices[used_indices, :]
|
|
784
|
+
triangles = lookup[triangles]
|
|
785
|
+
|
|
773
786
|
return eqx.tree_at(
|
|
774
787
|
lambda m: (
|
|
775
788
|
m.vertices,
|
|
@@ -781,8 +794,8 @@ class TriangleMesh(eqx.Module):
|
|
|
781
794
|
),
|
|
782
795
|
self,
|
|
783
796
|
(
|
|
784
|
-
|
|
785
|
-
|
|
797
|
+
vertices,
|
|
798
|
+
triangles,
|
|
786
799
|
self.face_colors[self.mask, :]
|
|
787
800
|
if self.face_colors is not None
|
|
788
801
|
else None,
|
|
@@ -843,6 +856,59 @@ class TriangleMesh(eqx.Module):
|
|
|
843
856
|
self.vertices + jnp.asarray(translation),
|
|
844
857
|
)
|
|
845
858
|
|
|
859
|
+
def clip(
|
|
860
|
+
self,
|
|
861
|
+
x_min: Float[ArrayLike, ""] | None = None,
|
|
862
|
+
x_max: Float[ArrayLike, ""] | None = None,
|
|
863
|
+
y_min: Float[ArrayLike, ""] | None = None,
|
|
864
|
+
y_max: Float[ArrayLike, ""] | None = None,
|
|
865
|
+
z_min: Float[ArrayLike, ""] | None = None,
|
|
866
|
+
z_max: Float[ArrayLike, ""] | None = None,
|
|
867
|
+
) -> Self:
|
|
868
|
+
"""
|
|
869
|
+
Clip all vertex coordinates to the given bounds.
|
|
870
|
+
|
|
871
|
+
Args:
|
|
872
|
+
x_min: The minimum x coordinate.
|
|
873
|
+
x_max: The maximum x coordinate.
|
|
874
|
+
y_min: The minimum y coordinate.
|
|
875
|
+
y_max: The maximum y coordinate.
|
|
876
|
+
z_min: The minimum z coordinate.
|
|
877
|
+
z_max: The maximum z coordinate.
|
|
878
|
+
|
|
879
|
+
Returns:
|
|
880
|
+
A new mesh with clipped vertices.
|
|
881
|
+
|
|
882
|
+
Examples:
|
|
883
|
+
The following example shows the effect on a plotted mesh before and after clipping.
|
|
884
|
+
|
|
885
|
+
.. plotly::
|
|
886
|
+
:context: reset
|
|
887
|
+
|
|
888
|
+
>>> from differt.geometry import TriangleMesh
|
|
889
|
+
>>>
|
|
890
|
+
>>> mesh = TriangleMesh.box(length=4.0, width=2.0, height=1.0)
|
|
891
|
+
>>> fig = mesh.plot(backend="plotly")
|
|
892
|
+
>>> fig # doctest: +SKIP
|
|
893
|
+
|
|
894
|
+
.. plotly::
|
|
895
|
+
:context:
|
|
896
|
+
|
|
897
|
+
>>> clipped = mesh.clip(x_min=-1.0, x_max=1.0, y_min=-0.5, y_max=0.5)
|
|
898
|
+
>>> fig = clipped.plot(backend="plotly")
|
|
899
|
+
>>> fig # doctest: +SKIP
|
|
900
|
+
"""
|
|
901
|
+
vertices = self.vertices
|
|
902
|
+
|
|
903
|
+
if x_min is not None or x_max is not None:
|
|
904
|
+
vertices = vertices.at[:, 0].apply(lambda x: x.clip(min=x_min, max=x_max))
|
|
905
|
+
if y_min is not None or y_max is not None:
|
|
906
|
+
vertices = vertices.at[:, 1].apply(lambda y: y.clip(min=y_min, max=y_max))
|
|
907
|
+
if z_min is not None or z_max is not None:
|
|
908
|
+
vertices = vertices.at[:, 2].apply(lambda z: z.clip(min=z_min, max=z_max))
|
|
909
|
+
|
|
910
|
+
return eqx.tree_at(lambda m: m.vertices, self, vertices)
|
|
911
|
+
|
|
846
912
|
@classmethod
|
|
847
913
|
def empty(cls) -> Self:
|
|
848
914
|
"""
|
|
@@ -1724,8 +1790,8 @@ class TriangleMesh(eqx.Module):
|
|
|
1724
1790
|
if sample_objects:
|
|
1725
1791
|
indices = jnp.arange(self.num_triangles)
|
|
1726
1792
|
indices_inside_bounds = (
|
|
1727
|
-
self.object_bounds[:, 0] <= indices[:, None] # type: ignore[
|
|
1728
|
-
) & (indices[:, None] < self.object_bounds[:, 1]) # type: ignore[
|
|
1793
|
+
self.object_bounds[:, 0] <= indices[:, None] # type: ignore[ty:not-subscriptable]
|
|
1794
|
+
) & (indices[:, None] < self.object_bounds[:, 1]) # type: ignore[ty:not-subscriptable]
|
|
1729
1795
|
triangle_indices = indices_inside_bounds.argmax(
|
|
1730
1796
|
axis=1
|
|
1731
1797
|
) # Exactly one bound should be true for each triangle
|
|
@@ -1757,7 +1823,7 @@ class TriangleMesh(eqx.Module):
|
|
|
1757
1823
|
|
|
1758
1824
|
if sample_objects:
|
|
1759
1825
|
indices = jnp.concatenate([
|
|
1760
|
-
jnp.arange(self.object_bounds[i, 0], self.object_bounds[i, 1]) # type: ignore[
|
|
1826
|
+
jnp.arange(self.object_bounds[i, 0], self.object_bounds[i, 1]) # type: ignore[ty:not-subscriptable]
|
|
1761
1827
|
for i in indices
|
|
1762
1828
|
])
|
|
1763
1829
|
if self.assume_quads:
|
|
@@ -1892,3 +1958,404 @@ class TriangleMesh(eqx.Module):
|
|
|
1892
1958
|
if return_indices:
|
|
1893
1959
|
return mesh, indices
|
|
1894
1960
|
return mesh
|
|
1961
|
+
|
|
1962
|
+
def _keep_within(
|
|
1963
|
+
self,
|
|
1964
|
+
x_min: Float[ArrayLike, ""] | None = None,
|
|
1965
|
+
x_max: Float[ArrayLike, ""] | None = None,
|
|
1966
|
+
y_min: Float[ArrayLike, ""] | None = None,
|
|
1967
|
+
y_max: Float[ArrayLike, ""] | None = None,
|
|
1968
|
+
z_min: Float[ArrayLike, ""] | None = None,
|
|
1969
|
+
z_max: Float[ArrayLike, ""] | None = None,
|
|
1970
|
+
preserve_objects: bool = True,
|
|
1971
|
+
keep_any: bool = False,
|
|
1972
|
+
clip: bool = False,
|
|
1973
|
+
) -> Self:
|
|
1974
|
+
if preserve_objects and self.object_bounds is not None:
|
|
1975
|
+
warnings.warn(
|
|
1976
|
+
"Preserving objects is not fully supported yet, and some bugs may occur. Use with caution.",
|
|
1977
|
+
UserWarning,
|
|
1978
|
+
stacklevel=2,
|
|
1979
|
+
)
|
|
1980
|
+
|
|
1981
|
+
triangle_vertices = self.triangle_vertices
|
|
1982
|
+
xs, ys, zs = jnp.unstack(triangle_vertices, axis=-1)
|
|
1983
|
+
|
|
1984
|
+
vertex_mask = jnp.ones_like(xs, dtype=bool)
|
|
1985
|
+
if x_min is not None:
|
|
1986
|
+
vertex_mask &= xs >= x_min
|
|
1987
|
+
if x_max is not None:
|
|
1988
|
+
vertex_mask &= xs <= x_max
|
|
1989
|
+
if y_min is not None:
|
|
1990
|
+
vertex_mask &= ys >= y_min
|
|
1991
|
+
if y_max is not None:
|
|
1992
|
+
vertex_mask &= ys <= y_max
|
|
1993
|
+
if z_min is not None:
|
|
1994
|
+
vertex_mask &= zs >= z_min
|
|
1995
|
+
if z_max is not None:
|
|
1996
|
+
vertex_mask &= zs <= z_max
|
|
1997
|
+
|
|
1998
|
+
mask = (
|
|
1999
|
+
jnp.any(vertex_mask, axis=-1) if keep_any else jnp.all(vertex_mask, axis=-1)
|
|
2000
|
+
)
|
|
2001
|
+
|
|
2002
|
+
if self.mask is not None:
|
|
2003
|
+
mask &= self.mask
|
|
2004
|
+
|
|
2005
|
+
if preserve_objects and self.object_bounds is not None:
|
|
2006
|
+
object_ends = self.object_bounds[:, 1]
|
|
2007
|
+
object_ids = jnp.searchsorted(
|
|
2008
|
+
object_ends, jnp.arange(self.num_triangles), side="right"
|
|
2009
|
+
)
|
|
2010
|
+
object_counts = (
|
|
2011
|
+
jnp.zeros(self.object_bounds.shape[0], dtype=int).at[object_ids].add(1)
|
|
2012
|
+
)
|
|
2013
|
+
object_kept_counts = (
|
|
2014
|
+
jnp
|
|
2015
|
+
.zeros(self.object_bounds.shape[0], dtype=int)
|
|
2016
|
+
.at[object_ids]
|
|
2017
|
+
.add(mask.astype(int))
|
|
2018
|
+
)
|
|
2019
|
+
object_mask = (
|
|
2020
|
+
object_kept_counts > 0
|
|
2021
|
+
if keep_any
|
|
2022
|
+
else object_kept_counts == object_counts
|
|
2023
|
+
)
|
|
2024
|
+
mask = object_mask[object_ids]
|
|
2025
|
+
if self.mask is not None:
|
|
2026
|
+
mask &= self.mask
|
|
2027
|
+
|
|
2028
|
+
mesh = eqx.tree_at(lambda m: m.mask, self, mask, is_leaf=lambda x: x is None)
|
|
2029
|
+
if clip:
|
|
2030
|
+
mesh = mesh.clip(
|
|
2031
|
+
x_min=x_min,
|
|
2032
|
+
x_max=x_max,
|
|
2033
|
+
y_min=y_min,
|
|
2034
|
+
y_max=y_max,
|
|
2035
|
+
z_min=z_min,
|
|
2036
|
+
z_max=z_max,
|
|
2037
|
+
)
|
|
2038
|
+
return mesh
|
|
2039
|
+
|
|
2040
|
+
def keep_all_within(
|
|
2041
|
+
self,
|
|
2042
|
+
x_min: Float[ArrayLike, ""] | None = None,
|
|
2043
|
+
x_max: Float[ArrayLike, ""] | None = None,
|
|
2044
|
+
y_min: Float[ArrayLike, ""] | None = None,
|
|
2045
|
+
y_max: Float[ArrayLike, ""] | None = None,
|
|
2046
|
+
z_min: Float[ArrayLike, ""] | None = None,
|
|
2047
|
+
z_max: Float[ArrayLike, ""] | None = None,
|
|
2048
|
+
*,
|
|
2049
|
+
preserve_objects: bool = True,
|
|
2050
|
+
clip: bool = False,
|
|
2051
|
+
) -> Self:
|
|
2052
|
+
"""
|
|
2053
|
+
Return a new mesh, keeping only the triangles with all vertices within the given bounds.
|
|
2054
|
+
|
|
2055
|
+
If ``preserve_objects`` is set to :data:`True`, and :attr:`object_bounds` is not
|
|
2056
|
+
:data:`None`, then all triangles belonging to the same object as a triangle with all
|
|
2057
|
+
vertices within the given bounds are kept, but only if all triangles in that object
|
|
2058
|
+
satisfy the bounds.
|
|
2059
|
+
|
|
2060
|
+
Warning:
|
|
2061
|
+
Using setting ``preserve_objects`` to :data:`True` with this method may lead to unexpected results, as it is not fully supported yet. Use with caution.
|
|
2062
|
+
|
|
2063
|
+
Args:
|
|
2064
|
+
x_min: The minimum x coordinate.
|
|
2065
|
+
x_max: The maximum x coordinate.
|
|
2066
|
+
y_min: The minimum y coordinate.
|
|
2067
|
+
y_max: The maximum y coordinate.
|
|
2068
|
+
z_min: The minimum z coordinate.
|
|
2069
|
+
z_max: The maximum z coordinate.
|
|
2070
|
+
preserve_objects: Whether to preserve objects.
|
|
2071
|
+
clip: Whether to clip the vertices of the returned mesh to the given bounds.
|
|
2072
|
+
|
|
2073
|
+
Returns:
|
|
2074
|
+
A new mesh with the triangles filtered according to the given bounds.
|
|
2075
|
+
|
|
2076
|
+
:seealso::
|
|
2077
|
+
|
|
2078
|
+
:meth:`keep_any_within`
|
|
2079
|
+
|
|
2080
|
+
Examples:
|
|
2081
|
+
The following example shows how to filter the simple street canyon scene.
|
|
2082
|
+
|
|
2083
|
+
.. plotly::
|
|
2084
|
+
:context: reset
|
|
2085
|
+
|
|
2086
|
+
>>> from differt.scene import (
|
|
2087
|
+
... TriangleScene,
|
|
2088
|
+
... download_sionna_scenes,
|
|
2089
|
+
... get_sionna_scene,
|
|
2090
|
+
... )
|
|
2091
|
+
>>>
|
|
2092
|
+
>>> download_sionna_scenes() # doctest: +SKIP
|
|
2093
|
+
>>> file = get_sionna_scene("simple_street_canyon")
|
|
2094
|
+
>>> mesh = TriangleScene.load_xml(file).mesh
|
|
2095
|
+
>>> fig = mesh.plot(backend="plotly")
|
|
2096
|
+
>>> fig # doctest: +SKIP
|
|
2097
|
+
|
|
2098
|
+
Here, we keep the objects that have all vertices inside the selected range.
|
|
2099
|
+
|
|
2100
|
+
.. plotly::
|
|
2101
|
+
:context:
|
|
2102
|
+
|
|
2103
|
+
>>> fig = mesh.keep_all_within(y_min=-20.0).plot(
|
|
2104
|
+
... backend="plotly",
|
|
2105
|
+
... )
|
|
2106
|
+
>>> fig # doctest: +SKIP
|
|
2107
|
+
|
|
2108
|
+
By default, and if :attr:`object_bounds` is not :data:`None`, the filtering is done by objects. You can disable this behavior by setting ``preserve_objects`` to :data:`False`.
|
|
2109
|
+
|
|
2110
|
+
.. plotly::
|
|
2111
|
+
:context:
|
|
2112
|
+
|
|
2113
|
+
>>> fig = mesh.keep_all_within(
|
|
2114
|
+
... y_min=-20.0,
|
|
2115
|
+
... preserve_objects=False,
|
|
2116
|
+
... ).plot(backend="plotly")
|
|
2117
|
+
>>> fig # doctest: +SKIP
|
|
2118
|
+
"""
|
|
2119
|
+
return self._keep_within(
|
|
2120
|
+
x_min=x_min,
|
|
2121
|
+
x_max=x_max,
|
|
2122
|
+
y_min=y_min,
|
|
2123
|
+
y_max=y_max,
|
|
2124
|
+
z_min=z_min,
|
|
2125
|
+
z_max=z_max,
|
|
2126
|
+
preserve_objects=preserve_objects,
|
|
2127
|
+
keep_any=False,
|
|
2128
|
+
clip=clip,
|
|
2129
|
+
)
|
|
2130
|
+
|
|
2131
|
+
def keep_any_within(
|
|
2132
|
+
self,
|
|
2133
|
+
x_min: Float[ArrayLike, ""] | None = None,
|
|
2134
|
+
x_max: Float[ArrayLike, ""] | None = None,
|
|
2135
|
+
y_min: Float[ArrayLike, ""] | None = None,
|
|
2136
|
+
y_max: Float[ArrayLike, ""] | None = None,
|
|
2137
|
+
z_min: Float[ArrayLike, ""] | None = None,
|
|
2138
|
+
z_max: Float[ArrayLike, ""] | None = None,
|
|
2139
|
+
*,
|
|
2140
|
+
preserve_objects: bool = True,
|
|
2141
|
+
clip: bool = False,
|
|
2142
|
+
) -> Self:
|
|
2143
|
+
"""
|
|
2144
|
+
Return a new mesh, keeping only the triangles with at least one vertex within the given bounds.
|
|
2145
|
+
|
|
2146
|
+
If ``preserve_objects`` is set to :data:`True`, and :attr:`object_bounds` is not
|
|
2147
|
+
:data:`None`, then all triangles belonging to the same object as a triangle with at
|
|
2148
|
+
least one vertex within the given bounds are kept.
|
|
2149
|
+
|
|
2150
|
+
Warning:
|
|
2151
|
+
Using setting ``preserve_objects`` to :data:`True` with this method may lead to unexpected results, as it is not fully supported yet. Use with caution.
|
|
2152
|
+
|
|
2153
|
+
Args:
|
|
2154
|
+
x_min: The minimum x coordinate.
|
|
2155
|
+
x_max: The maximum x coordinate.
|
|
2156
|
+
y_min: The minimum y coordinate.
|
|
2157
|
+
y_max: The maximum y coordinate.
|
|
2158
|
+
z_min: The minimum z coordinate.
|
|
2159
|
+
z_max: The maximum z coordinate.
|
|
2160
|
+
preserve_objects: Whether to preserve objects.
|
|
2161
|
+
clip: Whether to clip the vertices of the returned mesh to the given bounds.
|
|
2162
|
+
|
|
2163
|
+
Returns:
|
|
2164
|
+
A new mesh with the triangles filtered according to the given bounds.
|
|
2165
|
+
|
|
2166
|
+
:seealso::
|
|
2167
|
+
|
|
2168
|
+
:meth:`keep_all_within`
|
|
2169
|
+
|
|
2170
|
+
Examples:
|
|
2171
|
+
The following example shows how to filter the simple street canyon scene.
|
|
2172
|
+
|
|
2173
|
+
.. plotly::
|
|
2174
|
+
:context: reset
|
|
2175
|
+
|
|
2176
|
+
>>> from differt.scene import (
|
|
2177
|
+
... TriangleScene,
|
|
2178
|
+
... download_sionna_scenes,
|
|
2179
|
+
... get_sionna_scene,
|
|
2180
|
+
... )
|
|
2181
|
+
>>>
|
|
2182
|
+
>>> download_sionna_scenes() # doctest: +SKIP
|
|
2183
|
+
>>> file = get_sionna_scene("simple_street_canyon")
|
|
2184
|
+
>>> mesh = TriangleScene.load_xml(file).mesh
|
|
2185
|
+
>>> fig = mesh.plot(backend="plotly")
|
|
2186
|
+
>>> fig # doctest: +SKIP
|
|
2187
|
+
|
|
2188
|
+
Here, we keep the objects that have all vertices inside the selected range.
|
|
2189
|
+
|
|
2190
|
+
.. plotly::
|
|
2191
|
+
:context:
|
|
2192
|
+
|
|
2193
|
+
>>> fig = mesh.keep_any_within(x_max=0.0).plot(
|
|
2194
|
+
... backend="plotly",
|
|
2195
|
+
... )
|
|
2196
|
+
>>> fig # doctest: +SKIP
|
|
2197
|
+
|
|
2198
|
+
By default, and if :attr:`object_bounds` is not :data:`None`, the filtering is done by objects. You can disable this behavior by setting ``preserve_objects`` to :data:`False`.
|
|
2199
|
+
|
|
2200
|
+
.. plotly::
|
|
2201
|
+
:context:
|
|
2202
|
+
|
|
2203
|
+
>>> fig = mesh.keep_any_within(
|
|
2204
|
+
... x_max=0.0,
|
|
2205
|
+
... preserve_objects=False,
|
|
2206
|
+
... ).plot(backend="plotly")
|
|
2207
|
+
>>> fig # doctest: +SKIP
|
|
2208
|
+
|
|
2209
|
+
Finally, we can also clip the vertices of the returned mesh to the given bounds. This is especially useful to trim the plane of the ground in outdoor scenes.
|
|
2210
|
+
|
|
2211
|
+
.. plotly::
|
|
2212
|
+
:context:
|
|
2213
|
+
|
|
2214
|
+
>>> fig = mesh.keep_any_within(
|
|
2215
|
+
... x_max=+15.0,
|
|
2216
|
+
... clip=True,
|
|
2217
|
+
... ).plot(backend="plotly")
|
|
2218
|
+
>>> fig # doctest: +SKIP
|
|
2219
|
+
"""
|
|
2220
|
+
return self._keep_within(
|
|
2221
|
+
x_min=x_min,
|
|
2222
|
+
x_max=x_max,
|
|
2223
|
+
y_min=y_min,
|
|
2224
|
+
y_max=y_max,
|
|
2225
|
+
z_min=z_min,
|
|
2226
|
+
z_max=z_max,
|
|
2227
|
+
preserve_objects=preserve_objects,
|
|
2228
|
+
keep_any=True,
|
|
2229
|
+
clip=clip,
|
|
2230
|
+
)
|
|
2231
|
+
|
|
2232
|
+
def center(self) -> Self:
|
|
2233
|
+
"""
|
|
2234
|
+
Return a new mesh, centered around the origin.
|
|
2235
|
+
|
|
2236
|
+
The center of the mesh is computed as the center of its bounding box,
|
|
2237
|
+
and the mesh is translated so that this center is at the origin in the (x, y) plane.
|
|
2238
|
+
The z-coordinate remains unchanged.
|
|
2239
|
+
|
|
2240
|
+
This method is useful for centering scenes after filtering operations that may
|
|
2241
|
+
have removed parts of the mesh (e.g., using :meth:`keep_all_within` or :meth:`keep_any_within`).
|
|
2242
|
+
|
|
2243
|
+
Returns:
|
|
2244
|
+
A new centered mesh with the same structure but with vertices translated.
|
|
2245
|
+
|
|
2246
|
+
Examples:
|
|
2247
|
+
The following example shows how to center a mesh around the origin.
|
|
2248
|
+
|
|
2249
|
+
.. plotly::
|
|
2250
|
+
:context: reset
|
|
2251
|
+
|
|
2252
|
+
>>> from differt.geometry import TriangleMesh
|
|
2253
|
+
>>>
|
|
2254
|
+
>>> mesh = TriangleMesh.box(
|
|
2255
|
+
... length=4.0, width=2.0, height=1.0
|
|
2256
|
+
... ).translate(jnp.array([2.0, 1.0, 0.0]))
|
|
2257
|
+
>>> fig = mesh.plot(backend="plotly")
|
|
2258
|
+
>>> fig # doctest: +SKIP
|
|
2259
|
+
|
|
2260
|
+
After centering, the mesh is translated to the origin in the (x, y) plane:
|
|
2261
|
+
|
|
2262
|
+
.. plotly::
|
|
2263
|
+
:context:
|
|
2264
|
+
|
|
2265
|
+
>>> centered = mesh.center()
|
|
2266
|
+
>>> fig = centered.plot(backend="plotly")
|
|
2267
|
+
>>> fig # doctest: +SKIP
|
|
2268
|
+
"""
|
|
2269
|
+
(x_min, y_min, _), (x_max, y_max, _) = self.bounding_box
|
|
2270
|
+
center = jnp.array([(x_min + x_max) * 0.5, (y_min + y_max) * 0.5, 0.0])
|
|
2271
|
+
return eqx.tree_at(lambda m: m.vertices, self, self.vertices - center)
|
|
2272
|
+
|
|
2273
|
+
def add_ground(
|
|
2274
|
+
self,
|
|
2275
|
+
x_scale: Float[ArrayLike, ""] = 1.0,
|
|
2276
|
+
y_scale: Float[ArrayLike, ""] = 1.0,
|
|
2277
|
+
z: Float[ArrayLike, ""] | None = None,
|
|
2278
|
+
material_name: str = "itu_concrete",
|
|
2279
|
+
face_color: Float[ArrayLike, "3"] = jnp.array([0.539, 0.539, 0.539]),
|
|
2280
|
+
) -> Self:
|
|
2281
|
+
"""
|
|
2282
|
+
Add a ground plane to this mesh.
|
|
2283
|
+
|
|
2284
|
+
The ground plane is a quadrilateral (represented as two triangles) added at the bottom of the mesh,
|
|
2285
|
+
i.e., below all existing triangles. The ground plane is scaled based on the bounding box of the mesh
|
|
2286
|
+
to create a realistic proportional ground.
|
|
2287
|
+
|
|
2288
|
+
This method is particularly useful for scenes that have been filtered or clipped (e.g., using
|
|
2289
|
+
:meth:`keep_all_within` or :meth:`keep_any_within`), as these operations may remove the original ground plane.
|
|
2290
|
+
|
|
2291
|
+
Args:
|
|
2292
|
+
x_scale: The scale factor of the ground plane along the x-axis. A value of 1.0 means the ground
|
|
2293
|
+
has the same width as the mesh's bounding box. Default is 1.0.
|
|
2294
|
+
y_scale: The scale factor of the ground plane along the y-axis. A value of 1.0 means the ground
|
|
2295
|
+
has the same depth as the mesh's bounding box. Default is 1.0.
|
|
2296
|
+
z: The z coordinate of the ground plane. If :data:`None`, the ground is placed at the minimum
|
|
2297
|
+
z coordinate of the mesh's bounding box. Default is :data:`None`.
|
|
2298
|
+
material_name: The name of the material for the ground plane. Default is ``"itu_concrete"``.
|
|
2299
|
+
face_color: The RGB color of the ground plane faces, with values in the range [0, 1].
|
|
2300
|
+
Default is ``[0.539, 0.539, 0.539]`` (concrete gray).
|
|
2301
|
+
|
|
2302
|
+
Returns:
|
|
2303
|
+
A new mesh with a ground plane appended.
|
|
2304
|
+
|
|
2305
|
+
Examples:
|
|
2306
|
+
The following example shows how to add a ground plane to a simple mesh.
|
|
2307
|
+
|
|
2308
|
+
.. plotly::
|
|
2309
|
+
:context: reset
|
|
2310
|
+
|
|
2311
|
+
>>> from differt.geometry import TriangleMesh
|
|
2312
|
+
>>>
|
|
2313
|
+
>>> mesh = TriangleMesh.box(length=2.0, width=2.0, height=1.0)
|
|
2314
|
+
>>> fig = mesh.plot(backend="plotly")
|
|
2315
|
+
>>> fig # doctest: +SKIP
|
|
2316
|
+
|
|
2317
|
+
After adding a ground plane, the mesh includes an additional quadrilateral at the bottom:
|
|
2318
|
+
|
|
2319
|
+
.. plotly::
|
|
2320
|
+
:context:
|
|
2321
|
+
|
|
2322
|
+
>>> with_ground = mesh.add_ground()
|
|
2323
|
+
>>> fig = with_ground.plot(backend="plotly")
|
|
2324
|
+
>>> fig # doctest: +SKIP
|
|
2325
|
+
|
|
2326
|
+
You can customize the ground plane size and appearance. For example, a larger ground plane with a different color:
|
|
2327
|
+
|
|
2328
|
+
.. plotly::
|
|
2329
|
+
:context:
|
|
2330
|
+
|
|
2331
|
+
>>> custom_ground = mesh.add_ground(
|
|
2332
|
+
... x_scale=2.0, y_scale=2.0, face_color=jnp.array([0.2, 0.2, 0.2])
|
|
2333
|
+
... )
|
|
2334
|
+
>>> fig = custom_ground.plot(backend="plotly")
|
|
2335
|
+
>>> fig # doctest: +SKIP
|
|
2336
|
+
"""
|
|
2337
|
+
(x_min, y_min, z_min), (x_max, y_max, _) = self.bounding_box
|
|
2338
|
+
if z is None:
|
|
2339
|
+
z = z_min
|
|
2340
|
+
|
|
2341
|
+
center = jnp.array([(x_min + x_max) * 0.5, (y_min + y_max) * 0.5, z])
|
|
2342
|
+
u = jnp.array([(x_max - x_min) * x_scale * 0.5, 0.0, 0.0])
|
|
2343
|
+
v = jnp.array([0.0, (y_max - y_min) * y_scale * 0.5, 0.0])
|
|
2344
|
+
vertices = jnp.array([
|
|
2345
|
+
center + u + v,
|
|
2346
|
+
center - u + v,
|
|
2347
|
+
center - u - v,
|
|
2348
|
+
center + u - v,
|
|
2349
|
+
])
|
|
2350
|
+
triangles = jnp.array([[0, 1, 2], [0, 2, 3]], dtype=int)
|
|
2351
|
+
|
|
2352
|
+
ground = TriangleMesh(
|
|
2353
|
+
vertices=vertices,
|
|
2354
|
+
triangles=triangles,
|
|
2355
|
+
object_bounds=jnp.array([[0, 2]]),
|
|
2356
|
+
face_colors=jnp.array([face_color, face_color]),
|
|
2357
|
+
face_materials=jnp.array([0, 0]),
|
|
2358
|
+
material_names=(material_name,),
|
|
2359
|
+
assume_quads=True,
|
|
2360
|
+
)
|
|
2361
|
+
return self.append(ground)
|
|
@@ -35,7 +35,7 @@ def draw_mesh(
|
|
|
35
35
|
vertices: Real[ArrayLike, "num_vertices 3"],
|
|
36
36
|
triangles: Int[ArrayLike, "num_triangles 3"],
|
|
37
37
|
**kwargs: Any,
|
|
38
|
-
) -> Canvas | MplFigure | Figure:
|
|
38
|
+
) -> Canvas | MplFigure | Figure:
|
|
39
39
|
"""
|
|
40
40
|
Plot a 3D mesh made of triangles.
|
|
41
41
|
|
|
@@ -86,6 +86,7 @@ def draw_mesh(
|
|
|
86
86
|
>>> fig # doctest: +SKIP
|
|
87
87
|
|
|
88
88
|
"""
|
|
89
|
+
raise NotImplementedError
|
|
89
90
|
|
|
90
91
|
|
|
91
92
|
@draw_mesh.register("vispy")
|
|
@@ -98,14 +99,14 @@ def _(
|
|
|
98
99
|
|
|
99
100
|
canvas, view = process_vispy_kwargs(kwargs)
|
|
100
101
|
|
|
101
|
-
if vertices.size == 0 or triangles.size == 0: # type: ignore[reportAttributeAccessIssue] # pragma: no cover
|
|
102
|
-
return canvas
|
|
103
|
-
|
|
104
102
|
kwargs.setdefault("shading", "flat")
|
|
105
103
|
|
|
106
104
|
vertices = np.asarray(vertices)
|
|
107
105
|
triangles = np.asarray(triangles)
|
|
108
106
|
|
|
107
|
+
if vertices.size == 0 or triangles.size == 0:
|
|
108
|
+
return canvas
|
|
109
|
+
|
|
109
110
|
view.add(Mesh(vertices=vertices, faces=triangles, **kwargs))
|
|
110
111
|
view.camera.set_range()
|
|
111
112
|
|
|
@@ -137,6 +138,19 @@ def _(
|
|
|
137
138
|
) -> Figure:
|
|
138
139
|
fig = process_plotly_kwargs(kwargs)
|
|
139
140
|
kwargs.setdefault("flatshading", True)
|
|
141
|
+
kwargs.setdefault(
|
|
142
|
+
"lighting",
|
|
143
|
+
{
|
|
144
|
+
"ambient": 0.5,
|
|
145
|
+
"diffuse": 0.9,
|
|
146
|
+
"fresnel": 0.1,
|
|
147
|
+
"specular": 0.9,
|
|
148
|
+
"roughness": 0.5,
|
|
149
|
+
"facenormalsepsilon": 1e-15,
|
|
150
|
+
"vertexnormalsepsilon": 1e-15,
|
|
151
|
+
},
|
|
152
|
+
)
|
|
153
|
+
kwargs.setdefault("lightposition", {"x": 100000, "y": 100000, "z": 100000})
|
|
140
154
|
|
|
141
155
|
if (
|
|
142
156
|
face_colors := kwargs.pop("face_colors", None)
|
|
@@ -153,7 +167,7 @@ def _(
|
|
|
153
167
|
def draw_paths(
|
|
154
168
|
paths: Real[ArrayLike, "*batch path_length 3"],
|
|
155
169
|
**kwargs: Any,
|
|
156
|
-
) -> Canvas | MplFigure | Figure:
|
|
170
|
+
) -> Canvas | MplFigure | Figure:
|
|
157
171
|
"""
|
|
158
172
|
Plot a batch of paths of the same length.
|
|
159
173
|
|
|
@@ -195,6 +209,7 @@ def draw_paths(
|
|
|
195
209
|
... )
|
|
196
210
|
>>> fig # doctest: +SKIP
|
|
197
211
|
"""
|
|
212
|
+
raise NotImplementedError
|
|
198
213
|
|
|
199
214
|
|
|
200
215
|
@draw_paths.register("vispy")
|
|
@@ -263,7 +278,7 @@ def draw_rays(
|
|
|
263
278
|
*,
|
|
264
279
|
ratio: Float[ArrayLike, ""] = 0.1,
|
|
265
280
|
**kwargs: Any,
|
|
266
|
-
) -> Canvas | MplFigure | Figure:
|
|
281
|
+
) -> Canvas | MplFigure | Figure:
|
|
267
282
|
"""
|
|
268
283
|
Plot a batch of rays.
|
|
269
284
|
|
|
@@ -345,6 +360,7 @@ def draw_rays(
|
|
|
345
360
|
... )
|
|
346
361
|
>>> fig # doctest: +SKIP
|
|
347
362
|
"""
|
|
363
|
+
raise NotImplementedError
|
|
348
364
|
|
|
349
365
|
|
|
350
366
|
@draw_rays.register("vispy")
|
|
@@ -359,14 +375,14 @@ def _(
|
|
|
359
375
|
|
|
360
376
|
canvas, view = process_vispy_kwargs(kwargs)
|
|
361
377
|
|
|
362
|
-
if ray_origins.size == 0 or ray_directions.size == 0: # type: ignore[reportAttributeAccessIssue] # pragma: no cover
|
|
363
|
-
return canvas
|
|
364
|
-
|
|
365
378
|
kwargs.setdefault("width", 3.0)
|
|
366
379
|
kwargs.setdefault("arrow_size", 6.0)
|
|
367
380
|
|
|
368
381
|
ray_origins = np.asarray(ray_origins).reshape(-1, 3)
|
|
369
382
|
ray_directions = np.asarray(ray_directions).reshape(-1, 3)
|
|
383
|
+
|
|
384
|
+
if ray_origins.size == 0 or ray_directions.size == 0:
|
|
385
|
+
return canvas
|
|
370
386
|
ratio = np.asarray(ratio)
|
|
371
387
|
body_ends = ray_origins + (1 - ratio) * ray_directions
|
|
372
388
|
|
|
@@ -394,7 +410,7 @@ def _(
|
|
|
394
410
|
) -> MplFigure:
|
|
395
411
|
fig, ax = process_matplotlib_kwargs(kwargs)
|
|
396
412
|
|
|
397
|
-
kwargs.setdefault("arrow_length_ratio", float(ratio))
|
|
413
|
+
kwargs.setdefault("arrow_length_ratio", float(np.asarray(ratio)))
|
|
398
414
|
|
|
399
415
|
ray_origins = np.asarray(ray_origins).reshape(-1, 3)
|
|
400
416
|
ray_directions = np.asarray(ray_directions).reshape(-1, 3)
|
|
@@ -467,7 +483,7 @@ def draw_markers(
|
|
|
467
483
|
labels: Sequence[str] | None = None,
|
|
468
484
|
text_kwargs: Mapping[str, Any] | None = None,
|
|
469
485
|
**kwargs: Any,
|
|
470
|
-
) -> Canvas | MplFigure | Figure:
|
|
486
|
+
) -> Canvas | MplFigure | Figure:
|
|
471
487
|
"""
|
|
472
488
|
Plot markers and, optionally, their label.
|
|
473
489
|
|
|
@@ -508,6 +524,7 @@ def draw_markers(
|
|
|
508
524
|
>>> fig = draw_markers(markers, labels, backend="plotly")
|
|
509
525
|
>>> fig # doctest: +SKIP
|
|
510
526
|
"""
|
|
527
|
+
raise NotImplementedError
|
|
511
528
|
|
|
512
529
|
|
|
513
530
|
@draw_markers.register("vispy")
|
|
@@ -521,13 +538,16 @@ def _(
|
|
|
521
538
|
|
|
522
539
|
canvas, view = process_vispy_kwargs(kwargs)
|
|
523
540
|
|
|
524
|
-
if markers.size == 0: # type: ignore[reportAttributeAccessIssue] # pragma: no cover
|
|
525
|
-
return canvas
|
|
526
|
-
|
|
527
541
|
kwargs.setdefault("size", 1)
|
|
528
542
|
kwargs.setdefault("edge_width_rel", 0.05)
|
|
529
543
|
kwargs.setdefault("scaling", "scene")
|
|
530
|
-
markers = np.asarray(markers)
|
|
544
|
+
markers = np.asarray(markers)
|
|
545
|
+
|
|
546
|
+
if markers.size == 0:
|
|
547
|
+
return canvas
|
|
548
|
+
|
|
549
|
+
markers = markers.reshape(-1, 3)
|
|
550
|
+
|
|
531
551
|
view.add(Markers(pos=markers, **kwargs))
|
|
532
552
|
|
|
533
553
|
if labels:
|
|
@@ -600,7 +620,7 @@ def draw_image(
|
|
|
600
620
|
y: Real[ArrayLike, " rows"] | Real[ArrayLike, "rows cols 3"] | None = None,
|
|
601
621
|
z0: float = 0.0,
|
|
602
622
|
**kwargs: Any,
|
|
603
|
-
) -> Canvas | MplFigure | Figure:
|
|
623
|
+
) -> Canvas | MplFigure | Figure:
|
|
604
624
|
"""
|
|
605
625
|
Plot a 2D image on a 3D canvas, at a fixed z-coordinate.
|
|
606
626
|
|
|
@@ -651,6 +671,7 @@ def draw_image(
|
|
|
651
671
|
>>> fig2 # doctest: +SKIP
|
|
652
672
|
|
|
653
673
|
"""
|
|
674
|
+
raise NotImplementedError
|
|
654
675
|
|
|
655
676
|
|
|
656
677
|
@draw_image.register("vispy")
|
|
@@ -759,7 +780,7 @@ def draw_contour(
|
|
|
759
780
|
levels: int | Real[ArrayLike, " num_levels"] | None = None,
|
|
760
781
|
fill: bool = False,
|
|
761
782
|
**kwargs: Any,
|
|
762
|
-
) -> Canvas | MplFigure | Figure:
|
|
783
|
+
) -> Canvas | MplFigure | Figure:
|
|
763
784
|
"""
|
|
764
785
|
Plot a 2D contour on a 3D canvas, at a fixed z-coordinate.
|
|
765
786
|
|
|
@@ -810,6 +831,7 @@ def draw_contour(
|
|
|
810
831
|
>>> fig2 # doctest: +SKIP
|
|
811
832
|
|
|
812
833
|
"""
|
|
834
|
+
raise NotImplementedError
|
|
813
835
|
|
|
814
836
|
|
|
815
837
|
@draw_contour.register("vispy")
|
|
@@ -896,7 +918,7 @@ def _(
|
|
|
896
918
|
|
|
897
919
|
y = np.arange(m) if y is None else np.asarray(y)
|
|
898
920
|
|
|
899
|
-
if not
|
|
921
|
+
if levels is not None and not np.isscalar(levels):
|
|
900
922
|
levels = np.asarray(levels)
|
|
901
923
|
|
|
902
924
|
if fill:
|
|
@@ -927,7 +949,7 @@ def _(
|
|
|
927
949
|
if isinstance(levels, int):
|
|
928
950
|
kwargs.setdefault("autocontour", True)
|
|
929
951
|
kwargs.setdefault("ncontours", levels)
|
|
930
|
-
elif
|
|
952
|
+
elif levels is not None and not np.isscalar(levels):
|
|
931
953
|
levels = np.asarray(levels)
|
|
932
954
|
msg = (
|
|
933
955
|
"Plotly does not support arbitrary level values, but only linearly spaced levels. "
|
|
@@ -960,7 +982,7 @@ def draw_surface(
|
|
|
960
982
|
z: Real[ArrayLike, "rows cols"],
|
|
961
983
|
colors: Real[ArrayLike, "rows cols"] | Real[ArrayLike, "rows cols 3"] | None = None,
|
|
962
984
|
**kwargs: Any,
|
|
963
|
-
) -> Canvas | MplFigure | Figure:
|
|
985
|
+
) -> Canvas | MplFigure | Figure:
|
|
964
986
|
"""
|
|
965
987
|
Plot a 3D surface.
|
|
966
988
|
|
|
@@ -1010,6 +1032,7 @@ def draw_surface(
|
|
|
1010
1032
|
>>> fig2 # doctest: +SKIP
|
|
1011
1033
|
|
|
1012
1034
|
"""
|
|
1035
|
+
raise NotImplementedError
|
|
1013
1036
|
|
|
1014
1037
|
|
|
1015
1038
|
@draw_surface.register("vispy")
|
|
@@ -14,6 +14,7 @@ from typing import (
|
|
|
14
14
|
ParamSpec,
|
|
15
15
|
Protocol,
|
|
16
16
|
TypeVar,
|
|
17
|
+
cast,
|
|
17
18
|
get_args,
|
|
18
19
|
no_type_check,
|
|
19
20
|
runtime_checkable,
|
|
@@ -76,7 +77,7 @@ config = _Config()
|
|
|
76
77
|
"""Mutable config object with mutability of default values guarded by a lock."""
|
|
77
78
|
|
|
78
79
|
|
|
79
|
-
def get_backend(backend:
|
|
80
|
+
def get_backend(backend: str | None = None) -> BackendName:
|
|
80
81
|
"""
|
|
81
82
|
Return the name of the backend to use.
|
|
82
83
|
|
|
@@ -113,10 +114,11 @@ def get_backend(backend: LiteralString | None = None) -> BackendName:
|
|
|
113
114
|
if importlib.util.find_spec(backend) is None:
|
|
114
115
|
msg = f"Could not find backend '{backend}', did you install it?"
|
|
115
116
|
raise ImportError(msg)
|
|
116
|
-
|
|
117
|
+
|
|
118
|
+
return cast("BackendName", backend)
|
|
117
119
|
|
|
118
120
|
|
|
119
|
-
def set_defaults(backend:
|
|
121
|
+
def set_defaults(backend: str | None = None, **kwargs: Any) -> BackendName:
|
|
120
122
|
"""
|
|
121
123
|
Set default backend and keyword arguments for future plotting utilities.
|
|
122
124
|
|
|
@@ -184,7 +186,7 @@ def set_defaults(backend: LiteralString | None = None, **kwargs: Any) -> Backend
|
|
|
184
186
|
return backend
|
|
185
187
|
|
|
186
188
|
|
|
187
|
-
def set_backend(backend:
|
|
189
|
+
def set_backend(backend: str) -> None:
|
|
188
190
|
"""
|
|
189
191
|
Set default backend for future plotting utilities.
|
|
190
192
|
|
|
@@ -207,7 +209,7 @@ def set_backend(backend: LiteralString) -> None:
|
|
|
207
209
|
set_defaults(backend=backend)
|
|
208
210
|
|
|
209
211
|
|
|
210
|
-
def update_defaults(backend:
|
|
212
|
+
def update_defaults(backend: str | None = None, **kwargs: Any) -> BackendName:
|
|
211
213
|
"""
|
|
212
214
|
Update default backend and keyword arguments for future plotting utilities.
|
|
213
215
|
|
|
@@ -232,7 +234,7 @@ def update_defaults(backend: LiteralString | None = None, **kwargs: Any) -> Back
|
|
|
232
234
|
|
|
233
235
|
|
|
234
236
|
@contextmanager
|
|
235
|
-
def use(backend:
|
|
237
|
+
def use(backend: str | None = None, **kwargs: Any) -> Iterator[BackendName]:
|
|
236
238
|
"""
|
|
237
239
|
Create a context manager that updates plotting defaults and returns the current default backend.
|
|
238
240
|
|
|
@@ -302,11 +304,11 @@ class _Dispatcher(Protocol[P, T]):
|
|
|
302
304
|
) -> T: ...
|
|
303
305
|
def register(
|
|
304
306
|
self,
|
|
305
|
-
backend:
|
|
307
|
+
backend: str,
|
|
306
308
|
) -> Callable[[Callable[P, T]], Callable[P, T]]: ...
|
|
307
309
|
|
|
308
310
|
|
|
309
|
-
def dispatch(fun: Callable[P,
|
|
311
|
+
def dispatch(fun: Callable[P, T]) -> _Dispatcher[P, T]:
|
|
310
312
|
"""
|
|
311
313
|
Transform a function into a backend dispatcher for plot functions.
|
|
312
314
|
|
|
@@ -373,10 +375,10 @@ def dispatch(fun: Callable[P, PlotOutput]) -> _Dispatcher[P, T]:
|
|
|
373
375
|
Traceback (most recent call last):
|
|
374
376
|
ValueError: Unsupported backend 'numpy', allowed values are: ...
|
|
375
377
|
"""
|
|
376
|
-
registry: dict[str, Callable[P,
|
|
378
|
+
registry: dict[str, Callable[P, T]] = {}
|
|
377
379
|
|
|
378
380
|
def register(
|
|
379
|
-
backend:
|
|
381
|
+
backend: str,
|
|
380
382
|
) -> Callable[[Callable[P, T]], Callable[P, T]]:
|
|
381
383
|
"""
|
|
382
384
|
Return a wrapper that will call the decorated function for the specified backend.
|
|
@@ -431,37 +433,26 @@ def dispatch(fun: Callable[P, PlotOutput]) -> _Dispatcher[P, T]:
|
|
|
431
433
|
*args: P.args,
|
|
432
434
|
**kwargs: P.kwargs,
|
|
433
435
|
) -> T:
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
Returns:
|
|
442
|
-
The result of the call.
|
|
443
|
-
"""
|
|
444
|
-
# We cannot currently add keyword argument to the signature,
|
|
445
|
-
# at least Pyright will not allow that,
|
|
446
|
-
# see: https://github.com/microsoft/pyright/issues/5844.
|
|
447
|
-
#
|
|
448
|
-
# The motivation is detailed in P612:
|
|
449
|
-
# https://peps.python.org/pep-0612/#concatenating-keyword-parameters.
|
|
450
|
-
backend_opt: LiteralString | None = kwargs.pop("backend", None) # type: ignore[reportArgumentType]
|
|
436
|
+
# We cannot add keyword argument to the signature, because ParamSpec does not
|
|
437
|
+
# allow parameters between
|
|
438
|
+
# *args: P.args and **kwargs: P.kwargs.
|
|
439
|
+
backend_opt = kwargs.pop("backend", None)
|
|
440
|
+
if backend_opt is not None and not isinstance(backend_opt, str):
|
|
441
|
+
msg = "Argument 'backend' must be a string."
|
|
442
|
+
raise TypeError(msg)
|
|
451
443
|
backend = get_backend(backend_opt)
|
|
452
444
|
|
|
453
445
|
try:
|
|
454
|
-
return registry[backend](*args, **kwargs)
|
|
446
|
+
return registry[backend](*args, **kwargs)
|
|
455
447
|
except KeyError:
|
|
456
448
|
msg = f"No backend implementation for '{backend}'"
|
|
457
449
|
raise NotImplementedError(
|
|
458
450
|
msg,
|
|
459
451
|
) from None
|
|
460
452
|
|
|
461
|
-
wrapper.register = register # type: ignore[
|
|
462
|
-
wrapper.registry = types.MappingProxyType(registry) # type: ignore[
|
|
463
|
-
|
|
464
|
-
return wrapper # type: ignore[reportReportType]
|
|
453
|
+
wrapper.register = register # type: ignore[ty:unresolved-attribute]
|
|
454
|
+
wrapper.registry = types.MappingProxyType(registry) # type: ignore[ty:unresolved-attribute]
|
|
455
|
+
return wrapper # type: ignore[ty:invalid-return-type]
|
|
465
456
|
|
|
466
457
|
|
|
467
458
|
def view_from_canvas(canvas: Canvas) -> ViewBox:
|
|
@@ -4,7 +4,7 @@ __all__ = ("DeepMIMO", "export")
|
|
|
4
4
|
|
|
5
5
|
from collections.abc import Iterable, Iterator, Mapping
|
|
6
6
|
from dataclasses import KW_ONLY, asdict
|
|
7
|
-
from typing import Any, Generic, Literal, TypeGuard
|
|
7
|
+
from typing import TYPE_CHECKING, Any, Generic, Literal, TypeGuard
|
|
8
8
|
|
|
9
9
|
import equinox as eqx
|
|
10
10
|
import jax
|
|
@@ -33,6 +33,16 @@ from differt.utils import safe_divide
|
|
|
33
33
|
|
|
34
34
|
from ._deepmimo_types import ArrayType
|
|
35
35
|
|
|
36
|
+
if TYPE_CHECKING:
|
|
37
|
+
try:
|
|
38
|
+
import sionna.rt
|
|
39
|
+
except ImportError:
|
|
40
|
+
SionnaPaths = Any
|
|
41
|
+
else:
|
|
42
|
+
SionnaPaths = sionna.rt.Paths
|
|
43
|
+
else:
|
|
44
|
+
SionnaPaths = Any
|
|
45
|
+
|
|
36
46
|
|
|
37
47
|
def _pad_and_concat(
|
|
38
48
|
left: Shaped[Array, "num_tx num_rx num_paths_left num_interactions_left ..."],
|
|
@@ -146,7 +156,7 @@ class DeepMIMO(eqx.Module, Generic[ArrayType]):
|
|
|
146
156
|
|
|
147
157
|
def _sort(
|
|
148
158
|
self,
|
|
149
|
-
paths:
|
|
159
|
+
paths: SionnaPaths,
|
|
150
160
|
) -> "DeepMIMO[Array]":
|
|
151
161
|
"""Utility function to sort the DeepMIMO based on :class:`sionna.rt.Paths`' vertices.""" # noqa: DOC201, DOC501
|
|
152
162
|
if _is_jax_dtype(self):
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
from typing import Any
|
|
1
|
+
from typing import Any, no_type_check
|
|
2
2
|
|
|
3
3
|
import equinox as eqx
|
|
4
4
|
import fpt_jax
|
|
@@ -169,17 +169,21 @@ def fermat_path_on_linear_objects(
|
|
|
169
169
|
return jnp.broadcast_to(object_origins, (*batch, num_objects, 3)).astype(dtype)
|
|
170
170
|
|
|
171
171
|
# Needed until https://github.com/jax-ml/jax/issues/34697 is resolved
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
172
|
+
@no_type_check
|
|
173
|
+
def _call_fpt_jax() -> Array:
|
|
174
|
+
return fpt_jax.trace_rays(
|
|
175
|
+
from_vertices,
|
|
176
|
+
to_vertices,
|
|
177
|
+
object_origins,
|
|
178
|
+
object_vectors,
|
|
179
|
+
num_iters=steps,
|
|
180
|
+
unroll=unroll,
|
|
181
|
+
num_iters_linesearch=linesearch_steps,
|
|
182
|
+
unroll_linesearch=unroll_linesearch,
|
|
183
|
+
implicit_diff=implicit_diff,
|
|
184
|
+
)
|
|
185
|
+
|
|
186
|
+
return _call_fpt_jax()
|
|
183
187
|
|
|
184
188
|
|
|
185
189
|
@eqx.filter_jit
|
|
@@ -192,14 +192,12 @@ def _image_method(
|
|
|
192
192
|
_forward,
|
|
193
193
|
init=from_vertex,
|
|
194
194
|
xs=(mirror_vertices, mirror_normals),
|
|
195
|
-
unroll=True,
|
|
196
195
|
)
|
|
197
196
|
_, paths = jax.lax.scan(
|
|
198
197
|
_backward,
|
|
199
198
|
init=to_vertex,
|
|
200
199
|
xs=(mirror_vertices, mirror_normals, images),
|
|
201
200
|
reverse=True,
|
|
202
|
-
unroll=True,
|
|
203
201
|
)
|
|
204
202
|
|
|
205
203
|
return paths
|
|
@@ -2,7 +2,7 @@ import math
|
|
|
2
2
|
import typing
|
|
3
3
|
import warnings
|
|
4
4
|
from collections.abc import Iterator, Mapping
|
|
5
|
-
from typing import TYPE_CHECKING, Any, Literal, overload
|
|
5
|
+
from typing import TYPE_CHECKING, Any, Literal, cast, overload
|
|
6
6
|
|
|
7
7
|
import equinox as eqx
|
|
8
8
|
import jax
|
|
@@ -1270,15 +1270,18 @@ class TriangleScene(eqx.Module):
|
|
|
1270
1270
|
to = from_ + 1
|
|
1271
1271
|
|
|
1272
1272
|
if chunk_size:
|
|
1273
|
+
# Type checker not able to infer that order is not None in this branch, even with the previous checks, so we need to cast it
|
|
1274
|
+
order = cast("int", order)
|
|
1275
|
+
|
|
1273
1276
|
path_candidates_iter = graph.all_paths_array_chunks(
|
|
1274
1277
|
from_=from_,
|
|
1275
1278
|
to=to,
|
|
1276
|
-
depth=order + 2,
|
|
1279
|
+
depth=order + 2,
|
|
1277
1280
|
include_from_and_to=False,
|
|
1278
1281
|
chunk_size=chunk_size,
|
|
1279
1282
|
)
|
|
1280
1283
|
it = (
|
|
1281
|
-
_compute_paths( # type: ignore[no-matching-overload]
|
|
1284
|
+
_compute_paths( # type: ignore[ty:no-matching-overload]
|
|
1282
1285
|
self.mesh,
|
|
1283
1286
|
tx_vertices,
|
|
1284
1287
|
rx_vertices,
|
|
@@ -1301,11 +1304,13 @@ class TriangleScene(eqx.Module):
|
|
|
1301
1304
|
return it
|
|
1302
1305
|
|
|
1303
1306
|
if path_candidates is None:
|
|
1307
|
+
# Type checker not able to infer that order is not None in this branch, even with the previous checks, so we need to cast it
|
|
1308
|
+
order = cast("int", order)
|
|
1304
1309
|
path_candidates = jnp.asarray(
|
|
1305
1310
|
graph.all_paths_array(
|
|
1306
1311
|
from_=from_,
|
|
1307
1312
|
to=to,
|
|
1308
|
-
depth=order + 2, #
|
|
1313
|
+
depth=order + 2, # order not narrowed in this branch
|
|
1309
1314
|
include_from_and_to=False,
|
|
1310
1315
|
),
|
|
1311
1316
|
dtype=int,
|
|
@@ -1318,7 +1323,7 @@ class TriangleScene(eqx.Module):
|
|
|
1318
1323
|
if self.mesh.assume_quads:
|
|
1319
1324
|
path_candidates -= path_candidates % 2
|
|
1320
1325
|
|
|
1321
|
-
return _compute_paths( # type: ignore[no-matching-overload]
|
|
1326
|
+
return _compute_paths( # type: ignore[ty:no-matching-overload]
|
|
1322
1327
|
self.mesh,
|
|
1323
1328
|
tx_vertices,
|
|
1324
1329
|
rx_vertices,
|
|
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
|