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
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058 #ifndef __VCGLIB_POINT2
00059 #define __VCGLIB_POINT2
00060
00061 #include <assert.h>
00062 #include <vcg/math/base.h>
00063
00064 namespace vcg {
00065
00073 template <class P2ScalarType> class Point2
00074 {
00075 protected:
00077 P2ScalarType _v[2];
00078 public:
00080 typedef P2ScalarType ScalarType;
00081 enum {Dimension = 2};
00082
00084
00088 inline const ScalarType &X() const {return _v[0];}
00089 inline const ScalarType &Y() const {return _v[1];}
00090 inline ScalarType &X() {return _v[0];}
00091 inline ScalarType &Y() {return _v[1];}
00092 inline const ScalarType * V() const
00093 {
00094 return _v;
00095 }
00096 inline ScalarType * V()
00097 {
00098 return _v;
00099 }
00100 inline ScalarType & V( const int i )
00101 {
00102 assert(i>=0 && i<2);
00103 return _v[i];
00104 }
00105 inline const ScalarType & V( const int i ) const
00106 {
00107 assert(i>=0 && i<2);
00108 return _v[i];
00109 }
00110 inline const ScalarType & operator [] ( const int i ) const
00111 {
00112 assert(i>=0 && i<2);
00113 return _v[i];
00114 }
00115 inline ScalarType & operator [] ( const int i )
00116 {
00117 assert(i>=0 && i<2);
00118 return _v[i];
00119 }
00121
00122 inline Point2 () { }
00124 inline Point2 ( const ScalarType nx, const ScalarType ny )
00125 {
00126 _v[0] = nx; _v[1] = ny;
00127 }
00129 inline Point2 ( Point2 const & p)
00130 {
00131 _v[0]= p._v[0]; _v[1]= p._v[1];
00132 }
00134 inline Point2 & operator =( Point2 const & p)
00135 {
00136 _v[0]= p._v[0]; _v[1]= p._v[1];
00137 return *this;
00138 }
00140 inline void SetZero()
00141 { _v[0] = 0;_v[1] = 0;}
00143 inline ScalarType operator * ( Point2 const & p ) const
00144 {
00145 return ( _v[0]*p._v[0] + _v[1]*p._v[1] );
00146 }
00147 inline ScalarType dot( const Point2 & p ) const { return (*this) * p; }
00149 inline ScalarType operator ^ ( Point2 const & p ) const
00150 {
00151 return _v[0]*p._v[1] - _v[1]*p._v[0];
00152 }
00154
00155 inline Point2 operator + ( Point2 const & p) const
00156 {
00157 return Point2<ScalarType>( _v[0]+p._v[0], _v[1]+p._v[1] );
00158 }
00159 inline Point2 operator - ( Point2 const & p) const
00160 {
00161 return Point2<ScalarType>( _v[0]-p._v[0], _v[1]-p._v[1] );
00162 }
00163 inline Point2 operator * ( const ScalarType s ) const
00164 {
00165 return Point2<ScalarType>( _v[0] * s, _v[1] * s );
00166 }
00167 inline Point2 operator / ( const ScalarType s ) const
00168 {
00169 return Point2<ScalarType>( _v[0] / s, _v[1] / s );
00170 }
00171 inline Point2 & operator += ( Point2 const & p)
00172 {
00173 _v[0] += p._v[0]; _v[1] += p._v[1];
00174 return *this;
00175 }
00176 inline Point2 & operator -= ( Point2 const & p)
00177 {
00178 _v[0] -= p._v[0]; _v[1] -= p._v[1];
00179 return *this;
00180 }
00181 inline Point2 & operator *= ( const ScalarType s )
00182 {
00183 _v[0] *= s; _v[1] *= s;
00184 return *this;
00185 }
00186 inline Point2 & operator /= ( const ScalarType s )
00187 {
00188 _v[0] /= s; _v[1] /= s;
00189 return *this;
00190 }
00192
00193 inline ScalarType Norm( void ) const
00194 {
00195 return math::Sqrt( _v[0]*_v[0] + _v[1]*_v[1] );
00196 }
00198 inline ScalarType SquaredNorm( void ) const
00199 {
00200 return ( _v[0]*_v[0] + _v[1]*_v[1] );
00201 }
00202 inline Point2 & Scale( const ScalarType sx, const ScalarType sy )
00203 {
00204 _v[0] *= sx;
00205 _v[1] *= sy;
00206 return * this;
00207 }
00209 inline Point2 & Normalize( void )
00210 {
00211 ScalarType n = math::Sqrt(_v[0]*_v[0] + _v[1]*_v[1]);
00212 if(n>0.0) { _v[0] /= n; _v[1] /= n; }
00213 return *this;
00214 }
00216 inline bool operator == ( Point2 const & p ) const
00217 {
00218 return (_v[0]==p._v[0] && _v[1]==p._v[1]);
00219 }
00221 inline bool operator != ( Point2 const & p ) const
00222 {
00223 return ( (_v[0]!=p._v[0]) || (_v[1]!=p._v[1]) );
00224 }
00226 inline bool operator < ( Point2 const & p ) const
00227 {
00228 return (_v[1]!=p._v[1])?(_v[1]<p._v[1]):
00229 (_v[0]<p._v[0]);
00230 }
00232 inline bool operator > ( Point2 const & p ) const
00233 {
00234 return (_v[1]!=p._v[1])?(_v[1]>p._v[1]):
00235 (_v[0]>p._v[0]);
00236 }
00238 inline bool operator <= ( Point2 const & p ) const
00239 {
00240 return (_v[1]!=p._v[1])?(_v[1]< p._v[1]):
00241 (_v[0]<=p._v[0]);
00242 }
00244 inline bool operator >= ( Point2 const & p ) const
00245 {
00246 return (_v[1]!=p._v[1])?(_v[1]> p._v[1]):
00247 (_v[0]>=p._v[0]);
00248 }
00250 inline ScalarType Distance( Point2 const & p ) const
00251 {
00252 return Norm(*this-p);
00253 }
00255 inline ScalarType SquaredDistance( Point2 const & p ) const
00256 {
00257 return (*this-p).SquaredNorm();
00258 }
00260 inline ScalarType Angle() const {
00261 return math::Atan2(_v[1],_v[0]);
00262 }
00264 inline Point2 & Cartesian2Polar()
00265 {
00266 ScalarType t = Angle();
00267 _v[0] = Norm();
00268 _v[1] = t;
00269 return *this;
00270 }
00272 inline Point2 & Polar2Cartesian()
00273 {
00274 ScalarType l = _v[0];
00275 _v[0] = (ScalarType)(l*math::Cos(_v[1]));
00276 _v[1] = (ScalarType)(l*math::Sin(_v[1]));
00277 return *this;
00278 }
00280 inline Point2 & Rotate( const ScalarType rad )
00281 {
00282 ScalarType t = _v[0];
00283 ScalarType s = math::Sin(rad);
00284 ScalarType c = math::Cos(rad);
00285
00286 _v[0] = _v[0]*c - _v[1]*s;
00287 _v[1] = t *s + _v[1]*c;
00288
00289 return *this;
00290 }
00291
00294 inline ScalarType Ext( const int i ) const
00295 {
00296 if(i>=0 && i<2) return _v[i];
00297 else return 0;
00298 }
00300 template <class T>
00301 inline void Import( const Point2<T> & b )
00302 {
00303 _v[0] = b.X(); _v[1] = b.Y();
00304 }
00306 template <class T>
00307 static Point2 Construct( const Point2<T> & b )
00308 {
00309 return Point2(b.X(),b.Y());
00310 }
00311
00312
00313 };
00314
00315
00316 template <class T>
00317 inline T Angle( Point2<T> const & p0, Point2<T> const & p1 )
00318 {
00319 return p1.Angle() - p0.Angle();
00320 }
00321
00322 template <class T>
00323 inline Point2<T> operator - ( Point2<T> const & p ){
00324 return Point2<T>( -p[0], -p[1] );
00325 }
00326
00327 template <class T>
00328 inline Point2<T> operator * ( const T s, Point2<T> const & p ){
00329 return Point2<T>( p[0] * s, p[1] * s );
00330 }
00331
00332 template <class T>
00333 inline T Norm( Point2<T> const & p ){
00334 return p.Norm();
00335 }
00336
00337 template <class T>
00338 inline T SquaredNorm( Point2<T> const & p ){
00339 return p.SquaredNorm();
00340 }
00341
00342 template <class T>
00343 inline Point2<T> & Normalize( Point2<T> & p ){
00344 return p.Normalize();
00345 }
00346
00347 template <class T>
00348 inline T Distance( Point2<T> const & p1,Point2<T> const & p2 ){
00349 return Norm(p1-p2);
00350 }
00351
00352 template <class T>
00353 inline T SquaredDistance( Point2<T> const & p1,Point2<T> const & p2 ){
00354 return SquaredNorm(p1-p2);
00355 }
00356
00357 typedef Point2<short> Point2s;
00358 typedef Point2<int> Point2i;
00359 typedef Point2<float> Point2f;
00360 typedef Point2<double> Point2d;
00361
00363 }
00364 #endif