|
20 | 20 | circumsphere, |
21 | 21 | point_in_simplex, |
22 | 22 | resolve_triangulation_class, |
| 23 | + rust_default_loss, |
23 | 24 | simplex_volume_in_embedding, |
24 | 25 | ) |
25 | 26 | from adaptive.notebook_integration import ensure_holoviews, ensure_plotly |
@@ -333,7 +334,9 @@ def __init__( |
333 | 334 | ): |
334 | 335 | self._triangulation_class = resolve_triangulation_class(triangulation_backend) |
335 | 336 | self._vdim = None |
336 | | - self.loss_per_simplex = loss_per_simplex or default_loss |
| 337 | + # Prefer the Rust implementation of the default loss when the Rust |
| 338 | + # backend is active; it computes the same embedded simplex volume. |
| 339 | + self.loss_per_simplex = loss_per_simplex or rust_default_loss or default_loss |
337 | 340 |
|
338 | 341 | if hasattr(self.loss_per_simplex, "nth_neighbors"): |
339 | 342 | if self.loss_per_simplex.nth_neighbors > 1: |
@@ -649,35 +652,46 @@ def tell_pending(self, point, *, simplex=None): |
649 | 652 | if self.tri is None: |
650 | 653 | return |
651 | 654 |
|
| 655 | + for simpl in self._simplices_containing_point(point, simplex): |
| 656 | + _, to_add = self._add_pending_point_to_simplex(point, simpl) |
| 657 | + if to_add is None: |
| 658 | + continue |
| 659 | + self._update_subsimplex_losses(simpl, to_add) |
| 660 | + |
| 661 | + def _simplices_containing_point(self, point, simplex=None): |
| 662 | + """All simplices of the triangulation containing `point`, found from |
| 663 | + the `simplex` hint when given.""" |
| 664 | + if hasattr(self.tri, "simplices_containing"): |
| 665 | + # Rust backend: one call instead of a point_in_simplex loop. |
| 666 | + return self.tri.simplices_containing(point, simplex=simplex) |
| 667 | + |
652 | 668 | simplex = tuple(simplex or self.tri.locate_point(point)) |
653 | 669 | if not simplex: |
654 | | - return |
655 | | - # Simplex is None if pending point is outside the triangulation, |
656 | | - # then you do not have subtriangles |
| 670 | + return [] |
| 671 | + # Simplex is empty if the pending point is outside the |
| 672 | + # triangulation, then you do not have subtriangles |
657 | 673 |
|
658 | | - simplex = tuple(simplex) |
659 | 674 | simplices = [self.tri.vertex_to_simplices[i] for i in simplex] |
660 | 675 | neighbors = set.union(*simplices) |
661 | 676 | # Neighbours also includes the simplex itself |
| 677 | + return [s for s in neighbors if self.tri.point_in_simplex(point, s)] |
662 | 678 |
|
663 | | - for simpl in neighbors: |
664 | | - _, to_add = self._try_adding_pending_point_to_simplex(point, simpl) |
665 | | - if to_add is None: |
666 | | - continue |
667 | | - self._update_subsimplex_losses(simpl, to_add) |
668 | | - |
669 | | - def _try_adding_pending_point_to_simplex(self, point, simplex): |
670 | | - # try to insert it |
671 | | - if not self.tri.point_in_simplex(point, simplex): |
672 | | - return None, None |
673 | | - |
| 679 | + def _add_pending_point_to_simplex(self, point, simplex): |
| 680 | + """Insert `point` into the subtriangulation of `simplex`, which must |
| 681 | + contain the point.""" |
674 | 682 | if simplex not in self._subtriangulations: |
675 | 683 | vertices = self.tri.get_vertices(simplex) |
676 | 684 | self._subtriangulations[simplex] = self._triangulation_class(vertices) |
677 | 685 |
|
678 | 686 | self._pending_to_simplex[point] = simplex |
679 | 687 | return self._subtriangulations[simplex].add_point(point) |
680 | 688 |
|
| 689 | + def _try_adding_pending_point_to_simplex(self, point, simplex): |
| 690 | + # try to insert it |
| 691 | + if not self.tri.point_in_simplex(point, simplex): |
| 692 | + return None, None |
| 693 | + return self._add_pending_point_to_simplex(point, simplex) |
| 694 | + |
681 | 695 | def _update_subsimplex_losses(self, simplex, new_subsimplices): |
682 | 696 | loss = self._losses[simplex] |
683 | 697 |
|
|
0 commit comments