Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / skia / src / core / SkPoint.cpp
1
2 /*
3  * Copyright 2008 The Android Open Source Project
4  *
5  * Use of this source code is governed by a BSD-style license that can be
6  * found in the LICENSE file.
7  */
8
9
10 #include "SkMathPriv.h"
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
33 void SkPoint::setIRectFan(int l, int t, int r, int b, size_t stride) {
34     SkASSERT(stride >= sizeof(SkPoint));
35
36     ((SkPoint*)((intptr_t)this + 0 * stride))->set(SkIntToScalar(l),
37                                                    SkIntToScalar(t));
38     ((SkPoint*)((intptr_t)this + 1 * stride))->set(SkIntToScalar(l),
39                                                    SkIntToScalar(b));
40     ((SkPoint*)((intptr_t)this + 2 * stride))->set(SkIntToScalar(r),
41                                                    SkIntToScalar(b));
42     ((SkPoint*)((intptr_t)this + 3 * stride))->set(SkIntToScalar(r),
43                                                    SkIntToScalar(t));
44 }
45
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
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
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
98 SkScalar SkPoint::Normalize(SkPoint* pt) {
99     float x = pt->fX;
100     float y = pt->fY;
101     float mag2;
102     if (isLengthNearlyZero(x, y, &mag2)) {
103         return 0;
104     }
105
106     float mag, scale;
107     if (SkScalarIsFinite(mag2)) {
108         mag = sk_float_sqrt(mag2);
109         scale = 1 / mag;
110     } else {
111         // our mag2 step overflowed to infinity, so use doubles instead.
112         // much slower, but needed when x or y are very large, other wise we
113         // divide by inf. and return (0,0) vector.
114         double xx = x;
115         double yy = y;
116         double magmag = sqrt(xx * xx + yy * yy);
117         mag = (float)magmag;
118         // we perform the divide with the double magmag, to stay exactly the
119         // same as setLength. It would be faster to perform the divide with
120         // mag, but it is possible that mag has overflowed to inf. but still
121         // have a non-zero value for scale (thanks to denormalized numbers).
122         scale = (float)(1 / magmag);
123     }
124     pt->set(x * scale, y * scale);
125     return mag;
126 }
127
128 SkScalar SkPoint::Length(SkScalar dx, SkScalar dy) {
129     float mag2 = dx * dx + dy * dy;
130     if (SkScalarIsFinite(mag2)) {
131         return sk_float_sqrt(mag2);
132     } else {
133         double xx = dx;
134         double yy = dy;
135         return (float)sqrt(xx * xx + yy * yy);
136     }
137 }
138
139 /*
140  *  We have to worry about 2 tricky conditions:
141  *  1. underflow of mag2 (compared against nearlyzero^2)
142  *  2. overflow of mag2 (compared w/ isfinite)
143  *
144  *  If we underflow, we return false. If we overflow, we compute again using
145  *  doubles, which is much slower (3x in a desktop test) but will not overflow.
146  */
147 bool SkPoint::setLength(float x, float y, float length) {
148     float mag2;
149     if (isLengthNearlyZero(x, y, &mag2)) {
150         return false;
151     }
152
153     float scale;
154     if (SkScalarIsFinite(mag2)) {
155         scale = length / sk_float_sqrt(mag2);
156     } else {
157         // our mag2 step overflowed to infinity, so use doubles instead.
158         // much slower, but needed when x or y are very large, other wise we
159         // divide by inf. and return (0,0) vector.
160         double xx = x;
161         double yy = y;
162     #ifdef SK_DISCARD_DENORMALIZED_FOR_SPEED
163         // The iOS ARM processor discards small denormalized numbers to go faster.
164         // Casting this to a float would cause the scale to go to zero. Keeping it
165         // as a double for the multiply keeps the scale non-zero.
166         double dscale = length / sqrt(xx * xx + yy * yy);
167         fX = x * dscale;
168         fY = y * dscale;
169         return true;
170     #else
171         scale = (float)(length / sqrt(xx * xx + yy * yy));
172     #endif
173     }
174     fX = x * scale;
175     fY = y * scale;
176     return true;
177 }
178
179 bool SkPoint::setLengthFast(float length) {
180     return this->setLengthFast(fX, fY, length);
181 }
182
183 bool SkPoint::setLengthFast(float x, float y, float length) {
184     float mag2;
185     if (isLengthNearlyZero(x, y, &mag2)) {
186         return false;
187     }
188
189     float scale;
190     if (SkScalarIsFinite(mag2)) {
191         scale = length * sk_float_rsqrt(mag2);  // <--- this is the difference
192     } else {
193         // our mag2 step overflowed to infinity, so use doubles instead.
194         // much slower, but needed when x or y are very large, other wise we
195         // divide by inf. and return (0,0) vector.
196         double xx = x;
197         double yy = y;
198         scale = (float)(length / sqrt(xx * xx + yy * yy));
199     }
200     fX = x * scale;
201     fY = y * scale;
202     return true;
203 }
204
205
206 ///////////////////////////////////////////////////////////////////////////////
207
208 SkScalar SkPoint::distanceToLineBetweenSqd(const SkPoint& a,
209                                            const SkPoint& b,
210                                            Side* side) const {
211
212     SkVector u = b - a;
213     SkVector v = *this - a;
214
215     SkScalar uLengthSqd = u.lengthSqd();
216     SkScalar det = u.cross(v);
217     if (side) {
218         SkASSERT(-1 == SkPoint::kLeft_Side &&
219                   0 == SkPoint::kOn_Side &&
220                   1 == kRight_Side);
221         *side = (Side) SkScalarSignAsInt(det);
222     }
223     return SkScalarMulDiv(det, det, uLengthSqd);
224 }
225
226 SkScalar SkPoint::distanceToLineSegmentBetweenSqd(const SkPoint& a,
227                                                   const SkPoint& b) const {
228     // See comments to distanceToLineBetweenSqd. If the projection of c onto
229     // u is between a and b then this returns the same result as that
230     // function. Otherwise, it returns the distance to the closer of a and
231     // b. Let the projection of v onto u be v'.  There are three cases:
232     //    1. v' points opposite to u. c is not between a and b and is closer
233     //       to a than b.
234     //    2. v' points along u and has magnitude less than y. c is between
235     //       a and b and the distance to the segment is the same as distance
236     //       to the line ab.
237     //    3. v' points along u and has greater magnitude than u. c is not
238     //       not between a and b and is closer to b than a.
239     // v' = (u dot v) * u / |u|. So if (u dot v)/|u| is less than zero we're
240     // in case 1. If (u dot v)/|u| is > |u| we are in case 3. Otherwise
241     // we're in case 2. We actually compare (u dot v) to 0 and |u|^2 to
242     // avoid a sqrt to compute |u|.
243
244     SkVector u = b - a;
245     SkVector v = *this - a;
246
247     SkScalar uLengthSqd = u.lengthSqd();
248     SkScalar uDotV = SkPoint::DotProduct(u, v);
249
250     if (uDotV <= 0) {
251         return v.lengthSqd();
252     } else if (uDotV > uLengthSqd) {
253         return b.distanceToSqd(*this);
254     } else {
255         SkScalar det = u.cross(v);
256         return SkScalarMulDiv(det, det, uLengthSqd);
257     }
258 }