Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More developer friendly interface #34

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

avaziman
Copy link

@avaziman avaziman commented Aug 20, 2024

This PR adds functions to sketch add_point, add_line and constrain_* functions to sketch, to make the developer experience nicer and less verbose, by hiding the Rc<Refcell<>> construction.
Here's how it compares to the previous API:
OLD:

impl RotatedRectangleDemo {
    pub fn new() -> Self {
        let sketch = Rc::new(RefCell::new(Sketch::new()));

        // This time we have to choose some random start points to break the symmetry
        let point_a = Rc::new(RefCell::new(Point2::new(0.0, 0.1)));
        let point_b = Rc::new(RefCell::new(Point2::new(0.3, 0.0)));
        let point_c = Rc::new(RefCell::new(Point2::new(0.3, 0.3)));
        let point_d = Rc::new(RefCell::new(Point2::new(0.1, 0.3)));

        let point_reference = Rc::new(RefCell::new(Point2::new(1.0, 0.0)));

        sketch
            .borrow_mut()
            .add_primitive(PrimitiveCell::Point2(point_a.clone()))
            .unwrap();
        sketch
            .borrow_mut()
            .add_primitive(PrimitiveCell::Point2(point_b.clone()))
            .unwrap();
        sketch
            .borrow_mut()
            .add_primitive(PrimitiveCell::Point2(point_c.clone()))
            .unwrap();
        sketch
            .borrow_mut()
            .add_primitive(PrimitiveCell::Point2(point_d.clone()))
            .unwrap();
        sketch
            .borrow_mut()
            .add_primitive(PrimitiveCell::Point2(point_reference.clone()))
            .unwrap();

        let line_a = Rc::new(RefCell::new(Line::new(point_a.clone(), point_b.clone())));
        let line_b = Rc::new(RefCell::new(Line::new(point_b.clone(), point_c.clone())));
        let line_c = Rc::new(RefCell::new(Line::new(point_c.clone(), point_d.clone())));
        let line_d = Rc::new(RefCell::new(Line::new(point_d.clone(), point_a.clone())));

        sketch
            .borrow_mut()
            .add_primitive(PrimitiveCell::Line(line_a.clone()))
            .unwrap();
        sketch
            .borrow_mut()
            .add_primitive(PrimitiveCell::Line(line_b.clone()))
            .unwrap();
        sketch
            .borrow_mut()
            .add_primitive(PrimitiveCell::Line(line_c.clone()))
            .unwrap();
        sketch
            .borrow_mut()
            .add_primitive(PrimitiveCell::Line(line_d.clone()))
            .unwrap();

        // Fix point a to origin
        sketch
            .borrow_mut()
            .add_constraint(ConstraintCell::FixPoint(Rc::new(RefCell::new(
                FixPoint::new(point_a.clone(), Vector2::new(0.0, 0.0)),
            ))))
            .unwrap();

        // Constrain line_a and line_b to be perpendicular
        sketch
            .borrow_mut()
            .add_constraint(ConstraintCell::PerpendicularLines(Rc::new(RefCell::new(
                PerpendicularLines::new(line_a.clone(), line_b.clone()),
            ))))
            .unwrap();

        // Constrain line_b and line_c to be perpendicular
        sketch
            .borrow_mut()
            .add_constraint(ConstraintCell::PerpendicularLines(Rc::new(RefCell::new(
                PerpendicularLines::new(line_b.clone(), line_c.clone()),
            ))))
            .unwrap();

        // Constrain line_c and line_d to be perpendicular
        sketch
            .borrow_mut()
            .add_constraint(ConstraintCell::PerpendicularLines(Rc::new(RefCell::new(
                PerpendicularLines::new(line_c.clone(), line_d.clone()),
            ))))
            .unwrap();

        // // Constrain line_d and line_a to be perpendicular
        // sketch.borrow_mut().add_constraint(Rc::new(RefCell::new(PerpendicularLines::new(
        //     line_d.clone(),
        //     line_a.clone(),
        // ))));

        // Constrain the length of line_a to 2
        sketch
            .borrow_mut()
            .add_constraint(ConstraintCell::EuclideanDistance(Rc::new(RefCell::new(
                EuclidianDistanceBetweenPoints::new(point_a.clone(), point_b.clone(), 2.0),
            ))))
            .unwrap();

        // Constrain the length of line_b to 3
        sketch
            .borrow_mut()
            .add_constraint(ConstraintCell::EuclideanDistance(Rc::new(RefCell::new(
                EuclidianDistanceBetweenPoints::new(point_a.clone(), point_d.clone(), 3.0),
            ))))
            .unwrap();

        // Fix reference point
        sketch
            .borrow_mut()
            .add_constraint(ConstraintCell::FixPoint(Rc::new(RefCell::new(
                FixPoint::new(point_reference.clone(), Vector2::new(1.0, 0.0)),
            ))))
            .unwrap();

        // Constrain rotation of line_a to 45 degrees
        sketch
            .borrow_mut()
            .add_constraint(ConstraintCell::AngleBetweenPoints(Rc::new(RefCell::new(
                AngleBetweenPoints::new(
                    point_reference.clone(),
                    point_b.clone(),
                    point_a.clone(),
                    f64::to_radians(45.0),
                ),
            ))))
            .unwrap();

        Self {
            sketch,
            point_a,
            point_b,
            point_c,
            point_d,
            point_reference,
        }
    }

with PR:

pub fn new() -> Result<RotatedRectangleDemo, ISOTopeError> {
        let mut sketch = Sketch::new();

        // This time we have to choose some random start points to break the symmetry
        let point_a = sketch.add_point2(0.0, 0.1)?;
        let point_b = sketch.add_point2(0.3, 0.0)?;
        let point_c = sketch.add_point2(0.3, 0.3)?;
        let point_d = sketch.add_point2(0.1, 0.3)?;

        let point_reference = sketch.add_point2(1.0, 0.0)?;

        let line_a = sketch.add_line(point_a.clone(), point_b.clone())?;
        let line_b = sketch.add_line(point_b.clone(), point_c.clone())?;
        let line_c = sketch.add_line(point_c.clone(), point_d.clone())?;
        let line_d = sketch.add_line(point_d.clone(), point_a.clone())?;

        // Fix point a to origin
        sketch.constrain_fix_point(point_a.clone(), Vector2::new(0.0, 0.0))?;

        // Constrain line_a and line_b to be perpendicular
        sketch.constrain_perpendicular_lines(line_a, line_b.clone())?;

        // Constrain line_b and line_c to be perpendicular
        sketch.constrain_perpendicular_lines(line_b, line_c.clone())?;
        sketch.constrain_perpendicular_lines(line_c, line_d)?;

        // // Constrain line_d and line_a to be perpendicular
        // sketch.borrow_mut().add_constraint(Rc::new(RefCell::new(PerpendicularLines::new(
        //     line_d.clone(),
        //     line_a.clone(),
        // ))));

        // Constrain the length of line_a to 2
        sketch.constrain_distance_euclidean(point_a.clone(), point_b.clone(), 2.0)?;

        // Constrain the length of line_b to 3
        sketch.constrain_distance_euclidean(point_a.clone(), point_d.clone(), 3.0)?;

        // Fix reference point
        sketch.constrain_fix_point(point_reference.clone(), Vector2::new(1.0, 0.0))?;

        // Constrain rotation of line_a to 45 degrees
        sketch.constrain_angle_between_points(
            point_reference.clone(),
            point_b.clone(),
            point_a.clone(),
            f64::to_radians(45.0),
        )?;

        Ok(Self {
            sketch,
            point_a,
            point_b,
            point_c,
            point_d,
            point_reference,
        })
    }```
    
    

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant