Air-Trap 1.0.0
A multiplayer R-Type clone game engine built with C++23 and ECS architecture
Loading...
Searching...
No Matches
VecBase.tpp
Go to the documentation of this file.
1/*
2** EPITECH PROJECT, 2025
3** R-Type
4** File description:
5** VecBase.tpp, CRTP base vector class implementation
6*/
7
8/*
9** MIT License
10**
11** Copyright (c) 2025 Robin Toillon
12**
13** Permission is hereby granted, free of charge, to any person obtaining
14** a copy of this software and associated documentation files (the
15** "Software"), to deal in the Software without restriction, including
16** without limitation the rights to use, copy, modify, merge, publish,
17** distribute, sublicense, and/or sell copies of the Software, and to
18** permit persons to whom the Software is furnished to do so, subject to
19** the following conditions:
20**
21** The above copyright notice and this permission notice shall be
22** included in all copies or substantial portions of the Software.
23**
24** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
27** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
28** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
29** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
30** SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31*/
32
33/**
34 * @file VecBase.tpp
35 * @brief Implementation of the CRTP base vector class.
36 * @author Robin Toillon
37 */
38
39namespace rtp::details
40{
41 ///////////////////////////////////////////////////////////////////////////
42 // Math Operations
43 ///////////////////////////////////////////////////////////////////////////
44
45 template <Numeric T, std::size_t N>
46 template <typename Self>
47 constexpr auto VecBase<T, N>::squaredDistance(this const Self &self,
48 const std::remove_cvref_t<Self> &other) noexcept
49 -> T
50 {
51 T sum = T{0};
52
53 for (std::size_t i = 0; i < N; ++i) {
54 T diff = self[i] - other[i];
55 sum += diff * diff;
56 }
57
58 return sum;
59 }
60
61 template <Numeric T, std::size_t N>
62 template <typename Self>
63 constexpr auto VecBase<T, N>::distance(this const Self &self,
64 const std::remove_cvref_t<Self> &other) noexcept
65 -> RealT
66 {
67 return static_cast<RealT>(std::sqrt(self.squaredDistance(other)));
68 }
69
70 template <Numeric T, std::size_t N>
71 template <typename Self>
72 constexpr auto VecBase<T, N>::squaredLength(this const Self &self) noexcept
73 -> T
74 {
75 T sum = T{0};
76
77 for (std::size_t i = 0; i < N; ++i)
78 sum += self[i] * self[i];
79
80 return sum;
81 }
82
83 template <Numeric T, std::size_t N>
84 template <typename Self>
85 constexpr auto VecBase<T, N>::length(this const Self &self) noexcept
86 -> RealT
87 {
88 return static_cast<RealT>(std::sqrt(self.squaredLength()));
89 }
90
91 template <Numeric T, std::size_t N>
92 template <typename Self>
93 constexpr auto VecBase<T, N>::normalize(this Self &self) noexcept
94 -> Self &
95 requires std::floating_point<T>
96 {
97 T len = self.length();
98
99 if (len != T{0})
100 for (std::size_t i = 0; i < N; ++i)
101 self[i] /= len;
102
103 return self;
104 }
105
106 template <Numeric T, std::size_t N>
107 template <typename Self>
108 constexpr auto VecBase<T, N>::normalized(this const Self &self) noexcept
109 -> std::remove_cvref_t<Self>
110 requires std::floating_point<T>
111 {
112 std::remove_cvref_t<Self> copy = self;
113 return copy.normalize();
114 }
115
116 template <Numeric T, std::size_t N>
117 template <typename Self>
118 constexpr auto VecBase<T, N>::dot(this const Self &self,
119 const std::remove_cvref_t<Self>
120 &other) noexcept
121 -> T
122 {
123 T result = T{0};
124
125 for (std::size_t i = 0; i < N; ++i)
126 result += self[i] * other[i];
127
128 return result;
129 }
130
131 template <Numeric T, std::size_t N>
132 template <typename Self>
133 constexpr auto VecBase<T, N>::project(this const Self &self,
134 const std::remove_cvref_t<Self> &other) noexcept
135 -> std::remove_cvref_t<Self>
136 requires std::floating_point<T>
137 {
138 using VecType = std::remove_cvref_t<Self>;
139
140 T otherSquaredLength = other.squaredLength();
141 if (otherSquaredLength == T{0})
142 return VecType{};
143
144 T scalar = self.dot(other) / otherSquaredLength;
145 VecType result;
146 for (std::size_t i = 0; i < N; ++i)
147 result[i] = other[i] * scalar;
148
149 return result;
150 }
151
152 ///////////////////////////////////////////////////////////////////////////
153 // Vector & Vector operations (Member Operators)
154 ///////////////////////////////////////////////////////////////////////////
155
156 template <Numeric T, std::size_t N>
157 template <typename Self>
158 constexpr auto &VecBase<T, N>::operator+=(this Self &self,
159 const std::remove_cvref_t<Self> &other) noexcept
160 {
161 for (std::size_t i = 0; i < N; ++i)
162 self[i] += other[i];
163 return self;
164 }
165
166 template <Numeric T, std::size_t N>
167 template <typename Self>
168 constexpr auto &VecBase<T, N>::operator-=(this Self &self,
169 const std::remove_cvref_t<Self> &other) noexcept
170 {
171 for (std::size_t i = 0; i < N; ++i)
172 self[i] -= other[i];
173 return self;
174 }
175
176 template <Numeric T, std::size_t N>
177 template <typename Self>
178 constexpr auto &VecBase<T, N>::operator*=(this Self &self,
179 const std::remove_cvref_t<Self> &other) noexcept
180 {
181 for (std::size_t i = 0; i < N; ++i)
182 self[i] *= other[i];
183 return self;
184 }
185
186 template <Numeric T, std::size_t N>
187 template <typename Self>
188 constexpr auto &VecBase<T, N>::operator/=(this Self &self,
189 const std::remove_cvref_t<Self> &other) noexcept
190 {
191 for (std::size_t i = 0; i < N; ++i)
192 self[i] /= other[i];
193 return self;
194 }
195
196 ///////////////////////////////////////////////////////////////////////////
197 // Vector & Scalar operations (Member Operators)
198 ///////////////////////////////////////////////////////////////////////////
199
200 template <Numeric T, std::size_t N>
201 template <typename Self>
202 constexpr auto &VecBase<T, N>::operator+=(this Self &self, T scalar) noexcept
203 {
204 for (std::size_t i = 0; i < N; ++i)
205 self[i] += scalar;
206 return self;
207 }
208
209 template <Numeric T, std::size_t N>
210 template <typename Self>
211 constexpr auto &VecBase<T, N>::operator-=(this Self &self, T scalar) noexcept
212 {
213 for (std::size_t i = 0; i < N; ++i)
214 self[i] -= scalar;
215 return self;
216 }
217
218 template <Numeric T, std::size_t N>
219 template <typename Self>
220 constexpr auto &VecBase<T, N>::operator*=(this Self &self, T scalar) noexcept
221 {
222 for (std::size_t i = 0; i < N; ++i)
223 self[i] *= scalar;
224 return self;
225 }
226
227 template <Numeric T, std::size_t N>
228 template <typename Self>
229 constexpr auto &VecBase<T, N>::operator/=(this Self &self, T scalar) noexcept
230 {
231 for (std::size_t i = 0; i < N; ++i)
232 self[i] /= scalar;
233 return self;
234 }
235}