1 /*****************************************************************************/
5 /* Example program that demonstrates how to call Triangle. */
7 /* Accompanies Triangle Version 1.3 */
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 */
19 /*****************************************************************************/
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. */
28 #else /* not SINGLE */
30 #endif /* not SINGLE */
36 extern void *malloc();
38 #endif /* _STDLIB_H_ */
40 /*****************************************************************************/
42 /* report() Print the input or output. */
44 /*****************************************************************************/
46 void report(io, markers, reporttriangles, reportneighbors, reportsegments,
47 reportedges, reportnorms)
48 struct triangulateio *io;
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]);
63 if (io->numberofpointattributes > 0) {
64 printf(" attributes");
66 for (j = 0; j < io->numberofpointattributes; j++) {
68 io->pointattributelist[i * io->numberofpointattributes + j]);
71 printf(" marker %d\n", io->pointmarkerlist[i]);
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]);
85 if (io->numberoftriangleattributes > 0) {
86 printf(" attributes");
88 for (j = 0; j < io->numberoftriangleattributes; j++) {
89 printf(" %.6g", io->triangleattributelist[i *
90 io->numberoftriangleattributes + j]);
94 if (reportneighbors) {
95 printf("Triangle %4d neighbors:", i);
96 for (j = 0; j < 3; j++) {
97 printf(" %4d", io->neighborlist[i * 3 + j]);
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]);
112 printf(" marker %d\n", io->segmentmarkerlist[i]);
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]);
126 if (reportnorms && (io->edgelist[i * 2 + 1] == -1)) {
127 for (j = 0; j < 2; j++) {
128 printf(" %.6g", io->normlist[i * 2 + j]);
132 printf(" marker %d\n", io->edgemarkerlist[i]);
141 /*****************************************************************************/
143 /* main() Create and refine a mesh. */
145 /*****************************************************************************/
149 struct triangulateio in, mid, out, vorout;
151 /* Define input points. */
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 *
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;
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. */
186 printf("Input point set:\n\n");
187 report(&in, 1, 0, 0, 0, 0, 0);
189 /* Make necessary initializations so that Triangle can return a */
190 /* triangulation in `mid' and a voronoi diagram in `vorout'. */
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. */
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. */
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). */
219 triangulate("pczAevn", &in, &mid, &vorout);
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);
226 /* Attach area constraints to the triangles in preparation for */
227 /* refining the triangulation. */
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;
234 /* Make necessary initializations so that Triangle can return a */
235 /* triangulation in `out'. */
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;
244 /* Refine the triangulation according to the attached */
245 /* triangle area constraints. */
247 triangulate("prazBP", &mid, &out, (struct triangulateio *) NULL);
249 printf("Refined triangulation:\n\n");
250 report(&out, 0, 1, 0, 0, 0, 0);
252 /* Free all allocated arrays, including those allocated by Triangle. */
255 free(in.pointattributelist);
256 free(in.pointmarkerlist);
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);
268 free(mid.edgemarkerlist);
269 free(vorout.pointlist);
270 free(vorout.pointattributelist);
271 free(vorout.edgelist);
272 free(vorout.normlist);
274 free(out.pointattributelist);
275 free(out.trianglelist);
276 free(out.triangleattributelist);