mirror of
https://github.com/GraphiteEditor/Graphite.git
synced 2025-08-04 13:30:48 +00:00
Put all #[test]
in a mod tests
(#2728)
workspace: put all `#[test]` in a `mod tests`
This commit is contained in:
parent
579bedd9ff
commit
006209c5d0
12 changed files with 748 additions and 696 deletions
|
@ -91,44 +91,48 @@ pub fn get_current_normalized_pivot(inputs: &[NodeInput]) -> DVec2 {
|
|||
if let Some(&TaggedValue::DVec2(pivot)) = inputs[5].as_value() { pivot } else { DVec2::splat(0.5) }
|
||||
}
|
||||
|
||||
/// 
|
||||
///
|
||||
/// Source:
|
||||
/// ```tex
|
||||
/// \begin{bmatrix}
|
||||
/// S_{x}\cos(\theta)-S_{y}\sin(\theta)H_{y} & S_{x}\cos(\theta)H_{x}-S_{y}\sin(\theta) & T_{x}\\
|
||||
/// S_{x}\sin(\theta)+S_{y}\cos(\theta)H_{y} & S_{x}\sin(\theta)H_{x}+S_{y}\cos(\theta) & T_{y}\\
|
||||
/// 0 & 0 & 1
|
||||
/// \end{bmatrix}
|
||||
/// ```
|
||||
#[test]
|
||||
fn derive_transform() {
|
||||
for shear_x in -10..=10 {
|
||||
let shear_x = (shear_x as f64) / 2.;
|
||||
for angle in (0..=360).step_by(15) {
|
||||
let angle = (angle as f64).to_radians();
|
||||
for scale_x in 1..10 {
|
||||
let scale_x = (scale_x as f64) / 5.;
|
||||
for scale_y in 1..10 {
|
||||
let scale_y = (scale_y as f64) / 5.;
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
/// 
|
||||
///
|
||||
/// Source:
|
||||
/// ```tex
|
||||
/// \begin{bmatrix}
|
||||
/// S_{x}\cos(\theta)-S_{y}\sin(\theta)H_{y} & S_{x}\cos(\theta)H_{x}-S_{y}\sin(\theta) & T_{x}\\
|
||||
/// S_{x}\sin(\theta)+S_{y}\cos(\theta)H_{y} & S_{x}\sin(\theta)H_{x}+S_{y}\cos(\theta) & T_{y}\\
|
||||
/// 0 & 0 & 1
|
||||
/// \end{bmatrix}
|
||||
/// ```
|
||||
#[test]
|
||||
fn derive_transform() {
|
||||
for shear_x in -10..=10 {
|
||||
let shear_x = (shear_x as f64) / 2.;
|
||||
for angle in (0..=360).step_by(15) {
|
||||
let angle = (angle as f64).to_radians();
|
||||
for scale_x in 1..10 {
|
||||
let scale_x = (scale_x as f64) / 5.;
|
||||
for scale_y in 1..10 {
|
||||
let scale_y = (scale_y as f64) / 5.;
|
||||
|
||||
let shear = DVec2::new(shear_x, 0.);
|
||||
let scale = DVec2::new(scale_x, scale_y);
|
||||
let translate = DVec2::new(5666., 644.);
|
||||
let shear = DVec2::new(shear_x, 0.);
|
||||
let scale = DVec2::new(scale_x, scale_y);
|
||||
let translate = DVec2::new(5666., 644.);
|
||||
|
||||
let original_transform = DAffine2::from_cols(
|
||||
DVec2::new(scale.x * angle.cos() - scale.y * angle.sin() * shear.y, scale.x * angle.sin() + scale.y * angle.cos() * shear.y),
|
||||
DVec2::new(scale.x * angle.cos() * shear.x - scale.y * angle.sin(), scale.x * angle.sin() * shear.x + scale.y * angle.cos()),
|
||||
translate,
|
||||
);
|
||||
let original_transform = DAffine2::from_cols(
|
||||
DVec2::new(scale.x * angle.cos() - scale.y * angle.sin() * shear.y, scale.x * angle.sin() + scale.y * angle.cos() * shear.y),
|
||||
DVec2::new(scale.x * angle.cos() * shear.x - scale.y * angle.sin(), scale.x * angle.sin() * shear.x + scale.y * angle.cos()),
|
||||
translate,
|
||||
);
|
||||
|
||||
let (new_scale, new_angle, new_translation, new_shear) = compute_scale_angle_translation_shear(original_transform);
|
||||
let new_transform = DAffine2::from_scale_angle_translation(new_scale, new_angle, new_translation) * DAffine2::from_cols_array(&[1., new_shear.y, new_shear.x, 1., 0., 0.]);
|
||||
let (new_scale, new_angle, new_translation, new_shear) = compute_scale_angle_translation_shear(original_transform);
|
||||
let new_transform = DAffine2::from_scale_angle_translation(new_scale, new_angle, new_translation) * DAffine2::from_cols_array(&[1., new_shear.y, new_shear.x, 1., 0., 0.]);
|
||||
|
||||
assert!(
|
||||
new_transform.abs_diff_eq(original_transform, 1e-10),
|
||||
"original_transform {original_transform} new_transform {new_transform} / scale {scale} new_scale {new_scale} / angle {angle} new_angle {new_angle} / shear {shear} / new_shear {new_shear}",
|
||||
);
|
||||
assert!(
|
||||
new_transform.abs_diff_eq(original_transform, 1e-10),
|
||||
"original_transform {original_transform} new_transform {new_transform} / scale {scale} new_scale {new_scale} / angle {angle} new_angle {new_angle} / shear {shear} / new_shear {new_shear}",
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -522,49 +522,53 @@ pub struct NodeRelations {
|
|||
// Helper functions
|
||||
// ================
|
||||
|
||||
#[test]
|
||||
fn test_tree() {
|
||||
let mut metadata = DocumentMetadata::default();
|
||||
let root = LayerNodeIdentifier::ROOT_PARENT;
|
||||
let metadata = &mut metadata;
|
||||
root.push_child(metadata, LayerNodeIdentifier::new_unchecked(NodeId(3)));
|
||||
assert_eq!(root.children(metadata).collect::<Vec<_>>(), vec![LayerNodeIdentifier::new_unchecked(NodeId(3))]);
|
||||
root.push_child(metadata, LayerNodeIdentifier::new_unchecked(NodeId(6)));
|
||||
assert_eq!(root.children(metadata).map(LayerNodeIdentifier::to_node).collect::<Vec<_>>(), vec![NodeId(3), NodeId(6)]);
|
||||
assert_eq!(root.descendants(metadata).map(LayerNodeIdentifier::to_node).collect::<Vec<_>>(), vec![NodeId(3), NodeId(6)]);
|
||||
LayerNodeIdentifier::new_unchecked(NodeId(3)).add_after(metadata, LayerNodeIdentifier::new_unchecked(NodeId(4)));
|
||||
LayerNodeIdentifier::new_unchecked(NodeId(3)).add_before(metadata, LayerNodeIdentifier::new_unchecked(NodeId(2)));
|
||||
LayerNodeIdentifier::new_unchecked(NodeId(6)).add_before(metadata, LayerNodeIdentifier::new_unchecked(NodeId(5)));
|
||||
LayerNodeIdentifier::new_unchecked(NodeId(6)).add_after(metadata, LayerNodeIdentifier::new_unchecked(NodeId(9)));
|
||||
LayerNodeIdentifier::new_unchecked(NodeId(6)).push_child(metadata, LayerNodeIdentifier::new_unchecked(NodeId(8)));
|
||||
LayerNodeIdentifier::new_unchecked(NodeId(6)).push_front_child(metadata, LayerNodeIdentifier::new_unchecked(NodeId(7)));
|
||||
root.push_front_child(metadata, LayerNodeIdentifier::new_unchecked(NodeId(1)));
|
||||
assert_eq!(
|
||||
root.children(metadata).map(LayerNodeIdentifier::to_node).collect::<Vec<_>>(),
|
||||
vec![NodeId(1), NodeId(2), NodeId(3), NodeId(4), NodeId(5), NodeId(6), NodeId(9)]
|
||||
);
|
||||
assert_eq!(
|
||||
root.descendants(metadata).map(LayerNodeIdentifier::to_node).collect::<Vec<_>>(),
|
||||
vec![NodeId(1), NodeId(2), NodeId(3), NodeId(4), NodeId(5), NodeId(6), NodeId(7), NodeId(8), NodeId(9)]
|
||||
);
|
||||
assert_eq!(
|
||||
root.descendants(metadata).map(LayerNodeIdentifier::to_node).rev().collect::<Vec<_>>(),
|
||||
vec![NodeId(9), NodeId(8), NodeId(7), NodeId(6), NodeId(5), NodeId(4), NodeId(3), NodeId(2), NodeId(1)]
|
||||
);
|
||||
assert!(root.children(metadata).all(|child| child.parent(metadata) == Some(root)));
|
||||
LayerNodeIdentifier::new_unchecked(NodeId(6)).delete(metadata);
|
||||
LayerNodeIdentifier::new_unchecked(NodeId(1)).delete(metadata);
|
||||
LayerNodeIdentifier::new_unchecked(NodeId(9)).push_child(metadata, LayerNodeIdentifier::new_unchecked(NodeId(10)));
|
||||
assert_eq!(
|
||||
root.children(metadata).map(LayerNodeIdentifier::to_node).collect::<Vec<_>>(),
|
||||
vec![NodeId(2), NodeId(3), NodeId(4), NodeId(5), NodeId(9)]
|
||||
);
|
||||
assert_eq!(
|
||||
root.descendants(metadata).map(LayerNodeIdentifier::to_node).collect::<Vec<_>>(),
|
||||
vec![NodeId(2), NodeId(3), NodeId(4), NodeId(5), NodeId(9), NodeId(10)]
|
||||
);
|
||||
assert_eq!(
|
||||
root.descendants(metadata).map(LayerNodeIdentifier::to_node).rev().collect::<Vec<_>>(),
|
||||
vec![NodeId(10), NodeId(9), NodeId(5), NodeId(4), NodeId(3), NodeId(2)]
|
||||
);
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
#[test]
|
||||
fn test_tree() {
|
||||
let mut metadata = DocumentMetadata::default();
|
||||
let root = LayerNodeIdentifier::ROOT_PARENT;
|
||||
let metadata = &mut metadata;
|
||||
root.push_child(metadata, LayerNodeIdentifier::new_unchecked(NodeId(3)));
|
||||
assert_eq!(root.children(metadata).collect::<Vec<_>>(), vec![LayerNodeIdentifier::new_unchecked(NodeId(3))]);
|
||||
root.push_child(metadata, LayerNodeIdentifier::new_unchecked(NodeId(6)));
|
||||
assert_eq!(root.children(metadata).map(LayerNodeIdentifier::to_node).collect::<Vec<_>>(), vec![NodeId(3), NodeId(6)]);
|
||||
assert_eq!(root.descendants(metadata).map(LayerNodeIdentifier::to_node).collect::<Vec<_>>(), vec![NodeId(3), NodeId(6)]);
|
||||
LayerNodeIdentifier::new_unchecked(NodeId(3)).add_after(metadata, LayerNodeIdentifier::new_unchecked(NodeId(4)));
|
||||
LayerNodeIdentifier::new_unchecked(NodeId(3)).add_before(metadata, LayerNodeIdentifier::new_unchecked(NodeId(2)));
|
||||
LayerNodeIdentifier::new_unchecked(NodeId(6)).add_before(metadata, LayerNodeIdentifier::new_unchecked(NodeId(5)));
|
||||
LayerNodeIdentifier::new_unchecked(NodeId(6)).add_after(metadata, LayerNodeIdentifier::new_unchecked(NodeId(9)));
|
||||
LayerNodeIdentifier::new_unchecked(NodeId(6)).push_child(metadata, LayerNodeIdentifier::new_unchecked(NodeId(8)));
|
||||
LayerNodeIdentifier::new_unchecked(NodeId(6)).push_front_child(metadata, LayerNodeIdentifier::new_unchecked(NodeId(7)));
|
||||
root.push_front_child(metadata, LayerNodeIdentifier::new_unchecked(NodeId(1)));
|
||||
assert_eq!(
|
||||
root.children(metadata).map(LayerNodeIdentifier::to_node).collect::<Vec<_>>(),
|
||||
vec![NodeId(1), NodeId(2), NodeId(3), NodeId(4), NodeId(5), NodeId(6), NodeId(9)]
|
||||
);
|
||||
assert_eq!(
|
||||
root.descendants(metadata).map(LayerNodeIdentifier::to_node).collect::<Vec<_>>(),
|
||||
vec![NodeId(1), NodeId(2), NodeId(3), NodeId(4), NodeId(5), NodeId(6), NodeId(7), NodeId(8), NodeId(9)]
|
||||
);
|
||||
assert_eq!(
|
||||
root.descendants(metadata).map(LayerNodeIdentifier::to_node).rev().collect::<Vec<_>>(),
|
||||
vec![NodeId(9), NodeId(8), NodeId(7), NodeId(6), NodeId(5), NodeId(4), NodeId(3), NodeId(2), NodeId(1)]
|
||||
);
|
||||
assert!(root.children(metadata).all(|child| child.parent(metadata) == Some(root)));
|
||||
LayerNodeIdentifier::new_unchecked(NodeId(6)).delete(metadata);
|
||||
LayerNodeIdentifier::new_unchecked(NodeId(1)).delete(metadata);
|
||||
LayerNodeIdentifier::new_unchecked(NodeId(9)).push_child(metadata, LayerNodeIdentifier::new_unchecked(NodeId(10)));
|
||||
assert_eq!(
|
||||
root.children(metadata).map(LayerNodeIdentifier::to_node).collect::<Vec<_>>(),
|
||||
vec![NodeId(2), NodeId(3), NodeId(4), NodeId(5), NodeId(9)]
|
||||
);
|
||||
assert_eq!(
|
||||
root.descendants(metadata).map(LayerNodeIdentifier::to_node).collect::<Vec<_>>(),
|
||||
vec![NodeId(2), NodeId(3), NodeId(4), NodeId(5), NodeId(9), NodeId(10)]
|
||||
);
|
||||
assert_eq!(
|
||||
root.descendants(metadata).map(LayerNodeIdentifier::to_node).rev().collect::<Vec<_>>(),
|
||||
vec![NodeId(10), NodeId(9), NodeId(5), NodeId(4), NodeId(3), NodeId(2)]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -382,317 +382,321 @@ impl DistributionSnapper {
|
|||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn merge_intersecting_test() {
|
||||
let mut rectangles = vec![Rect::from_square(DVec2::ZERO, 2.), Rect::from_square(DVec2::new(10., 0.), 2.)];
|
||||
DistributionSnapper::merge_intersecting(&mut rectangles);
|
||||
assert_eq!(rectangles.len(), 2);
|
||||
|
||||
let mut rectangles = vec![
|
||||
Rect::from_square(DVec2::ZERO, 2.),
|
||||
Rect::from_square(DVec2::new(1., 0.), 2.),
|
||||
Rect::from_square(DVec2::new(10., 0.), 2.),
|
||||
Rect::from_square(DVec2::new(11., 0.), 2.),
|
||||
];
|
||||
DistributionSnapper::merge_intersecting(&mut rectangles);
|
||||
assert_eq!(rectangles.len(), 6);
|
||||
assert_eq!(rectangles[0], Rect::from_box([DVec2::new(-2., -2.), DVec2::new(3., 2.)]));
|
||||
assert_eq!(rectangles[3], Rect::from_box([DVec2::new(8., -2.), DVec2::new(13., 2.)]));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dist_simple_2() {
|
||||
let rectangles = [10., 20.].map(|x| Rect::from_square(DVec2::new(x, 0.), 2.));
|
||||
let source = Rect::from_square(DVec2::new(0.5, 0.), 2.);
|
||||
let (offset, rectangles) = DistributionSnapper::top_level_matches(source, &rectangles, 1., dist_right);
|
||||
assert_eq!(offset, Some(DistributionMatch { first: 5.5, equal: 6. }));
|
||||
assert_eq!(rectangles.len(), 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dist_simple_3() {
|
||||
let rectangles = [10., 20., 30.].map(|x| Rect::from_square(DVec2::new(x, 0.), 2.));
|
||||
let source = Rect::from_square(DVec2::new(0.5, 0.), 2.);
|
||||
let (offset, rectangles) = DistributionSnapper::top_level_matches(source, &rectangles, 1., dist_right);
|
||||
assert_eq!(offset, Some(DistributionMatch { first: 5.5, equal: 6. }));
|
||||
assert_eq!(rectangles.len(), 3);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dist_out_of_tolerance() {
|
||||
let rectangles = [10., 20.].map(|x| Rect::from_square(DVec2::new(x, 0.), 2.));
|
||||
let source = Rect::from_square(DVec2::new(0.5, 0.), 2.);
|
||||
let (offset, rectangles) = DistributionSnapper::top_level_matches(source, &rectangles, 0.4, dist_right);
|
||||
assert_eq!(offset, None);
|
||||
assert_eq!(rectangles.len(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dist_with_nonsense() {
|
||||
let source = Rect::from_square(DVec2::new(0.5, 0.), 2.);
|
||||
let rectangles = [2., 10., 15., 20.].map(|x| Rect::from_square(DVec2::new(x, 0.), 2.));
|
||||
let (offset, rectangles) = DistributionSnapper::top_level_matches(source, &rectangles, 1., dist_right);
|
||||
assert_eq!(offset, Some(DistributionMatch { first: 5.5, equal: 6. }));
|
||||
assert_eq!(rectangles.len(), 2);
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
fn assert_boxes_in_order(rectangles: &VecDeque<Rect>, index: usize) {
|
||||
for (&first, &second) in rectangles.iter().zip(rectangles.iter().skip(1)) {
|
||||
assert!(first.max()[index] < second.min()[index], "{first:?} {second:?} {index}")
|
||||
mod tests {
|
||||
use super::*;
|
||||
#[test]
|
||||
fn merge_intersecting_test() {
|
||||
let mut rectangles = vec![Rect::from_square(DVec2::ZERO, 2.), Rect::from_square(DVec2::new(10., 0.), 2.)];
|
||||
DistributionSnapper::merge_intersecting(&mut rectangles);
|
||||
assert_eq!(rectangles.len(), 2);
|
||||
|
||||
let mut rectangles = vec![
|
||||
Rect::from_square(DVec2::ZERO, 2.),
|
||||
Rect::from_square(DVec2::new(1., 0.), 2.),
|
||||
Rect::from_square(DVec2::new(10., 0.), 2.),
|
||||
Rect::from_square(DVec2::new(11., 0.), 2.),
|
||||
];
|
||||
DistributionSnapper::merge_intersecting(&mut rectangles);
|
||||
assert_eq!(rectangles.len(), 6);
|
||||
assert_eq!(rectangles[0], Rect::from_box([DVec2::new(-2., -2.), DVec2::new(3., 2.)]));
|
||||
assert_eq!(rectangles[3], Rect::from_box([DVec2::new(8., -2.), DVec2::new(13., 2.)]));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dist_simple_2() {
|
||||
let rectangles = [10., 20.].map(|x| Rect::from_square(DVec2::new(x, 0.), 2.));
|
||||
let source = Rect::from_square(DVec2::new(0.5, 0.), 2.);
|
||||
let (offset, rectangles) = DistributionSnapper::top_level_matches(source, &rectangles, 1., dist_right);
|
||||
assert_eq!(offset, Some(DistributionMatch { first: 5.5, equal: 6. }));
|
||||
assert_eq!(rectangles.len(), 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dist_simple_3() {
|
||||
let rectangles = [10., 20., 30.].map(|x| Rect::from_square(DVec2::new(x, 0.), 2.));
|
||||
let source = Rect::from_square(DVec2::new(0.5, 0.), 2.);
|
||||
let (offset, rectangles) = DistributionSnapper::top_level_matches(source, &rectangles, 1., dist_right);
|
||||
assert_eq!(offset, Some(DistributionMatch { first: 5.5, equal: 6. }));
|
||||
assert_eq!(rectangles.len(), 3);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dist_out_of_tolerance() {
|
||||
let rectangles = [10., 20.].map(|x| Rect::from_square(DVec2::new(x, 0.), 2.));
|
||||
let source = Rect::from_square(DVec2::new(0.5, 0.), 2.);
|
||||
let (offset, rectangles) = DistributionSnapper::top_level_matches(source, &rectangles, 0.4, dist_right);
|
||||
assert_eq!(offset, None);
|
||||
assert_eq!(rectangles.len(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dist_with_nonsense() {
|
||||
let source = Rect::from_square(DVec2::new(0.5, 0.), 2.);
|
||||
let rectangles = [2., 10., 15., 20.].map(|x| Rect::from_square(DVec2::new(x, 0.), 2.));
|
||||
let (offset, rectangles) = DistributionSnapper::top_level_matches(source, &rectangles, 1., dist_right);
|
||||
assert_eq!(offset, Some(DistributionMatch { first: 5.5, equal: 6. }));
|
||||
assert_eq!(rectangles.len(), 2);
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
fn assert_boxes_in_order(rectangles: &VecDeque<Rect>, index: usize) {
|
||||
for (&first, &second) in rectangles.iter().zip(rectangles.iter().skip(1)) {
|
||||
assert!(first.max()[index] < second.min()[index], "{first:?} {second:?} {index}")
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dist_snap_point_right() {
|
||||
let dist_snapper = DistributionSnapper {
|
||||
right: [2., 10., 15., 20.].map(|x| Rect::from_square(DVec2::new(x, 0.), 2.)).to_vec(),
|
||||
left: [-2.].map(|x| Rect::from_square(DVec2::new(x, 0.), 2.)).to_vec(),
|
||||
..Default::default()
|
||||
};
|
||||
let source = Rect::from_square(DVec2::new(0.5, 0.), 2.);
|
||||
let snap_results = &mut SnapResults::default();
|
||||
dist_snapper.snap_bbox_points(1., &SnapCandidatePoint::default(), snap_results, SnapConstraint::None, source);
|
||||
assert_eq!(snap_results.points.len(), 1);
|
||||
assert_eq!(snap_results.points[0].distance, 0.5);
|
||||
assert_eq!(snap_results.points[0].distribution_equal_distance_horizontal, Some(6.));
|
||||
let mut expected_box = Rect::from_square(DVec2::new(0., 0.), 2.);
|
||||
expected_box[0].y = expected_box[0].y.min(dist_snapper.left[0][1].y);
|
||||
expected_box[1].y = expected_box[1].y.min(dist_snapper.left[0][1].y);
|
||||
|
||||
assert_eq!(snap_results.points[0].distribution_boxes_horizontal.len(), 3);
|
||||
assert_eq!(snap_results.points[0].distribution_boxes_horizontal[0], expected_box);
|
||||
assert_boxes_in_order(&snap_results.points[0].distribution_boxes_horizontal, 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dist_snap_point_right_left() {
|
||||
let dist_snapper = DistributionSnapper {
|
||||
right: [2., 10., 15., 20.].map(|x| Rect::from_square(DVec2::new(x, 0.), 2.)).to_vec(),
|
||||
left: [-2., -10., -15., -20.].map(|x| Rect::from_square(DVec2::new(x, 0.), 2.)).to_vec(),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let source = Rect::from_square(DVec2::new(0.5, 0.), 2.);
|
||||
let snap_results = &mut SnapResults::default();
|
||||
dist_snapper.snap_bbox_points(1., &SnapCandidatePoint::default(), snap_results, SnapConstraint::None, source);
|
||||
|
||||
assert_eq!(snap_results.points.len(), 1);
|
||||
assert_eq!(snap_results.points[0].distance, 0.5);
|
||||
assert_eq!(snap_results.points[0].distribution_equal_distance_horizontal, Some(6.));
|
||||
assert_eq!(snap_results.points[0].distribution_boxes_horizontal.len(), 5);
|
||||
|
||||
let mut expected_left1 = dist_snapper.left[1];
|
||||
let mut expected_center = Rect::from_square(DVec2::new(0., 0.), 2.);
|
||||
|
||||
expected_center[0].y = expected_center[0].y.min(dist_snapper.left[1][1].y).min(dist_snapper.right[0][1].y);
|
||||
expected_center[1].y = expected_center[1].y.min(dist_snapper.left[1][1].y).min(dist_snapper.right[0][1].y);
|
||||
|
||||
expected_left1[0].y = expected_left1[0].y.min(dist_snapper.left[0][1].y).min(expected_center[1].y);
|
||||
expected_left1[1].y = expected_left1[1].y.min(dist_snapper.left[0][1].y).min(expected_center[1].y);
|
||||
|
||||
assert_eq!(snap_results.points[0].distribution_boxes_horizontal[1], expected_left1);
|
||||
assert_eq!(snap_results.points[0].distribution_boxes_horizontal[2], expected_center);
|
||||
assert_boxes_in_order(&snap_results.points[0].distribution_boxes_horizontal, 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dist_snap_point_left() {
|
||||
let dist_snapper = DistributionSnapper {
|
||||
left: [-2., -10., -15., -20.].map(|x| Rect::from_square(DVec2::new(x, 0.), 2.)).to_vec(),
|
||||
..Default::default()
|
||||
};
|
||||
let source = Rect::from_square(DVec2::new(0.5, 0.), 2.);
|
||||
let snap_results = &mut SnapResults::default();
|
||||
dist_snapper.snap_bbox_points(1., &SnapCandidatePoint::default(), snap_results, SnapConstraint::None, source);
|
||||
assert_eq!(snap_results.points.len(), 1);
|
||||
assert_eq!(snap_results.points[0].distance, 0.5);
|
||||
assert_eq!(snap_results.points[0].distribution_equal_distance_horizontal, Some(6.));
|
||||
assert_eq!(snap_results.points[0].distribution_boxes_horizontal.len(), 3);
|
||||
assert_eq!(snap_results.points[0].distribution_boxes_horizontal[2], Rect::from_square(DVec2::new(0., 0.), 2.));
|
||||
assert_boxes_in_order(&snap_results.points[0].distribution_boxes_horizontal, 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dist_snap_point_left_right() {
|
||||
let dist_snapper = DistributionSnapper {
|
||||
left: [-2., -10., -15., -20.].map(|x| Rect::from_square(DVec2::new(x, 0.), 2.)).to_vec(),
|
||||
right: [2., 10., 15.].map(|x| Rect::from_square(DVec2::new(x, 0.), 2.)).to_vec(),
|
||||
..Default::default()
|
||||
};
|
||||
let source = Rect::from_square(DVec2::new(0.5, 0.), 2.);
|
||||
let snap_results = &mut SnapResults::default();
|
||||
dist_snapper.snap_bbox_points(1., &SnapCandidatePoint::default(), snap_results, SnapConstraint::None, source);
|
||||
assert_eq!(snap_results.points.len(), 1);
|
||||
assert_eq!(snap_results.points[0].distance, 0.5);
|
||||
assert_eq!(snap_results.points[0].distribution_equal_distance_horizontal, Some(6.));
|
||||
assert_eq!(snap_results.points[0].distribution_boxes_horizontal.len(), 4);
|
||||
assert_eq!(snap_results.points[0].distribution_boxes_horizontal[2], Rect::from_square(DVec2::new(0., 0.), 2.));
|
||||
assert_boxes_in_order(&snap_results.points[0].distribution_boxes_horizontal, 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dist_snap_point_center_x() {
|
||||
let dist_snapper = DistributionSnapper {
|
||||
left: [-10., -15.].map(|x| Rect::from_square(DVec2::new(x, 0.), 2.)).to_vec(),
|
||||
right: [10., 15.].map(|x| Rect::from_square(DVec2::new(x, 0.), 2.)).to_vec(),
|
||||
..Default::default()
|
||||
};
|
||||
let source = Rect::from_square(DVec2::new(0.5, 0.), 2.);
|
||||
let snap_results = &mut SnapResults::default();
|
||||
dist_snapper.snap_bbox_points(1., &SnapCandidatePoint::default(), snap_results, SnapConstraint::None, source);
|
||||
assert_eq!(snap_results.points.len(), 1);
|
||||
assert_eq!(snap_results.points[0].distance, 0.5);
|
||||
assert_eq!(snap_results.points[0].distribution_equal_distance_horizontal, Some(6.));
|
||||
|
||||
let mut expected_box = Rect::from_square(DVec2::new(0., 0.), 2.);
|
||||
expected_box[0].y = expected_box[0].y.min(dist_snapper.left[0][1].y);
|
||||
expected_box[1].y = expected_box[1].y.min(dist_snapper.left[0][1].y);
|
||||
|
||||
assert_eq!(snap_results.points[0].distribution_boxes_horizontal.len(), 3);
|
||||
assert_eq!(snap_results.points[0].distribution_boxes_horizontal[1], expected_box);
|
||||
assert_boxes_in_order(&snap_results.points[0].distribution_boxes_horizontal, 0);
|
||||
}
|
||||
|
||||
// ----------------------------------
|
||||
|
||||
#[test]
|
||||
fn dist_snap_point_down() {
|
||||
let dist_snapper = DistributionSnapper {
|
||||
down: [2., 10., 15., 20.].map(|y| Rect::from_square(DVec2::new(0., y), 2.)).to_vec(),
|
||||
up: [-2.].map(|y| Rect::from_square(DVec2::new(0., y), 2.)).to_vec(),
|
||||
..Default::default()
|
||||
};
|
||||
let source = Rect::from_square(DVec2::new(0., 0.5), 2.);
|
||||
let snap_results = &mut SnapResults::default();
|
||||
dist_snapper.snap_bbox_points(1., &SnapCandidatePoint::default(), snap_results, SnapConstraint::None, source);
|
||||
assert_eq!(snap_results.points.len(), 1);
|
||||
assert_eq!(snap_results.points[0].distance, 0.5);
|
||||
assert_eq!(snap_results.points[0].distribution_equal_distance_vertical, Some(6.));
|
||||
|
||||
let mut expected_box = Rect::from_square(DVec2::new(0., 0.), 2.);
|
||||
expected_box[0].x = expected_box[0].x.min(dist_snapper.down[0][1].x);
|
||||
expected_box[1].x = expected_box[1].x.min(dist_snapper.down[0][1].x);
|
||||
|
||||
assert_eq!(snap_results.points[0].distribution_boxes_vertical.len(), 3);
|
||||
assert_eq!(snap_results.points[0].distribution_boxes_vertical[0], expected_box);
|
||||
assert_boxes_in_order(&snap_results.points[0].distribution_boxes_vertical, 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dist_snap_point_down_up() {
|
||||
let dist_snapper = DistributionSnapper {
|
||||
down: [2., 10., 15., 20.].map(|y| Rect::from_square(DVec2::new(0., y), 2.)).to_vec(),
|
||||
up: [-2., -10., -15., -20.].map(|y| Rect::from_square(DVec2::new(0., y), 2.)).to_vec(),
|
||||
..Default::default()
|
||||
};
|
||||
let source = Rect::from_square(DVec2::new(0., 0.5), 2.);
|
||||
let snap_results = &mut SnapResults::default();
|
||||
dist_snapper.snap_bbox_points(1., &SnapCandidatePoint::default(), snap_results, SnapConstraint::None, source);
|
||||
|
||||
assert_eq!(snap_results.points.len(), 1);
|
||||
assert_eq!(snap_results.points[0].distance, 0.5);
|
||||
assert_eq!(snap_results.points[0].distribution_equal_distance_vertical, Some(6.));
|
||||
assert_eq!(snap_results.points[0].distribution_boxes_vertical.len(), 5);
|
||||
|
||||
let mut expected_center = Rect::from_square(DVec2::new(0., 0.), 2.);
|
||||
expected_center[0].x = expected_center[0].x.min(dist_snapper.up[1][1].x).min(dist_snapper.down[0][1].x);
|
||||
expected_center[1].x = expected_center[1].x.min(dist_snapper.up[1][1].x).min(dist_snapper.down[0][1].x);
|
||||
|
||||
let mut expected_up = Rect::from_square(DVec2::new(0., -10.), 2.);
|
||||
expected_up[0].x = expected_up[0].x.min(dist_snapper.up[0][1].x).min(expected_center[0].x);
|
||||
expected_up[1].x = expected_up[1].x.min(dist_snapper.up[0][1].x).min(expected_center[1].x);
|
||||
|
||||
assert_eq!(snap_results.points[0].distribution_boxes_vertical[1], expected_up);
|
||||
assert_eq!(snap_results.points[0].distribution_boxes_vertical[2], expected_center);
|
||||
assert_boxes_in_order(&snap_results.points[0].distribution_boxes_vertical, 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dist_snap_point_up() {
|
||||
let dist_snapper = DistributionSnapper {
|
||||
up: [-2., -10., -15., -20.].map(|y| Rect::from_square(DVec2::new(0., y), 2.)).to_vec(),
|
||||
..Default::default()
|
||||
};
|
||||
let source = Rect::from_square(DVec2::new(0., 0.5), 2.);
|
||||
let snap_results = &mut SnapResults::default();
|
||||
dist_snapper.snap_bbox_points(1., &SnapCandidatePoint::default(), snap_results, SnapConstraint::None, source);
|
||||
assert_eq!(snap_results.points.len(), 1);
|
||||
assert_eq!(snap_results.points[0].distance, 0.5);
|
||||
assert_eq!(snap_results.points[0].distribution_equal_distance_vertical, Some(6.));
|
||||
assert_eq!(snap_results.points[0].distribution_boxes_vertical.len(), 3);
|
||||
assert_eq!(snap_results.points[0].distribution_boxes_vertical[2], Rect::from_square(DVec2::new(0., 0.), 2.));
|
||||
assert_boxes_in_order(&snap_results.points[0].distribution_boxes_vertical, 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dist_snap_point_up_down() {
|
||||
let dist_snapper = DistributionSnapper {
|
||||
up: [-2., -10., -15., -20.].map(|y| Rect::from_square(DVec2::new(0., y), 2.)).to_vec(),
|
||||
down: [2., 10., 15.].map(|y| Rect::from_square(DVec2::new(0., y), 2.)).to_vec(),
|
||||
..Default::default()
|
||||
};
|
||||
let source = Rect::from_square(DVec2::new(0., 0.5), 2.);
|
||||
let snap_results = &mut SnapResults::default();
|
||||
dist_snapper.snap_bbox_points(1., &SnapCandidatePoint::default(), snap_results, SnapConstraint::None, source);
|
||||
assert_eq!(snap_results.points.len(), 1);
|
||||
assert_eq!(snap_results.points[0].distance, 0.5);
|
||||
assert_eq!(snap_results.points[0].distribution_equal_distance_vertical, Some(6.));
|
||||
assert_eq!(snap_results.points[0].distribution_boxes_vertical.len(), 4);
|
||||
assert_eq!(snap_results.points[0].distribution_boxes_vertical[2], Rect::from_square(DVec2::new(0., 0.), 2.));
|
||||
assert_boxes_in_order(&snap_results.points[0].distribution_boxes_vertical, 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dist_snap_point_center_y() {
|
||||
let dist_snapper = DistributionSnapper {
|
||||
up: [-10., -15.].map(|y| Rect::from_square(DVec2::new(0., y), 2.)).to_vec(),
|
||||
down: [10., 15.].map(|y| Rect::from_square(DVec2::new(0., y), 2.)).to_vec(),
|
||||
..Default::default()
|
||||
};
|
||||
let source = Rect::from_square(DVec2::new(0., 0.5), 2.);
|
||||
let snap_results = &mut SnapResults::default();
|
||||
dist_snapper.snap_bbox_points(1., &SnapCandidatePoint::default(), snap_results, SnapConstraint::None, source);
|
||||
|
||||
assert_eq!(snap_results.points.len(), 1);
|
||||
assert_eq!(snap_results.points[0].distance, 0.5);
|
||||
assert_eq!(snap_results.points[0].distribution_equal_distance_vertical, Some(6.));
|
||||
assert_eq!(snap_results.points[0].distribution_boxes_vertical.len(), 3);
|
||||
|
||||
let mut expected_box = Rect::from_square(DVec2::new(0., 0.), 2.);
|
||||
expected_box[0].x = expected_box[0].x.min(dist_snapper.up[0][1].x).min(dist_snapper.down[0][1].x);
|
||||
expected_box[1].x = expected_box[1].x.min(dist_snapper.up[0][1].x).min(dist_snapper.down[0][1].x);
|
||||
|
||||
assert_eq!(snap_results.points[0].distribution_boxes_vertical[1], expected_box);
|
||||
assert_boxes_in_order(&snap_results.points[0].distribution_boxes_vertical, 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dist_snap_point_center_xy() {
|
||||
let dist_snapper = DistributionSnapper {
|
||||
up: [-10., -15.].map(|y| Rect::from_square(DVec2::new(0., y), 2.)).to_vec(),
|
||||
down: [10., 15.].map(|y| Rect::from_square(DVec2::new(0., y), 2.)).to_vec(),
|
||||
left: [-12., -15.].map(|x| Rect::from_square(DVec2::new(x, 0.), 2.)).to_vec(),
|
||||
right: [12., 15.].map(|x| Rect::from_square(DVec2::new(x, 0.), 2.)).to_vec(),
|
||||
..Default::default()
|
||||
};
|
||||
let source = Rect::from_square(DVec2::new(0.3, 0.4), 2.);
|
||||
let snap_results = &mut SnapResults::default();
|
||||
dist_snapper.snap_bbox_points(1., &SnapCandidatePoint::default(), snap_results, SnapConstraint::None, source);
|
||||
|
||||
assert_eq!(snap_results.points.len(), 1);
|
||||
assert_eq!(snap_results.points[0].distance, 0.5000000000000001);
|
||||
assert_eq!(snap_results.points[0].distribution_equal_distance_horizontal, Some(8.));
|
||||
assert_eq!(snap_results.points[0].distribution_equal_distance_vertical, Some(6.));
|
||||
assert_eq!(snap_results.points[0].distribution_boxes_horizontal.len(), 3);
|
||||
assert_eq!(snap_results.points[0].distribution_boxes_vertical.len(), 3);
|
||||
|
||||
assert!(snap_results.points[0].distribution_boxes_horizontal[1][0].y <= dist_snapper.left[0][1].y);
|
||||
assert!(snap_results.points[0].distribution_boxes_horizontal[1][1].y <= dist_snapper.left[0][1].y);
|
||||
assert!(snap_results.points[0].distribution_boxes_vertical[1][0].x <= dist_snapper.up[0][1].x);
|
||||
assert!(snap_results.points[0].distribution_boxes_vertical[1][1].x <= dist_snapper.up[0][1].x);
|
||||
|
||||
assert_eq!(Rect::from_box(snap_results.points[0].source_bounds.unwrap().bounding_box()), Rect::from_square(DVec2::new(0., 0.), 2.));
|
||||
assert_boxes_in_order(&snap_results.points[0].distribution_boxes_horizontal, 0);
|
||||
assert_boxes_in_order(&snap_results.points[0].distribution_boxes_vertical, 1);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dist_snap_point_right() {
|
||||
let dist_snapper = DistributionSnapper {
|
||||
right: [2., 10., 15., 20.].map(|x| Rect::from_square(DVec2::new(x, 0.), 2.)).to_vec(),
|
||||
left: [-2.].map(|x| Rect::from_square(DVec2::new(x, 0.), 2.)).to_vec(),
|
||||
..Default::default()
|
||||
};
|
||||
let source = Rect::from_square(DVec2::new(0.5, 0.), 2.);
|
||||
let snap_results = &mut SnapResults::default();
|
||||
dist_snapper.snap_bbox_points(1., &SnapCandidatePoint::default(), snap_results, SnapConstraint::None, source);
|
||||
assert_eq!(snap_results.points.len(), 1);
|
||||
assert_eq!(snap_results.points[0].distance, 0.5);
|
||||
assert_eq!(snap_results.points[0].distribution_equal_distance_horizontal, Some(6.));
|
||||
let mut expected_box = Rect::from_square(DVec2::new(0., 0.), 2.);
|
||||
expected_box[0].y = expected_box[0].y.min(dist_snapper.left[0][1].y);
|
||||
expected_box[1].y = expected_box[1].y.min(dist_snapper.left[0][1].y);
|
||||
|
||||
assert_eq!(snap_results.points[0].distribution_boxes_horizontal.len(), 3);
|
||||
assert_eq!(snap_results.points[0].distribution_boxes_horizontal[0], expected_box);
|
||||
assert_boxes_in_order(&snap_results.points[0].distribution_boxes_horizontal, 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dist_snap_point_right_left() {
|
||||
let dist_snapper = DistributionSnapper {
|
||||
right: [2., 10., 15., 20.].map(|x| Rect::from_square(DVec2::new(x, 0.), 2.)).to_vec(),
|
||||
left: [-2., -10., -15., -20.].map(|x| Rect::from_square(DVec2::new(x, 0.), 2.)).to_vec(),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let source = Rect::from_square(DVec2::new(0.5, 0.), 2.);
|
||||
let snap_results = &mut SnapResults::default();
|
||||
dist_snapper.snap_bbox_points(1., &SnapCandidatePoint::default(), snap_results, SnapConstraint::None, source);
|
||||
|
||||
assert_eq!(snap_results.points.len(), 1);
|
||||
assert_eq!(snap_results.points[0].distance, 0.5);
|
||||
assert_eq!(snap_results.points[0].distribution_equal_distance_horizontal, Some(6.));
|
||||
assert_eq!(snap_results.points[0].distribution_boxes_horizontal.len(), 5);
|
||||
|
||||
let mut expected_left1 = dist_snapper.left[1];
|
||||
let mut expected_center = Rect::from_square(DVec2::new(0., 0.), 2.);
|
||||
|
||||
expected_center[0].y = expected_center[0].y.min(dist_snapper.left[1][1].y).min(dist_snapper.right[0][1].y);
|
||||
expected_center[1].y = expected_center[1].y.min(dist_snapper.left[1][1].y).min(dist_snapper.right[0][1].y);
|
||||
|
||||
expected_left1[0].y = expected_left1[0].y.min(dist_snapper.left[0][1].y).min(expected_center[1].y);
|
||||
expected_left1[1].y = expected_left1[1].y.min(dist_snapper.left[0][1].y).min(expected_center[1].y);
|
||||
|
||||
assert_eq!(snap_results.points[0].distribution_boxes_horizontal[1], expected_left1);
|
||||
assert_eq!(snap_results.points[0].distribution_boxes_horizontal[2], expected_center);
|
||||
assert_boxes_in_order(&snap_results.points[0].distribution_boxes_horizontal, 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dist_snap_point_left() {
|
||||
let dist_snapper = DistributionSnapper {
|
||||
left: [-2., -10., -15., -20.].map(|x| Rect::from_square(DVec2::new(x, 0.), 2.)).to_vec(),
|
||||
..Default::default()
|
||||
};
|
||||
let source = Rect::from_square(DVec2::new(0.5, 0.), 2.);
|
||||
let snap_results = &mut SnapResults::default();
|
||||
dist_snapper.snap_bbox_points(1., &SnapCandidatePoint::default(), snap_results, SnapConstraint::None, source);
|
||||
assert_eq!(snap_results.points.len(), 1);
|
||||
assert_eq!(snap_results.points[0].distance, 0.5);
|
||||
assert_eq!(snap_results.points[0].distribution_equal_distance_horizontal, Some(6.));
|
||||
assert_eq!(snap_results.points[0].distribution_boxes_horizontal.len(), 3);
|
||||
assert_eq!(snap_results.points[0].distribution_boxes_horizontal[2], Rect::from_square(DVec2::new(0., 0.), 2.));
|
||||
assert_boxes_in_order(&snap_results.points[0].distribution_boxes_horizontal, 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dist_snap_point_left_right() {
|
||||
let dist_snapper = DistributionSnapper {
|
||||
left: [-2., -10., -15., -20.].map(|x| Rect::from_square(DVec2::new(x, 0.), 2.)).to_vec(),
|
||||
right: [2., 10., 15.].map(|x| Rect::from_square(DVec2::new(x, 0.), 2.)).to_vec(),
|
||||
..Default::default()
|
||||
};
|
||||
let source = Rect::from_square(DVec2::new(0.5, 0.), 2.);
|
||||
let snap_results = &mut SnapResults::default();
|
||||
dist_snapper.snap_bbox_points(1., &SnapCandidatePoint::default(), snap_results, SnapConstraint::None, source);
|
||||
assert_eq!(snap_results.points.len(), 1);
|
||||
assert_eq!(snap_results.points[0].distance, 0.5);
|
||||
assert_eq!(snap_results.points[0].distribution_equal_distance_horizontal, Some(6.));
|
||||
assert_eq!(snap_results.points[0].distribution_boxes_horizontal.len(), 4);
|
||||
assert_eq!(snap_results.points[0].distribution_boxes_horizontal[2], Rect::from_square(DVec2::new(0., 0.), 2.));
|
||||
assert_boxes_in_order(&snap_results.points[0].distribution_boxes_horizontal, 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dist_snap_point_center_x() {
|
||||
let dist_snapper = DistributionSnapper {
|
||||
left: [-10., -15.].map(|x| Rect::from_square(DVec2::new(x, 0.), 2.)).to_vec(),
|
||||
right: [10., 15.].map(|x| Rect::from_square(DVec2::new(x, 0.), 2.)).to_vec(),
|
||||
..Default::default()
|
||||
};
|
||||
let source = Rect::from_square(DVec2::new(0.5, 0.), 2.);
|
||||
let snap_results = &mut SnapResults::default();
|
||||
dist_snapper.snap_bbox_points(1., &SnapCandidatePoint::default(), snap_results, SnapConstraint::None, source);
|
||||
assert_eq!(snap_results.points.len(), 1);
|
||||
assert_eq!(snap_results.points[0].distance, 0.5);
|
||||
assert_eq!(snap_results.points[0].distribution_equal_distance_horizontal, Some(6.));
|
||||
|
||||
let mut expected_box = Rect::from_square(DVec2::new(0., 0.), 2.);
|
||||
expected_box[0].y = expected_box[0].y.min(dist_snapper.left[0][1].y);
|
||||
expected_box[1].y = expected_box[1].y.min(dist_snapper.left[0][1].y);
|
||||
|
||||
assert_eq!(snap_results.points[0].distribution_boxes_horizontal.len(), 3);
|
||||
assert_eq!(snap_results.points[0].distribution_boxes_horizontal[1], expected_box);
|
||||
assert_boxes_in_order(&snap_results.points[0].distribution_boxes_horizontal, 0);
|
||||
}
|
||||
|
||||
// ----------------------------------
|
||||
|
||||
#[test]
|
||||
fn dist_snap_point_down() {
|
||||
let dist_snapper = DistributionSnapper {
|
||||
down: [2., 10., 15., 20.].map(|y| Rect::from_square(DVec2::new(0., y), 2.)).to_vec(),
|
||||
up: [-2.].map(|y| Rect::from_square(DVec2::new(0., y), 2.)).to_vec(),
|
||||
..Default::default()
|
||||
};
|
||||
let source = Rect::from_square(DVec2::new(0., 0.5), 2.);
|
||||
let snap_results = &mut SnapResults::default();
|
||||
dist_snapper.snap_bbox_points(1., &SnapCandidatePoint::default(), snap_results, SnapConstraint::None, source);
|
||||
assert_eq!(snap_results.points.len(), 1);
|
||||
assert_eq!(snap_results.points[0].distance, 0.5);
|
||||
assert_eq!(snap_results.points[0].distribution_equal_distance_vertical, Some(6.));
|
||||
|
||||
let mut expected_box = Rect::from_square(DVec2::new(0., 0.), 2.);
|
||||
expected_box[0].x = expected_box[0].x.min(dist_snapper.down[0][1].x);
|
||||
expected_box[1].x = expected_box[1].x.min(dist_snapper.down[0][1].x);
|
||||
|
||||
assert_eq!(snap_results.points[0].distribution_boxes_vertical.len(), 3);
|
||||
assert_eq!(snap_results.points[0].distribution_boxes_vertical[0], expected_box);
|
||||
assert_boxes_in_order(&snap_results.points[0].distribution_boxes_vertical, 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dist_snap_point_down_up() {
|
||||
let dist_snapper = DistributionSnapper {
|
||||
down: [2., 10., 15., 20.].map(|y| Rect::from_square(DVec2::new(0., y), 2.)).to_vec(),
|
||||
up: [-2., -10., -15., -20.].map(|y| Rect::from_square(DVec2::new(0., y), 2.)).to_vec(),
|
||||
..Default::default()
|
||||
};
|
||||
let source = Rect::from_square(DVec2::new(0., 0.5), 2.);
|
||||
let snap_results = &mut SnapResults::default();
|
||||
dist_snapper.snap_bbox_points(1., &SnapCandidatePoint::default(), snap_results, SnapConstraint::None, source);
|
||||
|
||||
assert_eq!(snap_results.points.len(), 1);
|
||||
assert_eq!(snap_results.points[0].distance, 0.5);
|
||||
assert_eq!(snap_results.points[0].distribution_equal_distance_vertical, Some(6.));
|
||||
assert_eq!(snap_results.points[0].distribution_boxes_vertical.len(), 5);
|
||||
|
||||
let mut expected_center = Rect::from_square(DVec2::new(0., 0.), 2.);
|
||||
expected_center[0].x = expected_center[0].x.min(dist_snapper.up[1][1].x).min(dist_snapper.down[0][1].x);
|
||||
expected_center[1].x = expected_center[1].x.min(dist_snapper.up[1][1].x).min(dist_snapper.down[0][1].x);
|
||||
|
||||
let mut expected_up = Rect::from_square(DVec2::new(0., -10.), 2.);
|
||||
expected_up[0].x = expected_up[0].x.min(dist_snapper.up[0][1].x).min(expected_center[0].x);
|
||||
expected_up[1].x = expected_up[1].x.min(dist_snapper.up[0][1].x).min(expected_center[1].x);
|
||||
|
||||
assert_eq!(snap_results.points[0].distribution_boxes_vertical[1], expected_up);
|
||||
assert_eq!(snap_results.points[0].distribution_boxes_vertical[2], expected_center);
|
||||
assert_boxes_in_order(&snap_results.points[0].distribution_boxes_vertical, 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dist_snap_point_up() {
|
||||
let dist_snapper = DistributionSnapper {
|
||||
up: [-2., -10., -15., -20.].map(|y| Rect::from_square(DVec2::new(0., y), 2.)).to_vec(),
|
||||
..Default::default()
|
||||
};
|
||||
let source = Rect::from_square(DVec2::new(0., 0.5), 2.);
|
||||
let snap_results = &mut SnapResults::default();
|
||||
dist_snapper.snap_bbox_points(1., &SnapCandidatePoint::default(), snap_results, SnapConstraint::None, source);
|
||||
assert_eq!(snap_results.points.len(), 1);
|
||||
assert_eq!(snap_results.points[0].distance, 0.5);
|
||||
assert_eq!(snap_results.points[0].distribution_equal_distance_vertical, Some(6.));
|
||||
assert_eq!(snap_results.points[0].distribution_boxes_vertical.len(), 3);
|
||||
assert_eq!(snap_results.points[0].distribution_boxes_vertical[2], Rect::from_square(DVec2::new(0., 0.), 2.));
|
||||
assert_boxes_in_order(&snap_results.points[0].distribution_boxes_vertical, 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dist_snap_point_up_down() {
|
||||
let dist_snapper = DistributionSnapper {
|
||||
up: [-2., -10., -15., -20.].map(|y| Rect::from_square(DVec2::new(0., y), 2.)).to_vec(),
|
||||
down: [2., 10., 15.].map(|y| Rect::from_square(DVec2::new(0., y), 2.)).to_vec(),
|
||||
..Default::default()
|
||||
};
|
||||
let source = Rect::from_square(DVec2::new(0., 0.5), 2.);
|
||||
let snap_results = &mut SnapResults::default();
|
||||
dist_snapper.snap_bbox_points(1., &SnapCandidatePoint::default(), snap_results, SnapConstraint::None, source);
|
||||
assert_eq!(snap_results.points.len(), 1);
|
||||
assert_eq!(snap_results.points[0].distance, 0.5);
|
||||
assert_eq!(snap_results.points[0].distribution_equal_distance_vertical, Some(6.));
|
||||
assert_eq!(snap_results.points[0].distribution_boxes_vertical.len(), 4);
|
||||
assert_eq!(snap_results.points[0].distribution_boxes_vertical[2], Rect::from_square(DVec2::new(0., 0.), 2.));
|
||||
assert_boxes_in_order(&snap_results.points[0].distribution_boxes_vertical, 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dist_snap_point_center_y() {
|
||||
let dist_snapper = DistributionSnapper {
|
||||
up: [-10., -15.].map(|y| Rect::from_square(DVec2::new(0., y), 2.)).to_vec(),
|
||||
down: [10., 15.].map(|y| Rect::from_square(DVec2::new(0., y), 2.)).to_vec(),
|
||||
..Default::default()
|
||||
};
|
||||
let source = Rect::from_square(DVec2::new(0., 0.5), 2.);
|
||||
let snap_results = &mut SnapResults::default();
|
||||
dist_snapper.snap_bbox_points(1., &SnapCandidatePoint::default(), snap_results, SnapConstraint::None, source);
|
||||
|
||||
assert_eq!(snap_results.points.len(), 1);
|
||||
assert_eq!(snap_results.points[0].distance, 0.5);
|
||||
assert_eq!(snap_results.points[0].distribution_equal_distance_vertical, Some(6.));
|
||||
assert_eq!(snap_results.points[0].distribution_boxes_vertical.len(), 3);
|
||||
|
||||
let mut expected_box = Rect::from_square(DVec2::new(0., 0.), 2.);
|
||||
expected_box[0].x = expected_box[0].x.min(dist_snapper.up[0][1].x).min(dist_snapper.down[0][1].x);
|
||||
expected_box[1].x = expected_box[1].x.min(dist_snapper.up[0][1].x).min(dist_snapper.down[0][1].x);
|
||||
|
||||
assert_eq!(snap_results.points[0].distribution_boxes_vertical[1], expected_box);
|
||||
assert_boxes_in_order(&snap_results.points[0].distribution_boxes_vertical, 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dist_snap_point_center_xy() {
|
||||
let dist_snapper = DistributionSnapper {
|
||||
up: [-10., -15.].map(|y| Rect::from_square(DVec2::new(0., y), 2.)).to_vec(),
|
||||
down: [10., 15.].map(|y| Rect::from_square(DVec2::new(0., y), 2.)).to_vec(),
|
||||
left: [-12., -15.].map(|x| Rect::from_square(DVec2::new(x, 0.), 2.)).to_vec(),
|
||||
right: [12., 15.].map(|x| Rect::from_square(DVec2::new(x, 0.), 2.)).to_vec(),
|
||||
..Default::default()
|
||||
};
|
||||
let source = Rect::from_square(DVec2::new(0.3, 0.4), 2.);
|
||||
let snap_results = &mut SnapResults::default();
|
||||
dist_snapper.snap_bbox_points(1., &SnapCandidatePoint::default(), snap_results, SnapConstraint::None, source);
|
||||
|
||||
assert_eq!(snap_results.points.len(), 1);
|
||||
assert_eq!(snap_results.points[0].distance, 0.5000000000000001);
|
||||
assert_eq!(snap_results.points[0].distribution_equal_distance_horizontal, Some(8.));
|
||||
assert_eq!(snap_results.points[0].distribution_equal_distance_vertical, Some(6.));
|
||||
assert_eq!(snap_results.points[0].distribution_boxes_horizontal.len(), 3);
|
||||
assert_eq!(snap_results.points[0].distribution_boxes_vertical.len(), 3);
|
||||
|
||||
assert!(snap_results.points[0].distribution_boxes_horizontal[1][0].y <= dist_snapper.left[0][1].y);
|
||||
assert!(snap_results.points[0].distribution_boxes_horizontal[1][1].y <= dist_snapper.left[0][1].y);
|
||||
assert!(snap_results.points[0].distribution_boxes_vertical[1][0].x <= dist_snapper.up[0][1].x);
|
||||
assert!(snap_results.points[0].distribution_boxes_vertical[1][1].x <= dist_snapper.up[0][1].x);
|
||||
|
||||
assert_eq!(Rect::from_box(snap_results.points[0].source_bounds.unwrap().bounding_box()), Rect::from_square(DVec2::new(0., 0.), 2.));
|
||||
assert_boxes_in_order(&snap_results.points[0].distribution_boxes_horizontal, 0);
|
||||
assert_boxes_in_order(&snap_results.points[0].distribution_boxes_vertical, 1);
|
||||
}
|
||||
|
|
|
@ -800,59 +800,64 @@ impl BoundingBoxManager {
|
|||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn skew_transform_singular() {
|
||||
for edge in [
|
||||
SelectedEdges::new(true, false, false, false, [DVec2::NEG_ONE, DVec2::ONE]),
|
||||
SelectedEdges::new(false, true, false, false, [DVec2::NEG_ONE, DVec2::ONE]),
|
||||
SelectedEdges::new(false, false, true, false, [DVec2::NEG_ONE, DVec2::ONE]),
|
||||
SelectedEdges::new(false, false, false, true, [DVec2::NEG_ONE, DVec2::ONE]),
|
||||
] {
|
||||
// The determinant is 0.
|
||||
let transform = DAffine2::from_cols_array(&[2.; 6]);
|
||||
// This shouldn't panic. We don't really care about the behavior in this test.
|
||||
let _ = edge.skew_transform(DVec2::new(1.5, 1.5), transform, false);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn skew_transform_correct() {
|
||||
for edge in [
|
||||
SelectedEdges::new(true, false, false, false, [DVec2::NEG_ONE, DVec2::ONE]),
|
||||
SelectedEdges::new(false, true, false, false, [DVec2::NEG_ONE, DVec2::ONE]),
|
||||
SelectedEdges::new(false, false, true, false, [DVec2::NEG_ONE, DVec2::ONE]),
|
||||
SelectedEdges::new(false, false, false, true, [DVec2::NEG_ONE, DVec2::ONE]),
|
||||
] {
|
||||
// Random transform with det != 0.
|
||||
let to_viewport_transform = DAffine2::from_cols_array(&[2., 1., 0., 1., 2., 3.]);
|
||||
// Random mouse position.
|
||||
let mouse = DVec2::new(1.5, 1.5);
|
||||
let final_transform = edge.skew_transform(mouse, to_viewport_transform, false);
|
||||
|
||||
// This is the current handle that goes under the mouse.
|
||||
let opposite = edge.pivot_from_bounds(edge.bounds[0], edge.bounds[1]);
|
||||
let dragging_point = edge.pivot_from_bounds(edge.bounds[1], edge.bounds[0]);
|
||||
|
||||
let viewport_dragging_point = to_viewport_transform.transform_point2(dragging_point);
|
||||
let parallel_to_x = edge.top || edge.bottom;
|
||||
let parallel_to_y = !parallel_to_x && (edge.left || edge.right);
|
||||
|
||||
let drag_vector = mouse - viewport_dragging_point;
|
||||
let document_drag_vector = to_viewport_transform.inverse().transform_vector2(drag_vector);
|
||||
|
||||
let sign = if edge.top || edge.left { -1. } else { 1. };
|
||||
let scale_factor = (edge.bounds[1] - edge.bounds[0])[parallel_to_x as usize].abs().recip() * sign;
|
||||
let scaled_document_drag = document_drag_vector * scale_factor;
|
||||
|
||||
let skew = DAffine2::from_mat2(DMat2::from_cols_array(&[
|
||||
1.,
|
||||
if parallel_to_y { scaled_document_drag.y } else { 0. },
|
||||
if parallel_to_x { scaled_document_drag.x } else { 0. },
|
||||
1.,
|
||||
]));
|
||||
|
||||
let constructed_transform = DAffine2::from_translation(opposite) * skew * DAffine2::from_translation(-opposite);
|
||||
|
||||
assert_eq!(constructed_transform, final_transform);
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn skew_transform_singular() {
|
||||
for edge in [
|
||||
SelectedEdges::new(true, false, false, false, [DVec2::NEG_ONE, DVec2::ONE]),
|
||||
SelectedEdges::new(false, true, false, false, [DVec2::NEG_ONE, DVec2::ONE]),
|
||||
SelectedEdges::new(false, false, true, false, [DVec2::NEG_ONE, DVec2::ONE]),
|
||||
SelectedEdges::new(false, false, false, true, [DVec2::NEG_ONE, DVec2::ONE]),
|
||||
] {
|
||||
// The determinant is 0.
|
||||
let transform = DAffine2::from_cols_array(&[2.; 6]);
|
||||
// This shouldn't panic. We don't really care about the behavior in this test.
|
||||
let _ = edge.skew_transform(DVec2::new(1.5, 1.5), transform, false);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn skew_transform_correct() {
|
||||
for edge in [
|
||||
SelectedEdges::new(true, false, false, false, [DVec2::NEG_ONE, DVec2::ONE]),
|
||||
SelectedEdges::new(false, true, false, false, [DVec2::NEG_ONE, DVec2::ONE]),
|
||||
SelectedEdges::new(false, false, true, false, [DVec2::NEG_ONE, DVec2::ONE]),
|
||||
SelectedEdges::new(false, false, false, true, [DVec2::NEG_ONE, DVec2::ONE]),
|
||||
] {
|
||||
// Random transform with det != 0.
|
||||
let to_viewport_transform = DAffine2::from_cols_array(&[2., 1., 0., 1., 2., 3.]);
|
||||
// Random mouse position.
|
||||
let mouse = DVec2::new(1.5, 1.5);
|
||||
let final_transform = edge.skew_transform(mouse, to_viewport_transform, false);
|
||||
|
||||
// This is the current handle that goes under the mouse.
|
||||
let opposite = edge.pivot_from_bounds(edge.bounds[0], edge.bounds[1]);
|
||||
let dragging_point = edge.pivot_from_bounds(edge.bounds[1], edge.bounds[0]);
|
||||
|
||||
let viewport_dragging_point = to_viewport_transform.transform_point2(dragging_point);
|
||||
let parallel_to_x = edge.top || edge.bottom;
|
||||
let parallel_to_y = !parallel_to_x && (edge.left || edge.right);
|
||||
|
||||
let drag_vector = mouse - viewport_dragging_point;
|
||||
let document_drag_vector = to_viewport_transform.inverse().transform_vector2(drag_vector);
|
||||
|
||||
let sign = if edge.top || edge.left { -1. } else { 1. };
|
||||
let scale_factor = (edge.bounds[1] - edge.bounds[0])[parallel_to_x as usize].abs().recip() * sign;
|
||||
let scaled_document_drag = document_drag_vector * scale_factor;
|
||||
|
||||
let skew = DAffine2::from_mat2(DMat2::from_cols_array(&[
|
||||
1.,
|
||||
if parallel_to_y { scaled_document_drag.y } else { 0. },
|
||||
if parallel_to_x { scaled_document_drag.x } else { 0. },
|
||||
1.,
|
||||
]));
|
||||
|
||||
let constructed_transform = DAffine2::from_translation(opposite) * skew * DAffine2::from_translation(-opposite);
|
||||
|
||||
assert_eq!(constructed_transform, final_transform);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue