Added "Normalize" flag

This commit is contained in:
mtarini 2004-03-09 16:47:32 +00:00
parent c24a286899
commit 5f11437fd1
1 changed files with 36 additions and 16 deletions

View File

@ -24,6 +24,9 @@
History History
$Log: not supported by cvs2svn $ $Log: not supported by cvs2svn $
Revision 1.2 2004/03/08 19:46:47 tarini
First Version (tarini)
Revision 1.1 2004/03/08 16:15:48 tarini Revision 1.1 2004/03/08 16:15:48 tarini
first version (tarini) first version (tarini)
@ -47,8 +50,9 @@ Templated class for 3D lines.
This is the class for infinite lines in 3D space. A Line is stored just as two Point3: This is the class for infinite lines in 3D space. A Line is stored just as two Point3:
an origin and a direction (not necessarily normalized). an origin and a direction (not necessarily normalized).
@param LineScalarType (template parameter) Specifies the type of scalar used to represent coords. @param LineScalarType (template parameter) Specifies the type of scalar used to represent coords.
@param NORM: if on, the direction is always Normalized
*/ */
template <class LineScalarType > template <class LineScalarType, bool NORM=false>
class Line3 class Line3
{ {
public: public:
@ -60,7 +64,7 @@ public:
typedef Point3<LineScalarType> PointType; typedef Point3<LineScalarType> PointType;
/// The point type /// The point type
typedef Line3<LineScalarType> LineType; typedef Line3<LineScalarType,NORM> LineType;
private: private:
@ -76,12 +80,14 @@ public:
inline const PointType &Ori() const { return _ori; } inline const PointType &Ori() const { return _ori; }
inline const PointType &Dir() const { return _dir; } inline const PointType &Dir() const { return _dir; }
inline PointType &Ori() { return _ori; } 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 /// The empty constructor
Line3() {}; Line3() {};
/// The (origin, direction) constructor /// The (origin, direction) constructor
LineType(const PointType &ori, const PointType &dir) { _ori=ori; _dir=dir; }; LineType(const PointType &ori, const PointType &dir) {SetOri(ori); SetDir(dir);};
/// Operator to compare two lines /// Operator to compare two lines
inline bool operator == ( LineType const & p ) const inline bool operator == ( LineType const & p ) const
{ return _ori==p._ori && _dir==p._dir; } { return _ori==p._ori && _dir==p._dir; }
@ -90,23 +96,32 @@ public:
{ return _ori!=p._ori || _dir!=p._dir; } { return _ori!=p._ori || _dir!=p._dir; }
/// Projects a point on the line /// Projects a point on the line
inline ScalarType Projection( const PointType &p ) const inline ScalarType Projection( const PointType &p ) const
{ ScalarType l = dire.SquaredNorm(); { if (NORM) return ScalarType((p-_ori)*_dir);
return ScalarType((p-_ori)*_dir/l); } else return ScalarType((p-_ori)*_dir/_dir.SquaredNorm());
///set up of the line. }
bool IsNorm() const {return NORM;};
///set the origin
void SetOri( const PointType & ori )
{ _ori=ori; }
///set the direction
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 ) void Set( const PointType & ori, const PointType & dir )
{ _ori = ori; _dir=dir } { SetOri(ori); SetDir(dir); }
/// calculates the point of parameter t on the line. /// calculates the point of parameter t on the line.
inline PointType P( const ScalarType t ) const inline PointType P( const ScalarType t ) const
{ return orig + dire * t; } { return orig + dire * t; }
/// normalizes direction field /// normalizes direction field
LineType &Normalize() Line3<ScalarType,true> &Normalize()
{ _dir.Normalize(); return *this;} { if (!NORM) _dir.Normalize(); return *((Line3<ScalarType,true>*)this);}
static LineType &Normalize(LineType &p) static Line3<ScalarType,true> &Normalize(LineType &p)
{ p.Normalize(); return p;} { p.Normalize(); return *((Line3<ScalarType,true>*)(&p));}
/// importer for different line types /// importer for different line types
template <class Q> template <class Q, bool K>
inline void Import( const Line3<Q> & b ) 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();
} }
}; // end class definition }; // end class definition
@ -117,6 +132,11 @@ typedef Line3<int> Line3i;
typedef Line3<float> Line3f; typedef Line3<float> Line3f;
typedef Line3<double> Line3d; typedef Line3<double> Line3d;
typedef Line3<short ,true> Line3sN;
typedef Line3<int ,true> Line3iN;
typedef Line3<float ,true> Line3fN;
typedef Line3<double,true> Line3dN;
/*@}*/ /*@}*/