From b454588f41efced793c22649a2328607c4879105 Mon Sep 17 00:00:00 2001 From: Chris Hodapp Date: Sun, 23 Feb 2020 20:54:24 -0500 Subject: [PATCH] Added some helpers in util.rs --- README.md | 2 +- src/examples.rs | 53 +++++++++++++++++++++++++++---------------------- src/lib.rs | 1 + src/util.rs | 31 +++++++++++++++++++++++++++++ 4 files changed, 62 insertions(+), 25 deletions(-) create mode 100644 src/util.rs diff --git a/README.md b/README.md index 9362e72..9a8c440 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ ## Highest priority: -- Fix `twist` example. +- Clean up `twist` - maybe make a struct or trait. - Do transforms compose in the *reverse* of automata_scratch? This appears to be the case. diff --git a/src/examples.rs b/src/examples.rs index 6485e5b..8d373fc 100644 --- a/src/examples.rs +++ b/src/examples.rs @@ -4,6 +4,7 @@ use nalgebra::*; use crate::openmesh::{OpenMesh, Tag, Mat4, Vertex, vertex}; use crate::rule::{Rule, RuleStep, Child}; use crate::prim; +use crate::util; fn curve_horn_start() -> RuleStep { let id = nalgebra::geometry::Transform3::identity().to_homogeneous(); @@ -267,19 +268,19 @@ fn ram_horn_branch() -> RuleStep { // Meant to be a copy of twist_from_gen from Python & automata_scratch pub fn twist_start() -> RuleStep { //let ang=0.1; - let dz=0.2; let dx0=2.0; - let dy=0.1; let count=4; // TODO: Factor these out (see twist) - let seed = vec![ + let seed = util::subdivide_cycle(&vec![ vertex(-0.5, 0.0, -0.5), - vertex(-0.5, 0.0, 0.5), + vertex( 0.5, 0.0, -0.5), vertex( 0.5, 0.0, 0.5), - vertex( 0.5, 0.0, -0.5), - ]; - // TODO: Subdivide method + vertex(-0.5, 0.0, 0.5), + ], 2); + // TODO: Factor out + let n = seed.len(); + // TODO: Factor out subdiv size // Quarter-turn in radians: let qtr = std::f32::consts::FRAC_PI_2; @@ -296,7 +297,7 @@ pub fn twist_start() -> RuleStep { Child { rule: Rule::Recurse(twist), xf: xf, - vmap: (4*i..4*(i+count)).collect(), // N.B. + vmap: (n*i..n*(i+count)).collect(), // N.B. } }).collect(); @@ -320,10 +321,8 @@ pub fn twist_start() -> RuleStep { pub fn twist() -> RuleStep { let ang=0.1; - let dz=0.2; let dx0=2.0; let dy=0.1; - let count=4; // TODO: Factor these out (see twist_start) let y = &Vector3::y_axis(); @@ -331,34 +330,28 @@ pub fn twist() -> RuleStep { geometry::Rotation3::from_axis_angle(y, ang).to_homogeneous() * geometry::Translation3::new(dx0, dy, 0.0).to_homogeneous(); - let seed = vec![ + let seed_orig = vec![ vertex(-0.5, 0.0, -0.5), - vertex(-0.5, 0.0, 0.5), + vertex( 0.5, 0.0, -0.5), vertex( 0.5, 0.0, 0.5), - vertex( 0.5, 0.0, -0.5), + vertex(-0.5, 0.0, 0.5), // TODO: Likewise factor these out ].iter().map(|v| incr * v).collect(); + let seed = util::subdivide_cycle(&seed_orig, 2); + let n = seed.len(); + // TODO: Factor out subdiv size RuleStep { geom: OpenMesh { verts: seed, - faces: vec![ - Tag::Body(1), Tag::Parent(0), Tag::Body(0), - Tag::Parent(1), Tag::Parent(0), Tag::Body(1), - Tag::Body(2), Tag::Parent(1), Tag::Body(1), - Tag::Parent(2), Tag::Parent(1), Tag::Body(2), - Tag::Body(3), Tag::Parent(2), Tag::Body(2), - Tag::Parent(3), Tag::Parent(2), Tag::Body(3), - Tag::Body(0), Tag::Parent(3), Tag::Body(3), - Tag::Parent(0), Tag::Parent(3), Tag::Body(0), - ], + faces: util::parallel_zigzag_faces(n), }, final_geom: prim::empty_mesh(), // TODO: Close properly children: vec![ Child { rule: Rule::Recurse(twist), xf: incr, - vmap: vec![0,1,2,3], + vmap: (0..n).collect(), }, ], } @@ -366,6 +359,18 @@ pub fn twist() -> RuleStep { pub fn main() { + { + let vs = vec![ + vertex(-0.5, 0.0, -0.5), + vertex( 0.5, 0.0, -0.5), + vertex( 0.5, 0.0, 0.5), + vertex(-0.5, 0.0, 0.5), + ]; + let vs2 = util::subdivide_cycle(&vs, 2); + println!("vs={:?}", vs); + println!("vs2={:?}", vs2); + } + let run_test = |r: Rule, iters, name| { println!("Running {}...", name); let (mesh, nodes) = r.to_mesh(iters); diff --git a/src/lib.rs b/src/lib.rs index 9fb430e..32daf02 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,6 +2,7 @@ pub mod examples; pub mod openmesh; pub mod rule; pub mod prim; +pub mod util; //pub use crate::examples; //pub use crate::openmesh::test_thing; diff --git a/src/util.rs b/src/util.rs new file mode 100644 index 0000000..66baa85 --- /dev/null +++ b/src/util.rs @@ -0,0 +1,31 @@ +use crate::openmesh::{OpenMesh, Tag, Mat4, Vertex, vertex}; +use crate::rule::{Rule, RuleStep, Child}; + +/// Linearly subdivides a list of points that are to be treated as a +/// cycle. This produces 'count' points for every element of 'p' +/// (thus, the returned length will be `count*p.len()`). +pub fn subdivide_cycle(p: &Vec, count: usize) -> Vec { + let n = p.len(); + (0..n).map(|i| { + // The inner loop is interpolating between v1 and v2: + let v1 = &p[i]; + let v2 = &p[(i+1) % n]; + (0..count).map(move |j| { + // This is just lerping in count+1 equally spaced steps: + let f = (j as f32) / (count as f32); + v1*(1.0-f) + v2*f + }) + }).flatten().collect() +} +// TODO: This can be generalized to an iterator or to IntoIterator +// trait bound + +pub fn parallel_zigzag_faces(count: usize) -> Vec { + (0..count).map(|i0| { + let i1 = (i0+1) % count; + vec![ + Tag::Body(i1), Tag::Parent(i0), Tag::Body(i0), + Tag::Parent(i1), Tag::Parent(i0), Tag::Body(i1), + ] + }).flatten().collect() +}