caryclark@google.com | 9e49fb6 | 2012-08-27 14:11:33 +0000 | [diff] [blame] | 1 | /* |
| 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.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 7 | #include "CurveIntersection.h" |
caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 8 | #include "Extrema.h" |
| 9 | #include "IntersectionUtilities.h" |
| 10 | #include "LineParameters.h" |
| 11 | |
| 12 | static 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 | |
| 19 | static int coincident_line(const Quadratic& quad, Quadratic& reduction) { |
| 20 | reduction[0] = reduction[1] = quad[0]; |
| 21 | return 1; |
| 22 | } |
| 23 | |
| 24 | static 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.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 30 | 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] | 31 | 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 | |
| 41 | static 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.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 47 | 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] | 48 | 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 | |
| 58 | static 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.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 69 | if (!isLinear(quad, startIndex, endIndex)) { |
caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 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) { |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 88 | 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] | 89 | } else { |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 90 | 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] | 91 | } |
| 92 | if (root) { |
| 93 | _Point extrema; |
| 94 | extrema.x = interp_quad_coords(quad[0].x, quad[1].x, quad[2].x, tValue); |
caryclark@google.com | 9f60291 | 2013-01-24 21:47:16 +0000 | [diff] [blame^] | 95 | extrema.y = interp_quad_coords(quad[0].y, quad[1].y, quad[2].y, tValue); |
caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 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) |
caryclark@google.com | 9f3e9a5 | 2012-12-10 12:50:53 +0000 | [diff] [blame] | 103 | ^ (quad[0].x < quad[2].x); |
caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 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) |
caryclark@google.com | 9f3e9a5 | 2012-12-10 12:50:53 +0000 | [diff] [blame] | 109 | ^ (quad[0].y < quad[2].y); |
caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 110 | } |
| 111 | reduction[replace] = extrema; |
| 112 | } |
| 113 | return 2; |
| 114 | } |
| 115 | |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 116 | bool isLinear(const Quadratic& quad, int startIndex, int endIndex) { |
| 117 | LineParameters lineParameters; |
| 118 | lineParameters.quadEndPoints(quad, startIndex, endIndex); |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 119 | // 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.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 123 | } |
| 124 | |
caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 125 | // reduce to a quadratic or smaller |
| 126 | // look for identical points |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 127 | // look for all four points in a line |
caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 128 | // 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 |
| 131 | int 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.com | 6d0032a | 2013-01-04 19:41:13 +0000 | [diff] [blame] | 151 | if (AlmostEqualUlps(quad[index].x, quad[minX].x)) { |
caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 152 | minXSet |= 1 << index; |
| 153 | } |
caryclark@google.com | 6d0032a | 2013-01-04 19:41:13 +0000 | [diff] [blame] | 154 | if (AlmostEqualUlps(quad[index].y, quad[minY].y)) { |
caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 155 | minYSet |= 1 << index; |
| 156 | } |
| 157 | } |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 158 | if (minXSet == 0x7) { // test for vertical line |
| 159 | if (minYSet == 0x7) { // return 1 if all four are coincident |
caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 160 | 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 | } |