blob: 096f6b6643017dca8d853377d7b3f04c826b7b11 [file] [log] [blame]
caryclark@google.com9e49fb62012-08-27 14:11:33 +00001/*
2 * Copyright 2012 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
caryclark@google.comc6825902012-02-03 22:07:47 +00007#include "CurveIntersection.h"
caryclark@google.com639df892012-01-10 21:46:10 +00008#include "Extrema.h"
9#include "IntersectionUtilities.h"
10#include "LineParameters.h"
11
12static double interp_quad_coords(double a, double b, double c, double t)
13{
14 double ab = interp(a, b, t);
15 double bc = interp(b, c, t);
16 return interp(ab, bc, t);
17}
18
19static int coincident_line(const Quadratic& quad, Quadratic& reduction) {
20 reduction[0] = reduction[1] = quad[0];
21 return 1;
22}
23
24static int vertical_line(const Quadratic& quad, Quadratic& reduction) {
25 double tValue;
26 reduction[0] = quad[0];
27 reduction[1] = quad[2];
28 int smaller = reduction[1].y > reduction[0].y;
29 int larger = smaller ^ 1;
caryclark@google.comfa0588f2012-04-26 21:01:06 +000030 if (findExtrema(quad[0].y, quad[1].y, quad[2].y, &tValue)) {
caryclark@google.com639df892012-01-10 21:46:10 +000031 double yExtrema = interp_quad_coords(quad[0].y, quad[1].y, quad[2].y, tValue);
32 if (reduction[smaller].y > yExtrema) {
33 reduction[smaller].y = yExtrema;
34 } else if (reduction[larger].y < yExtrema) {
35 reduction[larger].y = yExtrema;
36 }
37 }
38 return 2;
39}
40
41static int horizontal_line(const Quadratic& quad, Quadratic& reduction) {
42 double tValue;
43 reduction[0] = quad[0];
44 reduction[1] = quad[2];
45 int smaller = reduction[1].x > reduction[0].x;
46 int larger = smaller ^ 1;
caryclark@google.comfa0588f2012-04-26 21:01:06 +000047 if (findExtrema(quad[0].x, quad[1].x, quad[2].x, &tValue)) {
caryclark@google.com639df892012-01-10 21:46:10 +000048 double xExtrema = interp_quad_coords(quad[0].x, quad[1].x, quad[2].x, tValue);
49 if (reduction[smaller].x > xExtrema) {
50 reduction[smaller].x = xExtrema;
51 } else if (reduction[larger].x < xExtrema) {
52 reduction[larger].x = xExtrema;
53 }
54 }
55 return 2;
56}
57
58static int check_linear(const Quadratic& quad, Quadratic& reduction,
59 int minX, int maxX, int minY, int maxY) {
60 int startIndex = 0;
61 int endIndex = 2;
62 while (quad[startIndex].approximatelyEqual(quad[endIndex])) {
63 --endIndex;
64 if (endIndex == 0) {
65 printf("%s shouldn't get here if all four points are about equal", __FUNCTION__);
66 assert(0);
67 }
68 }
caryclark@google.com15fa1382012-05-07 20:49:36 +000069 if (!isLinear(quad, startIndex, endIndex)) {
caryclark@google.com639df892012-01-10 21:46:10 +000070 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) {
caryclark@google.comfa0588f2012-04-26 21:01:06 +000088 root = findExtrema(quad[0].x, quad[1].x, quad[2].x, &tValue);
caryclark@google.com639df892012-01-10 21:46:10 +000089 } else {
caryclark@google.comfa0588f2012-04-26 21:01:06 +000090 root = findExtrema(quad[0].y, quad[1].y, quad[2].y, &tValue);
caryclark@google.com639df892012-01-10 21:46:10 +000091 }
92 if (root) {
93 _Point extrema;
94 extrema.x = interp_quad_coords(quad[0].x, quad[1].x, quad[2].x, tValue);
caryclark@google.com9f602912013-01-24 21:47:16 +000095 extrema.y = interp_quad_coords(quad[0].y, quad[1].y, quad[2].y, tValue);
caryclark@google.com639df892012-01-10 21:46:10 +000096 // 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)
caryclark@google.com9f3e9a52012-12-10 12:50:53 +0000103 ^ (quad[0].x < quad[2].x);
caryclark@google.com639df892012-01-10 21:46:10 +0000104 } 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)
caryclark@google.com9f3e9a52012-12-10 12:50:53 +0000109 ^ (quad[0].y < quad[2].y);
caryclark@google.com639df892012-01-10 21:46:10 +0000110 }
111 reduction[replace] = extrema;
112 }
113 return 2;
114}
115
caryclark@google.com15fa1382012-05-07 20:49:36 +0000116bool isLinear(const Quadratic& quad, int startIndex, int endIndex) {
117 LineParameters lineParameters;
118 lineParameters.quadEndPoints(quad, startIndex, endIndex);
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000119 // FIXME: maybe it's possible to avoid this and compare non-normalized
120 lineParameters.normalize();
121 double distance = lineParameters.controlPtDistance(quad);
122 return approximately_zero(distance);
caryclark@google.com15fa1382012-05-07 20:49:36 +0000123}
124
caryclark@google.com639df892012-01-10 21:46:10 +0000125// reduce to a quadratic or smaller
126// look for identical points
rmistry@google.comd6176b02012-08-23 18:14:13 +0000127// look for all four points in a line
caryclark@google.com639df892012-01-10 21:46:10 +0000128 // note that three points in a line doesn't simplify a cubic
129// look for approximation with single quadratic
130 // save approximation with multiple quadratics for later
131int reduceOrder(const Quadratic& quad, Quadratic& reduction) {
132 int index, minX, maxX, minY, maxY;
133 int minXSet, minYSet;
134 minX = maxX = minY = maxY = 0;
135 minXSet = minYSet = 0;
136 for (index = 1; index < 3; ++index) {
137 if (quad[minX].x > quad[index].x) {
138 minX = index;
139 }
140 if (quad[minY].y > quad[index].y) {
141 minY = index;
142 }
143 if (quad[maxX].x < quad[index].x) {
144 maxX = index;
145 }
146 if (quad[maxY].y < quad[index].y) {
147 maxY = index;
148 }
149 }
150 for (index = 0; index < 3; ++index) {
caryclark@google.com6d0032a2013-01-04 19:41:13 +0000151 if (AlmostEqualUlps(quad[index].x, quad[minX].x)) {
caryclark@google.com639df892012-01-10 21:46:10 +0000152 minXSet |= 1 << index;
153 }
caryclark@google.com6d0032a2013-01-04 19:41:13 +0000154 if (AlmostEqualUlps(quad[index].y, quad[minY].y)) {
caryclark@google.com639df892012-01-10 21:46:10 +0000155 minYSet |= 1 << index;
156 }
157 }
caryclark@google.comfa0588f2012-04-26 21:01:06 +0000158 if (minXSet == 0x7) { // test for vertical line
159 if (minYSet == 0x7) { // return 1 if all four are coincident
caryclark@google.com639df892012-01-10 21:46:10 +0000160 return coincident_line(quad, reduction);
161 }
162 return vertical_line(quad, reduction);
163 }
164 if (minYSet == 0xF) { // test for horizontal line
165 return horizontal_line(quad, reduction);
166 }
167 int result = check_linear(quad, reduction, minX, maxX, minY, maxY);
168 if (result) {
169 return result;
170 }
171 memcpy(reduction, quad, sizeof(Quadratic));
172 return 3;
173}