IOLink IOL_v1.8.0_release
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
124 double squaredLength() const;
125
131 double length() const;
132
136 void normalize();
137
138 Iterator begin() noexcept { return m_data.begin(); }
139 ConstIterator begin() const noexcept { return m_data.begin(); }
140 Iterator end() noexcept { return m_data.end(); }
141 ConstIterator end() const noexcept { return m_data.end(); }
142
143 bool operator==(const Vector& other) const;
144 bool operator!=(const Vector& other) const;
145
146 std::string toString() const;
147
148 // ==================== //
149 // Arithmetic operators //
150 // ==================== //
151
152 Vector operator-() const;
153
154 Vector& operator+=(const Vector& v);
155 Vector& operator-=(const Vector& v);
156
157 inline Vector operator+(const Vector& v) const { return Vector(*this) += v; }
158 inline Vector operator-(const Vector& v) const { return Vector(*this) -= v; }
159
160 Vector& operator*=(ValueType value);
161 Vector& operator/=(ValueType value);
162
163 inline Vector operator*(ValueType v) const { return Vector(*this) *= v; }
164 inline Vector operator/(ValueType v) const { return Vector(*this) /= v; }
165
166 friend inline Vector operator*(ValueType value, Vector v) { return v *= value; }
167
171 ValueType dot(const Vector& v) const;
172
178 Vector cross(const Vector& v) const;
179
180 // ==================== //
181 // GLSL style operators //
182 // ==================== //
183
184 Vector& operator*=(const Vector& v);
185 Vector& operator/=(const Vector& v);
186
187 inline Vector operator*(const Vector& v) const { return Vector(*this) *= v; }
188 inline Vector operator/(const Vector& v) const { return Vector(*this) /= v; }
189
190 bool operator<(const Vector& other) const;
191 bool operator<=(const Vector& other) const;
192 bool operator>(const Vector& other) const;
193 bool operator>=(const Vector& other) const;
194
195 operator VectorX<ValueType>() const;
196
197private:
198 std::array<ValueType, N> m_data;
199};
200
201template <typename T, size_t N>
202inline std::ostream&
203operator<<(std::ostream& os, const Vector<T, N>& v)
204{
205 os << v.toString();
206 return os;
207}
208
209// ===================== //
210// Template declarations //
211// ===================== //
212
213//------------
214// 2D
215
216extern template class IOLINK_API_IMPORT Vector<int8_t, 2>;
217extern template class IOLINK_API_IMPORT Vector<int16_t, 2>;
218extern template class IOLINK_API_IMPORT Vector<int32_t, 2>;
219extern template class IOLINK_API_IMPORT Vector<int64_t, 2>;
220
221extern template class IOLINK_API_IMPORT Vector<uint8_t, 2>;
222extern template class IOLINK_API_IMPORT Vector<uint16_t, 2>;
223extern template class IOLINK_API_IMPORT Vector<uint32_t, 2>;
224extern template class IOLINK_API_IMPORT Vector<uint64_t, 2>;
225
226extern template class IOLINK_API_IMPORT Vector<float, 2>;
227extern template class IOLINK_API_IMPORT Vector<double, 2>;
228
229//------------
230// 3D
231
232extern template class IOLINK_API_IMPORT Vector<int8_t, 3>;
233extern template class IOLINK_API_IMPORT Vector<int16_t, 3>;
234extern template class IOLINK_API_IMPORT Vector<int32_t, 3>;
235extern template class IOLINK_API_IMPORT Vector<int64_t, 3>;
236
237extern template class IOLINK_API_IMPORT Vector<uint8_t, 3>;
238extern template class IOLINK_API_IMPORT Vector<uint16_t, 3>;
239extern template class IOLINK_API_IMPORT Vector<uint32_t, 3>;
240extern template class IOLINK_API_IMPORT Vector<uint64_t, 3>;
241
242extern template class IOLINK_API_IMPORT Vector<float, 3>;
243extern template class IOLINK_API_IMPORT Vector<double, 3>;
244
245//------------
246// 3D
247
248extern template class IOLINK_API_IMPORT Vector<int8_t, 4>;
249extern template class IOLINK_API_IMPORT Vector<int16_t, 4>;
250extern template class IOLINK_API_IMPORT Vector<int32_t, 4>;
251extern template class IOLINK_API_IMPORT Vector<int64_t, 4>;
252
253extern template class IOLINK_API_IMPORT Vector<uint8_t, 4>;
254extern template class IOLINK_API_IMPORT Vector<uint16_t, 4>;
255extern template class IOLINK_API_IMPORT Vector<uint32_t, 4>;
256extern template class IOLINK_API_IMPORT Vector<uint64_t, 4>;
257
258extern template class IOLINK_API_IMPORT Vector<float, 4>;
259extern template class IOLINK_API_IMPORT Vector<double, 4>;
260
261//==================== //
262// Aliases declaration //
263//==================== //
264
265//------------
266// 2D
267
278
279//------------
280// 3D
281
292
293//------------
294// 4D
295
306
307} // end namespace iolink