Skip to content

Commit

Permalink
Move isketch to its own mod
Browse files Browse the repository at this point in the history
Signed-off-by: Dimitris Zervas <[email protected]>
  • Loading branch information
dzervas committed Jun 11, 2024
1 parent 59fa1ad commit ab3e2e3
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 112 deletions.
Empty file.
114 changes: 114 additions & 0 deletions packages/cadmium/src/isketch/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
use std::cell::RefCell;
use std::collections::BTreeMap;
use std::rc::Rc;

use isotope::decompose::face::Face;
use isotope::primitives::point2::Point2 as ISOPoint2;
use isotope::primitives::PrimitiveCell;
use isotope::sketch::Sketch;
use serde::{Deserialize, Serialize};
use tsify_next::Tsify;


use crate::IDType;
use crate::archetypes::{Plane, PlaneDescription};
use crate::error::CADmiumError;
use crate::feature::point::Point3;
use crate::message::Identifiable;
use crate::workbench::Workbench;

pub mod compound;
pub mod primitive;

#[derive(Tsify, Debug, Clone, Serialize, Deserialize)]
#[tsify(into_wasm_abi, from_wasm_abi)]
pub struct ISketch {
// TODO: Make it private with a setter
pub plane: Rc<RefCell<Plane>>,

sketch: Rc<RefCell<Sketch>>,
points_3d: BTreeMap<u64, Point3>,
}

impl ISketch {
// TODO: Maybe pass the plane as refcell?
pub fn new(plane: Rc<RefCell<Plane>>) -> Self {
// The key difference between Sketch and RealSketch is that Sketch lives
// in 2D and RealSketch lives in 3D. So we need to convert the points

let mut real_sketch = Self {
plane: plane.clone(),
points_3d: BTreeMap::new(),
// primitives: sketch.borrow().primitives().iter().map(|(id, prim)| (*id, prim.borrow().to_primitive())).collect(),
// constraints: sketch.borrow().constraints().iter().map(|c| c.borrow().get_type()).collect(),
sketch: Rc::new(RefCell::new(Sketch::new())),
};

for (id, point) in real_sketch.sketch.borrow().get_all_points().iter() {
real_sketch.points_3d.insert(*id, Point3::from_plane_point(&plane.borrow().clone(), point));
}

real_sketch
}

pub fn try_from_plane_description(wb: &Workbench, plane_description: &PlaneDescription) -> anyhow::Result<Self> {
let plane = match plane_description {
PlaneDescription::PlaneId(plane_id) =>
wb.planes.get(plane_id).ok_or(anyhow::anyhow!("Failed to find plane with id {}", plane_id))?,
PlaneDescription::SolidFace { solid_id: _, normal: _ } => todo!("Implement SolidFace"),
}.clone();
Ok(Self::new(plane))
}

/// Helper function to go from an isotope point2D to a point_3D, as calculated during new
pub fn get_point_3d(&self, point: Rc<RefCell<ISOPoint2>>) -> Result<(u64, Point3), CADmiumError> {
let cell = PrimitiveCell::Point2(point.clone());
let point_id = self.sketch.borrow().get_primitive_id(&cell).unwrap();

if let Some(result) = self.points_3d.get(&point_id) {
Ok((point_id, result.clone()))
} else {
// TODO: While I'd like to calculate and add the point_3d here, we'll pollute everything with mut
// let point_3d = Point3::from_plane_point(&self.plane.borrow(), &point.borrow());

// Ok((point_id,
// self.points_3d
// .insert(point_id, point_3d)
// .ok_or(CADmiumError::Point3DCalculationFailed)?))
Err(CADmiumError::Point3DCalculationFailed)
}
}

pub fn sketch(&self) -> Rc<RefCell<Sketch>> {
self.sketch.clone()
}

pub fn faces(&self) -> Vec<Face> {
// TODO: How do we keep track of faces vs IDs?
self.sketch.borrow().get_merged_faces()
}

pub fn find_point_ref(&self, x: f64, y: f64) -> Option<Rc<RefCell<ISOPoint2>>> {
self.sketch.borrow().primitives().iter().find_map(|(_, prim)| {
if let PrimitiveCell::Point2(point_ref) = prim {
let point = point_ref.borrow();
if (point.x() - x).abs() < 0.0001 && (point.y() - y).abs() < 0.0001 {
Some(point_ref.clone())
} else {
None
}
} else {
None
}
})
}
}

