gfx/skia/trunk/src/core/SkPoint.cpp

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

michael@0 1
michael@0 2 /*
michael@0 3 * Copyright 2008 The Android Open Source Project
michael@0 4 *
michael@0 5 * Use of this source code is governed by a BSD-style license that can be
michael@0 6 * found in the LICENSE file.
michael@0 7 */
michael@0 8
michael@0 9
michael@0 10 #include "SkPoint.h"
michael@0 11
michael@0 12 void SkIPoint::rotateCW(SkIPoint* dst) const {
michael@0 13 SkASSERT(dst);
michael@0 14
michael@0 15 // use a tmp in case this == dst
michael@0 16 int32_t tmp = fX;
michael@0 17 dst->fX = -fY;
michael@0 18 dst->fY = tmp;
michael@0 19 }
michael@0 20
michael@0 21 void SkIPoint::rotateCCW(SkIPoint* dst) const {
michael@0 22 SkASSERT(dst);
michael@0 23
michael@0 24 // use a tmp in case this == dst
michael@0 25 int32_t tmp = fX;
michael@0 26 dst->fX = fY;
michael@0 27 dst->fY = -tmp;
michael@0 28 }
michael@0 29
michael@0 30 ///////////////////////////////////////////////////////////////////////////////
michael@0 31
michael@0 32 void SkPoint::setIRectFan(int l, int t, int r, int b, size_t stride) {
michael@0 33 SkASSERT(stride >= sizeof(SkPoint));
michael@0 34
michael@0 35 ((SkPoint*)((intptr_t)this + 0 * stride))->set(SkIntToScalar(l),
michael@0 36 SkIntToScalar(t));
michael@0 37 ((SkPoint*)((intptr_t)this + 1 * stride))->set(SkIntToScalar(l),
michael@0 38 SkIntToScalar(b));
michael@0 39 ((SkPoint*)((intptr_t)this + 2 * stride))->set(SkIntToScalar(r),
michael@0 40 SkIntToScalar(b));
michael@0 41 ((SkPoint*)((intptr_t)this + 3 * stride))->set(SkIntToScalar(r),
michael@0 42 SkIntToScalar(t));
michael@0 43 }
michael@0 44
michael@0 45 void SkPoint::setRectFan(SkScalar l, SkScalar t, SkScalar r, SkScalar b,
michael@0 46 size_t stride) {
michael@0 47 SkASSERT(stride >= sizeof(SkPoint));
michael@0 48
michael@0 49 ((SkPoint*)((intptr_t)this + 0 * stride))->set(l, t);
michael@0 50 ((SkPoint*)((intptr_t)this + 1 * stride))->set(l, b);
michael@0 51 ((SkPoint*)((intptr_t)this + 2 * stride))->set(r, b);
michael@0 52 ((SkPoint*)((intptr_t)this + 3 * stride))->set(r, t);
michael@0 53 }
michael@0 54
michael@0 55 void SkPoint::rotateCW(SkPoint* dst) const {
michael@0 56 SkASSERT(dst);
michael@0 57
michael@0 58 // use a tmp in case this == dst
michael@0 59 SkScalar tmp = fX;
michael@0 60 dst->fX = -fY;
michael@0 61 dst->fY = tmp;
michael@0 62 }
michael@0 63
michael@0 64 void SkPoint::rotateCCW(SkPoint* dst) const {
michael@0 65 SkASSERT(dst);
michael@0 66
michael@0 67 // use a tmp in case this == dst
michael@0 68 SkScalar tmp = fX;
michael@0 69 dst->fX = fY;
michael@0 70 dst->fY = -tmp;
michael@0 71 }
michael@0 72
michael@0 73 void SkPoint::scale(SkScalar scale, SkPoint* dst) const {
michael@0 74 SkASSERT(dst);
michael@0 75 dst->set(SkScalarMul(fX, scale), SkScalarMul(fY, scale));
michael@0 76 }
michael@0 77
michael@0 78 bool SkPoint::normalize() {
michael@0 79 return this->setLength(fX, fY, SK_Scalar1);
michael@0 80 }
michael@0 81
michael@0 82 bool SkPoint::setNormalize(SkScalar x, SkScalar y) {
michael@0 83 return this->setLength(x, y, SK_Scalar1);
michael@0 84 }
michael@0 85
michael@0 86 bool SkPoint::setLength(SkScalar length) {
michael@0 87 return this->setLength(fX, fY, length);
michael@0 88 }
michael@0 89
michael@0 90 // Returns the square of the Euclidian distance to (dx,dy).
michael@0 91 static inline float getLengthSquared(float dx, float dy) {
michael@0 92 return dx * dx + dy * dy;
michael@0 93 }
michael@0 94
michael@0 95 // Calculates the square of the Euclidian distance to (dx,dy) and stores it in
michael@0 96 // *lengthSquared. Returns true if the distance is judged to be "nearly zero".
michael@0 97 //
michael@0 98 // This logic is encapsulated in a helper method to make it explicit that we
michael@0 99 // always perform this check in the same manner, to avoid inconsistencies
michael@0 100 // (see http://code.google.com/p/skia/issues/detail?id=560 ).
michael@0 101 static inline bool isLengthNearlyZero(float dx, float dy,
michael@0 102 float *lengthSquared) {
michael@0 103 *lengthSquared = getLengthSquared(dx, dy);
michael@0 104 return *lengthSquared <= (SK_ScalarNearlyZero * SK_ScalarNearlyZero);
michael@0 105 }
michael@0 106
michael@0 107 SkScalar SkPoint::Normalize(SkPoint* pt) {
michael@0 108 float x = pt->fX;
michael@0 109 float y = pt->fY;
michael@0 110 float mag2;
michael@0 111 if (isLengthNearlyZero(x, y, &mag2)) {
michael@0 112 return 0;
michael@0 113 }
michael@0 114
michael@0 115 float mag, scale;
michael@0 116 if (SkScalarIsFinite(mag2)) {
michael@0 117 mag = sk_float_sqrt(mag2);
michael@0 118 scale = 1 / mag;
michael@0 119 } else {
michael@0 120 // our mag2 step overflowed to infinity, so use doubles instead.
michael@0 121 // much slower, but needed when x or y are very large, other wise we
michael@0 122 // divide by inf. and return (0,0) vector.
michael@0 123 double xx = x;
michael@0 124 double yy = y;
michael@0 125 double magmag = sqrt(xx * xx + yy * yy);
michael@0 126 mag = (float)magmag;
michael@0 127 // we perform the divide with the double magmag, to stay exactly the
michael@0 128 // same as setLength. It would be faster to perform the divide with
michael@0 129 // mag, but it is possible that mag has overflowed to inf. but still
michael@0 130 // have a non-zero value for scale (thanks to denormalized numbers).
michael@0 131 scale = (float)(1 / magmag);
michael@0 132 }
michael@0 133 pt->set(x * scale, y * scale);
michael@0 134 return mag;
michael@0 135 }
michael@0 136
michael@0 137 SkScalar SkPoint::Length(SkScalar dx, SkScalar dy) {
michael@0 138 float mag2 = dx * dx + dy * dy;
michael@0 139 if (SkScalarIsFinite(mag2)) {
michael@0 140 return sk_float_sqrt(mag2);
michael@0 141 } else {
michael@0 142 double xx = dx;
michael@0 143 double yy = dy;
michael@0 144 return (float)sqrt(xx * xx + yy * yy);
michael@0 145 }
michael@0 146 }
michael@0 147
michael@0 148 /*
michael@0 149 * We have to worry about 2 tricky conditions:
michael@0 150 * 1. underflow of mag2 (compared against nearlyzero^2)
michael@0 151 * 2. overflow of mag2 (compared w/ isfinite)
michael@0 152 *
michael@0 153 * If we underflow, we return false. If we overflow, we compute again using
michael@0 154 * doubles, which is much slower (3x in a desktop test) but will not overflow.
michael@0 155 */
michael@0 156 bool SkPoint::setLength(float x, float y, float length) {
michael@0 157 float mag2;
michael@0 158 if (isLengthNearlyZero(x, y, &mag2)) {
michael@0 159 return false;
michael@0 160 }
michael@0 161
michael@0 162 float scale;
michael@0 163 if (SkScalarIsFinite(mag2)) {
michael@0 164 scale = length / sk_float_sqrt(mag2);
michael@0 165 } else {
michael@0 166 // our mag2 step overflowed to infinity, so use doubles instead.
michael@0 167 // much slower, but needed when x or y are very large, other wise we
michael@0 168 // divide by inf. and return (0,0) vector.
michael@0 169 double xx = x;
michael@0 170 double yy = y;
michael@0 171 scale = (float)(length / sqrt(xx * xx + yy * yy));
michael@0 172 }
michael@0 173 fX = x * scale;
michael@0 174 fY = y * scale;
michael@0 175 return true;
michael@0 176 }
michael@0 177
michael@0 178 bool SkPoint::setLengthFast(float length) {
michael@0 179 return this->setLengthFast(fX, fY, length);
michael@0 180 }
michael@0 181
michael@0 182 bool SkPoint::setLengthFast(float x, float y, float length) {
michael@0 183 float mag2;
michael@0 184 if (isLengthNearlyZero(x, y, &mag2)) {
michael@0 185 return false;
michael@0 186 }
michael@0 187
michael@0 188 float scale;
michael@0 189 if (SkScalarIsFinite(mag2)) {
michael@0 190 scale = length * sk_float_rsqrt(mag2); // <--- this is the difference
michael@0 191 } else {
michael@0 192 // our mag2 step overflowed to infinity, so use doubles instead.
michael@0 193 // much slower, but needed when x or y are very large, other wise we
michael@0 194 // divide by inf. and return (0,0) vector.
michael@0 195 double xx = x;
michael@0 196 double yy = y;
michael@0 197 scale = (float)(length / sqrt(xx * xx + yy * yy));
michael@0 198 }
michael@0 199 fX = x * scale;
michael@0 200 fY = y * scale;
michael@0 201 return true;
michael@0 202 }
michael@0 203
michael@0 204
michael@0 205 ///////////////////////////////////////////////////////////////////////////////
michael@0 206
michael@0 207 SkScalar SkPoint::distanceToLineBetweenSqd(const SkPoint& a,
michael@0 208 const SkPoint& b,
michael@0 209 Side* side) const {
michael@0 210
michael@0 211 SkVector u = b - a;
michael@0 212 SkVector v = *this - a;
michael@0 213
michael@0 214 SkScalar uLengthSqd = u.lengthSqd();
michael@0 215 SkScalar det = u.cross(v);
michael@0 216 if (NULL != side) {
michael@0 217 SkASSERT(-1 == SkPoint::kLeft_Side &&
michael@0 218 0 == SkPoint::kOn_Side &&
michael@0 219 1 == kRight_Side);
michael@0 220 *side = (Side) SkScalarSignAsInt(det);
michael@0 221 }
michael@0 222 return SkScalarMulDiv(det, det, uLengthSqd);
michael@0 223 }
michael@0 224
michael@0 225 SkScalar SkPoint::distanceToLineSegmentBetweenSqd(const SkPoint& a,
michael@0 226 const SkPoint& b) const {
michael@0 227 // See comments to distanceToLineBetweenSqd. If the projection of c onto
michael@0 228 // u is between a and b then this returns the same result as that
michael@0 229 // function. Otherwise, it returns the distance to the closer of a and
michael@0 230 // b. Let the projection of v onto u be v'. There are three cases:
michael@0 231 // 1. v' points opposite to u. c is not between a and b and is closer
michael@0 232 // to a than b.
michael@0 233 // 2. v' points along u and has magnitude less than y. c is between
michael@0 234 // a and b and the distance to the segment is the same as distance
michael@0 235 // to the line ab.
michael@0 236 // 3. v' points along u and has greater magnitude than u. c is not
michael@0 237 // not between a and b and is closer to b than a.
michael@0 238 // v' = (u dot v) * u / |u|. So if (u dot v)/|u| is less than zero we're
michael@0 239 // in case 1. If (u dot v)/|u| is > |u| we are in case 3. Otherwise
michael@0 240 // we're in case 2. We actually compare (u dot v) to 0 and |u|^2 to
michael@0 241 // avoid a sqrt to compute |u|.
michael@0 242
michael@0 243 SkVector u = b - a;
michael@0 244 SkVector v = *this - a;
michael@0 245
michael@0 246 SkScalar uLengthSqd = u.lengthSqd();
michael@0 247 SkScalar uDotV = SkPoint::DotProduct(u, v);
michael@0 248
michael@0 249 if (uDotV <= 0) {
michael@0 250 return v.lengthSqd();
michael@0 251 } else if (uDotV > uLengthSqd) {
michael@0 252 return b.distanceToSqd(*this);
michael@0 253 } else {
michael@0 254 SkScalar det = u.cross(v);
michael@0 255 return SkScalarMulDiv(det, det, uLengthSqd);
michael@0 256 }
michael@0 257 }

mercurial