wavelength 1.0.0
Loading...
Searching...
No Matches
key.h
Go to the documentation of this file.
1#pragma once
2
3#include <vector>
4
5#include "math/color.h"
6#include "math/quaternion.h"
7#include "math/transform.h"
8#include "math/vector2.h"
9#include "math/vector3.h"
10#include "os/binstream.h"
11#include "os/prnstream.h"
12
20template <typename T>
21struct Key {
23 float frame;
24};
25
45template <typename T>
46int AtFrame(const std::vector<Key<T>> &keys,
47 float fFrame,
48 const Key<T> *&pPrev,
49 const Key<T> *&pNext,
50 float &fRatio) {
51 if (keys.empty()) {
52 pNext = nullptr;
53 pPrev = nullptr;
54 return 0;
55 }
56 if (fFrame <= keys.front().frame) {
57 pPrev = &keys.front();
58 fRatio = 0.0f;
59 pNext = &keys.front();
60 return 0;
61 }
62 const Key<T> &last = keys.back();
63 if (last.frame <= fFrame) {
64 fRatio = 0.0f;
65 pNext = &last;
66 pPrev = &last;
67 const int nSize = static_cast<int>(keys.size());
68 return fFrame == last.frame ? nSize - 1 : nSize;
69 }
70 int nLow = 0;
71 int nHigh = static_cast<int>(keys.size()) - 1;
72 while (nLow + 1 < nHigh) {
73 const int nMiddle = (nLow + nHigh) >> 1;
74 const Key<T> &middle = keys.at(nMiddle);
75 if (fFrame == middle.frame) {
76 pPrev = &middle;
77 pNext = &middle;
78 fRatio = 0.0f;
79 return nMiddle;
80 }
81 if (middle.frame < fFrame) {
82 nLow = nMiddle;
83 } else {
84 nHigh = nMiddle;
85 }
86 }
87 pPrev = &keys.at(nLow);
88 pNext = &keys.at(nHigh);
89 fRatio = (fFrame - keys.at(nLow).frame) / (keys.at(nHigh).frame - pPrev->frame);
90 return nHigh;
91}
92
103inline void Interp(const Vector3 &a, const Vector3 &b, float fRatio, Vector3 &out) {
104 if (fRatio == 0.0f) {
105 out = a;
106 } else if (fRatio == 1.0f) {
107 out = b;
108 } else {
109 const float fInverse = 1.0f - fRatio;
110 out = b;
111 out.x = b.x * fRatio + a.x * fInverse;
112 out.y = b.y * fRatio + a.y * fInverse;
113 out.z = b.z * fRatio + a.z * fInverse;
114 }
115}
116
125inline void Interp(const Vector2 &a, const Vector2 &b, float fRatio, Vector2 &out) {
126 if (fRatio == 0.0f) {
127 out = a;
128 } else if (fRatio == 1.0f) {
129 out = b;
130 } else {
131 out.x = (b.x - a.x) * fRatio + a.x;
132 out.y = (b.y - a.y) * fRatio + a.y;
133 }
134}
135
144inline void Interp(const Color &a, const Color &b, float fRatio, Color &out) {
145 const float fInverse = 1.0f - fRatio;
146 out.r = b.r * fRatio + a.r * fInverse;
147 out.g = b.g * fRatio + a.g * fInverse;
148 out.b = b.b * fRatio + a.b * fInverse;
149 out.a = b.a * fRatio + a.a * fInverse;
150}
151
160inline void Interp(float fA, float fB, float fRatio, float &fOut) {
161 fOut = (fB - fA) * fRatio + fA;
162}
163
177template <typename T>
178void Interp(T *pA, [[maybe_unused]] T *pB, [[maybe_unused]] float fRatio, T *&pOut) {
179 pOut = pA;
180}
181
192template <typename T>
193void InterpKeys(const std::vector<Key<T>> &keys, float fFrame, T &out) {
194 const Key<T> *pPrev;
195 const Key<T> *pNext;
196 float fRatio = 0.0f;
197 AtFrame(keys, fFrame, pPrev, pNext, fRatio);
198 if (pPrev != nullptr) {
199 Interp(pPrev->value, pNext->value, fRatio, out);
200 }
201}
202
210template <typename T>
211float FirstFrame(const std::vector<Key<T>> &keys) {
212 return keys.empty() ? 0.0f : keys.front().frame;
213}
214
222template <typename T>
223float LastFrame(const std::vector<Key<T>> &keys) {
224 return keys.empty() ? 0.0f : keys.back().frame;
225}
226
233inline void WriteKeyValue(BinStream &stream, float fValue) {
234 stream.WriteEndian(&fValue, sizeof(fValue));
235}
236
243inline void WriteKeyValue(BinStream &stream, const Vector3 &v) {
244 WriteKeyValue(stream, v.x);
245 WriteKeyValue(stream, v.y);
246 WriteKeyValue(stream, v.z);
247}
248
255inline void WriteKeyValue(BinStream &stream, const Vector2 &v) {
256 WriteKeyValue(stream, v.x);
257 WriteKeyValue(stream, v.y);
258}
259
266inline void WriteKeyValue(BinStream &stream, const Quat &quat) {
267 WriteKeyValue(stream, quat.x);
268 WriteKeyValue(stream, quat.y);
269 WriteKeyValue(stream, quat.z);
270 WriteKeyValue(stream, quat.w);
271}
272
279inline void WriteKeyValue(BinStream &stream, const Color &color) {
280 WriteKeyValue(stream, color.r);
281 WriteKeyValue(stream, color.g);
282 WriteKeyValue(stream, color.b);
283 WriteKeyValue(stream, color.a);
284}
285
292inline void ReadKeyValue(BinStream &stream, float &fValue) {
293 stream.ReadEndian(&fValue, sizeof(fValue));
294}
295
302inline void ReadKeyValue(BinStream &stream, Vector3 &v) {
303 ReadKeyValue(stream, v.x);
304 ReadKeyValue(stream, v.y);
305 ReadKeyValue(stream, v.z);
306}
307
314inline void ReadKeyValue(BinStream &stream, Vector2 &v) {
315 ReadKeyValue(stream, v.x);
316 ReadKeyValue(stream, v.y);
317}
318
325inline void ReadKeyValue(BinStream &stream, Quat &quat) {
326 ReadKeyValue(stream, quat.x);
327 ReadKeyValue(stream, quat.y);
328 ReadKeyValue(stream, quat.z);
329 ReadKeyValue(stream, quat.w);
330}
331
338inline void ReadKeyValue(BinStream &stream, Color &color) {
339 ReadKeyValue(stream, color.r);
340 ReadKeyValue(stream, color.g);
341 ReadKeyValue(stream, color.b);
342 ReadKeyValue(stream, color.a);
343}
344
360template <typename T>
361void WriteKeyValue(BinStream &stream, const std::vector<T> &values) {
362 const int nSize = static_cast<int>(values.size());
363 stream.WriteEndian(&nSize, sizeof(nSize));
364 for (const T &value : values) {
365 WriteKeyValue(stream, value);
366 }
367}
368
384template <typename T>
385void ReadKeyValue(BinStream &stream, std::vector<T> &values) {
386 int nSize;
387 stream.ReadEndian(&nSize, sizeof(nSize));
388 values.resize(nSize, T());
389 for (T &value : values) {
390 ReadKeyValue(stream, value);
391 }
392}
393
418template <typename T>
419BinStream &operator<<(BinStream &stream, const Key<T> &key) {
420 WriteKeyValue(stream, key.value);
421 WriteKeyValue(stream, key.frame);
422 return stream;
423}
424
449template <typename T>
451 ReadKeyValue(stream, key.value);
452 ReadKeyValue(stream, key.frame);
453 return stream;
454}
455
482template <typename T>
483PrnStream &operator<<(PrnStream &stream, const Key<T> &key) {
484 stream << "(frame:" << key.frame << " value:" << key.value << ")";
485 return stream;
486}
Stream of raw bytes, the base of the memory, file, and network streams.
Definition binstream.h:22
void ReadEndian(void *pData, int nBytes)
Read a value of 2, 4, 8, or 16 bytes, reversing its bytes unless the stream is little endian.
void WriteEndian(const void *pData, int nBytes)
Write a value of 2, 4, 8, or 16 bytes, reversing its bytes unless the stream is little endian.
Text output stream.
Definition prnstream.h:16
Rotation quaternion, stored as the vector part followed by the scalar part.
Definition quaternion.h:19
float x
Definition quaternion.h:47
float z
Definition quaternion.h:49
float y
Definition quaternion.h:48
float w
Definition quaternion.h:50
float FirstFrame(const std::vector< Key< T > > &keys)
Report the frame of the first key, or 0 for no keys.
Definition key.h:211
BinStream & operator<<(BinStream &stream, const Key< T > &key)
Write a key as its value followed by its frame.
Definition key.h:419
void InterpKeys(const std::vector< Key< T > > &keys, float fFrame, T &out)
Interpolate keys at a frame.
Definition key.h:193
BinStream & operator>>(BinStream &stream, Key< T > &key)
Read a key the key writer wrote.
Definition key.h:450
void ReadKeyValue(BinStream &stream, float &fValue)
Read a float key value.
Definition key.h:292
void WriteKeyValue(BinStream &stream, float fValue)
Write a float key value.
Definition key.h:233
float LastFrame(const std::vector< Key< T > > &keys)
Report the frame of the last key, or 0 for no keys.
Definition key.h:223
int AtFrame(const std::vector< Key< T > > &keys, float fFrame, const Key< T > *&pPrev, const Key< T > *&pNext, float &fRatio)
Find the keys around a frame.
Definition key.h:46
void Interp(const Vector3 &a, const Vector3 &b, float fRatio, Vector3 &out)
Blend two vectors, taking either end exactly at a position of 0 or 1.
Definition key.h:103
Linear colour with four float components.
Definition color.h:12
float b
Definition color.h:15
float r
Definition color.h:13
float g
Definition color.h:14
float a
Definition color.h:16
One keyframe of an animation channel, a value at a frame.
Definition key.h:21
float frame
Definition key.h:23
T value
Definition key.h:22
Two-component vector, used for texture coordinates.
Definition vector2.h:12
float x
Definition vector2.h:13
float y
Definition vector2.h:14
Three-component vector, padded to a PlayStation 2 quadword.
Definition vector3.h:14
float x
Definition vector3.h:15
float y
Definition vector3.h:16
float z
Definition vector3.h:17