impl Identifiable for Rc<RefCell<ISketch>> {
type Parent = Rc<RefCell<Workbench>>;
const ID_NAME: &'static str = "sketch_id";

fn from_parent_id(parent: &Self::Parent, id: IDType) -> anyhow::Result<Self> {
Ok(parent.borrow().sketches.get(&id).ok_or(anyhow::anyhow!(""))?.clone())
}
}
Original file line number Diff line number Diff line change
@@ -1,115 +1,17 @@
use std::cell::RefCell;
use std::collections::BTreeMap;
use std::rc::Rc;

use isotope::decompose::face::Face;
use isotope::primitives::line::Line;
use isotope::primitives::point2::Point2 as ISOPoint2;
use isotope::primitives::PrimitiveCell;
use isotope::sketch::Sketch;
use serde::{Deserialize, Serialize};
use tsify_next::Tsify;

use crate::archetypes::{Plane, PlaneDescription};
use crate::error::CADmiumError;
use crate::feature::point::Point3;
use crate::message::MessageHandler;
use crate::IDType;
use crate::feature::point::Point3;

#[derive(Tsify, Debug, Clone, Serialize, Deserialize)]
#[tsify(into_wasm_abi, from_wasm_abi)]
pub struct ISketch {
// TODO: Make it private with a setter
pub plane: Rc<RefCell<Plane>>,

sketch: Rc<RefCell<Sketch>>,
points_3d: BTreeMap<u64, Point3>,
}

impl ISketch {
// TODO: Maybe pass the plane as refcell?
pub fn new(plane: Rc<RefCell<Plane>>) -> Self {
// The key difference between Sketch and RealSketch is that Sketch lives
// in 2D and RealSketch lives in 3D. So we need to convert the points

let mut real_sketch = Self {
plane: plane.clone(),
points_3d: BTreeMap::new(),
// primitives: sketch.borrow().primitives().iter().map(|(id, prim)| (*id, prim.borrow().to_primitive())).collect(),
// constraints: sketch.borrow().constraints().iter().map(|c| c.borrow().get_type()).collect(),
sketch: Rc::new(RefCell::new(Sketch::new())),
};

for (id, point) in real_sketch.sketch.borrow().get_all_points().iter() {
real_sketch.points_3d.insert(*id, Point3::from_plane_point(&plane.borrow().clone(), point));
}

real_sketch
}

pub fn try_from_plane_description(wb: &Workbench, plane_description: &PlaneDescription) -> anyhow::Result<Self> {
let plane = match plane_description {
PlaneDescription::PlaneId(plane_id) =>
wb.planes.get(plane_id).ok_or(anyhow::anyhow!("Failed to find plane with id {}", plane_id))?,
PlaneDescription::SolidFace { solid_id: _, normal: _ } => todo!("Implement SolidFace"),
}.clone();
Ok(Self::new(plane))
}

/// Helper function to go from an isotope point2D to a point_3D, as calculated during new
pub fn get_point_3d(&self, point: Rc<RefCell<ISOPoint2>>) -> Result<(u64, Point3), CADmiumError> {
let cell = PrimitiveCell::Point2(point.clone());
let point_id = self.sketch.borrow().get_primitive_id(&cell).unwrap();

if let Some(result) = self.points_3d.get(&point_id) {
Ok((point_id, result.clone()))
} else {
// TODO: While I'd like to calculate and add the point_3d here, we'll pollute everything with mut
// let point_3d = Point3::from_plane_point(&self.plane.borrow(), &point.borrow());

// Ok((point_id,
// self.points_3d
// .insert(point_id, point_3d)
// .ok_or(CADmiumError::Point3DCalculationFailed)?))
Err(CADmiumError::Point3DCalculationFailed)
}
}

pub fn sketch(&self) -> Rc<RefCell<Sketch>> {
self.sketch.clone()
}

pub fn faces(&self) -> Vec<Face> {
// TODO: How do we keep track of faces vs IDs?
self.sketch.borrow().get_merged_faces()
}

pub fn find_point_ref(&self, x: f64, y: f64) -> Option<Rc<RefCell<ISOPoint2>>> {
self.sketch.borrow().primitives().iter().find_map(|(_, prim)| {
if let PrimitiveCell::Point2(point_ref) = prim {
let point = point_ref.borrow();
if (point.x() - x).abs() < 0.0001 && (point.y() - y).abs() < 0.0001 {
Some(point_ref.clone())
} else {
None
}
} else {
None
}
})
}
}

