Skip to content

Commit

Permalink
Fixes #18
Browse files Browse the repository at this point in the history
  • Loading branch information
TobiasJacob committed May 29, 2024
1 parent adfdeac commit 627b304
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 8 deletions.
63 changes: 61 additions & 2 deletions src/constraints/angle_between_points.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ impl ConstraintLike for AngleBetweenPoints {

let grad_dot_product_from_d1 = d2.transpose();
let grad_dot_product_from_d2 = d1.transpose();
let grad_norm_from_d1 = d1.transpose();
let grad_norm_to_d2 = d2.transpose();
let grad_norm_from_d1 = d1.transpose() / norm1;
let grad_norm_to_d2 = d2.transpose() / norm2;

let grad_cos_theta_from_dot_product = 1.0 / (norm1 * norm2);
let grad_cos_theta_from_norm1 = -dot_product / (norm1 * norm1 * norm2);
Expand Down Expand Up @@ -235,4 +235,63 @@ mod tests {

assert!(constr1.borrow().loss_value() < 0.001,);
}

#[test]
fn test_specific_case() {
let sketch = Rc::new(RefCell::new(Sketch::new()));

let point_a = Rc::new(RefCell::new(Point2::new(
0.7805516932908316,
-0.00782612334736288,
)));
let point_b = Rc::new(RefCell::new(Point2::new(
1.22103191002294,
0.004601914768224987,
)));
let point_middle = Rc::new(RefCell::new(Point2::new(
0.013589691730458502,
-0.10039941813640837,
)));

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_middle.clone()))
.unwrap();

let constr1 = Rc::new(RefCell::new(AngleBetweenPoints::new(
point_a.clone(),
point_b.clone(),
point_middle.clone(),
std::f64::consts::PI / 2.0,
)));
sketch
.borrow_mut()
.add_constraint(ConstraintCell::AngleBetweenPoints(constr1.clone()))
.unwrap();

sketch
.borrow_mut()
.check_gradients(1e-6, constr1.clone(), 1e-4);
let solver = GradientBasedSolver::new(sketch.clone());
solver.solve();

println!("point_a: {:?}", point_a.as_ref().borrow());
println!("point_b: {:?}", point_b.as_ref().borrow());
println!("point_middle: {:?}", point_middle.as_ref().borrow());

println!(
"current angle: {}",
constr1.borrow().current_angle() * 180.0 / std::f64::consts::PI
);

assert!(constr1.borrow().loss_value() < 0.001,);
}
}
6 changes: 3 additions & 3 deletions src/examples/test_rectangle_rotated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,19 +192,19 @@ mod tests {
);
assert!(
(rectangle.point_b.as_ref().borrow().data()
- Vector2::new(f64::sqrt(2.0), f64::sqrt(2.0)))
- Vector2::new(f64::sqrt(2.0), -f64::sqrt(2.0)))
.norm()
< 1e-5
);
assert!(
(rectangle.point_c.as_ref().borrow().data()
- Vector2::new(5.0 / f64::sqrt(2.0), -1.0 / f64::sqrt(2.0)))
- Vector2::new(5.0 / f64::sqrt(2.0), 1.0 / f64::sqrt(2.0)))
.norm()
< 1e-5
);
assert!(
(rectangle.point_d.as_ref().borrow().data()
- Vector2::new(3.0 / f64::sqrt(2.0), -3.0 / f64::sqrt(2.0)))
- Vector2::new(3.0 / f64::sqrt(2.0), 3.0 / f64::sqrt(2.0)))
.norm()
< 1e-5
);
Expand Down
6 changes: 3 additions & 3 deletions src/solvers/bfgs_solver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,19 +129,19 @@ mod tests {
);
assert!(
(rectangle.point_b.as_ref().borrow().data()
- Vector2::new(f64::sqrt(2.0), f64::sqrt(2.0)))
- Vector2::new(f64::sqrt(2.0), -f64::sqrt(2.0)))
.norm()
< 1e-5
);
assert!(
(rectangle.point_c.as_ref().borrow().data()
- Vector2::new(5.0 / f64::sqrt(2.0), -1.0 / f64::sqrt(2.0)))
- Vector2::new(5.0 / f64::sqrt(2.0), 1.0 / f64::sqrt(2.0)))
.norm()
< 1e-5
);
assert!(
(rectangle.point_d.as_ref().borrow().data()
- Vector2::new(3.0 / f64::sqrt(2.0), -3.0 / f64::sqrt(2.0)))
- Vector2::new(3.0 / f64::sqrt(2.0), 3.0 / f64::sqrt(2.0)))
.norm()
< 1e-5
);
Expand Down

0 comments on commit 627b304

Please sign in to comment.