summaryrefslogtreecommitdiff
path: root/ssg/include/sginterpolator.h
blob: f638682b4601ae05ee462e1b68fa920d9de4375e (plain) (blame)
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
#ifndef SGINTERPOLATOR_H
#define SGINTERPOLATOR_H

#include "sgpoint.h"

class SGInterpolator
{
public:
    SGInterpolator() { /* caller must call Init later */ }

    SGInterpolator(float aX1, float aY1,
                    float aX2, float aY2)
    {
      init(aX1, aY1, aX2, aY2);
    }

    SGInterpolator(SGPointF pt1, SGPointF pt2)
    {
      init(pt1.x(), pt1.y(), pt2.x(), pt2.y());
    }

    void init(float aX1, float aY1,
              float aX2, float aY2);

    float value(float aX) const;

    void GetSplineDerivativeValues(float aX, float& aDX, float& aDY) const;
private:
    void
      CalcSampleValues();

      /**
       * Returns x(t) given t, x1, and x2, or y(t) given t, y1, and y2.
       */
      static float
      CalcBezier(float aT, float aA1, float aA2);

      /**
       * Returns dx/dt given t, x1, and x2, or dy/dt given t, y1, and y2.
       */
      static float
      GetSlope(float aT, float aA1, float aA2);

      float
      GetTForX(float aX) const;

      float
      NewtonRaphsonIterate(float aX, float aGuessT) const;

      float
      BinarySubdivide(float aX, float aA, float aB) const;

      static float
      A(float aA1, float aA2)
      {
        return 1.0 - 3.0 * aA2 + 3.0 * aA1;
      }

      static float
      B(float aA1, float aA2)
      {
        return 3.0 * aA2 - 6.0 * aA1;
      }

      static float
      C(float aA1)
      {
        return 3.0 * aA1;
      }

    float mX1;
    float mY1;
    float mX2;
    float mY2;
    enum { kSplineTableSize = 11 };
    float mSampleValues[kSplineTableSize];
    static const float kSampleStepSize;
};
#endif // SGINTERPOLATOR_H