added import function to TexCoord2 and fixed inconsistencies with Point2

This commit is contained in:
Luigi Malomo 2019-12-10 17:01:20 +01:00
parent cfbece0e98
commit 7f5ebbd2c5
2 changed files with 193 additions and 167 deletions

View File

@ -2,7 +2,7 @@
* VCGLib o o * * VCGLib o o *
* Visual and Computer Graphics Library o o * * Visual and Computer Graphics Library o o *
* _ O _ * * _ O _ *
* Copyright(C) 2004-2016 \/)\/ * * Copyright(C) 2004-2019 \/)\/ *
* Visual Computing Lab /\/| * * Visual Computing Lab /\/| *
* ISTI - Italian National Research Council | * * ISTI - Italian National Research Council | *
* \ * * \ *
@ -70,7 +70,8 @@ namespace vcg {
The class is templated over the ScalarType class that is used to represent coordinates. The class is templated over the ScalarType class that is used to represent coordinates.
All the usual operator overloading (* + - ...) is present. All the usual operator overloading (* + - ...) is present.
*/ */
template <class P2ScalarType> class Point2 template <class P2ScalarType>
class Point2
{ {
protected: protected:
/// The only data member. Hidden to user. /// The only data member. Hidden to user.
@ -126,12 +127,12 @@ public:
_v[0] = nx; _v[1] = ny; _v[0] = nx; _v[1] = ny;
} }
/// copy constructor /// copy constructor
inline Point2 ( Point2 const & p) inline Point2 ( const Point2 & p)
{ {
_v[0]= p._v[0]; _v[1]= p._v[1]; _v[0]= p._v[0]; _v[1]= p._v[1];
} }
/// copy /// copy
inline Point2 & operator =( Point2 const & p) inline Point2 & operator =( const Point2 & p)
{ {
_v[0]= p._v[0]; _v[1]= p._v[1]; _v[0]= p._v[0]; _v[1]= p._v[1];
return *this; return *this;
@ -140,23 +141,23 @@ public:
inline void SetZero() inline void SetZero()
{ _v[0] = 0;_v[1] = 0;} { _v[0] = 0;_v[1] = 0;}
/// dot product /// dot product
inline ScalarType operator * ( Point2 const & p ) const inline ScalarType operator * ( const Point2 & p ) const
{ {
return ( _v[0]*p._v[0] + _v[1]*p._v[1] ); return ( _v[0]*p._v[0] + _v[1]*p._v[1] );
} }
inline ScalarType dot( const Point2 & p ) const { return (*this) * p; } inline ScalarType dot( const Point2 & p ) const { return (*this) * p; }
/// cross product /// cross product
inline ScalarType operator ^ ( Point2 const & p ) const inline ScalarType operator ^ ( const Point2 & p ) const
{ {
return _v[0]*p._v[1] - _v[1]*p._v[0]; return _v[0]*p._v[1] - _v[1]*p._v[0];
} }
//@{ //@{
/** @name Linearity for 2d points (operators +, -, *, /, *= ...) **/ /** @name Linearity for 2d points (operators +, -, *, /, *= ...) **/
inline Point2 operator + ( Point2 const & p) const inline Point2 operator + ( const Point2 & p) const
{ {
return Point2<ScalarType>( _v[0]+p._v[0], _v[1]+p._v[1] ); return Point2<ScalarType>( _v[0]+p._v[0], _v[1]+p._v[1] );
} }
inline Point2 operator - ( Point2 const & p) const inline Point2 operator - ( const Point2 & p) const
{ {
return Point2<ScalarType>( _v[0]-p._v[0], _v[1]-p._v[1] ); return Point2<ScalarType>( _v[0]-p._v[0], _v[1]-p._v[1] );
} }
@ -168,24 +169,28 @@ public:
{ {
return Point2<ScalarType>( _v[0] / s, _v[1] / s ); return Point2<ScalarType>( _v[0] / s, _v[1] / s );
} }
inline Point2 & operator += ( Point2 const & p) inline Point2 & operator += ( const Point2 & p)
{ {
_v[0] += p._v[0]; _v[1] += p._v[1]; _v[0] += p._v[0];
_v[1] += p._v[1];
return *this; return *this;
} }
inline Point2 & operator -= ( Point2 const & p) inline Point2 & operator -= ( const Point2 & p)
{ {
_v[0] -= p._v[0]; _v[1] -= p._v[1]; _v[0] -= p._v[0];
_v[1] -= p._v[1];
return *this; return *this;
} }
inline Point2 & operator *= ( const ScalarType s ) inline Point2 & operator *= ( const ScalarType s )
{ {
_v[0] *= s; _v[1] *= s; _v[0] *= s;
_v[1] *= s;
return *this; return *this;
} }
inline Point2 & operator /= ( const ScalarType s ) inline Point2 & operator /= ( const ScalarType s )
{ {
_v[0] /= s; _v[1] /= s; _v[0] /= s;
_v[1] /= s;
return *this; return *this;
} }
//@} //@}
@ -209,55 +214,58 @@ public:
inline Point2 & Normalize( void ) inline Point2 & Normalize( void )
{ {
ScalarType n = math::Sqrt(_v[0]*_v[0] + _v[1]*_v[1]); ScalarType n = math::Sqrt(_v[0]*_v[0] + _v[1]*_v[1]);
if(n>0.0) { _v[0] /= n; _v[1] /= n; } if(n>0.0) {
_v[0] /= n; _v[1] /= n;
}
return *this; return *this;
} }
/// points equality /// points equality
inline bool operator == ( Point2 const & p ) const inline bool operator == ( const Point2 & p ) const
{ {
return (_v[0]==p._v[0] && _v[1]==p._v[1]); return (_v[0]==p._v[0] && _v[1]==p._v[1]);
} }
/// disparity between points /// disparity between points
inline bool operator != ( Point2 const & p ) const inline bool operator != ( const Point2 & p ) const
{ {
return ( (_v[0]!=p._v[0]) || (_v[1]!=p._v[1]) ); return ( (_v[0]!=p._v[0]) || (_v[1]!=p._v[1]) );
} }
/// lexical ordering /// lexical ordering
inline bool operator < ( Point2 const & p ) const inline bool operator < ( const Point2 & p ) const
{ {
return (_v[1]!=p._v[1])?(_v[1]<p._v[1]): return (_v[1]!=p._v[1])?(_v[1]<p._v[1]):
(_v[0]<p._v[0]); (_v[0]<p._v[0]);
} }
/// lexical ordering /// lexical ordering
inline bool operator > ( Point2 const & p ) const inline bool operator > ( const Point2 & p ) const
{ {
return (_v[1]!=p._v[1])?(_v[1]>p._v[1]): return (_v[1]!=p._v[1])?(_v[1]>p._v[1]):
(_v[0]>p._v[0]); (_v[0]>p._v[0]);
} }
/// lexical ordering /// lexical ordering
inline bool operator <= ( Point2 const & p ) const inline bool operator <= ( const Point2 & p ) const
{ {
return (_v[1]!=p._v[1])?(_v[1]< p._v[1]): return (_v[1]!=p._v[1])?(_v[1]< p._v[1]):
(_v[0]<=p._v[0]); (_v[0]<=p._v[0]);
} }
/// lexical ordering /// lexical ordering
inline bool operator >= ( Point2 const & p ) const inline bool operator >= ( const Point2 & p ) const
{ {
return (_v[1]!=p._v[1])?(_v[1]> p._v[1]): return (_v[1]!=p._v[1])?(_v[1]> p._v[1]):
(_v[0]>=p._v[0]); (_v[0]>=p._v[0]);
} }
/// returns the distance to another point p /// returns the distance to another point p
inline ScalarType Distance( Point2 const & p ) const inline ScalarType Distance( const Point2 & p ) const
{ {
return Norm(*this-p); return Norm(*this-p);
} }
/// returns the suqared distance to another point p /// returns the suqared distance to another point p
inline ScalarType SquaredDistance( Point2 const & p ) const inline ScalarType SquaredDistance( const Point2 & p ) const
{ {
return (*this-p).SquaredNorm(); return (*this-p).SquaredNorm();
} }
/// returns the angle with X axis (radiants, in [-PI, +PI] ) /// returns the angle with X axis (radiants, in [-PI, +PI] )
inline ScalarType Angle() const { inline ScalarType Angle() const
{
return math::Atan2(_v[1],_v[0]); return math::Atan2(_v[1],_v[0]);
} }
/// transform the point in cartesian coords into polar coords /// transform the point in cartesian coords into polar coords
@ -289,18 +297,21 @@ public:
return *this; return *this;
} }
/// Questa funzione estende il vettore ad un qualsiasi numero di dimensioni /// This function extends the vector to any arbitrary domension
/// paddando gli elementi estesi con zeri /// virtually padding missing elements with zeros
inline ScalarType Ext( const int i ) const inline ScalarType Ext( const int i ) const
{ {
if(i>=0 && i<2) return _v[i]; if(i>=0 && i<2)
else return 0; return _v[i];
else
return 0;
} }
/// imports from 2D points of different types /// imports from 2D points of different types
template <class T> template <class T>
inline void Import( const Point2<T> & b ) inline void Import( const Point2<T> & b )
{ {
_v[0] = b.X(); _v[1] = b.Y(); _v[0] = ScalarType(b.X());
_v[1] = ScalarType(b.Y());
} }
template <class EigenVector> template <class EigenVector>
inline void FromEigenVector(const EigenVector & b) inline void FromEigenVector(const EigenVector & b)
@ -318,7 +329,16 @@ public:
template <class T> template <class T>
static Point2 Construct( const Point2<T> & b ) static Point2 Construct( const Point2<T> & b )
{ {
return Point2(b.X(),b.Y()); 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) static inline Point2 Zero(void)
@ -330,8 +350,6 @@ public:
{ {
return Point2(1,1); return Point2(1,1);
} }
}; // end class definition }; // end class definition
@ -376,9 +394,9 @@ inline T SquaredDistance( Point2<T> const & p1,Point2<T> const & p2 ){
return SquaredNorm(p1-p2); return SquaredNorm(p1-p2);
} }
template <class SCALARTYPE> template <class T>
inline Point2<SCALARTYPE> Abs(const Point2<SCALARTYPE> & p) { inline Point2<T> Abs(const Point2<T> & p) {
return (Point2<SCALARTYPE>(math::Abs(p[0]), math::Abs(p[1]))); return (Point2<T>(math::Abs(p[0]), math::Abs(p[1])));
} }
typedef Point2<short> Point2s; typedef Point2<short> Point2s;

