proj home

Files   Classes   Functions   Hierarchy  

line.h

Go to the documentation of this file.
00001 #ifndef LINE_H
00002 #define LINE_H
00003 
00004 #include <mathlib.h>
00005 #include <typedefs.h>
00006 #include <zero.h>
00007 
00020 template< typename PT, typename PD >
00021 class line
00022 {
00023 public:
00024 
00026   PT nml;
00027 
00029   PT pos;
00030 
00032   line(PT const & nml_, PT const & pos_);
00033 
00035   line(PT const & p0, PT const & p1, boolc endpoints);
00036 
00038   void construct(PT const & nml_,PT const & pos_); 
00040   void constructfromendpoints(PT const & p0, PT const & p1)
00041     { pos=p0; nml=(p1-p0); }
00042 
00044   void normalize();
00045 
00047   void normalize(PD const len);
00048     
00050   PT const operator () (PD const t) const
00051     { return pos + nml*t; }
00052 
00054   boolc tD2(PD & t, PT const & p) const;
00055 
00057   boolc tD3(PD & t, PT const & p) const;
00058   
00060   operator stringc () const;
00061 
00063   boolc isnormalzero() const
00064     { return zero<PD>::test(nml.dot()); }
00065 
00070   void nearestpoint(PD & t, PT const & p) const;
00071 
00075   void nearestpointcapped(PD & t, PT const & p) const;
00076   
00079   void nearestpoint(PT & q, PT const & p) const;
00080 
00082   static void nearestpointonline
00083   (
00084     PT & x,
00085     PT const & p,
00086     PT const & a,
00087     PT const & b
00088   );
00089 
00092   boolc lineD3minimized
00093   (
00094     PD & t1, 
00095     PD & t2, 
00096     line<PT,PD> const & L2
00097   ) const;
00098 
00099 
00101   boolc intersects
00102   (
00103     PD & t0,
00104     PD & t1,
00105     line<PT,PD> const & L2
00106   );
00107 
00108 };
00109 
00110 
00111 //---------------------------------------------------------
00112 // Implementation
00113 
00114 template< typename PT, typename PD >
00115 boolc line<PT,PD>::intersects
00116 (
00117   PD & t0,
00118   PD & t1,
00119   line<PT,PD> const & L2
00120 )
00121 {
00122   return solver<PD>::d2linearequ
00123   (
00124     t0,
00125     t1,
00126     nml.x,
00127     L2.nml.x*((PD)-1.0),
00128     nml.y,
00129     L2.nml.y*((PD)-1.0),
00130     L2.pos.x-pos.x,
00131     L2.pos.y-pos.y
00132   );
00133 }
00134 
00135 template< typename PT, typename PD >
00136 line<PT,PD>:: operator stringc () const
00137 {
00138   return (stringc)nml + " " + (stringc)pos;
00139 }
00140 
00141 template< typename PT, typename PD >
00142 boolc line<PT,PD>::lineD3minimized
00143 (
00144   PD & t1, 
00145   PD & t2, 
00146   line<PT,PD> const & L2
00147 ) const
00148 {
00149   PT C(pos-L2.pos);
00150   PT E1(nml);
00151   PT E2(L2.nml*(-1));
00152 
00153   bool res;
00154   res = solver<PD>::d2linearequ
00155   (
00156     t1,t2,
00157     E1.dot(),E1.dot(E2),
00158     E1.dot(E2),E2.dot(),
00159     C.dot(E1)*(-1),
00160     C.dot(E2)*(-1)
00161   );
00162 
00163   return res;
00164 }
00165 
00166 template< typename PT, typename PD >
00167 void line<PT,PD>::construct(PT const & nml_,PT const & pos_) 
00168 { 
00169   nml = nml_; 
00170   pos=pos_; 
00171 }
00172 
00173 template< typename PT, typename PD >
00174 line<PT,PD>::line(PT const & p0, PT const & p1, boolc endpoints)
00175   : nml(p1-p0), pos(p0) 
00176 { 
00177   assert(endpoints); 
00178 }
00179 
00180 template< typename PT, typename PD >
00181 line<PT,PD>::line(PT const & nml_, PT const & pos_)
00182   :nml(nml_), pos(pos_) 
00183 {
00184 }
00185 
00186 template< typename PT, typename PD >
00187 void line<PT,PD>::normalize()
00188 { 
00189   nml.normalize(); 
00190 }
00191 
00192 template< typename PT, typename PD >
00193 void line<PT,PD>::normalize(PD const len)
00194 { 
00195   nml.normalize(); 
00196   nml *= len; 
00197 }
00198 
00199 template< typename PT, typename PD >
00200 boolc line<PT,PD>::tD2(PD & t, PT const & p) const
00201 {
00202   if (zero<PD>::test(nml.x))
00203   {
00204     if (zero<PD>::test(nml.y))
00205       return false;
00206 
00207     t = (p.y-pos.y)/nml.y;
00208   }
00209   else
00210     t = (p.x-pos.x)/nml.x;
00211 
00212   assert( zero<PD>::test( (operator()(t)-p).dot()) );
00213   return true;
00214 }
00215 
00216 template< typename PT, typename PD >
00217 boolc line<PT,PD>::tD3(PD & t, PT const & p) const
00218 {
00219   if (zero<PD>::test(nml.x))
00220   {
00221     if (zero<PD>::test(nml.y))
00222     {
00223       if (zero<PD>::test(nml.z))
00224         return false;
00225 
00226       t = (p.z-pos.z)/nml.z;
00227     }
00228     else
00229       t = (p.y-pos.y)/nml.y;
00230   }
00231   else
00232     t = (p.x-pos.x)/nml.x;
00233 
00234   assert( zero<PD>::test( (operator()(t)-p).dot()) );
00235   return true;
00236 }
00237 
00238 template< typename PT, typename PD >
00239 void line<PT,PD>::nearestpoint(PD & t, PT const & p) const
00240 {
00241   assert(zero<PD>::test(nml.dot())==false);
00242 
00243   t=(p-pos).dot(nml)/nml.dot();
00244 }
00245 
00246 template< typename PT, typename PD >
00247 void line<PT,PD>::nearestpointcapped(PD & t, PT const & p) const
00248 {
00249   nearestpoint(t,p);
00250   if (t<((PD)0.0))
00251     t = (PD)0.0;
00252   if (t>((PD)1.0))
00253     t = (PD)1.0;
00254 }
00255 
00256 template< typename PT, typename PD >
00257 void line<PT,PD>::nearestpoint(PT & q, PT const & p) const
00258 {
00259   assert(zero<PD>::test(nml.dot())==false);
00260 
00261   q = pos;
00262   q += nml*(p-pos).dot(nml)/nml.dot();
00263 }
00264 
00265 template< typename PT, typename PD >
00266 void line<PT,PD>::nearestpointonline
00267 (
00268   PT & x,
00269   PT const & p,
00270   PT const & a,
00271   PT const & b
00272 )
00273 {  
00274   assert(zero<PD>::test(b.dot())==false);
00275   x = a + b*( (p-a).dot(b)/(b.dot(b)) );
00276 }
00277 
00278 
00279 #endif
00280 
00281 

Generated on Fri Mar 4 00:49:28 2011 for Chelton Evans Source by  doxygen 1.5.8