Skip to content

Commit 4289947

Browse files
committed
Moved zooming logic into apply layout
1 parent 559fb1f commit 4289947

File tree

1 file changed

+25
-19
lines changed

1 file changed

+25
-19
lines changed

src/local_pathfinding/local_pathfinding/visualizer.py

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -722,8 +722,6 @@ def compute_and_add_state_space(
722722
boat_xy_km: Tuple[float, float],
723723
goal_xy_km: Tuple[float, float],
724724
fig: go.Figure,
725-
zoom_needed: bool,
726-
last_range: Optional[Dict[str, List[float]]],
727725
):
728726
"""
729727
Build the visualization state-space overlay around the boat and goal. Then, add the built
@@ -737,8 +735,6 @@ def compute_and_add_state_space(
737735
boat_xy_km: (x, y) boat position in km.
738736
goal_xy_km: (x, y) goal position in km.
739737
fig: Target Plotly figure.
740-
zoom_needed: whether we want to zoom into the state space
741-
last_range: previously stored axis ranges to maintain axes if zoom not needed.
742738
"""
743739
boat_pos = cs.XY(boat_xy_km[0], boat_xy_km[1])
744740
goal_pos = cs.XY(goal_xy_km[0], goal_xy_km[1])
@@ -762,17 +758,6 @@ def compute_and_add_state_space(
762758
layer="below",
763759
)
764760

765-
if zoom_needed:
766-
fig.update_layout(
767-
xaxis=dict(range=[x_min, x_max], autorange=False),
768-
yaxis=dict(range=[y_min, y_max], autorange=False),
769-
)
770-
elif last_range is not None:
771-
fig.update_layout(
772-
xaxis=dict(range=last_range["x"], autorange=False),
773-
yaxis=dict(range=last_range["y"], autorange=False),
774-
)
775-
776761

777762
def add_goal_change_popup(fig: go.Figure, message: Optional[str]) -> None:
778763
"""
@@ -797,16 +782,24 @@ def add_goal_change_popup(fig: go.Figure, message: Optional[str]) -> None:
797782
)
798783

799784

800-
def apply_layout(fig: go.Figure) -> None:
785+
def apply_layout(
786+
vs: VisualizerState,
787+
fig: go.Figure,
788+
zoom_needed: bool,
789+
last_range: Optional[Dict[str, List[float]]]
790+
) -> None:
801791
"""
802792
Apply the main plot layout configuration (axis titles, domains, legend, and optional ranges).
803793
804794
Args:
805795
fig: Target Plotly figure.
796+
zoom_needed: whether we want to zoom into the state space
797+
last_range: previously stored axis ranges to maintain axes if zoom not needed.
806798
"""
807799
xaxis = dict(domain=[0.0, 0.98])
808800
yaxis = dict(domain=[0.30, 1.0])
809801

802+
# Base Layout
810803
fig.update_layout(
811804
xaxis_title="X (Km)",
812805
yaxis_title="Y (Km)",
@@ -818,6 +811,19 @@ def apply_layout(fig: go.Figure) -> None:
818811
uirevision="constant",
819812
)
820813

814+
# Behavior for zooming into state space / persisting user changes
815+
if zoom_needed:
816+
min_bounds, max_bounds = get_state_space_bounds(vs)
817+
fig.update_layout(
818+
xaxis=dict(range=[min_bounds.x, max_bounds.x], autorange=False),
819+
yaxis=dict(range=[min_bounds.y, max_bounds.y], autorange=False),
820+
)
821+
elif last_range is not None:
822+
fig.update_layout(
823+
xaxis=dict(range=last_range["x"], autorange=False),
824+
yaxis=dict(range=last_range["y"], autorange=False),
825+
)
826+
821827

822828
def build_figure(
823829
vs: VisualizerState,
@@ -903,9 +909,9 @@ def build_figure(
903909
fig.add_annotation(annotation)
904910

905911
# Computing State space overlay and adding it to the plot
906-
zoom_needed = last_goal_xy_km is None or last_range is None
907-
compute_and_add_state_space(vs, boat_xy_km, goal_xy_km, fig, zoom_needed, last_range)
908-
apply_layout(fig)
912+
zoom_needed = last_range is None
913+
compute_and_add_state_space(vs, boat_xy_km, goal_xy_km, fig)
914+
apply_layout(vs, fig, zoom_needed, last_range)
909915
add_goal_change_popup(fig, goal_change.message) # Popup message for goal change
910916
return fig, goal_change.new_goal_xy_rounded
911917

0 commit comments

Comments
 (0)