blob: b7e0ea9f332d8cb7c9964f89f13d6a944a056315 [file] [log] [blame]
caryclark@google.com639df892012-01-10 21:46:10 +00001#include "CubicIntersection.h"
2#include "Extrema.h"
3#include "IntersectionUtilities.h"
4#include "LineParameters.h"
5
6static double interp_cubic_coords(const double* src, double t)
7{
8 double ab = interp(src[0], src[2], t);
9 double bc = interp(src[2], src[4], t);
10 double cd = interp(src[4], src[6], t);
11 double abc = interp(ab, bc, t);
12 double bcd = interp(bc, cd, t);
13 return interp(abc, bcd, t);
14}
15
16static int coincident_line(const Cubic& cubic, Cubic& reduction) {
17 reduction[0] = reduction[1] = cubic[0];
18 return 1;
19}
20
21static int vertical_line(const Cubic& cubic, Cubic& reduction) {
22 double tValues[2];
23 reduction[0] = cubic[0];
24 reduction[1] = cubic[3];
25 int smaller = reduction[1].y > reduction[0].y;
26 int larger = smaller ^ 1;
27 int roots = SkFindCubicExtrema(cubic[0].y, cubic[1].y, cubic[2].y, cubic[3].y, tValues);
28 for (int index = 0; index < roots; ++index) {
29 double yExtrema = interp_cubic_coords(&cubic[0].y, tValues[index]);
30 if (reduction[smaller].y > yExtrema) {
31 reduction[smaller].y = yExtrema;
32 continue;
33 }
34 if (reduction[larger].y < yExtrema) {
35 reduction[larger].y = yExtrema;
36 }
37 }
38 return 2;
39}
40
41static int horizontal_line(const Cubic& cubic, Cubic& reduction) {
42 double tValues[2];
43 reduction[0] = cubic[0];
44 reduction[1] = cubic[3];
45 int smaller = reduction[1].x > reduction[0].x;
46 int larger = smaller ^ 1;
47 int roots = SkFindCubicExtrema(cubic[0].x, cubic[1].x, cubic[2].x, cubic[3].x, tValues);
48 for (int index = 0; index < roots; ++index) {
49 double xExtrema = interp_cubic_coords(&cubic[0].x, tValues[index]);
50 if (reduction[smaller].x > xExtrema) {
51 reduction[smaller].x = xExtrema;
52 continue;
53 }
54 if (reduction[larger].x < xExtrema) {
55 reduction[larger].x = xExtrema;
56 }
57 }
58 return 2;
59}
60
61// check to see if it is a quadratic or a line
62static int check_quadratic(const Cubic& cubic, Cubic& reduction,
63 int minX, int maxX, int minY, int maxY) {
64 double dx10 = cubic[1].x - cubic[0].x;
65 double dx23 = cubic[2].x - cubic[3].x;
66 double midX = cubic[0].x + dx10 * 3 / 2;
67 if (!approximately_equal(midX - cubic[3].x, dx23 * 3 / 2)) {
68 return 0;
69 }
70 double dy10 = cubic[1].y - cubic[0].y;
71 double dy23 = cubic[2].y - cubic[3].y;
72 double midY = cubic[0].y + dy10 * 3 / 2;
73 if (!approximately_equal(midY - cubic[3].y, dy23 * 3 / 2)) {
74 return 0;
75 }
76 reduction[0] = cubic[0];
77 reduction[1].x = midX;
78 reduction[1].y = midY;
79 reduction[2] = cubic[3];
80 return 3;
81}
82
83static int check_linear(const Cubic& cubic, Cubic& reduction,
84 int minX, int maxX, int minY, int maxY) {
85 int startIndex = 0;
86 int endIndex = 3;
87 while (cubic[startIndex].approximatelyEqual(cubic[endIndex])) {
88 --endIndex;
89 if (endIndex == 0) {
90 printf("%s shouldn't get here if all four points are about equal", __FUNCTION__);
91 assert(0);
92 }
93 }
94 LineParameters lineParameters;
95 lineParameters.cubicEndPoints(cubic, startIndex, endIndex);
96 double normalSquared = lineParameters.normalSquared();
97 double distance[2]; // distance is not normalized
98 int mask = other_two(startIndex, endIndex);
99 int inner1 = startIndex ^ mask;
100 int inner2 = endIndex ^ mask;
101 lineParameters.controlPtDistance(cubic, inner1, inner2, distance);
102 double limit = normalSquared * SquaredEpsilon;
103 int index;
104 for (index = 0; index < 2; ++index) {
105 double distSq = distance[index];
106 distSq *= distSq;
107 if (distSq > limit) {
108 return 0;
109 }
110 }
111 // four are colinear: return line formed by outside
112 reduction[0] = cubic[0];
113 reduction[1] = cubic[3];
114 int sameSide1;
115 int sameSide2;
116 bool useX = cubic[maxX].x - cubic[minX].x >= cubic[maxY].y - cubic[minY].y;
117 if (useX) {
118 sameSide1 = sign(cubic[0].x - cubic[1].x) + sign(cubic[3].x - cubic[1].x);
119 sameSide2 = sign(cubic[0].x - cubic[2].x) + sign(cubic[3].x - cubic[2].x);
120 } else {
121 sameSide1 = sign(cubic[0].y - cubic[1].y) + sign(cubic[3].y - cubic[1].y);
122 sameSide2 = sign(cubic[0].y - cubic[2].y) + sign(cubic[3].y - cubic[2].y);
123 }
124 if (sameSide1 == sameSide2 && (sameSide1 & 3) != 2) {
125 return 2;
126 }
127 double tValues[2];
128 int roots;
129 if (useX) {
130 roots = SkFindCubicExtrema(cubic[0].x, cubic[1].x, cubic[2].x, cubic[3].x, tValues);
131 } else {
132 roots = SkFindCubicExtrema(cubic[0].y, cubic[1].y, cubic[2].y, cubic[3].y, tValues);
133 }
134 for (index = 0; index < roots; ++index) {
135 _Point extrema;
136 extrema.x = interp_cubic_coords(&cubic[0].x, tValues[index]);
137 extrema.y = interp_cubic_coords(&cubic[0].y, tValues[index]);
138 // sameSide > 0 means mid is smaller than either [0] or [3], so replace smaller
139 int replace;
140 if (useX) {
141 if (extrema.x < cubic[0].x ^ extrema.x < cubic[3].x) {
142 continue;
143 }
144 replace = (extrema.x < cubic[0].x | extrema.x < cubic[3].x)
145 ^ cubic[0].x < cubic[3].x;
146 } else {
147 if (extrema.y < cubic[0].y ^ extrema.y < cubic[3].y) {
148 continue;
149 }
150 replace = (extrema.y < cubic[0].y | extrema.y < cubic[3].y)
151 ^ cubic[0].y < cubic[3].y;
152 }
153 reduction[replace] = extrema;
154 }
155 return 2;
156}
157
158/* food for thought:
159http://objectmix.com/graphics/132906-fast-precision-driven-cubic-quadratic-piecewise-degree-reduction-algos-2-a.html
160
161Given points c1, c2, c3 and c4 of a cubic Bezier, the points of the
162corresponding quadratic Bezier are (given in convex combinations of
163points):
164
165q1 = (11/13)c1 + (3/13)c2 -(3/13)c3 + (2/13)c4
166q2 = -c1 + (3/2)c2 + (3/2)c3 - c4
167q3 = (2/13)c1 - (3/13)c2 + (3/13)c3 + (11/13)c4
168
169Of course, this curve does not interpolate the end-points, but it would
170be interesting to see the behaviour of such a curve in an applet.
171
172--
173Kalle Rutanen
174http://kaba.hilvi.org
175
176*/
177
178// reduce to a quadratic or smaller
179// look for identical points
180// look for all four points in a line
181 // note that three points in a line doesn't simplify a cubic
182// look for approximation with single quadratic
183 // save approximation with multiple quadratics for later
184int reduceOrder(const Cubic& cubic, Cubic& reduction, ReduceOrder_Flags allowQuadratics) {
185 int index, minX, maxX, minY, maxY;
186 int minXSet, minYSet;
187 minX = maxX = minY = maxY = 0;
188 minXSet = minYSet = 0;
189 for (index = 1; index < 4; ++index) {
190 if (cubic[minX].x > cubic[index].x) {
191 minX = index;
192 }
193 if (cubic[minY].y > cubic[index].y) {
194 minY = index;
195 }
196 if (cubic[maxX].x < cubic[index].x) {
197 maxX = index;
198 }
199 if (cubic[maxY].y < cubic[index].y) {
200 maxY = index;
201 }
202 }
203 for (index = 0; index < 4; ++index) {
204 if (approximately_equal(cubic[index].x, cubic[minX].x)) {
205 minXSet |= 1 << index;
206 }
207 if (approximately_equal(cubic[index].y, cubic[minY].y)) {
208 minYSet |= 1 << index;
209 }
210 }
211 if (minXSet == 0xF) { // test for vertical line
212 if (minYSet == 0xF) { // return 1 if all four are coincident
213 return coincident_line(cubic, reduction);
214 }
215 return vertical_line(cubic, reduction);
216 }
217 if (minYSet == 0xF) { // test for horizontal line
218 return horizontal_line(cubic, reduction);
219 }
220 int result = check_linear(cubic, reduction, minX, maxX, minY, maxY);
221 if (result) {
222 return result;
223 }
224 if (allowQuadratics && (result = check_quadratic(cubic, reduction, minX, maxX, minY, maxY))) {
225 return result;
226 }
227 memcpy(reduction, cubic, sizeof(Cubic));
228 return 4;
229}