blob: 6fa091db8ebdfa0578c16e7256a0daaac6b16d5c [file] [log] [blame]
caryclark@google.com07393ca2013-04-08 11:47:37 +00001/*
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 */
7#include "SkPathOpsLine.h"
8
caryclark@google.com4fdbb222013-07-23 15:27:41 +00009SkDPoint SkDLine::ptAtT(double t) const {
10 if (0 == t) {
11 return fPts[0];
12 }
13 if (1 == t) {
14 return fPts[1];
15 }
caryclark@google.com07393ca2013-04-08 11:47:37 +000016 double one_t = 1 - t;
17 SkDPoint result = { one_t * fPts[0].fX + t * fPts[1].fX, one_t * fPts[0].fY + t * fPts[1].fY };
18 return result;
19}
caryclark@google.comfa2aeee2013-07-15 13:29:13 +000020
21double SkDLine::exactPoint(const SkDPoint& xy) const {
22 if (xy == fPts[0]) { // do cheapest test first
23 return 0;
24 }
25 if (xy == fPts[1]) {
26 return 1;
27 }
28 return -1;
29}
30
caryclarkdac1d172014-06-17 05:15:38 -070031double SkDLine::nearPoint(const SkDPoint& xy, bool* unequal) const {
caryclark@google.comfa2aeee2013-07-15 13:29:13 +000032 if (!AlmostBetweenUlps(fPts[0].fX, xy.fX, fPts[1].fX)
33 || !AlmostBetweenUlps(fPts[0].fY, xy.fY, fPts[1].fY)) {
34 return -1;
35 }
36 // project a perpendicular ray from the point to the line; find the T on the line
37 SkDVector len = fPts[1] - fPts[0]; // the x/y magnitudes of the line
38 double denom = len.fX * len.fX + len.fY * len.fY; // see DLine intersectRay
39 SkDVector ab0 = xy - fPts[0];
40 double numer = len.fX * ab0.fX + ab0.fY * len.fY;
41 if (!between(0, numer, denom)) {
42 return -1;
43 }
caryclarkb6693002015-12-16 12:28:35 -080044 if (!denom) {
45 return 0;
46 }
caryclark@google.comfa2aeee2013-07-15 13:29:13 +000047 double t = numer / denom;
caryclark@google.com4fdbb222013-07-23 15:27:41 +000048 SkDPoint realPt = ptAtT(t);
caryclark@google.com570863f2013-09-16 15:55:01 +000049 double dist = realPt.distance(xy); // OPTIMIZATION: can we compare against distSq instead ?
caryclark@google.comfa2aeee2013-07-15 13:29:13 +000050 // find the ordinal in the original line with the largest unsigned exponent
51 double tiniest = SkTMin(SkTMin(SkTMin(fPts[0].fX, fPts[0].fY), fPts[1].fX), fPts[1].fY);
52 double largest = SkTMax(SkTMax(SkTMax(fPts[0].fX, fPts[0].fY), fPts[1].fX), fPts[1].fY);
53 largest = SkTMax(largest, -tiniest);
caryclarkb6693002015-12-16 12:28:35 -080054 if (!AlmostEqualUlps_Pin(largest, largest + dist)) { // is the dist within ULPS tolerance?
caryclark@google.comfa2aeee2013-07-15 13:29:13 +000055 return -1;
56 }
caryclarkdac1d172014-06-17 05:15:38 -070057 if (unequal) {
58 *unequal = (float) largest != (float) (largest + dist);
59 }
caryclark65b427c2014-09-18 10:32:57 -070060 t = SkPinT(t); // a looser pin breaks skpwww_lptemp_com_3
caryclark@google.comfa2aeee2013-07-15 13:29:13 +000061 SkASSERT(between(0, t, 1));
62 return t;
63}
64
caryclark@google.com570863f2013-09-16 15:55:01 +000065bool SkDLine::nearRay(const SkDPoint& xy) const {
66 // project a perpendicular ray from the point to the line; find the T on the line
67 SkDVector len = fPts[1] - fPts[0]; // the x/y magnitudes of the line
68 double denom = len.fX * len.fX + len.fY * len.fY; // see DLine intersectRay
69 SkDVector ab0 = xy - fPts[0];
70 double numer = len.fX * ab0.fX + ab0.fY * len.fY;
71 double t = numer / denom;
72 SkDPoint realPt = ptAtT(t);
73 double dist = realPt.distance(xy); // OPTIMIZATION: can we compare against distSq instead ?
74 // find the ordinal in the original line with the largest unsigned exponent
75 double tiniest = SkTMin(SkTMin(SkTMin(fPts[0].fX, fPts[0].fY), fPts[1].fX), fPts[1].fY);
76 double largest = SkTMax(SkTMax(SkTMax(fPts[0].fX, fPts[0].fY), fPts[1].fX), fPts[1].fY);
77 largest = SkTMax(largest, -tiniest);
78 return RoughlyEqualUlps(largest, largest + dist); // is the dist within ULPS tolerance?
79}
80
caryclark@google.comfa2aeee2013-07-15 13:29:13 +000081double SkDLine::ExactPointH(const SkDPoint& xy, double left, double right, double y) {
82 if (xy.fY == y) {
83 if (xy.fX == left) {
84 return 0;
85 }
86 if (xy.fX == right) {
87 return 1;
88 }
89 }
90 return -1;
91}
92
93double SkDLine::NearPointH(const SkDPoint& xy, double left, double right, double y) {
caryclark@google.com570863f2013-09-16 15:55:01 +000094 if (!AlmostBequalUlps(xy.fY, y)) {
caryclark@google.comfa2aeee2013-07-15 13:29:13 +000095 return -1;
96 }
97 if (!AlmostBetweenUlps(left, xy.fX, right)) {
98 return -1;
99 }
100 double t = (xy.fX - left) / (right - left);
101 t = SkPinT(t);
102 SkASSERT(between(0, t, 1));
caryclark@google.com570863f2013-09-16 15:55:01 +0000103 double realPtX = (1 - t) * left + t * right;
104 SkDVector distU = {xy.fY - y, xy.fX - realPtX};
105 double distSq = distU.fX * distU.fX + distU.fY * distU.fY;
106 double dist = sqrt(distSq); // OPTIMIZATION: can we compare against distSq instead ?
107 double tiniest = SkTMin(SkTMin(y, left), right);
108 double largest = SkTMax(SkTMax(y, left), right);
109 largest = SkTMax(largest, -tiniest);
110 if (!AlmostEqualUlps(largest, largest + dist)) { // is the dist within ULPS tolerance?
111 return -1;
112 }
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000113 return t;
114}
115
116double SkDLine::ExactPointV(const SkDPoint& xy, double top, double bottom, double x) {
117 if (xy.fX == x) {
118 if (xy.fY == top) {
119 return 0;
120 }
121 if (xy.fY == bottom) {
122 return 1;
123 }
124 }
125 return -1;
126}
127
128double SkDLine::NearPointV(const SkDPoint& xy, double top, double bottom, double x) {
caryclark@google.com570863f2013-09-16 15:55:01 +0000129 if (!AlmostBequalUlps(xy.fX, x)) {
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000130 return -1;
131 }
132 if (!AlmostBetweenUlps(top, xy.fY, bottom)) {
133 return -1;
134 }
135 double t = (xy.fY - top) / (bottom - top);
136 t = SkPinT(t);
137 SkASSERT(between(0, t, 1));
caryclark@google.com570863f2013-09-16 15:55:01 +0000138 double realPtY = (1 - t) * top + t * bottom;
139 SkDVector distU = {xy.fX - x, xy.fY - realPtY};
140 double distSq = distU.fX * distU.fX + distU.fY * distU.fY;
141 double dist = sqrt(distSq); // OPTIMIZATION: can we compare against distSq instead ?
142 double tiniest = SkTMin(SkTMin(x, top), bottom);
143 double largest = SkTMax(SkTMax(x, top), bottom);
144 largest = SkTMax(largest, -tiniest);
145 if (!AlmostEqualUlps(largest, largest + dist)) { // is the dist within ULPS tolerance?
146 return -1;
147 }
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000148 return t;
149}