xref: /reactos/sdk/include/dxsdk/d3dvec.inl (revision d6eebaa4)
1
2#include <math.h>
3
4
5inline _D3DVECTOR::_D3DVECTOR(D3DVALUE f)
6{
7  x = y = z = f;
8}
9
10inline
11_D3DVECTOR::_D3DVECTOR(D3DVALUE _x, D3DVALUE _y, D3DVALUE _z)
12{
13  x = _x;
14  y = _y;
15  z = _z;
16}
17
18inline _D3DVECTOR::_D3DVECTOR(const D3DVALUE f[3])
19{
20  x = f[0];
21  y = f[1];
22  z = f[2];
23}
24
25inline const D3DVALUE& _D3DVECTOR::operator[](int i) const
26{
27  return (&x)[i];
28}
29
30inline D3DVALUE& _D3DVECTOR::operator[](int i)
31{
32  return (&x)[i];
33}
34
35inline _D3DVECTOR& _D3DVECTOR::operator += (const _D3DVECTOR& v)
36{
37  x += v.x;
38  y += v.y;
39  z += v.z;
40  return *this;
41}
42
43inline _D3DVECTOR& _D3DVECTOR::operator -= (const _D3DVECTOR& v)
44{
45  x -= v.x;
46  y -= v.y;
47  z -= v.z;
48  return *this;
49}
50
51inline _D3DVECTOR& _D3DVECTOR::operator *= (const _D3DVECTOR& v)
52{
53  x *= v.x;
54  y *= v.y;
55  z *= v.z;
56  return *this;
57}
58
59inline _D3DVECTOR& _D3DVECTOR::operator /= (const _D3DVECTOR& v)
60{
61  x /= v.x;
62  y /= v.y;
63  z /= v.z;
64  return *this;
65}
66
67inline _D3DVECTOR& _D3DVECTOR::operator *= (D3DVALUE s)
68{
69  x *= s;
70  y *= s;
71  z *= s;
72  return *this;
73}
74
75inline _D3DVECTOR& _D3DVECTOR::operator /= (D3DVALUE s)
76{
77  x /= s;
78  y /= s;
79  z /= s;
80   return *this;
81}
82
83inline _D3DVECTOR operator + (const _D3DVECTOR& v)
84{
85  return v;
86}
87
88inline _D3DVECTOR operator - (const _D3DVECTOR& v)
89{
90  return _D3DVECTOR(-v.x, -v.y, -v.z);
91}
92
93inline _D3DVECTOR operator + (const _D3DVECTOR& v1, const _D3DVECTOR& v2)
94{
95  return _D3DVECTOR(v1.x+v2.x, v1.y+v2.y, v1.z+v2.z);
96}
97
98inline _D3DVECTOR operator - (const _D3DVECTOR& v1, const _D3DVECTOR& v2)
99{
100  return _D3DVECTOR(v1.x-v2.x, v1.y-v2.y, v1.z-v2.z);
101}
102
103inline _D3DVECTOR operator * (const _D3DVECTOR& v1, const _D3DVECTOR& v2)
104{
105  return _D3DVECTOR(v1.x*v2.x, v1.y*v2.y, v1.z*v2.z);
106}
107
108inline _D3DVECTOR operator / (const _D3DVECTOR& v1, const _D3DVECTOR& v2)
109{
110  return _D3DVECTOR(v1.x/v2.x, v1.y/v2.y, v1.z/v2.z);
111}
112
113inline int operator < (const _D3DVECTOR& v1, const _D3DVECTOR& v2)
114{
115  return v1[0] < v2[0] && v1[1] < v2[1] && v1[2] < v2[2];
116}
117
118inline int operator <= (const _D3DVECTOR& v1, const _D3DVECTOR& v2)
119{
120  return v1[0] <= v2[0] && v1[1] <= v2[1] && v1[2] <= v2[2];
121}
122
123inline _D3DVECTOR operator * (const _D3DVECTOR& v, D3DVALUE s)
124{
125  return _D3DVECTOR(s*v.x, s*v.y, s*v.z);
126}
127
128inline _D3DVECTOR operator * (D3DVALUE s, const _D3DVECTOR& v)
129{
130  return _D3DVECTOR(s*v.x, s*v.y, s*v.z);
131}
132
133inline _D3DVECTOR operator / (const _D3DVECTOR& v, D3DVALUE s)
134{
135  return _D3DVECTOR(v.x/s, v.y/s, v.z/s);
136}
137
138inline int operator == (const _D3DVECTOR& v1, const _D3DVECTOR& v2)
139{
140  return v1.x==v2.x && v1.y==v2.y && v1.z == v2.z;
141}
142
143inline D3DVALUE Magnitude (const _D3DVECTOR& v)
144{
145  return (D3DVALUE) sqrt(SquareMagnitude(v));
146}
147
148inline D3DVALUE SquareMagnitude (const _D3DVECTOR& v)
149{
150  return v.x*v.x + v.y*v.y + v.z*v.z;
151}
152
153inline _D3DVECTOR Normalize (const _D3DVECTOR& v)
154{
155  return v / Magnitude(v);
156}
157
158inline D3DVALUE Min (const _D3DVECTOR& v)
159{
160  D3DVALUE ret = v.x;
161  if (v.y < ret)
162      ret = v.y;
163
164  if (v.z < ret)
165      ret = v.z;
166
167  return ret;
168}
169
170inline D3DVALUE Max (const _D3DVECTOR& v)
171{
172  D3DVALUE ret = v.x;
173  if (ret < v.y)
174      ret = v.y;
175
176  if (ret < v.z)
177      ret = v.z;
178
179  return ret;
180}
181
182inline _D3DVECTOR Minimize (const _D3DVECTOR& v1, const _D3DVECTOR& v2)
183{
184  return _D3DVECTOR( v1[0] < v2[0] ? v1[0] : v2[0],
185                     v1[1] < v2[1] ? v1[1] : v2[1],
186                     v1[2] < v2[2] ? v1[2] : v2[2]);
187}
188
189inline _D3DVECTOR Maximize (const _D3DVECTOR& v1, const _D3DVECTOR& v2)
190{
191  return _D3DVECTOR( v1[0] > v2[0] ? v1[0] : v2[0],
192                     v1[1] > v2[1] ? v1[1] : v2[1],
193                     v1[2] > v2[2] ? v1[2] : v2[2]);
194}
195
196inline D3DVALUE DotProduct (const _D3DVECTOR& v1, const _D3DVECTOR& v2)
197{
198  return v1.x*v2.x + v1.y * v2.y + v1.z*v2.z;
199}
200
201inline _D3DVECTOR CrossProduct (const _D3DVECTOR& v1, const _D3DVECTOR& v2)
202{
203  _D3DVECTOR result;
204  result[0] = v1[1] * v2[2] - v1[2] * v2[1];
205  result[1] = v1[2] * v2[0] - v1[0] * v2[2];
206  result[2] = v1[0] * v2[1] - v1[1] * v2[0];
207
208  return result;
209}
210
211inline _D3DMATRIX operator* (const _D3DMATRIX& a, const _D3DMATRIX& b)
212{
213  _D3DMATRIX ret;
214  for (int i=0; i<4; i++)
215  {
216    for (int j=0; j<4; j++)
217    {
218      ret(i, j) = 0.0f;
219      for (int k=0; k<4; k++)
220      {
221        ret(i, j) += a(i, k) * b(k, j);
222      }
223    }
224}
225  return ret;
226}
227
228