pub mod basis;
pub mod construct;
pub mod curve;
pub mod knot;
pub mod surface;
pub mod tessellate;
pub use basis::{basis_funcs, ders_basis_funcs, find_span, uniform_clamped_knots};
pub use construct::{loft, loft_with_params, sweep};
pub use curve::NurbsCurve;
pub use knot::{compact, remove_knot};
pub use surface::NurbsSurface;
pub use tessellate::{tessellate_curve, tessellate_surface, TessellationOptions};
pub type V3 = [f64; 3];
pub type V4 = [f64; 4];
pub(crate) mod v3 {
use super::V3;
pub fn add(a: V3, b: V3) -> V3 {
[a[0] + b[0], a[1] + b[1], a[2] + b[2]]
}
pub fn sub(a: V3, b: V3) -> V3 {
[a[0] - b[0], a[1] - b[1], a[2] - b[2]]
}
pub fn scale(a: V3, s: f64) -> V3 {
[a[0] * s, a[1] * s, a[2] * s]
}
pub fn dot(a: V3, b: V3) -> f64 {
a[0] * b[0] + a[1] * b[1] + a[2] * b[2]
}
pub fn cross(a: V3, b: V3) -> V3 {
[
a[1] * b[2] - a[2] * b[1],
a[2] * b[0] - a[0] * b[2],
a[0] * b[1] - a[1] * b[0],
]
}
pub fn norm(a: V3) -> f64 {
dot(a, a).sqrt()
}
pub fn dist(a: V3, b: V3) -> f64 {
norm(sub(a, b))
}
pub fn normalize(a: V3) -> Option<V3> {
let l = norm(a);
if l < 1e-300 {
None
} else {
Some([a[0] / l, a[1] / l, a[2] / l])
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum NurbsError {
DegreeTooHigh { degree: usize, n_ctrl: usize },
KnotCount { expected: usize, got: usize },
KnotsNotMonotonic,
DegenerateDomain,
WeightCount { expected: usize, got: usize },
NonPositiveWeight(usize),
GridSize { expected: usize, got: usize },
Incompatible(&'static str),
}
impl std::fmt::Display for NurbsError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::DegreeTooHigh { degree, n_ctrl } => write!(
f,
"degree {degree} needs at least {} control points, got {n_ctrl}",
degree + 1
),
Self::KnotCount { expected, got } => {
write!(f, "knot vector must have {expected} entries, got {got}")
}
Self::KnotsNotMonotonic => write!(f, "knot vector is not non-decreasing"),
Self::DegenerateDomain => write!(f, "parametric domain is empty"),
Self::WeightCount { expected, got } => {
write!(f, "expected {expected} weights, got {got}")
}
Self::NonPositiveWeight(i) => write!(f, "weight {i} is not strictly positive"),
Self::GridSize { expected, got } => {
write!(f, "control grid must have {expected} points, got {got}")
}
Self::Incompatible(why) => write!(f, "curves are incompatible: {why}"),
}
}
}
impl std::error::Error for NurbsError {}
pub(crate) fn project(h: V4) -> V3 {
if h[3].abs() < 1e-300 {
[h[0], h[1], h[2]]
} else {
[h[0] / h[3], h[1] / h[3], h[2] / h[3]]
}
}