Phyth v0.1.0
A dimensional safe C++ header-only mechanics and electromagnetics simulation library.
Loading...
Searching...
No Matches
Public Types | Public Member Functions | Protected Attributes | List of all members
Phyth::TimeHistory< T > Class Template Reference

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_
 

Detailed Description

template<typename T>
class Phyth::TimeHistory< T >

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:

Template Parameters
TThe 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.

Member Typedef Documentation

◆ ValueType

template<typename T >
using Phyth::TimeHistory< T >::ValueType = T

Definition at line 37 of file TimeHistory.hpp.

Member Function Documentation

◆ GetSize()

template<typename T >
size_t Phyth::TimeHistory< T >::GetSize ( ) const
inlinenoexcept

Get the number of stored samples.

Definition at line 122 of file TimeHistory.hpp.

◆ GetValueByOffset()

template<typename T >
ValueType Phyth::TimeHistory< T >::GetValueByOffset ( const Quantity< Second >  offset) const
inline

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.

Parameters
offsetThe time to look back (must be >= 0 and not exceed history duration)
Returns
The value at (current_time - offset)
Exceptions
std::out_of_rangeIf 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.

◆ GetValueByTime()

template<typename T >
ValueType Phyth::TimeHistory< T >::GetValueByTime ( const Quantity< Second >  time) const
inline

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.

Parameters
timeThe time to query (must be >= 0 and within recorded range)
Returns
The interpolated value at the requested time
Exceptions
std::out_of_rangeIf 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.

◆ IsEmpty()

template<typename T >
bool Phyth::TimeHistory< T >::IsEmpty ( ) const
inlinenoexcept

Check if the history is empty.

Definition at line 125 of file TimeHistory.hpp.

◆ Register()

template<typename T >
void Phyth::TimeHistory< T >::Register ( T  value)
inline

Append a value to the history.

Parameters
valueThe 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.

Member Data Documentation

◆ history_

template<typename T >
std::vector<ValueType> Phyth::TimeHistory< T >::history_
protected

Definition at line 128 of file TimeHistory.hpp.


The documentation for this class was generated from the following file: