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