ProtoTracer  1.0
Real-time 3D rendering and animation engine
Loading...
Searching...
No Matches
FunctionGenerator.cpp
Go to the documentation of this file.
1#include "FunctionGenerator.h"
2
3FunctionGenerator::FunctionGenerator(Function function, float minimum, float maximum, float period) {
4 this->function = function;
5 this->minimum = minimum;
6 this->maximum = maximum;
7 this->period = period;
8}
9
10void FunctionGenerator::SetPeriod(float period) {
11 this->period = period;
12}
13
15 this->function = function;
16}
17
19 float currentTime = fmod(micros() / 1000000.0f, period);
20 float ratio = currentTime / period;
21
22 switch (function) {
23 case Triangle:
24 return TriangleWave(ratio);
25 case Square:
26 return SquareWave(ratio);
27 case Sine:
28 return SineWave(ratio);
29 case Sawtooth:
30 return SawtoothWave(ratio);
31 case Gravity:
32 return GravityFunction(ratio);
33 default:
34 return 0.0f;
35 }
36}
37
39 float wave = (ratio > 0.5f ? 1.0f - ratio : ratio) * 2.0f;
40 return Mathematics::Map(wave, 0.0f, 1.0f, minimum, maximum);
41}
42
44 float wave = ratio > 0.5f ? 1.0f : 0.0f;
45 return Mathematics::Map(wave, 0.0f, 1.0f, minimum, maximum);
46}
47
48float FunctionGenerator::SineWave(float ratio) {
49 float wave = sinf(ratio * 360.0f * 3.14159f / 180.0f);
50 return Mathematics::Map(wave, -1.0f, 1.0f, minimum, maximum);
51}
52
54 return Mathematics::Map(ratio, 0.0f, 1.0f, minimum, maximum);
55}
56
57float FunctionGenerator::GravityFunction(float ratio) {//drop for first half, then stay at the bottom for the second half
58 return (ratio * 2.0f) < 1.0f ? ratio * ratio * 4.0f : 1.0f;
59}
Provides a generator for common mathematical wave functions.
float SquareWave(float ratio)
Generates a square wave value.
void SetPeriod(float period)
Sets the period of the waveform.
float SawtoothWave(float ratio)
Generates a sawtooth wave value.
float minimum
Minimum value of the waveform.
float maximum
Maximum value of the waveform.
Function
Enumerates the supported wave functions.
@ Square
Square waveform.
@ Gravity
Gravity-like function.
@ Sawtooth
Sawtooth waveform.
@ Sine
Sine waveform.
@ Triangle
Triangle waveform.
FunctionGenerator(Function function, float minimum, float maximum, float period)
Constructor to initialize the FunctionGenerator with parameters.
float GravityFunction(float ratio)
Generates a gravity-like function value.
float Update()
Updates and calculates the next value of the waveform.
Function function
Current waveform function.
float SineWave(float ratio)
Generates a sine wave value.
void SetFunction(Function function)
Sets the waveform function.
float TriangleWave(float ratio)
Generates a triangle wave value.
float period
Period of the waveform.
Implements a generic Kalman Filter for 1D data.
static T Map(T value, T inLow, T inMax, T outMin, T outMax)
Maps a value from one range to another.