home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / programming / source / graphicgems4.lha / GemsIV / delaunay / test.C < prev   
Encoding:
C/C++ Source or Header  |  1995-02-06  |  3.3 KB  |  189 lines

  1. /* TEST PROGRAM FOR DELAUNAY */
  2.  
  3. #include <stdlib.h>
  4. #include <stream.h>
  5. #include <string.h>
  6. #include <gl.h>
  7. #include <device.h>
  8. #include <quadedge.h>
  9.  
  10. void getArguments(int, char**);
  11. void InsertPoints(Subdivision&);
  12. void display(Subdivision&, Real, Real, Real, Real);
  13.  
  14. char *program;
  15. int num = 20;
  16.  
  17. main(int argc, char** argv)
  18. {
  19.     getArguments(argc, argv);
  20.  
  21.     // Construct a triangle containing the unit square:
  22.     Point2d p1(-1,-1), p2(2,-1), p3(0.5,3);
  23.     Subdivision mesh(p1, p2, p3);
  24.  
  25.     InsertPoints(mesh);
  26.     display(mesh, -.1, -.1, 1.1, 1.1);
  27.  
  28.     exit(0);
  29. }
  30.  
  31. static void usage()
  32. {
  33.     cerr << "usage: " << program << " [ -n number_of_points ]\n";
  34. }
  35.  
  36. static void errmsg(char *msg)
  37. {
  38.     cerr << program << ": " << msg << endl;
  39.     usage();
  40.     exit(1);
  41. }
  42.  
  43. void getArguments(int argc, char** argv)
  44. {
  45.     program = argv[0];
  46.  
  47.     if(argc == 2 && strcmp(argv[1], "-h") == 0) {
  48.         usage();
  49.         exit(0);
  50.     }
  51.  
  52.     for (int i = 1; i < argc && argv[i][0] == '-'; i++)
  53.         if (strcmp(argv[i], "-n") == 0) {
  54.             if(++i < argc)
  55.                 num = atoi(argv[i]);
  56.             else
  57.                 errmsg("option ``-n'': missing parameter");
  58.         } else
  59.             errmsg("unknown option");
  60.  
  61.     if(argc - i > 0)
  62.         errmsg("too many parameters");
  63. }
  64.  
  65. void InsertPoints(Subdivision& mesh)
  66. {
  67.     for (int i = 0; i < num; i++) {
  68.         double u = drand48();
  69.         double v = drand48();
  70.         mesh.InsertSite(Point2d(u,v));
  71.     }
  72. }
  73.  
  74. long createWindow()
  75. {
  76.     prefsize(512, 512);
  77.     long w = winopen("Delaunay Triangulation");
  78.     doublebuffer();
  79.     RGBmode();
  80.     gconfig();
  81.     return w;
  82. }
  83.  
  84. void draw(Subdivision& mesh)
  85. {
  86.     cpack(0);
  87.     clear();
  88.     cpack(0xffffffff);
  89.  
  90.     /* draw your stuff here */
  91.     mesh.Draw();
  92.  
  93.     swapbuffers();
  94. }
  95.  
  96. void setView(float cx, float cy, float zoom, float xsize, float ysize)
  97. {
  98.     float dx = 0.5 * xsize / zoom;
  99.     float dy = 0.5 * ysize / zoom;
  100.  
  101.     ortho2(cx - dx, cx + dx, cy - dy, cy + dy);
  102. }
  103.  
  104. void printHelp()
  105. {
  106.     cerr << "<left>       insert a new point\n"
  107.          << "  <h>     print help message\n"
  108.          << "  <q>     quit\n";
  109. }
  110.  
  111. void getMouse (long,
  112.         float cx, float cy, float zoom,
  113.         float xsize, float ysize,
  114.         float& x, float& y)
  115. {
  116.     long sx, sy, ox, oy, sxsize, sysize;
  117.     Coord p[3], v[3];
  118.  
  119.     sx = getvaluator(MOUSEX);
  120.     sy = getvaluator(MOUSEY);
  121.     getorigin(&ox, &oy);
  122.     sx -= ox;
  123.     sy -= oy;
  124.     getsize(&sxsize, &sysize);
  125.     xsize /= zoom;
  126.     ysize /= zoom;
  127.     x = (float(sx) / float(sxsize)) * xsize - 0.5 * xsize + cx;
  128.     y = (float(sy) / float(sysize)) * ysize - 0.5 * ysize + cy;
  129. }
  130.  
  131. void display(Subdivision& mesh, Real left, Real bottom, Real right, Real top)
  132. {
  133.     long wid;
  134.     float cx, cy, zoom, xsize, ysize;
  135.  
  136.     wid = createWindow();
  137.  
  138.     cx = 0.5 * (left + right);
  139.     cy = 0.5 * (bottom + top);
  140.     zoom = 1;
  141.     xsize = 1.1 * (right - left);
  142.     ysize = 1.1 * (top - bottom);
  143.  
  144.     setView(cx, cy, zoom, xsize, ysize);
  145.  
  146.     qdevice(LEFTMOUSE);
  147.     qdevice(HKEY);
  148.     qdevice(QKEY);
  149.  
  150.     qreset();
  151.     qenter(REDRAW, (short)wid);
  152.  
  153.     for (int done = FALSE; !done; ) {
  154.         short val;
  155.  
  156.         switch(qread(&val)) {
  157.         case REDRAW:
  158.             winset(wid);
  159.             reshapeviewport();
  160.             setView(cx, cy, zoom, xsize, ysize);
  161.             draw(mesh);
  162.             break;
  163.         case LEFTMOUSE:
  164.             if(val) {
  165.                 float x, y;
  166.                 getMouse(wid, cx, cy, zoom, xsize, ysize, x, y);
  167.                 cerr << "Mouse at (" << x << "," << y << ")\n";
  168.                 x = (x < 0) ? 0 : (x > 1) ? 1 : x;
  169.                 y = (y < 0) ? 0 : (y > 1) ? 1 : y;
  170.                 mesh.InsertSite(Point2d(x, y));
  171.                 qreset();
  172.                 draw(mesh);
  173.             }
  174.             break;
  175.         case HKEY:
  176.             if(val)
  177.                 printHelp();
  178.             break;
  179.         case QKEY:
  180.             if(val)
  181.                 done = TRUE;
  182.             break;
  183.         }
  184.     }
  185.  
  186.     winclose(wid);
  187.     gexit();
  188. }
  189.