2004-02-13 01:44:53 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* VCGLib o o *
|
|
|
|
* Visual and Computer Graphics Library o o *
|
|
|
|
* _ O _ *
|
|
|
|
* Copyright(C) 2004 \/)\/ *
|
|
|
|
* 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 $
|
2004-05-10 15:22:25 +02:00
|
|
|
Revision 1.4 2004/04/05 11:57:32 cignoni
|
|
|
|
Add V() access function
|
|
|
|
|
2004-04-05 13:57:32 +02:00
|
|
|
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.
|
|
|
|
|
2004-03-10 18:42:40 +01:00
|
|
|
Revision 1.2 2004/03/03 15:07:40 cignoni
|
|
|
|
renamed protected member v -> _v
|
|
|
|
|
2004-03-03 16:07:40 +01:00
|
|
|
Revision 1.1 2004/02/13 00:44:53 cignoni
|
|
|
|
First commit...
|
|
|
|
|
2004-02-13 01:44:53 +01:00
|
|
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef __VCGLIB_POINT2
|
|
|
|
#define __VCGLIB_POINT2
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <vcg/math/base.h>
|
|
|
|
|
|
|
|
namespace vcg {
|
|
|
|
|
2004-03-10 18:42:40 +01:00
|
|
|
/** \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
|
2004-02-13 01:44:53 +01:00
|
|
|
{
|
|
|
|
protected:
|
2004-03-10 18:42:40 +01:00
|
|
|
/// The only data member. Hidden to user.
|
|
|
|
P2ScalarType _v[2];
|
2004-03-03 16:07:40 +01:00
|
|
|
public:
|
2004-03-10 18:42:40 +01:00
|
|
|
/// the scalar type
|
|
|
|
typedef P2ScalarType ScalarType;
|
|
|
|
|
|
|
|
//@{
|
|
|
|
|
|
|
|
/** @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];}
|
2004-04-05 13:57:32 +02:00
|
|
|
inline const ScalarType * V() const
|
|
|
|
{
|
|
|
|
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];
|
|
|
|
}
|
2004-03-10 18:42:40 +01:00
|
|
|
inline const ScalarType & operator [] ( const int i ) const
|
2004-02-13 01:44:53 +01:00
|
|
|
{
|
|
|
|
assert(i>=0 && i<2);
|
2004-03-03 16:07:40 +01:00
|
|
|
return _v[i];
|
2004-02-13 01:44:53 +01:00
|
|
|
}
|
2004-03-10 18:42:40 +01:00
|
|
|
inline ScalarType & operator [] ( const int i )
|
2004-02-13 01:44:53 +01:00
|
|
|
{
|
|
|
|
assert(i>=0 && i<2);
|
2004-03-03 16:07:40 +01:00
|
|
|
return _v[i];
|
2004-02-13 01:44:53 +01:00
|
|
|
}
|
2004-03-10 18:42:40 +01:00
|
|
|
//@}
|
|
|
|
/// empty constructor (does nothing)
|
2004-02-13 01:44:53 +01:00
|
|
|
inline Point2 () { }
|
2004-03-10 18:42:40 +01:00
|
|
|
/// x,y constructor
|
|
|
|
inline Point2 ( const ScalarType nx, const ScalarType ny )
|
2004-02-13 01:44:53 +01:00
|
|
|
{
|
2004-03-03 16:07:40 +01:00
|
|
|
_v[0] = nx; _v[1] = ny;
|
2004-02-13 01:44:53 +01:00
|
|
|
}
|
2004-03-10 18:42:40 +01:00
|
|
|
/// copy constructor
|
2004-02-13 01:44:53 +01:00
|
|
|
inline Point2 ( Point2 const & p)
|
|
|
|
{
|
2004-03-03 16:07:40 +01:00
|
|
|
_v[0]= p._v[0]; _v[1]= p._v[1];
|
2004-02-13 01:44:53 +01:00
|
|
|
}
|
2004-03-10 18:42:40 +01:00
|
|
|
/// copy
|
2004-02-13 01:44:53 +01:00
|
|
|
inline Point2 & operator =( Point2 const & p)
|
|
|
|
{
|
2004-03-03 16:07:40 +01:00
|
|
|
_v[0]= p._v[0]; _v[1]= p._v[1];
|
2004-02-13 01:44:53 +01:00
|
|
|
return *this;
|
|
|
|
}
|
2004-03-10 18:42:40 +01:00
|
|
|
/// sets the point to (0,0)
|
2004-02-13 01:44:53 +01:00
|
|
|
inline void Zero()
|
2004-03-10 18:42:40 +01:00
|
|
|
{ _v[0] = 0;_v[1] = 0;}
|
|
|
|
/// dot product
|
|
|
|
inline ScalarType operator * ( Point2 const & p ) const
|
2004-02-13 01:44:53 +01:00
|
|
|
{
|
2004-03-10 18:42:40 +01:00
|
|
|
return ( _v[0]*p._v[0] + _v[1]*p._v[1] );
|
2004-02-13 01:44:53 +01:00
|
|
|
}
|
2004-03-10 18:42:40 +01:00
|
|
|
/// cross product
|
|
|
|
inline ScalarType operator ^ ( Point2 const & p ) const
|
|
|
|
{
|
|
|
|
return _v[0]*p._v[1] - _v[1]*p._v[0];
|
|
|
|
}
|
|
|
|
//@{
|
|
|
|
/** @name Linearity for 2d points (operators +, -, *, /, *= ...) **/
|
2004-02-13 01:44:53 +01:00
|
|
|
inline Point2 operator + ( Point2 const & p) const
|
|
|
|
{
|
2004-03-10 18:42:40 +01:00
|
|
|
return Point2<ScalarType>( _v[0]+p._v[0], _v[1]+p._v[1] );
|
2004-02-13 01:44:53 +01:00
|
|
|
}
|
|
|
|
inline Point2 operator - ( Point2 const & p) const
|
|
|
|
{
|
2004-03-10 18:42:40 +01:00
|
|
|
return Point2<ScalarType>( _v[0]-p._v[0], _v[1]-p._v[1] );
|
2004-02-13 01:44:53 +01:00
|
|
|
}
|
2004-03-10 18:42:40 +01:00
|
|
|
inline Point2 operator * ( const ScalarType s ) const
|
2004-02-13 01:44:53 +01:00
|
|
|
{
|
2004-03-10 18:42:40 +01:00
|
|
|
return Point2<ScalarType>( _v[0] * s, _v[1] * s );
|
2004-02-13 01:44:53 +01:00
|
|
|
}
|
2004-03-10 18:42:40 +01:00
|
|
|
inline Point2 operator / ( const ScalarType s ) const
|
2004-02-13 01:44:53 +01:00
|
|
|
{
|
2004-03-10 18:42:40 +01:00
|
|
|
return Point2<ScalarType>( _v[0] / s, _v[1] / s );
|
2004-02-13 01:44:53 +01:00
|
|
|
}
|
|
|
|
inline Point2 & operator += ( Point2 const & p)
|
|
|
|
{
|
2004-03-03 16:07:40 +01:00
|
|
|
_v[0] += p._v[0]; _v[1] += p._v[1];
|
2004-02-13 01:44:53 +01:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
inline Point2 & operator -= ( Point2 const & p)
|
|
|
|
{
|
2004-03-03 16:07:40 +01:00
|
|
|
_v[0] -= p._v[0]; _v[1] -= p._v[1];
|
2004-02-13 01:44:53 +01:00
|
|
|
return *this;
|
|
|
|
}
|
2004-03-10 18:42:40 +01:00
|
|
|
inline Point2 & operator *= ( const ScalarType s )
|
2004-02-13 01:44:53 +01:00
|
|
|
{
|
2004-03-03 16:07:40 +01:00
|
|
|
_v[0] *= s; _v[1] *= s;
|
2004-02-13 01:44:53 +01:00
|
|
|
return *this;
|
|
|
|
}
|
2004-03-10 18:42:40 +01:00
|
|
|
inline Point2 & operator /= ( const ScalarType s )
|
2004-02-13 01:44:53 +01:00
|
|
|
{
|
2004-03-03 16:07:40 +01:00
|
|
|
_v[0] /= s; _v[1] /= s;
|
2004-02-13 01:44:53 +01:00
|
|
|
return *this;
|
|
|
|
}
|
2004-03-10 18:42:40 +01:00
|
|
|
//@}
|
|
|
|
/// returns the norm (Euclidian)
|
|
|
|
inline ScalarType Norm( void ) const
|
2004-02-13 01:44:53 +01:00
|
|
|
{
|
2004-03-10 18:42:40 +01:00
|
|
|
return math::Sqrt( _v[0]*_v[0] + _v[1]*_v[1] );
|
2004-02-13 01:44:53 +01:00
|
|
|
}
|
2004-03-10 18:42:40 +01:00
|
|
|
/// returns the squared norm (Euclidian)
|
|
|
|
inline ScalarType SquaredNorm( void ) const
|
2004-02-13 01:44:53 +01:00
|
|
|
{
|
2004-03-03 16:07:40 +01:00
|
|
|
return ( _v[0]*_v[0] + _v[1]*_v[1] );
|
2004-02-13 01:44:53 +01:00
|
|
|
}
|
2004-03-10 18:42:40 +01:00
|
|
|
inline Point2 & Scale( const ScalarType sx, const ScalarType sy );
|
|
|
|
/// normalizes, and returns itself as result
|
2004-02-13 01:44:53 +01:00
|
|
|
inline Point2 & Normalize( void )
|
|
|
|
{
|
2004-03-10 18:42:40 +01:00
|
|
|
ScalarType n = math::Sqrt(_v[0]*_v[0] + _v[1]*_v[1]);
|
2004-03-03 16:07:40 +01:00
|
|
|
if(n>0.0) { _v[0] /= n; _v[1] /= n; }
|
2004-02-13 01:44:53 +01:00
|
|
|
return *this;
|
|
|
|
}
|
2004-03-10 18:42:40 +01:00
|
|
|
/// points equality
|
2004-02-13 01:44:53 +01:00
|
|
|
inline bool operator == ( Point2 const & p ) const
|
|
|
|
{
|
2004-03-03 16:07:40 +01:00
|
|
|
return (_v[0]==p._v[0] && _v[1]==p._v[1]);
|
2004-02-13 01:44:53 +01:00
|
|
|
}
|
2004-03-10 18:42:40 +01:00
|
|
|
/// disparity between points
|
2004-02-13 01:44:53 +01:00
|
|
|
inline bool operator != ( Point2 const & p ) const
|
|
|
|
{
|
2004-03-03 16:07:40 +01:00
|
|
|
return ( (_v[0]!=p._v[0]) || (_v[1]!=p._v[1]) );
|
2004-02-13 01:44:53 +01:00
|
|
|
}
|
2004-03-10 18:42:40 +01:00
|
|
|
/// lexical ordering
|
2004-02-13 01:44:53 +01:00
|
|
|
inline bool operator < ( Point2 const & p ) const
|
|
|
|
{
|
2004-03-03 16:07:40 +01:00
|
|
|
return (_v[1]!=p._v[1])?(_v[1]<p._v[1]):
|
|
|
|
(_v[0]<p._v[0]);
|
2004-02-13 01:44:53 +01:00
|
|
|
}
|
2004-03-10 18:42:40 +01:00
|
|
|
/// lexical ordering
|
2004-02-13 01:44:53 +01:00
|
|
|
inline bool operator > ( Point2 const & p ) const
|
|
|
|
{
|
2004-03-03 16:07:40 +01:00
|
|
|
return (_v[1]!=p._v[1])?(_v[1]>p._v[1]):
|
|
|
|
(_v[0]>p._v[0]);
|
2004-02-13 01:44:53 +01:00
|
|
|
}
|
2004-03-10 18:42:40 +01:00
|
|
|
/// lexical ordering
|
2004-02-13 01:44:53 +01:00
|
|
|
inline bool operator <= ( Point2 const & p ) const
|
|
|
|
{
|
2004-03-03 16:07:40 +01:00
|
|
|
return (_v[1]!=p._v[1])?(_v[1]< p._v[1]):
|
|
|
|
(_v[0]<=p._v[0]);
|
2004-02-13 01:44:53 +01:00
|
|
|
}
|
2004-03-10 18:42:40 +01:00
|
|
|
/// lexical ordering
|
2004-02-13 01:44:53 +01:00
|
|
|
inline bool operator >= ( Point2 const & p ) const
|
|
|
|
{
|
2004-03-03 16:07:40 +01:00
|
|
|
return (_v[1]!=p._v[1])?(_v[1]> p._v[1]):
|
|
|
|
(_v[0]>=p._v[0]);
|
2004-02-13 01:44:53 +01:00
|
|
|
}
|
2004-03-10 18:42:40 +01:00
|
|
|
/// returns the distance to another point p
|
|
|
|
inline ScalarType Distance( Point2 const & p ) const
|
2004-02-13 01:44:53 +01:00
|
|
|
{
|
|
|
|
return Norm(*this-p);
|
|
|
|
}
|
2004-03-10 18:42:40 +01:00
|
|
|
/// returns the suqared distance to another point p
|
|
|
|
inline ScalarType SquaredDistance( Point2 const & p ) const
|
2004-02-13 01:44:53 +01:00
|
|
|
{
|
|
|
|
return Norm2(*this-p);
|
|
|
|
}
|
2004-03-10 18:42:40 +01:00
|
|
|
/// returns the angle with X axis (radiants, in [-PI, +PI] )
|
|
|
|
inline Point2 &Angle(){
|
2004-05-10 15:22:25 +02:00
|
|
|
return math::Atan2(_v[1],_v[0]);
|
|
|
|
}
|
2004-03-10 18:42:40 +01:00
|
|
|
/// transform the point in cartesian coords into polar coords
|
2004-02-13 01:44:53 +01:00
|
|
|
inline Point2 & Cartesian2Polar()
|
|
|
|
{
|
2004-03-10 18:42:40 +01:00
|
|
|
ScalarType t = Angle();
|
|
|
|
_v[0] = Norm();
|
2004-03-03 16:07:40 +01:00
|
|
|
_v[1] = t;
|
2004-02-13 01:44:53 +01:00
|
|
|
return *this;
|
|
|
|
}
|
2004-03-10 18:42:40 +01:00
|
|
|
/// transform the point in polar coords into cartesian coords
|
2004-02-13 01:44:53 +01:00
|
|
|
inline Point2 & Polar2Cartesian()
|
|
|
|
{
|
2004-03-10 18:42:40 +01:00
|
|
|
ScalarType l = _v[0];
|
|
|
|
_v[0] = (ScalarType)(l*math::Cos(_v[1]));
|
|
|
|
_v[1] = (ScalarType)(l*math::Sin(_v[1]));
|
2004-02-13 01:44:53 +01:00
|
|
|
return *this;
|
|
|
|
}
|
2004-03-10 18:42:40 +01:00
|
|
|
/// rotates the point of an angle (radiants, counterclockwise)
|
|
|
|
inline Point2 & Rotate( const ScalarType rad )
|
2004-02-13 01:44:53 +01:00
|
|
|
{
|
2004-03-10 18:42:40 +01:00
|
|
|
ScalarType t = _v[0];
|
|
|
|
ScalarType s = math::Sin(rad);
|
|
|
|
ScalarType c = math::Cos(rad);
|
2004-02-13 01:44:53 +01:00
|
|
|
|
2004-03-03 16:07:40 +01:00
|
|
|
_v[0] = _v[0]*c - _v[1]*s;
|
|
|
|
_v[1] = t *s + _v[1]*c;
|
2004-02-13 01:44:53 +01:00
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Questa funzione estende il vettore ad un qualsiasi numero di dimensioni
|
|
|
|
/// paddando gli elementi estesi con zeri
|
2004-03-10 18:42:40 +01:00
|
|
|
inline ScalarType Ext( const int i ) const
|
2004-02-13 01:44:53 +01:00
|
|
|
{
|
2004-03-03 16:07:40 +01:00
|
|
|
if(i>=0 && i<2) return _v[i];
|
2004-02-13 01:44:53 +01:00
|
|
|
else return 0;
|
|
|
|
}
|
2004-03-10 18:42:40 +01:00
|
|
|
/// imports from 2D points of different types
|
|
|
|
template <class T>
|
|
|
|
inline void Import( const Point2<T> & b )
|
|
|
|
{
|
|
|
|
_v[0] = p.X(); _v[1] = p.Y();
|
|
|
|
}
|
|
|
|
/// constructs a 2D points from an existing one of different type
|
|
|
|
template <class T>
|
|
|
|
static Point2 Construct( const Point2<T> & b )
|
|
|
|
{
|
|
|
|
return Point2(b.X(),b.Y());
|
|
|
|
}
|
2004-02-13 01:44:53 +01:00
|
|
|
|
|
|
|
|
|
|
|
}; // end class definition
|
|
|
|
|
|
|
|
|
2004-03-10 18:42:40 +01:00
|
|
|
template <class T>
|
|
|
|
inline T Angle( Point2<T> const & p1, Point2<T> const & p2 )
|
2004-02-13 01:44:53 +01:00
|
|
|
{
|
2004-03-10 18:42:40 +01:00
|
|
|
return p1.Angle() - p0.Angle();
|
2004-02-13 01:44:53 +01:00
|
|
|
}
|
|
|
|
|
2004-03-10 18:42:40 +01:00
|
|
|
template <class T>
|
|
|
|
inline Point2<T> operator - ( Point2<T> const & p ){
|
|
|
|
return Point2<T>( -p._v[0], -p._v[1] );
|
2004-02-13 01:44:53 +01:00
|
|
|
}
|
|
|
|
|
2004-03-10 18:42:40 +01:00
|
|
|
template <class T>
|
|
|
|
inline Point2<T> operator * ( const T s, Point2<T> const & p ){
|
|
|
|
return Point2<T>( p._v[0] * s, p._v[1] * s );
|
2004-02-13 01:44:53 +01:00
|
|
|
}
|
|
|
|
|
2004-03-10 18:42:40 +01:00
|
|
|
template <class T>
|
|
|
|
inline T Norm( Point2<T> const & p ){
|
|
|
|
return p.Norm();
|
2004-02-13 01:44:53 +01:00
|
|
|
}
|
|
|
|
|
2004-03-10 18:42:40 +01:00
|
|
|
template <class T>
|
|
|
|
inline T SquaredNorm( Point2<T> const & p ){
|
|
|
|
return p.SquaredNorm();
|
2004-02-13 01:44:53 +01:00
|
|
|
}
|
|
|
|
|
2004-03-10 18:42:40 +01:00
|
|
|
template <class T>
|
|
|
|
inline Point2<T> & Normalize( Point2<T> & p ){
|
|
|
|
return p.Normalize();
|
2004-02-13 01:44:53 +01:00
|
|
|
}
|
|
|
|
|
2004-03-10 18:42:40 +01:00
|
|
|
template <class T>
|
|
|
|
inline T Distance( Point2<T> const & p1,Point2<T> const & p2 ){
|
2004-02-13 01:44:53 +01:00
|
|
|
return Norm(p1-p2);
|
|
|
|
}
|
|
|
|
|
2004-03-10 18:42:40 +01:00
|
|
|
template <class T>
|
|
|
|
inline T SquaredDistance( Point2<T> const & p1,Point2<T> const & p2 ){
|
2004-02-13 01:44:53 +01:00
|
|
|
return Norm2(p1-p2);
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef Point2<short> Point2s;
|
|
|
|
typedef Point2<int> Point2i;
|
|
|
|
typedef Point2<float> Point2f;
|
|
|
|
typedef Point2<double> Point2d;
|
|
|
|
|
2004-03-10 18:42:40 +01:00
|
|
|
/*@}*/
|
2004-02-13 01:44:53 +01:00
|
|
|
} // end namespace
|
|
|
|
#endif
|