Changed the interface of the interpolating function of a segment (2D and 3D) from the ambiguous P(float) to Lerp(float). Note that with the previous interface it could happen that seg.P(1) != seg.P0() just becouse seg.P(1) was the result of a lerp.

This commit is contained in:
Paolo Cignoni 2013-11-06 16:06:57 +00:00
parent c8ef412821
commit 3bdf666e86
2 changed files with 57 additions and 57 deletions

View File

@ -8,7 +8,7 @@
* \ * * \ *
* All rights reserved. * * All rights reserved. *
* * * *
* This program is free software; you can redistribute it and/or modify * * 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 * * it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or * * the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. * * (at your option) any later version. *
@ -39,15 +39,15 @@ namespace vcg {
/** \addtogroup space */ /** \addtogroup space */
/*@{*/ /*@{*/
/** /**
Templated class for 3D segment. Templated class for 3D segment.
This is the class for a segment in 3D space. A Segment is stored just as its two extrema (Point3). This is the class for a segment in 3D space. A Segment is stored just as its two extrema (Point3).
@param SegmentScalarType (template parameter) Specifies the type of scalar used to represent coords. @param SegmentScalarType (template parameter) Specifies the type of scalar used to represent coords.
*/ */
template <class SegmentScalarType > template <class SegmentScalarType >
class Segment2 class Segment2
{ {
public: public:
/// The scalar type /// The scalar type
typedef SegmentScalarType ScalarType; typedef SegmentScalarType ScalarType;
@ -65,42 +65,42 @@ private:
public: public:
/// Members to access either extrema /// Members to access either extrema
inline const PointType &P0() const { return _p0; } inline const PointType &P0() const { return _p0; }
inline const PointType &P1() const { return _p1; } inline const PointType &P1() const { return _p1; }
inline PointType &P0() { return _p0; } inline PointType &P0() { return _p0; }
inline PointType &P1() { return _p1; } inline PointType &P1() { return _p1; }
/// The empty constructor /// The empty constructor
Segment2() {}; Segment2() {};
/// The (a,b) constructor /// The (a,b) constructor
Segment2(const PointType &a, const PointType &b) { _p0=a; _p1=b; }; Segment2(const PointType &a, const PointType &b) { _p0=a; _p1=b; };
/// Operator to compare segments /// Operator to compare segments
inline bool operator == ( SegmentType const & p ) const inline bool operator == ( SegmentType const & p ) const
{ return _p0==p._p0 && _p1==p._p1; } { return _p0==p._p0 && _p1==p._p1; }
/// Operator to dispare segments /// Operator to dispare segments
inline bool operator != ( SegmentType const & p ) const inline bool operator != ( SegmentType const & p ) const
{ return _p0!=p._p0 || _p1!=p._p1; } { return _p0!=p._p0 || _p1!=p._p1; }
/// initializes the segment with its extrema /// initializes the segment with its extrema
void Set( const PointType &a, const PointType &b) void Set( const PointType &a, const PointType &b)
{ _p0=a; _p1=b;} { _p0=a; _p1=b;}
/// calculates the point of parameter t on the segment. /// calculates the point of parameter t on the segment.
/// if t is in [0..1] returned point is inside the segment /// if t is in [0..1] returned point is inside the segment
inline PointType P( const ScalarType t ) const inline PointType Lerp( const ScalarType t ) const
{ return _p0 + (_p1 - _p0) * t; } { return _p0 + (_p1 - _p0) * t; }
/// return the middle point /// return the middle point
inline PointType MidPoint( ) const inline PointType MidPoint( ) const
{ return ( _p0 + _p1) / ScalarType(2.0) ; } { return ( _p0 + _p1) / ScalarType(2.0) ; }
/// return the bounding box /// return the bounding box
inline Box2<ScalarType> BBox( ) const inline Box2<ScalarType> BBox( ) const
{ {
Box2<ScalarType> t; Box2<ScalarType> t;
t.Add(_p0); t.Add(_p0);
t.Add(_p1); t.Add(_p1);
return t; return t;
} }
/// returns segment length /// returns segment length
ScalarType Length() ScalarType Length()
{ return (_p0 - _p1).Norm(); } { return (_p0 - _p1).Norm(); }
/// returns segment length /// returns segment length
ScalarType Length() const ScalarType Length() const
@ -143,8 +143,8 @@ typedef Segment2<int> Segment2i;
typedef Segment2<float> Segment2f; typedef Segment2<float> Segment2f;
typedef Segment2<double> Segment2d; typedef Segment2<double> Segment2d;
template <class ScalarType> template <class ScalarType>
Point2<ScalarType> ClosestPoint( Segment2<ScalarType> s, const Point2<ScalarType> & p) Point2<ScalarType> ClosestPoint( Segment2<ScalarType> s, const Point2<ScalarType> & p)
{ {
vcg::Line2<ScalarType, true> l; vcg::Line2<ScalarType, true> l;
l.Set(s.P0(),s.P1()-s.P0()); l.Set(s.P0(),s.P1()-s.P0());

View File

@ -8,7 +8,7 @@
* \ * * \ *
* All rights reserved. * * All rights reserved. *
* * * *
* This program is free software; you can redistribute it and/or modify * * 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 * * it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or * * the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. * * (at your option) any later version. *
@ -58,15 +58,15 @@ namespace vcg {
/** \addtogroup space */ /** \addtogroup space */
/*@{*/ /*@{*/
/** /**
Templated class for 3D segment. Templated class for 3D segment.
This is the class for a segment in 3D space. A Segment is stored just as its two extrema (Point3). This is the class for a segment in 3D space. A Segment is stored just as its two extrema (Point3).
@param SegmentScalarType (template parameter) Specifies the type of scalar used to represent coords. @param SegmentScalarType (template parameter) Specifies the type of scalar used to represent coords.
*/ */
template <class SegmentScalarType > template <class SegmentScalarType >
class Segment3 class Segment3
{ {
public: public:
/// The scalar type /// The scalar type
typedef SegmentScalarType ScalarType; typedef SegmentScalarType ScalarType;
@ -84,11 +84,11 @@ private:
public: public:
/// Members to access either extrema /// Members to access either extrema
inline const PointType &P0() const { return _p0; } inline const PointType &P0() const { return _p0; }
inline const PointType &P1() const { return _p1; } inline const PointType &P1() const { return _p1; }
inline PointType &P0() { return _p0; } inline PointType &P0() { return _p0; }
inline PointType &P1() { return _p1; } inline PointType &P1() { return _p1; }
inline PointType &operator[] (const int i) {return i==0?_p0:_p1;} inline PointType &operator[] (const int i) {return i==0?_p0:_p1;}
inline const PointType &operator[] (const int i) const {return i==0?_p0:_p1;} inline const PointType &operator[] (const int i) const {return i==0?_p0:_p1;}
@ -108,7 +108,7 @@ public:
{ _p0=a; _p1=b;} { _p0=a; _p1=b;}
/// calculates the point of parameter t on the segment. /// calculates the point of parameter t on the segment.
/// if t is in [0..1] returned point is inside the segment /// if t is in [0..1] returned point is inside the segment
inline PointType P( const ScalarType t ) const inline PointType Lerp( const ScalarType t ) const
{ return _p0 + (_p1 - _p0) * t; } { return _p0 + (_p1 - _p0) * t; }
/// return the middle point /// return the middle point
inline PointType MidPoint( ) const inline PointType MidPoint( ) const
@ -119,7 +119,7 @@ public:
{ return ( _p1 - _p0).Normalize() ; } { return ( _p1 - _p0).Normalize() ; }
/// return the bounding box /// return the bounding box
inline Box3<ScalarType> BBox( ) const inline Box3<ScalarType> BBox( ) const
{ Box3<ScalarType> t; { Box3<ScalarType> t;
if (_p0[0]<_p1[0]) { t.min[0]=_p0[0];t.max[0]=_p1[0];} else { t.min[0]=_p1[0];t.max[0]=_p0[0];} if (_p0[0]<_p1[0]) { t.min[0]=_p0[0];t.max[0]=_p1[0];} else { t.min[0]=_p1[0];t.max[0]=_p0[0];}
if (_p0[1]<_p1[1]) { t.min[1]=_p0[1];t.max[1]=_p1[1];} else { t.min[1]=_p1[1];t.max[1]=_p0[1];} if (_p0[1]<_p1[1]) { t.min[1]=_p0[1];t.max[1]=_p1[1];} else { t.min[1]=_p1[1];t.max[1]=_p0[1];}
if (_p0[2]<_p1[2]) { t.min[2]=_p0[2];t.max[2]=_p1[2];} else { t.min[2]=_p1[2];t.max[2]=_p0[2];} if (_p0[2]<_p1[2]) { t.min[2]=_p0[2];t.max[2]=_p1[2];} else { t.min[2]=_p1[2];t.max[2]=_p0[2];}
@ -186,8 +186,8 @@ typedef Segment3<double> Segment3d;
// } // }
//}; //end of Distance method //}; //end of Distance method
// //
//template <class ScalarType> //template <class ScalarType>
//Point3<ScalarType> ClosestPoint( Segment3<ScalarType> s, const Point3<ScalarType> & p) //Point3<ScalarType> ClosestPoint( Segment3<ScalarType> s, const Point3<ScalarType> & p)
//{ //{
// vcg::Line3<ScalarType> l; // vcg::Line3<ScalarType> l;
// l.Set(s.P0(),s.P1()-s.P0()); // l.Set(s.P0(),s.P1()-s.P0());
@ -207,10 +207,10 @@ typedef Segment3<double> Segment3d;
// else // else
// return (s.P1()); // return (s.P1());
// } // }
// /*ScalarType t = s.Projection(p); // /*ScalarType t = s.Projection(p);
// if (s<0) return s.P0(); // if (s<0) return s.P0();
// if (s>1) return s.P0(); // if (s>1) return s.P0();
// return s.P(t);*/ // return s.P(t);*/
//} //}
/*@}*/ /*@}*/