tricall.c
上传用户:yflamps
上传日期:2010-04-01
资源大小:155k
文件大小:10k
源码类别:

3D图形编程

开发平台:

C/C++

  1. /*****************************************************************************/
  2. /*                                                                           */
  3. /*  (tricall.c)                                                              */
  4. /*                                                                           */
  5. /*  Example program that demonstrates how to call Triangle.                  */
  6. /*                                                                           */
  7. /*  Accompanies Triangle Version 1.6                                         */
  8. /*  July 19, 1996                                                            */
  9. /*                                                                           */
  10. /*  This file is placed in the public domain (but the file that it calls     */
  11. /*  is still copyrighted!) by                                                */
  12. /*  Jonathan Richard Shewchuk                                                */
  13. /*  2360 Woolsey #H                                                          */
  14. /*  Berkeley, California  94705-1927                                         */
  15. /*  jrs@cs.berkeley.edu                                                      */
  16. /*                                                                           */
  17. /*****************************************************************************/
  18. /* If SINGLE is defined when triangle.o is compiled, it should also be       */
  19. /*   defined here.  If not, it should not be defined here.                   */
  20. /* #define SINGLE */
  21. #ifdef SINGLE
  22. #define REAL float
  23. #else /* not SINGLE */
  24. #define REAL double
  25. #endif /* not SINGLE */
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include "triangle.h"
  29. /*****************************************************************************/
  30. /*                                                                           */
  31. /*  report()   Print the input or output.                                    */
  32. /*                                                                           */
  33. /*****************************************************************************/
  34. void report(io, markers, reporttriangles, reportneighbors, reportsegments,
  35.             reportedges, reportnorms)
  36. struct triangulateio *io;
  37. int markers;
  38. int reporttriangles;
  39. int reportneighbors;
  40. int reportsegments;
  41. int reportedges;
  42. int reportnorms;
  43. {
  44.   int i, j;
  45.   for (i = 0; i < io->numberofpoints; i++) {
  46.     printf("Point %4d:", i);
  47.     for (j = 0; j < 2; j++) {
  48.       printf("  %.6g", io->pointlist[i * 2 + j]);
  49.     }
  50.     if (io->numberofpointattributes > 0) {
  51.       printf("   attributes");
  52.     }
  53.     for (j = 0; j < io->numberofpointattributes; j++) {
  54.       printf("  %.6g",
  55.              io->pointattributelist[i * io->numberofpointattributes + j]);
  56.     }
  57.     if (markers) {
  58.       printf("   marker %dn", io->pointmarkerlist[i]);
  59.     } else {
  60.       printf("n");
  61.     }
  62.   }
  63.   printf("n");
  64.   if (reporttriangles || reportneighbors) {
  65.     for (i = 0; i < io->numberoftriangles; i++) {
  66.       if (reporttriangles) {
  67.         printf("Triangle %4d points:", i);
  68.         for (j = 0; j < io->numberofcorners; j++) {
  69.           printf("  %4d", io->trianglelist[i * io->numberofcorners + j]);
  70.         }
  71.         if (io->numberoftriangleattributes > 0) {
  72.           printf("   attributes");
  73.         }
  74.         for (j = 0; j < io->numberoftriangleattributes; j++) {
  75.           printf("  %.6g", io->triangleattributelist[i *
  76.                                          io->numberoftriangleattributes + j]);
  77.         }
  78.         printf("n");
  79.       }
  80.       if (reportneighbors) {
  81.         printf("Triangle %4d neighbors:", i);
  82.         for (j = 0; j < 3; j++) {
  83.           printf("  %4d", io->neighborlist[i * 3 + j]);
  84.         }
  85.         printf("n");
  86.       }
  87.     }
  88.     printf("n");
  89.   }
  90.   if (reportsegments) {
  91.     for (i = 0; i < io->numberofsegments; i++) {
  92.       printf("Segment %4d points:", i);
  93.       for (j = 0; j < 2; j++) {
  94.         printf("  %4d", io->segmentlist[i * 2 + j]);
  95.       }
  96.       if (markers) {
  97.         printf("   marker %dn", io->segmentmarkerlist[i]);
  98.       } else {
  99.         printf("n");
  100.       }
  101.     }
  102.     printf("n");
  103.   }
  104.   if (reportedges) {
  105.     for (i = 0; i < io->numberofedges; i++) {
  106.       printf("Edge %4d points:", i);
  107.       for (j = 0; j < 2; j++) {
  108.         printf("  %4d", io->edgelist[i * 2 + j]);
  109.       }
  110.       if (reportnorms && (io->edgelist[i * 2 + 1] == -1)) {
  111.         for (j = 0; j < 2; j++) {
  112.           printf("  %.6g", io->normlist[i * 2 + j]);
  113.         }
  114.       }
  115.       if (markers) {
  116.         printf("   marker %dn", io->edgemarkerlist[i]);
  117.       } else {
  118.         printf("n");
  119.       }
  120.     }
  121.     printf("n");
  122.   }
  123. }
  124. /*****************************************************************************/
  125. /*                                                                           */
  126. /*  main()   Create and refine a mesh.                                       */
  127. /*                                                                           */
  128. /*****************************************************************************/
  129. int main()
  130. {
  131.   struct triangulateio in, mid, out, vorout;
  132.   /* Define input points. */
  133.   in.numberofpoints = 4;
  134.   in.numberofpointattributes = 1;
  135.   in.pointlist = (REAL *) malloc(in.numberofpoints * 2 * sizeof(REAL));
  136.   in.pointlist[0] = 0.0;
  137.   in.pointlist[1] = 0.0;
  138.   in.pointlist[2] = 1.0;
  139.   in.pointlist[3] = 0.0;
  140.   in.pointlist[4] = 1.0;
  141.   in.pointlist[5] = 10.0;
  142.   in.pointlist[6] = 0.0;
  143.   in.pointlist[7] = 10.0;
  144.   in.pointattributelist = (REAL *) malloc(in.numberofpoints *
  145.                                           in.numberofpointattributes *
  146.                                           sizeof(REAL));
  147.   in.pointattributelist[0] = 0.0;
  148.   in.pointattributelist[1] = 1.0;
  149.   in.pointattributelist[2] = 11.0;
  150.   in.pointattributelist[3] = 10.0;
  151.   in.pointmarkerlist = (int *) malloc(in.numberofpoints * sizeof(int));
  152.   in.pointmarkerlist[0] = 0;
  153.   in.pointmarkerlist[1] = 2;
  154.   in.pointmarkerlist[2] = 0;
  155.   in.pointmarkerlist[3] = 0;
  156.   in.numberofsegments = 0;
  157.   in.numberofholes = 0;
  158.   in.numberofregions = 1;
  159.   in.regionlist = (REAL *) malloc(in.numberofregions * 4 * sizeof(REAL));
  160.   in.regionlist[0] = 0.5;
  161.   in.regionlist[1] = 5.0;
  162.   in.regionlist[2] = 7.0;            /* Regional attribute (for whole mesh). */
  163.   in.regionlist[3] = 0.1;          /* Area constraint that will not be used. */
  164.   printf("Input point set:nn");
  165.   report(&in, 1, 0, 0, 0, 0, 0);
  166.   /* Make necessary initializations so that Triangle can return a */
  167.   /*   triangulation in `mid' and a voronoi diagram in `vorout'.  */
  168.   mid.pointlist = (REAL *) NULL;            /* Not needed if -N switch used. */
  169.   /* Not needed if -N switch used or number of point attributes is zero: */
  170.   mid.pointattributelist = (REAL *) NULL;
  171.   mid.pointmarkerlist = (int *) NULL; /* Not needed if -N or -B switch used. */
  172.   mid.trianglelist = (int *) NULL;          /* Not needed if -E switch used. */
  173.   /* Not needed if -E switch used or number of triangle attributes is zero: */
  174.   mid.triangleattributelist = (REAL *) NULL;
  175.   mid.neighborlist = (int *) NULL;         /* Needed only if -n switch used. */
  176.   /* Needed only if segments are output (-p or -c) and -P not used: */
  177.   mid.segmentlist = (int *) NULL;
  178.   /* Needed only if segments are output (-p or -c) and -P and -B not used: */
  179.   mid.segmentmarkerlist = (int *) NULL;
  180.   mid.edgelist = (int *) NULL;             /* Needed only if -e switch used. */
  181.   mid.edgemarkerlist = (int *) NULL;   /* Needed if -e used and -B not used. */
  182.   vorout.pointlist = (REAL *) NULL;        /* Needed only if -v switch used. */
  183.   /* Needed only if -v switch used and number of attributes is not zero: */
  184.   vorout.pointattributelist = (REAL *) NULL;
  185.   vorout.edgelist = (int *) NULL;          /* Needed only if -v switch used. */
  186.   vorout.normlist = (REAL *) NULL;         /* Needed only if -v switch used. */
  187.   /* Triangulate the points.  Switches are chosen to read and write a  */
  188.   /*   PSLG (p), preserve the convex hull (c), number everything from  */
  189.   /*   zero (z), assign a regional attribute to each element (A), and  */
  190.   /*   produce an edge list (e), a Voronoi diagram (v), and a triangle */
  191.   /*   neighbor list (n).                                              */
  192.   triangulate("pczAevn", &in, &mid, &vorout);
  193.   printf("Initial triangulation:nn");
  194.   report(&mid, 1, 1, 1, 1, 1, 0);
  195.   printf("Initial Voronoi diagram:nn");
  196.   report(&vorout, 0, 0, 0, 0, 1, 1);
  197.   /* Attach area constraints to the triangles in preparation for */
  198.   /*   refining the triangulation.                               */
  199.   /* Needed only if -r and -a switches used: */
  200.   mid.trianglearealist = (REAL *) malloc(mid.numberoftriangles * sizeof(REAL));
  201.   mid.trianglearealist[0] = 3.0;
  202.   mid.trianglearealist[1] = 1.0;
  203.   /* Make necessary initializations so that Triangle can return a */
  204.   /*   triangulation in `out'.                                    */
  205.   out.pointlist = (REAL *) NULL;            /* Not needed if -N switch used. */
  206.   /* Not needed if -N switch used or number of attributes is zero: */
  207.   out.pointattributelist = (REAL *) NULL;
  208.   out.trianglelist = (int *) NULL;          /* Not needed if -E switch used. */
  209.   /* Not needed if -E switch used or number of triangle attributes is zero: */
  210.   out.triangleattributelist = (REAL *) NULL;
  211.   /* Refine the triangulation according to the attached */
  212.   /*   triangle area constraints.                       */
  213.   triangulate("prazBP", &mid, &out, (struct triangulateio *) NULL);
  214.   printf("Refined triangulation:nn");
  215.   report(&out, 0, 1, 0, 0, 0, 0);
  216.   /* Free all allocated arrays, including those allocated by Triangle. */
  217.   free(in.pointlist);
  218.   free(in.pointattributelist);
  219.   free(in.pointmarkerlist);
  220.   free(in.regionlist);
  221.   free(mid.pointlist);
  222.   free(mid.pointattributelist);
  223.   free(mid.pointmarkerlist);
  224.   free(mid.trianglelist);
  225.   free(mid.triangleattributelist);
  226.   free(mid.trianglearealist);
  227.   free(mid.neighborlist);
  228.   free(mid.segmentlist);
  229.   free(mid.segmentmarkerlist);
  230.   free(mid.edgelist);
  231.   free(mid.edgemarkerlist);
  232.   free(vorout.pointlist);
  233.   free(vorout.pointattributelist);
  234.   free(vorout.edgelist);
  235.   free(vorout.normlist);
  236.   free(out.pointlist);
  237.   free(out.pointattributelist);
  238.   free(out.trianglelist);
  239.   free(out.triangleattributelist);
  240.   return 0;
  241. }