differt 0.0.21__tar.gz → 0.0.23__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.
Files changed (27) hide show
  1. {differt-0.0.21 → differt-0.0.23}/PKG-INFO +2 -2
  2. {differt-0.0.21 → differt-0.0.23}/pyproject.toml +1 -1
  3. {differt-0.0.21 → differt-0.0.23}/src/differt/geometry/paths.py +25 -26
  4. {differt-0.0.21 → differt-0.0.23}/src/differt/geometry/triangle_mesh.py +269 -35
  5. {differt-0.0.21 → differt-0.0.23}/src/differt/geometry/utils.py +52 -18
  6. {differt-0.0.21 → differt-0.0.23}/src/differt/plotting/__init__.py +6 -6
  7. {differt-0.0.21 → differt-0.0.23}/src/differt/plotting/_core.py +267 -86
  8. {differt-0.0.21 → differt-0.0.23}/src/differt/plotting/_utils.py +2 -2
  9. {differt-0.0.21 → differt-0.0.23}/src/differt/rt/utils.py +4 -4
  10. {differt-0.0.21 → differt-0.0.23}/src/differt/scene/triangle_scene.py +146 -19
  11. {differt-0.0.21 → differt-0.0.23}/src/differt/utils.py +25 -7
  12. {differt-0.0.21 → differt-0.0.23}/.gitignore +0 -0
  13. {differt-0.0.21 → differt-0.0.23}/LICENSE.md +0 -0
  14. {differt-0.0.21 → differt-0.0.23}/README.md +0 -0
  15. {differt-0.0.21 → differt-0.0.23}/src/differt/__init__.py +0 -0
  16. {differt-0.0.21 → differt-0.0.23}/src/differt/em/__init__.py +0 -0
  17. {differt-0.0.21 → differt-0.0.23}/src/differt/em/constants.py +0 -0
  18. {differt-0.0.21 → differt-0.0.23}/src/differt/em/special.py +0 -0
  19. {differt-0.0.21 → differt-0.0.23}/src/differt/em/utd.py +0 -0
  20. {differt-0.0.21 → differt-0.0.23}/src/differt/em/utils.py +0 -0
  21. {differt-0.0.21 → differt-0.0.23}/src/differt/geometry/__init__.py +0 -0
  22. {differt-0.0.21 → differt-0.0.23}/src/differt/rt/__init__.py +0 -0
  23. {differt-0.0.21 → differt-0.0.23}/src/differt/rt/fermat.py +0 -0
  24. {differt-0.0.21 → differt-0.0.23}/src/differt/rt/image_method.py +0 -0
  25. {differt-0.0.21 → differt-0.0.23}/src/differt/scene/__init__.py +0 -0
  26. {differt-0.0.21 → differt-0.0.23}/src/differt/scene/sionna.py +0 -0
  27. {differt-0.0.21 → differt-0.0.23}/src/differt/version.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: DiffeRT
3
- Version: 0.0.21
3
+ Version: 0.0.23
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.21
24
+ Requires-Dist: differt-core==0.0.23
25
25
  Requires-Dist: equinox>=0.11.5
26
26
  Requires-Dist: filelock>=3.15.4
27
27
  Requires-Dist: jax>=0.4.32
