8 #ifndef META_OCEAN_MATH_VECTOR4_H
9 #define META_OCEAN_MATH_VECTOR4_H
15 #include <type_traits>
22 template <
typename T>
class VectorT4;
119 inline VectorT4(
const T&
x,
const T&
y,
const T&
z,
const T&
w) noexcept;
139 inline explicit VectorT4(
const T* valueArray) noexcept;
152 template <
typename U>
197 inline T
sqr()
const;
229 inline const T&
x() const noexcept;
235 inline T&
x() noexcept;
241 inline const T&
y() const noexcept;
247 inline T&
y() noexcept;
253 inline const T&
z() const noexcept;
259 inline T&
z() noexcept;
265 inline const T&
w() const noexcept;
271 inline T&
w() noexcept;
289 inline const T*
data() const noexcept;
295 inline T*
data() noexcept;
301 inline
bool isNull() const;
329 inline
bool operator==(const
VectorT4<T>& vector) const;
336 inline
bool operator!=(const
VectorT4<T>& vector) const;
370 inline
VectorT4<T> operator-() const;
377 inline T operator*(const
VectorT4<T>& vector) const;
384 inline
VectorT4<T> operator*(const T& value) const;
391 inline
VectorT4<T>& operator*=(const T& value);
401 inline
VectorT4<T> operator/(const T& value) const;
411 inline
VectorT4<T>& operator/=(const T& value);
419 inline
bool operator<(const
VectorT4<T>& vector) const;
426 inline const T& operator[](const
unsigned int index) const noexcept;
433 inline T& operator[](const
unsigned int index) noexcept;
440 inline const T& operator()(const
unsigned int index) const noexcept;
447 inline T& operator()(const
unsigned int index) noexcept;
453 inline const T* operator()() const noexcept;
459 inline T* operator()() noexcept;
466 inline
size_t operator()(const
VectorT4<T>& vector) const;
474 template <typename U>
483 template <typename U>
493 template <typename U>
502 template <
typename T>
511 template <
typename T>
514 memcpy(values_, vector(),
sizeof(T) * 3);
518 template <
typename T>
521 memcpy(values_, vector(),
sizeof(T) * 3);
525 template <
typename T>
528 memcpy(values_, valueArray,
sizeof(T) * 4);
531 template <
typename T>
534 values_[0] = vector.values_[0];
535 values_[1] = vector.values_[1];
536 values_[2] = vector.values_[2];
537 values_[3] = vector.values_[3];
540 template <
typename T>
541 template <
typename U>
544 values_[0] = T(vector[0]);
545 values_[1] = T(vector[1]);
546 values_[2] = T(vector[2]);
547 values_[3] = T(vector[3]);
550 template <
typename T>
556 ocean_assert(
false &&
"Division by zero!");
560 T factor = T(1) / len;
561 return VectorT4<T>(values_[0] * factor, values_[1] * factor, values_[2] * factor, values_[3] * factor);
564 template <
typename T>
567 const T len = length();
574 const T factor = T(1) / len;
575 return VectorT4<T>(values_[0] * factor, values_[1] * factor, values_[2] * factor, values_[3] * factor);
578 template <
typename T>
581 const T len = length();
588 const T factor = T(1) / len;
589 return VectorT4<T>(values_[0] * factor, values_[1] * factor, values_[2] * factor, values_[3] * factor);
592 template <
typename T>
595 const T len = length();
601 const T factor = T(1) / len;
602 values_[0] *= factor;
603 values_[1] *= factor;
604 values_[2] *= factor;
605 values_[3] *= factor;
609 template <
typename T>
612 return NumericT<T>::sqrt(values_[0] * values_[0] + values_[1] * values_[1] + values_[2] * values_[2] + values_[3] * values_[3]);
615 template <
typename T>
618 return values_[0] * values_[0] + values_[1] * values_[1] + values_[2] * values_[2] + values_[3] * values_[3];
621 template <
typename T>
628 const T thisLength = length();
629 const T rightLength = right.
length();
633 ocean_assert(
false &&
"Invalid vector!");
637 const T dot = values_[0] * right.
values_[0] + values_[1] * right.
values_[1] + values_[2] * right.
values_[2] + values_[3] * right.
values_[3];
642 template <
typename T>
645 const VectorT4<T> normalizedThis(normalizedOrZero());
648 const T dotProduct = normalizedThis * normalizedRight;
653 template <
typename T>
659 template <
typename T>
665 template <
typename T>
671 template <
typename T>
677 template <
typename T>
683 template <
typename T>
689 template <
typename T>
695 template <
typename T>
701 template <
typename T>
707 template <
typename T>
713 template <
typename T>
716 return VectorT3<T>(values_[0], values_[1], values_[2]);
719 template <
typename T>
725 template <
typename T>
731 template <
typename T>
738 template <
typename T>
744 template <
typename T>
753 template <
typename T>
761 values_[0] = vector.
values_[0];
762 values_[1] = vector.
values_[1];
763 values_[2] = vector.
values_[2];
764 values_[3] = vector.
values_[3];
769 template <
typename T>
778 template <
typename T>
787 template <
typename T>
793 template <
typename T>
796 values_[0] += vector.
values_[0];
797 values_[1] += vector.
values_[1];
798 values_[2] += vector.
values_[2];
799 values_[3] += vector.
values_[3];
804 template <
typename T>
810 template <
typename T>
813 values_[0] -= vector.
values_[0];
814 values_[1] -= vector.
values_[1];
815 values_[2] -= vector.
values_[2];
816 values_[3] -= vector.
values_[3];
821 template <
typename T>
824 return VectorT4<T>(-values_[0], -values_[1], -values_[2], -values_[3]);
827 template <
typename T>
830 return values_[0] * vector.
values_[0] + values_[1] * vector.
values_[1] + values_[2] * vector.
values_[2] + values_[3] * vector.
values_[3];
833 template <
typename T>
836 return VectorT4<T>(values_[0] * value, values_[1] * value, values_[2] * value, values_[3] * value);
839 template <
typename T>
850 template <
typename T>
854 const T factor = T(1) / value;
856 return VectorT4<T>(values_[0] * factor, values_[1] * factor, values_[2] * factor, values_[3] * factor);
859 template <
typename T>
864 const T factor = T(1) / value;
866 values_[0] *= factor;
867 values_[1] *= factor;
868 values_[2] *= factor;
869 values_[3] *= factor;
874 template <
typename T>
877 return values_[0] < right.
values_[0]
878 || (values_[0] == right.
values_[0] && (values_[1] < right.
values_[1]
879 || (values_[1] == right.
values_[1] && (values_[2] < right.
values_[2]
880 || (values_[2] == right.
values_[2] && values_[3] < right.
values_[3])))));
883 template <
typename T>
886 ocean_assert(index < 4u);
887 return values_[index];
890 template <
typename T>
893 ocean_assert(index < 4u);
894 return values_[index];
897 template <
typename T>
900 ocean_assert(index < 4u);
901 return values_[index];
904 template <
typename T>
907 ocean_assert(index < 4u);
908 return values_[index];
911 template <
typename T>
917 template <
typename T>
923 template <
typename T>
926 size_t seed = std::hash<T>{}(vector.
x());
927 seed ^= std::hash<T>{}(vector.
y()) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
928 seed ^= std::hash<T>{}(vector.
z()) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
929 seed ^= std::hash<T>{}(vector.
w()) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
938 return std::move(vectors);
945 return std::move(vectors);
948 template <
typename T>
949 template <
typename U>
952 std::vector<VectorT4<T>> result;
953 result.reserve(vectors.size());
955 for (
typename std::vector<
VectorT4<U>>::const_iterator i = vectors.cbegin(); i != vectors.cend(); ++i)
957 result.emplace_back(*i);
977 template <
typename T>
978 template <
typename U>
981 std::vector<VectorT4<T>> result;
982 result.reserve(vectors.size());
984 for (
typename std::vector<
VectorT4<U>>::const_iterator i = vectors.begin(); i != vectors.end(); ++i)
986 result.emplace_back(*i);
992 template <
typename T>
993 template <
typename U>
996 std::vector<VectorT4<T>> result;
997 result.reserve(size);
999 for (
size_t n = 0; n < size; ++n)
1001 result.emplace_back(vectors[n]);
1007 template <
typename T>
1010 stream <<
"[" << vector.
x() <<
", " << vector.
y() <<
", " << vector.
z() <<
", " << vector.
w() <<
"]";
1015 template <
bool tActive,
typename T>
1016 MessageObject<tActive>&
operator<<(MessageObject<tActive>& messageObject,
const VectorT4<T>& vector)
1018 return messageObject <<
"[" << vector.x() <<
", " << vector.y() <<
", " << vector.z() <<
", " << vector.w() <<
"]";
1021 template <
bool tActive,
typename T>
1022 MessageObject<tActive>&
operator<<(MessageObject<tActive>&& messageObject,
const VectorT4<T>& vector)
1024 return messageObject <<
"[" << vector.x() <<
", " << vector.y() <<
", " << vector.z() <<
", " << vector.w() <<
"]";
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 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
This class implements a vector with two elements.
Definition: Vector2.h:96
This class implements a vector with three elements.
Definition: Vector3.h:97
This class implements a vector with four elements.
Definition: Vector4.h:97
VectorT4< T > operator/(const T &value) const
Divides this vector by a scalar.
Definition: Vector4.h:851
VectorT3< T > xyz() const noexcept
Returns the x, y and z component of the vector as new 3D vector.
Definition: Vector4.h:714
bool isOrthogonal(const VectorT4< T > &right) const
Returns whether two vectors are orthogonal.
Definition: Vector4.h:654
const T * operator()() const noexcept
Access operator.
Definition: Vector4.h:912
T Type
Definition of the used data type.
Definition: Vector4.h:103
VectorT4< T > normalizedOrZero() const
Returns the normalized vector.
Definition: Vector4.h:565
bool operator==(const VectorT4< T > &vector) const
Returns whether two vectors are identical up to a small epsilon.
Definition: Vector4.h:770
T length() const
Returns the length of the vector.
Definition: Vector4.h:610
static std::vector< VectorT4< T > > vectors2vectors(std::vector< VectorT4< U >> &&vectors)
Converts vectors with specific data type to vectors with different data type.
Definition: Vector4.h:950
const T & y() const noexcept
Returns the y value.
Definition: Vector4.h:672
const T & x() const noexcept
Returns the x value.
Definition: Vector4.h:660
bool operator!=(const VectorT4< T > &vector) const
Returns whether two vectors are not identical up to a small epsilon.
Definition: Vector4.h:779
VectorT4< T > & operator+=(const VectorT4< T > &vector)
Adds and assigns two vectors.
Definition: Vector4.h:794
bool isEqual(const VectorT4< T > &vector, const T eps) const
Returns whether two vectors are equal up to a specified epsilon.
Definition: Vector4.h:745
const T * data() const noexcept
Returns an pointer to the vector elements.
Definition: Vector4.h:720
const T & z() const noexcept
Returns the z value.
Definition: Vector4.h:684
VectorT4()=default
Creates a new 2D vector with zeros as default values.
bool isNull() const
Returns whether this vector is a null vector up to a small epsilon.
Definition: Vector4.h:732
VectorT4< T > normalized() const
Returns the normalized vector.
Definition: Vector4.h:551
VectorT4< T > & operator/=(const T &value)
Divides and assigns this vector by a scalar.
Definition: Vector4.h:860
VectorT4< T > & operator*=(const T &value)
Multiplies and assigns this vector with a scalar.
Definition: Vector4.h:840
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: Vector4.h:739
VectorT4< T > normalizedOrValue(const VectorT4< T > &value) const
Returns the normalized vector.
Definition: Vector4.h:579
T values_[4]
The four values of the vector, with element order x, y, z, w.
Definition: Vector4.h:499
const T & operator[](const unsigned int index) const noexcept
Element access operator.
Definition: Vector4.h:884
T operator*(const VectorT4< T > &vector) const
Returns the dot product of two vectors.
Definition: Vector4.h:828
VectorT4< T > operator+(const VectorT4< T > &vector) const
Adds two vectors.
Definition: Vector4.h:788
T sqr() const
Returns the square of the vector length.
Definition: Vector4.h:616
VectorT4< T > & operator=(const VectorT4< T > &vector)
Copy assigns a vector.
Definition: Vector4.h:754
const T & w() const noexcept
Returns the w value.
Definition: Vector4.h:696
bool normalize()
Normalizes this vector.
Definition: Vector4.h:593
VectorT4< T > & operator-=(const VectorT4< T > &vector)
Subtracts and assigns two vectors.
Definition: Vector4.h:811
VectorT2< T > xy() const noexcept
Returns the x and y component of the vector as new 2D vector.
Definition: Vector4.h:708
VectorT4< T > operator-() const
Returns the negated vector.
Definition: Vector4.h:822
bool operator<(const VectorT4< T > &vector) const
Compares two vector objects and returns whether the left vector represents a smaller value than the r...
Definition: Vector4.h:875
T angle(const VectorT4< T > &right) const
Returns the angle between this vector and a second vectors.
Definition: Vector4.h:622
bool isParallel(const VectorT4< T > &right) const
Returns whether two vectors are parallel.
Definition: Vector4.h:643
std::vector< VectorI4 > VectorsI4
Definition of a vector holding VectorI4 objects.
Definition: Vector4.h:86
std::vector< Vector4 > Vectors4
Definition of a vector holding Vector4 objects.
Definition: Vector4.h:65
VectorT4< int > VectorI4
Definition of a 4D vector with integer values.
Definition: Vector4.h:50
VectorT4< Scalar > Vector4
Definition of a 4D vector.
Definition: Vector4.h:22
std::vector< VectorT4< T > > VectorsT4
Definition of a typename alias for vectors with VectorT4 objects.
Definition: Vector4.h:58
std::vector< VectorD4 > VectorsD4
Definition of a vector holding VectorD4 objects.
Definition: Vector4.h:72
VectorT4< float > VectorF4
Definition of a 4D vector with float values.
Definition: Vector4.h:43
VectorT4< double > VectorD4
Definition of a 4D vector with double values.
Definition: Vector4.h:36
std::vector< VectorF4 > VectorsF4
Definition of a vector holding VectorF4 objects.
Definition: Vector4.h:79
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15
std::ostream & operator<<(std::ostream &stream, const HighPerformanceStatistic &highPerformanceStatistic)
Definition: HighPerformanceTimer.h:963