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