opynsim
Unofficial C++ Documentation
Loading...
Searching...
No Matches
geometric_functions.h
Go to the documentation of this file.
1#pragma once
2
6
7#include <cmath>
8#include <concepts>
9#include <cstddef>
10#include <type_traits>
11
12namespace osc
13{
14 // returns the square root of `num`
15 template<std::floating_point T>
16 T sqrt(T num)
17 {
18 return std::sqrt(num);
19 }
20
21 // returns the inverse square root of `x` (i.e. `1/sqrt(x)`)
22 template<std::floating_point T>
24 {
25 return static_cast<T>(1) / sqrt(x);
26 }
27
28 // returns the dot product of `x` and `y` (i.e. `x * y`)
29 template<typename T>
30 requires std::is_arithmetic_v<T>
31 constexpr T dot(T x, T y)
32 {
33 return x * y;
34 }
35
36 // returns the dot product of `x` and `y`
37 template<typename T, size_t N>
38 requires std::is_arithmetic_v<T>
39 constexpr T dot(const Vector<T, N>& x, const Vector<T, N>& y)
40 {
41 T acc = x[0] * y[0];
42 for (size_t i = 1; i < N; ++i) {
43 acc += x[i] * y[i];
44 }
45 return acc;
46 }
47
48 // returns the cross product of `x` and `y`
49 template<typename T>
50 requires std::is_arithmetic_v<T>
51 constexpr Vector<T, 3> cross(const Vector<T, 3>& x, const Vector<T, 3>& y)
52 {
53 return Vector<T, 3>(
54 x.y() * y.z() - y.y() * x.z(),
55 x.z() * y.x() - y.z() * x.x(),
56 x.x() * y.y() - y.x() * x.y()
57 );
58 }
59
60 // returns the length of the provided vector
61 template<std::floating_point T, size_t N>
63 {
64 return sqrt(dot(v, v));
65 }
66
67 // returns the squared length of the provided vector
68 template<std::floating_point T, size_t N>
69 constexpr T length2(const Vector<T, N>& v)
70 {
71 return dot(v, v);
72 }
73
74 // returns `v` normalized to a length of 1
75 template<std::floating_point T, size_t N>
77 {
78 return v * inversesqrt(dot(v, v));
79 }
80
81 // returns the aspect ratio of the vector (effectively: `FloatingPointResult{x}/FloatingPointResult{y}`)
82 template<std::integral T, std::floating_point FloatingPointResult = float>
83 constexpr FloatingPointResult aspect_ratio_of(Vector<T, 2> v)
84 {
85 return static_cast<FloatingPointResult>(v.x()) / static_cast<FloatingPointResult>(v.y());
86 }
87
88 // returns `v.() / v.y()` (i.e. the aspect ratio of `v`).
89 template<std::floating_point T>
91 {
92 return v.x() / v.y();
93 }
94
95 // returns the area of a 2D rectangle that begins at the origin and ends at `v`
96 template<typename T>
97 requires std::is_arithmetic_v<T>
98 constexpr T area_of(const Vector<T, 2>& v)
99 {
100 return v.x() * v.y();
101 }
102
103 // Returns true if unit vectors `a` and `b` are parallel within `tolerance`.
104 // The sign of the direction is ignored.
105 template<std::floating_point T, size_t N>
106 requires (N > 1)
108 const Vector<T, N>& a,
109 const Vector<T, N>& b,
110 T tolerance = epsilon_v<T>)
111 {
112 return equal_within_absdiff(abs(dot(a, b)), T(1), tolerance);
113 }
114
115 // Returns true if unit vectors `a` and `b` are codirectional within `tolerance`.
116 template<std::floating_point T, size_t N>
117 requires (N > 1)
119 const Vector<T, N>& a,
120 const Vector<T, N>& b,
121 T tolerance = epsilon_v<T>)
122 {
123 return equal_within_absdiff(dot(a, b), T(1), tolerance);
124 }
125}
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:62
constexpr T dot(T x, T y)
Definition geometric_functions.h:31
constexpr T area_of(const Vector< T, 2 > &v)
Definition geometric_functions.h:98
bool is_codirectional(const Vector< T, N > &a, const Vector< T, N > &b, T tolerance=epsilon_v< T >)
Definition geometric_functions.h:118
bool is_parallel(const Vector< T, N > &a, const Vector< T, N > &b, T tolerance=epsilon_v< T >)
Definition geometric_functions.h:107
and LessThanComparable< T > bool equal_within_absdiff(T x, T y, T absdiff)
Definition common_functions.h:242
constexpr T length2(const Vector< T, N > &v)
Definition geometric_functions.h:69
T abs(T num)
Definition common_functions.h:24
Vector< T, N > normalize(const Vector< T, N > &v)
Definition geometric_functions.h:76
T inversesqrt(T x)
Definition geometric_functions.h:23
T sqrt(T num)
Definition geometric_functions.h:16
constexpr CoordinateDirection cross(CoordinateDirection x, CoordinateDirection y)
Definition coordinate_direction.h:124
constexpr FloatingPointResult aspect_ratio_of(Vector< T, 2 > v)
Definition geometric_functions.h:83