Skip to content

Commit

Permalink
Added a simple api hiding nalgebra's complexity
Browse files Browse the repository at this point in the history
  • Loading branch information
gammelalf committed Sep 7, 2022
1 parent 3cf843d commit c434d30
Show file tree
Hide file tree
Showing 4 changed files with 366 additions and 7 deletions.
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ pub mod bounding_box;
pub mod graham_scan;
pub mod nbezier;
pub mod npolynomial;
pub mod simple;

pub use crate::nbezier::BezierCurve;
pub use crate::simple::SimpleCurve;

#[cfg(test)]
mod tests {
Expand Down
13 changes: 12 additions & 1 deletion src/nbezier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ pub struct BezierCurve<T, R, C, S>(pub Matrix<T, R, C, S>);
/// Wrapper around [`nalgebra::OMatrix`] interpreting it as a bezier curve.
pub type OBezierCurve<T, R, C> = BezierCurve<T, R, C, Owned<T, R, C>>;

impl<T: RealField, R: Dim, C: Dim, S: Storage<T, R, C>> BezierCurve<T, R, C, S> {
/// Get the curves degree
///
/// For example a cubic curve has degree 3 and 4 control points
pub fn degree(&self) -> usize {
self.0.ncols() - 1
}
}

impl<T: RealField, R: Dim, C: Dim, S: Storage<T, R, C>> BezierCurve<T, R, C, S>
where
// Column arithemtic required in each step
Expand Down Expand Up @@ -311,7 +320,7 @@ impl<T: RealField, C: Dim, S: Storage<T, U2, C>> BezierCurve<T, U2, C, S> {
///
/// This box will also contain the whole curve, but can highly overestimate it.
/// It can be used as a fast way to estimate intersections.
/// For a more precise checks consider: [`convex_hull`]
/// For a more precise boundary consider: [`convex_hull`]
///
/// [`convex_hull`]: BezierCurve::convex_hull
pub fn bounding_box(&self) -> BoundingBox<T> {
Expand Down Expand Up @@ -437,6 +446,8 @@ impl<T: RealField, C: Dim, S: Storage<T, U2, C>> BezierCurve<T, U2, C, S> {
}

/// WIP
///
/// TODO: make other generic instead of Self
pub fn get_intersections(&self, other: &Self) -> Vec<Vector2<T>>
where
// Clone curves
Expand Down
19 changes: 13 additions & 6 deletions src/npolynomial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@ use nalgebra::{
};
use std::fmt::{self, Write};

/// "Normal" polynomial of dynamic degree using numbers as coefficents.
pub type Polynomial1xX<T> = Polynomial<T, U1, Dynamic, Owned<T, U1, Dynamic>>;
/// Polynomial of dynamic defree using 2D Vectors as coefficents.
pub type Polynomial2xX<T> = Polynomial<T, U2, Dynamic, Owned<T, U2, Dynamic>>;
/// Wrapper around [`nalgebra::OMatrix`] interpreting it as a polynomial.
pub type OPolynomial<T, R, C> = Polynomial<T, R, C, Owned<T, R, C>>;

/// Wrapper around [`nalgebra::Matrix`] interpreting it as a polynomial:
/// $p: \R \to \R^r $ where $r$ is the number of rows i.e. the generic `R` parameter
Expand All @@ -20,6 +18,15 @@ pub type Polynomial2xX<T> = Polynomial<T, U2, Dynamic, Owned<T, U2, Dynamic>>;
/// and columns are the different powers' coefficents.
pub struct Polynomial<T, R, C, S>(pub Matrix<T, R, C, S>);

impl<T: RealField, R: Dim, C: Dim, S: Storage<T, R, C>> Polynomial<T, R, C, S> {
/// Get the polynomial degree i.e its highest power
///
/// For example a quadratic polynomial has degree 2 and 3 coefficients
pub fn degree(&self) -> usize {
self.0.ncols() - 1
}
}

/* Eval, Derive, Integrate */
impl<T: Scalar, R: Dim, C: Dim, S: Storage<T, R, C>> Polynomial<T, R, C, S> {
/// Evaluate `self` at position `x` and store the result into `out`.
Expand Down Expand Up @@ -159,7 +166,7 @@ pub type DimPolyProd<C1, C2> = DimDiff<DimSum<C1, C2>, U1>;

/* Roots */
impl<T: Scalar, S: Storage<T, U1, U2>> Polynomial<T, U1, U2, S> {
/// Calculate a quadratic's roots.
/// Calculate a linar's root.
pub fn roots(&self) -> Vec<T>
where
T: RealField,
Expand All @@ -175,7 +182,7 @@ impl<T: Scalar, S: Storage<T, U1, U2>> Polynomial<T, U1, U2, S> {
}
}
impl<T: Scalar, S: Storage<T, U1, U3>> Polynomial<T, U1, U3, S> {
/// Calculate a cubic's roots.
/// Calculate a quadratic's roots.
pub fn roots(&self) -> Vec<T>
where
T: RealField,
Expand Down
Loading

0 comments on commit c434d30

Please sign in to comment.