blob: 345ef3744865529405f90923a6ae3d4df577f26b [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@android.com8a1c16f2008-12-17 15:59:43 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2008 The Android Open Source Project
reed@android.com8a1c16f2008-12-17 15:59:43 +00004 *
epoger@google.comec3ed6a2011-07-28 14:26:00 +00005 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
reed@android.com8a1c16f2008-12-17 15:59:43 +00007 */
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
caryclark936b7342014-07-11 12:14:51 -070010#include "SkMathPriv.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000011#include "SkPoint.h"
12
13void SkIPoint::rotateCW(SkIPoint* dst) const {
14 SkASSERT(dst);
15
16 // use a tmp in case this == dst
17 int32_t tmp = fX;
18 dst->fX = -fY;
19 dst->fY = tmp;
20}
21
22void SkIPoint::rotateCCW(SkIPoint* dst) const {
23 SkASSERT(dst);
24
25 // use a tmp in case this == dst
26 int32_t tmp = fX;
27 dst->fX = fY;
28 dst->fY = -tmp;
29}
30
31///////////////////////////////////////////////////////////////////////////////
32
reed@google.com7744c202011-05-06 19:26:26 +000033void SkPoint::setIRectFan(int l, int t, int r, int b, size_t stride) {
34 SkASSERT(stride >= sizeof(SkPoint));
rmistry@google.comfbfcd562012-08-23 18:09:54 +000035
36 ((SkPoint*)((intptr_t)this + 0 * stride))->set(SkIntToScalar(l),
reed@google.com7744c202011-05-06 19:26:26 +000037 SkIntToScalar(t));
rmistry@google.comfbfcd562012-08-23 18:09:54 +000038 ((SkPoint*)((intptr_t)this + 1 * stride))->set(SkIntToScalar(l),
reed@google.com7744c202011-05-06 19:26:26 +000039 SkIntToScalar(b));
rmistry@google.comfbfcd562012-08-23 18:09:54 +000040 ((SkPoint*)((intptr_t)this + 2 * stride))->set(SkIntToScalar(r),
reed@google.com7744c202011-05-06 19:26:26 +000041 SkIntToScalar(b));
rmistry@google.comfbfcd562012-08-23 18:09:54 +000042 ((SkPoint*)((intptr_t)this + 3 * stride))->set(SkIntToScalar(r),
reed@google.com7744c202011-05-06 19:26:26 +000043 SkIntToScalar(t));
44}
45
reed@android.com8a1c16f2008-12-17 15:59:43 +000046void SkPoint::rotateCW(SkPoint* dst) const {
47 SkASSERT(dst);
48
49 // use a tmp in case this == dst
50 SkScalar tmp = fX;
51 dst->fX = -fY;
52 dst->fY = tmp;
53}
54
55void SkPoint::rotateCCW(SkPoint* dst) const {
56 SkASSERT(dst);
57
58 // use a tmp in case this == dst
59 SkScalar tmp = fX;
60 dst->fX = fY;
61 dst->fY = -tmp;
62}
63
64void SkPoint::scale(SkScalar scale, SkPoint* dst) const {
65 SkASSERT(dst);
66 dst->set(SkScalarMul(fX, scale), SkScalarMul(fY, scale));
67}
68
reed@android.com8a1c16f2008-12-17 15:59:43 +000069bool SkPoint::normalize() {
70 return this->setLength(fX, fY, SK_Scalar1);
71}
72
73bool SkPoint::setNormalize(SkScalar x, SkScalar y) {
74 return this->setLength(x, y, SK_Scalar1);
75}
76
77bool SkPoint::setLength(SkScalar length) {
78 return this->setLength(fX, fY, length);
79}
80
epoger@google.com94fa43c2012-04-11 17:51:01 +000081// Returns the square of the Euclidian distance to (dx,dy).
82static inline float getLengthSquared(float dx, float dy) {
83 return dx * dx + dy * dy;
84}
85
86// Calculates the square of the Euclidian distance to (dx,dy) and stores it in
87// *lengthSquared. Returns true if the distance is judged to be "nearly zero".
88//
89// This logic is encapsulated in a helper method to make it explicit that we
90// always perform this check in the same manner, to avoid inconsistencies
91// (see http://code.google.com/p/skia/issues/detail?id=560 ).
92static inline bool isLengthNearlyZero(float dx, float dy,
93 float *lengthSquared) {
94 *lengthSquared = getLengthSquared(dx, dy);
95 return *lengthSquared <= (SK_ScalarNearlyZero * SK_ScalarNearlyZero);
96}
97
epoger@google.com1fd56dc2011-06-15 18:04:58 +000098SkScalar SkPoint::Normalize(SkPoint* pt) {
reed@google.com5a5fe582013-05-03 15:59:39 +000099 float x = pt->fX;
100 float y = pt->fY;
epoger@google.com94fa43c2012-04-11 17:51:01 +0000101 float mag2;
reed@google.com5a5fe582013-05-03 15:59:39 +0000102 if (isLengthNearlyZero(x, y, &mag2)) {
reeda8b326c2014-12-09 11:50:32 -0800103 pt->set(0, 0);
reed@google.com5a5fe582013-05-03 15:59:39 +0000104 return 0;
epoger@google.com1fd56dc2011-06-15 18:04:58 +0000105 }
reed@google.com5a5fe582013-05-03 15:59:39 +0000106
107 float mag, scale;
108 if (SkScalarIsFinite(mag2)) {
109 mag = sk_float_sqrt(mag2);
110 scale = 1 / mag;
111 } else {
112 // our mag2 step overflowed to infinity, so use doubles instead.
113 // much slower, but needed when x or y are very large, other wise we
114 // divide by inf. and return (0,0) vector.
115 double xx = x;
116 double yy = y;
117 double magmag = sqrt(xx * xx + yy * yy);
118 mag = (float)magmag;
119 // we perform the divide with the double magmag, to stay exactly the
120 // same as setLength. It would be faster to perform the divide with
121 // mag, but it is possible that mag has overflowed to inf. but still
122 // have a non-zero value for scale (thanks to denormalized numbers).
123 scale = (float)(1 / magmag);
124 }
125 pt->set(x * scale, y * scale);
126 return mag;
epoger@google.com1fd56dc2011-06-15 18:04:58 +0000127}
128
reed@android.com8a1c16f2008-12-17 15:59:43 +0000129SkScalar SkPoint::Length(SkScalar dx, SkScalar dy) {
reed@google.com5a5fe582013-05-03 15:59:39 +0000130 float mag2 = dx * dx + dy * dy;
131 if (SkScalarIsFinite(mag2)) {
132 return sk_float_sqrt(mag2);
133 } else {
134 double xx = dx;
135 double yy = dy;
136 return (float)sqrt(xx * xx + yy * yy);
137 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000138}
139
reed@google.com5a5fe582013-05-03 15:59:39 +0000140/*
141 * We have to worry about 2 tricky conditions:
142 * 1. underflow of mag2 (compared against nearlyzero^2)
143 * 2. overflow of mag2 (compared w/ isfinite)
144 *
145 * If we underflow, we return false. If we overflow, we compute again using
146 * doubles, which is much slower (3x in a desktop test) but will not overflow.
147 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000148bool SkPoint::setLength(float x, float y, float length) {
epoger@google.com94fa43c2012-04-11 17:51:01 +0000149 float mag2;
reed@google.com5a5fe582013-05-03 15:59:39 +0000150 if (isLengthNearlyZero(x, y, &mag2)) {
reeda8b326c2014-12-09 11:50:32 -0800151 this->set(0, 0);
reed@google.com5a5fe582013-05-03 15:59:39 +0000152 return false;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000153 }
reed@google.com5a5fe582013-05-03 15:59:39 +0000154
155 float scale;
156 if (SkScalarIsFinite(mag2)) {
157 scale = length / sk_float_sqrt(mag2);
158 } else {
159 // our mag2 step overflowed to infinity, so use doubles instead.
160 // much slower, but needed when x or y are very large, other wise we
161 // divide by inf. and return (0,0) vector.
162 double xx = x;
163 double yy = y;
bungeman4760f322015-06-24 13:08:51 -0700164 #ifdef SK_CPU_FLUSH_TO_ZERO
caryclark936b7342014-07-11 12:14:51 -0700165 // The iOS ARM processor discards small denormalized numbers to go faster.
166 // Casting this to a float would cause the scale to go to zero. Keeping it
167 // as a double for the multiply keeps the scale non-zero.
168 double dscale = length / sqrt(xx * xx + yy * yy);
169 fX = x * dscale;
170 fY = y * dscale;
171 return true;
172 #else
reed@google.com5a5fe582013-05-03 15:59:39 +0000173 scale = (float)(length / sqrt(xx * xx + yy * yy));
caryclark936b7342014-07-11 12:14:51 -0700174 #endif
reed@google.com5a5fe582013-05-03 15:59:39 +0000175 }
176 fX = x * scale;
177 fY = y * scale;
178 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000179}
180
commit-bot@chromium.org11e5b972013-11-08 20:14:16 +0000181bool SkPoint::setLengthFast(float length) {
182 return this->setLengthFast(fX, fY, length);
epoger@google.com94fa43c2012-04-11 17:51:01 +0000183}
184
commit-bot@chromium.org11e5b972013-11-08 20:14:16 +0000185bool SkPoint::setLengthFast(float x, float y, float length) {
186 float mag2;
187 if (isLengthNearlyZero(x, y, &mag2)) {
reeda8b326c2014-12-09 11:50:32 -0800188 this->set(0, 0);
commit-bot@chromium.org11e5b972013-11-08 20:14:16 +0000189 return false;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000190 }
191
commit-bot@chromium.org11e5b972013-11-08 20:14:16 +0000192 float scale;
193 if (SkScalarIsFinite(mag2)) {
194 scale = length * sk_float_rsqrt(mag2); // <--- this is the difference
reed@android.com8a1c16f2008-12-17 15:59:43 +0000195 } else {
commit-bot@chromium.org11e5b972013-11-08 20:14:16 +0000196 // our mag2 step overflowed to infinity, so use doubles instead.
197 // much slower, but needed when x or y are very large, other wise we
198 // divide by inf. and return (0,0) vector.
199 double xx = x;
200 double yy = y;
201 scale = (float)(length / sqrt(xx * xx + yy * yy));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000202 }
commit-bot@chromium.org11e5b972013-11-08 20:14:16 +0000203 fX = x * scale;
204 fY = y * scale;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000205 return true;
206}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000207
reed@android.com8a1c16f2008-12-17 15:59:43 +0000208
reed@google.com7744c202011-05-06 19:26:26 +0000209///////////////////////////////////////////////////////////////////////////////
210
bsalomon@google.com647a8042011-08-23 14:39:01 +0000211SkScalar SkPoint::distanceToLineBetweenSqd(const SkPoint& a,
212 const SkPoint& b,
213 Side* side) const {
214
215 SkVector u = b - a;
216 SkVector v = *this - a;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000217
bsalomon@google.com647a8042011-08-23 14:39:01 +0000218 SkScalar uLengthSqd = u.lengthSqd();
219 SkScalar det = u.cross(v);
bsalomon49f085d2014-09-05 13:34:00 -0700220 if (side) {
bsalomon@google.com647a8042011-08-23 14:39:01 +0000221 SkASSERT(-1 == SkPoint::kLeft_Side &&
222 0 == SkPoint::kOn_Side &&
223 1 == kRight_Side);
224 *side = (Side) SkScalarSignAsInt(det);
225 }
egdaniel5a23a142015-02-25 06:41:47 -0800226 SkScalar temp = det / uLengthSqd;
227 temp *= det;
228 return temp;
bsalomon@google.com647a8042011-08-23 14:39:01 +0000229}
230
231SkScalar SkPoint::distanceToLineSegmentBetweenSqd(const SkPoint& a,
reed@google.com7744c202011-05-06 19:26:26 +0000232 const SkPoint& b) const {
233 // See comments to distanceToLineBetweenSqd. If the projection of c onto
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000234 // u is between a and b then this returns the same result as that
reed@google.com7744c202011-05-06 19:26:26 +0000235 // function. Otherwise, it returns the distance to the closer of a and
236 // b. Let the projection of v onto u be v'. There are three cases:
237 // 1. v' points opposite to u. c is not between a and b and is closer
238 // to a than b.
239 // 2. v' points along u and has magnitude less than y. c is between
240 // a and b and the distance to the segment is the same as distance
241 // to the line ab.
242 // 3. v' points along u and has greater magnitude than u. c is not
243 // not between a and b and is closer to b than a.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000244 // v' = (u dot v) * u / |u|. So if (u dot v)/|u| is less than zero we're
reed@google.com7744c202011-05-06 19:26:26 +0000245 // in case 1. If (u dot v)/|u| is > |u| we are in case 3. Otherwise
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000246 // we're in case 2. We actually compare (u dot v) to 0 and |u|^2 to
reed@google.com7744c202011-05-06 19:26:26 +0000247 // avoid a sqrt to compute |u|.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000248
reed@google.com7744c202011-05-06 19:26:26 +0000249 SkVector u = b - a;
250 SkVector v = *this - a;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000251
reed@google.com7744c202011-05-06 19:26:26 +0000252 SkScalar uLengthSqd = u.lengthSqd();
253 SkScalar uDotV = SkPoint::DotProduct(u, v);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000254
reed@google.com7744c202011-05-06 19:26:26 +0000255 if (uDotV <= 0) {
256 return v.lengthSqd();
257 } else if (uDotV > uLengthSqd) {
258 return b.distanceToSqd(*this);
259 } else {
260 SkScalar det = u.cross(v);
egdaniel5a23a142015-02-25 06:41:47 -0800261 SkScalar temp = det / uLengthSqd;
262 temp *= det;
263 return temp;
reed@google.com7744c202011-05-06 19:26:26 +0000264 }
265}