Vector2.hh
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #ifndef VECTOR2_HH
00028 #define VECTOR2_HH
00029
00030 #include <iostream>
00031 #include <math.h>
00032 #include <fstream>
00033
00034 namespace gazebo
00035 {
00039
00041 template< typename T>
00042 class Vector2
00043 {
00045 public: Vector2();
00046
00048 public: Vector2( const T &x, const T &y );
00049
00051 public: Vector2( const Vector2<T> &pt );
00052
00054 public: virtual ~Vector2();
00055
00057 public: T Distance( const Vector2<T> &pt ) const;
00058
00060 public: void Normalize();
00061
00063 public: void Set(T x, T y);
00064
00066 public: Vector2<T> GetCrossProd(const Vector2<T> &pt) const;
00067
00069 public: const Vector2<T> &operator=( const Vector2<T> &pt );
00070
00072 public: const Vector2<T> &operator=( T value );
00073
00075 public: Vector2<T> operator+( const Vector2<T> &pt ) const;
00076
00078 public: const Vector2<T> &operator+=( const Vector2<T> &pt );
00079
00081 public: Vector2<T> operator-( const Vector2<T> &pt ) const;
00082
00084 public: const Vector2<T> &operator-=( const Vector2<T> &pt );
00085
00087 public: const Vector2<T> operator/( const Vector2<T> &pt ) const;
00088
00090 public: const Vector2<T> &operator/=( const Vector2<T> &pt );
00091
00093 public: const Vector2<T> operator/( T v ) const;
00094
00096 public: const Vector2<T> &operator/=( T v );
00097
00099 public: const Vector2<T> operator*( const Vector2<T> &pt ) const;
00100
00102 public: const Vector2<T> &operator*=( const Vector2<T> &pt );
00103
00105 public: const Vector2<T> operator*( T v ) const;
00106
00108 public: const Vector2<T> &operator*=( T v );
00109
00111 public: bool operator==( const Vector2<T> &pt ) const;
00112
00114 public: bool operator!=( const Vector2<T> &pt ) const;
00115
00117 public: bool IsFinite() const;
00118
00120 public: T operator[](unsigned int index) const;
00121
00123 public: T x;
00124
00126 public: T y;
00127
00132 public: friend std::ostream &operator<<( std::ostream &out, const gazebo::Vector2<T> &pt )
00133 {
00134 out << pt.x << " " << pt.y;
00135
00136 return out;
00137 }
00138
00139 };
00140
00142
00143
00145
00146 template<typename T>
00147 Vector2<T>::Vector2()
00148 : x(0), y(0)
00149 {
00150 }
00151
00153
00154 template<typename T>
00155 Vector2<T>::Vector2( const T &x, const T &y )
00156 : x(x), y(y)
00157 {
00158 }
00159
00161
00162 template<typename T>
00163 Vector2<T>::Vector2( const Vector2 &pt )
00164 : x(pt.x), y(pt.y)
00165 {
00166 }
00167
00169
00170 template<typename T>
00171 Vector2<T>::~Vector2()
00172 {
00173 }
00174
00176
00177 template<typename T>
00178 T Vector2<T>::Distance(const Vector2<T> &pt ) const
00179 {
00180 return (T)sqrt((this->x-pt.x)*(this->x-pt.x) + (this->y-pt.y)*(this->y-pt.y));
00181 }
00182
00184
00185 template<typename T>
00186 void Vector2<T>::Normalize()
00187 {
00188 T d = (T)sqrt(this->x * this->x + this->y * this->y);
00189
00190 this->x /= d;
00191 this->y /= d;
00192 }
00193
00195
00196 template<typename T>
00197 void Vector2<T>::Set(T x, T y)
00198 {
00199 this->x = x;
00200 this->y = y;
00201 }
00202
00203
00205
00206 template<typename T>
00207 const Vector2<T> &Vector2<T>::operator=( const Vector2<T> &pt )
00208 {
00209 this->x = pt.x;
00210 this->y = pt.y;
00211
00212 return *this;
00213 }
00214
00217 template<typename T>
00218 const Vector2<T> &Vector2<T>::operator=( T value )
00219 {
00220 this->x = value;
00221 this->y = value;
00222
00223 return *this;
00224 }
00225
00226
00227
00229
00230 template<typename T>
00231 Vector2<T> Vector2<T>::operator+( const Vector2<T> &pt ) const
00232 {
00233 return Vector2<T>(this->x + pt.x, this->y + pt.y);
00234 }
00235
00236 template<typename T>
00237 const Vector2<T> &Vector2<T>::operator+=( const Vector2<T> &pt )
00238 {
00239 this->x += pt.x;
00240 this->y += pt.y;
00241
00242 return *this;
00243 }
00244
00246
00247 template<typename T>
00248 Vector2<T> Vector2<T>::operator-( const Vector2<T> &pt ) const
00249 {
00250 return Vector2<T>(this->x - pt.x, this->y - pt.y);
00251 }
00252
00253 template<typename T>
00254 const Vector2<T> &Vector2<T>::operator-=( const Vector2<T> &pt )
00255 {
00256 this->x -= pt.x;
00257 this->y -= pt.y;
00258
00259 return *this;
00260 }
00261
00262
00264
00265
00266 template<typename T>
00267 const Vector2<T> Vector2<T>::operator/( const Vector2<T> &pt ) const
00268 {
00269 return Vector2<T>(this->x / pt.x, this->y / pt.y);
00270 }
00271
00272 template<typename T>
00273 const Vector2<T> &Vector2<T>::operator/=( const Vector2<T> &pt )
00274 {
00275 this->x /= pt.x;
00276 this->y /= pt.y;
00277
00278 return *this;
00279 }
00280
00281 template<typename T>
00282 const Vector2<T> Vector2<T>::operator/( T v ) const
00283 {
00284 return Vector2<T>(this->x / v, this->y / v);
00285 }
00286
00287 template<typename T>
00288 const Vector2<T> &Vector2<T>::operator/=( T v )
00289 {
00290 this->x /= v;
00291 this->y /= v;
00292
00293 return *this;
00294 }
00295
00296
00297
00299
00300 template<typename T>
00301 const Vector2<T> Vector2<T>::operator*( const Vector2<T> &pt ) const
00302 {
00303 return Vector2<T>(this->x * pt.x, this->y * pt.y);
00304 }
00305
00306 template<typename T>
00307 const Vector2<T> &Vector2<T>::operator*=( const Vector2<T> &pt )
00308 {
00309 this->x *= pt.x;
00310 this->y *= pt.y;
00311
00312 return *this;
00313 }
00314
00315 template<typename T>
00316 const Vector2<T> Vector2<T>::operator*( T v ) const
00317 {
00318 return Vector2<T>(this->x * v, this->y * v);
00319 }
00320
00321 template<typename T>
00322 const Vector2<T> &Vector2<T>::operator*=( T v)
00323 {
00324 this->x *= v;
00325 this->y *= v;
00326
00327 return *this;
00328 }
00329
00331
00332 template<typename T>
00333 bool Vector2<T>::operator==( const Vector2<T> &pt ) const
00334 {
00335 return this->x == pt.x && this->y == pt.y;
00336 }
00337
00339
00340 template<typename T>
00341 bool Vector2<T>::operator!=( const Vector2<T> &pt ) const
00342 {
00343 return !(*this == pt);
00344 }
00345
00347
00348 template<typename T>
00349 bool Vector2<T>::IsFinite() const
00350 {
00351 return finite(this->x) && finite(this->y);
00352 }
00353
00356 template<typename T>
00357 T Vector2<T>::operator[](unsigned int index) const
00358 {
00359 switch (index)
00360 {
00361 case 0:
00362 return this->x;
00363 case 1:
00364 return this->y;
00365 default:
00366 return 0;
00367 }
00368 }
00369 }
00370
00371 #endif