1 #include "MT_Optimize.h"
3 GEN_INLINE MT_Vector3& MT_Vector3::operator+=(const MT_Vector3& v) {
4 m_co[0] += v[0]; m_co[1] += v[1]; m_co[2] += v[2];
8 GEN_INLINE MT_Vector3& MT_Vector3::operator-=(const MT_Vector3& v) {
9 m_co[0] -= v[0]; m_co[1] -= v[1]; m_co[2] -= v[2];
13 GEN_INLINE MT_Vector3& MT_Vector3::operator*=(MT_Scalar s) {
14 m_co[0] *= s; m_co[1] *= s; m_co[2] *= s;
18 GEN_INLINE MT_Vector3& MT_Vector3::operator/=(MT_Scalar s) {
19 MT_assert(!MT_fuzzyZero(s));
20 return *this *= MT_Scalar(1.0) / s;
23 GEN_INLINE MT_Vector3 operator+(const MT_Vector3& v1, const MT_Vector3& v2) {
24 return MT_Vector3(v1[0] + v2[0], v1[1] + v2[1], v1[2] + v2[2]);
27 GEN_INLINE MT_Vector3 operator-(const MT_Vector3& v1, const MT_Vector3& v2) {
28 return MT_Vector3(v1[0] - v2[0], v1[1] - v2[1], v1[2] - v2[2]);
31 GEN_INLINE MT_Vector3 operator-(const MT_Vector3& v) {
32 return MT_Vector3(-v[0], -v[1], -v[2]);
35 GEN_INLINE MT_Vector3 operator*(const MT_Vector3& v, MT_Scalar s) {
36 return MT_Vector3(v[0] * s, v[1] * s, v[2] * s);
39 GEN_INLINE MT_Vector3 operator*(MT_Scalar s, const MT_Vector3& v) { return v * s; }
41 GEN_INLINE MT_Vector3 operator/(const MT_Vector3& v, MT_Scalar s) {
42 MT_assert(!MT_fuzzyZero(s));
43 return v * (MT_Scalar(1.0) / s);
46 GEN_INLINE MT_Vector3 operator*(const MT_Vector3& v1, const MT_Vector3& v2) {
47 return MT_Vector3(v1[0] * v2[0], v1[1] * v2[1], v1[2] * v2[2]);
50 GEN_INLINE MT_Scalar MT_Vector3::dot(const MT_Vector3& v) const {
51 return m_co[0] * v[0] + m_co[1] * v[1] + m_co[2] * v[2];
54 GEN_INLINE MT_Scalar MT_Vector3::length2() const { return dot(*this); }
55 GEN_INLINE MT_Scalar MT_Vector3::length() const { return sqrt(length2()); }
57 GEN_INLINE MT_Vector3 MT_Vector3::absolute() const {
58 return MT_Vector3(MT_abs(m_co[0]), MT_abs(m_co[1]), MT_abs(m_co[2]));
61 GEN_INLINE bool MT_Vector3::fuzzyZero() const {
62 return MT_fuzzyZero(length2());
65 GEN_INLINE void MT_Vector3::noiseGate(MT_Scalar threshold) {
66 if (length2() < threshold) {
67 setValue(MT_Scalar(0.0), MT_Scalar(0.0), MT_Scalar(0.0));
71 GEN_INLINE void MT_Vector3::normalize() { *this /= length(); }
72 GEN_INLINE MT_Vector3 MT_Vector3::normalized() const { return *this / length(); }
73 GEN_INLINE MT_Vector3 MT_Vector3::safe_normalized() const {
74 MT_Scalar len = length();
75 return MT_fuzzyZero(len) ?
76 MT_Vector3(MT_Scalar(1.0), MT_Scalar(0.0), MT_Scalar(0.0)) :
80 GEN_INLINE void MT_Vector3::scale(MT_Scalar xx, MT_Scalar yy, MT_Scalar zz) {
81 m_co[0] *= xx; m_co[1] *= yy; m_co[2] *= zz;
84 GEN_INLINE MT_Vector3 MT_Vector3::scaled(MT_Scalar xx, MT_Scalar yy, MT_Scalar zz) const {
85 return MT_Vector3(m_co[0] * xx, m_co[1] * yy, m_co[2] * zz);
88 GEN_INLINE MT_Scalar MT_Vector3::angle(const MT_Vector3& v) const {
89 MT_Scalar s = sqrt(length2() * v.length2());
90 MT_assert(!MT_fuzzyZero(s));
91 return acos(dot(v) / s);
94 GEN_INLINE MT_Vector3 MT_Vector3::cross(const MT_Vector3& v) const {
95 return MT_Vector3(m_co[1] * v[2] - m_co[2] * v[1],
96 m_co[2] * v[0] - m_co[0] * v[2],
97 m_co[0] * v[1] - m_co[1] * v[0]);
100 GEN_INLINE MT_Scalar MT_Vector3::triple(const MT_Vector3& v1, const MT_Vector3& v2) const {
101 return m_co[0] * (v1[1] * v2[2] - v1[2] * v2[1]) +
102 m_co[1] * (v1[2] * v2[0] - v1[0] * v2[2]) +
103 m_co[2] * (v1[0] * v2[1] - v1[1] * v2[0]);
106 GEN_INLINE int MT_Vector3::closestAxis() const {
107 MT_Vector3 a = absolute();
108 return a[0] < a[1] ? (a[1] < a[2] ? 2 : 1) : (a[0] < a[2] ? 2 : 0);
111 GEN_INLINE MT_Vector3 MT_Vector3::random() {
112 MT_Scalar z = MT_Scalar(2.0) * MT_random() - MT_Scalar(1.0);
113 MT_Scalar r = sqrt(MT_Scalar(1.0) - z * z);
114 MT_Scalar t = MT_2_PI * MT_random();
115 return MT_Vector3(r * cos(t), r * sin(t), z);
118 GEN_INLINE MT_Scalar MT_dot(const MT_Vector3& v1, const MT_Vector3& v2) {
122 GEN_INLINE MT_Scalar MT_length2(const MT_Vector3& v) { return v.length2(); }
123 GEN_INLINE MT_Scalar MT_length(const MT_Vector3& v) { return v.length(); }
125 GEN_INLINE bool MT_fuzzyZero(const MT_Vector3& v) { return v.fuzzyZero(); }
126 GEN_INLINE bool MT_fuzzyEqual(const MT_Vector3& v1, const MT_Vector3& v2) {
127 return MT_fuzzyZero(v1 - v2);
130 GEN_INLINE MT_Scalar MT_angle(const MT_Vector3& v1, const MT_Vector3& v2) { return v1.angle(v2); }
131 GEN_INLINE MT_Vector3 MT_cross(const MT_Vector3& v1, const MT_Vector3& v2) { return v1.cross(v2); }
132 GEN_INLINE MT_Scalar MT_triple(const MT_Vector3& v1, const MT_Vector3& v2, const MT_Vector3& v3) {
133 return v1.triple(v2, v3);