00001 /*-----------------------------------------------------------------------* 00002 Project:Nintendo Augmented Reality Library. 00003 File:narVec2.h 00004 00005 Copyright (C)2011-2012 Nintendo Co., Ltd. All rights reserved. 00006 00007 These coded instructions, statements, and computer programs contain 00008 proprietary information of Nintendo and/or its licensed developers 00009 and are protected by national and international copyright laws. They 00010 may not be disclosed to third parties or copied or duplicated in any 00011 form, in whole or in part, without the prior written consent of 00012 Nintendo. 00013 The content herein is highly confidential and should be handled 00014 accordingly. 00015 *-----------------------------------------------------------------------*/ 00016 00018 00023 #ifndef NAR_VEC_2_H__ 00024 #define NAR_VEC_2_H__ 00025 00026 #include "narPort.h" 00027 #include "narMath.h" 00028 00029 namespace mw { namespace nar 00030 { 00034 template < class T > 00035 struct Vec2_ts 00036 { 00037 Vec2_ts() 00038 {} 00039 00040 Vec2_ts( T x, T y ) 00041 { 00042 Set( x, y ); 00043 } 00044 00045 Vec2_ts( T val ) 00046 { 00047 SetAll( val ); 00048 } 00049 00050 Vec2_ts( const Vec2_ts< T > & r ) 00051 { 00052 Set( r ); 00053 } 00054 00055 void Set( const Vec2_ts< T > & r ) 00056 { 00057 Set( r.X(), r.Y() ); 00058 } 00059 00060 void Set( T x, T y ) 00061 { 00062 X() = x; 00063 Y() = y; 00064 } 00065 00066 void SetAll( T val ) 00067 { 00068 Set( val, val ); 00069 } 00070 00071 T & X() { return ma_[ me_X ]; } 00072 T & Y() { return ma_[ me_Y ]; } 00073 const T & X() const { return ma_[ me_X ]; } 00074 const T & Y() const { return ma_[ me_Y ]; } 00075 00078 Vec2_ts & Add( T x, T y ) 00079 { 00080 X() += x; 00081 Y() += y; 00082 return *this; 00083 } 00084 00087 Vec2_ts & Add( const Vec2_ts< T > & r ) 00088 { 00089 return Add( r.X(), r.Y() ); 00090 } 00091 00094 Vec2_ts operator+( const Vec2_ts< T > & r ) const 00095 { 00096 Vec2_ts ret( *this ); 00097 return ret.Add( r ); 00098 } 00099 00102 Vec2_ts & Sub( T x, T y ) 00103 { 00104 X() -= x; 00105 Y() -= y; 00106 return *this; 00107 } 00108 00111 Vec2_ts & Sub( const Vec2_ts< T > & r ) 00112 { 00113 return Sub( r.X(), r.Y() ); 00114 } 00115 00118 Vec2_ts & Scale( T a ) 00119 { 00120 X() *= a; 00121 Y() *= a; 00122 return *this; 00123 } 00124 00127 Vec2_ts operator*( T s ) const 00128 { 00129 Vec2_ts< T > ret( *this ); 00130 return ret.Scale( s ); 00131 } 00132 00135 Vec2_ts & Div( T a ) 00136 { 00137 T invA = static_cast< T >( 1 ) / a; 00138 return Scale( invA ); 00139 } 00140 00143 T Dot( const Vec2_ts< T > & r ) const 00144 { 00145 return X() * r.X() + Y() * r.Y(); 00146 } 00147 00150 T Cross( const Vec2_ts< T > & r ) const 00151 { 00152 return X() * r.Y() - Y() * r.X(); 00153 } 00154 00157 bool operator==( const Vec2_ts< T > & r ) const 00158 { 00159 return X() == r.X() && Y() == r.Y(); 00160 } 00161 00164 bool operator!=( const Vec2_ts< T > & r ) const 00165 { 00166 return !* this == r; 00167 } 00168 00171 bool Equal( T x, T y ) 00172 { 00173 return X() == x && Y() == y; 00174 } 00175 00178 void Rotate90() 00179 { 00180 Set( -Y(), X() ); 00181 } 00182 00185 T GetSquareLength() const 00186 { 00187 return Pow2( X() ) + Pow2( Y() ); 00188 } 00189 00192 T GetSquareDistance( const Vec2_ts< T > & r ) const 00193 { 00194 Vec2_ts< T > ret( r ); 00195 ret.Sub( *this ); 00196 return ret.GetSquareLength(); 00197 } 00198 00199 private: 00200 enum 00201 { 00202 me_X, me_Y, me_ArrayNum 00203 }; 00204 00205 T ma_[ me_ArrayNum ]; 00206 }; 00207 00208 struct Vec2I_st; 00212 struct Vec2F_st : public Vec2_ts< f32 > 00213 { 00214 Vec2F_st() 00215 {} 00216 00217 Vec2F_st( const Vec2_ts< f32 > & r ) 00218 { 00219 Set( r.X(), r.Y() ); 00220 } 00221 00224 Vec2F_st( const Vec2I_st & r ); 00225 00226 Vec2F_st( int x, int y ) 00227 { 00228 Set( static_cast< f32 >( x ), static_cast< f32 >( y ) ); 00229 } 00230 00231 Vec2F_st( u32 x, u32 y ) 00232 { 00233 Set( static_cast< f32 >( x ), static_cast< f32 >( y ) ); 00234 } 00235 00236 Vec2F_st( f32 x, f32 y ) 00237 : Vec2_ts< f32 >( x, y ) 00238 {} 00239 00242 bool Normalize() 00243 { 00244 f32 len = GetLength(); 00245 if ( len < c_Epsilon ) return false; 00246 Scale( static_cast< f32 >( 1.0 ) / len ); 00247 return true; 00248 } 00249 00252 f32 GetLength() 00253 { 00254 return Sqrt( GetSquareLength() ); 00255 } 00256 00257 }; 00258 00262 struct Vec2I_st :public Vec2_ts< s32 > 00263 { 00264 Vec2I_st() 00265 {} 00266 00267 Vec2I_st( s32 x ) 00268 : Vec2_ts< s32 >( x ) 00269 {} 00270 00271 Vec2I_st( s32 x, s32 y ) 00272 : Vec2_ts< s32 >( x, y ) 00273 {} 00274 00275 Vec2I_st & Scale( f32 a ) 00276 { 00277 X() *= a; 00278 Y() *= a; 00279 return *this; 00280 } 00281 }; 00282 00285 inline Vec2F_st::Vec2F_st( const Vec2I_st & r ) 00286 { 00287 Set( static_cast< f32 >( r.X() ), 00288 static_cast< f32 >( r.Y() ) ); 00289 } 00290 00291 template < class T > 00292 static inline Vec2_ts< T > operator+( const Vec2_ts< T > & r_A, const Vec2_ts< T > & r_B ) 00293 { 00294 Vec2_ts< T > ret( r_A ); 00295 return ret.Add( r_B ); 00296 } 00297 00298 } 00299 } 00300 #endif 00301
© 2011-2012 Nintendo. All rights reserved.
CONFIDENTIAL