444 lines
15 KiB
C++
444 lines
15 KiB
C++
// =====================================================================================================================
|
|
// fennec, a free and open source game engine
|
|
// Copyright © 2025 Medusa Slockbower
|
|
//
|
|
// This program is free software: you can redistribute it and/or modify
|
|
// it under the terms of the GNU General Public License as published by
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
// (at your option) any later version.
|
|
//
|
|
// This program is distributed in the hope that it will be useful,
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
// GNU General Public License for more details.
|
|
//
|
|
// You should have received a copy of the GNU General Public License
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
// =====================================================================================================================
|
|
|
|
///
|
|
/// \file trigonometric.h
|
|
/// \brief the \ref fennec_math_trigonometric
|
|
///
|
|
///
|
|
/// \details
|
|
/// \author Medusa Slockbower
|
|
///
|
|
/// \copyright Copyright © 2025 Medusa Slockbower ([GPLv3](https://www.gnu.org/licenses/gpl-3.0.en.html))
|
|
///
|
|
///
|
|
|
|
#ifndef FENNEC_MATH_TRIGONOMETRIC_H
|
|
#define FENNEC_MATH_TRIGONOMETRIC_H
|
|
|
|
///
|
|
///
|
|
///
|
|
/// \page fennec_math_trigonometric Trigonometry
|
|
///
|
|
/// \brief The fennec Trigonometry Module
|
|
///
|
|
///
|
|
///
|
|
/// \section section_fennec_conversions Angle Conversions
|
|
///
|
|
/// <table width="100%" class="fieldtable" id="table_fennec_math_trigonometry_angle_conversions">
|
|
/// <tr><th style="vertical-align: top">Syntax
|
|
/// <th style="vertical-align: top">Description
|
|
///
|
|
/// <tr><td width="50%" style="vertical-align: top"> <br>
|
|
/// \ref fennec::degrees "genFType degrees(genFType x)"
|
|
/// <td width="50%" style="vertical-align: top">
|
|
/// \copydoc fennec::degrees
|
|
///
|
|
/// <tr><td width="50%" style="vertical-align: top"> <br>
|
|
/// \ref fennec::radians "genFType radians(genFType x)"
|
|
/// <td width="50%" style="vertical-align: top">
|
|
/// \copydoc fennec::radians
|
|
///
|
|
/// </table>
|
|
///
|
|
///
|
|
///
|
|
/// \section section_fennec_trigonometric_functions Trigonometric Functions
|
|
///
|
|
/// <table width="100%" class="fieldtable" id="table_fennec_math_trigonometry_angle_conversions">
|
|
/// <tr><th style="vertical-align: top">Syntax
|
|
/// <th style="vertical-align: top">Description
|
|
///
|
|
/// <tr><td width="50%" style="vertical-align: top"> <br>
|
|
/// \ref fennec::sin "genFType sin(genFType x)"
|
|
/// <td width="50%" style="vertical-align: top">
|
|
/// \copydoc fennec::sin
|
|
///
|
|
/// <tr><td width="50%" style="vertical-align: top"> <br>
|
|
/// \ref fennec::cos "genFType cos(genFType x)"
|
|
/// <td width="50%" style="vertical-align: top">
|
|
/// \copydoc fennec::cos
|
|
///
|
|
/// <tr><td width="50%" style="vertical-align: top"> <br>
|
|
/// \ref fennec::tan "genFType tan(genFType x)"
|
|
/// <td width="50%" style="vertical-align: top">
|
|
/// \copydoc fennec::tan
|
|
///
|
|
///
|
|
/// <tr><td width="50%" style="vertical-align: top"> <br>
|
|
/// \ref fennec::asin "genFType asin(genFType x)"
|
|
/// <td width="50%" style="vertical-align: top">
|
|
/// \copydoc fennec::asin
|
|
///
|
|
/// <tr><td width="50%" style="vertical-align: top"> <br>
|
|
/// \ref fennec::acos "genFType acos(genFType x)"
|
|
/// <td width="50%" style="vertical-align: top">
|
|
/// \copydoc fennec::acos
|
|
///
|
|
/// <tr><td width="50%" style="vertical-align: top"> <br>
|
|
/// \ref fennec::atan "genFType atan(genFType x)"
|
|
/// <td width="50%" style="vertical-align: top">
|
|
/// \copydoc fennec::atan
|
|
///
|
|
/// </table>
|
|
///
|
|
///
|
|
///
|
|
/// \section section_fennec_hyperbolic_functions Hyperbolic Functions
|
|
///
|
|
/// <table width="100%" class="fieldtable" id="table_fennec_math_trigonometry_angle_conversions">
|
|
/// <tr><th style="vertical-align: top">Syntax
|
|
/// <th style="vertical-align: top">Description
|
|
///
|
|
/// <tr><td width="50%" style="vertical-align: top"> <br>
|
|
/// \ref fennec::sinh "genFType sinh(genFType x)"
|
|
/// <td width="50%" style="vertical-align: top">
|
|
/// \copydoc fennec::sinh
|
|
///
|
|
/// <tr><td width="50%" style="vertical-align: top"> <br>
|
|
/// \ref fennec::cosh "genFType cosh(genFType x)"
|
|
/// <td width="50%" style="vertical-align: top">
|
|
/// \copydoc fennec::cosh
|
|
///
|
|
/// <tr><td width="50%" style="vertical-align: top"> <br>
|
|
/// \ref fennec::tanh "genFType tanh(genFType x)"
|
|
/// <td width="50%" style="vertical-align: top">
|
|
/// \copydoc fennec::tanh
|
|
///
|
|
///
|
|
/// <tr><td width="50%" style="vertical-align: top"> <br>
|
|
/// \ref fennec::asinh "genFType asinh(genFType x)"
|
|
/// <td width="50%" style="vertical-align: top">
|
|
/// \copydoc fennec::asinh
|
|
///
|
|
/// <tr><td width="50%" style="vertical-align: top"> <br>
|
|
/// \ref fennec::acosh "genFType acosh(genFType x)"
|
|
/// <td width="50%" style="vertical-align: top">
|
|
/// \copydoc fennec::acosh
|
|
///
|
|
/// <tr><td width="50%" style="vertical-align: top"> <br>
|
|
/// \ref fennec::atanh "genFType atanh(genFType x)"
|
|
/// <td width="50%" style="vertical-align: top">
|
|
/// \copydoc fennec::atanh
|
|
///
|
|
/// </table>
|
|
///
|
|
///
|
|
///
|
|
///
|
|
///
|
|
|
|
#include <cmath>
|
|
|
|
namespace fennec
|
|
{
|
|
|
|
// Angle Conversions ===================================================================================================
|
|
|
|
/// \name Angle Conversions
|
|
/// @{
|
|
|
|
///
|
|
/// \brief Converts \f$degrees\f$ to \f$radians\f$, i.e., \f$degrees\cdot\frac{180}{\pi}\f$
|
|
///
|
|
/// \returns the angle \f$\theta\f$ in \f$radians\f$ <br><br>
|
|
/// \details Converts \f$degrees\f$ to \f$radians\f$, i.e., \f$degrees\cdot\frac{180}{\pi}\f$ <br><br>
|
|
///
|
|
/// \tparam genType floating point type
|
|
/// \param degrees the angle \f$\theta\f$ in \f$degrees\f$
|
|
template<typename genType>
|
|
constexpr genType radians(genType degrees)
|
|
{ return degrees * 0.01745329251994329576923690768489; }
|
|
|
|
template<typename genType, size_t...i>
|
|
constexpr vector<genType, i...> radians(const vector<genType, i...>& degrees)
|
|
{ return degrees * 0.01745329251994329576923690768489; }
|
|
|
|
|
|
///
|
|
/// \brief Converts \f$radians\f$ to \f$degrees\f$, i.e., \f$radians\cdot\frac{\pi}{180}\f$
|
|
///
|
|
/// \returns the angle \f$\theta\f$ in \f$degrees\f$ <br><br>
|
|
/// \details Converts \f$radians\f$ to \f$degrees\f$, i.e., \f$radians\cdot\frac{\pi}{180}\f$ <br><br>
|
|
///
|
|
/// \tparam genType floating point type
|
|
/// \param radians the angle \f$\theta\f$ in \f$radians\f$
|
|
template<typename genType>
|
|
constexpr genType degrees(genType radians)
|
|
{ return radians * 57.29577951308232087679815481410517; }
|
|
|
|
template<typename genType, size_t...i>
|
|
constexpr vector<genType, i...> degrees(const vector<genType, i...>& radians)
|
|
{ return radians * 57.29577951308232087679815481410517; }
|
|
|
|
/// @}
|
|
|
|
|
|
|
|
// Trigonometric ======================================================================================================
|
|
|
|
/// \name Trigonometric Functions
|
|
/// @{
|
|
|
|
///
|
|
/// \brief The standard trigonometric sine
|
|
///
|
|
/// \returns the sine of \f$\theta\f$ in the range \f$\left[-1,\,1\right]\f$ <br><br>
|
|
/// \details The standard trigonometric sine <br><br>
|
|
///
|
|
/// \tparam genType floating point type
|
|
/// \param x the angle \f$\theta\f$ in \f$radians\f$
|
|
template<typename genType>
|
|
constexpr genType sin(genType x)
|
|
{ return std::sin(x); }
|
|
|
|
template<typename genType, size_t...i>
|
|
constexpr vector<genType, i...> sin(const vector<genType, i...>& x)
|
|
{ return vector<genType, i...>(fennec::sin(x[i]) ...); }
|
|
|
|
|
|
///
|
|
/// \brief The Standard Trigonometric Cosine
|
|
///
|
|
/// \returns the cosine of \f$\theta\f$ in the range \f$\left[-1,\,1\right]\f$ <br><br>
|
|
/// \details The Standard Trigonometric Cosine <br><br>
|
|
///
|
|
/// \tparam genType floating point type
|
|
/// \param x the angle \f$\theta\f$ in \f$radians\f$
|
|
template<typename genType>
|
|
constexpr genType cos(genType x)
|
|
{ return std::cos(x); }
|
|
|
|
template<typename genType, size_t...i>
|
|
constexpr vector<genType, i...> cos(const vector<genType, i...>& x)
|
|
{ return vector<genType, i...>(fennec::cos(x[i]) ...); }
|
|
|
|
|
|
///
|
|
/// \brief The Standard Trigonometric Tangent
|
|
///
|
|
/// \returns The Tangent of \f$\theta\f$ in the Range \f$\left[-\inf,\,\inf\right]\f$<br><br>
|
|
/// \details The Standard Trigonometric Tangent <br><br>
|
|
///
|
|
/// \tparam genType floating point type
|
|
/// \param x The Angle \f$\theta\f$ in \f$radians\f$
|
|
template<typename genType>
|
|
constexpr genType tan(genType x)
|
|
{ return std::tan(x); }
|
|
|
|
template<typename genType, size_t...i>
|
|
constexpr vector<genType, i...> tan(const vector<genType, i...>& x)
|
|
{ return vector<genType, i...>(fennec::tan(x[i]) ...); }
|
|
|
|
/// @}
|
|
|
|
|
|
|
|
/// \name Inverse Trigonometric Functions
|
|
/// @{
|
|
|
|
///
|
|
/// \brief Arc Sine. Returns an angle \f$\theta\f$ whose sine is /a x.
|
|
///
|
|
/// \returns an angle \f$\theta\f$ whose sine is /a x. <br><br>
|
|
/// \details Arc Sine. The range of values returned by this functions is
|
|
/// \f$\left[-\pi/2,\pi/2\right]\f$. Results are undefined if \f$\left|x\right|\,>\,1\f$. <br><br>
|
|
///
|
|
/// \tparam genType floating point type
|
|
/// \param x The Sine Value produced by \f$\theta\f$
|
|
template<typename genType>
|
|
constexpr genType asin(genType x)
|
|
{ return std::asin(x); }
|
|
|
|
template<typename genType, size_t...i>
|
|
constexpr vector<genType, i...> asin(const vector<genType, i...>& x)
|
|
{ return vector<genType, i...>(fennec::asin(x[i]) ...); }
|
|
|
|
|
|
///
|
|
/// \brief Arc Cosine. Returns an angle \f$\theta\f$ whose cosine is /a x.
|
|
///
|
|
/// \returns an angle \f$\theta\f$ whose cosine is /a x.
|
|
/// \details Arc Cosine. The range of values returned by this functions is
|
|
/// \f$\left[0,\pi\right]\f$. Results are undefined if \f$\left|x\right|\,>\,1\f$.
|
|
///
|
|
/// \tparam genType floating point type
|
|
/// \param x The Cosine Value produced by \f$\theta\f$
|
|
template<typename genType>
|
|
constexpr genType acos(genType x)
|
|
{ return std::acos(x); }
|
|
|
|
template<typename genType, size_t...i>
|
|
constexpr vector<genType, i...> acos(const vector<genType, i...>& x)
|
|
{ return vector<genType, i...>(fennec::acos(x[i]) ...); }
|
|
|
|
|
|
///
|
|
/// \brief Arc Tangent. Returns an angle \f$\theta\f$ whose tangent is /a y_over_x.
|
|
///
|
|
/// \returns an angle \f$\theta\f$ whose tangent is /a y_over_x.
|
|
/// \details Arc Tangent. The range of values returned by this functions is
|
|
/// \f$\left[\frac{-\pi}{2},\frac{\pi}{2}\right]\f$. Results are undefined if \f$\left|x\right|\,>\,1\f$.
|
|
///
|
|
/// \tparam genType floating point type
|
|
/// \param y_over_x The Cosine Value produced by \f$\theta\f$
|
|
template<typename genType>
|
|
constexpr genType atan(genType y_over_x)
|
|
{ return std::atan(y_over_x); }
|
|
|
|
template<typename genType, size_t...i>
|
|
constexpr vector<genType, i...> atan(const vector<genType, i...>& y_over_x)
|
|
{ return vector<genType, i...>(fennec::atan(y_over_x[i]) ...); }
|
|
|
|
|
|
///
|
|
/// \brief Arc Tangent. Returns an angle whose tangent is \f$\frac{y}{x}\f$.
|
|
///
|
|
/// \returns an angle whose tangent is \f$\frac{y}{x}\f$. <br><br>
|
|
/// \details Arc Tangent. The signs of \a x and \a y are used to determine what quadrant the angle is in.
|
|
/// The range of values returned by this functions is \f$\left[-\pi,\pi\right]\f$ <br><br>
|
|
///
|
|
/// \tparam genType floating point type
|
|
/// \param y The Sine Value produced by \f$\theta\f$
|
|
/// \param x The Cosine Value produced by \f$\theta\f$
|
|
template<typename genType>
|
|
constexpr genType atan(genType y, genType x)
|
|
{ return std::atan2(y, x); }
|
|
|
|
template<typename genType, size_t...i>
|
|
constexpr vector<genType, i...> atan(const vector<genType, i...>& y, const vector<genType, i...>& x)
|
|
{ return vector<genType, i...>(fennec::atan(y[i], x[i]) ...); }
|
|
/// @}
|
|
|
|
|
|
|
|
// Hyperbolic Functions ================================================================================================
|
|
|
|
/// \name Hyperbolic Functions
|
|
/// @{
|
|
|
|
///
|
|
/// \brief Returns the Hyperbolic Sine Function, \f$\frac{{e}^{x}-{e}^{-x}}{2}\f$
|
|
///
|
|
/// \returns The Hyperbolic Sine of \f$x\f$, \f$\frac{{e}^{x}-{e}^{-x}}{2}\f$ <br><br>
|
|
///
|
|
/// \tparam genType floating point type
|
|
/// \param x The Hyperbolic Angle \f$\alpha\f$
|
|
template<typename genType>
|
|
constexpr genType sinh(genType x)
|
|
{ return std::sinh(x); }
|
|
|
|
template<typename genType, size_t...i>
|
|
constexpr vector<genType, i...> sinh(const vector<genType, i...>& x)
|
|
{ return vector<genType, i...>(fennec::sinh(x[i]) ...); }
|
|
|
|
|
|
///
|
|
/// \brief Returns the Hyperbolic Cosine Function, \f$\frac{{e}^{x}+{e}^{-x}}{2}\f$
|
|
///
|
|
/// \returns The Hyperbolic Cosine of \f$x\f$, \f$\frac{{e}^{x}+{e}^{-x}}{2}\f$ <br><br>
|
|
///
|
|
/// \param x The Hyperbolic Angle \f$\alpha\f$
|
|
template<typename genType>
|
|
constexpr genType cosh(genType x)
|
|
{ return std::cosh(x); }
|
|
|
|
template<typename genType, size_t...i>
|
|
constexpr vector<genType, i...> cosh(const vector<genType, i...>& x)
|
|
{ return vector<genType, i...>(fennec::cosh(x[i]) ...); }
|
|
|
|
|
|
///
|
|
/// \brief Returns the Hyperbolic Tangent Function, \f$\frac{\text{sinh}(x)}{\text{cosh}(x)}\f$
|
|
///
|
|
/// \returns The Hyperbolic Tangent of \f$x\f$, \f$\frac{{e}^{x}+{e}^{-x}}{2}\f$ <br><br>
|
|
///
|
|
/// \param x The Hyperbolic Angle \f$\alpha\f$
|
|
template<typename genType, size_t...i>
|
|
constexpr genType tanh(genType x)
|
|
{ return std::tanh(x); }
|
|
|
|
template<typename genType, size_t...i>
|
|
constexpr vector<genType, i...> tanh(const vector<genType, i...>& x)
|
|
{ return vector<genType, i...>(fennec::tanh(x[i]) ...); }
|
|
/// @}
|
|
|
|
|
|
|
|
/// \name Inverse Hyperbolic Functions
|
|
/// @{
|
|
|
|
///
|
|
/// \brief The Inverse Hyperbolic Sine Function
|
|
///
|
|
/// \returns the value \f$y\f$ that fulfills \f$x=\text{sinh}(y)\f$ <br><br>
|
|
/// \details The Inverse Hyperbolic Sine Function <br><br>
|
|
///
|
|
/// \param x the hyperbolic angle \f$\alpha\f$
|
|
template<typename genType, size_t...i>
|
|
constexpr genType asinh(genType x)
|
|
{ return std::asinh(x); }
|
|
|
|
template<typename genType, size_t...i>
|
|
constexpr vector<genType, i...> asinh(const vector<genType, i...>& x)
|
|
{ return vector<genType, i...>(fennec::asinh(x[i]) ...); }
|
|
|
|
|
|
///
|
|
/// \brief The Inverse Hyperbolic Cosine Function
|
|
///
|
|
/// \returns the value \f$y\f$ that fulfills \f$x=\text{cosh}(y)\f$ <br><br>
|
|
/// \details The Inverse Hyperbolic Cosine Function <br><br>
|
|
///
|
|
/// \param x the hyperbolic angle \f$\alpha\f$
|
|
template<typename genType, size_t...i>
|
|
constexpr genType acosh(genType x)
|
|
{ return std::acosh(x); }
|
|
|
|
template<typename genType, size_t...i>
|
|
constexpr vector<genType, i...> acosh(const vector<genType, i...>& x)
|
|
{ return vector<genType, i...>(fennec::acosh(x[i]) ...); }
|
|
|
|
|
|
///
|
|
/// \brief The Inverse Hyperbolic Tangent Function
|
|
///
|
|
/// \returns the value \f$y\f$ that fulfills \f$x=\text{atanh}(y)\f$ <br><br>
|
|
/// \details The Inverse Hyperbolic Tangent Function <br><br>
|
|
///
|
|
/// \param x The Hyperbolic Angle \f$\alpha\f$
|
|
template<typename genType, size_t...i>
|
|
constexpr genType atanh(genType x)
|
|
{ return std::atanh(x); }
|
|
|
|
template<typename genType, size_t...i>
|
|
constexpr vector<genType, i...> atanh(const vector<genType, i...>& x)
|
|
{ return vector<genType, i...>(fennec::atanh(x[i]) ...); }
|
|
|
|
/// @}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // FENNEC_MATH_TRIGONOMETRIC_H
|