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 | #ifndef SkCurve_DEFINED |
| 9 | #define SkCurve_DEFINED |
| 10 | |
| 11 | #include "SkScalar.h" |
Brian Osman | 8b6283f | 2019-02-14 16:55:21 -0500 | [diff] [blame] | 12 | #include "SkTArray.h" |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 13 | |
| 14 | class SkFieldVisitor; |
| 15 | class SkRandom; |
| 16 | |
Brian Osman | e12e499 | 2019-02-19 15:39:18 -0500 | [diff] [blame] | 17 | /** |
| 18 | * SkCurve implements a keyframed 1D function, useful for animating values over time. This pattern |
| 19 | * is common in digital content creation tools. An SkCurve might represent rotation, scale, opacity, |
| 20 | * or any other scalar quantity. |
| 21 | * |
| 22 | * An SkCurve has a logical domain of [0, 1], and is made of one or more SkCurveSegments. |
| 23 | * Each segment describes the behavior of the curve in some sub-domain. For an SkCurve with N |
| 24 | * segments, there are (N - 1) intermediate x-values that subdivide the domain. The first and last |
| 25 | * x-values are implicitly 0 and 1: |
| 26 | * |
| 27 | * 0 ... x[0] ... x[1] ... ... 1 |
| 28 | * Segment_0 Segment_1 ... Segment_N-1 |
| 29 | * |
| 30 | * Each segment describes a function over [0, 1] - x-values are re-normalized to the segment's |
| 31 | * domain when being evaluated. The segments are cubic polynomials, defined by four values (fMin). |
| 32 | * These are the values at x=0 and x=1, as well as control points at x=1/3 and x=2/3. |
| 33 | * |
| 34 | * For segments with fConstant == true, only the first value is used (fMin[0]). |
| 35 | * |
| 36 | * Each segment has two additional features for creating interesting (and varied) animation: |
| 37 | * - A segment can be ranged. Ranged segments have two sets of coefficients, and a random value |
| 38 | * taken from the SkRandom will be used to lerp betwen them. Typically, the SkRandom passed to |
| 39 | * eval will be in the same state at each call, so this value will be stable. That causes a |
| 40 | * ranged SkCurve to produce a single smooth cubic function somewhere within the range defined |
| 41 | * by fMin and fMax. |
| 42 | * - A segment can be bidirectional. In that case, after a value is computed, it will be negated |
| 43 | * 50% of the time. |
| 44 | */ |
| 45 | |
Brian Osman | 8b6283f | 2019-02-14 16:55:21 -0500 | [diff] [blame] | 46 | struct SkCurveSegment { |
| 47 | SkScalar eval(SkScalar x, SkRandom& random) const; |
| 48 | void visitFields(SkFieldVisitor* v); |
| 49 | |
| 50 | void setConstant(SkScalar c) { |
| 51 | fConstant = true; |
| 52 | fRanged = false; |
| 53 | fMin[0] = c; |
| 54 | } |
| 55 | |
| 56 | SkScalar fMin[4] = { 0.0f, 0.0f, 0.0f, 0.0f }; |
| 57 | SkScalar fMax[4] = { 0.0f, 0.0f, 0.0f, 0.0f }; |
| 58 | |
| 59 | bool fConstant = true; |
| 60 | bool fRanged = false; |
| 61 | bool fBidirectional = false; |
| 62 | }; |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 63 | |
| 64 | struct SkCurve { |
| 65 | SkCurve(SkScalar c = 0.0f) { |
Brian Osman | 8b6283f | 2019-02-14 16:55:21 -0500 | [diff] [blame] | 66 | fSegments.push_back().setConstant(c); |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 67 | } |
| 68 | |
Brian Osman | e12e499 | 2019-02-19 15:39:18 -0500 | [diff] [blame] | 69 | // Evaluate this curve at x, using random for curves that have ranged or bidirectional segments. |
Brian Osman | 8b6283f | 2019-02-14 16:55:21 -0500 | [diff] [blame] | 70 | SkScalar eval(SkScalar x, SkRandom& random) const; |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 71 | void visitFields(SkFieldVisitor* v); |
Brian Osman | e12e499 | 2019-02-19 15:39:18 -0500 | [diff] [blame] | 72 | |
| 73 | // Returns the (very conversative) range of this SkCurve in extents (as [minimum, maximum]). |
Brian Osman | 112aa2d | 2019-02-15 10:45:56 -0500 | [diff] [blame] | 74 | void getExtents(SkScalar extents[2]) const; |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 75 | |
Brian Osman | e12e499 | 2019-02-19 15:39:18 -0500 | [diff] [blame] | 76 | // It should always be true that (fXValues.count() + 1) == fSegments.count() |
Brian Osman | 8b6283f | 2019-02-14 16:55:21 -0500 | [diff] [blame] | 77 | SkTArray<SkScalar, true> fXValues; |
| 78 | SkTArray<SkCurveSegment, true> fSegments; |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 79 | }; |
| 80 | |
| 81 | #endif // SkCurve_DEFINED |