DNA Calib 1.1
Project brief
Vec.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "tdm/Types.h"
6
7namespace tdm {
8
9template<dim_t L, typename T>
10struct vec {
11 using value_type = T;
12
13 static constexpr dim_t dimensions() {
14 return L;
15 }
16
18
19 vec() : values{} {
20 }
21
22 ~vec() = default;
23
24 vec(const vec& rhs) = default;
25 vec& operator=(const vec& rhs) = default;
26
27 vec(vec&& rhs) = default;
28 vec& operator=(vec&& rhs) = default;
29
30 template<typename U>
31 vec(const vec<L, U>& rhs) {
32 std::copy(std::begin(rhs.values), std::end(rhs.values), std::begin(values));
33 }
34
35 template<typename U>
36 vec& operator=(const vec<L, U>& rhs) {
37 std::copy(std::begin(rhs.values), std::end(rhs.values), std::begin(values));
38 return *this;
39 }
40
41 template<typename ... Vs, typename std::enable_if<(sizeof...(Vs) == L) || (sizeof...(Vs) == 0)>::type* = nullptr>
42 vec(Vs... vs) : values{vs ...} {
43 }
44
45 template<typename U, typename ..., typename std::enable_if<std::is_convertible<U, T>::value && (L > 1)>::type * = nullptr>
46 explicit vec(U v) {
47 std::fill_n(values, dimensions(), v);
48 }
49
50 template<typename U, typename ..., typename std::enable_if<std::is_convertible<U, T>::value>::type* = nullptr>
51 explicit vec(U* pv) {
52 std::copy_n(pv, dimensions(), std::begin(values));
53 }
54
55 T& operator[](dim_t index) {
56 assert(index < dimensions());
57 return values[index];
58 }
59
60 const T& operator[](dim_t index) const {
61 assert(index < dimensions());
62 return values[index];
63 }
64
65 struct Vectorized;
66
67 template<typename F>
68 vec& apply(F func) {
69 for (dim_t i{}; i < dimensions(); ++i) {
70 func(values[i], i);
71 }
72 return *this;
73 }
74
75 template<typename F>
76 const vec& apply(F func) const {
77 for (dim_t i{}; i < dimensions(); ++i) {
78 func(values[i], i);
79 }
80 return *this;
81 }
82
84 return apply([](T& v, dim_t /*unused*/) {
85 ++v;
86 });
87 }
88
90 return apply([](T& v, dim_t /*unused*/) {
91 --v;
92 });
93 }
94
95 template<typename U>
96 vec& operator+=(U rhs) {
97 return apply([rhs](T& v, dim_t /*unused*/) {
98 v += rhs;
99 });
100 }
101
102 template<typename U>
103 vec& operator+=(const vec<L, U>& rhs) {
104 return apply([&rhs](T& v, dim_t i) {
105 v += rhs[i];
106 });
107 }
108
109 vec& operator+=(const vec& rhs) {
110 return operator+=<T>(rhs);
111 }
112
113 template<typename U>
114 vec& operator-=(U rhs) {
115 return apply([rhs](T& v, dim_t /*unused*/) {
116 v -= rhs;
117 });
118 }
119
120 template<typename U>
121 vec& operator-=(const vec<L, U>& rhs) {
122 return apply([&rhs](T& v, dim_t i) {
123 v -= rhs[i];
124 });
125 }
126
127 vec& operator-=(const vec& rhs) {
128 return operator-=<T>(rhs);
129 }
130
131 template<typename U>
132 vec& operator*=(U rhs) {
133 return apply([rhs](T& v, dim_t /*unused*/) {
134 v *= rhs;
135 });
136 }
137
138 template<typename U>
139 vec& operator*=(const vec<L, U>& rhs) {
140 return apply([&rhs](T& v, dim_t i) {
141 v *= rhs[i];
142 });
143 }
144
145 vec& operator*=(const vec& rhs) {
146 return operator*=<T>(rhs);
147 }
148
149 template<typename U>
150 vec& operator/=(U rhs) {
151 return apply([rhs](T& v, dim_t /*unused*/) {
152 v /= rhs;
153 });
154 }
155
156 template<typename U>
157 vec& operator/=(const vec<L, U>& rhs) {
158 return apply([&rhs](T& v, dim_t i) {
159 v /= rhs[i];
160 });
161 }
162
163 vec& operator/=(const vec& rhs) {
164 return operator/=<T>(rhs);
165 }
166
167 template<typename ..., typename V = T>
168 typename std::enable_if<std::is_floating_point<V>::value, V>::type length() const {
169 const auto& v = *this;
170 return std::sqrt((v * v).sum());
171 }
172
173 template<typename ..., typename V = T>
174 typename std::enable_if<std::is_floating_point<V>::value, vec&>::type normalize() {
175 return operator/=(length());
176 }
177
179 return apply([](T& v, dim_t /*unused*/) {
180 v = -v;
181 });
182 }
183
184 T sum() const {
185 T retval{};
186 apply([&retval](const T& v, dim_t /*unused*/) {
187 retval += v;
188 });
189 return retval;
190 }
191
192};
193
194template<dim_t L, typename T>
195inline bool operator==(const vec<L, T>& lhs, const vec<L, T>& rhs) {
196 bool equal = true;
197 lhs.apply([&equal, &rhs](const T& v, dim_t i) {
198 equal = equal && (v == rhs[i]);
199 });
200 return equal;
201}
202
203template<dim_t L, typename T>
204inline bool operator!=(const vec<L, T>& lhs, const vec<L, T>& rhs) {
205 return !(lhs == rhs);
206}
207
208template<dim_t L, typename T>
209inline vec<L, T> operator+(const vec<L, T>& v) {
210 return v;
211}
212
213template<dim_t L, typename T>
215 return v.negate();
216}
217
218template<dim_t L, typename T, typename U>
219inline vec<L, T> operator+(const vec<L, T>& lhs, const vec<L, U>& rhs) {
220 return vec<L, T>(lhs) += rhs;
221}
222
223template<dim_t L, typename T, typename U>
224inline vec<L, T> operator+(const vec<L, T>& lhs, U rhs) {
225 return vec<L, T>(lhs) += rhs;
226}
227
228template<dim_t L, typename T, typename U>
229inline vec<L, T> operator+(T lhs, const vec<L, U>& rhs) {
230 return vec<L, T>(lhs) += rhs;
231}
232
233template<dim_t L, typename T, typename U>
234inline vec<L, T> operator-(const vec<L, T>& lhs, const vec<L, U>& rhs) {
235 return vec<L, T>(lhs) -= rhs;
236}
237
238template<dim_t L, typename T, typename U>
239inline vec<L, T> operator-(const vec<L, T>& lhs, U rhs) {
240 return vec<L, T>(lhs) -= rhs;
241}
242
243template<dim_t L, typename T, typename U>
244inline vec<L, T> operator-(T lhs, const vec<L, U>& rhs) {
245 return vec<L, T>(lhs) -= rhs;
246}
247
248template<dim_t L, typename T, typename U>
249inline vec<L, T> operator*(const vec<L, T>& lhs, const vec<L, U>& rhs) {
250 return vec<L, T>(lhs) *= rhs;
251}
252
253template<dim_t L, typename T, typename U>
254inline typename std::enable_if<std::is_arithmetic<U>::value, vec<L, T> >::type operator*(const vec<L, T>& lhs, U rhs) {
255 return vec<L, T>(lhs) *= rhs;
256}
257
258template<dim_t L, typename T, typename U>
259inline typename std::enable_if<std::is_arithmetic<T>::value, vec<L, T> >::type operator*(T lhs, const vec<L, U>& rhs) {
260 return vec<L, T>(lhs) *= rhs;
261}
262
263template<dim_t L, typename T, typename U>
264inline vec<L, T> operator/(const vec<L, T>& lhs, const vec<L, U>& rhs) {
265 return vec<L, T>(lhs) /= rhs;
266}
267
268template<dim_t L, typename T, typename U>
269inline typename std::enable_if<std::is_arithmetic<U>::value, vec<L, T> >::type operator/(const vec<L, T>& lhs, U rhs) {
270 return vec<L, T>(lhs) /= rhs;
271}
272
273template<dim_t L, typename T, typename U>
274inline typename std::enable_if<std::is_arithmetic<T>::value, vec<L, T> >::type operator/(T lhs, const vec<L, U>& rhs) {
275 return vec<L, T>(lhs) /= rhs;
276}
277
278} // namespace tdm
void copy(const TSource &source, TDestination &destination)
Definition: utils/Extd.h:123
Definition: Computations.h:19
std::size_t dim_t
Definition: Types.h:22
bool operator!=(const mat< R, C, T > &lhs, const mat< R, C, T > &rhs)
Definition: Mat.h:285
bool operator==(const mat< R, C, T > &lhs, const mat< R, C, T > &rhs)
Definition: Mat.h:275
mat< R, C, T > operator*(const mat< R, C, T > &lhs, T rhs)
Definition: Mat.h:332
mat< R, C, T > operator/(const mat< R, C, T > &lhs, T rhs)
Definition: Mat.h:373
mat< R, C, T > operator-(const mat< R, C, T > &m)
Definition: Mat.h:295
mat< R, C, T > operator+(const mat< R, C, T > &m)
Definition: Mat.h:290
Definition: Vec.h:10
static constexpr dim_t dimensions()
Definition: Vec.h:13
vec(Vs... vs)
Definition: Vec.h:42
vec & apply(F func)
Definition: Vec.h:68
vec & operator/=(const vec &rhs)
Definition: Vec.h:163
vec & operator/=(U rhs)
Definition: Vec.h:150
std::enable_if< std::is_floating_point< V >::value, V >::type length() const
Definition: Vec.h:168
vec(U *pv)
Definition: Vec.h:51
T value_type
Definition: Vec.h:11
vec & negate()
Definition: Vec.h:178
T & operator[](dim_t index)
Definition: Vec.h:55
vec & operator-=(const vec< L, U > &rhs)
Definition: Vec.h:121
vec & operator--()
Definition: Vec.h:89
vec & operator=(vec &&rhs)=default
vec & operator*=(const vec< L, U > &rhs)
Definition: Vec.h:139
vec(const vec &rhs)=default
vec & operator/=(const vec< L, U > &rhs)
Definition: Vec.h:157
vec & operator-=(const vec &rhs)
Definition: Vec.h:127
vec & operator+=(const vec< L, U > &rhs)
Definition: Vec.h:103
vec & operator*=(const vec &rhs)
Definition: Vec.h:145
vec & operator++()
Definition: Vec.h:83
vec(vec &&rhs)=default
vec(U v)
Definition: Vec.h:46
const vec & apply(F func) const
Definition: Vec.h:76
vec & operator=(const vec< L, U > &rhs)
Definition: Vec.h:36
vec & operator-=(U rhs)
Definition: Vec.h:114
vec & operator*=(U rhs)
Definition: Vec.h:132
vec()
Definition: Vec.h:19
vec & operator=(const vec &rhs)=default
~vec()=default
std::enable_if< std::is_floating_point< V >::value, vec & >::type normalize()
Definition: Vec.h:174
T sum() const
Definition: Vec.h:184
vec & operator+=(U rhs)
Definition: Vec.h:96
vec & operator+=(const vec &rhs)
Definition: Vec.h:109
vec(const vec< L, U > &rhs)
Definition: Vec.h:31
const T & operator[](dim_t index) const
Definition: Vec.h:60
value_type values[L]
Definition: Vec.h:17