Optimize cubic Bezier sampling a bit

This commit is contained in:
Patrick Walton 2018-12-29 18:26:08 -08:00
parent ad9da96336
commit 1a6b5bd27d
1 changed files with 9 additions and 6 deletions

View File

@ -1736,12 +1736,15 @@ impl CubicAxis {
} }
impl SolveT for CubicAxis { impl SolveT for CubicAxis {
fn sample(&self, t: f32) -> f32 { fn sample(&self, t: f32) -> f32 {
// FIXME(pcwalton): Use Horner's method or something. let t2 = t * t;
let p01 = lerp(self.from, self.ctrl0, t); let t3 = t2 * t;
let p12 = lerp(self.ctrl0, self.ctrl1, t);
let p23 = lerp(self.ctrl1, self.to, t); let c3 = self.to + 3.0 * (self.ctrl0 - self.ctrl1) - self.from;
let (p012, p123) = (lerp(p01, p12, t), lerp(p12, p23, t)); let c2 = 3.0 * (self.from - 2.0 * self.ctrl0 + self.ctrl1);
lerp(p012, p123, t) let c1 = 3.0 * (self.ctrl0 - self.from);
let c0 = self.from;
c3 * t3 + c2 * t2 + c1 * t + c0
} }
fn sample_deriv(&self, t: f32) -> f32 { fn sample_deriv(&self, t: f32) -> f32 {
let inv_t = 1.0 - t; let inv_t = 1.0 - t;