opynsim
Unofficial C++ Documentation
Loading...
Searching...
No Matches
geometric_functions.h
Go to the documentation of this file.
1#pragma once
2
4
5#include <cmath>
6#include <concepts>
7#include <cstddef>
8#include <type_traits>
9
10namespace osc
11{
12 // returns the square root of `num`
13 template<std::floating_point T>
15 {
16 return std::sqrt(num);
17 }
18
19 // returns the inverse square root of `x` (i.e. `1/sqrt(x)`)
20 template<std::floating_point T>
22 {
23 return static_cast<T>(1) / sqrt(x);
24 }
25
26 // returns the dot product of `x` and `y` (i.e. `x * y`)
27 template<typename T>
28 requires std::is_arithmetic_v<T>
29 constexpr T dot(T x, T y)
30 {
31 return x * y;
32 }
33
34 // returns the dot product of `x` and `y`
35 template<typename T, size_t N>
36 requires std::is_arithmetic_v<T>
37 constexpr T dot(const Vector<T, N>& x, const Vector<T, N>& y)
38 {
39 T acc = x[0] * y[0];
40 for (size_t i = 1; i < N; ++i) {
41 acc += x[i] * y[i];
42 }
43 return acc;
44 }
45
46 // returns the cross product of `x` and `y`
47 template<typename T>
48 requires std::is_arithmetic_v<T>
49 constexpr Vector<T, 3> cross(const Vector<T, 3>& x, const Vector<T, 3>& y)
50 {
51 return Vector<T, 3>(
52 x.y() * y.z() - y.y() * x.z(),
53 x.z() * y.x() - y.z() * x.x(),
54 x.x() * y.y() - y.x() * x.y()
55 );
56 }
57
58 // returns the length of the provided vector
59 template<std::floating_point T, size_t N>
61 {
62 return sqrt(dot(v, v));
63 }
64
65 // returns the squared length of the provided vector
66 template<std::floating_point T, size_t N>
67 constexpr T length2(const Vector<T, N>& v)
68 {
69 return dot(v, v);
70 }
71
72 // returns `v` normalized to a length of 1
73 template<std::floating_point T, size_t N>
75 {
76 return v * inversesqrt(dot(v, v));
77 }
78
79 // returns the aspect ratio of the vector (effectively: `FloatingPointResult{x}/FloatingPointResult{y}`)
80 template<std::integral T, std::floating_point FloatingPointResult = float>
82 {
83 return static_cast<FloatingPointResult>(v.x()) / static_cast<FloatingPointResult>(v.y());
84 }
85
86 // returns `v.() / v.y()` (i.e. the aspect ratio of `v`).
87 template<std::floating_point T>
89 {
90 return v.x() / v.y();
91 }
92
93 // returns the area of a 2D rectangle that begins at the origin and ends at `v`
94 template<typename T>
95 requires std::is_arithmetic_v<T>
96 constexpr T area_of(const Vector<T, 2>& v)
97 {
98 return v.x() * v.y();
99 }
100}
Definition vector.h:63
constexpr reference x()
Returns (*this)[0]
Definition vector.h:157
constexpr reference z()
Returns (*this)[2]
Definition vector.h:167
constexpr reference y()
Returns (*this)[1]
Definition vector.h:162
Definition custom_decoration_generator.h:5
T length(const Vector< T, N > &v)
Definition geometric_functions.h:60
constexpr T dot(T x, T y)
Definition geometric_functions.h:29
constexpr T area_of(const Vector< T, 2 > &v)
Definition geometric_functions.h:96
constexpr T length2(const Vector< T, N > &v)
Definition geometric_functions.h:67
constexpr U to(T &&value)
Definition conversion.h:56
Vector< T, N > normalize(const Vector< T, N > &v)
Definition geometric_functions.h:74
T inversesqrt(T x)
Definition geometric_functions.h:21
T sqrt(T num)
Definition geometric_functions.h:14
constexpr CoordinateDirection cross(CoordinateDirection x, CoordinateDirection y)
Definition coordinate_direction.h:117
constexpr FloatingPointResult aspect_ratio_of(Vector< T, 2 > v)
Definition geometric_functions.h:81