//! Replay oracle recipes and compare with Fusion's measurements; import oracle STEP files and //! compare them with the same measurements. use serde_json::{Value, json}; use crate::{read_json, recipe, seams}; /// Relative tolerance for volume and area (our curved-face tessellation is within ~2e-4). pub const REL_TOL: f64 = 1e-3; /// Cases compared loosely (relative tolerance on volume and area, topology not compared), and why. const APPROXIMATE: [(&str, f64, &str); 1] = [( "49-helical-sweep", 5e-3, "Fusion sweeps along a spline fitted through helix points; SolveCraft builds the true helix (a coil), so the size is compared within 0.5% and the topology not at all", )]; fn check_case(dir: &str) -> Value { let name = std::path::Path::new(dir).file_name().map(|x| x.to_string_lossy().to_string()).unwrap_or_default(); let rp = format!("{dir}/recipe.json"); let mp = format!("{dir}/measure.json"); let (recipe, want) = match (read_json(&rp), read_json(&mp)) { (Ok(r), Ok(m)) => (r, m), (Err(e), _) | (_, Err(e)) => return json!({"case": name, "pass": false, "error": e}), }; let script = match recipe::to_script(&recipe) { Ok(s) => s, Err(e) => return json!({"case": name, "pass": false, "skipped": true, "error": e}), }; let mut s = solvecraft_engine::Session::default(); if let Err(e) = s.run_script(&script) { return json!({"case": name, "pass": false, "error": e.to_string()}); } let got = match s.execute("MeasureCommand", &json!({})) { Ok(m) => m, Err(e) => return json!({"case": name, "pass": false, "error": e.to_string()}), }; let mut checks = Vec::new(); let mut pass = true; let mut num = |what: &str, g: Option, w: Option, rel: f64| { let ok = match (g, w) { (Some(g), Some(w)) => (g - w).abs() <= rel * w.abs().max(1.0), _ => false, }; pass &= ok; checks.push(json!({"check": what, "got": g, "want": w, "ok": ok})); }; // Cases where Fusion's part is itself an approximation: compare size loosely, not topology. let approx = APPROXIMATE.iter().find(|(c, _, _)| *c == name); let tol = approx.map(|(_, t, _)| *t).unwrap_or(REL_TOL); let within = |k: &str, rel: f64| match (got["total"][k].as_f64(), want["total"][k].as_f64()) { (Some(g), Some(w)) => (g - w).abs() <= rel * w.abs().max(1.0), _ => false, }; let size_ok = got["body_count"] == want["body_count"] && within("volume_mm3", tol) && within("area_mm2", tol); num("body_count", got["body_count"].as_f64(), want["body_count"].as_f64(), 0.0); num("volume_mm3", got["total"]["volume_mm3"].as_f64(), want["total"]["volume_mm3"].as_f64(), tol); num("area_mm2", got["total"]["area_mm2"].as_f64(), want["total"]["area_mm2"].as_f64(), tol); let mut seam_note = None; if approx.is_none() { let exact = ["faces", "edges", "vertices"].iter().all(|k| got["total"][k].as_f64().is_some() && got["total"][k] == want["total"][k]); let fusion_seams = want["bodies"].as_array().and_then(|bs| { bs.iter().map(seams::normalised).try_fold((0, 0, 0), |a, n| n.map(|n| (a.0 + n.0, a.1 + n.1, a.2 + n.2))) }); match fusion_seams { // Size matches and only the topology differs: compare with Fusion's seam splits merged. Some((f, e, v)) if size_ok && !exact => { let raw: Vec = ["faces", "edges", "vertices"] .iter() .map(|k| { let ours: i64 = got["bodies"].as_array().into_iter().flatten().filter_map(|b| b["kernel"][k].as_i64()).sum(); format!("{k} {ours}/{}", want["total"][k]) }) .collect(); seam_note = Some(format!( "topology compared seam-normalised: Fusion's {}/{}/{} faces/edges/vertices become {f}/{e}/{v} with closed-surface patches counted once and seam edges and vertices dropped (raw SolveCraft/Fusion: {})", want["total"]["faces"], want["total"]["edges"], want["total"]["vertices"], raw.join(", ") )); num("faces (seam-normalised)", got["total"]["faces"].as_f64(), Some(f as f64), 0.0); num("edges (seam-normalised)", got["total"]["edges"].as_f64(), Some(e as f64), 0.0); num("vertices (seam-normalised)", got["total"]["vertices"].as_f64(), Some(v as f64), 0.0); } _ => { num("faces", got["total"]["faces"].as_f64(), want["total"]["faces"].as_f64(), 0.0); num("edges", got["total"]["edges"].as_f64(), want["total"]["edges"].as_f64(), 0.0); num("vertices", got["total"]["vertices"].as_f64(), want["total"]["vertices"].as_f64(), 0.0); } } } // Our STEP export of the rebuilt part: well formed, and read back with no warnings and the // same volume. let (ok, note) = step_export_check(&s, got["total"]["volume_mm3"].as_f64().unwrap_or(0.0)); pass &= ok; checks.push(json!({"check": "step_export", "got": note, "want": "valid, re-imports cleanly", "ok": ok})); let mut out = json!({"case": name, "pass": pass, "checks": checks}); if let Some((_, _, why)) = approx { out["note"] = json!(why); } if let Some(n) = seam_note { out["note"] = json!(n); } let effective: Vec = recipe["features"] .as_array() .into_iter() .flatten() .filter(|f| f.get("fusion_effective_extent").is_some()) .map(|f| f["name"].as_str().unwrap_or("?").to_string()) .collect(); if !effective.is_empty() { out["note"] = json!(format!( "known Fusion difference: {} replayed with the extent Fusion actually used (its through-all is sized as 1.1 × the farthest vertex, so curved tops stop it short; SolveCraft's through-all goes truly through)", effective.join(", ") )); } out } /// Export the session's bodies as STEP, validate the file and import it again. fn step_export_check(s: &solvecraft_engine::Session, volume: f64) -> (bool, String) { let st = s.model.state(); let bytes = match solvecraft_engine::io::export(&st, &[], solvecraft_engine::io::Format::Step, "oracle") { Ok(b) => b, Err(e) => return (false, format!("export: {e}")), }; let text = String::from_utf8_lossy(&bytes); if let Err(e) = solvecraft_engine::kernel::step_validate(&text) { return (false, format!("invalid: {e}")); } match solvecraft_engine::kernel::step_orientation_errors(&text) { Ok(errs) if !errs.is_empty() => return (false, format!("faces inside out: {}", errs.join("; "))), Err(e) => return (false, format!("orientation check: {e}")), Ok(_) => {} } match solvecraft_engine::kernel::step_import(&text) { Ok(imp) if !imp.warnings.is_empty() => (false, format!("warnings: {}", imp.warnings.join("; "))), Ok(imp) if imp.bodies.len() != st.bodies.len() => (false, format!("{} bodies back of {}", imp.bodies.len(), st.bodies.len())), Ok(imp) => { let m: Vec<_> = imp.bodies.iter().filter_map(|b| solvecraft_engine::kernel::measure(&b.body).ok()).collect(); let m0: Vec<_> = st.bodies.iter().filter_map(|b| solvecraft_engine::kernel::measure(&b.body).ok()).collect(); let v: f64 = m.iter().map(|x| x.volume).sum(); let (a, a0): (f64, f64) = (m.iter().map(|x| x.area).sum(), m0.iter().map(|x| x.area).sum()); let faces: usize = imp.bodies.iter().map(|b| b.file_faces).sum(); // Pieces of one analytic surface are written as one face: compare with the merged count. let faces0: usize = m0.iter().map(|x| x.merged.faces).sum(); let close = |x: f64, y: f64| (x - y).abs() <= REL_TOL * y.abs().max(1.0); let faces_raw: usize = st.bodies.iter().map(|b| b.body.face_count()).sum(); let ok = close(v, volume) && close(a, a0) && faces <= faces_raw && faces >= faces0.min(faces_raw); (ok, format!("re-imported volume {v:.3} (was {volume:.3}), area {a:.3} (was {a0:.3}), faces {faces} (was {faces0})")) } Err(e) => (false, format!("import: {e}")), } } /// Cases whose Fusion area is not the geometric one: Fusion reports the horn torus face of a /// tight sweep (tube touching its axis) at three times π²·R·r, its quarter-turn area; the /// volumes agree. Their area is not compared. const FUSION_AREA_OFF: &[&str] = &["50-sweep-tight-bend"]; /// Import a case's `part.step` (as File → Open does) and compare with Fusion's measurements: /// body count, volume and area (relative 1e-3) and the faces read from the file. fn check_step_case(dir: &str) -> Value { let name = std::path::Path::new(dir).file_name().map(|x| x.to_string_lossy().to_string()).unwrap_or_default(); let sp = format!("{dir}/part.step"); let want = match read_json(&format!("{dir}/measure.json")) { Ok(m) => m, Err(e) => return json!({"case": name, "pass": false, "error": e}), }; let t0 = std::time::Instant::now(); let mut s = solvecraft_engine::Session::default(); let opened = match s.execute("doc.open", &json!({"path": sp})) { Ok(v) => v, Err(e) => return json!({"case": name, "pass": false, "error": e.to_string()}), }; let got = match s.execute("MeasureCommand", &json!({})) { Ok(m) => m, Err(e) => return json!({"case": name, "pass": false, "error": e.to_string()}), }; let ms = t0.elapsed().as_secs_f64() * 1000.0; // Faces read from the file (the kernel may split a face that wraps around a closed surface). let file_faces = std::fs::read(&sp) .ok() .and_then(|b| solvecraft_engine::kernel::step_import_shared(&String::from_utf8_lossy(&b)).ok()) .map(|imp| imp.bodies.iter().map(|b| b.file_faces).sum::() as f64); let mut checks = Vec::new(); let mut pass = true; let mut num = |what: &str, g: Option, w: Option, rel: f64| { let ok = match (g, w) { (Some(g), Some(w)) => (g - w).abs() <= rel * w.abs().max(1.0), _ => false, }; pass &= ok; checks.push(json!({"check": what, "got": g, "want": w, "ok": ok})); }; num("body_count", got["body_count"].as_f64(), want["body_count"].as_f64(), 0.0); num("volume_mm3", got["total"]["volume_mm3"].as_f64(), want["total"]["volume_mm3"].as_f64(), REL_TOL); if !FUSION_AREA_OFF.contains(&name.as_str()) { num("area_mm2", got["total"]["area_mm2"].as_f64(), want["total"]["area_mm2"].as_f64(), REL_TOL); } num("faces", file_faces, want["total"]["faces"].as_f64(), 0.0); json!({ "case": name, "pass": pass, "checks": checks, "ms": ms, "bodies": opened["bodies"], "warnings": opened["warnings"], "info": {"kernel_faces_merged": got["total"]["faces"], "edges": got["total"]["edges"], "want_edges": want["total"]["edges"], "vertices": got["total"]["vertices"], "want_vertices": want["total"]["vertices"]}, }) } pub fn step_corpus(args: &[String]) -> Result<(), String> { let dirs: Vec<&String> = args.iter().filter(|a| !a.starts_with("--")).collect(); if dirs.is_empty() { return Err("step-corpus: give one or more case directories (each with part.step and measure.json)".into()); } let results: Vec = dirs.iter().map(|d| check_step_case(d)).collect(); report(args, &results); let failed = results.iter().filter(|r| !r["pass"].as_bool().unwrap_or(false)).count(); if failed > 0 { Err(format!("{failed} STEP case(s) failed")) } else { Ok(()) } } fn report(args: &[String], results: &[Value]) { if args.iter().any(|a| a == "--json") { println!("{}", serde_json::to_string_pretty(&Value::Array(results.to_vec())).unwrap_or_default()); return; } for r in results { let status = if r["pass"].as_bool().unwrap_or(false) { "PASS" } else if r["skipped"].as_bool().unwrap_or(false) { "SKIP" } else { "FAIL" }; let detail: Vec = r["checks"] .as_array() .into_iter() .flatten() .filter(|c| !c["ok"].as_bool().unwrap_or(false)) .map(|c| format!("{} got {} want {}", c["check"].as_str().unwrap_or(""), c["got"], c["want"])) .collect(); let err = r["error"].as_str().map(|e| format!(" ({e})")).unwrap_or_default(); println!( "{status} {}{err}{}", r["case"].as_str().unwrap_or(""), if detail.is_empty() { String::new() } else { format!(": {}", detail.join("; ")) } ); } } pub fn run(args: &[String]) -> Result<(), String> { let dirs: Vec<&String> = args.iter().filter(|a| !a.starts_with("--")).collect(); if dirs.is_empty() { return Err("oracle: give one or more case directories".into()); } let results: Vec = dirs.iter().map(|d| check_case(d)).collect(); report(args, &results); let failed = results.iter().filter(|r| !r["pass"].as_bool().unwrap_or(false) && !r["skipped"].as_bool().unwrap_or(false)).count(); if failed > 0 { Err(format!("{failed} oracle case(s) failed")) } else { Ok(()) } }