proj home

Files   Classes   Functions   Hierarchy  

quickhull2Dtest.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 <point.h>
00010 #include <pointsdisplay.h>
00011 #include <print.h>
00012 #include <quickhull2D.h>
00013 #include <quickhull2Dtest.h>
00014 #include <random.h>
00015 #include <stringserialization.h>
00016 #include <zpr.h>
00017 
00018 
00019 //template<>
00020 //double const halfspaceD2<point2<double>,double>::zero = 1E-15;
00021 
00022 //template<>
00023 //float const halfspaceD2<point2<float>,float>::zero = 1E-6;
00024 
00025 void quickhull2Dtest::keyboard01
00026 (
00027   unsigned char key, 
00028   int x, 
00029   int y
00030 )
00031 {
00032   switch (key)
00033   {
00034     case 27:
00035       exit(0);
00036       break;
00037   }
00038 }
00039 
00040 void quickhull2Dtest::display01()
00041 { 
00042   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00043 
00044   gobj::global->draw();
00045 
00046 
00047   glerrordisplay();
00048   
00049   glutSwapBuffers();
00050 }
00051 
00052 void quickhull2Dtest::test02(int argc, char** argv)
00053 {
00054   glutInit(&argc,argv);
00055   glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
00056   glutInitWindowSize(800,600);
00057   glutCreateWindow("");
00058   glutDisplayFunc(display01);
00059   glutKeyboardFunc(keyboard01);
00060 
00061   OpenGLinitialisation();
00062 
00063   glEnable(GL_DEPTH_TEST);
00064   glEnable(GL_CULL_FACE);
00065   glEnable(GL_NORMALIZE);
00066 
00067   xGraphics.set();
00068 
00069   commandline cmd(argc,argv);
00070 
00071   cout << "Calculate the hull of a randomly generated set of points" << endl;
00072   cout << "  inside a circle. The hull points are colored red." << endl;
00073   cout << "Options:" << endl;
00074   cout << "  numPoints=4500  varies the number of points." << endl;
00075   
00076   uint numPoints(500);
00077   cmd.mapvar(numPoints,"numPoints");
00078 
00079   bool labelpoints(false);
00080   cmd.mapvar(labelpoints,"labelpoints");
00081   bool drawpoints(true);
00082   cmd.mapvar(drawpoints,"drawpoints");
00083 
00084   typedef point2<double> pt2;
00085   vector< pt2 > v;
00086   generateRandomPointsInCircle< pt2, double, random11<double> >(v,numPoints);
00087   //vectorprint::space="\n";
00088   //cout << v << endl;
00089 
00090   quickhull2D< pt2, double > qh(v);
00091 
00092   gobjpush( new gobjglDisable(GL_LIGHTING) );
00093   gobjpush( new gobjglColor3f(0.0,1.0,0.0) );
00094   pointsdisplay2D<pt2> dp(*gobj::global,v,drawpoints,labelpoints);
00095 
00096   vector< pt2 > v2;
00097   for (uint i=0; i<qh.boundary.size(); ++i)
00098     v2.push_back( v[qh.boundary[i]] );
00099   gobjpush( new gobjglColor3f(1.0,0.0,0.0) );
00100   pointsdisplay2D<pt2> dp2(*gobj::global,v2,true,false);
00101   dp2.gq->radius=0.02;
00102 
00103   gobjpush( new gobjglBegin(GL_LINE_LOOP) );
00104   for (uint i=0; i<qh.boundary.size(); ++i)
00105     { gobjpush( new gobjglVertex2f(v[qh.boundary[i]]) ); }
00106   gobjpush( new gobjglEnd() );
00107 
00108   gobj::global->displaylist(1);
00109   zpr zz;
00110   glutMainLoop();
00111 }
00112 
00113 
00114 void quickhull2Dtest::test03()
00115 {
00116   uintc n=6;
00117   vector< point2<float> > v;
00118   generateRandomPointsInCircle< point2<float>, float, random11<float> >(v,n);
00119   cout << print(v,"\n") << endl;
00120 
00121   // <TODO>
00122   //quickhull2D<point2<float>,float> q1(v);
00123   //cout << q1.boundary << endl;
00124 }
00125 
00126 void quickhull2Dtest::test04(int argc, char** argv)
00127 {
00128   commandline cmd(argc,argv);
00129   
00130   uint numPoints(10);
00131   cmd.mapvar(numPoints,"numPoints");
00132   string pointsfile;
00133   cmd.mapvar(pointsfile,"pointsfile");
00134   if (pointsfile.empty())
00135   {
00136     cout << "error:  A filename was not given. eg pointsfile=data.txt" << endl;
00137     return;
00138   }
00139   
00140   vector< point2<double> > pts;
00141   generateRandomPointsInCircle< point2<double>, double, random11<float> >
00142     (pts,numPoints);
00143 
00144   if (vectorfile::serialize(pointsfile,pts)==false)
00145   {
00146     cout << "error:  Failed to write file" << pointsfile << "." << endl;
00147     return;
00148   }
00149  
00150   cout << "success:  Written out the points to file " << pointsfile << "." << endl;
00151   cout << endl;
00152 
00153 }
00154 
00155 void quickhull2Dtest::test05(int argc, char** argv)
00156 {
00157   commandline cmd(argc,argv);
00158 
00159   string pointsfile;
00160   cmd.mapvar(pointsfile,"pointsfile");
00161   string hullfile;
00162   cmd.mapvar(hullfile,"hullfile");
00163 
00164   boolc pointsfileinvalid(pointsfile.empty());
00165   if (pointsfileinvalid)
00166     cout << "error:  A pointsfile was not given. eg pointsfile=data.txt" << endl;
00167   boolc hullfileinvalid(hullfile.empty());
00168   if (hullfileinvalid)
00169     cout << "error:  A hullfile not given. eg hullfile=index.txt" << endl;
00170 
00171   if (hullfileinvalid||pointsfileinvalid)
00172     return;
00173 
00174   typedef point2<double> pt2;
00175   vector< pt2 > pts;
00176   if (!vectorfile::deserialize(pts,pointsfile))
00177   {
00178     cout << "error:  Failed to read a file of 2D points from file.";
00179     cout << pointsfile << endl;
00180     return;
00181   }
00182 
00183   bool randomize(cmd.hastoken("randomize"));
00184   if (randomize)
00185   {
00186     cout << "Randomizing the input points." << endl;
00187 
00188 
00189     quickhull2Drandomized<pt2,double> qh(pts);
00190 
00191     if (qh.boundary.size()==0)
00192     {
00193       cout << "error:  No points on hull found by the quickhull algorithm." << endl;
00194       return;
00195     }
00196 
00197     if (!vectorfile::serialize(hullfile,qh.boundary))
00198     {
00199       cout << "error:  Failed to write the hull." << endl;
00200       return;
00201     }
00202   }
00203   else
00204   {
00205     quickhull2D<pt2,double> qh(pts);
00206 
00207     if (qh.boundary.size()==0)
00208     {
00209       cout << "error:  No points on hull found by the quickhull algorithm." << endl;
00210       return;
00211     }
00212 
00213     if (!vectorfile::serialize(hullfile,qh.boundary))
00214     {
00215       cout << "error:  Failed to write the hull." << endl;
00216       return;
00217     }
00218   }
00219 
00220   cout << "Successfully ran the quick hull algorithm." << endl;
00221   cout << endl;
00222 }
00223 
00224 void quickhull2Dtest::test06(int argc, char** arg)
00225 {
00226   glutInit(&argc,arg);
00227   glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
00228   glutInitWindowSize(800,600);
00229   glutCreateWindow("");
00230   glutDisplayFunc(display01);
00231   glutKeyboardFunc(keyboard01);
00232 
00233   OpenGLinitialisation();
00234 
00235   glEnable(GL_DEPTH_TEST);
00236   glEnable(GL_CULL_FACE);
00237   glEnable(GL_NORMALIZE);
00238 
00239   xGraphics.set();
00240 
00241   commandline cmd(argc,arg);
00242 
00243   string pointsfile;
00244   cmd.mapvar(pointsfile,"pointsfile");
00245   string hullfile;
00246   cmd.mapvar(hullfile,"hullfile");
00247 
00248   boolc pointsfileinvalid(pointsfile.empty());
00249   if (pointsfileinvalid)
00250     cout << "error:  A pointsfile was not given. eg pointsfile=data.txt" << endl;
00251   boolc hullfileinvalid(hullfile.empty());
00252   if (hullfileinvalid)
00253     cout << "error:  A hullfile not given. eg hullfile=index.txt" << endl;
00254 
00255   if (hullfileinvalid||pointsfileinvalid)
00256     return;
00257 
00258   typedef point2<double> pt2;
00259   vector< pt2 > pts;
00260   boolc pointsfilereadinvalid=
00261     !vectorfile::deserialize(pts,pointsfile);
00262 
00263   if (pointsfilereadinvalid)
00264     cout << "error:  Failed to read a file of 2D points from file. " << endl;
00265 
00266   vector<uint> boundary;
00267   boolc hullfilereadinvalid=
00268     !vectorfile::deserialize(boundary,hullfile);
00269 
00270 cout << SHOW(boundary.size()) << endl;
00271 
00272   if (hullfilereadinvalid)
00273     cout << "error:  Failed to read hull file as indexes." << endl;
00274 
00275   if (pointsfilereadinvalid||hullfilereadinvalid)
00276     return;
00277 
00278   cout << "success: Read the points and hull indexes." << endl;
00279   cout << endl;
00280 
00281   bool labelpoints(false);
00282   cmd.mapvar(labelpoints,"labelpoints");
00283   bool drawpoints(true);
00284   cmd.mapvar(drawpoints,"drawpoints");
00285 
00286   gobjpush( new gobjglDisable(GL_LIGHTING) );
00287   gobjpush( new gobjglColor3f(0.0,1.0,0.0) );
00288   pointsdisplay2D<pt2> dp(*gobj::global,pts,drawpoints,labelpoints);
00289 
00290   vector< pt2 > v2;
00291   for (uint i=0; i<boundary.size(); ++i)
00292     v2.push_back( pts[boundary[i]] );
00293   gobjpush( new gobjglColor3f(1.0,0.0,0.0) );
00294   pointsdisplay2D<pt2> dp2(*gobj::global,v2,true,false,false);
00295   dp2.gq->radius=0.02;
00296   
00297   zpr zz;
00298 
00299   glutMainLoop();
00300 
00301 }
00302 
00303 

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