blob: f85cdc86ebccda1ec526afa4ff3fdc1b3c53e310 [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;
caryclark@google.comfa0588f2012-04-26 21:01:06 +000024 if (findExtrema(quad[0].y, quad[1].y, quad[2].y, &tValue)) {
caryclark@google.com639df892012-01-10 21:46:10 +000025 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;
caryclark@google.comfa0588f2012-04-26 21:01:06 +000041 if (findExtrema(quad[0].x, quad[1].x, quad[2].x, &tValue)) {
caryclark@google.com639df892012-01-10 21:46:10 +000042 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 }
caryclark@google.com15fa1382012-05-07 20:49:36 +000063 if (!isLinear(quad, startIndex, endIndex)) {
caryclark@google.com639df892012-01-10 21:46:10 +000064 return 0;
65 }
66 // four are colinear: return line formed by outside
67 reduction[0] = quad[0];
68 reduction[1] = quad[2];
69 int sameSide;
70 bool useX = quad[maxX].x - quad[minX].x >= quad[maxY].y - quad[minY].y;
71 if (useX) {
72 sameSide = sign(quad[0].x - quad[1].x) + sign(quad[2].x - quad[1].x);
73 } else {
74 sameSide = sign(quad[0].y - quad[1].y) + sign(quad[2].y - quad[1].y);
75 }
76 if ((sameSide & 3) != 2) {
77 return 2;
78 }
79 double tValue;
80 int root;
81 if (useX) {
caryclark@google.comfa0588f2012-04-26 21:01:06 +000082 root = findExtrema(quad[0].x, quad[1].x, quad[2].x, &tValue);
caryclark@google.com639df892012-01-10 21:46:10 +000083 } else {
caryclark@google.comfa0588f2012-04-26 21:01:06 +000084 root = findExtrema(quad[0].y, quad[1].y, quad[2].y, &tValue);
caryclark@google.com639df892012-01-10 21:46:10 +000085 }
86 if (root) {
87 _Point extrema;
88 extrema.x = interp_quad_coords(quad[0].x, quad[1].x, quad[2].x, tValue);
89 extrema.y = interp_quad_coords(quad[0].x, quad[1].x, quad[2].x, tValue);
90 // sameSide > 0 means mid is smaller than either [0] or [2], so replace smaller
91 int replace;
92 if (useX) {
93 if (extrema.x < quad[0].x ^ extrema.x < quad[2].x) {
94 return 2;
95 }
96 replace = (extrema.x < quad[0].x | extrema.x < quad[2].x)
97 ^ quad[0].x < quad[2].x;
98 } else {
99 if (extrema.y < quad[0].y ^ extrema.y < quad[2].y) {
100 return 2;
101 }
102 replace = (extrema.y < quad[0].y | extrema.y < quad[2].y)
103 ^ quad[0].y < quad[2].y;
104 }
105 reduction[replace] = extrema;
106 }
107 return 2;
108}
109
caryclark@google.com15fa1382012-05-07 20:49:36 +0000110bool isLinear(const Quadratic& quad, int startIndex, int endIndex) {
111 LineParameters lineParameters;
112 lineParameters.quadEndPoints(quad, startIndex, endIndex);
113 double normalSquared = lineParameters.normalSquared();
114 double distance = lineParameters.controlPtDistance(quad); // not normalized
115 double limit = normalSquared * SquaredEpsilon;
116 double distSq = distance * distance;
117 return distSq <= limit;
118}
119
caryclark@google.com639df892012-01-10 21:46:10 +0000120// reduce to a quadratic or smaller
121// look for identical points
122// look for all four points in a line
123 // note that three points in a line doesn't simplify a cubic
124// look for approximation with single quadratic
125 // save approximation with multiple quadratics for later
126int reduceOrder(const Quadratic& quad, Quadratic& reduction) {
127 int index, minX, maxX, minY, maxY;
128 int minXSet, minYSet;
129 minX = maxX = minY = maxY = 0;
130 minXSet = minYSet = 0;
131 for (index = 1; index < 3; ++index) {
132 if (quad[minX].x > quad[index].x) {
133 minX = index;
134 }
135 if (quad[minY].y > quad[index].y) {
136 minY = index;
137 }
138 if (quad[maxX].x < quad[index].x) {
139 maxX = index;
140 }
141 if (quad[maxY].y < quad[index].y) {
142 maxY = index;
143 }
144 }
145 for (index = 0; index < 3; ++index) {
146 if (approximately_equal(quad[index].x, quad[minX].x)) {
147 minXSet |= 1 << index;
148 }
149 if (approximately_equal(quad[index].y, quad[minY].y)) {
150 minYSet |= 1 << index;
151 }
152 }
caryclark@google.comfa0588f2012-04-26 21:01:06 +0000153 if (minXSet == 0x7) { // test for vertical line
154 if (minYSet == 0x7) { // return 1 if all four are coincident
caryclark@google.com639df892012-01-10 21:46:10 +0000155 return coincident_line(quad, reduction);
156 }
157 return vertical_line(quad, reduction);
158 }
159 if (minYSet == 0xF) { // test for horizontal line
160 return horizontal_line(quad, reduction);
161 }
162 int result = check_linear(quad, reduction, minX, maxX, minY, maxY);
163 if (result) {
164 return result;
165 }
166 memcpy(reduction, quad, sizeof(Quadratic));
167 return 3;
168}