8 #ifndef META_OCEAN_MATH_VECTOR2_H
9 #define META_OCEAN_MATH_VECTOR2_H
14 #include <type_traits>
21 template <
typename T>
class VectorT2;
118 inline explicit
VectorT2(const
bool setToHomogeneous) noexcept;
131 inline explicit
VectorT2(const T* valueArray) noexcept;
144 template <typename U>
255 inline const T&
x() const noexcept;
261 inline T&
x() noexcept;
267 inline const T&
y() const noexcept;
273 inline T&
y() noexcept;
279 inline const T*
data() const noexcept;
319 inline
bool operator==(const
VectorT2<T>& vector) const;
326 inline
bool operator!=(const
VectorT2<T>& vector) const;
367 inline T operator*(const
VectorT2<T>& vector) const;
374 inline
VectorT2<T> operator*(const T& value) const;
391 inline
VectorT2<T> operator/(const T& value) const;
409 inline
bool operator<(const
VectorT2<T>& vector) const;
416 inline const T& operator[](const
unsigned int index) const noexcept;
423 inline T& operator[](const
unsigned int index) noexcept;
430 inline const T& operator()(const
unsigned int index) const noexcept;
437 inline T& operator()(const
unsigned int index) noexcept;
443 inline const T* operator()() const noexcept;
449 inline T* operator()() noexcept;
456 inline
size_t operator()(const
VectorT2<T>& vector) const;
464 template <typename U>
473 template <typename U>
483 template <typename U>
492 template <typename T>
495 static_assert(std::is_arithmetic<T>::value,
"VectorT2 only supports arithmetic types");
499 template <
typename T>
502 if (setToHomogeneous)
514 template <
typename T>
521 template <
typename T>
528 template <
typename T>
531 values_[0] = vector.values_[0];
532 values_[1] = vector.values_[1];
535 template <
typename T>
536 template <
typename U>
543 template <
typename T>
549 template <
typename T>
557 template <
typename T>
563 ocean_assert(
false &&
"Devision by zero!");
567 const T factor = T(1) / len;
571 template <
typename T>
581 const T factor = T(1) / len;
585 template <
typename T>
595 const T factor = T(1) / len;
599 template <
typename T>
608 const T factor = T(1) / len;
614 template <
typename T>
620 template <
typename T>
626 template <
typename T>
632 template <
typename T>
655 template <
typename T>
662 const T thisLength =
length();
663 const T rightLength = right.
length();
667 ocean_assert(
false &&
"Invalid vector!");
676 template <
typename T>
679 ocean_assert(epsilon >= T(0));
684 const T dotProduct = normalizedThis * normalizedRight;
689 template <
typename T>
692 ocean_assert(epsilon >= T(0));
697 template <
typename T>
703 template <
typename T>
709 template <
typename T>
715 template <
typename T>
721 template <
typename T>
727 template <
typename T>
733 template <
typename T>
739 template <
typename T>
745 template <
typename T>
752 template <
typename T>
766 template <
typename T>
773 template <
typename T>
780 template <
typename T>
786 template <
typename T>
795 template <
typename T>
801 template <
typename T>
810 template <
typename T>
816 template <
typename T>
822 template <
typename T>
828 template <
typename T>
837 template <
typename T>
841 const T factor = T(1) / value;
846 template <
typename T>
850 const T factor = T(1) / value;
858 template <
typename T>
864 template <
typename T>
867 ocean_assert(index < 2u);
871 template <
typename T>
874 ocean_assert(index < 2u);
878 template <
typename T>
881 ocean_assert(index < 2u);
885 template <
typename T>
888 ocean_assert(index < 2u);
892 template <
typename T>
898 template <
typename T>
904 template <
typename T>
907 size_t seed = std::hash<T>{}(vector.
x());
908 seed ^= std::hash<T>{}(vector.
y()) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
917 return std::move(vectors);
924 return std::move(vectors);
927 template <
typename T>
928 template <
typename U>
931 std::vector<VectorT2<T>> result;
932 result.reserve(vectors.size());
934 for (
typename std::vector<
VectorT2<U>>::const_iterator i = vectors.cbegin(); i != vectors.cend(); ++i)
936 result.emplace_back(*i);
956 template <
typename T>
957 template <
typename U>
960 std::vector<VectorT2<T>> result;
961 result.reserve(vectors.size());
963 for (
typename std::vector<
VectorT2<U>>::const_iterator i = vectors.cbegin(); i != vectors.cend(); ++i)
965 result.emplace_back(*i);
971 template <
typename T>
972 template <
typename U>
975 std::vector<VectorT2<T>> result;
976 result.reserve(size);
978 for (
size_t n = 0; n < size; ++n)
980 result.emplace_back(vectors[n]);
986 template <
typename T>
989 stream <<
"[" << vector.
x() <<
", " << vector.
y() <<
"]";
994 template <
bool tActive,
typename T>
995 MessageObject<tActive>&
operator<<(MessageObject<tActive>& messageObject,
const VectorT2<T>& vector)
997 return messageObject <<
"[" << vector.x() <<
", " << vector.y() <<
"]";
1000 template <
bool tActive,
typename T>
1001 MessageObject<tActive>&
operator<<(MessageObject<tActive>&& messageObject,
const VectorT2<T>& vector)
1003 return messageObject <<
"[" << vector.x() <<
", " << vector.y() <<
"]";
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
const T & x() const noexcept
Returns the x value.
Definition: Vector2.h:698
VectorT2< T > & operator*=(const T &value)
Multiplies and assigns this vector with a scalar.
Definition: Vector2.h:829
const T & y() const noexcept
Returns the y value.
Definition: Vector2.h:710
VectorT2< T > operator+(const VectorT2< T > &vector) const
Adds two vectors.
Definition: Vector2.h:781
VectorT2< T > & operator+=(const VectorT2< T > &vector)
Adds and assigns two vectors.
Definition: Vector2.h:787
VectorT2< T > & operator-=(const VectorT2< T > &vector)
Subtracts and assigns two vectors.
Definition: Vector2.h:802
const T * operator()() const noexcept
Access operator.
Definition: Vector2.h:893
bool isOrthogonal(const VectorT2< T > &right, const T epsilon=NumericT< T >::eps()) const
Returns whether two vectors are orthogonal.
Definition: Vector2.h:690
VectorT2< T > & operator/=(const T &value)
Divides and assigns this vector by a scalar.
Definition: Vector2.h:847
T operator*(const VectorT2< T > &vector) const
Returns the dot product of two vectors.
Definition: Vector2.h:817
const T & operator[](const unsigned int index) const noexcept
Element access operator.
Definition: Vector2.h:865
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:740
VectorT2< T > operator-() const
Returns the negated vector.
Definition: Vector2.h:811
const T * data() const noexcept
Returns an pointer to the vector elements.
Definition: Vector2.h:722
bool operator==(const VectorT2< T > &vector) const
Returns whether two vectors are identical up to a small epsilon.
Definition: Vector2.h:767
T Type
Definition of the used data type.
Definition: Vector2.h:102
VectorT2< T > normalized() const
Returns the normalized vector.
Definition: Vector2.h:558
bool isEqual(const VectorT2< T > &vector, const T eps) const
Returns whether two vectors are equal up to a specified epsilon.
Definition: Vector2.h:746
T distance(const VectorT2< T > &right) const
Returns the distance between this 2D position and a second 2D position.
Definition: Vector2.h:627
VectorT2< T > normalizedOrZero() const
Returns the normalized vector.
Definition: Vector2.h:572
T angle(const VectorT2< T > &right) const
Returns the angle between this vector and a second vectors.
Definition: Vector2.h:656
T sqr() const
Returns the square of the vector length.
Definition: Vector2.h:621
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:929
VectorT2() noexcept
Creates a new 2D vector with undefined elements.
Definition: Vector2.h:493
VectorT2< T > normalizedOrValue(const VectorT2< T > &value) const
Returns the normalized vector.
Definition: Vector2.h:586
bool operator!=(const VectorT2< T > &vector) const
Returns whether two vectors are not identical up to a small epsilon.
Definition: Vector2.h:774
T values_[2]
The two values of the vector, with element order x, y.
Definition: Vector2.h:489
bool isNull() const
Returns whether this vector is a null vector up to a small epsilon.
Definition: Vector2.h:734
T sqrDistance(const VectorT2< T > &right) const
Returns the square distance between this 2D position and a second 2D position.
Definition: Vector2.h:633
VectorT2< T > & operator=(const VectorT2< T > &vector)
Copy assigns a vector.
Definition: Vector2.h:753
T length() const
Returns the length of the vector.
Definition: Vector2.h:615
bool normalize()
Normalizes this vector.
Definition: Vector2.h:600
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:859
T cross(const VectorT2< T > &vector) const
Returns the cross product of two 2D vectors.
Definition: Vector2.h:544
bool isParallel(const VectorT2< T > &right, const T epsilon=NumericT< T >::eps()) const
Returns whether two vectors are parallel.
Definition: Vector2.h:677
VectorT2< T > operator/(const T &value) const
Divides this vector by a scalar.
Definition: Vector2.h:838
VectorT2< T > perpendicular() const
Returns a vector perpendicular to this vectors.
Definition: Vector2.h:550
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:21
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15
std::ostream & operator<<(std::ostream &stream, const HighPerformanceStatistic &highPerformanceStatistic)
Definition: HighPerformanceTimer.h:963