~diresu/blender/blender-command-port

« back to all changes in this revision

Viewing changes to intern/moto/include/MT_Vector3.inl

  • Committer: theeth
  • Date: 2008-10-14 16:52:04 UTC
  • Revision ID: vcs-imports@canonical.com-20081014165204-r32w2gm6s0osvdhn
copy back trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "MT_Optimize.h"
 
2
 
 
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];
 
5
    return *this;
 
6
}
 
7
 
 
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];
 
10
    return *this;
 
11
}
 
12
 
 
13
GEN_INLINE MT_Vector3& MT_Vector3::operator*=(MT_Scalar s) {
 
14
    m_co[0] *= s; m_co[1] *= s; m_co[2] *= s;
 
15
    return *this;
 
16
}
 
17
 
 
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;
 
21
}
 
22
 
 
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]);
 
25
}
 
26
 
 
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]);
 
29
}
 
30
 
 
31
GEN_INLINE MT_Vector3 operator-(const MT_Vector3& v) {
 
32
    return MT_Vector3(-v[0], -v[1], -v[2]);
 
33
}
 
34
 
 
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);
 
37
}
 
38
 
 
39
GEN_INLINE MT_Vector3 operator*(MT_Scalar s, const MT_Vector3& v) { return v * s; }
 
40
 
 
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);
 
44
}
 
45
 
 
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]);
 
48
}
 
49
 
 
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];
 
52
}
 
53
 
 
54
GEN_INLINE MT_Scalar MT_Vector3::length2() const { return dot(*this); }
 
55
GEN_INLINE MT_Scalar MT_Vector3::length() const { return sqrt(length2()); }
 
56
 
 
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]));
 
59
}
 
60
 
 
61
GEN_INLINE bool MT_Vector3::fuzzyZero() const {
 
62
        return MT_fuzzyZero(length2());
 
63
}
 
64
 
 
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));
 
68
    }
 
69
}
 
70
 
 
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)) : 
 
77
        *this / len; 
 
78
}
 
79
 
 
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;
 
82
}
 
83
 
 
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);
 
86
}
 
87
 
 
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);
 
92
}
 
93
 
 
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]);
 
98
}
 
99
 
 
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]);
 
104
}
 
105
 
 
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);
 
109
}
 
110
 
 
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);
 
116
}
 
117
 
 
118
GEN_INLINE MT_Scalar  MT_dot(const MT_Vector3& v1, const MT_Vector3& v2) { 
 
119
    return v1.dot(v2);
 
120
}
 
121
 
 
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(); }
 
124
 
 
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); 
 
128
}
 
129
 
 
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);
 
134
}