RavEngine
Loading...
Searching...
No Matches
track.h
1//----------------------------------------------------------------------------//
2// //
3// ozz-animation is hosted at http://github.com/guillaumeblanc/ozz-animation //
4// and distributed under the MIT License (MIT). //
5// //
6// Copyright (c) Guillaume Blanc //
7// //
8// Permission is hereby granted, free of charge, to any person obtaining a //
9// copy of this software and associated documentation files (the "Software"), //
10// to deal in the Software without restriction, including without limitation //
11// the rights to use, copy, modify, merge, publish, distribute, sublicense, //
12// and/or sell copies of the Software, and to permit persons to whom the //
13// Software is furnished to do so, subject to the following conditions: //
14// //
15// The above copyright notice and this permission notice shall be included in //
16// all copies or substantial portions of the Software. //
17// //
18// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR //
19// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, //
20// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL //
21// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER //
22// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING //
23// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER //
24// DEALINGS IN THE SOFTWARE. //
25// //
26//----------------------------------------------------------------------------//
27
28#ifndef OZZ_OZZ_ANIMATION_RUNTIME_TRACK_H_
29#define OZZ_OZZ_ANIMATION_RUNTIME_TRACK_H_
30
31#include "ozz/animation/runtime/export.h"
32#include "ozz/base/io/archive_traits.h"
33#include "ozz/base/maths/quaternion.h"
34#include "ozz/base/maths/vec_float.h"
35#include "ozz/base/platform.h"
36#include "ozz/base/span.h"
37
38namespace ozz {
39namespace animation {
40
41// Forward declares the TrackBuilder, used to instantiate a Track.
42namespace offline {
43class TrackBuilder;
44}
45
46namespace internal {
47// Runtime user-channel track internal implementation.
48// The runtime track data structure exists for 1 to 4 float types (FloatTrack,
49// ..., Float4Track) and quaternions (QuaternionTrack). See RawTrack for more
50// details on track content. The runtime track data structure is optimized for
51// the processing of ozz::animation::TrackSamplingJob and
52// ozz::animation::TrackTriggeringJob. Keyframe ratios, values and interpolation
53// mode are all store as separate buffers in order to access the cache
54// coherently. Ratios are usually accessed/read alone from the jobs that all
55// start by looking up the keyframes to interpolate indeed.
56template <typename _ValueType>
57class OZZ_ANIMATION_DLL Track {
58 public:
59 typedef _ValueType ValueType;
60
61 Track();
62
63 // Allow move.
64 Track(Track&& _other);
65 Track& operator=(Track&& _other);
66
67 // Disables copy and assignation.
68 Track(Track const&) = delete;
69 void operator=(Track const&) = delete;
70
71 ~Track();
72
73 // Keyframe accessors.
74 span<const float> ratios() const { return ratios_; }
75 span<const _ValueType> values() const { return values_; }
76 span<const uint8_t> steps() const { return steps_; }
77
78 // Get the estimated track's size in bytes.
79 size_t size() const;
80
81 // Get track name.
82 const char* name() const { return name_ ? name_ : ""; }
83
84 // Serialization functions.
85 // Should not be called directly but through io::Archive << and >> operators.
86 void Save(ozz::io::OArchive& _archive) const;
87 void Load(ozz::io::IArchive& _archive, uint32_t _version);
88
89 private:
90 // TrackBuilder class is allowed to allocate a Track.
91 friend class offline::TrackBuilder;
92
93 // Internal destruction function.
94 void Allocate(size_t _keys_count, size_t _name_len);
95 void Deallocate();
96
97 // Keyframe ratios (0 is the beginning of the track, 1 is the end).
98 span<float> ratios_;
99
100 // Keyframe values.
101 span<_ValueType> values_;
102
103 // Keyframe modes (1 bit per key): 1 for step, 0 for linear.
104 span<uint8_t> steps_;
105
106 // Track name.
107 char* name_ = nullptr;
108};
109
110// Definition of operations policies per track value type.
111template <typename _ValueType>
113 inline static _ValueType Lerp(const _ValueType& _a, const _ValueType& _b,
114 float _alpha) {
115 return math::Lerp(_a, _b, _alpha);
116 }
117 inline static float Distance(const _ValueType& _a, const _ValueType& _b) {
118 return math::Length(_a - _b);
119 }
120 inline static _ValueType identity() { return _ValueType(0.f); }
121};
122
123// Specialization for float policy.
124template <>
125inline float TrackPolicy<float>::Distance(const float& _a, const float& _b) {
126 return std::abs(_a - _b);
127}
128
129// Specialization for quaternions policy.
130template <>
131inline math::Quaternion TrackPolicy<math::Quaternion>::Lerp(
132 const math::Quaternion& _a, const math::Quaternion& _b, float _alpha) {
133 // Uses NLerp to favor speed. This same function is used when optimizing the
134 // curve (key frame reduction), so "constant speed" interpolation can still be
135 // approximated with a lower tolerance value if it matters.
136 return math::NLerp(_a, _b, _alpha);
137}
138template <>
139inline float TrackPolicy<math::Quaternion>::Distance(
140 const math::Quaternion& _a, const math::Quaternion& _b) {
141 const float cos_half_angle =
142 _a.x * _b.x + _a.y * _b.y + _a.z * _b.z + _a.w * _b.w;
143 // Return value is 1 - half cosine, so the closer the quaternions, the closer
144 // to 0.
145 return 1.f - math::Min(1.f, std::abs(cos_half_angle));
146}
147template <>
148inline math::Quaternion TrackPolicy<math::Quaternion>::identity() {
149 return math::Quaternion::identity();
150}
151} // namespace internal
152
153// Runtime track data structure instantiation.
154class OZZ_ANIMATION_DLL FloatTrack : public internal::Track<float> {};
155class OZZ_ANIMATION_DLL Float2Track : public internal::Track<math::Float2> {};
156class OZZ_ANIMATION_DLL Float3Track : public internal::Track<math::Float3> {};
157class OZZ_ANIMATION_DLL Float4Track : public internal::Track<math::Float4> {};
158class OZZ_ANIMATION_DLL QuaternionTrack
159 : public internal::Track<math::Quaternion> {};
160
161} // namespace animation
162namespace io {
163OZZ_IO_TYPE_VERSION(1, animation::FloatTrack)
164OZZ_IO_TYPE_TAG("ozz-float_track", animation::FloatTrack)
165OZZ_IO_TYPE_VERSION(1, animation::Float2Track)
166OZZ_IO_TYPE_TAG("ozz-float2_track", animation::Float2Track)
167OZZ_IO_TYPE_VERSION(1, animation::Float3Track)
168OZZ_IO_TYPE_TAG("ozz-float3_track", animation::Float3Track)
169OZZ_IO_TYPE_VERSION(1, animation::Float4Track)
170OZZ_IO_TYPE_TAG("ozz-float4_track", animation::Float4Track)
171OZZ_IO_TYPE_VERSION(1, animation::QuaternionTrack)
172OZZ_IO_TYPE_TAG("ozz-quat_track", animation::QuaternionTrack)
173} // namespace io
174} // namespace ozz
175#endif // OZZ_OZZ_ANIMATION_RUNTIME_TRACK_H_
Definition track.h:155
Definition track.h:156
Definition track.h:157
Definition track.h:154
Definition track.h:159
Definition track.h:57
Definition track_builder.h:57
Definition archive.h:174
Definition archive.h:105
Definition quaternion.h:41
Definition span.h:37