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 #ifndef __VCGLIB_RAY2
00033 #define __VCGLIB_RAY2
00034
00035 #include <vcg/space/point2.h>
00036
00037 namespace vcg {
00038
00048 template <class RayScalarType, bool NORM=false>
00049 class Ray2
00050 {
00051 public:
00052
00054 typedef RayScalarType ScalarType;
00055
00057 typedef Point2<RayScalarType> PointType;
00058
00060 typedef Ray2<RayScalarType,NORM> RayType;
00061
00062 private:
00063
00065 PointType _ori;
00066
00068 PointType _dir;
00069
00070 public:
00071
00073
00077
00078 inline const PointType &Origin() const { return _ori; }
00079 inline PointType &Origin() { return _ori; }
00080 inline const PointType &Direction() const { return _dir; }
00082 inline void SetOrigin( const PointType & ori )
00083 { _ori=ori; }
00085 inline void SetDirection( const PointType & dir)
00086 { _dir=dir; if (NORM) _dir.Normalize(); }
00088 inline void Set( const PointType & ori, const PointType & dir )
00089 { SetOrigin(ori); SetDirection(dir); }
00091
00093
00095
00096 Ray2() {};
00098 Ray2(const PointType &ori, const PointType &dir) {SetOrigin(ori); SetDirection(dir);};
00100
00102 inline bool operator == ( RayType const & p ) const
00103 { return _ori==p._ori && _dir==p._dir; }
00105 inline bool operator != ( RayType const & p ) const
00106 { return _ori!=p._ori || _dir!=p._dir; }
00108 inline ScalarType Projection( const PointType &p ) const
00109 { if (NORM) return ScalarType((p-_ori)*_dir);
00110 else return ScalarType((p-_ori)*_dir/_dir.SquaredNorm());
00111 }
00113 static bool IsNormalized() {return NORM;};
00115 inline PointType P( const ScalarType t ) const
00116 { return _ori + _dir * t; }
00118 inline Ray2<ScalarType,true> &Normalize()
00119 { if (!NORM) _dir.Normalize(); return *((Ray2<ScalarType,true>*)this);}
00121 static Ray2<ScalarType,true> &Normalize(RayType &p)
00122 { p.Normalize(); return *((Ray2<ScalarType,true>*)(&p));}
00124 template <class Q, bool K>
00125 inline void Import( const Ray2<Q,K> & b )
00126 { _ori.Import( b.Origin() ); _dir.Import( b.Direction() );
00127 if ((NORM) && (!K)) _dir.Normalize();
00128
00129 }
00131 template <class Q, bool K>
00132 static RayType Construct( const Ray2<Q,K> & b )
00133 { RayType res; res.Import(b); return res;
00134 }
00135 PointType ClosestPoint(const PointType & p) const{
00136 return P(Projection(p));
00137 }
00139 inline void Flip(){
00140 _dir=-_dir;
00141 };
00142
00144
00151 inline Ray2<ScalarType,false> operator + ( RayType const & p) const
00152 {return Ray2<ScalarType,false> ( _ori+p.Origin(), _dir+p.Direction() );}
00153 inline Ray2<ScalarType,false> operator - ( RayType const & p) const
00154 {return Ray2<ScalarType,false> ( _ori-p.Origin(), _dir-p.Direction() );}
00155 inline Ray2<ScalarType,false> operator * ( const ScalarType s ) const
00156 {return Ray2<ScalarType,false> ( _ori*s, _dir*s );}
00157 inline Ray2<ScalarType,false> operator / ( const ScalarType s ) const
00158 {ScalarType s0=((ScalarType)1.0)/s; return RayType( _ori*s0, _dir*s0 );}
00160
00161
00163
00167
00168 Ray2(const Ray2<ScalarType,!NORM > &r)
00169 { Import(r); };
00171 inline RayType & operator = ( Ray2<ScalarType,!NORM> const &r)
00172 { Import(r); return *this; };
00174
00175 };
00176
00177 typedef Ray2<short> Ray2s;
00178 typedef Ray2<int> Ray2i;
00179 typedef Ray2<float> Ray2f;
00180 typedef Ray2<double> Ray2d;
00181
00182 typedef Ray2<short ,true> Ray2sN;
00183 typedef Ray2<int ,true> Ray2iN;
00184 typedef Ray2<float ,true> Ray2fN;
00185 typedef Ray2<double,true> Ray2dN;
00186
00188 template <class ScalarType, bool NORM>
00189 Point3<ScalarType> ClosestPoint( Ray2<ScalarType,NORM> r, const Point3<ScalarType> & p)
00190 {
00191 ScalarType t = r.Projection(p);
00192 if (t<0) return r.Origin();
00193 return r.P(t);
00194 }
00195
00198 }
00199 #endif