1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
|
/****************************************************************************
* VCGLib o o *
* Visual and Computer Graphics Library o o *
* _ O _ *
* Copyright(C) 2004-2019 \/)\/ *
* Visual Computing Lab /\/| *
* ISTI - Italian National Research Council | *
* \ *
* All rights reserved. *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License (http://www.gnu.org/licenses/gpl.txt) *
* for more details. *
* *
****************************************************************************/
/****************************************************************************
History
$Log: not supported by cvs2svn $
Revision 1.9 2006/10/07 16:51:43 m_di_benedetto
Implemented Scale() method (was only declared).
Revision 1.8 2006/01/19 13:53:19 m_di_benedetto
Fixed product by scalar and SquaredNorm()
Revision 1.7 2005/10/15 19:11:49 m_di_benedetto
Corrected return type in Angle() and protected member access in unary operator -
Revision 1.6 2005/03/18 16:34:42 fiorin
minor changes to comply gcc compiler
Revision 1.5 2004/05/10 13:22:25 cignoni
small syntax error Math -> math in Angle
Revision 1.4 2004/04/05 11:57:32 cignoni
Add V() access function
Revision 1.3 2004/03/10 17:42:40 tarini
Added comments (Dox) !
Added Import(). Costruct(), ScalarType... Corrected cross prod (sign). Added Angle. Now using Math:: stuff for trigon. etc.
Revision 1.2 2004/03/03 15:07:40 cignoni
renamed protected member v -> _v
Revision 1.1 2004/02/13 00:44:53 cignoni
First commit...
****************************************************************************/
#ifndef __VCGLIB_POINT2
#define __VCGLIB_POINT2
#include <assert.h>
#include <vcg/math/base.h>
namespace vcg {
/** \addtogroup space */
/*@{*/
/**
The templated class for representing a point in 2D space.
The class is templated over the ScalarType class that is used to represent coordinates.
All the usual operator overloading (* + - ...) is present.
*/
template <class P2ScalarType>
class Point2
{
protected:
/// The only data member. Hidden to user.
P2ScalarType _v[2];
public:
/// the scalar type
typedef P2ScalarType ScalarType;
enum {Dimension = 2};
//@{
/** @name Access to Coords.
access to coords is done by overloading of [] or explicit naming of coords (X,Y,)
("p[0]" or "p.X()" are equivalent) **/
inline const ScalarType &X() const {return _v[0];}
inline const ScalarType &Y() const {return _v[1];}
inline ScalarType &X() {return _v[0];}
inline ScalarType &Y() {return _v[1];}
inline const ScalarType * V() const
{
return _v;
}
inline ScalarType * V()
{
return _v;
}
inline ScalarType & V( const int i )
{
assert(i>=0 && i<2);
return _v[i];
}
inline const ScalarType & V( const int i ) const
{
assert(i>=0 && i<2);
return _v[i];
}
inline const ScalarType & operator [] ( const int i ) const
{
assert(i>=0 && i<2);
return _v[i];
}
inline ScalarType & operator [] ( const int i )
{
assert(i>=0 && i<2);
return _v[i];
}
//@}
/// empty constructor (does nothing)
inline Point2 () { }
/// x,y constructor
inline Point2 ( const ScalarType nx, const ScalarType ny )
{
_v[0] = nx; _v[1] = ny;
}
/// copy constructor
inline Point2 ( const Point2 & p)
{
_v[0]= p._v[0]; _v[1]= p._v[1];
}
/// copy
inline Point2 & operator =( const Point2 & p)
{
_v[0]= p._v[0]; _v[1]= p._v[1];
return *this;
}
/// sets the point to (0,0)
inline void SetZero()
{ _v[0] = 0;_v[1] = 0;}
/// dot product
inline ScalarType operator * ( const Point2 & p ) const
{
return ( _v[0]*p._v[0] + _v[1]*p._v[1] );
}
inline ScalarType dot( const Point2 & p ) const { return (*this) * p; }
/// cross product
inline ScalarType operator ^ ( const Point2 & p ) const
{
return _v[0]*p._v[1] - _v[1]*p._v[0];
}
//@{
/** @name Linearity for 2d points (operators +, -, *, /, *= ...) **/
inline Point2 operator + ( const Point2 & p) const
{
return Point2<ScalarType>( _v[0]+p._v[0], _v[1]+p._v[1] );
}
inline Point2 operator - ( const Point2 & p) const
{
return Point2<ScalarType>( _v[0]-p._v[0], _v[1]-p._v[1] );
}
inline Point2 operator * ( const ScalarType s ) const
{
return Point2<ScalarType>( _v[0] * s, _v[1] * s );
}
inline Point2 operator / ( const ScalarType s ) const
{
return Point2<ScalarType>( _v[0] / s, _v[1] / s );
}
inline Point2 & operator += ( const Point2 & p)
{
_v[0] += p._v[0];
_v[1] += p._v[1];
return *this;
}
inline Point2 & operator -= ( const Point2 & p)
{
_v[0] -= p._v[0];
_v[1] -= p._v[1];
return *this;
}
inline Point2 & operator *= ( const ScalarType s )
{
_v[0] *= s;
_v[1] *= s;
return *this;
}
inline Point2 & operator /= ( const ScalarType s )
{
_v[0] /= s;
_v[1] /= s;
return *this;
}
//@}
/// returns the norm (Euclidian)
inline ScalarType Norm( void ) const
{
return math::Sqrt( _v[0]*_v[0] + _v[1]*_v[1] );
}
/// returns the squared norm (Euclidian)
inline ScalarType SquaredNorm( void ) const
{
return ( _v[0]*_v[0] + _v[1]*_v[1] );
}
inline Point2 & Scale( const ScalarType sx, const ScalarType sy )
{
_v[0] *= sx;
_v[1] *= sy;
return * this;
}
/// normalizes, and returns itself as result
inline Point2 & Normalize( void )
{
ScalarType n = math::Sqrt(_v[0]*_v[0] + _v[1]*_v[1]);
if(n>0.0) {
_v[0] /= n; _v[1] /= n;
}
return *this;
}
/// points equality
inline bool operator == ( const Point2 & p ) const
{
return (_v[0]==p._v[0] && _v[1]==p._v[1]);
}
/// disparity between points
inline bool operator != ( const Point2 & p ) const
{
return ( (_v[0]!=p._v[0]) || (_v[1]!=p._v[1]) );
}
/// lexical ordering
inline bool operator < ( const Point2 & p ) const
{
return (_v[1]!=p._v[1])?(_v[1]<p._v[1]):
(_v[0]<p._v[0]);
}
/// lexical ordering
inline bool operator > ( const Point2 & p ) const
{
return (_v[1]!=p._v[1])?(_v[1]>p._v[1]):
(_v[0]>p._v[0]);
}
/// lexical ordering
inline bool operator <= ( const Point2 & p ) const
{
return (_v[1]!=p._v[1])?(_v[1]< p._v[1]):
(_v[0]<=p._v[0]);
}
/// lexical ordering
inline bool operator >= ( const Point2 & p ) const
{
return (_v[1]!=p._v[1])?(_v[1]> p._v[1]):
(_v[0]>=p._v[0]);
}
/// returns the distance to another point p
inline ScalarType Distance( const Point2 & p ) const
{
return Norm(*this-p);
}
/// returns the suqared distance to another point p
inline ScalarType SquaredDistance( const Point2 & p ) const
{
return (*this-p).SquaredNorm();
}
/// returns the angle with X axis (radiants, in [-PI, +PI] )
inline ScalarType Angle() const
{
return math::Atan2(_v[1],_v[0]);
}
/// transform the point in cartesian coords into polar coords
inline Point2 & Cartesian2Polar()
{
ScalarType t = Angle();
_v[0] = Norm();
_v[1] = t;
return *this;
}
/// transform the point in polar coords into cartesian coords
inline Point2 & Polar2Cartesian()
{
ScalarType l = _v[0];
_v[0] = (ScalarType)(l*math::Cos(_v[1]));
_v[1] = (ScalarType)(l*math::Sin(_v[1]));
return *this;
}
/// rotates the point of an angle (radiants, counterclockwise)
inline Point2 & Rotate( const ScalarType rad )
{
ScalarType t = _v[0];
ScalarType s = math::Sin(rad);
ScalarType c = math::Cos(rad);
_v[0] = _v[0]*c - _v[1]*s;
_v[1] = t*s + _v[1]*c;
return *this;
}
/// This function extends the vector to any arbitrary domension
/// virtually padding missing elements with zeros
inline ScalarType Ext( const int i ) const
{
if(i>=0 && i<2)
return _v[i];
else
return 0;
}
/// imports from 2D points of different types
template <class T>
inline void Import( const Point2<T> & b )
{
_v[0] = ScalarType(b.X());
_v[1] = ScalarType(b.Y());
}
template <class EigenVector>
inline void FromEigenVector(const EigenVector & b)
{
_v[0] = ScalarType(b[0]);
_v[1] = ScalarType(b[1]);
}
template <class EigenVector>
inline void ToEigenVector(EigenVector & b) const
{
b[0]=_v[0];
b[1]=_v[1];
}
template <class EigenVector>
inline EigenVector ToEigenVector(void) const
{
assert(EigenVector::RowsAtCompileTime == 2);
EigenVector b;
b << _v[0], _v[1];
return b;
}
/// constructs a 2D points from an existing one of different type
template <class T>
static Point2 Construct( const Point2<T> & b )
{
return Point2(ScalarType(b.X()), ScalarType(b.Y()));
}
static Point2 Construct( const Point2<ScalarType> & b )
{
return b;
}
template <class T>
static Point2 Construct( const T & x, const T & y)
{
return Point2(ScalarType(x), ScalarType(y));
}
static inline Point2 Zero(void)
{
return Point2(0,0);
}
static inline Point2 One(void)
{
return Point2(1,1);
}
}; // end class definition
template <class T>
inline T Angle( Point2<T> const & p0, Point2<T> const & p1 )
{
return p1.Angle() - p0.Angle();
}
template <class T>
inline Point2<T> operator - ( Point2<T> const & p ){
return Point2<T>( -p[0], -p[1] );
}
template <class T>
inline Point2<T> operator * ( const T s, Point2<T> const & p ){
return Point2<T>( p[0] * s, p[1] * s );
}
template <class T>
inline T Norm( Point2<T> const & p ){
return p.Norm();
}
template <class T>
inline T SquaredNorm( Point2<T> const & p ){
return p.SquaredNorm();
}
template <class T>
inline Point2<T> & Normalize( Point2<T> & p ){
return p.Normalize();
}
template <class T>
inline T Distance( Point2<T> const & p1,Point2<T> const & p2 ){
return Norm(p1-p2);
}
template <class T>
inline T SquaredDistance( Point2<T> const & p1,Point2<T> const & p2 ){
return SquaredNorm(p1-p2);
}
template <class T>
inline Point2<T> Abs(const Point2<T> & p) {
return (Point2<T>(math::Abs(p[0]), math::Abs(p[1])));
}
typedef Point2<short> Point2s;
typedef Point2<int> Point2i;
typedef Point2<float> Point2f;
typedef Point2<double> Point2d;
/*@}*/
} // end namespace
#endif
|