Thanks @jijinbei.
This commit is contained in:
1 parent
42d4ebeb16
commit
3333f8a62b
34 files changed
+10368
-7
No files matched your search
+1488
File diff suppressed because it is too large.
Load diff
+2230
File diff suppressed because it is too large.
Load diff
+596
@@ -0,0 +1,596 @@
|
||||
use super::*;
|
||||
use crate::errors::Error;
|
||||
use std::slice::SliceIndex;
|
||||
use std::vec::Vec;
|
||||
|
||||
impl KnotVec {
|
||||
/// empty constructor
|
||||
pub const fn new() -> KnotVec { KnotVec(Vec::new()) }
|
||||
|
||||
/// Returns the length of range.
|
||||
/// # Examples
|
||||
/// ```
|
||||
/// use truck_geometry::prelude::*;
|
||||
/// let knot_vec = KnotVec::from(vec![0.0, 6.0]);
|
||||
/// assert_eq!(knot_vec.range_length(), 6.0);
|
||||
/// ```
|
||||
#[inline(always)]
|
||||
pub fn range_length(&self) -> f64 {
|
||||
match self.is_empty() {
|
||||
true => 0.0,
|
||||
false => self[self.len() - 1] - self[0],
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns whether two knot vectors have the same range.
|
||||
/// # Examples
|
||||
/// ```
|
||||
/// use truck_geometry::prelude::*;
|
||||
/// let knot_vec0 = KnotVec::new(); // empty knot vector
|
||||
/// let knot_vec1 = KnotVec::from(vec![0.0, 0.0, 1.0, 1.0]);
|
||||
/// let knot_vec2 = KnotVec::from(vec![0.0, 0.5, 1.0]);
|
||||
/// let knot_vec3 = KnotVec::from(vec![0.0, 0.0, 2.0, 2.0]);
|
||||
/// assert!(knot_vec0.same_range(&KnotVec::new())); // both empty knot vector
|
||||
/// assert!(!knot_vec0.same_range(&knot_vec1));
|
||||
/// assert!(knot_vec1.same_range(&knot_vec2)); // the range of both knot vector is [0, 1].
|
||||
/// assert!(!knot_vec1.same_range(&knot_vec3));
|
||||
/// ```
|
||||
#[inline(always)]
|
||||
pub fn same_range(&self, other: &KnotVec) -> bool {
|
||||
match (self.is_empty(), other.is_empty()) {
|
||||
(false, false) => {
|
||||
self[0].near(&other[0]) && self.range_length().near(&other.range_length())
|
||||
}
|
||||
(true, true) => true,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
/// Removes one item.
|
||||
#[inline(always)]
|
||||
pub fn remove(&mut self, idx: usize) -> f64 { self.0.remove(idx) }
|
||||
|
||||
/// Returns the maximum index `i` of `self[i] <= x`
|
||||
/// Return `None` if `x < self[0] or self.len() == 0`.
|
||||
/// # Examples
|
||||
/// ```
|
||||
/// use truck_geometry::prelude::KnotVec;
|
||||
/// let mut knot_vec = KnotVec::from(vec![0.0, 0.0, 1.0, 2.0, 3.0, 3.0]);
|
||||
/// let idx = knot_vec.floor(1.5).unwrap();
|
||||
/// assert_eq!(idx, 2);
|
||||
/// ```
|
||||
#[inline(always)]
|
||||
pub fn floor(&self, x: f64) -> Option<usize> { self.iter().rposition(|t| *t <= x) }
|
||||
|
||||
/// Returns the multiplicity of the `i`th knot
|
||||
/// # Examples
|
||||
/// ```
|
||||
/// use truck_geometry::prelude::KnotVec;
|
||||
/// let knot_vec = KnotVec::from(vec![0.0, 0.0, 1.0, 2.0, 2.0, 2.0, 3.0, 3.0]);
|
||||
/// assert_eq!(knot_vec.multiplicity(5), 3);
|
||||
/// ```
|
||||
#[inline(always)]
|
||||
pub fn multiplicity(&self, i: usize) -> usize {
|
||||
self.iter().filter(|u| self[i].near(u)).count()
|
||||
}
|
||||
|
||||
/// Adds a knot and return the index of the added knot.
|
||||
/// # Examples
|
||||
/// ```
|
||||
/// use truck_geometry::prelude::KnotVec;
|
||||
/// let mut knot_vec = KnotVec::from(vec![0.0, 0.0, 1.0, 2.0, 3.0, 3.0]);
|
||||
/// let idx0 = knot_vec.add_knot(1.5);
|
||||
/// assert_eq!(idx0, 3);
|
||||
/// let idx1 = knot_vec.add_knot(-1.0);
|
||||
/// assert_eq!(idx1, 0);
|
||||
/// let ansvec = KnotVec::from(vec![-1.0, 0.0, 0.0, 1.0, 1.5, 2.0, 3.0, 3.0]);
|
||||
/// assert_eq!(knot_vec, ansvec);
|
||||
/// ```
|
||||
#[inline(always)]
|
||||
pub fn add_knot(&mut self, knot: f64) -> usize {
|
||||
match self.floor(knot) {
|
||||
Some(idx) => {
|
||||
self.0.insert(idx + 1, knot);
|
||||
idx + 1
|
||||
}
|
||||
None => {
|
||||
self.0.insert(0, knot);
|
||||
0
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Calculates B-spline basis functions at `t` with degree `degree`.
|
||||
/// # Panics
|
||||
/// If the length of `self` is not more than `degree`, panic occurs.
|
||||
/// # Remarks
|
||||
/// In this package, the B-spline basis function is based on the characteristic function of
|
||||
/// the right-open intervals [s, t). So, the value corresponding to the end point t = t_n is always 0.0.
|
||||
/// # Examples
|
||||
/// ```
|
||||
/// use truck_geometry::prelude::*;
|
||||
/// const N : usize = 100; // sample size in tests
|
||||
///
|
||||
/// // B-spline basis functions is a partition of unity in (t_k, t_{n - k}).
|
||||
/// let vec = vec![0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0];
|
||||
/// let knot_vec = KnotVec::from(vec);
|
||||
/// let degree = 2;
|
||||
/// for i in 0..N {
|
||||
/// let t = 2.0 + 4.0 / (N as f64) * (i as f64);
|
||||
/// let res = knot_vec.bspline_basis_functions(degree, t);
|
||||
/// let sum = res.iter().fold(0.0, |sum, a| sum + a);
|
||||
/// assert_near2!(sum, 1.0);
|
||||
/// }
|
||||
/// ```
|
||||
/// ```
|
||||
/// use truck_geometry::prelude::*;
|
||||
/// const N : usize = 100; // sample size in tests
|
||||
///
|
||||
/// // In some case, B-spline basis functions coincide with Bernstein polynomials.
|
||||
/// let vec = vec![0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0];
|
||||
/// let knot_vec = KnotVec::from(vec);
|
||||
/// let degree = 3;
|
||||
/// for i in 0..=N {
|
||||
/// let t = 1.0 / (N as f64) * (i as f64);
|
||||
/// let res = knot_vec.bspline_basis_functions(degree, t);
|
||||
/// let ans = [
|
||||
/// 1.0 * (1.0 - t) * (1.0 - t) * (1.0 - t),
|
||||
/// 3.0 * t * (1.0 - t) * (1.0 - t),
|
||||
/// 3.0 * t * t * (1.0 - t),
|
||||
/// 1.0 * t * t * t,
|
||||
/// ];
|
||||
/// for i in 0..4 { assert_near2!(res[i], ans[i]); }
|
||||
/// }
|
||||
/// ```
|
||||
pub fn bspline_basis_functions(&self, degree: usize, t: f64) -> Vec<f64> {
|
||||
match self.try_bspline_basis_functions(degree, t) {
|
||||
Ok(got) => got,
|
||||
Err(error) => panic!("{}", error),
|
||||
}
|
||||
}
|
||||
|
||||
/// Calculates B-spline basis functions at `t` with degree `degree`.
|
||||
/// # Failures
|
||||
/// - If the range of the knot vector is zero, returns [`Error::ZeroRange`].
|
||||
/// - If the length of `self` is not more than `degree`, returns [`Error::TooLargeDegree`].
|
||||
/// # Remarks
|
||||
/// In this package, the B-spline basis function is based on the characteristic function of
|
||||
/// the right-open intervals [s, t). So, the value corresponding to the end point t = t_n is always 0.0.
|
||||
/// # Examples
|
||||
/// ```
|
||||
/// use truck_geometry::prelude::*;
|
||||
/// const N : usize = 100; // sample size in tests
|
||||
///
|
||||
/// // B-spline basis functions is a partition of unity in (t_k, t_{n - k}).
|
||||
/// let vec = vec![0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0];
|
||||
/// let knot_vec = KnotVec::from(vec);
|
||||
/// let degree = 2;
|
||||
/// for i in 0..N {
|
||||
/// let t = 2.0 + 4.0 / (N as f64) * (i as f64);
|
||||
/// let res = knot_vec.try_bspline_basis_functions(degree, t).unwrap();
|
||||
/// let sum = res.iter().fold(0.0, |sum, a| sum + a);
|
||||
/// assert_near2!(sum, 1.0);
|
||||
/// }
|
||||
/// ```
|
||||
/// ```
|
||||
/// use truck_geometry::prelude::*;
|
||||
/// const N : usize = 100; // sample size in tests
|
||||
///
|
||||
/// // In some case, B-spline basis functions coincide with Bernstein polynomials.
|
||||
/// let vec = vec![0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0];
|
||||
/// let knot_vec = KnotVec::from(vec);
|
||||
/// let degree = 3;
|
||||
/// for i in 0..=N {
|
||||
/// let t = i as f64 / N as f64;
|
||||
/// let res = knot_vec.try_bspline_basis_functions(degree, t).unwrap();
|
||||
/// let ans = [
|
||||
/// 1.0 * (1.0 - t) * (1.0 - t) * (1.0 - t),
|
||||
/// 3.0 * t * (1.0 - t) * (1.0 - t),
|
||||
/// 3.0 * t * t * (1.0 - t),
|
||||
/// 1.0 * t * t * t,
|
||||
/// ];
|
||||
/// for i in 0..4 { assert_near2!(res[i], ans[i]); }
|
||||
/// }
|
||||
/// ```
|
||||
pub fn try_bspline_basis_functions(&self, degree: usize, t: f64) -> Result<Vec<f64>> {
|
||||
let n = self.len() - 1;
|
||||
if self[0].near(&self[n]) {
|
||||
return Err(Error::ZeroRange);
|
||||
} else if n < degree {
|
||||
return Err(Error::TooLargeDegree(n + 1, degree));
|
||||
}
|
||||
|
||||
let idx = {
|
||||
let idx = self
|
||||
.floor(t)
|
||||
.unwrap_or_else(|| self.floor(self[0]).unwrap());
|
||||
if idx == n {
|
||||
n - self.multiplicity(n)
|
||||
} else {
|
||||
idx
|
||||
}
|
||||
};
|
||||
let mut res = vec![0.0; n];
|
||||
res[idx] = 1.0;
|
||||
|
||||
for k in 1..=degree {
|
||||
let base = if idx < k { 0 } else { idx - k };
|
||||
let delta = self[base + k] - self[base];
|
||||
let max = if idx + k < n { idx } else { n - k - 1 };
|
||||
let mut a = inv_or_zero(delta) * (t - self[base]);
|
||||
for i in base..=max {
|
||||
let delta = self[i + k + 1] - self[i + 1];
|
||||
let b = inv_or_zero(delta) * (self[i + k + 1] - t);
|
||||
res[i] = a * res[i] + b * res[i + 1];
|
||||
a = 1.0 - b;
|
||||
}
|
||||
}
|
||||
|
||||
res.truncate(n - degree);
|
||||
Ok(res)
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
pub fn maximum_points(&self, degree: usize) -> Vec<f64> {
|
||||
let n = self.len();
|
||||
let m = n - degree - 1;
|
||||
let range = self.range_length();
|
||||
const N: i32 = 100;
|
||||
|
||||
let mut res = vec![0.0; m];
|
||||
let mut max = vec![0.0; m];
|
||||
for i in 1..N {
|
||||
let t = self[0] + range * (i as f64) / (N as f64);
|
||||
let vals = self.try_bspline_basis_functions(degree, t).unwrap();
|
||||
for j in 0..m {
|
||||
if max[j] < vals[j] {
|
||||
max[j] = vals[j];
|
||||
res[j] = t;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
res
|
||||
}
|
||||
|
||||
/// Transforms the knot vector
|
||||
/// # Examples
|
||||
/// ```
|
||||
/// use std::vec::Vec;
|
||||
/// use truck_geometry::prelude::KnotVec;
|
||||
/// let mut knot_vec = KnotVec::from(vec![1.0, 1.0, 2.0, 3.0, 4.0, 5.0, 5.0]);
|
||||
/// knot_vec.transform(2.0, 3.0);
|
||||
/// let res : Vec<f64> = knot_vec.into();
|
||||
/// assert_eq!(res, vec![5.0, 5.0, 7.0, 9.0, 11.0, 13.0, 13.0]);
|
||||
/// ```
|
||||
/// # Panics
|
||||
/// Panic occurs if `scalar` is not positive.
|
||||
pub fn transform(&mut self, scalar: f64, r#move: f64) -> &mut Self {
|
||||
assert!(scalar > 0.0, "The scalar {scalar} is not positive.");
|
||||
self.0
|
||||
.iter_mut()
|
||||
.for_each(move |vec| *vec = *vec * scalar + r#move);
|
||||
self
|
||||
}
|
||||
|
||||
/// Normalizes the knot vector i.e. makes the first value 0 and the last value 1.
|
||||
/// # Failures
|
||||
/// Returns [`Error::ZeroRange`] if the range of the knot vector is so small.
|
||||
/// # Examples
|
||||
/// ```
|
||||
/// use truck_geometry::prelude::KnotVec;
|
||||
/// let mut knot_vec = KnotVec::from(vec![1.0, 1.0, 2.0, 3.0, 4.0, 5.0, 5.0]);
|
||||
/// knot_vec.try_normalize().unwrap();
|
||||
/// let res : Vec<f64> = knot_vec.into();
|
||||
/// assert_eq!(res, vec![0.0, 0.0, 0.25, 0.5, 0.75, 1.0, 1.0]);
|
||||
/// ```
|
||||
pub fn try_normalize(&mut self) -> Result<&mut Self> {
|
||||
let range = self.range_length();
|
||||
if range.so_small() {
|
||||
return Err(Error::ZeroRange);
|
||||
}
|
||||
Ok(self.transform(1.0 / range, -self[0] / range))
|
||||
}
|
||||
|
||||
/// Normalizes the knot vector i.e. makes the first value 0 and the last value 1.
|
||||
/// # Panics
|
||||
/// Panic occurs if the range of the knot vector is so small.
|
||||
/// # Examples
|
||||
/// ```
|
||||
/// use truck_geometry::prelude::KnotVec;
|
||||
/// let mut knot_vec = KnotVec::from(vec![1.0, 1.0, 2.0, 3.0, 4.0, 5.0, 5.0]);
|
||||
/// knot_vec.normalize();
|
||||
/// let res : Vec<f64> = knot_vec.into();
|
||||
/// assert_eq!(res, vec![0.0, 0.0, 0.25, 0.5, 0.75, 1.0, 1.0]);
|
||||
/// ```
|
||||
#[inline(always)]
|
||||
pub fn normalize(&mut self) -> &mut Self {
|
||||
self.try_normalize()
|
||||
.unwrap_or_else(|error| panic!("{}", error))
|
||||
}
|
||||
|
||||
/// Translates the knot vector
|
||||
/// # Example
|
||||
/// ```
|
||||
/// use std::vec::Vec;
|
||||
/// use truck_geometry::prelude::KnotVec;
|
||||
/// let mut knot_vec = KnotVec::from(vec![1.0, 1.0, 2.0, 3.0, 4.0, 5.0, 5.0]);
|
||||
/// knot_vec.translate(3.0);
|
||||
/// let res : Vec<f64> = knot_vec.into();
|
||||
/// assert_eq!(res, vec![4.0, 4.0, 5.0, 6.0, 7.0, 8.0, 8.0]);
|
||||
/// ```
|
||||
pub fn translate(&mut self, x: f64) -> &mut Self { self.transform(1.0, x) }
|
||||
|
||||
/// Inverts the knot vector
|
||||
/// # Example
|
||||
/// ```
|
||||
/// use std::vec::Vec;
|
||||
/// use truck_geometry::prelude::KnotVec;
|
||||
/// let mut knot_vec = KnotVec::from(vec![1.0, 1.0, 1.0, 3.0, 5.0, 6.0]);
|
||||
/// knot_vec.invert();
|
||||
/// let res : Vec<f64> = knot_vec.into();
|
||||
/// assert_eq!(res, vec![1.0, 2.0, 4.0, 6.0, 6.0, 6.0]);
|
||||
///
|
||||
/// ```
|
||||
pub fn invert(&mut self) -> &mut Self {
|
||||
let n = self.len();
|
||||
if n == 0 {
|
||||
return self;
|
||||
}
|
||||
let range = self[0] + self[n - 1];
|
||||
let clone = self.0.clone();
|
||||
for (knot1, knot0) in clone.iter().rev().zip(&mut self.0) {
|
||||
*knot0 = range - knot1;
|
||||
}
|
||||
self
|
||||
}
|
||||
|
||||
/// Determines the knot vector is clamped for the given degree.
|
||||
/// # Examples
|
||||
/// ```
|
||||
/// use truck_geometry::prelude::KnotVec;
|
||||
/// let knot_vec = KnotVec::from(vec![0.0, 0.0, 0.0, 0.25, 0.5, 0.75, 1.0, 1.0, 1.0]);
|
||||
/// assert!(knot_vec.is_clamped(2));
|
||||
/// assert!(!knot_vec.is_clamped(3));
|
||||
/// ```
|
||||
#[inline(always)]
|
||||
pub fn is_clamped(&self, degree: usize) -> bool {
|
||||
self.multiplicity(0) > degree && self.multiplicity(self.len() - 1) > degree
|
||||
}
|
||||
|
||||
/// Concats two knot vectors.
|
||||
/// # Examples
|
||||
/// ```
|
||||
/// use truck_geometry::prelude::KnotVec;
|
||||
/// let mut knot_vec0 = KnotVec::from(vec![0.0, 0.0, 0.0, 1.0, 1.0, 1.0]);
|
||||
/// let knot_vec1 = KnotVec::from(vec![1.0, 1.0, 1.0, 2.0, 2.0, 2.0]);
|
||||
/// knot_vec0.try_concat(&knot_vec1, 2).unwrap();
|
||||
/// assert_eq!(knot_vec0.as_slice(), &[0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 2.0, 2.0, 2.0]);
|
||||
/// ```
|
||||
/// # Failures
|
||||
/// - If at least one of `self` or `other` is not clamped, returns [`Error::NotClampedKnotVector`]
|
||||
/// - If the last knot of `self` and the first knot of `other` are different, returns [`Error::DifferentBackFront`].
|
||||
pub fn try_concat(&mut self, other: &KnotVec, degree: usize) -> Result<&mut Self> {
|
||||
if !self.is_clamped(degree) || !other.is_clamped(degree) {
|
||||
return Err(Error::NotClampedKnotVector);
|
||||
}
|
||||
let back = self.0.last().unwrap();
|
||||
let front = other.0.first().unwrap();
|
||||
if front < back || !front.near(back) {
|
||||
return Err(Error::DifferentBackFront(*back, *front));
|
||||
}
|
||||
|
||||
self.0.truncate(self.len() - degree - 1);
|
||||
for knot in &other.0 {
|
||||
self.0.push(*knot);
|
||||
}
|
||||
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
/// Concats two knot vectors.
|
||||
/// # Examples
|
||||
/// ```
|
||||
/// use truck_geometry::prelude::KnotVec;
|
||||
/// let mut knot_vec0 = KnotVec::from(vec![0.0, 0.0, 0.0, 1.0, 1.0, 1.0]);
|
||||
/// let knot_vec1 = KnotVec::from(vec![1.0, 1.0, 1.0, 2.0, 2.0, 2.0]);
|
||||
/// knot_vec0.concat(&knot_vec1, 2);
|
||||
/// assert_eq!(knot_vec0.as_slice(), &[0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 2.0, 2.0, 2.0]);
|
||||
/// ```
|
||||
/// # Panics
|
||||
/// Panic occurs if:
|
||||
/// - at least one of `self` or `other` is not clamped.
|
||||
/// - the last knot of `self` and the first knot of `other` are different.
|
||||
#[inline(always)]
|
||||
pub fn concat(&mut self, other: &KnotVec, degree: usize) -> &mut Self {
|
||||
self.try_concat(other, degree)
|
||||
.unwrap_or_else(|error| panic!("{}", error))
|
||||
}
|
||||
|
||||
/// Returns trimmed vector by the specified range.
|
||||
/// # Examples
|
||||
/// ```
|
||||
/// use truck_geometry::prelude::*;
|
||||
/// let knot_vec = KnotVec::from(vec![0.0, 1.0, 2.0, 3.0, 4.0, 5.0]);
|
||||
/// let sub_vec = knot_vec.sub_vec(1..3);
|
||||
/// assert_eq!(sub_vec, KnotVec::from(vec![1.0, 2.0]));
|
||||
/// ```
|
||||
#[inline(always)]
|
||||
pub fn sub_vec<I: SliceIndex<[f64], Output = [f64]>>(&self, range: I) -> KnotVec {
|
||||
KnotVec(Vec::from(&self.0[range]))
|
||||
}
|
||||
|
||||
/// To single-multi description. i.e. decompose the unique vector of knots and the vector of
|
||||
/// multiplicity of knots.
|
||||
/// # Examples
|
||||
/// ```
|
||||
/// use truck_geometry::prelude::KnotVec;
|
||||
/// let knot_vec = KnotVec::from(vec![0.0, 0.0, 0.0, 1.0, 2.0, 2.0, 2.0, 2.0, 3.0, 3.0]);
|
||||
/// let (knots, mults) = knot_vec.to_single_multi();
|
||||
/// assert_eq!(knots, vec![0.0, 1.0, 2.0, 3.0]);
|
||||
/// assert_eq!(mults, vec![3, 1, 4, 2]);
|
||||
/// ```
|
||||
pub fn to_single_multi(&self) -> (Vec<f64>, Vec<usize>) {
|
||||
let mut knots = Vec::new();
|
||||
let mut mults = Vec::new();
|
||||
|
||||
let mut iter = self.as_slice().iter().peekable();
|
||||
let mut mult = 1;
|
||||
while let Some(knot) = iter.next() {
|
||||
if let Some(next) = iter.peek() {
|
||||
if knot.near(next) {
|
||||
mult += 1;
|
||||
} else {
|
||||
knots.push(*knot);
|
||||
mults.push(mult);
|
||||
mult = 1;
|
||||
}
|
||||
} else {
|
||||
knots.push(*knot);
|
||||
mults.push(mult);
|
||||
}
|
||||
}
|
||||
(knots, mults)
|
||||
}
|
||||
|
||||
/// Constructs from single-multi description.
|
||||
/// # Examples
|
||||
/// ```
|
||||
/// use truck_geometry::prelude::KnotVec;
|
||||
/// let knots = vec![0.0, 1.0, 2.0, 3.0];
|
||||
/// let mults = vec![3, 1, 4, 2];
|
||||
/// let knot_vec = KnotVec::from_single_multi(knots, mults).unwrap();
|
||||
/// assert_eq!(knot_vec, KnotVec::from(vec![0.0, 0.0, 0.0, 1.0, 2.0, 2.0, 2.0, 2.0, 3.0, 3.0]));
|
||||
/// ```
|
||||
pub fn from_single_multi(knots: Vec<f64>, mults: Vec<usize>) -> Result<KnotVec> {
|
||||
for i in 1..knots.len() {
|
||||
if knots[i - 1] > knots[i] {
|
||||
return Err(Error::NotSortedVector);
|
||||
}
|
||||
}
|
||||
|
||||
let mut vec = Vec::new();
|
||||
for i in 0..knots.len() {
|
||||
for _ in 0..mults[i] {
|
||||
vec.push(knots[i]);
|
||||
}
|
||||
}
|
||||
Ok(KnotVec(vec))
|
||||
}
|
||||
/// Constructs from `Vec<f64>`. do not sort, only check sorted.
|
||||
pub fn try_from(vec: Vec<f64>) -> Result<KnotVec> {
|
||||
for i in 1..vec.len() {
|
||||
if vec[i - 1] > vec[i] {
|
||||
return Err(Error::NotSortedVector);
|
||||
}
|
||||
}
|
||||
Ok(KnotVec(vec))
|
||||
}
|
||||
|
||||
/// Constructs the knot vector for the bezier spline.
|
||||
/// # Examples
|
||||
/// ```
|
||||
/// use truck_geometry::prelude::*;
|
||||
/// assert_eq!(
|
||||
/// *KnotVec::bezier_knot(3),
|
||||
/// vec![0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0],
|
||||
/// );
|
||||
/// ```
|
||||
pub fn bezier_knot(degree: usize) -> KnotVec {
|
||||
let mut vec = vec![0.0; degree + 1];
|
||||
vec.extend(std::iter::repeat(1.0).take(degree + 1));
|
||||
KnotVec(vec)
|
||||
}
|
||||
|
||||
/// Constructs the uniform knot vector
|
||||
/// # Examples
|
||||
/// ```
|
||||
/// use truck_geometry::prelude::*;
|
||||
/// assert_eq!(
|
||||
/// *KnotVec::uniform_knot(2, 5),
|
||||
/// vec![0.0, 0.0, 0.0, 0.2, 0.4, 0.6, 0.8, 1.0, 1.0, 1.0],
|
||||
/// );
|
||||
/// ```
|
||||
pub fn uniform_knot(degree: usize, division: usize) -> KnotVec {
|
||||
let mut vec = vec![0.0; degree + 1];
|
||||
vec.extend((1..division).map(|i| (i as f64) / (division as f64)));
|
||||
vec.extend(std::iter::repeat(1.0).take(degree + 1));
|
||||
KnotVec(vec)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Vec<f64>> for KnotVec {
|
||||
/// constructs from `Vec<f64>`. The vector will sorted by the order.
|
||||
/// ```
|
||||
/// use truck_geometry::prelude::KnotVec;
|
||||
/// let knot_vec = KnotVec::from(vec![1.0, 0.0, 3.0, 2.0]);
|
||||
/// let arr : Vec<f64> = knot_vec.into();
|
||||
/// assert_eq!(arr, vec![0.0, 1.0, 2.0, 3.0]);
|
||||
/// ```
|
||||
fn from(mut vec: Vec<f64>) -> KnotVec {
|
||||
vec.sort_by(|a, b| a.partial_cmp(b).unwrap());
|
||||
KnotVec(vec)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&Vec<f64>> for KnotVec {
|
||||
/// Constructs by the reference of vector. The clone of vector is sorted by the order.
|
||||
/// ```
|
||||
/// use truck_geometry::prelude::KnotVec;
|
||||
/// let knot_vec = KnotVec::from(&vec![1.0, 0.0, 3.0, 2.0]);
|
||||
/// let arr : Vec<f64> = knot_vec.into();
|
||||
/// assert_eq!(arr, vec![0.0, 1.0, 2.0, 3.0]);
|
||||
/// ```
|
||||
#[inline(always)]
|
||||
fn from(vec: &Vec<f64>) -> KnotVec {
|
||||
let mut copy_vec = vec.clone();
|
||||
copy_vec.sort_by(|a, b| a.partial_cmp(b).unwrap());
|
||||
KnotVec(copy_vec)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<KnotVec> for Vec<f64> {
|
||||
/// `KnotVec` into `Vec<f64>`
|
||||
/// ```
|
||||
/// use truck_geometry::prelude::KnotVec;
|
||||
/// let vec = vec![0.0, 1.0, 2.0, 3.0];
|
||||
/// let knot_vec = KnotVec::from(&vec);
|
||||
/// let vec0 : Vec<f64> = knot_vec.into();
|
||||
/// assert_eq!(vec, vec0);
|
||||
/// ```
|
||||
#[inline(always)]
|
||||
fn from(knotvec: KnotVec) -> Vec<f64> { knotvec.0 }
|
||||
}
|
||||
|
||||
impl FromIterator<f64> for KnotVec {
|
||||
#[inline(always)]
|
||||
fn from_iter<I: IntoIterator<Item = f64>>(iter: I) -> KnotVec {
|
||||
KnotVec::try_from(iter.into_iter().collect::<Vec<_>>()).unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> IntoIterator for &'a KnotVec {
|
||||
type Item = &'a f64;
|
||||
type IntoIter = std::slice::Iter<'a, f64>;
|
||||
#[inline(always)]
|
||||
fn into_iter(self) -> Self::IntoIter { self.0.iter() }
|
||||
}
|
||||
|
||||
impl std::ops::Deref for KnotVec {
|
||||
type Target = Vec<f64>;
|
||||
#[inline(always)]
|
||||
fn deref(&self) -> &Vec<f64> { &self.0 }
|
||||
}
|
||||
|
||||
impl AsRef<[f64]> for KnotVec {
|
||||
#[inline(always)]
|
||||
fn as_ref(&self) -> &[f64] { &self.0 }
|
||||
}
|
||||
|
||||
impl<'de> Deserialize<'de> for KnotVec {
|
||||
fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
|
||||
where D: serde::Deserializer<'de> {
|
||||
let vec = Vec::<f64>::deserialize(deserializer)?;
|
||||
Self::try_from(vec).map_err(serde::de::Error::custom)
|
||||
}
|
||||
}
|
||||
+135
@@ -0,0 +1,135 @@
|
||||
use crate::{prelude::*, *};
|
||||
use truck_base::cgmath64::control_point::ControlPoint;
|
||||
|
||||
/// knot vector
|
||||
#[derive(Clone, PartialEq, Debug, Default, Serialize)]
|
||||
pub struct KnotVec(Vec<f64>);
|
||||
|
||||
/// B-spline curve
|
||||
/// # Examples
|
||||
/// ```
|
||||
/// use truck_geometry::prelude::*;
|
||||
///
|
||||
/// // the knot vector
|
||||
/// let knot_vec = KnotVec::from(
|
||||
/// vec![0.0, 0.0, 0.0, 0.25, 0.25, 0.5, 0.5, 0.75, 0.75, 1.0, 1.0, 1.0]
|
||||
/// );
|
||||
///
|
||||
/// // sign up the control points in the vector of all points
|
||||
/// let ctrl_pts = vec![ // the vector of the indices of control points
|
||||
/// Vector4::new(0.0, -2.0, 0.0, 2.0),
|
||||
/// Vector4::new(1.0, -1.0, 0.0, 1.0),
|
||||
/// Vector4::new(1.0, 0.0, 0.0, 1.0),
|
||||
/// Vector4::new(1.0, 1.0, 0.0, 1.0),
|
||||
/// Vector4::new(0.0, 2.0, 0.0, 2.0),
|
||||
/// Vector4::new(-1.0, 1.0, 0.0, 1.0),
|
||||
/// Vector4::new(-1.0, 0.0, 0.0, 1.0),
|
||||
/// Vector4::new(-1.0, -1.0, 0.0, 1.0),
|
||||
/// Vector4::new(0.0, -2.0, 0.0, 2.0),
|
||||
/// ];
|
||||
///
|
||||
/// // construct the B-spline curve
|
||||
/// let bspline = BSplineCurve::new(knot_vec, ctrl_pts);
|
||||
///
|
||||
/// // This B-spline curve is a nurbs representation of the unit circle.
|
||||
/// const N : usize = 100; // sample size in test
|
||||
/// for i in 0..N {
|
||||
/// let t = 1.0 / (N as f64) * (i as f64);
|
||||
/// let v = bspline.subs(t); // We can use the instances as a function.
|
||||
/// let c = (v[0] / v[3]).powi(2) + (v[1] / v[3]).powi(2);
|
||||
/// assert_near2!(c, 1.0);
|
||||
/// }
|
||||
/// ```
|
||||
#[derive(Clone, PartialEq, Debug, Serialize)]
|
||||
pub struct BSplineCurve<P> {
|
||||
knot_vec: KnotVec, // the knot vector
|
||||
control_points: Vec<P>, // the indices of control points
|
||||
}
|
||||
|
||||
/// B-spline surface
|
||||
/// # Examples
|
||||
/// ```
|
||||
/// use truck_geometry::prelude::*;
|
||||
/// const N : usize = 100; // sample size in test
|
||||
///
|
||||
/// // the knot vectors
|
||||
/// let knot_vec0 = KnotVec::bezier_knot(3);
|
||||
/// let knot_vec1 = KnotVec::from(
|
||||
/// vec![0.0, 0.0, 0.0, 0.0, 0.5, 0.5, 0.5, 1.0, 1.0, 1.0, 1.0]
|
||||
/// );
|
||||
/// let knot_vecs = (knot_vec0, knot_vec1);
|
||||
///
|
||||
/// // the control points
|
||||
/// let mut v = vec![vec![Vector4::zero(); 7]; 4];
|
||||
/// v[0][0] = Vector4::new(0.0, 0.0, 1.0, 1.0);
|
||||
/// v[0][1] = &v[0][0] / 3.0;
|
||||
/// v[0][2] = v[0][1].clone();
|
||||
/// v[0][3] = v[0][0].clone();
|
||||
/// v[0][4] = v[0][1].clone();
|
||||
/// v[0][5] = v[0][1].clone();
|
||||
/// v[0][6] = v[0][0].clone();
|
||||
/// v[1][0] = Vector4::new(2.0, 0.0, 1.0, 1.0) / 3.0;
|
||||
/// v[1][1] = Vector4::new(2.0, 4.0, 1.0, 1.0) / 9.0;
|
||||
/// v[1][2] = Vector4::new(-2.0, 4.0, 1.0, 1.0) / 9.0;
|
||||
/// v[1][3] = Vector4::new(-2.0, 0.0, 1.0, 1.0) / 3.0;
|
||||
/// v[1][4] = Vector4::new(-2.0, -4.0, 1.0, 1.0) / 9.0;
|
||||
/// v[1][5] = Vector4::new(2.0, -4.0, 1.0, 1.0) / 9.0;
|
||||
/// v[1][6] = Vector4::new(2.0, 0.0, 1.0, 1.0) / 3.0;
|
||||
/// v[2][0] = Vector4::new(2.0, 0.0, -1.0, 1.0) / 3.0;
|
||||
/// v[2][1] = Vector4::new(2.0, 4.0, -1.0, 1.0) / 9.0;
|
||||
/// v[2][2] = Vector4::new(-2.0, 4.0, -1.0, 1.0) / 9.0;
|
||||
/// v[2][3] = Vector4::new(-2.0, 0.0, -1.0, 1.0) / 3.0;
|
||||
/// v[2][4] = Vector4::new(-2.0, -4.0, -1.0, 1.0) / 9.0;
|
||||
/// v[2][5] = Vector4::new(2.0, -4.0, -1.0, 1.0) / 9.0;
|
||||
/// v[2][6] = Vector4::new(2.0, 0.0, -1.0, 1.0) / 3.0;
|
||||
/// v[3][0] = Vector4::new(0.0, 0.0, -1.0, 1.0);
|
||||
/// v[3][1] = &v[3][0] / 3.0;
|
||||
/// v[3][2] = v[3][1].clone();
|
||||
/// v[3][3] = v[3][0].clone();
|
||||
/// v[3][4] = v[3][1].clone();
|
||||
/// v[3][5] = v[3][1].clone();
|
||||
/// v[3][6] = v[3][0].clone();
|
||||
///
|
||||
/// // cunstruct the B-spline curve
|
||||
/// let bspline = BSplineSurface::new(knot_vecs, v);
|
||||
///
|
||||
/// // This B-spline curve is a nurbs representation of the unit sphere.
|
||||
/// for i in 0..N {
|
||||
/// for j in 0..N {
|
||||
/// let u = 1.0 / (N as f64) * (i as f64);
|
||||
/// let v = 1.0 / (N as f64) * (j as f64);
|
||||
/// let v = bspline.subs(u, v); // We can use the instances as a function.
|
||||
/// let c = (v[0] / v[3]).powi(2) + (v[1] / v[3]).powi(2) + (v[2] / v[3]).powi(2);
|
||||
/// assert_near2!(c, 1.0);
|
||||
/// }
|
||||
/// }
|
||||
/// ```
|
||||
#[derive(Clone, PartialEq, Debug, Serialize)]
|
||||
pub struct BSplineSurface<P> {
|
||||
knot_vecs: (KnotVec, KnotVec),
|
||||
control_points: Vec<Vec<P>>,
|
||||
}
|
||||
|
||||
/// NURBS curve
|
||||
#[derive(Clone, PartialEq, Debug, Serialize, Deserialize)]
|
||||
pub struct NurbsCurve<V>(BSplineCurve<V>);
|
||||
|
||||
/// NURBS surface
|
||||
#[derive(Clone, PartialEq, Debug, Serialize, Deserialize)]
|
||||
pub struct NurbsSurface<V>(BSplineSurface<V>);
|
||||
|
||||
mod bspcurve;
|
||||
mod bspsurface;
|
||||
mod knot_vec;
|
||||
mod nurbscurve;
|
||||
mod nurbssurface;
|
||||
|
||||
#[doc(hidden)]
|
||||
#[inline(always)]
|
||||
pub fn inv_or_zero(delta: f64) -> f64 {
|
||||
if delta.so_small() {
|
||||
0.0
|
||||
} else {
|
||||
1.0 / delta
|
||||
}
|
||||
}
|
||||
+619
@@ -0,0 +1,619 @@
|
||||
use super::*;
|
||||
|
||||
impl<V> NurbsCurve<V> {
|
||||
/// Constructs the rationalized B-spline curve.
|
||||
#[inline(always)]
|
||||
pub const fn new(curve: BSplineCurve<V>) -> Self { NurbsCurve(curve) }
|
||||
|
||||
/// Returns the BSpline curve before rationalized.
|
||||
#[inline(always)]
|
||||
pub const fn non_rationalized(&self) -> &BSplineCurve<V> { &self.0 }
|
||||
|
||||
/// Returns the BSpline curve before rationalized.
|
||||
#[inline(always)]
|
||||
pub fn into_non_rationalized(self) -> BSplineCurve<V> { self.0 }
|
||||
|
||||
/// Returns the reference of the knot vector. cf.[`BSplineCurve::knot_vec`]
|
||||
#[inline(always)]
|
||||
pub const fn knot_vec(&self) -> &KnotVec { &self.0.knot_vec }
|
||||
|
||||
/// Returns the `idx`th knot. cf.[`BSplineCurve::knot`]
|
||||
#[inline(always)]
|
||||
pub fn knot(&self, idx: usize) -> f64 { self.0.knot_vec[idx] }
|
||||
|
||||
/// Returns the reference of the control points. cf.[`BSplineCurve::control_points`]
|
||||
#[inline(always)]
|
||||
pub const fn control_points(&self) -> &Vec<V> { &self.0.control_points }
|
||||
|
||||
/// Returns the reference of the control point corresponding to the index `idx`.
|
||||
/// cf.[`BSplineCurve::control_point`]
|
||||
#[inline(always)]
|
||||
pub fn control_point(&self, idx: usize) -> &V { &self.0.control_points[idx] }
|
||||
|
||||
/// Returns the mutable reference of the control point corresponding to index `idx`.
|
||||
/// cf.[`BSplineCurve::control_point_mut`]
|
||||
#[inline(always)]
|
||||
pub fn control_point_mut(&mut self, idx: usize) -> &mut V { &mut self.0.control_points[idx] }
|
||||
|
||||
/// Returns the iterator on all control points. cf.[`BSplineCurve::control_points_mut`]
|
||||
#[inline(always)]
|
||||
pub fn control_points_mut(&mut self) -> impl Iterator<Item = &mut V> {
|
||||
self.0.control_points.iter_mut()
|
||||
}
|
||||
|
||||
/// Applies the given transformation to all control points. cf.[`BSplineCurve::transform_control_points`]
|
||||
#[inline(always)]
|
||||
pub fn transform_control_points<F: FnMut(&mut V)>(&mut self, f: F) {
|
||||
self.0.transform_control_points(f)
|
||||
}
|
||||
|
||||
/// Returns the degree of NURBS curve. cf.[`BSplineCurve::degree`]
|
||||
#[inline(always)]
|
||||
pub fn degree(&self) -> usize { self.0.degree() }
|
||||
|
||||
/// Inverts a curve. cf.[`BSplineCurve::invert`]
|
||||
#[inline(always)]
|
||||
pub fn invert(&mut self) -> &mut Self {
|
||||
self.0.invert();
|
||||
self
|
||||
}
|
||||
|
||||
/// Returns whether the knot vector is clamped or not. cf.[`BSplineCurve::is_clamped`]
|
||||
#[inline(always)]
|
||||
pub fn is_clamped(&self) -> bool { self.0.knot_vec.is_clamped(self.0.degree()) }
|
||||
|
||||
/// Normalizes the knot vector. cf.[`BSplineCurve::knot_normalize`]
|
||||
#[inline(always)]
|
||||
pub fn knot_normalize(&mut self) -> &mut Self {
|
||||
self.0.knot_vec.try_normalize().unwrap();
|
||||
self
|
||||
}
|
||||
|
||||
/// Translates the knot vector. cf.[`BSplineCurve::knot_translate`]
|
||||
#[inline(always)]
|
||||
pub fn knot_translate(&mut self, x: f64) -> &mut Self {
|
||||
self.0.knot_vec.translate(x);
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl<V: Homogeneous<f64>> NurbsCurve<V> {
|
||||
/// Constructs a rationalization curve from the non-rationalized curve and weights.
|
||||
/// # Failures
|
||||
/// the length of `curve.control_points()` and `weights` must be the same.
|
||||
#[inline(always)]
|
||||
pub fn try_from_bspline_and_weights(
|
||||
curve: BSplineCurve<V::Point>,
|
||||
weights: Vec<f64>,
|
||||
) -> Result<Self> {
|
||||
let BSplineCurve {
|
||||
knot_vec,
|
||||
control_points,
|
||||
} = curve;
|
||||
if control_points.len() != weights.len() {
|
||||
return Err(Error::DifferentLength);
|
||||
}
|
||||
let control_points = control_points
|
||||
.into_iter()
|
||||
.zip(weights)
|
||||
.map(|(pt, w)| V::from_point_weight(pt, w))
|
||||
.collect();
|
||||
Ok(Self(BSplineCurve::new_unchecked(knot_vec, control_points)))
|
||||
}
|
||||
}
|
||||
|
||||
impl<V: Homogeneous<f64> + ControlPoint<f64, Diff = V>> NurbsCurve<V> {
|
||||
/// Returns the closure of substitution.
|
||||
#[inline(always)]
|
||||
pub fn get_closure(&self) -> impl Fn(f64) -> V::Point + '_ { move |t| self.subs(t) }
|
||||
}
|
||||
|
||||
impl<V: Homogeneous<f64> + ControlPoint<f64, Diff = V>> NurbsCurve<V>
|
||||
where V::Point: Tolerance
|
||||
{
|
||||
/// Returns whether all control points are the same or not.
|
||||
/// If the knot vector is clamped, it means whether the curve is constant or not.
|
||||
/// # Examples
|
||||
/// ```
|
||||
/// use truck_geometry::prelude::*;
|
||||
///
|
||||
/// let knot_vec = KnotVec::bezier_knot(2);
|
||||
/// let pt = Vector3::new(1.0, 2.0, 1.0);
|
||||
/// // allows differences upto scalars
|
||||
/// let mut ctrl_pts = vec![pt.clone(), pt.clone() * 2.0, pt.clone() * 3.0];
|
||||
/// let bspcurve = BSplineCurve::new(knot_vec.clone(), ctrl_pts.clone());
|
||||
/// assert!(!bspcurve.is_const());
|
||||
/// let const_curve = NurbsCurve::new(bspcurve);
|
||||
/// assert!(const_curve.is_const());
|
||||
///
|
||||
/// ctrl_pts.push(Vector3::new(2.0, 3.0, 1.0));
|
||||
/// let curve = NurbsCurve::new(BSplineCurve::new(knot_vec.clone(), ctrl_pts.clone()));
|
||||
/// assert!(!curve.is_const());
|
||||
/// ```
|
||||
/// # Remarks
|
||||
/// If the knot vector is not clamped and the BSpline basis function is not partition of unity,
|
||||
/// then perhaps returns true even if the curve is not constant.
|
||||
/// ```
|
||||
/// use truck_geometry::prelude::*;
|
||||
/// let knot_vec = KnotVec::uniform_knot(1, 5);
|
||||
/// let ctrl_pts = vec![Vector2::new(1.0, 2.0), Vector2::new(1.0, 2.0)];
|
||||
/// let bspcurve = BSplineCurve::new(knot_vec, ctrl_pts);
|
||||
///
|
||||
/// // bspcurve is not constant.
|
||||
/// assert_eq!(bspcurve.subs(0.0), Vector2::new(0.0, 0.0));
|
||||
/// assert_ne!(bspcurve.subs(0.5), Vector2::new(0.0, 0.0));
|
||||
///
|
||||
/// // bspcurve.is_const() is true
|
||||
/// assert!(bspcurve.is_const());
|
||||
/// ```
|
||||
pub fn is_const(&self) -> bool {
|
||||
let pt = self.0.control_points[0].to_point();
|
||||
self.0
|
||||
.control_points
|
||||
.iter()
|
||||
.all(move |vec| vec.to_point().near(&pt))
|
||||
}
|
||||
/// Determines whether `self` and `other` is near as the B-spline curves or not.
|
||||
///
|
||||
/// Divides each knot interval into the number of degree equal parts,
|
||||
/// and check `|self(t) - other(t)| < TOLERANCE` for each end points `t`.
|
||||
/// # Examples
|
||||
/// ```
|
||||
/// use truck_geometry::prelude::*;
|
||||
/// let knot_vec = KnotVec::from(
|
||||
/// vec![0.0, 0.0, 0.0, 1.0, 2.0, 3.0, 4.0, 4.0, 4.0]
|
||||
/// );
|
||||
/// let ctrl_pts = vec![
|
||||
/// Vector3::new(1.0, 1.0, 1.0),
|
||||
/// Vector3::new(3.0, 2.0, 2.0),
|
||||
/// Vector3::new(2.0, 3.0, 1.0),
|
||||
/// Vector3::new(4.0, 5.0, 2.0),
|
||||
/// Vector3::new(5.0, 4.0, 1.0),
|
||||
/// Vector3::new(1.0, 1.0, 2.0),
|
||||
/// ];
|
||||
/// let curve0 = NurbsCurve::new(BSplineCurve::new(knot_vec, ctrl_pts));
|
||||
/// let mut curve1 = curve0.clone();
|
||||
/// assert!(curve0.near_as_curve(&curve1));
|
||||
/// *curve1.control_point_mut(1) += Vector3::new(0.01, 0.0002, 0.0);
|
||||
/// assert!(!curve0.near_as_curve(&curve1));
|
||||
/// ```
|
||||
#[inline(always)]
|
||||
pub fn near_as_curve(&self, other: &Self) -> bool {
|
||||
self.0
|
||||
.sub_near_as_curve(&other.0, 2, move |x, y| x.to_point().near(&y.to_point()))
|
||||
}
|
||||
|
||||
/// Determines `self` and `other` is near in square order as the B-spline curves or not.
|
||||
///
|
||||
/// Divide each knot interval into the number of degree equal parts,
|
||||
/// and check `|self(t) - other(t)| < TOLERANCE`for each end points `t`.
|
||||
/// # Examples
|
||||
/// ```
|
||||
/// use truck_geometry::prelude::*;
|
||||
/// let knot_vec = KnotVec::from(
|
||||
/// vec![0.0, 0.0, 0.0, 1.0, 2.0, 3.0, 4.0, 4.0, 4.0]
|
||||
/// );
|
||||
/// let ctrl_pts = vec![
|
||||
/// Vector3::new(1.0, 1.0, 1.0),
|
||||
/// Vector3::new(3.0, 2.0, 2.0),
|
||||
/// Vector3::new(2.0, 3.0, 1.0),
|
||||
/// Vector3::new(4.0, 5.0, 2.0),
|
||||
/// Vector3::new(5.0, 4.0, 1.0),
|
||||
/// Vector3::new(1.0, 1.0, 2.0),
|
||||
/// ];
|
||||
/// let curve0 = NurbsCurve::new(BSplineCurve::new(knot_vec, ctrl_pts));
|
||||
/// let mut curve1 = curve0.clone();
|
||||
/// assert!(curve0.near_as_curve(&curve1));
|
||||
/// *curve1.control_point_mut(1) += Vector3::new(0.01, TOLERANCE, 0.0);
|
||||
/// assert!(!curve0.near2_as_curve(&curve1));
|
||||
/// ```
|
||||
#[inline(always)]
|
||||
pub fn near2_as_curve(&self, other: &Self) -> bool {
|
||||
self.0
|
||||
.sub_near_as_curve(&other.0, 2, move |x, y| x.to_point().near2(&y.to_point()))
|
||||
}
|
||||
}
|
||||
|
||||
impl<V: Homogeneous<f64> + ControlPoint<f64, Diff = V> + Tolerance> NurbsCurve<V> {
|
||||
/// Adds a knot `x`, and do not change `self` as a curve. cf.[`BSplineCurve::add_knot`]
|
||||
pub fn add_knot(&mut self, x: f64) -> &mut Self {
|
||||
self.0.add_knot(x);
|
||||
self
|
||||
}
|
||||
|
||||
/// Removes a knot corresponding to the indices `idx`, and do not change `self` as a curve.
|
||||
/// If cannot remove the knot, do not change `self` and return `self`.
|
||||
/// cf.[`BSplineCurve::remove_knot`]
|
||||
pub fn remove_knot(&mut self, idx: usize) -> &mut Self {
|
||||
let _ = self.try_remove_knot(idx);
|
||||
self
|
||||
}
|
||||
|
||||
/// Removes a knot corresponding to the indice `idx`, and do not change `self` as a curve.
|
||||
/// If the knot cannot be removed, returns [`Error::CannotRemoveKnot`].
|
||||
/// cf.[`BSplineCurve::try_remove_knot`]
|
||||
pub fn try_remove_knot(&mut self, idx: usize) -> Result<&mut Self> {
|
||||
self.0.try_remove_knot(idx)?;
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
/// Elevates 1 degree. cf.[`BSplineCurve::elevate_degree`]
|
||||
pub fn elevate_degree(&mut self) -> &mut Self {
|
||||
self.0.elevate_degree();
|
||||
self
|
||||
}
|
||||
|
||||
/// Makes the NURBS curve clamped. cf.[`BSplineCurve::clamp`]
|
||||
#[inline(always)]
|
||||
pub fn clamp(&mut self) -> &mut Self {
|
||||
self.0.clamp();
|
||||
self
|
||||
}
|
||||
|
||||
/// Repeats `Self::try_remove_knot()` from the back knot in turn until the knot cannot be removed.
|
||||
/// cf.[`BSplineCurve::optimize`]
|
||||
pub fn optimize(&mut self) -> &mut Self {
|
||||
self.0.optimize();
|
||||
self
|
||||
}
|
||||
|
||||
/// Makes two splines having the same degrees. cf.[`BSplineCurve::syncro_degree`]
|
||||
pub fn syncro_degree(&mut self, other: &mut Self) {
|
||||
let (degree0, degree1) = (self.degree(), other.degree());
|
||||
for _ in degree0..degree1 {
|
||||
self.elevate_degree();
|
||||
}
|
||||
for _ in degree1..degree0 {
|
||||
other.elevate_degree();
|
||||
}
|
||||
}
|
||||
|
||||
/// Makes two splines having the same normalized knot vectors. cf.[`BSplineCurve::syncro_knots`]
|
||||
pub fn syncro_knots(&mut self, other: &mut Self) { self.0.syncro_knots(&mut other.0) }
|
||||
}
|
||||
|
||||
impl<V: Homogeneous<f64> + ControlPoint<f64, Diff = V> + Tolerance> ParameterTransform
|
||||
for NurbsCurve<V>
|
||||
{
|
||||
#[inline(always)]
|
||||
fn parameter_transform(&mut self, scalar: f64, r#move: f64) -> &mut Self {
|
||||
self.0.parameter_transform(scalar, r#move);
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl<V: Homogeneous<f64> + ControlPoint<f64, Diff = V> + Tolerance> Cut for NurbsCurve<V> {
|
||||
#[inline(always)]
|
||||
fn cut(&mut self, t: f64) -> Self { NurbsCurve(self.0.cut(t)) }
|
||||
}
|
||||
|
||||
impl<V: Homogeneous<f64> + ControlPoint<f64, Diff = V> + Tolerance> Concat<NurbsCurve<V>>
|
||||
for NurbsCurve<V>
|
||||
where <V as Homogeneous<f64>>::Point: Debug
|
||||
{
|
||||
type Output = NurbsCurve<V>;
|
||||
fn try_concat(
|
||||
&self,
|
||||
other: &Self,
|
||||
) -> std::result::Result<Self, ConcatError<<V as Homogeneous<f64>>::Point>> {
|
||||
let curve0 = self.clone();
|
||||
let mut curve1 = other.clone();
|
||||
let w0 = curve0.0.control_points.last().unwrap().weight();
|
||||
let w1 = curve1.0.control_points[0].weight();
|
||||
curve1.transform_control_points(|pt| *pt *= w0 / w1);
|
||||
match curve0.0.try_concat(&curve1.0) {
|
||||
Ok(curve) => Ok(NurbsCurve::new(curve)),
|
||||
Err(err) => Err(err.point_map(|v| v.to_point())),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn concat_positive_test() {
|
||||
let mut part0 = NurbsCurve::new(BSplineCurve::new(
|
||||
KnotVec::uniform_knot(4, 4),
|
||||
(0..8)
|
||||
.map(|_| {
|
||||
Vector4::new(
|
||||
rand::random::<f64>(),
|
||||
rand::random::<f64>(),
|
||||
rand::random::<f64>(),
|
||||
rand::random::<f64>() + 0.5,
|
||||
)
|
||||
})
|
||||
.collect(),
|
||||
));
|
||||
let mut part1 = part0.cut(0.56);
|
||||
let w = 20.0 * rand::random::<f64>() - 10.0;
|
||||
part1.transform_control_points(|vec| *vec *= w);
|
||||
assert_near!(part0.back(), part1.front());
|
||||
concat_random_test(&part0, &part1, 10);
|
||||
}
|
||||
|
||||
impl<V: Homogeneous<f64> + ControlPoint<f64, Diff = V> + Tolerance> NurbsCurve<V>
|
||||
where V::Point: Tolerance
|
||||
{
|
||||
/// Makes the rational curve locally injective.
|
||||
/// # Example
|
||||
/// ```
|
||||
/// use truck_geometry::prelude::*;
|
||||
/// const N : usize = 100; // sample size for test
|
||||
///
|
||||
/// let knot_vec = KnotVec::from(
|
||||
/// vec![0.0, 0.0, 0.0, 1.0, 3.0, 4.0, 4.0, 4.0]
|
||||
/// );
|
||||
/// let control_points = vec![
|
||||
/// Vector4::new(1.0, 0.0, 0.0, 1.0),
|
||||
/// Vector4::new(0.0, 1.0, 0.0, 1.0),
|
||||
/// Vector4::new(0.0, 2.0, 0.0, 2.0),
|
||||
/// Vector4::new(0.0, 3.0, 0.0, 3.0),
|
||||
/// Vector4::new(0.0, 0.0, 3.0, 3.0),
|
||||
/// ];
|
||||
///
|
||||
/// let mut curve = NurbsCurve::new(BSplineCurve::new(knot_vec, control_points));
|
||||
/// let mut flag = false;
|
||||
/// for i in 0..N {
|
||||
/// let t = 4.0 * (i as f64) / (N as f64);
|
||||
/// let pt0 = curve.subs(t);
|
||||
/// let pt1 = curve.subs(t + 1.0 / (N as f64));
|
||||
/// flag = flag || pt0.near(&pt1);
|
||||
/// }
|
||||
/// // There exists t such that bspcurve(t) == bspcurve(t + 0.01).
|
||||
/// assert!(flag);
|
||||
///
|
||||
/// curve.make_locally_injective().knot_normalize();
|
||||
/// let mut flag = false;
|
||||
/// for i in 0..N {
|
||||
/// let t = 1.0 * (i as f64) / (N as f64);
|
||||
/// let pt0 = curve.subs(t);
|
||||
/// let pt1 = curve.subs(t + 1.0 / (N as f64));
|
||||
/// flag = flag || pt0.near(&pt1);
|
||||
/// }
|
||||
/// // There does not exist t such that bspcurve(t) == bspcurve(t + 0.01).
|
||||
/// assert!(!flag);
|
||||
/// ```
|
||||
/// # Remarks
|
||||
/// If `self` is a constant curve, then does nothing.
|
||||
/// ```
|
||||
/// use truck_geometry::prelude::*;
|
||||
/// let knot_vec = KnotVec::from(vec![0.0, 0.0, 0.0, 1.0, 2.0, 2.0, 2.0]);
|
||||
/// let ctrl_pts = vec![
|
||||
/// Vector3::new(1.0, 1.0, 1.0),
|
||||
/// Vector3::new(2.0, 2.0, 2.0),
|
||||
/// Vector3::new(3.0, 3.0, 3.0),
|
||||
/// Vector3::new(4.0, 4.0, 4.0),
|
||||
/// ];
|
||||
/// let mut curve = NurbsCurve::new(BSplineCurve::new(knot_vec, ctrl_pts));
|
||||
/// let org_curve = curve.clone();
|
||||
/// curve.make_locally_injective();
|
||||
/// assert_eq!(curve, org_curve);
|
||||
/// ```
|
||||
pub fn make_locally_injective(&mut self) -> &mut Self {
|
||||
let mut iter = self.0.bezier_decomposition().into_iter();
|
||||
while let Some(bezier) = iter.next().map(NurbsCurve::new) {
|
||||
if !bezier.is_const() {
|
||||
*self = bezier;
|
||||
break;
|
||||
}
|
||||
}
|
||||
let mut x = 0.0;
|
||||
for mut bezier in iter.map(NurbsCurve::new) {
|
||||
if bezier.is_const() {
|
||||
x += bezier.0.knot_vec.range_length();
|
||||
} else {
|
||||
let s0 = self.0.control_points.last().unwrap().weight();
|
||||
let s1 = bezier.0.control_points[0].weight();
|
||||
bezier
|
||||
.0
|
||||
.control_points
|
||||
.iter_mut()
|
||||
.for_each(move |vec| *vec *= s0 / s1);
|
||||
self.concat(bezier.knot_translate(-x));
|
||||
}
|
||||
}
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl<V: Homogeneous<f64> + ControlPoint<f64, Diff = V>> ParameterDivision1D for NurbsCurve<V>
|
||||
where V::Point: MetricSpace<Metric = f64> + HashGen<f64>
|
||||
{
|
||||
type Point = V::Point;
|
||||
#[inline(always)]
|
||||
fn parameter_division(&self, range: (f64, f64), tol: f64) -> (Vec<f64>, Vec<V::Point>) {
|
||||
algo::curve::parameter_division(self, range, tol)
|
||||
}
|
||||
}
|
||||
|
||||
impl<V: Homogeneous<f64> + ControlPoint<f64, Diff = V>> SearchNearestParameter<D1> for NurbsCurve<V>
|
||||
where
|
||||
V::Point: MetricSpace<Metric = f64>,
|
||||
<V::Point as EuclideanSpace>::Diff: InnerSpace + Tolerance,
|
||||
{
|
||||
type Point = V::Point;
|
||||
/// Searches the parameter `t` which minimize |self(t) - point| by Newton's method with initial guess `hint`.
|
||||
/// Returns `None` if the number of attempts exceeds `trial` i.e. if `trial == 0`, then the trial is only one time.
|
||||
/// # Examples
|
||||
/// ```
|
||||
/// use truck_geometry::prelude::*;
|
||||
///
|
||||
/// // Defines the half unit circle in x > 0.
|
||||
/// let knot_vec = KnotVec::bezier_knot(2);
|
||||
/// let ctrl_pts = vec![Vector3::new(0.0, -1.0, 1.0), Vector3::new(1.0, 0.0, 0.0), Vector3::new(0.0, 1.0, 1.0)];
|
||||
/// let curve = NurbsCurve::new(BSplineCurve::new(knot_vec, ctrl_pts));
|
||||
///
|
||||
/// // search rational nearest parameter
|
||||
/// let pt = Point2::new(1.0, 2.0);
|
||||
/// let hint = 0.8;
|
||||
/// let t = curve.search_nearest_parameter(pt, Some(hint), 100).unwrap();
|
||||
///
|
||||
/// // check the answer
|
||||
/// let res = curve.subs(t);
|
||||
/// let ans = Point2::from_vec(pt.to_vec().normalize());
|
||||
/// assert_near!(res, ans);
|
||||
/// ```
|
||||
/// # Remarks
|
||||
/// It may converge to a local solution depending on the hint.
|
||||
/// ```
|
||||
/// use truck_geometry::prelude::*;
|
||||
///
|
||||
/// // Same curve and point as above example
|
||||
/// let knot_vec = KnotVec::bezier_knot(2);
|
||||
/// let ctrl_pts = vec![Vector3::new(0.0, -1.0, 1.0), Vector3::new(1.0, 0.0, 0.0), Vector3::new(0.0, 1.0, 1.0)];
|
||||
/// let curve = NurbsCurve::new(BSplineCurve::new(knot_vec, ctrl_pts));
|
||||
/// let pt = Point2::new(1.0, 2.0);
|
||||
///
|
||||
/// // another hint
|
||||
/// let hint = 0.5;
|
||||
///
|
||||
/// // Newton's method is vibration divergent.
|
||||
/// assert!(curve.search_nearest_parameter(pt, Some(hint), 100).is_none());
|
||||
/// ```
|
||||
#[inline(always)]
|
||||
fn search_nearest_parameter<H: Into<SPHint1D>>(
|
||||
&self,
|
||||
point: V::Point,
|
||||
hint: H,
|
||||
trial: usize,
|
||||
) -> Option<f64> {
|
||||
let hint = match hint.into() {
|
||||
SPHint1D::Parameter(hint) => hint,
|
||||
SPHint1D::Range(x, y) => {
|
||||
algo::curve::presearch(self, point, (x, y), PRESEARCH_DIVISION)
|
||||
}
|
||||
SPHint1D::None => {
|
||||
algo::curve::presearch(self, point, self.range_tuple(), PRESEARCH_DIVISION)
|
||||
}
|
||||
};
|
||||
algo::curve::search_nearest_parameter(self, point, hint, trial)
|
||||
}
|
||||
}
|
||||
|
||||
impl<V: Homogeneous<f64> + ControlPoint<f64, Diff = V>> SearchParameter<D1> for NurbsCurve<V>
|
||||
where
|
||||
V::Point: MetricSpace<Metric = f64>,
|
||||
<V::Point as EuclideanSpace>::Diff: InnerSpace + Tolerance,
|
||||
{
|
||||
type Point = V::Point;
|
||||
#[inline(always)]
|
||||
fn search_parameter<H: Into<SPHint1D>>(
|
||||
&self,
|
||||
point: V::Point,
|
||||
hint: H,
|
||||
trial: usize,
|
||||
) -> Option<f64> {
|
||||
let hint = match hint.into() {
|
||||
SPHint1D::Parameter(hint) => hint,
|
||||
SPHint1D::Range(x, y) => {
|
||||
algo::curve::presearch(self, point, (x, y), PRESEARCH_DIVISION)
|
||||
}
|
||||
SPHint1D::None => {
|
||||
algo::curve::presearch(self, point, self.range_tuple(), PRESEARCH_DIVISION)
|
||||
}
|
||||
};
|
||||
algo::curve::search_parameter(self, point, hint, trial)
|
||||
}
|
||||
}
|
||||
|
||||
impl<V: Homogeneous<f64>> NurbsCurve<V>
|
||||
where V::Point: Bounded<Scalar = f64>
|
||||
{
|
||||
/// Returns the bounding box including all control points.
|
||||
#[inline(always)]
|
||||
pub fn roughly_bounding_box(&self) -> BoundingBox<V::Point> {
|
||||
self.0.control_points.iter().map(|p| p.to_point()).collect()
|
||||
}
|
||||
}
|
||||
|
||||
impl<V: Homogeneous<f64> + ControlPoint<f64, Diff = V>> ParametricCurve for NurbsCurve<V> {
|
||||
type Point = V::Point;
|
||||
type Vector = <V::Point as EuclideanSpace>::Diff;
|
||||
#[inline(always)]
|
||||
fn subs(&self, t: f64) -> Self::Point { self.0.subs(t).to_point() }
|
||||
#[inline(always)]
|
||||
fn der(&self, t: f64) -> Self::Vector {
|
||||
let pt = self.0.subs(t);
|
||||
let der = self.0.der(t);
|
||||
pt.rat_der(der)
|
||||
}
|
||||
#[inline(always)]
|
||||
fn der2(&self, t: f64) -> Self::Vector {
|
||||
let pt = self.0.subs(t);
|
||||
let der = self.0.der(t);
|
||||
let der2 = self.0.der2(t);
|
||||
pt.rat_der2(der, der2)
|
||||
}
|
||||
#[inline(always)]
|
||||
fn parameter_range(&self) -> ParameterRange {
|
||||
(
|
||||
Bound::Included(self.0.knot_vec[0]),
|
||||
Bound::Included(self.0.knot_vec[self.0.knot_vec.len() - 1]),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl<V: Homogeneous<f64> + ControlPoint<f64, Diff = V>> BoundedCurve for NurbsCurve<V> {}
|
||||
|
||||
impl<V: Clone> Invertible for NurbsCurve<V> {
|
||||
#[inline(always)]
|
||||
fn invert(&mut self) { self.invert(); }
|
||||
#[inline(always)]
|
||||
fn inverse(&self) -> Self {
|
||||
let mut curve = self.0.clone();
|
||||
curve.invert();
|
||||
NurbsCurve(curve)
|
||||
}
|
||||
}
|
||||
|
||||
impl<M, V: Copy> Transformed<M> for NurbsCurve<V>
|
||||
where M: Copy + std::ops::Mul<V, Output = V>
|
||||
{
|
||||
#[inline(always)]
|
||||
fn transform_by(&mut self, trans: M) {
|
||||
self.0
|
||||
.control_points
|
||||
.iter_mut()
|
||||
.for_each(move |v| *v = trans * *v)
|
||||
}
|
||||
}
|
||||
|
||||
impl<V: Homogeneous<f64>> From<BSplineCurve<V::Point>> for NurbsCurve<V> {
|
||||
fn from(bspcurve: BSplineCurve<V::Point>) -> NurbsCurve<V> {
|
||||
NurbsCurve::new(BSplineCurve::new_unchecked(
|
||||
bspcurve.knot_vec,
|
||||
bspcurve
|
||||
.control_points
|
||||
.into_iter()
|
||||
.map(V::from_point)
|
||||
.collect(),
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parameter_division() {
|
||||
let knot_vec = KnotVec::uniform_knot(2, 3);
|
||||
let ctrl_pts = vec![
|
||||
Vector4::new(0.0, 0.0, 0.0, 1.0),
|
||||
Vector4::new(2.0, 0.0, 0.0, 2.0),
|
||||
Vector4::new(0.0, 3.0, 0.0, 3.0),
|
||||
Vector4::new(0.0, 0.0, 2.0, 2.0),
|
||||
Vector4::new(1.0, 1.0, 1.0, 1.0),
|
||||
];
|
||||
let curve = NurbsCurve::new(BSplineCurve::new(knot_vec, ctrl_pts));
|
||||
let tol = 0.01;
|
||||
let (div, pts) = curve.parameter_division(curve.range_tuple(), tol * 0.5);
|
||||
let knot_vec = curve.knot_vec();
|
||||
assert_eq!(knot_vec[0], div[0]);
|
||||
assert_eq!(knot_vec.range_length(), div.last().unwrap() - div[0]);
|
||||
for i in 1..div.len() {
|
||||
let pt0 = curve.subs(div[i - 1]);
|
||||
assert_eq!(pt0, pts[i - 1]);
|
||||
let pt1 = curve.subs(div[i]);
|
||||
assert_eq!(pt1, pts[i]);
|
||||
let value_middle = pt0.midpoint(pt1);
|
||||
let param_middle = curve.subs((div[i - 1] + div[i]) / 2.0);
|
||||
let dist = value_middle.distance(param_middle);
|
||||
assert!(dist < tol, "large distance: {dist}");
|
||||
}
|
||||
}
|
||||
+920
@@ -0,0 +1,920 @@
|
||||
use super::*;
|
||||
|
||||
impl<V> NurbsSurface<V> {
|
||||
/// constructor
|
||||
#[inline(always)]
|
||||
pub const fn new(bspsurface: BSplineSurface<V>) -> Self { NurbsSurface(bspsurface) }
|
||||
|
||||
/// Returns the nurbs surface before rationalized
|
||||
#[inline(always)]
|
||||
pub const fn non_rationalized(&self) -> &BSplineSurface<V> { &self.0 }
|
||||
/// Returns the nurbs surface before rationalized
|
||||
#[inline(always)]
|
||||
pub fn non_rationalized_mut(&mut self) -> &mut BSplineSurface<V> { &mut self.0 }
|
||||
|
||||
/// Returns the nurbs surface before rationalized
|
||||
#[inline(always)]
|
||||
pub fn into_non_rationalized(self) -> BSplineSurface<V> { self.0 }
|
||||
|
||||
/// Returns the reference of the knot vectors
|
||||
#[inline(always)]
|
||||
pub const fn knot_vecs(&self) -> &(KnotVec, KnotVec) { &self.0.knot_vecs }
|
||||
|
||||
/// Returns the u knot vector.
|
||||
#[inline(always)]
|
||||
pub const fn uknot_vec(&self) -> &KnotVec { &self.0.knot_vecs.0 }
|
||||
/// Returns the v knot vector.
|
||||
#[inline(always)]
|
||||
pub const fn vknot_vec(&self) -> &KnotVec { &self.0.knot_vecs.1 }
|
||||
|
||||
/// Returns the `idx`th u knot.
|
||||
#[inline(always)]
|
||||
pub fn uknot(&self, idx: usize) -> f64 { self.0.knot_vecs.0[idx] }
|
||||
/// returns the `idx`th v knot.
|
||||
#[inline(always)]
|
||||
pub fn vknot(&self, idx: usize) -> f64 { self.0.knot_vecs.1[idx] }
|
||||
|
||||
/// Returns the reference of the vector of the control points
|
||||
#[inline(always)]
|
||||
pub const fn control_points(&self) -> &Vec<Vec<V>> { &self.0.control_points }
|
||||
|
||||
/// Returns the reference of the control point corresponding to the index `(idx0, idx1)`.
|
||||
#[inline(always)]
|
||||
pub fn control_point(&self, idx0: usize, idx1: usize) -> &V {
|
||||
&self.0.control_points[idx0][idx1]
|
||||
}
|
||||
|
||||
/// Apply the given transformation to all control points.
|
||||
#[inline(always)]
|
||||
pub fn transform_control_points<F: FnMut(&mut V)>(&mut self, f: F) {
|
||||
self.0.transform_control_points(f)
|
||||
}
|
||||
|
||||
/// Returns the iterator over the control points in the `column_idx`th row.
|
||||
/// # Examples
|
||||
/// ```
|
||||
/// use truck_geometry::prelude::*;
|
||||
/// let uknot_vec = KnotVec::bezier_knot(1);
|
||||
/// let vknot_vec = KnotVec::bezier_knot(2);
|
||||
/// let knot_vecs = (uknot_vec, vknot_vec);
|
||||
/// let ctrl_pts = vec![
|
||||
/// vec![Vector3::new(0.0, 0.0, 0.0), Vector3::new(1.0, 0.0, 1.0), Vector3::new(2.0, 0.0, 2.0)],
|
||||
/// vec![Vector3::new(0.0, 1.0, 0.0), Vector3::new(1.0, 1.0, 1.0), Vector3::new(2.0, 1.0, 2.0)],
|
||||
/// ];
|
||||
/// let bspsurface = BSplineSurface::new(knot_vecs, ctrl_pts);
|
||||
/// let mut iter = bspsurface.ctrl_pts_row_iter(1);
|
||||
/// assert_eq!(iter.next(), Some(&Vector3::new(1.0, 0.0, 1.0)));
|
||||
/// assert_eq!(iter.next(), Some(&Vector3::new(1.0, 1.0, 1.0)));
|
||||
/// assert_eq!(iter.next(), None);
|
||||
/// ```
|
||||
#[inline(always)]
|
||||
pub fn ctrl_pts_row_iter(
|
||||
&self,
|
||||
column_idx: usize,
|
||||
) -> impl ExactSizeIterator<Item = &V> + std::iter::FusedIterator<Item = &V> {
|
||||
self.0.ctrl_pts_row_iter(column_idx)
|
||||
}
|
||||
|
||||
/// Returns the iterator over the control points in the `row_idx`th row.
|
||||
/// # Examples
|
||||
/// ```
|
||||
/// use truck_geometry::prelude::*;
|
||||
/// let uknot_vec = KnotVec::bezier_knot(1);
|
||||
/// let vknot_vec = KnotVec::bezier_knot(2);
|
||||
/// let knot_vecs = (uknot_vec, vknot_vec);
|
||||
/// let ctrl_pts = vec![
|
||||
/// vec![Vector3::new(0.0, 0.0, 0.0), Vector3::new(1.0, 0.0, 1.0), Vector3::new(2.0, 0.0, 2.0)],
|
||||
/// vec![Vector3::new(0.0, 1.0, 0.0), Vector3::new(1.0, 1.0, 1.0), Vector3::new(2.0, 1.0, 2.0)],
|
||||
/// ];
|
||||
/// let bspsurface = BSplineSurface::new(knot_vecs, ctrl_pts);
|
||||
/// let mut iter = bspsurface.ctrl_pts_column_iter(1);
|
||||
/// assert_eq!(iter.next(), Some(&Vector3::new(0.0, 1.0, 0.0)));
|
||||
/// assert_eq!(iter.next(), Some(&Vector3::new(1.0, 1.0, 1.0)));
|
||||
/// assert_eq!(iter.next(), Some(&Vector3::new(2.0, 1.0, 2.0)));
|
||||
/// assert_eq!(iter.next(), None);
|
||||
/// ```
|
||||
#[inline(always)]
|
||||
pub fn ctrl_pts_column_iter(&self, row_idx: usize) -> std::slice::Iter<'_, V> {
|
||||
self.0.control_points[row_idx].iter()
|
||||
}
|
||||
|
||||
/// Returns the mutable reference of the control point corresponding to index `(idx0, idx1)`.
|
||||
#[inline(always)]
|
||||
pub fn control_point_mut(&mut self, idx0: usize, idx1: usize) -> &mut V {
|
||||
&mut self.0.control_points[idx0][idx1]
|
||||
}
|
||||
|
||||
/// Returns the iterator on all control points
|
||||
#[inline(always)]
|
||||
pub fn control_points_mut(&mut self) -> impl Iterator<Item = &mut V> {
|
||||
self.0.control_points.iter_mut().flatten()
|
||||
}
|
||||
/// Returns the degrees of B-spline surface
|
||||
/// # Examples
|
||||
/// ```
|
||||
/// use truck_geometry::prelude::*;
|
||||
/// let uknot_vec = KnotVec::from(vec![0.0, 0.0, 1.0, 1.0]);
|
||||
/// let vknot_vec = KnotVec::from(vec![0.0, 0.0, 0.0, 1.0, 1.0, 1.0]);
|
||||
/// let knot_vecs = (uknot_vec, vknot_vec);
|
||||
/// let ctrl_pts = vec![
|
||||
/// vec![Vector3::new(0.0, 0.0, 0.0), Vector3::new(1.0, 0.0, 1.0), Vector3::new(2.0, 0.0, 2.0)],
|
||||
/// vec![Vector3::new(0.0, 1.0, 0.0), Vector3::new(1.0, 1.0, 1.0), Vector3::new(2.0, 1.0, 2.0)],
|
||||
/// ];
|
||||
/// let bspsurface = BSplineSurface::new(knot_vecs, ctrl_pts);
|
||||
/// assert_eq!(bspsurface.udegree(), 1);
|
||||
/// ```
|
||||
#[inline(always)]
|
||||
pub fn udegree(&self) -> usize { self.0.udegree() }
|
||||
|
||||
/// Returns the degrees of B-spline surface
|
||||
/// # Examples
|
||||
/// ```
|
||||
/// use truck_geometry::prelude::*;
|
||||
/// let uknot_vec = KnotVec::from(vec![0.0, 0.0, 1.0, 1.0]);
|
||||
/// let vknot_vec = KnotVec::from(vec![0.0, 0.0, 0.0, 1.0, 1.0, 1.0]);
|
||||
/// let knot_vecs = (uknot_vec, vknot_vec);
|
||||
/// let ctrl_pts = vec![
|
||||
/// vec![Vector3::new(0.0, 0.0, 0.0), Vector3::new(1.0, 0.0, 1.0), Vector3::new(2.0, 0.0, 2.0)],
|
||||
/// vec![Vector3::new(0.0, 1.0, 0.0), Vector3::new(1.0, 1.0, 1.0), Vector3::new(2.0, 1.0, 2.0)],
|
||||
/// ];
|
||||
/// let bspsurface = BSplineSurface::new(knot_vecs, ctrl_pts);
|
||||
/// assert_eq!(bspsurface.vdegree(), 2);
|
||||
/// ```
|
||||
#[inline(always)]
|
||||
pub fn vdegree(&self) -> usize { self.0.vdegree() }
|
||||
|
||||
/// Returns the degrees of B-spline surface
|
||||
/// # Examples
|
||||
/// ```
|
||||
/// use truck_geometry::prelude::*;
|
||||
/// let uknot_vec = KnotVec::from(vec![0.0, 0.0, 1.0, 1.0]);
|
||||
/// let vknot_vec = KnotVec::from(vec![0.0, 0.0, 0.0, 1.0, 1.0, 1.0]);
|
||||
/// let knot_vecs = (uknot_vec, vknot_vec);
|
||||
/// let ctrl_pts = vec![
|
||||
/// vec![Vector3::new(0.0, 0.0, 0.0), Vector3::new(1.0, 0.0, 1.0), Vector3::new(2.0, 0.0, 2.0)],
|
||||
/// vec![Vector3::new(0.0, 1.0, 0.0), Vector3::new(1.0, 1.0, 1.0), Vector3::new(2.0, 1.0, 2.0)],
|
||||
/// ];
|
||||
/// let bspsurface = BSplineSurface::new(knot_vecs, ctrl_pts);
|
||||
/// assert_eq!(bspsurface.degrees(), (1, 2));
|
||||
/// ```
|
||||
#[inline(always)]
|
||||
pub fn degrees(&self) -> (usize, usize) { (self.udegree(), self.vdegree()) }
|
||||
/// Returns whether the knot vectors are clamped or not.
|
||||
#[inline(always)]
|
||||
pub fn is_clamped(&self) -> bool { self.0.is_clamped() }
|
||||
/// Swaps two parameters.
|
||||
pub fn swap_axes(&mut self) -> &mut Self
|
||||
where V: Clone {
|
||||
self.0.swap_axes();
|
||||
self
|
||||
}
|
||||
/// The range of the parameter of the surface.
|
||||
#[inline(always)]
|
||||
pub fn parameter_range(&self) -> (ParameterRange, ParameterRange) { self.0.parameter_range() }
|
||||
/// Creates the curve whose control points are the `idx`th column control points of `self`.
|
||||
#[inline(always)]
|
||||
pub fn column_curve(&self, row_idx: usize) -> NurbsCurve<V>
|
||||
where V: Clone {
|
||||
NurbsCurve(self.0.column_curve(row_idx))
|
||||
}
|
||||
/// Creates the column sectional curve.
|
||||
#[inline(always)]
|
||||
pub fn row_curve(&self, column_idx: usize) -> NurbsCurve<V>
|
||||
where V: Clone {
|
||||
NurbsCurve(self.0.row_curve(column_idx))
|
||||
}
|
||||
}
|
||||
|
||||
impl<V: Homogeneous<f64>> NurbsSurface<V> {
|
||||
/// Constructs a rationalization surface from the non-rationalized surface and weights.
|
||||
/// # Failures
|
||||
/// the length of `surface.control_points()` and `weights` must be the same.
|
||||
#[inline(always)]
|
||||
pub fn try_from_bspline_and_weights(
|
||||
surface: BSplineSurface<V::Point>,
|
||||
weights: Vec<Vec<f64>>,
|
||||
) -> Result<Self> {
|
||||
let BSplineSurface {
|
||||
knot_vecs,
|
||||
control_points,
|
||||
} = surface;
|
||||
if control_points.len() != weights.len() {
|
||||
return Err(Error::DifferentLength);
|
||||
}
|
||||
let control_points = control_points
|
||||
.into_iter()
|
||||
.zip(weights)
|
||||
.map(|(control_points, weights)| {
|
||||
if control_points.len() != weights.len() {
|
||||
return Err(Error::DifferentLength);
|
||||
}
|
||||
Ok(control_points
|
||||
.into_iter()
|
||||
.zip(weights)
|
||||
.map(|(pt, w)| V::from_point_weight(pt, w))
|
||||
.collect::<Vec<_>>())
|
||||
})
|
||||
.collect::<Result<Vec<_>>>()?;
|
||||
Ok(Self(BSplineSurface::new_unchecked(
|
||||
knot_vecs,
|
||||
control_points,
|
||||
)))
|
||||
}
|
||||
}
|
||||
|
||||
impl<V: Homogeneous<f64> + ControlPoint<f64, Diff = V>> NurbsSurface<V> {
|
||||
/// Substitutes to a NURBS surface.
|
||||
#[inline(always)]
|
||||
pub fn subs(&self, u: f64, v: f64) -> V::Point { self.0.subs(u, v).to_point() }
|
||||
/// Substitutes derived NURBS surface by the first parameter `u`.
|
||||
#[inline(always)]
|
||||
pub fn uder(&self, u: f64, v: f64) -> <V::Point as EuclideanSpace>::Diff {
|
||||
let pt = self.0.subs(u, v);
|
||||
let ud = self.0.uder(u, v);
|
||||
pt.rat_der(ud)
|
||||
}
|
||||
/// Substitutes derived NURBS surface by the first parameter `v`.
|
||||
#[inline(always)]
|
||||
pub fn vder(&self, u: f64, v: f64) -> <V::Point as EuclideanSpace>::Diff {
|
||||
let pt = self.0.subs(u, v);
|
||||
let vd = self.0.vder(u, v);
|
||||
pt.rat_der(vd)
|
||||
}
|
||||
/// Substitutes 2nd-ord derived NURBS surface by the first parameter `u`.
|
||||
#[inline(always)]
|
||||
pub fn uuder(&self, u: f64, v: f64) -> <V::Point as EuclideanSpace>::Diff {
|
||||
let pt = self.0.subs(u, v);
|
||||
let ud = self.0.uder(u, v);
|
||||
let uud = self.0.uuder(u, v);
|
||||
pt.rat_der2(ud, uud)
|
||||
}
|
||||
/// Substitutes 2nd-ord derived NURBS surface by the first parameter `v`.
|
||||
#[inline(always)]
|
||||
pub fn vvder(&self, u: f64, v: f64) -> <V::Point as EuclideanSpace>::Diff {
|
||||
let pt = self.0.subs(u, v);
|
||||
let vd = self.0.vder(u, v);
|
||||
let vvd = self.0.vvder(u, v);
|
||||
pt.rat_der2(vd, vvd)
|
||||
}
|
||||
/// Substitutes 2nd-ord derived NURBS surface by both parameter `u, v`.
|
||||
#[inline(always)]
|
||||
pub fn uvder(&self, u: f64, v: f64) -> <V::Point as EuclideanSpace>::Diff {
|
||||
let pt = self.0.subs(u, v);
|
||||
let ud = self.0.uder(u, v);
|
||||
let vd = self.0.vder(u, v);
|
||||
let uvd = self.0.uvder(u, v);
|
||||
pt.rat_cross_der(ud, vd, uvd)
|
||||
}
|
||||
/// Returns the closure of substitution.
|
||||
#[inline(always)]
|
||||
pub fn get_closure(&self) -> impl Fn(f64, f64) -> V::Point + '_ { move |u, v| self.subs(u, v) }
|
||||
}
|
||||
|
||||
impl<V: Homogeneous<f64> + ControlPoint<f64, Diff = V>> NurbsSurface<V>
|
||||
where V::Point: Tolerance
|
||||
{
|
||||
/// Returns whether constant curve or not, i.e. all control points are same or not.
|
||||
/// # Examples
|
||||
/// ```
|
||||
/// use truck_geometry::prelude::*;
|
||||
/// let uknot_vec = KnotVec::bezier_knot(1);
|
||||
/// let vknot_vec = KnotVec::bezier_knot(2);
|
||||
/// let pt = Vector3::new(1.0, 2.0, 1.0);
|
||||
/// // allows differences upto scalars
|
||||
/// let ctrl_pts = vec![
|
||||
/// vec![pt.clone(), pt.clone() * 2.0, pt.clone() * 3.0],
|
||||
/// vec![pt.clone() * 0.5, pt.clone() * 0.25, pt.clone() * 0.125],
|
||||
/// ];
|
||||
/// let mut surface = NurbsSurface::new(BSplineSurface::new((uknot_vec, vknot_vec), ctrl_pts));
|
||||
/// assert!(surface.is_const());
|
||||
///
|
||||
/// *surface.control_point_mut(1, 2) = Vector3::new(2.0, 3.0, 1.0);
|
||||
/// assert!(!surface.is_const());
|
||||
/// ```
|
||||
#[inline(always)]
|
||||
pub fn is_const(&self) -> bool {
|
||||
let pt = self.0.control_points[0][0].to_point();
|
||||
for vec in self.0.control_points.iter().flat_map(|pts| pts.iter()) {
|
||||
if !vec.to_point().near(&pt) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
true
|
||||
}
|
||||
/// Determines whether `self` and `other` is near as the B-spline rational surfaces or not.
|
||||
///
|
||||
/// Divides each knot domain into the number of degree equal parts,
|
||||
/// and check `|self(u, v) - other(u, v)| < TOLERANCE` for each end points `(u, v)`.
|
||||
/// # Examples
|
||||
/// ```
|
||||
/// use truck_geometry::prelude::*;
|
||||
/// let knot_vecs = (KnotVec::bezier_knot(3), KnotVec::bezier_knot(2));
|
||||
/// let ctrl_pts = vec![
|
||||
/// vec![Vector3::new(0.0, 0.0, 1.0), Vector3::new(0.5, -1.0, 2.0), Vector3::new(1.0, 0.0, 1.0)],
|
||||
/// vec![Vector3::new(0.0, 1.0, 1.0), Vector3::new(0.5, 1.0, 1.0), Vector3::new(1.0, 1.0, 1.0)],
|
||||
/// vec![Vector3::new(0.0, 2.0, 1.0), Vector3::new(0.5, 2.0, 3.0), Vector3::new(1.0, 2.0, 1.0)],
|
||||
/// vec![Vector3::new(0.0, 3.0, 1.0), Vector3::new(0.5, 3.5, 2.0), Vector3::new(1.0, 3.0, 1.0)],
|
||||
/// ];
|
||||
/// let surface0 = NurbsSurface::new(BSplineSurface::new(knot_vecs, ctrl_pts));
|
||||
/// let mut surface1 = surface0.clone();
|
||||
/// assert!(surface0.near_as_surface(&surface1));
|
||||
///
|
||||
/// *surface1.control_point_mut(1, 1) = Vector3::new(0.5, 1.0, 0.9);
|
||||
/// assert!(!surface0.near_as_surface(&surface1));
|
||||
/// ```
|
||||
#[inline(always)]
|
||||
pub fn near_as_surface(&self, other: &Self) -> bool {
|
||||
self.0
|
||||
.sub_near_as_surface(&other.0, 2, move |x, y| x.to_point().near(&y.to_point()))
|
||||
}
|
||||
/// Determines whether `self` and `other` is near in square order as the B-spline rational
|
||||
/// surfaces or not.
|
||||
///
|
||||
/// Divides each knot domain into the number of degree equal parts,
|
||||
/// and check `|self(u, v) - other(u, v)| < TOLERANCE` for each end points `(u, v)`.
|
||||
/// # Examples
|
||||
/// ```
|
||||
/// use truck_geometry::prelude::*;
|
||||
/// let eps = TOLERANCE;
|
||||
/// let knot_vecs = (KnotVec::bezier_knot(3), KnotVec::bezier_knot(2));
|
||||
/// let ctrl_pts = vec![
|
||||
/// vec![Vector3::new(0.0, 0.0, 1.0), Vector3::new(0.5, -1.0, 2.0), Vector3::new(1.0, 0.0, 1.0)],
|
||||
/// vec![Vector3::new(0.0, 1.0, 1.0), Vector3::new(0.5, 1.0, 1.0), Vector3::new(1.0, 1.0, 1.0)],
|
||||
/// vec![Vector3::new(0.0, 2.0, 1.0), Vector3::new(0.5, 2.0, 3.0), Vector3::new(1.0, 2.0, 1.0)],
|
||||
/// vec![Vector3::new(0.0, 3.0, 1.0), Vector3::new(0.5, 3.5, 2.0), Vector3::new(1.0, 3.0, 1.0)],
|
||||
/// ];
|
||||
/// let surface0 = NurbsSurface::new(BSplineSurface::new(knot_vecs, ctrl_pts));
|
||||
/// let mut surface1 = surface0.clone();
|
||||
/// assert!(surface0.near_as_surface(&surface1));
|
||||
///
|
||||
/// *surface1.control_point_mut(1, 1) = Vector3::new(0.5, 1.0, 1.0 - eps);
|
||||
/// assert!(surface0.near_as_surface(&surface1));
|
||||
/// assert!(!surface0.near2_as_surface(&surface1));
|
||||
/// ```
|
||||
#[inline(always)]
|
||||
pub fn near2_as_surface(&self, other: &Self) -> bool {
|
||||
self.0
|
||||
.sub_near_as_surface(&other.0, 2, move |x, y| x.to_point().near2(&y.to_point()))
|
||||
}
|
||||
}
|
||||
|
||||
impl<V: Homogeneous<f64> + ControlPoint<f64, Diff = V> + Tolerance> NurbsSurface<V> {
|
||||
/// Adds a knot `x` of the first parameter `u`, and do not change `self` as a surface.
|
||||
#[inline(always)]
|
||||
pub fn add_uknot(&mut self, x: f64) -> &mut Self {
|
||||
self.0.add_uknot(x);
|
||||
self
|
||||
}
|
||||
/// Adds a knot `x` of the first parameter `u`, and do not change `self` as a surface.
|
||||
#[inline(always)]
|
||||
pub fn add_vknot(&mut self, x: f64) -> &mut Self {
|
||||
self.0.add_vknot(x);
|
||||
self
|
||||
}
|
||||
/// Removes the uknot corresponding to the indice `idx`, and do not change `self` as a curve.
|
||||
/// If the knot cannot be removed, returns [`Error::CannotRemoveKnot`].
|
||||
#[inline(always)]
|
||||
pub fn try_remove_uknot(&mut self, idx: usize) -> Result<&mut Self> {
|
||||
match self.0.try_remove_uknot(idx) {
|
||||
Ok(_) => Ok(self),
|
||||
Err(error) => Err(error),
|
||||
}
|
||||
}
|
||||
/// Removes the uknot corresponding to the indices `idx`, and do not change `self` as a curve.
|
||||
/// If cannot remove the knot, do not change `self` and return `self`.
|
||||
#[inline(always)]
|
||||
pub fn remove_uknot(&mut self, idx: usize) -> &mut Self {
|
||||
self.0.remove_uknot(idx);
|
||||
self
|
||||
}
|
||||
/// Removes the uknot corresponding to the indice `idx`, and do not change `self` as a curve.
|
||||
/// If the knot cannot be removed, returns [`Error::CannotRemoveKnot`].
|
||||
#[inline(always)]
|
||||
pub fn try_remove_vknot(&mut self, idx: usize) -> Result<&mut Self> {
|
||||
match self.0.try_remove_vknot(idx) {
|
||||
Ok(_) => Ok(self),
|
||||
Err(error) => Err(error),
|
||||
}
|
||||
}
|
||||
/// Removes the uknot corresponding to the indices `idx`, and do not change `self` as a curve.
|
||||
/// If cannot remove the knot, do not change `self` and return `self`.
|
||||
#[inline(always)]
|
||||
pub fn remove_vknot(&mut self, idx: usize) -> &mut Self {
|
||||
self.0.remove_vknot(idx);
|
||||
self
|
||||
}
|
||||
/// Elevates the udegree.
|
||||
#[inline(always)]
|
||||
pub fn elevate_udegree(&mut self) -> &mut Self {
|
||||
self.0.elevate_udegree();
|
||||
self
|
||||
}
|
||||
/// Elevates the vdegree.
|
||||
#[inline(always)]
|
||||
pub fn elevate_vdegree(&mut self) -> &mut Self {
|
||||
self.0.elevate_vdegree();
|
||||
self
|
||||
}
|
||||
/// Aligns the udegree with the same degrees.
|
||||
#[inline(always)]
|
||||
pub fn syncro_uvdegrees(&mut self) -> &mut Self {
|
||||
self.0.syncro_uvdegrees();
|
||||
self
|
||||
}
|
||||
/// Makes the uknot vector and the vknot vector the same knot vector.
|
||||
#[inline(always)]
|
||||
pub fn syncro_uvknots(&mut self) -> &mut Self {
|
||||
self.0.syncro_uvknots();
|
||||
self
|
||||
}
|
||||
|
||||
/// Cuts the surface into two surfaces at the parameter `u`
|
||||
#[inline(always)]
|
||||
pub fn ucut(&mut self, u: f64) -> Self { Self::new(self.0.ucut(u)) }
|
||||
|
||||
/// Cuts the surface into two surfaces at the parameter `v`
|
||||
#[inline(always)]
|
||||
pub fn vcut(&mut self, v: f64) -> Self { Self::new(self.0.vcut(v)) }
|
||||
|
||||
/// Normalizes the knot vectors
|
||||
#[inline(always)]
|
||||
pub fn knot_normalize(&mut self) -> &mut Self {
|
||||
self.0.knot_normalize();
|
||||
self
|
||||
}
|
||||
/// Translates the knot vectors.
|
||||
#[inline(always)]
|
||||
pub fn knot_translate(&mut self, x: f64, y: f64) -> &mut Self {
|
||||
self.0.knot_translate(x, y);
|
||||
self
|
||||
}
|
||||
|
||||
/// Removes knots in order from the back
|
||||
#[inline(always)]
|
||||
pub fn optimize(&mut self) -> &mut Self {
|
||||
self.0.optimize();
|
||||
self
|
||||
}
|
||||
|
||||
/// Get the boundary by four splitted curves.
|
||||
/// # Example
|
||||
/// ```
|
||||
/// use truck_geometry::prelude::*;
|
||||
/// let knot_vecs = (KnotVec::bezier_knot(3), KnotVec::bezier_knot(2));
|
||||
/// let ctrl_pts = vec![
|
||||
/// vec![Vector3::new(0.0, 0.0, 1.0), Vector3::new(0.5, -1.0, 2.0), Vector3::new(1.0, 0.0, 1.0)],
|
||||
/// vec![Vector3::new(0.0, 1.0, 2.0), Vector3::new(0.5, 1.0, 3.0), Vector3::new(1.0, 1.0, 2.0)],
|
||||
/// vec![Vector3::new(0.0, 2.0, 2.0), Vector3::new(0.5, 2.0, 3.0), Vector3::new(1.0, 2.0, 2.0)],
|
||||
/// vec![Vector3::new(0.0, 3.0, 1.0), Vector3::new(0.5, 3.5, 2.0), Vector3::new(1.0, 3.0, 1.0)],
|
||||
/// ];
|
||||
/// let bspsurface = NurbsSurface::new(BSplineSurface::new(knot_vecs, ctrl_pts));
|
||||
/// let curves = bspsurface.splitted_boundary();
|
||||
/// assert_eq!(
|
||||
/// curves[0].control_points(),
|
||||
/// &vec![
|
||||
/// Vector3::new(0.0, 0.0, 1.0),
|
||||
/// Vector3::new(0.0, 1.0, 2.0),
|
||||
/// Vector3::new(0.0, 2.0, 2.0),
|
||||
/// Vector3::new(0.0, 3.0, 1.0),
|
||||
/// ],
|
||||
/// );
|
||||
/// assert_eq!(
|
||||
/// curves[1].control_points(),
|
||||
/// &vec![
|
||||
/// Vector3::new(0.0, 3.0, 1.0),
|
||||
/// Vector3::new(0.5, 3.5, 2.0),
|
||||
/// Vector3::new(1.0, 3.0, 1.0),
|
||||
/// ],
|
||||
/// );
|
||||
/// assert_eq!(
|
||||
/// curves[2].control_points(),
|
||||
/// &vec![
|
||||
/// Vector3::new(1.0, 3.0, 1.0),
|
||||
/// Vector3::new(1.0, 2.0, 2.0),
|
||||
/// Vector3::new(1.0, 1.0, 2.0),
|
||||
/// Vector3::new(1.0, 0.0, 1.0),
|
||||
/// ],
|
||||
/// );
|
||||
/// assert_eq!(
|
||||
/// curves[3].control_points(),
|
||||
/// &vec![
|
||||
/// Vector3::new(1.0, 0.0, 1.0),
|
||||
/// Vector3::new(0.5, -1.0, 2.0),
|
||||
/// Vector3::new(0.0, 0.0, 1.0),
|
||||
/// ],
|
||||
/// );
|
||||
/// ```
|
||||
#[inline(always)]
|
||||
pub fn splitted_boundary(&self) -> [NurbsCurve<V>; 4] {
|
||||
TryFrom::try_from(
|
||||
self.0
|
||||
.splitted_boundary()
|
||||
.iter()
|
||||
.cloned()
|
||||
.map(NurbsCurve::new)
|
||||
.collect::<Vec<_>>(),
|
||||
)
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
/// Extracts the boundary of surface
|
||||
#[inline(always)]
|
||||
pub fn boundary(&self) -> NurbsCurve<V> { NurbsCurve::new(self.0.boundary()) }
|
||||
}
|
||||
|
||||
impl<V: Homogeneous<f64>> SearchNearestParameter<D2> for NurbsSurface<V>
|
||||
where
|
||||
Self: ParametricSurface<Point = V::Point, Vector = <V::Point as EuclideanSpace>::Diff>,
|
||||
V::Point: EuclideanSpace<Scalar = f64> + MetricSpace<Metric = f64>,
|
||||
<V::Point as EuclideanSpace>::Diff: InnerSpace<Scalar = f64> + Tolerance,
|
||||
{
|
||||
type Point = V::Point;
|
||||
/// Searches the parameter `(u, v)` which minimize `|self(u, v) - point|` by Newton's method
|
||||
/// with initial guess `(u0, v0)`. If the repeated trial does not converge, then returns `None`.
|
||||
/// # Examples
|
||||
/// ```
|
||||
/// use truck_geometry::prelude::*;
|
||||
/// let knot_vecs = (KnotVec::bezier_knot(3), KnotVec::bezier_knot(2));
|
||||
/// let ctrl_pts = vec![
|
||||
/// vec![Vector3::new(0.0, 0.0, 1.0), Vector3::new(1.0, -2.0, 2.0), Vector3::new(1.0, 0.0, 1.0)],
|
||||
/// vec![Vector3::new(0.0, 2.0, 2.0), Vector3::new(2.0, 4.0, 4.0), Vector3::new(2.0, 2.0, 2.0)],
|
||||
/// vec![Vector3::new(0.0, 4.0, 2.0), Vector3::new(2.0, 8.0, 4.0), Vector3::new(2.0, 4.0, 2.0)],
|
||||
/// vec![Vector3::new(0.0, 3.0, 1.0), Vector3::new(1.0, 7.0, 2.0), Vector3::new(1.0, 3.0, 1.0)],
|
||||
/// ];
|
||||
/// let surface = NurbsSurface::new(BSplineSurface::new(knot_vecs, ctrl_pts));
|
||||
/// let pt = surface.subs(0.3, 0.7);
|
||||
/// let (u, v) = surface.search_nearest_parameter(pt, Some((0.5, 0.5)), 100).unwrap();
|
||||
/// assert!(u.near(&0.3) && v.near(&0.7));
|
||||
/// ```
|
||||
/// # Remarks
|
||||
/// It may converge to a local solution depending on the hint.
|
||||
/// cf. [`BSplineCurve::search_rational_nearest_parameter`](struct.BSplineCurve.html#method.search_rational_nearest_parameter)
|
||||
#[inline(always)]
|
||||
fn search_nearest_parameter<H: Into<SPHint2D>>(
|
||||
&self,
|
||||
point: V::Point,
|
||||
hint: H,
|
||||
trials: usize,
|
||||
) -> Option<(f64, f64)> {
|
||||
let hint = match hint.into() {
|
||||
SPHint2D::Parameter(x, y) => (x, y),
|
||||
SPHint2D::Range(range0, range1) => {
|
||||
algo::surface::presearch(self, point, (range0, range1), PRESEARCH_DIVISION)
|
||||
}
|
||||
SPHint2D::None => {
|
||||
algo::surface::presearch(self, point, self.range_tuple(), PRESEARCH_DIVISION)
|
||||
}
|
||||
};
|
||||
algo::surface::search_nearest_parameter(self, point, hint, trials)
|
||||
}
|
||||
}
|
||||
|
||||
impl<V: Homogeneous<f64>> NurbsSurface<V>
|
||||
where V::Point: Bounded<Scalar = f64>
|
||||
{
|
||||
/// Returns the bounding box including all control points.
|
||||
#[inline(always)]
|
||||
pub fn roughly_bounding_box(&self) -> BoundingBox<V::Point> {
|
||||
self.0
|
||||
.control_points
|
||||
.iter()
|
||||
.flatten()
|
||||
.map(|pt| pt.to_point())
|
||||
.collect()
|
||||
}
|
||||
}
|
||||
|
||||
impl SearchParameter<D2> for NurbsSurface<Vector3> {
|
||||
type Point = Point2;
|
||||
/// Search the parameter `(u, v)` such that `self.subs(u, v).rational_projection()` is near `pt`.
|
||||
/// If cannot find, then return `None`.
|
||||
/// # Examples
|
||||
/// ```
|
||||
/// use truck_geometry::prelude::*;
|
||||
/// let knot_vec = KnotVec::uniform_knot(2, 2);
|
||||
/// let ctrl_pts = vec![
|
||||
/// vec![Vector3::new(0.0, 0.0, 1.0), Vector3::new(0.1, 0.0, 0.5), Vector3::new(0.5, 0.0, 0.3), Vector3::new(1.0, 0.0, 1.0)],
|
||||
/// vec![Vector3::new(0.0, 0.1, 0.1), Vector3::new(0.2, 0.2, 0.1), Vector3::new(0.4, 0.3, 0.4), Vector3::new(1.0, 0.3, 0.7)],
|
||||
/// vec![Vector3::new(0.0, 0.5, 0.4), Vector3::new(0.3, 0.6, 0.5), Vector3::new(0.6, 0.4, 1.0), Vector3::new(1.0, 0.5, 0.4)],
|
||||
/// vec![Vector3::new(0.0, 1.0, 1.0), Vector3::new(0.1, 1.0, 1.0), Vector3::new(0.5, 1.0, 0.5), Vector3::new(1.0, 1.0, 0.3)],
|
||||
/// ];
|
||||
/// let bspsurface = BSplineSurface::new((knot_vec.clone(), knot_vec), ctrl_pts);
|
||||
/// let surface = NurbsSurface::new(bspsurface);
|
||||
///
|
||||
/// let pt = surface.subs(0.3, 0.7);
|
||||
/// let (u, v) = surface.search_parameter(pt, Some((0.5, 0.5)), 100).unwrap();
|
||||
/// assert_near!(surface.subs(u, v), pt);
|
||||
/// ```
|
||||
#[inline(always)]
|
||||
fn search_parameter<H: Into<SPHint2D>>(
|
||||
&self,
|
||||
point: Point2,
|
||||
hint: H,
|
||||
trials: usize,
|
||||
) -> Option<(f64, f64)> {
|
||||
let hint = match hint.into() {
|
||||
SPHint2D::Parameter(x, y) => (x, y),
|
||||
SPHint2D::Range(range0, range1) => {
|
||||
algo::surface::presearch(self, point, (range0, range1), PRESEARCH_DIVISION)
|
||||
}
|
||||
SPHint2D::None => {
|
||||
algo::surface::presearch(self, point, self.range_tuple(), PRESEARCH_DIVISION)
|
||||
}
|
||||
};
|
||||
algo::surface::search_parameter2d(self, point, hint, trials)
|
||||
}
|
||||
}
|
||||
|
||||
impl<V: Clone> Invertible for NurbsSurface<V> {
|
||||
#[inline(always)]
|
||||
fn invert(&mut self) { self.swap_axes(); }
|
||||
#[inline(always)]
|
||||
fn inverse(&self) -> Self {
|
||||
let mut surface = self.clone();
|
||||
surface.swap_axes();
|
||||
surface
|
||||
}
|
||||
}
|
||||
|
||||
impl<V: Homogeneous<f64> + ControlPoint<f64, Diff = V>> ParametricSurface for NurbsSurface<V> {
|
||||
type Point = V::Point;
|
||||
type Vector = <V::Point as EuclideanSpace>::Diff;
|
||||
#[inline(always)]
|
||||
fn subs(&self, u: f64, v: f64) -> Self::Point { self.subs(u, v) }
|
||||
#[inline(always)]
|
||||
fn uder(&self, u: f64, v: f64) -> Self::Vector { self.uder(u, v) }
|
||||
#[inline(always)]
|
||||
fn vder(&self, u: f64, v: f64) -> Self::Vector { self.vder(u, v) }
|
||||
#[inline(always)]
|
||||
fn uuder(&self, u: f64, v: f64) -> Self::Vector { self.uuder(u, v) }
|
||||
#[inline(always)]
|
||||
fn uvder(&self, u: f64, v: f64) -> Self::Vector { self.uvder(u, v) }
|
||||
#[inline(always)]
|
||||
fn vvder(&self, u: f64, v: f64) -> Self::Vector { self.vvder(u, v) }
|
||||
#[inline(always)]
|
||||
fn parameter_range(&self) -> (ParameterRange, ParameterRange) { self.parameter_range() }
|
||||
}
|
||||
|
||||
impl ParametricSurface3D for NurbsSurface<Vector4> {
|
||||
#[inline(always)]
|
||||
fn normal(&self, u: f64, v: f64) -> Vector3 {
|
||||
let pt = self.0.subs(u, v);
|
||||
let ud = self.0.uder(u, v);
|
||||
let vd = self.0.vder(u, v);
|
||||
pt.rat_der(ud).cross(pt.rat_der(vd)).normalize()
|
||||
}
|
||||
}
|
||||
|
||||
impl<V: Homogeneous<f64> + ControlPoint<f64, Diff = V>> ParameterDivision2D for NurbsSurface<V>
|
||||
where V::Point: MetricSpace<Metric = f64> + HashGen<f64>
|
||||
{
|
||||
#[inline(always)]
|
||||
fn parameter_division(
|
||||
&self,
|
||||
range: ((f64, f64), (f64, f64)),
|
||||
tol: f64,
|
||||
) -> (Vec<f64>, Vec<f64>) {
|
||||
algo::surface::parameter_division(self, range, tol)
|
||||
}
|
||||
}
|
||||
|
||||
impl<V> BoundedSurface for NurbsSurface<V> where Self: ParametricSurface {}
|
||||
|
||||
impl IncludeCurve<NurbsCurve<Vector3>> for NurbsSurface<Vector3> {
|
||||
#[inline(always)]
|
||||
fn include(&self, curve: &NurbsCurve<Vector3>) -> bool {
|
||||
let pt = curve.subs(curve.knot_vec()[0]);
|
||||
let mut hint = match self.search_parameter(pt, None, INCLUDE_CURVE_TRIALS) {
|
||||
Some(got) => got,
|
||||
None => return false,
|
||||
};
|
||||
let uknot_vec = self.uknot_vec();
|
||||
let vknot_vec = self.vknot_vec();
|
||||
let degree = curve.degree() * 6;
|
||||
let (knots, _) = curve.knot_vec().to_single_multi();
|
||||
for i in 1..knots.len() {
|
||||
for j in 1..=degree {
|
||||
let p = j as f64 / degree as f64;
|
||||
let t = knots[i - 1] * (1.0 - p) + knots[i] * p;
|
||||
let pt = curve.subs(t);
|
||||
hint = match self.search_parameter(pt, Some(hint), INCLUDE_CURVE_TRIALS) {
|
||||
Some(got) => got,
|
||||
None => return false,
|
||||
};
|
||||
if !self.subs(hint.0, hint.1).near(&pt)
|
||||
|| hint.0 < uknot_vec[0] - TOLERANCE
|
||||
|| hint.0 - uknot_vec[0] > uknot_vec.range_length() + TOLERANCE
|
||||
|| hint.1 < vknot_vec[0] - TOLERANCE
|
||||
|| hint.1 - vknot_vec[0] > vknot_vec.range_length() + TOLERANCE
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
impl IncludeCurve<BSplineCurve<Point3>> for NurbsSurface<Vector4> {
|
||||
#[inline(always)]
|
||||
fn include(&self, curve: &BSplineCurve<Point3>) -> bool {
|
||||
let pt = curve.front();
|
||||
let mut hint = match self.search_parameter(pt, None, INCLUDE_CURVE_TRIALS) {
|
||||
Some(got) => got,
|
||||
None => return false,
|
||||
};
|
||||
let uknot_vec = self.uknot_vec();
|
||||
let vknot_vec = self.vknot_vec();
|
||||
let degree = curve.degree() * 6;
|
||||
let (knots, _) = curve.knot_vec().to_single_multi();
|
||||
for i in 1..knots.len() {
|
||||
for j in 1..=degree {
|
||||
let p = j as f64 / degree as f64;
|
||||
let t = knots[i - 1] * (1.0 - p) + knots[i] * p;
|
||||
let pt = curve.subs(t);
|
||||
hint = match self.search_parameter(pt, Some(hint), INCLUDE_CURVE_TRIALS) {
|
||||
Some(got) => got,
|
||||
None => return false,
|
||||
};
|
||||
if !self.subs(hint.0, hint.1).near(&pt)
|
||||
|| hint.0 < uknot_vec[0] - TOLERANCE
|
||||
|| hint.0 - uknot_vec[0] > uknot_vec.range_length() + TOLERANCE
|
||||
|| hint.1 < vknot_vec[0] - TOLERANCE
|
||||
|| hint.1 - vknot_vec[0] > vknot_vec.range_length() + TOLERANCE
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
impl IncludeCurve<NurbsCurve<Vector4>> for NurbsSurface<Vector4> {
|
||||
#[inline(always)]
|
||||
fn include(&self, curve: &NurbsCurve<Vector4>) -> bool {
|
||||
let pt = curve.front();
|
||||
let mut hint = match self.search_parameter(pt, None, INCLUDE_CURVE_TRIALS) {
|
||||
Some(got) => got,
|
||||
None => return false,
|
||||
};
|
||||
let uknot_vec = self.uknot_vec();
|
||||
let vknot_vec = self.vknot_vec();
|
||||
let degree = curve.degree() * 6;
|
||||
let (knots, _) = curve.knot_vec().to_single_multi();
|
||||
for i in 1..knots.len() {
|
||||
for j in 1..=degree {
|
||||
let p = j as f64 / degree as f64;
|
||||
let t = knots[i - 1] * (1.0 - p) + knots[i] * p;
|
||||
let pt = curve.subs(t);
|
||||
hint = match self.search_parameter(pt, Some(hint), INCLUDE_CURVE_TRIALS) {
|
||||
Some(got) => got,
|
||||
None => return false,
|
||||
};
|
||||
if !self.subs(hint.0, hint.1).near(&pt)
|
||||
|| hint.0 < uknot_vec[0] - TOLERANCE
|
||||
|| hint.0 - uknot_vec[0] > uknot_vec.range_length() + TOLERANCE
|
||||
|| hint.1 < vknot_vec[0] - TOLERANCE
|
||||
|| hint.1 - vknot_vec[0] > vknot_vec.range_length() + TOLERANCE
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
impl<M, V: Copy> Transformed<M> for NurbsSurface<V>
|
||||
where M: Copy + std::ops::Mul<V, Output = V>
|
||||
{
|
||||
#[inline(always)]
|
||||
fn transform_by(&mut self, trans: M) {
|
||||
self.0
|
||||
.control_points
|
||||
.iter_mut()
|
||||
.flatten()
|
||||
.for_each(move |v| *v = trans * *v)
|
||||
}
|
||||
}
|
||||
|
||||
impl SearchParameter<D2> for NurbsSurface<Vector4> {
|
||||
type Point = Point3;
|
||||
/// Search the parameter `(u, v)` such that `self.subs(u, v).rational_projection()` is near `pt`.
|
||||
/// If cannot find, then return `None`.
|
||||
/// # Examples
|
||||
/// ```
|
||||
/// use truck_geometry::prelude::*;
|
||||
/// let knot_vec = KnotVec::uniform_knot(2, 2);
|
||||
/// let ctrl_pts = vec![
|
||||
/// vec![Vector4::new(0.0, 0.0, 0.0, 1.0), Vector4::new(0.1, 0.0, 0.5, 0.4), Vector4::new(1.0, 0.0, 0.6, 2.0), Vector4::new(0.4, 0.0, 0.4, 0.4)],
|
||||
/// vec![Vector4::new(0.0, 0.2, 0.2, 2.0), Vector4::new(0.24, 0.24, 0.1, 1.2), Vector4::new(2.4, 1.8, 2.4, 0.6), Vector4::new(1.4, 0.42, 0.98, 1.4)],
|
||||
/// vec![Vector4::new(0.0, 1.5, 1.2, 3.0), Vector4::new(1.02, 2.04, 1.7, 3.4), Vector4::new(0.42, 0.28, 0.7, 0.7), Vector4::new(0.6, 0.3, 0.0, 0.6)],
|
||||
/// vec![Vector4::new(0.0, 1.0, 1.0, 1.0), Vector4::new(0.2, 2.0, 2.0, 2.0), Vector4::new(0.85, 1.7, 0.85, 1.7), Vector4::new(1.0, 1.0, 0.3, 1.0)],
|
||||
/// ];
|
||||
/// let bspsurface = BSplineSurface::new((knot_vec.clone(), knot_vec), ctrl_pts);
|
||||
/// let surface = NurbsSurface::new(bspsurface);
|
||||
///
|
||||
/// let pt = surface.subs(0.3, 0.7);
|
||||
/// let (u, v) = surface.search_parameter(pt, Some((0.5, 0.5)), 100).unwrap();
|
||||
/// assert_near!(surface.subs(u, v), pt);
|
||||
/// ```
|
||||
fn search_parameter<H: Into<SPHint2D>>(
|
||||
&self,
|
||||
point: Point3,
|
||||
hint: H,
|
||||
trials: usize,
|
||||
) -> Option<(f64, f64)> {
|
||||
let hint = match hint.into() {
|
||||
SPHint2D::Parameter(x, y) => (x, y),
|
||||
SPHint2D::Range(range0, range1) => {
|
||||
algo::surface::presearch(self, point, (range0, range1), PRESEARCH_DIVISION)
|
||||
}
|
||||
SPHint2D::None => {
|
||||
algo::surface::presearch(self, point, self.range_tuple(), PRESEARCH_DIVISION)
|
||||
}
|
||||
};
|
||||
algo::surface::search_parameter3d(self, point, hint, trials)
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_include2d() {
|
||||
let knot_vec = KnotVec::uniform_knot(2, 3);
|
||||
let ctrl_pts = vec![
|
||||
vec![
|
||||
Vector3::new(0.0, 0.0, 1.0),
|
||||
Vector3::new(0.05, 0.0, 0.5),
|
||||
Vector3::new(0.15, 0.0, 0.3),
|
||||
Vector3::new(1.0, 0.0, 1.0),
|
||||
],
|
||||
vec![
|
||||
Vector3::new(0.0, 0.01, 0.1),
|
||||
Vector3::new(0.02, 0.02, 0.1),
|
||||
Vector3::new(0.16, 0.12, 0.4),
|
||||
Vector3::new(0.7, 0.21, 0.7),
|
||||
],
|
||||
vec![
|
||||
Vector3::new(0.0, 0.02, 0.4),
|
||||
Vector3::new(0.15, 0.3, 0.5),
|
||||
Vector3::new(0.6, 0.4, 1.0),
|
||||
Vector3::new(0.4, 0.2, 0.4),
|
||||
],
|
||||
vec![
|
||||
Vector3::new(0.0, 1.0, 1.0),
|
||||
Vector3::new(0.1, 1.0, 1.0),
|
||||
Vector3::new(0.25, 0.5, 0.5),
|
||||
Vector3::new(0.3, 0.3, 0.3),
|
||||
],
|
||||
];
|
||||
let surface = BSplineSurface::new((knot_vec.clone(), knot_vec), ctrl_pts);
|
||||
let bnd_box = BoundingBox::from_iter(&[Vector2::new(0.2, 0.3), Vector2::new(0.8, 0.6)]);
|
||||
let mut curve = surface.sectional_curve(bnd_box);
|
||||
curve.control_points_mut().for_each(|pt| *pt *= 3.0);
|
||||
let surface = NurbsSurface::new(surface);
|
||||
let curve = NurbsCurve::new(curve);
|
||||
assert!(surface.include(&curve));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_include3d() {
|
||||
let knot_vec = KnotVec::bezier_knot(2);
|
||||
let ctrl_pts = vec![
|
||||
vec![
|
||||
Vector4::new(-1.0, -1.0, 2.0, 1.0),
|
||||
Vector4::new(-1.0, 0.0, 0.0, 1.0),
|
||||
Vector4::new(-1.0, 1.0, 2.0, 1.0),
|
||||
],
|
||||
vec![
|
||||
Vector4::new(0.0, -1.0, 0.0, 1.0),
|
||||
Vector4::new(0.0, 0.0, -2.0, 1.0),
|
||||
Vector4::new(0.0, 1.0, 0.0, 1.0),
|
||||
],
|
||||
vec![
|
||||
Vector4::new(1.0, -1.0, 2.0, 1.0),
|
||||
Vector4::new(1.0, 0.0, 0.0, 1.0),
|
||||
Vector4::new(1.0, 1.0, 2.0, 1.0),
|
||||
],
|
||||
];
|
||||
let surface = NurbsSurface::new(BSplineSurface::new((knot_vec.clone(), knot_vec), ctrl_pts));
|
||||
|
||||
let knot_vec = KnotVec::from(vec![
|
||||
0.0, 0.0, 0.0, 0.25, 0.25, 0.5, 0.5, 0.75, 0.75, 1.0, 1.0, 1.0,
|
||||
]);
|
||||
let ctrl_pts = vec![
|
||||
// the vector of the indices of control points
|
||||
Vector4::new(0.0, -2.0, 2.0, 2.0),
|
||||
Vector4::new(1.0, -1.0, 1.0, 1.0),
|
||||
Vector4::new(1.0, 0.0, 1.0, 1.0),
|
||||
Vector4::new(1.0, 1.0, 1.0, 1.0),
|
||||
Vector4::new(0.0, 2.0, 2.0, 2.0),
|
||||
Vector4::new(-1.0, 1.0, 1.0, 1.0),
|
||||
Vector4::new(-1.0, 0.0, 1.0, 1.0),
|
||||
Vector4::new(-1.0, -1.0, 1.0, 1.0),
|
||||
Vector4::new(0.0, -2.0, 2.0, 2.0),
|
||||
];
|
||||
let mut curve = NurbsCurve::new(BSplineCurve::new(knot_vec, ctrl_pts));
|
||||
assert!(surface.include(&curve));
|
||||
*curve.control_point_mut(1) += Vector4::new(0.0, 0.0, 0.00001, 0.0);
|
||||
assert!(!surface.include(&curve));
|
||||
}
|
||||
Reference in new issue
Block a user