View File

@ -2,7 +2,7 @@
* VCGLib o o * * VCGLib o o *
* Visual and Computer Graphics Library o o * * Visual and Computer Graphics Library o o *
* _ O _ * * _ O _ *
* Copyright(C) 2004-2016 \/)\/ * * Copyright(C) 2004-2019 \/)\/ *
* Visual Computing Lab /\/| * * Visual Computing Lab /\/| *
* ISTI - Italian National Research Council | * * ISTI - Italian National Research Council | *
* \ * * \ *
@ -50,13 +50,12 @@ public:
typedef Point2<T> PointType; typedef Point2<T> PointType;
typedef T ScalarType; typedef T ScalarType;
protected:
private:
PointType _t[NMAX]; PointType _t[NMAX];
short _n[NMAX]; short _n[NMAX];
public: public:
TexCoord2(T u, T v) { if(NMAX>0) _n[0]=0; _t[0][0]=u; _t[0][1]=v; } TexCoord2(T u, T v){ _n[0]=0; _t[0][0]=u; _t[0][1]=v; }
TexCoord2() { } TexCoord2() { }
inline const PointType &P() const { return _t[0]; } inline const PointType &P() const { return _t[0]; }
@ -86,6 +85,15 @@ public:
inline short & N(int i) { assert(i>0 && i<NMAX); return _n[i]; } inline short & N(int i) { assert(i>0 && i<NMAX); return _n[i]; }
inline short N(int i) const { assert(i>0 && i<NMAX); return _n[i]; } inline short N(int i) const { assert(i>0 && i<NMAX); return _n[i]; }
template <class S>
inline void Import( const TexCoord2<S> & tc )
{
for (int i=0; i<NMAX; i++)
{
_t[i].Import(tc.P(i));
_n[i] = tc.N(i);
}
}
/* <OLD_METHODS> (lowercase ones). DEPRECATED. TO BE REMOVED SOON.*/ /* <OLD_METHODS> (lowercase ones). DEPRECATED. TO BE REMOVED SOON.*/
/**/inline T & u() { return _t[0][0]; } /**/inline T & u() { return _t[0][0]; }
@ -209,10 +217,10 @@ public:
enum { n_coords=1}; enum { n_coords=1};
}; };
typedef TexCoord2<float> TexCoord2f; typedef TexCoord2<float> TexCoord2f;
typedef TexCoord2<double> TexCoord2d; typedef TexCoord2<double> TexCoord2d;
/*@}*/ /*@}*/
} }