vcglib/vcg/space/point2.h

281 lines
7.2 KiB
C++

/****************************************************************************
* 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 $
Revision 1.1 2004/02/13 00:44:53 cignoni
First commit...
****************************************************************************/
#ifndef __VCGLIB_POINT2
#define __VCGLIB_POINT2
//#include <limits>
#include <assert.h>
#include <vcg/math/base.h>
namespace vcg {
template <class FLTYPE> class Point2
{
protected:
FLTYPE _v[2];
public:
typedef FLTYPE scalar;
inline const FLTYPE &X() const {return _v[0];}
inline const FLTYPE &Y() const {return _v[1];}
inline FLTYPE &X() {return _v[0];}
inline FLTYPE &Y() {return _v[1];}
inline const FLTYPE & operator [] ( const int i ) const
{
assert(i>=0 && i<2);
return _v[i];
}
inline FLTYPE & operator [] ( const int i )
{
assert(i>=0 && i<2);
return _v[i];
}
inline Point2 () { }
inline Point2 ( const FLTYPE nx, const FLTYPE ny )
{
_v[0] = nx; _v[1] = ny;
}
inline Point2 ( Point2 const & p)
{
_v[0]= p._v[0]; _v[1]= p._v[1];
}
inline Point2 & operator =( Point2 const & p)
{
_v[0]= p._v[0]; _v[1]= p._v[1];
return *this;
}
inline void Zero()
{
_v[0] = 0;
_v[1] = 0;
}
inline Point2 operator + ( Point2 const & p) const
{
return Point2<FLTYPE>( _v[0]+p._v[0], _v[1]+p._v[1] );
}
inline Point2 operator - ( Point2 const & p) const
{
return Point2<FLTYPE>( _v[0]-p._v[0], _v[1]-p._v[1] );
}
inline Point2 operator * ( const FLTYPE s ) const
{
return Point2<FLTYPE>( _v[0] * s, _v[1] * s );
}
inline Point2 operator / ( const FLTYPE s ) const
{
return Point2<FLTYPE>( _v[0] / s, _v[1] / s );
}
inline FLTYPE operator * ( Point2 const & p ) const
{
return ( _v[0]*p._v[0] + _v[1]*p._v[1] );
}
inline FLTYPE operator ^ ( Point2 const & p ) const
{
return _v[1]*p._v[0] - _v[0]*p._v[1];
}
inline Point2 & operator += ( Point2 const & p)
{
_v[0] += p._v[0]; _v[1] += p._v[1];
return *this;
}
inline Point2 & operator -= ( Point2 const & p)
{
_v[0] -= p._v[0]; _v[1] -= p._v[1];
return *this;
}
inline Point2 & operator *= ( const FLTYPE s )
{
_v[0] *= s; _v[1] *= s;
return *this;
}
inline Point2 & operator /= ( const FLTYPE s )
{
_v[0] /= s; _v[1] /= s;
return *this;
}
inline FLTYPE Norm( void ) const
{
return Sqrt( _v[0]*_v[0] + _v[1]*_v[1] );
}
inline FLTYPE SquaredNorm( void ) const
{
return ( _v[0]*_v[0] + _v[1]*_v[1] );
}
inline Point2 & Scale( const FLTYPE sx, const FLTYPE sy );
inline Point2 & Normalize( void )
{
FLTYPE n = Sqrt(_v[0]*_v[0] + _v[1]*_v[1]);
if(n>0.0) { _v[0] /= n; _v[1] /= n; }
return *this;
}
inline bool operator == ( Point2 const & p ) const
{
return (_v[0]==p._v[0] && _v[1]==p._v[1]);
}
inline bool operator != ( Point2 const & p ) const
{
return ( (_v[0]!=p._v[0]) || (_v[1]!=p._v[1]) );
}
inline bool operator < ( Point2 const & p ) const
{
return (_v[1]!=p._v[1])?(_v[1]<p._v[1]):
(_v[0]<p._v[0]);
}
inline bool operator > ( Point2 const & p ) const
{
return (_v[1]!=p._v[1])?(_v[1]>p._v[1]):
(_v[0]>p._v[0]);
}
inline bool operator <= ( Point2 const & p ) const
{
return (_v[1]!=p._v[1])?(_v[1]< p._v[1]):
(_v[0]<=p._v[0]);
}
inline bool operator >= ( Point2 const & p ) const
{
return (_v[1]!=p._v[1])?(_v[1]> p._v[1]):
(_v[0]>=p._v[0]);
}
inline FLTYPE Distance( Point2 const & p ) const
{
return Norm(*this-p);
}
inline FLTYPE SquaredDistance( Point2 const & p ) const
{
return Norm2(*this-p);
}
inline Point2 & Cartesian2Polar()
{
FLTYPE t = (FLTYPE)atan2(_v[1],_v[0]);
_v[0] = Sqrt(_v[0]*_v[0]+_v[1]*_v[1]);
_v[1] = t;
return *this;
}
inline Point2 & Polar2Cartesian()
{
FLTYPE l = _v[0];
_v[0] = (FLTYPE)(l*cos(_v[1]));
_v[1] = (FLTYPE)(l*sin(_v[1]));
return *this;
}
inline Point2 & rotate( const FLTYPE a )
{
FLTYPE t = _v[0];
FLTYPE s = sin(a);
FLTYPE c = cos(a);
_v[0] = _v[0]*c - _v[1]*s;
_v[1] = t *s + _v[1]*c;
return *this;
}
/// Questa funzione estende il vettore ad un qualsiasi numero di dimensioni
/// paddando gli elementi estesi con zeri
inline FLTYPE Ext( const int i ) const
{
if(i>=0 && i<2) return _v[i];
else return 0;
}
}; // end class definition
template <class FLTYPE>
inline FLTYPE Angle( Point2<FLTYPE> const & p1, Point2<FLTYPE> const & p2 )
{
return atan2(p2[1],p2[0]) - atan2(p1[1],p1[0]);
}
template <class FLTYPE>
inline Point2<FLTYPE> operator - ( Point2<FLTYPE> const & p ){
return Point2<FLTYPE>( -p._v[0], -p._v[1] );
}
template <class FLTYPE>
inline Point2<FLTYPE> operator * ( const FLTYPE s, Point2<FLTYPE> const & p ){
return Point2<FLTYPE>( p._v[0] * s, p._v[1] * s );
}
template <class FLTYPE>
inline FLTYPE Norm( Point2<FLTYPE> const & p ){
return Sqrt( p._v[0]*p._v[0] + p._v[1]*p._v[1] );
}
template <class FLTYPE>
inline FLTYPE Norm2( Point2<FLTYPE> const & p ){
return ( p._v[0]*p._v[0] + p._v[1]*p._v[1] );
}
template <class FLTYPE>
inline Point2<FLTYPE> & Normalize( Point2<FLTYPE> & p ){
FLTYPE n = Sqrt( p._v[0]*p._v[0] + p._v[1]*p._v[1] );
if(n>0.0) p/=n;
return p;
}
template <class FLTYPE>
inline FLTYPE Distance( Point2<FLTYPE> const & p1,Point2<FLTYPE> const & p2 ){
return Norm(p1-p2);
}
template <class FLTYPE>
inline FLTYPE SquaredDistance( Point2<FLTYPE> const & p1,Point2<FLTYPE> const & p2 ){
return Norm2(p1-p2);
}
typedef Point2<short> Point2s;
typedef Point2<int> Point2i;
typedef Point2<float> Point2f;
typedef Point2<double> Point2d;
} // end namespace
#endif