smaller update

This commit is contained in:
mtarini 2004-03-09 19:29:52 +00:00
parent 5ed7bf6aba
commit 86e23fb5d4
3 changed files with 100 additions and 42 deletions

View File

@ -24,6 +24,9 @@
History
$Log: not supported by cvs2svn $
Revision 1.3 2004/03/09 16:47:32 tarini
Added "Normalize" flag
Revision 1.2 2004/03/08 19:46:47 tarini
First Version (tarini)
@ -99,34 +102,40 @@ public:
{ if (NORM) return ScalarType((p-_ori)*_dir);
else return ScalarType((p-_ori)*_dir/_dir.SquaredNorm());
}
bool IsNorm() const {return NORM;};
inline bool IsNorm() const {return NORM;};
///set the origin
void SetOri( const PointType & ori )
inline void SetOri( const PointType & ori )
{ _ori=ori; }
///set the direction
void SetDir( const PointType & dir)
inline void SetDir( const PointType & dir)
{ _dir=dir; if (NORM) _dir.Normalize(); }
///set both the origina and direction.
void Set( const PointType & ori, const PointType & dir )
inline void Set( const PointType & ori, const PointType & dir )
{ SetOri(ori); SetDir(dir); }
/// calculates the point of parameter t on the line.
inline PointType P( const ScalarType t ) const
{ return orig + dire * t; }
/// normalizes direction field
{ return _ori + _dir * t; }
/// normalizes direction field (returns a Normalized Line)
Line3<ScalarType,true> &Normalize()
{ if (!NORM) _dir.Normalize(); return *((Line3<ScalarType,true>*)this);}
/// normalizes direction field (returns a Normalized Line) - static version
static Line3<ScalarType,true> &Normalize(LineType &p)
{ p.Normalize(); return *((Line3<ScalarType,true>*)(&p));}
/// importer for different line types
template <class Q, bool K>
inline void Import( const Line3<Q,K> & b )
{ _ori.Import( b._ori); _dir.Import( b._dir);
{ _ori.Import( b.Ori()); _dir.Import( b.Dir());
if ((NORM) && (!K)) _dir.Normalize();
}
PointType ClosestPoint(const PointType & p) const{
return P(Projection(p));
}
/// flips the line
inline void Flip(){
_dir=-_dir;
};
}; // end class definition
typedef Line3<short> Line3s;
typedef Line3<int> Line3i;
typedef Line3<float> Line3f;
@ -137,6 +146,12 @@ typedef Line3<int ,true> Line3iN;
typedef Line3<float ,true> Line3fN;
typedef Line3<double,true> Line3dN;
/// returns closest point
template <class ScalarType, bool NORM>
Point3<ScalarType> ClosestPoint( Line3<ScalarType,NORM> l, const Point3<ScalarType> & p)
{
return l.P(l.Projection(p));
}
/*@}*/

View File

