Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 116 additions & 4 deletions ezpz/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use std::{f64::consts::PI, str::FromStr};
use super::*;
use crate::{
datatypes::{
Angle,
inputs::{DatumCircularArc, DatumPoint},
Angle, AngleKind,
inputs::{DatumCircularArc, DatumLineSegment, DatumPoint},
outputs::Point,
},
textual::{OutcomeAnalysis, Problem},
Expand Down Expand Up @@ -334,6 +334,118 @@ fn angle_constraints() {
}
}

#[test]
fn triangle_with_two_angles_accepts_a_side_length_constraint() {
let mut ids = IdGenerator::default();

let line1 = DatumLineSegment::new(DatumPoint::new(&mut ids), DatumPoint::new(&mut ids));
let line2 = DatumLineSegment::new(DatumPoint::new(&mut ids), DatumPoint::new(&mut ids));
let line3 = DatumLineSegment::new(DatumPoint::new(&mut ids), DatumPoint::new(&mut ids));

let initial_guesses = vec![
(0, 5.12),
(1, -3.84),
(2, 30.36),
(3, 13.16),
(4, -11.5),
(5, 10.93),
(6, -68.72),
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changing the -68 to a -28 makes this test case converge. I think this shows a problem with one of our constraints stability, I think it should be reasonable to find convergence here.

(7, 47.11),
(8, 19.83),
(9, -2.54),
(10, 6.78),
(11, -20.41),
];

let config = Config::default();
let requests_with_size = vec![
// User-defined constraints.
// Make the three lines form a triangle:
ConstraintRequest::new(
Constraint::PointsCoincident(
DatumPoint { x_id: 2, y_id: 3 },
DatumPoint { x_id: 4, y_id: 5 },
),
0,
),
ConstraintRequest::new(
Constraint::PointsCoincident(
DatumPoint { x_id: 6, y_id: 7 },
DatumPoint { x_id: 8, y_id: 9 },
),
0,
),
ConstraintRequest::new(
Constraint::PointsCoincident(
DatumPoint { x_id: 10, y_id: 11 },
DatumPoint { x_id: 0, y_id: 1 },
),
0,
),
// Fix two of the angles:
ConstraintRequest::new(
Constraint::LinesAtAngle(
DatumLineSegment {
p0: DatumPoint { x_id: 8, y_id: 9 },
p1: DatumPoint { x_id: 10, y_id: 11 },
},
DatumLineSegment {
p0: DatumPoint { x_id: 0, y_id: 1 },
p1: DatumPoint { x_id: 2, y_id: 3 },
},
AngleKind::Other(Angle::from_degrees(119.21)),
),
0,
),
ConstraintRequest::new(
Constraint::LinesAtAngle(
DatumLineSegment {
p0: DatumPoint { x_id: 0, y_id: 1 },
p1: DatumPoint { x_id: 2, y_id: 3 },
},
DatumLineSegment {
p0: DatumPoint { x_id: 4, y_id: 5 },
p1: DatumPoint { x_id: 6, y_id: 7 },
},
AngleKind::Perpendicular,
),
0,
),
// Fix the size of one of the triangle's sides..
ConstraintRequest::new(
Constraint::Distance(
DatumPoint { x_id: 4, y_id: 5 },
DatumPoint { x_id: 6, y_id: 7 },
71.24,
),
0,
),
];

let solved_with_size = solve(&requests_with_size, initial_guesses, config)
.expect("triangle with an explicit side length should solve");
assert!(
solved_with_size.is_satisfied(),
"adding the side-length constraint should still produce a satisfied solve"
);

let solved_point = |point: DatumPoint| Point {
x: solved_with_size.final_values[point.id_x() as usize],
y: solved_with_size.final_values[point.id_y() as usize],
};

assert_points_eq(solved_point(line1.p1), solved_point(line2.p0));
assert_points_eq(solved_point(line2.p1), solved_point(line3.p0));
assert_points_eq(solved_point(line3.p1), solved_point(line1.p0));

let solved_line2_start = solved_point(line2.p0);
let solved_line2_end = solved_point(line2.p1);
assert_nearly_eq(
solved_line2_start.euclidean_distance(solved_line2_end),
71.24,
);
}

#[test]
fn perpendicular() {
let solved = run("perpendicular");
Expand Down Expand Up @@ -845,8 +957,8 @@ fn strange_nonconvergence() {
ConstraintRequest::highest_priority(Constraint::PointsCoincident(r, s)),
ConstraintRequest::highest_priority(Constraint::PointsCoincident(q, p)),
ConstraintRequest::highest_priority(Constraint::LinesEqualLength(
datatypes::inputs::DatumLineSegment { p0: q, p1: r },
datatypes::inputs::DatumLineSegment { p0: s, p1: t },
DatumLineSegment { p0: q, p1: r },
DatumLineSegment { p0: s, p1: t },
)),
];
let initial_guesses = vec![
Expand Down
Loading