Files
OneCAD-Draft/src/modules/view/zoom_window.rs
T
PanosandClaude Opus 5 391a74a615 perf: let one tessellation serve every background
Switching to a Layout re-tessellated the whole model — `memo_miss=186455`,
2034 ms — to arrive at geometry identical to what was already in the memo.
The only thing that differed was colour.

`local_wires_for` already knows this: it stores the raw colour and drops
its background argument with `let _ = bg_color;`, noting that
`Batches::finalize` applies `adapt_to_bg` last, "so the cache no longer
has to rebuild on BACKGROUND / layout-switch". The tessellation memo's
guard had not been told, and still mixed the background in, so every
entry was discarded on a switch.

`finalize` now records what its colour resolution consumed — the raw
colour, the raw glyph colours, and the three flags it branches on — as
`WireModel::bg_adapt`, and `resolve_colors_for_bg` replays it for another
background. The guard drops the background, so a memo entry serves any
layout.

The resolution recomputes from the recorded raw colour rather than from
the resolved one, which makes it idempotent and therefore safe over a set
that mixes memo hits with freshly tessellated misses. Re-adapting the
resolved colour would not be safe: `adapt_to_bg` snaps a near-white colour
to pure black on a light background, and pure black comes back pure
*white* on a dark one, losing the tint. A test pins that asymmetry so the
raw colour is not optimised away later.

The contract is tested against the real `finalize` rather than a copy of
its logic: for every background pair, building under one and resolving for
the other lands exactly where building under the other would have.

`bg_adapt` is `None` for wires built anywhere else, and the resolution
skips them, so nothing outside the block cache changes colour. It is also
`None` when the resolution would be a no-op — a preserved colour with no
canvas fill looks the same under every background — which keeps the common
case at no cost.

Ordering is unchanged: the pass runs inside the builder, before
`append_scene_markers` and `apply_refedit_fade`, both of which have their
own separate background dependency.

`wires-build` gains `colors=` so the pass has a number of its own.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-05 18:53:16 +03:00

180 lines
5.3 KiB
Rust

// ZOOM WINDOW command — pick two corners to define the zoom area.
use glam::{DVec3, Vec3};
use crate::t;
use crate::command::{CadCommand, CmdOption, CmdResult};
use crate::modules::{IconKind, ModuleEvent, ToolDef};
use crate::scene::model::wire_model::WireModel;
pub const ICON: IconKind =
IconKind::Svg(include_bytes!("../../../assets/icons/zoom_window.svg"));
/// Ribbon button: zoom into a rectangle picked by two corners.
pub fn tool() -> ToolDef {
ToolDef {
id: "ZOOM_WINDOW",
label: "Zoom Window",
icon: ICON,
event: ModuleEvent::Command("ZOOM WINDOW".to_string()),
}
}
pub struct ZoomWindowCommand {
first: Option<Vec3>,
scale_prompt: bool,
}
impl ZoomWindowCommand {
pub fn new() -> Self {
Self {
first: None,
scale_prompt: false,
}
}
}
impl CadCommand for ZoomWindowCommand {
fn name(&self) -> &'static str {
"ZOOM WINDOW"
}
fn prompt(&self) -> String {
if self.scale_prompt {
t!("ZOOM scale factor (e.g. 2 or 0.5):").into_owned()
} else if self.first.is_none() {
t!("ZOOM WINDOW Specify first corner:").into_owned()
} else {
t!("ZOOM WINDOW Specify opposite corner:").into_owned()
}
}
fn options(&self) -> Vec<CmdOption> {
if self.first.is_some() || self.scale_prompt {
return Vec::new();
}
vec![
CmdOption::new("Window", "W"),
CmdOption::new("Extents", "E"),
CmdOption::new("Previous", "P"),
CmdOption::new("Object", "O"),
CmdOption::new("All", "A"),
CmdOption::new("Dynamic", "D"),
CmdOption::new("Extents All", "EA"),
CmdOption::new("In", "I"),
CmdOption::new("Out", "OUT"),
CmdOption::new("Scale", "S"),
]
}
fn wants_text_input(&self) -> bool {
true
}
fn on_text_input(&mut self, text: &str) -> Option<CmdResult> {
let text = text.trim();
if text.is_empty() {
return None;
}
if self.scale_prompt {
return Some(CmdResult::Dispatch(format!("ZOOM SCALE {text}")));
}
let command = match text.to_uppercase().as_str() {
"W" | "WINDOW" => {
self.first = None;
return Some(CmdResult::NeedPoint);
}
"E" | "EXTENTS" => "ZOOM EXTENTS",
"P" | "PREVIOUS" => "ZOOM PREVIOUS",
"O" | "OBJECT" => "ZOOM OBJECT",
"A" | "ALL" => "ZOOM ALL",
"D" | "DYNAMIC" => "ZOOM DYNAMIC",
"EA" | "EXTENTS ALL" => "ZOOM EXTENTS ALL",
"I" | "IN" => "ZOOM IN",
"OUT" => "ZOOM OUT",
"S" | "SCALE" => {
self.first = None;
self.scale_prompt = true;
return Some(CmdResult::NeedPoint);
}
_ => return None,
};
Some(CmdResult::Dispatch(command.to_string()))
}
fn on_point(&mut self, pt: DVec3) -> CmdResult {
if self.scale_prompt {
return CmdResult::NeedPoint;
}
let pt = pt.as_vec3();
if let Some(p1) = self.first {
CmdResult::ZoomToWindow { p1: p1.as_dvec3(), p2: pt.as_dvec3() }
} else {
self.first = Some(pt);
CmdResult::NeedPoint
}
}
fn on_enter(&mut self) -> CmdResult {
CmdResult::Cancel
}
fn on_escape(&mut self) -> CmdResult {
CmdResult::Cancel
}
fn window_corner_pick(&self) -> bool {
// Both zoom-window corners are free points; Ortho/Polar must not pin
// the opposite corner to an axis or the window collapses to a line
// (#363, same class as #291).
true
}
fn on_mouse_move(&mut self, pt: DVec3) -> Option<WireModel> { let pt = pt.as_vec3();
let p1 = self.first?;
let min = p1.min(pt);
let max = p1.max(pt);
// Draw a rectangle preview
Some(WireModel {
bg_adapt: None,
point_marker: None,
taper_widths: Vec::new(),
pattern_stations: Vec::new(),
world_width: 0.0,
depth_override: None,
display_visible: true,
plot_visible: true,
fill_is_3d: false,
fill_is_2d_solid: false,
render_instance: None,
pick_tris: Vec::new(),
pick_tris_low: Vec::new(),
dash_from_start: false,
dash_align_end: None,
text_verts: Vec::new(),
name: "zoom_window_preview".into(),
points: vec![
[min.x, min.y, 0.0],
[max.x, min.y, 0.0],
[max.x, max.y, 0.0],
[min.x, max.y, 0.0],
[min.x, min.y, 0.0],
],
points_low: Vec::new(),
color: WireModel::CYAN,
selected: false,
pattern_length: 0.0,
pattern: [0.0; 8],
line_weight_px: 1.0,
snap_pts: vec![],
tangent_geoms: vec![],
aci: 0,
key_vertices: vec![],
aabb: WireModel::UNBOUNDED_AABB,
plinegen: true,
fill_tris: vec![],
fill_tris_low: Vec::new(),
})
}
}