use crate::message::{Identifiable, MessageHandler};
use crate::workbench::Workbench;

impl Identifiable for Rc<RefCell<ISketch>> {
type Parent = Rc<RefCell<Workbench>>;
const ID_NAME: &'static str = "sketch_id";

fn from_parent_id(parent: &Self::Parent, id: IDType) -> anyhow::Result<Self> {
Ok(parent.borrow().sketches.get(&id).ok_or(anyhow::anyhow!(""))?.clone())
}
}
use super::ISketch;

#[derive(Tsify, Debug, Clone, Serialize, Deserialize)]
#[tsify(from_wasm_abi, into_wasm_abi)]
Expand Down
10 changes: 5 additions & 5 deletions packages/cadmium/src/message/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ pub enum Message {
WorkbenchSketchSetPlane(IDWrap<crate::workbench::SetSketchPlane>),
WorkbenchPointUpdate(IDWrap<IDWrap<crate::feature::point::WorkbenchPointUpdate>>),

SketchAddPoint(IDWrap<IDWrap<crate::isketch::AddPoint>>),
SketchAddArc(IDWrap<IDWrap<crate::isketch::AddArc>>),
SketchAddCircle(IDWrap<IDWrap<crate::isketch::AddCircle>>),
SketchAddLine(IDWrap<IDWrap<crate::isketch::AddLine>>),
SketchDeletePrimitive(IDWrap<IDWrap<crate::isketch::DeletePrimitive>>),
SketchAddPoint(IDWrap<IDWrap<crate::isketch::primitive::AddPoint>>),
SketchAddArc(IDWrap<IDWrap<crate::isketch::primitive::AddArc>>),
SketchAddCircle(IDWrap<IDWrap<crate::isketch::primitive::AddCircle>>),
SketchAddLine(IDWrap<IDWrap<crate::isketch::primitive::AddLine>>),
SketchDeletePrimitive(IDWrap<IDWrap<crate::isketch::primitive::DeletePrimitive>>),

FeatureExtrusionAdd(IDWrap<crate::feature::extrusion::Add>),
FeatureExtrusionUpdateFaces(IDWrap<crate::feature::extrusion::UpdateFaces>),
Expand Down
9 changes: 3 additions & 6 deletions packages/cadmium/src/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,16 +102,13 @@ pub mod tests {

use crate::archetypes::PlaneDescription;

use crate::isketch::AddLine;
use crate::isketch::AddPoint;
use crate::isketch::primitive::{AddLine, AddPoint};
use crate::message::idwrap::IDWrap;
use crate::message::MessageHandler;
use crate::feature::extrusion;
use crate::feature::extrusion::Direction;
use crate::feature::extrusion::Mode;
use crate::feature::extrusion::{Direction, Mode};
use crate::step;
use crate::workbench::AddSketch;
use crate::workbench::SetSketchPlane;
use crate::workbench::{AddSketch, SetSketchPlane};
use crate::IDType;

use super::*;
Expand Down

0 comments on commit ab3e2e3

Please sign in to comment.