differt 0.0.33__tar.gz → 0.0.35__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.33 → differt-0.0.35}/PKG-INFO +2 -2
- {differt-0.0.33 → differt-0.0.35}/pyproject.toml +1 -1
- {differt-0.0.33 → differt-0.0.35}/src/differt/em/_antenna.py +32 -2
- {differt-0.0.33 → differt-0.0.35}/src/differt/geometry/_triangle_mesh.py +289 -17
- {differt-0.0.33 → differt-0.0.35}/.gitignore +0 -0
- {differt-0.0.33 → differt-0.0.35}/LICENSE.md +0 -0
- {differt-0.0.33 → differt-0.0.35}/README.md +0 -0
- {differt-0.0.33 → differt-0.0.35}/src/differt/__init__.py +0 -0
- {differt-0.0.33 → differt-0.0.35}/src/differt/_version.py +0 -0
- {differt-0.0.33 → differt-0.0.35}/src/differt/em/__init__.py +0 -0
- {differt-0.0.33 → differt-0.0.35}/src/differt/em/_constants.py +0 -0
- {differt-0.0.33 → differt-0.0.35}/src/differt/em/_fresnel.py +0 -0
- {differt-0.0.33 → differt-0.0.35}/src/differt/em/_interaction_type.py +0 -0
- {differt-0.0.33 → differt-0.0.35}/src/differt/em/_material.py +0 -0
- {differt-0.0.33 → differt-0.0.35}/src/differt/em/_utd.py +0 -0
- {differt-0.0.33 → differt-0.0.35}/src/differt/em/_utils.py +0 -0
- {differt-0.0.33 → differt-0.0.35}/src/differt/geometry/__init__.py +0 -0
- {differt-0.0.33 → differt-0.0.35}/src/differt/geometry/_paths.py +0 -0
- {differt-0.0.33 → differt-0.0.35}/src/differt/geometry/_utils.py +0 -0
- {differt-0.0.33 → differt-0.0.35}/src/differt/plotting/__init__.py +0 -0
- {differt-0.0.33 → differt-0.0.35}/src/differt/plotting/_core.py +0 -0
- {differt-0.0.33 → differt-0.0.35}/src/differt/plotting/_utils.py +0 -0
- {differt-0.0.33 → differt-0.0.35}/src/differt/py.typed +0 -0
- {differt-0.0.33 → differt-0.0.35}/src/differt/rt/__init__.py +0 -0
- {differt-0.0.33 → differt-0.0.35}/src/differt/rt/_fermat.py +0 -0
- {differt-0.0.33 → differt-0.0.35}/src/differt/rt/_image_method.py +0 -0
- {differt-0.0.33 → differt-0.0.35}/src/differt/rt/_utils.py +0 -0
- {differt-0.0.33 → differt-0.0.35}/src/differt/scene/__init__.py +0 -0
- {differt-0.0.33 → differt-0.0.35}/src/differt/scene/_sionna.py +0 -0
- {differt-0.0.33 → differt-0.0.35}/src/differt/scene/_triangle_scene.py +0 -0
- {differt-0.0.33 → differt-0.0.35}/src/differt/utils.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: DiffeRT
|
|
3
|
-
Version: 0.0.
|
|
3
|
+
Version: 0.0.35
|
|
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.0.
|
|
24
|
+
Requires-Dist: differt-core==0.0.35
|
|
25
25
|
Requires-Dist: equinox>=0.11.5
|
|
26
26
|
Requires-Dist: filelock>=3.15.4
|
|
27
27
|
Requires-Dist: jax>=0.4.32
|
|
@@ -7,7 +7,11 @@ import jax
|
|
|
7
7
|
import jax.numpy as jnp
|
|
8
8
|
from jaxtyping import Array, ArrayLike, Float, Inexact
|
|
9
9
|
|
|
10
|
-
from differt.geometry import
|
|
10
|
+
from differt.geometry import (
|
|
11
|
+
cartesian_to_spherical,
|
|
12
|
+
normalize,
|
|
13
|
+
spherical_to_cartesian,
|
|
14
|
+
)
|
|
11
15
|
from differt.plotting import PlotOutput, draw_surface
|
|
12
16
|
from differt.utils import dot, safe_divide
|
|
13
17
|
|
|
@@ -253,7 +257,7 @@ class Antenna(BaseAntenna):
|
|
|
253
257
|
|
|
254
258
|
gain = p / p.max()
|
|
255
259
|
|
|
256
|
-
r
|
|
260
|
+
r = self.center + (r - self.center) * gain
|
|
257
261
|
gain = jnp.squeeze(gain, axis=-1)
|
|
258
262
|
|
|
259
263
|
return draw_surface(
|
|
@@ -284,6 +288,9 @@ class Dipole(Antenna):
|
|
|
284
288
|
|
|
285
289
|
If this is provided, this takes precedence over ``current``.
|
|
286
290
|
center: The center position of the antenna, from which the fields are radiated.
|
|
291
|
+
look_at: When provided, re-orient the antenna to look at the given point.
|
|
292
|
+
|
|
293
|
+
This overrides the direction of the dipole moment.
|
|
287
294
|
|
|
288
295
|
Examples:
|
|
289
296
|
The following example shows how to plot the radiation
|
|
@@ -327,6 +334,17 @@ class Dipole(Antenna):
|
|
|
327
334
|
... bbox_to_anchor=(0.5 + jnp.cos(angle) / 2, 0.5 + jnp.sin(angle) / 2),
|
|
328
335
|
... )
|
|
329
336
|
>>> plt.show() # doctest: +SKIP
|
|
337
|
+
|
|
338
|
+
The third example shows how to orient the antenna to look at a given point.
|
|
339
|
+
|
|
340
|
+
.. plotly::
|
|
341
|
+
:fig-vars: fig
|
|
342
|
+
|
|
343
|
+
>>> from differt.em import Dipole
|
|
344
|
+
>>>
|
|
345
|
+
>>> ant = Dipole(frequency=1e9, look_at=jnp.array([0.0, -1.0, -1.0]))
|
|
346
|
+
>>> fig = ant.plot_radiation_pattern(backend="plotly")
|
|
347
|
+
>>> fig # doctest: +SKIP
|
|
330
348
|
"""
|
|
331
349
|
|
|
332
350
|
length: Float[Array, " "] = eqx.field(converter=jnp.asarray)
|
|
@@ -344,6 +362,7 @@ class Dipole(Antenna):
|
|
|
344
362
|
current: Float[ArrayLike, " "] | None = 1.0,
|
|
345
363
|
charge: Float[ArrayLike, " "] | None = None,
|
|
346
364
|
center: Float[ArrayLike, "3"] = jnp.array([0.0, 0.0, 0.0]),
|
|
365
|
+
look_at: Float[ArrayLike, "3"] | None = None,
|
|
347
366
|
) -> None:
|
|
348
367
|
super().__init__(jnp.asarray(frequency), center=center)
|
|
349
368
|
|
|
@@ -363,6 +382,17 @@ class Dipole(Antenna):
|
|
|
363
382
|
/ (jnp.linalg.norm(moment) * self.angular_frequency)
|
|
364
383
|
)
|
|
365
384
|
|
|
385
|
+
if look_at is not None:
|
|
386
|
+
moment = spherical_to_cartesian(
|
|
387
|
+
cartesian_to_spherical(moment)
|
|
388
|
+
+ (
|
|
389
|
+
cartesian_to_spherical(
|
|
390
|
+
normalize(jnp.asarray(look_at) - self.center)[0]
|
|
391
|
+
)
|
|
392
|
+
- cartesian_to_spherical(jnp.array([1.0, 0.0, 0.0]))
|
|
393
|
+
)
|
|
394
|
+
)
|
|
395
|
+
|
|
366
396
|
self.moment = moment # type: ignore[reportAttributeAccessIssue]
|
|
367
397
|
|
|
368
398
|
@property
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
# ruff: noqa: ERA001
|
|
2
2
|
|
|
3
|
-
from collections.abc import Iterator
|
|
4
|
-
from
|
|
3
|
+
from collections.abc import Callable, Iterator
|
|
4
|
+
from dataclasses import replace
|
|
5
|
+
from typing import TYPE_CHECKING, Any, Generic, TypeVar, overload
|
|
5
6
|
|
|
6
7
|
import equinox as eqx
|
|
7
8
|
import jax
|
|
@@ -86,6 +87,101 @@ def triangles_contain_vertices_assuming_inside_same_plane(
|
|
|
86
87
|
return all_pos | all_neg
|
|
87
88
|
|
|
88
89
|
|
|
90
|
+
_Index = slice | Int[ArrayLike, " "] | Int[Array, " n"] | Bool[Array, " num_triangles"]
|
|
91
|
+
_T = TypeVar("_T", bound="TriangleMesh")
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
class _TriangleMeshVerticesUpdateHelper(Generic[_T]):
|
|
95
|
+
"""A helper class to update vertices of a triangle mesh."""
|
|
96
|
+
|
|
97
|
+
__slots__ = ("mesh",)
|
|
98
|
+
|
|
99
|
+
def __init__(self, mesh: _T) -> None:
|
|
100
|
+
self.mesh = mesh
|
|
101
|
+
|
|
102
|
+
def __getitem__(self, index: _Index) -> "_TriangleMeshVerticesUpdateRef[_T]":
|
|
103
|
+
return _TriangleMeshVerticesUpdateRef(self.mesh, index)
|
|
104
|
+
|
|
105
|
+
def __repr__(self) -> str:
|
|
106
|
+
return f"_TriangleMeshVerticesUpdateHelper({self.mesh!r})"
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
class _TriangleMeshVerticesUpdateRef(Generic[_T]):
|
|
110
|
+
"""A reference to update vertices of a triangle mesh."""
|
|
111
|
+
|
|
112
|
+
__slots__ = ("index", "mesh")
|
|
113
|
+
|
|
114
|
+
def __init__(self, mesh: _T, index: _Index) -> None:
|
|
115
|
+
self.mesh = mesh
|
|
116
|
+
self.index = index
|
|
117
|
+
|
|
118
|
+
def __repr__(self) -> str:
|
|
119
|
+
return f"_TriangleMeshVerticesUpdateRef({self.mesh!r}, {self.index!r})"
|
|
120
|
+
|
|
121
|
+
def _triangles_index(self, **kwargs: Any) -> _Index:
|
|
122
|
+
if self.index == slice(None):
|
|
123
|
+
# TODO: check if we can use fast path but avoid updating vertices
|
|
124
|
+
# that are not referenced by any triangle
|
|
125
|
+
return self.index # Fast path
|
|
126
|
+
index = self.mesh.triangles.at[self.index, :].get(**kwargs).reshape(-1)
|
|
127
|
+
return jnp.unique(
|
|
128
|
+
index, size=len(index), fill_value=self.mesh.vertices.shape[0]
|
|
129
|
+
)
|
|
130
|
+
|
|
131
|
+
def set(self, values: Any, **kwargs: Any) -> _T:
|
|
132
|
+
index = self._triangles_index(**kwargs)
|
|
133
|
+
return eqx.tree_at(
|
|
134
|
+
lambda m: m.vertices,
|
|
135
|
+
self.mesh,
|
|
136
|
+
self.mesh.vertices.at[index, :].set(
|
|
137
|
+
values, indices_are_sorted=True, unique_indices=True
|
|
138
|
+
),
|
|
139
|
+
)
|
|
140
|
+
|
|
141
|
+
def get(self, **kwargs: Any) -> Float[ArrayLike, "num_indexed_triangles 3"]:
|
|
142
|
+
index = self._triangles_index(**kwargs)
|
|
143
|
+
return self.mesh.vertices.at[index, :].get(
|
|
144
|
+
indices_are_sorted=True, unique_indices=True
|
|
145
|
+
)
|
|
146
|
+
|
|
147
|
+
def apply(
|
|
148
|
+
self,
|
|
149
|
+
func: Callable[
|
|
150
|
+
[Float[ArrayLike, "num_indexed_triangles 3"]],
|
|
151
|
+
Float[Array, "num_indexed_triangles 3"],
|
|
152
|
+
],
|
|
153
|
+
**kwargs: Any,
|
|
154
|
+
) -> _T:
|
|
155
|
+
index = self._triangles_index(**kwargs)
|
|
156
|
+
return eqx.tree_at(
|
|
157
|
+
lambda m: m.vertices,
|
|
158
|
+
self.mesh,
|
|
159
|
+
self.mesh.vertices.at[index, :].apply(
|
|
160
|
+
func, indices_are_sorted=True, unique_indices=True
|
|
161
|
+
),
|
|
162
|
+
)
|
|
163
|
+
|
|
164
|
+
def add(self, values: Any, **kwargs: Any) -> _T:
|
|
165
|
+
index = self._triangles_index(**kwargs)
|
|
166
|
+
return eqx.tree_at(
|
|
167
|
+
lambda m: m.vertices,
|
|
168
|
+
self.mesh,
|
|
169
|
+
self.mesh.vertices.at[index, :].add(
|
|
170
|
+
values, indices_are_sorted=True, unique_indices=True
|
|
171
|
+
),
|
|
172
|
+
)
|
|
173
|
+
|
|
174
|
+
def mul(self, values: Any, **kwargs: Any) -> _T:
|
|
175
|
+
index = self._triangles_index(**kwargs)
|
|
176
|
+
return eqx.tree_at(
|
|
177
|
+
lambda m: m.vertices,
|
|
178
|
+
self.mesh,
|
|
179
|
+
self.mesh.vertices.at[index, :].mul(
|
|
180
|
+
values, indices_are_sorted=True, unique_indices=True
|
|
181
|
+
),
|
|
182
|
+
)
|
|
183
|
+
|
|
184
|
+
|
|
89
185
|
class TriangleMesh(eqx.Module):
|
|
90
186
|
"""A simple geometry made of triangles."""
|
|
91
187
|
|
|
@@ -292,6 +388,95 @@ class TriangleMesh(eqx.Module):
|
|
|
292
388
|
(jnp.min(vertices, axis=0), jnp.max(vertices, axis=0)),
|
|
293
389
|
)
|
|
294
390
|
|
|
391
|
+
@property
|
|
392
|
+
def at(self): # noqa: ANN202
|
|
393
|
+
"""Helper property for updating a subset of triangle vertices.
|
|
394
|
+
|
|
395
|
+
This ``at`` property is used to update vertices of a triangle mesh,
|
|
396
|
+
based on triangles indices,
|
|
397
|
+
similar to how the ``at`` property is used in :attr:`jax.numpy.ndarray.at`.
|
|
398
|
+
|
|
399
|
+
In particular, the following methods are available:
|
|
400
|
+
|
|
401
|
+
- ``set(values, **kwargs)``: Set the vertices of selected triangles to some values;
|
|
402
|
+
- ``apply(func, **kwargs)``: Apply a function to the vertices of selected triangles;
|
|
403
|
+
- ``add(values, **kwargs)``: Add some values to the vertices of selected triangles;
|
|
404
|
+
- ``mul(values, **kwargs)``: Multiply the vertices of selected triangles by some values;
|
|
405
|
+
- ``get(values, **kwargs)``: Get the vertices of selected triangles.
|
|
406
|
+
|
|
407
|
+
E.g., ``mesh.at[0:2].add([1.0, 2.0, 3.0])`` will translate the first two triangles.
|
|
408
|
+
|
|
409
|
+
Each method takes additional keyword parameters that are passed to the methods
|
|
410
|
+
of :attr:`jax.numpy.ndarray.at`. Because the vertices of a triangle mesh may be shared
|
|
411
|
+
between multiple triangles, this method prevents update the same vertice multiple times
|
|
412
|
+
by ignoring duplicate vertex indices. As a result, providing duplicate triangle indices
|
|
413
|
+
will not result in duplicate updates.
|
|
414
|
+
|
|
415
|
+
Warning:
|
|
416
|
+
As duplicate vertices are ignored, the number of update vertices is not
|
|
417
|
+
necessarily equal to the number of triangles selected times three. Moreover,
|
|
418
|
+
vertices are re-ordered when duplicates are removed. As a results, you should
|
|
419
|
+
not apply any update that depends on the order of or the number of updated
|
|
420
|
+
the vertices.
|
|
421
|
+
|
|
422
|
+
Examples:
|
|
423
|
+
The following example shows how to translate the first two (triangle) faces.
|
|
424
|
+
|
|
425
|
+
.. plotly::
|
|
426
|
+
|
|
427
|
+
>>> from differt.geometry import TriangleMesh
|
|
428
|
+
>>>
|
|
429
|
+
>>> mesh = (
|
|
430
|
+
... sum(
|
|
431
|
+
... TriangleMesh.box().iter_objects(),
|
|
432
|
+
... start=TriangleMesh.empty(),
|
|
433
|
+
... )
|
|
434
|
+
... .at[0:2]
|
|
435
|
+
... .add([1.0, 1.0, 0.0])
|
|
436
|
+
... )
|
|
437
|
+
>>> fig = mesh.plot(opacity=0.5, backend="plotly")
|
|
438
|
+
>>> fig # doctest: +SKIP
|
|
439
|
+
|
|
440
|
+
In the above example, splitting the cube mesh into separate objects is necessary,
|
|
441
|
+
as the vertices of the cube are shared between the faces. If the cube was not split,
|
|
442
|
+
the translation would be applied to all the faces that share the vertices of the first two faces.
|
|
443
|
+
|
|
444
|
+
.. plotly::
|
|
445
|
+
|
|
446
|
+
>>> from differt.geometry import TriangleMesh
|
|
447
|
+
>>>
|
|
448
|
+
>>> mesh = TriangleMesh.box().at[0:2].add([1.0, 1.0, 0.0])
|
|
449
|
+
>>> fig = mesh.plot(opacity=0.5, backend="plotly")
|
|
450
|
+
>>> fig # doctest: +SKIP
|
|
451
|
+
|
|
452
|
+
Finally, the :attr:`at` property is lazily evaluated, so checking that the index
|
|
453
|
+
is valid is not performed until a method is called.
|
|
454
|
+
|
|
455
|
+
>>> from differt.geometry import TriangleMesh
|
|
456
|
+
>>>
|
|
457
|
+
>>> mesh = TriangleMesh.box()
|
|
458
|
+
>>> mesh.at
|
|
459
|
+
_TriangleMeshVerticesUpdateHelper(TriangleMesh(
|
|
460
|
+
vertices=f32[8,3],
|
|
461
|
+
triangles=i32[10,3],
|
|
462
|
+
material_names=(),
|
|
463
|
+
object_bounds=i32[5,2]
|
|
464
|
+
))
|
|
465
|
+
>>> index = jnp.array([True, False])
|
|
466
|
+
>>> mesh.at[index]
|
|
467
|
+
_TriangleMeshVerticesUpdateRef(TriangleMesh(
|
|
468
|
+
vertices=f32[8,3],
|
|
469
|
+
triangles=i32[10,3],
|
|
470
|
+
material_names=(),
|
|
471
|
+
object_bounds=i32[5,2]
|
|
472
|
+
), Array([ True, False], dtype=bool))
|
|
473
|
+
>>> mesh.at[index].add(1.0) # doctest: +IGNORE_EXCEPTION_DETAIL
|
|
474
|
+
Traceback (most recent call last):
|
|
475
|
+
IndexError: boolean index did not match shape of indexed array in index 0:
|
|
476
|
+
got (2,), expected (10,)
|
|
477
|
+
"""
|
|
478
|
+
return _TriangleMeshVerticesUpdateHelper(self)
|
|
479
|
+
|
|
295
480
|
def rotate(self, rotation_matrix: Float[ArrayLike, "3 3"]) -> Self:
|
|
296
481
|
"""
|
|
297
482
|
Return a new mesh by applying a rotation matrix to all triangle coordinates.
|
|
@@ -337,7 +522,7 @@ class TriangleMesh(eqx.Module):
|
|
|
337
522
|
return eqx.tree_at(
|
|
338
523
|
lambda m: m.vertices,
|
|
339
524
|
self,
|
|
340
|
-
self.vertices + translation,
|
|
525
|
+
self.vertices + jnp.asarray(translation),
|
|
341
526
|
)
|
|
342
527
|
|
|
343
528
|
@classmethod
|
|
@@ -350,7 +535,7 @@ class TriangleMesh(eqx.Module):
|
|
|
350
535
|
"""
|
|
351
536
|
return cls(vertices=jnp.empty((0, 3)), triangles=jnp.empty((0, 3), dtype=int))
|
|
352
537
|
|
|
353
|
-
def append(self, other: "TriangleMesh") ->
|
|
538
|
+
def append(self, other: "TriangleMesh") -> Self:
|
|
354
539
|
"""
|
|
355
540
|
Return a new mesh by appending another mesh to this one.
|
|
356
541
|
|
|
@@ -358,11 +543,27 @@ class TriangleMesh(eqx.Module):
|
|
|
358
543
|
|
|
359
544
|
For convenience, you can also use the ``+`` operator.
|
|
360
545
|
|
|
361
|
-
..
|
|
546
|
+
.. note::
|
|
547
|
+
|
|
548
|
+
The following rules are applied when merging two meshes:
|
|
362
549
|
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
550
|
+
* The vertices are concatenated;
|
|
551
|
+
* The triangles are concatenated, and the indices of the second mesh are updated;
|
|
552
|
+
* The face colors are concatenated. If one mesh has colors while the other does not,
|
|
553
|
+
then mesh with no colors will have its face colors set to black (0, 0, 0);
|
|
554
|
+
* The face materials are concatenated. If ``other`` has face materials not included
|
|
555
|
+
in ``self``, then the face materials from ``other`` are renumbered.
|
|
556
|
+
If one mesh has colors while the other does not,
|
|
557
|
+
then mesh with no colors will have its face materials set to ``-1``;
|
|
558
|
+
* The material names are merged, keeping only unique names;
|
|
559
|
+
* The object bounds are concatenated only if both meshes have them set,
|
|
560
|
+
otherwise, the object bounds are set to :data:`None`;
|
|
561
|
+
* The :attr:`assume_quads` flag is set to :data:`True` if both meshes have it set to :data:`True`.
|
|
562
|
+
|
|
563
|
+
Two important exceptions are:
|
|
564
|
+
|
|
565
|
+
* If one mesh is empty, a copy of the other mesh is returned as is;
|
|
566
|
+
* If both meshes are empty, then a copy of ``self`` is returned.
|
|
366
567
|
|
|
367
568
|
Args:
|
|
368
569
|
other: The mesh to append.
|
|
@@ -387,10 +588,10 @@ class TriangleMesh(eqx.Module):
|
|
|
387
588
|
>>> fig = mesh.plot(opacity=0.5, backend="plotly")
|
|
388
589
|
>>> fig # doctest: +SKIP
|
|
389
590
|
"""
|
|
390
|
-
if self.is_empty:
|
|
391
|
-
return eqx.tree_at(lambda _: (), other, ())
|
|
392
591
|
if other.is_empty:
|
|
393
592
|
return eqx.tree_at(lambda _: (), self, ())
|
|
593
|
+
if self.is_empty:
|
|
594
|
+
return eqx.tree_at(lambda _: (), other, ())
|
|
394
595
|
|
|
395
596
|
vertices = jnp.concatenate((self.vertices, other.vertices), axis=0)
|
|
396
597
|
triangles = jnp.concatenate(
|
|
@@ -472,13 +673,18 @@ class TriangleMesh(eqx.Module):
|
|
|
472
673
|
else None
|
|
473
674
|
)
|
|
474
675
|
assume_quads = self.assume_quads and other.assume_quads
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
676
|
+
mesh = replace(self, material_names=material_names, assume_quads=assume_quads)
|
|
677
|
+
return eqx.tree_at(
|
|
678
|
+
lambda m: (
|
|
679
|
+
m.vertices,
|
|
680
|
+
m.triangles,
|
|
681
|
+
m.face_colors,
|
|
682
|
+
m.face_materials,
|
|
683
|
+
m.object_bounds,
|
|
684
|
+
),
|
|
685
|
+
mesh,
|
|
686
|
+
(vertices, triangles, face_colors, face_materials, object_bounds),
|
|
687
|
+
is_leaf=lambda x: x is None,
|
|
482
688
|
)
|
|
483
689
|
|
|
484
690
|
__add__ = append
|
|
@@ -649,6 +855,72 @@ class TriangleMesh(eqx.Module):
|
|
|
649
855
|
is_leaf=lambda x: x is None,
|
|
650
856
|
)
|
|
651
857
|
|
|
858
|
+
def set_materials(self, *names: str) -> Self:
|
|
859
|
+
"""
|
|
860
|
+
Return a copy of this mesh, with new face materials from material names.
|
|
861
|
+
|
|
862
|
+
If a material name is not in :attr:`material_names`, it is added.
|
|
863
|
+
|
|
864
|
+
Args:
|
|
865
|
+
names: The material names.
|
|
866
|
+
If one name is provided, it will be applied to all triangles.
|
|
867
|
+
|
|
868
|
+
Returns:
|
|
869
|
+
A new mesh with updated face materials.
|
|
870
|
+
|
|
871
|
+
Raises:
|
|
872
|
+
ValueError: If the number of names is not 1, :attr:`num_triangles`, or :attr:`num_objects` (if :attr:`assume_quads` is set to :data:`True`).
|
|
873
|
+
"""
|
|
874
|
+
if len(names) not in {1, self.num_triangles, self.num_objects}:
|
|
875
|
+
if self.assume_quads:
|
|
876
|
+
msg = f"Expected either 1, {self.num_triangles}, or {self.num_objects} names, got {len(names)}."
|
|
877
|
+
else:
|
|
878
|
+
msg = f"Expected either 1, or {self.num_triangles} names, got {len(names)}."
|
|
879
|
+
raise ValueError(msg)
|
|
880
|
+
material_names = dict.fromkeys(self.material_names)
|
|
881
|
+
if all(name in material_names for name in names):
|
|
882
|
+
material_names = {name: i for i, name in enumerate(material_names)}
|
|
883
|
+
face_materials = jnp.array([material_names[name] for name in names])
|
|
884
|
+
if self.assume_quads and len(names) == self.num_quads:
|
|
885
|
+
face_materials = jnp.repeat(face_materials, 2)
|
|
886
|
+
return self.set_face_materials(face_materials)
|
|
887
|
+
|
|
888
|
+
material_names = material_names | dict.fromkeys(names)
|
|
889
|
+
material_names = {name: i for i, name in enumerate(material_names)}
|
|
890
|
+
|
|
891
|
+
face_materials = jnp.array([material_names[name] for name in names])
|
|
892
|
+
if self.assume_quads and len(names) == self.num_quads:
|
|
893
|
+
face_materials = jnp.repeat(face_materials, 2)
|
|
894
|
+
mesh = replace(self, material_names=tuple(material_names))
|
|
895
|
+
return mesh.set_face_materials(face_materials)
|
|
896
|
+
|
|
897
|
+
def set_face_materials(
|
|
898
|
+
self, materials: Int[ArrayLike, " "] | Int[ArrayLike, "#num_triangles"]
|
|
899
|
+
) -> Self:
|
|
900
|
+
"""
|
|
901
|
+
Return a copy of this mesh, with new face materials.
|
|
902
|
+
|
|
903
|
+
Args:
|
|
904
|
+
materials: The material indices.
|
|
905
|
+
If one material is provided, it will be applied to all triangles.
|
|
906
|
+
|
|
907
|
+
No check is performed to verify that material indices are actually
|
|
908
|
+
in bounds of :attr:`material_names`.
|
|
909
|
+
|
|
910
|
+
Returns:
|
|
911
|
+
A new mesh with updated face materials.
|
|
912
|
+
"""
|
|
913
|
+
face_materials = jnp.broadcast_to(
|
|
914
|
+
jnp.asarray(materials),
|
|
915
|
+
self.num_triangles,
|
|
916
|
+
)
|
|
917
|
+
return eqx.tree_at(
|
|
918
|
+
lambda m: m.face_materials,
|
|
919
|
+
self,
|
|
920
|
+
face_materials,
|
|
921
|
+
is_leaf=lambda x: x is None,
|
|
922
|
+
)
|
|
923
|
+
|
|
652
924
|
@overload
|
|
653
925
|
@classmethod
|
|
654
926
|
def plane(
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|