IOLink  IOL_v1.6.1_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 
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 
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 
197 private:
198  std::array<ValueType, N> m_data;
199 };
200 
201 template <typename T, size_t N>
202 inline std::ostream&
203 operator<<(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 
216 extern template class IOLINK_API_IMPORT Vector<int8_t, 2>;
217 extern template class IOLINK_API_IMPORT Vector<int16_t, 2>;
218 extern template class IOLINK_API_IMPORT Vector<int32_t, 2>;
219 extern template class IOLINK_API_IMPORT Vector<int64_t, 2>;
220 
221 extern template class IOLINK_API_IMPORT Vector<uint8_t, 2>;
222 extern template class IOLINK_API_IMPORT Vector<uint16_t, 2>;
223 extern template class IOLINK_API_IMPORT Vector<uint32_t, 2>;
224 extern template class IOLINK_API_IMPORT Vector<uint64_t, 2>;
225 
226 extern template class IOLINK_API_IMPORT Vector<float, 2>;
227 extern template class IOLINK_API_IMPORT Vector<double, 2>;
228 
229 //------------
230 // 3D
231 
232 extern template class IOLINK_API_IMPORT Vector<int8_t, 3>;
233 extern template class IOLINK_API_IMPORT Vector<int16_t, 3>;
234 extern template class IOLINK_API_IMPORT Vector<int32_t, 3>;
235 extern template class IOLINK_API_IMPORT Vector<int64_t, 3>;
236 
237 extern template class IOLINK_API_IMPORT Vector<uint8_t, 3>;
238 extern template class IOLINK_API_IMPORT Vector<uint16_t, 3>;
239 extern template class IOLINK_API_IMPORT Vector<uint32_t, 3>;
240 extern template class IOLINK_API_IMPORT Vector<uint64_t, 3>;
241 
242 extern template class IOLINK_API_IMPORT Vector<float, 3>;
243 extern template class IOLINK_API_IMPORT Vector<double, 3>;
244 
245 //------------
246 // 3D
247 
248 extern template class IOLINK_API_IMPORT Vector<int8_t, 4>;
249 extern template class IOLINK_API_IMPORT Vector<int16_t, 4>;
250 extern template class IOLINK_API_IMPORT Vector<int32_t, 4>;
251 extern template class IOLINK_API_IMPORT Vector<int64_t, 4>;
252 
253 extern template class IOLINK_API_IMPORT Vector<uint8_t, 4>;
254 extern template class IOLINK_API_IMPORT Vector<uint16_t, 4>;
255 extern template class IOLINK_API_IMPORT Vector<uint32_t, 4>;
256 extern template class IOLINK_API_IMPORT Vector<uint64_t, 4>;
257 
258 extern template class IOLINK_API_IMPORT Vector<float, 4>;
259 extern 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