Skip to content

Commit

Permalink
Extruding a second solid is working
Browse files Browse the repository at this point in the history
  • Loading branch information
MattFerraro committed May 15, 2024
1 parent d5bdf07 commit 2e491ec
Show file tree
Hide file tree
Showing 3 changed files with 174 additions and 52 deletions.
110 changes: 103 additions & 7 deletions packages/cadmium/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@ use cadmium::oplog::EvolutionLog;
use cadmium::oplog::Operation;
use cadmium::project::Plane;
use cadmium::{oplog, sketch, Realization};
use truck_meshalgo::analyzers::CalcVolume;
use truck_meshalgo::filters::OptimizingFilter;
use truck_meshalgo::tessellation::{MeshableShape, MeshedShape};
use truck_modeling::builder::{translated, tsweep, vertex};
use truck_modeling::{Point3, Surface, Vector3};
use truck_polymesh::{obj, InnerSpace, Invertible, ParametricSurface, Tolerance};
use truck_polymesh::{
obj, InnerSpace, Invertible, ParametricSurface, ParametricSurface3D, Tolerance,
};
use truck_shapeops::{and, or, ShapeOpsCurve, ShapeOpsSurface};
use truck_topology::{Shell, Solid};

Expand Down Expand Up @@ -107,14 +110,107 @@ fn stacked_cubes() {

el.realize_extrusion(&extrusion_id);

// Create a plane on the face whose normal points up
let mut upward_face = None;
for (face_sha, face) in el.faces.iter() {
let surface = face.oriented_surface();
let normal = surface.normal(0.0, 0.0);
if normal.near(&Vector3::new(0.0, 0.0, 1.0)) {
upward_face = Some(face.clone());
}
}
let second_plane_id = el.append(Operation::CreatePlane {
nonce: "the second plane".to_string(),
workbench_id: workbench_id.clone(),
});
el.append(Operation::SetPlaneName {
plane_id: second_plane_id.clone(),
name: "Second Plane".to_string(),
});
match upward_face {
Some(face) => {
let set_plane = el.append(Operation::SetPlane {
plane_id: second_plane_id.clone(),
plane: Plane::from_truck_face(face),
});
}
None => {
println!("No upward face found!");
unreachable!();
}
}
let second_plane_real = el.realize_plane(&second_plane_id);

// Create a second sketch on top of the second plane
let second_sketch_id = el.append(Operation::CreateSketch {
nonce: "second sketch".to_string(),
workbench_id: workbench_id.clone(),
});
el.append(Operation::SetSketchName {
sketch_id: second_sketch_id.clone(),
name: "Second Sketch".to_string(),
});
el.append(Operation::SetSketchPlane {
sketch_id: second_sketch_id.clone(),
plane_id: second_plane_real.clone(),
});

// make a square
el.append(Operation::AddSketchLine {
sketch_id: second_sketch_id.clone(),
start: (20.0, 20.0),
end: (20.0, 80.0),
});
el.append(Operation::AddSketchLine {
sketch_id: second_sketch_id.clone(),
start: (20.0, 80.0),
end: (80.0, 80.0),
});
el.append(Operation::AddSketchLine {
sketch_id: second_sketch_id.clone(),
start: (80.0, 80.0),
end: (80.0, 20.0),
});
el.append(Operation::AddSketchLine {
sketch_id: second_sketch_id.clone(),
start: (80.0, 20.0),
end: (20.0, 20.0),
});

let second_realized_sketch = el.realize_sketch(&second_sketch_id);

// extrude the second square
let second_extrusion_id = el.append(Operation::CreateExtrusion {
workbench_id: workbench_id.clone(),
nonce: "second extrusion".to_string(),
});
el.append(Operation::SetExtrusionName {
extrusion_id: second_extrusion_id.clone(),
name: "Extrude2".to_string(),
});
el.append(Operation::SetExtrusionDepth {
extrusion_id: second_extrusion_id.clone(),
depth: 60.0,
});
el.append(Operation::SetExtrusionSketch {
extrusion_id: second_extrusion_id.clone(),
sketch_id: second_realized_sketch.clone(),
});
el.append(Operation::SetExtrusionFaces {
extrusion_id: second_extrusion_id.clone(),
faces: vec![0],
});
el.realize_extrusion(&second_extrusion_id);

// print each solid
// for (solid_id, solid) in el.solids.iter() {
// println!("Solid: {:?}", solid.truck_solid);
// solid.save_as_obj("first_solid.obj", 0.01);
// }
for (solid_id, solid) in el.solids.iter() {
let mut mesh = solid.truck_solid.triangulation(0.1).to_polygon();
mesh.put_together_same_attrs(0.1);
let v = mesh.volume();
println!("\nvolume: {}", v);
}

el.git_log();
// el.to_project();
// el.git_log();
}

