Go to the documentation of this file.00001 #ifndef OPENTISSUE_COLLISION_INTERSECT_INTERSECT_RECT_QUAD_EDGES_H
00002 #define OPENTISSUE_COLLISION_INTERSECT_INTERSECT_RECT_QUAD_EDGES_H
00003
00004
00005
00006
00007
00008
00009
00010 #include <OpenTissue/configuration.h>
00011
00012 namespace OpenTissue
00013 {
00014 namespace intersect
00015 {
00016
00042 template<typename real_type>
00043 int rect_quad_edges(real_type * rect, real_type * quad, bool * inside, real_type * ret)
00044 {
00045 int cnt = 0;
00046 real_type * r = ret;
00047 {
00048
00049 real_type qx0,qy0,qx1,qy1,tst;
00050 real_type * q = quad;
00051 for(int i=0;i<4;++i)
00052 {
00053 qx0 = *q; ++q;
00054 qy0 = *q; ++q;
00055 real_type * nextq = (i==3)?quad:q;
00056 qx1 = *nextq; ++nextq;
00057 qy1 = *nextq;
00058 bool inside0 = inside[i];
00059 bool inside1 = inside[(i+1)%4];
00060 if(inside0 && inside1)
00061 continue;
00062 real_type dx = (qx1-qx0);
00063 real_type dy = (qy1-qy0);
00064 if(dx)
00065 {
00066 real_type alpha = dy/dx;
00067 tst = - rect[0];
00068 if( ((qx0 < tst) && (qx1 > tst)) || ((qx0 > tst) && (qx1 < tst)) )
00069 {
00070 real_type qxt = -rect[0];
00071 real_type qyt = qy0 + (qxt-qx0)*alpha;
00072 if( (-rect[1] < qyt) && (qyt < rect[1]))
00073 {
00074 *r = qxt; ++r;
00075 *r = qyt; ++r;
00076 ++cnt;
00077 }
00078 }
00079 tst = rect[0];
00080 if( ((qx0 < tst) && (qx1 > tst)) || ((qx0 > tst) && (qx1 < tst)) )
00081 {
00082 real_type qxt = rect[0];
00083 real_type qyt = qy0 + (qxt-qx0)*alpha;
00084 if( (-rect[1] < qyt) && (qyt < rect[1]))
00085 {
00086 *r = qxt; ++r;
00087 *r = qyt; ++r;
00088 ++cnt;
00089 }
00090 }
00091 }
00092 if(dy)
00093 {
00094 real_type inv_alpha = dx/dy;
00095 tst = - rect[1];
00096 if( ((qy0 < tst) && (qy1 > tst)) || ((qy0 > tst) && (qy1 < tst)) )
00097 {
00098 real_type qyt = -rect[1];
00099 real_type qxt = qx0 + (qyt-qy0)*inv_alpha;
00100 if( (-rect[0] < qxt)&&(qxt < rect[0]))
00101 {
00102 *r = qxt; ++r;
00103 *r = qyt; ++r;
00104 ++cnt;
00105 }
00106 }
00107 tst = rect[1];
00108 if( ((qy0 < tst) && (qy1 > tst)) || ((qy0 > tst) && (qy1 < tst)) )
00109 {
00110 real_type qyt = rect[1];
00111 real_type qxt = qx0 + (qyt-qy0)*inv_alpha;
00112 if( (-rect[0] < qxt)&&(qxt < rect[0]))
00113 {
00114 *r = qxt; ++r;
00115 *r = qyt; ++r;
00116 ++cnt;
00117 }
00118 }
00119 }
00120 }
00121 }
00122 return cnt;
00123 }
00124
00125 }
00126
00127 }
00128
00129
00130 #endif