summaryrefslogtreecommitdiffstats
path: root/gfx/2d/BezierUtils.h
blob: 7871f028454e244bce7a1496094f3a32a3866443 (plain)
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
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef mozilla_BezierUtils_h_
#define mozilla_BezierUtils_h_

#include "mozilla/gfx/2D.h"
#include "gfxRect.h"

namespace mozilla {
namespace gfx {

// Control points for bezier curve
//
//                   mPoints[2]
//                    +-----___---+ mPoints[3]
//                      __--
//                   _--
//                  /
//                /
// mPoints[1] +  |
//            | |
//            ||
//            ||
//            |
//            |
//            |
//            |
// mPoints[0] +
struct Bezier {
  Point mPoints[4];
};

// Calculate a point or it's differential of a bezier curve formed by
// aBezier and parameter t.
//
//   GetBezierPoint = P(t)
//   GetBezierDifferential = P'(t)
//   GetBezierDifferential2 = P''(t)
//
//                   mPoints[2]
//                    +-----___---+ mPoints[3]
//                      __--     P(1)
//                   _--
//                  +
//                /  P(t)
// mPoints[1] +  |
//            | |
//            ||
//            ||
//            |
//            |
//            |
//            |
// mPoints[0] + P(0)
Point GetBezierPoint(const Bezier& aBezier, Float t);
Point GetBezierDifferential(const Bezier& aBezier, Float t);
Point GetBezierDifferential2(const Bezier& aBezier, Float t);

// Calculate length of a simple bezier curve formed by aBezier and range [a, b].
Float GetBezierLength(const Bezier& aBezier, Float a, Float b);

// Split bezier curve formed by aBezier into [0,t1], [t1,t2], [t2,1] parts, and
// stores control points for [t1,t2] to aSubBezier.
//
//                 ___---+
//             __+-     P(1)
//          _--   P(t2)
//         -
//       /  <-- aSubBezier
//      |
//     |
//    +
//    | P(t1)
//   |
//   |
//   |
//   |
//   + P(0)
void GetSubBezier(Bezier* aSubBezier, const Bezier& aBezier,
                  Float t1, Float t2);

// Find a nearest point on bezier curve formed by aBezier to a point aTarget.
// aInitialT is a hint to find the parameter t for the nearest point.
// If aT is non-null, parameter for the nearest point is stored to *aT.
// This function expects a bezier curve to be an approximation of elliptic arc.
// Otherwise it will return wrong point.
//
//  aTarget
//    +            ___---+
//             __--
//          _--
//         +
//       /  nearest point = P(t = *aT)
//      |
//     |
//    |
//    + P(aInitialT)
//   |
//   |
//   |
//   |
//   +
Point FindBezierNearestPoint(const Bezier& aBezier, const Point& aTarget,
                             Float aInitialT, Float* aT=nullptr);

// Calculate control points for a bezier curve that is an approximation of
// an elliptic arc.
//
//                                   aCornerSize.width
//                                 |<----------------->|
//                                 |                   |
//                     aCornerPoint|      mPoints[2]   |
//                    -------------+-------+-----___---+ mPoints[3]
//                    ^            |         __--
//                    |            |      _--
//                    |            |     -
//                    |            |   /
// aCornerSize.height | mPoints[1] +  |
//                    |            | |
//                    |            ||
//                    |            ||
//                    |            |
//                    |            |
//                    |            |
//                    v mPoints[0] |
//                    -------------+
void GetBezierPointsForCorner(Bezier* aBezier, mozilla::css::Corner aCorner,
                              const Point& aCornerPoint,
                              const Size& aCornerSize);

// Calculate the approximate length of a quarter elliptic arc formed by radii
// (a, b).
//
//                a
//      |<----------------->|
//      |                   |
//   ---+-------------___---+
//   ^  |         __--
//   |  |      _--
//   |  |     -
//   |  |   /
// b |  |  |
//   |  | |
//   |  ||
//   |  ||
//   |  |
//   |  |
//   |  |
//   v  |
//   ---+
Float GetQuarterEllipticArcLength(Float a, Float b);

// Calculate the distance between an elliptic arc formed by (origin, width,
// height), and a point P, along a line formed by |P + n * normal|.
// P should be outside of the ellipse, and the line should cross with the
// ellipse twice at n > 0 points.
//
//                            width
//                     |<----------------->|
//              origin |                   |
//          -----------+-------------___---+
//          ^  normal  |         __--
//          | P +->__  |      _--
//          |        --__    -
//          |          | --+
//   height |          |  |
//          |          | |
//          |          ||
//          |          ||
//          |          |
//          |          |
//          |          |
//          v          |
//          -----------+
Float CalculateDistanceToEllipticArc(const Point& P, const Point& normal,
                                     const Point& origin,
                                     Float width, Float height);

} // namespace gfx
} // namespace mozilla

#endif /* mozilla_BezierUtils_h_ */