vec.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // --- vec.h ---
  4. //
  5. //////////////////////////////////////////////////////////////////////////////
  6. #ifndef __ANGEL_VEC_H__
  7. #define __ANGEL_VEC_H__
  8. #include "Angel.h"
  9. namespace Angel {
  10. //////////////////////////////////////////////////////////////////////////////
  11. //
  12. // vec2.h - 2D vector
  13. //
  14. struct vec2 {
  15. GLfloat x;
  16. GLfloat y;
  17. //
  18. // --- Constructors and Destructors ---
  19. //
  20. vec2( GLfloat s = GLfloat(0.0) ) :
  21. x(s), y(s) {}
  22. vec2( GLfloat x, GLfloat y ) :
  23. x(x), y(y) {}
  24. vec2( const vec2& v )
  25. { x = v.x; y = v.y; }
  26. //
  27. // --- Indexing Operator ---
  28. //
  29. GLfloat& operator [] ( int i ) { return *(&x + i); }
  30. const GLfloat operator [] ( int i ) const { return *(&x + i); }
  31. //
  32. // --- (non-modifying) Arithematic Operators ---
  33. //
  34. vec2 operator - () const // unary minus operator
  35. { return vec2( -x, -y ); }
  36. vec2 operator + ( const vec2& v ) const
  37. { return vec2( x + v.x, y + v.y ); }
  38. vec2 operator - ( const vec2& v ) const
  39. { return vec2( x - v.x, y - v.y ); }
  40. vec2 operator * ( const GLfloat s ) const
  41. { return vec2( s*x, s*y ); }
  42. vec2 operator * ( const vec2& v ) const
  43. { return vec2( x*v.x, y*v.y ); }
  44. friend vec2 operator * ( const GLfloat s, const vec2& v )
  45. { return v * s; }
  46. vec2 operator / ( const GLfloat s ) const {
  47. #ifdef DEBUG
  48. if ( std::fabs(s) < DivideByZeroTolerance ) {
  49. std::cerr << "[" << __FILE__ << ":" << __LINE__ << "] "
  50. << "Division by zero" << std::endl;
  51. return vec2();
  52. }
  53. #endif // DEBUG
  54. GLfloat r = GLfloat(1.0) / s;
  55. return *this * r;
  56. }
  57. //
  58. // --- (modifying) Arithematic Operators ---
  59. //
  60. vec2& operator += ( const vec2& v )
  61. { x += v.x; y += v.y; return *this; }
  62. vec2& operator -= ( const vec2& v )
  63. { x -= v.x; y -= v.y; return *this; }
  64. vec2& operator *= ( const GLfloat s )
  65. { x *= s; y *= s; return *this; }
  66. vec2& operator *= ( const vec2& v )
  67. { x *= v.x; y *= v.y; return *this; }
  68. vec2& operator /= ( const GLfloat s ) {
  69. #ifdef DEBUG
  70. if ( std::fabs(s) < DivideByZeroTolerance ) {
  71. std::cerr << "[" << __FILE__ << ":" << __LINE__ << "] "
  72. << "Division by zero" << std::endl;
  73. }
  74. #endif // DEBUG
  75. GLfloat r = GLfloat(1.0) / s;
  76. *this *= r;
  77. return *this;
  78. }
  79. //
  80. // --- Insertion and Extraction Operators ---
  81. //
  82. friend std::ostream& operator << ( std::ostream& os, const vec2& v ) {
  83. return os << "( " << v.x << ", " << v.y << " )";
  84. }
  85. friend std::istream& operator >> ( std::istream& is, vec2& v )
  86. { return is >> v.x >> v.y ; }
  87. //
  88. // --- Conversion Operators ---
  89. //
  90. operator const GLfloat* () const
  91. { return static_cast<const GLfloat*>( &x ); }
  92. operator GLfloat* ()
  93. { return static_cast<GLfloat*>( &x ); }
  94. };
  95. //----------------------------------------------------------------------------
  96. //
  97. // Non-class vec2 Methods
  98. //
  99. inline
  100. GLfloat dot( const vec2& u, const vec2& v ) {
  101. return u.x * v.x + u.y * v.y;
  102. }
  103. inline
  104. GLfloat length( const vec2& v ) {
  105. return std::sqrt( dot(v,v) );
  106. }
  107. inline
  108. vec2 normalize( const vec2& v ) {
  109. return v / length(v);
  110. }
  111. //////////////////////////////////////////////////////////////////////////////
  112. //
  113. // vec3.h - 3D vector
  114. //
  115. //////////////////////////////////////////////////////////////////////////////
  116. struct vec3 {
  117. GLfloat x;
  118. GLfloat y;
  119. GLfloat z;
  120. //
  121. // --- Constructors and Destructors ---
  122. //
  123. vec3( GLfloat s = GLfloat(0.0) ) :
  124. x(s), y(s), z(s) {}
  125. vec3( GLfloat x, GLfloat y, GLfloat z ) :
  126. x(x), y(y), z(z) {}
  127. vec3( const vec3& v ) { x = v.x; y = v.y; z = v.z; }
  128. vec3( const vec2& v, const float f ) { x = v.x; y = v.y; z = f; }
  129. //
  130. // --- Indexing Operator ---
  131. //
  132. GLfloat& operator [] ( int i ) { return *(&x + i); }
  133. const GLfloat operator [] ( int i ) const { return *(&x + i); }
  134. //
  135. // --- (non-modifying) Arithematic Operators ---
  136. //
  137. vec3 operator - () const // unary minus operator
  138. { return vec3( -x, -y, -z ); }
  139. vec3 operator + ( const vec3& v ) const
  140. { return vec3( x + v.x, y + v.y, z + v.z ); }
  141. vec3 operator - ( const vec3& v ) const
  142. { return vec3( x - v.x, y - v.y, z - v.z ); }
  143. vec3 operator * ( const GLfloat s ) const
  144. { return vec3( s*x, s*y, s*z ); }
  145. vec3 operator * ( const vec3& v ) const
  146. { return vec3( x*v.x, y*v.y, z*v.z ); }
  147. friend vec3 operator * ( const GLfloat s, const vec3& v )
  148. { return v * s; }
  149. vec3 operator / ( const GLfloat s ) const {
  150. #ifdef DEBUG
  151. if ( std::fabs(s) < DivideByZeroTolerance ) {
  152. std::cerr << "[" << __FILE__ << ":" << __LINE__ << "] "
  153. << "Division by zero" << std::endl;
  154. return vec3();
  155. }
  156. #endif // DEBUG
  157. GLfloat r = GLfloat(1.0) / s;
  158. return *this * r;
  159. }
  160. //
  161. // --- (modifying) Arithematic Operators ---
  162. //
  163. vec3& operator += ( const vec3& v )
  164. { x += v.x; y += v.y; z += v.z; return *this; }
  165. vec3& operator -= ( const vec3& v )
  166. { x -= v.x; y -= v.y; z -= v.z; return *this; }
  167. vec3& operator *= ( const GLfloat s )
  168. { x *= s; y *= s; z *= s; return *this; }
  169. vec3& operator *= ( const vec3& v )
  170. { x *= v.x; y *= v.y; z *= v.z; return *this; }
  171. vec3& operator /= ( const GLfloat s ) {
  172. #ifdef DEBUG
  173. if ( std::fabs(s) < DivideByZeroTolerance ) {
  174. std::cerr << "[" << __FILE__ << ":" << __LINE__ << "] "
  175. << "Division by zero" << std::endl;
  176. }
  177. #endif // DEBUG
  178. GLfloat r = GLfloat(1.0) / s;
  179. *this *= r;
  180. return *this;
  181. }
  182. //
  183. // --- Insertion and Extraction Operators ---
  184. //
  185. friend std::ostream& operator << ( std::ostream& os, const vec3& v ) {
  186. return os << "( " << v.x << ", " << v.y << ", " << v.z << " )";
  187. }
  188. friend std::istream& operator >> ( std::istream& is, vec3& v )
  189. { return is >> v.x >> v.y >> v.z ; }
  190. //
  191. // --- Conversion Operators ---
  192. //
  193. operator const GLfloat* () const
  194. { return static_cast<const GLfloat*>( &x ); }
  195. operator GLfloat* ()
  196. { return static_cast<GLfloat*>( &x ); }
  197. };
  198. //----------------------------------------------------------------------------
  199. //
  200. // Non-class vec3 Methods
  201. //
  202. inline
  203. GLfloat dot( const vec3& u, const vec3& v ) {
  204. return u.x*v.x + u.y*v.y + u.z*v.z ;
  205. }
  206. inline
  207. GLfloat length( const vec3& v ) {
  208. return std::sqrt( dot(v,v) );
  209. }
  210. inline
  211. vec3 normalize( const vec3& v ) {
  212. return v / length(v);
  213. }
  214. inline
  215. vec3 cross(const vec3& a, const vec3& b )
  216. {
  217. return vec3( a.y * b.z - a.z * b.y,
  218. a.z * b.x - a.x * b.z,
  219. a.x * b.y - a.y * b.x );
  220. }
  221. //////////////////////////////////////////////////////////////////////////////
  222. //
  223. // vec4 - 4D vector
  224. //
  225. //////////////////////////////////////////////////////////////////////////////
  226. struct vec4 {
  227. GLfloat x;
  228. GLfloat y;
  229. GLfloat z;
  230. GLfloat w;
  231. //
  232. // --- Constructors and Destructors ---
  233. //
  234. vec4( GLfloat s = GLfloat(0.0) ) :
  235. x(s), y(s), z(s), w(s) {}
  236. vec4( GLfloat x, GLfloat y, GLfloat z, GLfloat w ) :
  237. x(x), y(y), z(z), w(w) {}
  238. vec4( const vec4& v ) { x = v.x; y = v.y; z = v.z; w = v.w; }
  239. vec4( const vec3& v, const float w = 1.0 ) : w(w)
  240. { x = v.x; y = v.y; z = v.z; }
  241. vec4( const vec2& v, const float z, const float w ) : z(z), w(w)
  242. { x = v.x; y = v.y; }
  243. //
  244. // --- Indexing Operator ---
  245. //
  246. GLfloat& operator [] ( int i ) { return *(&x + i); }
  247. const GLfloat operator [] ( int i ) const { return *(&x + i); }
  248. //
  249. // --- (non-modifying) Arithematic Operators ---
  250. //
  251. vec4 operator - () const // unary minus operator
  252. { return vec4( -x, -y, -z, -w ); }
  253. vec4 operator + ( const vec4& v ) const
  254. { return vec4( x + v.x, y + v.y, z + v.z, w + v.w ); }
  255. vec4 operator - ( const vec4& v ) const
  256. { return vec4( x - v.x, y - v.y, z - v.z, w - v.w ); }
  257. vec4 operator * ( const GLfloat s ) const
  258. { return vec4( s*x, s*y, s*z, s*w ); }
  259. vec4 operator * ( const vec4& v ) const
  260. { return vec4( x*v.x, y*v.y, z*v.z, w*v.z ); }
  261. friend vec4 operator * ( const GLfloat s, const vec4& v )
  262. { return v * s; }
  263. vec4 operator / ( const GLfloat s ) const {
  264. #ifdef DEBUG
  265. if ( std::fabs(s) < DivideByZeroTolerance ) {
  266. std::cerr << "[" << __FILE__ << ":" << __LINE__ << "] "
  267. << "Division by zero" << std::endl;
  268. return vec4();
  269. }
  270. #endif // DEBUG
  271. GLfloat r = GLfloat(1.0) / s;
  272. return *this * r;
  273. }
  274. //
  275. // --- (modifying) Arithematic Operators ---
  276. //
  277. vec4& operator += ( const vec4& v )
  278. { x += v.x; y += v.y; z += v.z; w += v.w; return *this; }
  279. vec4& operator -= ( const vec4& v )
  280. { x -= v.x; y -= v.y; z -= v.z; w -= v.w; return *this; }
  281. vec4& operator *= ( const GLfloat s )
  282. { x *= s; y *= s; z *= s; w *= s; return *this; }
  283. vec4& operator *= ( const vec4& v )
  284. { x *= v.x, y *= v.y, z *= v.z, w *= v.w; return *this; }
  285. vec4& operator /= ( const GLfloat s ) {
  286. #ifdef DEBUG
  287. if ( std::fabs(s) < DivideByZeroTolerance ) {
  288. std::cerr << "[" << __FILE__ << ":" << __LINE__ << "] "
  289. << "Division by zero" << std::endl;
  290. }
  291. #endif // DEBUG
  292. GLfloat r = GLfloat(1.0) / s;
  293. *this *= r;
  294. return *this;
  295. }
  296. //
  297. // --- Insertion and Extraction Operators ---
  298. //
  299. friend std::ostream& operator << ( std::ostream& os, const vec4& v ) {
  300. return os << "( " << v.x << ", " << v.y
  301. << ", " << v.z << ", " << v.w << " )";
  302. }
  303. friend std::istream& operator >> ( std::istream& is, vec4& v )
  304. { return is >> v.x >> v.y >> v.z >> v.w; }
  305. //
  306. // --- Conversion Operators ---
  307. //
  308. operator const GLfloat* () const
  309. { return static_cast<const GLfloat*>( &x ); }
  310. operator GLfloat* ()
  311. { return static_cast<GLfloat*>( &x ); }
  312. };
  313. //----------------------------------------------------------------------------
  314. //
  315. // Non-class vec4 Methods
  316. //
  317. inline
  318. GLfloat dot( const vec4& u, const vec4& v ) {
  319. return u.x*v.x + u.y*v.y + u.z*v.z + u.w+v.w;
  320. }
  321. inline
  322. GLfloat length( const vec4& v ) {
  323. return std::sqrt( dot(v,v) );
  324. }
  325. inline
  326. vec4 normalize( const vec4& v ) {
  327. return v / length(v);
  328. }
  329. inline
  330. vec3 cross(const vec4& a, const vec4& b )
  331. {
  332. return vec3( a.y * b.z - a.z * b.y,
  333. a.z * b.x - a.x * b.z,
  334. a.x * b.y - a.y * b.x );
  335. }
  336. //----------------------------------------------------------------------------
  337. } // namespace Angel
  338. #endif // __ANGEL_VEC_H__