herb | 7cf12dd | 2016-01-11 08:08:56 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 Google Inc. |
| 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 | #ifndef SkScaleToSides_DEFINED |
| 9 | #define SkScaleToSides_DEFINED |
| 10 | |
| 11 | #include <cmath> |
| 12 | #include "SkScalar.h" |
| 13 | #include "SkTypes.h" |
| 14 | |
herb | 97293c6 | 2016-01-22 11:58:55 -0800 | [diff] [blame] | 15 | class SkScaleToSides { |
herb | 7cf12dd | 2016-01-11 08:08:56 -0800 | [diff] [blame] | 16 | public: |
herb | 5e0883c | 2016-01-22 08:34:35 -0800 | [diff] [blame] | 17 | // This code assumes that a and b fit in a float, and therefore the resulting smaller value |
herb | 7cf12dd | 2016-01-11 08:08:56 -0800 | [diff] [blame] | 18 | // of a and b will fit in a float. The side of the rectangle may be larger than a float. |
| 19 | // Scale must be less than or equal to the ratio limit / (*a + *b). |
| 20 | // This code assumes that NaN and Inf are never passed in. |
| 21 | static void AdjustRadii(double limit, double scale, SkScalar* a, SkScalar* b) { |
| 22 | SkASSERTF(scale < 1.0 && scale > 0.0, "scale: %g", scale); |
| 23 | |
| 24 | *a = (float)((double)*a * scale); |
| 25 | *b = (float)((double)*b * scale); |
| 26 | |
herb | 5e0883c | 2016-01-22 08:34:35 -0800 | [diff] [blame] | 27 | if (*a + *b > limit) { |
herb | 7cf12dd | 2016-01-11 08:08:56 -0800 | [diff] [blame] | 28 | float* minRadius = a; |
| 29 | float* maxRadius = b; |
| 30 | |
| 31 | // Force minRadius to be the smaller of the two. |
| 32 | if (*minRadius > *maxRadius) { |
| 33 | SkTSwap(minRadius, maxRadius); |
| 34 | } |
| 35 | |
| 36 | // newMinRadius must be float in order to give the actual value of the radius. |
| 37 | // The newMinRadius will always be smaller than limit. The largest that minRadius can be |
| 38 | // is 1/2 the ratio of minRadius : (minRadius + maxRadius), therefore in the resulting |
herb | f5d4746 | 2016-02-19 16:54:12 -0800 | [diff] [blame] | 39 | // division, minRadius can be no larger than 1/2 limit + ULP. The newMinRadius can be |
| 40 | // 1/2 a ULP off at this point. |
herb | 7cf12dd | 2016-01-11 08:08:56 -0800 | [diff] [blame] | 41 | float newMinRadius = *minRadius; |
| 42 | |
| 43 | // Because newMaxRadius is the result of a double to float conversion, it can be larger |
| 44 | // than limit, but only by one ULP. |
| 45 | float newMaxRadius = (float)(limit - newMinRadius); |
| 46 | |
herb | f5d4746 | 2016-02-19 16:54:12 -0800 | [diff] [blame] | 47 | // The total sum of newMinRadius and newMaxRadius can be upto 1.5 ULPs off. If the |
| 48 | // sum is greater than the limit then newMaxRadius may have to be reduced twice. |
herb | 0d2bc84 | 2016-01-22 10:11:03 -0800 | [diff] [blame] | 49 | // Note: nextafterf is a c99 call and should be std::nextafter, but this is not |
| 50 | // implemented in the GCC ARM compiler. |
herb | 5e0883c | 2016-01-22 08:34:35 -0800 | [diff] [blame] | 51 | if (newMaxRadius + newMinRadius > limit) { |
herb | 0d2bc84 | 2016-01-22 10:11:03 -0800 | [diff] [blame] | 52 | newMaxRadius = nextafterf(newMaxRadius, 0.0f); |
herb | f5d4746 | 2016-02-19 16:54:12 -0800 | [diff] [blame] | 53 | if (newMaxRadius + newMinRadius > limit) { |
| 54 | newMaxRadius = nextafterf(newMaxRadius, 0.0f); |
| 55 | } |
herb | 7cf12dd | 2016-01-11 08:08:56 -0800 | [diff] [blame] | 56 | } |
| 57 | *maxRadius = newMaxRadius; |
| 58 | } |
| 59 | |
herb | 5e0883c | 2016-01-22 08:34:35 -0800 | [diff] [blame] | 60 | SkASSERTF(*a >= 0.0f && *b >= 0.0f, "a: %g, b: %g, limit: %g, scale: %g", *a, *b, limit, |
| 61 | scale); |
herb | f5d4746 | 2016-02-19 16:54:12 -0800 | [diff] [blame] | 62 | |
| 63 | SkASSERTF(*a + *b <= limit, |
| 64 | "\nlimit: %.17f, sum: %.17f, a: %.10f, b: %.10f, scale: %.20f", |
| 65 | limit, *a + *b, *a, *b, scale); |
herb | 7cf12dd | 2016-01-11 08:08:56 -0800 | [diff] [blame] | 66 | } |
| 67 | }; |
| 68 | #endif // ScaleToSides_DEFINED |