Files
fennec/include/fennec/math/geometric.h
Medusa Slockbower cf909624df - More Documentation
- Vulkan Configuration Implementations
 - Fixed build errors on GCC and Clang
2026-07-18 23:48:00 -04:00

291 lines
12 KiB
C++

// =====================================================================================================================
// fennec, a free and open source game engine
// Copyright © 2025 - 2026 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 fennec/math/geometric.h
/// \brief \ref fennec_math_geometric
///
///
/// \details
/// \author Medusa Slockbower
///
/// \copyright Copyright © 2025 - 2026 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 \math{x} and \math{y}, i.e., \math{x_0 \cdot y_0 + x_1 \cdot y_1 + \ldots}
///
/// \returns the dot product of \math{x} and \math{y}, i.e., \math{x_0 \cdot y_0 + x_0 \cdot y_0 + \ldots} <br><br>
/// \details we can represent this in linear algebra as the following, <br><br>
/// let \math{X=\left[\begin{array}\\ x_0 \\ x_1 \\ \vdots \\ x_N \end{array}\right]} <br>
/// let \math{Y=\left[\begin{array}\\ y_0 \\ y_1 \\ \vdots \\ y_N \end{array}\right]} <br><br>
///
/// then \math{\text{dot}(X, Y)=X \cdot Y^T} <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 \math{x}, i.e., \math{x_0^2 + x_1^2 + \ldots}
///
/// \returns the squared length of vector \math{x}, i.e., \math{x_0^2 + x_1^2 + \ldots} <br><br>
/// \details we can represent this in linear algebra as the following, <br><br>
/// let \math{X=\left[\begin{array}\\ x_0 \\ x_1 \\ \vdots \\ x_N \end{array}\right]} <br><br>
///
/// then \math{\text{length2}(X)=X \cdot X^T} <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 \math{x}, i.e., \math{\sqrt{x_0^2 + x_1^2 + \ldots}}
///
/// \returns the length of vector \math{x}, i.e., \math{\sqrt{x_0^2 + x_1^2 + \ldots}}<br><br>
/// \details we can represent this in linear algebra as the following, <br><br>
/// let \math{X=\left[\begin{array}\\ x_0 \\ x_1 \\ \vdots \\ x_N \end{array}\right]} <br><br>
///
/// then, \math{\text{length}(X)=\left|\left|X\right|\right|} <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 \math{x}, i.e., \math{\sqrt{x_0^2 + x_1^2 + \ldots}}
///
/// \returns the distance between \math{p_0} and \math{p_1}, i.e., \math{\left|{p_1-p_0}\right|}
/// \details we can represent this in linear algebra as the following, <br><br>
/// let \math{X=\left[\begin{array}\\ x_0 \\ x_1 \\ \vdots \\ x_N \end{array}\right]} <br>
/// let \math{Y=\left[\begin{array}\\ y_0 \\ y_1 \\ \vdots \\ y_N \end{array}\right]} <br><br>
///
/// then \math{\text{distance}(X, Y)=\left|\left|Y-X\right|\right|} <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 \math{x} and \math{y}, i.e.,
/// \math{\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)}
///
/// \returns the cross product of \math{x} and \math{y}, i.e.,
/// \math{\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)} <br><br>
/// \details we can represent this in linear algebra as the following, <br><br>
/// let \math{X=\left[\begin{array}\\ x_0 \\ x_1 \\ \vdots \\ x_N \end{array}\right]} <br>
/// let \math{Y=\left[\begin{array}\\ y_0 \\ y_1 \\ \vdots \\ y_N \end{array}\right]} <br><br>
///
/// then \math{\text{cross}(X, Y)=X \times Y} <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 \math{x}, but with a length of \math{1}, i.e.
///
/// \returns a vector in the same direction as \math{x}, but with a length of \math{1}, i.e.\math{\frac{x}{||x||}}<br><br>
/// \details we can represent this in linear algebra as the following, <br><br>
/// let \math{X=\left[\begin{array}\\ x_0 \\ x_1 \\ \vdots \\ x_N \end{array}\right]} <br><br>
///
/// then, \math{\text{length}(X)=\frac{X}{\left|\left|X\right|\right|}} <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 \math{\text{dot}(Nref, I)<0} return \math{N}, otherwise return \math{-N}.
///
/// \returns \math{N} if \math{\text{dot}(Nref,I)<0}, otherwise, returns \math{-N}.<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 \math{I} and surface orientation \math{N}, returns the reflection direction.
///
/// \returns The reflection direction, given the incident vector \math{I} and surface orientation \math{N} <br><br>
/// \details We can express this as, <br><br>
/// \math{\text{reflect}(I, N) = I - 2 N \cdot \text{dot}(N, I)} <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 \math{I} and surface normal \math{N}, and the ratio of indices of refraction \math{eta},
/// return the refraction vector.
///
/// \returns The refraction vector, given the incident vector \math{I}, surface normal \math{N}, and ratio \math{eta}.<br><br>
/// \details The result is computed by the refraction equation, <br><br>
/// let \math{k=1.0-eta^2 \cdot (1.0 - \text{dot}(N, I)^2)} <br>
/// then, \math{\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}} <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