00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047 #ifndef __VCGLIB_LINE3
00048 #define __VCGLIB_LINE3
00049
00050 #include <vcg/space/point3.h>
00051
00052 namespace vcg {
00053
00063 template <class LineScalarType, bool NORM=false>
00064 class Line3
00065 {
00066 public:
00067
00069 typedef LineScalarType ScalarType;
00070
00072 typedef Point3<LineScalarType> PointType;
00073
00075 typedef Line3<LineScalarType,NORM> LineType;
00076
00077 private:
00078
00080 PointType _ori;
00081
00083 PointType _dir;
00084
00085 public:
00086
00088
00092
00093 inline const PointType &Origin() const { return _ori; }
00094 inline PointType &Origin() { return _ori; }
00095 inline const PointType &Direction() const { return _dir; }
00097 inline void SetOrigin( const PointType & ori )
00098 { _ori=ori; }
00100 inline void SetDirection( const PointType & dir)
00101 { _dir=dir; if (NORM) _dir.Normalize(); }
00103 inline void Set( const PointType & ori, const PointType & dir )
00104 { SetOrigin(ori); SetDirection(dir); }
00106
00108
00110
00111 Line3() {};
00113 Line3(const PointType &ori, const PointType &dir) {SetOrigin(ori); SetDirection(dir);};
00115
00117 inline bool operator == ( LineType const & p ) const
00118 { return _ori==p._ori && _dir==p._dir; }
00120 inline bool operator != ( LineType const & p ) const
00121 { return _ori!=p._ori || _dir!=p._dir; }
00123 inline ScalarType Projection( const PointType &p ) const
00124 { if (NORM) return ScalarType((p-_ori).dot(_dir));
00125 else return ScalarType((p-_ori).dot(_dir)/_dir.SquaredNorm());
00126 }
00128 static bool IsNormalized() {return NORM;};
00130 inline PointType P( const ScalarType t ) const
00131 { return _ori + _dir * t; }
00133 inline Line3<ScalarType,true> &Normalize()
00134 { if (!NORM) _dir.Normalize(); return *((Line3<ScalarType,true>*)this);}
00136 static Line3<ScalarType,true> &Normalize(LineType &p)
00137 { p.Normalize(); return *((Line3<ScalarType,true>*)(&p));}
00139 template <class Q, bool K>
00140 inline void Import( const Line3<Q,K> & b )
00141 { _ori.Import( b.Origin() ); _dir.Import( b.Direction() );
00142 if ((NORM) && (!K)) _dir.Normalize();
00143
00144 }
00146 template <class Q, bool K>
00147 static LineType Construct( const Line3<Q,K> & b )
00148 { LineType res; res.Import(b); return res;
00149 }
00150 PointType ClosestPoint(const PointType & p) const{
00151 return P(Projection(p));
00152 }
00154 inline void Flip(){
00155 _dir=-_dir;
00156 };
00157
00159
00166 inline Line3<ScalarType,false> operator + ( LineType const & p) const
00167 {return Line3<ScalarType,false> ( _ori+p.Origin(), _dir+p.Direction() );}
00168 inline Line3<ScalarType,false> operator - ( LineType const & p) const
00169 {return Line3<ScalarType,false> ( _ori-p.Origin(), _dir-p.Direction() );}
00170 inline Line3<ScalarType,false> operator * ( const ScalarType s ) const
00171 {return Line3<ScalarType,false> ( _ori*s, _dir*s );}
00172 inline Line3<ScalarType,false> operator / ( const ScalarType s ) const
00173 {ScalarType s0=((ScalarType)1.0)/s; return LineType( _ori*s0, _dir*s0 );}
00175
00176
00178
00182
00183 Line3 (const Line3<ScalarType,!NORM > &r)
00184 { Import(r); };
00186 inline LineType & operator = ( Line3<ScalarType,!NORM> const &r)
00187 { Import(r); return *this; };
00189
00190 };
00191
00192 typedef Line3<short> Line3s;
00193 typedef Line3<int> Line3i;
00194 typedef Line3<float> Line3f;
00195 typedef Line3<double> Line3d;
00196
00197 typedef Line3<short ,true> Line3sN;
00198 typedef Line3<int ,true> Line3iN;
00199 typedef Line3<float ,true> Line3fN;
00200 typedef Line3<double,true> Line3dN;
00201
00203 template <class ScalarType, bool NORM>
00204 Point3<ScalarType> ClosestPoint( Line3<ScalarType,NORM> l, const Point3<ScalarType> & p)
00205 {
00206 return l.P(l.Projection(p));
00207 }
00208
00209 template <class ScalarType, bool NORM>
00210 ScalarType Distance(const Line3<ScalarType, NORM> &l,
00211 const Point3<ScalarType> &p) {
00212 Point3<ScalarType> o = l.ClosestPoint(p);
00213 return (o - p).Norm();
00214 }
00215
00218 }
00219 #endif