Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 Google LLC |
| 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 "SkCurve.h" |
| 9 | |
| 10 | #include "SkRandom.h" |
| 11 | #include "SkReflected.h" |
| 12 | |
Brian Osman | 8b6283f | 2019-02-14 16:55:21 -0500 | [diff] [blame] | 13 | static SkScalar eval_cubic(const SkScalar* pts, SkScalar x) { |
| 14 | SkScalar ix = (1 - x); |
| 15 | return pts[0]*ix*ix*ix + pts[1]*3*ix*ix*x + pts[2]*3*ix*x*x + pts[3]*x*x*x; |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 16 | } |
| 17 | |
Brian Osman | 8b6283f | 2019-02-14 16:55:21 -0500 | [diff] [blame] | 18 | SkScalar SkCurveSegment::eval(SkScalar x, SkRandom& random) const { |
| 19 | SkScalar result = fConstant ? fMin[0] : eval_cubic(fMin, x); |
| 20 | if (fRanged) { |
| 21 | result += ((fConstant ? fMax[0] : eval_cubic(fMax, x)) - result) * random.nextF(); |
| 22 | } |
| 23 | if (fBidirectional && random.nextBool()) { |
| 24 | result = -result; |
| 25 | } |
| 26 | return result; |
| 27 | } |
| 28 | |
| 29 | void SkCurveSegment::visitFields(SkFieldVisitor* v) { |
| 30 | v->visit("Constant", fConstant); |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 31 | v->visit("Ranged", fRanged); |
Brian Osman | 8b6283f | 2019-02-14 16:55:21 -0500 | [diff] [blame] | 32 | v->visit("Bidirectional", fBidirectional); |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 33 | v->visit("A0", fMin[0]); |
| 34 | v->visit("B0", fMin[1]); |
| 35 | v->visit("C0", fMin[2]); |
| 36 | v->visit("D0", fMin[3]); |
| 37 | v->visit("A1", fMax[0]); |
| 38 | v->visit("B1", fMax[1]); |
| 39 | v->visit("C1", fMax[2]); |
| 40 | v->visit("D1", fMax[3]); |
| 41 | } |
| 42 | |
Brian Osman | 8b6283f | 2019-02-14 16:55:21 -0500 | [diff] [blame] | 43 | SkScalar SkCurve::eval(SkScalar x, SkRandom& random) const { |
| 44 | SkASSERT(fSegments.count() == fXValues.count() + 1); |
| 45 | |
| 46 | int i = 0; |
| 47 | for (; i < fXValues.count(); ++i) { |
| 48 | if (x <= fXValues[i]) { |
| 49 | break; |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | SkScalar rangeMin = (i == 0) ? 0.0f : fXValues[i - 1]; |
| 54 | SkScalar rangeMax = (i == fXValues.count()) ? 1.0f : fXValues[i]; |
| 55 | SkScalar segmentX = (x - rangeMin) / (rangeMax - rangeMin); |
Brian Osman | 112aa2d | 2019-02-15 10:45:56 -0500 | [diff] [blame^] | 56 | if (!SkScalarIsFinite(segmentX)) { |
| 57 | segmentX = rangeMin; |
| 58 | } |
Brian Osman | 8b6283f | 2019-02-14 16:55:21 -0500 | [diff] [blame] | 59 | SkASSERT(0.0f <= segmentX && segmentX <= 1.0f); |
| 60 | return fSegments[i].eval(segmentX, random); |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 61 | } |
| 62 | |
Brian Osman | 8b6283f | 2019-02-14 16:55:21 -0500 | [diff] [blame] | 63 | void SkCurve::visitFields(SkFieldVisitor* v) { |
| 64 | v->visit("XValues", fXValues); |
| 65 | v->visit("Segments", fSegments); |
| 66 | |
| 67 | // Validate and fixup |
| 68 | if (fSegments.empty()) { |
| 69 | fSegments.push_back().setConstant(0.0f); |
| 70 | } |
| 71 | fXValues.resize_back(fSegments.count() - 1); |
| 72 | for (int i = 0; i < fXValues.count(); ++i) { |
| 73 | fXValues[i] = SkTPin(fXValues[i], i > 0 ? fXValues[i - 1] : 0.0f, 1.0f); |
| 74 | } |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 75 | } |
Brian Osman | 112aa2d | 2019-02-15 10:45:56 -0500 | [diff] [blame^] | 76 | |
| 77 | void SkCurve::getExtents(SkScalar extents[2]) const { |
| 78 | extents[0] = INFINITY; |
| 79 | extents[1] = -INFINITY; |
| 80 | auto extend = [=](SkScalar y) { |
| 81 | extents[0] = SkTMin(extents[0], y); |
| 82 | extents[1] = SkTMax(extents[1], y); |
| 83 | }; |
| 84 | for (const auto& segment : fSegments) { |
| 85 | for (int i = 0; i < (segment.fConstant ? 1 : 4); ++i) { |
| 86 | extend(segment.fMin[i]); |
| 87 | if (segment.fRanged) { |
| 88 | extend(segment.fMax[i]); |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | } |