caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +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 | 27c015d | 2016-09-23 05:47:20 -0700 | [diff] [blame] | 7 | #include "SkGeometry.h" |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 8 | #include "SkReduceOrder.h" |
| 9 | |
| 10 | int SkReduceOrder::reduce(const SkDLine& line) { |
| 11 | fLine[0] = line[0]; |
| 12 | int different = line[0] != line[1]; |
| 13 | fLine[1] = line[different]; |
| 14 | return 1 + different; |
| 15 | } |
| 16 | |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 17 | static int coincident_line(const SkDQuad& quad, SkDQuad& reduction) { |
| 18 | reduction[0] = reduction[1] = quad[0]; |
| 19 | return 1; |
| 20 | } |
| 21 | |
| 22 | static int reductionLineCount(const SkDQuad& reduction) { |
| 23 | return 1 + !reduction[0].approximatelyEqual(reduction[1]); |
| 24 | } |
| 25 | |
caryclark@google.com | 927b702 | 2013-11-25 14:18:21 +0000 | [diff] [blame] | 26 | static int vertical_line(const SkDQuad& quad, SkDQuad& reduction) { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 27 | reduction[0] = quad[0]; |
| 28 | reduction[1] = quad[2]; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 29 | return reductionLineCount(reduction); |
| 30 | } |
| 31 | |
caryclark@google.com | 927b702 | 2013-11-25 14:18:21 +0000 | [diff] [blame] | 32 | static int horizontal_line(const SkDQuad& quad, SkDQuad& reduction) { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 33 | reduction[0] = quad[0]; |
| 34 | reduction[1] = quad[2]; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 35 | return reductionLineCount(reduction); |
| 36 | } |
| 37 | |
caryclark@google.com | 927b702 | 2013-11-25 14:18:21 +0000 | [diff] [blame] | 38 | static int check_linear(const SkDQuad& quad, |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 39 | int minX, int maxX, int minY, int maxY, SkDQuad& reduction) { |
caryclark | 2d9c59f | 2015-10-30 13:56:50 -0700 | [diff] [blame] | 40 | if (!quad.isLinear(0, 2)) { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 41 | return 0; |
| 42 | } |
| 43 | // four are colinear: return line formed by outside |
| 44 | reduction[0] = quad[0]; |
| 45 | reduction[1] = quad[2]; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 46 | return reductionLineCount(reduction); |
| 47 | } |
| 48 | |
| 49 | // reduce to a quadratic or smaller |
| 50 | // look for identical points |
| 51 | // look for all four points in a line |
| 52 | // note that three points in a line doesn't simplify a cubic |
| 53 | // look for approximation with single quadratic |
| 54 | // save approximation with multiple quadratics for later |
caryclark@google.com | 927b702 | 2013-11-25 14:18:21 +0000 | [diff] [blame] | 55 | int SkReduceOrder::reduce(const SkDQuad& quad) { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 56 | int index, minX, maxX, minY, maxY; |
| 57 | int minXSet, minYSet; |
| 58 | minX = maxX = minY = maxY = 0; |
| 59 | minXSet = minYSet = 0; |
| 60 | for (index = 1; index < 3; ++index) { |
| 61 | if (quad[minX].fX > quad[index].fX) { |
| 62 | minX = index; |
| 63 | } |
| 64 | if (quad[minY].fY > quad[index].fY) { |
| 65 | minY = index; |
| 66 | } |
| 67 | if (quad[maxX].fX < quad[index].fX) { |
| 68 | maxX = index; |
| 69 | } |
| 70 | if (quad[maxY].fY < quad[index].fY) { |
| 71 | maxY = index; |
| 72 | } |
| 73 | } |
| 74 | for (index = 0; index < 3; ++index) { |
| 75 | if (AlmostEqualUlps(quad[index].fX, quad[minX].fX)) { |
| 76 | minXSet |= 1 << index; |
| 77 | } |
| 78 | if (AlmostEqualUlps(quad[index].fY, quad[minY].fY)) { |
| 79 | minYSet |= 1 << index; |
| 80 | } |
| 81 | } |
caryclark | 55888e4 | 2016-07-18 10:01:36 -0700 | [diff] [blame] | 82 | if ((minXSet & 0x05) == 0x5 && (minYSet & 0x05) == 0x5) { // test for degenerate |
| 83 | // this quad starts and ends at the same place, so never contributes |
| 84 | // to the fill |
| 85 | return coincident_line(quad, fQuad); |
| 86 | } |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 87 | if (minXSet == 0x7) { // test for vertical line |
caryclark@google.com | 927b702 | 2013-11-25 14:18:21 +0000 | [diff] [blame] | 88 | return vertical_line(quad, fQuad); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 89 | } |
caryclark | 65f5531 | 2014-11-13 06:58:52 -0800 | [diff] [blame] | 90 | if (minYSet == 0x7) { // test for horizontal line |
caryclark@google.com | 927b702 | 2013-11-25 14:18:21 +0000 | [diff] [blame] | 91 | return horizontal_line(quad, fQuad); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 92 | } |
caryclark@google.com | 927b702 | 2013-11-25 14:18:21 +0000 | [diff] [blame] | 93 | int result = check_linear(quad, minX, maxX, minY, maxY, fQuad); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 94 | if (result) { |
| 95 | return result; |
| 96 | } |
| 97 | fQuad = quad; |
| 98 | return 3; |
| 99 | } |
| 100 | |
| 101 | //////////////////////////////////////////////////////////////////////////////////// |
| 102 | |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 103 | static int coincident_line(const SkDCubic& cubic, SkDCubic& reduction) { |
| 104 | reduction[0] = reduction[1] = cubic[0]; |
| 105 | return 1; |
| 106 | } |
| 107 | |
| 108 | static int reductionLineCount(const SkDCubic& reduction) { |
| 109 | return 1 + !reduction[0].approximatelyEqual(reduction[1]); |
| 110 | } |
| 111 | |
caryclark@google.com | 927b702 | 2013-11-25 14:18:21 +0000 | [diff] [blame] | 112 | static int vertical_line(const SkDCubic& cubic, SkDCubic& reduction) { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 113 | reduction[0] = cubic[0]; |
| 114 | reduction[1] = cubic[3]; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 115 | return reductionLineCount(reduction); |
| 116 | } |
| 117 | |
caryclark@google.com | 927b702 | 2013-11-25 14:18:21 +0000 | [diff] [blame] | 118 | static int horizontal_line(const SkDCubic& cubic, SkDCubic& reduction) { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 119 | reduction[0] = cubic[0]; |
| 120 | reduction[1] = cubic[3]; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 121 | return reductionLineCount(reduction); |
| 122 | } |
| 123 | |
| 124 | // check to see if it is a quadratic or a line |
| 125 | static int check_quadratic(const SkDCubic& cubic, SkDCubic& reduction) { |
| 126 | double dx10 = cubic[1].fX - cubic[0].fX; |
| 127 | double dx23 = cubic[2].fX - cubic[3].fX; |
| 128 | double midX = cubic[0].fX + dx10 * 3 / 2; |
caryclark@google.com | 7ec5e39 | 2013-04-17 16:19:02 +0000 | [diff] [blame] | 129 | double sideAx = midX - cubic[3].fX; |
| 130 | double sideBx = dx23 * 3 / 2; |
| 131 | if (approximately_zero(sideAx) ? !approximately_equal(sideAx, sideBx) |
caryclark | b669300 | 2015-12-16 12:28:35 -0800 | [diff] [blame] | 132 | : !AlmostEqualUlps_Pin(sideAx, sideBx)) { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 133 | return 0; |
| 134 | } |
| 135 | double dy10 = cubic[1].fY - cubic[0].fY; |
| 136 | double dy23 = cubic[2].fY - cubic[3].fY; |
| 137 | double midY = cubic[0].fY + dy10 * 3 / 2; |
caryclark@google.com | 7ec5e39 | 2013-04-17 16:19:02 +0000 | [diff] [blame] | 138 | double sideAy = midY - cubic[3].fY; |
| 139 | double sideBy = dy23 * 3 / 2; |
| 140 | if (approximately_zero(sideAy) ? !approximately_equal(sideAy, sideBy) |
caryclark | b669300 | 2015-12-16 12:28:35 -0800 | [diff] [blame] | 141 | : !AlmostEqualUlps_Pin(sideAy, sideBy)) { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 142 | return 0; |
| 143 | } |
| 144 | reduction[0] = cubic[0]; |
| 145 | reduction[1].fX = midX; |
| 146 | reduction[1].fY = midY; |
| 147 | reduction[2] = cubic[3]; |
| 148 | return 3; |
| 149 | } |
| 150 | |
caryclark@google.com | 927b702 | 2013-11-25 14:18:21 +0000 | [diff] [blame] | 151 | static int check_linear(const SkDCubic& cubic, |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 152 | int minX, int maxX, int minY, int maxY, SkDCubic& reduction) { |
caryclark | 2d9c59f | 2015-10-30 13:56:50 -0700 | [diff] [blame] | 153 | if (!cubic.isLinear(0, 3)) { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 154 | return 0; |
| 155 | } |
| 156 | // four are colinear: return line formed by outside |
| 157 | reduction[0] = cubic[0]; |
| 158 | reduction[1] = cubic[3]; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 159 | return reductionLineCount(reduction); |
| 160 | } |
| 161 | |
| 162 | /* food for thought: |
| 163 | http://objectmix.com/graphics/132906-fast-precision-driven-cubic-quadratic-piecewise-degree-reduction-algos-2-a.html |
| 164 | |
| 165 | Given points c1, c2, c3 and c4 of a cubic Bezier, the points of the |
| 166 | corresponding quadratic Bezier are (given in convex combinations of |
| 167 | points): |
| 168 | |
| 169 | q1 = (11/13)c1 + (3/13)c2 -(3/13)c3 + (2/13)c4 |
| 170 | q2 = -c1 + (3/2)c2 + (3/2)c3 - c4 |
| 171 | q3 = (2/13)c1 - (3/13)c2 + (3/13)c3 + (11/13)c4 |
| 172 | |
| 173 | Of course, this curve does not interpolate the end-points, but it would |
| 174 | be interesting to see the behaviour of such a curve in an applet. |
| 175 | |
| 176 | -- |
| 177 | Kalle Rutanen |
| 178 | http://kaba.hilvi.org |
| 179 | |
| 180 | */ |
| 181 | |
| 182 | // reduce to a quadratic or smaller |
| 183 | // look for identical points |
| 184 | // look for all four points in a line |
| 185 | // note that three points in a line doesn't simplify a cubic |
| 186 | // look for approximation with single quadratic |
| 187 | // save approximation with multiple quadratics for later |
caryclark@google.com | 927b702 | 2013-11-25 14:18:21 +0000 | [diff] [blame] | 188 | int SkReduceOrder::reduce(const SkDCubic& cubic, Quadratics allowQuadratics) { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 189 | int index, minX, maxX, minY, maxY; |
| 190 | int minXSet, minYSet; |
| 191 | minX = maxX = minY = maxY = 0; |
| 192 | minXSet = minYSet = 0; |
| 193 | for (index = 1; index < 4; ++index) { |
| 194 | if (cubic[minX].fX > cubic[index].fX) { |
| 195 | minX = index; |
| 196 | } |
| 197 | if (cubic[minY].fY > cubic[index].fY) { |
| 198 | minY = index; |
| 199 | } |
| 200 | if (cubic[maxX].fX < cubic[index].fX) { |
| 201 | maxX = index; |
| 202 | } |
| 203 | if (cubic[maxY].fY < cubic[index].fY) { |
| 204 | maxY = index; |
| 205 | } |
| 206 | } |
| 207 | for (index = 0; index < 4; ++index) { |
| 208 | double cx = cubic[index].fX; |
| 209 | double cy = cubic[index].fY; |
| 210 | double denom = SkTMax(fabs(cx), SkTMax(fabs(cy), |
| 211 | SkTMax(fabs(cubic[minX].fX), fabs(cubic[minY].fY)))); |
| 212 | if (denom == 0) { |
| 213 | minXSet |= 1 << index; |
| 214 | minYSet |= 1 << index; |
| 215 | continue; |
| 216 | } |
| 217 | double inv = 1 / denom; |
| 218 | if (approximately_equal_half(cx * inv, cubic[minX].fX * inv)) { |
| 219 | minXSet |= 1 << index; |
| 220 | } |
| 221 | if (approximately_equal_half(cy * inv, cubic[minY].fY * inv)) { |
| 222 | minYSet |= 1 << index; |
| 223 | } |
| 224 | } |
| 225 | if (minXSet == 0xF) { // test for vertical line |
| 226 | if (minYSet == 0xF) { // return 1 if all four are coincident |
| 227 | return coincident_line(cubic, fCubic); |
| 228 | } |
caryclark@google.com | 927b702 | 2013-11-25 14:18:21 +0000 | [diff] [blame] | 229 | return vertical_line(cubic, fCubic); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 230 | } |
| 231 | if (minYSet == 0xF) { // test for horizontal line |
caryclark@google.com | 927b702 | 2013-11-25 14:18:21 +0000 | [diff] [blame] | 232 | return horizontal_line(cubic, fCubic); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 233 | } |
caryclark@google.com | 927b702 | 2013-11-25 14:18:21 +0000 | [diff] [blame] | 234 | int result = check_linear(cubic, minX, maxX, minY, maxY, fCubic); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 235 | if (result) { |
| 236 | return result; |
| 237 | } |
| 238 | if (allowQuadratics == SkReduceOrder::kAllow_Quadratics |
| 239 | && (result = check_quadratic(cubic, fCubic))) { |
| 240 | return result; |
| 241 | } |
| 242 | fCubic = cubic; |
| 243 | return 4; |
| 244 | } |
| 245 | |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 246 | SkPath::Verb SkReduceOrder::Quad(const SkPoint a[3], SkPoint* reducePts) { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 247 | SkDQuad quad; |
| 248 | quad.set(a); |
| 249 | SkReduceOrder reducer; |
caryclark@google.com | 927b702 | 2013-11-25 14:18:21 +0000 | [diff] [blame] | 250 | int order = reducer.reduce(quad); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 251 | if (order == 2) { // quad became line |
| 252 | for (int index = 0; index < order; ++index) { |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 253 | *reducePts++ = reducer.fLine[index].asSkPoint(); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 254 | } |
| 255 | } |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 256 | return SkPathOpsPointsToVerb(order - 1); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 257 | } |
| 258 | |
caryclark | 27c015d | 2016-09-23 05:47:20 -0700 | [diff] [blame] | 259 | SkPath::Verb SkReduceOrder::Conic(const SkConic& c, SkPoint* reducePts) { |
| 260 | SkPath::Verb verb = SkReduceOrder::Quad(c.fPts, reducePts); |
| 261 | if (verb > SkPath::kLine_Verb && c.fW == 1) { |
caryclark | 1049f12 | 2015-04-20 08:31:59 -0700 | [diff] [blame] | 262 | return SkPath::kQuad_Verb; |
| 263 | } |
| 264 | return verb == SkPath::kQuad_Verb ? SkPath::kConic_Verb : verb; |
| 265 | } |
| 266 | |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 267 | SkPath::Verb SkReduceOrder::Cubic(const SkPoint a[4], SkPoint* reducePts) { |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 268 | if (SkDPoint::ApproximatelyEqual(a[0], a[1]) && SkDPoint::ApproximatelyEqual(a[0], a[2]) |
| 269 | && SkDPoint::ApproximatelyEqual(a[0], a[3])) { |
| 270 | reducePts[0] = a[0]; |
| 271 | return SkPath::kMove_Verb; |
| 272 | } |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 273 | SkDCubic cubic; |
| 274 | cubic.set(a); |
| 275 | SkReduceOrder reducer; |
caryclark@google.com | 927b702 | 2013-11-25 14:18:21 +0000 | [diff] [blame] | 276 | int order = reducer.reduce(cubic, kAllow_Quadratics); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 277 | if (order == 2 || order == 3) { // cubic became line or quad |
| 278 | for (int index = 0; index < order; ++index) { |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 279 | *reducePts++ = reducer.fQuad[index].asSkPoint(); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 280 | } |
| 281 | } |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 282 | return SkPathOpsPointsToVerb(order - 1); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 283 | } |