20#include <glm/common.hpp>
21#include <glm/exponential.hpp>
22#include <glm/geometric.hpp>
23#include <glm/gtc/constants.hpp>
24#include <glm/gtc/matrix_inverse.hpp>
25#include <glm/gtc/matrix_transform.hpp>
26#include <glm/trigonometric.hpp>
28#include <glm/vector_relational.hpp>
30#define GLM_ENABLE_EXPERIMENTAL
31#include <glm/ext/matrix_clip_space.hpp>
32#include <glm/gtc/matrix_transform.hpp>
33#include <glm/gtc/type_ptr.hpp>
34#include <glm/gtx/matrix_decompose.hpp>
35#include <glm/gtx/matrix_major_storage.hpp>
36#include <glm/gtx/matrix_transform_2d.hpp>
37#include <glm/gtx/norm.hpp>
38#include <glm/gtx/normal.hpp>
40#include <glm/gtx/rotate_vector.hpp>
41#include <glm/gtx/string_cast.hpp>
47#define IMPORT_FROM_GLM(Symbol) \
48 namespace CeresEngine::inline Math { \
140#undef IMPORT_FROM_GLM
144 static constexpr double PI = 3.14159265358979323846;
236 return t *
t * (3.0 - 2.0 *
t);
254 template<
typename T, glm::length_t D>
257 for(glm::length_t
i = 0;
i <
D;
i++) {
278 template<
typename T>
inline T lerp(
T t,
T min,
T max) {
return (1.0 -
t) * min +
t * max; }
283 template<
typename T>
inline T invLerp(
T val,
T min,
T max) {
return clamp((
val - min) / std::max(max - min, 0.0001),
T(0),
T(1)); }
286 template<
typename A,
typename B>
inline std::common_type_t<A, B>
gcd(
const A& a,
const B& b) {
return (b == 0) ? a :
gcd(b, a % b); }
289 template<
typename A,
typename B>
inline std::common_type_t<A, B>
lcm(
const A& a,
const B& b) {
return (a * b) /
gcd(a, b); }
358 static constexpr T THIRD = (1 /
T(3));
436 T p = -(3 /
T(8)) *
sqA +
B;
438 T r = -(3 /
T(256)) *
sqA *
sqA + (1 / (
T)16) *
sqA *
B - (1 / (
T)4) *
A *
C +
D;
505 const double t2 =
t *
t;
506 const double t3 =
t2 *
t;
508 double a = 2 *
t3 - 3 *
t2 + 1;
509 double b =
t3 - 2 *
t2 +
t;
510 double c = -2 *
t3 + 3 *
t2;
526 const double t2 =
t *
t;
528 double a = 6 *
t2 - 6 *
t;
529 double b = 3 *
t2 - 4 *
t + 1;
530 double c = -6 *
t2 + 6 *
t;
531 double d = 3 *
t2 - 2 *
t;
563 template<
class T,
typename L>
565 L length2 = length * length;
606 for(
int i = 1;
i <
order + 1; ++
i) {
607 h[
i] = (b - a) / pow(2,
i - 1);
612 for(
int i = 2;
i <
order + 1; ++
i) {
614 for(
int k = 1;
k <= pow(2,
i - 2); ++
k) {
618 r[
i][1] = 0.5 * (r[
i - 1][1] +
h[
i - 1] *
coeff);
621 for(
int i = 2;
i <
order + 1; ++
i) {
622 for(
int j = 2;
j <=
i; ++
j) {
623 r[
i][
j] = r[
i][
j - 1] + (r[
i][
j - 1] - r[
i - 1][
j - 1]) / (pow(4,
j - 1) - 1);
639 const T radius =
half * (b - a);
640 const T center =
half * (b + a);
661 output += (index % base) *
frac;
672 using std::numeric_limits;
676 if(
len <= std::numeric_limits<T>::epsilon())
683 using std::numeric_limits;
687 if(
len <= std::numeric_limits<T>::epsilon())
706 template<
typename T,
typename TI,
typename TS>
714 const T half =
static_cast<T>(0.5);
727 return ortho(left, right, bottom, top,
T{-1},
T{1});
747 return glm::rowMajor4(glm::make_mat4(data));
754 const T data[9] = {
c, -
s,
T{0},
s,
c,
T{0},
T{0},
T{0},
T{1}};
756 return glm::rowMajor3(glm::make_mat3(data));
763 template<
typename VecT,
typename T> VecT
bezierImpl(
const VecT* p,
const int n, T t1, T t2,
int stride = 1) {
767 return t1 * p[0] + t2 * p[stride];
768 return t1 *
bezierImpl(p, n - 1, t1, t2, stride) + t2 *
bezierImpl(p + stride, n - 1, t1, t2, stride);
774 static_assert(
D > 0,
"At least one control point needed.");
775 return detail::bezierImpl(&p[0],
D,
static_cast<T>(1) -
t,
t);
779 static_assert(
D > 0,
"At least one control point needed.");
780 return detail::bezierImpl(&p[0],
D,
static_cast<T>(1) -
t,
t);
784 static_assert(
D0 > 0,
"At least one control point needed.");
785 static_assert(
D1 > 0,
"At least one control point needed.");
788 for(
int i = 0;
i <
D1; ++
i) {
797 static VecT
calc(
const VecT (&p)[D], T t) {
799 for(
int i = 0; i < D - 1; ++i) {
800 temp[i] =
static_cast<T
>(D - 1) * (p[i + 1] - p[i]);
807 static VecT
calc(
const VecT (&p)[D], T t) {
return bezier(p, t); }
811 static VecT
calc(
const VecT (&p)[1], T t) {
return bezier(p, t); }
815 static VecT
calc(
const VecT (&)[1], T) {
return VecT{
static_cast<T
>(0)}; }
821 static_assert(
O > 0,
"The derivative order must be at least one.");
822 static_assert(
D > 0,
"At least one control point needed.");
827 static_assert(
O > 0,
"The derivative order must be at least one.");
828 static_assert(
D > 0,
"At least one control point needed.");
833 static_assert(
O > 0,
"Order of the Jacobian must be at least one.");
834 static_assert(
D0 > 0,
"At least one control point needed.");
835 static_assert(
D1 > 0,
"At least one control point needed.");
838 for(
int i = 0;
i <
D0; ++
i) {
839 temp0[
i] = detail::bezierImpl(&p[0][
i],
D1,
static_cast<T>(1) -
t[1],
t[1],
D0);
843 for(
int i = 0;
i <
D1; ++
i) {
856#define IMPORT_GLM_CONSTANT_NAMED(Symbol, Name) template<typename T> inline const T Name = glm::Symbol<T>()
857#define IMPORT_GLM_CONSTANT(Symbol) IMPORT_GLM_CONSTANT_NAMED(Symbol, Symbol)
#define CE_DISABLE_WARNING_DEPRECATED_DECLARATIONS
Definition Macros.hpp:498
#define CE_DISABLE_WARNING_DEPRECATED_VOLATILE
Definition Macros.hpp:497
#define CE_DISABLE_WARNING_POP
Definition Macros.hpp:485
#define CE_DISABLE_WARNING_PUSH
Definition Macros.hpp:484
#define IMPORT_GLM_CONSTANT_NAMED(Symbol, Name)
Definition Math.hpp:856
#define IMPORT_FROM_GLM(Symbol)
Definition Math.hpp:47
#define IMPORT_GLM_CONSTANT(Symbol)
Definition Math.hpp:857
Axis axis(const StringView &str) noexcept
Returns the Axis constant that is represented by str.
auto transform(Container &container, Transform &&transform)
Returns an iterable object that iterates over the values of the container and applies transform to ev...
Definition Iterator.hpp:436
constexpr size_t hash(const T &v)
Generates a hash for the provided type.
Definition Hash.hpp:25
VecT bezierImpl(const VecT *p, const int n, T t1, T t2, int stride=1)
Definition Math.hpp:763
T invLerp(T val, T min, T max)
Determines the position of a value between two other values.
Definition Math.hpp:283
bool approxEquals(T a, T b, T tolerance=std::numeric_limits< T >::epsilon())
Compare two doubles, using tolerance for inaccuracies.
Definition FloatingPoint.hpp:17
T quintic(T val)
Performs quintic interpolation where val is the value to map onto a quintic S-curve.
Definition Math.hpp:241
TVector2< T > bezier(const TVector2< T >(&p)[D], T t)
Definition Math.hpp:773
glm::qua< T, glm::highp > TQuaternion
A quaternion type that uses a internal representation of type T
Definition Quaternion.hpp:23
constexpr FixedPoint< T, P > frac(const FixedPoint< T, P > &x)
Returns the fractional portion of a fixed-point value.
Definition FixedPoint.hpp:417
auto cubic(T val1, T val2, T val3, T val4, U f)
Performs cubic interpolation between two values bound between two other values where f is the alpha v...
Definition Math.hpp:246
T rombergIntegration(T a, T b, int order, Integrand &&integrand)
Calculates the Romberg Integration.
Definition Math.hpp:602
int solveQuadratic(T A, T B, T C, T *roots)
Solves the quadratic equation with the parameters A, B, C.
Definition Math.hpp:321
int solveLinear(T A, T B, T *roots)
Solves the linear equation with the parameters A, B.
Definition Math.hpp:301
constexpr TMatrix4< T > infinitePerspective(T fovy, T aspect, T zNear) noexcept
Creates a matrix for a symmetric perspective-view frustum with far plane at infinite with default han...
Definition Math.hpp:194
TVector3< T > project(const TVector3< T > &v, const TMatrix4< T > &modelViewProj, const TVector2< TI > &viewportOrigin, const TVector2< TS > &viewportSize)
Definition Math.hpp:707
TQuaternion< T > qrotate(const T &angle, const TVector3< T > &axis)
Definition Math.hpp:699
int solveQuartic(T A, T B, T C, T D, T E, T *roots)
Solves the quartic equation with the parameters A, B, C, D, E.
Definition Math.hpp:428
TMatrix4< T > ortho2D(const T &left, const T &right, const T &bottom, const T &top)
Definition Math.hpp:726
constexpr FixedPoint< T, P > cos(const FixedPoint< T, P > &x)
Returns cosine of a fixed-point value.
Definition FixedPoint.hpp:588
glm::vec< D, T, glm::highp > TVector
A D-dimensional vector type that uses a internal representation of type T
Definition Vector.hpp:39
TVector2< T > bezierDerivative(const TVector2< T >(&p)[D], T t)
Definition Math.hpp:820
TMatrix4< T > constexpr flipProjectionY(TMatrix4< T > proj) noexcept
Definition Math.hpp:146
TVector2< T > cross(const TVector2< T > &v)
Definition Math.hpp:692
TVector3< T > slerp(const TVector3< T > &v1, const TVector3< T > &v2, const T &a)
Definition Math.hpp:730
T cubicHermiteD1(const double t, const T &pointA, const T &pointB, const T &tangentA, const T &tangentB)
Evaluates the first derivative of a cubic Hermite curve at a specific point.
Definition Math.hpp:525
TMatrix< 3, 3, T > TMatrix3
A 3x3 type that uses a internal representation of type T
Definition Matrix.hpp:41
int solveCubic(T A, T B, T C, T D, T *roots)
Solves the cubic equation with the parameters A, B, C, D.
Definition Math.hpp:357
TMatrix< 4, 4, T > TMatrix4
A 4x4 type that uses a internal representation of type T
Definition Matrix.hpp:54
constexpr FixedPoint< T, P > sqrt(const FixedPoint< T, P > &x)
Returns square root of a fixed-point value.
Definition FixedPoint.hpp:458
FixedPoint< B, F > sin(FixedPoint< B, F > x) noexcept
Definition FixedPoint.hpp:554
TMatrix< 2, 3, T > TMatrix2x3
A 2x3 type that uses a internal representation of type T
Definition Matrix.hpp:67
T lerp(T t, T min, T max)
Linearly interpolates between the two values using t.
Definition Math.hpp:278
T angle(const TVector2< T > &v1, const TVector2< T > &v2)
Definition Math.hpp:670
Array< T, 4 > cubicHermiteCoefficients(const T &pointA, const T &pointB, const T &tangentA, const T &tangentB)
Calculates coefficients needed for evaluating a cubic curve in Hermite form.
Definition Math.hpp:547
TVector< 4, T > TVector4
A four dimensional vector type that uses a internal representation of type T
Definition Vector.hpp:115
T gaussianQuadrature(T a, T b, T *roots, T *coefficients, Integrand &&integrand)
Calculates the Gaussian Quadrature.
Definition Math.hpp:637
constexpr TMatrix4< T > ortho(T left, T right, T bottom, T top, T zNear, T zFar)
Creates a matrix for projecting two-dimensional coordinates onto the screen.
Definition Math.hpp:227
double smoothStep(T val1, T val2, T t)
Performs smooth Hermite interpolation between values.
Definition Math.hpp:234
constexpr TMatrix4< T > perspective(T fovy, T aspect, T zNear, T zFar) noexcept
Creates a matrix for a symetric perspective-view frustum based on the default handedness and default ...
Definition Math.hpp:170
TMatrix2x3< T > bezier2Jacobian(const TVector3< T >(&p)[D1][D0], const TVector2< T > &t)
Definition Math.hpp:832
std::common_type_t< A, B > lcm(const A &a, const B &b)
Return the least common multiple between two values.
Definition Math.hpp:289
std::common_type_t< A, B > gcd(const A &a, const B &b)
Return the greater common divisor between two values.
Definition Math.hpp:286
constexpr TMatrix4< T > frustum(T left, T right, T bottom, T top, T zNear, T zFar) noexcept
Creates a frustum matrix with default handedness, using the default handedness and default near and f...
Definition Math.hpp:157
TVector3< T > normal(const TVector3< T > &p1, const TVector3< T > &p2, const TVector3< T > &p3)
Definition Math.hpp:704
TVector< 2, T > TVector2
A two dimensional vector type that uses a internal representation of type T
Definition Vector.hpp:45
TVector< 3, T > TVector3
A three dimensional vector type that uses a internal representation of type T
Definition Vector.hpp:80
TMatrix4< T > rotate(const TVector3< T > &angle)
Definition Math.hpp:737
TVector3< T > bezier2(const TVector3< T >(&p)[D1][D0], const TVector2< T > &t)
Definition Math.hpp:783
constexpr TMatrix4< T > tweakedInfinitePerspective(T fovy, T aspect, T zNear) noexcept
Creates a matrix for a symmetric perspective-view frustum with far plane at infinite for graphics har...
Definition Math.hpp:205
T cubicHermite(const double t, const T &pointA, const T &pointB, const T &tangentA, const T &tangentB)
Evaluates a cubic Hermite curve at a specific point.
Definition Math.hpp:504
constexpr FixedPoint< T, P > acos(FixedPoint< T, P > x)
Returns arccosine of a fixed-point value.
Definition FixedPoint.hpp:475
T haltonSequence(int index, int base)
Generates numbers in a deterministic sequence suitable for Monte Carlo algorithms.
Definition Math.hpp:656
constexpr TMatrix4< T > perspectiveFov(T fov, T width, T height, T zNear, T zFar) noexcept
Builds a perspective projection matrix based on a field of view and the default handedness and defaul...
Definition Math.hpp:183
static VecT calc(const VecT(&p)[1], T t)
Definition Math.hpp:811
static VecT calc(const VecT(&p)[D], T t)
Definition Math.hpp:807
static VecT calc(const VecT(&)[1], T)
Definition Math.hpp:815
static VecT calc(const VecT(&p)[D], T t)
Definition Math.hpp:797