3 * Copyright 2008 The Android Open Source Project
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
10 #include "SkMathPriv.h"
13 void SkIPoint::rotateCW(SkIPoint* dst) const {
16 // use a tmp in case this == dst
22 void SkIPoint::rotateCCW(SkIPoint* dst) const {
25 // use a tmp in case this == dst
31 ///////////////////////////////////////////////////////////////////////////////
33 void SkPoint::setIRectFan(int l, int t, int r, int b, size_t stride) {
34 SkASSERT(stride >= sizeof(SkPoint));
36 ((SkPoint*)((intptr_t)this + 0 * stride))->set(SkIntToScalar(l),
38 ((SkPoint*)((intptr_t)this + 1 * stride))->set(SkIntToScalar(l),
40 ((SkPoint*)((intptr_t)this + 2 * stride))->set(SkIntToScalar(r),
42 ((SkPoint*)((intptr_t)this + 3 * stride))->set(SkIntToScalar(r),
46 void SkPoint::setRectFan(SkScalar l, SkScalar t, SkScalar r, SkScalar b,
48 SkASSERT(stride >= sizeof(SkPoint));
50 ((SkPoint*)((intptr_t)this + 0 * stride))->set(l, t);
51 ((SkPoint*)((intptr_t)this + 1 * stride))->set(l, b);
52 ((SkPoint*)((intptr_t)this + 2 * stride))->set(r, b);
53 ((SkPoint*)((intptr_t)this + 3 * stride))->set(r, t);
56 void SkPoint::rotateCW(SkPoint* dst) const {
59 // use a tmp in case this == dst
65 void SkPoint::rotateCCW(SkPoint* dst) const {
68 // use a tmp in case this == dst
74 void SkPoint::scale(SkScalar scale, SkPoint* dst) const {
76 dst->set(SkScalarMul(fX, scale), SkScalarMul(fY, scale));
79 bool SkPoint::normalize() {
80 return this->setLength(fX, fY, SK_Scalar1);
83 bool SkPoint::setNormalize(SkScalar x, SkScalar y) {
84 return this->setLength(x, y, SK_Scalar1);
87 bool SkPoint::setLength(SkScalar length) {
88 return this->setLength(fX, fY, length);
91 // Returns the square of the Euclidian distance to (dx,dy).
92 static inline float getLengthSquared(float dx, float dy) {
93 return dx * dx + dy * dy;
96 // Calculates the square of the Euclidian distance to (dx,dy) and stores it in
97 // *lengthSquared. Returns true if the distance is judged to be "nearly zero".
99 // This logic is encapsulated in a helper method to make it explicit that we
100 // always perform this check in the same manner, to avoid inconsistencies
101 // (see http://code.google.com/p/skia/issues/detail?id=560 ).
102 static inline bool isLengthNearlyZero(float dx, float dy,
103 float *lengthSquared) {
104 *lengthSquared = getLengthSquared(dx, dy);
105 return *lengthSquared <= (SK_ScalarNearlyZero * SK_ScalarNearlyZero);
108 SkScalar SkPoint::Normalize(SkPoint* pt) {
112 if (isLengthNearlyZero(x, y, &mag2)) {
117 if (SkScalarIsFinite(mag2)) {
118 mag = sk_float_sqrt(mag2);
121 // our mag2 step overflowed to infinity, so use doubles instead.
122 // much slower, but needed when x or y are very large, other wise we
123 // divide by inf. and return (0,0) vector.
126 double magmag = sqrt(xx * xx + yy * yy);
128 // we perform the divide with the double magmag, to stay exactly the
129 // same as setLength. It would be faster to perform the divide with
130 // mag, but it is possible that mag has overflowed to inf. but still
131 // have a non-zero value for scale (thanks to denormalized numbers).
132 scale = (float)(1 / magmag);
134 pt->set(x * scale, y * scale);
138 SkScalar SkPoint::Length(SkScalar dx, SkScalar dy) {
139 float mag2 = dx * dx + dy * dy;
140 if (SkScalarIsFinite(mag2)) {
141 return sk_float_sqrt(mag2);
145 return (float)sqrt(xx * xx + yy * yy);
150 * We have to worry about 2 tricky conditions:
151 * 1. underflow of mag2 (compared against nearlyzero^2)
152 * 2. overflow of mag2 (compared w/ isfinite)
154 * If we underflow, we return false. If we overflow, we compute again using
155 * doubles, which is much slower (3x in a desktop test) but will not overflow.
157 bool SkPoint::setLength(float x, float y, float length) {
159 if (isLengthNearlyZero(x, y, &mag2)) {
164 if (SkScalarIsFinite(mag2)) {
165 scale = length / sk_float_sqrt(mag2);
167 // our mag2 step overflowed to infinity, so use doubles instead.
168 // much slower, but needed when x or y are very large, other wise we
169 // divide by inf. and return (0,0) vector.
172 #ifdef SK_DISCARD_DENORMALIZED_FOR_SPEED
173 // The iOS ARM processor discards small denormalized numbers to go faster.
174 // Casting this to a float would cause the scale to go to zero. Keeping it
175 // as a double for the multiply keeps the scale non-zero.
176 double dscale = length / sqrt(xx * xx + yy * yy);
181 scale = (float)(length / sqrt(xx * xx + yy * yy));
189 bool SkPoint::setLengthFast(float length) {
190 return this->setLengthFast(fX, fY, length);
193 bool SkPoint::setLengthFast(float x, float y, float length) {
195 if (isLengthNearlyZero(x, y, &mag2)) {
200 if (SkScalarIsFinite(mag2)) {
201 scale = length * sk_float_rsqrt(mag2); // <--- this is the difference
203 // our mag2 step overflowed to infinity, so use doubles instead.
204 // much slower, but needed when x or y are very large, other wise we
205 // divide by inf. and return (0,0) vector.
208 scale = (float)(length / sqrt(xx * xx + yy * yy));
216 ///////////////////////////////////////////////////////////////////////////////
218 SkScalar SkPoint::distanceToLineBetweenSqd(const SkPoint& a,
223 SkVector v = *this - a;
225 SkScalar uLengthSqd = u.lengthSqd();
226 SkScalar det = u.cross(v);
228 SkASSERT(-1 == SkPoint::kLeft_Side &&
229 0 == SkPoint::kOn_Side &&
231 *side = (Side) SkScalarSignAsInt(det);
233 return SkScalarMulDiv(det, det, uLengthSqd);
236 SkScalar SkPoint::distanceToLineSegmentBetweenSqd(const SkPoint& a,
237 const SkPoint& b) const {
238 // See comments to distanceToLineBetweenSqd. If the projection of c onto
239 // u is between a and b then this returns the same result as that
240 // function. Otherwise, it returns the distance to the closer of a and
241 // b. Let the projection of v onto u be v'. There are three cases:
242 // 1. v' points opposite to u. c is not between a and b and is closer
244 // 2. v' points along u and has magnitude less than y. c is between
245 // a and b and the distance to the segment is the same as distance
247 // 3. v' points along u and has greater magnitude than u. c is not
248 // not between a and b and is closer to b than a.
249 // v' = (u dot v) * u / |u|. So if (u dot v)/|u| is less than zero we're
250 // in case 1. If (u dot v)/|u| is > |u| we are in case 3. Otherwise
251 // we're in case 2. We actually compare (u dot v) to 0 and |u|^2 to
252 // avoid a sqrt to compute |u|.
255 SkVector v = *this - a;
257 SkScalar uLengthSqd = u.lengthSqd();
258 SkScalar uDotV = SkPoint::DotProduct(u, v);
261 return v.lengthSqd();
262 } else if (uDotV > uLengthSqd) {
263 return b.distanceToSqd(*this);
265 SkScalar det = u.cross(v);
266 return SkScalarMulDiv(det, det, uLengthSqd);