• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Classes
  • Files
  • Examples
  • File List
  • File Members

/home/hauberg/Dokumenter/Capture/humim-tracker-0.1/src/OpenTissue/OpenTissue/collision/intersect/intersect_rect_quad.h

Go to the documentation of this file.
00001 #ifndef OPENTISSUE_COLLISION_INTERSECT_INTERSECT_RECT_QUAD_H
00002 #define OPENTISSUE_COLLISION_INTERSECT_INTERSECT_RECT_QUAD_H
00003 //
00004 // OpenTissue Template Library
00005 // - A generic toolbox for physics-based modeling and simulation.
00006 // Copyright (C) 2008 Department of Computer Science, University of Copenhagen.
00007 //
00008 // OTTL is licensed under zlib: http://opensource.org/licenses/zlib-license.php
00009 //
00010 #include <OpenTissue/configuration.h>
00011 
00012 
00013 namespace OpenTissue
00014 {
00015   namespace intersect
00016   {
00017 
00043     template<typename real_type>
00044     int rect_quad(real_type * rect,real_type * quad,real_type * ret)
00045     {
00046       int cnt = 0;
00047       real_type * r = ret;
00048       {
00049         //--- Test the four vertices of the quad to see if they are
00050         //--- inside the rect or lies on the boundary
00051         real_type * q = quad;
00052         for(int i=0;i<4;++i)
00053         {
00054           real_type qx = *q;  ++q;
00055           real_type qy = *q;  ++q;
00056           if( (-rect[0] <= qx) && (qx <= rect[0]) && (-rect[1] <= qy) && (qy <= rect[1]))
00057           {
00058             *r = qx; ++r;
00059             *r = qy; ++r;
00060             ++cnt;
00061           }
00062         }
00063       }
00064       {
00065         //--- Test the four edges of the quad for crossing the edges of the rect.
00066         real_type qx0,qy0,qx1,qy1;
00067         real_type * q = quad;
00068         for(int i=0;i<4;++i)
00069         {
00070           qx0 = *q;     ++q;
00071           qy0 = *q;     ++q;
00072 
00073           real_type * nextq = (i==3)?quad:q;
00074           qx1 = *nextq; ++nextq;
00075           qy1 = *nextq;
00076           //--- Math derivations for finding crossings with vertical rect-sides
00077           //
00078           // The formula for the x-coordinate of a straight line is:
00079           //
00080           //  qx0 + t (qx1-qx0) = qxt
00081           //
00082           //  Knowing qxt, which we do since it is given by the rect side, we can isolate the t-parameter
00083           //
00084           //  t  = (qxt-qx0) / (qx1-qx0)
00085           //
00086           //  Now the formula for the y-coordinate is:
00087           //
00088           //  qy0 + t (qy1-qy0) = qyt
00089           //
00090           //  Substituting the t-parameter value, allow us to compute the y-value of the crossing point.
00091           //
00092           //  qy0 + ( (qxt-qx0)/(qx1-qx0) ) (qy1-qy0) = qyt
00093           //
00094           // Cleaning up the parenteses we have
00095           //
00096           //  qyt = qy0 + (qxt-qx0)*(qy1-qy0)/(qx1-qx0)
00097           //
00098           real_type dx = (qx1-qx0);
00099           real_type dy = (qy1-qy0);
00100           if(fabs(dx)>0)
00101           {
00102             real_type alpha = dy/dx;
00103             if(
00104               ((qx0 < -rect[0]) && (qx1 > -rect[0]))
00105               ||
00106               ((qx0 > -rect[0]) && (qx1 < -rect[0]))
00107               )
00108             {
00109               real_type qxt = -rect[0];
00110               real_type qyt = qy0 + (qxt-qx0)*alpha;
00111               if( (-rect[1] < qyt) &&   (qyt < rect[1]))
00112               {
00113                 *r = qxt; ++r;
00114                 *r = qyt; ++r;
00115                 ++cnt;
00116               }
00117             }
00118             if(
00119               ((qx0 < rect[0]) && (qx1 > rect[0]))
00120               ||
00121               ((qx0 > rect[0]) && (qx1 < rect[0]))
00122               )
00123             {
00124               real_type qxt = rect[0];
00125               real_type qyt = qy0 + (qxt-qx0)*alpha;
00126               if( (-rect[1] < qyt) &&   (qyt < rect[1]))
00127               {
00128                 *r = qxt; ++r;
00129                 *r = qyt; ++r;
00130                 ++cnt;
00131               }
00132             }
00133           }
00134           //--- Math derivations for finding crossings with horizontal rect-sides
00135           //
00136           // The formula for the y-coordinate of a straight line is:
00137           //
00138           //  qy0 + t (qy1-qy0) = qyt
00139           //
00140           //  Knowing qyt, which we do since it is given by the rect side, we can isolate the t-parameter
00141           //
00142           //  t  = (qyt-qy0) / (qy1-qy0)
00143           //
00144           //  Now the formula for the x-coordinate is:
00145           //
00146           //  qxt = qx0 + t (qx1-qx0)
00147           //
00148           //  Substituting the t-parameter value, allow us to compute the x-value of the crossing point.
00149           //
00150           //  qxt = qx0 + (qyt-qy0)(qx1-qx0)/(qy1-qy0)
00151           //
00152           //
00153           if(fabs(dy)>0)
00154           {
00155             real_type inv_alpha = dx/dy;
00156             if(
00157               ((qy0 < -rect[1]) && (qy1 > -rect[1]))
00158               ||
00159               ((qy0 > -rect[1]) && (qy1 < -rect[1]))
00160               )
00161             {
00162               real_type qyt = -rect[1];
00163               real_type qxt = qx0 + (qyt-qy0)*inv_alpha;
00164               if( (-rect[0] < qxt)&&(qxt < rect[0]))
00165               {
00166                 *r = qxt;  ++r;
00167                 *r = qyt;  ++r;
00168                 ++cnt;
00169               }
00170             }
00171             if(
00172               ((qy0 < rect[1]) && (qy1 > rect[1]))
00173               ||
00174               ((qy0 > rect[1]) && (qy1 < rect[1]))
00175               )
00176             {
00177               real_type qyt = rect[1];
00178               real_type qxt = qx0 + (qyt-qy0)*inv_alpha;
00179               if( (-rect[0] < qxt)&&(qxt < rect[0]))
00180               {
00181                 *r = qxt;  ++r;
00182                 *r = qyt;  ++r;
00183                 ++cnt;
00184               }
00185             }
00186           }
00187         }
00188       }
00189 
00190       return cnt;
00191     }
00192 
00193   } //End of namespace intersect
00194 
00195 } //End of namespace OpenTissue
00196 
00197 // OPENTISSUE_COLLISION_INTERSECT_INTERSECT_RECT_QUAD_H
00198 #endif

Generated on Thu Dec 1 2011 12:50:54 for HUMIM Tracker by  doxygen 1.7.1