proj home

Files   Classes   Functions   Hierarchy  

pathlinesegtest.cpp

Go to the documentation of this file.
00001 #include <iomanip>
00002 using namespace std;
00003 
00004 #include <commandline.h>
00005 #include <graphmisc.h>
00006 #include <minexpdim.h>
00007 #include <pathlineseg.h>
00008 #include <pathlinesegdraw.h>
00009 #include <pathlinesegtest.h>
00010 #include <print.h>
00011 #include <stringserialization.h>
00012 #include <zpr.h>
00013 
00014 pathlinesegtest* pathlinesegtest::self=0;
00015 
00016 string pathlinesegtest::doc[] = 
00017 {
00018   "",
00019   "Display pathlineseg by reading a file.",
00020   "Read a pathlineseg from a string.",
00021   "Test equivalent dist01 and pathlinesegvec() function.",
00022   "Minimize a path and optionally save: file2=\"somefile.txt\"."
00023 };
00024 
00025 pathlinesegtest::pathlinesegtest(int argc, char** argv)
00026 {
00027   glutInit(&argc,argv);
00028   glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
00029   glutInitWindowSize(600,600);
00030   glutCreateWindow("");
00031   glutDisplayFunc(display01);
00032   glutKeyboardFunc(keyboard01);
00033 
00034   OpenGLinitialisation();
00035 
00036   glEnable(GL_DEPTH_TEST);
00037   glEnable(GL_CULL_FACE);
00038   glEnable(GL_NORMALIZE);
00039 
00040   xGraphics.set();
00041 }
00042 
00043 void pathlinesegtest::test01(int argc, char** argv)
00044 {
00045   cout << "Display pathlineseg. file=\"somefile.txt\"" << "\n"; 
00046   cout << "  optional: pointsdraw, pointsnumber, pointsnumber, " << "\n";
00047   cout << "  curvepointsdraw, controldraw, segdraw." << "\n";
00048 
00049   assert(self==0);
00050   self = new pathlinesegtest(argc,argv);
00051 
00052   gobjpush(new myaxes(1.0));
00053 
00054   // Read segments file in and graphically display.
00055 
00056   string cmdarg("./main prog=30 file=path001.txt");
00057 
00058   commandline cmd(argc,argv);
00059   string file="";
00060   cmd.mapvar(file,"file");
00061   if (file.empty())
00062   {
00063     cout << "error: need file eg" << endl;
00064     cout << "  " << cmdarg << endl;
00065     return;
00066   }
00067  
00068   string str;
00069   bool res=filestring::serialize(str,file);
00070   if (res==false)
00071   {
00072     cout << "error: " << cmdarg << endl; 
00073     return;
00074   }
00075   if (str.empty())
00076   { 
00077     cout << "error: empty file input." << endl; return; 
00078   }
00079 
00080   pathlineseg ps;
00081   ps.serializeInverse(str);
00082 
00083   cout << (stringc)ps << endl;
00084 
00085   pathlinesegdraw* psd = new pathlinesegdraw(&ps,false);
00086 
00087   cmd.mapvar(psd->pointsdraw,"pointsdraw");
00088   cmd.mapvar(psd->pointsnumber,"pointsnumber");
00089   cmd.mapvar(psd->curvepointsdraw,"curvepointsdraw");
00090   cmd.mapvar(psd->controldraw,"controldraw");
00091   cmd.mapvar(psd->segsdraw,"segsdraw");
00092   psd->update();
00093   gobjpush(psd);
00094 
00095 //cout << SHOW(res);
00096 // cout << "*" <<  str << "*" << endl;
00097 
00098   zpr zz;
00099   zz.update();
00100   glutMainLoop();
00101 }
00102 
00103 void pathlinesegtest::display01()
00104 {
00105   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00106 
00107   gobj::global->draw();
00108 
00109   glerrordisplay();
00110   
00111   glutSwapBuffers();
00112 }
00113 
00114 void pathlinesegtest::keyboard01(unsigned char key, int x, int y)
00115 {
00116   switch (key)
00117   {
00118     case 27:
00119       exit(0);
00120       break;
00121   }
00122 }
00123 
00124 void pathlinesegtest::test02()
00125 {
00126   pathlineseg ps;
00127 
00128   uint sg[] = { 1,2,2,3,3,4 };
00129   copy(sg,sg+6,back_inserter(ps.segments));
00130 
00131   cout << (stringc)ps << endl;
00132   cout << endl;
00133 
00134   cout << "serializeInverse" << endl;
00135   string s2=
00136 "<path>\
00137 <info>Path line segments</info>\
00138 <points2D>\
00139  0 0\
00140  0.0 0.0\
00141  .2 .3 .4 .7\
00142  1.2 .2\
00143  1.6 .12\
00144 </points2D>\
00145 <control>\
00146 1 2 3 4\
00147 </control>\
00148 <segments>\
00149 1 2 2 3 3 4\
00150 </segments>\
00151 </path>";
00152 
00153   pathlineseg ps2;
00154   ps2.serializeInverse(s2);
00155 
00156   cout << (stringc)ps2 << endl;
00157 }
00158 
00159 int pathlinesegtest::unittest01(int argc, char** argv)
00160 {
00161   commandline cmd(argc,argv);
00162   string file="path002.txt";
00163   cmd.mapvar(file,"file");
00164   if (file.empty())
00165   {
00166     cout << "error: need file in command line eg file=path002.txt" << endl;
00167     return 1;
00168   }
00169 
00170   string str;
00171   assertreturnOS(filestring::serialize(str,file));
00172   assertreturnOS(str.empty()==false);
00173 
00174   pathlineseg ps;
00175   ps.serializeInverse(str);
00176 
00177 cout << (stringc)ps << endl;
00178   double psdist01=ps.dist01();
00179 cout << SHOW(psdist01) << endl;
00180 //cout << SHOW(ps.dist01()) << endl;
00181 
00182 cout << endl;
00183 
00184   pathlinesegvec psf(ps);
00185 
00186   psf.read();
00187   psf.distfn=1;
00188   psf();
00189   cout << printvecfunc(psf.xi,psf.dim+1) << endl;
00190 
00191   // TODO - read a pathlineseg file in
00192   // and print out.
00193   
00194 
00195   double psfdist = psf.fnvalue();
00196   cout << SHOW(psfdist) << endl;
00197 
00198   // Testing that the two distance functions are the same.
00199   assertreturnOS(zero<double>::test(psdist01-psfdist));
00200 
00201   double psdist02=ps.dist02();
00202   psf.distfn=2;
00203   cout << SHOW(psdist02) << endl;
00204   psf();
00205   psfdist=psf.fnvalue();
00206   cout << SHOW(psfdist) << endl;
00207   assertreturnOS(zero<double>::test(psdist02-psfdist));
00208 
00209   // Testing writing back to instance.
00210   for (uint i=0; i<=psf.dim; ++i)
00211   {
00212     psf.xi[i] = 0.5 + i;
00213   }
00214   cout << printvecfunc(psf.xi,psf.dim+1) << endl;
00215   psf.write();
00216 
00217   cout << (stringc)ps << endl;
00218 
00219 
00220   return 0;
00221 }
00222 
00223 
00224 int pathlinesegtest::test03(int argc, char** argv)
00225 {
00226   commandline cmd(argc,argv);
00227   string file="path002.txt";
00228   cmd.mapvar(file,"file");
00229   if (file.empty())
00230   {
00231     cout << "error: need file in command line eg file=path002.txt" << endl;
00232     return 1;
00233   }
00234 
00235   string str;
00236   assertreturnOS(filestring::serialize(str,file));
00237   assertreturnOS(str.empty()==false);
00238 
00239   // Aim: display original and minimized paths.
00240 
00241   // Original 
00242   pathlineseg ps0;
00243   ps0.serializeInverse(str);
00244 
00245   // Path to be minimized.
00246   pathlineseg ps;
00247   ps.serializeInverse(str);
00248 
00249   pathlinesegvec* psf = new pathlinesegvec(ps);
00250   psf->read();
00251 
00252   double h0=0.3;
00253   cmd.mapvar(h0,"h0");
00254   minexpdimN<double> mn(*psf,h0);
00255 
00256   // Change the distance function.
00257   cmd.mapvar(psf->distfn,"distfn");
00258 
00259   mn.reset();
00260 
00261   cout << printvecfunc(psf->xi,psf->dim+1) << endl;
00262 
00263   uint i=0;
00264   uint imax=4;
00265   cmd.mapvar(imax,"imax");
00266   do
00267   {
00268     ++mn;
00269     cout << setw(3) << i << " " << printvecfunc(mn.xi,psf->dim+1) << endl;
00270     //cout << " " << mn.fn->counter << " " << mn.himax() << endl;
00271     ++i;
00272 
00273 
00274   } while ( (i<imax) );
00275 
00276   psf->write();
00277 
00278 
00279   string file2("");
00280   cmd.mapvar(file2,"file2");
00281   if (file2.empty())
00282   { 
00283     cout << (stringc)ps << endl;
00284   }
00285   else
00286   {
00287     assertreturnOS(filestring::deserialize(file2,(stringc)ps));
00288   }
00289 
00290   return 0;
00291 }
00292 
00293 
00294 

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