diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/algebra/line.rs | 24 | ||||
-rw-r--r-- | src/algebra/mod.rs | 2 | ||||
-rw-r--r-- | src/algebra/plane.rs | 6 | ||||
-rw-r--r-- | src/constraint/slider.rs | 33 | ||||
-rw-r--r-- | src/main.rs | 1 |
5 files changed, 46 insertions, 20 deletions
diff --git a/src/algebra/line.rs b/src/algebra/line.rs new file mode 100644 index 0000000..45733b9 --- /dev/null +++ b/src/algebra/line.rs @@ -0,0 +1,24 @@ +use crate::{particle_system::Scalar, Point, Vector}; + +pub struct Line { + pub point: Point, + + /// Has to be normalized + pub vector: Vector, +} + +impl Line { + pub fn new(point: Point, vector: Vector) -> Self { + Self { + point, + vector: vector.normalize(), + } + } + + pub fn distance_to_point(&self, point: Point) -> Scalar { + let diff = point - self.point; + let lambda = diff.dot(&self.vector); + + (diff - lambda * self.vector).norm() + } +} diff --git a/src/algebra/mod.rs b/src/algebra/mod.rs new file mode 100644 index 0000000..884ccfc --- /dev/null +++ b/src/algebra/mod.rs @@ -0,0 +1,2 @@ +pub mod plane; +pub mod line; diff --git a/src/algebra/plane.rs b/src/algebra/plane.rs new file mode 100644 index 0000000..e82c843 --- /dev/null +++ b/src/algebra/plane.rs @@ -0,0 +1,6 @@ +use crate::{Point, Vector}; + +pub struct Plane { + point: Point, + vectors: [Vector; 2], +} 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<Scalar>, } @@ -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>) -> Scalar { let position = q.fixed_rows::<N>(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<Scalar>) { diff --git a/src/main.rs b/src/main.rs index 58e649f..0b5704b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,6 +10,7 @@ mod particle_system; mod ppm; mod solver; mod force; +mod algebra; fn main() { let ppm = PPM { |