Files
fennec/include/fennec/math/trigonometric.h

456 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 page_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 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(genType) "genFType degrees(genFType)"
/// <td width="50%" style="vertical-align: top">
/// \copydoc fennec::degrees(fennec::genType)
///
/// <tr><td width="50%" style="vertical-align: top"> <br>
/// \ref fennec::radians(genType) "genFType radians(genFType)"
/// <td width="50%" style="vertical-align: top">
/// \copydoc fennec::radians(fennec::genType)
///
/// </table>
///
///
///
/// \section section_fennec_trigonometric_functions Trigonometry
///
/// <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(genType) "genFType sin(genFType)"
/// <td width="50%" style="vertical-align: top">
/// \copydoc fennec::sin(fennec::genType)
///
/// <tr><td width="50%" style="vertical-align: top"> <br>
/// \ref fennec::cos(genType) "genFType cos(genFType)"
/// <td width="50%" style="vertical-align: top">
/// \copydoc fennec::cos(fennec::genType)
///
/// <tr><td width="50%" style="vertical-align: top"> <br>
/// \ref fennec::tan(genType) "genFType tan(genFType)"
/// <td width="50%" style="vertical-align: top">
/// \copydoc fennec::tan(fennec::genType)
///
///
/// <tr><td width="50%" style="vertical-align: top"> <br>
/// \ref fennec::asin(genType) "genFType asin(genFType)"
/// <td width="50%" style="vertical-align: top">
/// \copydoc fennec::asin(fennec::genType)
///
/// <tr><td width="50%" style="vertical-align: top"> <br>
/// \ref fennec::acos(genType) "genFType acos(genFType)"
/// <td width="50%" style="vertical-align: top">
/// \copydoc fennec::acos(fennec::genType)
///
/// <tr><td width="50%" style="vertical-align: top"> <br>
/// \ref fennec::atan(genType) "genFType atan(genFType)"
/// <td width="50%" style="vertical-align: top">
/// \copydoc fennec::atan(fennec::genType)
///
/// </table>
///
///
///
/// \section section_fennec_hyperbolic_functions Trigonometry
///
/// <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(genType) "genFType sinh(genFType)"
/// <td width="50%" style="vertical-align: top">
/// \copydoc fennec::sinh(fennec::genType)
///
/// <tr><td width="50%" style="vertical-align: top"> <br>
/// \ref fennec::cosh(genType) "genFType cosh(genFType)"
/// <td width="50%" style="vertical-align: top">
/// \copydoc fennec::cosh(fennec::genType)
///
/// <tr><td width="50%" style="vertical-align: top"> <br>
/// \ref fennec::tanh(genType) "genFType tanh(genFType)"
/// <td width="50%" style="vertical-align: top">
/// \copydoc fennec::tanh(fennec::genType)
///
///
/// <tr><td width="50%" style="vertical-align: top"> <br>
/// \ref fennec::asinh(genFType) "genFType asinh(genFType)"
/// <td width="50%" style="vertical-align: top">
/// \copydoc fennec::asinh(fennec::genType)
///
/// <tr><td width="50%" style="vertical-align: top"> <br>
/// \ref fennec::acosh(genFType) "genFType acosh(genFType)"
/// <td width="50%" style="vertical-align: top">
/// \copydoc fennec::acosh(fennec::genType)
///
/// <tr><td width="50%" style="vertical-align: top"> <br>
/// \ref fennec::atanh(genFType) "genFType atanh(genFType)"
/// <td width="50%" style="vertical-align: top">
/// \copydoc fennec::atanh(fennec::genType)
///
/// </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
/// @{
///
/// \fn fennec::sin(fennec::genType)
/// \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]) ...); }
///
/// \fn fennec::cos(fennec::genType)
/// \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]) ...); }
///
/// \fn fennec::tan(fennec::genType)
/// \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
/// @{
///
/// \fn fennec::asin(fennec::genType)
/// \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]) ...); }
///
/// \fn fennec::acos(fennec::genType)
/// \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]) ...); }
///
/// \fn fennec::atan(fennec::genType)
/// \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]) ...); }
///
/// \fn fennec::tan(fennec::genType, fennec::genType)
/// \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
/// @{
///
/// \fn fennec::sinh(fennec::genType)
/// \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]) ...); }
///
/// \fn fennec::cosh(fennec::genType)
/// \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]) ...); }
///
/// \fn fennec::tanh(fennec::genType)
/// \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
/// @{
///
/// \fn fennec::asinh(fennec::genType)
/// \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]) ...); }
///
/// \fn fennec::cosh(fennec::genType)
/// \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]) ...); }
///
/// \fn fennec::atan(fennec::genType)
/// \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