Initial commit - test serial
This commit is contained in:
@ -0,0 +1,65 @@
|
||||
/**
|
||||
* Copyright 2020 Jonathan Bayless
|
||||
*
|
||||
* Use of this source code is governed by an MIT-style license that can be found
|
||||
* in the LICENSE file or at https://opensource.org/licenses/MIT.
|
||||
*/
|
||||
#ifndef _MATH_QUINTIC_POLYNOMIAL_HPP_
|
||||
#define _MATH_QUINTIC_POLYNOMIAL_HPP_
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace squiggles {
|
||||
class QuinticPolynomial {
|
||||
public:
|
||||
/**
|
||||
* Defines the polynomial function for a spline in one dimension.
|
||||
*
|
||||
* @param s_p The starting position of the curve in meters.
|
||||
* @param s_v The starting velocity of the curve in meters per second.
|
||||
* @param s_a The starting acceleration of the curve in meters per second per
|
||||
* second.
|
||||
* @param g_p The goal or ending position of the curve in meters.
|
||||
* @param g_v The goal or ending velocity of the curve in meters per second.
|
||||
* @param g_a The goal or ending acceleration of the curve in meters per
|
||||
* second per second.
|
||||
* @param t The desired duration for the curve in seconds.
|
||||
*/
|
||||
QuinticPolynomial(double s_p,
|
||||
double s_v,
|
||||
double s_a,
|
||||
double g_p,
|
||||
double g_v,
|
||||
double g_a,
|
||||
double t);
|
||||
|
||||
/**
|
||||
* Calculates the values of the polynomial and its derivatives at the given
|
||||
* time stamp.
|
||||
*/
|
||||
double calc_point(double t);
|
||||
double calc_first_derivative(double t);
|
||||
double calc_second_derivative(double t);
|
||||
double calc_third_derivative(double t);
|
||||
|
||||
/**
|
||||
* Serializes the Quintic Polynomial data for debugging.
|
||||
*
|
||||
* @return The Quintic Polynomial data.
|
||||
*/
|
||||
std::string to_string() const {
|
||||
return "QuinticPolynomial: {0: " + std::to_string(a0) +
|
||||
" 1: " + std::to_string(a1) + " 2: " + std::to_string(a2) +
|
||||
" 3: " + std::to_string(a3) + " 4: " + std::to_string(a4) +
|
||||
" 5: " + std::to_string(a5) + "}";
|
||||
}
|
||||
|
||||
protected:
|
||||
/**
|
||||
* The coefficients for each term of the polynomial.
|
||||
*/
|
||||
double a0, a1, a2, a3, a4, a5;
|
||||
};
|
||||
} // namespace squiggles
|
||||
|
||||
#endif
|
61
SerialTest/include/okapi/squiggles/math/utils.hpp
Normal file
61
SerialTest/include/okapi/squiggles/math/utils.hpp
Normal file
@ -0,0 +1,61 @@
|
||||
/**
|
||||
* Copyright 2020 Jonathan Bayless
|
||||
*
|
||||
* Use of this source code is governed by an MIT-style license that can be found
|
||||
* in the LICENSE file or at https://opensource.org/licenses/MIT.
|
||||
*/
|
||||
#ifndef _MATH_UTILS_HPP_
|
||||
#define _MATH_UTILS_HPP_
|
||||
|
||||
#include <cmath>
|
||||
#include <iostream>
|
||||
|
||||
namespace squiggles {
|
||||
/**
|
||||
* Returns the sign value of the given value.
|
||||
*
|
||||
* @return 1 if the value is positive, -1 if the value is negative, and 0 if
|
||||
* the value is 0.
|
||||
*/
|
||||
template <class T> inline int sgn(T v) {
|
||||
return (v > T(0)) - (v < T(0));
|
||||
}
|
||||
|
||||
inline bool
|
||||
nearly_equal(const double& a, const double& b, double epsilon = 1e-5) {
|
||||
return std::fabs(a - b) < epsilon;
|
||||
}
|
||||
} // namespace squiggles
|
||||
|
||||
namespace std {
|
||||
// Copied from https://github.com/emsr/cxx_linear
|
||||
template <typename _Float>
|
||||
constexpr std::enable_if_t<
|
||||
std::is_floating_point_v<_Float> &&
|
||||
__cplusplus <= 201703L, // Only defines this function if C++ standard < 20
|
||||
_Float>
|
||||
lerp(_Float __a, _Float __b, _Float __t) {
|
||||
if (std::isnan(__a) || std::isnan(__b) || std::isnan(__t))
|
||||
return std::numeric_limits<_Float>::quiet_NaN();
|
||||
else if ((__a <= _Float{0} && __b >= _Float{0}) ||
|
||||
(__a >= _Float{0} && __b <= _Float{0}))
|
||||
// ab <= 0 but product could overflow.
|
||||
#ifndef FMA
|
||||
return __t * __b + (_Float{1} - __t) * __a;
|
||||
#else
|
||||
return std::fma(__t, __b, (_Float{1} - __t) * __a);
|
||||
#endif
|
||||
else if (__t == _Float{1})
|
||||
return __b;
|
||||
else { // monotonic near t == 1.
|
||||
#ifndef FMA
|
||||
const auto __x = __a + __t * (__b - __a);
|
||||
#else
|
||||
const auto __x = std::fma(__t, __b - __a, __a);
|
||||
#endif
|
||||
return (__t > _Float{1}) == (__b > __a) ? std::max(__b, __x)
|
||||
: std::min(__b, __x);
|
||||
}
|
||||
}
|
||||
} // namespace std
|
||||
#endif
|
Reference in New Issue
Block a user