epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 3 | * Copyright 2008 The Android Open Source Project |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 4 | * |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 9 | |
caryclark | 936b734 | 2014-07-11 12:14:51 -0700 | [diff] [blame] | 10 | #include "SkMathPriv.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 11 | #include "SkPoint.h" |
| 12 | |
| 13 | void 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 | |
| 22 | void 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.com | 7744c20 | 2011-05-06 19:26:26 +0000 | [diff] [blame] | 33 | void SkPoint::setIRectFan(int l, int t, int r, int b, size_t stride) { |
| 34 | SkASSERT(stride >= sizeof(SkPoint)); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 35 | |
| 36 | ((SkPoint*)((intptr_t)this + 0 * stride))->set(SkIntToScalar(l), |
reed@google.com | 7744c20 | 2011-05-06 19:26:26 +0000 | [diff] [blame] | 37 | SkIntToScalar(t)); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 38 | ((SkPoint*)((intptr_t)this + 1 * stride))->set(SkIntToScalar(l), |
reed@google.com | 7744c20 | 2011-05-06 19:26:26 +0000 | [diff] [blame] | 39 | SkIntToScalar(b)); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 40 | ((SkPoint*)((intptr_t)this + 2 * stride))->set(SkIntToScalar(r), |
reed@google.com | 7744c20 | 2011-05-06 19:26:26 +0000 | [diff] [blame] | 41 | SkIntToScalar(b)); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 42 | ((SkPoint*)((intptr_t)this + 3 * stride))->set(SkIntToScalar(r), |
reed@google.com | 7744c20 | 2011-05-06 19:26:26 +0000 | [diff] [blame] | 43 | SkIntToScalar(t)); |
| 44 | } |
| 45 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 46 | void 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 | |
| 55 | void 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 | |
| 64 | void SkPoint::scale(SkScalar scale, SkPoint* dst) const { |
| 65 | SkASSERT(dst); |
| 66 | dst->set(SkScalarMul(fX, scale), SkScalarMul(fY, scale)); |
| 67 | } |
| 68 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 69 | bool SkPoint::normalize() { |
| 70 | return this->setLength(fX, fY, SK_Scalar1); |
| 71 | } |
| 72 | |
| 73 | bool SkPoint::setNormalize(SkScalar x, SkScalar y) { |
| 74 | return this->setLength(x, y, SK_Scalar1); |
| 75 | } |
| 76 | |
| 77 | bool SkPoint::setLength(SkScalar length) { |
| 78 | return this->setLength(fX, fY, length); |
| 79 | } |
| 80 | |
epoger@google.com | 94fa43c | 2012-04-11 17:51:01 +0000 | [diff] [blame] | 81 | // Returns the square of the Euclidian distance to (dx,dy). |
| 82 | static 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 ). |
| 92 | static 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.com | 1fd56dc | 2011-06-15 18:04:58 +0000 | [diff] [blame] | 98 | SkScalar SkPoint::Normalize(SkPoint* pt) { |
reed@google.com | 5a5fe58 | 2013-05-03 15:59:39 +0000 | [diff] [blame] | 99 | float x = pt->fX; |
| 100 | float y = pt->fY; |
epoger@google.com | 94fa43c | 2012-04-11 17:51:01 +0000 | [diff] [blame] | 101 | float mag2; |
reed@google.com | 5a5fe58 | 2013-05-03 15:59:39 +0000 | [diff] [blame] | 102 | if (isLengthNearlyZero(x, y, &mag2)) { |
reed | a8b326c | 2014-12-09 11:50:32 -0800 | [diff] [blame] | 103 | pt->set(0, 0); |
reed@google.com | 5a5fe58 | 2013-05-03 15:59:39 +0000 | [diff] [blame] | 104 | return 0; |
epoger@google.com | 1fd56dc | 2011-06-15 18:04:58 +0000 | [diff] [blame] | 105 | } |
reed@google.com | 5a5fe58 | 2013-05-03 15:59:39 +0000 | [diff] [blame] | 106 | |
| 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.com | 1fd56dc | 2011-06-15 18:04:58 +0000 | [diff] [blame] | 127 | } |
| 128 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 129 | SkScalar SkPoint::Length(SkScalar dx, SkScalar dy) { |
reed@google.com | 5a5fe58 | 2013-05-03 15:59:39 +0000 | [diff] [blame] | 130 | 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.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 138 | } |
| 139 | |
reed@google.com | 5a5fe58 | 2013-05-03 15:59:39 +0000 | [diff] [blame] | 140 | /* |
| 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.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 148 | bool SkPoint::setLength(float x, float y, float length) { |
epoger@google.com | 94fa43c | 2012-04-11 17:51:01 +0000 | [diff] [blame] | 149 | float mag2; |
reed@google.com | 5a5fe58 | 2013-05-03 15:59:39 +0000 | [diff] [blame] | 150 | if (isLengthNearlyZero(x, y, &mag2)) { |
reed | a8b326c | 2014-12-09 11:50:32 -0800 | [diff] [blame] | 151 | this->set(0, 0); |
reed@google.com | 5a5fe58 | 2013-05-03 15:59:39 +0000 | [diff] [blame] | 152 | return false; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 153 | } |
reed@google.com | 5a5fe58 | 2013-05-03 15:59:39 +0000 | [diff] [blame] | 154 | |
| 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; |
bungeman | 4760f32 | 2015-06-24 13:08:51 -0700 | [diff] [blame] | 164 | #ifdef SK_CPU_FLUSH_TO_ZERO |
caryclark | 936b734 | 2014-07-11 12:14:51 -0700 | [diff] [blame] | 165 | // 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.com | 5a5fe58 | 2013-05-03 15:59:39 +0000 | [diff] [blame] | 173 | scale = (float)(length / sqrt(xx * xx + yy * yy)); |
caryclark | 936b734 | 2014-07-11 12:14:51 -0700 | [diff] [blame] | 174 | #endif |
reed@google.com | 5a5fe58 | 2013-05-03 15:59:39 +0000 | [diff] [blame] | 175 | } |
| 176 | fX = x * scale; |
| 177 | fY = y * scale; |
| 178 | return true; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 179 | } |
| 180 | |
commit-bot@chromium.org | 11e5b97 | 2013-11-08 20:14:16 +0000 | [diff] [blame] | 181 | bool SkPoint::setLengthFast(float length) { |
| 182 | return this->setLengthFast(fX, fY, length); |
epoger@google.com | 94fa43c | 2012-04-11 17:51:01 +0000 | [diff] [blame] | 183 | } |
| 184 | |
commit-bot@chromium.org | 11e5b97 | 2013-11-08 20:14:16 +0000 | [diff] [blame] | 185 | bool SkPoint::setLengthFast(float x, float y, float length) { |
| 186 | float mag2; |
| 187 | if (isLengthNearlyZero(x, y, &mag2)) { |
reed | a8b326c | 2014-12-09 11:50:32 -0800 | [diff] [blame] | 188 | this->set(0, 0); |
commit-bot@chromium.org | 11e5b97 | 2013-11-08 20:14:16 +0000 | [diff] [blame] | 189 | return false; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 190 | } |
| 191 | |
commit-bot@chromium.org | 11e5b97 | 2013-11-08 20:14:16 +0000 | [diff] [blame] | 192 | float scale; |
| 193 | if (SkScalarIsFinite(mag2)) { |
| 194 | scale = length * sk_float_rsqrt(mag2); // <--- this is the difference |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 195 | } else { |
commit-bot@chromium.org | 11e5b97 | 2013-11-08 20:14:16 +0000 | [diff] [blame] | 196 | // 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.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 202 | } |
commit-bot@chromium.org | 11e5b97 | 2013-11-08 20:14:16 +0000 | [diff] [blame] | 203 | fX = x * scale; |
| 204 | fY = y * scale; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 205 | return true; |
| 206 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 207 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 208 | |
reed@google.com | 7744c20 | 2011-05-06 19:26:26 +0000 | [diff] [blame] | 209 | /////////////////////////////////////////////////////////////////////////////// |
| 210 | |
bsalomon@google.com | 647a804 | 2011-08-23 14:39:01 +0000 | [diff] [blame] | 211 | SkScalar 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.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 217 | |
bsalomon@google.com | 647a804 | 2011-08-23 14:39:01 +0000 | [diff] [blame] | 218 | SkScalar uLengthSqd = u.lengthSqd(); |
| 219 | SkScalar det = u.cross(v); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 220 | if (side) { |
bsalomon@google.com | 647a804 | 2011-08-23 14:39:01 +0000 | [diff] [blame] | 221 | SkASSERT(-1 == SkPoint::kLeft_Side && |
| 222 | 0 == SkPoint::kOn_Side && |
| 223 | 1 == kRight_Side); |
| 224 | *side = (Side) SkScalarSignAsInt(det); |
| 225 | } |
egdaniel | 5a23a14 | 2015-02-25 06:41:47 -0800 | [diff] [blame] | 226 | SkScalar temp = det / uLengthSqd; |
| 227 | temp *= det; |
| 228 | return temp; |
bsalomon@google.com | 647a804 | 2011-08-23 14:39:01 +0000 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | SkScalar SkPoint::distanceToLineSegmentBetweenSqd(const SkPoint& a, |
reed@google.com | 7744c20 | 2011-05-06 19:26:26 +0000 | [diff] [blame] | 232 | const SkPoint& b) const { |
| 233 | // See comments to distanceToLineBetweenSqd. If the projection of c onto |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 234 | // u is between a and b then this returns the same result as that |
reed@google.com | 7744c20 | 2011-05-06 19:26:26 +0000 | [diff] [blame] | 235 | // 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.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 244 | // v' = (u dot v) * u / |u|. So if (u dot v)/|u| is less than zero we're |
reed@google.com | 7744c20 | 2011-05-06 19:26:26 +0000 | [diff] [blame] | 245 | // in case 1. If (u dot v)/|u| is > |u| we are in case 3. Otherwise |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 246 | // we're in case 2. We actually compare (u dot v) to 0 and |u|^2 to |
reed@google.com | 7744c20 | 2011-05-06 19:26:26 +0000 | [diff] [blame] | 247 | // avoid a sqrt to compute |u|. |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 248 | |
reed@google.com | 7744c20 | 2011-05-06 19:26:26 +0000 | [diff] [blame] | 249 | SkVector u = b - a; |
| 250 | SkVector v = *this - a; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 251 | |
reed@google.com | 7744c20 | 2011-05-06 19:26:26 +0000 | [diff] [blame] | 252 | SkScalar uLengthSqd = u.lengthSqd(); |
| 253 | SkScalar uDotV = SkPoint::DotProduct(u, v); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 254 | |
reed@google.com | 7744c20 | 2011-05-06 19:26:26 +0000 | [diff] [blame] | 255 | 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); |
egdaniel | 5a23a14 | 2015-02-25 06:41:47 -0800 | [diff] [blame] | 261 | SkScalar temp = det / uLengthSqd; |
| 262 | temp *= det; |
| 263 | return temp; |
reed@google.com | 7744c20 | 2011-05-06 19:26:26 +0000 | [diff] [blame] | 264 | } |
| 265 | } |