10 #ifndef INCLUDED_ATOMSMATHVEC_H
11 #define INCLUDED_ATOMSMATHVEC_H
13 #include <AtomsMath/ImathMath.h>
14 #include <AtomsMath/ImathNamespace.h>
20 #if (defined _WIN32 || defined _WIN64) && defined _MSC_VER
22 # pragma warning(push)
23 # pragma warning(disable : 4290)
26 ATOMSMATH_INTERNAL_NAMESPACE_HEADER_ENTER
28 template <
class T>
class Vec2;
29 template <
class T>
class Vec3;
30 template <
class T>
class Vec4;
42 template <
class T>
class Vec2
54 ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14 T&
operator[] (
int i) noexcept;
57 ATOMSMATH_HOSTDEVICE constexpr
const T&
operator[] (
int i)
const noexcept;
63 ATOMSMATH_HOSTDEVICE
Vec2() noexcept;
66 ATOMSMATH_HOSTDEVICE constexpr
explicit Vec2 (T a) noexcept;
69 ATOMSMATH_HOSTDEVICE constexpr
Vec2 (T a, T b) noexcept;
72 ATOMSMATH_HOSTDEVICE constexpr
Vec2 (
const Vec2& v) noexcept;
75 template <
class S> ATOMSMATH_HOSTDEVICE constexpr
Vec2 (
const Vec2<S>& v) noexcept;
78 ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14
const Vec2&
operator= (
const Vec2& v) noexcept;
89 template <class S> ATOMSMATH_HOSTDEVICE
void setValue (S a, S b) noexcept;
92 template <class S> ATOMSMATH_HOSTDEVICE
void setValue (const
Vec2<S>& v) noexcept;
95 template <class S> ATOMSMATH_HOSTDEVICE
void getValue (S& a, S& b) const noexcept;
98 template <class S> ATOMSMATH_HOSTDEVICE
void getValue (
Vec2<S>& v) const noexcept;
101 ATOMSMATH_HOSTDEVICE T*
getValue() noexcept;
104 ATOMSMATH_HOSTDEVICE const T*
getValue() const noexcept;
112 template <class S> ATOMSMATH_HOSTDEVICE constexpr
bool operator== (const
Vec2<S>& v) const noexcept;
116 template <class S> ATOMSMATH_HOSTDEVICE constexpr
bool operator!= (const
Vec2<S>& v) const noexcept;
123 ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14
bool equalWithAbsError (const
Vec2<T>& v, T e) const noexcept;
130 ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14
bool equalWithRelError (const
Vec2<T>& v, T e) const noexcept;
133 ATOMSMATH_HOSTDEVICE constexpr T
dot (const
Vec2& v) const noexcept;
136 ATOMSMATH_HOSTDEVICE constexpr T operator^ (const
Vec2& v) const noexcept;
140 ATOMSMATH_HOSTDEVICE constexpr T
cross (const
Vec2& v) const noexcept;
144 ATOMSMATH_HOSTDEVICE constexpr T operator% (const
Vec2& v) const noexcept;
147 ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14 const
Vec2& operator+= (const
Vec2& v) noexcept;
150 ATOMSMATH_HOSTDEVICE constexpr
Vec2 operator+ (const
Vec2& v) const noexcept;
153 ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14 const
Vec2& operator-= (const
Vec2& v) noexcept;
156 ATOMSMATH_HOSTDEVICE constexpr
Vec2 operator- (const
Vec2& v) const noexcept;
159 ATOMSMATH_HOSTDEVICE constexpr
Vec2 operator-() const noexcept;
162 ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14 const
Vec2&
negate() noexcept;
165 ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14 const
Vec2& operator*= (const
Vec2& v) noexcept;
168 ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14 const
Vec2& operator*= (T a) noexcept;
171 ATOMSMATH_HOSTDEVICE constexpr
Vec2 operator* (const
Vec2& v) const noexcept;
174 ATOMSMATH_HOSTDEVICE constexpr
Vec2 operator* (T a) const noexcept;
177 ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14 const
Vec2& operator/= (const
Vec2& v) noexcept;
180 ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14 const
Vec2& operator/= (T a) noexcept;
183 ATOMSMATH_HOSTDEVICE constexpr
Vec2 operator/ (const
Vec2& v) const noexcept;
186 ATOMSMATH_HOSTDEVICE constexpr
Vec2 operator/ (T a) const noexcept;
194 ATOMSMATH_HOSTDEVICE T
length() const noexcept;
198 ATOMSMATH_HOSTDEVICE constexpr T
length2() const noexcept;
230 ATOMSMATH_HOSTDEVICE constexpr static T
baseTypeLowest() noexcept {
return std::numeric_limits<T>::lowest(); }
233 ATOMSMATH_HOSTDEVICE constexpr
static T
baseTypeMax() noexcept {
return std::numeric_limits<T>::max(); }
236 ATOMSMATH_HOSTDEVICE constexpr
static T
baseTypeSmallest() noexcept {
return std::numeric_limits<T>::min(); }
239 ATOMSMATH_HOSTDEVICE constexpr
static T
baseTypeEpsilon() noexcept {
return std::numeric_limits<T>::epsilon(); }
244 ATOMSMATH_HOSTDEVICE constexpr
static unsigned int dimensions() noexcept {
return 2; }
252 ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14 T lengthTiny() const noexcept;
271 ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14 T&
operator[] (
int i) noexcept;
274 ATOMSMATH_HOSTDEVICE constexpr
const T&
operator[] (
int i)
const noexcept;
280 ATOMSMATH_HOSTDEVICE
Vec3() noexcept;
283 ATOMSMATH_HOSTDEVICE constexpr
explicit Vec3 (T a) noexcept;
286 ATOMSMATH_HOSTDEVICE constexpr
Vec3 (T a, T b, T c) noexcept;
289 ATOMSMATH_HOSTDEVICE constexpr
Vec3 (
const Vec3& v) noexcept;
292 template <
class S> ATOMSMATH_HOSTDEVICE constexpr
Vec3 (
const Vec3<S>& v) noexcept;
297 template <
class S> ATOMSMATH_HOSTDEVICE
explicit constexpr
Vec3 (
const Vec4<S>& v) noexcept;
302 explicit ATOMSMATH_CONSTEXPR14
Vec3 (
const Vec4<S>& v, InfException);
305 ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14
const Vec3&
operator= (
const Vec3& v) noexcept;
316 template <class S> ATOMSMATH_HOSTDEVICE
void setValue (S a, S b, S c) noexcept;
319 template <class S> ATOMSMATH_HOSTDEVICE
void setValue (const
Vec3<S>& v) noexcept;
322 template <class S> ATOMSMATH_HOSTDEVICE
void getValue (S& a, S& b, S& c) const noexcept;
325 template <class S> ATOMSMATH_HOSTDEVICE
void getValue (
Vec3<S>& v) const noexcept;
328 ATOMSMATH_HOSTDEVICE T*
getValue() noexcept;
331 ATOMSMATH_HOSTDEVICE const T*
getValue() const noexcept;
339 template <class S> ATOMSMATH_HOSTDEVICE constexpr
bool operator== (const
Vec3<S>& v) const noexcept;
342 template <class S> ATOMSMATH_HOSTDEVICE constexpr
bool operator!= (const
Vec3<S>& v) const noexcept;
349 ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14
bool equalWithAbsError (const
Vec3<T>& v, T e) const noexcept;
356 ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14
bool equalWithRelError (const
Vec3<T>& v, T e) const noexcept;
359 ATOMSMATH_HOSTDEVICE constexpr T
dot (const
Vec3& v) const noexcept;
362 ATOMSMATH_HOSTDEVICE constexpr T operator^ (const
Vec3& v) const noexcept;
365 ATOMSMATH_HOSTDEVICE constexpr
Vec3 cross (const
Vec3& v) const noexcept;
368 ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14 const
Vec3& operator%= (const
Vec3& v) noexcept;
371 ATOMSMATH_HOSTDEVICE constexpr
Vec3 operator% (const
Vec3& v) const noexcept;
374 ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14 const
Vec3& operator+= (const
Vec3& v) noexcept;
377 ATOMSMATH_HOSTDEVICE constexpr
Vec3 operator+ (const
Vec3& v) const noexcept;
380 ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14 const
Vec3& operator-= (const
Vec3& v) noexcept;
383 ATOMSMATH_HOSTDEVICE constexpr
Vec3 operator- (const
Vec3& v) const noexcept;
386 ATOMSMATH_HOSTDEVICE constexpr
Vec3 operator-() const noexcept;
389 ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14 const
Vec3&
negate() noexcept;
392 ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14 const
Vec3& operator*= (const
Vec3& v) noexcept;
395 ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14 const
Vec3& operator*= (T a) noexcept;
398 ATOMSMATH_HOSTDEVICE constexpr
Vec3 operator* (const
Vec3& v) const noexcept;
401 ATOMSMATH_HOSTDEVICE constexpr
Vec3 operator* (T a) const noexcept;
404 ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14 const
Vec3& operator/= (const
Vec3& v) noexcept;
407 ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14 const
Vec3& operator/= (T a) noexcept;
410 ATOMSMATH_HOSTDEVICE constexpr
Vec3 operator/ (const
Vec3& v) const noexcept;
413 ATOMSMATH_HOSTDEVICE constexpr
Vec3 operator/ (T a) const noexcept;
421 ATOMSMATH_HOSTDEVICE T
length() const noexcept;
425 ATOMSMATH_HOSTDEVICE constexpr T
length2() const noexcept;
457 ATOMSMATH_HOSTDEVICE constexpr static T
baseTypeLowest() noexcept {
return std::numeric_limits<T>::lowest(); }
460 ATOMSMATH_HOSTDEVICE constexpr
static T
baseTypeMax() noexcept {
return std::numeric_limits<T>::max(); }
463 ATOMSMATH_HOSTDEVICE constexpr
static T
baseTypeSmallest() noexcept {
return std::numeric_limits<T>::min(); }
466 ATOMSMATH_HOSTDEVICE constexpr
static T
baseTypeEpsilon() noexcept {
return std::numeric_limits<T>::epsilon(); }
471 ATOMSMATH_HOSTDEVICE constexpr
static unsigned int dimensions() noexcept {
return 3; }
478 ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14 T lengthTiny() const noexcept;
497 ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14 T&
operator[] (
int i) noexcept;
500 ATOMSMATH_HOSTDEVICE constexpr
const T&
operator[] (
int i)
const noexcept;
506 ATOMSMATH_HOSTDEVICE
Vec4() noexcept;
509 ATOMSMATH_HOSTDEVICE constexpr
explicit Vec4 (T a) noexcept;
512 ATOMSMATH_HOSTDEVICE constexpr
Vec4 (T a, T b, T c, T d) noexcept;
515 ATOMSMATH_HOSTDEVICE constexpr
Vec4 (
const Vec4& v) noexcept;
518 template <
class S> ATOMSMATH_HOSTDEVICE constexpr
Vec4 (
const Vec4<S>& v) noexcept;
521 template <
class S> ATOMSMATH_HOSTDEVICE
explicit constexpr
Vec4 (
const Vec3<S>& v) noexcept;
524 ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14
const Vec4&
operator= (
const Vec4& v) noexcept;
535 template <class S> ATOMSMATH_HOSTDEVICE constexpr
bool operator== (const
Vec4<S>& v) const noexcept;
538 template <class S> ATOMSMATH_HOSTDEVICE constexpr
bool operator!= (const
Vec4<S>& v) const noexcept;
545 ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14
bool equalWithAbsError (const
Vec4<T>& v, T e) const noexcept;
552 ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14
bool equalWithRelError (const
Vec4<T>& v, T e) const noexcept;
555 ATOMSMATH_HOSTDEVICE constexpr T
dot (const
Vec4& v) const noexcept;
558 ATOMSMATH_HOSTDEVICE constexpr T operator^ (const
Vec4& v) const noexcept;
561 ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14 const
Vec4& operator+= (const
Vec4& v) noexcept;
564 ATOMSMATH_HOSTDEVICE constexpr
Vec4 operator+ (const
Vec4& v) const noexcept;
567 ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14 const
Vec4& operator-= (const
Vec4& v) noexcept;
570 ATOMSMATH_HOSTDEVICE constexpr
Vec4 operator- (const
Vec4& v) const noexcept;
573 ATOMSMATH_HOSTDEVICE constexpr
Vec4 operator-() const noexcept;
576 ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14 const
Vec4&
negate() noexcept;
579 ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14 const
Vec4& operator*= (const
Vec4& v) noexcept;
582 ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14 const
Vec4& operator*= (T a) noexcept;
585 ATOMSMATH_HOSTDEVICE constexpr
Vec4 operator* (const
Vec4& v) const noexcept;
588 ATOMSMATH_HOSTDEVICE constexpr
Vec4 operator* (T a) const noexcept;
591 ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14 const
Vec4& operator/= (const
Vec4& v) noexcept;
594 ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14 const
Vec4& operator/= (T a) noexcept;
597 ATOMSMATH_HOSTDEVICE constexpr
Vec4 operator/ (const
Vec4& v) const noexcept;
600 ATOMSMATH_HOSTDEVICE constexpr
Vec4 operator/ (T a) const noexcept;
608 ATOMSMATH_HOSTDEVICE T
length() const noexcept;
612 ATOMSMATH_HOSTDEVICE constexpr T
length2() const noexcept;
644 ATOMSMATH_HOSTDEVICE constexpr static T
baseTypeLowest() noexcept {
return std::numeric_limits<T>::lowest(); }
647 ATOMSMATH_HOSTDEVICE constexpr
static T
baseTypeMax() noexcept {
return std::numeric_limits<T>::max(); }
650 ATOMSMATH_HOSTDEVICE constexpr
static T
baseTypeSmallest() noexcept {
return std::numeric_limits<T>::min(); }
653 ATOMSMATH_HOSTDEVICE constexpr
static T
baseTypeEpsilon() noexcept {
return std::numeric_limits<T>::epsilon(); }
658 ATOMSMATH_HOSTDEVICE constexpr
static unsigned int dimensions() noexcept {
return 4; }
665 ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14 T lengthTiny() const noexcept;
669 template <class T> std::ostream& operator<< (std::ostream& s, const
Vec2<T>& v);
672 template <class T> std::ostream& operator<< (std::ostream& s, const
Vec3<T>& v);
675 template <class T> std::ostream& operator<< (std::ostream& s, const
Vec4<T>& v);
678 template <class T> ATOMSMATH_HOSTDEVICE constexpr
Vec2<T> operator* (T a, const
Vec2<T>& v) noexcept;
681 template <class T> ATOMSMATH_HOSTDEVICE constexpr
Vec3<T> operator* (T a, const
Vec3<T>& v) noexcept;
684 template <class T> ATOMSMATH_HOSTDEVICE constexpr
Vec4<T> operator* (T a, const
Vec4<T>& v) noexcept;
736 template <>
short Vec2<
short>::
length() const noexcept = delete;
745 template <>
int Vec2<
int>::
length() const noexcept = delete;
754 template <>
short Vec3<
short>::
length() const noexcept = delete;
763 template <>
int Vec3<
int>::
length() const noexcept = delete;
772 template <>
short Vec4<
short>::
length() const noexcept = delete;
781 template <>
int Vec4<
int>::
length() const noexcept = delete;
794 ATOMSMATH_CONSTEXPR14 inline T&
795 Vec2<T>::operator[] (
int i) noexcept
801 constexpr
inline const T&
827 template <
class T>
template <
class S> constexpr
inline Vec2<T>::Vec2 (
const Vec2<S>& v) noexcept
828 : x(T(v.x)), y(T(v.y))
833 ATOMSMATH_CONSTEXPR14
inline const Vec2<T>&
888 return (
const T*) &x;
893 constexpr
inline bool
896 return x == v.x && y == v.y;
901 constexpr
inline bool
904 return x != v.x || y != v.y;
908 ATOMSMATH_CONSTEXPR14
inline bool
911 for (
int i = 0; i < 2; i++)
912 if (!ATOMSMATH_INTERNAL_NAMESPACE::equalWithAbsError ((*
this)[i], v[i], e))
919 ATOMSMATH_CONSTEXPR14
inline bool
922 for (
int i = 0; i < 2; i++)
923 if (!ATOMSMATH_INTERNAL_NAMESPACE::equalWithRelError ((*
this)[i], v[i], e))
933 return x * v.x + y * v.y;
947 return x * v.y - y * v.x;
954 return x * v.y - y * v.x;
958 ATOMSMATH_CONSTEXPR14
inline const Vec2<T>&
970 return Vec2 (x + v.x, y + v.y);
974 ATOMSMATH_CONSTEXPR14
inline const Vec2<T>&
986 return Vec2 (x - v.x, y - v.y);
993 return Vec2 (-x, -y);
997 ATOMSMATH_CONSTEXPR14
inline const Vec2<T>&
1006 ATOMSMATH_CONSTEXPR14
inline const Vec2<T>&
1015 ATOMSMATH_CONSTEXPR14
inline const Vec2<T>&
1027 return Vec2 (x * v.x, y * v.y);
1034 return Vec2 (x * a, y * a);
1038 ATOMSMATH_CONSTEXPR14
inline const Vec2<T>&
1047 ATOMSMATH_CONSTEXPR14
inline const Vec2<T>&
1059 return Vec2 (x / v.x, y / v.y);
1066 return Vec2 (x / a, y / a);
1070 ATOMSMATH_CONSTEXPR14
inline T
1073 T absX = std::abs(x);
1074 T absY = std::abs(y);
1081 if (ATOMSMATH_UNLIKELY(max == T (0)))
1093 return max * std::sqrt (absX * absX + absY * absY);
1102 if (ATOMSMATH_UNLIKELY(
length2 < T (2) * std::numeric_limits<T>::min()))
1103 return lengthTiny();
1121 if (ATOMSMATH_LIKELY(l != T (0)))
1142 #ifdef ATOMS_USE_EXCEPTIONS
1143 if (ATOMSMATH_UNLIKELY(l == T (0)))
1144 throw std::domain_error (
"Cannot normalize null vector.");
1167 if (ATOMSMATH_UNLIKELY(l == T (0)))
1168 return Vec2 (T (0));
1170 return Vec2 (x / l, y / l);
1178 #ifdef ATOMS_USE_EXCEPTIONS
1179 if (ATOMSMATH_UNLIKELY(l == T (0)))
1180 throw std::domain_error (
"Cannot normalize null vector.");
1182 return Vec2 (x / l, y / l);
1190 return Vec2 (x / l, y / l);
1198 ATOMSMATH_CONSTEXPR14
inline T&
1205 constexpr
inline const T&
1227 : x(v.x), y(v.y), z(v.z)
1231 template <
class T>
template <
class S> constexpr
inline Vec3<T>::Vec3 (
const Vec3<S>& v) noexcept
1232 : x(T(v.x)), y(T(v.y)), z(T(v.z))
1237 ATOMSMATH_CONSTEXPR14
inline const Vec3<T>&
1246 template <
class T>
template <
class S> constexpr
inline Vec3<T>::Vec3 (
const Vec4<S>& v) noexcept
1247 : x(T(v.x/v.w)), y(T(v.y/v.w)), z(T(v.z/v.w))
1260 T absW = (vw >= T (0)) ? vw : -vw;
1265 #ifdef ATOMS_USE_EXCEPTIONS
1266 if (vx <= -m || vx >= m || vy <= -m || vy >= m || vz <= -m || vz >= m)
1267 throw std::domain_error (
"Cannot normalize point at infinity.");
1327 return (
const T*) &x;
1332 constexpr
inline bool
1335 return x == v.x && y == v.y && z == v.z;
1340 constexpr
inline bool
1343 return x != v.x || y != v.y || z != v.z;
1347 ATOMSMATH_CONSTEXPR14
inline bool
1350 for (
int i = 0; i < 3; i++)
1351 if (!ATOMSMATH_INTERNAL_NAMESPACE::equalWithAbsError ((*
this)[i], v[i], e))
1358 ATOMSMATH_CONSTEXPR14
inline bool
1361 for (
int i = 0; i < 3; i++)
1362 if (!ATOMSMATH_INTERNAL_NAMESPACE::equalWithRelError ((*
this)[i], v[i], e))
1372 return x * v.x + y * v.y + z * v.z;
1386 return Vec3 (y * v.z - z * v.y, z * v.x - x * v.z, x * v.y - y * v.x);
1390 ATOMSMATH_CONSTEXPR14
inline const Vec3<T>&
1393 T a = y * v.z - z * v.y;
1394 T b = z * v.x - x * v.z;
1395 T c = x * v.y - y * v.x;
1406 return Vec3 (y * v.z - z * v.y, z * v.x - x * v.z, x * v.y - y * v.x);
1410 ATOMSMATH_CONSTEXPR14
inline const Vec3<T>&
1423 return Vec3 (x + v.x, y + v.y, z + v.z);
1427 ATOMSMATH_CONSTEXPR14
inline const Vec3<T>&
1440 return Vec3 (x - v.x, y - v.y, z - v.z);
1447 return Vec3 (-x, -y, -z);
1451 ATOMSMATH_CONSTEXPR14
inline const Vec3<T>&
1461 ATOMSMATH_CONSTEXPR14
inline const Vec3<T>&
1471 ATOMSMATH_CONSTEXPR14
inline const Vec3<T>&
1484 return Vec3 (x * v.x, y * v.y, z * v.z);
1491 return Vec3 (x * a, y * a, z * a);
1495 ATOMSMATH_CONSTEXPR14
inline const Vec3<T>&
1505 ATOMSMATH_CONSTEXPR14
inline const Vec3<T>&
1518 return Vec3 (x / v.x, y / v.y, z / v.z);
1525 return Vec3 (x / a, y / a, z / a);
1529 ATOMSMATH_CONSTEXPR14
inline T
1532 T absX = (x >= T (0)) ? x : -x;
1533 T absY = (y >= T (0)) ? y : -y;
1534 T absZ = (z >= T (0)) ? z : -z;
1544 if (ATOMSMATH_UNLIKELY(max == T (0)))
1557 return max * std::sqrt (absX * absX + absY * absY + absZ * absZ);
1566 if (ATOMSMATH_UNLIKELY(
length2 < T (2) * std::numeric_limits<T>::min()))
1567 return lengthTiny();
1585 if (ATOMSMATH_LIKELY(l != T (0)))
1606 #ifdef ATOMS_USE_EXCEPTIONS
1607 if (ATOMSMATH_UNLIKELY(l == T (0)))
1608 throw std::domain_error (
"Cannot normalize null vector.");
1633 if (ATOMSMATH_UNLIKELY((l == T (0))))
1634 return Vec3 (T (0));
1636 return Vec3 (x / l, y / l, z / l);
1644 #ifdef ATOMS_USE_EXCEPTIONS
1645 if (ATOMSMATH_UNLIKELY(l == T (0)))
1646 throw std::domain_error (
"Cannot normalize null vector.");
1648 return Vec3 (x / l, y / l, z / l);
1656 return Vec3 (x / l, y / l, z / l);
1664 ATOMSMATH_CONSTEXPR14
inline T&
1671 constexpr
inline const T&
1683 : x(a), y(a), z(a), w(a)
1688 : x(a), y(b), z(c), w(d)
1693 : x(v.x), y(v.y), z(v.z), w(v.w)
1697 template <
class T>
template <
class S> constexpr
inline Vec4<T>::Vec4 (
const Vec4<S>& v) noexcept
1698 : x(T(v.x)), y(T(v.y)), z(T(v.z)), w(T(v.w))
1703 ATOMSMATH_CONSTEXPR14
inline const Vec4<T>&
1713 template <
class T>
template <
class S> constexpr
inline Vec4<T>::Vec4 (
const Vec3<S>& v) noexcept
1714 : x(T(v.x)), y(T(v.y)), z(T(v.z)), w(T(1))
1720 constexpr
inline bool
1723 return x == v.x && y == v.y && z == v.z && w == v.w;
1728 constexpr
inline bool
1731 return x != v.x || y != v.y || z != v.z || w != v.w;
1735 ATOMSMATH_CONSTEXPR14
inline bool
1738 for (
int i = 0; i < 4; i++)
1739 if (!ATOMSMATH_INTERNAL_NAMESPACE::equalWithAbsError ((*
this)[i], v[i], e))
1746 ATOMSMATH_CONSTEXPR14
inline bool
1749 for (
int i = 0; i < 4; i++)
1750 if (!ATOMSMATH_INTERNAL_NAMESPACE::equalWithRelError ((*
this)[i], v[i], e))
1760 return x * v.x + y * v.y + z * v.z + w * v.w;
1771 ATOMSMATH_CONSTEXPR14
inline const Vec4<T>&
1785 return Vec4 (x + v.x, y + v.y, z + v.z, w + v.w);
1789 ATOMSMATH_CONSTEXPR14
inline const Vec4<T>&
1803 return Vec4 (x - v.x, y - v.y, z - v.z, w - v.w);
1810 return Vec4 (-x, -y, -z, -w);
1814 ATOMSMATH_CONSTEXPR14
inline const Vec4<T>&
1825 ATOMSMATH_CONSTEXPR14
inline const Vec4<T>&
1836 ATOMSMATH_CONSTEXPR14
inline const Vec4<T>&
1850 return Vec4 (x * v.x, y * v.y, z * v.z, w * v.w);
1857 return Vec4 (x * a, y * a, z * a, w * a);
1861 ATOMSMATH_CONSTEXPR14
inline const Vec4<T>&
1872 ATOMSMATH_CONSTEXPR14
inline const Vec4<T>&
1886 return Vec4 (x / v.x, y / v.y, z / v.z, w / v.w);
1893 return Vec4 (x / a, y / a, z / a, w / a);
1897 ATOMSMATH_CONSTEXPR14
inline T
1900 T absX = (x >= T (0)) ? x : -x;
1901 T absY = (y >= T (0)) ? y : -y;
1902 T absZ = (z >= T (0)) ? z : -z;
1903 T absW = (w >= T (0)) ? w : -w;
1916 if (ATOMSMATH_UNLIKELY(max == T (0)))
1930 return max * std::sqrt (absX * absX + absY * absY + absZ * absZ + absW * absW);
1939 if (ATOMSMATH_UNLIKELY(
length2 < T (2) * std::numeric_limits<T>::min()))
1940 return lengthTiny();
1958 if (ATOMSMATH_LIKELY(l != T (0)))
1980 #ifdef ATOMS_USE_EXCEPTIONS
1981 if (ATOMSMATH_UNLIKELY(l == T (0)))
1982 throw std::domain_error (
"Cannot normalize null vector.");
2009 if (ATOMSMATH_UNLIKELY(l == T (0)))
2010 return Vec4 (T (0));
2012 return Vec4 (x / l, y / l, z / l, w / l);
2020 #ifdef ATOMS_USE_EXCEPTIONS
2021 if (ATOMSMATH_UNLIKELY(l == T (0)))
2022 throw std::domain_error (
"Cannot normalize null vector.");
2024 return Vec4 (x / l, y / l, z / l, w / l);
2032 return Vec4 (x / l, y / l, z / l, w / l);
2041 operator<< (std::ostream& s,
const Vec2<T>& v)
2043 return s <<
'(' << v.x <<
", " << v.y <<
')';
2048 operator<< (std::ostream& s,
const Vec3<T>& v)
2050 return s <<
'(' << v.x <<
", " << v.y <<
", " << v.z <<
')';
2055 operator<< (std::ostream& s,
const Vec4<T>& v)
2057 return s <<
'(' << v.x <<
", " << v.y <<
", " << v.z <<
", " << v.w <<
')';
2068 return Vec2<T> (a * v.x, a * v.y);
2075 return Vec3<T> (a * v.x, a * v.y, a * v.z);
2082 return Vec4<T> (a * v.x, a * v.y, a * v.z, a * v.w);
2085 #if (defined _WIN32 || defined _WIN64) && defined _MSC_VER
2086 # pragma warning(pop)
2089 ATOMSMATH_INTERNAL_NAMESPACE_HEADER_EXIT
Definition: ImathVec.h:43
constexpr ATOMSMATH_HOSTDEVICE T dot(const Vec2 &v) const noexcept
Dot product.
Definition: ImathVec.h:931
ATOMSMATH_HOSTDEVICE const Vec2 & normalize() noexcept
Normalize in place. If length()==0, return a null vector.
Definition: ImathVec.h:1117
constexpr ATOMSMATH_HOSTDEVICE T operator^(const Vec2 &v) const noexcept
Dot product.
Definition: ImathVec.h:938
ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14 bool equalWithRelError(const Vec2< T > &v, T e) const noexcept
Definition: ImathVec.h:920
ATOMSMATH_HOSTDEVICE Vec2< T > normalizedNonNull() const noexcept
Definition: ImathVec.h:1187
ATOMSMATH_HOSTDEVICE const Vec2 & normalizeNonNull() noexcept
Definition: ImathVec.h:1153
ATOMSMATH_HOSTDEVICE Vec2< T > normalized() const noexcept
Return a normalized vector. Does not modify *this.
Definition: ImathVec.h:1163
ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14 const Vec2 & operator=(const Vec2 &v) noexcept
Assignment.
Definition: ImathVec.h:834
constexpr ATOMSMATH_HOSTDEVICE T length2() const noexcept
Definition: ImathVec.h:1110
constexpr ATOMSMATH_HOSTDEVICE T cross(const Vec2 &v) const noexcept
Definition: ImathVec.h:945
ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14 bool equalWithAbsError(const Vec2< T > &v, T e) const noexcept
Definition: ImathVec.h:909
T BaseType
Definition: ImathVec.h:248
Vec2< T > normalizedExc() const
Definition: ImathVec.h:1175
ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14 const Vec2 & operator-=(const Vec2 &v) noexcept
Component-wise subtraction.
Definition: ImathVec.h:975
constexpr ATOMSMATH_HOSTDEVICE Vec2 operator*(const Vec2 &v) const noexcept
Component-wise multiplication.
Definition: ImathVec.h:1025
~Vec2() noexcept=default
Destructor.
constexpr ATOMSMATH_HOSTDEVICE Vec2 operator-() const noexcept
Component-wise multiplication by -1.
Definition: ImathVec.h:991
constexpr ATOMSMATH_HOSTDEVICE bool operator==(const Vec2< S > &v) const noexcept
Equality.
ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14 const Vec2 & operator*=(const Vec2 &v) noexcept
Component-wise multiplication.
Definition: ImathVec.h:1007
constexpr ATOMSMATH_HOSTDEVICE Vec2 operator+(const Vec2 &v) const noexcept
Component-wise addition.
Definition: ImathVec.h:968
constexpr static ATOMSMATH_HOSTDEVICE T baseTypeLowest() noexcept
Largest possible negative value.
Definition: ImathVec.h:230
ATOMSMATH_HOSTDEVICE T length() const noexcept
Return the Euclidean norm.
Definition: ImathVec.h:1098
constexpr ATOMSMATH_HOSTDEVICE bool operator!=(const Vec2< S > &v) const noexcept
Inequality.
constexpr static ATOMSMATH_HOSTDEVICE unsigned int dimensions() noexcept
Return the number of dimensions, i.e. 2.
Definition: ImathVec.h:244
constexpr ATOMSMATH_HOSTDEVICE Vec2(const Vec2< S > &v) noexcept
Construct from Vec2 of another base type.
constexpr ATOMSMATH_HOSTDEVICE Vec2 operator/(const Vec2 &v) const noexcept
Component-wise division.
Definition: ImathVec.h:1057
constexpr static ATOMSMATH_HOSTDEVICE T baseTypeMax() noexcept
Largest possible positive value.
Definition: ImathVec.h:233
ATOMSMATH_HOSTDEVICE Vec2() noexcept
Uninitialized by default.
Definition: ImathVec.h:807
ATOMSMATH_HOSTDEVICE T * getValue() noexcept
Return a raw pointer to the array of values.
Definition: ImathVec.h:879
ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14 const Vec2 & negate() noexcept
Component-wise multiplication by -1.
Definition: ImathVec.h:998
constexpr ATOMSMATH_HOSTDEVICE T operator%(const Vec2 &v) const noexcept
Definition: ImathVec.h:952
const Vec2 & normalizeExc()
Normalize in place. If length()==0, throw an exception.
Definition: ImathVec.h:1138
ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14 const Vec2 & operator/=(const Vec2 &v) noexcept
Component-wise division.
Definition: ImathVec.h:1039
ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14 T & operator[](int i) noexcept
Element access by index.
Definition: ImathVec.h:795
constexpr static ATOMSMATH_HOSTDEVICE T baseTypeEpsilon() noexcept
Smallest possible e for which 1+e != 1.
Definition: ImathVec.h:239
ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14 const Vec2 & operator+=(const Vec2 &v) noexcept
Component-wise addition.
Definition: ImathVec.h:959
constexpr static ATOMSMATH_HOSTDEVICE T baseTypeSmallest() noexcept
Smallest possible positive value.
Definition: ImathVec.h:236
ATOMSMATH_HOSTDEVICE void setValue(S a, S b) noexcept
Set the value.
Definition: ImathVec.h:260
ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14 const Vec3 & operator=(const Vec3 &v) noexcept
Assignment.
Definition: ImathVec.h:1238
ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14 bool equalWithRelError(const Vec3< T > &v, T e) const noexcept
Definition: ImathVec.h:1359
constexpr ATOMSMATH_HOSTDEVICE Vec3 operator%(const Vec3 &v) const noexcept
Right-handed cross product.
Definition: ImathVec.h:1404
~Vec3() noexcept=default
Destructor.
constexpr ATOMSMATH_HOSTDEVICE Vec3 operator+(const Vec3 &v) const noexcept
Component-wise addition.
Definition: ImathVec.h:1421
constexpr ATOMSMATH_HOSTDEVICE T operator^(const Vec3 &v) const noexcept
Dot product.
Definition: ImathVec.h:1377
constexpr static ATOMSMATH_HOSTDEVICE T baseTypeSmallest() noexcept
Smallest possible positive value.
Definition: ImathVec.h:463
ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14 T & operator[](int i) noexcept
Element access by index.
Definition: ImathVec.h:1199
ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14 bool equalWithAbsError(const Vec3< T > &v, T e) const noexcept
Definition: ImathVec.h:1348
T BaseType
Definition: ImathVec.h:475
constexpr ATOMSMATH_HOSTDEVICE Vec3 operator/(const Vec3 &v) const noexcept
Component-wise division.
Definition: ImathVec.h:1516
ATOMSMATH_HOSTDEVICE T * getValue() noexcept
Return a raw pointer to the array of values.
Definition: ImathVec.h:1318
ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14 const Vec3 & operator/=(const Vec3 &v) noexcept
Component-wise division.
Definition: ImathVec.h:1496
constexpr ATOMSMATH_HOSTDEVICE Vec3 cross(const Vec3 &v) const noexcept
Right-handed cross product.
Definition: ImathVec.h:1384
ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14 const Vec3 & operator+=(const Vec3 &v) noexcept
Component-wise addition.
Definition: ImathVec.h:1411
constexpr ATOMSMATH_HOSTDEVICE bool operator==(const Vec3< S > &v) const noexcept
Equality.
ATOMSMATH_HOSTDEVICE Vec3() noexcept
Uninitialized by default.
Definition: ImathVec.h:1211
ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14 const Vec3 & negate() noexcept
Component-wise multiplication by -1.
Definition: ImathVec.h:1452
ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14 const Vec3 & operator*=(const Vec3 &v) noexcept
Component-wise multiplication.
Definition: ImathVec.h:1462
constexpr ATOMSMATH_HOSTDEVICE T dot(const Vec3 &v) const noexcept
Dot product.
Definition: ImathVec.h:1370
constexpr ATOMSMATH_HOSTDEVICE T length2() const noexcept
Definition: ImathVec.h:1574
ATOMSMATH_HOSTDEVICE Vec3< T > normalized() const noexcept
Return a normalized vector. Does not modify *this.
Definition: ImathVec.h:1629
ATOMSMATH_HOSTDEVICE T length() const noexcept
Return the Euclidean norm.
Definition: ImathVec.h:1562
constexpr ATOMSMATH_HOSTDEVICE bool operator!=(const Vec3< S > &v) const noexcept
Inequality.
constexpr static ATOMSMATH_HOSTDEVICE T baseTypeMax() noexcept
Largest possible positive value.
Definition: ImathVec.h:460
constexpr static ATOMSMATH_HOSTDEVICE unsigned int dimensions() noexcept
Return the number of dimensions, i.e. 3.
Definition: ImathVec.h:471
ATOMSMATH_HOSTDEVICE const Vec3 & normalize() noexcept
Normalize in place. If length()==0, return a null vector.
Definition: ImathVec.h:1581
constexpr static ATOMSMATH_HOSTDEVICE T baseTypeEpsilon() noexcept
Smallest possible e for which 1+e != 1.
Definition: ImathVec.h:466
ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14 const Vec3 & operator-=(const Vec3 &v) noexcept
Component-wise subtraction.
Definition: ImathVec.h:1428
Vec3< T > normalizedExc() const
Definition: ImathVec.h:1641
constexpr ATOMSMATH_HOSTDEVICE Vec3 operator-() const noexcept
Component-wise multiplication by -1.
Definition: ImathVec.h:1445
constexpr ATOMSMATH_HOSTDEVICE Vec3(const Vec3< S > &v) noexcept
Construct from Vec3 of another base type.
ATOMSMATH_HOSTDEVICE Vec3< T > normalizedNonNull() const noexcept
Definition: ImathVec.h:1653
constexpr ATOMSMATH_HOSTDEVICE Vec3(const Vec4< S > &v) noexcept
constexpr ATOMSMATH_HOSTDEVICE Vec3 operator*(const Vec3 &v) const noexcept
Component-wise multiplication.
Definition: ImathVec.h:1482
ATOMSMATH_HOSTDEVICE void setValue(S a, S b, S c) noexcept
Set the value.
ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14 const Vec3 & operator%=(const Vec3 &v) noexcept
Right-handed cross product.
Definition: ImathVec.h:1391
const Vec3 & normalizeExc()
Normalize in place. If length()==0, throw an exception.
Definition: ImathVec.h:1603
ATOMSMATH_HOSTDEVICE const Vec3 & normalizeNonNull() noexcept
Definition: ImathVec.h:1618
Definition: ImathVec.h:486
constexpr ATOMSMATH_HOSTDEVICE bool operator!=(const Vec4< S > &v) const noexcept
Inequality.
~Vec4() noexcept=default
Destructor.
constexpr ATOMSMATH_HOSTDEVICE Vec4 operator-() const noexcept
Component-wise multiplication by -1.
Definition: ImathVec.h:1808
constexpr ATOMSMATH_HOSTDEVICE T length2() const noexcept
Definition: ImathVec.h:1947
ATOMSMATH_HOSTDEVICE T length() const noexcept
Return the Euclidean norm.
Definition: ImathVec.h:1935
constexpr static ATOMSMATH_HOSTDEVICE unsigned int dimensions() noexcept
Return the number of dimensions, i.e. 4.
Definition: ImathVec.h:658
constexpr ATOMSMATH_HOSTDEVICE Vec4 operator*(const Vec4 &v) const noexcept
Component-wise multiplication.
Definition: ImathVec.h:1848
Vec4< T > normalizedExc() const
Definition: ImathVec.h:2017
ATOMSMATH_HOSTDEVICE Vec4() noexcept
Uninitialized by default.
Definition: ImathVec.h:1677
ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14 const Vec4 & operator+=(const Vec4 &v) noexcept
Component-wise addition.
Definition: ImathVec.h:1772
ATOMSMATH_HOSTDEVICE Vec4< T > normalizedNonNull() const noexcept
Definition: ImathVec.h:2029
ATOMSMATH_HOSTDEVICE const Vec4 & normalize() noexcept
Normalize in place. If length()==0, return a null vector.
Definition: ImathVec.h:1954
constexpr ATOMSMATH_HOSTDEVICE Vec4 operator+(const Vec4 &v) const noexcept
Component-wise addition.
Definition: ImathVec.h:1783
ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14 const Vec4 & operator=(const Vec4 &v) noexcept
Assignment.
Definition: ImathVec.h:1704
constexpr ATOMSMATH_HOSTDEVICE T dot(const Vec4 &v) const noexcept
Dot product.
Definition: ImathVec.h:1758
ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14 const Vec4 & operator*=(const Vec4 &v) noexcept
Component-wise multiplication.
Definition: ImathVec.h:1826
constexpr ATOMSMATH_HOSTDEVICE Vec4 operator/(const Vec4 &v) const noexcept
Component-wise division.
Definition: ImathVec.h:1884
constexpr ATOMSMATH_HOSTDEVICE Vec4(const Vec3< S > &v) noexcept
Vec3 to Vec4 conversion, sets w to 1.
ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14 bool equalWithAbsError(const Vec4< T > &v, T e) const noexcept
Definition: ImathVec.h:1736
ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14 T & operator[](int i) noexcept
Element access by index.
Definition: ImathVec.h:1665
ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14 const Vec4 & operator-=(const Vec4 &v) noexcept
Component-wise subtraction.
Definition: ImathVec.h:1790
constexpr static ATOMSMATH_HOSTDEVICE T baseTypeMax() noexcept
Largest possible positive value.
Definition: ImathVec.h:647
constexpr static ATOMSMATH_HOSTDEVICE T baseTypeEpsilon() noexcept
Smallest possible e for which 1+e != 1.
Definition: ImathVec.h:653
ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14 const Vec4 & operator/=(const Vec4 &v) noexcept
Component-wise division.
Definition: ImathVec.h:1862
constexpr ATOMSMATH_HOSTDEVICE bool operator==(const Vec4< S > &v) const noexcept
Equality.
ATOMSMATH_HOSTDEVICE const Vec4 & normalizeNonNull() noexcept
Definition: ImathVec.h:1993
T BaseType
Definition: ImathVec.h:662
constexpr static ATOMSMATH_HOSTDEVICE T baseTypeSmallest() noexcept
Smallest possible positive value.
Definition: ImathVec.h:650
const Vec4 & normalizeExc()
Normalize in place. If length()==0, throw an exception.
Definition: ImathVec.h:1977
ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14 const Vec4 & negate() noexcept
Component-wise multiplication by -1.
Definition: ImathVec.h:1815
ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14 bool equalWithRelError(const Vec4< T > &v, T e) const noexcept
Definition: ImathVec.h:1747
ATOMSMATH_HOSTDEVICE Vec4< T > normalized() const noexcept
Return a normalized vector. Does not modify *this.
Definition: ImathVec.h:2005
constexpr ATOMSMATH_HOSTDEVICE Vec4(const Vec4< S > &v) noexcept
Construct from Vec4 of another base type.
constexpr ATOMSMATH_HOSTDEVICE T operator^(const Vec4 &v) const noexcept
Dot product.
Definition: ImathVec.h:1765