FhSim  3.1.0
Marine systems simulation
Loading...
Searching...
No Matches
SpeedFrequencyLookup.h
1#ifndef SpeedFrequencyLookup_h__
2#define SpeedFrequencyLookup_h__
3
4#include <algorithm>
5#include <cstddef>
6#include <vector>
7
8namespace Ship
9{
10
13{
14 std::size_t lo;
15 double fraction;
16};
17
20inline AxisBracket FindAxisBracket(const std::vector<double>& axis, double value)
21{
22 const auto upper = std::upper_bound(axis.begin(), axis.end(), value); // first node above the value
23 std::size_t hi = static_cast<std::size_t>(upper - axis.begin());
24 hi = std::clamp<std::size_t>(hi, 1, axis.size() - 1);
25 const std::size_t lo = hi - 1;
26 return {lo, (value - axis[lo]) / (axis[hi] - axis[lo])};
27}
28
34template<class T>
35T BlendSpeedFrequency(const T& q00, const T& q01, const T& q10, const T& q11, double speedFraction, double frequencyFraction)
36{
37 return q00 * ((1.0 - speedFraction) * (1.0 - frequencyFraction)) + q01 * ((1.0 - speedFraction) * frequencyFraction) +
38 q10 * (speedFraction * (1.0 - frequencyFraction)) + q11 * (speedFraction * frequencyFraction);
39}
40
41} // namespace Ship
42
43#endif // SpeedFrequencyLookup_h__
Simple waypoint object.
Definition CableAttach.h:16
AxisBracket FindAxisBracket(const std::vector< double > &axis, double value)
Definition SpeedFrequencyLookup.h:20
T BlendSpeedFrequency(const T &q00, const T &q01, const T &q10, const T &q11, double speedFraction, double frequencyFraction)
Definition SpeedFrequencyLookup.h:35
Two neighbouring nodes of an ascending axis and where a value lies between them.
Definition SpeedFrequencyLookup.h:13
std::size_t lo
index of the lower node; lo + 1 is the upper node
Definition SpeedFrequencyLookup.h:14
double fraction
0 at the lower node, 1 at the upper node; outside [0, 1] when the value is off the axis
Definition SpeedFrequencyLookup.h:15