differt 0.0.19__tar.gz → 0.0.21__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.0.19 → differt-0.0.21}/PKG-INFO +9 -4
- {differt-0.0.19 → differt-0.0.21}/pyproject.toml +1 -1
- {differt-0.0.19 → differt-0.0.21}/src/differt/geometry/paths.py +1 -1
- {differt-0.0.19 → differt-0.0.21}/src/differt/geometry/triangle_mesh.py +32 -4
- {differt-0.0.19 → differt-0.0.21}/src/differt/plotting/_core.py +6 -2
- {differt-0.0.19 → differt-0.0.21}/src/differt/plotting/_utils.py +9 -3
- {differt-0.0.19 → differt-0.0.21}/src/differt/rt/image_method.py +5 -2
- {differt-0.0.19 → differt-0.0.21}/src/differt/rt/utils.py +19 -9
- {differt-0.0.19 → differt-0.0.21}/src/differt/scene/triangle_scene.py +226 -68
- {differt-0.0.19 → differt-0.0.21}/src/differt/utils.py +17 -12
- {differt-0.0.19 → differt-0.0.21}/.gitignore +0 -0
- {differt-0.0.19 → differt-0.0.21}/LICENSE.md +0 -0
- {differt-0.0.19 → differt-0.0.21}/README.md +0 -0
- {differt-0.0.19 → differt-0.0.21}/src/differt/__init__.py +0 -0
- {differt-0.0.19 → differt-0.0.21}/src/differt/em/__init__.py +0 -0
- {differt-0.0.19 → differt-0.0.21}/src/differt/em/constants.py +0 -0
- {differt-0.0.19 → differt-0.0.21}/src/differt/em/special.py +0 -0
- {differt-0.0.19 → differt-0.0.21}/src/differt/em/utd.py +0 -0
- {differt-0.0.19 → differt-0.0.21}/src/differt/em/utils.py +0 -0
- {differt-0.0.19 → differt-0.0.21}/src/differt/geometry/__init__.py +0 -0
- {differt-0.0.19 → differt-0.0.21}/src/differt/geometry/utils.py +0 -0
- {differt-0.0.19 → differt-0.0.21}/src/differt/plotting/__init__.py +0 -0
- {differt-0.0.19 → differt-0.0.21}/src/differt/rt/__init__.py +0 -0
- {differt-0.0.19 → differt-0.0.21}/src/differt/rt/fermat.py +0 -0
- {differt-0.0.19 → differt-0.0.21}/src/differt/scene/__init__.py +0 -0
- {differt-0.0.19 → differt-0.0.21}/src/differt/scene/sionna.py +0 -0
- {differt-0.0.19 → differt-0.0.21}/src/differt/version.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.3
|
|
2
2
|
Name: DiffeRT
|
|
3
|
-
Version: 0.0.
|
|
3
|
+
Version: 0.0.21
|
|
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,7 +21,7 @@ Classifier: Topic :: Scientific/Engineering
|
|
|
21
21
|
Requires-Python: >=3.10
|
|
22
22
|
Requires-Dist: beartype>=0.17.2
|
|
23
23
|
Requires-Dist: chex>=0.1.86
|
|
24
|
-
Requires-Dist: differt-core==0.0.
|
|
24
|
+
Requires-Dist: differt-core==0.0.21
|
|
25
25
|
Requires-Dist: equinox>=0.11.5
|
|
26
26
|
Requires-Dist: filelock>=3.15.4
|
|
27
27
|
Requires-Dist: jax>=0.4.32
|
|
@@ -180,12 +180,17 @@ or benchmark it:
|
|
|
180
180
|
pytest --benchmark-enable differt/tests/benchmarks
|
|
181
181
|
```
|
|
182
182
|
|
|
183
|
+
## Citing
|
|
184
|
+
|
|
185
|
+
If you use this software, please cite it as
|
|
186
|
+
[detailed here](https://differt.readthedocs.io/latest/citing.html).
|
|
187
|
+
|
|
183
188
|
[pypi-version-badge]: https://img.shields.io/pypi/v/DiffeRT?label=DiffeRT&color=blueviolet
|
|
184
189
|
[pypi-version-url]: https://pypi.org/project/DiffeRT/
|
|
185
190
|
[pypi-python-version-badge]: https://img.shields.io/pypi/pyversions/DiffeRT?color=orange
|
|
186
191
|
[documentation-badge]: https://readthedocs.org/projects/differt/badge/?version=latest
|
|
187
192
|
[documentation-url]: https://differt.readthedocs.io/latest/?badge=latest
|
|
188
|
-
[doi-badge]: https://zenodo.org/badge/DOI/10.5281/zenodo.
|
|
189
|
-
[doi-url]: https://doi.org/10.5281/zenodo.
|
|
193
|
+
[doi-badge]: https://zenodo.org/badge/DOI/10.5281/zenodo.13895021.svg
|
|
194
|
+
[doi-url]: https://doi.org/10.5281/zenodo.13895021
|
|
190
195
|
[codecov-badge]: https://codecov.io/gh/jeertmans/DiffeRT/branch/main/graph/badge.svg?token=v63alnTWzu
|
|
191
196
|
[codecov-url]: https://codecov.io/gh/jeertmans/DiffeRT
|
|
@@ -251,7 +251,7 @@ class Paths(eqx.Module):
|
|
|
251
251
|
|
|
252
252
|
>>> from differt.geometry.paths import Paths
|
|
253
253
|
>>>
|
|
254
|
-
>>> key = jax.random.
|
|
254
|
+
>>> key = jax.random.key(1234)
|
|
255
255
|
>>> key_v, key_o = jax.random.split(key, 2)
|
|
256
256
|
>>> *batch, path_length = (2, 6, 3)
|
|
257
257
|
>>> vertices = jax.random.uniform(key_v, (*batch, path_length, 3))
|
|
@@ -121,6 +121,18 @@ class TriangleMesh(eqx.Module):
|
|
|
121
121
|
If the present mesh contains multiple objects, usually as a result of appending
|
|
122
122
|
multiple meshes together, this array contain start end end indices for each sub mesh.
|
|
123
123
|
"""
|
|
124
|
+
assume_quads: bool = eqx.field(default=False)
|
|
125
|
+
"""Flag indicating whether triangles can be paired into quadrilaterals.
|
|
126
|
+
|
|
127
|
+
Setting this to :data:`True` will not check anything, except that
|
|
128
|
+
:attr:`num_triangles` should is even, but each two consecutive
|
|
129
|
+
triangles are assumed to represent a quadrilateral surface.
|
|
130
|
+
"""
|
|
131
|
+
|
|
132
|
+
def __check_init__(self) -> None: # noqa: D105,PLW3201
|
|
133
|
+
if self.assume_quads and (self.triangles.shape[0] % 2) != 0:
|
|
134
|
+
msg = "You cannot set 'assume_quads' to 'True' if the number of triangles is not even!"
|
|
135
|
+
raise ValueError(msg)
|
|
124
136
|
|
|
125
137
|
@jaxtyped(
|
|
126
138
|
typechecker=None
|
|
@@ -163,6 +175,19 @@ class TriangleMesh(eqx.Module):
|
|
|
163
175
|
"""The number of triangles."""
|
|
164
176
|
return self.triangles.shape[0]
|
|
165
177
|
|
|
178
|
+
@property
|
|
179
|
+
def num_quads(self) -> int:
|
|
180
|
+
"""The number of quadrilaterals.
|
|
181
|
+
|
|
182
|
+
Raises:
|
|
183
|
+
ValueError: If :attr:`assume_quads` is :data:`False`.
|
|
184
|
+
"""
|
|
185
|
+
if not self.assume_quads:
|
|
186
|
+
msg = "Cannot access the number of quadrilaterals if 'assume_quads' is set to 'False'."
|
|
187
|
+
raise ValueError(msg)
|
|
188
|
+
|
|
189
|
+
return self.triangles.shape[0] // 2
|
|
190
|
+
|
|
166
191
|
@property
|
|
167
192
|
@jax.jit
|
|
168
193
|
@jaxtyped(typechecker=typechecker)
|
|
@@ -171,6 +196,9 @@ class TriangleMesh(eqx.Module):
|
|
|
171
196
|
|
|
172
197
|
TODO: improve description.
|
|
173
198
|
"""
|
|
199
|
+
if self.triangles.size == 0:
|
|
200
|
+
return jnp.empty_like(self.vertices, shape=(0, 3, 3))
|
|
201
|
+
|
|
174
202
|
return jnp.take(self.vertices, self.triangles, axis=0)
|
|
175
203
|
|
|
176
204
|
@classmethod
|
|
@@ -294,11 +322,11 @@ class TriangleMesh(eqx.Module):
|
|
|
294
322
|
A new plane mesh.
|
|
295
323
|
|
|
296
324
|
Raises:
|
|
297
|
-
ValueError: If
|
|
298
|
-
|
|
325
|
+
ValueError: If neither ``other_vertices`` nor ``normal`` has been provided,
|
|
326
|
+
or if both have been provided simultaneously.
|
|
299
327
|
"""
|
|
300
328
|
if (other_vertices == ()) == (normal is None):
|
|
301
|
-
msg = "You must specify one of
|
|
329
|
+
msg = "You must specify one of 'other_vertices' or 'normal', not both."
|
|
302
330
|
raise ValueError(msg)
|
|
303
331
|
if other_vertices:
|
|
304
332
|
if len(other_vertices) != 2: # noqa: PLR2004
|
|
@@ -406,7 +434,7 @@ class TriangleMesh(eqx.Module):
|
|
|
406
434
|
Args:
|
|
407
435
|
size: The size of the sample, i.e., the number of triangles.
|
|
408
436
|
replace: Whether to sample with or without replacement.
|
|
409
|
-
key: The :func:`jax.random.
|
|
437
|
+
key: The :func:`jax.random.key` to be used.
|
|
410
438
|
|
|
411
439
|
Returns:
|
|
412
440
|
A new random mesh.
|
|
@@ -281,8 +281,12 @@ def draw_rays(
|
|
|
281
281
|
>>> from differt.plotting import draw_rays
|
|
282
282
|
>>>
|
|
283
283
|
>>> ray_origins = np.zeros(3)
|
|
284
|
-
>>> ray_directions = np.asarray(
|
|
285
|
-
|
|
284
|
+
>>> ray_directions = np.asarray(
|
|
285
|
+
... fibonacci_lattice(50)
|
|
286
|
+
... ) # From JAX to NumPy array
|
|
287
|
+
>>> ray_origins, ray_directions = np.broadcast_arrays(
|
|
288
|
+
... ray_origins, ray_directions
|
|
289
|
+
... )
|
|
286
290
|
>>> fig = draw_rays(
|
|
287
291
|
... ray_origins,
|
|
288
292
|
... ray_directions,
|
|
@@ -153,9 +153,13 @@ def set_defaults(backend: str | None = None, **kwargs: Any) -> BackendName:
|
|
|
153
153
|
'matplotlib'
|
|
154
154
|
>>> my_plot() # So that now it defaults to 'matplotlib' and color='red'
|
|
155
155
|
Using matplotlib backend with args = (), kwargs = {'color': 'red'}
|
|
156
|
-
>>> my_plot(
|
|
156
|
+
>>> my_plot(
|
|
157
|
+
... backend="vispy"
|
|
158
|
+
... ) # Of course, the 'vispy' backend is still available
|
|
157
159
|
Using vispy backend with args = (), kwargs = {'color': 'red'}
|
|
158
|
-
>>> my_plot(
|
|
160
|
+
>>> my_plot(
|
|
161
|
+
... backend="vispy", color="green"
|
|
162
|
+
... ) # And we can also override any default
|
|
159
163
|
Using vispy backend with args = (), kwargs = {'color': 'green'}
|
|
160
164
|
>>> dplt.set_defaults("vispy") # Reset all defaults
|
|
161
165
|
'vispy'
|
|
@@ -204,7 +208,9 @@ def use(backend: str | None = None, **kwargs: Any) -> Iterator[BackendName]:
|
|
|
204
208
|
>>>
|
|
205
209
|
>>> my_plot() # When not specified, use default backend
|
|
206
210
|
Using vispy backend with args = (), kwargs = {}
|
|
207
|
-
>>> with
|
|
211
|
+
>>> with (
|
|
212
|
+
... dplt.use()
|
|
213
|
+
... ): # No parameters = reset defaults (except the default backend)
|
|
208
214
|
... my_plot()
|
|
209
215
|
Using vispy backend with args = (), kwargs = {}
|
|
210
216
|
>>> with dplt.use("plotly"): # We can change the default backend
|
|
@@ -76,7 +76,10 @@ Examples:
|
|
|
76
76
|
... draw_paths(full_path, marker={"color": "green"}, name="Final path")
|
|
77
77
|
... markers = jnp.vstack((from_vertex, to_vertex))
|
|
78
78
|
... draw_markers(
|
|
79
|
-
... markers,
|
|
79
|
+
... markers,
|
|
80
|
+
... labels=["BS", "UE"],
|
|
81
|
+
... marker={"color": "black"},
|
|
82
|
+
... name="BS/UE",
|
|
80
83
|
... )
|
|
81
84
|
... fig.update_layout(scene_aspectmode="data")
|
|
82
85
|
>>> fig # doctest: +SKIP
|
|
@@ -120,7 +123,7 @@ def image_of_vertices_with_respect_to_mirrors(
|
|
|
120
123
|
... image_of_vertices_with_respect_to_mirrors,
|
|
121
124
|
... )
|
|
122
125
|
>>>
|
|
123
|
-
>>> key = jax.random.
|
|
126
|
+
>>> key = jax.random.key(0)
|
|
124
127
|
>>> (
|
|
125
128
|
... key0,
|
|
126
129
|
... key1,
|
|
@@ -76,17 +76,17 @@ class SizedIterator(Iterator, Sized, Generic[T]):
|
|
|
76
76
|
|
|
77
77
|
__slots__ = ("_iter", "_size")
|
|
78
78
|
|
|
79
|
-
def __init__(self, iter: Iterator[T], size: int | Callable[[], int]) -> None:
|
|
79
|
+
def __init__(self, iter: Iterator[T], size: int | Callable[[], int]) -> None: # noqa: A002,D107
|
|
80
80
|
self._iter = iter
|
|
81
81
|
self._size = size
|
|
82
82
|
|
|
83
|
-
def __iter__(self) -> Self:
|
|
83
|
+
def __iter__(self) -> Self: # noqa: D105
|
|
84
84
|
return self
|
|
85
85
|
|
|
86
|
-
def __next__(self) -> T:
|
|
86
|
+
def __next__(self) -> T: # noqa: D105
|
|
87
87
|
return next(self._iter)
|
|
88
88
|
|
|
89
|
-
def __len__(self) -> int:
|
|
89
|
+
def __len__(self) -> int: # noqa: D105
|
|
90
90
|
if isinstance(self._size, int):
|
|
91
91
|
return self._size
|
|
92
92
|
return self._size()
|
|
@@ -230,13 +230,18 @@ def rays_intersect_triangles(
|
|
|
230
230
|
>>> from differt.rt.utils import (
|
|
231
231
|
... rays_intersect_triangles,
|
|
232
232
|
... )
|
|
233
|
-
>>> from differt.scene.sionna import
|
|
233
|
+
>>> from differt.scene.sionna import (
|
|
234
|
+
... get_sionna_scene,
|
|
235
|
+
... download_sionna_scenes,
|
|
236
|
+
... )
|
|
234
237
|
>>> from differt.scene.triangle_scene import TriangleScene
|
|
235
238
|
>>>
|
|
236
239
|
>>> download_sionna_scenes()
|
|
237
240
|
>>> file = get_sionna_scene("simple_street_canyon")
|
|
238
241
|
>>> scene = TriangleScene.load_xml(file)
|
|
239
|
-
>>> scene = eqx.tree_at(
|
|
242
|
+
>>> scene = eqx.tree_at(
|
|
243
|
+
... lambda s: s.transmitters, scene, jnp.array([-33, 0, 32.0])
|
|
244
|
+
... )
|
|
240
245
|
>>> ray_origins, ray_directions = jnp.broadcast_arrays(
|
|
241
246
|
... scene.transmitters, fibonacci_lattice(25)
|
|
242
247
|
... )
|
|
@@ -362,7 +367,7 @@ def rays_intersect_any_triangle(
|
|
|
362
367
|
|
|
363
368
|
@jaxtyped(typechecker=typechecker)
|
|
364
369
|
def scan_fun(
|
|
365
|
-
intersect: Bool[Array, "
|
|
370
|
+
intersect: Bool[Array, " *batch"],
|
|
366
371
|
triangle_vertices: Float[Array, "*#batch 3 3"],
|
|
367
372
|
) -> tuple[Bool[Array, " *batch"], None]:
|
|
368
373
|
t, hit = rays_intersect_triangles(
|
|
@@ -421,13 +426,18 @@ def triangles_visible_from_vertices(
|
|
|
421
426
|
>>> from differt.rt.utils import (
|
|
422
427
|
... triangles_visible_from_vertices,
|
|
423
428
|
... )
|
|
424
|
-
>>> from differt.scene.sionna import
|
|
429
|
+
>>> from differt.scene.sionna import (
|
|
430
|
+
... get_sionna_scene,
|
|
431
|
+
... download_sionna_scenes,
|
|
432
|
+
... )
|
|
425
433
|
>>> from differt.scene.triangle_scene import TriangleScene
|
|
426
434
|
>>>
|
|
427
435
|
>>> download_sionna_scenes()
|
|
428
436
|
>>> file = get_sionna_scene("simple_street_canyon")
|
|
429
437
|
>>> scene = TriangleScene.load_xml(file)
|
|
430
|
-
>>> scene = eqx.tree_at(
|
|
438
|
+
>>> scene = eqx.tree_at(
|
|
439
|
+
... lambda s: s.transmitters, scene, jnp.array([-33, 0, 32.0])
|
|
440
|
+
... )
|
|
431
441
|
>>> visible_triangles = triangles_visible_from_vertices(
|
|
432
442
|
... scene.transmitters,
|
|
433
443
|
... scene.mesh.triangle_vertices,
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
# ruff: noqa: ERA001
|
|
3
3
|
|
|
4
4
|
import sys
|
|
5
|
+
import warnings
|
|
5
6
|
from collections.abc import Mapping
|
|
6
7
|
from typing import Any
|
|
7
8
|
|
|
@@ -10,7 +11,11 @@ import jax
|
|
|
10
11
|
import jax.numpy as jnp
|
|
11
12
|
import numpy as np
|
|
12
13
|
from beartype import beartype as typechecker
|
|
13
|
-
from
|
|
14
|
+
from jax.experimental import mesh_utils
|
|
15
|
+
from jax.experimental.shard_map import shard_map
|
|
16
|
+
from jax.sharding import Mesh
|
|
17
|
+
from jax.sharding import PartitionSpec as P # noqa: N814
|
|
18
|
+
from jaxtyping import Array, ArrayLike, Bool, Float, Int, jaxtyped
|
|
14
19
|
|
|
15
20
|
import differt_core.scene.triangle_scene
|
|
16
21
|
from differt.geometry.paths import Paths
|
|
@@ -44,20 +49,44 @@ def _compute_paths(
|
|
|
44
49
|
from_vertices: Float[Array, "num_from_vertices 3"],
|
|
45
50
|
to_vertices: Float[Array, "num_to_vertices 3"],
|
|
46
51
|
path_candidates: Int[Array, "num_path_candidates order"],
|
|
47
|
-
|
|
52
|
+
*,
|
|
53
|
+
parallel: bool = False,
|
|
54
|
+
epsilon: Float[ArrayLike, " "] | None = None,
|
|
55
|
+
hit_tol: Float[ArrayLike, " "] | None = None,
|
|
56
|
+
min_len: Float[ArrayLike, " "] | None = None,
|
|
48
57
|
) -> Paths:
|
|
49
|
-
|
|
50
|
-
|
|
58
|
+
if min_len is None:
|
|
59
|
+
dtype = jnp.result_type(mesh.vertices, from_vertices, to_vertices)
|
|
60
|
+
min_len = 10 * jnp.finfo(dtype).eps
|
|
51
61
|
|
|
52
62
|
# 1 - Broadcast arrays
|
|
53
63
|
|
|
54
|
-
num_path_candidates = path_candidates.shape
|
|
64
|
+
num_path_candidates, order = path_candidates.shape
|
|
55
65
|
|
|
56
66
|
# [num_path_candidates order 3]
|
|
57
|
-
triangles = jnp.take(mesh.triangles, path_candidates, axis=0)
|
|
67
|
+
triangles = jnp.take(mesh.triangles, path_candidates, axis=0).reshape(
|
|
68
|
+
num_path_candidates, order, 3
|
|
69
|
+
) # reshape required if mesh is empty
|
|
58
70
|
|
|
59
71
|
# [num_path_candidates order 3 3]
|
|
60
|
-
triangle_vertices = jnp.take(mesh.vertices, triangles, axis=0)
|
|
72
|
+
triangle_vertices = jnp.take(mesh.vertices, triangles, axis=0).reshape(
|
|
73
|
+
num_path_candidates, order, 3, 3
|
|
74
|
+
) # reshape required if mesh is empty
|
|
75
|
+
|
|
76
|
+
if mesh.assume_quads:
|
|
77
|
+
# [num_path_candidates order 2 3]
|
|
78
|
+
quads = jnp.take(
|
|
79
|
+
mesh.triangles,
|
|
80
|
+
jnp.stack((path_candidates, path_candidates + 1), axis=-1),
|
|
81
|
+
axis=0,
|
|
82
|
+
).reshape(num_path_candidates, order, 2, 3) # reshape required if mesh is empty
|
|
83
|
+
|
|
84
|
+
# [num_path_candidates order 2 3 3]
|
|
85
|
+
quad_vertices = jnp.take(mesh.vertices, quads, axis=0).reshape(
|
|
86
|
+
num_path_candidates, order, 2, 3, 3
|
|
87
|
+
) # reshape required if mesh is empty
|
|
88
|
+
else:
|
|
89
|
+
quad_vertices = None
|
|
61
90
|
|
|
62
91
|
# [num_path_candidates order 3]
|
|
63
92
|
mirror_vertices = triangle_vertices[
|
|
@@ -69,67 +98,124 @@ def _compute_paths(
|
|
|
69
98
|
# [num_path_candidates order 3]
|
|
70
99
|
mirror_normals = jnp.take(mesh.normals, path_candidates, axis=0)
|
|
71
100
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
paths
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
# [tx_batch_flat rx_batch_flat num_path_candidates order+1 3]
|
|
92
|
-
ray_origins = full_paths[..., :-1, :]
|
|
93
|
-
# [tx_batch_flat rx_batch_flat num_path_candidates order+1 3]
|
|
94
|
-
ray_directions = jnp.diff(full_paths, axis=-2)
|
|
95
|
-
|
|
96
|
-
# 3.1 - Check if paths vertices are inside respective triangles
|
|
97
|
-
|
|
98
|
-
# [tx_batch_flat rx_batch_flat num_path_candidates]
|
|
99
|
-
inside_triangles = rays_intersect_triangles(
|
|
100
|
-
ray_origins[..., :-1, :],
|
|
101
|
-
ray_directions[..., :-1, :],
|
|
102
|
-
triangle_vertices,
|
|
103
|
-
epsilon=epsilon,
|
|
104
|
-
)[1].all(axis=-1) # Reduce on 'order' axis
|
|
101
|
+
@jaxtyped(typechecker=typechecker)
|
|
102
|
+
def fun(
|
|
103
|
+
from_vertices: Float[Array, "num_from_vertices 3"],
|
|
104
|
+
to_vertices: Float[Array, "num_to_vertices 3"],
|
|
105
|
+
) -> tuple[
|
|
106
|
+
Float[
|
|
107
|
+
Array, "num_from_vertices num_to_vertices num_path_candidates path_length 3"
|
|
108
|
+
],
|
|
109
|
+
Bool[Array, "num_from_vertices num_to_vertices num_path_candidates"],
|
|
110
|
+
]:
|
|
111
|
+
# 2 - Trace paths
|
|
112
|
+
|
|
113
|
+
# [num_from_vertices num_to_vertices num_path_candidates order 3]
|
|
114
|
+
paths = image_method(
|
|
115
|
+
from_vertices[:, None, None, :],
|
|
116
|
+
to_vertices[None, :, None, :],
|
|
117
|
+
mirror_vertices,
|
|
118
|
+
mirror_normals,
|
|
119
|
+
)
|
|
105
120
|
|
|
106
|
-
|
|
121
|
+
# [num_from_vertices num_to_vertices num_path_candidates order+2 3]
|
|
122
|
+
full_paths = assemble_paths(
|
|
123
|
+
from_vertices[:, None, None, None, :],
|
|
124
|
+
paths,
|
|
125
|
+
to_vertices[None, :, None, None, :],
|
|
126
|
+
)
|
|
107
127
|
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
128
|
+
# 3 - Identify invalid paths
|
|
129
|
+
|
|
130
|
+
# [num_from_vertices num_to_vertices num_path_candidates order+1 3]
|
|
131
|
+
ray_origins = full_paths[..., :-1, :]
|
|
132
|
+
# [num_from_vertices num_to_vertices num_path_candidates order+1 3]
|
|
133
|
+
ray_directions = jnp.diff(full_paths, axis=-2)
|
|
134
|
+
|
|
135
|
+
# 3.1 - Check if paths vertices are inside respective triangles
|
|
136
|
+
|
|
137
|
+
# [num_from_vertices num_to_vertices num_path_candidates]
|
|
138
|
+
if mesh.assume_quads:
|
|
139
|
+
inside_triangles = (
|
|
140
|
+
rays_intersect_triangles(
|
|
141
|
+
ray_origins[..., :-1, None, :],
|
|
142
|
+
ray_directions[..., :-1, None, :],
|
|
143
|
+
quad_vertices, # type: ignore[reportArgumentType]
|
|
144
|
+
epsilon=epsilon,
|
|
145
|
+
)[1]
|
|
146
|
+
.any(axis=-1)
|
|
147
|
+
.all(axis=-1)
|
|
148
|
+
) # Reduce on 'order' axis and on the two triangles (per quad)
|
|
149
|
+
else:
|
|
150
|
+
inside_triangles = rays_intersect_triangles(
|
|
151
|
+
ray_origins[..., :-1, :],
|
|
152
|
+
ray_directions[..., :-1, :],
|
|
153
|
+
triangle_vertices,
|
|
154
|
+
epsilon=epsilon,
|
|
155
|
+
)[1].all(axis=-1) # Reduce on 'order' axis
|
|
156
|
+
|
|
157
|
+
# 3.2 - Check if consecutive path vertices are on the same side of mirrors
|
|
158
|
+
|
|
159
|
+
# [num_from_vertices num_to_vertices num_path_candidates]
|
|
160
|
+
valid_reflections = consecutive_vertices_are_on_same_side_of_mirrors(
|
|
161
|
+
full_paths,
|
|
162
|
+
mirror_vertices,
|
|
163
|
+
mirror_normals,
|
|
164
|
+
).all(axis=-1) # Reduce on 'order'
|
|
165
|
+
|
|
166
|
+
# 3.3 - Identify paths that are blocked by other objects
|
|
167
|
+
|
|
168
|
+
# [num_from_vertices num_to_vertices num_path_candidates]
|
|
169
|
+
blocked = rays_intersect_any_triangle(
|
|
170
|
+
ray_origins,
|
|
171
|
+
ray_directions,
|
|
172
|
+
mesh.triangle_vertices,
|
|
173
|
+
epsilon=epsilon,
|
|
174
|
+
hit_tol=hit_tol,
|
|
175
|
+
).any(axis=-1) # Reduce on 'order'
|
|
176
|
+
|
|
177
|
+
# 3.4 - Identify path segments that are too small (e.g., double-reflection inside an edge)
|
|
178
|
+
|
|
179
|
+
ray_lengths = jnp.sum(ray_directions * ray_directions, axis=-1) # Squared norm
|
|
180
|
+
|
|
181
|
+
too_small = (ray_lengths < min_len).any(
|
|
182
|
+
axis=-1
|
|
183
|
+
) # Any path segment being too small
|
|
184
|
+
|
|
185
|
+
mask = inside_triangles & valid_reflections & ~blocked & ~too_small
|
|
186
|
+
|
|
187
|
+
return full_paths, mask
|
|
188
|
+
|
|
189
|
+
if parallel:
|
|
190
|
+
num_devices = jax.device_count()
|
|
191
|
+
|
|
192
|
+
# TODO: allow also to have i,i mesh if product of both is a multiple of 'num_devices'
|
|
193
|
+
if from_vertices.shape[0] % num_devices == 0:
|
|
194
|
+
in_specs = (P("i", None), P(None, None))
|
|
195
|
+
out_specs = (P("i", None, None, None, None), P("i", None, None))
|
|
196
|
+
elif to_vertices.shape[0] % num_devices == 0:
|
|
197
|
+
in_specs = (P(None, None), P("i", None))
|
|
198
|
+
out_specs = (P(None, "i", None, None, None), P(None, "i", None))
|
|
199
|
+
else:
|
|
200
|
+
msg = (
|
|
201
|
+
f"Found {num_devices} devices available, "
|
|
202
|
+
"but could not find any input with a size that is a multiple of that value. "
|
|
203
|
+
"Please user a number of transmitter or receiver points that is a "
|
|
204
|
+
f"multiple of {num_devices}."
|
|
205
|
+
)
|
|
206
|
+
raise ValueError(msg)
|
|
114
207
|
|
|
115
|
-
|
|
208
|
+
fun = shard_map( # type: ignore[reportAssigmentType]
|
|
209
|
+
fun,
|
|
210
|
+
Mesh(mesh_utils.create_device_mesh((num_devices,)), axis_names=("i",)),
|
|
211
|
+
in_specs=in_specs,
|
|
212
|
+
out_specs=out_specs,
|
|
213
|
+
)
|
|
116
214
|
|
|
117
|
-
|
|
118
|
-
blocked = rays_intersect_any_triangle(
|
|
119
|
-
ray_origins,
|
|
120
|
-
ray_directions,
|
|
121
|
-
mesh.triangle_vertices,
|
|
122
|
-
epsilon=epsilon,
|
|
123
|
-
hit_tol=hit_tol,
|
|
124
|
-
).any(axis=-1) # Reduce on 'order'
|
|
215
|
+
vertices, mask = fun(from_vertices, to_vertices)
|
|
125
216
|
|
|
126
217
|
# 4 - Generate output paths and reshape
|
|
127
218
|
|
|
128
|
-
vertices = full_paths
|
|
129
|
-
mask = inside_triangles & valid_reflections & ~blocked
|
|
130
|
-
|
|
131
|
-
# TODO: we also need to somehow mask degenerate paths, e.g., when two reflections occur on an edge
|
|
132
|
-
|
|
133
219
|
object_dtype = path_candidates.dtype
|
|
134
220
|
|
|
135
221
|
tx_objects = jnp.arange(from_vertices.shape[0], dtype=object_dtype)
|
|
@@ -303,24 +389,70 @@ class TriangleScene(eqx.Module):
|
|
|
303
389
|
return cls.from_core(core_scene)
|
|
304
390
|
|
|
305
391
|
def compute_paths(
|
|
306
|
-
self,
|
|
392
|
+
self,
|
|
393
|
+
order: int | None = None,
|
|
394
|
+
*,
|
|
395
|
+
chunk_size: int | None = None,
|
|
396
|
+
path_candidates: Int[Array, "num_path_candidates order"] | None = None,
|
|
397
|
+
parallel: bool = False,
|
|
398
|
+
epsilon: Float[ArrayLike, " "] | None = None,
|
|
399
|
+
hit_tol: Float[ArrayLike, " "] | None = None,
|
|
400
|
+
min_len: Float[ArrayLike, " "] | None = None,
|
|
307
401
|
) -> Paths | SizedIterator[Paths]:
|
|
308
402
|
"""
|
|
309
403
|
Compute paths between all pairs of transmitters and receivers in the scene, that undergo a fixed number of interaction with objects.
|
|
310
404
|
|
|
405
|
+
Note:
|
|
406
|
+
Currently, only :abbr:`LOS (line of sight)` and fixed ``order`` reflection paths are computed,
|
|
407
|
+
using the :func:`image_method<differt.rt.image_method.image_method>`. More types of interactions
|
|
408
|
+
and path tracing methods will be added in the future, so stay tuned!
|
|
409
|
+
|
|
311
410
|
Args:
|
|
312
411
|
order: The number of interaction, i.e., the number of bounces.
|
|
412
|
+
|
|
413
|
+
This or ``path_candidates`` must be specified.
|
|
313
414
|
chunk_size: If specified, it will iterate through chunks of path
|
|
314
415
|
candidates, and yield the result as an iterator over paths chunks.
|
|
315
|
-
|
|
416
|
+
|
|
417
|
+
Unused if ``path_candidates`` is provided.
|
|
418
|
+
path_candidates: An option array of path candidates, see :ref:`path_candidates`.
|
|
419
|
+
|
|
420
|
+
This is helpful to only generate paths on a subset of the scene.
|
|
421
|
+
|
|
422
|
+
If ``self.mesh.assume_quads`` is :data:`True`, then path candidates are
|
|
423
|
+
rounded down toward the nearest even value.
|
|
424
|
+
parallel: If :data:`True`, ray tracing is performed in parallel across all available
|
|
425
|
+
devices. Either the number of transmitters or the number of receivers
|
|
426
|
+
**must** be a multiple of :func:`jax.device_count`, otherwise an error is raised.
|
|
427
|
+
epsilon: Tolelance for checking ray / objects intersection, see
|
|
428
|
+
:func:`rays_intersect_triangles<differt.rt.utils.rays_intersect_triangles>`.
|
|
429
|
+
hit_tol: Tolerance for checking blockage (i.e., obstruction), see
|
|
316
430
|
:func:`rays_intersect_any_triangle<differt.rt.utils.rays_intersect_any_triangle>`.
|
|
431
|
+
min_len: Minimal (squared) length that each path segment must have for a path to be valid.
|
|
432
|
+
|
|
433
|
+
If not specified, the default is ten times the epsilon value
|
|
434
|
+
of the currently used floating point dtype.
|
|
317
435
|
|
|
318
436
|
Returns:
|
|
319
437
|
The paths, as class wrapping path vertices, object indices, and a masked
|
|
320
438
|
identify valid paths.
|
|
439
|
+
|
|
440
|
+
Raises:
|
|
441
|
+
ValueError: If neither ``order`` nor ``path_candidates`` has been provided,
|
|
442
|
+
or if both have been provided simultaneously.
|
|
321
443
|
"""
|
|
444
|
+
if (order is None) == (path_candidates is None):
|
|
445
|
+
msg = "You must specify one of 'order' or `path_candidates`, not both."
|
|
446
|
+
raise ValueError(msg)
|
|
447
|
+
if (chunk_size is not None) and (path_candidates is not None):
|
|
448
|
+
msg = "Argument 'chunk_size' is ignored when 'path_candidates' is provided."
|
|
449
|
+
warnings.warn(msg, UserWarning, stacklevel=2)
|
|
450
|
+
chunk_size = None
|
|
451
|
+
|
|
322
452
|
# 0 - Constants arrays of chunks
|
|
323
|
-
|
|
453
|
+
num_objects = (
|
|
454
|
+
self.mesh.num_quads if self.mesh.assume_quads else self.mesh.num_triangles
|
|
455
|
+
)
|
|
324
456
|
tx_batch = self.transmitters.shape[:-1]
|
|
325
457
|
rx_batch = self.receivers.shape[:-1]
|
|
326
458
|
|
|
@@ -331,21 +463,47 @@ class TriangleScene(eqx.Module):
|
|
|
331
463
|
|
|
332
464
|
if chunk_size:
|
|
333
465
|
path_candidates_iter = generate_all_path_candidates_chunks_iter(
|
|
334
|
-
|
|
466
|
+
num_objects,
|
|
467
|
+
order, # type: ignore[reportArgumentType]
|
|
468
|
+
chunk_size=chunk_size,
|
|
335
469
|
)
|
|
336
470
|
size = path_candidates_iter.__len__
|
|
337
471
|
it = (
|
|
338
472
|
_compute_paths(
|
|
339
|
-
self.mesh,
|
|
473
|
+
self.mesh,
|
|
474
|
+
from_vertices,
|
|
475
|
+
to_vertices,
|
|
476
|
+
2 * path_candidates if self.mesh.assume_quads else path_candidates,
|
|
477
|
+
parallel=parallel,
|
|
478
|
+
epsilon=epsilon,
|
|
479
|
+
hit_tol=hit_tol,
|
|
480
|
+
min_len=min_len,
|
|
340
481
|
).reshape(*tx_batch, *rx_batch, path_candidates.shape[0])
|
|
341
482
|
for path_candidates in path_candidates_iter
|
|
342
483
|
)
|
|
343
484
|
|
|
344
485
|
return SizedIterator(it, size=size)
|
|
345
486
|
|
|
346
|
-
path_candidates
|
|
487
|
+
if path_candidates is None:
|
|
488
|
+
path_candidates = generate_all_path_candidates(
|
|
489
|
+
num_objects,
|
|
490
|
+
order, # type: ignore[reportArgumentType]
|
|
491
|
+
)
|
|
492
|
+
|
|
493
|
+
if self.mesh.assume_quads:
|
|
494
|
+
path_candidates = 2 * path_candidates
|
|
495
|
+
elif self.mesh.assume_quads:
|
|
496
|
+
path_candidates -= path_candidates % 2
|
|
497
|
+
|
|
347
498
|
return _compute_paths(
|
|
348
|
-
self.mesh,
|
|
499
|
+
self.mesh,
|
|
500
|
+
from_vertices,
|
|
501
|
+
to_vertices,
|
|
502
|
+
path_candidates,
|
|
503
|
+
parallel=parallel,
|
|
504
|
+
epsilon=epsilon,
|
|
505
|
+
hit_tol=hit_tol,
|
|
506
|
+
min_len=min_len,
|
|
349
507
|
).reshape(*tx_batch, *rx_batch, path_candidates.shape[0])
|
|
350
508
|
|
|
351
509
|
def plot(
|
|
@@ -43,7 +43,7 @@ def sorted_array2(array: Shaped[Array, "m n"]) -> Shaped[Array, "m n"]:
|
|
|
43
43
|
... )
|
|
44
44
|
>>>
|
|
45
45
|
>>> arr = jnp.arange(10).reshape(5, 2)
|
|
46
|
-
>>> key = jax.random.
|
|
46
|
+
>>> key = jax.random.key(1234)
|
|
47
47
|
>>> (
|
|
48
48
|
... key1,
|
|
49
49
|
... key2,
|
|
@@ -160,7 +160,9 @@ def minimize(
|
|
|
160
160
|
>>> # You can also change the optimizer and the number of steps
|
|
161
161
|
>>> import optax
|
|
162
162
|
>>> optimizer = optax.noisy_sgd(learning_rate=0.003)
|
|
163
|
-
>>> x, y = minimize(
|
|
163
|
+
>>> x, y = minimize(
|
|
164
|
+
... f, jnp.zeros(5), args=(4.0,), steps=10000, optimizer=optimizer
|
|
165
|
+
... )
|
|
164
166
|
>>> chex.assert_trees_all_close(x, 4.0 * jnp.ones(5), rtol=1e-2)
|
|
165
167
|
>>> chex.assert_trees_all_close(y, 0.0, atol=1e-3)
|
|
166
168
|
|
|
@@ -173,7 +175,7 @@ def minimize(
|
|
|
173
175
|
>>>
|
|
174
176
|
>>> batch = (1, 2, 3)
|
|
175
177
|
>>> n = 10
|
|
176
|
-
>>> key = jax.random.
|
|
178
|
+
>>> key = jax.random.key(1234)
|
|
177
179
|
>>> offset = jax.random.uniform(key, (*batch, n))
|
|
178
180
|
>>>
|
|
179
181
|
>>> def f(x, offset, scale=2.0):
|
|
@@ -233,17 +235,20 @@ def minimize(
|
|
|
233
235
|
@jaxtyped(typechecker=typechecker)
|
|
234
236
|
def sample_points_in_bounding_box(
|
|
235
237
|
bounding_box: Float[Array, "2 3"],
|
|
236
|
-
|
|
238
|
+
shape: tuple[int, ...] | None = None,
|
|
237
239
|
*,
|
|
238
240
|
key: PRNGKeyArray,
|
|
239
|
-
) ->
|
|
241
|
+
) -> (
|
|
242
|
+
Float[Array, "*shape 3"] | Float[Array, "3"]
|
|
243
|
+
): # TODO: use symbolic expression to link to 'shape' parameter
|
|
240
244
|
"""
|
|
241
245
|
Sample point(s) in a 3D bounding box.
|
|
242
246
|
|
|
243
247
|
Args:
|
|
244
248
|
bounding_box: The bounding box (min. and max. coordinates).
|
|
245
|
-
|
|
246
|
-
the returned array is 1D. Otherwise,
|
|
249
|
+
shape: The sample shape or :data:`None`. If :data:`None`,
|
|
250
|
+
the returned array is 1D. Otherwise, the shape
|
|
251
|
+
of the returned array is ``(*shape, 3)``.
|
|
247
252
|
key: The :func:`jax.random.PRNGKey` to be used.
|
|
248
253
|
|
|
249
254
|
Returns:
|
|
@@ -253,9 +258,9 @@ def sample_points_in_bounding_box(
|
|
|
253
258
|
amax = bounding_box[1, :]
|
|
254
259
|
scale = amax - amin
|
|
255
260
|
|
|
256
|
-
if
|
|
257
|
-
|
|
258
|
-
|
|
261
|
+
if shape is None:
|
|
262
|
+
shape = ()
|
|
263
|
+
|
|
264
|
+
r = jax.random.uniform(key, shape=(*shape, 3))
|
|
259
265
|
|
|
260
|
-
r
|
|
261
|
-
return r * scale[None, :] + amin[None, :]
|
|
266
|
+
return r * scale + amin
|
|
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
|