blob: e6ce04444a7de25f95f310e8098ec4a656087172 [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_quad_coords(double a, double b, double c, double t)
7{
8 double ab = interp(a, b, t);
9 double bc = interp(b, c, t);
10 return interp(ab, bc, t);
11}
12
13static int coincident_line(const Quadratic& quad, Quadratic& reduction) {
14 reduction[0] = reduction[1] = quad[0];
15 return 1;
16}
17
18static int vertical_line(const Quadratic& quad, Quadratic& reduction) {
19 double tValue;
20 reduction[0] = quad[0];
21 reduction[1] = quad[2];
22 int smaller = reduction[1].y > reduction[0].y;
23 int larger = smaller ^ 1;
24 if (SkFindQuadExtrema(quad[0].y, quad[1].y, quad[2].y, &tValue)) {
25 double yExtrema = interp_quad_coords(quad[0].y, quad[1].y, quad[2].y, tValue);
26 if (reduction[smaller].y > yExtrema) {
27 reduction[smaller].y = yExtrema;
28 } else if (reduction[larger].y < yExtrema) {
29 reduction[larger].y = yExtrema;
30 }
31 }
32 return 2;
33}
34
35static int horizontal_line(const Quadratic& quad, Quadratic& reduction) {
36 double tValue;
37 reduction[0] = quad[0];
38 reduction[1] = quad[2];
39 int smaller = reduction[1].x > reduction[0].x;
40 int larger = smaller ^ 1;
41 if (SkFindQuadExtrema(quad[0].x, quad[1].x, quad[2].x, &tValue)) {
42 double xExtrema = interp_quad_coords(quad[0].x, quad[1].x, quad[2].x, tValue);
43 if (reduction[smaller].x > xExtrema) {
44 reduction[smaller].x = xExtrema;
45 } else if (reduction[larger].x < xExtrema) {
46 reduction[larger].x = xExtrema;
47 }
48 }
49 return 2;
50}
51
52static int check_linear(const Quadratic& quad, Quadratic& reduction,
53 int minX, int maxX, int minY, int maxY) {
54 int startIndex = 0;
55 int endIndex = 2;
56 while (quad[startIndex].approximatelyEqual(quad[endIndex])) {
57 --endIndex;
58 if (endIndex == 0) {
59 printf("%s shouldn't get here if all four points are about equal", __FUNCTION__);
60 assert(0);
61 }
62 }
63 LineParameters lineParameters;
64 lineParameters.quadEndPoints(quad, startIndex, endIndex);
65 double normalSquared = lineParameters.normalSquared();
66 double distance = lineParameters.controlPtDistance(quad); // not normalized
67 double limit = normalSquared * SquaredEpsilon;
68 double distSq = distance * distance;
69 if (distSq > limit) {
70 return 0;
71 }
72 // four are colinear: return line formed by outside
73 reduction[0] = quad[0];
74 reduction[1] = quad[2];
75 int sameSide;
76 bool useX = quad[maxX].x - quad[minX].x >= quad[maxY].y - quad[minY].y;
77 if (useX) {
78 sameSide = sign(quad[0].x - quad[1].x) + sign(quad[2].x - quad[1].x);
79 } else {
80 sameSide = sign(quad[0].y - quad[1].y) + sign(quad[2].y - quad[1].y);
81 }
82 if ((sameSide & 3) != 2) {
83 return 2;
84 }
85 double tValue;
86 int root;
87 if (useX) {
88 root = SkFindQuadExtrema(quad[0].x, quad[1].x, quad[2].x, &tValue);
89 } else {
90 root = SkFindQuadExtrema(quad[0].y, quad[1].y, quad[2].y, &tValue);
91 }
92 if (root) {
93 _Point extrema;
94 extrema.x = interp_quad_coords(quad[0].x, quad[1].x, quad[2].x, tValue);
95 extrema.y = interp_quad_coords(quad[0].x, quad[1].x, quad[2].x, tValue);
96 // sameSide > 0 means mid is smaller than either [0] or [2], so replace smaller
97 int replace;
98 if (useX) {
99 if (extrema.x < quad[0].x ^ extrema.x < quad[2].x) {
100 return 2;
101 }
102 replace = (extrema.x < quad[0].x | extrema.x < quad[2].x)
103 ^ quad[0].x < quad[2].x;
104 } else {
105 if (extrema.y < quad[0].y ^ extrema.y < quad[2].y) {
106 return 2;
107 }
108 replace = (extrema.y < quad[0].y | extrema.y < quad[2].y)
109 ^ quad[0].y < quad[2].y;
110 }
111 reduction[replace] = extrema;
112 }
113 return 2;
114}
115
116// reduce to a quadratic or smaller
117// look for identical points
118// look for all four points in a line
119 // note that three points in a line doesn't simplify a cubic
120// look for approximation with single quadratic
121 // save approximation with multiple quadratics for later
122int reduceOrder(const Quadratic& quad, Quadratic& reduction) {
123 int index, minX, maxX, minY, maxY;
124 int minXSet, minYSet;
125 minX = maxX = minY = maxY = 0;
126 minXSet = minYSet = 0;
127 for (index = 1; index < 3; ++index) {
128 if (quad[minX].x > quad[index].x) {
129 minX = index;
130 }
131 if (quad[minY].y > quad[index].y) {
132 minY = index;
133 }
134 if (quad[maxX].x < quad[index].x) {
135 maxX = index;
136 }
137 if (quad[maxY].y < quad[index].y) {
138 maxY = index;
139 }
140 }
141 for (index = 0; index < 3; ++index) {
142 if (approximately_equal(quad[index].x, quad[minX].x)) {
143 minXSet |= 1 << index;
144 }
145 if (approximately_equal(quad[index].y, quad[minY].y)) {
146 minYSet |= 1 << index;
147 }
148 }
149 if (minXSet == 0xF) { // test for vertical line
150 if (minYSet == 0xF) { // return 1 if all four are coincident
151 return coincident_line(quad, reduction);
152 }
153 return vertical_line(quad, reduction);
154 }
155 if (minYSet == 0xF) { // test for horizontal line
156 return horizontal_line(quad, reduction);
157 }
158 int result = check_linear(quad, reduction, minX, maxX, minY, maxY);
159 if (result) {
160 return result;
161 }
162 memcpy(reduction, quad, sizeof(Quadratic));
163 return 3;
164}