2 * Copyright (C) 2007 David Adam
3 * Copyright (C) 2007 Tony Wasserka
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 #ifndef __D3DX8MATH_INL__
21 #define __D3DX8MATH_INL__
23 /* constructors & operators */
26 inline D3DXVECTOR2::D3DXVECTOR2()
30 inline D3DXVECTOR2::D3DXVECTOR2(CONST FLOAT *pf)
37 inline D3DXVECTOR2::D3DXVECTOR2(FLOAT fx, FLOAT fy)
43 inline D3DXVECTOR2::operator FLOAT* ()
48 inline D3DXVECTOR2::operator CONST FLOAT* () const
50 return (CONST FLOAT*)&x;
53 inline D3DXVECTOR2& D3DXVECTOR2::operator += (CONST D3DXVECTOR2& v)
60 inline D3DXVECTOR2& D3DXVECTOR2::operator -= (CONST D3DXVECTOR2& v)
67 inline D3DXVECTOR2& D3DXVECTOR2::operator *= (FLOAT f)
74 inline D3DXVECTOR2& D3DXVECTOR2::operator /= (FLOAT f)
81 inline D3DXVECTOR2 D3DXVECTOR2::operator + () const
86 inline D3DXVECTOR2 D3DXVECTOR2::operator - () const
88 return D3DXVECTOR2(-x, -y);
91 inline D3DXVECTOR2 D3DXVECTOR2::operator + (CONST D3DXVECTOR2& v) const
93 return D3DXVECTOR2(x + v.x, y + v.y);
96 inline D3DXVECTOR2 D3DXVECTOR2::operator - (CONST D3DXVECTOR2& v) const
98 return D3DXVECTOR2(x - v.x, y - v.y);
101 inline D3DXVECTOR2 D3DXVECTOR2::operator * (FLOAT f) const
103 return D3DXVECTOR2(x * f, y * f);
106 inline D3DXVECTOR2 D3DXVECTOR2::operator / (FLOAT f) const
108 return D3DXVECTOR2(x / f, y / f);
111 inline D3DXVECTOR2 operator * (FLOAT f, CONST D3DXVECTOR2& v)
113 return D3DXVECTOR2(f * v.x, f * v.y);
116 inline BOOL D3DXVECTOR2::operator == (CONST D3DXVECTOR2& v) const
118 return x == v.x && y == v.y;
121 inline BOOL D3DXVECTOR2::operator != (CONST D3DXVECTOR2& v) const
123 return x != v.x || y != v.y;
126 inline D3DXVECTOR3::D3DXVECTOR3()
130 inline D3DXVECTOR3::D3DXVECTOR3(CONST FLOAT *pf)
138 inline D3DXVECTOR3::D3DXVECTOR3(CONST D3DVECTOR& v)
145 inline D3DXVECTOR3::D3DXVECTOR3(FLOAT fx, FLOAT fy, FLOAT fz)
152 inline D3DXVECTOR3::operator FLOAT* ()
157 inline D3DXVECTOR3::operator CONST FLOAT* () const
159 return (CONST FLOAT*)&x;
162 inline D3DXVECTOR3& D3DXVECTOR3::operator += (CONST D3DXVECTOR3& v)
170 inline D3DXVECTOR3& D3DXVECTOR3::operator -= (CONST D3DXVECTOR3& v)
178 inline D3DXVECTOR3& D3DXVECTOR3::operator *= (FLOAT f)
186 inline D3DXVECTOR3& D3DXVECTOR3::operator /= (FLOAT f)
194 inline D3DXVECTOR3 D3DXVECTOR3::operator + () const
199 inline D3DXVECTOR3 D3DXVECTOR3::operator - () const
201 return D3DXVECTOR3(-x, -y, -z);
204 inline D3DXVECTOR3 D3DXVECTOR3::operator + (CONST D3DXVECTOR3& v) const
206 return D3DXVECTOR3(x + v.x, y + v.y, z + v.z);
209 inline D3DXVECTOR3 D3DXVECTOR3::operator - (CONST D3DXVECTOR3& v) const
211 return D3DXVECTOR3(x - v.x, y - v.y, z - v.z);
214 inline D3DXVECTOR3 D3DXVECTOR3::operator * (FLOAT f) const
216 return D3DXVECTOR3(x * f, y * f, z * f);
219 inline D3DXVECTOR3 D3DXVECTOR3::operator / (FLOAT f) const
221 return D3DXVECTOR3(x / f, y / f, z / f);
224 inline D3DXVECTOR3 operator * (FLOAT f, CONST D3DXVECTOR3& v)
226 return D3DXVECTOR3(f * v.x, f * v.y, f * v.z);
229 inline BOOL D3DXVECTOR3::operator == (CONST D3DXVECTOR3& v) const
231 return x == v.x && y == v.y && z == v.z;
234 inline BOOL D3DXVECTOR3::operator != (CONST D3DXVECTOR3& v) const
236 return x != v.x || y != v.y || z != v.z;
239 #endif /* __cplusplus */
241 /*_______________D3DXCOLOR_____________________*/
243 static inline D3DXCOLOR* D3DXColorAdd(D3DXCOLOR *pout, CONST D3DXCOLOR *pc1, CONST D3DXCOLOR *pc2)
245 if ( !pout || !pc1 || !pc2 ) return NULL;
246 pout->r = (pc1->r) + (pc2->r);
247 pout->g = (pc1->g) + (pc2->g);
248 pout->b = (pc1->b) + (pc2->b);
249 pout->a = (pc1->a) + (pc2->a);
253 static inline D3DXCOLOR* D3DXColorLerp(D3DXCOLOR *pout, CONST D3DXCOLOR *pc1, CONST D3DXCOLOR *pc2, FLOAT s)
255 if ( !pout || !pc1 || !pc2 ) return NULL;
256 pout->r = (1-s) * (pc1->r) + s *(pc2->r);
257 pout->g = (1-s) * (pc1->g) + s *(pc2->g);
258 pout->b = (1-s) * (pc1->b) + s *(pc2->b);
259 pout->a = (1-s) * (pc1->a) + s *(pc2->a);
263 static inline D3DXCOLOR* D3DXColorModulate(D3DXCOLOR *pout, CONST D3DXCOLOR *pc1, CONST D3DXCOLOR *pc2)
265 if ( !pout || !pc1 || !pc2 ) return NULL;
266 pout->r = (pc1->r) * (pc2->r);
267 pout->g = (pc1->g) * (pc2->g);
268 pout->b = (pc1->b) * (pc2->b);
269 pout->a = (pc1->a) * (pc2->a);
273 static inline D3DXCOLOR* D3DXColorNegative(D3DXCOLOR *pout, CONST D3DXCOLOR *pc)
275 if ( !pout || !pc ) return NULL;
276 pout->r = 1.0f - pc->r;
277 pout->g = 1.0f - pc->g;
278 pout->b = 1.0f - pc->b;
283 static inline D3DXCOLOR* D3DXColorScale(D3DXCOLOR *pout, CONST D3DXCOLOR *pc, FLOAT s)
285 if ( !pout || !pc ) return NULL;
286 pout->r = s* (pc->r);
287 pout->g = s* (pc->g);
288 pout->b = s* (pc->b);
289 pout->a = s* (pc->a);
293 static inline D3DXCOLOR* D3DXColorSubtract(D3DXCOLOR *pout, CONST D3DXCOLOR *pc1, CONST D3DXCOLOR *pc2)
295 if ( !pout || !pc1 || !pc2 ) return NULL;
296 pout->r = (pc1->r) - (pc2->r);
297 pout->g = (pc1->g) - (pc2->g);
298 pout->b = (pc1->b) - (pc2->b);
299 pout->a = (pc1->a) - (pc2->a);
303 /*_______________D3DXVECTOR2________________________*/
305 static inline D3DXVECTOR2* D3DXVec2Add(D3DXVECTOR2 *pout, CONST D3DXVECTOR2 *pv1, CONST D3DXVECTOR2 *pv2)
307 if ( !pout || !pv1 || !pv2) return NULL;
308 pout->x = pv1->x + pv2->x;
309 pout->y = pv1->y + pv2->y;
313 static inline FLOAT D3DXVec2CCW(CONST D3DXVECTOR2 *pv1, CONST D3DXVECTOR2 *pv2)
315 if ( !pv1 || !pv2) return 0.0f;
316 return ( (pv1->x) * (pv2->y) - (pv1->y) * (pv2->x) );
319 static inline FLOAT D3DXVec2Dot(CONST D3DXVECTOR2 *pv1, CONST D3DXVECTOR2 *pv2)
321 if ( !pv1 || !pv2) return 0.0f;
322 return ( (pv1->x * pv2->x + pv1->y * pv2->y) );
325 static inline FLOAT D3DXVec2Length(CONST D3DXVECTOR2 *pv)
327 if (!pv) return 0.0f;
328 return sqrt( (pv->x) * (pv->x) + (pv->y) * (pv->y) );
331 static inline FLOAT D3DXVec2LengthSq(CONST D3DXVECTOR2 *pv)
333 if (!pv) return 0.0f;
334 return( (pv->x) * (pv->x) + (pv->y) * (pv->y) );
337 static inline D3DXVECTOR2* D3DXVec2Lerp(D3DXVECTOR2 *pout, CONST D3DXVECTOR2 *pv1, CONST D3DXVECTOR2 *pv2, FLOAT s)
339 if ( !pout || !pv1 || !pv2) return NULL;
340 pout->x = (1-s) * (pv1->x) + s * (pv2->x);
341 pout->y = (1-s) * (pv1->y) + s * (pv2->y);
345 static inline D3DXVECTOR2* D3DXVec2Maximize(D3DXVECTOR2 *pout, CONST D3DXVECTOR2 *pv1, CONST D3DXVECTOR2 *pv2)
347 if ( !pout || !pv1 || !pv2) return NULL;
348 pout->x = max(pv1->x , pv2->x);
349 pout->y = max(pv1->y , pv2->y);
353 static inline D3DXVECTOR2* D3DXVec2Minimize(D3DXVECTOR2 *pout, CONST D3DXVECTOR2 *pv1, CONST D3DXVECTOR2 *pv2)
355 if ( !pout || !pv1 || !pv2) return NULL;
356 pout->x = min(pv1->x , pv2->x);
357 pout->y = min(pv1->y , pv2->y);
361 static inline D3DXVECTOR2* D3DXVec2Scale(D3DXVECTOR2 *pout, CONST D3DXVECTOR2 *pv, FLOAT s)
363 if ( !pout || !pv) return NULL;
364 pout->x = s * (pv->x);
365 pout->y = s * (pv->y);
369 static inline D3DXVECTOR2* D3DXVec2Subtract(D3DXVECTOR2 *pout, CONST D3DXVECTOR2 *pv1, CONST D3DXVECTOR2 *pv2)
371 if ( !pout || !pv1 || !pv2) return NULL;
372 pout->x = pv1->x - pv2->x;
373 pout->y = pv1->y - pv2->y;
377 /*__________________D3DXVECTOR3_______________________*/
379 static inline D3DXVECTOR3* D3DXVec3Add(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv1, CONST D3DXVECTOR3 *pv2)
381 if ( !pout || !pv1 || !pv2) return NULL;
382 pout->x = pv1->x + pv2->x;
383 pout->y = pv1->y + pv2->y;
384 pout->z = pv1->z + pv2->z;
388 static inline D3DXVECTOR3* D3DXVec3Cross(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv1, CONST D3DXVECTOR3 *pv2)
390 if ( !pout || !pv1 || !pv2) return NULL;
391 pout->x = (pv1->y) * (pv2->z) - (pv1->z) * (pv2->y);
392 pout->y = (pv1->z) * (pv2->x) - (pv1->x) * (pv2->z);
393 pout->z = (pv1->x) * (pv2->y) - (pv1->y) * (pv2->x);
397 static inline FLOAT D3DXVec3Dot(CONST D3DXVECTOR3 *pv1, CONST D3DXVECTOR3 *pv2)
399 if ( !pv1 || !pv2 ) return 0.0f;
400 return (pv1->x) * (pv2->x) + (pv1->y) * (pv2->y) + (pv1->z) * (pv2->z);
403 static inline FLOAT D3DXVec3Length(CONST D3DXVECTOR3 *pv)
405 if (!pv) return 0.0f;
406 return sqrt( (pv->x) * (pv->x) + (pv->y) * (pv->y) + (pv->z) * (pv->z) );
409 static inline FLOAT D3DXVec3LengthSq(CONST D3DXVECTOR3 *pv)
411 if (!pv) return 0.0f;
412 return (pv->x) * (pv->x) + (pv->y) * (pv->y) + (pv->z) * (pv->z);
415 static inline D3DXVECTOR3* D3DXVec3Lerp(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv1, CONST D3DXVECTOR3 *pv2, FLOAT s)
417 if ( !pout || !pv1 || !pv2) return NULL;
418 pout->x = (1-s) * (pv1->x) + s * (pv2->x);
419 pout->y = (1-s) * (pv1->y) + s * (pv2->y);
420 pout->z = (1-s) * (pv1->z) + s * (pv2->z);
424 static inline D3DXVECTOR3* D3DXVec3Maximize(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv1, CONST D3DXVECTOR3 *pv2)
426 if ( !pout || !pv1 || !pv2) return NULL;
427 pout->x = max(pv1->x , pv2->x);
428 pout->y = max(pv1->y , pv2->y);
429 pout->z = max(pv1->z , pv2->z);
433 static inline D3DXVECTOR3* D3DXVec3Minimize(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv1, CONST D3DXVECTOR3 *pv2)
435 if ( !pout || !pv1 || !pv2) return NULL;
436 pout->x = min(pv1->x , pv2->x);
437 pout->y = min(pv1->y , pv2->y);
438 pout->z = min(pv1->z , pv2->z);
442 static inline D3DXVECTOR3* D3DXVec3Scale(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv, FLOAT s)
444 if ( !pout || !pv) return NULL;
445 pout->x = s * (pv->x);
446 pout->y = s * (pv->y);
447 pout->z = s * (pv->z);
451 static inline D3DXVECTOR3* D3DXVec3Subtract(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv1, CONST D3DXVECTOR3 *pv2)
453 if ( !pout || !pv1 || !pv2) return NULL;
454 pout->x = pv1->x - pv2->x;
455 pout->y = pv1->y - pv2->y;
456 pout->z = pv1->z - pv2->z;
459 /*__________________D3DXVECTOR4_______________________*/
461 static inline D3DXVECTOR4* D3DXVec4Add(D3DXVECTOR4 *pout, CONST D3DXVECTOR4 *pv1, CONST D3DXVECTOR4 *pv2)
463 if ( !pout || !pv1 || !pv2) return NULL;
464 pout->x = pv1->x + pv2->x;
465 pout->y = pv1->y + pv2->y;
466 pout->z = pv1->z + pv2->z;
467 pout->w = pv1->w + pv2->w;
471 static inline FLOAT D3DXVec4Dot(CONST D3DXVECTOR4 *pv1, CONST D3DXVECTOR4 *pv2)
473 if (!pv1 || !pv2 ) return 0.0f;
474 return (pv1->x) * (pv2->x) + (pv1->y) * (pv2->y) + (pv1->z) * (pv2->z) + (pv1->w) * (pv2->w);
477 static inline FLOAT D3DXVec4Length(CONST D3DXVECTOR4 *pv)
479 if (!pv) return 0.0f;
480 return sqrt( (pv->x) * (pv->x) + (pv->y) * (pv->y) + (pv->z) * (pv->z) + (pv->w) * (pv->w) );
483 static inline FLOAT D3DXVec4LengthSq(CONST D3DXVECTOR4 *pv)
485 if (!pv) return 0.0f;
486 return (pv->x) * (pv->x) + (pv->y) * (pv->y) + (pv->z) * (pv->z) + (pv->w) * (pv->w);
489 static inline D3DXVECTOR4* D3DXVec4Lerp(D3DXVECTOR4 *pout, CONST D3DXVECTOR4 *pv1, CONST D3DXVECTOR4 *pv2, FLOAT s)
491 if ( !pout || !pv1 || !pv2) return NULL;
492 pout->x = (1-s) * (pv1->x) + s * (pv2->x);
493 pout->y = (1-s) * (pv1->y) + s * (pv2->y);
494 pout->z = (1-s) * (pv1->z) + s * (pv2->z);
495 pout->w = (1-s) * (pv1->w) + s * (pv2->w);
500 static inline D3DXVECTOR4* D3DXVec4Maximize(D3DXVECTOR4 *pout, CONST D3DXVECTOR4 *pv1, CONST D3DXVECTOR4 *pv2)
502 if ( !pout || !pv1 || !pv2) return NULL;
503 pout->x = max(pv1->x , pv2->x);
504 pout->y = max(pv1->y , pv2->y);
505 pout->z = max(pv1->z , pv2->z);
506 pout->w = max(pv1->w , pv2->w);
510 static inline D3DXVECTOR4* D3DXVec4Minimize(D3DXVECTOR4 *pout, CONST D3DXVECTOR4 *pv1, CONST D3DXVECTOR4 *pv2)
512 if ( !pout || !pv1 || !pv2) return NULL;
513 pout->x = min(pv1->x , pv2->x);
514 pout->y = min(pv1->y , pv2->y);
515 pout->z = min(pv1->z , pv2->z);
516 pout->w = min(pv1->w , pv2->w);
520 static inline D3DXVECTOR4* D3DXVec4Scale(D3DXVECTOR4 *pout, CONST D3DXVECTOR4 *pv, FLOAT s)
522 if ( !pout || !pv) return NULL;
523 pout->x = s * (pv->x);
524 pout->y = s * (pv->y);
525 pout->z = s * (pv->z);
526 pout->w = s * (pv->w);
530 static inline D3DXVECTOR4* D3DXVec4Subtract(D3DXVECTOR4 *pout, CONST D3DXVECTOR4 *pv1, CONST D3DXVECTOR4 *pv2)
532 if ( !pout || !pv1 || !pv2) return NULL;
533 pout->x = pv1->x - pv2->x;
534 pout->y = pv1->y - pv2->y;
535 pout->z = pv1->z - pv2->z;
536 pout->w = pv1->w - pv2->w;
540 /*__________________D3DXMatrix____________________*/
541 #ifdef NONAMELESSUNION
542 # define D3DX_U(x) (x).u
544 # define D3DX_U(x) (x)
547 static inline D3DXMATRIX* D3DXMatrixIdentity(D3DXMATRIX *pout)
549 if ( !pout ) return NULL;
550 D3DX_U(*pout).m[0][1] = 0.0f;
551 D3DX_U(*pout).m[0][2] = 0.0f;
552 D3DX_U(*pout).m[0][3] = 0.0f;
553 D3DX_U(*pout).m[1][0] = 0.0f;
554 D3DX_U(*pout).m[1][2] = 0.0f;
555 D3DX_U(*pout).m[1][3] = 0.0f;
556 D3DX_U(*pout).m[2][0] = 0.0f;
557 D3DX_U(*pout).m[2][1] = 0.0f;
558 D3DX_U(*pout).m[2][3] = 0.0f;
559 D3DX_U(*pout).m[3][0] = 0.0f;
560 D3DX_U(*pout).m[3][1] = 0.0f;
561 D3DX_U(*pout).m[3][2] = 0.0f;
562 D3DX_U(*pout).m[0][0] = 1.0f;
563 D3DX_U(*pout).m[1][1] = 1.0f;
564 D3DX_U(*pout).m[2][2] = 1.0f;
565 D3DX_U(*pout).m[3][3] = 1.0f;
569 static inline BOOL D3DXMatrixIsIdentity(D3DXMATRIX *pm)
572 D3DXMATRIX testmatrix;
574 if ( !pm ) return FALSE;
575 D3DXMatrixIdentity(&testmatrix);
580 if ( D3DX_U(*pm).m[i][j] != D3DX_U(testmatrix).m[i][j] ) return FALSE;
587 /*__________________D3DXPLANE____________________*/
589 static inline FLOAT D3DXPlaneDot(CONST D3DXPLANE *pp, CONST D3DXVECTOR4 *pv)
591 if ( !pp || !pv ) return 0.0f;
592 return ( (pp->a) * (pv->x) + (pp->b) * (pv->y) + (pp->c) * (pv->z) + (pp->d) * (pv->w) );
595 static inline FLOAT D3DXPlaneDotCoord(CONST D3DXPLANE *pp, CONST D3DXVECTOR4 *pv)
597 if ( !pp || !pv ) return 0.0f;
598 return ( (pp->a) * (pv->x) + (pp->b) * (pv->y) + (pp->c) * (pv->z) + (pp->d) );
601 static inline FLOAT D3DXPlaneDotNormal(CONST D3DXPLANE *pp, CONST D3DXVECTOR4 *pv)
603 if ( !pp || !pv ) return 0.0f;
604 return ( (pp->a) * (pv->x) + (pp->b) * (pv->y) + (pp->c) * (pv->z) );
607 /*__________________D3DXQUATERNION____________________*/
609 static inline D3DXQUATERNION* D3DXQuaternionConjugate(D3DXQUATERNION *pout, CONST D3DXQUATERNION *pq)
611 if ( !pout || !pq) return NULL;
619 static inline FLOAT D3DXQuaternionDot(CONST D3DXQUATERNION *pq1, CONST D3DXQUATERNION *pq2)
621 if ( !pq1 || !pq2 ) return 0.0f;
622 return (pq1->x) * (pq2->x) + (pq1->y) * (pq2->y) + (pq1->z) * (pq2->z) + (pq1->w) * (pq2->w);
625 static inline D3DXQUATERNION* D3DXQuaternionIdentity(D3DXQUATERNION *pout)
627 if ( !pout) return NULL;
635 static inline BOOL D3DXQuaternionIsIdentity(D3DXQUATERNION *pq)
637 if ( !pq) return FALSE;
638 return ( (pq->x == 0.0f) && (pq->y == 0.0f) && (pq->z == 0.0f) && (pq->w == 1.0f) );
641 static inline FLOAT D3DXQuaternionLength(CONST D3DXQUATERNION *pq)
643 if (!pq) return 0.0f;
644 return sqrt( (pq->x) * (pq->x) + (pq->y) * (pq->y) + (pq->z) * (pq->z) + (pq->w) * (pq->w) );
647 static inline FLOAT D3DXQuaternionLengthSq(CONST D3DXQUATERNION *pq)
649 if (!pq) return 0.0f;
650 return (pq->x) * (pq->x) + (pq->y) * (pq->y) + (pq->z) * (pq->z) + (pq->w) * (pq->w);