1//************************************ bs::framework - Copyright 2018 Marko Pintera **************************************//
2//*********** Licensed under the MIT license. See LICENSE.md for full terms. This notice is not to be removed. ***********//
3#pragma once
4
5#include "BsCorePrerequisites.h"
6
7namespace bs
8{
9 /** @addtogroup Animation-Internal
10 * @{
11 */
12
13 /**
14 * Holds cached information used for animation curve evaluation so that sequential evaluations can be sped up.
15 * You should not use the same instance of this object for evaluating multiple different animation curves.
16 */
17 template <class T>
18 struct TCurveCache
19 {
20 private:
21 friend class TAnimationCurve<T>;
22
23 /** Left-most key the curve was last evaluated at. -1 if no cached data. */
24 mutable UINT32 cachedKey = (UINT32)-1;
25
26 /** Time relative to the animation curve, at which the cached data starts. */
27 mutable float cachedCurveStart = std::numeric_limits<float>::infinity();
28
29 /** Time relative to the animation curve, at which the cached data end. */
30 mutable float cachedCurveEnd = 0.0f;
31
32 /**
33 * Coefficients of the cubic hermite curve, in order [t^3, t^2, t, 1]. Coefficients assume unnormalized @p t, with
34 * length of @p cachedCurveEnd - @p cachedCurveStart.
35 */
36 mutable T cachedCubicCoefficients[4]{};
37 };
38
39 /**
40 * Holds cached information used for integrated animation curve evaluation. Evaluations with the cache provided are
41 * significantly faster than non-cached evaluations.
42 */
43 template <class T>
44 struct TCurveIntegrationCache
45 {
46 ~TCurveIntegrationCache()
47 {
48 bs_free(segmentSums);
49 }
50
51 private:
52 friend class TAnimationCurve<T>;
53
54 /** Initializes the memory required for single integration cache. */
55 void init(UINT32 numKeys) const
56 {
57 segmentSums = (T*)bs_alloc(sizeof(T) * numKeys * 5);
58 coeffs = (T(*)[4])(segmentSums + numKeys);
59 }
60
61 /** Initializes the memory required for double integration cache. */
62 void initDouble(UINT32 numKeys) const
63 {
64 segmentSums = (T*)bs_alloc(sizeof(T) * numKeys * 6);
65 doubleSegmentSums = (T*)(segmentSums + numKeys);
66 coeffs = (T(*)[4])(segmentSums + numKeys * 2);
67 }
68
69 /**
70 * Contains a list of cumulative areas beneath the curve of each segment. A segment represents the part of the curve
71 * between two keyframes.
72 */
73 mutable T* segmentSums = nullptr;
74
75 /**
76 * Contains a list of cumulative doubly integrated areas beneath the curve of each segment. A segment represents
77 * the part of the curve between two keyframes.
78 */
79 mutable T* doubleSegmentSums = nullptr;
80
81 /** Contains a list of integrated cubic coefficients for each keyframe. */
82 mutable T (*coeffs)[4] = nullptr;
83 };
84
85 /** @} */
86}