Skip to content

Commit

Permalink
fix: ts layout mirrored
Browse files Browse the repository at this point in the history
  • Loading branch information
zxch3n committed Jun 13, 2022
1 parent 0cde6ef commit bef916a
Show file tree
Hide file tree
Showing 8 changed files with 137 additions and 15 deletions.
4 changes: 0 additions & 4 deletions rust/crates/tidy-tree/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,3 @@ tinyset = "0.4.10"
[dev-dependencies]
rand = "0.8.5"

[profile.bench]
opt-level = 3
overflow-checks = false

39 changes: 39 additions & 0 deletions rust/crates/tidy-tree/benches/gen.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use std::ptr::NonNull;

use rand::prelude::*;
use tidy_tree::{geometry::Coord, Node};

pub fn gen_node(rng: &mut StdRng) -> Node {
Node {
id: rng.gen(),
width: rng.gen_range(5..50) as Coord,
height: rng.gen_range(5..50) as Coord,
x: 0.,
y: 0.,
relative_x: 0.,
relative_y: 0.,
bbox: Default::default(),
parent: None,
children: vec![],
tidy: None,
}
}

pub fn gen_tree(rng: &mut StdRng, num: usize) -> Node {
let root = gen_node(rng);
let mut nodes: Vec<NonNull<Node>> = vec![(&root).into()];
let max_chosen_size = 100;
for _ in 0..num {
let parent_index = if nodes.len() < max_chosen_size {
rng.gen_range(0..nodes.len())
} else {
rng.gen_range((nodes.len() - max_chosen_size)..nodes.len())
};
let parent = unsafe { nodes[parent_index].as_mut() };
let node = gen_node(rng);
parent.append_child(node);
nodes.push(parent.children.last().unwrap().as_ref().into());
}

root
}
31 changes: 31 additions & 0 deletions rust/crates/tidy-tree/benches/layout.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use rand::{prelude::StdRng, SeedableRng};
use tidy_tree::{BasicLayout, Layout, TidyLayout};
mod gen;

// #[test]
// fn tidy() {
// let mut rng = StdRng::seed_from_u64(1001);
// let mut tree = gen::gen_tree(&mut rng, 100_000);
// let layout = TidyLayout {
// parent_child_margin: 10.,
// peer_margin: 10.,
// };

// for _ in 0..10 {
// layout.layout(&mut tree);
// }
// }

#[test]
fn basic() {
let mut rng = StdRng::seed_from_u64(1001);
let mut tree = gen::gen_tree(&mut rng, 100_000);
let layout = BasicLayout {
parent_child_margin: 10.,
peer_margin: 10.,
};

for _ in 0..1000 {
layout.layout(&mut tree);
}
}
2 changes: 1 addition & 1 deletion rust/crates/tidy-tree/tests/gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub fn gen_node(rng: &mut StdRng) -> Node {
pub fn gen_tree(rng: &mut StdRng, num: usize) -> Node {
let root = gen_node(rng);
let mut nodes: Vec<NonNull<Node>> = vec![(&root).into()];
let max_chosen_size = 100;
let max_chosen_size = 1000000;
for _ in 0..num {
let parent_index = if nodes.len() < max_chosen_size {
rng.gen_range(0..nodes.len())
Expand Down
1 change: 1 addition & 0 deletions src/TidyComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export const TidyComponent = ({ root, layoutType, updateTrigger }: Props) => {
layoutRef.current = await TidyLayout.create(type);
const innerRoot = layoutRef.current.set_root(root);
layoutRef.current.layout();
console.log(innerRoot);
renderRef.current.init(innerRoot);
};

Expand Down
44 changes: 44 additions & 0 deletions src/stories/Tidy.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,47 @@ export const TidyLayout = ({ layoutType, ...props }: Props) => {
</div>
);
};

export const Example0 = () => {
return (
<TidyComponent
root={node(10, 10, [
node(10, 10, [
node(10, 10, [
node(10, 10),
node(10, 10),
node(10, 10),
node(10, 10),
node(10, 10),
node(10, 10),
node(10, 10),
node(10, 10),
node(10, 10),
node(10, 10),
node(10, 10),
node(10, 10),
node(10, 10),
node(10, 10),
node(10, 10),
node(10, 10),
]),
]),
node(10, 10, [node(10, 10), node(10, 10), node(10, 10), node(10, 10)]),
node(10, 10),
node(10, 40),
])}
updateTrigger={0}
layoutType={LayoutTypeStr.Tidy}
/>
);
};

function node(width: number, height: number, children: Node[] = []): Node {
return {
x: 0,
y: 0,
width,
height,
children,
};
}
2 changes: 1 addition & 1 deletion src/tidy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ export class TidyLayout extends Disposable {
height.push(node.height);
parents.push(node.parentId ?? NULL_ID());
this.idToNode.set(node.id!, node as InnerNode);
for (const child of node.children) {
for (const child of node.children.concat().reverse()) {
if (child.parentId == null) {
child.parentId = node.id;
}
Expand Down
29 changes: 20 additions & 9 deletions test/tidy.test.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,34 @@
import { TidyLayout, initWasm, LayoutType } from '../src/tidy';
import { describe, it } from 'vitest';
import { beforeAll, expect, describe, it } from 'vitest';
import { createTree } from '../src/utils';
import { readFile } from 'fs/promises';
import { debugStrToTree } from '../src/stories/debugToTree';
import * as path from 'path';

describe('tidy', () => {
/**
* it takes 20ms to layout 100k nodes
*/
it('benchmark tidy', async () => {
beforeAll(async () => {
const wasm = await readFile(
path.join(__dirname, '../wasm_dist/wasm_bg.wasm'),
);
await initWasm(wasm);
});

it('order', async () => {
const tidy = await TidyLayout.create(LayoutType.Tidy);
const root = createTree(100);
tidy.set_root(root);
tidy.layout();
for (let i = 1; i < root.children.length; i++) {
expect(root.children[i].x - root.children[i].width / 2).toBeGreaterThan(
root.children[i - 1].x - root.children[i - 1].width / 2,
);
}
});

/**
* it takes 20ms to layout 100k nodes
*/
it('benchmark tidy', async () => {
const tidy = await TidyLayout.create(LayoutType.Tidy);
const root = createTree(100_000);
tidy.set_root(root);
Expand All @@ -25,10 +40,6 @@ describe('tidy', () => {
});

it('benchmark naive', async () => {
const wasm = await readFile(
path.join(__dirname, '../wasm_dist/wasm_bg.wasm'),
);
await initWasm(wasm);
const tidy = await TidyLayout.create(LayoutType.Basic);
const root = createTree(100_000);
tidy.set_root(root);
Expand Down

0 comments on commit bef916a

Please sign in to comment.