mal-math
C++20 mathematics library.
All Classes Namespaces Files Functions Variables Typedefs Concepts
math.hpp
Go to the documentation of this file.
1#pragma once
12#if defined(__clang__)
13// TODO
14#elif defined(__GNUC__) || defined(__GNUG__)
15// TODO
16#elif defined(_MSC_VER)
17#pragma warning(push)
18#pragma warning(disable : 6001) // Using uninitialized memory 'variable'.
19#pragma warning(disable : 6287) // Redundant code: the left and right subexpressions are identical
20#pragma warning(disable : 26495) // Variable 'variable' is uninitialized. Always initialize a member variable (type.6).
21#endif
22
23#include "math/mat.hpp"
24#include "math/ray.hpp"
25#include "math/intersection.hpp"
26#include "math/triangle.hpp"
27#include "math/plane.hpp"
28#include "math/box.hpp"
29#include "math/quaternion.hpp"
30#include "math/sphere.hpp"
31#include "math/random.hpp"
32
37namespace mal_math
38{
39 namespace numbers = ::std::numbers;
40
55 template <AnyVec Vector>
56 [[nodiscard]] constexpr vec<Vector::size, std::remove_const_t<typename Vector::type>> reflect(Vector const &normal, Vector const &incident) noexcept
57 {
58 return incident - 2.0f * dot(normal, incident) * normal;
59 }
60
76 template <AnyVec Vector>
77 [[nodiscard]] constexpr vec<Vector::size, std::remove_const_t<typename Vector::type>> refract(Vector const &normal, Vector const &incident, float eta) noexcept
78 {
79 auto const cos_theta = dot(normal, incident);
80 auto const r_out_perp = eta * (incident - cos_theta * normal);
81 auto const r_out_parallel = -std::sqrt(std::abs(1.0f - squared_length(r_out_perp))) * normal;
82 return r_out_perp + r_out_parallel;
83 }
84} // namespace mal_math
85
90namespace std {
97 template <size_t size_x, typename vector_type>
98 struct hash<mal_math::vec<size_x, vector_type>> {
99 [[nodiscard]] constexpr size_t operator()(mal_math::vec<size_x, vector_type> const &v) const noexcept {
100 size_t seed = 0;
101 for (auto const &e : v.data) {
102 seed ^= std::hash<vector_type>{}(e)+0x9e3779b9 + (seed << 6) + (seed >> 2);
103 }
104 return seed;
105 }
106 };
114 template <size_t size_x, size_t size_y, typename matrix_type>
115 struct hash<mal_math::mat<size_x, size_y, matrix_type>> {
116 [[nodiscard]] constexpr size_t operator()(mal_math::mat<size_x, size_y, matrix_type> const &m) const noexcept {
117 size_t seed = 0;
118 for (auto const &e : m.data) {
119 seed ^= std::hash<matrix_type>{}(e)+0x9e3779b9 + (seed << 6) + (seed >> 2);
120 }
121 return seed;
122 }
123 };
131 template<typename quaternion_type>
132 struct hash<mal_math::qua<quaternion_type>> {
133 [[nodiscard]] constexpr size_t operator()(mal_math::qua<quaternion_type> const &q) const noexcept {
134 size_t seed = 0;
135 for (auto const &e : q.data) {
136 seed ^= std::hash<quaternion_type>{}(e)+0x9e3779b9 + (seed << 6) + (seed >> 2);
137 }
138 return seed;
139 }
140 };
141} // namespace std
142#if defined(__clang__)
143// TODO
144#elif defined(__GNUC__) || defined(__GNUG__)
145// TODO
146#elif defined(_MSC_VER)
147#pragma warning(pop)
148#endif
Defines a templated bounding box class for 3D primitives.
Provides functionalities to represent and handle intersections in 3D space.
Provides matrix definitions tailored for various sizes and primitive types.
Contains mathematical utility functions and classes.
constexpr std::remove_const_t< typename T::type > squared_length(T const &vector) noexcept
Compute the squared length of the given vector.
Definition vec_math.inl:284
constexpr T dot(qua< T > const &left, qua< U > const &right) noexcept
Computes the dot product of two quaternions.
constexpr vec< Vector::size, std::remove_const_t< typename Vector::type > > reflect(Vector const &normal, Vector const &incident) noexcept
Computes the reflection of an incident vector based on a surface's normal.
Definition math.hpp:56
constexpr vec< Vector::size, std::remove_const_t< typename Vector::type > > refract(Vector const &normal, Vector const &incident, float eta) noexcept
Computes the refraction of a vector given an interface's normal and an index of refraction.
Definition math.hpp:77
STL namespace.
Provides the definition of the Plane structure and its associated operations.
Quaternion operations and related mathematical functions.
Provides functionality for random number generation and manipulation in vector spaces.
Contains the definition of the Ray class.
Defines the Sphere structure and its associated functionality.
Definition of matrix with dimensions rows x columns and type T
Definition matnxn.hpp:39
Represents a quaternion with primitive type T.
Definition of the mathematical vector with fixed size L and type T
Definition vecn.hpp:22
constexpr size_t operator()(mal_math::mat< size_x, size_y, matrix_type > const &m) const noexcept
Definition math.hpp:116
constexpr size_t operator()(mal_math::qua< quaternion_type > const &q) const noexcept
Definition math.hpp:133
constexpr size_t operator()(mal_math::vec< size_x, vector_type > const &v) const noexcept
Definition math.hpp:99
Defines the Triangle structure and methods for ray-triangle intersection.