Added some helpers in util.rs
This commit is contained in:
parent
beff9d587b
commit
b454588f41
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
## Highest priority:
|
## Highest priority:
|
||||||
|
|
||||||
- Fix `twist` example.
|
- Clean up `twist` - maybe make a struct or trait.
|
||||||
- Do transforms compose in the *reverse* of automata_scratch? This
|
- Do transforms compose in the *reverse* of automata_scratch? This
|
||||||
appears to be the case.
|
appears to be the case.
|
||||||
|
|
||||||
|
|||||||
@ -4,6 +4,7 @@ use nalgebra::*;
|
|||||||
use crate::openmesh::{OpenMesh, Tag, Mat4, Vertex, vertex};
|
use crate::openmesh::{OpenMesh, Tag, Mat4, Vertex, vertex};
|
||||||
use crate::rule::{Rule, RuleStep, Child};
|
use crate::rule::{Rule, RuleStep, Child};
|
||||||
use crate::prim;
|
use crate::prim;
|
||||||
|
use crate::util;
|
||||||
|
|
||||||
fn curve_horn_start() -> RuleStep {
|
fn curve_horn_start() -> RuleStep {
|
||||||
let id = nalgebra::geometry::Transform3::identity().to_homogeneous();
|
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
|
// Meant to be a copy of twist_from_gen from Python & automata_scratch
|
||||||
pub fn twist_start() -> RuleStep {
|
pub fn twist_start() -> RuleStep {
|
||||||
//let ang=0.1;
|
//let ang=0.1;
|
||||||
let dz=0.2;
|
|
||||||
let dx0=2.0;
|
let dx0=2.0;
|
||||||
let dy=0.1;
|
|
||||||
let count=4;
|
let count=4;
|
||||||
// TODO: Factor these out (see twist)
|
// 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),
|
vertex( 0.5, 0.0, 0.5),
|
||||||
vertex( 0.5, 0.0, -0.5),
|
vertex(-0.5, 0.0, 0.5),
|
||||||
];
|
], 2);
|
||||||
// TODO: Subdivide method
|
// TODO: Factor out
|
||||||
|
let n = seed.len();
|
||||||
|
// TODO: Factor out subdiv size
|
||||||
|
|
||||||
// Quarter-turn in radians:
|
// Quarter-turn in radians:
|
||||||
let qtr = std::f32::consts::FRAC_PI_2;
|
let qtr = std::f32::consts::FRAC_PI_2;
|
||||||
@ -296,7 +297,7 @@ pub fn twist_start() -> RuleStep {
|
|||||||
Child {
|
Child {
|
||||||
rule: Rule::Recurse(twist),
|
rule: Rule::Recurse(twist),
|
||||||
xf: xf,
|
xf: xf,
|
||||||
vmap: (4*i..4*(i+count)).collect(), // N.B.
|
vmap: (n*i..n*(i+count)).collect(), // N.B.
|
||||||
}
|
}
|
||||||
}).collect();
|
}).collect();
|
||||||
|
|
||||||
@ -320,10 +321,8 @@ pub fn twist_start() -> RuleStep {
|
|||||||
|
|
||||||
pub fn twist() -> RuleStep {
|
pub fn twist() -> RuleStep {
|
||||||
let ang=0.1;
|
let ang=0.1;
|
||||||
let dz=0.2;
|
|
||||||
let dx0=2.0;
|
let dx0=2.0;
|
||||||
let dy=0.1;
|
let dy=0.1;
|
||||||
let count=4;
|
|
||||||
// TODO: Factor these out (see twist_start)
|
// TODO: Factor these out (see twist_start)
|
||||||
|
|
||||||
let y = &Vector3::y_axis();
|
let y = &Vector3::y_axis();
|
||||||
@ -331,34 +330,28 @@ pub fn twist() -> RuleStep {
|
|||||||
geometry::Rotation3::from_axis_angle(y, ang).to_homogeneous() *
|
geometry::Rotation3::from_axis_angle(y, ang).to_homogeneous() *
|
||||||
geometry::Translation3::new(dx0, dy, 0.0).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),
|
||||||
vertex( 0.5, 0.0, -0.5),
|
vertex(-0.5, 0.0, 0.5),
|
||||||
// TODO: Likewise factor these out
|
// TODO: Likewise factor these out
|
||||||
].iter().map(|v| incr * v).collect();
|
].iter().map(|v| incr * v).collect();
|
||||||
|
let seed = util::subdivide_cycle(&seed_orig, 2);
|
||||||
|
let n = seed.len();
|
||||||
|
// TODO: Factor out subdiv size
|
||||||
|
|
||||||
RuleStep {
|
RuleStep {
|
||||||
geom: OpenMesh {
|
geom: OpenMesh {
|
||||||
verts: seed,
|
verts: seed,
|
||||||
faces: vec![
|
faces: util::parallel_zigzag_faces(n),
|
||||||
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),
|
|
||||||
],
|
|
||||||
},
|
},
|
||||||
final_geom: prim::empty_mesh(), // TODO: Close properly
|
final_geom: prim::empty_mesh(), // TODO: Close properly
|
||||||
children: vec![
|
children: vec![
|
||||||
Child {
|
Child {
|
||||||
rule: Rule::Recurse(twist),
|
rule: Rule::Recurse(twist),
|
||||||
xf: incr,
|
xf: incr,
|
||||||
vmap: vec![0,1,2,3],
|
vmap: (0..n).collect(),
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
}
|
}
|
||||||
@ -366,6 +359,18 @@ pub fn twist() -> RuleStep {
|
|||||||
|
|
||||||
pub fn main() {
|
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| {
|
let run_test = |r: Rule, iters, name| {
|
||||||
println!("Running {}...", name);
|
println!("Running {}...", name);
|
||||||
let (mesh, nodes) = r.to_mesh(iters);
|
let (mesh, nodes) = r.to_mesh(iters);
|
||||||
|
|||||||
@ -2,6 +2,7 @@ pub mod examples;
|
|||||||
pub mod openmesh;
|
pub mod openmesh;
|
||||||
pub mod rule;
|
pub mod rule;
|
||||||
pub mod prim;
|
pub mod prim;
|
||||||
|
pub mod util;
|
||||||
|
|
||||||
//pub use crate::examples;
|
//pub use crate::examples;
|
||||||
//pub use crate::openmesh::test_thing;
|
//pub use crate::openmesh::test_thing;
|
||||||
|
|||||||
31
src/util.rs
Normal file
31
src/util.rs
Normal file
@ -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<Vertex>, count: usize) -> Vec<Vertex> {
|
||||||
|
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<Tag> {
|
||||||
|
(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()
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user