2 * Copyright (C) 2007 David Adam
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #ifndef __D3DX8MATH_INL__
20 #define __D3DX8MATH_INL__
22 /*_______________D3DXVECTOR2________________________*/
24 static inline D3DXVECTOR2* D3DXVec2Add(D3DXVECTOR2 *pout, CONST D3DXVECTOR2 *pv1, CONST D3DXVECTOR2 *pv2)
26 if ( !pout || !pv1 || !pv2) return NULL;
27 pout->x = pv1->x + pv2->x;
28 pout->y = pv1->y + pv2->y;
32 static inline FLOAT D3DXVec2CCW(CONST D3DXVECTOR2 *pv1, CONST D3DXVECTOR2 *pv2)
34 if ( !pv1 || !pv2) return 0.0f;
35 return ( (pv1->x) * (pv2->y) - (pv1->y) * (pv2->x) );
38 static inline FLOAT D3DXVec2Dot(CONST D3DXVECTOR2 *pv1, CONST D3DXVECTOR2 *pv2)
40 if ( !pv1 || !pv2) return 0.0f;
41 return ( (pv1->x * pv2->x + pv1->y * pv2->y) );
44 static inline FLOAT D3DXVec2Length(CONST D3DXVECTOR2 *pv)
47 return sqrt( (pv->x) * (pv->x) + (pv->y) * (pv->y) );
50 static inline FLOAT D3DXVec2LengthSq(CONST D3DXVECTOR2 *pv)
53 return( (pv->x) * (pv->x) + (pv->y) * (pv->y) );
56 static inline D3DXVECTOR2* D3DXVec2Lerp(D3DXVECTOR2 *pout, CONST D3DXVECTOR2 *pv1, CONST D3DXVECTOR2 *pv2, FLOAT s)
58 if ( !pout || !pv1 || !pv2) return NULL;
59 pout->x = (1-s) * (pv1->x) + s * (pv2->x);
60 pout->y = (1-s) * (pv1->y) + s * (pv2->y);
64 static inline D3DXVECTOR2* D3DXVec2Maximize(D3DXVECTOR2 *pout, CONST D3DXVECTOR2 *pv1, CONST D3DXVECTOR2 *pv2)
66 if ( !pout || !pv1 || !pv2) return NULL;
67 pout->x = max(pv1->x , pv2->x);
68 pout->y = max(pv1->y , pv2->y);
72 static inline D3DXVECTOR2* D3DXVec2Minimize(D3DXVECTOR2 *pout, CONST D3DXVECTOR2 *pv1, CONST D3DXVECTOR2 *pv2)
74 if ( !pout || !pv1 || !pv2) return NULL;
75 pout->x = min(pv1->x , pv2->x);
76 pout->y = min(pv1->y , pv2->y);
80 static inline D3DXVECTOR2* D3DXVec2Scale(D3DXVECTOR2 *pout, CONST D3DXVECTOR2 *pv, FLOAT s)
82 if ( !pout || !pv) return NULL;
83 pout->x = s * (pv->x);
84 pout->y = s * (pv->y);
88 static inline D3DXVECTOR2* D3DXVec2Subtract(D3DXVECTOR2 *pout, CONST D3DXVECTOR2 *pv1, CONST D3DXVECTOR2 *pv2)
90 if ( !pout || !pv1 || !pv2) return NULL;
91 pout->x = pv1->x - pv2->x;
92 pout->y = pv1->y - pv2->y;
96 /*__________________D3DXVECTOR3_______________________*/
98 static inline D3DXVECTOR3* D3DXVec3Add(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv1, CONST D3DXVECTOR3 *pv2)
100 if ( !pout || !pv1 || !pv2) return NULL;
101 pout->x = pv1->x + pv2->x;
102 pout->y = pv1->y + pv2->y;
103 pout->z = pv1->z + pv2->z;
107 static inline D3DXVECTOR3* D3DXVec3Cross(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv1, CONST D3DXVECTOR3 *pv2)
109 if ( !pout || !pv1 || !pv2) return NULL;
110 pout->x = (pv1->y) * (pv2->z) - (pv1->z) * (pv2->y);
111 pout->y = (pv1->z) * (pv2->x) - (pv1->x) * (pv2->z);
112 pout->z = (pv1->x) * (pv2->y) - (pv1->y) * (pv2->x);
116 static inline FLOAT D3DXVec3Dot(CONST D3DXVECTOR3 *pv1, CONST D3DXVECTOR3 *pv2)
118 if ( !pv1 || !pv2 ) return 0.0f;
119 return (pv1->x) * (pv2->x) + (pv1->y) * (pv2->y) + (pv1->z) * (pv2->z);
122 static inline FLOAT D3DXVec3Length(CONST D3DXVECTOR3 *pv)
124 if (!pv) return 0.0f;
125 return sqrt( (pv->x) * (pv->x) + (pv->y) * (pv->y) + (pv->z) * (pv->z) );
128 static inline FLOAT D3DXVec3LengthSq(CONST D3DXVECTOR3 *pv)
130 if (!pv) return 0.0f;
131 return (pv->x) * (pv->x) + (pv->y) * (pv->y) + (pv->z) * (pv->z);
134 static inline D3DXVECTOR3* D3DXVec3Lerp(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv1, CONST D3DXVECTOR3 *pv2, FLOAT s)
136 if ( !pout || !pv1 || !pv2) return NULL;
137 pout->x = (1-s) * (pv1->x) + s * (pv2->x);
138 pout->y = (1-s) * (pv1->y) + s * (pv2->y);
139 pout->z = (1-s) * (pv1->z) + s * (pv2->z);
143 static inline D3DXVECTOR3* D3DXVec3Maximize(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv1, CONST D3DXVECTOR3 *pv2)
145 if ( !pout || !pv1 || !pv2) return NULL;
146 pout->x = max(pv1->x , pv2->x);
147 pout->y = max(pv1->y , pv2->y);
148 pout->z = max(pv1->z , pv2->z);
152 static inline D3DXVECTOR3* D3DXVec3Minimize(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv1, CONST D3DXVECTOR3 *pv2)
154 if ( !pout || !pv1 || !pv2) return NULL;
155 pout->x = min(pv1->x , pv2->x);
156 pout->y = min(pv1->y , pv2->y);
157 pout->z = min(pv1->z , pv2->z);
161 static inline D3DXVECTOR3* D3DXVec3Scale(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv, FLOAT s)
163 if ( !pout || !pv) return NULL;
164 pout->x = s * (pv->x);
165 pout->y = s * (pv->y);
166 pout->z = s * (pv->z);
170 static inline D3DXVECTOR3* D3DXVec3Subtract(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv1, CONST D3DXVECTOR3 *pv2)
172 if ( !pout || !pv1 || !pv2) return NULL;
173 pout->x = pv1->x - pv2->x;
174 pout->y = pv1->y - pv2->y;
175 pout->z = pv1->z - pv2->z;
178 /*__________________D3DXVECTOR4_______________________*/
180 static inline D3DXVECTOR4* D3DXVec4Add(D3DXVECTOR4 *pout, CONST D3DXVECTOR4 *pv1, CONST D3DXVECTOR4 *pv2)
182 if ( !pout || !pv1 || !pv2) return NULL;
183 pout->x = pv1->x + pv2->x;
184 pout->y = pv1->y + pv2->y;
185 pout->z = pv1->z + pv2->z;
186 pout->w = pv1->w + pv2->w;
190 static inline FLOAT D3DXVec4Dot(CONST D3DXVECTOR4 *pv1, CONST D3DXVECTOR4 *pv2)
192 if (!pv1 || !pv2 ) return 0.0f;
193 return (pv1->x) * (pv2->x) + (pv1->y) * (pv2->y) + (pv1->z) * (pv2->z) + (pv1->w) * (pv2->w);
196 static inline FLOAT D3DXVec4Length(CONST D3DXVECTOR4 *pv)
198 if (!pv) return 0.0f;
199 return sqrt( (pv->x) * (pv->x) + (pv->y) * (pv->y) + (pv->z) * (pv->z) + (pv->w) * (pv->w) );
202 static inline FLOAT D3DXVec4LengthSq(CONST D3DXVECTOR4 *pv)
204 if (!pv) return 0.0f;
205 return (pv->x) * (pv->x) + (pv->y) * (pv->y) + (pv->z) * (pv->z) + (pv->w) * (pv->w);
208 static inline D3DXVECTOR4* D3DXVec4Lerp(D3DXVECTOR4 *pout, CONST D3DXVECTOR4 *pv1, CONST D3DXVECTOR4 *pv2, FLOAT s)
210 if ( !pout || !pv1 || !pv2) return NULL;
211 pout->x = (1-s) * (pv1->x) + s * (pv2->x);
212 pout->y = (1-s) * (pv1->y) + s * (pv2->y);
213 pout->z = (1-s) * (pv1->z) + s * (pv2->z);
214 pout->w = (1-s) * (pv1->w) + s * (pv2->w);
219 static inline D3DXVECTOR4* D3DXVec4Maximize(D3DXVECTOR4 *pout, CONST D3DXVECTOR4 *pv1, CONST D3DXVECTOR4 *pv2)
221 if ( !pout || !pv1 || !pv2) return NULL;
222 pout->x = max(pv1->x , pv2->x);
223 pout->y = max(pv1->y , pv2->y);
224 pout->z = max(pv1->z , pv2->z);
225 pout->w = max(pv1->w , pv2->w);
229 static inline D3DXVECTOR4* D3DXVec4Minimize(D3DXVECTOR4 *pout, CONST D3DXVECTOR4 *pv1, CONST D3DXVECTOR4 *pv2)
231 if ( !pout || !pv1 || !pv2) return NULL;
232 pout->x = min(pv1->x , pv2->x);
233 pout->y = min(pv1->y , pv2->y);
234 pout->z = min(pv1->z , pv2->z);
235 pout->w = min(pv1->w , pv2->w);
239 static inline D3DXVECTOR4* D3DXVec4Scale(D3DXVECTOR4 *pout, CONST D3DXVECTOR4 *pv, FLOAT s)
241 if ( !pout || !pv) return NULL;
242 pout->x = s * (pv->x);
243 pout->y = s * (pv->y);
244 pout->z = s * (pv->z);
245 pout->w = s * (pv->w);
249 static inline D3DXVECTOR4* D3DXVec4Subtract(D3DXVECTOR4 *pout, CONST D3DXVECTOR4 *pv1, CONST D3DXVECTOR4 *pv2)
251 if ( !pout || !pv1 || !pv2) return NULL;
252 pout->x = pv1->x - pv2->x;
253 pout->y = pv1->y - pv2->y;
254 pout->z = pv1->z - pv2->z;
255 pout->w = pv1->w - pv2->w;
259 /*__________________D3DXQUATERNION____________________*/
261 static inline D3DXQUATERNION* D3DXQuaternionConjugate(D3DXQUATERNION *pout, CONST D3DXQUATERNION *pq)
263 if ( !pout || !pq) return NULL;
271 static inline FLOAT D3DXQuaternionDot(CONST D3DXQUATERNION *pq1, CONST D3DXQUATERNION *pq2)
273 if ( !pq1 || !pq2 ) return 0.0f;
274 return (pq1->x) * (pq2->x) + (pq1->y) * (pq2->y) + (pq1->z) * (pq2->z) + (pq1->w) * (pq2->w);
277 static inline D3DXQUATERNION* D3DXQuaternionIdentity(D3DXQUATERNION *pout)
279 if ( !pout) return NULL;
287 static inline FLOAT D3DXQuaternionLength(CONST D3DXQUATERNION *pq)
289 if (!pq) return 0.0f;
290 return sqrt( (pq->x) * (pq->x) + (pq->y) * (pq->y) + (pq->z) * (pq->z) + (pq->w) * (pq->w) );
293 static inline FLOAT D3DXQuaternionLengthSq(CONST D3DXQUATERNION *pq)
295 if (!pq) return 0.0f;
296 return (pq->x) * (pq->x) + (pq->y) * (pq->y) + (pq->z) * (pq->z) + (pq->w) * (pq->w);