Ocean
Loading...
Searching...
No Matches
Vector2.h
Go to the documentation of this file.
1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
8#ifndef META_OCEAN_MATH_VECTOR2_H
9#define META_OCEAN_MATH_VECTOR2_H
10
11#include "ocean/math/Math.h"
12#include "ocean/math/Numeric.h"
13
14#include <type_traits>
15#include <vector>
16
17namespace Ocean
18{
19
20// Forward declaration.
21template <typename T> class VectorT2;
22
23/**
24 * Definition of a 2D vector.
25 * @see VectorT2
26 * @ingroup math
27 */
29
30/**
31 * Definition of a 2D vector with double values.
32 * @see VectorT2
33 * @ingroup math
34 */
36
37/**
38 * Definition of a 2D vector with float values.
39 * @see VectorT2
40 * @ingroup math
41 */
43
44/**
45 * Definition of a 2D vector with integer values.
46 * @see VectorT2
47 * @ingroup math
48 */
50
51/**
52 * Definition of a typename alias for vectors with VectorT2 objects.
53 * @see VectorT2
54 * @ingroup math
55 */
56template <typename T>
57using VectorsT2 = std::vector<VectorT2<T>>;
58
59/**
60 * Definition of a vector holding Vector2 objects.
61 * @see Vector2
62 * @ingroup math
63 */
64typedef std::vector<Vector2> Vectors2;
65
66/**
67 * Definition of a vector holding VectorD2 objects.
68 * @see VectorD2
69 * @ingroup math
70 */
71typedef std::vector<VectorD2> VectorsD2;
72
73/**
74 * Definition of a vector holding VectorF2 objects.
75 * @see VectorF2
76 * @ingroup math
77 */
78typedef std::vector<VectorF2> VectorsF2;
79
80/**
81 * Definition of a vector holding VectorI2 objects.
82 * @see VectorI2
83 * @ingroup math
84 */
85typedef std::vector<VectorI2> VectorsI2;
86
87/**
88 * This class implements a vector with two elements.
89 * The element order is: (x, y).
90 * @tparam T Data type of the vector elements.
91 * @see Vector2, VectorF2, VectorF3.
92 * @ingroup math
93 */
94template <typename T>
96{
97 public:
98
99 /**
100 * Definition of the used data type.
101 */
102 typedef T Type;
103
104 public:
105
106 /**
107 * Creates a new 2D vector with undefined elements.
108 * Beware: The elements are neither zero nor a specific value!
109 * This is useful in situations where VectorT2 objects are created (e.g, by a large array or vector)
110 * and their values are assigned in a function afterwards.
111 */
112 inline VectorT2() noexcept;
113
114 /**
115 * Creates a new 2D vector.
116 * @param setToHomogeneous Determines whether a homogeneous vector (0, 1) will be created, otherwise the vector is initialized with zeros
117 */
118 inline explicit VectorT2(const bool setToHomogeneous) noexcept;
119
120 /**
121 * Creates a new 2D vector with specified coordinates.
122 * @param x X value
123 * @param y Y value
124 */
125 inline VectorT2(const T x, const T y) noexcept;
126
127 /**
128 * Creates a new 2D vector with a given array of elements.
129 * @param valueArray Array with at least two elements
130 */
131 inline explicit VectorT2(const T* valueArray) noexcept;
132
133 /**
134 * Copies a vector.
135 * @param vector 2D vector that is copied
136 */
137 inline VectorT2(const VectorT2<T>& vector) noexcept;
138
139 /**
140 * Copies a vector with different element data type than T.
141 * @param vector The 2D vector to copy
142 * @tparam U The element data type of the second vector
143 */
144 template <typename U>
145 inline explicit VectorT2(const VectorT2<U>& vector) noexcept;
146
147 /**
148 * Returns the cross product of two 2D vectors.
149 * The cross product is the resulting z-component of a cross product between two 3D vectors having 0 as z-component.
150 * @param vector Right vector
151 * @return Cross product
152 * @see perpendicular().
153 */
154 inline T cross(const VectorT2<T>& vector) const;
155
156 /**
157 * Returns a vector perpendicular to this vectors.
158 * The cross product between this vector and the resulting perpendicular vector will have a positive value.<br>
159 * @return Perpendicular vector with identical length as this vector
160 * @see cross().
161 */
162 inline VectorT2<T> perpendicular() const;
163
164 /**
165 * Returns the normalized vector.
166 * Beware: This function does not throw an exception if this vector cannot be normalized.<br>
167 * Thus, ensure that this vector is not zero before calling this function.<br>
168 * Or even better, use different normalization functions like: normalizedOrZero(), normalizedOrValue(), or normalize().<br>
169 * In case, the vector cannot be normalized, an uninitialized vector will be returned (due to performance reasons).
170 * @return This vector as unit vector (vector with length 1)
171 * @see normalizedOrZero(), normalizedOrValue(), normalize().
172 */
173 inline VectorT2<T> normalized() const;
174
175 /**
176 * Returns the normalized vector.
177 * If this vector cannot be normalized the zero vector is returned.
178 * @return Vector with length 1
179 */
180 inline VectorT2<T> normalizedOrZero() const;
181
182 /**
183 * Returns the normalized vector.
184 * If this vector cannot be normalized the given vector is returned.
185 * @param value Vector that will be returned if the vector cannot be normalized
186 * @return Vector with length 1
187 */
188 inline VectorT2<T> normalizedOrValue(const VectorT2<T>& value) const;
189
190 /**
191 * Normalizes this vector
192 * @return True, if the vector could be normalized
193 */
194 inline bool normalize();
195
196 /**
197 * Returns the length of the vector.
198 * @return Vector length
199 */
200 inline T length() const;
201
202 /**
203 * Returns the square of the vector length.
204 * @return Square of vector length
205 */
206 inline T sqr() const;
207
208 /**
209 * Returns the distance between this 2D position and a second 2D position.
210 * @param right Second 2D position
211 * @return Distance between the two points
212 */
213 inline T distance(const VectorT2<T>& right) const;
214
215 /**
216 * Returns the square distance between this 2D position and a second 2D position.
217 * @param right Second 2D position
218 * @return Square distance between the two points
219 */
220 inline T sqrDistance(const VectorT2<T>& right) const;
221
222 /**
223 * Returns the angle between this vector and a second vectors.
224 * Beware: This vector must not be zero.<br>
225 * Beware: This function does not throw an exception if one or both vectors are zero.<br>
226 * In case, the angle cannot be determined -1 will be returned.
227 * @param right Second vector, must not be zero
228 * @return Angle between both vectors in radian, with range [0, PI], -1 in case of an error
229 * @see isNull().
230 */
231 T angle(const VectorT2<T>& right) const;
232
233 /**
234 * Returns whether two vectors are parallel.
235 * A zero vector is be parallel.
236 * @param right The right vector
237 * @param epsilon The epsilon to be used, with range [0, infinity)
238 * @return True, if so
239 */
240 bool isParallel(const VectorT2<T>& right, const T epsilon = NumericT<T>::eps()) const;
241
242 /**
243 * Returns whether two vectors are orthogonal.
244 * A zero vector is not orthogonal.
245 * @param right The right vector
246 * @param epsilon The epsilon to be used, with range [0, infinity)
247 * @return True, if so
248 */
249 bool isOrthogonal(const VectorT2<T>& right, const T epsilon = NumericT<T>::eps()) const;
250
251 /**
252 * Returns the x value.
253 * @return X value
254 */
255 inline const T& x() const noexcept;
256
257 /**
258 * Returns the x value.
259 * @return X value
260 */
261 inline T& x() noexcept;
262
263 /**
264 * Returns the y value.
265 * @return Y value
266 */
267 inline const T& y() const noexcept;
268
269 /**
270 * Returns the y value.
271 * @return Y value
272 */
273 inline T& y() noexcept;
274
275 /**
276 * Returns an pointer to the vector elements.
277 * @return Pointer to elements
278 */
279 inline const T* data() const noexcept;
280
281 /**
282 * Returns an pointer to the vector elements.
283 * @return Pointer to elements
284 */
285 inline T* data() noexcept;
286
287 /**
288 * Returns whether this vector is a null vector up to a small epsilon.
289 * @return True, if so
290 */
291 inline bool isNull() const;
292
293 /**
294 * Returns whether this vector is a unit vector (whether the vector has the length 1).
295 * @param eps Epsilon to be used, with range [0, infinity)
296 * @return True, if so
297 */
298 inline bool isUnit(const T eps = NumericT<T>::eps()) const;
299
300 /**
301 * Returns whether two vectors are equal up to a specified epsilon.
302 * @param vector Second vector
303 * @param eps Epsilon to be used
304 * @return True, if so
305 */
306 inline bool isEqual(const VectorT2<T>& vector, const T eps) const;
307
308 /**
309 * Copy assigns a vector
310 * @param vector 2D vector that is copied
311 */
312 inline VectorT2<T>& operator=(const VectorT2<T>& vector);
313
314 /**
315 * Returns whether two vectors are identical up to a small epsilon.
316 * @param vector Right vector
317 * @return True, if so
318 */
319 inline bool operator==(const VectorT2<T>& vector) const;
320
321 /**
322 * Returns whether two vectors are not identical up to a small epsilon.
323 * @param vector Right vector
324 * @return True, if so
325 */
326 inline bool operator!=(const VectorT2<T>& vector) const;
327
328 /**
329 * Adds two vectors.
330 * @param vector Right vector
331 * @return Sum vector
332 */
333 inline VectorT2<T> operator+(const VectorT2<T>& vector) const;
334
335 /**
336 * Adds and assigns two vectors.
337 * @param vector Right vector
338 * @return Reference to this vector
339 */
340 inline VectorT2<T>& operator+=(const VectorT2<T>& vector);
341
342 /**
343 * Subtracts two vectors.
344 * @param vector Right vector
345 * @return Difference vector
346 */
347 inline VectorT2<T> operator-(const VectorT2<T>& vector) const;
348
349 /**
350 * Subtracts and assigns two vectors.
351 * @param vector Right vector
352 * @return Reference to this vector
353 */
354 inline VectorT2<T>& operator-=(const VectorT2<T>& vector);
355
356 /**
357 * Returns the negated vector.
358 * @return Negated vector
359 */
360 inline VectorT2<T> operator-() const;
361
362 /**
363 * Returns the dot product of two vectors.
364 * @param vector Right vector
365 * @return Dot product
366 */
367 inline T operator*(const VectorT2<T>& vector) const;
368
369 /**
370 * Multiplies this vector with a scalar.
371 * @param value Scalar value
372 * @return Resulting vector
373 */
374 inline VectorT2<T> operator*(const T& value) const;
375
376 /**
377 * Multiplies and assigns this vector with a scalar.
378 * @param value Scalar value
379 * @return Reference to this vector
380 */
381 inline VectorT2<T>& operator*=(const T& value);
382
383 /**
384 * Divides this vector by a scalar.
385 * Beware: This function does not throw an exception if the given value is zero.<br>
386 * Thus, ensure that given value is not zero before calling this function.<br>
387 * In case, the given value is zero, the result is undefined.
388 * @param value Scalar value to be used as denominator, must not be zero
389 * @return Resulting vector
390 */
391 inline VectorT2<T> operator/(const T& value) const;
392
393 /**
394 * Divides and assigns this vector by a scalar.
395 * Beware: This function does not throw an exception if the given value is zero.<br>
396 * Thus, ensure that given value is not zero before calling this function.<br>
397 * In case, the given value is zero, the result is undefined.
398 * @param value Scalar value to be used as denominator, must not be zero
399 * @return Reference to this vector
400 */
401 inline VectorT2<T>& operator/=(const T& value);
402
403 /**
404 * Compares two vector objects and returns whether the left vector represents a smaller value than the right vector.
405 * First the first component of both vectors are compared, if these values are equal then the next components are compares and so on.<br>
406 * @param vector The second vector to compare
407 * @return True, if so
408 */
409 inline bool operator<(const VectorT2<T>& vector) const;
410
411 /**
412 * Element access operator.
413 * @param index Index of the element to access, with range [0, 1]
414 * @return Element of the vector
415 */
416 inline const T& operator[](const unsigned int index) const noexcept;
417
418 /**
419 * Element access operator.
420 * @param index Index of the element to access, with range [0, 1]
421 * @return Element of the vector
422 */
423 inline T& operator[](const unsigned int index) noexcept;
424
425 /**
426 * Element access operator.
427 * @param index Index of the element to access, with range [0, 1]
428 * @return Element of the vector
429 */
430 inline const T& operator()(const unsigned int index) const noexcept;
431
432 /**
433 * Element access operator.
434 * @param index Index of the element to access, with range [0, 1]
435 * @return Element of the vector
436 */
437 inline T& operator()(const unsigned int index) noexcept;
438
439 /**
440 * Access operator.
441 * @return Pointer to the elements
442 */
443 inline const T* operator()() const noexcept;
444
445 /**
446 * Access operator.
447 * @return Pointer to the elements
448 */
449 inline T* operator()() noexcept;
450
451 /**
452 * Hash function.
453 * @param vector The vector for which the hash value will be determined
454 * @return The resulting hash value
455 */
456 inline size_t operator()(const VectorT2<T>& vector) const;
457
458 /**
459 * Returns a 2D vector with all elements set to NumericT::minValue().
460 * @return The resulting 2D vector
461 */
462 static inline VectorT2<T> minValue();
463
464 /**
465 * Returns a 2D vector with all elements set to NumericT::maxValue().
466 * @return The resulting 2D vector
467 */
468 static inline VectorT2<T> maxValue();
469
470 /**
471 * Converts vectors with specific data type to vectors with different data type.
472 * @param vectors The vectors to convert
473 * @return The converted vectors
474 * @tparam U The element data type of the vectors to convert
475 */
476 template <typename U>
477 static inline std::vector<VectorT2<T>> vectors2vectors(std::vector<VectorT2<U>>&& vectors);
478
479 /**
480 * Converts vectors with specific data type to vectors with different data type.
481 * @param vectors The vectors to convert
482 * @return The converted vectors
483 * @tparam U The element data type of the vectors to convert
484 */
485 template <typename U>
486 static inline std::vector<VectorT2<T>> vectors2vectors(const std::vector<VectorT2<U>>& vectors);
487
488 /**
489 * Converts vectors with specific data type to vectors with different data type.
490 * @param vectors The vectors to convert
491 * @param size The number of vector to convert
492 * @return The converted vectors
493 * @tparam U The element data type of the vectors to convert
494 */
495 template <typename U>
496 static inline std::vector<VectorT2<T>> vectors2vectors(const VectorT2<U>* vectors, const size_t size);
497
498 protected:
499
500 /// The two values of the vector, with element order x, y.
502};
503
504template <typename T>
505inline VectorT2<T>::VectorT2() noexcept
506{
507 static_assert(std::is_arithmetic<T>::value, "VectorT2 only supports arithmetic types");
508 // nothing to do here
509}
510
511template <typename T>
512inline VectorT2<T>::VectorT2(const bool setToHomogeneous) noexcept
513{
514 if (setToHomogeneous)
515 {
516 values_[0] = T(0.0);
517 values_[1] = T(1.0);
518 }
519 else
520 {
521 values_[0] = T(0.0);
522 values_[1] = T(0.0);
523 }
524}
525
526template <typename T>
527inline VectorT2<T>::VectorT2(const T x, const T y) noexcept
528{
529 values_[0] = x;
530 values_[1] = y;
531}
532
533template <typename T>
534inline VectorT2<T>::VectorT2(const T* valueArray) noexcept
535{
536 values_[0] = valueArray[0];
537 values_[1] = valueArray[1];
538}
539
540template <typename T>
541inline VectorT2<T>::VectorT2(const VectorT2<T>& vector) noexcept
542{
543 values_[0] = vector.values_[0];
544 values_[1] = vector.values_[1];
545}
546
547template <typename T>
548template <typename U>
549inline VectorT2<T>::VectorT2(const VectorT2<U>& vector) noexcept
550{
551 values_[0] = T(vector[0]);
552 values_[1] = T(vector[1]);
553}
554
555template <typename T>
556inline T VectorT2<T>::cross(const VectorT2<T>& vector) const
557{
558 return values_[0] * vector.values_[1] - vector.values_[0] * values_[1];
559}
560
561template <typename T>
563{
564 ocean_assert((values_[0] == 0 && values_[1] == 0) || cross(VectorT2<T>(-values_[1], values_[0])) > 0);
565
566 return VectorT2<T>(-values_[1], values_[0]);
567}
568
569template <typename T>
571{
572 T len = length();
574 {
575 ocean_assert(false && "Devision by zero!");
576 return VectorT2<T>();
577 }
578
579 const T factor = T(1) / len;
580 return VectorT2<T>(values_[0] * factor, values_[1] * factor);
581}
582
583template <typename T>
585{
586 const T len = length();
587
589 {
590 return *this;
591 }
592
593 const T factor = T(1) / len;
594 return VectorT2<T>(values_[0] * factor, values_[1] * factor);
595}
596
597template <typename T>
599{
600 const T len = length();
601
603 {
604 return value;
605 }
606
607 const T factor = T(1) / len;
608 return VectorT2<T>(values_[0] * factor, values_[1] * factor);
609}
610
611template <typename T>
613{
614 const T len = length();
616 {
617 return false;
618 }
619
620 const T factor = T(1) / len;
621 values_[0] *= factor;
622 values_[1] *= factor;
623 return true;
624}
625
626template <typename T>
627inline T VectorT2<T>::length() const
628{
629 return NumericT<T>::sqrt(values_[0] * values_[0] + values_[1] * values_[1]);
630}
631
632template <typename T>
633inline T VectorT2<T>::sqr() const
634{
635 return values_[0] * values_[0] + values_[1] * values_[1];
636}
637
638template <typename T>
639inline T VectorT2<T>::distance(const VectorT2<T>& right) const
640{
641 return NumericT<T>::sqrt(sqrDistance(right));
642}
643
644template <typename T>
645inline T VectorT2<T>::sqrDistance(const VectorT2<T>& right) const
646{
647#ifdef OCEAN_DEBUG
648 if (!std::is_same<T, typename SignedTyper<T>::Type>::value)
649 {
650 typedef typename SignedTyper<T>::Type SignedT;
651
652 // -15 == 46 - 61
653 // 225 == -15 * -15
654
655 // 4294967281u == 46u - 61u
656 // 225u == 4294967281u * 4294967281u
657
658 const SignedT debugSqr = NumericT<SignedT>::sqr(SignedT(values_[0]) - SignedT(right.values_[0])) + NumericT<SignedT>::sqr(SignedT(values_[1]) - SignedT(right.values_[1]));
659
660 ocean_assert(T(debugSqr) == NumericT<T>::sqr(values_[0] - right.values_[0]) + NumericT<T>::sqr(values_[1] - right.values_[1]));
661 }
662#endif
663
664 return NumericT<T>::sqr(values_[0] - right.values_[0]) + NumericT<T>::sqr(values_[1] - right.values_[1]);
665}
666
667template <typename T>
668T VectorT2<T>::angle(const VectorT2<T>& right) const
669{
670 // a * b = cos * |a| * |b|
671 // cos = (a * b) / (|a| * |b|)
672 // we separate the sqrt determinations to receive a higher accuracy
673
674 const T thisLength = length();
675 const T rightLength = right.length();
676
677 if (NumericT<T>::isEqualEps(thisLength) || NumericT<T>::isEqualEps(rightLength))
678 {
679 ocean_assert(false && "Invalid vector!");
680 return T(-1);
681 }
682
683 const T dot = values_[0] * right.values_[0] + values_[1] * right.values_[1];
684
685 return NumericT<T>::acos((dot / thisLength) / rightLength);
686}
687
688template <typename T>
689bool VectorT2<T>::isParallel(const VectorT2<T>& right, const T epsilon) const
690{
691 ocean_assert(epsilon >= T(0));
692
693 const VectorT2<T> normalizedThis(normalizedOrZero());
694 const VectorT2<T> normalizedRight(right.normalizedOrZero());
695
696 const T dotProduct = normalizedThis * normalizedRight;
697
698 return NumericT<T>::isEqual(dotProduct, T(1), epsilon) || NumericT<T>::isEqual(dotProduct, T(-1), epsilon);
699}
700
701template <typename T>
702bool VectorT2<T>::isOrthogonal(const VectorT2<T>& right, const T epsilon) const
703{
704 ocean_assert(epsilon >= T(0));
705
706 return NumericT<T>::isEqual(values_[0] * right.values_[0] + values_[1] * right.values_[1], T(0), epsilon);
707}
708
709template <typename T>
710inline const T& VectorT2<T>::x() const noexcept
711{
712 return values_[0];
713}
714
715template <typename T>
716inline T& VectorT2<T>::x() noexcept
717{
718 return values_[0];
719}
720
721template <typename T>
722inline const T& VectorT2<T>::y() const noexcept
723{
724 return values_[1];
725}
726
727template <typename T>
728inline T& VectorT2<T>::y() noexcept
729{
730 return values_[1];
731}
732
733template <typename T>
734inline const T* VectorT2<T>::data() const noexcept
735{
736 return values_;
737}
738
739template <typename T>
740inline T* VectorT2<T>::data() noexcept
741{
742 return values_;
743}
744
745template <typename T>
746inline bool VectorT2<T>::isNull() const
747{
749}
750
751template <typename T>
752inline bool VectorT2<T>::isUnit(const T eps) const
753{
754 return NumericT<T>::isEqual(length(), T(1), eps);
755}
756
757template <typename T>
758inline bool VectorT2<T>::isEqual(const VectorT2<T>& vector, const T eps) const
759{
760 return NumericT<T>::isEqual(values_[0], vector.values_[0], eps)
761 && NumericT<T>::isEqual(values_[1], vector.values_[1], eps);
762}
763
764template <typename T>
766{
767 if (this == &vector)
768 {
769 return *this;
770 }
771
772 values_[0] = vector.values_[0];
773 values_[1] = vector.values_[1];
774
775 return *this;
776}
777
778template <typename T>
779inline bool VectorT2<T>::operator==(const VectorT2<T>& vector) const
780{
781 return NumericT<T>::isEqual(values_[0], vector.values_[0])
782 && NumericT<T>::isEqual(values_[1], vector.values_[1]);
783}
784
785template <typename T>
786inline bool VectorT2<T>::operator!=(const VectorT2<T>& vector) const
787{
788 return NumericT<T>::isNotEqual(values_[0], vector.values_[0])
789 || NumericT<T>::isNotEqual(values_[1], vector.values_[1]);
790}
791
792template <typename T>
794{
795 return VectorT2<T>(values_[0] + vector.values_[0], values_[1] + vector.values_[1]);
796}
797
798template <typename T>
800{
801 values_[0] += vector.values_[0];
802 values_[1] += vector.values_[1];
803
804 return *this;
805}
806
807template <typename T>
809{
810 return VectorT2<T>(values_[0] - vector.values_[0], values_[1] - vector.values_[1]);
811}
812
813template <typename T>
815{
816 values_[0] -= vector.values_[0];
817 values_[1] -= vector.values_[1];
818
819 return *this;
820}
821
822template <typename T>
824{
825 return VectorT2<T>(-values_[0], -values_[1]);
826}
827
828template <typename T>
829inline T VectorT2<T>::operator*(const VectorT2<T>& vector) const
830{
831 return values_[0] * vector.values_[0] + values_[1] * vector.values_[1];
832}
833
834template <typename T>
835inline VectorT2<T> VectorT2<T>::operator*(const T& value) const
836{
837 return VectorT2<T>(values_[0] * value, values_[1] * value);
838}
839
840template <typename T>
842{
843 values_[0] *= value;
844 values_[1] *= value;
845
846 return *this;
847}
848
849template <typename T>
850inline VectorT2<T> VectorT2<T>::operator/(const T& value) const
851{
852 ocean_assert(NumericT<T>::isNotEqualEps(value));
853 const T factor = T(1) / value;
854
855 return VectorT2<T>(values_[0] * factor, values_[1] * factor);
856}
857
858template <typename T>
860{
861 ocean_assert(NumericT<T>::isNotEqualEps(value));
862 const T factor = T(1) / value;
863
864 values_[0] *= factor;
865 values_[1] *= factor;
866
867 return *this;
868}
869
870template <typename T>
871inline bool VectorT2<T>::operator<(const VectorT2<T>& vector) const
872{
873 return values_[0] < vector.values_[0] || (values_[0] == vector.values_[0] && values_[1] < vector.values_[1]);
874}
875
876template <typename T>
877inline const T& VectorT2<T>::operator[](const unsigned int index) const noexcept
878{
879 ocean_assert(index < 2u);
880 return values_[index];
881}
882
883template <typename T>
884inline T& VectorT2<T>::operator[](const unsigned int index) noexcept
885{
886 ocean_assert(index < 2u);
887 return values_[index];
888}
889
890template <typename T>
891inline const T& VectorT2<T>::operator()(const unsigned int index) const noexcept
892{
893 ocean_assert(index < 2u);
894 return values_[index];
895}
896
897template <typename T>
898inline T& VectorT2<T>::operator()(const unsigned int index) noexcept
899{
900 ocean_assert(index < 2u);
901 return values_[index];
902}
903
904template <typename T>
905inline const T* VectorT2<T>::operator()() const noexcept
906{
907 return values_;
908}
909
910template <typename T>
911inline T* VectorT2<T>::operator()() noexcept
912{
913 return values_;
914}
915
916template <typename T>
917inline size_t VectorT2<T>::operator()(const VectorT2<T>& vector) const
918{
919 size_t seed = std::hash<T>{}(vector.x());
920 seed ^= std::hash<T>{}(vector.y()) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
921
922 return seed;
923}
924
925template <typename T>
930
931template <typename T>
936
937template <>
938template <>
939inline std::vector<VectorT2<float>> VectorT2<float>::vectors2vectors(std::vector<VectorT2<float>>&& vectors)
940{
941 return std::move(vectors);
942}
943
944template <>
945template <>
946inline std::vector<VectorT2<double>> VectorT2<double>::vectors2vectors(std::vector<VectorT2<double>>&& vectors)
947{
948 return std::move(vectors);
949}
950
951template <typename T>
952template <typename U>
953inline std::vector<VectorT2<T>> VectorT2<T>::vectors2vectors(std::vector<VectorT2<U>>&& vectors)
954{
955 std::vector<VectorT2<T>> result;
956 result.reserve(vectors.size());
957
958 for (typename std::vector<VectorT2<U>>::const_iterator i = vectors.cbegin(); i != vectors.cend(); ++i)
959 {
960 result.emplace_back(*i);
961 }
962
963 return result;
964}
965
966template <>
967template <>
968inline std::vector<VectorT2<float>> VectorT2<float>::vectors2vectors(const std::vector<VectorT2<float>>& vectors)
969{
970 return vectors;
971}
972
973template <>
974template <>
975inline std::vector<VectorT2<double>> VectorT2<double>::vectors2vectors(const std::vector<VectorT2<double>>& vectors)
976{
977 return vectors;
978}
979
980template <typename T>
981template <typename U>
982inline std::vector<VectorT2<T>> VectorT2<T>::vectors2vectors(const std::vector<VectorT2<U>>& vectors)
983{
984 std::vector<VectorT2<T>> result;
985 result.reserve(vectors.size());
986
987 for (typename std::vector<VectorT2<U>>::const_iterator i = vectors.cbegin(); i != vectors.cend(); ++i)
988 {
989 result.emplace_back(*i);
990 }
991
992 return result;
993}
994
995template <typename T>
996template <typename U>
997inline std::vector<VectorT2<T>> VectorT2<T>::vectors2vectors(const VectorT2<U>* vectors, const size_t size)
998{
999 std::vector<VectorT2<T>> result;
1000 result.reserve(size);
1001
1002 for (size_t n = 0; n < size; ++n)
1003 {
1004 result.emplace_back(vectors[n]);
1005 }
1006
1007 return result;
1008}
1009
1010template <typename T>
1011std::ostream& operator<<(std::ostream& stream, const VectorT2<T>& vector)
1012{
1013 stream << "[" << vector.x() << ", " << vector.y() << "]";
1014
1015 return stream;
1016}
1017
1018template <bool tActive, typename T>
1019MessageObject<tActive>& operator<<(MessageObject<tActive>& messageObject, const VectorT2<T>& vector)
1020{
1021 return messageObject << "[" << vector.x() << ", " << vector.y() << "]";
1022}
1023
1024template <bool tActive, typename T>
1025MessageObject<tActive>& operator<<(MessageObject<tActive>&& messageObject, const VectorT2<T>& vector)
1026{
1027 return messageObject << "[" << vector.x() << ", " << vector.y() << "]";
1028}
1029
1030}
1031
1032#endif // META_OCEAN_MATH_VECTOR2_H
This class provides basic numeric functionalities.
Definition Numeric.h:57
static T sqrt(const T value)
Returns the square root of a given value.
Definition Numeric.h:1533
static bool isEqual(const T first, const T second)
Returns whether two values are equal up to a small epsilon.
Definition Numeric.h:2386
static constexpr T sqr(const T value)
Returns the square of a given value.
Definition Numeric.h:1495
static constexpr bool isEqualEps(const T value)
Returns whether a value is smaller than or equal to a small epsilon.
Definition Numeric.h:2087
static T acos(const T value)
Returns the arccosine of a given value.
Definition Numeric.h:2907
static bool isNotEqual(const T first, const T second)
Returns whether two values are not equal up to a small epsilon.
Definition Numeric.h:2613
T Type
Definition of the signed data type, if existing.
Definition DataType.h:294
This class implements a vector with two elements.
Definition Vector2.h:96
const T & x() const noexcept
Returns the x value.
Definition Vector2.h:710
VectorT2< T > & operator*=(const T &value)
Multiplies and assigns this vector with a scalar.
Definition Vector2.h:841
const T & y() const noexcept
Returns the y value.
Definition Vector2.h:722
VectorT2< T > operator+(const VectorT2< T > &vector) const
Adds two vectors.
Definition Vector2.h:793
VectorT2< T > & operator+=(const VectorT2< T > &vector)
Adds and assigns two vectors.
Definition Vector2.h:799
static VectorT2< T > minValue()
Returns a 2D vector with all elements set to NumericT::minValue().
Definition Vector2.h:926
VectorT2< T > & operator-=(const VectorT2< T > &vector)
Subtracts and assigns two vectors.
Definition Vector2.h:814
const T * operator()() const noexcept
Access operator.
Definition Vector2.h:905
bool isOrthogonal(const VectorT2< T > &right, const T epsilon=NumericT< T >::eps()) const
Returns whether two vectors are orthogonal.
Definition Vector2.h:702
VectorT2< T > & operator/=(const T &value)
Divides and assigns this vector by a scalar.
Definition Vector2.h:859
T operator*(const VectorT2< T > &vector) const
Returns the dot product of two vectors.
Definition Vector2.h:829
const T & operator[](const unsigned int index) const noexcept
Element access operator.
Definition Vector2.h:877
bool isUnit(const T eps=NumericT< T >::eps()) const
Returns whether this vector is a unit vector (whether the vector has the length 1).
Definition Vector2.h:752
static VectorT2< T > maxValue()
Returns a 2D vector with all elements set to NumericT::maxValue().
Definition Vector2.h:932
static std::vector< VectorT2< T > > vectors2vectors(std::vector< VectorT2< U > > &&vectors)
Converts vectors with specific data type to vectors with different data type.
Definition Vector2.h:953
VectorT2< T > operator-() const
Returns the negated vector.
Definition Vector2.h:823
const T * data() const noexcept
Returns an pointer to the vector elements.
Definition Vector2.h:734
bool operator==(const VectorT2< T > &vector) const
Returns whether two vectors are identical up to a small epsilon.
Definition Vector2.h:779
T Type
Definition of the used data type.
Definition Vector2.h:102
VectorT2< T > normalized() const
Returns the normalized vector.
Definition Vector2.h:570
bool isEqual(const VectorT2< T > &vector, const T eps) const
Returns whether two vectors are equal up to a specified epsilon.
Definition Vector2.h:758
T distance(const VectorT2< T > &right) const
Returns the distance between this 2D position and a second 2D position.
Definition Vector2.h:639
VectorT2< T > normalizedOrZero() const
Returns the normalized vector.
Definition Vector2.h:584
T angle(const VectorT2< T > &right) const
Returns the angle between this vector and a second vectors.
Definition Vector2.h:668
T sqr() const
Returns the square of the vector length.
Definition Vector2.h:633
VectorT2() noexcept
Creates a new 2D vector with undefined elements.
Definition Vector2.h:505
VectorT2< T > normalizedOrValue(const VectorT2< T > &value) const
Returns the normalized vector.
Definition Vector2.h:598
bool operator!=(const VectorT2< T > &vector) const
Returns whether two vectors are not identical up to a small epsilon.
Definition Vector2.h:786
T values_[2]
The two values of the vector, with element order x, y.
Definition Vector2.h:501
bool isNull() const
Returns whether this vector is a null vector up to a small epsilon.
Definition Vector2.h:746
T sqrDistance(const VectorT2< T > &right) const
Returns the square distance between this 2D position and a second 2D position.
Definition Vector2.h:645
VectorT2< T > & operator=(const VectorT2< T > &vector)
Copy assigns a vector.
Definition Vector2.h:765
T length() const
Returns the length of the vector.
Definition Vector2.h:627
bool normalize()
Normalizes this vector.
Definition Vector2.h:612
bool operator<(const VectorT2< T > &vector) const
Compares two vector objects and returns whether the left vector represents a smaller value than the r...
Definition Vector2.h:871
T cross(const VectorT2< T > &vector) const
Returns the cross product of two 2D vectors.
Definition Vector2.h:556
bool isParallel(const VectorT2< T > &right, const T epsilon=NumericT< T >::eps()) const
Returns whether two vectors are parallel.
Definition Vector2.h:689
VectorT2< T > operator/(const T &value) const
Divides this vector by a scalar.
Definition Vector2.h:850
VectorT2< T > perpendicular() const
Returns a vector perpendicular to this vectors.
Definition Vector2.h:562
VectorT2< int > VectorI2
Definition of a 2D vector with integer values.
Definition Vector2.h:49
VectorT2< double > VectorD2
Definition of a 2D vector with double values.
Definition Vector2.h:35
std::vector< VectorI2 > VectorsI2
Definition of a vector holding VectorI2 objects.
Definition Vector2.h:85
std::vector< VectorD2 > VectorsD2
Definition of a vector holding VectorD2 objects.
Definition Vector2.h:71
std::vector< VectorT2< T > > VectorsT2
Definition of a typename alias for vectors with VectorT2 objects.
Definition Vector2.h:57
std::vector< VectorF2 > VectorsF2
Definition of a vector holding VectorF2 objects.
Definition Vector2.h:78
std::vector< Vector2 > Vectors2
Definition of a vector holding Vector2 objects.
Definition Vector2.h:64
VectorT2< float > VectorF2
Definition of a 2D vector with float values.
Definition Vector2.h:42
VectorT2< Scalar > Vector2
Definition of a 2D vector.
Definition Vector2.h:28
The namespace covering the entire Ocean framework.
Definition Accessor.h:15
std::ostream & operator<<(std::ostream &stream, const HighPerformanceStatistic &highPerformanceStatistic)
Definition HighPerformanceTimer.h:963