blob: 9c015e5ea4936a7039adf918cef6f31b2d0dd604 [file] [log] [blame]
reed@android.com8a1c16f2008-12-17 15:59:43 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2006 The Android Open Source Project
reed@android.com8a1c16f2008-12-17 15:59:43 +00003 *
epoger@google.comec3ed6a2011-07-28 14:26:00 +00004 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
reed@android.com8a1c16f2008-12-17 15:59:43 +00006 */
7
8#ifndef SkScalar_DEFINED
9#define SkScalar_DEFINED
10
reed95dd1772015-08-17 18:29:47 -070011#include "../private/SkFloatingPoint.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000012
Leon Scroggins8233fc82017-02-02 13:12:23 +000013#undef SK_SCALAR_IS_FLOAT
14#define SK_SCALAR_IS_FLOAT 1
15
reedbd160592014-10-31 06:55:41 -070016typedef float SkScalar;
reed@android.com8a1c16f2008-12-17 15:59:43 +000017
reedbd160592014-10-31 06:55:41 -070018#define SK_Scalar1 1.0f
19#define SK_ScalarHalf 0.5f
20#define SK_ScalarSqrt2 1.41421356f
21#define SK_ScalarPI 3.14159265f
22#define SK_ScalarTanPIOver8 0.414213562f
23#define SK_ScalarRoot2Over2 0.707106781f
24#define SK_ScalarMax 3.402823466e+38f
25#define SK_ScalarInfinity SK_FloatInfinity
26#define SK_ScalarNegativeInfinity SK_FloatNegativeInfinity
27#define SK_ScalarNaN SK_FloatNaN
reed@android.com8a1c16f2008-12-17 15:59:43 +000028
reed@google.com8f4d2302013-12-17 16:44:46 +000029#define SkScalarFloorToScalar(x) sk_float_floor(x)
30#define SkScalarCeilToScalar(x) sk_float_ceil(x)
31#define SkScalarRoundToScalar(x) sk_float_floor((x) + 0.5f)
bungemandd67e3d2016-03-10 13:39:30 -080032#define SkScalarTruncToScalar(x) sk_float_trunc(x)
reed@google.com1b202802011-08-01 20:49:45 +000033
reed@google.com8f4d2302013-12-17 16:44:46 +000034#define SkScalarFloorToInt(x) sk_float_floor2int(x)
35#define SkScalarCeilToInt(x) sk_float_ceil2int(x)
36#define SkScalarRoundToInt(x) sk_float_round2int(x)
reedbd160592014-10-31 06:55:41 -070037
38#define SkScalarAbs(x) sk_float_abs(x)
39#define SkScalarCopySign(x, y) sk_float_copysign(x, y)
40#define SkScalarMod(x, y) sk_float_mod(x,y)
reedbd160592014-10-31 06:55:41 -070041#define SkScalarSqrt(x) sk_float_sqrt(x)
42#define SkScalarPow(b, e) sk_float_pow(b, e)
43
44#define SkScalarSin(radians) (float)sk_float_sin(radians)
45#define SkScalarCos(radians) (float)sk_float_cos(radians)
46#define SkScalarTan(radians) (float)sk_float_tan(radians)
47#define SkScalarASin(val) (float)sk_float_asin(val)
48#define SkScalarACos(val) (float)sk_float_acos(val)
49#define SkScalarATan2(y, x) (float)sk_float_atan2(y,x)
50#define SkScalarExp(x) (float)sk_float_exp(x)
51#define SkScalarLog(x) (float)sk_float_log(x)
reed7729e562015-01-16 08:35:09 -080052#define SkScalarLog2(x) (float)sk_float_log2(x)
reedbd160592014-10-31 06:55:41 -070053
reedbd160592014-10-31 06:55:41 -070054//////////////////////////////////////////////////////////////////////////////////////////////////
55
56#define SkIntToScalar(x) static_cast<SkScalar>(x)
robertphillipsf054b172016-05-13 05:06:19 -070057#define SkIntToFloat(x) static_cast<float>(x)
Ben Wagner47a540f2017-09-18 13:20:32 -040058#define SkScalarTruncToInt(x) sk_float_saturate2int(x)
reedbd160592014-10-31 06:55:41 -070059
60#define SkScalarToFloat(x) static_cast<float>(x)
61#define SkFloatToScalar(x) static_cast<SkScalar>(x)
62#define SkScalarToDouble(x) static_cast<double>(x)
63#define SkDoubleToScalar(x) static_cast<SkScalar>(x)
64
65#define SK_ScalarMin (-SK_ScalarMax)
66
67static inline bool SkScalarIsNaN(SkScalar x) { return x != x; }
68
69/** Returns true if x is not NaN and not infinite
70 */
71static inline bool SkScalarIsFinite(SkScalar x) {
72 // We rely on the following behavior of infinities and nans
73 // 0 * finite --> 0
74 // 0 * infinity --> NaN
75 // 0 * NaN --> NaN
76 SkScalar prod = x * 0;
77 // At this point, prod will either be NaN or 0
reed454fa712015-02-10 08:46:22 -080078 return !SkScalarIsNaN(prod);
79}
80
81static inline bool SkScalarsAreFinite(SkScalar a, SkScalar b) {
82 SkScalar prod = 0;
83 prod *= a;
84 prod *= b;
85 // At this point, prod will either be NaN or 0
86 return !SkScalarIsNaN(prod);
87}
88
89static inline bool SkScalarsAreFinite(const SkScalar array[], int count) {
90 SkScalar prod = 0;
91 for (int i = 0; i < count; ++i) {
92 prod *= array[i];
93 }
94 // At this point, prod will either be NaN or 0
ehsan.akhgari6f904752014-12-15 12:08:47 -080095 return !SkScalarIsNaN(prod);
reedbd160592014-10-31 06:55:41 -070096}
reed@google.com1b202802011-08-01 20:49:45 +000097
commit-bot@chromium.org4e332f82014-05-05 16:04:42 +000098/**
99 * Variant of SkScalarRoundToInt, that performs the rounding step (adding 0.5) explicitly using
100 * double, to avoid possibly losing the low bit(s) of the answer before calling floor().
101 *
102 * This routine will likely be slower than SkScalarRoundToInt(), and should only be used when the
103 * extra precision is known to be valuable.
104 *
105 * In particular, this catches the following case:
106 * SkScalar x = 0.49999997;
107 * int ix = SkScalarRoundToInt(x);
108 * SkASSERT(0 == ix); // <--- fails
109 * ix = SkDScalarRoundToInt(x);
110 * SkASSERT(0 == ix); // <--- succeeds
111 */
112static inline int SkDScalarRoundToInt(SkScalar x) {
113 double xx = x;
114 xx += 0.5;
115 return (int)floor(xx);
116}
117
bungemandd67e3d2016-03-10 13:39:30 -0800118/** Returns the fractional part of the scalar. */
119static inline SkScalar SkScalarFraction(SkScalar x) {
120 return x - SkScalarTruncToScalar(x);
121}
122
reedbd160592014-10-31 06:55:41 -0700123static inline SkScalar SkScalarClampMax(SkScalar x, SkScalar max) {
bungeman167eb172015-03-09 13:40:15 -0700124 x = SkTMin(x, max);
125 x = SkTMax<SkScalar>(x, 0);
126 return x;
reed@google.com8f4d2302013-12-17 16:44:46 +0000127}
reedbd160592014-10-31 06:55:41 -0700128
129static inline SkScalar SkScalarPin(SkScalar x, SkScalar min, SkScalar max) {
bungemanfd0ecf42015-04-16 12:18:28 -0700130 return SkTPin(x, min, max);
reed@google.com8f4d2302013-12-17 16:44:46 +0000131}
reedbd160592014-10-31 06:55:41 -0700132
133SkScalar SkScalarSinCos(SkScalar radians, SkScalar* cosValue);
134
135static inline SkScalar SkScalarSquare(SkScalar x) { return x * x; }
136
Mike Reedfaffa862018-02-12 11:46:19 -0500137#define SkScalarDiv(numer, denom) sk_ieee_float_divide(numer, denom)
138#define SkScalarInvert(x) sk_ieee_float_divide(SK_Scalar1, (x))
139#define SkScalarFastInvert(x) sk_ieee_float_divide(SK_Scalar1, (x))
140#define SkScalarAve(a, b) (((a) + (b)) * SK_ScalarHalf)
141#define SkScalarHalf(a) ((a) * SK_ScalarHalf)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000142
reed@google.com8f4d2302013-12-17 16:44:46 +0000143#define SkDegreesToRadians(degrees) ((degrees) * (SK_ScalarPI / 180))
commit-bot@chromium.orgc8d73282014-01-06 18:17:24 +0000144#define SkRadiansToDegrees(radians) ((radians) * (180 / SK_ScalarPI))
reed@android.com8a1c16f2008-12-17 15:59:43 +0000145
reedbd160592014-10-31 06:55:41 -0700146static inline SkScalar SkMaxScalar(SkScalar a, SkScalar b) { return a > b ? a : b; }
147static inline SkScalar SkMinScalar(SkScalar a, SkScalar b) { return a < b ? a : b; }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000148
reed@google.com8f4d2302013-12-17 16:44:46 +0000149static inline bool SkScalarIsInt(SkScalar x) {
Brian Salomon34169692017-08-28 15:32:01 -0400150 return x == SkScalarFloorToScalar(x);
reed@google.com8f4d2302013-12-17 16:44:46 +0000151}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000152
bsalomon@google.com647a8042011-08-23 14:39:01 +0000153/**
154 * Returns -1 || 0 || 1 depending on the sign of value:
155 * -1 if x < 0
156 * 0 if x == 0
157 * 1 if x > 0
158 */
159static inline int SkScalarSignAsInt(SkScalar x) {
160 return x < 0 ? -1 : (x > 0);
161}
162
163// Scalar result version of above
164static inline SkScalar SkScalarSignAsScalar(SkScalar x) {
165 return x < 0 ? -SK_Scalar1 : ((x > 0) ? SK_Scalar1 : 0);
166}
reed@google.com1b202802011-08-01 20:49:45 +0000167
reed@android.com8a1c16f2008-12-17 15:59:43 +0000168#define SK_ScalarNearlyZero (SK_Scalar1 / (1 << 12))
169
reed@android.com187d5592009-07-08 14:03:56 +0000170static inline bool SkScalarNearlyZero(SkScalar x,
robertphillips64b0f5f2016-01-25 14:19:56 -0800171 SkScalar tolerance = SK_ScalarNearlyZero) {
reed@google.com9b5ca292012-04-12 12:51:32 +0000172 SkASSERT(tolerance >= 0);
173 return SkScalarAbs(x) <= tolerance;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000174}
175
epoger@google.com1fd56dc2011-06-15 18:04:58 +0000176static inline bool SkScalarNearlyEqual(SkScalar x, SkScalar y,
robertphillips30c4cae2015-09-15 10:20:55 -0700177 SkScalar tolerance = SK_ScalarNearlyZero) {
reed@google.com9b5ca292012-04-12 12:51:32 +0000178 SkASSERT(tolerance >= 0);
179 return SkScalarAbs(x-y) <= tolerance;
epoger@google.com1fd56dc2011-06-15 18:04:58 +0000180}
181
reed@android.com8a1c16f2008-12-17 15:59:43 +0000182/** Linearly interpolate between A and B, based on t.
183 If t is 0, return A
184 If t is 1, return B
185 else interpolate.
186 t must be [0..SK_Scalar1]
187*/
reed@android.com187d5592009-07-08 14:03:56 +0000188static inline SkScalar SkScalarInterp(SkScalar A, SkScalar B, SkScalar t) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000189 SkASSERT(t >= 0 && t <= SK_Scalar1);
reed@google.com1a5e51f2014-01-27 13:41:02 +0000190 return A + (B - A) * t;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000191}
192
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000193/** Interpolate along the function described by (keys[length], values[length])
194 for the passed searchKey. SearchKeys outside the range keys[0]-keys[Length]
195 clamp to the min or max value. This function was inspired by a desire
196 to change the multiplier for thickness in fakeBold; therefore it assumes
197 the number of pairs (length) will be small, and a linear search is used.
198 Repeated keys are allowed for discontinuous functions (so long as keys is
199 monotonically increasing), and if key is the value of a repeated scalar in
200 keys, the first one will be used. However, that may change if a binary
201 search is used.
202*/
203SkScalar SkScalarInterpFunc(SkScalar searchKey, const SkScalar keys[],
204 const SkScalar values[], int length);
205
reed@google.come1e7d7a2012-10-31 19:59:23 +0000206/*
207 * Helper to compare an array of scalars.
208 */
209static inline bool SkScalarsEqual(const SkScalar a[], const SkScalar b[], int n) {
reed@google.come1e7d7a2012-10-31 19:59:23 +0000210 SkASSERT(n >= 0);
211 for (int i = 0; i < n; ++i) {
212 if (a[i] != b[i]) {
213 return false;
214 }
215 }
216 return true;
reed@google.come1e7d7a2012-10-31 19:59:23 +0000217}
218
reed@android.com8a1c16f2008-12-17 15:59:43 +0000219#endif