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 | */ |
george | 9eb182a | 2014-06-20 12:01:06 -0700 | [diff] [blame] | 7 | |
| 8 | #ifndef SkLineParameters_DEFINED |
| 9 | #define SkLineParameters_DEFINED |
| 10 | |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 11 | #include "SkPathOpsCubic.h" |
| 12 | #include "SkPathOpsLine.h" |
| 13 | #include "SkPathOpsQuad.h" |
| 14 | |
| 15 | // Sources |
| 16 | // computer-aided design - volume 22 number 9 november 1990 pp 538 - 549 |
| 17 | // online at http://cagd.cs.byu.edu/~tom/papers/bezclip.pdf |
| 18 | |
| 19 | // This turns a line segment into a parameterized line, of the form |
| 20 | // ax + by + c = 0 |
| 21 | // When a^2 + b^2 == 1, the line is normalized. |
| 22 | // The distance to the line for (x, y) is d(x,y) = ax + by + c |
| 23 | // |
| 24 | // Note that the distances below are not necessarily normalized. To get the true |
| 25 | // distance, it's necessary to either call normalize() after xxxEndPoints(), or |
| 26 | // divide the result of xxxDistance() by sqrt(normalSquared()) |
| 27 | |
| 28 | class SkLineParameters { |
| 29 | public: |
commit-bot@chromium.org | 866f4e3 | 2013-11-21 17:04:29 +0000 | [diff] [blame] | 30 | |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 31 | bool cubicEndPoints(const SkDCubic& pts) { |
commit-bot@chromium.org | 866f4e3 | 2013-11-21 17:04:29 +0000 | [diff] [blame] | 32 | int endIndex = 1; |
| 33 | cubicEndPoints(pts, 0, endIndex); |
| 34 | if (dy() != 0) { |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 35 | return true; |
commit-bot@chromium.org | 866f4e3 | 2013-11-21 17:04:29 +0000 | [diff] [blame] | 36 | } |
| 37 | if (dx() == 0) { |
| 38 | cubicEndPoints(pts, 0, ++endIndex); |
| 39 | SkASSERT(endIndex == 2); |
| 40 | if (dy() != 0) { |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 41 | return true; |
caryclark@google.com | cffbcc3 | 2013-06-04 17:59:42 +0000 | [diff] [blame] | 42 | } |
commit-bot@chromium.org | 866f4e3 | 2013-11-21 17:04:29 +0000 | [diff] [blame] | 43 | if (dx() == 0) { |
| 44 | cubicEndPoints(pts, 0, ++endIndex); // line |
| 45 | SkASSERT(endIndex == 3); |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 46 | return false; |
commit-bot@chromium.org | 866f4e3 | 2013-11-21 17:04:29 +0000 | [diff] [blame] | 47 | } |
| 48 | } |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 49 | // FIXME: after switching to round sort, remove bumping fA |
commit-bot@chromium.org | 866f4e3 | 2013-11-21 17:04:29 +0000 | [diff] [blame] | 50 | if (dx() < 0) { // only worry about y bias when breaking cw/ccw tie |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 51 | return true; |
commit-bot@chromium.org | 866f4e3 | 2013-11-21 17:04:29 +0000 | [diff] [blame] | 52 | } |
| 53 | // if cubic tangent is on x axis, look at next control point to break tie |
| 54 | // control point may be approximate, so it must move significantly to account for error |
| 55 | if (NotAlmostEqualUlps(pts[0].fY, pts[++endIndex].fY)) { |
| 56 | if (pts[0].fY > pts[endIndex].fY) { |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 57 | fA = DBL_EPSILON; // push it from 0 to slightly negative (y() returns -a) |
commit-bot@chromium.org | 866f4e3 | 2013-11-21 17:04:29 +0000 | [diff] [blame] | 58 | } |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 59 | return true; |
commit-bot@chromium.org | 866f4e3 | 2013-11-21 17:04:29 +0000 | [diff] [blame] | 60 | } |
| 61 | if (endIndex == 3) { |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 62 | return true; |
commit-bot@chromium.org | 866f4e3 | 2013-11-21 17:04:29 +0000 | [diff] [blame] | 63 | } |
| 64 | SkASSERT(endIndex == 2); |
| 65 | if (pts[0].fY > pts[3].fY) { |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 66 | fA = DBL_EPSILON; // push it from 0 to slightly negative (y() returns -a) |
caryclark@google.com | cffbcc3 | 2013-06-04 17:59:42 +0000 | [diff] [blame] | 67 | } |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 68 | return true; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | void cubicEndPoints(const SkDCubic& pts, int s, int e) { |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 72 | fA = pts[s].fY - pts[e].fY; |
| 73 | fB = pts[e].fX - pts[s].fX; |
| 74 | fC = pts[s].fX * pts[e].fY - pts[e].fX * pts[s].fY; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 75 | } |
| 76 | |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 77 | double cubicPart(const SkDCubic& part) { |
| 78 | cubicEndPoints(part); |
| 79 | if (part[0] == part[1] || ((const SkDLine& ) part[0]).nearRay(part[2])) { |
| 80 | return pointDistance(part[3]); |
| 81 | } |
| 82 | return pointDistance(part[2]); |
| 83 | } |
| 84 | |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 85 | void lineEndPoints(const SkDLine& pts) { |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 86 | fA = pts[0].fY - pts[1].fY; |
| 87 | fB = pts[1].fX - pts[0].fX; |
| 88 | fC = pts[0].fX * pts[1].fY - pts[1].fX * pts[0].fY; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 89 | } |
| 90 | |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 91 | bool quadEndPoints(const SkDQuad& pts) { |
caryclark@google.com | cffbcc3 | 2013-06-04 17:59:42 +0000 | [diff] [blame] | 92 | quadEndPoints(pts, 0, 1); |
commit-bot@chromium.org | 866f4e3 | 2013-11-21 17:04:29 +0000 | [diff] [blame] | 93 | if (dy() != 0) { |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 94 | return true; |
commit-bot@chromium.org | 866f4e3 | 2013-11-21 17:04:29 +0000 | [diff] [blame] | 95 | } |
| 96 | if (dx() == 0) { |
caryclark@google.com | cffbcc3 | 2013-06-04 17:59:42 +0000 | [diff] [blame] | 97 | quadEndPoints(pts, 0, 2); |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 98 | return false; |
commit-bot@chromium.org | 866f4e3 | 2013-11-21 17:04:29 +0000 | [diff] [blame] | 99 | } |
| 100 | if (dx() < 0) { // only worry about y bias when breaking cw/ccw tie |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 101 | return true; |
commit-bot@chromium.org | 866f4e3 | 2013-11-21 17:04:29 +0000 | [diff] [blame] | 102 | } |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 103 | // FIXME: after switching to round sort, remove this |
commit-bot@chromium.org | 866f4e3 | 2013-11-21 17:04:29 +0000 | [diff] [blame] | 104 | if (pts[0].fY > pts[2].fY) { |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 105 | fA = DBL_EPSILON; |
caryclark@google.com | cffbcc3 | 2013-06-04 17:59:42 +0000 | [diff] [blame] | 106 | } |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 107 | return true; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | void quadEndPoints(const SkDQuad& pts, int s, int e) { |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 111 | fA = pts[s].fY - pts[e].fY; |
| 112 | fB = pts[e].fX - pts[s].fX; |
| 113 | fC = pts[s].fX * pts[e].fY - pts[e].fX * pts[s].fY; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 114 | } |
| 115 | |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 116 | double quadPart(const SkDQuad& part) { |
| 117 | quadEndPoints(part); |
| 118 | return pointDistance(part[2]); |
| 119 | } |
| 120 | |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 121 | double normalSquared() const { |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 122 | return fA * fA + fB * fB; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | bool normalize() { |
| 126 | double normal = sqrt(normalSquared()); |
| 127 | if (approximately_zero(normal)) { |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 128 | fA = fB = fC = 0; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 129 | return false; |
| 130 | } |
| 131 | double reciprocal = 1 / normal; |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 132 | fA *= reciprocal; |
| 133 | fB *= reciprocal; |
| 134 | fC *= reciprocal; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 135 | return true; |
| 136 | } |
| 137 | |
| 138 | void cubicDistanceY(const SkDCubic& pts, SkDCubic& distance) const { |
| 139 | double oneThird = 1 / 3.0; |
| 140 | for (int index = 0; index < 4; ++index) { |
| 141 | distance[index].fX = index * oneThird; |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 142 | distance[index].fY = fA * pts[index].fX + fB * pts[index].fY + fC; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 143 | } |
| 144 | } |
| 145 | |
| 146 | void quadDistanceY(const SkDQuad& pts, SkDQuad& distance) const { |
| 147 | double oneHalf = 1 / 2.0; |
| 148 | for (int index = 0; index < 3; ++index) { |
| 149 | distance[index].fX = index * oneHalf; |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 150 | distance[index].fY = fA * pts[index].fX + fB * pts[index].fY + fC; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 151 | } |
| 152 | } |
| 153 | |
| 154 | double controlPtDistance(const SkDCubic& pts, int index) const { |
| 155 | SkASSERT(index == 1 || index == 2); |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 156 | return fA * pts[index].fX + fB * pts[index].fY + fC; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | double controlPtDistance(const SkDQuad& pts) const { |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 160 | return fA * pts[1].fX + fB * pts[1].fY + fC; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | double pointDistance(const SkDPoint& pt) const { |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 164 | return fA * pt.fX + fB * pt.fY + fC; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | double dx() const { |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 168 | return fB; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | double dy() const { |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 172 | return -fA; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | private: |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 176 | double fA; |
| 177 | double fB; |
| 178 | double fC; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 179 | }; |
george | 9eb182a | 2014-06-20 12:01:06 -0700 | [diff] [blame] | 180 | |
| 181 | #endif |