From bd9424635e997ce70504b564a3561506ee6d51df Mon Sep 17 00:00:00 2001 From: eug-vs Date: Sat, 14 Dec 2024 18:35:30 +0100 Subject: refactor: add Line struct --- src/constraint/slider.rs | 33 +++++++++++++-------------------- 1 file changed, 13 insertions(+), 20 deletions(-) (limited to 'src/constraint/slider.rs') diff --git a/src/constraint/slider.rs b/src/constraint/slider.rs index 526b6f8..ceea4ea 100644 --- a/src/constraint/slider.rs +++ b/src/constraint/slider.rs @@ -2,18 +2,18 @@ use std::usize; use nalgebra::{DVector, RowDVector}; -use crate::{particle_system::{Scalar, N}, ParticleSystem, Point, Vector}; +use crate::{ + algebra::line::Line, + particle_system::{Scalar, N}, + ParticleSystem, Point, Vector, +}; use super::Constraint; - pub struct SliderConstraint { pub particle_id: usize, - pub point: Point, - - /// Has to be normalized - pub dir: Vector, + pub line: Line, jacobian: RowDVector, } @@ -21,15 +21,11 @@ pub struct SliderConstraint { impl ParticleSystem { pub fn add_slider_constraint(&mut self, particle_id: usize, direction: Vector) { let point = self.particles[particle_id].position; - self.constraints.push(Box::new( - SliderConstraint { - particle_id, - point, - dir: direction.normalize(), - jacobian: RowDVector::zeros(self.particles.len() * N), - } - )); - + self.constraints.push(Box::new(SliderConstraint { + particle_id, + line: Line::new(point, direction), + jacobian: RowDVector::zeros(self.particles.len() * N), + })); } } @@ -40,11 +36,8 @@ impl Constraint for SliderConstraint { fn c(&self, q: &DVector) -> Scalar { let position = q.fixed_rows::(self.particle_id * N); - let d = position - self.point.coords; - let lambda = d.dot(&self.dir); - let distance_to_line = d - lambda * self.dir; - - distance_to_line.norm() + let point = Point::from_coordinates(position.into()); + self.line.distance_to_point(point) } fn set_jacobian(&mut self, jacobian: RowDVector) { -- cgit v1.2.3