FERS 1.0.0
The Flexible Extensible Radar Simulator
Loading...
Searching...
No Matches
path_utils.h File Reference

Utility functions for path interpolation and exception handling. More...

#include <algorithm>
+ Include dependency graph for path_utils.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

class  math::PathException
 Exception class for handling path-related errors. More...
 

Namespaces

namespace  math
 

Concepts

concept  Interpolatable
 Concept for types that can be interpolated.
 

Functions

template<Interpolatable T>
void getPositionStatic (T &coord, const std::vector< T > &coords)
 Interpolates a static position from a list of coordinates.
 
template<Interpolatable T>
void getPositionLinear (RealType t, T &coord, const std::vector< T > &coords)
 Performs linear interpolation between coordinate points.
 
template<Interpolatable T>
void getPositionCubic (RealType t, T &coord, const std::vector< T > &coords, const std::vector< T > &dd)
 Performs cubic spline interpolation between coordinate points.
 
template<Interpolatable T>
void finalizeCubic (const std::vector< T > &coords, std::vector< T > &dd)
 Finalizes cubic spline interpolation by calculating second derivatives.
 

Detailed Description

Utility functions for path interpolation and exception handling.

The cubic interpolation functions are based on methods described in "Numerical Recipes in C, Second Edition" by Press et al., but the code here is distinct from the original.

Definition in file path_utils.h.

Function Documentation

◆ finalizeCubic()

template<Interpolatable T>
void finalizeCubic ( const std::vector< T > &  coords,
std::vector< T > &  dd 
)

Finalizes cubic spline interpolation by calculating second derivatives.

Template Parameters
TThe type of the coordinate, which must satisfy the Interpolatable concept.
Parameters
coordsA vector of coordinates for which second derivatives will be calculated.
ddThe output vector that will store the calculated second derivatives.
Exceptions
PathExceptionif there are fewer than two points for interpolation.

Definition at line 173 of file path_utils.h.

174{
175 const int size = static_cast<int>(coords.size());
176 if (size < 2)
177 {
178 throw math::PathException("Not enough points for cubic interpolation");
179 }
180
181 std::vector<T> tmp(size);
182 dd.resize(size);
183
184 dd.front() = 0;
185 dd.back() = 0;
186
187 for (int i = 1; i < size - 1; ++i)
188 {
189 const T yrd = coords[i + 1] - coords[i];
190 const T yld = coords[i] - coords[i - 1];
191 const RealType xrd = coords[i + 1].t - coords[i].t;
192 const RealType xld = coords[i].t - coords[i - 1].t;
193 const RealType iw = coords[i + 1].t - coords[i - 1].t;
194 const RealType si = xld / iw;
195 const T p = dd[i - 1] * si + 2.0;
196 dd[i] = (si - 1.0) / p;
197 tmp[i] = ((yrd / xrd - yld / xld) * 6.0 / iw - tmp[i - 1] * si) / p;
198 }
199
200 for (int i = size - 2; i >= 0; --i)
201 {
202 dd[i] = dd[i] * dd[i + 1] + tmp[i];
203 }
204}
Exception class for handling path-related errors.
Definition path_utils.h:27
double RealType
Type for real numbers.
Definition config.h:27

Referenced by math::RotationPath::finalize().

+ Here is the caller graph for this function:

◆ getPositionCubic()

template<Interpolatable T>
void getPositionCubic ( RealType  t,
T &  coord,
const std::vector< T > &  coords,
const std::vector< T > &  dd 
)

Performs cubic spline interpolation between coordinate points.

The method used for calculating the spline is from "Numerical Recipes in C."

Template Parameters
TThe type of the coordinate, which must satisfy the Interpolatable concept.
Parameters
tThe interpolation factor (usually time) to determine the position.
coordThe output coordinate that will be interpolated.
coordsA vector of coordinates to interpolate between.
ddA vector of second derivatives used in the cubic interpolation.
Exceptions
PathExceptionif the list of coordinates is empty.

Definition at line 128 of file path_utils.h.

129{
130 if (coords.empty())
131 {
132 throw math::PathException("coord list empty during GetPositionCubic");
133 }
134
135 auto xrp = std::ranges::upper_bound(coords, t, {}, &T::t);
136
137 if (xrp == coords.begin())
138 {
139 coord = *xrp;
140 }
141 else if (xrp == coords.end())
142 {
143 coord = *(xrp - 1);
144 }
145 else
146 {
147 auto xri = std::distance(coords.begin(), xrp);
148 auto xli = xri - 1;
149
150 const RealType xrd = coords[xri].t - t;
151 const RealType xld = t - coords[xli].t;
152 const RealType iw = coords[xri].t - coords[xli].t;
153 const RealType iws = iw * iw / 6.0;
154 const RealType a = xrd / iw;
155 const RealType b = xld / iw;
156 const RealType c = (a * a * a - a) * iws;
157 const RealType d = (b * b * b - b) * iws;
158
159 coord = coords[xli] * a + coords[xri] * b + dd[xli] * c + dd[xri] * d;
160 }
161 coord.t = t;
162}
RealType c() noexcept
Get the speed of light.
Definition parameters.h:79

Referenced by math::Path::getPosition(), and math::RotationPath::getPosition().

+ Here is the caller graph for this function:

◆ getPositionLinear()

template<Interpolatable T>
void getPositionLinear ( RealType  t,
T &  coord,
const std::vector< T > &  coords 
)

Performs linear interpolation between coordinate points.

Template Parameters
TThe type of the coordinate, which must satisfy the Interpolatable concept.
Parameters
tThe interpolation factor (usually time) to determine the position.
coordThe output coordinate that will be interpolated.
coordsA vector of coordinates to interpolate between.
Exceptions
PathExceptionif the list of coordinates is empty.

Definition at line 85 of file path_utils.h.

86{
87 if (coords.empty())
88 {
89 throw math::PathException("coord list empty during GetPositionLinear");
90 }
91
92 auto xrp = std::ranges::upper_bound(coords, t, {}, &T::t);
93
94 if (xrp == coords.begin())
95 {
96 coord = *xrp;
97 }
98 else if (xrp == coords.end())
99 {
100 coord = *(xrp - 1);
101 }
102 else
103 {
104 auto xri = std::distance(coords.begin(), xrp);
105 auto xli = xri - 1;
106
107 const RealType iw = coords[xri].t - coords[xli].t;
108 const RealType rw = (coords[xri].t - t) / iw;
109 const RealType lw = 1 - rw;
110
111 coord = coords[xri] * lw + coords[xli] * rw;
112 }
113 coord.t = t;
114}

Referenced by math::Path::getPosition(), and math::RotationPath::getPosition().

+ Here is the caller graph for this function:

◆ getPositionStatic()

template<Interpolatable T>
void getPositionStatic ( T &  coord,
const std::vector< T > &  coords 
)

Interpolates a static position from a list of coordinates.

Template Parameters
TThe type of the coordinate, which must satisfy the Interpolatable concept.
Parameters
coordThe output coordinate to be set.
coordsA vector of coordinates from which the first will be selected.
Exceptions
PathExceptionif the list of coordinates is empty.

Definition at line 66 of file path_utils.h.

67{
68 if (coords.empty())
69 {
70 throw math::PathException("coord list empty during GetPositionStatic");
71 }
72 coord = coords[0];
73}

Referenced by math::Path::getPosition(), and math::RotationPath::getPosition().

+ Here is the caller graph for this function: