diff --git a/doc/changelog.d/239.fixed.md b/doc/changelog.d/239.fixed.md new file mode 100644 index 00000000..aea109df --- /dev/null +++ b/doc/changelog.d/239.fixed.md @@ -0,0 +1 @@ +Fix cross section position for apis that clear the scene diff --git a/src/ansys/visor/viewer/renderer/base.py b/src/ansys/visor/viewer/renderer/base.py index 9b4c5cad..0729a059 100644 --- a/src/ansys/visor/viewer/renderer/base.py +++ b/src/ansys/visor/viewer/renderer/base.py @@ -52,11 +52,13 @@ class IRenderer(ABC): _last_camera_state: "VisorCameraState | None" _last_cross_section_state: "VisorCrossSectionState | None" + _cross_section_plane_is_explicit: bool def __init__(self) -> None: - """Initialize the camera and cross-section records to ``None``.""" + """Initialize the camera and cross-section records to ``None``, the plane record as not explicit.""" self._last_camera_state = None self._last_cross_section_state = None + self._cross_section_plane_is_explicit = False # ------------------------------------------------------------------------ # Wire contract @@ -280,7 +282,11 @@ def set_cross_section_visibility(self, visible: bool) -> None: def sync_cross_section_plane( self, origin: list[float], normal: list[float] ) -> None: - """Store a copy of *origin* and *normal* as the plane record. + """Store a copy of *origin* and *normal* as the plane record and mark it explicit. + + Every deliberate placement -- a client drag, a server drag, a loaded + state -- arrives here. An explicit record is preserved across later + bounds updates until :meth:`reset_cross_section_plane`. An implementation with a widget overrides this, calls ``super()`` first, then writes the widget's VTK objects. @@ -291,11 +297,22 @@ def sync_cross_section_plane( self._last_cross_section_state = VisorCrossSectionState( origin=list(origin), normal=list(normal) ) + self._cross_section_plane_is_explicit = True def get_cross_section_plane(self) -> "VisorCrossSectionState | None": """Return the plane record, or ``None`` if nothing has written one yet.""" return self._last_cross_section_state + def reset_cross_section_plane(self) -> None: + """Discard the plane record and its explicit mark, so the next bounds update places a default plane. + + Called when the scene is cleared: a plane placed for the previous + scene's datasets has no meaning for the next one. Writes no VTK + object and notifies no client. + """ + self._last_cross_section_state = None + self._cross_section_plane_is_explicit = False + @abstractmethod def set_bounding_box_visibility(self, visible: bool) -> None: diff --git a/src/ansys/visor/viewer/renderer/pipeline_renderer.py b/src/ansys/visor/viewer/renderer/pipeline_renderer.py index 9b456132..4d342de5 100644 --- a/src/ansys/visor/viewer/renderer/pipeline_renderer.py +++ b/src/ansys/visor/viewer/renderer/pipeline_renderer.py @@ -424,15 +424,21 @@ def update_bounds(self, bounds: list[float]) -> None: the record is never ``None`` once a scene has been populated and ``get_state`` can assign it unconditionally, as it does the camera's. - **The seed runs only when there is no record.** This method is not - reached once per scene: ``set_part_visibility`` fans out to - ``_update_widget_bounds``, so a part toggle or a dataset add reaches - here too. Seeding unconditionally would therefore discard a plane the - user had dragged, on the next toggle, and the widget's own - ``update_bounds`` would already have overwritten both VTK objects with - the default plane. With a record present this writes the record - *back* to the widget instead -- after the widget's own call, never - before -- and leaves the record itself untouched. + **The seed runs whenever the record is not explicit**, that is, when + nothing has placed the plane through ``sync_cross_section_plane`` since + the last ``reset_cross_section_plane``. The default plane sits below + the scene, and every mapper clips against it, so a default record must + follow the bounds as datasets are added; a stale one clips away any + dataset that lies below it. + + This method is not reached once per scene: ``set_part_visibility`` + fans out to ``_update_widget_bounds``, so a part toggle or a dataset + add reaches here too. Seeding over an explicit record would therefore + discard a plane the user had dragged, on the next toggle, and the + widget's own ``update_bounds`` would already have overwritten both VTK + objects with the default plane. With an explicit record this writes + the record *back* to the widget instead -- after the widget's own + call, never before -- and leaves the record itself untouched. Ordering is load-bearing in both branches. Seeded before the widget call, the record holds the previous plane and is one populate behind; @@ -442,7 +448,7 @@ def update_bounds(self, bounds: list[float]) -> None: """ self._cross_section_widget.update_bounds(bounds) self._bounding_box_widget.update_bounds(bounds) - if self._last_cross_section_state is None: + if not self._cross_section_plane_is_explicit: plane = self._cross_section_widget.plane self._last_cross_section_state = VisorCrossSectionState( origin=list(plane.GetOrigin()), diff --git a/src/ansys/visor/viewer/vtk/scene/base.py b/src/ansys/visor/viewer/vtk/scene/base.py index f2b828d0..b355521a 100644 --- a/src/ansys/visor/viewer/vtk/scene/base.py +++ b/src/ansys/visor/viewer/vtk/scene/base.py @@ -413,11 +413,15 @@ def get_scene_details_json(self) -> str: def clear(self): """Remove all actors from the renderer and reset the scene. + Also discards the renderer's cross-section plane record, so the next + populate places a default plane on the new scene's bounds. + Holds ``_vtk_lock``: deregistering actors mutates the VTK renderer. """ with self._vtk_lock: if self._scene_graph is not None: self._renderer.deregister_all() + self._renderer.reset_cross_section_plane() self._dataset_registry.clear() self._scene_graph = None self._rebuild_variable_records_from_registry() diff --git a/tests/unit/renderer/test_local_renderer.py b/tests/unit/renderer/test_local_renderer.py index 3ce48138..632ea163 100644 --- a/tests/unit/renderer/test_local_renderer.py +++ b/tests/unit/renderer/test_local_renderer.py @@ -1190,6 +1190,11 @@ def test_set_pixel_scale_pushes_nothing(self, renderer): RECORDED_PLANE_ORIGIN = [7.0, 8.0, 9.0] RECORDED_PLANE_NORMAL = [1.0, 0.0, 0.0] +# What the widget's plane reports on a later populate. Every component +# differs from the seed's origin, so a record left at the first seed fails. +RESEEDED_PLANE_ORIGIN = [10.0, 11.0, 12.0] +RESEEDED_PLANE_NORMAL = [0.0, 0.0, 1.0] + # One literal per object, so asserting these two fails -- rather than # coincides -- if production names the widget, the render window or anything # else. Distinct from the camera's literals above for the same reason. @@ -1211,6 +1216,16 @@ def GetNormal(self): # noqa: N802 return tuple(SEEDED_PLANE_NORMAL) +class _ReseededPlaneDouble: + """Stand-in for the widget's ``vtkPlane`` after a later populate moved the default.""" + + def GetOrigin(self): # noqa: N802 + return tuple(RESEEDED_PLANE_ORIGIN) + + def GetNormal(self): # noqa: N802 + return tuple(RESEEDED_PLANE_NORMAL) + + class _CrossSectionWidgetDouble: """Stand-in for ``VisorCrossSectionWidget``. @@ -1418,4 +1433,105 @@ def test_update_bounds_with_a_record_leaves_the_record_unchanged( assert record.origin == RECORDED_PLANE_ORIGIN assert record.normal == RECORDED_PLANE_NORMAL + # -- update_bounds: a seeded record follows the bounds ------------------ + # + # A seeded record is the default plane, not a placement anyone chose. + # Every mapper clips against it, so it must be re-seeded on every bounds + # update; a record frozen at the first scene's bounds clips away any + # dataset added below it. + + def test_update_bounds_with_a_seeded_record_reseeds_the_record( + self, renderer, widget + ): + """A second update_bounds re-reads the widget's plane over a seeded record.""" + renderer.update_bounds(BOUNDS) + widget._plane = _ReseededPlaneDouble() + + renderer.update_bounds(BOUNDS) + + record = renderer.get_cross_section_plane() + assert record.origin == RESEEDED_PLANE_ORIGIN + assert record.normal == RESEEDED_PLANE_NORMAL + + def test_update_bounds_with_a_seeded_record_writes_nothing_back( + self, renderer, widget + ): + """Over a seeded record, the widget keeps the default its own update_bounds wrote.""" + renderer.update_bounds(BOUNDS) + widget.calls.clear() + + renderer.update_bounds(BOUNDS) + + assert widget.calls == [("update_bounds", BOUNDS)] + + def test_update_bounds_moves_a_seeded_plane_below_a_grown_scene(self, renderer): + """A real widget's clip plane drops to the new z-minimum when the scene grows downward. + + First bounds z 4..6; grown bounds z -1..6, so the default plane origin + is the z-midpoint 2.5 less 1.25 times the half-height 3.5. + """ + widget = VisorCrossSectionWidget(vtkRenderWindowInteractor()) + renderer._cross_section_widget = widget + renderer.update_bounds([-1.0, 1.0, -1.0, 1.0, 4.0, 6.0]) + + renderer.update_bounds([-1.0, 1.0, -1.0, 1.0, -1.0, 6.0]) + + assert widget.plane.GetOrigin() == pytest.approx((0.0, 0.0, -1.875)) + + # -- reset_cross_section_plane ------------------------------------------ + + def test_reset_cross_section_plane_discards_the_record(self, renderer, widget): + """After a reset there is no record, as before the first populate.""" + renderer.sync_cross_section_plane(RECORDED_PLANE_ORIGIN, RECORDED_PLANE_NORMAL) + + renderer.reset_cross_section_plane() + + assert renderer.get_cross_section_plane() is None + + def test_reset_cross_section_plane_writes_no_vtk_object(self, renderer, widget): + """The reset touches only the record; the next populate places the plane.""" + renderer.sync_cross_section_plane(RECORDED_PLANE_ORIGIN, RECORDED_PLANE_NORMAL) + widget.calls.clear() + + renderer.reset_cross_section_plane() + + assert widget.calls == [] + + def test_update_bounds_after_reset_reseeds_an_explicit_record(self, renderer, widget): + """A plane placed before the reset is not restored: the record is seeded from the widget.""" + renderer.sync_cross_section_plane(RECORDED_PLANE_ORIGIN, RECORDED_PLANE_NORMAL) + renderer.reset_cross_section_plane() + + renderer.update_bounds(BOUNDS) + + record = renderer.get_cross_section_plane() + assert record.origin == SEEDED_PLANE_ORIGIN + assert record.normal == SEEDED_PLANE_NORMAL + + def test_update_bounds_after_reset_writes_nothing_back(self, renderer, widget): + """After a reset the widget keeps its own default; the placed plane is not written back.""" + renderer.sync_cross_section_plane(RECORDED_PLANE_ORIGIN, RECORDED_PLANE_NORMAL) + renderer.reset_cross_section_plane() + widget.calls.clear() + + renderer.update_bounds(BOUNDS) + + assert widget.calls == [("update_bounds", BOUNDS)] + + def test_update_bounds_after_reset_places_the_plane_below_the_new_scene(self, renderer): + """A real widget's clip plane leaves a placed position for the new scene's default. + + The plane is placed at z 5, then reset; the new bounds z -1..1 put the + default origin at the z-midpoint 0 less 1.25 times the half-height 1. + """ + widget = VisorCrossSectionWidget(vtkRenderWindowInteractor()) + renderer._cross_section_widget = widget + renderer.update_bounds([-1.0, 1.0, -1.0, 1.0, 4.0, 6.0]) + renderer.sync_cross_section_plane([0.0, 0.0, 5.0], [0.0, 0.0, 1.0]) + renderer.reset_cross_section_plane() + + renderer.update_bounds([-1.0, 1.0, -1.0, 1.0, -1.0, 1.0]) + + assert widget.plane.GetOrigin() == pytest.approx((0.0, 0.0, -1.25)) + diff --git a/tests/unit/vtk/scene/test_base.py b/tests/unit/vtk/scene/test_base.py index 2ee5c064..00ce0e9c 100644 --- a/tests/unit/vtk/scene/test_base.py +++ b/tests/unit/vtk/scene/test_base.py @@ -879,6 +879,22 @@ def test_clear_holds_the_lock(mocked_scene): assert mocked_scene._vtk_lock.enter_count == mocked_scene._vtk_lock.exit_count +def test_clear_resets_the_cross_section_plane(mocked_scene): + """clear() discards the renderer's plane record, so the next populate seeds a default.""" + mocked_scene.clear() + + mocked_scene._renderer.reset_cross_section_plane.assert_called_once_with() + + +def test_clear_resets_the_cross_section_plane_under_the_lock(mocked_scene): + """The plane reset runs with the lock held.""" + observed = _depth_probe(mocked_scene, mocked_scene._renderer, "reset_cross_section_plane") + + mocked_scene.clear() + + assert observed["depth"] >= 1 + + def test_add_dataset_holds_the_lock(mocked_scene): """add_dataset() registers nodes with the lock held.""" subtree = mocked_scene._scene_graph.get_descendant_node.return_value