8#ifndef META_OCEAN_MATH_VECTOR3_H
9#define META_OCEAN_MATH_VECTOR3_H
22template <
typename T>
class VectorT3;
119 inline explicit
VectorT3(const
bool setToHomogeneous) noexcept;
133 inline explicit
VectorT3(const T* valueArray) noexcept;
153 template <typename U>
278 inline const T&
x() const noexcept;
284 inline T&
x() noexcept;
290 inline const T&
y() const noexcept;
296 inline T&
y() noexcept;
302 inline const T&
z() const noexcept;
308 inline T&
z() noexcept;
320 inline const T*
data() const noexcept;
360 inline
bool operator==(const
VectorT3<T>& vector) const;
367 inline
bool operator!=(const
VectorT3<T>& vector) const;
408 inline T operator*(const
VectorT3<T>& vector) const;
415 inline
VectorT3<T> operator*(const T& value) const;
432 inline
VectorT3<T> operator/(const T& value) const;
450 inline
bool operator<(const
VectorT3<T>& vector) const;
457 inline const T& operator[](const
unsigned int index) const noexcept;
464 inline T& operator[](const
unsigned int index) noexcept;
471 inline const T& operator()(const
unsigned int index) const noexcept;
478 inline T& operator()(const
unsigned int index) noexcept;
484 inline const T* operator()() const noexcept;
490 inline T* operator()() noexcept;
497 inline
size_t operator()(const
VectorT3<T>& vector) const;
517 template <typename U>
526 template <typename U>
536 template <typename U>
548 static_assert(std::is_arithmetic<T>::value,
"VectorT3 only supports arithmetic types");
555 if (setToHomogeneous)
580 memcpy(
values_, valueArray,
sizeof(T) * 3);
594 values_[0] = vector.values_[0];
595 values_[1] = vector.values_[1];
596 values_[2] = vector.values_[2];
622 ocean_assert(
false &&
"Division by zero!");
626 const T factor = T(1) / len;
640 const T factor = T(1) / len;
654 const T factor = T(1) / len;
667 const T factor = T(1) / len;
706 const T thisLength =
length();
707 const T rightLength = right.
length();
711 ocean_assert(
false &&
"Invalid vector!");
724 ocean_assert(*
this * normal >= 0);
727 const VectorT3<T> result(normal * ((normal * *
this) * 2) - *
this);
728 ocean_assert(
int(
angle(normal) * 100) ==
int(result.
angle(normal) * 100));
729 ocean_assert(result * normal >= 0);
733 return normal * ((normal * *
this) * 2) - *
this;
740 ocean_assert(*
this * normal >= 0);
741 ocean_assert(index > T(0));
743 const T dot = normal * *
this;
745 const T sqrtValue = T(1) - (index * index) * (T(1) - dot * dot);
748 if (sqrtValue < T(0))
755 const T angle0 =
angle(normal);
756 const T angle1 = result.
angle(-normal);
764 return normal * (index * dot -
NumericT<T>::sqrt(T(1) - (index * index) * (T(1) - dot * dot))) - *
this * index;
788 if (std::is_same<double, Scalar>::value)
793 const T dotProduct = normalizedThis * normalizedRight;
798 ocean_assert_accuracy(debugResult == (!
isNull() && !right.
isNull() && crossVector.
isNull()));
981 T factor = T(1) / value;
991 T factor = T(1) / value;
1000template <
typename T>
1008template <
typename T>
1011 ocean_assert(index < 3u);
1015template <
typename T>
1018 ocean_assert(index < 3u);
1022template <
typename T>
1025 ocean_assert(index < 3u);
1029template <
typename T>
1032 ocean_assert(index < 3u);
1036template <
typename T>
1042template <
typename T>
1048template <
typename T>
1051 size_t seed = std::hash<T>{}(vector.
x());
1052 seed ^= std::hash<T>{}(vector.
y()) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
1053 seed ^= std::hash<T>{}(vector.
z()) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
1058template <
typename T>
1064template <
typename T>
1074 return std::move(vectors);
1081 return std::move(vectors);
1084template <
typename T>
1085template <
typename U>
1088 std::vector<VectorT3<T>> result;
1089 result.reserve(vectors.size());
1091 for (
typename std::vector<
VectorT3<U>>::const_iterator i = vectors.cbegin(); i != vectors.cend(); ++i)
1093 result.emplace_back(*i);
1113template <
typename T>
1114template <
typename U>
1117 std::vector<VectorT3<T>> result;
1118 result.reserve(vectors.size());
1120 for (
typename std::vector<
VectorT3<U>>::const_iterator i = vectors.cbegin(); i != vectors.cend(); ++i)
1122 result.emplace_back(*i);
1128template <
typename T>
1129template <
typename U>
1132 std::vector< VectorT3<T>> result;
1133 result.reserve(size);
1135 for (
size_t n = 0; n < size; ++n)
1137 result.emplace_back(vectors[n]);
1143template <
typename T>
1146 stream <<
"[" << vector.
x() <<
", " << vector.
y() <<
", " << vector.
z() <<
"]";
1151template <
bool tActive,
typename T>
1152MessageObject<tActive>&
operator<<(MessageObject<tActive>& messageObject,
const VectorT3<T>& vector)
1154 return messageObject <<
"[" << vector.x() <<
", " << vector.y() <<
", " << vector.z() <<
"]";
1157template <
bool tActive,
typename T>
1158MessageObject<tActive>&
operator<<(MessageObject<tActive>&& messageObject,
const VectorT3<T>& vector)
1160 return messageObject <<
"[" << vector.x() <<
", " << vector.y() <<
", " << vector.z() <<
"]";
This class provides basic numeric functionalities.
Definition Numeric.h:57
static T sin(const T value)
Returns the sine of a given value.
Definition Numeric.h:1568
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
This class implements a vector with two elements.
Definition Vector2.h:96
This class implements a vector with three elements.
Definition Vector3.h:97
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 Vector3.h:873
VectorT3< T > normalizedOrValue(const VectorT3< T > &value) const
Returns the normalized vector.
Definition Vector3.h:645
const T * operator()() const noexcept
Access operator.
Definition Vector3.h:1037
VectorT3< T > perpendicular() const
Returns a vector that is perpendicular to this vector.
Definition Vector3.h:768
bool operator!=(const VectorT3< T > &vector) const
Returns whether two vectors are not identical up to a small epsilon.
Definition Vector3.h:910
VectorT3< T > & operator*=(const T &value)
Multiplies and assigns this vector with a scalar.
Definition Vector3.h:968
const T & y() const noexcept
Returns the y value.
Definition Vector3.h:824
VectorT3< T > & operator-=(const VectorT3< T > &vector)
Subtracts and assigns two vectors.
Definition Vector3.h:940
bool normalize()
Normalizes this vector.
Definition Vector3.h:659
T operator*(const VectorT3< T > &vector) const
Returns the dot product of two vectors.
Definition Vector3.h:956
VectorT3< T > cross(const VectorT3< T > &vector) const
Returns the cross product of two vectors.
Definition Vector3.h:609
bool isOrthogonal(const VectorT3< T > &right) const
Returns whether two vectors are orthogonal.
Definition Vector3.h:806
const T & operator[](const unsigned int index) const noexcept
Element access operator.
Definition Vector3.h:1009
const T & x() const noexcept
Returns the x value.
Definition Vector3.h:812
VectorT2< T > xy() const noexcept
Returns the x and y component of the vector as new 2D vector.
Definition Vector3.h:848
VectorT3< T > normalizedOrZero() const
Returns the normalized vector.
Definition Vector3.h:631
VectorT3< T > refract(const VectorT3< T > &normal, const T index) const
Returns the refracted vector of this vector corresponding to a given normal vector.
Definition Vector3.h:737
VectorT3< T > normalized() const
Returns the normalized vector.
Definition Vector3.h:617
static VectorT3< T > minValue()
Returns a 3D vector with all elements set to NumericT::minValue().
Definition Vector3.h:1059
const T & z() const noexcept
Returns the z value.
Definition Vector3.h:836
VectorT3() noexcept
Creates a new 3D vector with undefined elements.
Definition Vector3.h:546
bool isNull() const
Returns whether this vector is a null vector up to a small epsilon.
Definition Vector3.h:866
bool isEqual(const VectorT3< T > &vector, const T eps) const
Returns whether two vectors are equal up to a specified epsilon.
Definition Vector3.h:879
T values_[3]
The three values of the vector, with element order x, y, z.
Definition Vector3.h:542
const T * data() const noexcept
Returns an pointer to the vector elements.
Definition Vector3.h:854
VectorT3< T > & operator/=(const T &value)
Divides and assigns this vector by a scalar.
Definition Vector3.h:987
T distance(const VectorT3< T > &right) const
Returns the distance between this 3D position and a second 3D position.
Definition Vector3.h:688
T sqrDistance(const VectorT3< T > &right) const
Returns the square distance between this 3D position and a second 3D position.
Definition Vector3.h:694
VectorT3< T > operator/(const T &value) const
Divides this vector by a scalar.
Definition Vector3.h:978
static VectorT3< T > maxValue()
Returns a 3D vector with all elements set to NumericT::maxValue().
Definition Vector3.h:1065
VectorT3< T > operator-() const
Returns the negated vector.
Definition Vector3.h:950
T length() const
Returns the length of the vector.
Definition Vector3.h:676
bool isParallel(const VectorT3< T > &right) const
Returns whether two vectors are parallel.
Definition Vector3.h:785
VectorT3< T > operator+(const VectorT3< T > &vector) const
Adds two vectors.
Definition Vector3.h:918
VectorT3< T > reflect(const VectorT3< T > &normal) const
Returns the reflected vector of this vector corresponding to a given normal vector.
Definition Vector3.h:721
VectorT3< T > & operator+=(const VectorT3< T > &vector)
Adds and assigns two vectors.
Definition Vector3.h:924
VectorT3 & operator=(const VectorT3< T > &vector)
Copy assigns a vector.
Definition Vector3.h:887
T Type
Definition of the used data type.
Definition Vector3.h:103
bool operator==(const VectorT3< T > &vector) const
Returns whether two vectors are identical up to a small epsilon.
Definition Vector3.h:902
T sqr() const
Returns the square of the vector length.
Definition Vector3.h:682
T angle(const VectorT3< T > &right) const
Returns the angle between this vector and a second vectors.
Definition Vector3.h:700
static std::vector< VectorT3< T > > vectors2vectors(std::vector< VectorT3< U > > &&vectors)
Converts vectors with specific data type to vectors with different data type.
Definition Vector3.h:1086
bool operator<(const VectorT3< T > &vector) const
Compares two vector objects and returns whether the left vector represents a smaller value than the r...
Definition Vector3.h:1001
std::vector< VectorI3 > VectorsI3
Definition of a vector holding VectorI3 objects.
Definition Vector3.h:86
VectorT3< int > VectorI3
Definition of a 3D vector with integer values.
Definition Vector3.h:50
std::vector< VectorF3 > VectorsF3
Definition of a vector holding VectorF3 objects.
Definition Vector3.h:79
VectorT3< Scalar > Vector3
Definition of a 3D vector.
Definition Vector3.h:29
std::vector< VectorD3 > VectorsD3
Definition of a vector holding VectorD3 objects.
Definition Vector3.h:72
VectorT3< float > VectorF3
Definition of a 3D vector with float values.
Definition Vector3.h:43
std::vector< VectorT3< T > > VectorsT3
Definition of a typename alias for vectors with VectorT3 objects.
Definition Vector3.h:58
VectorT3< double > VectorD3
Definition of a 3D vector with double values.
Definition Vector3.h:36
std::vector< Vector3 > Vectors3
Definition of a vector holding Vector3 objects.
Definition Vector3.h:65
The namespace covering the entire Ocean framework.
Definition Accessor.h:15
std::ostream & operator<<(std::ostream &stream, const HighPerformanceStatistic &highPerformanceStatistic)
Definition HighPerformanceTimer.h:963