annotate lib/legacy_dx/d3dvec.inl @ 746:852ec78d9548

Слияние
author Ritor1
date Fri, 22 Mar 2013 23:46:32 +0600
parents 9c0607679772
children
rev   line source
0
Ritor1
parents:
diff changeset
1
Ritor1
parents:
diff changeset
2 /******************************************************************
Ritor1
parents:
diff changeset
3 * *
Ritor1
parents:
diff changeset
4 * D3DVec.inl *
Ritor1
parents:
diff changeset
5 * *
Ritor1
parents:
diff changeset
6 * Float-valued 3D vector class for Direct3D. *
Ritor1
parents:
diff changeset
7 * *
Ritor1
parents:
diff changeset
8 * Copyright (c) Microsoft Corp. All rights reserved. *
Ritor1
parents:
diff changeset
9 * *
Ritor1
parents:
diff changeset
10 ******************************************************************/
Ritor1
parents:
diff changeset
11
Ritor1
parents:
diff changeset
12 #include <math.h>
Ritor1
parents:
diff changeset
13
Ritor1
parents:
diff changeset
14 // =====================================
Ritor1
parents:
diff changeset
15 // Constructors
Ritor1
parents:
diff changeset
16 // =====================================
Ritor1
parents:
diff changeset
17
Ritor1
parents:
diff changeset
18 inline
Ritor1
parents:
diff changeset
19 _D3DVECTOR::_D3DVECTOR(D3DVALUE f)
Ritor1
parents:
diff changeset
20 {
Ritor1
parents:
diff changeset
21 x = y = z = f;
Ritor1
parents:
diff changeset
22 }
Ritor1
parents:
diff changeset
23
Ritor1
parents:
diff changeset
24 inline
Ritor1
parents:
diff changeset
25 _D3DVECTOR::_D3DVECTOR(D3DVALUE _x, D3DVALUE _y, D3DVALUE _z)
Ritor1
parents:
diff changeset
26 {
Ritor1
parents:
diff changeset
27 x = _x; y = _y; z = _z;
Ritor1
parents:
diff changeset
28 }
Ritor1
parents:
diff changeset
29
Ritor1
parents:
diff changeset
30 inline
Ritor1
parents:
diff changeset
31 _D3DVECTOR::_D3DVECTOR(const D3DVALUE f[3])
Ritor1
parents:
diff changeset
32 {
Ritor1
parents:
diff changeset
33 x = f[0]; y = f[1]; z = f[2];
Ritor1
parents:
diff changeset
34 }
Ritor1
parents:
diff changeset
35
Ritor1
parents:
diff changeset
36 // =====================================
Ritor1
parents:
diff changeset
37 // Access grants
Ritor1
parents:
diff changeset
38 // =====================================
Ritor1
parents:
diff changeset
39
Ritor1
parents:
diff changeset
40 inline const D3DVALUE&
Ritor1
parents:
diff changeset
41 _D3DVECTOR::operator[](int i) const
Ritor1
parents:
diff changeset
42 {
Ritor1
parents:
diff changeset
43 return (&x)[i];
Ritor1
parents:
diff changeset
44 }
Ritor1
parents:
diff changeset
45
Ritor1
parents:
diff changeset
46 inline D3DVALUE&
Ritor1
parents:
diff changeset
47 _D3DVECTOR::operator[](int i)
Ritor1
parents:
diff changeset
48 {
Ritor1
parents:
diff changeset
49 return (&x)[i];
Ritor1
parents:
diff changeset
50 }
Ritor1
parents:
diff changeset
51
Ritor1
parents:
diff changeset
52
Ritor1
parents:
diff changeset
53 // =====================================
Ritor1
parents:
diff changeset
54 // Assignment operators
Ritor1
parents:
diff changeset
55 // =====================================
Ritor1
parents:
diff changeset
56
Ritor1
parents:
diff changeset
57 inline _D3DVECTOR&
Ritor1
parents:
diff changeset
58 _D3DVECTOR::operator += (const _D3DVECTOR& v)
Ritor1
parents:
diff changeset
59 {
Ritor1
parents:
diff changeset
60 x += v.x; y += v.y; z += v.z;
Ritor1
parents:
diff changeset
61 return *_this;
Ritor1
parents:
diff changeset
62 }
Ritor1
parents:
diff changeset
63
Ritor1
parents:
diff changeset
64 inline _D3DVECTOR&
Ritor1
parents:
diff changeset
65 _D3DVECTOR::operator -= (const _D3DVECTOR& v)
Ritor1
parents:
diff changeset
66 {
Ritor1
parents:
diff changeset
67 x -= v.x; y -= v.y; z -= v.z;
Ritor1
parents:
diff changeset
68 return *_this;
Ritor1
parents:
diff changeset
69 }
Ritor1
parents:
diff changeset
70
Ritor1
parents:
diff changeset
71 inline _D3DVECTOR&
Ritor1
parents:
diff changeset
72 _D3DVECTOR::operator *= (const _D3DVECTOR& v)
Ritor1
parents:
diff changeset
73 {
Ritor1
parents:
diff changeset
74 x *= v.x; y *= v.y; z *= v.z;
Ritor1
parents:
diff changeset
75 return *_this;
Ritor1
parents:
diff changeset
76 }
Ritor1
parents:
diff changeset
77
Ritor1
parents:
diff changeset
78 inline _D3DVECTOR&
Ritor1
parents:
diff changeset
79 _D3DVECTOR::operator /= (const _D3DVECTOR& v)
Ritor1
parents:
diff changeset
80 {
Ritor1
parents:
diff changeset
81 x /= v.x; y /= v.y; z /= v.z;
Ritor1
parents:
diff changeset
82 return *_this;
Ritor1
parents:
diff changeset
83 }
Ritor1
parents:
diff changeset
84
Ritor1
parents:
diff changeset
85 inline _D3DVECTOR&
Ritor1
parents:
diff changeset
86 _D3DVECTOR::operator *= (D3DVALUE s)
Ritor1
parents:
diff changeset
87 {
Ritor1
parents:
diff changeset
88 x *= s; y *= s; z *= s;
Ritor1
parents:
diff changeset
89 return *_this;
Ritor1
parents:
diff changeset
90 }
Ritor1
parents:
diff changeset
91
Ritor1
parents:
diff changeset
92 inline _D3DVECTOR&
Ritor1
parents:
diff changeset
93 _D3DVECTOR::operator /= (D3DVALUE s)
Ritor1
parents:
diff changeset
94 {
Ritor1
parents:
diff changeset
95 x /= s; y /= s; z /= s;
Ritor1
parents:
diff changeset
96 return *_this;
Ritor1
parents:
diff changeset
97 }
Ritor1
parents:
diff changeset
98
Ritor1
parents:
diff changeset
99 inline _D3DVECTOR
Ritor1
parents:
diff changeset
100 operator + (const _D3DVECTOR& v)
Ritor1
parents:
diff changeset
101 {
Ritor1
parents:
diff changeset
102 return v;
Ritor1
parents:
diff changeset
103 }
Ritor1
parents:
diff changeset
104
Ritor1
parents:
diff changeset
105 inline _D3DVECTOR
Ritor1
parents:
diff changeset
106 operator - (const _D3DVECTOR& v)
Ritor1
parents:
diff changeset
107 {
Ritor1
parents:
diff changeset
108 return _D3DVECTOR(-v.x, -v.y, -v.z);
Ritor1
parents:
diff changeset
109 }
Ritor1
parents:
diff changeset
110
Ritor1
parents:
diff changeset
111 inline _D3DVECTOR
Ritor1
parents:
diff changeset
112 operator + (const _D3DVECTOR& v1, const _D3DVECTOR& v2)
Ritor1
parents:
diff changeset
113 {
Ritor1
parents:
diff changeset
114 return _D3DVECTOR(v1.x+v2.x, v1.y+v2.y, v1.z+v2.z);
Ritor1
parents:
diff changeset
115 }
Ritor1
parents:
diff changeset
116
Ritor1
parents:
diff changeset
117 inline _D3DVECTOR
Ritor1
parents:
diff changeset
118 operator - (const _D3DVECTOR& v1, const _D3DVECTOR& v2)
Ritor1
parents:
diff changeset
119 {
Ritor1
parents:
diff changeset
120 return _D3DVECTOR(v1.x-v2.x, v1.y-v2.y, v1.z-v2.z);
Ritor1
parents:
diff changeset
121 }
Ritor1
parents:
diff changeset
122
Ritor1
parents:
diff changeset
123 inline _D3DVECTOR
Ritor1
parents:
diff changeset
124 operator * (const _D3DVECTOR& v1, const _D3DVECTOR& v2)
Ritor1
parents:
diff changeset
125 {
Ritor1
parents:
diff changeset
126 return _D3DVECTOR(v1.x*v2.x, v1.y*v2.y, v1.z*v2.z);
Ritor1
parents:
diff changeset
127 }
Ritor1
parents:
diff changeset
128
Ritor1
parents:
diff changeset
129 inline _D3DVECTOR
Ritor1
parents:
diff changeset
130 operator / (const _D3DVECTOR& v1, const _D3DVECTOR& v2)
Ritor1
parents:
diff changeset
131 {
Ritor1
parents:
diff changeset
132 return _D3DVECTOR(v1.x/v2.x, v1.y/v2.y, v1.z/v2.z);
Ritor1
parents:
diff changeset
133 }
Ritor1
parents:
diff changeset
134
Ritor1
parents:
diff changeset
135 inline int
Ritor1
parents:
diff changeset
136 operator < (const _D3DVECTOR& v1, const _D3DVECTOR& v2)
Ritor1
parents:
diff changeset
137 {
Ritor1
parents:
diff changeset
138 return v1[0] < v2[0] && v1[1] < v2[1] && v1[2] < v2[2];
Ritor1
parents:
diff changeset
139 }
Ritor1
parents:
diff changeset
140
Ritor1
parents:
diff changeset
141 inline int
Ritor1
parents:
diff changeset
142 operator <= (const _D3DVECTOR& v1, const _D3DVECTOR& v2)
Ritor1
parents:
diff changeset
143 {
Ritor1
parents:
diff changeset
144 return v1[0] <= v2[0] && v1[1] <= v2[1] && v1[2] <= v2[2];
Ritor1
parents:
diff changeset
145 }
Ritor1
parents:
diff changeset
146
Ritor1
parents:
diff changeset
147 inline _D3DVECTOR
Ritor1
parents:
diff changeset
148 operator * (const _D3DVECTOR& v, D3DVALUE s)
Ritor1
parents:
diff changeset
149 {
Ritor1
parents:
diff changeset
150 return _D3DVECTOR(s*v.x, s*v.y, s*v.z);
Ritor1
parents:
diff changeset
151 }
Ritor1
parents:
diff changeset
152
Ritor1
parents:
diff changeset
153 inline _D3DVECTOR
Ritor1
parents:
diff changeset
154 operator * (D3DVALUE s, const _D3DVECTOR& v)
Ritor1
parents:
diff changeset
155 {
Ritor1
parents:
diff changeset
156 return _D3DVECTOR(s*v.x, s*v.y, s*v.z);
Ritor1
parents:
diff changeset
157 }
Ritor1
parents:
diff changeset
158
Ritor1
parents:
diff changeset
159 inline _D3DVECTOR
Ritor1
parents:
diff changeset
160 operator / (const _D3DVECTOR& v, D3DVALUE s)
Ritor1
parents:
diff changeset
161 {
Ritor1
parents:
diff changeset
162 return _D3DVECTOR(v.x/s, v.y/s, v.z/s);
Ritor1
parents:
diff changeset
163 }
Ritor1
parents:
diff changeset
164
Ritor1
parents:
diff changeset
165 inline int
Ritor1
parents:
diff changeset
166 operator == (const _D3DVECTOR& v1, const _D3DVECTOR& v2)
Ritor1
parents:
diff changeset
167 {
Ritor1
parents:
diff changeset
168 return v1.x==v2.x && v1.y==v2.y && v1.z == v2.z;
Ritor1
parents:
diff changeset
169 }
Ritor1
parents:
diff changeset
170
Ritor1
parents:
diff changeset
171 inline D3DVALUE
Ritor1
parents:
diff changeset
172 Magnitude (const _D3DVECTOR& v)
Ritor1
parents:
diff changeset
173 {
Ritor1
parents:
diff changeset
174 return (D3DVALUE) sqrt(SquareMagnitude(v));
Ritor1
parents:
diff changeset
175 }
Ritor1
parents:
diff changeset
176
Ritor1
parents:
diff changeset
177 inline D3DVALUE
Ritor1
parents:
diff changeset
178 SquareMagnitude (const _D3DVECTOR& v)
Ritor1
parents:
diff changeset
179 {
Ritor1
parents:
diff changeset
180 return v.x*v.x + v.y*v.y + v.z*v.z;
Ritor1
parents:
diff changeset
181 }
Ritor1
parents:
diff changeset
182
Ritor1
parents:
diff changeset
183 inline _D3DVECTOR
Ritor1
parents:
diff changeset
184 Normalize (const _D3DVECTOR& v)
Ritor1
parents:
diff changeset
185 {
Ritor1
parents:
diff changeset
186 return v / Magnitude(v);
Ritor1
parents:
diff changeset
187 }
Ritor1
parents:
diff changeset
188
Ritor1
parents:
diff changeset
189 inline D3DVALUE
Ritor1
parents:
diff changeset
190 Min (const _D3DVECTOR& v)
Ritor1
parents:
diff changeset
191 {
Ritor1
parents:
diff changeset
192 D3DVALUE ret = v.x;
Ritor1
parents:
diff changeset
193 if (v.y < ret) ret = v.y;
Ritor1
parents:
diff changeset
194 if (v.z < ret) ret = v.z;
Ritor1
parents:
diff changeset
195 return ret;
Ritor1
parents:
diff changeset
196 }
Ritor1
parents:
diff changeset
197
Ritor1
parents:
diff changeset
198 inline D3DVALUE
Ritor1
parents:
diff changeset
199 Max (const _D3DVECTOR& v)
Ritor1
parents:
diff changeset
200 {
Ritor1
parents:
diff changeset
201 D3DVALUE ret = v.x;
Ritor1
parents:
diff changeset
202 if (ret < v.y) ret = v.y;
Ritor1
parents:
diff changeset
203 if (ret < v.z) ret = v.z;
Ritor1
parents:
diff changeset
204 return ret;
Ritor1
parents:
diff changeset
205 }
Ritor1
parents:
diff changeset
206
Ritor1
parents:
diff changeset
207 inline _D3DVECTOR
Ritor1
parents:
diff changeset
208 Minimize (const _D3DVECTOR& v1, const _D3DVECTOR& v2)
Ritor1
parents:
diff changeset
209 {
Ritor1
parents:
diff changeset
210 return _D3DVECTOR( v1[0] < v2[0] ? v1[0] : v2[0],
Ritor1
parents:
diff changeset
211 v1[1] < v2[1] ? v1[1] : v2[1],
Ritor1
parents:
diff changeset
212 v1[2] < v2[2] ? v1[2] : v2[2]);
Ritor1
parents:
diff changeset
213 }
Ritor1
parents:
diff changeset
214
Ritor1
parents:
diff changeset
215 inline _D3DVECTOR
Ritor1
parents:
diff changeset
216 Maximize (const _D3DVECTOR& v1, const _D3DVECTOR& v2)
Ritor1
parents:
diff changeset
217 {
Ritor1
parents:
diff changeset
218 return _D3DVECTOR( v1[0] > v2[0] ? v1[0] : v2[0],
Ritor1
parents:
diff changeset
219 v1[1] > v2[1] ? v1[1] : v2[1],
Ritor1
parents:
diff changeset
220 v1[2] > v2[2] ? v1[2] : v2[2]);
Ritor1
parents:
diff changeset
221 }
Ritor1
parents:
diff changeset
222
Ritor1
parents:
diff changeset
223 inline D3DVALUE
Ritor1
parents:
diff changeset
224 DotProduct (const _D3DVECTOR& v1, const _D3DVECTOR& v2)
Ritor1
parents:
diff changeset
225 {
Ritor1
parents:
diff changeset
226 return v1.x*v2.x + v1.y * v2.y + v1.z*v2.z;
Ritor1
parents:
diff changeset
227 }
Ritor1
parents:
diff changeset
228
Ritor1
parents:
diff changeset
229 inline _D3DVECTOR
Ritor1
parents:
diff changeset
230 CrossProduct (const _D3DVECTOR& v1, const _D3DVECTOR& v2)
Ritor1
parents:
diff changeset
231 {
Ritor1
parents:
diff changeset
232 _D3DVECTOR result;
Ritor1
parents:
diff changeset
233
Ritor1
parents:
diff changeset
234 result[0] = v1[1] * v2[2] - v1[2] * v2[1];
Ritor1
parents:
diff changeset
235 result[1] = v1[2] * v2[0] - v1[0] * v2[2];
Ritor1
parents:
diff changeset
236 result[2] = v1[0] * v2[1] - v1[1] * v2[0];
Ritor1
parents:
diff changeset
237
Ritor1
parents:
diff changeset
238 return result;
Ritor1
parents:
diff changeset
239 }
Ritor1
parents:
diff changeset
240
Ritor1
parents:
diff changeset
241 inline _D3DMATRIX
Ritor1
parents:
diff changeset
242 operator* (const _D3DMATRIX& a, const _D3DMATRIX& b)
Ritor1
parents:
diff changeset
243 {
Ritor1
parents:
diff changeset
244 _D3DMATRIX ret;
Ritor1
parents:
diff changeset
245 for (int i=0; i<4; i++) {
Ritor1
parents:
diff changeset
246 for (int j=0; j<4; j++) {
Ritor1
parents:
diff changeset
247 ret(i, j) = 0.0f;
Ritor1
parents:
diff changeset
248 for (int k=0; k<4; k++) {
Ritor1
parents:
diff changeset
249 ret(i, j) += a(i, k) * b(k, j);
Ritor1
parents:
diff changeset
250 }
Ritor1
parents:
diff changeset
251 }
Ritor1
parents:
diff changeset
252 }
Ritor1
parents:
diff changeset
253 return ret;
Ritor1
parents:
diff changeset
254 }
Ritor1
parents:
diff changeset
255