michael@0: michael@0: /* michael@0: * Copyright 2008 The Android Open Source Project michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: michael@0: michael@0: #include "SkPoint.h" michael@0: michael@0: void SkIPoint::rotateCW(SkIPoint* dst) const { michael@0: SkASSERT(dst); michael@0: michael@0: // use a tmp in case this == dst michael@0: int32_t tmp = fX; michael@0: dst->fX = -fY; michael@0: dst->fY = tmp; michael@0: } michael@0: michael@0: void SkIPoint::rotateCCW(SkIPoint* dst) const { michael@0: SkASSERT(dst); michael@0: michael@0: // use a tmp in case this == dst michael@0: int32_t tmp = fX; michael@0: dst->fX = fY; michael@0: dst->fY = -tmp; michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: void SkPoint::setIRectFan(int l, int t, int r, int b, size_t stride) { michael@0: SkASSERT(stride >= sizeof(SkPoint)); michael@0: michael@0: ((SkPoint*)((intptr_t)this + 0 * stride))->set(SkIntToScalar(l), michael@0: SkIntToScalar(t)); michael@0: ((SkPoint*)((intptr_t)this + 1 * stride))->set(SkIntToScalar(l), michael@0: SkIntToScalar(b)); michael@0: ((SkPoint*)((intptr_t)this + 2 * stride))->set(SkIntToScalar(r), michael@0: SkIntToScalar(b)); michael@0: ((SkPoint*)((intptr_t)this + 3 * stride))->set(SkIntToScalar(r), michael@0: SkIntToScalar(t)); michael@0: } michael@0: michael@0: void SkPoint::setRectFan(SkScalar l, SkScalar t, SkScalar r, SkScalar b, michael@0: size_t stride) { michael@0: SkASSERT(stride >= sizeof(SkPoint)); michael@0: michael@0: ((SkPoint*)((intptr_t)this + 0 * stride))->set(l, t); michael@0: ((SkPoint*)((intptr_t)this + 1 * stride))->set(l, b); michael@0: ((SkPoint*)((intptr_t)this + 2 * stride))->set(r, b); michael@0: ((SkPoint*)((intptr_t)this + 3 * stride))->set(r, t); michael@0: } michael@0: michael@0: void SkPoint::rotateCW(SkPoint* dst) const { michael@0: SkASSERT(dst); michael@0: michael@0: // use a tmp in case this == dst michael@0: SkScalar tmp = fX; michael@0: dst->fX = -fY; michael@0: dst->fY = tmp; michael@0: } michael@0: michael@0: void SkPoint::rotateCCW(SkPoint* dst) const { michael@0: SkASSERT(dst); michael@0: michael@0: // use a tmp in case this == dst michael@0: SkScalar tmp = fX; michael@0: dst->fX = fY; michael@0: dst->fY = -tmp; michael@0: } michael@0: michael@0: void SkPoint::scale(SkScalar scale, SkPoint* dst) const { michael@0: SkASSERT(dst); michael@0: dst->set(SkScalarMul(fX, scale), SkScalarMul(fY, scale)); michael@0: } michael@0: michael@0: bool SkPoint::normalize() { michael@0: return this->setLength(fX, fY, SK_Scalar1); michael@0: } michael@0: michael@0: bool SkPoint::setNormalize(SkScalar x, SkScalar y) { michael@0: return this->setLength(x, y, SK_Scalar1); michael@0: } michael@0: michael@0: bool SkPoint::setLength(SkScalar length) { michael@0: return this->setLength(fX, fY, length); michael@0: } michael@0: michael@0: // Returns the square of the Euclidian distance to (dx,dy). michael@0: static inline float getLengthSquared(float dx, float dy) { michael@0: return dx * dx + dy * dy; michael@0: } michael@0: michael@0: // Calculates the square of the Euclidian distance to (dx,dy) and stores it in michael@0: // *lengthSquared. Returns true if the distance is judged to be "nearly zero". michael@0: // michael@0: // This logic is encapsulated in a helper method to make it explicit that we michael@0: // always perform this check in the same manner, to avoid inconsistencies michael@0: // (see http://code.google.com/p/skia/issues/detail?id=560 ). michael@0: static inline bool isLengthNearlyZero(float dx, float dy, michael@0: float *lengthSquared) { michael@0: *lengthSquared = getLengthSquared(dx, dy); michael@0: return *lengthSquared <= (SK_ScalarNearlyZero * SK_ScalarNearlyZero); michael@0: } michael@0: michael@0: SkScalar SkPoint::Normalize(SkPoint* pt) { michael@0: float x = pt->fX; michael@0: float y = pt->fY; michael@0: float mag2; michael@0: if (isLengthNearlyZero(x, y, &mag2)) { michael@0: return 0; michael@0: } michael@0: michael@0: float mag, scale; michael@0: if (SkScalarIsFinite(mag2)) { michael@0: mag = sk_float_sqrt(mag2); michael@0: scale = 1 / mag; michael@0: } else { michael@0: // our mag2 step overflowed to infinity, so use doubles instead. michael@0: // much slower, but needed when x or y are very large, other wise we michael@0: // divide by inf. and return (0,0) vector. michael@0: double xx = x; michael@0: double yy = y; michael@0: double magmag = sqrt(xx * xx + yy * yy); michael@0: mag = (float)magmag; michael@0: // we perform the divide with the double magmag, to stay exactly the michael@0: // same as setLength. It would be faster to perform the divide with michael@0: // mag, but it is possible that mag has overflowed to inf. but still michael@0: // have a non-zero value for scale (thanks to denormalized numbers). michael@0: scale = (float)(1 / magmag); michael@0: } michael@0: pt->set(x * scale, y * scale); michael@0: return mag; michael@0: } michael@0: michael@0: SkScalar SkPoint::Length(SkScalar dx, SkScalar dy) { michael@0: float mag2 = dx * dx + dy * dy; michael@0: if (SkScalarIsFinite(mag2)) { michael@0: return sk_float_sqrt(mag2); michael@0: } else { michael@0: double xx = dx; michael@0: double yy = dy; michael@0: return (float)sqrt(xx * xx + yy * yy); michael@0: } michael@0: } michael@0: michael@0: /* michael@0: * We have to worry about 2 tricky conditions: michael@0: * 1. underflow of mag2 (compared against nearlyzero^2) michael@0: * 2. overflow of mag2 (compared w/ isfinite) michael@0: * michael@0: * If we underflow, we return false. If we overflow, we compute again using michael@0: * doubles, which is much slower (3x in a desktop test) but will not overflow. michael@0: */ michael@0: bool SkPoint::setLength(float x, float y, float length) { michael@0: float mag2; michael@0: if (isLengthNearlyZero(x, y, &mag2)) { michael@0: return false; michael@0: } michael@0: michael@0: float scale; michael@0: if (SkScalarIsFinite(mag2)) { michael@0: scale = length / sk_float_sqrt(mag2); michael@0: } else { michael@0: // our mag2 step overflowed to infinity, so use doubles instead. michael@0: // much slower, but needed when x or y are very large, other wise we michael@0: // divide by inf. and return (0,0) vector. michael@0: double xx = x; michael@0: double yy = y; michael@0: scale = (float)(length / sqrt(xx * xx + yy * yy)); michael@0: } michael@0: fX = x * scale; michael@0: fY = y * scale; michael@0: return true; michael@0: } michael@0: michael@0: bool SkPoint::setLengthFast(float length) { michael@0: return this->setLengthFast(fX, fY, length); michael@0: } michael@0: michael@0: bool SkPoint::setLengthFast(float x, float y, float length) { michael@0: float mag2; michael@0: if (isLengthNearlyZero(x, y, &mag2)) { michael@0: return false; michael@0: } michael@0: michael@0: float scale; michael@0: if (SkScalarIsFinite(mag2)) { michael@0: scale = length * sk_float_rsqrt(mag2); // <--- this is the difference michael@0: } else { michael@0: // our mag2 step overflowed to infinity, so use doubles instead. michael@0: // much slower, but needed when x or y are very large, other wise we michael@0: // divide by inf. and return (0,0) vector. michael@0: double xx = x; michael@0: double yy = y; michael@0: scale = (float)(length / sqrt(xx * xx + yy * yy)); michael@0: } michael@0: fX = x * scale; michael@0: fY = y * scale; michael@0: return true; michael@0: } michael@0: michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: SkScalar SkPoint::distanceToLineBetweenSqd(const SkPoint& a, michael@0: const SkPoint& b, michael@0: Side* side) const { michael@0: michael@0: SkVector u = b - a; michael@0: SkVector v = *this - a; michael@0: michael@0: SkScalar uLengthSqd = u.lengthSqd(); michael@0: SkScalar det = u.cross(v); michael@0: if (NULL != side) { michael@0: SkASSERT(-1 == SkPoint::kLeft_Side && michael@0: 0 == SkPoint::kOn_Side && michael@0: 1 == kRight_Side); michael@0: *side = (Side) SkScalarSignAsInt(det); michael@0: } michael@0: return SkScalarMulDiv(det, det, uLengthSqd); michael@0: } michael@0: michael@0: SkScalar SkPoint::distanceToLineSegmentBetweenSqd(const SkPoint& a, michael@0: const SkPoint& b) const { michael@0: // See comments to distanceToLineBetweenSqd. If the projection of c onto michael@0: // u is between a and b then this returns the same result as that michael@0: // function. Otherwise, it returns the distance to the closer of a and michael@0: // b. Let the projection of v onto u be v'. There are three cases: michael@0: // 1. v' points opposite to u. c is not between a and b and is closer michael@0: // to a than b. michael@0: // 2. v' points along u and has magnitude less than y. c is between michael@0: // a and b and the distance to the segment is the same as distance michael@0: // to the line ab. michael@0: // 3. v' points along u and has greater magnitude than u. c is not michael@0: // not between a and b and is closer to b than a. michael@0: // v' = (u dot v) * u / |u|. So if (u dot v)/|u| is less than zero we're michael@0: // in case 1. If (u dot v)/|u| is > |u| we are in case 3. Otherwise michael@0: // we're in case 2. We actually compare (u dot v) to 0 and |u|^2 to michael@0: // avoid a sqrt to compute |u|. michael@0: michael@0: SkVector u = b - a; michael@0: SkVector v = *this - a; michael@0: michael@0: SkScalar uLengthSqd = u.lengthSqd(); michael@0: SkScalar uDotV = SkPoint::DotProduct(u, v); michael@0: michael@0: if (uDotV <= 0) { michael@0: return v.lengthSqd(); michael@0: } else if (uDotV > uLengthSqd) { michael@0: return b.distanceToSqd(*this); michael@0: } else { michael@0: SkScalar det = u.cross(v); michael@0: return SkScalarMulDiv(det, det, uLengthSqd); michael@0: } michael@0: }