blob: 58c8e2f21b520493ca380a6b4cee1904305b748b [file] [log] [blame]
Brian Osman7c979f52019-02-12 13:27:51 -05001/*
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 Osman8b6283f2019-02-14 16:55:21 -050012#include "SkTArray.h"
Brian Osman7c979f52019-02-12 13:27:51 -050013
14class SkFieldVisitor;
15class SkRandom;
16
Brian Osmane12e4992019-02-19 15:39:18 -050017/**
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 Osman8b6283f2019-02-14 16:55:21 -050046struct 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 Osman7c979f52019-02-12 13:27:51 -050063
64struct SkCurve {
65 SkCurve(SkScalar c = 0.0f) {
Brian Osman8b6283f2019-02-14 16:55:21 -050066 fSegments.push_back().setConstant(c);
Brian Osman7c979f52019-02-12 13:27:51 -050067 }
68
Brian Osmane12e4992019-02-19 15:39:18 -050069 // Evaluate this curve at x, using random for curves that have ranged or bidirectional segments.
Brian Osman8b6283f2019-02-14 16:55:21 -050070 SkScalar eval(SkScalar x, SkRandom& random) const;
Brian Osman7c979f52019-02-12 13:27:51 -050071 void visitFields(SkFieldVisitor* v);
Brian Osmane12e4992019-02-19 15:39:18 -050072
73 // Returns the (very conversative) range of this SkCurve in extents (as [minimum, maximum]).
Brian Osman112aa2d2019-02-15 10:45:56 -050074 void getExtents(SkScalar extents[2]) const;
Brian Osman7c979f52019-02-12 13:27:51 -050075
Brian Osmane12e4992019-02-19 15:39:18 -050076 // It should always be true that (fXValues.count() + 1) == fSegments.count()
Brian Osman8b6283f2019-02-14 16:55:21 -050077 SkTArray<SkScalar, true> fXValues;
78 SkTArray<SkCurveSegment, true> fSegments;
Brian Osman7c979f52019-02-12 13:27:51 -050079};
80
81#endif // SkCurve_DEFINED