caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 1 | #include "CurveIntersection.h" |
caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 2 | #include "Extrema.h" |
| 3 | #include "IntersectionUtilities.h" |
| 4 | #include "LineParameters.h" |
| 5 | |
| 6 | static 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 | |
| 13 | static int coincident_line(const Quadratic& quad, Quadratic& reduction) { |
| 14 | reduction[0] = reduction[1] = quad[0]; |
| 15 | return 1; |
| 16 | } |
| 17 | |
| 18 | static 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.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 24 | if (findExtrema(quad[0].y, quad[1].y, quad[2].y, &tValue)) { |
caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 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 | |
| 35 | static 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.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 41 | if (findExtrema(quad[0].x, quad[1].x, quad[2].x, &tValue)) { |
caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 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 | |
| 52 | static 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.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 63 | if (!isLinear(quad, startIndex, endIndex)) { |
caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 64 | 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.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 82 | root = findExtrema(quad[0].x, quad[1].x, quad[2].x, &tValue); |
caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 83 | } else { |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 84 | root = findExtrema(quad[0].y, quad[1].y, quad[2].y, &tValue); |
caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 85 | } |
| 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.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 110 | bool isLinear(const Quadratic& quad, int startIndex, int endIndex) { |
| 111 | LineParameters lineParameters; |
| 112 | lineParameters.quadEndPoints(quad, startIndex, endIndex); |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame^] | 113 | // FIXME: maybe it's possible to avoid this and compare non-normalized |
| 114 | lineParameters.normalize(); |
| 115 | double distance = lineParameters.controlPtDistance(quad); |
| 116 | return approximately_zero(distance); |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 117 | } |
| 118 | |
caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 119 | // reduce to a quadratic or smaller |
| 120 | // look for identical points |
| 121 | // look for all four points in a line |
| 122 | // note that three points in a line doesn't simplify a cubic |
| 123 | // look for approximation with single quadratic |
| 124 | // save approximation with multiple quadratics for later |
| 125 | int reduceOrder(const Quadratic& quad, Quadratic& reduction) { |
| 126 | int index, minX, maxX, minY, maxY; |
| 127 | int minXSet, minYSet; |
| 128 | minX = maxX = minY = maxY = 0; |
| 129 | minXSet = minYSet = 0; |
| 130 | for (index = 1; index < 3; ++index) { |
| 131 | if (quad[minX].x > quad[index].x) { |
| 132 | minX = index; |
| 133 | } |
| 134 | if (quad[minY].y > quad[index].y) { |
| 135 | minY = index; |
| 136 | } |
| 137 | if (quad[maxX].x < quad[index].x) { |
| 138 | maxX = index; |
| 139 | } |
| 140 | if (quad[maxY].y < quad[index].y) { |
| 141 | maxY = index; |
| 142 | } |
| 143 | } |
| 144 | for (index = 0; index < 3; ++index) { |
| 145 | if (approximately_equal(quad[index].x, quad[minX].x)) { |
| 146 | minXSet |= 1 << index; |
| 147 | } |
| 148 | if (approximately_equal(quad[index].y, quad[minY].y)) { |
| 149 | minYSet |= 1 << index; |
| 150 | } |
| 151 | } |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 152 | if (minXSet == 0x7) { // test for vertical line |
| 153 | if (minYSet == 0x7) { // return 1 if all four are coincident |
caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 154 | return coincident_line(quad, reduction); |
| 155 | } |
| 156 | return vertical_line(quad, reduction); |
| 157 | } |
| 158 | if (minYSet == 0xF) { // test for horizontal line |
| 159 | return horizontal_line(quad, reduction); |
| 160 | } |
| 161 | int result = check_linear(quad, reduction, minX, maxX, minY, maxY); |
| 162 | if (result) { |
| 163 | return result; |
| 164 | } |
| 165 | memcpy(reduction, quad, sizeof(Quadratic)); |
| 166 | return 3; |
| 167 | } |