Files Classes Functions Hierarchy
00001 #include <cassert> 00002 #include <fstream> 00003 #include <iostream> 00004 using namespace std; 00005 00006 00007 #include <boxOBBhalfspaceD2.h> 00008 #include <boxOBBhalfspaceD2test.h> 00009 #include <commandline.h> 00010 #include <pointsdisplay.h> 00011 #include <graphmisc.h> 00012 #include <menusystem.h> 00013 #include <zpr.h> 00014 00015 00016 boxOBBhalfspaceD2<point2<double>,double> * boxOBBhalfspaceD2test::Aptr; 00017 boxOBBhalfspaceD2<point2<double>,double> * boxOBBhalfspaceD2test::Bptr; 00018 00019 gobjContainer boxOBBhalfspaceD2test::boxes(true); 00020 00021 bool * boxOBBhalfspaceD2test::help = 0; 00022 00023 00024 void boxOBBhalfspaceD2test::display01() 00025 { 00026 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 00027 00028 gobj::global->draw(); 00029 00030 glerrordisplay(); 00031 00032 glutSwapBuffers(); 00033 } 00034 00035 void boxOBBhalfspaceD2test::keyboard01(unsigned char key, int x, int y) 00036 { 00037 static double delta = 0.1; 00038 00039 assert(Aptr!=0); 00040 assert(Bptr!=0); 00041 00042 switch (key) 00043 { 00044 case 27: 00045 exit(0); 00046 break; 00047 00048 case 'j': Bptr->center.x += delta; break; 00049 case 'J': Bptr->center.x -= delta; break; 00050 case 'k': Bptr->center.y += delta; break; 00051 case 'K': Bptr->center.y -= delta; break; 00052 case 'l': 00053 { 00054 transrotate2D tr(delta); 00055 tr.eval(Bptr->arm1); 00056 tr.eval(Bptr->arm2); 00057 break; 00058 } 00059 case 'L': 00060 { 00061 transrotate2D tr(-delta); 00062 tr.eval(Bptr->arm1); 00063 tr.eval(Bptr->arm2); 00064 break; 00065 } 00066 00067 case 'a': Aptr->center.x += delta; break; 00068 case 'A': Aptr->center.x -= delta; break; 00069 case 's': Aptr->center.y += delta; break; 00070 case 'S': Aptr->center.y -= delta; break; 00071 case 'd': 00072 { 00073 transrotate2D tr(delta); 00074 tr.eval(Aptr->arm1); 00075 tr.eval(Aptr->arm2); 00076 break; 00077 } 00078 case 'D': 00079 { 00080 transrotate2D tr(-delta); 00081 tr.eval(Aptr->arm1); 00082 tr.eval(Aptr->arm2); 00083 break; 00084 } 00085 00086 case 'p': 00087 cout << (stringc)(*Aptr) << endl; 00088 cout << (stringc)(*Bptr) << endl; 00089 break; 00090 00091 case '+': delta *= 10.0; if (delta==0.0) delta=0.1; break; 00092 case '-': delta /= 10.0; break; 00093 00094 case 'h': if (help!=0) *help = !*help; break; 00095 } 00096 00097 update01(); 00098 glutPostRedisplay(); 00099 } 00100 00101 void boxOBBhalfspaceD2test::update01() 00102 { 00103 assert(Aptr!=0); 00104 assert(Bptr!=0); 00105 00106 bool intersection = (Aptr->intersects(*Bptr)); 00107 00108 vector< point2<double> > pts(4); 00109 Aptr->cornerpoints(pts[0],pts[1],pts[2],pts[3]); 00110 00111 boxes.nuke(); 00112 00113 boxes.push(new gobjglColor3ub(184,134,11)); 00114 00115 vector<point2<double> > v(&Aptr->qi[0],&Aptr->qi[0]+4); 00116 pointsdisplay2D<point2<double> > qidp(boxes,v,true,true,false); 00117 qidp.gq->radius=0.01; 00118 00119 boxes.push(new gobjglBegin(GL_LINES)); 00120 boxes.push(new gobjglVertex2f(pts[0])); 00121 boxes.push(new gobjglVertex2f(pts[1])); 00122 boxes.push(new gobjglVertex2f(pts[1])); 00123 boxes.push(new gobjglVertex2f(pts[2])); 00124 boxes.push(new gobjglVertex2f(pts[2])); 00125 boxes.push(new gobjglVertex2f(pts[3])); 00126 boxes.push(new gobjglVertex2f(pts[3])); 00127 boxes.push(new gobjglVertex2f(pts[0])); 00128 00129 Bptr->cornerpoints(pts[0],pts[1],pts[2],pts[3]); 00130 00131 boxes.push(new gobjglColor3ub(0,191,255)); 00132 00133 00134 00135 boxes.push(new gobjglVertex2f(pts[0])); 00136 boxes.push(new gobjglVertex2f(pts[1])); 00137 boxes.push(new gobjglVertex2f(pts[1])); 00138 boxes.push(new gobjglVertex2f(pts[2])); 00139 boxes.push(new gobjglVertex2f(pts[2])); 00140 boxes.push(new gobjglVertex2f(pts[3])); 00141 boxes.push(new gobjglVertex2f(pts[3])); 00142 boxes.push(new gobjglVertex2f(pts[0])); 00143 00144 boxes.push(new gobjglColor3ub(255,0,255)); 00145 boxes.push(new gobjglVertex2f(Aptr->center)); 00146 boxes.push(new gobjglVertex2f(Bptr->center)); 00147 00148 boxes.push(new gobjglEnd()); 00149 00150 boxes.push(new gobjglColor3ub(0,191,255)); 00151 pointsdisplay2D<point2<double> > pidp(boxes,pts,true,true,false); 00152 pidp.gq->radius=0.01; 00153 00154 if (intersection) 00155 { 00156 double transparency=0.4; 00157 Aptr->cornerpoints(pts[0],pts[1],pts[2],pts[3]); 00158 //boxes.push(new gobjglColor3ub(255,0,0)); 00159 00160 boxes.push(new gobjglEnable(GL_BLEND)); 00161 boxes.push( 00162 new gobjglBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA) ); 00163 boxes.push( 00164 new gobjglColor4f(1.0,0.0,0.0,transparency)); 00165 00166 boxes.push(new gobjglBegin(GL_TRIANGLES)); 00167 boxes.push(new gobjglVertex2f(pts[0])); 00168 boxes.push(new gobjglVertex2f(pts[1])); 00169 boxes.push(new gobjglVertex2f(pts[2])); 00170 boxes.push(new gobjglVertex2f(pts[2])); 00171 boxes.push(new gobjglVertex2f(pts[3])); 00172 boxes.push(new gobjglVertex2f(pts[0])); 00173 00174 Bptr->cornerpoints(pts[0],pts[1],pts[2],pts[3]); 00175 boxes.push(new gobjglVertex2f(pts[0])); 00176 boxes.push(new gobjglVertex2f(pts[1])); 00177 boxes.push(new gobjglVertex2f(pts[2])); 00178 boxes.push(new gobjglVertex2f(pts[2])); 00179 boxes.push(new gobjglVertex2f(pts[3])); 00180 boxes.push(new gobjglVertex2f(pts[0])); 00181 boxes.push(new gobjglEnd()); 00182 00183 boxes.push(new gobjglDisable(GL_BLEND)); 00184 } 00185 } 00186 00187 void boxOBBhalfspaceD2test::test01(int & argc, char** argv) 00188 { 00189 glutInit(&argc,argv); 00190 glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); 00191 glutInitWindowSize(800,600); 00192 glutCreateWindow(""); 00193 glutDisplayFunc(display01); 00194 glutKeyboardFunc(keyboard01); 00195 00196 OpenGLinitialisation(); 00197 00198 glEnable(GL_DEPTH_TEST); 00199 glEnable(GL_CULL_FACE); 00200 glEnable(GL_NORMALIZE); 00201 00202 zpr zz; 00203 00204 xGraphics.set(); 00205 00206 commandline cmd(argc,argv); 00207 00208 string in; 00209 00210 cmd.mapvar(in,"in"); 00211 if ( in.empty() ) 00212 { 00213 cout << "error: in=filename expected" << endl; 00214 return; 00215 } 00216 00217 ifstream filein(in.c_str()); 00218 assert(filein.good()==true); 00219 if (filein.good()==false) 00220 return; 00221 00222 typedef point2<double> pt2; 00223 00224 pt2 center; 00225 pt2 arm1; 00226 pt2 arm2; 00227 double arm1len; 00228 double arm2len; 00229 00230 filein >> center; 00231 filein >> arm1; 00232 filein >> arm2; 00233 filein >> arm1len; 00234 filein >> arm2len; 00235 boxOBBhalfspaceD2<point2<double>,double> 00236 A(center,arm1,arm2,arm1len,arm2len); 00237 00238 filein >> center; 00239 filein >> arm1; 00240 filein >> arm2; 00241 filein >> arm1len; 00242 filein >> arm2len; 00243 boxOBBhalfspaceD2<point2<double>,double> 00244 B(center,arm1,arm2,arm1len,arm2len); 00245 00246 00247 Aptr = & A; 00248 Bptr = & B; 00249 gobjpush(&boxes); 00250 00251 update01(); 00252 00253 menusystem * menu = 00254 new menusystem(0,0,true,point2<GLint>(20,20),10); 00255 00256 gobjSwitch<> * menuswitch = new gobjSwitch<>(menu,true); 00257 help = & menuswitch->isdrawn; 00258 gobjpush(menuswitch); 00259 00260 menu->addfont12("OBB intersection test"); 00261 menu->addnewline(); 00262 menu->addnewline(); 00263 menu->addfont10("j J Move box B left or right."); 00264 menu->addnewline(); 00265 menu->addfont10("k K Move box B up or down."); 00266 menu->addnewline(); 00267 menu->addfont10("l L Rotate box B."); 00268 menu->addnewline(); 00269 00270 menu->addfont10("a A Move box A left or right."); 00271 menu->addnewline(); 00272 menu->addfont10("s S Move box A up or down."); 00273 menu->addnewline(); 00274 menu->addfont10("d D Rotate box A."); 00275 menu->addnewline(); 00276 00277 menu->addnewline(); 00278 menu->addfont10("p Print boxes to terminal."); 00279 menu->addnewline(); 00280 menu->addfont10("+ - Increase or decrease change."); 00281 menu->addnewline(); 00282 menu->addfont10("h Toggle this help menu."); 00283 menu->addnewline(); 00284 menu->addfont10("ESC Quit"); 00285 00286 00287 zz.update(); 00288 glutMainLoop(); 00289 } 00290 00291 00292 int boxOBBhalfspaceD2test::unittest01() 00293 { 00294 typedef point2<double> pt2; 00295 pt2 pi[4] = 00296 { 00297 pt2(6.0,0.0), pt2(7.0,0.0), 00298 pt2(7.0,3.0), pt2(6.0,3.0) 00299 }; 00300 00301 boxOBBhalfspaceD2<pt2,double> bx; 00302 bx.construct(pi[0],pi[1],pi[2],pi[3]); 00303 00304 assertreturnOS( 00305 zero<double>::test( (bx.center-pt2(6.5,1.5)).dot() ) ); 00306 00307 pt2 zi[4]; 00308 bx.cornerpoints(zi[0],zi[1],zi[2],zi[3]); 00309 00310 for (uint i=0; i<4; ++i) 00311 assertreturnOS( zero<double>::test( (pi[i]-zi[i]).dot() ) ); 00312 00313 return 0; 00314 } 00315 00316 int boxOBBhalfspaceD2test::unittest02(int & argc, char** argv) 00317 { 00318 typedef point2<double> pt2; 00319 00320 commandline cmd(argc,argv); 00321 00322 string p[5]; 00323 string pstr[5]={"p0","p1","p2","p3","q"}; 00324 00325 pt2 pi[5]; 00326 00327 bool inside(true); 00328 cmd.mapvar(inside,"inside"); 00329 00330 for (uint i=0; i<5; ++i) 00331 { 00332 cmd.mapvar(p[i],pstr[i]); 00333 if ( p[i].empty() ) 00334 return 1; 00335 00336 pi[i].serializeInverseBrackets(p[i]); 00337 00338 cout << "pi[" << i << "]=" << pi[i] << endl; 00339 } 00340 00341 boxOBBhalfspaceD2<pt2,double> bx; 00342 bx.construct(pi[0],pi[1],pi[2],pi[3]); 00343 00344 if (inside) 00345 { 00346 if (bx.intersects(pi[4])==false) 00347 return 1; 00348 00349 return 0; 00350 } 00351 00352 if (bx.intersects(pi[4])==true) 00353 return 1; 00354 00355 return 0; 00356 } 00357 00358
1.5.8