From 2017d9a605d66b833e947b4c141c70cdf4c69250 Mon Sep 17 00:00:00 2001 From: Chris Hodapp Date: Tue, 6 Oct 2020 10:12:19 -0400 Subject: [PATCH] barbs example: end recursion by scale check, not iteration count --- src/examples.rs | 10 ++++++++-- src/xform.rs | 7 +++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/examples.rs b/src/examples.rs index 7a3a3c0..633b8db 100644 --- a/src/examples.rs +++ b/src/examples.rs @@ -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]) { - if iters <= 0 { + if self.depth_check(&xform, iters) { self.faces.extend_from_slice(&[ bound[0], bound[2], bound[1], bound[0], bound[3], bound[2], @@ -118,7 +124,7 @@ impl BarbsCtxt { 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(&[ bound[0], bound[2], bound[1], bound[0], bound[3], bound[2], diff --git a/src/xform.rs b/src/xform.rs index 9f5e582..e529fa0 100644 --- a/src/xform.rs +++ b/src/xform.rs @@ -48,6 +48,13 @@ impl Transform { pub fn transform(&self, verts: &Vec) -> Vec { 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 {