mal-math
C++20 mathematics library.
All Classes Namespaces Files Functions Variables Typedefs Concepts
vec_math.hpp
Go to the documentation of this file.
1#pragma once
2#include "vec.hpp"
12namespace mal_math
13{
17 template <AnyVec U>
18 constexpr std::istream &operator>>(std::istream &is, U &vec);
22 template <AnyVec U>
23 constexpr std::ostream &operator<<(std::ostream &os, U const &vec);
27 template <AnyVec U>
28 constexpr std::ostream &operator<<(std::ostream &os, U &vec);
38 template <size_t n, AnyVec Vector>
39 [[nodiscard]] constexpr vec<n, typename Vector::type> &as_rvec(Vector &v) noexcept requires(n >= 2 && n <= Vector::size);
40
48 template <AnyVec Vector>
49 [[nodiscard]] constexpr vec<Vector::size, typename Vector::type> &as_rvec(Vector &v) noexcept;
50
61 template <size_t n, Primitive U, AnyVec Vector>
62 [[nodiscard]] constexpr vec<n, U> as_vec(Vector const &v) noexcept requires(n >= 2 && n <= Vector::size);
72 template <size_t n, AnyVec Vector>
73 [[nodiscard]] constexpr vec<n, std::remove_const_t<typename Vector::type>> as_vec(Vector const &v) noexcept requires(n >= 2 && n <= Vector::size);
81 template <AnyVec Vector>
82 [[nodiscard]] constexpr vec<Vector::size, std::remove_const_t<typename Vector::type>> as_vec(Vector const &v) noexcept;
83
93 template <size_t n, AnyVec Vector>
94 [[nodiscard]] constexpr vec<n, typename Vector::type> const &as_crvec(Vector const &v) noexcept requires(n >= 2 && n <= Vector::size);
95
103 template <AnyVec Vector>
104 [[nodiscard]] constexpr vec<Vector::size, typename Vector::type> const &as_crvec(Vector const &v) noexcept;
105
106 template <size_t size, Primitive T, Primitive U>
107 [[nodiscard]] constexpr vec<size, T> operator+(vec<size, T> const &vector, U const value) noexcept;
108 template <size_t size, Primitive T, Primitive U>
109 [[nodiscard]] constexpr vec<size, T> operator-(vec<size, T> const &vector, U const value) noexcept;
110 template <size_t size, Primitive T, Primitive U>
111 [[nodiscard]] constexpr vec<size, T> operator*(vec<size, T> const &vector, U const value) noexcept;
112 template <size_t size, Primitive T, Primitive U>
113 [[nodiscard]] constexpr vec<size, T> operator/(vec<size, T> const &vector, U const value) noexcept;
114
115 template <size_t size, Primitive T, Primitive U>
116 [[nodiscard]] constexpr vec<size, T> operator*(U const value, vec<size, T> const &vector) noexcept;
117 template <size_t size, Primitive T, Primitive U>
118 [[nodiscard]] constexpr vec<size, T> operator/(U const value, vec<size, T> const &vector) noexcept;
119 template <size_t size, Primitive T, Primitive U>
120 [[nodiscard]] constexpr vec<size, T> operator+(U const value, vec<size, T> const &vector) noexcept;
121 template <size_t size, Primitive T, Primitive U>
122 [[nodiscard]] constexpr vec<size, T> operator-(U const value, vec<size, T> const &vector) noexcept;
123
124 template <size_t size, Primitive T, Primitive U>
125 [[nodiscard]] constexpr vec<size, std::remove_const_t<T>> operator*(U const value, _detail::rvec<size, T> const &vector) noexcept;
126 template <size_t size, Primitive T, Primitive U>
127 [[nodiscard]] constexpr vec<size, std::remove_const_t<T>> operator+(_detail::rvec<size, T> const &vector, U const value) noexcept;
128 template <size_t size, Primitive T, Primitive U>
129 [[nodiscard]] constexpr vec<size, std::remove_const_t<T>> operator-(_detail::rvec<size, T> const &vector, U const value) noexcept;
130 template <size_t size, Primitive T, Primitive U>
131 [[nodiscard]] constexpr vec<size, std::remove_const_t<T>> operator*(_detail::rvec<size, T> const &vector, U const value) noexcept;
132 template <size_t size, Primitive T, Primitive U>
133 [[nodiscard]] constexpr vec<size, std::remove_const_t<T>> operator/(_detail::rvec<size, T> const &vector, U const value) noexcept;
134 template <size_t size, Primitive T, Primitive U>
135 [[nodiscard]] constexpr vec<size, std::remove_const_t<T>> operator%(_detail::rvec<size, T> const &left, U const value) noexcept;
136
137 template <size_t size, Primitive T, Primitive U>
138 [[nodiscard]] constexpr vec<size, T> operator+(vec<size, T> const &left, vec<size, U> const &right) noexcept;
139 template <size_t size, Primitive T, Primitive U>
140 [[nodiscard]] constexpr vec<size, T> operator-(vec<size, T> const &left, vec<size, U> const &right) noexcept;
141 template <size_t size, Primitive T, Primitive U>
142 [[nodiscard]] constexpr vec<size, T> operator*(vec<size, T> const &left, vec<size, U> const &right) noexcept;
143 template <size_t size, Primitive T, Primitive U>
144 [[nodiscard]] constexpr vec<size, T> operator/(vec<size, T> const &left, vec<size, U> const &right) noexcept;
145 template <size_t size, Primitive T, Primitive U>
146 [[nodiscard]] constexpr vec<size, T> operator%(vec<size, T> const &left, U const value) noexcept;
147 template <size_t size, Primitive T, Primitive U>
148 [[nodiscard]] constexpr vec<size, T> operator%(vec<size, T> const &left, vec<size, U> const &right) noexcept;
149
150 template <size_t size, Primitive T, Primitive U>
151 [[nodiscard]] constexpr vec<size, std::remove_const_t<T>> operator+(_detail::rvec<size, T> const &left, vec<size, U> const &right) noexcept;
152 template <size_t size, Primitive T, Primitive U>
153 [[nodiscard]] constexpr vec<size, std::remove_const_t<T>> operator-(_detail::rvec<size, T> const &left, vec<size, U> const &right) noexcept;
154 template <size_t size, Primitive T, Primitive U>
155 [[nodiscard]] constexpr vec<size, std::remove_const_t<T>> operator*(_detail::rvec<size, T> const &left, vec<size, U> const &right) noexcept;
156 template <size_t size, Primitive T, Primitive U>
157 [[nodiscard]] constexpr vec<size, std::remove_const_t<T>> operator/(_detail::rvec<size, T> const &left, vec<size, U> const &right) noexcept;
158 template <size_t size, Primitive T, Primitive U>
159 [[nodiscard]] constexpr vec<size, std::remove_const_t<T>> operator%(_detail::rvec<size, T> const &left, vec<size, U> const &right) noexcept;
160
161 template <size_t size, Primitive T, Primitive U>
162 [[nodiscard]] constexpr vec<size, std::remove_const_t<T>> operator+(_detail::rvec<size, T> const &left, _detail::rvec<size, U> const &right) noexcept;
163 template <size_t size, Primitive T, Primitive U>
164 [[nodiscard]] constexpr vec<size, std::remove_const_t<T>> operator-(_detail::rvec<size, T> const &left, _detail::rvec<size, U> const &right) noexcept;
165 template <size_t size, Primitive T, Primitive U>
166 [[nodiscard]] constexpr vec<size, std::remove_const_t<T>> operator*(_detail::rvec<size, T> const &left, _detail::rvec<size, U> const &right) noexcept;
167 template <size_t size, Primitive T, Primitive U>
168 [[nodiscard]] constexpr vec<size, std::remove_const_t<T>> operator/(_detail::rvec<size, T> const &left, _detail::rvec<size, U> const &right) noexcept;
169 template <size_t size, Primitive T, Primitive U>
170 [[nodiscard]] constexpr vec<size, std::remove_const_t<T>> operator%(_detail::rvec<size, T> const &left, _detail::rvec<size, U> const &right) noexcept;
171
172 template <size_t size, Primitive T, Primitive U>
173 [[nodiscard]] constexpr vec<size, T> operator+(vec<size, T> const &left, _detail::rvec<size, U> const &right) noexcept;
174 template <size_t size, Primitive T, Primitive U>
175 [[nodiscard]] constexpr vec<size, T> operator-(vec<size, T> const &left, _detail::rvec<size, U> const &right) noexcept;
176 template <size_t size, Primitive T, Primitive U>
177 [[nodiscard]] constexpr vec<size, T> operator*(vec<size, T> const &left, _detail::rvec<size, U> const &right) noexcept;
178 template <size_t size, Primitive T, Primitive U>
179 [[nodiscard]] constexpr vec<size, T> operator/(vec<size, T> const &left, _detail::rvec<size, U> const &right) noexcept;
180 template <size_t size, Primitive T, Primitive U>
181 [[nodiscard]] constexpr vec<size, T> operator%(vec<size, T> const &left, _detail::rvec<size, U> const &right) noexcept;
182
183 template <AnyVec T, AnyVec U>
184 [[nodiscard]] constexpr bool operator==(T const &left, U const &right) noexcept requires(T::size == U::size);
185
186 template <AnyVec T, AnyVec U>
187 [[nodiscard]] constexpr bool operator!=(T const &left, U const &right) noexcept requires(T::size == U::size);
195 template <AnyVec T>
196 [[nodiscard]] constexpr typename std::remove_const_t<typename T::type> squared_length(T const &vector) noexcept;
204 template <AnyVec T>
205 [[nodiscard]] constexpr auto length(T const &vector) noexcept;
206
214 template <AnyVec T>
215 [[nodiscard]] constexpr vec<T::size, std::remove_const_t<typename T::type>> unit_vector(T const &vector) noexcept;
224 template <AnyVec T>
225 [[nodiscard]] constexpr vec<T::size, std::remove_const_t<typename T::type>> normalize(T const &vector) noexcept;
226
237 template <AnyVec T, AnyVec U>
238 constexpr std::remove_const_t<typename T::type> dot(T const &left, U const &right) noexcept requires(T::size == U::size);
239
250 template <AnyVec T, AnyVec U>
251 constexpr std::remove_const_t<typename T::type> angle(T const &left, U const &right) noexcept requires(T::size == U::size);
252
253 template <AnyVec T>
254 constexpr vec<T::size, std::remove_const_t<typename T::type>> cos(T const &vector) noexcept;
255 template <AnyVec T>
256 constexpr vec<T::size, std::remove_const_t<typename T::type>> sin(T const &vector) noexcept;
257 template <AnyVec T>
258 constexpr vec<T::size, std::remove_const_t<typename T::type>> tan(T const &vector) noexcept;
259 template <AnyVec T>
260 constexpr vec<T::size, std::remove_const_t<typename T::type>> acos(T const &vector) noexcept;
261 template <AnyVec T>
262 constexpr vec<T::size, std::remove_const_t<typename T::type>> asin(T const &vector) noexcept;
263 template <AnyVec T>
264 constexpr vec<T::size, std::remove_const_t<typename T::type>> atan(T const &vector) noexcept;
265 template <AnyVec T, AnyVec U>
266 constexpr vec<T::size, std::remove_const_t<typename T::type>> atan(T const &left, U const &right) noexcept requires(T::size == U::size);
267 template <AnyVec T, AnyVec U>
268 constexpr vec<T::size, std::remove_const_t<typename T::type>> atan2(T const &left, U const &right) noexcept requires(T::size == U::size);
269 template <AnyVec T>
270 constexpr vec<T::size, std::remove_const_t<typename T::type>> cosh(T const &vector) noexcept;
271 template <AnyVec T>
272 constexpr vec<T::size, std::remove_const_t<typename T::type>> sinh(T const &vector) noexcept;
273 template <AnyVec T>
274 constexpr vec<T::size, std::remove_const_t<typename T::type>> tanh(T const &vector) noexcept;
275 template <AnyVec T>
276 constexpr vec<T::size, std::remove_const_t<typename T::type>> acosh(T const &vector) noexcept;
277 template <AnyVec T>
278 constexpr vec<T::size, std::remove_const_t<typename T::type>> asinh(T const &vector) noexcept;
279 template <AnyVec T>
280 constexpr vec<T::size, std::remove_const_t<typename T::type>> atanh(T const &vector) noexcept;
281 template <AnyVec T>
282 constexpr vec<T::size, std::remove_const_t<typename T::type>> exp(T const &vector) noexcept;
283 template <AnyVec T>
284 constexpr vec<T::size, std::remove_const_t<typename T::type>> exp2(T const &vector) noexcept;
285 template <AnyVec T>
286 constexpr vec<T::size, std::remove_const_t<typename T::type>> expm1(T const &vector) noexcept;
287 template <AnyVec T>
288 constexpr vec<T::size, std::remove_const_t<typename T::type>> log(T const &vector) noexcept;
289 template <AnyVec T>
290 constexpr vec<T::size, std::remove_const_t<typename T::type>> log2(T const &vector) noexcept;
291 template <AnyVec T>
292 constexpr vec<T::size, std::remove_const_t<typename T::type>> log10(T const &vector) noexcept;
293 template <AnyVec T>
294 constexpr vec<T::size, std::remove_const_t<typename T::type>> log1p(T const &vector) noexcept;
295 template <AnyVec T>
296 constexpr vec<T::size, std::remove_const_t<typename T::type>> logb(T const &vector) noexcept;
297 template <AnyVec T>
298 constexpr vec<T::size, std::remove_const_t<typename T::type>> sqrt(T const &vector) noexcept;
299 template <AnyVec T>
300 constexpr vec<T::size, std::remove_const_t<typename T::type>> inversesqrt(T const &vector) noexcept;
301
302 template <AnyVec T>
303 constexpr vec<T::size, std::remove_const_t<typename T::type>> abs(T const &vector) noexcept;
304 template <AnyVec T>
305 constexpr vec<T::size, std::remove_const_t<typename T::type>> sign(T const &vector) noexcept;
306 template <AnyVec T>
307 constexpr vec<T::size, std::remove_const_t<typename T::type>> floor(T const &vector) noexcept;
308 template <AnyVec T>
309 constexpr vec<T::size, std::remove_const_t<typename T::type>> ceil(T const &vector) noexcept;
310 template <AnyVec T>
311 constexpr vec<T::size, std::remove_const_t<typename T::type>> trunc(T const &vector) noexcept;
312 template <AnyVec T>
313 constexpr vec<T::size, std::remove_const_t<typename T::type>> round(T const &vector) noexcept;
314 template <AnyVec T>
315 constexpr vec<T::size, std::remove_const_t<typename T::type>> roundEven(T const &vector) noexcept;
316 template <AnyVec T>
317 constexpr vec<T::size, std::remove_const_t<typename T::type>> fract(T const &vector) noexcept;
318 template <AnyVec T>
319 constexpr vec<T::size, std::remove_const_t<typename T::type>> mod(T const &vector, typename T::type const &scalar) noexcept;
320 template <AnyVec T, AnyVec U>
321 constexpr vec<T::size, std::remove_const_t<typename T::type>> mod(T const &left, U const &right) noexcept requires(T::size == U::size);
322
323 template <AnyVec T, Primitive U>
324 constexpr vec<T::size, std::remove_const_t<typename T::type>> pow(T const &vector, U const pow) noexcept;
325
326 template <AnyVec T, AnyVec U>
327 constexpr vec<T::size, std::remove_const_t<typename T::type>> pow(T const &vector1, U const &vector2) noexcept;
338 template <AnyVec T, AnyVec U>
339 constexpr vec<3, std::remove_const_t<typename T::type>> cross(T const &left, U const &right) noexcept requires(T::size == U::size && T::size == 3);
340
350 template <Primitive T, Primitive U>
351 constexpr void rclamp(T &left, U const min, U const max) noexcept;
352
362 template <AnyVec T, AnyVec U>
363 constexpr void rclamp(T &left, U const min, U const max) noexcept requires(T::size == U::size);
364
375 template <AnyVec T, Primitive U>
376 [[nodiscard]] constexpr vec<T::size, std::remove_const_t<typename T::type>> clamp(T const &left, U const min, U const max) noexcept;
377
388 template <AnyVec T, AnyVec U>
389 [[nodiscard]] constexpr vec<T::size, std::remove_const_t<typename T::type>> clamp(T const &left, U const &min, U const &max) noexcept requires(T::size == U::size);
390
401 template <Primitive T, Primitive U>
402 [[nodiscard]] constexpr T clamp(T left, U const min, U const max) noexcept;
412 template <AnyVec T, Primitive U>
413 [[nodiscard]] constexpr vec<T::size, std::remove_const_t<typename T::type>> min(T const &left, U const min) noexcept;
423 template <AnyVec T, Primitive U>
424 [[nodiscard]] constexpr vec<T::size, std::remove_const_t<typename T::type>> max(T const &left, U const max) noexcept;
425
435 template <AnyVec T, AnyVec U>
436 [[nodiscard]] constexpr vec<T::size, std::remove_const_t<typename T::type>> min(T const &left, U const &min) noexcept requires(T::size == U::size);
437
447 template <AnyVec T, AnyVec U>
448 [[nodiscard]] constexpr vec<T::size, std::remove_const_t<typename T::type>> max(T const &left, U const &max) noexcept requires(T::size == U::size);
449
458 template <AnyVec T, AnyVec U>
459 constexpr void rmin(T &left, U const &max) noexcept requires(T::size == U::size);
460
469 template <AnyVec T, AnyVec U>
470 constexpr void rmax(T &left, U const &min) noexcept requires(T::size == U::size);
471
480 template <AnyVec T, Primitive U>
481 constexpr void rmin(T &left, U const max) noexcept;
482
491 template <AnyVec T, Primitive U>
492 constexpr void rmax(T &left, U const min) noexcept;
493
502 template <Primitive T, Primitive U>
503 constexpr void rmin(T &left, U const max) noexcept;
504
513 template <Primitive T, Primitive U>
514 constexpr void rmax(T &left, U const min) noexcept;
523 template <AnyVec T>
524 [[nodiscard]] constexpr std::remove_const_t<typename T::type> distance(T const &left, T const &right) noexcept;
535 template <typename T, Primitive U>
536 [[nodiscard]] constexpr auto lerp(T const &from, T const &to, U const param) noexcept;
537
548 template <typename T, Primitive U>
549 [[nodiscard]] constexpr auto mix(T const &from, T const &to, U const param) noexcept;
550} // namespace mal_math
551#include "vec_math.inl"
Contains mathematical utility functions and classes.
constexpr auto lerp(T const &from, T const &to, U const param) noexcept
Linearly interpolate between two values.
Definition vec_math.inl:882
float atanh(T x) noexcept
float cos(T x) noexcept
constexpr vec< T::size, std::remove_const_t< typename T::type > > max(T const &left, U const max) noexcept
Returns a new vector with each element being the maximum of the corresponding element in left and min...
Definition vec_math.inl:798
float atan2(T x, T y) noexcept
float asin(T x) noexcept
float sin(T x) noexcept
float log1p(T x) noexcept
float acos(T x) noexcept
float acosh(T x) noexcept
float log2(T x) noexcept
constexpr T length(qua< T > const &q) noexcept
Computes the length (or magnitude) of a quaternion.
float expm1(T x) noexcept
float atan(T x) noexcept
float log10(T x) noexcept
constexpr void rmin(T &left, U const &max) noexcept
Modifies left in-place, setting each element to the minimum of itself and the corresponding element i...
Definition vec_math.inl:830
float exp(T x) noexcept
constexpr vec< n, typename Vector::type > const & as_crvec(Vector const &v) noexcept
Convert a given const vector to a const reference of vec with specified size and type.
Definition vec_math.inl:67
float mod(T x, U y) noexcept
constexpr T clamp(T x, T min, T max) noexcept
Function to clamp a value between a minimum and maximum.
float tanh(T x) noexcept
constexpr mat< T::size.x, T::size.y, std::remove_const_t< typename T::type > > operator-(T const &left, U const right)
Subtracts a matrix from a primitive.
Definition mat_math.inl:100
constexpr T angle(qua< T > const &quaternion) noexcept
Computes the angle (or magnitude) of a quaternion.
T roundEven(T x) noexcept
float inversesqrt(T x) noexcept
constexpr auto operator*(U const &lhs, V const &rhs)
Multiplies two matrices.
Definition mat_math.inl:33
float log(T x) noexcept
float exp2(T x) noexcept
T abs(T x) noexcept
constexpr std::remove_const_t< typename T::type > distance(T const &left, T const &right) noexcept
Compute the distance between two vectors.
Definition vec_math.inl:877
constexpr bool operator==(T const &left, U const &right) noexcept
Definition vec_math.inl:268
T trunc(T x) noexcept
float sinh(T x) noexcept
constexpr vec< n, U > as_vec(Vector const &v) noexcept
Create a new vector of a specified size and type from the given vector.
Definition vec_math.inl:34
float sqrt(T x) noexcept
T ceil(T x) noexcept
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 std::istream & operator>>(std::istream &is, U &matrix)
Reads a matrix from an input stream.
Definition mat_math.inl:8
int sign(T x) noexcept
constexpr std::ostream & operator<<(std::ostream &os, U const &matrix)
Writes a matrix to an output stream.
Definition mat_math.inl:20
constexpr void rclamp(T &left, U const min, U const max) noexcept
Clamp the value of left between min and max in-place.
Definition vec_math.inl:695
T floor(T x) noexcept
float asinh(T x) noexcept
constexpr T dot(qua< T > const &left, qua< U > const &right) noexcept
Computes the dot product of two quaternions.
constexpr void rmax(T &left, U const &min) noexcept
Modifies left in-place, setting each element to the maximum of itself and the corresponding element i...
Definition vec_math.inl:839
float pow(T x, U y) noexcept
constexpr bool operator!=(T const &left, U const &right) noexcept
Definition vec_math.inl:279
constexpr qua< T > normalize(qua< T > const &q) noexcept
Normalizes a quaternion.
constexpr vec< T::size, std::remove_const_t< typename T::type > > unit_vector(T const &vector) noexcept
Compute the unit vector (normalized vector) of the given vector.
Definition vec_math.inl:300
constexpr mat< T::size.x, T::size.y, std::remove_const_t< typename T::type > > operator+(U const left, T const &right)
Adds a matrix to a primitive.
Definition mat_math.inl:105
T round(T x) noexcept
float tan(T x) noexcept
constexpr vec< n, typename Vector::type > & as_rvec(Vector &v) noexcept
Convert a given vector to a reference of vec with specified size and type.
Definition vec_math.inl:56
float cosh(T x) noexcept
constexpr auto mix(T const &from, T const &to, U const param) noexcept
Mix between two values (same as lerp).
Definition vec_math.inl:887
float logb(T x) noexcept
constexpr vec< size, std::remove_const_t< T > > operator%(_detail::rvec< size, T > const &left, U const value) noexcept
Definition vec_math.inl:205
constexpr vec< T::size, std::remove_const_t< typename T::type > > min(T const &left, U const min) noexcept
Returns a new vector with each element being the minimum of the corresponding element in left and max...
Definition vec_math.inl:787
constexpr qua< T > operator/(U const value, qua< T > const &vector) noexcept
Scalar-quaternion division.
constexpr vec< 3, T > cross(qua< T > const &left, vec< 3, U > const &right) noexcept
Cross product between quaternion and vector.
T fract(T x) noexcept