| 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
11 namespace iolink
12{
13
35 template <typename T, size_t N>
36 class Vector final
37{
38 public:
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
54 Vector();
55
61 Vector(std::initializer_list<ValueType> init);
62
68 Vector(const VectorX<ValueType>& other);
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
207 private:
208 std::array<ValueType, N> m_data;
209};
210
211 template <typename T, size_t N>
212 inline 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
226 extern template class IOLINK_API_IMPORT Vector<int8_t, 2>;
227 extern template class IOLINK_API_IMPORT Vector<int16_t, 2>;
228 extern template class IOLINK_API_IMPORT Vector<int32_t, 2>;
229 extern template class IOLINK_API_IMPORT Vector<int64_t, 2>;
230
231 extern template class IOLINK_API_IMPORT Vector<uint8_t, 2>;
232 extern template class IOLINK_API_IMPORT Vector<uint16_t, 2>;
233 extern template class IOLINK_API_IMPORT Vector<uint32_t, 2>;
234 extern template class IOLINK_API_IMPORT Vector<uint64_t, 2>;
235
236 extern template class IOLINK_API_IMPORT Vector<float, 2>;
237 extern template class IOLINK_API_IMPORT Vector<double, 2>;
238
239//------------
240// 3D
241
242 extern template class IOLINK_API_IMPORT Vector<int8_t, 3>;
243 extern template class IOLINK_API_IMPORT Vector<int16_t, 3>;
244 extern template class IOLINK_API_IMPORT Vector<int32_t, 3>;
245 extern template class IOLINK_API_IMPORT Vector<int64_t, 3>;
246
247 extern template class IOLINK_API_IMPORT Vector<uint8_t, 3>;
248 extern template class IOLINK_API_IMPORT Vector<uint16_t, 3>;
249 extern template class IOLINK_API_IMPORT Vector<uint32_t, 3>;
250 extern template class IOLINK_API_IMPORT Vector<uint64_t, 3>;
251
252 extern template class IOLINK_API_IMPORT Vector<float, 3>;
253 extern template class IOLINK_API_IMPORT Vector<double, 3>;
254
255//------------
256// 3D
257
258 extern template class IOLINK_API_IMPORT Vector<int8_t, 4>;
259 extern template class IOLINK_API_IMPORT Vector<int16_t, 4>;
260 extern template class IOLINK_API_IMPORT Vector<int32_t, 4>;
261 extern template class IOLINK_API_IMPORT Vector<int64_t, 4>;
262
263 extern template class IOLINK_API_IMPORT Vector<uint8_t, 4>;
264 extern template class IOLINK_API_IMPORT Vector<uint16_t, 4>;
265 extern template class IOLINK_API_IMPORT Vector<uint32_t, 4>;
266 extern template class IOLINK_API_IMPORT Vector<uint64_t, 4>;
267
268 extern template class IOLINK_API_IMPORT Vector<float, 4>;
269 extern template class IOLINK_API_IMPORT Vector<double, 4>;
270
271//==================== //
272// Aliases declaration //
273//==================== //
274
275//------------
276// 2D
277
278 using Vector2u8 = Vector<uint8_t, 2>;
279 using Vector2u16 = Vector<uint16_t, 2>;
280 using Vector2u32 = Vector<uint32_t, 2>;
281 using Vector2u64 = Vector<uint64_t, 2>;
282 using Vector2i8 = Vector<int8_t, 2>;
283 using Vector2i16 = Vector<int16_t, 2>;
284 using Vector2i32 = Vector<int32_t, 2>;
285 using Vector2i64 = Vector<int64_t, 2>;
286 using Vector2f = Vector<float, 2>;
287 using Vector2d = Vector<double, 2>;
288
289//------------
290// 3D
291
292 using Vector3u8 = Vector<uint8_t, 3>;
293 using Vector3u16 = Vector<uint16_t, 3>;
294 using Vector3u32 = Vector<uint32_t, 3>;
295 using Vector3u64 = Vector<uint64_t, 3>;
296 using Vector3i8 = Vector<int8_t, 3>;
297 using Vector3i16 = Vector<int16_t, 3>;
298 using Vector3i32 = Vector<int32_t, 3>;
299 using Vector3i64 = Vector<int64_t, 3>;
300 using Vector3f = Vector<float, 3>;
301 using Vector3d = Vector<double, 3>;
302
303//------------
304// 4D
305
306 using Vector4u8 = Vector<uint8_t, 4>;
307 using Vector4u16 = Vector<uint16_t, 4>;
308 using Vector4u32 = Vector<uint32_t, 4>;
309 using Vector4u64 = Vector<uint64_t, 4>;
310 using Vector4i8 = Vector<int8_t, 4>;
311 using Vector4i16 = Vector<int16_t, 4>;
312 using Vector4i32 = Vector<int32_t, 4>;
313 using Vector4i64 = Vector<int64_t, 4>;
314 using Vector4f = Vector<float, 4>;
315 using Vector4d = Vector<double, 4>;
316
317} // end namespace iolink
iolink::Vector
An arithmetic vector.
Definition Vector.h:37
iolink::Vector::Vector
Vector(std::initializer_list< ValueType > init)
Create a vector with the values contained in a bracket list.
iolink::Vector::size
size_t size() const
Returns the size of the vector, which also correspond to vector number of dimension.
Definition Vector.h:104
iolink::Vector::length
double length() const
Returns the vector's norm.
iolink::Vector::dot
ValueType dot(const Vector &v) const
Dot product of two vectors.
iolink::Vector::Vector
Vector()
Create a vector with unitialized values.
iolink::Vector::Vector
Vector(const VectorX< ValueType > &other, ValueType padding)
Build a vector from a VectorX of same type.
iolink::Vector::data
const ValueType * data() const
Direct const access to the vector's data.
Definition Vector.h:127
iolink::Vector::Vector
Vector(const VectorX< ValueType > &other)
Build a Vector from a VectorX of same type.
iolink::Vector::Vector
Vector(const Vector< U, N > &other)
Build a vector from a vector storing another type.
Definition Vector.h:87
iolink::Vector::createUniform
static Vector createUniform(ValueType value)
Create a vector with the given value for all components.
iolink::Vector::squaredLength
double squaredLength() const
Returns the vector's norm, squared.
iolink::Vector::setAt
void setAt(size_t index, ValueType value)
Set value as given index.
Definition Vector.h:114
iolink::Vector::at
ValueType at(size_t index) const
Access vector's value at given index.
Definition Vector.h:109
iolink::Vector::normalize
void normalize()
Normalize the vector.
iolink::Vector::data
ValueType * data()
Direct access to the vector's data.
Definition Vector.h:122
iolink::Vector::cross
Vector cross(const Vector &v) const
Cross product of two vectors.
iolink::VectorX
A dynamically sized arithmetic vector.
Definition VectorX.h:18
iolink
All IOLink symbols are enclosed in this namespace.
Definition ArrayX.h:8