00001 
00002 
00003 
00004 
00005 
00006 
00007 #include <stdio.h>
00008 #include <stdlib.h>
00009 #include "cg3d.h"
00010 
00011 using namespace cg3d;
00012 
00014 double randFloat(void) {
00015     return (rand()&0xffFF)*(1.0/(float)0xffFF);
00016 }
00017 
00019 CkVector3d randPoint(void) { 
00020     return CkVector3d(randFloat(),randFloat(),randFloat()); 
00021 }
00022 
00025 CkVector3d randPlane(void) { 
00026     CkVector3d planeOrigin(0,0,0);
00027 #if 0 // Roundoff-friendly perfectly flat plane
00028     CkVector3d planeX(1,0,0); 
00029     CkVector3d planeY(0,1,0);
00030 #else //Roundoff-unfriendly skew plane
00031     CkVector3d planeX(0.7,0.2,0.1);
00032     CkVector3d planeY(0.3,0.8,-0.2);
00033 #endif
00034     return planeOrigin
00035         +randFloat()*planeX +randFloat()*planeY; 
00036 }
00037 
00038 const int tetTypes=6; 
00039 
00040 
00041 inline Tet3d randTet(int tetType,PointSet3d *ps) {
00042     CkVector3d A,B,C,D;
00043     do {
00044         A=randPoint(); B=randPoint(); C=randPoint();
00045         switch (tetType) 
00046         { 
00047         case 0:
00048             break;
00049         case 1:
00050             A=randPlane(); B=randPlane(); C=randPlane(); break;
00051         case 2:
00052             A=randPlane(); B=randPlane();break;
00053         case 3:
00054             A=CkVector3d(0.123,0.234,0.456); 
00055         case 4:
00056             B=CkVector3d(0.987,0.876,0.765); 
00057         case 5:
00058             C=CkVector3d(0.654,0.543,0.432); break;
00059         };
00060         D=randPoint();
00061     } while (tetVolume(A,B,C,D)<1.0e-3);
00062     Tet3d t(ps,A,B,C,D); testShape(t); return t;
00063     
00064 }
00065 
00066 
00067 void doTest(int firstTest,int lastTest) {
00068     printf("Running random tests from %d to %d\n",firstTest,lastTest);
00069     int nZero=0, nBig=0;
00070     for (int testNo=firstTest;testNo<lastTest;testNo++) {
00071         if (testNo%1024==0) {
00072             printf("."); 
00073             printf("to %d: (%d zero, %d big)\n",testNo,nZero,nBig);
00074             nZero=0; nBig=0; 
00075             fflush(stdout);
00076         }
00077         srand(testNo); 
00078         
00079         int tetType=(int)(0.999*randFloat()*tetTypes); 
00080         PointSet3d ps;
00081         Tet3d A(randTet(tetType,&ps));
00082         Tet3d B(randTet(tetType,&ps));
00083         
00084         double volume=intersectDebug(&ps,A,B);
00085         if (volume==-1) abort(); 
00086         if (volume<=0) nZero++;
00087         if (volume>1.0e-3) nBig++;
00088     }
00089     printf("All tests from %d to %d passed\n",firstTest,lastTest);
00090 }
00091 
00092 
00093 int main(int argc,char *argv[])
00094 {
00095     int firstTest=0, lastTest=2000000000;
00096     if (argc>1) firstTest=atoi(argv[1]);
00097     if (argc>2) lastTest=atoi(argv[2]);
00098     doTest(firstTest,lastTest);
00099 
00100     return 0;
00101 }
00102 
00103