@ -24,6 +24,9 @@
History
$Log: not supported by cvs2svn $
Revision 1.1 2004/03/08 19:46:47 tarini
First Version (tarini)
****************************************************************************/
@ -40,11 +43,13 @@ namespace vcg {
/** \addtogroup space */
/*@{*/
/**
Templated class for 3D ray.
This is the class for a semi-line in 3D space. A ray is stored just as origin and direction (Point3).
Templated class for 3D rays.
This is the class for infinite rays in 3D space. A Ray is stored just as two Point3:
an origin and a direction (not necessarily normalized).
@param RayScalarType (template parameter) Specifies the type of scalar used to represent coords.
@param NORM: if on, the direction is always Normalized
*/
template <class RayScalarType >
template <class RayScalarType, bool NORM=false>
class Ray3
{
public:
@ -56,7 +61,7 @@ public:
typedef Point3<RayScalarType> PointType;
/// The point type
typedef Ray3<RayScalarType> RayType;
typedef Ray3<RayScalarType,NORM> RayType;
private:
@ -72,49 +77,77 @@ public:
inline const PointType &Ori() const { return _ori; }
inline const PointType &Dir() const { return _dir; }
inline PointType &Ori() { return _ori; }
inline PointType &Dir() { return _dir; }
inline PointType &Dir() {
assert(NORM); // Direction can't be set for NORMALIZED Rays! Use SetDir instead!
return _dir;
}
/// The empty constructor
Ray3() {};
/// The (a,b) constructor
RayType(const PointType &a, const PointType &b) { _p0=a; _p1=b; };
/// The (origin, direction) constructor
RayType(const PointType &ori, const PointType &dir) {SetOri(ori); SetDir(dir);};
/// Operator to compare two rays
inline bool operator == ( LineType const & p ) const
inline bool operator == ( RayType const & p ) const
{ return _ori==p._ori && _dir==p._dir; }
/// Operator to dispare two rays
inline bool operator != ( LineType const & p ) const
inline bool operator != ( RayType const & p ) const
{ return _ori!=p._ori || _dir!=p._dir; }
/// Projects a point on the ray (returns distance)
/// projection exists iff result > 0
/// Projects a point on the ray
inline ScalarType Projection( const PointType &p ) const
{ ScalarType l = dire.SquaredNorm();
return ScalarType((p-_ori)*_dir/l); }
///set up of the line.
void Set( const PointType & ori, const PointType & dir )
{ _ori = ori; _dir=dir }
/// calculates the point of parameter t on the ray.
/// if t>0, point is on the ray
inline PointType P( const ScalarType t ) const
{ return orig + dire * t; }
/// normalizes direction field
LineType &Normalize()
{ _dir.Normalize(); return *this;}
static LineType &Normalize(LineType &p)
{ p.Normalize(); return p;}
/// importer for different Ray types
template <class Q>
inline void Import( const Ray3<Q> & b )
{ _ori.Import( b._ori); _dir.Import( b._dir);
{ if (NORM) return ScalarType((p-_ori)*_dir);
else return ScalarType((p-_ori)*_dir/_dir.SquaredNorm());
}
inline bool IsNorm() const {return NORM;};
///set the origin
inline void SetOri( const PointType & ori )
{ _ori=ori; }
///set the direction
inline void SetDir( const PointType & dir)
{ _dir=dir; if (NORM) _dir.Normalize(); }
///set both the origina and direction.
inline void Set( const PointType & ori, const PointType & dir )
{ SetOri(ori); SetDir(dir); }
/// calculates the point of parameter t on the ray.
inline PointType P( const ScalarType t ) const
{ return _ori + _dir * t; }
/// normalizes direction field (returns a Normalized Ray)
inline Ray3<ScalarType,true> &Normalize()
{ if (!NORM) _dir.Normalize(); return *((Ray3<ScalarType,true>*)this);}
/// normalizes direction field (returns a Normalized Ray) - static version
static Ray3<ScalarType,true> &Normalize(RayType &p)
{ p.Normalize(); return *((Ray3<ScalarType,true>*)(&p));}
/// importer for different ray types
template <class Q, bool K>
inline void Import( const Ray3<Q,K> & b )
{ _ori.Import( b.Ori() ); _dir.Import( b.Dir() );
if ((NORM) && (!K)) _dir.Normalize();
}
PointType ClosestPoint(const PointType & p) const{
return P(Projection(p));
}
/// flips the ray
inline void Flip(){
_dir=-_dir;
};
}; // end class definition
typedef Ray3<short> Ray3s;
typedef Ray3<int> Ray3i;
typedef Ray3<int> Ray3i;
typedef Ray3<float> Ray3f;
typedef Ray3<double> Ray3d;
typedef Ray3<short ,true> Ray3sN;
typedef Ray3<int ,true> Ray3iN;
typedef Ray3<float ,true> Ray3fN;
typedef Ray3<double,true> Ray3dN;
/// returns closest point
template <class ScalarType, bool NORM>
Point3<ScalarType> ClosestPoint( Ray3<ScalarType,NORM> r, const Point3<ScalarType> & p)
{
ScalarType t = r.Projection(p);
if (t<0) t=0;
return r.P(t);
}
/*@}*/

View File

@ -24,6 +24,9 @@
History
$Log: not supported by cvs2svn $
Revision 1.1 2004/03/08 19:46:47 tarini
First Version (tarini)
****************************************************************************/
@ -122,6 +125,13 @@ typedef Segment3<int> Segment3i;
typedef Segment3<float> Segment3f;
typedef Segment3<double> Segment3d;
template <class ScalarType>
Point3<ScalarType> ClosestPoint( Segment3<ScalarType> s, const Point3<ScalarType> & p)
{
ScalarType t = s.Projection(p);
clamp (); if (s<0) s=0;
return r.P(t);
}
/*@}*/