wavelength 1.0.0
Loading...
Searching...
No Matches
rndanimatable.h
Go to the documentation of this file.
1#pragma once
2
3#include <list>
4
5#include "os/binstream.h"
6#include "os/prnstream.h"
7#include "rnd/rndobject.h"
8
16class RndAnimatable : public virtual RndObject {
17public:
26
32 class Filter {
33 public:
40 virtual float Apply(float fValue) = 0;
41
48 virtual float Unapply(float fValue) = 0;
49
55 virtual void Print(PrnStream &stream) const = 0;
56
62 virtual void Save(BinStream &stream) = 0;
63
69 virtual void Load(BinStream &stream) = 0;
70
76 virtual int Type() = 0;
77
83 virtual void Copy(const Filter *pSource) = 0;
84 };
85
87 class ScaleOffset : public Filter {
88 public:
97 float Apply(float fValue) override {
98 return (fValue * mScale) + mOffset;
99 }
100
109 float Unapply(float fValue) override {
110 return (fValue - mOffset) / mScale;
111 }
112
120 void Print(PrnStream &stream) const override;
121
129 void Save(BinStream &stream) override {
130 stream.WriteEndian(&mScale, sizeof(mScale));
131 stream.WriteEndian(&mOffset, sizeof(mOffset));
132 }
133
141 void Load(BinStream &stream) override {
142 stream.ReadEndian(&mScale, sizeof(mScale));
143 stream.ReadEndian(&mOffset, sizeof(mOffset));
144 }
145
153 int Type() override {
154 return kFilterScaleOffset;
155 }
156
164 void Copy(const Filter *pSource) override {
165 *this = *static_cast<const ScaleOffset *>(pSource);
166 }
167
169 void SetOffset(float fOffset) {
170 mOffset = fOffset;
171 }
172
174 float GetOffset() const {
175 return mOffset;
176 }
177
179 float GetScale() const {
180 return mScale;
181 }
182
184 void SetScale(float fScale) {
185 mScale = fScale;
186 }
187
188 private:
189 float mScale;
190 float mOffset;
191 };
192
194 class MinMaxLoop : public Filter {
195 public:
204 float Apply(float fValue) override;
205
214 float Unapply(float fValue) override;
215
223 void Print(PrnStream &stream) const override;
224
232 void Save(BinStream &stream) override {
233 stream.WriteEndian(&mMin, sizeof(mMin));
234 stream.WriteEndian(&mMax, sizeof(mMax));
235 const char bLoop = static_cast<char>(mLoop);
236 stream.Write(&bLoop, sizeof(bLoop));
237 }
238
246 void Load(BinStream &stream) override {
247 stream.ReadEndian(&mMin, sizeof(mMin));
248 stream.ReadEndian(&mMax, sizeof(mMax));
249 char bLoop;
250 stream.Read(&bLoop, sizeof(bLoop));
251 mLoop = bLoop != 0;
252 }
253
261 int Type() override {
262 return kFilterMinMaxLoop;
263 }
264
272 void Copy(const Filter *pSource) override {
273 *this = *static_cast<const MinMaxLoop *>(pSource);
274 }
275
277 void SetMin(float fMin) {
278 mMin = fMin;
279 }
280
282 void SetMax(float fMax) {
283 mMax = fMax;
284 }
285
287 void SetLoop(int nLoop) {
288 mLoop = nLoop;
289 }
290
291 private:
292 float mMin;
293 float mMax;
294 int mLoop;
295 };
296
298 class ZeroOrder : public Filter {
299 public:
308 float Apply(float fValue) override;
309
318 float Unapply(float fValue) override;
319
327 void Print(PrnStream &stream) const override;
328
336 void Save(BinStream &stream) override {
337 stream.WriteEndian(&mLevel, sizeof(mLevel));
338 stream.WriteEndian(&mMaxDelta, sizeof(mMaxDelta));
339 }
340
348 void Load(BinStream &stream) override {
349 stream.ReadEndian(&mLevel, sizeof(mLevel));
350 stream.ReadEndian(&mMaxDelta, sizeof(mMaxDelta));
351 }
352
360 int Type() override {
361 return kFilterZeroOrder;
362 }
363
371 void Copy(const Filter *pSource) override {
372 *this = *static_cast<const ZeroOrder *>(pSource);
373 }
374
375 private:
376 float mLevel;
377 float mMaxDelta;
378 };
379
381 class FirstOrder : public Filter {
382 public:
391 float Apply(float fValue) override {
392 mLevel += (fValue - mLevel) * mRatio;
393 return mLevel;
394 }
395
404 float Unapply(float fValue) override {
405 return (mLevel - (fValue * mRatio)) / (1.0f - mRatio);
406 }
407
415 void Print(PrnStream &stream) const override;
416
424 void Save(BinStream &stream) override {
425 stream.WriteEndian(&mLevel, sizeof(mLevel));
426 stream.WriteEndian(&mRatio, sizeof(mRatio));
427 }
428
436 void Load(BinStream &stream) override {
437 stream.ReadEndian(&mLevel, sizeof(mLevel));
438 stream.ReadEndian(&mRatio, sizeof(mRatio));
439 }
440
448 int Type() override {
449 return kFilterFirstOrder;
450 }
451
459 void Copy(const Filter *pSource) override {
460 *this = *static_cast<const FirstOrder *>(pSource);
461 }
462
463 private:
464 float mLevel;
465 float mRatio;
466 };
467
469 class SecondOrder : public Filter {
470 public:
476 SecondOrder() : mVel(0.0f) {
477 }
478
487 float Apply(float fValue) override;
488
497 float Unapply(float fValue) override;
498
506 void Print(PrnStream &stream) const override;
507
517 void Save(BinStream &stream) override {
518 stream.WriteEndian(&mLevel, sizeof(mLevel));
519 stream.WriteEndian(&mSpring, sizeof(mSpring));
520 stream.WriteEndian(&mDamper, sizeof(mDamper));
521 }
522
530 void Load(BinStream &stream) override {
531 stream.ReadEndian(&mLevel, sizeof(mLevel));
532 stream.ReadEndian(&mSpring, sizeof(mSpring));
533 stream.ReadEndian(&mDamper, sizeof(mDamper));
534 }
535
543 int Type() override {
544 return kFilterSecondOrder;
545 }
546
554 void Copy(const Filter *pSource) override {
555 *this = *static_cast<const SecondOrder *>(pSource);
556 }
557
559 float GetLevel() const {
560 return mLevel;
561 }
562
564 void SetLevel(float fLevel) {
565 mLevel = fLevel;
566 }
567
569 float GetVel() const {
570 return mVel;
571 }
572
574 void SetVel(float fVel) {
575 mVel = fVel;
576 }
577
579 void SetSpring(float fSpring) {
580 mSpring = fSpring;
581 }
582
584 void SetDamper(float fDamper) {
585 mDamper = fDamper;
586 }
587
588 private:
589 float mLevel;
590 float mSpring;
591 float mDamper;
592 float mVel;
593 };
594
601 }
602
609 ~RndAnimatable() override;
610
620 virtual float EndFrame();
621
631 virtual void ListAnimObjects(std::list<RndObject *> &objects);
632
643 virtual int SetFrameSelf([[maybe_unused]] float fFrame) {
644 return 1;
645 }
646
656 void DumpText(PrnStream &stream) override;
657
665 void Save(BinStream &stream) override;
666
677 void Replace(RndObject *pFrom, RndObject *pTo) override;
678
687 void Copy(const RndObject *pSource, int nFlags) override;
688
696 void Load(BinStream &stream) override;
697
706
714 void SetFrame(float fFrame);
715
724 float FilterFrame(float fValue);
725
734 float UnfilterFrame(float fValue);
735
747
758 bool AddAnim(RndAnimatable *pAnim);
759
768
776
787 static Filter *NewFilter(int nType);
788
803 static void CollectChildren(std::list<RndObject *> &objects, RndAnimatable *pAnimatable) {
804 if (pAnimatable == nullptr) {
805 return;
806 }
807 for (RndAnimatable *pChild : pAnimatable->mAnims) {
808 objects.push_back(pChild);
809 CollectChildren(objects, pChild);
810 }
811 }
812
819 static int sRev;
820
822 const std::list<Filter *> &GetFilters() const {
823 return mFilters;
824 }
825
827 float GetFrame() const {
828 return mFrame;
829 }
830
832 float GetFilteredFrame() const {
833 return mFilteredFrame;
834 }
835
837 const std::list<RndAnimatable *> &GetAnims() const {
838 return mAnims;
839 }
840
842 std::list<Filter *> &GetFilters() {
843 return mFilters;
844 }
845
846protected:
856
866
867 std::list<RndAnimatable *> mAnims;
868
869 std::list<Filter *> mFilters;
870
871 float mFrame;
872
874};
875
886
899
910
920BinStream &operator<<(BinStream &stream, const std::list<RndAnimatable::Filter *> &filters);
921
931BinStream &operator>>(BinStream &stream, std::list<RndAnimatable::Filter *> &filters);
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.
virtual void Read(void *pData, int nBytes)=0
Read bytes.
virtual void Write(const void *pData, int nBytes)=0
Write bytes.
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
One stage of the frame filter chain.
Definition rndanimatable.h:32
virtual float Apply(float fValue)=0
Filter a frame.
virtual float Unapply(float fValue)=0
Undo Apply() as far as the stage allows.
virtual int Type()=0
Report the type tag.
virtual void Print(PrnStream &stream) const =0
Write the parameters.
virtual void Load(BinStream &stream)=0
Read what Save() wrote.
virtual void Save(BinStream &stream)=0
Write the parameters.
virtual void Copy(const Filter *pSource)=0
Copy the parameters of a stage of the same type.
Stage that moves the level toward the frame by a fixed ratio of the gap.
Definition rndanimatable.h:381
float Unapply(float fValue) override
Report the frame that leads from the level to a filtered frame.
Definition rndanimatable.h:404
int Type() override
Report the type tag.
Definition rndanimatable.h:448
void Print(PrnStream &stream) const override
Write the level and the ratio.
void Save(BinStream &stream) override
Write the level and the ratio.
Definition rndanimatable.h:424
void Copy(const Filter *pSource) override
Copy another stage.
Definition rndanimatable.h:459
float Apply(float fValue) override
Move the level toward a frame.
Definition rndanimatable.h:391
void Load(BinStream &stream) override
Read the level and the ratio.
Definition rndanimatable.h:436
Stage that clamps the frame to a range, or wraps it around the range.
Definition rndanimatable.h:194
void Save(BinStream &stream) override
Write the range and the loop flag.
Definition rndanimatable.h:232
void Load(BinStream &stream) override
Read the range and the loop flag.
Definition rndanimatable.h:246
void SetLoop(int nLoop)
Definition rndanimatable.h:287
void SetMin(float fMin)
Definition rndanimatable.h:277
void SetMax(float fMax)
Definition rndanimatable.h:282
float Apply(float fValue) override
Clamp or wrap a frame.
void Print(PrnStream &stream) const override
Write the range and the loop flag.
void Copy(const Filter *pSource) override
Copy another stage.
Definition rndanimatable.h:272
int Type() override
Report the type tag.
Definition rndanimatable.h:261
float Unapply(float fValue) override
Report the frame unchanged.
Stage that scales the frame and adds an offset.
Definition rndanimatable.h:87
void SetOffset(float fOffset)
Definition rndanimatable.h:169
void Load(BinStream &stream) override
Read the scale and the offset.
Definition rndanimatable.h:141
void SetScale(float fScale)
Definition rndanimatable.h:184
int Type() override
Report the type tag.
Definition rndanimatable.h:153
float Apply(float fValue) override
Scale and offset a frame.
Definition rndanimatable.h:97
float Unapply(float fValue) override
Remove the offset and the scale.
Definition rndanimatable.h:109
float GetScale() const
Definition rndanimatable.h:179
float GetOffset() const
Definition rndanimatable.h:174
void Print(PrnStream &stream) const override
Write the scale and the offset.
void Copy(const Filter *pSource) override
Copy another stage.
Definition rndanimatable.h:164
void Save(BinStream &stream) override
Write the scale and the offset.
Definition rndanimatable.h:129
SecondOrder()
Construct a stage at rest.
Definition rndanimatable.h:476
int Type() override
Report the type tag.
Definition rndanimatable.h:543
void Load(BinStream &stream) override
Read the level, the spring, and the damper.
Definition rndanimatable.h:530
float GetVel() const
Definition rndanimatable.h:569
void Copy(const Filter *pSource) override
Copy another stage.
Definition rndanimatable.h:554
void Print(PrnStream &stream) const override
Write the level, the spring, the damper, and the velocity.
void SetSpring(float fSpring)
Definition rndanimatable.h:579
void SetLevel(float fLevel)
Definition rndanimatable.h:564
float Unapply(float fValue) override
Report the level one step back.
void SetDamper(float fDamper)
Definition rndanimatable.h:584
void Save(BinStream &stream) override
Write the level, the spring, and the damper.
Definition rndanimatable.h:517
void SetVel(float fVel)
Definition rndanimatable.h:574
float Apply(float fValue) override
Advance the spring toward a frame.
float GetLevel() const
Definition rndanimatable.h:559
Stage that follows the frame by at most a fixed step a call.
Definition rndanimatable.h:298
int Type() override
Report the type tag.
Definition rndanimatable.h:360
float Unapply(float fValue) override
Report the level one step back from a frame.
void Load(BinStream &stream) override
Read the level and the step.
Definition rndanimatable.h:348
float Apply(float fValue) override
Step the level toward a frame.
void Save(BinStream &stream) override
Write the level and the step.
Definition rndanimatable.h:336
void Copy(const Filter *pSource) override
Copy another stage.
Definition rndanimatable.h:371
void Print(PrnStream &stream) const override
Write the level and the step.
RndAnimatable()
Construct an animatable at frame 0 without children or filters.
Definition rndanimatable.h:600
virtual float EndFrame()
Report the frame at which the animation ends.
float mFrame
Definition rndanimatable.h:871
float mFilteredFrame
Definition rndanimatable.h:873
float GetFrame() const
Definition rndanimatable.h:827
RndAnimatable * Parent()
Find the animatable whose children include this one.
void Load(BinStream &stream) override
Read what Save() wrote.
~RndAnimatable() override
Drop the references on the children and delete the filters.
void AcquireAnimRefs()
Take a reference on every child.
const std::list< Filter * > & GetFilters() const
Definition rndanimatable.h:822
std::list< Filter * > & GetFilters()
Definition rndanimatable.h:842
static void CollectChildren(std::list< RndObject * > &objects, RndAnimatable *pAnimatable)
Append every animation descendant of an animatable, depth first, each before its descendants.
Definition rndanimatable.h:803
float GetFilteredFrame() const
Definition rndanimatable.h:832
void RemoveAllAnims()
Remove every child.
void Replace(RndObject *pFrom, RndObject *pTo) override
Replace a child with another object.
static int sRev
The format version Save() writes and Load() accepts.
Definition rndanimatable.h:819
void RemoveAnim(RndAnimatable *pAnim)
Remove a child.
void ReleaseObjects()
Drop the references on the children without emptying the list, and delete the filters.
std::list< RndAnimatable * > mAnims
Definition rndanimatable.h:867
void DumpText(PrnStream &stream) override
Write a description of the animatable.
void Copy(const RndObject *pSource, int nFlags) override
Copy the filters, and the children when the flags request them.
float ChildrenEndFrame()
Report the latest end frame of the children, unfiltered through this animatable.
float UnfilterFrame(float fValue)
Run a filtered frame back through the filters, last first.
float FilterFrame(float fValue)
Run a frame through the filters.
void SetFrame(float fFrame)
Filter a frame, apply it, and pass the result on to the children.
virtual int SetFrameSelf(float fFrame)
Apply a filtered frame to this object alone.
Definition rndanimatable.h:643
void Save(BinStream &stream) override
Write the filters and the names of the children.
bool AddAnim(RndAnimatable *pAnim)
Append a child.
std::list< Filter * > mFilters
Definition rndanimatable.h:869
virtual void ListAnimObjects(std::list< RndObject * > &objects)
Add the objects the animation uses to a list.
FilterType
The type tags of the filters, as Save() writes them.
Definition rndanimatable.h:19
@ kFilterFirstOrder
Definition rndanimatable.h:23
@ kFilterZeroOrder
Definition rndanimatable.h:22
@ kFilterSecondOrder
Definition rndanimatable.h:24
@ kFilterScaleOffset
Definition rndanimatable.h:20
@ kFilterMinMaxLoop
Definition rndanimatable.h:21
const std::list< RndAnimatable * > & GetAnims() const
Definition rndanimatable.h:837
static Filter * NewFilter(int nType)
Create a filter stage of a type, with its parameters unset.
RndObject(const char *pszName)
Construct an object and register it under a name.
PrnStream & operator<<(PrnStream &stream, RndAnimatable::Filter *pFilter)
Write a filter stage as its type tag and its parameters.
BinStream & operator>>(BinStream &stream, RndAnimatable::Filter *&pFilter)
Read a filter stage as its type tag and its parameters.