use super::NurbsSurface;
use crate::curve::KNOT_IDENTITY_TOL;
use crate::Vec4;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum KnotDirection {
U,
V,
}
#[derive(Clone, Debug, PartialEq)]
pub enum KnotRefinementRefusal {
NonFinite { direction: KnotDirection },
OutsideDomain { direction: KnotDirection, parameter: f64, start: f64, end: f64 },
MultiplicityExceeded { direction: KnotDirection, parameter: f64, existing: usize, requested: usize, limit: usize },
InvalidSurface(String),
}
impl std::fmt::Display for KnotRefinementRefusal {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::NonFinite { direction } => write!(f, "knot refinement: non-finite {direction:?} parameter"),
Self::OutsideDomain { direction, parameter, start, end } => write!(
f,
"knot refinement: {direction:?} parameter {parameter} is outside the domain [{start}, {end}]"
),
Self::MultiplicityExceeded { direction, parameter, existing, requested, limit } => write!(
f,
"knot refinement: {direction:?} knot {parameter} already occurs {existing} times; \
{requested} more would exceed the limit {limit}"
),
Self::InvalidSurface(message) => write!(f, "knot refinement: invalid surface: {message}"),
}
}
}
impl std::error::Error for KnotRefinementRefusal {}
impl NurbsSurface {
pub fn insert_knot(
&self,
direction: KnotDirection,
parameter: f64,
count: usize,
) -> Result<NurbsSurface, KnotRefinementRefusal> {
let degree = match direction {
KnotDirection::U => self.degree_u,
KnotDirection::V => self.degree_v,
};
if count > degree + 1 {
return Err(KnotRefinementRefusal::MultiplicityExceeded {
direction,
parameter,
existing: 0,
requested: count,
limit: degree + 1,
});
}
self.refine_knots(direction, &vec![parameter; count])
}
pub fn refine_knots(
&self,
direction: KnotDirection,
parameters: &[f64],
) -> Result<NurbsSurface, KnotRefinementRefusal> {
self.domain_u().map_err(KnotRefinementRefusal::InvalidSurface)?;
let (degree, knots) = match direction {
KnotDirection::U => (self.degree_u, &self.knots_u),
KnotDirection::V => (self.degree_v, &self.knots_v),
};
let start = knots[degree];
let end = knots[knots.len() - 1 - degree];
let mut snapped = Vec::with_capacity(parameters.len());
for ¶meter in parameters {
if !parameter.is_finite() {
return Err(KnotRefinementRefusal::NonFinite { direction });
}
if parameter < start - KNOT_IDENTITY_TOL || parameter > end + KNOT_IDENTITY_TOL {
return Err(KnotRefinementRefusal::OutsideDomain { direction, parameter, start, end });
}
let clamped = parameter.clamp(start, end);
let value = knots[degree..knots.len() - degree]
.iter()
.chain(snapped.iter())
.copied()
.filter(|knot| (knot - clamped).abs() <= KNOT_IDENTITY_TOL)
.min_by(|a, b| (a - clamped).abs().total_cmp(&(b - clamped).abs()))
.unwrap_or(clamped);
snapped.push(value);
}
snapped.sort_by(f64::total_cmp);
let mut index = 0;
while index < snapped.len() {
let value = snapped[index];
let requested = snapped[index..].iter().take_while(|&&other| other == value).count();
let existing = knots.iter().filter(|&&knot| knot == value).count();
let at_end = value == start || value == end;
let limit = if at_end { degree + 1 } else { degree };
if existing + requested > limit {
return Err(KnotRefinementRefusal::MultiplicityExceeded {
direction,
parameter: value,
existing,
requested,
limit,
});
}
index += requested;
}
let mut knots = knots.clone();
let mut net = self.control_points.clone();
for &value in &snapped {
let (alphas, span) = boehm_table(&knots, degree, value);
net = match direction {
KnotDirection::U => apply_u(&net, &alphas, span, degree),
KnotDirection::V => net.iter().map(|row| apply_line(row, &alphas, span, degree)).collect(),
};
knots.insert(span + 1, value);
}
let (knots_u, knots_v) = match direction {
KnotDirection::U => (knots, self.knots_v.clone()),
KnotDirection::V => (self.knots_u.clone(), knots),
};
NurbsSurface::new(self.degree_u, self.degree_v, knots_u, knots_v, net)
.map_err(KnotRefinementRefusal::InvalidSurface)
}
}
fn boehm_table(knots: &[f64], degree: usize, value: f64) -> (Vec<f64>, usize) {
let count = knots.len() - degree - 1;
let mut span = degree;
for k in degree..count {
if knots[k] <= value {
span = k;
}
}
let alphas = (1..=degree)
.map(|i| {
let index = span - degree + i;
let denominator = knots[index + degree] - knots[index];
if denominator <= 0.0 { 0.0 } else { ((value - knots[index]) / denominator).clamp(0.0, 1.0) }
})
.collect();
(alphas, span)
}
fn blend(previous: Vec4, current: Vec4, alpha: f64) -> Vec4 {
current.scale(alpha).add(previous.scale(1.0 - alpha))
}
fn apply_line(points: &[Vec4], alphas: &[f64], span: usize, degree: usize) -> Vec<Vec4> {
let mut out = Vec::with_capacity(points.len() + 1);
out.extend_from_slice(&points[..=span - degree]);
for (i, alpha) in alphas.iter().enumerate() {
let index = span - degree + i + 1;
out.push(blend(points[index - 1], points[index], *alpha));
}
out.extend_from_slice(&points[span..]);
out
}
fn apply_u(net: &[Vec<Vec4>], alphas: &[f64], span: usize, degree: usize) -> Vec<Vec<Vec4>> {
let mut out = Vec::with_capacity(net.len() + 1);
out.extend(net[..=span - degree].iter().cloned());
for (i, alpha) in alphas.iter().enumerate() {
let index = span - degree + i + 1;
out.push(
net[index - 1]
.iter()
.zip(&net[index])
.map(|(previous, current)| blend(*previous, *current, *alpha))
.collect(),
);
}
out.extend(net[span..].iter().cloned());
out
}
impl NurbsSurface {
pub fn elevate_degree(&self, direction: KnotDirection, target: usize) -> Result<NurbsSurface, String> {
self.domain_u()?;
let (degree, knots) = match direction {
KnotDirection::U => (self.degree_u, &self.knots_u),
KnotDirection::V => (self.degree_v, &self.knots_v),
};
if target <= degree {
return Ok(self.clone());
}
let lines: Vec<Vec<Vec4>> = match direction {
KnotDirection::U => (0..self.control_points[0].len())
.map(|j| self.control_points.iter().map(|row| row[j]).collect())
.collect(),
KnotDirection::V => self.control_points.clone(),
};
let mut elevated_knots: Option<Vec<f64>> = None;
let mut elevated_lines = Vec::with_capacity(lines.len());
for line in lines {
let curve = crate::NurbsCurve::new(degree, knots.clone(), line)?;
let elevated = crate::coons::compat::elevate_degree(&curve, target)?;
match &elevated_knots {
None => elevated_knots = Some(elevated.knots.clone()),
Some(first) if *first == elevated.knots => {}
Some(_) => {
return Err("elevate_degree: lines of the net came back on different knot vectors".into())
}
}
elevated_lines.push(elevated.control_points);
}
let elevated_knots = elevated_knots.ok_or("elevate_degree: empty net")?;
match direction {
KnotDirection::U => {
let rows = elevated_lines[0].len();
let net = (0..rows).map(|i| elevated_lines.iter().map(|line| line[i]).collect()).collect();
NurbsSurface::new(target, self.degree_v, elevated_knots, self.knots_v.clone(), net)
}
KnotDirection::V => {
NurbsSurface::new(self.degree_u, target, self.knots_u.clone(), elevated_knots, elevated_lines)
}
}
}
}