Skip to content

Commit

Permalink
fix: symmetric bug
Browse files Browse the repository at this point in the history
  • Loading branch information
zxch3n committed Jun 7, 2022
1 parent 8a0c53c commit 2c3b6eb
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 33 deletions.
26 changes: 4 additions & 22 deletions rust/crates/tidy-tree/src/layout/tidy_layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,9 @@ impl Node {
let last = self.children.last().unwrap();
let last_child_pos = last.relative_x + last.tidy().modifier_to_subtree;
self.relative_x = (first_child_pos + last_child_pos) / 2.;
// make modifier_to_subtree + relative_x = 0. so that
// there will always be collision in `separation()`'s first loop
self.tidy_mut().modifier_to_subtree = -self.relative_x;
}

fn add_child_spacing(&mut self) {
Expand Down Expand Up @@ -289,27 +292,6 @@ impl TidyLayout {

self.first_walk(node.children.first_mut().unwrap());
let mut y_list = LinkedYList::new(0, node.children[0].extreme_right().bottom());
// if node.children[0]
// .extreme_right()
// .tidy()
// .thread_right
// .is_some()
// {
// println!("HHHHHHHH");
// println!("{:#?}", node.children[0]);
// println!("Extreme right");
// println!("{:#?}", node.children[0].extreme_right());
// println!("RIGHT");
// println!("{:#?}", unsafe {
// node.children[0]
// .extreme_right()
// .tidy()
// .thread_right
// .unwrap()
// .as_ref()
// });
// panic!();
// }
for i in 1..node.children.len() {
let current_child = node.children.get_mut(i).unwrap();
self.first_walk(current_child);
Expand Down Expand Up @@ -353,7 +335,7 @@ impl Layout for TidyLayout {
});
self.set_y(root);
self.first_walk(root);
self.second_walk(root, -root.relative_x);
self.second_walk(root, 0.);
}

fn partial_layout(
Expand Down
8 changes: 8 additions & 0 deletions rust/crates/tidy-tree/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,14 @@ impl Node {
node
}

pub fn new_with_children(id: usize, width: Coord, height: Coord, children: Vec<Self>) -> Self {
let mut node = Node::new(id, width, height);
for child in children {
node.append_child(child);
}
node
}

pub fn intersects(&self, other: &Self) -> bool {
self.x - self.width / 2. < other.x + other.width / 2.
&& self.x + self.width / 2. > other.x - other.width / 2.
Expand Down
19 changes: 10 additions & 9 deletions rust/crates/tidy-tree/tests/aesthetic_rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ pub fn assert_no_crossed_lines(root: &Node) {
let line = Line {
from: Point {
x: node.x,
y: node.y,
y: node.y + node.height,
},
to: Point {
x: child.x,
Expand Down Expand Up @@ -88,17 +88,14 @@ pub fn assert_symmetric(root: &Node, layout: &mut dyn Layout) {
pre_order_traversal_rev(&mirrored, |node| {
point_mirrored.push(node.x);
});
println!("{}", root.str());
println!("{}", mirrored.str());

assert_eq!(point_origin.len(), point_mirrored.len());
for i in 0..point_origin.len() {
assert!(
(point_origin[i] + point_mirrored[i]).abs() <= 1e-6,
"{} != {}",
point_origin[i],
-point_mirrored[i]
)
if (point_origin[i] + point_mirrored[i]).abs() > 1e-6 {
println!("{}", root.str());
println!("{}", mirrored.str());
panic!("{} != {}", point_origin[i], point_mirrored[i]);
}
}

fn pre_order_traversal_rev<F>(node: &Node, mut f: F)
Expand All @@ -119,6 +116,10 @@ pub fn assert_symmetric(root: &Node, layout: &mut dyn Layout) {
fn mirror(root: &Node) -> Node {
let mut root = root.clone();
root.post_order_traversal_mut(|node| {
node.x = 0.;
node.y = 0.;
node.relative_x = 0.;
node.relative_y = 0.;
let n = node.children.len();
for i in 0..n / 2 {
node.children.swap(i, n - i - 1);
Expand Down
26 changes: 24 additions & 2 deletions rust/crates/tidy-tree/tests/layout_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ use tidy_tree::{geometry::Coord, BasicLayout, Layout, Node, TidyLayout};
pub fn test_layout(layout: &mut dyn Layout) {
let mut rng = StdRng::seed_from_u64(1001);
for _ in 0..100 {
let mut tree = gen_tree(&mut rng, 200);
let mut tree = gen_tree(&mut rng, 500);
layout.layout(&mut tree);
// aesthetic_rules::assert_symmetric(&tree, layout);
aesthetic_rules::assert_no_overlap_nodes(&tree);
aesthetic_rules::assert_no_crossed_lines(&tree);
aesthetic_rules::check_nodes_order(&tree);
aesthetic_rules::check_y_position_in_same_level(&tree);
aesthetic_rules::assert_parent_centered(&tree);
aesthetic_rules::assert_symmetric(&tree, layout);
}
}

Expand Down Expand Up @@ -88,3 +88,25 @@ fn test_tidy_layout2() {
// println!("{}", root.str());
aesthetic_rules::assert_symmetric(&root, &mut tidy);
}

#[test]
fn test_tidy_layout3() {
let mut tidy = TidyLayout::new(1., 1.);
let mut root = Node::new(0, 8., 7.);
root.append_child(Node::new_with_children(
1,
3.,
9.,
vec![
Node::new(10, 3., 8.),
Node::new(10, 5., 5.),
Node::new(10, 6., 8.),
],
));
root.append_child(Node::new(3, 1., 1.));

tidy.layout(&mut root);
// println!("{}", root.str());
aesthetic_rules::assert_no_overlap_nodes(&root);
aesthetic_rules::assert_symmetric(&root, &mut tidy);
}

0 comments on commit 2c3b6eb

Please sign in to comment.