differt 0.4.0__tar.gz → 0.4.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.4.0 → differt-0.4.1}/PKG-INFO +3 -3
- {differt-0.4.0 → differt-0.4.1}/README.md +1 -1
- {differt-0.4.0 → differt-0.4.1}/pyproject.toml +1 -1
- {differt-0.4.0 → differt-0.4.1}/src/differt/em/_antenna.py +1 -1
- {differt-0.4.0 → differt-0.4.1}/src/differt/em/_utd.py +1 -1
- {differt-0.4.0 → differt-0.4.1}/src/differt/geometry/_triangle_mesh.py +5 -0
- {differt-0.4.0 → differt-0.4.1}/src/differt/plotting/_core.py +18 -6
- {differt-0.4.0 → differt-0.4.1}/src/differt/plugins/deepmimo.py +1 -1
- {differt-0.4.0 → differt-0.4.1}/src/differt/rt/_fermat.py +57 -78
- {differt-0.4.0 → differt-0.4.1}/src/differt/rt/_image_method.py +80 -80
- {differt-0.4.0 → differt-0.4.1}/src/differt/rt/_utils.py +289 -110
- {differt-0.4.0 → differt-0.4.1}/src/differt/scene/_triangle_scene.py +33 -29
- {differt-0.4.0 → differt-0.4.1}/.gitignore +0 -0
- {differt-0.4.0 → differt-0.4.1}/LICENSE.md +0 -0
- {differt-0.4.0 → differt-0.4.1}/src/differt/__init__.py +0 -0
- {differt-0.4.0 → differt-0.4.1}/src/differt/_version.py +0 -0
- {differt-0.4.0 → differt-0.4.1}/src/differt/em/__init__.py +0 -0
- {differt-0.4.0 → differt-0.4.1}/src/differt/em/_constants.py +0 -0
- {differt-0.4.0 → differt-0.4.1}/src/differt/em/_fresnel.py +0 -0
- {differt-0.4.0 → differt-0.4.1}/src/differt/em/_interaction_type.py +0 -0
- {differt-0.4.0 → differt-0.4.1}/src/differt/em/_material.py +0 -0
- {differt-0.4.0 → differt-0.4.1}/src/differt/em/_utils.py +0 -0
- {differt-0.4.0 → differt-0.4.1}/src/differt/geometry/__init__.py +0 -0
- {differt-0.4.0 → differt-0.4.1}/src/differt/geometry/_paths.py +0 -0
- {differt-0.4.0 → differt-0.4.1}/src/differt/geometry/_utils.py +0 -0
- {differt-0.4.0 → differt-0.4.1}/src/differt/plotting/__init__.py +0 -0
- {differt-0.4.0 → differt-0.4.1}/src/differt/plotting/_utils.py +0 -0
- {differt-0.4.0 → differt-0.4.1}/src/differt/plugins/__init__.py +0 -0
- {differt-0.4.0 → differt-0.4.1}/src/differt/plugins/_deepmimo_types.py +0 -0
- {differt-0.4.0 → differt-0.4.1}/src/differt/py.typed +0 -0
- {differt-0.4.0 → differt-0.4.1}/src/differt/rt/__init__.py +0 -0
- {differt-0.4.0 → differt-0.4.1}/src/differt/scene/__init__.py +0 -0
- {differt-0.4.0 → differt-0.4.1}/src/differt/scene/_sionna.py +0 -0
- {differt-0.4.0 → differt-0.4.1}/src/differt/utils.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: DiffeRT
|
|
3
|
-
Version: 0.4.
|
|
3
|
+
Version: 0.4.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
|
|
@@ -21,7 +21,7 @@ Classifier: Programming Language :: Python :: 3.13
|
|
|
21
21
|
Classifier: Topic :: Scientific/Engineering
|
|
22
22
|
Requires-Python: >=3.10
|
|
23
23
|
Requires-Dist: chex>=0.1.86
|
|
24
|
-
Requires-Dist: differt-core==0.4.
|
|
24
|
+
Requires-Dist: differt-core==0.4.1
|
|
25
25
|
Requires-Dist: equinox>=0.12.2
|
|
26
26
|
Requires-Dist: filelock>=3.15.4
|
|
27
27
|
Requires-Dist: jax>=0.4.32
|
|
@@ -140,7 +140,7 @@ If you use this software, please cite it as:
|
|
|
140
140
|
author = {Eertmans, Jérome},
|
|
141
141
|
url = {https://github.com/jeertmans/DiffeRT},
|
|
142
142
|
license = {MIT},
|
|
143
|
-
version = {v0.4.
|
|
143
|
+
version = {v0.4.1}
|
|
144
144
|
}
|
|
145
145
|
```
|
|
146
146
|
|
|
@@ -645,7 +645,7 @@ class RadiationPattern(BaseAntenna):
|
|
|
645
645
|
|
|
646
646
|
r = self.center + distance * jnp.stack((x, y, z), axis=-1)
|
|
647
647
|
|
|
648
|
-
s = self.poynting_vector(r)
|
|
648
|
+
s = self.poynting_vector(r)
|
|
649
649
|
|
|
650
650
|
p = jnp.linalg.norm(s, axis=-1, keepdims=True)
|
|
651
651
|
|
|
@@ -248,6 +248,11 @@ class TriangleMesh(eqx.Module):
|
|
|
248
248
|
|
|
249
249
|
Unless specified, the transformation or selection operations, like :meth:`rotate`,
|
|
250
250
|
will not take the mask into account, and will apply to all triangles.
|
|
251
|
+
|
|
252
|
+
.. important::
|
|
253
|
+
|
|
254
|
+
When :attr:`assume_quads` is :data:`True`, a quad is considered active if both triangles
|
|
255
|
+
that form the quad are active.
|
|
251
256
|
"""
|
|
252
257
|
|
|
253
258
|
def __check_init__(self) -> None: # noqa: PLW3201
|
|
@@ -99,10 +99,14 @@ def _(
|
|
|
99
99
|
|
|
100
100
|
canvas, view = process_vispy_kwargs(kwargs)
|
|
101
101
|
|
|
102
|
+
if vertices.size == 0 or triangles.size == 0: # type: ignore[reportAttributeAccessIssue] # pragma: no cover
|
|
103
|
+
return canvas
|
|
104
|
+
|
|
102
105
|
kwargs.setdefault("shading", "flat")
|
|
103
106
|
|
|
104
107
|
vertices = np.asarray(vertices)
|
|
105
108
|
triangles = np.asarray(triangles)
|
|
109
|
+
|
|
106
110
|
view.add(Mesh(vertices=vertices, faces=triangles, **kwargs))
|
|
107
111
|
view.camera.set_range()
|
|
108
112
|
|
|
@@ -202,6 +206,9 @@ def _(
|
|
|
202
206
|
|
|
203
207
|
canvas, view = process_vispy_kwargs(kwargs)
|
|
204
208
|
|
|
209
|
+
if paths.size == 0: # type: ignore[reportAttributeAccessIssue] # pragma: no cover
|
|
210
|
+
return canvas
|
|
211
|
+
|
|
205
212
|
kwargs.setdefault("width", 3.0)
|
|
206
213
|
kwargs.setdefault("marker_size", 0.0)
|
|
207
214
|
paths = np.asarray(paths)
|
|
@@ -211,7 +218,6 @@ def _(
|
|
|
211
218
|
connect[path_length - 1 :: path_length] = False
|
|
212
219
|
|
|
213
220
|
view.add(LinePlot(data=paths, connect=connect, **kwargs)) # type: ignore[reportArgumentType]
|
|
214
|
-
|
|
215
221
|
view.camera.set_range()
|
|
216
222
|
|
|
217
223
|
return canvas
|
|
@@ -353,6 +359,9 @@ def _(
|
|
|
353
359
|
|
|
354
360
|
canvas, view = process_vispy_kwargs(kwargs)
|
|
355
361
|
|
|
362
|
+
if ray_origins.size == 0 or ray_directions.size == 0: # type: ignore[reportAttributeAccessIssue] # pragma: no cover
|
|
363
|
+
return canvas
|
|
364
|
+
|
|
356
365
|
kwargs.setdefault("width", 3.0)
|
|
357
366
|
kwargs.setdefault("arrow_size", 6.0)
|
|
358
367
|
|
|
@@ -370,7 +379,6 @@ def _(
|
|
|
370
379
|
arrows = np.concatenate((body_ends, arrows_center), axis=-1)
|
|
371
380
|
|
|
372
381
|
view.add(Arrow(pos=pos, connect=connect, arrows=arrows, **kwargs)) # type: ignore[reportArgumentType]
|
|
373
|
-
|
|
374
382
|
view.camera.set_range()
|
|
375
383
|
|
|
376
384
|
return canvas
|
|
@@ -512,6 +520,10 @@ def _(
|
|
|
512
520
|
from vispy.scene.visuals import Markers, Text # noqa: PLC0415
|
|
513
521
|
|
|
514
522
|
canvas, view = process_vispy_kwargs(kwargs)
|
|
523
|
+
|
|
524
|
+
if markers.size == 0: # type: ignore[reportAttributeAccessIssue] # pragma: no cover
|
|
525
|
+
return canvas
|
|
526
|
+
|
|
515
527
|
kwargs.setdefault("size", 1)
|
|
516
528
|
kwargs.setdefault("edge_width_rel", 0.05)
|
|
517
529
|
kwargs.setdefault("scaling", "scene")
|
|
@@ -734,7 +746,7 @@ def _(
|
|
|
734
746
|
|
|
735
747
|
|
|
736
748
|
@dispatch
|
|
737
|
-
def draw_contour(
|
|
749
|
+
def draw_contour(
|
|
738
750
|
data: Real[ArrayLike, "rows cols"],
|
|
739
751
|
x: Real[ArrayLike, " cols"] | Real[ArrayLike, "rows cols 3"] | None = None,
|
|
740
752
|
y: Real[ArrayLike, " rows"] | Real[ArrayLike, "rows cols 3"] | None = None,
|
|
@@ -796,7 +808,7 @@ def draw_contour( # noqa: PLR0917
|
|
|
796
808
|
|
|
797
809
|
|
|
798
810
|
@draw_contour.register("vispy")
|
|
799
|
-
def _(
|
|
811
|
+
def _(
|
|
800
812
|
data: Real[ArrayLike, "rows cols"],
|
|
801
813
|
x: Real[ArrayLike, " cols"] | Real[ArrayLike, "rows cols"] | None = None,
|
|
802
814
|
y: Real[ArrayLike, " rows"] | Real[ArrayLike, "rows cols"] | None = None,
|
|
@@ -861,7 +873,7 @@ def _( # noqa: PLR0917
|
|
|
861
873
|
|
|
862
874
|
|
|
863
875
|
@draw_contour.register("matplotlib")
|
|
864
|
-
def _(
|
|
876
|
+
def _(
|
|
865
877
|
data: Real[ArrayLike, "rows cols"],
|
|
866
878
|
x: Real[ArrayLike, " cols"] | Real[ArrayLike, "rows cols"] | None = None,
|
|
867
879
|
y: Real[ArrayLike, " rows"] | Real[ArrayLike, "rows cols"] | None = None,
|
|
@@ -891,7 +903,7 @@ def _( # noqa: PLR0917
|
|
|
891
903
|
|
|
892
904
|
|
|
893
905
|
@draw_contour.register("plotly")
|
|
894
|
-
def _(
|
|
906
|
+
def _(
|
|
895
907
|
data: Real[ArrayLike, "rows cols"],
|
|
896
908
|
x: Real[ArrayLike, " cols"] | Real[ArrayLike, "rows cols"] | None = None,
|
|
897
909
|
y: Real[ArrayLike, " rows"] | Real[ArrayLike, "rows cols"] | None = None,
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
from functools import partial
|
|
2
1
|
from typing import Any, no_type_check
|
|
3
2
|
|
|
4
3
|
import chex
|
|
@@ -11,7 +10,6 @@ from jaxtyping import Array, ArrayLike, Float
|
|
|
11
10
|
from differt.geometry import orthogonal_basis
|
|
12
11
|
|
|
13
12
|
|
|
14
|
-
@partial(jax.jit, inline=True)
|
|
15
13
|
def _param_to_xyz(
|
|
16
14
|
p: Float[Array, "*batch num_object_x_num_dims"],
|
|
17
15
|
o: Float[Array, "*#batch num_objects 3"],
|
|
@@ -22,7 +20,6 @@ def _param_to_xyz(
|
|
|
22
20
|
return o + jnp.sum(p * v, axis=-2)
|
|
23
21
|
|
|
24
22
|
|
|
25
|
-
@partial(jax.jit, inline=True)
|
|
26
23
|
def _loss(
|
|
27
24
|
p: Float[Array, "*batch num_object_x_num_dims"],
|
|
28
25
|
from_: Float[Array, "*#batch 3"],
|
|
@@ -45,6 +42,47 @@ def _loss(
|
|
|
45
42
|
)
|
|
46
43
|
|
|
47
44
|
|
|
45
|
+
def _fermat_path_on_linear_objects(
|
|
46
|
+
from_vertex: Float[Array, "3"],
|
|
47
|
+
to_vertex: Float[Array, "3"],
|
|
48
|
+
object_origins: Float[Array, "num_objects 3"],
|
|
49
|
+
object_vectors: Float[Array, "num_objects num_dims 3"],
|
|
50
|
+
steps: int,
|
|
51
|
+
optimizer: optax.GradientTransformationExtraArgs,
|
|
52
|
+
) -> Float[Array, "num_objects 3"]:
|
|
53
|
+
dtype = jnp.result_type(from_vertex, to_vertex, object_origins, object_vectors)
|
|
54
|
+
x_0 = jnp.zeros(object_vectors.shape[0] * object_vectors.shape[1], dtype=dtype)
|
|
55
|
+
opt_state = optimizer.init(x_0)
|
|
56
|
+
value_and_grad = optax.value_and_grad_from_state(_loss)
|
|
57
|
+
|
|
58
|
+
@no_type_check
|
|
59
|
+
def update(
|
|
60
|
+
state: tuple[Float[Array, " num_unknowns"], Any],
|
|
61
|
+
_: None,
|
|
62
|
+
) -> tuple[tuple[Float[Array, " num_unknowns"], Any], float | Float[Array, " "]]:
|
|
63
|
+
x, opt_state = state
|
|
64
|
+
loss_value, grad = value_and_grad(
|
|
65
|
+
x, from_vertex, to_vertex, object_origins, object_vectors, state=opt_state
|
|
66
|
+
)
|
|
67
|
+
updates, opt_state = optimizer.update(
|
|
68
|
+
grad,
|
|
69
|
+
opt_state,
|
|
70
|
+
x,
|
|
71
|
+
value=loss_value,
|
|
72
|
+
grad=grad,
|
|
73
|
+
value_fn=_loss,
|
|
74
|
+
from_=from_vertex,
|
|
75
|
+
to=to_vertex,
|
|
76
|
+
o=object_origins,
|
|
77
|
+
v=object_vectors,
|
|
78
|
+
)
|
|
79
|
+
x = optax.apply_updates(x, updates)
|
|
80
|
+
return (x, opt_state), loss_value
|
|
81
|
+
|
|
82
|
+
(x, _), _ = jax.lax.scan(update, (x_0, opt_state), length=steps)
|
|
83
|
+
return _param_to_xyz(x, object_origins, object_vectors)
|
|
84
|
+
|
|
85
|
+
|
|
48
86
|
@eqx.filter_jit
|
|
49
87
|
def fermat_path_on_linear_objects(
|
|
50
88
|
from_vertices: Float[ArrayLike, "*#batch 3"],
|
|
@@ -176,90 +214,31 @@ def fermat_path_on_linear_objects(
|
|
|
176
214
|
object_origins = jnp.asarray(object_origins)
|
|
177
215
|
object_vectors = jnp.asarray(object_vectors)
|
|
178
216
|
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
object_origins.shape[:-2],
|
|
183
|
-
object_vectors.shape[:-3],
|
|
184
|
-
)
|
|
185
|
-
|
|
186
|
-
num_objects = object_origins.shape[-2]
|
|
187
|
-
|
|
188
|
-
if num_objects == 0:
|
|
217
|
+
if (num_objects := object_origins.shape[-2]) == 0 or (
|
|
218
|
+
object_vectors.shape[-2] == 0
|
|
219
|
+
):
|
|
189
220
|
# If there are no objects, return empty paths.
|
|
190
|
-
|
|
191
|
-
|
|
221
|
+
# If there are no dimensions, return origins.
|
|
222
|
+
batch = jnp.broadcast_shapes(
|
|
223
|
+
from_vertices.shape[:-1],
|
|
224
|
+
to_vertices.shape[:-1],
|
|
225
|
+
object_origins.shape[:-2],
|
|
226
|
+
object_vectors.shape[:-3],
|
|
192
227
|
)
|
|
193
|
-
return jnp.empty((*batch, 0, 3), dtype=dtype)
|
|
194
|
-
|
|
195
|
-
num_dims = object_vectors.shape[-2]
|
|
196
|
-
|
|
197
|
-
if num_dims == 0:
|
|
198
|
-
# If there are no dimension, return origins.
|
|
199
228
|
dtype = jnp.result_type(
|
|
200
229
|
from_vertices, to_vertices, object_origins, object_vectors
|
|
201
230
|
)
|
|
231
|
+
if num_objects == 0:
|
|
232
|
+
return jnp.empty((*batch, 0, 3), dtype=dtype)
|
|
202
233
|
return jnp.broadcast_to(object_origins, (*batch, num_objects, 3)).astype(dtype)
|
|
203
234
|
|
|
204
|
-
num_unknowns = num_objects * num_dims
|
|
205
|
-
|
|
206
|
-
objective = _loss
|
|
207
|
-
|
|
208
235
|
optimizer = optimizer if optimizer is not None else optax.lbfgs()
|
|
209
236
|
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
v: Float[Array, "num_objects num_dims 3"],
|
|
216
|
-
) -> Float[Array, "num_objects 3"]:
|
|
217
|
-
opt_state = optimizer.init(x_0)
|
|
218
|
-
|
|
219
|
-
value_and_grad = optax.value_and_grad_from_state(objective)
|
|
220
|
-
|
|
221
|
-
@no_type_check
|
|
222
|
-
def update(
|
|
223
|
-
state: tuple[Float[Array, "*batch num_unknowns"], Any],
|
|
224
|
-
_: None,
|
|
225
|
-
) -> tuple[
|
|
226
|
-
tuple[Float[Array, "*batch num_unknowns"], Any], float | Float[Array, " "]
|
|
227
|
-
]:
|
|
228
|
-
x, opt_state = state
|
|
229
|
-
loss_value, grad = value_and_grad(x, from_, to, o, v, state=opt_state)
|
|
230
|
-
updates, opt_state = optimizer.update(
|
|
231
|
-
grad,
|
|
232
|
-
opt_state,
|
|
233
|
-
x,
|
|
234
|
-
value=loss_value,
|
|
235
|
-
grad=grad,
|
|
236
|
-
value_fn=objective,
|
|
237
|
-
from_=from_,
|
|
238
|
-
to=to,
|
|
239
|
-
o=o,
|
|
240
|
-
v=v,
|
|
241
|
-
)
|
|
242
|
-
x = optax.apply_updates(x, updates)
|
|
243
|
-
return (x, opt_state), loss_value
|
|
244
|
-
|
|
245
|
-
(x, _), _ = jax.lax.scan(update, (x_0, opt_state), length=steps)
|
|
246
|
-
return _param_to_xyz(x, o, v)
|
|
247
|
-
|
|
248
|
-
x_0 = jnp.zeros((*batch, num_unknowns))
|
|
249
|
-
|
|
250
|
-
for i in reversed(range(len(batch))):
|
|
251
|
-
minimize = jax.vmap(
|
|
252
|
-
minimize,
|
|
253
|
-
in_axes=(
|
|
254
|
-
0,
|
|
255
|
-
0 if from_vertices.ndim > i + 1 else None,
|
|
256
|
-
0 if to_vertices.ndim > i + 1 else None,
|
|
257
|
-
0 if object_origins.ndim > i + 2 else None,
|
|
258
|
-
0 if object_vectors.ndim > i + 3 else None,
|
|
259
|
-
),
|
|
260
|
-
)
|
|
261
|
-
|
|
262
|
-
return minimize(x_0, from_vertices, to_vertices, object_origins, object_vectors)
|
|
237
|
+
return jnp.vectorize(
|
|
238
|
+
_fermat_path_on_linear_objects,
|
|
239
|
+
excluded={4, 5},
|
|
240
|
+
signature="(3),(3),(n,3),(n,d,3)->(n,3)",
|
|
241
|
+
)(from_vertices, to_vertices, object_origins, object_vectors, steps, optimizer)
|
|
263
242
|
|
|
264
243
|
|
|
265
244
|
@eqx.filter_jit
|
|
@@ -135,6 +135,76 @@ def intersection_of_rays_with_planes(
|
|
|
135
135
|
)
|
|
136
136
|
|
|
137
137
|
|
|
138
|
+
def _forward(
|
|
139
|
+
previous_image: Float[Array, "3"],
|
|
140
|
+
mirror_vertex_and_normal: tuple[Float[Array, "3"], Float[Array, "3"]],
|
|
141
|
+
) -> tuple[Float[Array, "3"], Float[Array, "3"]]:
|
|
142
|
+
# Perform forward pass on vertices by computing consecutive images.
|
|
143
|
+
mirror_vertex, mirror_normal = mirror_vertex_and_normal
|
|
144
|
+
image = image_of_vertices_with_respect_to_mirrors(
|
|
145
|
+
previous_image,
|
|
146
|
+
mirror_vertex,
|
|
147
|
+
mirror_normal,
|
|
148
|
+
)
|
|
149
|
+
return image, image
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
def _backward(
|
|
153
|
+
previous_intersection: Float[Array, "3"],
|
|
154
|
+
mirror_vertex_normal_and_image: tuple[
|
|
155
|
+
Float[Array, "3"],
|
|
156
|
+
Float[Array, "3"],
|
|
157
|
+
Float[Array, "3"],
|
|
158
|
+
],
|
|
159
|
+
) -> tuple[Float[Array, "3"], Float[Array, "3"]]:
|
|
160
|
+
# Perform backward pass on images by computing the intersection with mirrors.
|
|
161
|
+
mirror_vertex, mirror_normal, image = mirror_vertex_normal_and_image
|
|
162
|
+
|
|
163
|
+
# We avoid NaNs (caused by subtraction of two infinities) by replacing
|
|
164
|
+
# previous_intersections with zeros when they are infinite.
|
|
165
|
+
no_previous_intersection = jnp.isinf(previous_intersection)
|
|
166
|
+
previous_intersection = jnp.where(
|
|
167
|
+
no_previous_intersection,
|
|
168
|
+
jnp.zeros_like(previous_intersection),
|
|
169
|
+
previous_intersection,
|
|
170
|
+
)
|
|
171
|
+
intersection = intersection_of_rays_with_planes(
|
|
172
|
+
previous_intersection,
|
|
173
|
+
image - previous_intersection,
|
|
174
|
+
mirror_vertex,
|
|
175
|
+
mirror_normal,
|
|
176
|
+
)
|
|
177
|
+
intersection: Array = jnp.where(
|
|
178
|
+
no_previous_intersection,
|
|
179
|
+
jnp.full_like(intersection, jnp.inf),
|
|
180
|
+
intersection,
|
|
181
|
+
)
|
|
182
|
+
return intersection, intersection
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
def _image_method(
|
|
186
|
+
from_vertex: Float[Array, "3"],
|
|
187
|
+
to_vertex: Float[Array, "3"],
|
|
188
|
+
mirror_vertices: Float[Array, "num_mirrors 3"],
|
|
189
|
+
mirror_normals: Float[Array, "num_mirrors 3"],
|
|
190
|
+
) -> Float[Array, "num_mirrors 3"]:
|
|
191
|
+
_, images = jax.lax.scan(
|
|
192
|
+
_forward,
|
|
193
|
+
init=from_vertex,
|
|
194
|
+
xs=(mirror_vertices, mirror_normals),
|
|
195
|
+
unroll=True,
|
|
196
|
+
)
|
|
197
|
+
_, paths = jax.lax.scan(
|
|
198
|
+
_backward,
|
|
199
|
+
init=to_vertex,
|
|
200
|
+
xs=(mirror_vertices, mirror_normals, images),
|
|
201
|
+
reverse=True,
|
|
202
|
+
unroll=True,
|
|
203
|
+
)
|
|
204
|
+
|
|
205
|
+
return paths
|
|
206
|
+
|
|
207
|
+
|
|
138
208
|
@jax.jit
|
|
139
209
|
def image_method(
|
|
140
210
|
from_vertices: Float[ArrayLike, "*#batch 3"],
|
|
@@ -278,93 +348,23 @@ def image_method(
|
|
|
278
348
|
mirror_vertices = jnp.asarray(mirror_vertices)
|
|
279
349
|
mirror_normals = jnp.asarray(mirror_normals)
|
|
280
350
|
|
|
281
|
-
# Put 'num_mirrors' axis as leading axis
|
|
282
|
-
mirror_vertices = jnp.moveaxis(mirror_vertices, -2, 0)
|
|
283
|
-
mirror_normals = jnp.moveaxis(mirror_normals, -2, 0)
|
|
284
|
-
|
|
285
|
-
# Broadcast scan carries, because shapes cannot differ between input and output
|
|
286
|
-
batch_and_3 = jnp.broadcast_shapes(
|
|
287
|
-
from_vertices.shape,
|
|
288
|
-
to_vertices.shape,
|
|
289
|
-
mirror_vertices.shape[1:],
|
|
290
|
-
mirror_normals.shape[1:],
|
|
291
|
-
)
|
|
292
|
-
|
|
293
351
|
if mirror_vertices.shape[0] == 0:
|
|
294
352
|
# If there are no mirrors, return empty array.
|
|
295
|
-
|
|
353
|
+
batch = jnp.broadcast_shapes(
|
|
354
|
+
from_vertices.shape[:-1],
|
|
355
|
+
to_vertices.shape[:-1],
|
|
356
|
+
mirror_vertices.shape[:-2],
|
|
357
|
+
mirror_normals.shape[:-2],
|
|
358
|
+
)
|
|
296
359
|
dtype = jnp.result_type(
|
|
297
360
|
from_vertices, to_vertices, mirror_vertices, mirror_normals
|
|
298
361
|
)
|
|
299
362
|
return jnp.empty((*batch, 0, 3), dtype=dtype)
|
|
300
363
|
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
previous_images: Float[Array, "*batch 3"],
|
|
306
|
-
mirror_vertices_and_normals: tuple[
|
|
307
|
-
Float[Array, "*#batch 3"], Float[Array, "*#batch 3"]
|
|
308
|
-
],
|
|
309
|
-
) -> tuple[Float[Array, "*batch 3"], Float[Array, "*batch 3"]]:
|
|
310
|
-
# ruff: noqa: DOC201
|
|
311
|
-
"""Perform forward pass on vertices by computing consecutive images."""
|
|
312
|
-
mirror_vertices, mirror_normals = mirror_vertices_and_normals
|
|
313
|
-
images = image_of_vertices_with_respect_to_mirrors(
|
|
314
|
-
previous_images,
|
|
315
|
-
mirror_vertices,
|
|
316
|
-
mirror_normals,
|
|
317
|
-
)
|
|
318
|
-
return images, images
|
|
319
|
-
|
|
320
|
-
def backward(
|
|
321
|
-
previous_intersections: Float[Array, "*batch 3"],
|
|
322
|
-
mirror_vertices_normals_and_images: tuple[
|
|
323
|
-
Float[Array, "*#batch 3"],
|
|
324
|
-
Float[Array, "*#batch 3"],
|
|
325
|
-
Float[Array, "*#batch 3"],
|
|
326
|
-
],
|
|
327
|
-
) -> tuple[Float[Array, "*batch 3"], Float[Array, "*batch 3"]]:
|
|
328
|
-
# ruff: noqa: DOC201
|
|
329
|
-
"""Perform backward pass on images by computing the intersection with mirrors."""
|
|
330
|
-
mirror_vertices, mirror_normals, images = mirror_vertices_normals_and_images
|
|
331
|
-
|
|
332
|
-
# We avoid NaNs (caused by subtraction of two infinities) by replacing
|
|
333
|
-
# previous_intersections with zeros when they are infinite.
|
|
334
|
-
no_previous_intersections = jnp.isinf(previous_intersections)
|
|
335
|
-
previous_intersections = jnp.where(
|
|
336
|
-
no_previous_intersections,
|
|
337
|
-
jnp.zeros_like(previous_intersections),
|
|
338
|
-
previous_intersections,
|
|
339
|
-
)
|
|
340
|
-
intersections = intersection_of_rays_with_planes(
|
|
341
|
-
previous_intersections,
|
|
342
|
-
images - previous_intersections,
|
|
343
|
-
mirror_vertices,
|
|
344
|
-
mirror_normals,
|
|
345
|
-
)
|
|
346
|
-
intersections: Array = jnp.where(
|
|
347
|
-
no_previous_intersections,
|
|
348
|
-
jnp.full_like(intersections, jnp.inf),
|
|
349
|
-
intersections,
|
|
350
|
-
)
|
|
351
|
-
return intersections, intersections
|
|
352
|
-
|
|
353
|
-
_, images = jax.lax.scan(
|
|
354
|
-
forward,
|
|
355
|
-
init=from_vertices,
|
|
356
|
-
xs=(mirror_vertices, mirror_normals),
|
|
357
|
-
unroll=True,
|
|
358
|
-
)
|
|
359
|
-
_, paths = jax.lax.scan(
|
|
360
|
-
backward,
|
|
361
|
-
init=to_vertices,
|
|
362
|
-
xs=(mirror_vertices, mirror_normals, images),
|
|
363
|
-
reverse=True,
|
|
364
|
-
unroll=True,
|
|
365
|
-
)
|
|
366
|
-
|
|
367
|
-
return jnp.moveaxis(paths, 0, -2) # Put 'num_mirrors' axis at the end
|
|
364
|
+
return jnp.vectorize(
|
|
365
|
+
_image_method,
|
|
366
|
+
signature="(3),(3),(n,3),(n,3)->(n,3)",
|
|
367
|
+
)(from_vertices, to_vertices, mirror_vertices, mirror_normals)
|
|
368
368
|
|
|
369
369
|
|
|
370
370
|
@overload
|
|
@@ -355,6 +355,7 @@ def rays_intersect_any_triangle(
|
|
|
355
355
|
*,
|
|
356
356
|
hit_tol: Float[ArrayLike, " "] | None = None,
|
|
357
357
|
smoothing_factor: None = None,
|
|
358
|
+
batch_size: int = 1,
|
|
358
359
|
**kwargs: Any,
|
|
359
360
|
) -> Bool[Array, " *batch"]: ...
|
|
360
361
|
|
|
@@ -368,6 +369,7 @@ def rays_intersect_any_triangle(
|
|
|
368
369
|
*,
|
|
369
370
|
hit_tol: Float[ArrayLike, " "] | None = None,
|
|
370
371
|
smoothing_factor: Float[ArrayLike, " "],
|
|
372
|
+
batch_size: int = 1,
|
|
371
373
|
**kwargs: Any,
|
|
372
374
|
) -> Float[Array, " *batch"]: ...
|
|
373
375
|
|
|
@@ -381,6 +383,7 @@ def rays_intersect_any_triangle(
|
|
|
381
383
|
*,
|
|
382
384
|
hit_tol: Float[ArrayLike, " "] | None = None,
|
|
383
385
|
smoothing_factor: Float[ArrayLike, " "] | None = None,
|
|
386
|
+
batch_size: int = 1,
|
|
384
387
|
**kwargs: Any,
|
|
385
388
|
) -> Bool[Array, " *batch"] | Float[Array, " *batch"]:
|
|
386
389
|
"""
|
|
@@ -388,7 +391,7 @@ def rays_intersect_any_triangle(
|
|
|
388
391
|
|
|
389
392
|
This function should be used when allocating an array of size
|
|
390
393
|
``*batch num_triangles 3`` (or bigger) is not possible, and you are only interested in
|
|
391
|
-
checking if at least one of the triangles is
|
|
394
|
+
checking if at least one of the triangles is intersected.
|
|
392
395
|
|
|
393
396
|
A triangle is considered to be intersected if
|
|
394
397
|
``t < (1 - hit_tol) & hit`` evaluates to :data:`True`.
|
|
@@ -416,6 +419,11 @@ def rays_intersect_any_triangle(
|
|
|
416
419
|
between 0 (:data:`False`) and 1 (:data:`True`).
|
|
417
420
|
|
|
418
421
|
For more details, refer to :ref:`smoothing`.
|
|
422
|
+
batch_size: The number of triangles to process in a single batch.
|
|
423
|
+
This allows to make a trade-off between memory usage and performance.
|
|
424
|
+
|
|
425
|
+
The batch size is automatically adjusted to be the minimum of the number of triangles
|
|
426
|
+
and the specified batch size.
|
|
419
427
|
kwargs: Keyword arguments passed to
|
|
420
428
|
:func:`rays_intersect_triangles`.
|
|
421
429
|
|
|
@@ -432,76 +440,99 @@ def rays_intersect_any_triangle(
|
|
|
432
440
|
|
|
433
441
|
hit_threshold = 1.0 - jnp.asarray(hit_tol)
|
|
434
442
|
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
443
|
+
num_triangles = triangle_vertices.shape[-3]
|
|
444
|
+
batch_size = max(min(batch_size, num_triangles), 1)
|
|
445
|
+
num_batches, rem = divmod(num_triangles, batch_size)
|
|
446
|
+
|
|
447
|
+
if active_triangles is not None:
|
|
448
|
+
active_triangles = jnp.asarray(active_triangles)
|
|
449
|
+
elif rem > 0:
|
|
450
|
+
active_triangles = jnp.ones((num_triangles,), dtype=bool)
|
|
451
|
+
|
|
452
|
+
# Pad the 'num_triangles' dimension to have a multiple of 'batch_size'
|
|
453
|
+
if rem > 0:
|
|
454
|
+
# note: Pyright is not able to infer the type of 'active_triangles' here
|
|
455
|
+
# and complains about it being 'Optional'.
|
|
456
|
+
num_batches += 1
|
|
457
|
+
pad_width = batch_size - rem
|
|
458
|
+
triangle_vertices = jnp.pad(
|
|
459
|
+
triangle_vertices,
|
|
460
|
+
[(0, 0) for _ in range(triangle_vertices.ndim - 3)]
|
|
461
|
+
+ [(0, pad_width), (0, 0), (0, 0)],
|
|
462
|
+
constant_values=0.0,
|
|
463
|
+
)
|
|
464
|
+
active_triangles = jnp.pad(
|
|
465
|
+
active_triangles, # type: ignore[reportArgumentType]
|
|
466
|
+
[(0, 0) for _ in range(active_triangles.ndim - 1)] + [(0, pad_width)], # type: ignore[reportOptionalMemberAccess]
|
|
467
|
+
constant_values=False,
|
|
468
|
+
)
|
|
442
469
|
|
|
470
|
+
# Combine the batch dimensions
|
|
443
471
|
batch = jnp.broadcast_shapes(
|
|
444
472
|
ray_origins.shape[:-1],
|
|
445
473
|
ray_directions.shape[:-1],
|
|
446
|
-
triangle_vertices.shape[
|
|
474
|
+
triangle_vertices.shape[:-3],
|
|
475
|
+
active_triangles.shape[:-1] if active_triangles is not None else (),
|
|
447
476
|
)
|
|
448
477
|
|
|
449
|
-
if
|
|
478
|
+
if num_triangles == 0:
|
|
479
|
+
# If there are no triangles, there are no intersections
|
|
480
|
+
return (
|
|
481
|
+
jnp.zeros(
|
|
482
|
+
batch,
|
|
483
|
+
dtype=jnp.result_type(ray_origins, ray_directions, triangle_vertices),
|
|
484
|
+
)
|
|
485
|
+
if smoothing_factor is not None
|
|
486
|
+
else jnp.zeros(batch, dtype=bool)
|
|
487
|
+
)
|
|
450
488
|
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
triangle_vertices,
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
smoothing_factor=smoothing_factor,
|
|
463
|
-
**kwargs,
|
|
489
|
+
def body_fun(
|
|
490
|
+
batch_index: Int[Array, " "],
|
|
491
|
+
intersect: Bool[Array, " *batch"] | Float[Array, " *batch"],
|
|
492
|
+
) -> Bool[Array, " *batch"] | Float[Array, " *batch"]:
|
|
493
|
+
start_index = batch_index * batch_size
|
|
494
|
+
batch_of_triangle_vertices = jax.lax.dynamic_slice_in_dim(
|
|
495
|
+
triangle_vertices, start_index, batch_size, axis=-3
|
|
496
|
+
)
|
|
497
|
+
batch_of_active_triangles = (
|
|
498
|
+
jax.lax.dynamic_slice_in_dim(
|
|
499
|
+
active_triangles, start_index, batch_size, axis=-1
|
|
464
500
|
)
|
|
465
|
-
|
|
466
|
-
|
|
501
|
+
if active_triangles is not None
|
|
502
|
+
else None
|
|
503
|
+
)
|
|
504
|
+
t, hit = rays_intersect_triangles(
|
|
505
|
+
ray_origins[..., None, :],
|
|
506
|
+
ray_directions[..., None, :],
|
|
507
|
+
batch_of_triangle_vertices,
|
|
508
|
+
smoothing_factor=smoothing_factor,
|
|
509
|
+
**kwargs,
|
|
510
|
+
)
|
|
511
|
+
|
|
512
|
+
if smoothing_factor is not None:
|
|
513
|
+
return (
|
|
467
514
|
intersect
|
|
468
515
|
+ jnp.minimum(
|
|
469
516
|
smoothing_function(hit_threshold - t, smoothing_factor), hit
|
|
470
|
-
)
|
|
471
|
-
).clip(
|
|
472
|
-
max=1.0,
|
|
473
|
-
)
|
|
474
|
-
return intersect, None
|
|
475
|
-
|
|
476
|
-
else:
|
|
517
|
+
).sum(axis=-1, where=batch_of_active_triangles)
|
|
518
|
+
).clip(max=1.0)
|
|
477
519
|
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
Float[Array, "*#batch 3 3"], Bool[Array, "*#batch"] | None
|
|
482
|
-
],
|
|
483
|
-
) -> tuple[Bool[Array, " *batch"], None]:
|
|
484
|
-
triangle_vertices, active_triangles = triangle_vertices_and_active
|
|
485
|
-
t, hit = rays_intersect_triangles(
|
|
486
|
-
ray_origins,
|
|
487
|
-
ray_directions,
|
|
488
|
-
triangle_vertices,
|
|
489
|
-
**kwargs,
|
|
490
|
-
)
|
|
491
|
-
intersect |= (t < hit_threshold) & hit & active_triangles
|
|
492
|
-
return intersect, None
|
|
520
|
+
return intersect | ((t < hit_threshold) & hit).any(
|
|
521
|
+
axis=-1, where=batch_of_active_triangles
|
|
522
|
+
)
|
|
493
523
|
|
|
494
|
-
|
|
524
|
+
init_val = (
|
|
495
525
|
jnp.zeros(batch)
|
|
496
526
|
if smoothing_factor is not None
|
|
497
527
|
else jnp.zeros(batch, dtype=jnp.bool)
|
|
498
528
|
)
|
|
499
529
|
|
|
500
|
-
return jax.lax.
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
530
|
+
return jax.lax.fori_loop(
|
|
531
|
+
0,
|
|
532
|
+
num_batches,
|
|
533
|
+
body_fun,
|
|
534
|
+
init_val=init_val,
|
|
535
|
+
)
|
|
505
536
|
|
|
506
537
|
|
|
507
538
|
@eqx.filter_jit
|
|
@@ -510,6 +541,7 @@ def triangles_visible_from_vertices(
|
|
|
510
541
|
triangle_vertices: Float[ArrayLike, "*#batch num_triangles 3 3"],
|
|
511
542
|
active_triangles: Bool[ArrayLike, "*#batch num_triangles"] | None = None,
|
|
512
543
|
num_rays: int = int(1e6),
|
|
544
|
+
batch_size: int = 4096,
|
|
513
545
|
**kwargs: Any,
|
|
514
546
|
) -> Bool[Array, "*batch num_triangles"]:
|
|
515
547
|
"""
|
|
@@ -536,6 +568,14 @@ def triangles_visible_from_vertices(
|
|
|
536
568
|
num_rays: The number of rays to launch.
|
|
537
569
|
|
|
538
570
|
The larger, the more accurate.
|
|
571
|
+
|
|
572
|
+
In practice, this value is automatically adjusted to be a multiple of
|
|
573
|
+
``batch_size`` (increased if necessary).
|
|
574
|
+
batch_size: The number of rays to process in a single batch.
|
|
575
|
+
This allows to make a trade-off between memory usage and performance.
|
|
576
|
+
|
|
577
|
+
The batch size is automatically adjusted to be the minimum of the number of rays
|
|
578
|
+
and the specified batch size.
|
|
539
579
|
kwargs: Keyword arguments passed to
|
|
540
580
|
:func:`rays_intersect_triangles`.
|
|
541
581
|
|
|
@@ -547,6 +587,7 @@ def triangles_visible_from_vertices(
|
|
|
547
587
|
visible from a given transmitter, coloring them in dark gray.
|
|
548
588
|
|
|
549
589
|
.. plotly::
|
|
590
|
+
:context: reset
|
|
550
591
|
|
|
551
592
|
>>> import equinox as eqx
|
|
552
593
|
>>> from differt.rt import (
|
|
@@ -576,57 +617,126 @@ def triangles_visible_from_vertices(
|
|
|
576
617
|
... )
|
|
577
618
|
>>> fig = scene.plot(backend="plotly")
|
|
578
619
|
>>> fig # doctest: +SKIP
|
|
620
|
+
|
|
621
|
+
In this example, a receiver is placed at the opposite side of the street canyon,
|
|
622
|
+
and its visible triangles are colored in blue. Triangles that are visible from both
|
|
623
|
+
the transmitter and the receiver are colored in yellow.
|
|
624
|
+
|
|
625
|
+
.. plotly::
|
|
626
|
+
:context:
|
|
627
|
+
|
|
628
|
+
>>> scene = eqx.tree_at(
|
|
629
|
+
... lambda s: s.receivers, scene, jnp.array([33, 0, 1.5])
|
|
630
|
+
... )
|
|
631
|
+
>>> visible_triangles = triangles_visible_from_vertices(
|
|
632
|
+
... jnp.stack((scene.transmitters, scene.receivers)),
|
|
633
|
+
... scene.mesh.triangle_vertices,
|
|
634
|
+
... )
|
|
635
|
+
>>> triangles_visible_from_tx = visible_triangles[0, :]
|
|
636
|
+
>>> triangles_visible_from_rx = visible_triangles[1, :]
|
|
637
|
+
>>> visible_by_tx_color = jnp.array([0.2, 0.2, 0.2])
|
|
638
|
+
>>> visible_by_rx_color = jnp.array([0.2, 0.8, 0.2])
|
|
639
|
+
>>> visible_by_both_color = jnp.array([0.8, 0.8, 0.2])
|
|
640
|
+
>>> scene = eqx.tree_at(
|
|
641
|
+
... lambda s: s.mesh.face_colors,
|
|
642
|
+
... scene,
|
|
643
|
+
... scene.mesh.face_colors.at[triangles_visible_from_tx, :].set(
|
|
644
|
+
... visible_by_tx_color
|
|
645
|
+
... ),
|
|
646
|
+
... )
|
|
647
|
+
>>> scene = eqx.tree_at(
|
|
648
|
+
... lambda s: s.mesh.face_colors,
|
|
649
|
+
... scene,
|
|
650
|
+
... scene.mesh.face_colors.at[triangles_visible_from_rx, :].set(
|
|
651
|
+
... visible_by_rx_color
|
|
652
|
+
... ),
|
|
653
|
+
... )
|
|
654
|
+
>>> scene = eqx.tree_at(
|
|
655
|
+
... lambda s: s.mesh.face_colors,
|
|
656
|
+
... scene,
|
|
657
|
+
... scene.mesh.face_colors.at[
|
|
658
|
+
... triangles_visible_from_tx & triangles_visible_from_rx, :
|
|
659
|
+
... ].set(visible_by_both_color),
|
|
660
|
+
... )
|
|
661
|
+
>>> fig = scene.plot(backend="plotly")
|
|
662
|
+
>>> fig # doctest: +SKIP
|
|
579
663
|
"""
|
|
580
664
|
vertices = jnp.asarray(vertices)
|
|
581
665
|
triangle_vertices = jnp.asarray(triangle_vertices)
|
|
582
666
|
|
|
667
|
+
if active_triangles is not None:
|
|
668
|
+
active_triangles = jnp.asarray(active_triangles)
|
|
669
|
+
|
|
583
670
|
# [*batch 3]
|
|
584
671
|
ray_origins = vertices
|
|
585
672
|
|
|
586
|
-
# [2 3]
|
|
587
|
-
|
|
588
|
-
# TODO: compute frustum for each batch
|
|
589
|
-
# TODO: handle 'active_triangles'
|
|
590
|
-
frustum = viewing_frustum(
|
|
673
|
+
# [*batch 2 3]
|
|
674
|
+
frustum = jnp.vectorize(viewing_frustum, excluded={1}, signature="(3)->(2,3)")(
|
|
591
675
|
ray_origins,
|
|
592
676
|
triangle_vertices.reshape(*triangle_vertices.shape[:-3], -1, 3),
|
|
593
|
-
reduce=True,
|
|
594
677
|
)
|
|
595
678
|
|
|
596
|
-
#
|
|
597
|
-
|
|
679
|
+
# Make the 'num_rays' be a multiple of 'batch_size'
|
|
680
|
+
batch_size = min(batch_size, num_rays)
|
|
681
|
+
num_batches, rem = divmod(num_rays, batch_size)
|
|
598
682
|
|
|
599
|
-
|
|
683
|
+
if rem > 0:
|
|
684
|
+
num_batches += 1
|
|
685
|
+
num_rays = num_batches * batch_size
|
|
600
686
|
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
687
|
+
# [*batch num_rays 3]
|
|
688
|
+
ray_directions = jnp.vectorize(
|
|
689
|
+
lambda n, frustum: fibonacci_lattice(n, frustum=frustum),
|
|
690
|
+
excluded={0},
|
|
691
|
+
signature="(2,3)->(n,3)",
|
|
692
|
+
)(num_rays, frustum)
|
|
693
|
+
|
|
694
|
+
# Combine the batch dimensions
|
|
695
|
+
batch = jnp.broadcast_shapes(
|
|
696
|
+
ray_origins.shape[:-2],
|
|
697
|
+
ray_directions.shape[:-2],
|
|
698
|
+
triangle_vertices.shape[:-3],
|
|
699
|
+
active_triangles.shape[:-1] if active_triangles is not None else (),
|
|
700
|
+
)
|
|
701
|
+
|
|
702
|
+
def body_fun(
|
|
703
|
+
batch_index: Int[Array, " "],
|
|
704
|
+
visible_triangles: Bool[Array, "*batch num_triangles"],
|
|
705
|
+
) -> Bool[Array, "*batch num_triangles"]:
|
|
706
|
+
start_index = batch_index * batch_size
|
|
707
|
+
batch_of_ray_directions = jax.lax.dynamic_slice_in_dim(
|
|
708
|
+
ray_directions, start_index, batch_size, axis=-2
|
|
709
|
+
)
|
|
605
710
|
t, hit = rays_intersect_triangles(
|
|
606
|
-
ray_origins[..., None, :],
|
|
607
|
-
|
|
608
|
-
triangle_vertices,
|
|
711
|
+
ray_origins[..., None, None, :],
|
|
712
|
+
batch_of_ray_directions[..., None, :, :],
|
|
713
|
+
triangle_vertices[..., None, :, :],
|
|
609
714
|
**kwargs,
|
|
610
715
|
)
|
|
611
716
|
# A triangle is visible if it is the first triangle to be intersected by a ray.
|
|
612
|
-
|
|
717
|
+
return visible_triangles | (
|
|
613
718
|
t
|
|
614
719
|
== jnp.min(
|
|
615
720
|
t,
|
|
616
|
-
axis=-
|
|
721
|
+
axis=-2,
|
|
617
722
|
keepdims=True,
|
|
618
723
|
initial=jnp.inf,
|
|
619
|
-
where=(
|
|
724
|
+
where=(
|
|
725
|
+
hit & active_triangles[..., None]
|
|
726
|
+
if active_triangles is not None
|
|
727
|
+
else hit
|
|
728
|
+
),
|
|
620
729
|
)
|
|
621
|
-
)
|
|
730
|
+
).any(axis=-1) # Any ray hitting the triangle
|
|
622
731
|
|
|
623
|
-
|
|
732
|
+
init_val = jnp.zeros((*batch, triangle_vertices.shape[-3]), dtype=jnp.bool)
|
|
624
733
|
|
|
625
|
-
return jax.lax.
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
734
|
+
return jax.lax.fori_loop(
|
|
735
|
+
0,
|
|
736
|
+
num_batches,
|
|
737
|
+
body_fun,
|
|
738
|
+
init_val=init_val,
|
|
739
|
+
)
|
|
630
740
|
|
|
631
741
|
|
|
632
742
|
@eqx.filter_jit
|
|
@@ -635,6 +745,7 @@ def first_triangles_hit_by_rays(
|
|
|
635
745
|
ray_directions: Float[ArrayLike, "*#batch 3"],
|
|
636
746
|
triangle_vertices: Float[ArrayLike, "*#batch num_triangles 3 3"],
|
|
637
747
|
active_triangles: Bool[ArrayLike, "*#batch num_triangles"] | None = None,
|
|
748
|
+
batch_size: int = 1,
|
|
638
749
|
**kwargs: Any,
|
|
639
750
|
) -> tuple[Int[Array, " *batch"], Float[Array, " *batch"]]:
|
|
640
751
|
"""
|
|
@@ -653,6 +764,11 @@ def first_triangles_hit_by_rays(
|
|
|
653
764
|
which triangles are active, i.e., should be considered for intersection.
|
|
654
765
|
|
|
655
766
|
If not specified, all triangles are considered active.
|
|
767
|
+
batch_size: The number of triangles to process in a single batch.
|
|
768
|
+
This allows to make a trade-off between memory usage and performance.
|
|
769
|
+
|
|
770
|
+
The batch size is automatically adjusted to be the minimum of the number of triangles
|
|
771
|
+
and the specified batch size.
|
|
656
772
|
kwargs: Keyword arguments passed to
|
|
657
773
|
:func:`rays_intersect_triangles`.
|
|
658
774
|
|
|
@@ -666,43 +782,106 @@ def first_triangles_hit_by_rays(
|
|
|
666
782
|
ray_directions = jnp.asarray(ray_directions)
|
|
667
783
|
triangle_vertices = jnp.asarray(triangle_vertices)
|
|
668
784
|
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
785
|
+
num_triangles = triangle_vertices.shape[-3]
|
|
786
|
+
batch_size = max(min(batch_size, num_triangles), 1)
|
|
787
|
+
num_batches, rem = divmod(num_triangles, batch_size)
|
|
788
|
+
|
|
789
|
+
if active_triangles is not None:
|
|
790
|
+
active_triangles = jnp.asarray(active_triangles)
|
|
791
|
+
elif rem > 0:
|
|
792
|
+
active_triangles = jnp.ones((num_triangles,), dtype=bool)
|
|
793
|
+
|
|
794
|
+
# Pad the 'num_triangles' dimension to have a multiple of 'batch_size'
|
|
795
|
+
if rem > 0:
|
|
796
|
+
# note: Pyright is not able to infer the type of 'active_triangles' here
|
|
797
|
+
# and complains about it being 'Optional'.
|
|
798
|
+
num_batches += 1
|
|
799
|
+
pad_width = batch_size - rem
|
|
800
|
+
triangle_vertices = jnp.pad(
|
|
801
|
+
triangle_vertices,
|
|
802
|
+
[(0, 0) for _ in range(triangle_vertices.ndim - 3)]
|
|
803
|
+
+ [(0, pad_width), (0, 0), (0, 0)],
|
|
804
|
+
constant_values=0.0,
|
|
805
|
+
)
|
|
806
|
+
active_triangles = jnp.pad(
|
|
807
|
+
active_triangles, # type: ignore[reportArgumentType]
|
|
808
|
+
[(0, 0) for _ in range(active_triangles.ndim - 1)] + [(0, pad_width)], # type: ignore[reportOptionalMemberAccess]
|
|
809
|
+
constant_values=False,
|
|
810
|
+
)
|
|
676
811
|
|
|
812
|
+
# Combine the batch dimensions
|
|
677
813
|
batch = jnp.broadcast_shapes(
|
|
678
814
|
ray_origins.shape[:-1],
|
|
679
815
|
ray_directions.shape[:-1],
|
|
680
|
-
triangle_vertices.shape[
|
|
816
|
+
triangle_vertices.shape[:-3],
|
|
817
|
+
active_triangles.shape[:-1] if active_triangles is not None else (),
|
|
681
818
|
)
|
|
682
819
|
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
820
|
+
if num_triangles == 0:
|
|
821
|
+
# If there are no triangles, there are no hits
|
|
822
|
+
return (
|
|
823
|
+
jnp.full(batch, -1, dtype=jnp.int32),
|
|
824
|
+
jnp.full(
|
|
825
|
+
batch,
|
|
826
|
+
jnp.inf,
|
|
827
|
+
dtype=jnp.result_type(ray_origins, ray_directions, triangle_vertices),
|
|
828
|
+
),
|
|
829
|
+
)
|
|
830
|
+
|
|
831
|
+
def body_fun(
|
|
832
|
+
batch_index: Int[Array, " "],
|
|
833
|
+
carry: tuple[Int[Array, " *batch"], Float[Array, " *batch"]],
|
|
834
|
+
) -> tuple[Int[Array, " *batch"], Float[Array, " *batch"]]:
|
|
835
|
+
start_index = batch_index * batch_size
|
|
836
|
+
batch_of_triangle_vertices = jax.lax.dynamic_slice_in_dim(
|
|
837
|
+
triangle_vertices, start_index, batch_size, axis=-3
|
|
838
|
+
)
|
|
839
|
+
batch_of_active_triangles = (
|
|
840
|
+
jax.lax.dynamic_slice_in_dim(
|
|
841
|
+
active_triangles, start_index, batch_size, axis=-1
|
|
842
|
+
)
|
|
843
|
+
if active_triangles is not None
|
|
844
|
+
else None
|
|
845
|
+
)
|
|
846
|
+
indices, t_hit = carry
|
|
693
847
|
t, hit = rays_intersect_triangles(
|
|
694
|
-
ray_origins,
|
|
695
|
-
ray_directions,
|
|
696
|
-
|
|
848
|
+
ray_origins[..., None, :],
|
|
849
|
+
ray_directions[..., None, :],
|
|
850
|
+
batch_of_triangle_vertices,
|
|
697
851
|
**kwargs,
|
|
698
852
|
)
|
|
699
|
-
t = jnp.where(
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
853
|
+
t = jnp.where(
|
|
854
|
+
(hit & batch_of_active_triangles) if active_triangles is not None else hit,
|
|
855
|
+
t,
|
|
856
|
+
jnp.inf,
|
|
857
|
+
)
|
|
858
|
+
# Find the indices of the triangles hit in this batch
|
|
859
|
+
batch_indices = jnp.argmin(
|
|
860
|
+
t,
|
|
861
|
+
axis=-1,
|
|
862
|
+
)
|
|
863
|
+
# Find the distance to the first triangle hit in this batch
|
|
864
|
+
batch_t = jnp.take_along_axis(
|
|
865
|
+
t,
|
|
866
|
+
batch_indices[..., None],
|
|
867
|
+
axis=-1,
|
|
868
|
+
).squeeze(axis=-1)
|
|
869
|
+
indices = jnp.where(batch_t < t_hit, batch_indices + start_index, indices)
|
|
870
|
+
t_hit = jnp.minimum(batch_t, t_hit)
|
|
871
|
+
return (indices, t_hit)
|
|
872
|
+
|
|
873
|
+
init_val = (
|
|
874
|
+
-jnp.ones(batch, dtype=jnp.int32),
|
|
875
|
+
jnp.full(
|
|
876
|
+
batch,
|
|
877
|
+
jnp.inf,
|
|
878
|
+
dtype=jnp.result_type(ray_origins, ray_directions, triangle_vertices),
|
|
879
|
+
),
|
|
880
|
+
)
|
|
881
|
+
|
|
882
|
+
return jax.lax.fori_loop(
|
|
883
|
+
0,
|
|
884
|
+
num_batches,
|
|
885
|
+
body_fun,
|
|
886
|
+
init_val=init_val,
|
|
887
|
+
)
|
|
@@ -52,7 +52,7 @@ else:
|
|
|
52
52
|
|
|
53
53
|
|
|
54
54
|
@eqx.filter_jit
|
|
55
|
-
def _compute_paths(
|
|
55
|
+
def _compute_paths(
|
|
56
56
|
mesh: TriangleMesh,
|
|
57
57
|
tx_vertices: Float[Array, "num_tx_vertices 3"],
|
|
58
58
|
rx_vertices: Float[Array, "num_rx_vertices 3"],
|
|
@@ -75,14 +75,22 @@ def _compute_paths( # noqa: PLR0915
|
|
|
75
75
|
num_rx_vertices = rx_vertices.shape[0]
|
|
76
76
|
num_path_candidates, order = path_candidates.shape
|
|
77
77
|
|
|
78
|
-
|
|
78
|
+
if mesh.assume_quads:
|
|
79
|
+
# [num_path_candidates 2*order 3 3]
|
|
80
|
+
path_candidates = jnp.repeat(path_candidates, 2, axis=-1)
|
|
81
|
+
path_candidates = path_candidates.at[..., 1::2].add(1) # Shift odd indices by 1
|
|
82
|
+
k = 2
|
|
83
|
+
else:
|
|
84
|
+
k = 1
|
|
85
|
+
|
|
86
|
+
# [num_path_candidates k*order 3]
|
|
79
87
|
triangles = jnp.take(mesh.triangles, path_candidates, axis=0).reshape(
|
|
80
|
-
num_path_candidates, order, 3
|
|
88
|
+
num_path_candidates, k * order, 3
|
|
81
89
|
) # reshape required if mesh is empty
|
|
82
90
|
|
|
83
|
-
# [num_path_candidates order 3 3]
|
|
91
|
+
# [num_path_candidates k*order 3 3]
|
|
84
92
|
triangle_vertices = jnp.take(mesh.vertices, triangles, axis=0).reshape(
|
|
85
|
-
num_path_candidates, order, 3, 3
|
|
93
|
+
num_path_candidates, k * order, 3, 3
|
|
86
94
|
) # reshape required if mesh is empty
|
|
87
95
|
|
|
88
96
|
if mesh.mask is not None:
|
|
@@ -92,30 +100,18 @@ def _compute_paths( # noqa: PLR0915
|
|
|
92
100
|
else:
|
|
93
101
|
active_rays = None
|
|
94
102
|
|
|
95
|
-
if mesh.assume_quads:
|
|
96
|
-
# [num_path_candidates order 2 3]
|
|
97
|
-
quads = jnp.take(
|
|
98
|
-
mesh.triangles,
|
|
99
|
-
jnp.stack((path_candidates, path_candidates + 1), axis=-1),
|
|
100
|
-
axis=0,
|
|
101
|
-
).reshape(num_path_candidates, order, 2, 3) # reshape required if mesh is empty
|
|
102
|
-
|
|
103
|
-
# [num_path_candidates order 2 3 3]
|
|
104
|
-
quad_vertices = jnp.take(mesh.vertices, quads, axis=0).reshape(
|
|
105
|
-
num_path_candidates, order, 2, 3, 3
|
|
106
|
-
) # reshape required if mesh is empty
|
|
107
|
-
else:
|
|
108
|
-
quad_vertices = None
|
|
109
|
-
|
|
110
103
|
# [num_path_candidates order 3]
|
|
111
104
|
mirror_vertices = triangle_vertices[
|
|
112
105
|
...,
|
|
106
|
+
:: (2 if mesh.assume_quads else 1),
|
|
113
107
|
0,
|
|
114
108
|
:,
|
|
115
109
|
] # Only one vertex per triangle is needed
|
|
116
110
|
|
|
117
111
|
# [num_path_candidates order 3]
|
|
118
|
-
mirror_normals = jnp.take(
|
|
112
|
+
mirror_normals = jnp.take(
|
|
113
|
+
mesh.normals, path_candidates[..., :: (2 if mesh.assume_quads else 1)], axis=0
|
|
114
|
+
)
|
|
119
115
|
|
|
120
116
|
def fun(
|
|
121
117
|
tx_vertices: Float[Array, "num_tx_vertices 3"],
|
|
@@ -159,23 +155,29 @@ def _compute_paths( # noqa: PLR0915
|
|
|
159
155
|
if smoothing_factor is not None:
|
|
160
156
|
inside_triangles = (
|
|
161
157
|
rays_intersect_triangles(
|
|
162
|
-
ray_origins[..., :-1,
|
|
163
|
-
ray_directions[..., :-1,
|
|
164
|
-
|
|
158
|
+
jnp.repeat(ray_origins[..., :-1, :], 2, axis=-2),
|
|
159
|
+
jnp.repeat(ray_directions[..., :-1, :], 2, axis=-2),
|
|
160
|
+
triangle_vertices,
|
|
165
161
|
epsilon=epsilon,
|
|
166
162
|
smoothing_factor=smoothing_factor,
|
|
167
163
|
)[1]
|
|
164
|
+
.reshape(
|
|
165
|
+
num_tx_vertices, num_rx_vertices, num_path_candidates, order, 2
|
|
166
|
+
)
|
|
168
167
|
.max(axis=-1, initial=0.0)
|
|
169
168
|
.min(axis=-1, initial=1.0)
|
|
170
169
|
) # Reduce on 'order' axis and on the two triangles (per quad)
|
|
171
170
|
else:
|
|
172
171
|
inside_triangles = (
|
|
173
172
|
rays_intersect_triangles(
|
|
174
|
-
ray_origins[..., :-1,
|
|
175
|
-
ray_directions[..., :-1,
|
|
176
|
-
|
|
173
|
+
jnp.repeat(ray_origins[..., :-1, :], 2, axis=-2),
|
|
174
|
+
jnp.repeat(ray_directions[..., :-1, :], 2, axis=-2),
|
|
175
|
+
triangle_vertices,
|
|
177
176
|
epsilon=epsilon,
|
|
178
177
|
)[1]
|
|
178
|
+
.reshape(
|
|
179
|
+
num_tx_vertices, num_rx_vertices, num_path_candidates, order, 2
|
|
180
|
+
)
|
|
179
181
|
.any(axis=-1)
|
|
180
182
|
.all(axis=-1)
|
|
181
183
|
) # Reduce on 'order' axis and on the two triangles (per quad)
|
|
@@ -224,6 +226,7 @@ def _compute_paths( # noqa: PLR0915
|
|
|
224
226
|
epsilon=epsilon,
|
|
225
227
|
hit_tol=hit_tol,
|
|
226
228
|
smoothing_factor=smoothing_factor,
|
|
229
|
+
batch_size=1024,
|
|
227
230
|
).max(axis=-1, initial=0.0) # Reduce on 'order'
|
|
228
231
|
else:
|
|
229
232
|
blocked = rays_intersect_any_triangle(
|
|
@@ -233,6 +236,7 @@ def _compute_paths( # noqa: PLR0915
|
|
|
233
236
|
active_triangles=mesh.mask,
|
|
234
237
|
epsilon=epsilon,
|
|
235
238
|
hit_tol=hit_tol,
|
|
239
|
+
batch_size=1024,
|
|
236
240
|
).any(axis=-1) # Reduce on 'order'
|
|
237
241
|
|
|
238
242
|
# 3.4 - Identify path segments that are too small (e.g., double-reflection inside an edge)
|
|
@@ -320,7 +324,7 @@ def _compute_paths( # noqa: PLR0915
|
|
|
320
324
|
(num_tx_vertices, num_rx_vertices, num_path_candidates, 1),
|
|
321
325
|
)
|
|
322
326
|
path_candidates = jnp.broadcast_to(
|
|
323
|
-
path_candidates,
|
|
327
|
+
path_candidates[:, ::k],
|
|
324
328
|
(
|
|
325
329
|
num_tx_vertices,
|
|
326
330
|
num_rx_vertices,
|
|
@@ -948,7 +952,7 @@ class TriangleScene(eqx.Module):
|
|
|
948
952
|
confidence_threshold: Float[ArrayLike, " "] = 0.5,
|
|
949
953
|
) -> SBRPaths: ...
|
|
950
954
|
|
|
951
|
-
def compute_paths(
|
|
955
|
+
def compute_paths(
|
|
952
956
|
self,
|
|
953
957
|
order: int | None = None,
|
|
954
958
|
*,
|
|
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
|