Skip to content

Commit

Permalink
Implement extrude/bore from bottom as well
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom committed Jan 27, 2024
1 parent ebcdfe3 commit fe31774
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 11 deletions.
10 changes: 10 additions & 0 deletions detailer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -957,6 +957,16 @@ impl<'a> Widget<'a> {
group.amt = Some(amt);
}
}

if group.typ == GroupType::Extrude || group.typ == GroupType::Bore {
let mut bottom = group.bottom.is_some();
if ui.checkbox(
&mut bottom,
"Bottom",
).changed() {
group.bottom = bottom.then(|| ());
}
}
});
}
_ => {}
Expand Down
5 changes: 5 additions & 0 deletions drawing/src/data/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub struct Group {
pub features: Vec<FeatureKey>,

pub amt: Option<f64>,
pub bottom: Option<()>,
}

impl Group {
Expand All @@ -36,6 +37,7 @@ impl Group {
typ: self.typ,
name: self.name.clone(),
amt: self.amt,
bottom: self.bottom,
features_idx,
})
}
Expand All @@ -57,6 +59,7 @@ impl Group {
name: sg.name.clone(),
features,
amt: sg.amt,
bottom: sg.bottom,
})
}

Expand Down Expand Up @@ -159,6 +162,7 @@ pub struct SerializedGroup {
pub name: String,
pub features_idx: Vec<usize>,
pub amt: Option<f64>,
pub bottom: Option<()>,
}

#[cfg(test)]
Expand All @@ -176,6 +180,7 @@ mod tests {
name: "Ye".into(),
features: vec![point_key],
amt: None,
bottom: None,
}
.serialize(&HashMap::from([(point_key, 42)])),
Ok(SerializedGroup {
Expand Down
8 changes: 4 additions & 4 deletions drawing/src/data/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ pub enum ExportErr {

#[derive(Clone, Copy, Debug, serde::Deserialize, serde::Serialize)]
pub enum CADOp {
Extrude(f64),
Bore(f64),
Extrude(f64, bool), // true = extrude on the bottom
Bore(f64, bool), // true = bore from the bottom
Hole,
}

Expand Down Expand Up @@ -1171,12 +1171,12 @@ impl Data {
GroupType::Boundary | GroupType::Hole => {}
GroupType::Extrude => {
for p in paths.into_iter() {
ops.push((CADOp::Extrude(g.amt.unwrap_or(3.0)), p));
ops.push((CADOp::Extrude(g.amt.unwrap_or(3.0), g.bottom.is_some()), p));
}
}
GroupType::Bore => {
for p in paths.into_iter() {
ops.push((CADOp::Bore(g.amt.unwrap_or(3.0)), p));
ops.push((CADOp::Bore(g.amt.unwrap_or(3.0), g.bottom.is_some()), p));
}
}
}
Expand Down
57 changes: 50 additions & 7 deletions drawing/src/l/three_d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,24 +128,25 @@ pub fn extrude_from_paths(
.iter()
.map(|e| e.vertex_iter())
.flatten()
.fold(0.0, |acc: f64, v| acc.max(v.get_point().z));
.fold(-99999.0, |acc: f64, v| acc.max(v.get_point().z));
let top_offset_z = base[top_idx]
.absolute_boundaries()
.iter()
.map(|e| e.vertex_iter())
.flatten()
.fold(0.0, |acc: f64, v| acc.max(v.get_point().z));
.fold(-99999.0, |acc: f64, v| acc.max(v.get_point().z));

match op {
CADOp::Hole => {
if path.area().signum() > 0.0 {
w.invert(); // negative geometry must have edges clockwise
}
let f: Face = builder::try_attach_plane(&vec![w]).unwrap();
let solid = builder::tsweep(
let tf = builder::tsweep(
&f,
(top_offset_z - bottom_offset_z) * Vector3::unit_z(),
);
let solid = builder::translated(&tf, bottom_offset_z * Vector3::unit_z());
let mut b = solid.into_boundaries().pop().unwrap();

// Extract copies of the wires representing the boundaries of the hole.
Expand All @@ -158,7 +159,8 @@ pub fn extrude_from_paths(
b.pop();
base.extend(b.into_iter().skip(1));
}
CADOp::Extrude(amt) => {
CADOp::Extrude(amt, false) => {
// on top
if path.area().signum() < 0.0 {
w.invert(); // regular geometry must have edges counter-clockwise
}
Expand All @@ -176,7 +178,31 @@ pub fn extrude_from_paths(

done_parents.insert(i as isize, (bottom_idx, base.len() - 1));
}
CADOp::Bore(amt) => {
CADOp::Extrude(amt, true) => {
// on bottom
if path.area().signum() < 0.0 {
w.invert(); // regular geometry must have edges counter-clockwise
}
let f: Face = builder::try_attach_plane(&vec![w]).unwrap();
let tf = builder::tsweep(&f, *amt * Vector3::unit_z());
let solid =
builder::translated(&tf, (bottom_offset_z - *amt) * Vector3::unit_z());
let mut b = solid.into_boundaries().pop().unwrap();

// Cut the base shape at the boundary so we can glue the extrusion
let top_wire = &b.last().unwrap().boundaries()[0];
base[bottom_idx].add_boundary(top_wire.clone());

let next_face_idx = base.len();

// Add the faces of the extrusion except the top
b.pop();
base.extend(b.into_iter());

done_parents.insert(i as isize, (next_face_idx, top_idx));
}
CADOp::Bore(amt, false) => {
// on top
if path.area().signum() > 0.0 {
w.invert(); // negative geometry must have edges clockwise
}
Expand All @@ -196,6 +222,23 @@ pub fn extrude_from_paths(
base.extend(b.into_iter());
done_parents.insert(i as isize, (bottom_idx, next_face_idx));
}
CADOp::Bore(amt, true) => {
// on bottom
if path.area().signum() > 0.0 {
w.invert(); // negative geometry must have edges clockwise
}
let f: Face = builder::try_attach_plane(&vec![w]).unwrap();
let tf = builder::tsweep(&f, *amt * Vector3::unit_z());
let solid = builder::translated(&tf, bottom_offset_z * Vector3::unit_z());
let mut b = solid.into_boundaries().pop().unwrap();

Check warning on line 233 in drawing/src/l/three_d.rs

View workflow job for this annotation

GitHub Actions / Check wasm32

variable does not need to be mutable

Check warning on line 233 in drawing/src/l/three_d.rs

View workflow job for this annotation

GitHub Actions / Check

variable does not need to be mutable

Check warning on line 233 in drawing/src/l/three_d.rs

View workflow job for this annotation

GitHub Actions / Test Suite

variable does not need to be mutable

let bottom_wire = &b.first().unwrap().boundaries()[0];
base[bottom_idx].add_boundary(bottom_wire.clone());

// Add the faces of the bore except the bottom
base.extend(b.into_iter().skip(1));
done_parents.insert(i as isize, (base.len() - 1, top_idx));
}
}
done.insert(i);
}
Expand Down Expand Up @@ -525,7 +568,7 @@ mod tests {
}
.into_path(0.1),
vec![(
CADOp::Extrude(4.5),
CADOp::Extrude(4.5, false),
kurbo::Rect {
x0: 2.0,
y0: 2.0,
Expand Down Expand Up @@ -563,7 +606,7 @@ mod tests {
}
.into_path(0.1),
vec![(
CADOp::Bore(5.0),
CADOp::Bore(5.0, false),
kurbo::Rect {
x0: 2.0,
y0: 2.0,
Expand Down

0 comments on commit fe31774

Please sign in to comment.