Home | History | Annotate | Download | only in graphics
      1 /*
      2  * Copyright (C) 2008 Apple Inc. All Rights Reserved.
      3  *
      4  * Redistribution and use in source and binary forms, with or without
      5  * modification, are permitted provided that the following conditions
      6  * are met:
      7  * 1. Redistributions of source code must retain the above copyright
      8  *    notice, this list of conditions and the following disclaimer.
      9  * 2. Redistributions in binary form must reproduce the above copyright
     10  *    notice, this list of conditions and the following disclaimer in the
     11  *    documentation and/or other materials provided with the distribution.
     12  *
     13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
     14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
     17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
     21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     24  */
     25 
     26 #ifndef UnitBezier_h
     27 #define UnitBezier_h
     28 
     29 #include "wtf/Assertions.h"
     30 #include <math.h>
     31 
     32 namespace WebCore {
     33 
     34 struct UnitBezier {
     35     UnitBezier(double p1x, double p1y, double p2x, double p2y)
     36     {
     37         // Calculate the polynomial coefficients, implicit first and last control points are (0,0) and (1,1).
     38         cx = 3.0 * p1x;
     39         bx = 3.0 * (p2x - p1x) - cx;
     40         ax = 1.0 - cx -bx;
     41 
     42         cy = 3.0 * p1y;
     43         by = 3.0 * (p2y - p1y) - cy;
     44         ay = 1.0 - cy - by;
     45     }
     46 
     47     double sampleCurveX(double t)
     48     {
     49         // `ax t^3 + bx t^2 + cx t' expanded using Horner's rule.
     50         return ((ax * t + bx) * t + cx) * t;
     51     }
     52 
     53     double sampleCurveY(double t)
     54     {
     55         return ((ay * t + by) * t + cy) * t;
     56     }
     57 
     58     double sampleCurveDerivativeX(double t)
     59     {
     60         return (3.0 * ax * t + 2.0 * bx) * t + cx;
     61     }
     62 
     63     // Given an x value, find a parametric value it came from.
     64     double solveCurveX(double x, double epsilon)
     65     {
     66         ASSERT(x >= 0.0);
     67         ASSERT(x <= 1.0);
     68 
     69         double t0;
     70         double t1;
     71         double t2;
     72         double x2;
     73         double d2;
     74         int i;
     75 
     76         // First try a few iterations of Newton's method -- normally very fast.
     77         for (t2 = x, i = 0; i < 8; i++) {
     78             x2 = sampleCurveX(t2) - x;
     79             if (fabs (x2) < epsilon)
     80                 return t2;
     81             d2 = sampleCurveDerivativeX(t2);
     82             if (fabs(d2) < 1e-6)
     83                 break;
     84             t2 = t2 - x2 / d2;
     85         }
     86 
     87         // Fall back to the bisection method for reliability.
     88         t0 = 0.0;
     89         t1 = 1.0;
     90         t2 = x;
     91 
     92         while (t0 < t1) {
     93             x2 = sampleCurveX(t2);
     94             if (fabs(x2 - x) < epsilon)
     95                 return t2;
     96             if (x > x2)
     97                 t0 = t2;
     98             else
     99                 t1 = t2;
    100             t2 = (t1 - t0) * .5 + t0;
    101         }
    102 
    103         // Failure.
    104         return t2;
    105     }
    106 
    107     // Evaluates y at the given x. The epsilon parameter provides a hint as to the required
    108     // accuracy and is not guaranteed.
    109     double solve(double x, double epsilon)
    110     {
    111         if (x < 0.0)
    112             return 0.0;
    113         if (x > 1.0)
    114             return 1.0;
    115         return sampleCurveY(solveCurveX(x, epsilon));
    116     }
    117 
    118 private:
    119     double ax;
    120     double bx;
    121     double cx;
    122 
    123     double ay;
    124     double by;
    125     double cy;
    126 };
    127 
    128 } // namespace WebCore
    129 
    130 #endif // UnitBezier_h
    131