commit0adeba26e4Author: = <=> Date: Tue Mar 26 11:35:37 2024 +0100 minor fix commit011d8a573cAuthor: = <=> Date: Tue Mar 26 11:33:56 2024 +0100 fix stupid build error commit22406ad020Author: = <=> Date: Tue Mar 26 11:25:35 2024 +0100 yes commit4db55a372bAuthor: = <=> Date: Tue Mar 26 11:13:22 2024 +0100 csavgfdslgakdf;g commit20d176ccb5Author: AquaEBM <AquaEBM@gmail.com> Date: Tue Mar 26 11:07:27 2024 +0100 add range overlapping check utilities commit3863e7907fAuthor: AquaEBM <AquaEBM@gmail.com> Date: Tue Mar 26 10:24:35 2024 +0100 better edge case handling commit35b7d7bab0Author: = <=> Date: Tue Mar 26 10:19:21 2024 +0100 I am fucking retarded what the fuck is this stupid brain smoking commit6aca413b4fAuthor: Persson-dev <sim16.prib@gmail.com> Date: Sun Mar 24 18:14:02 2024 +0100 small refactor commitee322e3e7bAuthor: AquaEBM@gmail.com <AquaEBM@gmail.com> Date: Sun Mar 24 17:40:39 2024 +0100 improve tests, fix some bugs in Distance, (not working yet) commit864a15e4c8Merge:7efd821c1ded40Author: Persson-dev <sim16.prib@gmail.com> Date: Fri Mar 22 19:33:45 2024 +0100 Squashed commit of the following: commitc1ded40cc4Author: Persson-dev <sim16.prib@gmail.com> Date: Thu Mar 21 17:24:05 2024 +0100 improve network test commit285bf880eeAuthor: Persson-dev <sim16.prib@gmail.com> Date: Thu Mar 21 17:14:43 2024 +0100 fix warning commit06ff76607dAuthor: Persson-dev <sim16.prib@gmail.com> Date: Thu Mar 21 15:45:33 2024 +0100 add bots commit19229bbe8aAuthor: Persson-dev <sim16.prib@gmail.com> Date: Thu Mar 21 14:55:57 2024 +0100 fix warnings commit2b0930a734Author: Morph01 <thibaut6969delastreet@gmail.com> Date: Tue Mar 19 10:43:55 2024 +0100 hud of blitz (#16) Résoudre partiellement #12 Co-authored-by: Persson-dev <sim16.prib@gmail.com> Co-authored-by: Morph01 <145839520+Morph01@users.noreply.github.com> Co-authored-by: Simon Pribylski <sim16.prib@gmail.com> Reviewed-on: #16 commit84b6acad4cAuthor: Persson-dev <sim16.prib@gmail.com> Date: Tue Mar 19 10:15:54 2024 +0100 less spooky light commit9f94d51fc4Author: Persson-dev <sim16.prib@gmail.com> Date: Thu Mar 14 16:36:15 2024 +0100 Ajout de tests d'intégration (#17) Le paquet fournissant OpenGL a été temporairement remplacé par glew Reviewed-on: #17 Co-authored-by: Persson-dev <sim16.prib@gmail.com> Co-committed-by: Persson-dev <sim16.prib@gmail.com> commit9951256881Author: Simon Pribylski <sim16.prib@gmail.com> Date: Wed Mar 13 20:19:37 2024 +0100 action: install libsdl via apt commit4a02054648Author: Morph01 <145839520+Morph01@users.noreply.github.com> Date: Wed Mar 13 13:53:20 2024 +0100 compression tests commit19c39312bfAuthor: Morph01 <145839520+Morph01@users.noreply.github.com> Date: Wed Mar 13 13:51:57 2024 +0100 setup action commitd5014b1e8aAuthor: Persson-dev <sim16.prib@gmail.com> Date: Tue Mar 12 10:26:47 2024 +0100 Revert "extend Vec3" This reverts commitdccfa9c936. commitdccfa9c936Author: = <=> Date: Tue Mar 12 10:01:00 2024 +0100 extend Vec3 Merge branch 'main' into physics commit7efd8218eaAuthor: Persson-dev <sim16.prib@gmail.com> Date: Fri Mar 22 19:28:46 2024 +0100 moved functions in Maths.h commit08db7f84b9Author: Persson-dev <sim16.prib@gmail.com> Date: Fri Mar 22 19:16:48 2024 +0100 add missing include commit7119dea783Author: Persson-dev <sim16.prib@gmail.com> Date: Fri Mar 22 19:15:01 2024 +0100 merge 'main' branch commitc1ded40cc4Author: Persson-dev <sim16.prib@gmail.com> Date: Thu Mar 21 17:24:05 2024 +0100 improve network test commit285bf880eeAuthor: Persson-dev <sim16.prib@gmail.com> Date: Thu Mar 21 17:14:43 2024 +0100 fix warning commit06ff76607dAuthor: Persson-dev <sim16.prib@gmail.com> Date: Thu Mar 21 15:45:33 2024 +0100 add bots commit19229bbe8aAuthor: Persson-dev <sim16.prib@gmail.com> Date: Thu Mar 21 14:55:57 2024 +0100 fix warnings commit2b0930a734Author: Morph01 <thibaut6969delastreet@gmail.com> Date: Tue Mar 19 10:43:55 2024 +0100 hud of blitz (#16) Résoudre partiellement #12 Co-authored-by: Persson-dev <sim16.prib@gmail.com> Co-authored-by: Morph01 <145839520+Morph01@users.noreply.github.com> Co-authored-by: Simon Pribylski <sim16.prib@gmail.com> Reviewed-on: #16 commit84b6acad4cAuthor: Persson-dev <sim16.prib@gmail.com> Date: Tue Mar 19 10:15:54 2024 +0100 less spooky light commit9f94d51fc4Author: Persson-dev <sim16.prib@gmail.com> Date: Thu Mar 14 16:36:15 2024 +0100 Ajout de tests d'intégration (#17) Le paquet fournissant OpenGL a été temporairement remplacé par glew Reviewed-on: #17 Co-authored-by: Persson-dev <sim16.prib@gmail.com> Co-committed-by: Persson-dev <sim16.prib@gmail.com> commit9951256881Author: Simon Pribylski <sim16.prib@gmail.com> Date: Wed Mar 13 20:19:37 2024 +0100 action: install libsdl via apt commit4a02054648Author: Morph01 <145839520+Morph01@users.noreply.github.com> Date: Wed Mar 13 13:53:20 2024 +0100 compression tests commit19c39312bfAuthor: Morph01 <145839520+Morph01@users.noreply.github.com> Date: Wed Mar 13 13:51:57 2024 +0100 setup action commitd5014b1e8aAuthor: Persson-dev <sim16.prib@gmail.com> Date: Tue Mar 12 10:26:47 2024 +0100 Revert "extend Vec3" This reverts commitdccfa9c936. commitdccfa9c936Author: = <=> Date: Tue Mar 12 10:01:00 2024 +0100 extend Vec3 commit6e998fc368Author: AquaEBM@gmail.com <AquaEBM@gmail.com> Date: Fri Mar 22 16:41:52 2024 +0100 change tests commit441131a2f5Author: AquaEBM@gmail.com <AquaEBM@gmail.com> Date: Fri Mar 22 16:14:56 2024 +0100 rename Intersects to distance commitc875fa1deeAuthor: Persson-dev <sim16.prib@gmail.com> Date: Fri Mar 22 15:50:32 2024 +0100 more template commit5e4b318d67Author: AquaEBM@gmail.com <AquaEBM@gmail.com> Date: Fri Mar 22 15:26:07 2024 +0100 inline Reduce_<> functions commit076fa7badcAuthor: AquaEBM@gmail.com <AquaEBM@gmail.com> Date: Fri Mar 22 15:18:39 2024 +0100 fix compilation error commit1091abd034Author: AquaEBM <AquaEBM@gmail.com> Date: Fri Mar 22 14:58:26 2024 +0100 fix some compilation errors commitdd9ea3ece8Author: AquaEBM <AquaEBM@gmail.com> Date: Fri Mar 22 12:02:39 2024 +0100 update intersects, document changes commit6226161e31Author: AquaEBM <AquaEBM@gmail.com> Date: Fri Mar 22 09:22:06 2024 +0100 fix compilation error, add min/max functions for Vec3d commit6b32e8878eAuthor: AquaEBM <AquaEBM@gmail.com> Date: Fri Mar 22 09:18:34 2024 +0100 fix min/max functions
250 lines
5.9 KiB
C++
250 lines
5.9 KiB
C++
#pragma once
|
|
|
|
#include "blitz/maths/Vector.h"
|
|
#include <cmath>
|
|
|
|
namespace blitz {
|
|
namespace maths {
|
|
|
|
static constexpr float PI = 3.141592653f;
|
|
|
|
template <typename T>
|
|
/**
|
|
* @brief returns the amount of overlap between the ranges [a1 ; b1] and [a2 ; b2]
|
|
*/
|
|
T RangesOverlap(T& a1, T& b1, T& a2, T& b2) {
|
|
return std::min(std::max(a1, b1), std::max(a2, b2)) - std::max(std::min(a1, b1), std::min(a2, b2));
|
|
}
|
|
|
|
template <typename T>
|
|
/**
|
|
* @brief returns whether the ranges [a1 ; b1] and [a2 ; b2] overlap
|
|
*/
|
|
bool RangesOverlapping(T& a1, T& b1, T& a2, T& b2) {
|
|
return RangesOverlap(a1, a2, b1, b2) >= 0;
|
|
}
|
|
|
|
//////////////////////////////////////////////////////////////////
|
|
// Vectors //
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
template <typename T>
|
|
T Length(const Vec3<T>& vect) {
|
|
return std::sqrt(vect.x * vect.x + vect.y * vect.y + vect.z * vect.z);
|
|
}
|
|
|
|
template <typename T>
|
|
Vec3<T> Normalize(const Vec3<T>& vect) {
|
|
T length = Length(vect);
|
|
|
|
return {vect.x / length, vect.y / length, vect.z / length};
|
|
}
|
|
|
|
template <typename T>
|
|
Vec4<T> Normalize(const Vec4<T>& vect) {
|
|
T length = std::sqrt(vect.x * vect.x + vect.y * vect.y + vect.z * vect.z + vect.w * vect.w);
|
|
|
|
return {vect.x / length, vect.y / length, vect.z / length, vect.w / length};
|
|
}
|
|
|
|
template <typename T>
|
|
T Dot(const Vec3<T>& vect, const Vec3<T>& other) {
|
|
return vect.x * other.x + vect.y * other.y + vect.z * other.z;
|
|
}
|
|
|
|
template <typename T>
|
|
Vec3<T> Cross(const Vec3<T>& vect, const Vec3<T>& other) {
|
|
return {
|
|
vect.y * other.z - vect.z * other.y,
|
|
vect.z * other.x - vect.x * other.z,
|
|
vect.x * other.y - vect.y * other.x,
|
|
};
|
|
}
|
|
|
|
template <typename T>
|
|
T Dot(const Vec4<T>& vect, const Vec4<T>& other) {
|
|
return vect.x * other.x + vect.y * other.y + vect.z * other.z + vect.w * other.w;
|
|
}
|
|
|
|
template <typename T>
|
|
T Distance(const Vec3<T>& vect, const Vec3<T>& other) {
|
|
return Length(vect - other);
|
|
}
|
|
|
|
// it seems that `std::{min, max}`'s behavior conflicts with that of `cmath`'s `f{min, max}[f]`
|
|
// Why? Like I fucking know dude
|
|
|
|
|
|
template <typename T>
|
|
T ReduceMin(const Vec3<T>& vect) {
|
|
return std::min(std::min(vect.x, vect.y), vect.z);
|
|
}
|
|
|
|
template <typename T>
|
|
T ReduceMax(const Vec3<T>& vect) {
|
|
return std::max(std::max(vect.x, vect.y), vect.z);
|
|
}
|
|
|
|
/**
|
|
* @brief returns the (signed) minimal coordinate of the vector
|
|
*
|
|
* @param v
|
|
* @return constexpr T
|
|
*/
|
|
template <>
|
|
inline float ReduceMin<float>(const Vec3f& v) {
|
|
return std::fminf(std::fminf(v.x, v.y), v.z);
|
|
}
|
|
|
|
/**
|
|
* @brief returns the (signed) maximal coordinate of the vector
|
|
*
|
|
* @param v
|
|
* @return constexpr T
|
|
*/
|
|
template <>
|
|
inline float ReduceMax<float>(const Vec3f& v) {
|
|
return std::fmaxf(std::fmaxf(v.x, v.y), v.z);
|
|
}
|
|
|
|
/**
|
|
* @brief returns the (signed) minimal coordinate of the vector
|
|
*
|
|
* @param v
|
|
* @return constexpr T
|
|
*/
|
|
template <>
|
|
inline double ReduceMin<double>(const Vec3d& v) {
|
|
return std::fmin(std::fmin(v.x, v.y), v.z);
|
|
}
|
|
|
|
/**
|
|
* @brief returns the (signed) maximal coordinate of the vector
|
|
*
|
|
* @param v
|
|
* @return constexpr T
|
|
*/
|
|
template <>
|
|
inline double ReduceMax<double>(const Vec3d& v) {
|
|
return std::fmax(std::fmax(v.x, v.y), v.z);
|
|
}
|
|
|
|
/**
|
|
* @brief returns the coordinate-wise minimum of the given vectors,
|
|
* where a coordinate in the returned vector is NAN iff any of the two compared ones are NAN
|
|
*
|
|
* @tparam T
|
|
* @param self
|
|
* @param other
|
|
* @return constexpr Vec3f
|
|
*/
|
|
template <typename T>
|
|
constexpr Vec3<T> Min(const Vec3<T>& self, const Vec3<T>& other) {
|
|
return {
|
|
std::min(self.x, other.x),
|
|
std::min(self.y, other.y),
|
|
std::min(self.z, other.z),
|
|
};
|
|
}
|
|
|
|
/**
|
|
* @brief returns the coordinate-wise maximum of the given vectors,
|
|
* where a coordinate in the returned vector is NAN iff any of the two compared ones are NAN
|
|
*
|
|
* @tparam T
|
|
* @param self
|
|
* @param other
|
|
* @return constexpr Vec3f
|
|
*/
|
|
template <typename T>
|
|
constexpr Vec3<T> Max(const Vec3<T>& self, const Vec3<T>& other) {
|
|
return {
|
|
std::max(self.x, other.x),
|
|
std::max(self.y, other.y),
|
|
std::max(self.z, other.z),
|
|
};
|
|
}
|
|
|
|
//////////////////////////////////////////////////////////////////
|
|
// Matricies //
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
template <typename T>
|
|
Vec4<T> Dot(const Mat4<T>& mat, const Vec4<T>& vect) {
|
|
return {mat.x0 * vect.x + mat.x1 * vect.y + mat.x2 * vect.z + mat.x3 * vect.w,
|
|
mat.y0 * vect.x + mat.y1 * vect.y + mat.y2 * vect.z + mat.y3 * vect.w,
|
|
mat.z0 * vect.x + mat.z1 * vect.y + mat.z2 * vect.z + mat.z3 * vect.w,
|
|
mat.w0 * vect.x + mat.w1 * vect.y + mat.w2 * vect.z + mat.w3 * vect.w};
|
|
}
|
|
|
|
template <typename T>
|
|
Mat4<T> Dot(const Mat4<T>& mat, const Mat4<T>& other) {
|
|
Mat4<T> result{};
|
|
|
|
for (std::size_t i = 0; i < Mat4<T>::MATRIX_SIZE; i++) {
|
|
for (std::size_t j = 0; j < Mat4<T>::MATRIX_SIZE; j++) {
|
|
for (std::size_t k = 0; k < Mat4<T>::MATRIX_SIZE; k++) {
|
|
result.at(i, j) += mat.at(i, k) * other.at(k, j);
|
|
}
|
|
}
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
template <typename T>
|
|
Mat4<T> Identity() {
|
|
Mat4<T> result{};
|
|
|
|
result.x0 = static_cast<T>(1);
|
|
result.y1 = static_cast<T>(1);
|
|
result.z2 = static_cast<T>(1);
|
|
result.w3 = static_cast<T>(1);
|
|
|
|
return result;
|
|
}
|
|
|
|
template <typename T>
|
|
Mat4<T> Transpose(const Mat4<T>& mat) {
|
|
Mat4<T> result;
|
|
|
|
result.x1 = mat.y0;
|
|
result.x2 = mat.z0;
|
|
result.x3 = mat.w0;
|
|
result.y0 = mat.x1;
|
|
result.y2 = mat.z1;
|
|
result.y3 = mat.w1;
|
|
result.z0 = mat.x2;
|
|
result.z1 = mat.y2;
|
|
result.z3 = mat.w2;
|
|
result.w0 = mat.x3;
|
|
result.w1 = mat.y3;
|
|
result.w2 = mat.z3;
|
|
result.x0 = mat.x0;
|
|
result.y1 = mat.y1;
|
|
result.z2 = mat.z2;
|
|
result.w3 = mat.w3;
|
|
|
|
return result;
|
|
}
|
|
|
|
Mat4f Perspective(float fovY, float aspectRatio, float zNear, float zFar);
|
|
Mat4f Look(const Vec3f& eyePos, const Vec3f& front, const Vec3f& up);
|
|
|
|
Mat4f Inverse(const Mat4f& mat);
|
|
|
|
Mat4f Translate(const Vec3f& translation);
|
|
|
|
Mat4f Scale(const Vec3f& axisFactor);
|
|
|
|
Mat4f RotateX(float angle);
|
|
Mat4f RotateY(float angle);
|
|
Mat4f RotateZ(float angle);
|
|
|
|
Mat4f Rotate(const Vec3f& angles);
|
|
|
|
Mat4f Rotate(float angle, Vec3f axis);
|
|
|
|
} // namespace maths
|
|
} // namespace blitz
|