IceShard 1
A personal game engine project, with development focused on 2D/2.5D games.
Loading...
Searching...
No Matches
vector_operations.hxx
Go to the documentation of this file.
1
3
4#pragma once
5#include <ice/math/vector.hxx>
6#include <numeric>
7
8namespace ice::math_detail
9{
10
11 template<u32 Size, typename T, size_t... Values>
12 constexpr auto length2_sequenced(ice::math::vec<Size, T> vector, std::index_sequence<Values...>) noexcept -> T
13 {
14 return ((vector.v[0][Values] * vector.v[0][Values]) + ... + T{});
15 }
16
17 template<u32 Size, typename T, size_t... Values>
18 constexpr auto length2_sequenced(T(&array)[Size], std::index_sequence<Values...>) noexcept -> T
19 {
20 return ((array[Values] * array[Values]) + ... + T{});
21 }
22
23} // namespace detail
24
25namespace ice::math
26{
27
28 template<u32 Size, typename T, typename U = T>
29 constexpr auto add(vec<Size, T> left, vec<Size, U> right) noexcept -> vec<Size, T>;
30
31 template<u32 Size, typename T, typename U = T>
32 constexpr auto sub(vec<Size, T> left, vec<Size, U> right) noexcept -> vec<Size, T>;
33
34 template<u32 Size, typename T, typename U = T>
35 constexpr auto mul(vec<Size, T> left, U right) noexcept -> vec<Size, T>;
36
37 template<u32 Size, typename T, typename U = T>
38 constexpr auto div(vec<Size, T> left, U right) noexcept -> vec<Size, T>;
39
40 template<u32 Size, typename T>
41 constexpr auto length(vec<Size, T> left) noexcept -> T;
42
43 template<u32 Size, typename T>
44 constexpr auto length2(vec<Size, T> left) noexcept -> T;
45
46 constexpr auto cross(vec<3, f32> left, vec<3, f32> right) noexcept -> vec<3, f32>;
47
48 constexpr auto dot(vec<3, f32> left, vec<3, f32> right) noexcept -> f32;
49
50 inline auto atan2(vec<2, f32> point) noexcept -> rad;
51
52 template<u32 Size, typename T> requires(Size > 1)
53 inline auto normalize(vec<Size, T> value) noexcept -> vec<Size, T>;
54
55 template<u32 Size, typename T, typename U = T>
56 constexpr auto apply(vec<Size, T> left, U(*fn)(T) noexcept) noexcept -> vec<Size, U>;
57
58
59 template<u32 Size, typename T, typename U>
60 constexpr auto add(vec<Size, T> left, vec<Size, U> right) noexcept -> vec<Size, T>
61 {
62 vec<Size, T> result;
63 for (u32 i = 0; i < Size; ++i)
64 {
65 result.v[0][i] = left.v[0][i] + T{ right.v[0][i] };
66 }
67 return result;
68 }
69
70 template<u32 Size, typename T, typename U>
71 constexpr auto sub(vec<Size, T> left, vec<Size, U> right) noexcept -> vec<Size, T>
72 {
73 vec<Size, T> result;
74 for (u32 i = 0; i < Size; ++i)
75 {
76 result.v[0][i] = left.v[0][i] - T{ right.v[0][i] };
77 }
78 return result;
79 }
80
81 template<u32 Size, typename T, typename U>
82 constexpr auto mul(vec<Size, T> left, U right) noexcept -> vec<Size, T>
83 {
84 vec<Size, T> result;
85 for (u32 i = 0; i < Size; ++i)
86 {
87 result.v[0][i] = left.v[0][i] * T{ right };
88 }
89 return result;
90 }
91
92 template<u32 Size, typename T, typename U>
93 constexpr auto div(vec<Size, T> left, U right) noexcept -> vec<Size, T>
94 {
95 vec<Size, T> result;
96 for (u32 i = 0; i < Size; ++i)
97 {
98 result.v[0][i] = left.v[0][i] / T{ right };
99 }
100 return result;
101 }
102
103 template<u32 Size, typename T>
104 constexpr auto length2(vec<Size, T> left) noexcept -> T
105 {
106 return ice::math_detail::length2_sequenced(left, std::make_index_sequence<Size>{});
107 }
108
109 template<u32 Size, typename T>
110 constexpr auto length(vec<Size, T> left) noexcept -> T
111 {
113 }
114
115 constexpr auto cross(vec<3, f32> left, vec<3, f32> right) noexcept -> vec<3, f32>
116 {
117 return {
118 left.v[0][1] * right.v[0][2] - right.v[0][1] * left.v[0][2],
119 left.v[0][2] * right.v[0][0] - right.v[0][2] * left.v[0][0],
120 left.v[0][0] * right.v[0][1] - right.v[0][0] * left.v[0][1],
121 };
122 }
123
124 constexpr auto dot(vec<3, f32> left, vec<3, f32> right) noexcept -> f32
125 {
126 return left.v[0][0] * right.v[0][0] +
127 left.v[0][1] * right.v[0][1] +
128 left.v[0][2] * right.v[0][2];
129 }
130
131 inline auto atan2(vec<2, f32> point) noexcept -> rad
132 {
133 return { ice::math::atan2(point.v[0][0], point.v[0][1]) };
134 }
135
136 template<u32 Size, typename T> requires(Size > 1)
137 inline auto normalize(vec<Size, T> value) noexcept -> vec<Size, T>
138 {
139 if constexpr (Size == 4)
140 {
141 ICE_ASSERT_CORE(value.v[0][3] != 0.f);
142 ice::math::div(value, value.v[0][3]);
143 }
144
145 f32 const square_sum = ice::math::length2(value);
146 if (square_sum == 0)
147 {
148 return value;
149 }
150
151 f32 const sqrt_inverted = 1.f / sqrt(square_sum);
152 return mul(value, sqrt_inverted);
153 }
154
155 template<u32 Size, typename T, typename U>
156 constexpr auto apply(vec<Size, T> left, U(*fn)(T) noexcept) noexcept -> vec<Size, U>
157 {
158 vec<Size, U> result;
159 for (u32 i = 0; i < Size; ++i)
160 {
161 result.v[0][i] = fn(left.v[0][i]);
162 }
163 return result;
164 }
165
166} // namespace ice::math
#define ICE_ASSERT_CORE(expression)
Definition assert_core.hxx:43
Definition matrix_operations.hxx:9
constexpr auto length2_sequenced(ice::math::vec< Size, T > vector, std::index_sequence< Values... >) noexcept -> T
Definition vector_operations.hxx:12
Definition algorithm.hxx:8
rad32 rad
Definition angles.hxx:170
constexpr auto mul(U first, Args... args) noexcept
Definition algorithm.hxx:38
constexpr auto apply(vec< Size, T > left, U(*fn)(T) noexcept) noexcept -> vec< Size, U >
Definition vector_operations.hxx:156
constexpr auto div(U first, Args... args) noexcept
Definition algorithm.hxx:46
mat< Size, 1, T > vec
Definition vector.hxx:172
constexpr auto length2(vec< Size, T > left) noexcept -> T
Definition vector_operations.hxx:104
auto atan2(f32 x, f32 y) noexcept -> f32
Definition common.hxx:230
auto normalize(vec< Size, T > value) noexcept -> vec< Size, T >
Definition vector_operations.hxx:137
constexpr auto add(arr_t< Size, T > left, arr_t< Size, U > right) noexcept -> arr_t< Size, T >
Definition array_operations.hxx:71
constexpr auto dot(vec< 3, f32 > left, vec< 3, f32 > right) noexcept -> f32
Definition vector_operations.hxx:124
constexpr auto sqrt(f32 val) noexcept -> f32
Definition common.hxx:93
constexpr auto length(vec< Size, T > left) noexcept -> T
Definition vector_operations.hxx:110
constexpr auto sub(U first, Args... args) noexcept
Definition algorithm.hxx:30
constexpr auto cross(vec< 3, f32 > left, vec< 3, f32 > right) noexcept -> vec< 3, f32 >
Definition vector_operations.hxx:115
std::uint32_t u32
Definition types.hxx:26
float f32
Definition types.hxx:16
T v[count_columns][count_rows]
Definition matrix.hxx:17