From 5f2723952507dd7e052500b3b0f9e0b1734b4faf Mon Sep 17 00:00:00 2001 From: Chris Hodapp Date: Sun, 5 Apr 2020 18:20:49 -0400 Subject: [PATCH] Semi-sensible refactor for ramhorn_twist? --- README.md | 2 ++ src/examples.rs | 83 ++++++++++++++++++++++++++++++++----------------- src/rule.rs | 4 ++- 3 files changed, 60 insertions(+), 29 deletions(-) diff --git a/README.md b/README.md index 9ef6901..6f8125e 100644 --- a/README.md +++ b/README.md @@ -46,3 +46,5 @@ obtain". Can I fix this somehow? Looks like a French-ism that made its way in. - Multithread! This looks very task-parallel anywhere that I branch. +- Would being able to name a rule node (perhaps conditionally under + some compile-time flag) help for debugging? diff --git a/src/examples.rs b/src/examples.rs index 21b8beb..f758290 100644 --- a/src/examples.rs +++ b/src/examples.rs @@ -282,7 +282,7 @@ fn ramhorn_twist(depth: usize) -> Rule { // Quarter-turn in radians: let qtr = std::f32::consts::FRAC_PI_2; - let x = Vector3::x_axis(); + let z = Vector3::z_axis(); let v = Unit::new_normalize(Vector3::new(-1.0, 0.0, 1.0)); let incr: Transform = Transform::new(). @@ -327,14 +327,26 @@ fn ramhorn_twist(depth: usize) -> Rule { vec![ Child { rule: next_rule.clone(), - xf: incr.translate(0.0, 0.0, 0.5).rotate(&x, -qtr/3.0), + xf: incr.scale(0.5).translate(1.0, 1.0, 2.0).rotate(&z, qtr*0.0), vmap: vec![0,1,2,3], }, Child { - rule: next_rule, - xf: incr.translate(0.0, 0.0, 0.5).rotate(&x, qtr/3.0), + rule: next_rule.clone(), + xf: incr.scale(0.5).translate(-1.0, 1.0, 2.0).rotate(&z, qtr*1.0), vmap: vec![0,1,2,3], }, + Child { + rule: next_rule.clone(), + xf: incr.scale(0.5).translate(-1.0, -1.0, 2.0).rotate(&z, qtr*2.0), + vmap: vec![0,1,2,3], + }, + Child { + rule: next_rule.clone(), + xf: incr.scale(0.5).translate(1.0, -1.0, 2.0).rotate(&z, qtr*3.0), + vmap: vec![0,1,2,3], + }, + // TODO: Factor out repetition + // TODO: Produce midpoint/centroid vertices like 'start' does below ] } else { let next_rule = Rule { @@ -365,7 +377,7 @@ fn ramhorn_twist(depth: usize) -> Rule { translate(0.0, 0.0, -1.0) }; - let start = move |self_: Rc>| -> RuleEval { + let trans = move |self_: Rc>| -> RuleEval { RuleEval { geom: Rc::new(OpenMesh { @@ -382,33 +394,25 @@ fn ramhorn_twist(depth: usize) -> Rule { vertex( 0.0, -0.5, 1.0), // 7 (connects 3-0) // Top middle: vertex( 0.0, 0.0, 1.0), // 8 - // 'Bottom' vertices: - vertex(-0.5, -0.5, 0.0), // 9 - vertex(-0.5, 0.5, 0.0), // 10 - vertex( 0.5, 0.5, 0.0), // 11 - vertex( 0.5, -0.5, 0.0), // 12 ], faces: vec![ - // bottom face: - Tag::Body(9), Tag::Body(10), Tag::Body(11), - Tag::Body(9), Tag::Body(11), Tag::Body(12), // two faces straddling edge from vertex 0: - Tag::Body(9), Tag::Body(0), Tag::Body(4), - Tag::Body(9), Tag::Body(7), Tag::Body(0), + Tag::Parent(0), Tag::Body(0), Tag::Body(4), + Tag::Parent(0), Tag::Body(7), Tag::Body(0), // two faces straddling edge from vertex 1: - Tag::Body(10), Tag::Body(1), Tag::Body(5), - Tag::Body(10), Tag::Body(4), Tag::Body(1), + Tag::Parent(1), Tag::Body(1), Tag::Body(5), + Tag::Parent(1), Tag::Body(4), Tag::Body(1), // two faces straddling edge from vertex 2: - Tag::Body(11), Tag::Body(2), Tag::Body(6), - Tag::Body(11), Tag::Body(5), Tag::Body(2), + Tag::Parent(2), Tag::Body(2), Tag::Body(6), + Tag::Parent(2), Tag::Body(5), Tag::Body(2), // two faces straddling edge from vertex 3: - Tag::Body(12), Tag::Body(3), Tag::Body(7), - Tag::Body(12), Tag::Body(6), Tag::Body(3), + Tag::Parent(3), Tag::Body(3), Tag::Body(7), + Tag::Parent(3), Tag::Body(6), Tag::Body(3), // four faces from edge (0,1), (1,2), (2,3), (3,0): - Tag::Body(9), Tag::Body(4), Tag::Body(10), - Tag::Body(10), Tag::Body(5), Tag::Body(11), - Tag::Body(11), Tag::Body(6), Tag::Body(12), - Tag::Body(12), Tag::Body(7), Tag::Body(9), + Tag::Parent(0), Tag::Body(4), Tag::Parent(1), + Tag::Parent(1), Tag::Body(5), Tag::Parent(2), + Tag::Parent(2), Tag::Body(6), Tag::Parent(3), + Tag::Parent(3), Tag::Body(7), Tag::Parent(0), ], }), final_geom: Rc::new(prim::empty_mesh()), @@ -436,12 +440,35 @@ fn ramhorn_twist(depth: usize) -> Rule { // TODO: These vertex mappings appear to be right. // Explain *why* they are right. // TODO: Factor out the repetition here. - // TODO: 4 Rc::new calls in a row with identical - // params... why not just Rc? ], } }; + let start = move |self_: Rc>| -> RuleEval { + RuleEval { + geom: Rc::new(OpenMesh { + verts: vec![ + vertex(-0.5, -0.5, 0.0), + vertex(-0.5, 0.5, 0.0), + vertex( 0.5, 0.5, 0.0), + vertex( 0.5, -0.5, 0.0), + ], + faces: vec![ + Tag::Body(0), Tag::Body(1), Tag::Body(2), + Tag::Body(0), Tag::Body(2), Tag::Body(3), + ], + }), + final_geom: Rc::new(prim::empty_mesh()), + children: vec![ + Child { + rule: Rc::new(Rule { eval: Rc::new(trans.clone()), ctxt: self_.ctxt }), + xf: Transform::new(), + vmap: vec![0,1,2,3], + }, + ], + } + }; + Rule { eval: Rc::new(start), ctxt: RamHornCtxt { depth } } } @@ -619,5 +646,5 @@ pub fn main() { run_test_iter(&Rc::new(cube_thing()), 3, "cube_thing3"); run_test_iter(&Rc::new(twist(1.0, 2)), 200, "twist"); run_test_iter(&Rc::new(ramhorn()), 100, "ram_horn3"); - run_test_iter(&Rc::new(ramhorn_twist(6)), 40, "ram_horn3b_bug"); + run_test_iter(&Rc::new(ramhorn_twist(5)), 30, "ram_horn3b_bug"); } diff --git a/src/rule.rs b/src/rule.rs index e1e9236..2764aa4 100644 --- a/src/rule.rs +++ b/src/rule.rs @@ -222,6 +222,8 @@ impl Rule { // account for this, we must shift vmap by the offset that // 'geom.connect' gave us: let off = offsets[0]; + // (We pass a one-element vector to geom.connect() above + // so offsets always has just one element.) for child in eval.children.iter_mut() { child.vmap = child.vmap.iter().map(|n| n + off).collect(); } @@ -236,7 +238,7 @@ impl Rule { } if !eval.children.is_empty() { - // Recurse further (i.e. put more onto stack): + // Recurse further (i.e. put more onto stack): stack.push(State { rules: eval.children, next: 0,