Trashed my 'exit group' nonsense for parent vertices.

This commit is contained in:
Chris Hodapp 2020-02-21 19:59:47 -05:00
parent cae2d3df10
commit 00c62a31b8
5 changed files with 101 additions and 84 deletions

View File

@ -11,8 +11,7 @@
- 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.
- Implement some of the tougher examples from the above too, e.g. the - Implement some of the tougher examples from the above too, e.g. the
triple nested spiral triple nested spiral. See `examples.py`.
- Lots of Rust-kosher refactoring (once I understand Rust better)
- Actual Rust-style docs! - Actual Rust-style docs!
## If I'm bored: ## If I'm bored:

View File

@ -12,33 +12,21 @@ fn curve_horn_start() -> RuleStep {
std::f32::consts::PI).to_homogeneous(); std::f32::consts::PI).to_homogeneous();
RuleStep { RuleStep {
geom: OpenMesh { geom: OpenMesh {
verts: vec![], verts: vec![
faces: vec![ vertex(-0.5, -0.5, 0.0),
Tag::Exit(1, 0), Tag::Exit(1, 2), Tag::Exit(0, 1), vertex(-0.5, 0.5, 0.0),
Tag::Exit(1, 2), Tag::Exit(0, 3), Tag::Exit(0, 1), vertex( 0.5, 0.5, 0.0),
Tag::Exit(0, 0), Tag::Exit(0, 2), Tag::Exit(1, 1), vertex( 0.5, -0.5, 0.0),
Tag::Exit(0, 2), Tag::Exit(1, 3), Tag::Exit(1, 1),
Tag::Exit(0, 3), Tag::Exit(1, 2), Tag::Exit(0, 2),
Tag::Exit(1, 2), Tag::Exit(1, 3), Tag::Exit(0, 2),
Tag::Exit(1, 0), Tag::Exit(0, 1), Tag::Exit(0, 0),
Tag::Exit(1, 1), Tag::Exit(1, 0), Tag::Exit(0, 0),
// The above is connecting group 0 to group 1,
// straight across + with diagonal - but with group 1
// being flipped 180, so we remap vertices (0,1,2,3)
// to (1,0,3,2) and then flip winding order.
], ],
exit_groups: vec![4, 4], faces: vec![],
}, },
final_geom: prim::empty_mesh(), final_geom: prim::empty_mesh(),
children: vec![ children: vec![
(Rule::Recurse(curve_horn_thing_rule), id), // exit group 0 (Rule::Recurse(curve_horn_thing_rule), id),
(Rule::Recurse(curve_horn_thing_rule), flip180), // exit group 1 (Rule::Recurse(curve_horn_thing_rule), flip180),
], ],
} }
// TODO: The starting vertices above are duplicated because I // TODO: Fix the consequences of the 180 flip
// don't have any way for an exit vertex to stand in for multiple
// child vertices that happen to share the same location. I don't
// yet know a good way around this, so I am duplicating vertices.
} }
fn curve_horn_thing_rule() -> RuleStep { fn curve_horn_thing_rule() -> RuleStep {
@ -51,49 +39,45 @@ fn curve_horn_thing_rule() -> RuleStep {
let verts = vec![ let verts = vec![
vertex(-0.5, -0.5, 0.0), vertex(-0.5, -0.5, 0.0),
vertex(0.5, -0.5, 0.0),
vertex(-0.5, 0.5, 0.0), vertex(-0.5, 0.5, 0.0),
vertex(0.5, 0.5, 0.0), vertex( 0.5, 0.5, 0.0),
vertex( 0.5, -0.5, 0.0),
]; ];
let final_verts: Vec<Vertex> = verts.iter().map(|v| m * v).collect(); let next_verts: Vec<Vertex> = verts.iter().map(|v| m * v).collect();
let geom = OpenMesh { let geom = OpenMesh {
verts: verts, verts: next_verts.clone(),
faces: vec![ faces: vec![
// The below is just connecting two groups of 4 vertices // The below is just connecting two groups of 4 vertices
// each, straight across and then to the next. Note that // each, straight across and then to the next.
// since 'verts' doesn't go in a circle, it will look a Tag::Body(1), Tag::Parent(0), Tag::Body(0),
// little strange. Tag::Parent(1), Tag::Parent(0), Tag::Body(1),
Tag::Body(1), Tag::Exit(0, 3), Tag::Exit(0, 1), Tag::Body(2), Tag::Parent(1), Tag::Body(1),
Tag::Body(1), Tag::Body(3), Tag::Exit(0, 3), Tag::Parent(2), Tag::Parent(1), Tag::Body(2),
Tag::Exit(0, 0), Tag::Body(2), Tag::Body(0), Tag::Body(3), Tag::Parent(2), Tag::Body(2),
Tag::Exit(0, 0), Tag::Exit(0, 2), Tag::Body(2), Tag::Parent(3), Tag::Parent(2), Tag::Body(3),
Tag::Body(2), Tag::Exit(0, 3), Tag::Body(3), Tag::Body(0), Tag::Parent(3), Tag::Body(3),
Tag::Body(2), Tag::Exit(0, 2), Tag::Exit(0, 3), Tag::Parent(0), Tag::Parent(3), Tag::Body(0),
Tag::Body(0), Tag::Body(1), Tag::Exit(0, 1),
Tag::Body(0), Tag::Exit(0, 1), Tag::Exit(0, 0),
// TODO: I should really generate these, not hard-code them. // TODO: I should really generate these, not hard-code them.
], ],
exit_groups: vec![4],
}; };
// TODO: This could be made slightly nicer by taking it to a peak // TODO: This could be made slightly nicer by taking it to a peak
// instead of just flattening it in XY, but this is a pretty minor // instead of just flattening it in XY, but this is a pretty minor
// change. // change.
let final_geom = OpenMesh { let final_geom = OpenMesh {
verts: final_verts, verts: next_verts,
faces: vec![ faces: vec![
Tag::Body(0), Tag::Body(1), Tag::Body(3), Tag::Body(0), Tag::Body(1), Tag::Body(3),
Tag::Body(0), Tag::Body(3), Tag::Body(2), Tag::Body(0), Tag::Body(3), Tag::Body(2),
], ],
exit_groups: vec![],
}; };
RuleStep{ RuleStep{
geom: geom, geom: geom,
final_geom: final_geom, final_geom: final_geom,
children: vec![ children: vec![
(Rule::Recurse(curve_horn_thing_rule), m), // exit group 0 (Rule::Recurse(curve_horn_thing_rule), m),
], ],
} }
} }
@ -127,11 +111,69 @@ fn cube_thing_rule() -> RuleStep {
RuleStep { RuleStep {
geom: mesh, geom: mesh,
final_geom: prim::empty_mesh(), // no exit groups final_geom: prim::empty_mesh(),
children: turns.iter().map(gen_rulestep).collect(), children: turns.iter().map(gen_rulestep).collect(),
} }
} }
/*
// Conversion from Python & automata_scratch
fn ram_horn_start() -> RuleStep {
let id = nalgebra::geometry::Transform3::identity().to_homogeneous();
let flip180 = nalgebra::geometry::Rotation3::from_axis_angle(
&nalgebra::Vector3::y_axis(),
std::f32::consts::PI).to_homogeneous();
RuleStep {
geom: OpenMesh {
// 'Bottom' vertices:
verts: vec![
vertex(-0.5, -0.5, -0.5),
vertex(-0.5, 0.5, -0.5),
vertex( 0.5, 0.5, -0.5),
vertex( 0.5, -0.5, -0.5),
],
faces: vec![
// bottom face:
Tag::Body(0), Tag::Body(1), Tag::Body(2),
Tag::Body(0), Tag::Body(2), Tag::Body(3),
// two faces straddling edge from vertex 0:
Tag::Body(0), Tag::Exit(0, 0), Tag::Exit(0, 1),
Tag::Body(0), Tag::Exit(0, 3), Tag::Exit(0, 0),
// two faces straddling edge from vertex 1:
Tag::Body(1), Tag::Exit(1, 0), Tag::Exit(1, 1),
Tag::Body(1), Tag::Exit(1, 3), Tag::Exit(1, 0),
// two faces straddling edge from vertex 2:
Tag::Body(2), Tag::Exit(2, 0), Tag::Exit(2, 1),
Tag::Body(2), Tag::Exit(2, 3), Tag::Exit(2, 0),
// two faces straddling edge from vertex 3:
Tag::Body(3), Tag::Exit(3, 0), Tag::Exit(3, 1),
Tag::Body(3), Tag::Exit(3, 3), Tag::Exit(3, 0),
// four faces from edge (0,1), (1,2), (2,3), (3,0):
Tag::Body(0), Tag::Exit(0, 1)/*=Tag::Exit(1, 3)*/, Tag::Body(1),
Tag::Body(1), Tag::Exit(1, 1)/*=Tag::Exit(2, 3)*/, Tag::Body(2),
Tag::Body(2), Tag::Exit(2, 1)/*=Tag::Exit(3, 3)*/, Tag::Body(3),
Tag::Body(3), Tag::Exit(3, 1)/*=Tag::Exit(0, 3)*/, Tag::Body(0),
],
exit_groups: vec![4, 4, 4, 4],
},
final_geom: prim::empty_mesh(),
children: vec![
(Rule::Recurse(ram_horn), id), // exit group 0
(Rule::Recurse(ram_horn), id), // exit group 1
(Rule::Recurse(ram_horn), id), // exit group 2
(Rule::Recurse(ram_horn), id), // exit group 3
],
}
// TODO: How do I handle *duplicated* exit vertices? In this
// instance, multiple children connect to some of them - e.g. all
// Tag::Exit(n,2) are together, and Tag::Exit(n,1) is the same as
// Tag::Exit((n+1)%4, 3).
}
fn ram_horn() -> RuleStep {
}
*/
pub fn main() { pub fn main() {
let run_test = |r: Rule, iters, name| { let run_test = |r: Rule, iters, name| {
@ -144,6 +186,6 @@ pub fn main() {
}; };
run_test(Rule::Recurse(cube_thing_rule), 3, "cube_thing"); run_test(Rule::Recurse(cube_thing_rule), 3, "cube_thing");
run_test(Rule::Recurse(curve_horn_thing_rule), 100, "curve_horn_thing"); //run_test(Rule::Recurse(curve_horn_thing_rule), 100, "curve_horn_thing");
run_test(Rule::Recurse(curve_horn_start), 100, "curve_horn2"); run_test(Rule::Recurse(curve_horn_start), 100, "curve_horn2");
} }

View File

@ -16,7 +16,7 @@ pub fn vertex(x: f32, y: f32, z: f32) -> Vertex {
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub enum Tag { pub enum Tag {
Body(usize), Body(usize),
Exit(usize, usize), // (group, vertex) Parent(usize),
} }
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
@ -26,7 +26,6 @@ pub struct OpenMesh {
// Triangles, taken as every 3 values, treated each as indices // Triangles, taken as every 3 values, treated each as indices
// into 'verts': // into 'verts':
pub faces: Vec<Tag>, pub faces: Vec<Tag>,
pub exit_groups: Vec<usize>,
} }
impl OpenMesh { impl OpenMesh {
@ -37,7 +36,6 @@ impl OpenMesh {
// TODO: Is the above faster if I pack vectors into a // TODO: Is the above faster if I pack vectors into a
// bigger matrix, and transform that? // bigger matrix, and transform that?
faces: self.faces.clone(), // TODO: Use Rc? faces: self.faces.clone(), // TODO: Use Rc?
exit_groups: self.exit_groups.clone(),
} }
} }
@ -59,7 +57,7 @@ impl OpenMesh {
let get_vert = |j| { let get_vert = |j| {
match self.faces[j] { match self.faces[j] {
Tag::Body(n) => self.verts[n].xyz(), Tag::Body(n) => self.verts[n].xyz(),
Tag::Exit(_, _) => panic!("Cannot write_stl() if mesh has exit groups!"), Tag::Parent(_) => panic!("Cannot write_stl() if mesh has parent references!"),
} }
}; };
// TODO: Handle this behavior // TODO: Handle this behavior
@ -92,49 +90,31 @@ impl OpenMesh {
let mut verts: Vec<Vertex> = self.verts.clone(); let mut verts: Vec<Vertex> = self.verts.clone();
let mut faces = self.faces.clone(); let mut faces = self.faces.clone();
let mut exit_groups: Vec<usize> = vec![]; for other in others {
let mut body_offset = self.verts.len(); // body_offset corresponds to the position in 'verts' at
let mut exit_offset = 0; // which we're appending everything in 'other.verts' -
let mut offsets: Vec<usize> = vec![0; others.len()]; // thus, the offset we shift all indices in 'others' by.
for (i,other) in others.iter().enumerate() { let body_offset = verts.len();
// Append body vertices & exit vertices directly: // Copy all vertices from 'other':
verts.append(&mut other.verts.clone()); verts.append(&mut other.verts.clone());
// Append faces, shifting each kind by respective offset: // Append its faces:
faces.extend(other.faces.iter().map(|t| { faces.extend(other.faces.iter().map(|t| {
match t { match t {
// Apply aforementioned shift to its body vertices:
Tag::Body(n) => Tag::Body(n + body_offset), Tag::Body(n) => Tag::Body(n + body_offset),
Tag::Exit(g, n) => Tag::Exit(g + exit_groups.len(), n + exit_offset), // Since 'self' vertices are in the same order,
// parent vertex references retain same index:
Tag::Parent(n) => Tag::Body(*n),
} }
})); }));
if i < self.exit_groups.len() {
exit_offset += self.exit_groups[i];
}
exit_groups.append(&mut other.exit_groups.clone());
offsets[i] = body_offset;
// Increase offsets by the number of elements we appended:
body_offset += other.verts.len();
}
// All of the Exit face indices from 'self' need to be
// modified to refer to Body vertices of something in
// 'others':
for i in 0..faces.len() {
match faces[i] {
Tag::Exit(g, n) => {
faces[i] = Tag::Body(n + offsets[g]);
},
_ => { },
};
} }
OpenMesh { OpenMesh {
verts: verts, verts: verts,
faces: faces, faces: faces,
exit_groups: exit_groups,
} }
} }
} }

View File

@ -6,7 +6,6 @@ pub fn empty_mesh() -> OpenMesh {
OpenMesh { OpenMesh {
verts: vec![], verts: vec![],
faces: vec![], faces: vec![],
exit_groups: vec![],
} }
} }
@ -36,6 +35,5 @@ pub fn cube() -> OpenMesh {
Tag::Body(0), Tag::Body(1), Tag::Body(5), Tag::Body(0), Tag::Body(1), Tag::Body(5),
Tag::Body(0), Tag::Body(5), Tag::Body(4), Tag::Body(0), Tag::Body(5), Tag::Body(4),
], ],
exit_groups: vec![],
}.transform(geometry::Translation3::new(-0.5, -0.5, -0.5).to_homogeneous()) }.transform(geometry::Translation3::new(-0.5, -0.5, -0.5).to_homogeneous())
} }

View File

@ -40,8 +40,6 @@ impl Rule {
pub fn to_mesh(&self, iters_left: u32) -> (OpenMesh, u32) { pub fn to_mesh(&self, iters_left: u32) -> (OpenMesh, u32) {
let mut nodes: u32 = 1; let mut nodes: u32 = 1;
if iters_left <= 0 { if iters_left <= 0 {