Files Classes Functions Hierarchy
00001 00002 #include <commandline.h> 00003 #include <gobj.h> 00004 #include <graphmisc.h> 00005 #include <partitionequ.h> 00006 #include <partitionstest.h> 00007 #include <point.h> 00008 #include <random.h> 00009 #include <tetrahedronpartition.h> 00010 #include <trianglepartition.h> 00011 #include <typedefs.h> 00012 #include <zpr.h> 00013 00014 00015 partitionstest* partitionstest::global=0; 00016 00017 00018 template<> 00019 double zero<double>::val = 1E-15; 00020 00021 00022 void partitionstest::keyboard01 00023 ( 00024 unsigned char key, 00025 int x, 00026 int y 00027 ) 00028 { 00029 switch (key) 00030 { 00031 case 27: exit(0); break; 00032 } 00033 } 00034 00035 void partitionstest::display01() 00036 { 00037 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 00038 00039 assert(global!=0); 00040 00041 glColor3f(1.0,0.0,0.0); 00042 glBegin(GL_POINTS); 00043 uint sz=global->red.size(); 00044 for (uint i=0; i<sz; ++i) 00045 glVertex2f(global->vec[global->red[i]].x,global->vec[global->red[i]].y); 00046 glEnd(); 00047 00048 glColor3f(0.0,0.0,1.0); 00049 glBegin(GL_POINTS); 00050 sz=global->blue.size(); 00051 for (uint i=0; i<sz; ++i) 00052 glVertex2f(global->vec[global->blue[i]].x,global->vec[global->blue[i]].y); 00053 glEnd(); 00054 00055 glerrordisplay(); 00056 00057 glutSwapBuffers(); 00058 } 00059 00060 void partitionstest::reshape(GLsizei w1, GLsizei h1) 00061 { 00062 glViewport(0,0,w1,h1); 00063 00064 assert(global!=0); 00065 00066 double rx = (double)(global->width) / (double)w1; 00067 double ry = (double)(global->height) / (double)h1; 00068 00069 for (uint i=0; i<global->vec.size(); ++i) 00070 { 00071 global->vec[i].x *= rx; 00072 global->vec[i].y *= ry; 00073 } 00074 00075 global->width = w1; 00076 global->height = h1; 00077 } 00078 00079 partitionstest::partitionstest(int argc, char** argv) 00080 { 00081 glutInit(&argc, argv); 00082 glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); 00083 00084 width=600; 00085 height=600; 00086 glutInitWindowSize(width,height); 00087 glutCreateWindow(""); 00088 00089 glutDisplayFunc(display01); 00090 glutMouseFunc(mousefunc01); 00091 glutReshapeFunc(reshape); 00092 glutKeyboardFunc(keyboard01); 00093 00094 glClearColor(1.0,1.0,1.0,0.0); 00095 00096 glColor3d(0.0,0.0,0.0); 00097 glPointSize(3.0); 00098 00099 glMatrixMode(GL_PROJECTION); 00100 glLoadIdentity(); 00101 gluOrtho2D(0.0,1.0,0.0,1.0); 00102 00103 set(); 00104 } 00105 00106 void partitionstest::addpoint(intc x, intc y) 00107 { 00108 pt2 pos; 00109 pos.x = x/(double)width; 00110 pos.y = 1.0-y/(double)height; 00111 00112 uintc k=vec.size(); 00113 vec.push_back( pos ); 00114 00115 assert(ps!=0); 00116 00117 if (ps->isInside(pos)) 00118 red.push_back(k); 00119 else 00120 blue.push_back(k); 00121 00122 glutPostRedisplay(); 00123 } 00124 00125 void partitionstest::addstar 00126 ( 00127 intc k, 00128 intc x, 00129 intc y 00130 ) 00131 { 00132 addpoint(x,y); 00133 addpoint(x+k,y); 00134 addpoint(x-k,y); 00135 addpoint(x,y+k); 00136 addpoint(x+k,y+k); 00137 addpoint(x-k,y+k); 00138 addpoint(x,y-k); 00139 addpoint(x+k,y-k); 00140 addpoint(x-k,y-k); 00141 } 00142 00143 void partitionstest::addstarbig 00144 ( 00145 intc x, 00146 intc y 00147 ) 00148 { 00149 // Incrementing in Fibonacci pattern, no particular reason. 00150 // eg 6 = 3 + F[2], 11 = 6 + F[3], 00151 // 19 = 11 + F[4], 32 = 19 + F[5], ... 00152 addstar(1,x,y); 00153 addstar(3,x,y); 00154 addstar(6,x,y); 00155 addstar(11,x,y); 00156 addstar(19,x,y); 00157 addstar(32,x,y); 00158 addstar(53,x,y); 00159 } 00160 00161 00162 00163 00164 void partitionstest::mousefunc01 00165 ( 00166 int button, 00167 int state, 00168 int x, 00169 int y 00170 ) 00171 { 00172 assert(global!=0); 00173 00174 if (button==GLUT_LEFT_BUTTON && state==GLUT_DOWN) 00175 { 00176 global->addstarbig(x,y); 00177 } 00178 } 00179 00180 void partitionstest::test01() 00181 { 00182 pt2 a(0.0,0.0); 00183 pt2 b(0.5,1.0); 00184 ps = new halfspaceD2<pt2,double>(a,b); 00185 00186 glutPostRedisplay(); 00187 glutMainLoop(); 00188 } 00189 00190 00191 void partitionstest::test02() 00192 { 00193 pt2 a(0.3,0.3); 00194 pt2 b(0.4,0.8); 00195 pt2 c(0.7,0.2); 00196 ps = new trianglepartition< pt2, double >(a,c,b); 00197 00198 glutPostRedisplay(); 00199 glutMainLoop(); 00200 } 00201 00202 void partitionstest::test03() 00203 { 00204 halfspaceD2< pt2, double > L[4]; 00205 00206 L[0].set( pt2(0.4,0.8), pt2(0.3,0.3) ); 00207 L[1].set( pt2(0.3,0.3), pt2(0.7,0.2) ); 00208 L[2].set( pt2(0.7,0.2), pt2(0.4,0.5) ); 00209 L[3].set( pt2(0.4,0.5), pt2(0.4,0.8) ); 00210 00211 PeqCapture(ps,Peq(L[0])*Peq(L[1])*(Peq(L[2])+Peq(L[3]))); 00212 00213 glutPostRedisplay(); 00214 glutMainLoop(); 00215 } 00216 00217 void partitionstest::test04() 00218 { 00219 halfspaceD2< pt2, double > L[4]; 00220 00221 pt2 A(0.4,0.8); 00222 pt2 B(0.4,0.2); 00223 pt2 C(0.9,0.2); 00224 pt2 D(0.9,0.8); 00225 00226 L[0].set(A,B); 00227 L[1].set(B,C); 00228 L[2].set(C,D); 00229 L[3].set(D,A); 00230 00231 PeqCapture(ps,!(Peq(L[0])*Peq(L[1])*Peq(L[2])*Peq(L[3]))); 00232 00233 glutPostRedisplay(); 00234 glutMainLoop(); 00235 } 00236 00237 class circlepartition : public partitionspace<partitionstest::pt2> 00238 { 00239 public: 00240 00241 partitionstest::pt2 center; 00242 double radius; 00243 00244 circlepartition( partitionstest::pt2 const & _center, doublec _radius ) 00245 : center(_center), radius(_radius) {} 00246 00247 boolc isInside( partitionstest::pt2 const & x ) const 00248 { 00249 partitionstest::pt2 x2 = x-center; 00250 return x2.x*x2.x+x2.y*x2.y<radius*radius; 00251 } 00252 }; 00253 00254 00255 partitionstest::partitionstest() 00256 { 00257 } 00258 00259 void partitionstest::display02() 00260 { 00261 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 00262 00263 gobj::global->draw(); 00264 00265 glerrordisplay(); 00266 00267 glutSwapBuffers(); 00268 } 00269 00270 00271 void partitionstest::test05() 00272 { 00273 00274 halfspaceD2< pt2, double > L[4]; 00275 00276 pt2 A(0.4,0.8); 00277 pt2 B(0.4,0.2); 00278 pt2 C(0.9,0.2); 00279 pt2 D(0.9,0.8); 00280 00281 L[0].set(A,B); 00282 L[1].set(B,C); 00283 L[2].set(C,D); 00284 L[3].set(D,A); 00285 00286 circlepartition cir( pt2(0.5,0.4), 0.3 ); 00287 PeqCapture(ps,Peq(L[0])*Peq(L[1])*Peq(L[2])*Peq(L[3])-Peq(cir)); 00288 00289 glutPostRedisplay(); 00290 glutMainLoop(); 00291 } 00292 00293 00294 void partitionstest::test06(int argc, char** argv) 00295 { 00296 glutInit(&argc, argv); 00297 glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); 00298 00299 width=600; 00300 height=600; 00301 glutInitWindowSize(width,height); 00302 glutCreateWindow(""); 00303 glutDisplayFunc(display02); 00304 glutKeyboardFunc(keyboard01); 00305 00306 zpr zz; 00307 OpenGLinitialisation(); 00308 00309 xGraphics.set(); 00310 00311 00312 unsigned int numPoints=100000; 00313 00314 cout << "Testing tetrahedron" << endl; 00315 cout << "You can define the tetrhedron" << endl; 00316 cout << "eg" << endl; 00317 cout << "$./main a0=0.5 a1=0.0 a2=0.866 b0=-0.5 b1=0.0 b2=0.866 c0=-1.0 c1=1.0 c2=0.0 d0=0.0 d1=1.0 d2=0.0" << endl; 00318 cout << "Points are scattered about a unit cube at the origin." << endl; 00319 cout << "scale=2.5 Scale the cube." << endl; 00320 cout << "numPoints=" << numPoints << " Change the number of points." << endl; 00321 cout << "green=false Turn of green points." << endl; 00322 cout << endl; 00323 00324 commandline cmd(argc,argv); 00325 double a0=-0.5; 00326 double a1=0.0; 00327 double a2=0.866; 00328 00329 double b0=-0.5; 00330 double b1=0.0; 00331 double b2=-0.866; 00332 00333 double c0=1.0; 00334 double c1=0.0; 00335 double c2=0.0; 00336 00337 double d0=0.0; 00338 double d1=0.85556; 00339 double d2=0.0; 00340 00341 cmd.mapvar(numPoints,"numPoints"); 00342 00343 cmd.mapvar(a0,"a0"); 00344 cmd.mapvar(a1,"a1"); 00345 cmd.mapvar(a2,"a2"); 00346 cmd.mapvar(b0,"b0"); 00347 cmd.mapvar(b1,"b1"); 00348 cmd.mapvar(b2,"b2"); 00349 cmd.mapvar(c0,"c0"); 00350 cmd.mapvar(c1,"c1"); 00351 cmd.mapvar(c2,"c2"); 00352 cmd.mapvar(d0,"d0"); 00353 cmd.mapvar(d1,"d1"); 00354 cmd.mapvar(d2,"d2"); 00355 00356 bool green = false; 00357 cmd.mapvar(green,"green"); 00358 00359 double scale=2.0; 00360 cmd.mapvar(scale,"scale"); 00361 00362 pt3 p0(a0,a1,a2); 00363 pt3 p1(b0,b1,b2); 00364 pt3 p2(c0,c1,c2); 00365 pt3 p3(d0,d1,d2); 00366 00367 tetrahedronpartition< pt3, double > tet(p0,p2,p1,p3); 00368 00369 pt3 colr1(1.0,0.0,0.0); 00370 pt3 colr2(0.0,1.0,0.0); 00371 00372 random11<double> r; 00373 00374 pt3 X; 00375 00376 xGraphics.push( new gobjglDisable(GL_LIGHTING) ); 00377 00378 xGraphics.push( new gobjglBegin(GL_POINTS) ); 00379 00380 for (uint i=0; i<numPoints; ++i) 00381 { 00382 X = pt3(scale*(r()-0.5),scale*(r()-0.5),scale*(r()-0.5)); 00383 00384 if (tet.isInside(X)) 00385 { 00386 xGraphics.push( new gobjglColor3f(colr1.x,colr1.y,colr1.z) ); 00387 xGraphics.push( new gobjglVertex3f(X.x,X.y,X.z) ); 00388 } 00389 else 00390 { 00391 if (green) 00392 { 00393 xGraphics.push( new gobjglColor3f(colr2.x,colr2.y,colr2.z) ); 00394 xGraphics.push( new gobjglVertex3f(X.x,X.y,X.z) ); 00395 } 00396 } 00397 00398 } 00399 00400 xGraphics.push( new gobjglEnd() ); 00401 00402 zz.update(); 00403 00404 glutMainLoop(); 00405 } 00406
1.5.8