vcglib/vcg/space/line3.h

154 lines
5.5 KiB
C
Raw Normal View History

2004-03-08 17:15:48 +01:00
/****************************************************************************
* VCGLib o o *
* Visual and Computer Graphics Library o o *
* _ O _ *
* Copyright(C) 2004 \/)\/ *
* Visual Computing Lab /\/| *
* ISTI - Italian National Research Council | *
* \ *
* All rights reserved. *
* *
* 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 *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License (http://www.gnu.org/licenses/gpl.txt) *
* for more details. *
* *
****************************************************************************/
/****************************************************************************
History
$Log: not supported by cvs2svn $
2004-03-09 17:47:32 +01:00
2004-03-08 20:46:47 +01:00
Revision 1.1 2004/03/08 16:15:48 tarini
first version (tarini)
2004-03-08 17:15:48 +01:00
****************************************************************************/
#ifndef __VCGLIB_LINE3
#define __VCGLIB_LINE3
#include <vcg/space/point3.h>
namespace vcg {
/** \addtogroup space */
/*@{*/
/**
Templated class for 3D lines.
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).
@param LineScalarType (template parameter) Specifies the type of scalar used to represent coords.
2004-03-09 17:47:32 +01:00
@param NORM: if on, the direction is always Normalized
2004-03-08 17:15:48 +01:00
*/
2004-03-09 17:47:32 +01:00
template <class LineScalarType, bool NORM=false>
2004-03-08 17:15:48 +01:00
class Line3
{
public:
/// The scalar type
typedef LineScalarType ScalarType;
/// The point type
typedef Point3<LineScalarType> PointType;
/// The point type
2004-03-09 17:47:32 +01:00
typedef Line3<LineScalarType,NORM> LineType;
2004-03-08 17:15:48 +01:00
private:
2004-03-10 16:27:48 +01:00
/// Origingin
2004-03-08 17:15:48 +01:00
PointType _ori;
2004-03-10 16:27:48 +01:00
/// Directionection (not necessarily normalized)
2004-03-08 17:15:48 +01:00
PointType _dir;
public:
/// Members to access the origin, direction
2004-03-10 16:27:48 +01:00
inline const PointType &Origin() const { return _ori; }
inline const PointType &Direction() const { return _dir; }
inline PointType &Origin() { return _ori; }
inline PointType &Direction() {
assert(!IsNormalized()); // Directionection can't be set for NORMALIZED Lines! Use SetDirection instead!
2004-03-09 17:47:32 +01:00
return _dir;
}
2004-03-08 17:15:48 +01:00
/// The empty constructor
Line3() {};
/// The (origin, direction) constructor
2004-03-10 16:27:48 +01:00
LineType(const PointType &ori, const PointType &dir) {SetOrigin(ori); SetDirection(dir);};
2004-03-08 20:46:47 +01:00
/// Operator to compare two lines
2004-03-08 17:15:48 +01:00
inline bool operator == ( LineType const & p ) const
{ return _ori==p._ori && _dir==p._dir; }
2004-03-08 20:46:47 +01:00
/// Operator to dispare two lines
2004-03-08 17:15:48 +01:00
inline bool operator != ( LineType const & p ) const
{ return _ori!=p._ori || _dir!=p._dir; }
2004-03-08 20:46:47 +01:00
/// Projects a point on the line
inline ScalarType Projection( const PointType &p ) const
2004-03-09 17:47:32 +01:00
{ if (NORM) return ScalarType((p-_ori)*_dir);
else return ScalarType((p-_ori)*_dir/_dir.SquaredNorm());
}
2004-03-10 16:27:48 +01:00
inline bool IsNormalized() const {return NORM;};
2004-03-09 17:47:32 +01:00
///set the origin
2004-03-10 16:27:48 +01:00
inline void SetOrigin( const PointType & ori )
2004-03-09 17:47:32 +01:00
{ _ori=ori; }
///set the direction
2004-03-10 16:27:48 +01:00
inline void SetDirection( const PointType & dir)
2004-03-09 17:47:32 +01:00
{ _dir=dir; if (NORM) _dir.Normalize(); }
///set both the origina and direction.
2004-03-09 20:29:52 +01:00
inline void Set( const PointType & ori, const PointType & dir )
2004-03-10 16:27:48 +01:00
{ SetOrigin(ori); SetDirection(dir); }
2004-03-08 17:15:48 +01:00
/// calculates the point of parameter t on the line.
inline PointType P( const ScalarType t ) const
2004-03-09 20:29:52 +01:00
{ return _ori + _dir * t; }
/// normalizes direction field (returns a Normalized Line)
2004-03-09 17:47:32 +01:00
Line3<ScalarType,true> &Normalize()
{ if (!NORM) _dir.Normalize(); return *((Line3<ScalarType,true>*)this);}
2004-03-09 20:29:52 +01:00
/// normalizes direction field (returns a Normalized Line) - static version
2004-03-09 17:47:32 +01:00
static Line3<ScalarType,true> &Normalize(LineType &p)
{ p.Normalize(); return *((Line3<ScalarType,true>*)(&p));}
2004-03-08 20:46:47 +01:00
/// importer for different line types
2004-03-09 17:47:32 +01:00
template <class Q, bool K>
inline void Import( const Line3<Q,K> & b )
2004-03-10 16:27:48 +01:00
{ _ori.Import( b.Origin()); _dir.Import( b.Direction());
2004-03-09 17:47:32 +01:00
if ((NORM) && (!K)) _dir.Normalize();
2004-03-08 20:46:47 +01:00
}
2004-03-09 20:29:52 +01:00
PointType ClosestPoint(const PointType & p) const{
return P(Projection(p));
}
/// flips the line
inline void Flip(){
_dir=-_dir;
};
2004-03-08 17:15:48 +01:00
}; // end class definition
typedef Line3<short> Line3s;
typedef Line3<int> Line3i;
typedef Line3<float> Line3f;
typedef Line3<double> Line3d;
2004-03-09 17:47:32 +01:00
typedef Line3<short ,true> Line3sN;
typedef Line3<int ,true> Line3iN;
typedef Line3<float ,true> Line3fN;
typedef Line3<double,true> Line3dN;
2004-03-09 20:29:52 +01:00
/// 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));
}
2004-03-08 17:15:48 +01:00
/*@}*/
} // end namespace
#endif