Flutter Engine
The Flutter Engine
SkPoint.cpp
Go to the documentation of this file.
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
12
13#include <cmath>
14
15///////////////////////////////////////////////////////////////////////////////
16
17void SkPoint::scale(float scale, SkPoint* dst) const {
19 dst->set(fX * scale, fY * scale);
20}
21
23 return this->setLength(fX, fY, 1);
24}
25
26bool SkPoint::setNormalize(float x, float y) {
27 return this->setLength(x, y, 1);
28}
29
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 */
42template <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
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
79float 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
90bool SkPoint::setLength(float x, float y, float length) {
91 return set_point_length<false>(this, x, y, length);
92}
93
95 return set_point_length<true>(pt, pt->fX, pt->fY, length);
96}
97
98
99///////////////////////////////////////////////////////////////////////////////
100
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
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}
#define SkASSERT(cond)
Definition: SkAssert.h:116
static constexpr float sk_double_to_float(double x)
static bool SkIsFinite(T x, Pack... values)
static constexpr int sk_float_sgn(float x)
static constexpr double sk_ieee_double_divide(double numer, double denom)
static constexpr float sk_ieee_float_divide(float numer, float denom)
static int side(double x)
bool set_point_length(SkPoint *pt, float x, float y, float length, float *orig_length=nullptr)
Definition: SkPoint.cpp:42
static bool SetLengthFast(SkPoint *pt, float length)
Definition: SkPoint.cpp:94
static SkScalar LengthSqd(const SkPoint &pt)
Definition: SkPointPriv.h:63
static SkScalar DistanceToLineSegmentBetweenSqd(const SkPoint &pt, const SkPoint &a, const SkPoint &b)
Definition: SkPoint.cpp:126
static SkScalar DistanceToLineBetweenSqd(const SkPoint &pt, const SkPoint &a, const SkPoint &b, Side *side=nullptr)
Definition: SkPoint.cpp:101
static SkScalar DistanceToSqd(const SkPoint &pt, const SkPoint &a)
Definition: SkPointPriv.h:48
static bool b
struct MyStruct a[10]
size_t length
double y
double x
skia_private::AutoTArray< sk_sp< SkImageFilter > > filters TypedMatrix matrix TypedMatrix matrix SkScalar dx
Definition: SkRecords.h:208
dst
Definition: cp.py:12
SIN Vec< N, float > sqrt(const Vec< N, float > &x)
Definition: SkVx.h:706
const Scalar scale
bool setLength(float length)
Definition: SkPoint.cpp:30
float fX
x-axis value
Definition: SkPoint_impl.h:164
static float Normalize(SkVector *vec)
Definition: SkPoint.cpp:71
bool setNormalize(float x, float y)
Definition: SkPoint.cpp:26
static float DotProduct(const SkVector &a, const SkVector &b)
Definition: SkPoint_impl.h:518
void set(float x, float y)
Definition: SkPoint_impl.h:200
static float Length(float x, float y)
Definition: SkPoint.cpp:79
float cross(const SkVector &vec) const
Definition: SkPoint_impl.h:545
float length() const
Definition: SkPoint_impl.h:282
void scale(float scale, SkPoint *dst) const
Definition: SkPoint.cpp:17
float fY
y-axis value
Definition: SkPoint_impl.h:165
constexpr float y() const
Definition: SkPoint_impl.h:187
bool normalize()
Definition: SkPoint.cpp:22
constexpr float x() const
Definition: SkPoint_impl.h:181