8 #ifndef META_OCEAN_MATH_VECTOR3_H
9 #define META_OCEAN_MATH_VECTOR3_H
15 #include <type_traits>
22 template <
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;
505 template <typename U>
514 template <typename U>
524 template <typename U>
533 template <typename T>
536 static_assert(std::is_arithmetic<T>::value,
"VectorT3 only supports arithmetic types");
540 template <
typename T>
543 if (setToHomogeneous)
557 template <
typename T>
565 template <
typename T>
568 memcpy(
values_, valueArray,
sizeof(T) * 3);
571 template <
typename T>
579 template <
typename T>
582 values_[0] = vector.values_[0];
583 values_[1] = vector.values_[1];
584 values_[2] = vector.values_[2];
587 template <
typename T>
588 template <
typename U>
596 template <
typename T>
604 template <
typename T>
610 ocean_assert(
false &&
"Division by zero!");
614 const T factor = T(1) / len;
618 template <
typename T>
628 const T factor = T(1) / len;
632 template <
typename T>
642 const T factor = T(1) / len;
646 template <
typename T>
655 const T factor = T(1) / len;
663 template <
typename T>
669 template <
typename T>
675 template <
typename T>
681 template <
typename T>
687 template <
typename T>
694 const T thisLength =
length();
695 const T rightLength = right.
length();
699 ocean_assert(
false &&
"Invalid vector!");
708 template <
typename T>
712 ocean_assert(*
this * normal >= 0);
715 const VectorT3<T> result(normal * ((normal * *
this) * 2) - *
this);
716 ocean_assert(
int(
angle(normal) * 100) ==
int(result.
angle(normal) * 100));
717 ocean_assert(result * normal >= 0);
721 return normal * ((normal * *
this) * 2) - *
this;
724 template <
typename T>
728 ocean_assert(*
this * normal >= 0);
729 ocean_assert(index > T(0));
731 const T dot = normal * *
this;
733 const T sqrtValue = T(1) - (index * index) * (T(1) - dot * dot);
736 if (sqrtValue < T(0))
743 const T angle0 =
angle(normal);
744 const T angle1 = result.
angle(-normal);
752 return normal * (index * dot -
NumericT<T>::sqrt(T(1) - (index * index) * (T(1) - dot * dot))) - *
this * index;
755 template <
typename T>
772 template <
typename T>
776 if (std::is_same<double, Scalar>::value)
781 const T dotProduct = normalizedThis * normalizedRight;
786 ocean_assert_accuracy(debugResult == (!
isNull() && !right.
isNull() && crossVector.
isNull()));
793 template <
typename T>
799 template <
typename T>
805 template <
typename T>
811 template <
typename T>
817 template <
typename T>
823 template <
typename T>
829 template <
typename T>
835 template <
typename T>
841 template <
typename T>
847 template <
typename T>
853 template <
typename T>
860 template <
typename T>
866 template <
typename T>
874 template <
typename T>
889 template <
typename T>
897 template <
typename T>
905 template <
typename T>
911 template <
typename T>
921 template <
typename T>
927 template <
typename T>
937 template <
typename T>
943 template <
typename T>
949 template <
typename T>
955 template <
typename T>
965 template <
typename T>
969 T factor = T(1) / value;
974 template <
typename T>
979 T factor = T(1) / value;
988 template <
typename T>
996 template <
typename T>
999 ocean_assert(index < 3u);
1003 template <
typename T>
1006 ocean_assert(index < 3u);
1010 template <
typename T>
1013 ocean_assert(index < 3u);
1017 template <
typename T>
1020 ocean_assert(index < 3u);
1024 template <
typename T>
1030 template <
typename T>
1036 template <
typename T>
1039 size_t seed = std::hash<T>{}(vector.
x());
1040 seed ^= std::hash<T>{}(vector.
y()) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
1041 seed ^= std::hash<T>{}(vector.
z()) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
1050 return std::move(vectors);
1057 return std::move(vectors);
1060 template <
typename T>
1061 template <
typename U>
1064 std::vector<VectorT3<T>> result;
1065 result.reserve(vectors.size());
1067 for (
typename std::vector<
VectorT3<U>>::const_iterator i = vectors.cbegin(); i != vectors.cend(); ++i)
1069 result.emplace_back(*i);
1089 template <
typename T>
1090 template <
typename U>
1093 std::vector<VectorT3<T>> result;
1094 result.reserve(vectors.size());
1096 for (
typename std::vector<
VectorT3<U>>::const_iterator i = vectors.cbegin(); i != vectors.cend(); ++i)
1098 result.emplace_back(*i);
1104 template <
typename T>
1105 template <
typename U>
1108 std::vector< VectorT3<T>> result;
1109 result.reserve(size);
1111 for (
size_t n = 0; n < size; ++n)
1113 result.emplace_back(vectors[n]);
1119 template <
typename T>
1122 stream <<
"[" << vector.
x() <<
", " << vector.
y() <<
", " << vector.
z() <<
"]";
1127 template <
bool tActive,
typename T>
1128 MessageObject<tActive>&
operator<<(MessageObject<tActive>& messageObject,
const VectorT3<T>& vector)
1130 return messageObject <<
"[" << vector.x() <<
", " << vector.y() <<
", " << vector.z() <<
"]";
1133 template <
bool tActive,
typename T>
1134 MessageObject<tActive>&
operator<<(MessageObject<tActive>&& messageObject,
const VectorT3<T>& vector)
1136 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
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:861
VectorT3< T > normalizedOrValue(const VectorT3< T > &value) const
Returns the normalized vector.
Definition: Vector3.h:633
const T * operator()() const noexcept
Access operator.
Definition: Vector3.h:1025
VectorT3< T > perpendicular() const
Returns a vector that is perpendicular to this vector.
Definition: Vector3.h:756
bool operator!=(const VectorT3< T > &vector) const
Returns whether two vectors are not identical up to a small epsilon.
Definition: Vector3.h:898
VectorT3< T > & operator*=(const T &value)
Multiplies and assigns this vector with a scalar.
Definition: Vector3.h:956
const T & y() const noexcept
Returns the y value.
Definition: Vector3.h:812
VectorT3< T > & operator-=(const VectorT3< T > &vector)
Subtracts and assigns two vectors.
Definition: Vector3.h:928
bool normalize()
Normalizes this vector.
Definition: Vector3.h:647
T operator*(const VectorT3< T > &vector) const
Returns the dot product of two vectors.
Definition: Vector3.h:944
VectorT3< T > cross(const VectorT3< T > &vector) const
Returns the cross product of two vectors.
Definition: Vector3.h:597
bool isOrthogonal(const VectorT3< T > &right) const
Returns whether two vectors are orthogonal.
Definition: Vector3.h:794
const T & operator[](const unsigned int index) const noexcept
Element access operator.
Definition: Vector3.h:997
const T & x() const noexcept
Returns the x value.
Definition: Vector3.h:800
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:1062
VectorT2< T > xy() const noexcept
Returns the x and y component of the vector as new 2D vector.
Definition: Vector3.h:836
VectorT3< T > normalizedOrZero() const
Returns the normalized vector.
Definition: Vector3.h:619
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:725
VectorT3< T > normalized() const
Returns the normalized vector.
Definition: Vector3.h:605
const T & z() const noexcept
Returns the z value.
Definition: Vector3.h:824
VectorT3() noexcept
Creates a new 3D vector with undefined elements.
Definition: Vector3.h:534
bool isNull() const
Returns whether this vector is a null vector up to a small epsilon.
Definition: Vector3.h:854
bool isEqual(const VectorT3< T > &vector, const T eps) const
Returns whether two vectors are equal up to a specified epsilon.
Definition: Vector3.h:867
T values_[3]
The three values of the vector, with element order x, y, z.
Definition: Vector3.h:530
const T * data() const noexcept
Returns an pointer to the vector elements.
Definition: Vector3.h:842
VectorT3< T > & operator/=(const T &value)
Divides and assigns this vector by a scalar.
Definition: Vector3.h:975
T distance(const VectorT3< T > &right) const
Returns the distance between this 3D position and a second 3D position.
Definition: Vector3.h:676
T sqrDistance(const VectorT3< T > &right) const
Returns the square distance between this 3D position and a second 3D position.
Definition: Vector3.h:682
VectorT3< T > operator/(const T &value) const
Divides this vector by a scalar.
Definition: Vector3.h:966
VectorT3< T > operator-() const
Returns the negated vector.
Definition: Vector3.h:938
T length() const
Returns the length of the vector.
Definition: Vector3.h:664
bool isParallel(const VectorT3< T > &right) const
Returns whether two vectors are parallel.
Definition: Vector3.h:773
VectorT3< T > operator+(const VectorT3< T > &vector) const
Adds two vectors.
Definition: Vector3.h:906
VectorT3< T > reflect(const VectorT3< T > &normal) const
Returns the reflected vector of this vector corresponding to a given normal vector.
Definition: Vector3.h:709
VectorT3< T > & operator+=(const VectorT3< T > &vector)
Adds and assigns two vectors.
Definition: Vector3.h:912
VectorT3 & operator=(const VectorT3< T > &vector)
Copy assigns a vector.
Definition: Vector3.h:875
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:890
T sqr() const
Returns the square of the vector length.
Definition: Vector3.h:670
T angle(const VectorT3< T > &right) const
Returns the angle between this vector and a second vectors.
Definition: Vector3.h:688
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:989
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:22
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