blob: b9c3c595816bfc9e142437c95de27fb36343e895 [file] [log] [blame]
caryclark@google.comc6825902012-02-03 22:07:47 +00001#include "CurveIntersection.h"
caryclark@google.com639df892012-01-10 21:46:10 +00002#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;
caryclark@google.comfa0588f2012-04-26 21:01:06 +000027 int roots = findExtrema(cubic[0].y, cubic[1].y, cubic[2].y, cubic[3].y, tValues);
caryclark@google.com639df892012-01-10 21:46:10 +000028 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;
rmistry@google.comd6176b02012-08-23 18:14:13 +000033 }
caryclark@google.com639df892012-01-10 21:46:10 +000034 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;
caryclark@google.comfa0588f2012-04-26 21:01:06 +000047 int roots = findExtrema(cubic[0].x, cubic[1].x, cubic[2].x, cubic[3].x, tValues);
caryclark@google.com639df892012-01-10 21:46:10 +000048 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;
rmistry@google.comd6176b02012-08-23 18:14:13 +000053 }
caryclark@google.com639df892012-01-10 21:46:10 +000054 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
caryclark@google.coma3f05fa2012-06-01 17:44:28 +000062static int check_quadratic(const Cubic& cubic, Cubic& reduction) {
caryclark@google.com639df892012-01-10 21:46:10 +000063 double dx10 = cubic[1].x - cubic[0].x;
64 double dx23 = cubic[2].x - cubic[3].x;
65 double midX = cubic[0].x + dx10 * 3 / 2;
66 if (!approximately_equal(midX - cubic[3].x, dx23 * 3 / 2)) {
67 return 0;
68 }
69 double dy10 = cubic[1].y - cubic[0].y;
70 double dy23 = cubic[2].y - cubic[3].y;
71 double midY = cubic[0].y + dy10 * 3 / 2;
72 if (!approximately_equal(midY - cubic[3].y, dy23 * 3 / 2)) {
73 return 0;
74 }
75 reduction[0] = cubic[0];
76 reduction[1].x = midX;
77 reduction[1].y = midY;
78 reduction[2] = cubic[3];
79 return 3;
80}
81
82static int check_linear(const Cubic& cubic, Cubic& reduction,
83 int minX, int maxX, int minY, int maxY) {
84 int startIndex = 0;
85 int endIndex = 3;
86 while (cubic[startIndex].approximatelyEqual(cubic[endIndex])) {
87 --endIndex;
88 if (endIndex == 0) {
89 printf("%s shouldn't get here if all four points are about equal", __FUNCTION__);
90 assert(0);
91 }
92 }
caryclark@google.com15fa1382012-05-07 20:49:36 +000093 if (!isLinear(cubic, startIndex, endIndex)) {
94 return 0;
caryclark@google.com639df892012-01-10 21:46:10 +000095 }
96 // four are colinear: return line formed by outside
97 reduction[0] = cubic[0];
98 reduction[1] = cubic[3];
99 int sameSide1;
100 int sameSide2;
101 bool useX = cubic[maxX].x - cubic[minX].x >= cubic[maxY].y - cubic[minY].y;
102 if (useX) {
103 sameSide1 = sign(cubic[0].x - cubic[1].x) + sign(cubic[3].x - cubic[1].x);
104 sameSide2 = sign(cubic[0].x - cubic[2].x) + sign(cubic[3].x - cubic[2].x);
105 } else {
106 sameSide1 = sign(cubic[0].y - cubic[1].y) + sign(cubic[3].y - cubic[1].y);
107 sameSide2 = sign(cubic[0].y - cubic[2].y) + sign(cubic[3].y - cubic[2].y);
108 }
109 if (sameSide1 == sameSide2 && (sameSide1 & 3) != 2) {
110 return 2;
111 }
112 double tValues[2];
113 int roots;
114 if (useX) {
caryclark@google.comfa0588f2012-04-26 21:01:06 +0000115 roots = findExtrema(cubic[0].x, cubic[1].x, cubic[2].x, cubic[3].x, tValues);
caryclark@google.com639df892012-01-10 21:46:10 +0000116 } else {
caryclark@google.comfa0588f2012-04-26 21:01:06 +0000117 roots = findExtrema(cubic[0].y, cubic[1].y, cubic[2].y, cubic[3].y, tValues);
caryclark@google.com639df892012-01-10 21:46:10 +0000118 }
caryclark@google.com15fa1382012-05-07 20:49:36 +0000119 for (int index = 0; index < roots; ++index) {
caryclark@google.com639df892012-01-10 21:46:10 +0000120 _Point extrema;
121 extrema.x = interp_cubic_coords(&cubic[0].x, tValues[index]);
122 extrema.y = interp_cubic_coords(&cubic[0].y, tValues[index]);
123 // sameSide > 0 means mid is smaller than either [0] or [3], so replace smaller
124 int replace;
125 if (useX) {
126 if (extrema.x < cubic[0].x ^ extrema.x < cubic[3].x) {
127 continue;
128 }
129 replace = (extrema.x < cubic[0].x | extrema.x < cubic[3].x)
130 ^ cubic[0].x < cubic[3].x;
131 } else {
132 if (extrema.y < cubic[0].y ^ extrema.y < cubic[3].y) {
133 continue;
134 }
135 replace = (extrema.y < cubic[0].y | extrema.y < cubic[3].y)
136 ^ cubic[0].y < cubic[3].y;
137 }
138 reduction[replace] = extrema;
139 }
140 return 2;
141}
142
caryclark@google.com15fa1382012-05-07 20:49:36 +0000143bool isLinear(const Cubic& cubic, int startIndex, int endIndex) {
144 LineParameters lineParameters;
145 lineParameters.cubicEndPoints(cubic, startIndex, endIndex);
146 double normalSquared = lineParameters.normalSquared();
147 double distance[2]; // distance is not normalized
148 int mask = other_two(startIndex, endIndex);
149 int inner1 = startIndex ^ mask;
150 int inner2 = endIndex ^ mask;
151 lineParameters.controlPtDistance(cubic, inner1, inner2, distance);
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000152 double limit = normalSquared;
caryclark@google.com15fa1382012-05-07 20:49:36 +0000153 int index;
154 for (index = 0; index < 2; ++index) {
155 double distSq = distance[index];
156 distSq *= distSq;
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000157 if (approximately_greater(distSq, limit)) {
caryclark@google.com15fa1382012-05-07 20:49:36 +0000158 return false;
159 }
160 }
161 return true;
162}
163
caryclark@google.com639df892012-01-10 21:46:10 +0000164/* food for thought:
165http://objectmix.com/graphics/132906-fast-precision-driven-cubic-quadratic-piecewise-degree-reduction-algos-2-a.html
166
167Given points c1, c2, c3 and c4 of a cubic Bezier, the points of the
168corresponding quadratic Bezier are (given in convex combinations of
169points):
170
171q1 = (11/13)c1 + (3/13)c2 -(3/13)c3 + (2/13)c4
172q2 = -c1 + (3/2)c2 + (3/2)c3 - c4
173q3 = (2/13)c1 - (3/13)c2 + (3/13)c3 + (11/13)c4
174
175Of course, this curve does not interpolate the end-points, but it would
176be interesting to see the behaviour of such a curve in an applet.
177
178--
179Kalle Rutanen
180http://kaba.hilvi.org
181
182*/
183
184// reduce to a quadratic or smaller
185// look for identical points
rmistry@google.comd6176b02012-08-23 18:14:13 +0000186// look for all four points in a line
caryclark@google.com639df892012-01-10 21:46:10 +0000187 // note that three points in a line doesn't simplify a cubic
188// look for approximation with single quadratic
189 // save approximation with multiple quadratics for later
190int reduceOrder(const Cubic& cubic, Cubic& reduction, ReduceOrder_Flags allowQuadratics) {
191 int index, minX, maxX, minY, maxY;
192 int minXSet, minYSet;
193 minX = maxX = minY = maxY = 0;
194 minXSet = minYSet = 0;
195 for (index = 1; index < 4; ++index) {
196 if (cubic[minX].x > cubic[index].x) {
197 minX = index;
198 }
199 if (cubic[minY].y > cubic[index].y) {
200 minY = index;
201 }
202 if (cubic[maxX].x < cubic[index].x) {
203 maxX = index;
204 }
205 if (cubic[maxY].y < cubic[index].y) {
206 maxY = index;
207 }
208 }
209 for (index = 0; index < 4; ++index) {
210 if (approximately_equal(cubic[index].x, cubic[minX].x)) {
211 minXSet |= 1 << index;
212 }
213 if (approximately_equal(cubic[index].y, cubic[minY].y)) {
214 minYSet |= 1 << index;
215 }
216 }
217 if (minXSet == 0xF) { // test for vertical line
218 if (minYSet == 0xF) { // return 1 if all four are coincident
219 return coincident_line(cubic, reduction);
220 }
221 return vertical_line(cubic, reduction);
222 }
223 if (minYSet == 0xF) { // test for horizontal line
224 return horizontal_line(cubic, reduction);
225 }
226 int result = check_linear(cubic, reduction, minX, maxX, minY, maxY);
227 if (result) {
228 return result;
229 }
caryclark@google.coma3f05fa2012-06-01 17:44:28 +0000230 if (allowQuadratics && (result = check_quadratic(cubic, reduction))) {
caryclark@google.com639df892012-01-10 21:46:10 +0000231 return result;
232 }
233 memcpy(reduction, cubic, sizeof(Cubic));
234 return 4;
235}