From bd7a3fdd58ae7349cb3823814015747bd209bee0 Mon Sep 17 00:00:00 2001 From: Chris Hodapp Date: Sat, 3 Oct 2020 10:04:12 -0400 Subject: [PATCH] Fixed the bug for real. I think. Still needs cleanup... --- src/mesh.rs | 24 +++++++++++------------- src/rule.rs | 14 ++++++++------ 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/src/mesh.rs b/src/mesh.rs index db52438..897e884 100644 --- a/src/mesh.rs +++ b/src/mesh.rs @@ -155,15 +155,11 @@ impl MeshFunc { /// Treat this mesh as a 'parent' mesh to connect with any number /// of 'child' meshes, all of them paired with their respective /// vertex argument values (i.e. `arg_vals` from `Child`). - /// This returns a tuple of (new mesh, offsets), where 'offsets' - /// gives the offset of where child meshes were shifted in the new - /// mesh. - /// - /// That is, the vertices of 'children[i]' begin at vertex - /// 'offset[i]' of the new mesh. This is needed in order to adjust - /// references to vertices of a mesh in 'children' - such as - /// 'arg_vals' of `rule::Child`. - pub fn connect(&self, children: T) -> (MeshFunc, Vec) + /// This returns a tuple of (new mesh, new `arg_vals`), where + /// `arg_vals[i]` gives the remapped value of `children[i].arg_vals` + /// which accounts for where the referenced vertices were moved to. + /// (TODO: That definition is wrong; explain it right.) + pub fn connect(&self, children: T) -> (MeshFunc, Vec>) where U: Borrow, T: IntoIterator)> { @@ -174,7 +170,7 @@ impl MeshFunc { let mut verts: Vec = self.verts.clone(); let mut faces = self.faces.clone(); - let mut offsets: Vec = vec![]; + let mut offsets: Vec> = vec![]; println!("DEBUG: start verts={:?}", verts); @@ -193,6 +189,7 @@ impl MeshFunc { verts.extend(child.verts.iter().enumerate().filter_map(|(i,v)| { match v { VertexUnion::Vertex(_) => { + println!("placing child vert {} at {}", i, offset + j); remap[i] = j; j += 1; Some(v.clone()) @@ -222,7 +219,7 @@ impl MeshFunc { println!("DEBUG: offset={} mapping={:?}", offset, mapping); println!("DEBUG: remap={:?}", remap); - // All faces need copied, but if if the index was to + // All faces need copied, but if the index was to // a concrete vertex, then it needs shifted by 'offset'; // if an argument, it needs remapped. faces.extend(child.faces.iter().enumerate().map(|(i,n)| { @@ -230,14 +227,15 @@ impl MeshFunc { VertexUnion::Vertex(_) => remap[*n] + offset, VertexUnion::Arg(m) => mapping[m], }; - println!("face at i={} (n={}): new f={}, {} verts; vert={:?}", i, n, f, verts.len(), child.verts[*n]); + println!("face at i={} (n={}): new f={}, {} verts; vert={:?} -> {:?}", i, n, f, verts.len(), child.verts[*n], verts[f]); if f >= verts.len() { panic!("face >= num_verts") } f })); - offsets.push(offset); + let o2 = remap.iter().map(|n| n + offset).collect(); + offsets.push(o2); } let m = MeshFunc { diff --git a/src/rule.rs b/src/rule.rs index e7828c3..7626659 100644 --- a/src/rule.rs +++ b/src/rule.rs @@ -271,19 +271,21 @@ impl Rule { continue; } - let (g, offsets) = geom.connect(vec![(new_geom, child.arg_vals.clone())]); + let (g, remaps) = geom.connect(vec![(new_geom, child.arg_vals.clone())]); geom = g; // 'eval.children' may contain (via 'arg_vals') references to // indices of 'new_geom'. However, we don't connect() to // 'new_geom', but to the global geometry we just merged it - // into. To account for this, we must shift 'arg_vals' by - // the offset that 'geom.connect' gave us. - let off = offsets[0]; + // into. To account for this, we must remap 'arg_vals' by the + // mapping connect() gave us: + let remap = &remaps[0]; // (We pass a one-element vector to geom.connect() above - // so offsets always has just one element.) + // so remaps always has just one element.) for child in eval.children.iter_mut() { - child.arg_vals = child.arg_vals.iter().map(|n| n + off).collect(); + println!("DEBUG: got shifting child.arg_vals={:?}", child.arg_vals); + child.arg_vals = child.arg_vals.iter().map(|n| remap[*n]).collect(); + println!("DEBUG: new child.arg_vals={:?}", child.arg_vals); } // We're done evaluating this rule, so increment 'next'.