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