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
00005
00006
00007
00008
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
00050
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
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
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
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
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
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 }
00194
00195 }
00196
00197
00198 #endif