fn simple_cube() {
Expand Down
104 changes: 59 additions & 45 deletions packages/cadmium/src/oplog/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,14 @@ pub struct EvolutionLog {
pub sketches: HashMap<Sha, (usize, String)>,
pub real_sketches: HashMap<Sha, RealSketch>,
pub extrusions: HashMap<Sha, (usize, usize)>,
pub faces: HashMap<Sha, Face>,
pub faces: HashMap<
Sha,
truck_topology::Face<
truck_meshalgo::prelude::cgmath::Point3<f64>,
truck_modeling::Curve,
truck_modeling::Surface,
>,
>,
pub solids: HashMap<Sha, Solid>,
}

Expand Down Expand Up @@ -294,42 +301,42 @@ impl EvolutionLog {
self.cursor.clone()
}

pub fn find_faces(&mut self, workbench_id: &Sha, sketch_id: &Sha) -> Sha {
// TODO: delete this whole function. It is unnecessary
let (workbench_idx, sketch_id) = self.sketches.get(sketch_id).unwrap();
// let workbench_sha = self.workbenches_inverse.get(workbench_idx).unwrap();
let wb = self.project.workbenches.get(*workbench_idx).unwrap();

let step_idx = wb.step_id_from_unique_id(sketch_id).unwrap();
let step = wb.history.get(step_idx as usize).unwrap();

let mut new_face_ops = Vec::new();
if let StepData::Sketch { sketch, .. } = &step.data {
let (faces, _unused_segments) = sketch.find_faces();
for face in faces {
let face_op = Operation::CreateFace {
workbench_id: workbench_id.clone(),
sketch_id: sketch_id.clone(),
face: face.clone(),
};
println!("Face Op: {:?}", face_op);
new_face_ops.push(face_op);
}
} else {
unreachable!()
};

for face_op in new_face_ops {
self.append(face_op.clone());
if let Operation::CreateFace { face, .. } = face_op {
self.faces.insert(self.cursor.clone(), face.clone());
} else {
unreachable!()
}
}

self.cursor.clone()
}
// fn find_faces_do_not_use(&mut self, workbench_id: &Sha, sketch_id: &Sha) -> Sha {
// // TODO: delete this whole function. It is unnecessary
// let (workbench_idx, sketch_id) = self.sketches.get(sketch_id).unwrap();
// // let workbench_sha = self.workbenches_inverse.get(workbench_idx).unwrap();
// let wb = self.project.workbenches.get(*workbench_idx).unwrap();

// let step_idx = wb.step_id_from_unique_id(sketch_id).unwrap();
// let step = wb.history.get(step_idx as usize).unwrap();

// let mut new_face_ops = Vec::new();
// if let StepData::Sketch { sketch, .. } = &step.data {
// let (faces, _unused_segments) = sketch.find_faces();
// for face in faces {
// let face_op = Operation::CreateFace {
// workbench_id: workbench_id.clone(),
// sketch_id: sketch_id.clone(),
// face: face.clone(),
// };
// println!("Face Op: {:?}", face_op);
// new_face_ops.push(face_op);
// }
// } else {
// unreachable!()
// };

// for face_op in new_face_ops {
// self.append(face_op.clone());
// if let Operation::CreateFace { face, .. } = face_op {
// self.faces.insert(self.cursor.clone(), face.clone());
// } else {
// unreachable!()
// }
// }

// self.cursor.clone()
// }

pub fn realize_plane(&mut self, plane_id: &Sha) -> Sha {
let mut new_operations = vec![];
Expand Down Expand Up @@ -448,14 +455,21 @@ impl EvolutionLog {
self.append(new_op);
self.solids.insert(self.cursor.clone(), solid.clone());
for boundary in solid.truck_solid.boundaries() {
boundary.face_iter().for_each(|face| {
let o = Operation::CreateTruckFace {
workbench_id: workbench_sha.clone(),
solid_id: self.cursor.clone(),
face: face.clone(),
};
self.append(o);
});
boundary.face_iter().for_each(
|face: &truck_topology::Face<
truck_meshalgo::prelude::cgmath::Point3<f64>,
truck_modeling::Curve,
truck_modeling::Surface,
>| {
let o = Operation::CreateTruckFace {
workbench_id: workbench_sha.clone(),
solid_id: self.cursor.clone(),
face: face.clone(),
};
self.append(o);
self.faces.insert(self.cursor.clone(), face.clone());
},
);
}
}
} else {
Expand Down
12 changes: 12 additions & 0 deletions packages/cadmium/src/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1290,6 +1290,18 @@ impl Plane {
}
}

pub fn from_truck_face(tf: truck_modeling::Face) -> Self {
let os = tf.oriented_surface();
match os {
truck_modeling::geometry::Surface::Plane(p) => {
return Plane::from_truck(p);
}
_ => {
panic!("I only know how to put sketches on planes");
}
}
}

pub fn project(&self, point: &Point3) -> Point2 {
let minus_origin = point.minus(&self.origin);
let x = minus_origin.dot(&self.primary);
Expand Down

0 comments on commit 2e491ec

Please sign in to comment.