• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2008 The Android Open Source Project
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #include "src/core/SkMathPriv.h"
9 #include "src/core/SkPointPriv.h"
10 
11 ///////////////////////////////////////////////////////////////////////////////
dump(std::string & desc,int depth) const12 void SkPoint::dump(std::string& desc, int depth) const {
13     std::string split(depth, '\t');
14     desc += split + "\n SkPoint:{ \n";
15     desc += split + "\t fX: " + std::to_string(fX) + "\n";
16     desc += split + "\t fY: " + std::to_string(fY) + "\n";
17     desc += split + "}\n";
18 }
19 
scale(SkScalar scale,SkPoint * dst) const20 void SkPoint::scale(SkScalar scale, SkPoint* dst) const {
21     SkASSERT(dst);
22     dst->set(fX * scale, fY * scale);
23 }
24 
normalize()25 bool SkPoint::normalize() {
26     return this->setLength(fX, fY, SK_Scalar1);
27 }
28 
setNormalize(SkScalar x,SkScalar y)29 bool SkPoint::setNormalize(SkScalar x, SkScalar y) {
30     return this->setLength(x, y, SK_Scalar1);
31 }
32 
setLength(SkScalar length)33 bool SkPoint::setLength(SkScalar length) {
34     return this->setLength(fX, fY, length);
35 }
36 
37 /*
38  *  We have to worry about 2 tricky conditions:
39  *  1. underflow of mag2 (compared against nearlyzero^2)
40  *  2. overflow of mag2 (compared w/ isfinite)
41  *
42  *  If we underflow, we return false. If we overflow, we compute again using
43  *  doubles, which is much slower (3x in a desktop test) but will not overflow.
44  */
set_point_length(SkPoint * pt,float x,float y,float length,float * orig_length=nullptr)45 template <bool use_rsqrt> bool set_point_length(SkPoint* pt, float x, float y, float length,
46                                                 float* orig_length = nullptr) {
47     SkASSERT(!use_rsqrt || (orig_length == nullptr));
48 
49     // our mag2 step overflowed to infinity, so use doubles instead.
50     // much slower, but needed when x or y are very large, other wise we
51     // divide by inf. and return (0,0) vector.
52     double xx = x;
53     double yy = y;
54     double dmag = sqrt(xx * xx + yy * yy);
55     double dscale = sk_ieee_double_divide(length, dmag);
56     x *= dscale;
57     y *= dscale;
58     // check if we're not finite, or we're zero-length
59     if (!sk_float_isfinite(x) || !sk_float_isfinite(y) || (x == 0 && y == 0)) {
60         pt->set(0, 0);
61         return false;
62     }
63     float mag = 0;
64     if (orig_length) {
65         mag = sk_double_to_float(dmag);
66     }
67     pt->set(x, y);
68     if (orig_length) {
69         *orig_length = mag;
70     }
71     return true;
72 }
73 
Normalize(SkPoint * pt)74 SkScalar SkPoint::Normalize(SkPoint* pt) {
75     float mag;
76     if (set_point_length<false>(pt, pt->fX, pt->fY, 1.0f, &mag)) {
77         return mag;
78     }
79     return 0;
80 }
81 
Length(SkScalar dx,SkScalar dy)82 SkScalar SkPoint::Length(SkScalar dx, SkScalar dy) {
83     float mag2 = dx * dx + dy * dy;
84     if (SkScalarIsFinite(mag2)) {
85         return sk_float_sqrt(mag2);
86     } else {
87         double xx = dx;
88         double yy = dy;
89         return sk_double_to_float(sqrt(xx * xx + yy * yy));
90     }
91 }
92 
setLength(float x,float y,float length)93 bool SkPoint::setLength(float x, float y, float length) {
94     return set_point_length<false>(this, x, y, length);
95 }
96 
SetLengthFast(SkPoint * pt,float length)97 bool SkPointPriv::SetLengthFast(SkPoint* pt, float length) {
98     return set_point_length<true>(pt, pt->fX, pt->fY, length);
99 }
100 
101 
102 ///////////////////////////////////////////////////////////////////////////////
103 
DistanceToLineBetweenSqd(const SkPoint & pt,const SkPoint & a,const SkPoint & b,Side * side)104 SkScalar SkPointPriv::DistanceToLineBetweenSqd(const SkPoint& pt, const SkPoint& a,
105                                                const SkPoint& b,
106                                                Side* side) {
107 
108     SkVector u = b - a;
109     SkVector v = pt - a;
110 
111     SkScalar uLengthSqd = LengthSqd(u);
112     SkScalar det = u.cross(v);
113     if (side) {
114         SkASSERT(-1 == kLeft_Side &&
115                   0 == kOn_Side &&
116                   1 == kRight_Side);
117         *side = (Side) SkScalarSignAsInt(det);
118     }
119     SkScalar temp = sk_ieee_float_divide(det, uLengthSqd);
120     temp *= det;
121     // It's possible we have a degenerate line vector, or we're so far away it looks degenerate
122     // In this case, return squared distance to point A.
123     if (!SkScalarIsFinite(temp)) {
124         return LengthSqd(v);
125     }
126     return temp;
127 }
128 
DistanceToLineSegmentBetweenSqd(const SkPoint & pt,const SkPoint & a,const SkPoint & b)129 SkScalar SkPointPriv::DistanceToLineSegmentBetweenSqd(const SkPoint& pt, const SkPoint& a,
130                                                       const SkPoint& b) {
131     // See comments to distanceToLineBetweenSqd. If the projection of c onto
132     // u is between a and b then this returns the same result as that
133     // function. Otherwise, it returns the distance to the closer of a and
134     // b. Let the projection of v onto u be v'.  There are three cases:
135     //    1. v' points opposite to u. c is not between a and b and is closer
136     //       to a than b.
137     //    2. v' points along u and has magnitude less than y. c is between
138     //       a and b and the distance to the segment is the same as distance
139     //       to the line ab.
140     //    3. v' points along u and has greater magnitude than u. c is not
141     //       not between a and b and is closer to b than a.
142     // v' = (u dot v) * u / |u|. So if (u dot v)/|u| is less than zero we're
143     // in case 1. If (u dot v)/|u| is > |u| we are in case 3. Otherwise
144     // we're in case 2. We actually compare (u dot v) to 0 and |u|^2 to
145     // avoid a sqrt to compute |u|.
146 
147     SkVector u = b - a;
148     SkVector v = pt - a;
149 
150     SkScalar uLengthSqd = LengthSqd(u);
151     SkScalar uDotV = SkPoint::DotProduct(u, v);
152 
153     // closest point is point A
154     if (uDotV <= 0) {
155         return LengthSqd(v);
156     // closest point is point B
157     } else if (uDotV > uLengthSqd) {
158         return DistanceToSqd(b, pt);
159     // closest point is inside segment
160     } else {
161         SkScalar det = u.cross(v);
162         SkScalar temp = sk_ieee_float_divide(det, uLengthSqd);
163         temp *= det;
164         // It's possible we have a degenerate segment, or we're so far away it looks degenerate
165         // In this case, return squared distance to point A.
166         if (!SkScalarIsFinite(temp)) {
167             return LengthSqd(v);
168         }
169         return temp;
170     }
171 }
172