FERS 1.0.0
The Flexible Extensible Radar Simulator
Loading...
Searching...
No Matches
path.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-2.0-only
2//
3// Copyright (c) 2006-2008 Marc Brooker and Michael Inggs
4// Copyright (c) 2008-present FERS Contributors (see AUTHORS.md).
5//
6// See the GNU GPLv2 LICENSE file in the FERS project root for more information.
7
8/**
9 * @file path.cpp
10 * @brief Implementation of the Path class.
11 */
12
13#include "path.h"
14
15#include <algorithm>
16
17#include "coord.h"
18#include "core/logging.h"
19#include "geometry_ops.h"
20#include "path_utils.h"
21
22using logging::Level;
23
24namespace math
25{
26 void Path::addCoord(const Coord& coord) noexcept
27 {
28 auto comp = [](const Coord& a, const Coord& b) { return a.t < b.t; };
29
30 const auto iter = std::ranges::lower_bound(_coords, coord, comp);
31 _coords.insert(iter, coord);
32 _final = false;
33 }
34
36 {
37 if (!_final)
38 {
39 LOG(Level::FATAL, "Finalize not called before GetPosition");
40 throw PathException("Finalize not called before GetPosition");
41 }
42
43 Coord coord{};
44 switch (_type)
45 {
47 getPositionStatic(coord, _coords);
48 break;
50 getPositionLinear(t, coord, _coords);
51 break;
53 getPositionCubic(t, coord, _coords, _dd);
54 break;
55 }
56 return coord.pos;
57 }
58
60 {
61 if (!_final)
62 {
63 LOG(Level::FATAL, "Finalize not called before GetVelocity");
64 throw PathException("Finalize not called before GetVelocity");
65 }
66
67 if (_coords.empty())
68 {
69 return {0, 0, 0};
70 }
71
72 switch (_type)
73 {
75 return {0, 0, 0};
76
78 {
79 auto xrp = std::ranges::upper_bound(_coords, t, {}, &Coord::t);
80 size_t idx = std::distance(_coords.begin(), xrp);
81
82 // Clamp to valid segments
83 if (idx == 0)
84 idx = 1;
85 if (idx >= _coords.size())
86 idx = _coords.size() - 1;
87
88 if (idx < 1)
89 return {0, 0, 0}; // Should not happen if size >= 1 and clamp works
90
91 const auto& p1 = _coords[idx - 1];
92 const auto& p2 = _coords[idx];
93 const RealType dt = p2.t - p1.t;
94
95 if (dt <= EPSILON)
96 return {0, 0, 0};
97
98 return (p2.pos - p1.pos) / dt;
99 }
100
102 {
103 auto xrp = std::ranges::upper_bound(_coords, t, {}, &Coord::t);
104 size_t xri;
105 if (xrp == _coords.begin())
106 xri = 1;
107 else if (xrp == _coords.end())
108 xri = _coords.size() - 1;
109 else
110 xri = std::distance(_coords.begin(), xrp);
111
112 if (xri < 1 || xri >= _coords.size())
113 return {0, 0, 0};
114
115 size_t xli = xri - 1;
116
117 const RealType h = _coords[xri].t - _coords[xli].t;
118 if (h <= EPSILON)
119 return {0, 0, 0};
120
121 const RealType a = (_coords[xri].t - t) / h;
122 const RealType b = (t - _coords[xli].t) / h;
123
124 // Derivative coefficients
125 // da/dt = -1/h
126 // db/dt = 1/h
127 // dc/dt = -h/6 * (3a^2 - 1)
128 // dd/dt = h/6 * (3b^2 - 1)
129
130 const RealType da = -1.0 / h;
131 const RealType db = 1.0 / h;
132 const RealType dc = -h / 6.0 * (3.0 * a * a - 1.0);
133 const RealType dd_coeff = h / 6.0 * (3.0 * b * b - 1.0);
134
135 return _coords[xli].pos * da + _coords[xri].pos * db + _dd[xli].pos * dc + _dd[xri].pos * dd_coeff;
136 }
137 }
138 return {0, 0, 0};
139 }
140
142 {
143 if (!_final)
144 {
145 switch (_type)
146 {
149 break;
151 finalizeCubic<Coord>(_coords, _dd);
152 break;
153 }
154 _final = true;
155 }
156 }
157
158 void Path::setInterp(const InterpType settype) noexcept
159 {
160 _type = settype;
161 _final = false;
162 }
163}
Exception class for handling path-related errors.
Definition path_utils.h:27
Vec3 getPosition(RealType t) const
Retrieves the position at a given time along the path.
Definition path.cpp:35
void setInterp(InterpType settype) noexcept
Changes the interpolation type.
Definition path.cpp:158
InterpType
Types of interpolation supported by the Path class.
Definition path.h:36
Vec3 getVelocity(RealType t) const
Retrieves the velocity at a given time along the path.
Definition path.cpp:59
void addCoord(const Coord &coord) noexcept
Adds a coordinate to the path.
Definition path.cpp:26
void finalize()
Finalizes the path, preparing it for interpolation.
Definition path.cpp:141
A class representing a vector in rectangular coordinates.
double RealType
Type for real numbers.
Definition config.h:27
constexpr RealType EPSILON
Machine epsilon for real numbers.
Definition config.h:51
Coordinate and rotation structure operations.
Classes and operations for 3D geometry.
Header file for the logging system.
#define LOG(level,...)
Definition logging.h:19
Definition coord.h:18
Provides the definition and functionality of the Path class for handling coordinate-based paths with ...
Utility functions for path interpolation and exception handling.
void getPositionLinear(RealType t, T &coord, const std::vector< T > &coords)
Performs linear interpolation between coordinate points.
Definition path_utils.h:85
void getPositionStatic(T &coord, const std::vector< T > &coords)
Interpolates a static position from a list of coordinates.
Definition path_utils.h:66
void getPositionCubic(RealType t, T &coord, const std::vector< T > &coords, const std::vector< T > &dd)
Performs cubic spline interpolation between coordinate points.
Definition path_utils.h:128
Represents a position in 3D space with an associated time.
Definition coord.h:24
RealType t
Time.
Definition coord.h:26