Files
fennec/include/fennec/math/geometric.h
Medusa Slockbower 6f09c3f7fe - Bug fixing for RTTI
- Fixes for declval + separated into own file
 - is_iterable
 - fixes for doxygen generation
2025-11-29 23:43:18 -05:00

291 lines
12 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 geometric.h
/// \brief \ref fennec_math_geometric
///
///
/// \details
/// \author Medusa Slockbower
///
/// \copyright Copyright © 2025 Medusa Slockbower ([GPLv3](https://www.gnu.org/licenses/gpl-3.0.en.html))
///
///
#ifndef FENNEC_MATH_GEOMETRIC_H
#define FENNEC_MATH_GEOMETRIC_H
///
///
///
/// \page fennec_math_geometric Geometric
///
/// \brief The Geometric Functions defined in the [OpenGL 4.6 Shading Language Specification](https://registry.khronos.org/OpenGL/specs/gl/GLSLangSpec.4.60.pdf).
///
/// \code #include <fennec/math/geometric.h> \endcode
///
///
/// \section fennec_math_geometric_section_functions Geometric Functions
///
/// <table width="100%" class="fieldtable" id="table_fennec_math_geometric_functions">
/// <tr><th style="vertical-align: top">Syntax
/// <th style="vertical-align: top">Description
/// <tr><td width="50%" style="vertical-align: top"> <br>
/// \ref fennec::dot "float dot(genFType x, genFType y)" <br>
/// \ref fennec::dot "double dot(genDType x, genDType x)"
/// <td width="50%" style="vertical-align: top">
/// \copydoc fennec::dot
///
/// <tr><td width="50%" style="vertical-align: top" class="odd_c"> <br>
/// \ref fennec::length2 "float length2(genFType x)" <br>
/// \ref fennec::length2 "double length2(genDType x)"
/// <td width="50%" style="vertical-align: top" class="odd_c">
/// \copydoc fennec::length2
///
/// <tr><td width="50%" style="vertical-align: top"> <br>
/// \ref fennec::length "float length(genFType x)" <br>
/// \ref fennec::length "double length(genDType x)"
/// <td width="50%" style="vertical-align: top">
/// \copydoc fennec::length
///
/// <tr><td width="50%" style="vertical-align: top" class="odd_c"> <br>
/// \ref fennec::distance "float distance(genFType x, genFType y)" <br>
/// \ref fennec::distance "double distance(genDType x, genDType x)"
/// <td width="50%" style="vertical-align: top" class="odd_c">
/// \copydoc fennec::distance
///
/// <tr><td width="50%" style="vertical-align: top"> <br>
/// \ref fennec::normalize "float normalize(genFType x)" <br>
/// \ref fennec::normalize "double normalize(genDType x)"
/// <td width="50%" style="vertical-align: top">
/// \copydoc fennec::normalize
///
/// <tr><td width="50%" style="vertical-align: top" class="odd_c"> <br>
/// \ref fennec::cross "vec3 cross(vec3 x, vec3 y)" <br>
/// \ref fennec::cross "dvec3 cross(dvec3 x, dvec3 x)"
/// <td width="50%" style="vertical-align: top" class="odd_c">
/// \copydoc fennec::cross
///
/// <tr><td width="50%" style="vertical-align: top"> <br>
/// \ref fennec::faceforward "genFType faceforward(genFType N, genFType I, genFType Nref)" <br>
/// \ref fennec::faceforward "genDType faceforward(genDType N, genDType I, genDType Nref)"
/// <td width="50%" style="vertical-align: top">
/// \copydoc fennec::faceforward
///
/// <tr><td width="50%" style="vertical-align: top" class="odd_c"> <br>
/// \ref fennec::reflect "genFType reflect(genFType I, genFType N)" <br>
/// \ref fennec::reflect "genDType reflect(genDType I, genDType N)"
/// <td width="50%" style="vertical-align: top" class="odd_c">
/// \copydoc fennec::reflect
///
/// <tr><td width="50%" style="vertical-align: top"> <br>
/// \ref fennec::refract "genFType refract(genFType N, genFType I, float eta)" <br>
/// \ref fennec::refract "genDType refract(genDType N, genDType I, double eta)"
/// <td width="50%" style="vertical-align: top">
/// \copydoc fennec::refract
///
/// </table>
///
///
///
#include <fennec/math/vector.h>
#include <fennec/math/common.h>
#include <fennec/math/exponential.h>
namespace fennec
{
// dot -----------------------------------------------------------------------------------------------------------------
///
/// \brief Returns the dot product of \f$x\f$ and \f$y\f$, i.e., \f$x_0 \cdot y_0 + x_1 \cdot y_1 + \ldots\f$
///
/// \returns the dot product of \f$x\f$ and \f$y\f$, i.e., \f$x_0 \cdot y_0 + x_0 \cdot y_0 + \ldots\f$ <br><br>
/// \details we can represent this in linear algebra as the following, <br><br>
/// let \f$X=\left[\begin{array}\\ x_0 \\ x_1 \\ \vdots \\ x_N \end{array}\right]\f$ <br>
/// let \f$Y=\left[\begin{array}\\ y_0 \\ y_1 \\ \vdots \\ y_N \end{array}\right]\f$ <br><br>
///
/// then \f$\text{dot}(X, Y)=X \cdot Y^T\f$ <br><br>
///
/// \param x first vector
/// \param y second vector
template<typename genType, size_t...i>
constexpr genType dot(const vector<genType, i...>& x, const vector<genType, i...>& y) {
return ((x[i] * y[i]) + ...);
}
// length2 -------------------------------------------------------------------------------------------------------------
///
/// \brief Returns the squared length of vector \f$x\f$, i.e., \f$x_0^2 + x_1^2 + \ldots\f$
///
/// \returns the squared length of vector \f$x\f$, i.e., \f$x_0^2 + x_1^2 + \ldots\f$ <br><br>
/// \details we can represent this in linear algebra as the following, <br><br>
/// let \f$X=\left[\begin{array}\\ x_0 \\ x_1 \\ \vdots \\ x_N \end{array}\right]\f$ <br><br>
///
/// then \f$\text{length2}(X)=X \cdot X^T\f$ <br><br>
///
/// \param x the vector
template<typename genType, size_t...i>
constexpr genType length2(const vector<genType, i...>& x) {
return fennec::dot(x, x);
}
// length --------------------------------------------------------------------------------------------------------------
///
/// \brief Returns the length of vector \f$x\f$, i.e., \f$\sqrt{x_0^2 + x_1^2 + \ldots}\f$
///
/// \returns the length of vector \f$x\f$, i.e., \f$\sqrt{x_0^2 + x_1^2 + \ldots}\f$<br><br>
/// \details we can represent this in linear algebra as the following, <br><br>
/// let \f$X=\left[\begin{array}\\ x_0 \\ x_1 \\ \vdots \\ x_N \end{array}\right]\f$ <br><br>
///
/// then, \f$\text{length}(X)=\left|\left|X\right|\right|\f$ <br><br>
///
/// \param x the vector
template<typename genType, size_t...i>
constexpr genType length(const vector<genType, i...>& x) {
return fennec::sqrt(fennec::length2(x));
}
// distance ------------------------------------------------------------------------------------------------------------
///
/// \brief Returns the length of vector \f$x\f$, i.e., \f$\sqrt{x_0^2 + x_1^2 + \ldots}\f$
///
/// \returns the distance between \f$p_0\f$ and \f$p_1\f$, i.e., \f$\left|{p_1-p_0}\right|\f$
/// \details we can represent this in linear algebra as the following, <br><br>
/// let \f$X=\left[\begin{array}\\ x_0 \\ x_1 \\ \vdots \\ x_N \end{array}\right]\f$ <br>
/// let \f$Y=\left[\begin{array}\\ y_0 \\ y_1 \\ \vdots \\ y_N \end{array}\right]\f$ <br><br>
///
/// then \f$\text{distance}(X, Y)=\left|\left|Y-X\right|\right|\f$ <br><br>
///
/// \param p0 first vector
/// \param p1 second vector
template<typename genType, size_t...i>
constexpr genType distance(const vector<genType, i...>& p0, const vector<genType, i...>& p1) {
return fennec::length(p1 - p0);
}
// cross ---------------------------------------------------------------------------------------------------------------
///
/// \brief Returns the cross product of \f$x\f$ and \f$y\f$, i.e.,
/// \f$\left({x_1 \cdot y_2 - y_1 \cdot x_2, x_2 \cdot y_0 - y_2 \cdot x_0, x_0 \cdot y_1 - y_0 \cdot x_1}\right)\f$
///
/// \returns the cross product of \f$x\f$ and \f$y\f$, i.e.,
/// \f$\left({x_1 \cdot y_2 - y_1 \cdot x_2, x_2 \cdot y_0 - y_2 \cdot x_0, x_0 \cdot y_1 - y_0 \cdot x_1}\right)\f$ <br><br>
/// \details we can represent this in linear algebra as the following, <br><br>
/// let \f$X=\left[\begin{array}\\ x_0 \\ x_1 \\ \vdots \\ x_N \end{array}\right]\f$ <br>
/// let \f$Y=\left[\begin{array}\\ y_0 \\ y_1 \\ \vdots \\ y_N \end{array}\right]\f$ <br><br>
///
/// then \f$\text{cross}(X, Y)=X \times Y\f$ <br><br>
///
/// \param x first vector
/// \param y second vector
template<typename genType, size_t...i> requires(sizeof...(i) == 3)
constexpr vector<genType, i...> cross(const vector<genType, i...>& x, const vector<genType, i...>& y) {
return vector<genType, i...>(x[1]*y[2]-y[1]*x[2], x[2]*y[0]-y[2]*x[0], x[0]*y[1]-y[0]*x[1]);
}
// normalize -----------------------------------------------------------------------------------------------------------
///
/// \brief Returns a vector in the same direction as \f$x\f$, but with a length of \f$1\f$, i.e.
///
/// \returns a vector in the same direction as \f$x\f$, but with a length of \f$1\f$, i.e.\f$\frac{x}{||x||}\f$<br><br>
/// \details we can represent this in linear algebra as the following, <br><br>
/// let \f$X=\left[\begin{array}\\ x_0 \\ x_1 \\ \vdots \\ x_N \end{array}\right]\f$ <br><br>
///
/// then, \f$\text{length}(X)=\frac{X}{\left|\left|X\right|\right|}\f$ <br><br>
///
/// \param x
template<typename genType, size_t...i>
constexpr vector<genType, i...> normalize(const vector<genType, i...>& x) {
return x * fennec::inversesqrt(fennec::dot(x, x));
}
// faceforward ---------------------------------------------------------------------------------------------------------
///
/// \brief If \f$\text{dot}(Nref, I)<0\f$ return \f$N\f$, otherwise return \f$-N\f$.
///
/// \returns \f$N\f$ if \f$\text{dot}(Nref,I)<0\f$, otherwise, returns \f$-N\f$.<br><br>
///
/// \param N the vector
/// \param I the incident
/// \param Nref the reference
template<typename genType, size_t...i>
constexpr vector<genType, i...> faceforward(const vector<genType, i...>& N, const vector<genType, i...>& I, const vector<genType, i...>& Nref) {
return fennec::sign(fennec::dot(Nref, I)) * N;
}
// reflect -------------------------------------------------------------------------------------------------------------
///
/// \brief For the incident vector \f$I\f$ and surface orientation \f$N\f$, returns the reflection direction.
///
/// \returns The reflection direction, given the incident vector \f$I\f$ and surface orientation \f$N\f$ <br><br>
/// \details We can express this as, <br><br>
/// \f$\text{reflect}(I, N) = I - 2 N \cdot \text{dot}(N, I)\f$ <br><br>
///
/// \param I the incident
/// \param N the surface orientation
template<typename genType, size_t...i>
constexpr vector<genType, i...> reflect(const vector<genType, i...>& I, const vector<genType, i...>& N) {
return I - genType(2.0) * fennec::dot(N, I) * N;
}
// refract -------------------------------------------------------------------------------------------------------------
///
/// \brief For the incident vector \f$I\f$ and surface normal \f$N\f$, and the ratio of indices of refraction \f$eta\f$,
/// return the refraction vector.
///
/// \returns The refraction vector, given the incident vector \f$I\f$, surface normal \f$N\f$, and ratio \f$eta\f$.<br><br>
/// \details The result is computed by the refraction equation, <br><br>
/// let \f$k=1.0-eta^2 \cdot (1.0 - \text{dot}(N, I)^2)\f$ <br>
/// then, \f$\text{refract}(I, N, eta)=\begin{cases} 0.0 & k<0.0, \\ eta \cdot I - N \cdot (eta \cdot \text{dot}(N, I) + \sqrt{k}) \end{cases}\f$ <br><br>
///
/// \param I the incident
/// \param N the surface normal
/// \param eta the ratio of indices of refraction
template<typename genType, size_t...i>
constexpr vector<genType, i...> refract(const vector<genType, i...>& I, const vector<genType, i...>& N, genType eta) {
genType ndi = fennec::dot(N, I);
genType k = genType(1.0) - eta * eta * (genType(1.0) - ndi * ndi);
if (k < 0) return vector<genType, i...>(0);
return eta * I - N * (eta * ndi + fennec::sqrt(k));
}
}
#endif // FENNEC_MATH_GEOMETRIC_H