Fix ramhorn_branch example
This commit is contained in:
parent
c68c06e018
commit
82eeac71a8
@ -66,9 +66,8 @@ related to Rust.
|
|||||||
|
|
||||||
## Highest priority:
|
## Highest priority:
|
||||||
|
|
||||||
- Fix `ramhorn_branch`.
|
- See about a refactor that respects the same model, but involves
|
||||||
- Once I've fixed that, see about a refactor that respects the
|
much less ceremony and boilerplate.
|
||||||
same model, but involves much less ceremony and boilerplate.
|
|
||||||
- Look at performance.
|
- Look at performance.
|
||||||
- Start at `to_mesh_iter()`. The cost of small appends/connects
|
- Start at `to_mesh_iter()`. The cost of small appends/connects
|
||||||
seems to be killing performance.
|
seems to be killing performance.
|
||||||
|
|||||||
168
src/examples.rs
168
src/examples.rs
@ -9,7 +9,7 @@ use crate::util;
|
|||||||
use crate::util::VecExt;
|
use crate::util::VecExt;
|
||||||
use crate::mesh::{Mesh, MeshFunc, VertexUnion, vert_args};
|
use crate::mesh::{Mesh, MeshFunc, VertexUnion, vert_args};
|
||||||
use crate::xform::{Transform, Vertex, vertex, id};
|
use crate::xform::{Transform, Vertex, vertex, id};
|
||||||
use crate::rule::{Rule, RuleEval, Child};
|
use crate::rule::{Rule, RuleFn, RuleEval, Child};
|
||||||
use crate::prim;
|
use crate::prim;
|
||||||
use crate::dcel;
|
use crate::dcel;
|
||||||
use crate::dcel::{VertSpec};
|
use crate::dcel::{VertSpec};
|
||||||
@ -812,12 +812,7 @@ pub fn ramhorn() -> Rule<()> {
|
|||||||
],
|
],
|
||||||
};
|
};
|
||||||
let final_geom = MeshFunc {
|
let final_geom = MeshFunc {
|
||||||
verts: vec![
|
verts: vert_args(s4..s7),
|
||||||
VertexUnion::Arg(s4),
|
|
||||||
VertexUnion::Arg(s5),
|
|
||||||
VertexUnion::Arg(s6),
|
|
||||||
VertexUnion::Arg(s7),
|
|
||||||
],
|
|
||||||
// TODO: Factor out this repetition
|
// TODO: Factor out this repetition
|
||||||
faces: vec![
|
faces: vec![
|
||||||
0, 2, 1,
|
0, 2, 1,
|
||||||
@ -907,8 +902,6 @@ pub fn ramhorn() -> Rule<()> {
|
|||||||
Rule { eval: Rc::new(start), ctxt: () }
|
Rule { eval: Rc::new(start), ctxt: () }
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
|
|
||||||
#[derive(Copy, Clone)]
|
#[derive(Copy, Clone)]
|
||||||
pub struct RamHornCtxt {
|
pub struct RamHornCtxt {
|
||||||
depth: usize,
|
depth: usize,
|
||||||
@ -922,30 +915,30 @@ pub fn ramhorn_branch(depth: usize, f: f32) -> Rule<RamHornCtxt> {
|
|||||||
rotate(&v, 0.4 * f).
|
rotate(&v, 0.4 * f).
|
||||||
scale(1.0 - (1.0 - 0.95)*f);
|
scale(1.0 - (1.0 - 0.95)*f);
|
||||||
|
|
||||||
let seed = vec![
|
let (a0, s0, sn);
|
||||||
vertex(-0.5, -0.5, 0.0),
|
let seed = vec_indexed![
|
||||||
vertex(-0.5, 0.5, 0.0),
|
@a0 VertexUnion::Arg(0),
|
||||||
vertex( 0.5, 0.5, 0.0),
|
VertexUnion::Arg(1),
|
||||||
vertex( 0.5, -0.5, 0.0),
|
VertexUnion::Arg(2),
|
||||||
|
VertexUnion::Arg(3),
|
||||||
|
@s0 VertexUnion::Vertex(vertex(-0.5, -0.5, 0.0)),
|
||||||
|
VertexUnion::Vertex(vertex(-0.5, 0.5, 0.0)),
|
||||||
|
VertexUnion::Vertex(vertex( 0.5, 0.5, 0.0)),
|
||||||
|
VertexUnion::Vertex(vertex( 0.5, -0.5, 0.0)),
|
||||||
|
@sn,
|
||||||
];
|
];
|
||||||
let next = incr.transform(&seed);
|
let geom = util::parallel_zigzag(seed.clone(), s0..sn, a0..s0).transform(&incr);
|
||||||
let geom = Rc::new(OpenMesh {
|
let final_geom = MeshFunc {
|
||||||
verts: next,
|
verts: seed.clone(),
|
||||||
alias_verts: vec![],
|
|
||||||
faces: util::parallel_zigzag_faces(4),
|
|
||||||
// TODO: Fix this (parallel_zigzag_faces has parents)
|
|
||||||
});
|
|
||||||
let final_geom = Rc::new(OpenMesh {
|
|
||||||
verts: vec![],
|
|
||||||
alias_verts: vec![0, 1, 2, 3],
|
|
||||||
faces: vec![
|
faces: vec![
|
||||||
0, 2, 1,
|
s0 + 0, s0 + 2, s0 + 1,
|
||||||
0, 3, 2,
|
s0 + 0, s0 + 3, s0 + 2,
|
||||||
],
|
],
|
||||||
});
|
}.transform(&incr);
|
||||||
|
// TODO: Why is this redundant transform needed?
|
||||||
|
|
||||||
let opening_xform = |i| {
|
let opening_xform = |i| {
|
||||||
let r = FRAC_PI_2 * i;
|
let r = FRAC_PI_2 * (i as f32);
|
||||||
Transform::new().
|
Transform::new().
|
||||||
rotate(&nalgebra::Vector3::z_axis(), r).
|
rotate(&nalgebra::Vector3::z_axis(), r).
|
||||||
translate(0.25, 0.25, 0.0).
|
translate(0.25, 0.25, 0.0).
|
||||||
@ -953,19 +946,24 @@ pub fn ramhorn_branch(depth: usize, f: f32) -> Rule<RamHornCtxt> {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// 'transition' geometry (when something splits):
|
// 'transition' geometry (when something splits):
|
||||||
let trans_verts = vec![
|
let (v0, v1, v2, v3, m01, m12, m23, m30, mid);
|
||||||
|
let trans_verts = vec_indexed![
|
||||||
|
VertexUnion::Arg(0),
|
||||||
|
VertexUnion::Arg(1),
|
||||||
|
VertexUnion::Arg(2),
|
||||||
|
VertexUnion::Arg(3),
|
||||||
// 'Top' vertices:
|
// 'Top' vertices:
|
||||||
vertex(-0.5, -0.5, 0.0), // 0 (above 9)
|
@v0 VertexUnion::Vertex(vertex(-0.5, -0.5, 0.0)), // 0 (above 9)
|
||||||
vertex(-0.5, 0.5, 0.0), // 1 (above 10)
|
@v1 VertexUnion::Vertex(vertex(-0.5, 0.5, 0.0)), // 1 (above 10)
|
||||||
vertex( 0.5, 0.5, 0.0), // 2 (above 11)
|
@v2 VertexUnion::Vertex(vertex( 0.5, 0.5, 0.0)), // 2 (above 11)
|
||||||
vertex( 0.5, -0.5, 0.0), // 3 (above 12)
|
@v3 VertexUnion::Vertex(vertex( 0.5, -0.5, 0.0)), // 3 (above 12)
|
||||||
// Top edge midpoints:
|
// Top edge midpoints:
|
||||||
vertex(-0.5, 0.0, 0.0), // 4 (connects 0-1)
|
@m01 VertexUnion::Vertex(vertex(-0.5, 0.0, 0.0)), // 4 (connects 0-1)
|
||||||
vertex( 0.0, 0.5, 0.0), // 5 (connects 1-2)
|
@m12 VertexUnion::Vertex(vertex( 0.0, 0.5, 0.0)), // 5 (connects 1-2)
|
||||||
vertex( 0.5, 0.0, 0.0), // 6 (connects 2-3)
|
@m23 VertexUnion::Vertex(vertex( 0.5, 0.0, 0.0)), // 6 (connects 2-3)
|
||||||
vertex( 0.0, -0.5, 0.0), // 7 (connects 3-0)
|
@m30 VertexUnion::Vertex(vertex( 0.0, -0.5, 0.0)), // 7 (connects 3-0)
|
||||||
// Top middle:
|
// Top middle:
|
||||||
vertex( 0.0, 0.0, 0.0), // 8
|
@mid VertexUnion::Vertex(vertex( 0.0, 0.0, 0.0)), // 8
|
||||||
];
|
];
|
||||||
let trans_faces = vec![
|
let trans_faces = vec![
|
||||||
// two faces straddling edge from vertex 0:
|
// two faces straddling edge from vertex 0:
|
||||||
@ -986,46 +984,30 @@ pub fn ramhorn_branch(depth: usize, f: f32) -> Rule<RamHornCtxt> {
|
|||||||
2, 10, 3,
|
2, 10, 3,
|
||||||
3, 11, 0,
|
3, 11, 0,
|
||||||
];
|
];
|
||||||
let trans_geom = Rc::new(OpenMesh {
|
let trans_geom = MeshFunc {
|
||||||
alias_verts: vec![0, 1, 2, 3],
|
|
||||||
verts: trans_verts.clone(),
|
verts: trans_verts.clone(),
|
||||||
faces: trans_faces.clone(),
|
faces: trans_faces.clone(),
|
||||||
});
|
};
|
||||||
let trans_children = move |recur: RuleFn<RamHornCtxt>, ctxt: RamHornCtxt| {
|
let trans_children = move |recur: RuleFn<RamHornCtxt>, ctxt: RamHornCtxt| {
|
||||||
vec![
|
vec![
|
||||||
Child {
|
child!(rule!(recur, ctxt), opening_xform(0), m12, v2, m23, mid),
|
||||||
rule: Rc::new(Rule { eval: recur.clone(), ctxt }),
|
child!(rule!(recur, ctxt), opening_xform(1), m01, v1, m12, mid),
|
||||||
xf: opening_xform(0.0),
|
child!(rule!(recur, ctxt), opening_xform(2), m30, v0, m01, mid),
|
||||||
arg_vals: vec![5,2,6,8],
|
child!(rule!(recur, ctxt), opening_xform(3), m23, v3, m30, mid),
|
||||||
},
|
|
||||||
Child {
|
|
||||||
rule: Rc::new(Rule { eval: recur.clone(), ctxt }),
|
|
||||||
xf: opening_xform(1.0),
|
|
||||||
arg_vals: vec![4,1,5,8],
|
|
||||||
},
|
|
||||||
Child {
|
|
||||||
rule: Rc::new(Rule { eval: recur.clone(), ctxt }),
|
|
||||||
xf: opening_xform(2.0),
|
|
||||||
arg_vals: vec![7,0,4,8],
|
|
||||||
},
|
|
||||||
Child {
|
|
||||||
rule: Rc::new(Rule { eval: recur.clone(), ctxt }),
|
|
||||||
xf: opening_xform(3.0),
|
|
||||||
arg_vals: vec![6,3,7,8],
|
|
||||||
},
|
|
||||||
// TODO: These vertex mappings appear to be right.
|
// TODO: These vertex mappings appear to be right.
|
||||||
// Explain *why* they are right.
|
// Explain *why* they are right.
|
||||||
// TODO: Factor out the repetition here.
|
|
||||||
]
|
]
|
||||||
};
|
};
|
||||||
|
|
||||||
let tg = trans_geom.clone();
|
let tg = Rc::new(trans_geom);
|
||||||
|
let fg = Rc::new(final_geom);
|
||||||
|
let g = Rc::new(geom);
|
||||||
// TODO: Why is that necessary?
|
// TODO: Why is that necessary?
|
||||||
let recur = move |self_: Rc<Rule<RamHornCtxt>>| -> RuleEval<RamHornCtxt> {
|
let recur = rule_fn!(RamHornCtxt => |self_, tg| {
|
||||||
if self_.ctxt.depth <= 0 {
|
if self_.ctxt.depth <= 0 {
|
||||||
RuleEval {
|
RuleEval {
|
||||||
geom: tg.clone(),
|
geom: tg,
|
||||||
final_geom: final_geom.clone(),
|
final_geom: fg.clone(),
|
||||||
// This final_geom will leave midpoint/centroid
|
// This final_geom will leave midpoint/centroid
|
||||||
// vertices, but stopping here means none are
|
// vertices, but stopping here means none are
|
||||||
// connected anyway - so they can just be ignored.
|
// connected anyway - so they can just be ignored.
|
||||||
@ -1037,51 +1019,45 @@ pub fn ramhorn_branch(depth: usize, f: f32) -> Rule<RamHornCtxt> {
|
|||||||
ctxt: RamHornCtxt { depth: self_.ctxt.depth - 1 },
|
ctxt: RamHornCtxt { depth: self_.ctxt.depth - 1 },
|
||||||
};
|
};
|
||||||
RuleEval {
|
RuleEval {
|
||||||
geom: geom.clone(),
|
geom: g.clone(),
|
||||||
final_geom: final_geom.clone(),
|
final_geom: fg.clone(),
|
||||||
children: vec![
|
children: vec![
|
||||||
Child {
|
child!(Rc::new(next_rule), incr, s0, s0+1, s0+2, s0+3),
|
||||||
rule: Rc::new(next_rule),
|
|
||||||
xf: incr,
|
|
||||||
arg_vals: vec![0,1,2,3],
|
|
||||||
},
|
|
||||||
],
|
],
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
});
|
||||||
|
|
||||||
let trans = move |self_: Rc<Rule<RamHornCtxt>>| -> RuleEval<RamHornCtxt> {
|
let trans = rule_fn!(RamHornCtxt => |self_| {
|
||||||
RuleEval {
|
RuleEval {
|
||||||
geom: trans_geom.clone(),
|
geom: tg.clone(),
|
||||||
final_geom: Rc::new(prim::empty_mesh()),
|
final_geom: Rc::new(prim::empty_mesh().to_meshfunc()),
|
||||||
children: trans_children(Rc::new(recur.clone()), self_.ctxt),
|
children: trans_children(recur.clone(), self_.ctxt),
|
||||||
}
|
}
|
||||||
};
|
});
|
||||||
|
|
||||||
let start = move |self_: Rc<Rule<RamHornCtxt>>| -> RuleEval<RamHornCtxt> {
|
let start = rule_fn!(RamHornCtxt => |self_, seed| {
|
||||||
|
let g = MeshFunc {
|
||||||
|
verts: seed[s0..sn].to_vec(),
|
||||||
|
// FIXME (use names for indices)
|
||||||
|
faces: vec![
|
||||||
|
0, 1, 2,
|
||||||
|
0, 2, 3,
|
||||||
|
],
|
||||||
|
}.transform(&id().translate(0.0, 0.0, -0.5));
|
||||||
RuleEval {
|
RuleEval {
|
||||||
geom: Rc::new(OpenMesh {
|
geom: Rc::new(g),
|
||||||
verts: Transform::new().translate(0.0, 0.0, -0.5).transform(&seed),
|
final_geom: Rc::new(prim::empty_mesh().to_meshfunc()),
|
||||||
alias_verts: vec![],
|
|
||||||
faces: vec![
|
|
||||||
0, 1, 2,
|
|
||||||
0, 2, 3,
|
|
||||||
],
|
|
||||||
}),
|
|
||||||
final_geom: Rc::new(prim::empty_mesh()),
|
|
||||||
children: vec![
|
children: vec![
|
||||||
Child {
|
child!(rule!(trans, self_.ctxt), id(), 0, 1, 2, 3),
|
||||||
rule: Rc::new(Rule { eval: Rc::new(trans.clone()), ctxt: self_.ctxt }),
|
|
||||||
xf: Transform::new(),
|
|
||||||
arg_vals: vec![0,1,2,3],
|
|
||||||
},
|
|
||||||
],
|
],
|
||||||
}
|
}
|
||||||
};
|
});
|
||||||
|
|
||||||
Rule { eval: Rc::new(start), ctxt: RamHornCtxt { depth } }
|
Rule { eval: start, ctxt: RamHornCtxt { depth } }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
#[derive(Copy, Clone)]
|
#[derive(Copy, Clone)]
|
||||||
pub struct RamHornCtxt2 {
|
pub struct RamHornCtxt2 {
|
||||||
depth: usize,
|
depth: usize,
|
||||||
|
|||||||
@ -128,12 +128,12 @@ mod tests {
|
|||||||
run_test(examples::ramhorn(), 100, "ram_horn3", false);
|
run_test(examples::ramhorn(), 100, "ram_horn3", false);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
#[test]
|
#[test]
|
||||||
fn ramhorn_branch() {
|
fn ramhorn_branch() {
|
||||||
run_test(examples::ramhorn_branch(24, 0.25), 32, "ram_horn_branch", false);
|
run_test(examples::ramhorn_branch(24, 0.25), 32, "ram_horn_branch", false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
#[test]
|
#[test]
|
||||||
fn ramhorn_branch_random() {
|
fn ramhorn_branch_random() {
|
||||||
run_test(examples::ramhorn_branch_random(24, 0.25), 32, "ram_horn_branch_random", false);
|
run_test(examples::ramhorn_branch_random(24, 0.25), 32, "ram_horn_branch_random", false);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user