added import function to TexCoord2 and fixed inconsistencies with Point2
This commit is contained in:
parent
cfbece0e98
commit
7f5ebbd2c5
|
@ -2,7 +2,7 @@
|
|||
* VCGLib o o *
|
||||
* Visual and Computer Graphics Library o o *
|
||||
* _ O _ *
|
||||
* Copyright(C) 2004-2016 \/)\/ *
|
||||
* Copyright(C) 2004-2019 \/)\/ *
|
||||
* Visual Computing Lab /\/| *
|
||||
* ISTI - Italian National Research Council | *
|
||||
* \ *
|
||||
|
@ -65,15 +65,16 @@ 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
|
||||
template <class P2ScalarType>
|
||||
class Point2
|
||||
{
|
||||
protected:
|
||||
/// The only data member. Hidden to user.
|
||||
/// The only data member. Hidden to user.
|
||||
P2ScalarType _v[2];
|
||||
public:
|
||||
/// the scalar type
|
||||
|
@ -126,12 +127,12 @@ public:
|
|||
_v[0] = nx; _v[1] = ny;
|
||||
}
|
||||
/// copy constructor
|
||||
inline Point2 ( Point2 const & p)
|
||||
inline Point2 ( const Point2 & p)
|
||||
{
|
||||
_v[0]= p._v[0]; _v[1]= p._v[1];
|
||||
}
|
||||
/// copy
|
||||
inline Point2 & operator =( Point2 const & p)
|
||||
inline Point2 & operator =( const Point2 & p)
|
||||
{
|
||||
_v[0]= p._v[0]; _v[1]= p._v[1];
|
||||
return *this;
|
||||
|
@ -140,23 +141,23 @@ public:
|
|||
inline void SetZero()
|
||||
{ _v[0] = 0;_v[1] = 0;}
|
||||
/// 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] );
|
||||
}
|
||||
inline ScalarType dot( const Point2 & p ) const { return (*this) * p; }
|
||||
/// 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];
|
||||
}
|
||||
//@{
|
||||
/** @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] );
|
||||
}
|
||||
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] );
|
||||
}
|
||||
|
@ -168,24 +169,28 @@ public:
|
|||
{
|
||||
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;
|
||||
}
|
||||
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;
|
||||
}
|
||||
inline Point2 & operator *= ( const ScalarType s )
|
||||
{
|
||||
_v[0] *= s; _v[1] *= s;
|
||||
_v[0] *= s;
|
||||
_v[1] *= s;
|
||||
return *this;
|
||||
}
|
||||
inline Point2 & operator /= ( const ScalarType s )
|
||||
{
|
||||
_v[0] /= s; _v[1] /= s;
|
||||
_v[0] /= s;
|
||||
_v[1] /= s;
|
||||
return *this;
|
||||
}
|
||||
//@}
|
||||
|
@ -209,55 +214,58 @@ public:
|
|||
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; }
|
||||
if(n>0.0) {
|
||||
_v[0] /= n; _v[1] /= n;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
/// 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]);
|
||||
}
|
||||
/// 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]) );
|
||||
}
|
||||
/// 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]):
|
||||
(_v[0]<p._v[0]);
|
||||
}
|
||||
/// 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]):
|
||||
(_v[0]>p._v[0]);
|
||||
}
|
||||
/// 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]):
|
||||
(_v[0]<=p._v[0]);
|
||||
}
|
||||
/// 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]):
|
||||
(_v[0]>=p._v[0]);
|
||||
}
|
||||
/// 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);
|
||||
}
|
||||
/// 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();
|
||||
}
|
||||
/// 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]);
|
||||
}
|
||||
/// transform the point in cartesian coords into polar coords
|
||||
|
@ -284,23 +292,26 @@ public:
|
|||
ScalarType c = math::Cos(rad);
|
||||
|
||||
_v[0] = _v[0]*c - _v[1]*s;
|
||||
_v[1] = t *s + _v[1]*c;
|
||||
_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
|
||||
/// 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;
|
||||
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] = b.X(); _v[1] = b.Y();
|
||||
_v[0] = ScalarType(b.X());
|
||||
_v[1] = ScalarType(b.Y());
|
||||
}
|
||||
template <class EigenVector>
|
||||
inline void FromEigenVector(const EigenVector & b)
|
||||
|
@ -318,7 +329,16 @@ public:
|
|||
template <class T>
|
||||
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)
|
||||
|
@ -330,8 +350,6 @@ public:
|
|||
{
|
||||
return Point2(1,1);
|
||||
}
|
||||
|
||||
|
||||
}; // end class definition
|
||||
|
||||
|
||||
|
@ -376,9 +394,9 @@ inline T SquaredDistance( Point2<T> const & p1,Point2<T> const & p2 ){
|
|||
return SquaredNorm(p1-p2);
|
||||
}
|
||||
|
||||
template <class SCALARTYPE>
|
||||
inline Point2<SCALARTYPE> Abs(const Point2<SCALARTYPE> & p) {
|
||||
return (Point2<SCALARTYPE>(math::Abs(p[0]), math::Abs(p[1])));
|
||||
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;
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
* VCGLib o o *
|
||||
* Visual and Computer Graphics Library o o *
|
||||
* _ O _ *
|
||||
* Copyright(C) 2004-2016 \/)\/ *
|
||||
* Copyright(C) 2004-2019 \/)\/ *
|
||||
* Visual Computing Lab /\/| *
|
||||
* ISTI - Italian National Research Council | *
|
||||
* \ *
|
||||
|
@ -39,8 +39,8 @@ namespace vcg {
|
|||
coordinate set for the same entity (e.g. when you have two completely different
|
||||
parametrizations over the same surface);
|
||||
|
||||
This class is intended to be used when multiple textures id are shared over the same surface.
|
||||
so for each coord the id of the texture is stored. If no texture id is needed see the vcg::TexCoord2Simple class.
|
||||
This class is intended to be used when multiple textures id are shared over the same surface.
|
||||
so for each coord the id of the texture is stored. If no texture id is needed see the vcg::TexCoord2Simple class.
|
||||
*/
|
||||
|
||||
template<class T = float, int NMAX = 1>
|
||||
|
@ -50,13 +50,12 @@ public:
|
|||
typedef Point2<T> PointType;
|
||||
typedef T ScalarType;
|
||||
|
||||
|
||||
private:
|
||||
protected:
|
||||
PointType _t[NMAX];
|
||||
short _n[NMAX];
|
||||
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() { }
|
||||
|
||||
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) 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.*/
|
||||
/**/inline T & u() { return _t[0][0]; }
|
||||
|
@ -209,10 +217,10 @@ public:
|
|||
enum { n_coords=1};
|
||||
|
||||
};
|
||||
|
||||
typedef TexCoord2<float> TexCoord2f;
|
||||
typedef TexCoord2<double> TexCoord2d;
|
||||
|
||||
|
||||
/*@}*/
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue