barbs example: end recursion by scale check, not iteration count

This commit is contained in:
Chris Hodapp 2020-10-06 10:12:19 -04:00
parent 004f665e4b
commit 2017d9a605
2 changed files with 15 additions and 2 deletions

View File

@ -97,9 +97,15 @@ impl BarbsCtxt {
} }
} }
pub fn depth_check(&self, xform: &Transform, iters: usize) -> bool {
// Assume all scales are the same (for now)
let (s, _, _) = xform.get_scale();
return s < 0.005;
}
pub fn barb(&mut self, iters: usize, xform: Transform, bound: [usize; 4]) { pub fn barb(&mut self, iters: usize, xform: Transform, bound: [usize; 4]) {
if iters <= 0 { if self.depth_check(&xform, iters) {
self.faces.extend_from_slice(&[ self.faces.extend_from_slice(&[
bound[0], bound[2], bound[1], bound[0], bound[2], bound[1],
bound[0], bound[3], bound[2], bound[0], bound[3], bound[2],
@ -118,7 +124,7 @@ impl BarbsCtxt {
pub fn main(&mut self, iters: usize, xform: Transform, bound: [usize; 4]) { pub fn main(&mut self, iters: usize, xform: Transform, bound: [usize; 4]) {
if iters <= 0 { if self.depth_check(&xform, iters) {
self.faces.extend_from_slice(&[ self.faces.extend_from_slice(&[
bound[0], bound[2], bound[1], bound[0], bound[2], bound[1],
bound[0], bound[3], bound[2], bound[0], bound[3], bound[2],

View File

@ -48,6 +48,13 @@ impl Transform {
pub fn transform(&self, verts: &Vec<Vertex>) -> Vec<Vertex> { pub fn transform(&self, verts: &Vec<Vertex>) -> Vec<Vertex> {
verts.iter().map(|v| self.mtx * v).collect() verts.iter().map(|v| self.mtx * v).collect()
} }
/// Returns X, Y, and Z scale factors for this transformation.
pub fn get_scale(&self) -> (f32, f32, f32) {
(self.mtx.column(0).norm(),
self.mtx.column(1).norm(),
self.mtx.column(2).norm())
}
} }
impl Mul for Transform { impl Mul for Transform {