@@ -16,7 +16,7 @@ classifiers = [
16
16
  dependencies = [
17
17
  "beartype>=0.17.2",
18
18
  "chex>=0.1.86",
19
- "differt-core==0.0.21",
19
+ "differt-core==0.0.23",
20
20
  "equinox>=0.11.5",
21
21
  "filelock>=3.15.4",
22
22
  "jax>=0.4.32",
@@ -7,11 +7,10 @@ from typing import Any
7
7
  import equinox as eqx
8
8
  import jax
9
9
  import jax.numpy as jnp
10
- import numpy as np
11
10
  from beartype import beartype as typechecker
12
11
  from jaxtyping import Array, ArrayLike, Bool, Float, Int, Shaped, jaxtyped
13
12
 
14
- from differt.plotting import draw_paths
13
+ from differt.plotting import PlotOutput, draw_paths
15
14
 
16
15
  if sys.version_info >= (3, 11):
17
16
  from typing import Self
@@ -21,7 +20,7 @@ else:
21
20
 
22
21
  @jax.jit
23
22
  @jaxtyped(typechecker=typechecker)
24
- def _cluster_ids(array: Shaped[Array, "batch n"]) -> Int[Array, " batch"]:
23
+ def _cell_ids(array: Shaped[Array, "batch n"]) -> Int[Array, " batch"]:
25
24
  @jaxtyped(typechecker=typechecker)
26
25
  def scan_fun(
27
26
  indices: Int[Array, " batch"],
@@ -41,15 +40,15 @@ def _cluster_ids(array: Shaped[Array, "batch n"]) -> Int[Array, " batch"]:
41
40
 
42
41
  @jax.jit
43
42
  @jaxtyped(typechecker=typechecker)
44
- def merge_cluster_ids(
45
- cluster_ids_a: Int[Array, " *batch"],
46
- cluster_ids_b: Int[Array, " *batch"],
43
+ def merge_cell_ids(
44
+ cell_ids_a: Int[Array, " *batch"],
45
+ cell_ids_b: Int[Array, " *batch"],
47
46
  ) -> Int[Array, " *batch"]:
48
47
  """
49
- Merge two arrays of cluster indices as returned by :meth:`Paths.multipath_clusters`.
48
+ Merge two arrays of cell indices as returned by :meth:`Paths.multipath_cells`.
50
49
 
51
- Let the returned array be ``cluster_ids``,
52
- then ``cluster_ids[i] == cluster_ids[j]`` for all ``i``,
50
+ Let the returned array be ``cell_ids``,
51
+ then ``cell_ids[i] == cell_ids[j]`` for all ``i``,
53
52
  ``j`` indices if
54
53
  ``(groups_a[i], groups_b[i]) == (groups_a[j], groups_b[j])``,
55
54
  granted that arrays have been reshaped to uni-dimensional
@@ -61,15 +60,15 @@ def merge_cluster_ids(
61
60
  do with the ones used in individual arrays.
62
61
 
63
62
  Args:
64
- cluster_ids_a: The first array of cluster indices.
65
- cluster_ids_b: The second array of cluster indices.
63
+ cell_ids_a: The first array of cell indices.
64
+ cell_ids_b: The second array of cell indices.
66
65
 
67
66
  Returns:
68
67
  The new array group indices.
69
68
  """
70
- batch = cluster_ids_a.shape
71
- return _cluster_ids(
72
- jnp.stack((cluster_ids_a, cluster_ids_b), axis=-1).reshape(-1, 2),
69
+ batch = cell_ids_a.shape
70
+ return _cell_ids(
71
+ jnp.stack((cell_ids_a, cell_ids_b), axis=-1).reshape(-1, 2),
73
72
  ).reshape(batch)
74
73
 
75
74
 
@@ -134,7 +133,7 @@ class Paths(eqx.Module):
134
133
  @property
135
134
  @jax.jit
136
135
  @jaxtyped(typechecker=typechecker)
137
- def num_valid_paths(self) -> Int[ArrayLike, ""]:
136
+ def num_valid_paths(self) -> Int[ArrayLike, " "]:
138
137
  """The number of paths kept by :attr:`mask`.
139
138
 
140
139
  If :attr:`mask` is not :data:`None`, then the output value can be traced by JAX.
@@ -176,15 +175,15 @@ class Paths(eqx.Module):
176
175
 
177
176
  @eqx.filter_jit
178
177
  @jaxtyped(typechecker=typechecker)
179
- def multipath_clusters(
178
+ def multipath_cells(
180
179
  self,
181
180
  axis: int = -1,
182
181
  ) -> Int[Array, " *partial_batch"]:
183
182
  """
184
- Return an array of same multipath cluster indices.
183
+ Return an array of same multipath cell indices.
185
184
 
186
- Let the returned array be ``cluster_ids``,
187
- then ``cluster_ids[i] == cluster_ids[j]`` for all ``i``,
185
+ Let the returned array be ``cell_ids``,
186
+ then ``cell_ids[i] == cell_ids[j]`` for all ``i``,
188
187
  ``j`` indices if ``self.mask[i, :] == self.mask[j, :]``,
189
188
  granted that each array has been reshaped to a two-dimensional
190
189
  array and that ``axis`` is the last dimension. Of course, this
@@ -210,7 +209,7 @@ class Paths(eqx.Module):
210
209
 
211
210
  By default, the last axis is used to match the
212
211
  ``num_path_candidates`` axis as returned by
213
- :meth:`TriangleScene.compute_paths<differt.scene.triangle_scene.TriangleScene.compute_paths`.
212
+ :meth:`TriangleScene.compute_paths<differt.scene.triangle_scene.TriangleScene.compute_paths>`.
214
213
 
215
214
  Returns:
216
215
  The array of group indices.
@@ -219,13 +218,13 @@ class Paths(eqx.Module):
219
218
  ValueError: If :attr:`mask` is None.
220
219
  """
221
220
  if self.mask is None:
222
- msg = "Cannot create multiplath clusters from non-existing mask!"
221
+ msg = "Cannot create multiplath cells from non-existing mask!"
223
222
  raise ValueError(msg)
224
223
 
225
224
  mask = jnp.moveaxis(self.mask, axis, -1)
226
225
  *partial_batch, last_axis = mask.shape
227
226
 
228
- return _cluster_ids(mask.reshape(-1, last_axis)).reshape(partial_batch)
227
+ return _cell_ids(mask.reshape(-1, last_axis)).reshape(partial_batch)
229
228
 
230
229
  @jax.jit
231
230
  @jaxtyped(typechecker=typechecker)
@@ -237,7 +236,7 @@ class Paths(eqx.Module):
237
236
  undergo the same types of interactions.
238
237
 
239
238
  Internally, it uses the same logic as
240
- :meth:`multipath_clusters`, but applied to object indices
239
+ :meth:`multipath_cells`, but applied to object indices
241
240
  rather than on mask.
242
241
 
243
242
  Returns:
@@ -278,7 +277,7 @@ class Paths(eqx.Module):
278
277
  *batch, path_length = self.objects.shape
279
278
 
280
279
  objects = self.objects.reshape((-1, path_length))
281
- return _cluster_ids(objects).reshape(batch)
280
+ return _cell_ids(objects).reshape(batch)
282
281
 
283
282
  def __iter__(self) -> Iterator[Self]:
284
283
  """Return an iterator over masked paths.
@@ -311,7 +310,7 @@ class Paths(eqx.Module):
311
310
  """
312
311
  return jnp.sum(fun(self.vertices), where=self.mask)
313
312
 
314
- def plot(self, **kwargs: Any) -> Any:
313
+ def plot(self, **kwargs: Any) -> PlotOutput:
315
314
  """
316
315
  Plot the (masked) paths on a 3D scene.
317
316
 
@@ -322,4 +321,4 @@ class Paths(eqx.Module):
322
321
  Returns:
323
322
  The resulting plot output.
324
323
  """
325
- return draw_paths(np.asarray(self.masked_vertices), **kwargs)
324
+ return draw_paths(self.masked_vertices, **kwargs)
@@ -2,17 +2,16 @@
2
2
  # ruff: noqa: ERA001
3
3
 
4
4
  import sys
5
- from typing import Any
5
+ from typing import Any, Literal, overload
6
6
 
7
7
  import equinox as eqx
8
8
  import jax
9
9
  import jax.numpy as jnp
10
- import numpy as np
11
10
  from beartype import beartype as typechecker
12
11
  from jaxtyping import Array, ArrayLike, Bool, Float, Int, PRNGKeyArray, jaxtyped
13
12
 
14
13
  import differt_core.geometry.triangle_mesh
15
- from differt.plotting import draw_mesh
14
+ from differt.plotting import PlotOutput, draw_mesh
16
15
 
17
16
  from .utils import normalize, orthogonal_basis, rotation_matrix_along_axis
18
17
 
@@ -105,7 +104,7 @@ class TriangleMesh(eqx.Module):
105
104
  """The array of face materials.
106
105
 
107
106
  The array contains the material indices,
108
- with a special placeholder value of :data:`-1`.
107
+ with a special placeholder value of ``-1``.
109
108
  The obtain the name of the material, see :attr:`material_names`.
110
109
  This attribute is :data:`None` if all face materials are unset.
111
110
  """
@@ -137,7 +136,7 @@ class TriangleMesh(eqx.Module):
137
136
  @jaxtyped(
138
137
  typechecker=None
139
138
  ) # typing.Self is (currently) not compatible with jaxtyping and beartype
140
- def __getitem__(self, key: slice | Int[ArrayLike, "*batch"]) -> Self:
139
+ def __getitem__(self, key: slice | Int[ArrayLike, " n"]) -> Self:
141
140
  """Return a copy of this mesh, taking only specific triangles.
142
141
 
143
142
  Warning:
@@ -188,19 +187,42 @@ class TriangleMesh(eqx.Module):
188
187
 
189
188
  return self.triangles.shape[0] // 2
190
189
 
190
+ @property
191
+ def num_objects(self) -> int:
192
+ """The number of objects.
193
+
194
+ This is a convenient alias to :attr:`num_quads` :attr:`assume_quads` is :data:`True`
195
+ else :attr:`num_triangles`.
196
+ """
197
+ return self.num_quads if self.assume_quads else self.num_triangles
198
+
191
199
  @property
192
200
  @jax.jit
193
201
  @jaxtyped(typechecker=typechecker)
194
202
  def triangle_vertices(self) -> Float[Array, "{self.num_triangles} 3 3"]:
195
- """The array of indexed triangle vertices.
196
-
197
- TODO: improve description.
198
- """
203
+ """The array of indexed triangle vertices."""
199
204
  if self.triangles.size == 0:
200
205
  return jnp.empty_like(self.vertices, shape=(0, 3, 3))
201
206
 
202
207
  return jnp.take(self.vertices, self.triangles, axis=0)
203
208
 
209
+ def set_assume_quads(self, flag: bool = True) -> Self:
210
+ """
211
+ Return a copy of this mesh with :attr:`assume_quads` set to ``flag``.
212
+
213
+ Unlike with using :func:`equinox.tree_at`, this function will also
214
+ perform runtime checks.
215
+
216
+ Args:
217
+ flag: The new flag value.
218
+
219
+ Returns:
220
+ A new mesh.
221
+ """
222
+ mesh = eqx.tree_at(lambda m: m.assume_quads, self, flag)
223
+ mesh.__check_init__()
224
+ return mesh
225
+
204
226
  @classmethod
205
227
  def from_core(
206
228
  cls, core_mesh: differt_core.geometry.triangle_mesh.TriangleMesh
@@ -254,6 +276,66 @@ class TriangleMesh(eqx.Module):
254
276
  (jnp.min(vertices, axis=0), jnp.max(vertices, axis=0)),
255
277
  )
256
278
 
279
+ @eqx.filter_jit
280
+ @jaxtyped(
281
+ typechecker=None
282
+ ) # typing.Self is (currently) not compatible with jaxtyping and beartype
283
+ def rotate(self, rotation_matrix: Float[Array, "3 3"]) -> Self:
284
+ """
285
+ Return a new mesh by applying a rotation matrix to all triangle coordinates.
286
+
287
+ Args:
288
+ rotation_matrix: The rotation matrix.
289
+
290
+ Returns:
291
+ The new rotated mesh.
292
+ """
293
+ return eqx.tree_at(
294
+ lambda m: m.vertices,
295
+ self,
296
+ (rotation_matrix @ self.vertices.T).T,
297
+ )
298
+
299
+ @eqx.filter_jit
300
+ @jaxtyped(
301
+ typechecker=None
302
+ ) # typing.Self is (currently) not compatible with jaxtyping and beartype
303
+ def scale(self, scale_factor: Float[ArrayLike, " "]) -> Self:
304
+ """
305
+ Return a new mesh by applying a scale factor to all triangle coordinates.
306
+
307
+ Args:
308
+ scale_factor: The scate factor.
309
+
310
+ Returns:
311
+ The new scaled mesh.
312
+ """
313
+ return eqx.tree_at(
314
+ lambda m: m.vertices,
315
+ self,
316
+ self.vertices * scale_factor,
317
+ )
318
+
319
+ @eqx.filter_jit
320
+ @jaxtyped(
321
+ typechecker=None
322
+ ) # typing.Self is (currently) not compatible with jaxtyping and beartype
323
+ def translate(self, translation: Float[Array, "3"]) -> Self:
324
+ """
325
+ Return a new mesh by applying a translation to all triangle coordinates.
326
+
327
+ Args:
328
+ translation: The translation vector.
329
+
330
+ Returns:
331
+ The new translated mesh.
332
+ """
333
+ return eqx.tree_at(
334
+ lambda m: m.vertices,
335
+ self,
336
+ self.vertices + translation,
337
+ )
338
+
257
339
  @classmethod
258
340
  def empty(cls) -> Self:
259
341
  """
@@ -290,26 +372,62 @@ class TriangleMesh(eqx.Module):
290
372
  is_leaf=lambda x: x is None,
291
373
  )
292
374
 
375
+ @overload
376
+ @classmethod
377
+ def plane(
378
+ cls,
379
+ vertex_a: Float[Array, "3"],
380
+ vertex_b: Float[Array, "3"],
381
+ vertex_c: Float[Array, "3"],
382
+ *,
383
+ normal: Literal[None] = None,
384
+ side_length: Float[ArrayLike, " "] = 1.0,
385
+ rotate: Float[ArrayLike, " "] | None = None,
386
+ ) -> Self: ...
387
+
388
+ @overload
389
+ @classmethod
390
+ def plane(
391
+ cls,
392
+ vertex_a: Float[Array, "3"],
393
+ vertex_b: Literal[None] = None,
394
+ vertex_c: Literal[None] = None,
395
+ *,
396
+ normal: Float[Array, "3"],
397
+ side_length: Float[ArrayLike, " "] = 1.0,
398
+ rotate: Float[ArrayLike, " "] | None = None,
399
+ ) -> Self: ...
400
+
293
401
  @classmethod
294
402
  @jaxtyped(
295
403
  typechecker=None
296
404
  ) # typing.Self is (currently) not compatible with jaxtyping and beartype
297
405
  def plane(
298
406
  cls,
299
- vertex: Float[Array, "3"],
300
- *other_vertices: Float[Array, "3"],
407
+ vertex_a: Float[Array, "3"],
408
+ vertex_b: Float[Array, "3"] | None = None,
409
+ vertex_c: Float[Array, "3"] | None = None,
410
+ *,
301
411
  normal: Float[Array, "3"] | None = None,
302
412
  side_length: Float[ArrayLike, " "] = 1.0,
303
413
  rotate: Float[ArrayLike, " "] | None = None,
304
414
  ) -> Self:
305
415
  """
306
- Create an plane mesh, made of two triangles.
416
+ Create a plane mesh, made of two triangles.
417
+
418
+ Note:
419
+ The mesh satisfies the guarantees
420
+ expected when setting
421
+ :attr:`assume_quads` to :data:`True`.
307
422
 
308
423
  Args:
309
- vertex: The center of the plane.
310
- other_vertices: Two other vertices that define the plane.
424
+ vertex_a: The center of the plane.
425
+ vertex_b: Any second vertex on the plane.
311
426
 
312
- This or ``normal`` is required.
427
+ This and ``vertex_c``, or ``normal`` is required.
428
+ vertex_c: Any third vertex on the plane.
429
+
430
+ This and ``vertex_b``, or ``normal`` is required.
313
431
  normal: The plane normal.
314
432
 
315
433
  Must be of unit length.
@@ -322,23 +440,22 @@ class TriangleMesh(eqx.Module):
322
440
  A new plane mesh.
323
441
 
324
442
  Raises:
325
- ValueError: If neither ``other_vertices`` nor ``normal`` has been provided,
443
+ ValueError: If neither ``vertex_b`` and ``vertex_c``, nor ``normal`` have been provided,
326
444
  or if both have been provided simultaneously.
327
445
  """
328
- if (other_vertices == ()) == (normal is None):
329
- msg = "You must specify one of 'other_vertices' or 'normal', not both."
446
+ if (vertex_b is None) != (vertex_c is None):
447
+ msg = "You must specify either of both of 'vertex_b' and 'vertex_c', or none."
448
+ raise ValueError(msg)
449
+
450
+ if (vertex_b is None) == (normal is None):
451
+ msg = "You must specify one of ('vertex_b', 'vertex_c') or 'normal', not both."
330
452
  raise ValueError(msg)
331
- if other_vertices:
332
- if len(other_vertices) != 2: # noqa: PLR2004
333
- msg = (
334
- "You must provide exactly 3 vertices to create a new plane, "
335
- f"but you provided {len(other_vertices) + 1}."
336
- )
337
- raise ValueError(msg)
338
- u = other_vertices[0] - vertex
339
- v = other_vertices[1] - vertex
453
+
454
+ if vertex_b is not None:
455
+ u = vertex_b - vertex_a
456
+ v = vertex_c - vertex_a
340
457
  w = jnp.cross(u, v)
341
- (normal, _) = normalize(w)
458
+ normal = normalize(w)[0]
342
459
 
343
460
  u, v = orthogonal_basis(
344
461
  normal, # type: ignore[reportArgumentType]
@@ -353,11 +470,77 @@ class TriangleMesh(eqx.Module):
353
470
  rotation_matrix = rotation_matrix_along_axis(rotate, normal)
354
471
  vertices = (rotation_matrix @ vertices.T).T
355
472
 
356
- vertices += vertex
473
+ vertices += vertex_a
357
474
 
358
475
  triangles = jnp.array([[0, 1, 2], [0, 2, 3]], dtype=int)
359
476
  return cls(vertices=vertices, triangles=triangles)
360
477
 
478
+ @classmethod
479
+ @jaxtyped(
480
+ typechecker=None
481
+ ) # typing.Self is (currently) not compatible with jaxtyping and beartype
482
+ def box(
483
+ cls,
484
+ length: Float[ArrayLike, " "] = 1.0,
485
+ width: Float[ArrayLike, " "] = 1.0,
486
+ height: Float[ArrayLike, " "] = 1.0,
487
+ *,
488
+ with_top: bool = False,
489
+ ) -> Self:
490
+ """
491
+ Create a box mesh, with an optional opening on the top.
492
+
493
+ Note:
494
+ The mesh satisfies the guarantees
495
+ expected when setting
496
+ :attr:`assume_quads` to :data:`True`.
497
+
498
+ Args:
499
+ length: The length of the box (along x-axis).
500
+ width: The width of the box (along y-axis).
501
+ height: The height of the box (along z-axis).
502
+ with_top: Whether the top of part
503
+ of the box is included or not.
504
+
505
+ Returns:
506
+ A new box mesh.
507
+ """
508
+ dx = jnp.array([length * 0.5, 0.0, 0.0])
509
+ dy = jnp.array([0.0, width * 0.5, 0.0])
510
+ dz = jnp.array([0.0, 0.0, height * 0.5])
511
+
512
+ vertices = jnp.stack((
513
+ +dx + dy + dz,
514
+ +dx + dy - dz,
515
+ -dx + dy - dz,
516
+ -dx + dy + dz,
517
+ -dx - dy - dz,
518
+ -dx - dy + dz,
519
+ +dx - dy - dz,
520
+ +dx - dy + dz,
521
+ ))
522
+ triangles = jnp.array(
523
+ [
524
+ [0, 1, 2],
525
+ [0, 2, 3],
526
+ [3, 2, 4],
527
+ [3, 4, 5],
528
+ [5, 4, 6],
529
+ [5, 6, 7],
530
+ [7, 6, 1],
531
+ [7, 1, 0],
532
+ [1, 4, 2], # Bottom
533
+ [1, 6, 4],
534
+ ],
535
+ dtype=int,
536
+ )
537
+ if with_top:
538
+ triangles = jnp.concatenate(
539
+ (triangles, jnp.asarray([[0, 3, 5], [0, 5, 7]])),
540
+ axis=0,
541
+ )
542
+ return cls(vertices=vertices, triangles=triangles)
543
+
361
544
  @property
362
545
  def is_empty(self) -> bool:
363
546
  """Whether this scene has no triangle."""
@@ -397,7 +580,7 @@ class TriangleMesh(eqx.Module):
397
580
  core_mesh = differt_core.geometry.triangle_mesh.TriangleMesh.load_ply(file)
398
581
  return cls.from_core(core_mesh)
399
582
 
400
- def plot(self, **kwargs: Any) -> Any:
583
+ def plot(self, **kwargs: Any) -> PlotOutput:
401
584
  """
402
585
  Plot this mesh on a 3D scene.
403
586
 
@@ -409,11 +592,11 @@ class TriangleMesh(eqx.Module):
409
592
  The resulting plot output.
410
593
  """
411
594
  if "face_colors" not in kwargs and self.face_colors is not None:
412
- kwargs["face_colors"] = np.asarray(self.face_colors)
595
+ kwargs["face_colors"] = self.face_colors
413
596
 
414
597
  return draw_mesh(
415
- vertices=np.asarray(self.vertices),
416
- triangles=np.asarray(self.triangles),
598
+ vertices=self.vertices,
599
+ triangles=self.triangles,
417
600
  **kwargs,
418
601
  )
419
602
 
@@ -425,15 +608,28 @@ class TriangleMesh(eqx.Module):
425
608
  self,
426
609
  size: int,
427
610
  replace: bool = False,
611
+ preserve: bool = False,
428
612
  *,
429
613
  key: PRNGKeyArray,
430
614
  ) -> Self:
431
615
  """
432
616
  Generate a new mesh by randomly sampling triangles from this geometry.
433
617
 
618
+ Warning:
619
+ If :attr:`assume_quads` is :data:`True`, then quadrilaterals are
620
+ sampled.
621
+
434
622
  Args:
435
623
  size: The size of the sample, i.e., the number of triangles.
436
624
  replace: Whether to sample with or without replacement.
625
+ preserve: Whether to preserve :attr:`object_bounds`, otherwise
626
+ it is discarded.
627
+
628
+ Object bounds are re-generated by sorting the randomly generated samples,
629
+ which takes additional time.
630
+
631
+ Setting this to :data:`True` has no effect if :attr:`object_bounds`
632
+ is :data:`None`.
437
633
  key: The :func:`jax.random.key` to be used.
438
634
 
439
635
  Returns:
@@ -441,8 +637,46 @@ class TriangleMesh(eqx.Module):
441
637
  """
442
638
  indices = jax.random.choice(
443
639
  key,
444
- self.num_triangles,
640
+ self.num_objects,
445
641
  shape=(size,),
446
642
  replace=replace,
447
643
  )
448
- return self[indices]
644
+
645
+ if self.assume_quads:
646
+ indices *= 2
647
+
648
+ if preserve and self.object_bounds is not None:
649
+ indices = jnp.sort(indices)
650
+ object_bounds = jnp.stack(
651
+ (
652
+ jnp.searchsorted(indices, self.object_bounds[:, 0]),
653
+ jnp.searchsorted(indices, self.object_bounds[:, 1]),
654
+ ),
655
+ axis=-1,
656
+ )
657
+ else:
658
+ object_bounds = None
659
+
660
+ if self.assume_quads:
661
+ indices = jnp.stack((indices, indices + 1), axis=-1).reshape(-1)
662
+
663
+ return eqx.tree_at(
664
+ lambda m: (
665
+ m.vertices,
666
+ m.triangles,
667
+ m.face_colors,
668
+ m.face_materials,
669
+ m.object_bounds,
670
+ ),
671
+ self,
672
+ (
673
+ self.vertices,
674
+ self.triangles[indices, :],
675
+ self.face_colors[indices, :] if self.face_colors is not None else None,
676
+ self.face_materials[indices]
677
+ if self.face_materials is not None
678
+ else None,
679
+ object_bounds,
680
+ ),
681
+ is_leaf=lambda x: x is None,
682
+ )