proj home

Files   Classes   Functions   Hierarchy  

quickhull3Dtest.cpp

Go to the documentation of this file.
00001 #include <vector>
00002 #include <fstream>
00003 using namespace std;
00004 
00005 #include <commandline.h>
00006 #include <generaterandompoints.h>
00007 #include <graphmisc.h>
00008 #include <gobj.h>
00009 #include <mathlib.h>
00010 #include <menusystem.h>
00011 #include <point.h>
00012 #include <pointsdisplay.h>
00013 #include <print.h>
00014 #include <printvector.h>
00015 #include <quickhull3D.h>
00016 #include <quickhull3Dtest.h>
00017 #include <random.h>
00018 #include <stringserialization.h>
00019 #include <zpr.h>
00020 
00021 quickhull3Dtest* quickhull3Dtest::member=0;
00022 
00023 void quickhull3Dtest::keyboard01
00024 (
00025   unsigned char key, 
00026   int x, 
00027   int y
00028 )
00029 {
00030   assert(member!=0);
00031   assert(member->displayaxes!=0);
00032   assert(member->quickhull!=0);
00033 
00034   switch (key)
00035   {
00036     case 27: exit(0); break;
00037     case 'a': member->displayaxes->toggle(); break;
00038 
00039     case 'p': member->pointsdisplay = ! member->pointsdisplay; break;
00040     case 'P': member->pointsnumber= ! member->pointsnumber;
00041       member->pointscontainer.nuke();
00042       pointsdisplay3D< pt3 >(
00043         member->pointscontainer,
00044         member->pts,
00045         member->pointsdisplay,
00046         member->pointsnumber);
00047       break;
00048 
00049     case 'h': member->menuhelp = ! member->menuhelp; break;
00050 
00051     case 'R':
00052     {
00053       quickhull3D< pt3, double > & qh(*(member->quickhull));
00054       qh.reset(); 
00055     }
00056     dynamicupdate();
00057     break;
00058 
00059     case 'r': 
00060     {
00061       quickhull3D< pt3, double > & qh(*(member->quickhull));
00062       for ( ; !qh; ++qh);
00063     }
00064     dynamicupdate();
00065     break;
00066 
00067     case 's':
00068     {
00069       quickhull3D< pt3, double > & qh(*(member->quickhull));
00070       if (!qh)
00071         ++qh;
00072     }
00073     dynamicupdate();
00074     break;
00075 
00076     case 'x': 
00077     {
00078       quickhull3D< pt3, double > & qh(*(member->quickhull));
00079       for ( qh.reset(); !qh; ++qh);
00080     }
00081     dynamicupdate();
00082     break;
00083      
00084   }
00085 
00086   glutPostRedisplay();
00087 }
00088 
00089 void quickhull3Dtest::display01()
00090 { 
00091   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00092 
00093   gobj::global->draw();
00094 
00095 
00096   glerrordisplay();
00097   
00098   glutSwapBuffers();
00099 }
00100 
00101 quickhull3Dtest::quickhull3Dtest()
00102   : xGraphics(true), pointscontainer(true), dynamic(true), 
00103     pointsdisplay(true),
00104     pointsnumber(false), menuhelp(true), menuswitch(0), quickhull(0)
00105 {
00106 }
00107 
00108 
00109 void quickhull3Dtest::dynamicupdate()
00110 {
00111   assert(member!=0);
00112 
00113   member->dynamic.nuke();
00114   member->dynamic.globalpush();
00115 
00116   gobjpush(new gobjglPushAttrib(GL_LIGHTING));
00117   gobjpush(new gobjglPushAttrib(GL_CURRENT_BIT));
00118   gobjpush(new gobjglEnable(GL_LIGHTING));
00119 
00120   gobjpush( new gobjglEnable(GL_BLEND));
00121   gobjpush(new gobjglBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA));
00122   gobjpush(new gobjglColor4f(191.0/255.0,0.0,0.0,0.3));
00123 
00124   // Draw the hull points found so far.
00125 
00126   gobjQuadric * q2 = new gobjQuadric();
00127   q2->radius = 0.10;
00128   q2->slices=25;
00129   q2->loops=7;
00130   gobjpush(q2);
00131 
00132   quickhull3D< pt3, double > & qh(*member->quickhull);
00133 
00134   for (uint i=0; i<qh.boundary.size(); ++i)
00135     { gobjpush( new gobjMySphereDraw(qh.pts[qh.boundary[i]],q2) ); }
00136 
00137   // Draw each half-space container and its points. 
00138 
00139   random11<double> r;
00140 
00141   gobjQuadric * q3 = new gobjQuadric();
00142   q3->radius = 0.02;
00143   q3->slices=25;
00144   q3->loops=7;
00145   gobjpush(q3);
00146 
00147   if (qh.cs.size()>0)
00148   {
00149     uint current = qh.cs.size()-1;
00150 
00151     gobjpush(new gobjglColor3f(1.0,0.0,0.0));
00152     gobjpush(new gobjglDisable(GL_LIGHTING));
00153 
00154     gobjpush(new gobjglBegin(GL_LINES));
00155 
00156     assert( qh.cs[current]!=0 );
00157     quickhull3D<pt3,double>::HS & hs(qh.cs[current]->halfspace);
00158 
00159     gobjpush(new gobjglVertex3f(hs.p0.x,hs.p0.y,hs.p0.z));
00160     gobjpush(new gobjglVertex3f(hs.p1.x,hs.p1.y,hs.p1.z));
00161     gobjpush(new gobjglVertex3f(hs.p1.x,hs.p1.y,hs.p1.z));
00162     gobjpush(new gobjglVertex3f(hs.p2.x,hs.p2.y,hs.p2.z));
00163     gobjpush(new gobjglVertex3f(hs.p2.x,hs.p2.y,hs.p2.z));
00164     gobjpush(new gobjglVertex3f(hs.p0.x,hs.p0.y,hs.p0.z));
00165 
00166     pt3 q0
00167     ((hs.p0.x+hs.p1.x+hs.p2.x)/3.0,
00168       (hs.p0.y+hs.p1.y+hs.p2.y)/3.0,
00169       (hs.p0.z+hs.p1.z+hs.p2.z)/3.0
00170     );
00171     pt3 q1(q0.x+hs.normal.x,q0.y+hs.normal.y,q0.z+hs.normal.z);
00172 
00173     gobjpush(new gobjglVertex3f(q0.x,q0.y,q0.z));
00174     gobjpush(new gobjglVertex3f(q1.x,q1.y,q1.z));
00175 
00176     gobjpush(new gobjglEnd());
00177 
00178     gobjpush(new gobjglEnable(GL_LIGHTING));
00179   }
00180 
00181   for (uint i=0; i<qh.cs.size(); ++i)
00182   {
00183     gobjpush(new gobjglColor4d(r(),r(),r(),r()));
00184 
00185     assert( qh.cs[i]!=0 );
00186     quickhull3D<pt3,double>::HS & hs(qh.cs[i]->halfspace);
00187 
00188     gobjpush(new gobjglBegin(GL_TRIANGLES));
00189     gobjpush(new gobjglVertex3f(hs.p0.x,hs.p0.y,hs.p0.z));
00190     gobjpush(new gobjglVertex3f(hs.p1.x,hs.p1.y,hs.p1.z));
00191     gobjpush(new gobjglVertex3f(hs.p2.x,hs.p2.y,hs.p2.z));
00192     gobjpush(new gobjglEnd());
00193     
00194     list<uint> & L(qh.cs[i]->index);
00195     for (list<uint>::iterator k=L.begin();
00196       k!=L.end(); ++k)
00197     {
00198       gobjpush( new gobjMySphereDraw(qh.pts[*k],q3) ); 
00199     }
00200   }
00201 
00202   gobjpush(new gobjglPopAttrib());
00203   gobjpush(new gobjglPopAttrib());
00204 
00205   member->dynamic.globalpop();
00206 }
00207 
00208 
00209 
00210 void quickhull3Dtest::test01(int argc, char** argv)
00211 {
00212   glutInit(&argc,argv);
00213   glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
00214   glutInitWindowSize(800,600);
00215   glutCreateWindow("");
00216   glutDisplayFunc(display01);
00217   glutKeyboardFunc(keyboard01);
00218 
00219   OpenGLinitialisation();
00220 
00221   glEnable(GL_DEPTH_TEST);
00222   glEnable(GL_CULL_FACE);
00223   glEnable(GL_NORMALIZE);
00224 
00225   xGraphics.set();
00226 
00227   member = this;
00228   displayaxes = new gobjSwitch<>( new myaxes(1.0), true);
00229   gobjpush(displayaxes);
00230 
00231   commandline cmd(argc,argv);
00232   uint numPoints(10);
00233   cmd.mapvar(numPoints,"n");
00234 
00235   typedef point3<double> pt3;
00236   //vector< pt3 > v;
00237   generateRandomPointsInSphere< pt3, double, random11<double> >(pts,numPoints+1);
00238 
00239   //cout << SHOW(pts.size()) << endl;
00240 
00241   gobjpush(new gobjglColor3f(0.0,1.0,0.0));
00242   gobjpush(new gobjglDisable(GL_LIGHTING));
00243 
00244   pointsdisplay3D< pt3 >( member->pointscontainer, pts, pointsdisplay, pointsnumber );
00245   gobjpush(new gobjSwitch<bool &>(&member->pointscontainer,pointsdisplay,false));
00246 
00247   menusystem * menu = 
00248     new menusystem(0,0,true,point2<GLint>(60,30),10);
00249   menu->fontcolor = point4<float>(218.0/255.0,165.0/255.0,32.0/255.0,0.75);
00250 
00251   menuswitch = new gobjSwitch<bool &>(menu,menuhelp);
00252 
00253   menu->addfont12("Hull calculation in 3D",2);
00254   menu->addfont10("p   Toggle the points.",1);
00255   menu->addfont10("P   Toggle numbering the points.",1);
00256   menu->addfont10("a   Toggle the axes.",2);
00257 
00258   menu->addfont10("x   reset and run",1);
00259   menu->addfont10("R   reset",1);
00260   menu->addfont10("r   run",1);
00261   menu->addfont10("s   step",2);
00262 
00263   menu->addfont10("h   toggle this help menu",1);
00264   menu->addfont10("ESC  quit");
00265 
00266   gobjpush(menuswitch);
00267 
00268   gobjpush(new gobjSwitch<>(&dynamic,true,false));
00269   
00270   glutPostRedisplay();
00271 
00272   quickhull = new quickhull3D< pt3, double >(pts);
00273 
00274   //quickhull3D< pt3, double > qh(pts);
00275 
00276   //for ( qh.reset(); !qh; ++qh);
00277   
00278 
00279   //cout << print(qh.boundary) << endl;
00280 
00281 
00282 /*
00283   quickhull3D< pt3, double > qh(v);
00284 
00285   //cout << SHOW(print(qh.boundary)) << endl;
00286 
00287   gobjpush(new gobjglDisable(GL_LIGHTING));
00288   gobjpush(new gobjglColor3ub(255,0,0));
00289   gobjpush(new gobjglBegin(GL_POINTS));
00290   for (uint i=0; i<qh.boundary.size(); ++i)
00291     { gobjpush(new gobjglVertex3d(v[qh.boundary[i]])); }
00292   gobjpush(new gobjglEnd());
00293 
00294   vector<uint> v2;
00295   integersetdiff(v2,qh.boundary,numPoints);
00296   gobjpush(new gobjglColor3ub(0,255,0));
00297   gobjpush(new gobjglBegin(GL_POINTS));
00298   for (uint i=0; i<v2.size(); ++i)
00299     { gobjpush(new gobjglVertex3d(v[v2[i]])); }
00300   gobjpush(new gobjglEnd());
00301 */
00302 
00303 
00304   //gobj::global->displaylist(1);
00305 
00306 
00307   zpr zz;
00308   glutMainLoop();
00309 }
00310 
00311 
00312 /*
00313 
00314 void quickhull3Dtest::test02(int argc, char** argv)
00315 {
00316   glutInit(&argc,argv);
00317   glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
00318   glutInitWindowSize(800,600);
00319   glutCreateWindow("");
00320   glutDisplayFunc(display01);
00321   glutKeyboardFunc(keyboard01);
00322 
00323   OpenGLinitialisation();
00324 
00325   glEnable(GL_DEPTH_TEST);
00326   glEnable(GL_CULL_FACE);
00327   glEnable(GL_NORMALIZE);
00328 
00329   xGraphics.set();
00330 
00331   commandline cmd(argc,argv);
00332 
00333   uint numPoints(500);
00334   cmd.mapvar(numPoints,"numPoints");
00335 
00336   bool labelpoints(false);
00337   cmd.mapvar(labelpoints,"labelpoints");
00338   bool drawpoints(true);
00339   cmd.mapvar(drawpoints,"drawpoints");
00340 
00341   typedef point3<double> pt3;
00342   vector< pt3 > v;
00343   generateRandomPointsInSphere< pt3, double, random11<double> >(v,numPoints);
00344   
00345   gobjpush( new gobjglDisable(GL_LIGHTING) );
00346   gobjpush( new gobjglColor3f(0.0,1.0,0.0) );
00347   pointsdisplay3D<pt3> dp(*gobj::global,v,drawpoints,labelpoints);
00348 
00349 //cout << "Pre quickhull3D" << endl;
00350   quickhull3D< pt3, double > qh(v);
00351 //cout << "Post quickhull3D" << endl;
00352 
00353   vector< pt3 > v2;
00354   for (uint i=0; i<qh.boundary.size(); ++i)
00355     v2.push_back( v[qh.boundary[i]] );
00356   gobjpush( new gobjglColor3f(1.0,0.0,0.0) );
00357   pointsdisplay3D<pt3> dp2(*gobj::global,v2,true,false);
00358   dp2.gq->radius=0.02;
00359 
00360 
00361   //gobj::global->displaylist(1);
00362   zpr zz;
00363   glutMainLoop();
00364 }
00365 */
00366 
00367 
00368 
00369 
00370 
00371 
00372 

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