mirror of
https://github.com/storytold/cadcraft.git
synced 2026-12-03 04:26:43 -05:00
A string in inputs went straight to the angle parser, so empty text and coordinates were refused and the call took the next input. Empty or blank text is now Enter (initget bit 1 still decides), and coordinates are a point in the current UCS in every form the prompt takes (1,1, @1,1, 1<45, *1,1), the relative ones from the last point. Numbers, unit-bearing text and keywords are read as before.
203 lines
10 KiB
Rust
203 lines
10 KiB
Rust
//! `getangle` and `getorient` answered from the `lisp` command's `inputs` return radians (#3398):
|
|
//! an input is read as the same answer at the prompt, a number as that number typed, so the
|
|
//! result works in `polar` and `cos`.
|
|
|
|
use std::f64::consts::{FRAC_PI_2, FRAC_PI_4, PI};
|
|
|
|
use cadcraft_engine::Session;
|
|
use serde_json::{Value, json};
|
|
|
|
/// How far two numbers may differ and still be the same angle or length.
|
|
const TOL: f64 = 1e-9;
|
|
|
|
/// The `lisp` command's result for `code` answered with `inputs`.
|
|
fn run(s: &mut Session, code: &str, inputs: Value) -> Value {
|
|
s.execute("lisp", &json!({ "code": code, "inputs": inputs })).unwrap()
|
|
}
|
|
|
|
/// The printed value of `code` answered with `inputs`.
|
|
fn value(s: &mut Session, code: &str, inputs: Value) -> String {
|
|
let r = run(s, code, inputs);
|
|
r["value"].as_str().unwrap_or_else(|| panic!("{code}: {r}")).to_string()
|
|
}
|
|
|
|
/// How far the number `code` gives is from `want`. LISP takes the difference, at full precision,
|
|
/// since a printed real keeps six digits.
|
|
fn off(s: &mut Session, code: &str, inputs: Value, want: f64) -> f64 {
|
|
let text = value(s, &format!("(- {code} {want:?})"), inputs);
|
|
text.parse::<f64>().unwrap_or_else(|_| panic!("{code}: {text}")).abs()
|
|
}
|
|
|
|
/// A number given as an input is read as that number typed: degrees by default, returned in
|
|
/// radians, as a whole number, a real and a string.
|
|
#[test]
|
|
fn a_number_given_to_getangle_is_degrees_typed_and_comes_back_in_radians() {
|
|
let mut s = Session::new();
|
|
// The report's case: 45 came back as 45.
|
|
assert!(off(&mut s, "(getangle '(0 0) \"Angle: \")", json!([45]), FRAC_PI_4) < TOL);
|
|
assert!(off(&mut s, "(getangle)", json!([22.5]), FRAC_PI_4 / 2.0) < TOL);
|
|
// The same answer as text came back as the string.
|
|
assert!(off(&mut s, "(getangle '(0 0) \"Angle: \")", json!(["45"]), FRAC_PI_4) < TOL);
|
|
}
|
|
|
|
/// The angle `getangle` returns can go straight to `polar`: 10 units along a typed 45 lands on
|
|
/// the 45 degree point.
|
|
#[test]
|
|
fn polar_takes_the_angle_getangle_returns() {
|
|
let mut s = Session::new();
|
|
let (x, y) = (10.0 * FRAC_PI_4.cos(), 10.0 * FRAC_PI_4.sin());
|
|
let code = format!("(distance (polar '(0 0) (getangle '(0 0) \"Angle: \") 10) (list {x:?} {y:?}))");
|
|
assert!(off(&mut s, &code, json!([45]), 0.0) < TOL, "10 units along 45 degrees");
|
|
}
|
|
|
|
/// Points given as inputs are points picked: two of them, or the base point and one, give the
|
|
/// angle of their line, the next call keeps its own input, and a lone first point gives nil.
|
|
#[test]
|
|
fn points_given_to_getangle_give_the_angle_of_their_line() {
|
|
let mut s = Session::new();
|
|
// Two points, without a base point.
|
|
assert!(off(&mut s, "(getangle)", json!([[1, 1], [2, 3]]), 2f64.atan2(1.0)) < TOL);
|
|
// The base point and one point.
|
|
assert!(off(&mut s, "(getangle '(1 1))", json!([[1, 2]]), FRAC_PI_2) < TOL);
|
|
// The call after it still gets its own answer.
|
|
assert_eq!(value(&mut s, "(list (equal (getangle) (/ pi 4) 1e-9) (getreal))", json!([[0, 0], [5, 5], 2.5])), "(T 2.5)");
|
|
// A first point with no second to follow: no angle.
|
|
assert_eq!(value(&mut s, "(getangle)", json!([[1, 1]])), "nil");
|
|
}
|
|
|
|
/// A null input is Enter: nil from both functions, and it answers one call only.
|
|
#[test]
|
|
fn enter_given_to_getangle_is_nil() {
|
|
let mut s = Session::new();
|
|
assert_eq!(value(&mut s, "(getangle '(0 0) \"Angle: \")", json!([null])), "nil");
|
|
assert_eq!(value(&mut s, "(getorient)", json!([null])), "nil");
|
|
// Enter answers one call only.
|
|
assert_eq!(value(&mut s, "(list (getangle) (equal (getangle) (/ pi 6) 1e-9))", json!([null, 30])), "(nil T)");
|
|
}
|
|
|
|
/// Text given as an input takes the unit forms the command line takes: radians, grads, degrees
|
|
/// and degrees with minutes.
|
|
#[test]
|
|
fn text_given_to_getangle_takes_the_unit_forms_the_command_line_takes() {
|
|
let mut s = Session::new();
|
|
for (text, want) in [("0.5r", 0.5), ("50g", FRAC_PI_4), ("45d", FRAC_PI_4), ("30d30'", 30.5f64.to_radians())] {
|
|
assert!(off(&mut s, "(getangle)", json!([text]), want) < TOL, "{text}");
|
|
}
|
|
}
|
|
|
|
/// A bare number is read in the drawing's angular units (AUNITS): radians and grads, while a
|
|
/// form that names its unit keeps it.
|
|
#[test]
|
|
fn a_number_given_to_getangle_is_read_in_aunits() {
|
|
let mut s = Session::new();
|
|
// Radians.
|
|
assert!(off(&mut s, "(progn (setvar \"AUNITS\" 3) (getangle))", json!([0.5]), 0.5) < TOL);
|
|
assert!(off(&mut s, "(getorient)", json!([0.5]), 0.5) < TOL);
|
|
// Grads.
|
|
assert!(off(&mut s, "(progn (setvar \"AUNITS\" 2) (getangle))", json!([50]), FRAC_PI_4) < TOL);
|
|
// A unit typed with the number still wins.
|
|
assert!(off(&mut s, "(getangle)", json!(["45d"]), FRAC_PI_4) < TOL);
|
|
}
|
|
|
|
/// With the zero direction turned north (ANGBASE), `getangle` measures from it and `getorient`
|
|
/// from east, for a typed angle and for a picked point.
|
|
#[test]
|
|
fn getorient_returns_radians_from_east_whatever_angbase_is() {
|
|
let mut s = Session::new();
|
|
assert!(off(&mut s, "(getorient '(0 0) \"Angle: \")", json!([45]), FRAC_PI_4) < TOL);
|
|
assert!(off(&mut s, "(getorient '(0 0))", json!([[-1, 0]]), PI) < TOL);
|
|
// ANGBASE north: 45 typed is 45 degrees from north. `getangle` returns the angle from
|
|
// ANGBASE, `getorient` the direction from east.
|
|
assert!(off(&mut s, "(setvar \"ANGBASE\" (/ pi 2))", json!([]), FRAC_PI_2) < TOL);
|
|
assert!(off(&mut s, "(getangle)", json!([45]), FRAC_PI_4) < TOL);
|
|
assert!(off(&mut s, "(getorient)", json!([45]), 3.0 * FRAC_PI_4) < TOL);
|
|
// A point west of the base: 90 degrees from north, 180 from east.
|
|
assert!(off(&mut s, "(getangle '(0 0))", json!([[-1, 0]]), FRAC_PI_2) < TOL);
|
|
assert!(off(&mut s, "(getorient '(0 0))", json!([[-1, 0]]), PI) < TOL);
|
|
}
|
|
|
|
/// An input the prompt would refuse is reported in the output and the next input is taken, as a
|
|
/// prompt asks again; `initget` bits and keywords apply.
|
|
#[test]
|
|
fn an_answer_getangle_refuses_is_reported_and_the_next_one_taken() {
|
|
let mut s = Session::new();
|
|
let r = run(&mut s, "(equal (getangle) (/ pi 6) 1e-9)", json!(["north-ish", 30]));
|
|
assert_eq!(r["value"], "T", "{r}");
|
|
let output = r["output"].as_array().unwrap();
|
|
assert!(output.iter().any(|l| l.as_str().is_some_and(|l| l.contains("angle"))), "the refusal is in the output: {r}");
|
|
// `initget` still applies: zero refused, then a keyword.
|
|
let code = "(progn (initget 2 \"Reference\") (getangle))";
|
|
assert_eq!(value(&mut s, code, json!([0, "r"])), "\"Reference\"");
|
|
}
|
|
|
|
/// Empty or blank text given as an input is Enter, as an empty line is at the prompt: nil, and
|
|
/// the next call keeps its own input. With `initget` bit 1 Enter is refused, and the next input
|
|
/// is the answer.
|
|
#[test]
|
|
fn empty_text_given_to_getangle_is_enter() {
|
|
let mut s = Session::new();
|
|
assert_eq!(value(&mut s, "(list (getangle) (getreal))", json!(["", 30])), "(nil 30)");
|
|
assert_eq!(value(&mut s, "(list (getorient '(0 0)) (getreal))", json!([" ", 30])), "(nil 30)");
|
|
// Enter is not allowed: the empty text is refused as Enter is, and 30 answers the call.
|
|
let r = run(&mut s, "(progn (initget 1) (list (equal (getangle) (/ pi 6) 1e-9) (getreal)))", json!(["", 30, 2.5]));
|
|
assert_eq!(r["value"], "(T 2.5)", "{r}");
|
|
let null = run(&mut s, "(progn (initget 1) (list (equal (getangle) (/ pi 6) 1e-9) (getreal)))", json!([null, 30, 2.5]));
|
|
assert_eq!(r["output"], null["output"], "refused with the words Enter is refused with");
|
|
}
|
|
|
|
/// Coordinates given as text are a point, as they are typed at the prompt: with the base point
|
|
/// they give the angle of the line, two of them do without one, and the next call keeps its own
|
|
/// input.
|
|
#[test]
|
|
fn coordinate_text_given_to_getangle_is_a_point() {
|
|
let mut s = Session::new();
|
|
assert_eq!(value(&mut s, "(list (equal (getangle '(0 0)) (/ pi 4) 1e-9) (getreal))", json!(["1,1", 2.5])), "(T 2.5)");
|
|
assert!(off(&mut s, "(getangle)", json!(["1,1", "2,3"]), 2f64.atan2(1.0)) < TOL);
|
|
assert!(off(&mut s, "(getorient '(1 1))", json!(["1,2"]), FRAC_PI_2) < TOL);
|
|
// The polar and relative forms are points as well (the last point is the origin here).
|
|
assert!(off(&mut s, "(getangle '(0 0))", json!(["1<45"]), FRAC_PI_4) < TOL);
|
|
assert!(off(&mut s, "(getangle '(0 0))", json!(["@1,1"]), FRAC_PI_4) < TOL);
|
|
// A keyword that also reads as a point (`@`, the last point) stays the keyword, as at the prompt.
|
|
assert_eq!(value(&mut s, "(progn (initget \"@\") (getangle '(1 1)))", json!(["@"])), "\"@\"");
|
|
}
|
|
|
|
/// The angle `text` gives when typed at the prompt of `call`, and how far the angle it gives as
|
|
/// an input is from that.
|
|
fn typed_and_given(s: &mut Session, call: &str, text: &str) -> (String, f64) {
|
|
s.cmdline(&format!("(setq typed {call})")).unwrap();
|
|
s.cmdline(text).unwrap();
|
|
let typed = value(s, "typed", json!([]));
|
|
let apart = value(s, &format!("(abs (- {call} typed))"), json!([text]));
|
|
let apart = apart.parse().unwrap_or_else(|_| panic!("{call} {text}: {apart} (typed: {typed})"));
|
|
(typed, apart)
|
|
}
|
|
|
|
/// Every form of point and angle text gives, as an input, the angle it gives typed at the
|
|
/// prompt: absolute, relative to the last point, polar, world coordinates, bare and unit-bearing
|
|
/// angles; also with the zero direction north and in a moved, turned UCS.
|
|
#[test]
|
|
fn text_given_to_getangle_is_read_as_the_same_text_typed_at_the_prompt() {
|
|
const TEXTS: [&str; 12] = ["1,1", "3,4,5", "@1,1", "@2<30", "@", "1<45", "2<<30", "#3,4", "*1,2", "*@1,2", "45", "0.5r"];
|
|
let mut s = Session::new();
|
|
s.last_point = cadcraft_geom::Vec2::new(5.0, 2.0);
|
|
for text in TEXTS {
|
|
for call in ["(getangle '(1 0))", "(getorient '(1 0))"] {
|
|
let (typed, apart) = typed_and_given(&mut s, call, text);
|
|
assert!(apart < TOL, "{call} {text}: typed {typed}, {apart} apart");
|
|
}
|
|
}
|
|
// The relative forms count from the last point, not the origin.
|
|
let (typed, _) = typed_and_given(&mut s, "(getangle '(5 2))", "@1,1");
|
|
assert_eq!(typed, value(&mut s, "(/ pi 4)", json!([])));
|
|
// North is zero, and the UCS sits at (100, 50) turned a quarter turn.
|
|
value(&mut s, "(setvar \"ANGBASE\" (/ pi 2))", json!([]));
|
|
s.execute("ucs", &json!({"option": "origin", "origin": [100, 50]})).unwrap();
|
|
s.execute("ucs", &json!({"option": "z", "angle": 90})).unwrap();
|
|
for text in TEXTS {
|
|
for call in ["(getangle '(1 0))", "(getorient '(1 0))"] {
|
|
let (typed, apart) = typed_and_given(&mut s, call, text);
|
|
assert!(apart < TOL, "ANGBASE and UCS, {call} {text}: typed {typed}, {apart} apart");
|
|
}
|
|
}
|
|
}
|