1 inline void Vector3::normalize()
3 float len = (float)sqrt(x * x + y * y + z * z);
11 inline float &Vector3::operator[] (int idx)
13 return idx == 0 ? x : (idx == 1 ? y : z);
16 inline const float &Vector3::operator[] (int idx) const
18 return idx == 0 ? x : (idx == 1 ? y : z);
21 inline Vector3 operator -(const Vector3 &v)
23 return Vector3(-v.x, -v.y, -v.z);
26 inline Vector3 operator +(const Vector3 &a, const Vector3 &b)
28 return Vector3(a.x + b.x, a.y + b.y, a.z + b.z);
31 inline Vector3 operator -(const Vector3 &a, const Vector3 &b)
33 return Vector3(a.x - b.x, a.y - b.y, a.z - b.z);
36 inline Vector3 operator *(const Vector3 &a, const Vector3 &b)
38 return Vector3(a.x * b.x, a.y * b.y, a.z * b.z);
41 inline Vector3 operator /(const Vector3 &a, const Vector3 &b)
43 return Vector3(a.x / b.x, a.y / b.y, a.z / b.z);
46 inline Vector3 operator *(const Vector3 &v, float s)
48 return Vector3(v.x * s, v.y * s, v.z * s);
51 inline Vector3 operator *(float s, const Vector3 &v)
53 return Vector3(s * v.x, s * v.y, s * v.z);
56 inline Vector3 operator /(const Vector3 &v, float s)
58 return Vector3(v.x / s, v.y / s, v.z / s);
61 inline Vector3 operator /(float s, const Vector3 &v)
63 return Vector3(s / v.x, s / v.y, s / v.z);
66 inline Vector3 &operator +=(Vector3 &a, const Vector3 &b)
74 inline Vector3 &operator -=(Vector3 &a, const Vector3 &b)
82 inline Vector3 &operator *=(Vector3 &a, const Vector3 &b)
90 inline Vector3 &operator /=(Vector3 &a, const Vector3 &b)
98 inline Vector3 &operator *=(Vector3 &v, float s)
106 inline Vector3 &operator /=(Vector3 &v, float s)
114 inline bool operator ==(const Vector3 &a, const Vector3 &b)
116 return a.x == b.x && a.y == b.y && a.z == b.z;
119 inline bool operator !=(const Vector3 &a, const Vector3 &b)
124 inline float dot(const Vector3 &a, const Vector3 &b)
126 return a.x * b.x + a.y * b.y + a.z * b.z;
129 inline Vector3 cross(const Vector3 &a, const Vector3 &b)
131 return Vector3(a.y * b.z - a.z * b.y,
132 a.z * b.x - a.x * b.z,
133 a.x * b.y - a.y * b.x);
136 inline float length(const Vector3 &v)
138 return (float)sqrt(v.x * v.x + v.y * v.y + v.z * v.z);
141 inline float length_sq(const Vector3 &v)
143 return v.x * v.x + v.y * v.y + v.z * v.z;
146 inline Vector3 normalize(const Vector3 &v)
148 float len = length(v);
153 return Vector3(v.x / len, v.y / len, v.z / len);
156 inline Vector3 reflect(const Vector3 &v, const Vector3 &n)
158 return v - n * dot(n, v) * 2.0;
161 inline Vector3 refract(const Vector3 &v, const Vector3 &n, float ior)
163 float ndotv = dot(n, v);
164 float k = 1.0f - ior * ior * (1.0f - ndotv * ndotv);
168 return ior * v - (ior * ndotv + sqrt(k)) * n;
171 inline Vector3 refract(const Vector3 &v, const Vector3 &n, float from_ior, float to_ior)
173 if(to_ior == 0.0f) to_ior = 1.0f;
174 return refract(v, n, from_ior / to_ior);
177 inline float distance(const Vector3 &a, const Vector3 &b)
179 return length(a - b);
182 inline float distance_sq(const Vector3 &a, const Vector3 &b)
184 return length_sq(a - b);
187 inline Vector3 faceforward(const Vector3 &n, const Vector3 &vi, const Vector3 &ng)
189 return dot(ng, vi) < 0.0f ? n : -n;
192 inline Vector3 major(const Vector3 &v)
194 int m = major_idx(v);
200 inline int major_idx(const Vector3 &v)
202 return fabs(v.x) >= fabs(v.y) && fabs(v.x) > fabs(v.z) ? 0 :
203 (fabs(v.y) >= fabs(v.z) ? 1 : 2);
206 inline Vector3 proj_axis(const Vector3 &v, const Vector3 &axis)
208 return axis * dot(v, axis);
212 inline Vector3 rotate(const Vector3 &v, const Quaternion &q)
217 inline Vector3 rotate(const Vector3 &v, const Vector3 &axis, float angle)
222 inline Vector3 rotate(const Vector3 &v, const Vector3 &euler)