Add utility function to transform Vec<Vertex>

This commit is contained in:
Chris Hodapp 2020-03-08 14:34:44 -04:00
parent cf9124d04c
commit 7714f743d5
3 changed files with 12 additions and 9 deletions

View File

@ -20,8 +20,6 @@
a `Vec<Vertex>` to transform each element and make another vector. a `Vec<Vertex>` to transform each element and make another vector.
- I have seen many of my bugs come from: all this arithmetic on - I have seen many of my bugs come from: all this arithmetic on
indices. I generate vertex maps more or less manually. indices. I generate vertex maps more or less manually.
- Helper method to transform `Vec<Vertex>` and such. I do this
often.
- Docs on modules - Docs on modules
- Grep for all TODOs in code, really. - Grep for all TODOs in code, really.
- Look at everything in README.md in automata_scratch. - Look at everything in README.md in automata_scratch.

View File

@ -1,7 +1,7 @@
use nalgebra::*; use nalgebra::*;
//pub mod examples; //pub mod examples;
use crate::openmesh::{OpenMesh, Tag, Mat4, Vertex, vertex}; use crate::openmesh::{OpenMesh, Tag, Mat4, Vertex, vertex, transform};
use crate::rule::{Rule, RuleEval, Child}; use crate::rule::{Rule, RuleEval, Child};
use crate::prim; use crate::prim;
use crate::util; use crate::util;
@ -60,7 +60,7 @@ impl CurveHorn {
fn recur(&self) -> RuleEval<Self> { fn recur(&self) -> RuleEval<Self> {
let verts = self.seed.clone(); let verts = self.seed.clone();
let next_verts: Vec<Vertex> = verts.iter().map(|v| self.incr * v).collect(); let next_verts: Vec<Vertex> = transform(&verts, &self.incr);
let geom = OpenMesh { let geom = OpenMesh {
verts: next_verts.clone(), verts: next_verts.clone(),
@ -254,7 +254,7 @@ impl RamHorn {
vertex( 0.5, 0.5, 1.0), vertex( 0.5, 0.5, 1.0),
vertex( 0.5, -0.5, 1.0), vertex( 0.5, -0.5, 1.0),
]; ];
let next = seed.iter().map(|v| incr * v).collect(); let next = transform(&seed, &incr);
let geom = OpenMesh { let geom = OpenMesh {
verts: next, verts: next,
faces: vec![ faces: vec![
@ -303,12 +303,12 @@ impl Twist {
pub fn init(f: f32, subdiv: usize) -> (Twist, Rule<Twist>) { pub fn init(f: f32, subdiv: usize) -> (Twist, Rule<Twist>) {
let xf = geometry::Rotation3::from_axis_angle(&Vector3::x_axis(), -0.7).to_homogeneous(); let xf = geometry::Rotation3::from_axis_angle(&Vector3::x_axis(), -0.7).to_homogeneous();
let seed = vec![ let seed = transform(&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),
].iter().map(|v| xf * v).collect(); ], &xf);
let seed_sub = util::subdivide_cycle(&seed, subdiv); let seed_sub = util::subdivide_cycle(&seed, subdiv);
let t = Twist { let t = Twist {
dx0: 2.0, dx0: 2.0,
@ -350,7 +350,7 @@ impl Twist {
// Use byproducts of this to make 'count' copies of 'seed' with // Use byproducts of this to make 'count' copies of 'seed' with
// this same transform: // this same transform:
let meshes = children.iter().map(|child| { let meshes = children.iter().map(|child| {
let mut vs = self.seed_sub.iter().map(|v| child.xf * v).collect(); let mut vs = transform(&self.seed_sub, &child.xf);
// and in the process, generate faces for these seeds: // and in the process, generate faces for these seeds:
let (centroid, f) = util::connect_convex(&vs, false); let (centroid, f) = util::connect_convex(&vs, false);
vs.push(centroid); vs.push(centroid);
@ -370,7 +370,7 @@ impl Twist {
geometry::Rotation3::from_axis_angle(y, self.ang).to_homogeneous() * geometry::Rotation3::from_axis_angle(y, self.ang).to_homogeneous() *
geometry::Translation3::new(self.dx0, self.dy, 0.0).to_homogeneous(); geometry::Translation3::new(self.dx0, self.dy, 0.0).to_homogeneous();
let seed_orig = self.seed.iter().map(|v| incr * v).collect(); let seed_orig = transform(&self.seed, &incr);
let seed_sub = util::subdivide_cycle(&seed_orig, self.subdiv); let seed_sub = util::subdivide_cycle(&seed_orig, self.subdiv);
let n = seed_sub.len(); let n = seed_sub.len();

View File

@ -14,6 +14,11 @@ pub fn vertex(x: f32, y: f32, z: f32) -> Vertex {
Vertex::new(x, y, z, 1.0) Vertex::new(x, y, z, 1.0)
} }
/// Transforms a vector of vertices:
pub fn transform(verts: &Vec<Vertex>, xf: &Mat4) -> Vec<Vertex> {
verts.iter().map(|v| xf * v).collect()
}
/// A type for a 'tagged' vertex index referring either to an index of /// A type for a 'tagged' vertex index referring either to an index of
/// a mesh, or of its parent. /// a mesh, or of its parent.
#[derive(Clone, Debug)] #[derive(Clone, Debug)]