|
Phyth v0.1.0
A dimensional safe C++ header-only mechanics and electromagnetics simulation library.
|
Time-based history with linear interpolation. More...
#include <TimeHistory.hpp>
Public Types | |
| using | ValueType = T |
Public Member Functions | |
| void | Register (T value) |
| Append a value to the history. | |
| ValueType | GetValueByTime (const Quantity< Second > time) const |
| Get the value at the specified time (linear interpolation) | |
| ValueType | GetValueByOffset (const Quantity< Second > offset) const |
| Get the value at a certain time offset before the current time. | |
| size_t | GetSize () const noexcept |
| Get the number of stored samples. | |
| bool | IsEmpty () const noexcept |
| Check if the history is empty. | |
Protected Attributes | |
| std::vector< ValueType > | history_ |
Time-based history with linear interpolation.
TimeHistory stores a sequence of values sampled at fixed time intervals (Config::dt). It provides methods to retrieve values at arbitrary times using linear interpolation between samples.
This is useful for:
| T | The type of value to store (typically Quantity or Vector3<Quantity>) |
Example: TimeHistory<Quantity<Meter>> history; for (double t = 0; t < 10; t += Config::dt) { history.Register(particle.GetPosition().x); } auto pos_at_2_5s = history.GetValueByTime(2.5_s); auto pos_1s_ago = history.GetValueByOffset(1.0_s);
Definition at line 35 of file TimeHistory.hpp.
Definition at line 37 of file TimeHistory.hpp.
|
inlinenoexcept |
Get the number of stored samples.
Definition at line 122 of file TimeHistory.hpp.
Get the value at a certain time offset before the current time.
This is useful for implementing delays or looking up past states. The "current time" is implicitly the last recorded sample point.
| offset | The time to look back (must be >= 0 and not exceed history duration) |
| std::out_of_range | If offset is invalid or exceeds history range |
Example: // Get the position 0.5 seconds ago auto past_pos = history.GetValueByOffset(0.5_s);
Note: offset = 0_s returns the most recent recorded value (not interpolated to the exact current time, but the last stored sample point).
Definition at line 104 of file TimeHistory.hpp.
Get the value at the specified time (linear interpolation)
If the time exactly matches a sampling point, returns the stored value. Otherwise, performs linear interpolation between adjacent samples.
| time | The time to query (must be >= 0 and within recorded range) |
| std::out_of_range | If time exceeds the historical data range |
Example: // If history has samples at t=0, 0.001, 0.002, ... auto val = history.GetValueByTime(0.0015_s); // interpolates between t=0.001 and t=0.002
Definition at line 65 of file TimeHistory.hpp.
|
inlinenoexcept |
Check if the history is empty.
Definition at line 125 of file TimeHistory.hpp.
Append a value to the history.
| value | The value to store at the current time step |
Each call should correspond to one simulation time step. The time index is implicit: the first call is t=0, second is t=Config::dt, etc.
Definition at line 47 of file TimeHistory.hpp.
|
protected |
Definition at line 128 of file TimeHistory.hpp.