include/crbn/basic/vec2.hpp

00001 
00002 #ifndef __vec2d_hpp__
00003 #define __vec2d_hpp__
00004 
00005 #include <crbn/basic/scalar.hpp>
00006 
00007 typedef struct vec2 {
00008   union {
00009     struct { float x, y; };
00010     float data[2];
00011   };
00012 
00013   vec2() : x(0.0), y(0.0) {}
00014   vec2( const float& a ) : x(a), y(a) {}
00015   vec2( const float& a, const float& b ) : x(a), y(b) {}
00016   vec2( const vec2& v ) : x(v.x), y(v.y) {};
00017 };
00018 
00019 // a = b + c
00020 inline void vadd( vec2& a, vec2& b, vec2& c ) {
00021   a.x = b.x + c.x;
00022   a.y = b.y + c.y;
00023 }
00024 // a += b
00025 inline void vadd( vec2& a, vec2& b ) {
00026   a.x += b.x;
00027   a.y += b.y;
00028 }
00029 // a = b - c
00030 inline void vsub( vec2& a, vec2& b, vec2& c ) {
00031   a.x = b.x - c.x;
00032   a.y = b.y - c.y;
00033 }
00034 // a -= b
00035 inline void vsub( vec2& a, vec2& b ) {
00036   a.x -= b.x;
00037   a.y -= b.y;
00038 }
00039 
00040 // a = -b
00041 inline void vneg( vec2& a, vec2& b ){
00042   a.x = - b.x;
00043   a.y = - b.y;
00044 } 
00045 // a = -a
00046 inline void vneg( vec2& a ) {
00047   a.x = - a.x;
00048   a.y = - a.y;
00049 }
00050 
00051 //  a = d * b
00052 inline void vmul( vec2& a, float f, vec2& b ) {
00053   a.x = f * b.x;
00054   a.y = f * b.y;
00055 }
00056 
00057 //  a *= d
00058 inline void vmul( vec2& a, const float f ) {
00059   a.x *= f;
00060   a.y *= f;
00061 }
00062 // a_i *= b_i for each component
00063 inline void vmul( vec2& a, vec2& b ) {
00064   a.x *= b.x;
00065   a.y *= b.y;
00066 }
00067 
00068 //  a = b / d
00069 inline void vdiv( vec2& a, vec2& b, float f ) {
00070   f = 1.0f / f;
00071   a.x = f * b.x;
00072   a.y = f * b.y;
00073 }
00074 //  a /= d
00075 inline void vdiv( vec2& a, float f ) {
00076   f = 1.0f / f;
00077   a.x *= f;
00078   a.y *= f;
00079 }
00080 
00081 // a += b + f * c
00082 inline void vaddfmul( vec2& a,  vec2& b, float f,  vec2& c ) {
00083   a.x += b.x + f * c.x;
00084   a.y += b.y + f * c.y;
00085 }
00086 
00087 // a += f * b
00088 inline void vaddfmul( vec2& a, float f,  vec2& b ) {
00089   a.x += f * b.x;
00090   a.y += f * b.y;
00091 }
00092 
00093 // a = normalize( b )
00094 inline void vnormalize( vec2& a,  vec2& b ) {
00095   float l = 1.0f / sqrtf( sqr( b.x ) + sqr( b.y ) );
00096   a.x = b.x * l;
00097   a.y = b.y * l;
00098 }
00099 // a = normalize( a )
00100 inline void vnormalize( vec2& a ) {
00101   float l = 1.0f / sqrtf( sqr( a.x ) + sqr( a.y ) );
00102   a.x *= l;
00103   a.y *= l;
00104 }
00105 
00106 // ret = norm( v )
00107 inline float vlength( vec2& v ) {
00108   return sqrtf( sqr( v.x ) + sqr( v.y ) );
00109 }
00110 // ret = norm( v ) ^ 2
00111 inline float vlength2( vec2& v ) {
00112   return sqr( v.x ) + sqr( v.y );
00113 }
00114 
00115 // ret = distance( a, b )
00116 inline float vdistance( vec2& a, vec2& b ) {
00117   return sqrtf( sqr( a.x - b.x ) + sqr( a.y - b.y ) );
00118 }
00119 // ret = distance( a, b ) ^ 2
00120 inline float vdistance2( vec2& a, vec2& b ) {
00121   return sqr( a.x - b.x ) + sqr( a.y - b.y );
00122 }
00123 
00124 // ret = a . b
00125 inline float vdot( vec2& a, vec2& b ) {
00126   return a.x * b.x + a.x * b.x;
00127 }
00128 //            o
00129 // ret = a . b
00130 inline float vdoto( vec2& a, vec2& b ) {
00131   return a.y * b.x - a.x * b.y;
00132 }
00133 // ret = b ^ c
00134 inline float vcross( vec2& a, vec2& b ) {
00135   return a.x * b.y - b.x * a.y;
00136 }
00137 
00138 // v_i = min( a_i, b_i ) for each component
00139 inline void vmin( vec2& v, vec2& a, vec2& b ) {
00140   v.x = min( a.x, b.x );
00141   v.y = min( a.y, b.y );
00142 }
00143 // v_i = max( a_i, b_i ) for each component
00144 inline void vmax( vec2& v, vec2& a, vec2& b ) {
00145   v.x = max( a.x, b.x );
00146   v.y = max( a.y, b.y );
00147 }
00148 
00149 #endif // __vec2d_hpp__

Generated on Tue Nov 14 15:40:08 2006 for libcrbn by  doxygen 1.5.0