vector.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. Copyright (C) 2005 Michael Liebscher <johnnycanuck@users.sourceforge.net>
  3. Copyright (C) 1997-2001 Id Software, Inc.
  4. This program is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU General Public License
  6. as published by the Free Software Foundation; either version 2
  7. of the License, or (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  15. */
  16. /*
  17. * vector.h: 2D and 3D vector math routines.
  18. *
  19. * Author: Michael Liebscher <johnnycanuck@users.sourceforge.net>
  20. *
  21. * Acknowledgement:
  22. * Portion of this code was derived from Quake II, and was originally
  23. * written by Id Software, Inc.
  24. *
  25. */
  26. #ifndef __VECTOR_H__
  27. #define __VECTOR_H__
  28. typedef float vec_t;
  29. typedef vec_t vec2_t[ 2 ];
  30. typedef vec_t vec3_t[ 3 ];
  31. typedef vec_t vec4_t[ 4 ];
  32. typedef vec_t vec5_t[ 5 ];
  33. extern vec3_t vec3_origin;
  34. /////////////////////////////////////////////////////////////////////
  35. //
  36. // 3D Vector routines
  37. //
  38. /////////////////////////////////////////////////////////////////////
  39. #define vectorClear( a ) ( (a)[ 0 ] = (a)[ 1 ] = (a)[ 2 ] = 0 )
  40. #define vectorNegate( a, b ) ( (b)[ 0 ] = (-a)[ 0 ], (b)[ 1 ] = (-a)[ 1 ], (b)[ 2 ] = (-a)[ 2 ] )
  41. #define vectorSet( v, x, y, z ) ( (v)[ 0 ] = ( x ), (v)[ 1 ] = ( y ), (v)[ 2 ] = ( z ) )
  42. #define vectorInverse( a ) ( (a)[ 0 ] = (-a)[ 0 ], (a)[ 1 ] = (-a)[ 1 ], (a)[ 2 ] = (-a)[ 2 ] )
  43. #if 1
  44. #define DotProduct( x, y ) ( (x)[ 0 ] * (y)[ 0 ] + (x)[ 1 ] * (y)[ 1 ] + (x)[ 2 ] * (y)[ 2 ] )
  45. #define vectorSubtract( a, b, c ) ( (c)[ 0 ] = (a)[ 0 ] - (b)[ 0 ], (c)[ 1 ] = (a)[ 1 ] - (b)[ 1 ], (c)[ 2 ] = (a)[ 2 ] - (b)[ 2 ] )
  46. #define vectorAdd( a, b, c ) ( (c)[ 0 ] = (a)[ 0 ] + (b)[ 0 ], (c)[ 1 ] = (a)[ 1 ] + (b)[ 1 ], (c)[ 2 ] = (a)[ 2 ] + (b)[ 2 ] )
  47. #define vectorCopy( a, b ) ( (b)[ 0 ] = (a)[ 0 ], (b)[ 1 ] = (a)[ 1 ], (b)[ 2 ] = (a)[ 2 ] )
  48. #define vectorScale( v, s, o ) ( (o)[ 0 ] = (v)[ 0 ] * (s),(o)[ 1 ] = (v)[ 1 ] * (s), (o)[ 2 ] = (v)[ 2 ] * (s) )
  49. #define vectorMA( v, s, b, o ) ( (o)[ 0 ] = (v)[ 0 ] + (b)[ 0 ]*(s),(o)[ 1 ] = (v)[ 1 ] + (b)[ 1 ] * (s),(o)[ 2 ] = (v)[ 2 ] + (b)[ 2 ] * (s) )
  50. #else
  51. /* just in case you don't want to use the macros */
  52. #define DotProduct( x, y ) _vectorDotProduct( x, y )
  53. #define vectorSubtract( a, b, c ) _vectorSubtract( a, b, c )
  54. #define vectorAdd( a, b, c ) _vectorAdd( a, b, c )
  55. #define vectorCopy( a, b ) _vectorCopy( a, b )
  56. #define vectorScale( v, s, o ) _vectorScale( v, s, o )
  57. #define vectorMA( v, s, b, o ) _vectorMA( v, s, b, o )
  58. #endif
  59. extern void vectorCrossProduct( const vec3_t v1, const vec3_t v2, vec3_t cross );
  60. extern int vectorCompare( const vec3_t v1, const vec3_t v2 );
  61. extern vec_t vectorLength( const vec3_t v );
  62. extern vec_t (*pfVectorNormalize)( vec3_t vec );
  63. extern void angleVectors( const vec3_t angles, vec3_t forward, vec3_t right, vec3_t up );
  64. extern void RotatePointAroundVector( vec3_t dst, const vec3_t dir, const vec3_t point, float degrees );
  65. extern void PerpendicularVector( vec3_t dst, const vec3_t src );
  66. extern void ProjectPointOnPlane( vec3_t dst, const vec3_t p, const vec3_t normal );
  67. extern void AddPointToBounds( vec3_t v, vec3_t mins, vec3_t maxs );
  68. extern float RadiusFromBounds( const vec3_t mins, const vec3_t maxs );
  69. /////////////////////////////////////////////////////////////////////
  70. //
  71. // 2D Vector routines
  72. //
  73. /////////////////////////////////////////////////////////////////////
  74. #define vector2DClear( a ) ( (a)[ 0 ] = (a)[ 1 ] = 0 )
  75. #define vector2DNegate( a, b ) ( (b)[ 0 ] = (-a)[ 0 ], (b)[ 1 ] = (-a)[ 1 ] )
  76. #define vector2DSet( v, x, y ) ( (v)[ 0 ] = ( x ), (v)[ 1 ] = ( y ) )
  77. #define vector2DInverse( a ) ( (a)[ 0 ] = (-a)[ 0 ], (a)[ 1 ] = (-a)[ 1 ] )
  78. #define vector2DPerpDot( a, b ) ( (a)[ 0 ] * (b)[ 1 ] - (a)[ 1 ] * (b)[ 0 ] )
  79. #define vector2DDotProduct( x, y ) ( (x)[ 0 ] * (y)[ 0 ] + (x)[ 1 ] * (y)[ 1 ] )
  80. #define vector2DSubtract( a, b, c ) ( (c)[ 0 ] = (a)[ 0 ] - (b)[ 0 ], (c)[ 1 ] = (a)[ 1 ] - (b)[ 1 ] )
  81. #define vector2DAdd( a, b, c ) ( (c)[ 0 ] = (a)[ 0 ] + (b)[ 0 ], (c)[ 1 ] = (a)[ 1 ] + (b)[ 1 ] )
  82. #define vector2DCopy( a, b ) ( (b)[ 0 ] = (a)[ 0 ], (b)[ 1 ] = (a)[ 1 ] )
  83. #define vector2DScale( v, s, o ) ( (o)[ 0 ] = (v)[ 0 ] * (s), (o)[ 1 ] = (v)[ 1 ] * (s) )
  84. #define vector2DMA( v, s, b, o ) ( (o)[ 0 ] = (v)[ 0 ] + (b)[ 0 ]*(s), (o)[ 1 ] = (v)[ 1 ] + (b)[ 1 ] * (s) )
  85. extern W32 vector2DCompare( const vec2_t v1, const vec2_t v2 );
  86. extern vec_t vector2DLength( const vec2_t v );
  87. #endif /* __VECTOR_H__ */