IOLink  IOL_v1.1.0_release
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 
59  Vector(std::initializer_list<ValueType> init);
60 
66  Vector(const VectorX<ValueType>& other);
67 
76  Vector(const VectorX<ValueType>& other, ValueType padding);
77 
84  template <typename U>
85  explicit Vector(const Vector<U, N>& other)
86  {
87  for (size_t i = 0; i < N; ++i)
88  {
89  m_data[i] = static_cast<ValueType>(other[i]);
90  }
91  }
92 
93  Vector(const Vector& other) = default;
94  Vector& operator=(const Vector& other) = default;
95 
96  Vector(Vector&& other) noexcept = default;
97  Vector& operator=(Vector&& other) noexcept = default;
98 
102  size_t size() const { return N; }
103 
107  inline ValueType at(size_t index) const { return m_data[index]; }
108 
112  inline void setAt(size_t index, ValueType value) { m_data[index] = value; }
113 
114  inline ValueType operator[](size_t index) const { return m_data[index]; }
115  inline ValueType& operator[](size_t index) { return m_data[index]; }
116 
122  double squaredLength() const;
123 
129  double length() const;
130 
134  void normalize();
135 
136  Iterator begin() noexcept { return m_data.begin(); }
137  ConstIterator begin() const noexcept { return m_data.begin(); }
138  Iterator end() noexcept { return m_data.end(); }
139  ConstIterator end() const noexcept { return m_data.end(); }
140 
141  bool operator==(const Vector& other) const;
142  bool operator!=(const Vector& other) const;
143 
144  std::string toString() const;
145 
146  // ==================== //
147  // Arithmetic operators //
148  // ==================== //
149 
150  Vector operator-() const;
151 
152  Vector& operator+=(const Vector& v);
153  Vector& operator-=(const Vector& v);
154 
155  inline Vector operator+(const Vector& v) const { return Vector(*this) += v; }
156  inline Vector operator-(const Vector& v) const { return Vector(*this) -= v; }
157 
158  Vector& operator*=(ValueType value);
159  Vector& operator/=(ValueType value);
160 
161  inline Vector operator*(ValueType v) const { return Vector(*this) *= v; }
162  inline Vector operator/(ValueType v) const { return Vector(*this) /= v; }
163 
164  friend inline Vector operator*(ValueType value, Vector v) { return v *= value; }
165 
169  ValueType dot(const Vector& v) const;
170 
176  Vector cross(const Vector& v) const;
177 
178  // ==================== //
179  // GLSL style operators //
180  // ==================== //
181 
182  Vector& operator*=(const Vector& v);
183  Vector& operator/=(const Vector& v);
184 
185  inline Vector operator*(const Vector& v) const { return Vector(*this) *= v; }
186  inline Vector operator/(const Vector& v) const { return Vector(*this) /= v; }
187 
188  bool operator<(const Vector& other) const;
189  bool operator<=(const Vector& other) const;
190  bool operator>(const Vector& other) const;
191  bool operator>=(const Vector& other) const;
192 
193  operator VectorX<ValueType>() const;
194 
195 private:
196  std::array<ValueType, N> m_data;
197 };
198 
199 template <typename T, size_t N>
200 inline std::ostream&
201 operator<<(std::ostream& os, const Vector<T, N>& v)
202 {
203  os << v.toString();
204  return os;
205 }
206 
207 // ===================== //
208 // Template declarations //
209 // ===================== //
210 
211 //------------
212 // 2D
213 
214 extern template class IOLINK_API_IMPORT Vector<int8_t, 2>;
215 extern template class IOLINK_API_IMPORT Vector<int16_t, 2>;
216 extern template class IOLINK_API_IMPORT Vector<int32_t, 2>;
217 extern template class IOLINK_API_IMPORT Vector<int64_t, 2>;
218 
219 extern template class IOLINK_API_IMPORT Vector<uint8_t, 2>;
220 extern template class IOLINK_API_IMPORT Vector<uint16_t, 2>;
221 extern template class IOLINK_API_IMPORT Vector<uint32_t, 2>;
222 extern template class IOLINK_API_IMPORT Vector<uint64_t, 2>;
223 
224 extern template class IOLINK_API_IMPORT Vector<float, 2>;
225 extern template class IOLINK_API_IMPORT Vector<double, 2>;
226 
227 //------------
228 // 3D
229 
230 extern template class IOLINK_API_IMPORT Vector<int8_t, 3>;
231 extern template class IOLINK_API_IMPORT Vector<int16_t, 3>;
232 extern template class IOLINK_API_IMPORT Vector<int32_t, 3>;
233 extern template class IOLINK_API_IMPORT Vector<int64_t, 3>;
234 
235 extern template class IOLINK_API_IMPORT Vector<uint8_t, 3>;
236 extern template class IOLINK_API_IMPORT Vector<uint16_t, 3>;
237 extern template class IOLINK_API_IMPORT Vector<uint32_t, 3>;
238 extern template class IOLINK_API_IMPORT Vector<uint64_t, 3>;
239 
240 extern template class IOLINK_API_IMPORT Vector<float, 3>;
241 extern template class IOLINK_API_IMPORT Vector<double, 3>;
242 
243 //------------
244 // 3D
245 
246 extern template class IOLINK_API_IMPORT Vector<int8_t, 4>;
247 extern template class IOLINK_API_IMPORT Vector<int16_t, 4>;
248 extern template class IOLINK_API_IMPORT Vector<int32_t, 4>;
249 extern template class IOLINK_API_IMPORT Vector<int64_t, 4>;
250 
251 extern template class IOLINK_API_IMPORT Vector<uint8_t, 4>;
252 extern template class IOLINK_API_IMPORT Vector<uint16_t, 4>;
253 extern template class IOLINK_API_IMPORT Vector<uint32_t, 4>;
254 extern template class IOLINK_API_IMPORT Vector<uint64_t, 4>;
255 
256 extern template class IOLINK_API_IMPORT Vector<float, 4>;
257 extern template class IOLINK_API_IMPORT Vector<double, 4>;
258 
259 //==================== //
260 // Aliases declaration //
261 //==================== //
262 
263 //------------
264 // 2D
265 
276 
277 //------------
278 // 3D
279 
290 
291 //------------
292 // 4D
293 
304 
305 } // end namespace iolink