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