proj home

Files   Classes   Functions   Hierarchy  

boxOBBhalfspaceD2.h

Go to the documentation of this file.
00001 #ifndef BOXOBBHALFSPACED2_H
00002 #define BOXOBBHALFSPACED2_H
00003 
00004 #include <sstream>
00005 using namespace std;
00006 
00007 #include <line.h>
00008 #include <typedefs.h>
00009 #include <zero.h>
00010 
00018 template< typename PT, typename PD >
00019 class boxOBBhalfspaceD2
00020 {
00021 public:
00022 
00024   PT center;
00026   PT arm1;
00028   PT arm2;
00030   PD arm1len;
00032   PD arm2len;
00033 
00035   static PT qi[4];
00037   static PT pi[5];
00038 
00039 
00041   boxOBBhalfspaceD2()
00042     {}
00043 
00045   boxOBBhalfspaceD2
00046   (
00047     PT const & _center,
00048     PT const & _arm1,
00049     PT const & _arm2,
00050     PD const _arm1len,
00051     PD const _arm2len
00052   );
00053 
00055   template< typename W >
00056   void construct
00057   (
00058     W const & p0,
00059     W const & p1,
00060     W const & p2,
00061     W const & p3
00062   );
00063 
00065   boolc intersects( PT const & p ) const;
00066 
00068   boolc intersects( boxOBBhalfspaceD2<PT,PD> & B2 );
00069 
00071   boolc sees( boxOBBhalfspaceD2<PT,PD> & B2 );
00072 
00074   void cornerpoints( PT & p0, PT & p1, PT & p2, PT & p3 ) const;
00075 
00077   operator stringc () const;
00078 
00079 };
00080 
00081 
00082 //---------------------------------------------------------
00083 // Implementation
00084 
00085 template< typename PT, typename PD >
00086 PT boxOBBhalfspaceD2<PT,PD>::qi[4];
00087 
00088 template< typename PT, typename PD >
00089 PT boxOBBhalfspaceD2<PT,PD>::pi[5];
00090 
00091 template< typename PT, typename PD >
00092 boxOBBhalfspaceD2<PT,PD>::operator stringc () const
00093 {
00094   stringstream ss;  
00095 
00096   ss << center << "  " << arm1 << "  " << arm2 << "  ";
00097   ss << arm1len << "  " << arm2len;
00098 
00099   return ss.str();
00100 }
00101 
00102 
00103 template< typename PT, typename PD >
00104 void boxOBBhalfspaceD2<PT,PD>::cornerpoints
00105 ( 
00106   PT & p0, 
00107   PT & p1, 
00108   PT & p2, 
00109   PT & p3 
00110 ) const
00111 {
00112   p0 = center - arm1*arm1len - arm2*arm2len;
00113   p1 = center + arm1*arm1len - arm2*arm2len;
00114   p2 = center + arm1*arm1len + arm2*arm2len;
00115   p3 = center - arm1*arm1len + arm2*arm2len;
00116 }
00117 
00118 template< typename PT, typename PD >
00119 template< typename W >
00120 void boxOBBhalfspaceD2<PT,PD>::construct
00121 (
00122   W const & p0,
00123   W const & p1,
00124   W const & p2,
00125   W const & p3
00126 )
00127 {
00128   center = ((p0+p1+p2+p3)*0.25);
00129   arm1 = (p1-p0);
00130   arm1.normalize();
00131   arm2 = (p3-p0);
00132   arm2.normalize();
00133   arm1len = (p0-p1).distance()*0.5;
00134   arm2len = (p0-p3).distance()*0.5;
00135 
00136   assert( zero<PD>::test((p0+arm1*arm1len*2.0+arm2*arm2len*2.0-p2).dot())==true );
00137 }
00138 
00139 template< typename PT, typename PD >
00140 boxOBBhalfspaceD2<PT,PD>::boxOBBhalfspaceD2
00141 (
00142   PT const & _center,
00143   PT const & _arm1,
00144   PT const & _arm2,
00145   PD const _arm1len,
00146   PD const _arm2len
00147 )
00148   : center(_center), arm1(_arm1), arm2(_arm2), arm1len(_arm1len),
00149   arm2len(_arm2len)
00150 {
00151   assert( zero<PD>::test(arm1.dot(arm2)) == true );
00152   assert( zero<PD>::test(arm1.dot())==false );
00153   assert( zero<PD>::test(arm2.dot())==false );
00154   assert( zero<PD>::test(arm1.dot()-1.0)==true );
00155   assert( zero<PD>::test(arm2.dot()-1.0)==true );
00156 }
00157 
00158 template< typename PT, typename PD >
00159 boolc boxOBBhalfspaceD2<PT,PD>::intersects
00160 ( 
00161   boxOBBhalfspaceD2<PT,PD> & B2 
00162 ) 
00163 {
00164   if (sees(B2)==false)
00165     return true;
00166 
00167   return ! B2.sees(*this);
00168 }
00169 
00170 template< typename PT, typename PD >
00171 boolc boxOBBhalfspaceD2<PT,PD>::sees
00172 ( 
00173   boxOBBhalfspaceD2<PT,PD> & B2 
00174 ) 
00175 {
00176   B2.cornerpoints(pi[0],pi[1],pi[2],pi[3]);
00177 
00178 //cout << SHOW(arm1) << endl;
00179 //cout << SHOW(arm2) << endl;
00180 //cout << SHOW(arm1len) << endl;
00181 //cout << SHOW(arm2len) << endl;
00182 
00183   //static point2<double> qi[4];
00184   qi[0] = center + arm1*(arm1len);
00185   qi[1] = center + arm1*(-arm1len);
00186   qi[2] = center + arm2*(arm2len);
00187   qi[3] = center + arm2*(-arm2len);
00188 
00189   pi[4] = B2.center;
00190 
00191 //cout << SHOW(center) << endl;
00192 //cout << SHOW(qi[2]) << endl;
00193 
00194   uint count=0;
00195   for (uint k=0; k<5; ++k)
00196   {
00197 //cout << SHOW(k) << endl;
00198 //cout << SHOW((pi[k]-qi[0]).dot(arm1)>0.0) << endl;
00199     if ((pi[k]-qi[0]).dot(arm1)>0.0)
00200     {
00201       ++count;
00202       continue;
00203     }
00204 //cout << SHOW((pi[k]-qi[1]).dot(arm1)<0.0) << endl;
00205     if ((pi[k]-qi[1]).dot(arm1)<0.0)
00206     {
00207       ++count;
00208       continue;
00209     }
00210 //cout << SHOW((pi[k]-qi[2]).dot(arm2)>0.0) << endl;
00211     if ((pi[k]-qi[2]).dot(arm2)>0.0)
00212     {
00213       ++count;
00214       continue;
00215     }
00216 //cout << SHOW((pi[k]-qi[3]).dot(arm2)<0.0) << endl;
00217     if ((pi[k]-qi[3]).dot(arm2)<0.0)
00218     {
00219       ++count;
00220     }
00221   }
00222 //cout << SHOW(count) << endl;
00223 
00224   return (count==5);
00225 }
00226 
00227 template< typename PT, typename PD >
00228 boolc boxOBBhalfspaceD2<PT,PD>::intersects( PT const & p ) const
00229 {
00230   //if ((p-(center+arm1*arm1len)).dot(arm1) > (PD)0)
00231   if ((p-(center+arm1*arm1len)).dot(arm1) > zero<PD>::val)
00232     return false;
00233   //if ((p-(center-arm1*arm1len)).dot(arm1) < (PD)0)
00234   if (((center-arm1*arm1len)-p).dot(arm1) > zero<PD>::val)
00235     return false;
00236 
00237   //if ((p-(center+arm2*arm2len)).dot(arm2) > (PD)0)
00238   if ((p-(center+arm2*arm2len)).dot(arm2) > zero<PD>::val)
00239     return false;
00240   //if ((p-(center-arm2*arm2len)).dot(arm2) < (PD)0)
00241   if (((center-arm2*arm2len)-p).dot(arm2) > zero<PD>::val)
00242     return false;
00243 
00244   return true;
00245 }
00246 
00247 
00248 #endif
00249 
00250 

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