IOLink 1.11.0
Loading...
Searching...
No Matches
Vector.h
1#pragma once
2
3#include <iolink/IOLinkAPI.h>
4#include <iolink/VectorX.h>
5
6#include <array>
7#include <cstdint>
8#include <initializer_list>
9#include <iostream>
10
11namespace iolink
12{
13
35template <typename T, size_t N>
36class Vector final
37{
38public:
39 using ValueType = T;
40
41 using Iterator = typename std::array<T, N>::iterator;
42 using ConstIterator = typename std::array<T, N>::const_iterator;
43
49 static Vector createUniform(ValueType value);
50
55
61 Vector(std::initializer_list<ValueType> init);
62
69
78 Vector(const VectorX<ValueType>& other, ValueType padding);
79
86 template <typename U>
87 explicit Vector(const Vector<U, N>& other)
88 {
89 for (size_t i = 0; i < N; ++i)
90 {
91 m_data[i] = static_cast<ValueType>(other[i]);
92 }
93 }
94
95 Vector(const Vector& other) = default;
96 Vector& operator=(const Vector& other) = default;
97
98 Vector(Vector&& other) noexcept = default;
99 Vector& operator=(Vector&& other) noexcept = default;
100
104 size_t size() const { return N; }
105
109 inline ValueType at(size_t index) const { return m_data[index]; }
110
114 inline void setAt(size_t index, ValueType value) { m_data[index] = value; }
115
116 inline ValueType operator[](size_t index) const { return m_data[index]; }
117 inline ValueType& operator[](size_t index) { return m_data[index]; }
118
122 inline ValueType* data() { return m_data.data(); }
123
127 inline const ValueType* data() const { return m_data.data(); }
128
134 double squaredLength() const;
135
141 double length() const;
142
146 void normalize();
147
148 Iterator begin() noexcept { return m_data.begin(); }
149 ConstIterator begin() const noexcept { return m_data.begin(); }
150 Iterator end() noexcept { return m_data.end(); }
151 ConstIterator end() const noexcept { return m_data.end(); }
152
153 bool operator==(const Vector& other) const;
154 bool operator!=(const Vector& other) const;
155
156 std::string toString() const;
157
158 // ==================== //
159 // Arithmetic operators //
160 // ==================== //
161
162 Vector operator-() const;
163
164 Vector& operator+=(const Vector& v);
165 Vector& operator-=(const Vector& v);
166
167 inline Vector operator+(const Vector& v) const { return Vector(*this) += v; }
168 inline Vector operator-(const Vector& v) const { return Vector(*this) -= v; }
169
170 Vector& operator*=(ValueType value);
171 Vector& operator/=(ValueType value);
172
173 inline Vector operator*(ValueType v) const { return Vector(*this) *= v; }
174 inline Vector operator/(ValueType v) const { return Vector(*this) /= v; }
175
176 friend inline Vector operator*(ValueType value, Vector v) { return v *= value; }
177
181 ValueType dot(const Vector& v) const;
182
188 Vector cross(const Vector& v) const;
189
190 // ==================== //
191 // GLSL style operators //
192 // ==================== //
193
194 Vector& operator*=(const Vector& v);
195 Vector& operator/=(const Vector& v);
196
197 inline Vector operator*(const Vector& v) const { return Vector(*this) *= v; }
198 inline Vector operator/(const Vector& v) const { return Vector(*this) /= v; }
199
200 bool operator<(const Vector& other) const;
201 bool operator<=(const Vector& other) const;
202 bool operator>(const Vector& other) const;
203 bool operator>=(const Vector& other) const;
204
205 operator VectorX<ValueType>() const;
206
207private:
208 std::array<ValueType, N> m_data;
209};
210
211template <typename T, size_t N>
212inline std::ostream&
213operator<<(std::ostream& os, const Vector<T, N>& v)
214{
215 os << v.toString();
216 return os;
217}
218
219// ===================== //
220// Template declarations //
221// ===================== //
222
223//------------
224// 2D
225
226extern template class IOLINK_API_IMPORT Vector<int8_t, 2>;
227extern template class IOLINK_API_IMPORT Vector<int16_t, 2>;
228extern template class IOLINK_API_IMPORT Vector<int32_t, 2>;
229extern template class IOLINK_API_IMPORT Vector<int64_t, 2>;
230
231extern template class IOLINK_API_IMPORT Vector<uint8_t, 2>;
232extern template class IOLINK_API_IMPORT Vector<uint16_t, 2>;
233extern template class IOLINK_API_IMPORT Vector<uint32_t, 2>;
234extern template class IOLINK_API_IMPORT Vector<uint64_t, 2>;
235
236extern template class IOLINK_API_IMPORT Vector<float, 2>;
237extern template class IOLINK_API_IMPORT Vector<double, 2>;
238
239//------------
240// 3D
241
242extern template class IOLINK_API_IMPORT Vector<int8_t, 3>;
243extern template class IOLINK_API_IMPORT Vector<int16_t, 3>;
244extern template class IOLINK_API_IMPORT Vector<int32_t, 3>;
245extern template class IOLINK_API_IMPORT Vector<int64_t, 3>;
246
247extern template class IOLINK_API_IMPORT Vector<uint8_t, 3>;
248extern template class IOLINK_API_IMPORT Vector<uint16_t, 3>;
249extern template class IOLINK_API_IMPORT Vector<uint32_t, 3>;
250extern template class IOLINK_API_IMPORT Vector<uint64_t, 3>;
251
252extern template class IOLINK_API_IMPORT Vector<float, 3>;
253extern template class IOLINK_API_IMPORT Vector<double, 3>;
254
255//------------
256// 3D
257
258extern template class IOLINK_API_IMPORT Vector<int8_t, 4>;
259extern template class IOLINK_API_IMPORT Vector<int16_t, 4>;
260extern template class IOLINK_API_IMPORT Vector<int32_t, 4>;
261extern template class IOLINK_API_IMPORT Vector<int64_t, 4>;
262
263extern template class IOLINK_API_IMPORT Vector<uint8_t, 4>;
264extern template class IOLINK_API_IMPORT Vector<uint16_t, 4>;
265extern template class IOLINK_API_IMPORT Vector<uint32_t, 4>;
266extern template class IOLINK_API_IMPORT Vector<uint64_t, 4>;
267
268extern template class IOLINK_API_IMPORT Vector<float, 4>;
269extern template class IOLINK_API_IMPORT Vector<double, 4>;
270
271//==================== //
272// Aliases declaration //
273//==================== //
274
275//------------
276// 2D
277
288
289//------------
290// 3D
291
302
303//------------
304// 4D
305
316
317} // end namespace iolink