2 /******************************************************************
6 * Float-valued 3D vector class for Direct3D. *
8 * Copyright (c) 1996-1997 Microsoft Corp. All rights reserved. *
10 ******************************************************************/
14 // =====================================
16 // =====================================
19 _D3DVECTOR::_D3DVECTOR(D3DVALUE f)
25 _D3DVECTOR::_D3DVECTOR(D3DVALUE _x, D3DVALUE _y, D3DVALUE _z)
27 x = _x; y = _y; z = _z;
31 _D3DVECTOR::_D3DVECTOR(const D3DVALUE f[3])
33 x = f[0]; y = f[1]; z = f[2];
36 // =====================================
38 // =====================================
40 inline const D3DVALUE&
41 _D3DVECTOR::operator[](int i) const
47 _D3DVECTOR::operator[](int i)
53 // =====================================
54 // Assignment operators
55 // =====================================
58 _D3DVECTOR::operator += (const _D3DVECTOR& v)
60 x += v.x; y += v.y; z += v.z;
65 _D3DVECTOR::operator -= (const _D3DVECTOR& v)
67 x -= v.x; y -= v.y; z -= v.z;
72 _D3DVECTOR::operator *= (const _D3DVECTOR& v)
74 x *= v.x; y *= v.y; z *= v.z;
79 _D3DVECTOR::operator /= (const _D3DVECTOR& v)
81 x /= v.x; y /= v.y; z /= v.z;
86 _D3DVECTOR::operator *= (D3DVALUE s)
88 x *= s; y *= s; z *= s;
93 _D3DVECTOR::operator /= (D3DVALUE s)
95 x /= s; y /= s; z /= s;
100 operator + (const _D3DVECTOR& v)
106 operator - (const _D3DVECTOR& v)
108 return _D3DVECTOR(-v.x, -v.y, -v.z);
112 operator + (const _D3DVECTOR& v1, const _D3DVECTOR& v2)
114 return _D3DVECTOR(v1.x+v2.x, v1.y+v2.y, v1.z+v2.z);
118 operator - (const _D3DVECTOR& v1, const _D3DVECTOR& v2)
120 return _D3DVECTOR(v1.x-v2.x, v1.y-v2.y, v1.z-v2.z);
124 operator * (const _D3DVECTOR& v1, const _D3DVECTOR& v2)
126 return _D3DVECTOR(v1.x*v2.x, v1.y*v2.y, v1.z*v2.z);
130 operator / (const _D3DVECTOR& v1, const _D3DVECTOR& v2)
132 return _D3DVECTOR(v1.x/v2.x, v1.y/v2.y, v1.z/v2.z);
136 operator < (const _D3DVECTOR& v1, const _D3DVECTOR& v2)
138 return v1[0] < v2[0] && v1[1] < v2[1] && v1[2] < v2[2];
142 operator <= (const _D3DVECTOR& v1, const _D3DVECTOR& v2)
144 return v1[0] <= v2[0] && v1[1] <= v2[1] && v1[2] <= v2[2];
148 operator * (const _D3DVECTOR& v, D3DVALUE s)
150 return _D3DVECTOR(s*v.x, s*v.y, s*v.z);
154 operator * (D3DVALUE s, const _D3DVECTOR& v)
156 return _D3DVECTOR(s*v.x, s*v.y, s*v.z);
160 operator / (const _D3DVECTOR& v, D3DVALUE s)
162 return _D3DVECTOR(v.x/s, v.y/s, v.z/s);
166 operator == (const _D3DVECTOR& v1, const _D3DVECTOR& v2)
168 return v1.x==v2.x && v1.y==v2.y && v1.z == v2.z;
172 Magnitude (const _D3DVECTOR& v)
174 return (D3DVALUE) sqrt(SquareMagnitude(v));
178 SquareMagnitude (const _D3DVECTOR& v)
180 return v.x*v.x + v.y*v.y + v.z*v.z;
184 Normalize (const _D3DVECTOR& v)
186 return v / Magnitude(v);
190 Min (const _D3DVECTOR& v)
193 if (v.y < ret) ret = v.y;
194 if (v.z < ret) ret = v.z;
199 Max (const _D3DVECTOR& v)
202 if (ret < v.y) ret = v.y;
203 if (ret < v.z) ret = v.z;
208 Minimize (const _D3DVECTOR& v1, const _D3DVECTOR& v2)
210 return _D3DVECTOR( v1[0] < v2[0] ? v1[0] : v2[0],
211 v1[1] < v2[1] ? v1[1] : v2[1],
212 v1[2] < v2[2] ? v1[2] : v2[2]);
216 Maximize (const _D3DVECTOR& v1, const _D3DVECTOR& v2)
218 return _D3DVECTOR( v1[0] > v2[0] ? v1[0] : v2[0],
219 v1[1] > v2[1] ? v1[1] : v2[1],
220 v1[2] > v2[2] ? v1[2] : v2[2]);
224 DotProduct (const _D3DVECTOR& v1, const _D3DVECTOR& v2)
226 return v1.x*v2.x + v1.y * v2.y + v1.z*v2.z;
230 CrossProduct (const _D3DVECTOR& v1, const _D3DVECTOR& v2)
234 result[0] = v1[1] * v2[2] - v1[2] * v2[1];
235 result[1] = v1[2] * v2[0] - v1[0] * v2[2];
236 result[2] = v1[0] * v2[1] - v1[1] * v2[0];