1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609
use super::point::Point;
use super::LineSegment;
use super::QuadraticBezier;
use super::*;
/// A 2d cubic Bezier curve defined by four points: the starting point, two successive
/// control points and the ending point.
/// The curve is defined by equation:
/// ```∀ t ∈ [0..1], P(t) = (1 - t)³ * start + 3 * (1 - t)² * t * ctrl1 + 3 * t² * (1 - t) * ctrl2 + t³ * end```
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct CubicBezier<P> {
pub(crate) start: P,
pub(crate) ctrl1: P,
pub(crate) ctrl2: P,
pub(crate) end: P,
}
//#[allow(dead_code)]
impl<P> CubicBezier<P>
where
P: Point,
{
pub fn new(start: P, ctrl1: P, ctrl2: P, end: P) -> Self {
CubicBezier {
start,
ctrl1,
ctrl2,
end,
}
}
/// Evaluate a CubicBezier curve at t by direct evaluation of the polynomial (not numerically stable)
pub fn eval(&self, t: P::Scalar) -> P {
self.start * ((-t + 1.0) * (-t + 1.0) * (-t + 1.0))
+ self.ctrl1 * (t * (-t + 1.0) * (-t + 1.0) * 3.0)
+ self.ctrl2 * (t * t * (-t + 1.0) * 3.0)
+ self.end * (t * t * t)
}
/// Evaluate a CubicBezier curve at t using the numerically stable De Casteljau algorithm
pub fn eval_casteljau(&self, t: P::Scalar) -> P {
// unrolled de casteljau algorithm
// _1ab is the first iteration from first (a) to second (b) control point and so on
let ctrl_1ab = self.start + (self.ctrl1 - self.start) * t;
let ctrl_1bc = self.ctrl1 + (self.ctrl2 - self.ctrl1) * t;
let ctrl_1cd = self.ctrl2 + (self.end - self.ctrl2) * t;
// second iteration
let ctrl_2ab = ctrl_1ab + (ctrl_1bc - ctrl_1ab) * t;
let ctrl_2bc = ctrl_1bc + (ctrl_1cd - ctrl_1bc) * t;
// third iteration, return final point on the curve ctrl_3ab
ctrl_2ab + (ctrl_2bc - ctrl_2ab) * t
}
pub fn control_points(&self) -> [P; 4] {
[self.start, self.ctrl1, self.ctrl2, self.end]
}
/// Returns the x coordinate of the curve evaluated at t
/// Convenience shortcut for bezier.eval(t).x()
pub fn axis(&self, t: P::Scalar, axis: usize) -> P::Scalar {
let t2 = t * t;
let t3 = t2 * t;
let one_t = -t + 1.0;
let one_t2 = one_t * one_t;
let one_t3 = one_t2 * one_t;
one_t3 * self.start.axis(axis)
+ one_t2 * t * self.ctrl1.axis(axis) * 3.0
+ one_t * t2 * self.ctrl2.axis(axis) * 3.0
+ t3 * self.end.axis(axis)
}
/// Approximates the arc length of the curve by flattening it with straight line segments.
/// Remember arclen also works by linear approximation, not the integral, so we have to accept error!
/// This approximation is unfeasable if desired accuracy is greater than 2 decimal places
pub fn arclen(&self, nsteps: usize) -> P::Scalar {
let stepsize = 1.0 / (nsteps as NativeFloat);
let mut arclen = P::Scalar::from(0.0);
for t in 1..nsteps {
let t = P::Scalar::from(t as NativeFloat) * 1.0 / (nsteps as NativeFloat);
let p1 = self.eval_casteljau(t);
let p2 = self.eval_casteljau(t + stepsize);
arclen = arclen + (p1 - p2).squared_length().sqrt();
}
arclen
}
pub fn split(&self, t: P::Scalar) -> (Self, Self) {
let t = t.into();
// unrolled de casteljau algorithm
// _1ab is the first iteration from first (a) to second (b) control point and so on
let ctrl_1ab = self.start + (self.ctrl1 - self.start) * t;
let ctrl_1bc = self.ctrl1 + (self.ctrl2 - self.ctrl1) * t;
let ctrl_1cd = self.ctrl2 + (self.end - self.ctrl2) * t;
// second iteration
let ctrl_2ab = ctrl_1ab + (ctrl_1bc - ctrl_1ab) * t;
let ctrl_2bc = ctrl_1bc + (ctrl_1cd - ctrl_1bc) * t;
// third iteration, final point on the curve
let ctrl_3ab = ctrl_2ab + (ctrl_2bc - ctrl_2ab) * t;
(
CubicBezier {
start: self.start,
ctrl1: ctrl_1ab,
ctrl2: ctrl_2ab,
end: ctrl_3ab,
},
CubicBezier {
start: ctrl_3ab,
ctrl1: ctrl_2bc,
ctrl2: ctrl_1cd,
end: self.end,
},
)
}
/// Return the derivative curve.
/// The derivative is also a bezier curve but of degree n-1 (cubic->quadratic)
/// Since it returns the derivative function, eval() needs to be called separately
pub fn derivative(&self) -> QuadraticBezier<P> {
QuadraticBezier {
start: (self.ctrl1 - self.start) * 3.0,
ctrl: (self.ctrl2 - self.ctrl1) * 3.0,
end: (self.end - self.ctrl2) * 3.0,
}
}
/// Direct Derivative - Sample the axis coordinate at 'axis' of the curve's derivative at t
/// without creating a new curve. This is a convenience function for .derivative().eval(t).axis(n)
/// Parameters:
/// t: the sampling parameter on the curve interval [0..1]
/// axis: the index of the coordinate axis [0..N]
/// Returns:
/// Scalar value of the points own type type F
/// May be deprecated in the future.
/// This function can cause out of bounds panic when axis is larger than dimension of P
pub fn dd(&self, t: P::Scalar, axis: usize) -> P::Scalar {
let t2 = t * t;
let c0 = t * -3.0 + t * 6.0 - 3.0;
let c1 = t2 * 9.0 - t * 12.0 + 3.0;
let c2 = t2 * -9.0 + t * 6.0;
let c3 = t2 * 3.0;
self.start.axis(axis) * c0
+ self.ctrl1.axis(axis) * c1
+ self.ctrl2.axis(axis) * c2
+ self.end.axis(axis) * c3
}
// pub fn curvature(&self, t: P::Scalar) -> F
// where
// F: P::Scalarloat,
// P: Sub<P, Output = P>
// + Add<P, Output = P>
// + Mul<F, Output = P>,
// P::Scalar: Sub<F, Output = F>
// + Add<F, Output = F>
// + Mul<F, Output = F>
// + Float
// + Into
// {
// let d = self.derivative();
// let dd = d.derivative();
// let dx = d.x(t);
// let dy = d.y(t);
// let ddx = dd.x(t);
// let ddy = dd.y(t);
// let numerator = dx * ddy.into() - ddx * dy;
// let denominator = (dx*dx + dy*dy).powf(1.5.into());
// return numerator / denominator
// }
// pub fn radius(&self, t: P::Scalar) -> F
// where
// F: P::Scalarloat,
// P: Sub<P, Output = P>
// + Add<P, Output = P>
// + Mul<F, Output = P>,
// P::Scalar: Sub<F, Output = F>
// + Add<F, Output = F>
// + Mul<F, Output = F>
// + Float
// + Into
// {
// return 1.0.into() / self.curvature(t)
// }
/// Calculates the minimum distance between given 'point' and the curve.
/// Uses two passes with the same amount of steps in t:
/// 1. coarse search over the whole curve
/// 2. fine search around the minimum yielded by the coarse search
pub fn distance_to_point(&self, point: P) -> P::Scalar {
let nsteps: usize = 64;
let mut tmin: P::Scalar = 0.5.into();
let mut dmin: P::Scalar = (point - self.start).squared_length();
// 1. coarse pass
for i in 0..nsteps {
// calculate next step value
let t: P::Scalar = (i as NativeFloat * 1.0 as NativeFloat / (nsteps as NativeFloat)).into();
// calculate distance to candidate
let candidate = self.eval(t);
if (candidate - point).squared_length() < dmin {
tmin = t;
dmin = (candidate - point).squared_length();
}
}
// 2. fine pass
for i in 0..nsteps {
// calculate next step value ( a 64th of a 64th from first step)
let t: P::Scalar = (i as NativeFloat * 1.0 as NativeFloat / ((nsteps*nsteps) as NativeFloat)).into();
// calculate distance to candidate centered around tmin from before
let candidate: P = self.eval(tmin + t - t*(nsteps as NativeFloat/ 2.0) );
if (candidate - point).squared_length() < dmin {
tmin = t;
dmin = (candidate - point).squared_length();
}
}
dmin.sqrt()
}
pub fn baseline(&self) -> LineSegment<P> {
LineSegment {
start: self.start,
end: self.end,
}
}
pub fn is_linear(&self, tolerance: P::Scalar) -> bool {
// if start and end are (nearly) the same
if (self.start - self.end).squared_length() < P::Scalar::epsilon() {
return false;
}
// else check if ctrl points lie on baseline
self.are_points_colinear(tolerance)
}
fn are_points_colinear(&self, tolerance: P::Scalar) -> bool {
let line = self.baseline();
line.distance_to_point(self.ctrl1) <= tolerance
&& line.distance_to_point(self.ctrl2) <= tolerance
}
// Returs if the whole set of control points can be considered one singular point
// given some tolerance.
// TODO use machine epsilon vs squared_length OK?
pub fn is_a_point(&self, tolerance: P::Scalar) -> bool {
let tolerance_squared = tolerance * tolerance;
// Use <= so that tolerance can be zero.
(self.start - self.end).squared_length() <= tolerance_squared
&& (self.start - self.ctrl1).squared_length() <= tolerance_squared
&& (self.end - self.ctrl2).squared_length() <= tolerance_squared
}
/// Compute the real roots of the cubic bezier function with
/// parameters of the form a*t^3 + b*t^2 + c*t + d for each dimension
/// using cardano's algorithm (code adapted from github.com/nical/lyon)
/// returns an ArrayVec of the present roots (max 3)
#[allow(clippy::many_single_char_names)] // this is math, get over it
pub(crate) fn real_roots(
&self,
a: P::Scalar,
b: P::Scalar,
c: P::Scalar,
d: P::Scalar,
) -> ArrayVec<[P::Scalar; 3]> {
let mut result = ArrayVec::new();
let pi = P::Scalar::from(core::f32::consts::PI.into()).into();
// check if can be handled below cubic order
if a.abs() < P::Scalar::epsilon() {
if b.abs() < P::Scalar::epsilon() {
if c.abs() < P::Scalar::epsilon() {
// no solutions
return result;
}
// is linear equation
result.push(-d / c);
return result;
}
// is quadratic equation
let delta = c * c - b * d * 4.0;
if delta > 0.0.into() {
let sqrt_delta = delta.sqrt();
result.push((-c - sqrt_delta) / (b * 2.0));
result.push((-c + sqrt_delta) / (b * 2.0));
} else if delta.abs() < P::Scalar::epsilon() {
result.push(-c / (b * 2.0));
}
return result;
}
// is cubic equation -> use cardano's algorithm
let frac_1_3 = P::Scalar::from(1.0 / 3.0);
let bn = b / a;
let cn = c / a;
let dn = d / a;
let delta0: P::Scalar = (cn * 3.0 - bn * bn) / 9.0;
let delta1: P::Scalar = (bn * cn * 9.0 - dn * 27.0 - bn * bn * bn * 2.0) / 54.0;
let delta_01: P::Scalar = delta0 * delta0 * delta0 + delta1 * delta1;
if delta_01 >= P::Scalar::from(0.0) {
let delta_p_sqrt: P::Scalar = delta1 + delta_01.sqrt();
let delta_m_sqrt: P::Scalar = delta1 - delta_01.sqrt();
let s = delta_p_sqrt.signum() * delta_p_sqrt.abs().powf(frac_1_3);
let t = delta_m_sqrt.signum() * delta_m_sqrt.abs().powf(frac_1_3);
result.push(-bn * frac_1_3 + (s + t));
// Don't add the repeated root when s + t == 0.
if (s - t).abs() < P::Scalar::epsilon() && (s + t).abs() >= P::Scalar::epsilon() {
result.push(-bn * frac_1_3 - (s + t) / 2.0);
}
} else {
let theta = (delta1 / (-delta0 * delta0 * delta0).sqrt()).acos();
let two_sqrt_delta0 = (-delta0).sqrt() * 2.0;
result.push(two_sqrt_delta0 * Float::cos(theta * frac_1_3) - bn * frac_1_3);
result
.push(two_sqrt_delta0 * Float::cos((theta + 2.0 * pi) * frac_1_3) - bn * frac_1_3);
result
.push(two_sqrt_delta0 * Float::cos((theta + 4.0 * pi) * frac_1_3) - bn * frac_1_3);
}
result
}
/// Solves the cubic bezier function given a particular coordinate axis value
/// by solving the roots for the axis functions
/// Parameters:
/// value: the coordinate value on the particular axis
/// axis: the index of the axis
/// Returns those roots of the function that are in the interval [0.0, 1.0].
#[allow(dead_code)]
fn solve_t_for_axis(&self, value: P::Scalar, axis: usize) -> ArrayVec<[P::Scalar; 3]> {
let mut result = ArrayVec::new();
// check if all points are the same or if the curve is really just a line
if self.is_a_point(P::Scalar::epsilon())
|| (self.are_points_colinear(P::Scalar::epsilon())
&& (self.start - self.end).squared_length() < P::Scalar::epsilon())
{
return result;
}
let a = -self.start.axis(axis) + self.ctrl1.axis(axis) * 3.0 - self.ctrl2.axis(axis) * 3.0
+ self.end.axis(axis);
let b =
self.start.axis(axis) * 3.0 - self.ctrl1.axis(axis) * 6.0 + self.ctrl2.axis(axis) * 3.0;
let c = -self.start.axis(axis) * 3.0 + self.ctrl1.axis(axis) * 3.0;
let d = self.start.axis(axis) - value;
let roots = self.real_roots(a, b, c, d);
for root in roots {
if root > 0.0.into() && root < 1.0.into() {
result.push(root);
}
}
result
}
/// Return the bounding box of the curve as an array of (min, max) tuples for each dimension (its index)
pub fn bounding_box(&self) -> [(P::Scalar, P::Scalar); P::DIM] {
// calculate coefficients for the derivative: at^2 + bt + c
// from the expansion of the cubic bezier curve: sum_i=0_to_3( binomial(3, i) * t^i * (1-t)^(n-i) )
// yields coeffcients
// po: [1, -2, 1]
// p1: [0, 2, -2]
// p2: [0, 0, 1]
// c b a
let mut bounds = [(0.0.into(), 0.0.into()); P::DIM];
let derivative = self.derivative();
// calculate coefficients for derivative
let a: P = derivative.start + derivative.ctrl * -2.0 + derivative.end;
let b: P = derivative.start * -2.0 + derivative.ctrl * 2.0;
let c: P = derivative.start;
// calculate roots for t over x axis and plug them into the bezier function
// to get x,y values (make vec 2 bigger for t=0,t=1 values)
// loop over any of the points dimensions (they're all the same)
for (dim, _) in a.into_iter().enumerate() {
let mut extrema: ArrayVec<[P::Scalar; 4]> = ArrayVec::new();
extrema.extend(
derivative
.real_roots(a.axis(dim), b.axis(dim), c.axis(dim))
.into_iter(),
);
// only retain roots for which t is in [0..1]
extrema.retain(|root| -> bool { root > &mut 0.0.into() && root < &mut 1.0.into() });
// evaluates roots in original function
for t in extrema.iter_mut() {
*t = self.eval_casteljau(*t).axis(dim);
}
// add y-values for start and end point as candidates
extrema.push(self.start.axis(dim));
extrema.push(self.end.axis(dim));
// sort to get min and max values for bounding box
extrema.sort_unstable_by(|a, b| a.partial_cmp(b).unwrap());
// determine xmin, xmax, ymin, ymax, from the set {B(xroots), B(yroots), B(0), B(1)}
// (Intermediate control points can't form a boundary)
// .unwrap() is ok as it can never be empty as it always at least contains the endpoints
bounds[dim] = (extrema[0], *extrema.last().unwrap());
}
bounds
}
}
#[cfg(test)]
mod tests {
use super::PointN;
use super::*;
#[test]
fn circle_approximation_error() {
// define closure for unit circle
let circle =
|p: PointN<f64, 2>| -> f64 { p.into_iter().map(|x| x * x).sum::<f64>().sqrt() - 1f64 };
// define control points for 4 bezier segments
// control points are chosen for minimum radial distance error
// according to: http://spencermortensen.com/articles/bezier-circle/
// TODO don't hardcode values
let c = 0.551915024494;
let max_drift_perc = 0.019608; // radial drift percent
let max_error = max_drift_perc * 0.01; // absolute max radial error
let bezier_quadrant_1 = CubicBezier {
start: PointN::new([0f64, 1f64]),
ctrl1: PointN::new([c, 1f64]),
ctrl2: PointN::new([1f64, c]),
end: PointN::new([1f64, 0f64]),
};
let bezier_quadrant_2 = CubicBezier {
start: PointN::new([1f64, 0f64]),
ctrl1: PointN::new([1f64, -c]),
ctrl2: PointN::new([c, -1f64]),
end: PointN::new([0f64, -1f64]),
};
let bezier_quadrant_3 = CubicBezier {
start: PointN::new([0f64, -1f64]),
ctrl1: PointN::new([-c, -1f64]),
ctrl2: PointN::new([-1f64, -c]),
end: PointN::new([-1f64, 0f64]),
};
let bezier_quadrant_4 = CubicBezier {
start: PointN::new([-1f64, 0f64]),
ctrl1: PointN::new([-1f64, c]),
ctrl2: PointN::new([-c, 1f64]),
end: PointN::new([0f64, 1f64]),
};
let nsteps = 1000;
for t in 0..=nsteps {
let t = t as f64 * 1f64 / (nsteps as f64);
let point = bezier_quadrant_1.eval(t);
let contour = circle(point);
assert!(contour.abs() <= max_error);
let point = bezier_quadrant_2.eval(t);
let contour = circle(point);
assert!(contour.abs() <= max_error);
let point = bezier_quadrant_3.eval(t);
let contour = circle(point);
assert!(contour.abs() <= max_error);
let point = bezier_quadrant_4.eval(t);
let contour = circle(point);
assert!(contour.abs() <= max_error);
}
}
#[test]
fn circle_circumference_approximation() {
// define control points for 4 cubic bezier segments to best approximate a unit circle
// control points are chosen for minimum radial distance error, see circle_approximation_error() in this file
// given this, the circumference will also be close to 2*pi
// (remember arclen also works by linear approximation, not the true integral, so we have to accept error)!
// This approximation is unfeasable if desired accuracy is greater than ~2 decimal places (at 1000 steps)
// TODO don't hardcode values, solve for them
let c = 0.551915024494;
let max_error = 1e-2;
let nsteps = 1e3 as usize;
let pi = 3.14159265359;
let tau = 2. * pi;
let bezier_quadrant_1 = CubicBezier {
start: PointN::new([0f64, 1f64]),
ctrl1: PointN::new([c, 1f64]),
ctrl2: PointN::new([1f64, c]),
end: PointN::new([1f64, 0f64]),
};
let bezier_quadrant_2 = CubicBezier {
start: PointN::new([1f64, 0f64]),
ctrl1: PointN::new([1f64, -c]),
ctrl2: PointN::new([c, -1f64]),
end: PointN::new([0f64, -1f64]),
};
let bezier_quadrant_3 = CubicBezier {
start: PointN::new([0f64, -1f64]),
ctrl1: PointN::new([-c, -1f64]),
ctrl2: PointN::new([-1f64, -c]),
end: PointN::new([-1f64, 0f64]),
};
let bezier_quadrant_4 = CubicBezier {
start: PointN::new([-1f64, 0f64]),
ctrl1: PointN::new([-1f64, c]),
ctrl2: PointN::new([-c, 1f64]),
end: PointN::new([0f64, 1f64]),
};
let circumference = bezier_quadrant_1.arclen(nsteps)
+ bezier_quadrant_2.arclen(nsteps)
+ bezier_quadrant_3.arclen(nsteps)
+ bezier_quadrant_4.arclen(nsteps);
//dbg!(circumference);
//dbg!(tau);
assert!(((tau + max_error) > circumference) && ((tau - max_error) < circumference));
}
#[test]
fn eval_equivalence_casteljau() {
// all eval methods should be approximately equivalent for well defined test cases
// and not equivalent where numerical stability becomes an issue for normal eval
let bezier = CubicBezier::new(
PointN::new([0f64, 1.77f64]),
PointN::new([1.1f64, -1f64]),
PointN::new([4.3f64, 3f64]),
PointN::new([3.2f64, -4f64]),
);
let nsteps: usize = 1000;
for t in 0..=nsteps {
let t = t as f64 * 1f64 / (nsteps as f64);
let p1 = bezier.eval(t);
let p2 = bezier.eval_casteljau(t);
let err = p2 - p1;
assert!(err.squared_length() < EPSILON);
}
}
#[test]
fn split_equivalence() {
// chose some arbitrary control points and construct a cubic bezier
let bezier = CubicBezier {
start: PointN::new([0f64, 1.77f64]),
ctrl1: PointN::new([2.9f64, 0f64]),
ctrl2: PointN::new([4.3f64, 3f64]),
end: PointN::new([3.2f64, -4f64]),
};
// split it at an arbitrary point
let at = 0.5;
let (left, right) = bezier.split(at);
// compare left and right subcurves with parent curve
// take the difference of the two points which must not exceed the absolute error
let nsteps: usize = 1000;
for t in 0..=nsteps {
let t = t as f64 * 1f64 / (nsteps as f64);
// left
let mut err = bezier.eval(t / 2.0) - left.eval(t);
assert!(err.squared_length() < EPSILON);
// right
err = bezier.eval((t * 0.5) + 0.5) - right.eval(t);
assert!(err.squared_length() < EPSILON);
}
}
#[test]
fn bounding_box_contains() {
// check if bounding box for a curve contains all points (with some approximation error)
let bezier = CubicBezier {
start: PointN::new([0f64, 1.77f64]),
ctrl1: PointN::new([2.9f64, 0f64]),
ctrl2: PointN::new([4.3f64, -3f64]),
end: PointN::new([3.2f64, 4f64]),
};
let bounds = bezier.bounding_box();
let max_err = 1e-2;
let nsteps: usize = 100;
for t in 0..=nsteps {
let t = t as f64 * 1f64 / (nsteps as f64);
let p = bezier.eval_casteljau(t);
//dbg!(t);
//dbg!(p);
//dbg!(xmin-max_err, ymin-max_err, xmax+max_err, ymax+max_err);
for (idx, axis) in p.into_iter().enumerate() {
assert!((axis >= (bounds[idx].0 - max_err)) && (axis <= (bounds[idx].1 + max_err)))
}
}
}
#[test]
fn distance_to_point() {
// degree 3, 4 control points => 4+3+1=8 knots
let curve = CubicBezier{
start: PointN::new([0f64, 1.77f64]),
ctrl1: PointN::new([1.1f64, -1f64]),
ctrl2: PointN::new([4.3f64, 3f64]),
end: PointN::new([3.2f64, -4f64]),
};
assert!(curve.distance_to_point(PointN::new([-5.1, -5.6])) > curve.distance_to_point(PointN::new([5.1, 5.6])));
}
}