tritet 3.1.0

Triangle and tetrahedron mesh generators
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
#include "interface_triangle.h"

#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "constants.h"
#include "tricall_report.h"

void zero_triangle_data(struct triangulateio *data) {
    if (data == NULL) {
        return;
    }

    // points
    data->pointlist = NULL;
    data->pointattributelist = NULL;
    data->pointmarkerlist = NULL;
    data->numberofpoints = 0;
    data->numberofpointattributes = 0;

    // triangles
    data->trianglelist = NULL;
    data->triangleattributelist = NULL;
    data->trianglearealist = NULL;
    data->neighborlist = NULL;
    data->numberoftriangles = 0;
    data->numberofcorners = 0;
    data->numberoftriangleattributes = 0;

    // segments
    data->segmentlist = NULL;
    data->segmentmarkerlist = NULL;
    data->numberofsegments = 0;

    // holes
    data->holelist = NULL;
    data->numberofholes = 0;

    // regions
    data->regionlist = NULL;
    data->numberofregions = 0;

    // edges
    data->edgelist = NULL;
    data->edgemarkerlist = NULL;
    data->normlist = NULL;
    data->numberofedges = 0;
}

void free_triangle_data(struct triangulateio *data) {
    if (data == NULL) {
        return;
    }

    // Points
    if (data->pointlist != NULL) {
        free(data->pointlist);
    }
    if (data->pointattributelist != NULL) {
        free(data->pointattributelist);
    }
    if (data->pointmarkerlist != NULL) {
        free(data->pointmarkerlist);
    }

    // Triangles
    if (data->trianglelist != NULL) {
        free(data->trianglelist);
    }
    if (data->triangleattributelist != NULL) {
        free(data->triangleattributelist);
    }
    if (data->trianglearealist != NULL) {
        free(data->trianglearealist);
    }
    if (data->neighborlist != NULL) {
        free(data->neighborlist);
    }

    // Segments
    if (data->segmentlist != NULL) {
        free(data->segmentlist);
    }
    if (data->segmentmarkerlist != NULL) {
        free(data->segmentmarkerlist);
    }

    // Holes
    if (data->holelist != NULL) {
        free(data->holelist);
    }

    // Regions
    if (data->regionlist != NULL) {
        free(data->regionlist);
    }

    // Edges
    if (data->edgelist != NULL) {
        free(data->edgelist);
    }
    if (data->edgemarkerlist != NULL) {
        free(data->edgemarkerlist);
    }
    if (data->normlist != NULL) {
        free(data->normlist);
    }

    zero_triangle_data(data);
}

struct ExtTrigen *tri_new_trigen(int32_t npoint, int32_t nsegment, int32_t nregion, int32_t nhole) {
    if (npoint < 3) {
        return NULL;
    }

    // trigen
    struct ExtTrigen *trigen = (struct ExtTrigen *)malloc(sizeof(struct ExtTrigen));
    if (trigen == NULL) {
        return NULL;
    }
    zero_triangle_data(&trigen->input);
    zero_triangle_data(&trigen->output);
    zero_triangle_data(&trigen->voronoi);

    // points
    trigen->input.pointlist = (double *)malloc(npoint * 2 * sizeof(double));
    if (trigen->input.pointlist == NULL) {
        free(trigen);
        return NULL;
    }
    trigen->input.numberofpoints = npoint;

    // point markers
    trigen->input.pointmarkerlist = (int32_t *)malloc(npoint * sizeof(int32_t));
    if (trigen->input.pointmarkerlist == NULL) {
        free(trigen);
        return NULL;
    }

    // segments
    if (nsegment > 0) {
        trigen->input.segmentlist = (int32_t *)malloc(nsegment * 2 * sizeof(int32_t));
        if (trigen->input.segmentlist == NULL) {
            free_triangle_data(&trigen->input);
            free(trigen);
            return NULL;
        }
        trigen->input.segmentmarkerlist = (int32_t *)malloc(nsegment * sizeof(int32_t));
        if (trigen->input.segmentmarkerlist == NULL) {
            free_triangle_data(&trigen->input);
            free(trigen);
            return NULL;
        }
        trigen->input.numberofsegments = nsegment;
    }

    // regions
    if (nregion > 0) {
        trigen->input.regionlist = (double *)malloc(nregion * 4 * sizeof(double));
        if (trigen->input.regionlist == NULL) {
            free_triangle_data(&trigen->input);
            free(trigen);
            return NULL;
        }
        trigen->input.numberofregions = nregion;
    }

    // holes
    if (nhole > 0) {
        trigen->input.holelist = (double *)malloc(nhole * 2 * sizeof(double));
        if (trigen->input.holelist == NULL) {
            free_triangle_data(&trigen->input);
            free(trigen);
            return NULL;
        }
        trigen->input.numberofholes = nhole;
    }

    return trigen;
}

void tri_drop_trigen(struct ExtTrigen *trigen) {
    if (trigen == NULL) {
        return;
    }
    free_triangle_data(&trigen->input);
    free_triangle_data(&trigen->output);
    free_triangle_data(&trigen->voronoi);
    free(trigen);
}

int32_t tri_set_point(struct ExtTrigen *trigen, int32_t index, int32_t marker, double x, double y) {
    if (trigen == NULL) {
        return TRITET_ERROR_NULL_DATA;
    }
    if (trigen->input.pointlist == NULL) {
        return TRITET_ERROR_NULL_POINT_LIST;
    }
    if (index >= trigen->input.numberofpoints) {
        return TRITET_ERROR_INVALID_POINT_INDEX;
    }
    trigen->input.pointlist[index * 2] = x;
    trigen->input.pointlist[index * 2 + 1] = y;
    trigen->input.pointmarkerlist[index] = marker;
    return TRITET_SUCCESS;
}

int32_t tri_set_segment(struct ExtTrigen *trigen, int32_t index, int32_t marker, int32_t a, int32_t b) {
    if (trigen == NULL) {
        return TRITET_ERROR_NULL_DATA;
    }
    if (trigen->input.segmentlist == NULL || trigen->input.segmentmarkerlist == NULL) {
        return TRITET_ERROR_NULL_SEGMENT_LIST;
    }
    if (index >= trigen->input.numberofsegments) {
        return TRITET_ERROR_INVALID_SEGMENT_INDEX;
    }
    if (a >= trigen->input.numberofpoints || b >= trigen->input.numberofpoints) {
        return TRITET_ERROR_INVALID_SEGMENT_POINT_ID;
    }
    trigen->input.segmentlist[index * 2] = a;
    trigen->input.segmentlist[index * 2 + 1] = b;
    trigen->input.segmentmarkerlist[index] = marker;
    return TRITET_SUCCESS;
}

int32_t tri_set_region(struct ExtTrigen *trigen, int32_t index, int32_t marker, double x, double y, double max_area) {
    // Shewchuk: If you are using the -A and -a switches simultaneously and wish to assign an marker
    // to some region without imposing an area constraint, use a negative maximum area.
    if (trigen == NULL) {
        return TRITET_ERROR_NULL_DATA;
    }
    if (trigen->input.regionlist == NULL) {
        return TRITET_ERROR_NULL_REGION_LIST;
    }
    if (index >= trigen->input.numberofregions) {
        return TRITET_ERROR_INVALID_REGION_INDEX;
    }
    trigen->input.regionlist[index * 4] = x;
    trigen->input.regionlist[index * 4 + 1] = y;
    trigen->input.regionlist[index * 4 + 2] = marker;
    trigen->input.regionlist[index * 4 + 3] = max_area;
    return TRITET_SUCCESS;
}

int32_t tri_set_hole(struct ExtTrigen *trigen, int32_t index, double x, double y) {
    if (trigen == NULL) {
        return TRITET_ERROR_NULL_DATA;
    }
    if (trigen->input.holelist == NULL) {
        return TRITET_ERROR_NULL_HOLE_LIST;
    }
    if (index >= trigen->input.numberofholes) {
        return TRITET_ERROR_INVALID_HOLE_INDEX;
    }
    trigen->input.holelist[index * 2] = x;
    trigen->input.holelist[index * 2 + 1] = y;
    return TRITET_SUCCESS;
}

int32_t tri_run_delaunay(struct ExtTrigen *trigen, int32_t verbose) {
    if (trigen == NULL) {
        return TRITET_ERROR_NULL_DATA;
    }
    if (trigen->input.pointlist == NULL) {
        return TRITET_ERROR_NULL_POINT_LIST;
    }

    // clear previous data
    free_triangle_data(&trigen->output);

    // Triangulate the points
    // Switches:
    // * `z` -- number everything from zero (z)
    char command[10];
    strcpy(command, "z");
    if (verbose == TRITET_FALSE) {
        strcat(command, "Q");
    }
    triangulate(command, &trigen->input, &trigen->output, NULL);

    // After triangulate (with -p switch), output.regionlist gets the content of input.regionlist and
    // output.holelist gets the content of input.holelist. Thus, these output variables must be set
    // to NULL in order to tell free_data to ignore them and avoid a double-free memory issue.
    trigen->output.regionlist = NULL;
    trigen->output.holelist = NULL;

    if (verbose == TRITET_TRUE) {
        report(&trigen->output, 1, 1, 0, 0, 0, 0);
    }
    return TRITET_SUCCESS;
}

int32_t tri_run_voronoi(struct ExtTrigen *trigen, int32_t verbose) {
    if (trigen == NULL) {
        return TRITET_ERROR_NULL_DATA;
    }
    if (trigen->input.pointlist == NULL) {
        return TRITET_ERROR_NULL_POINT_LIST;
    }

    // clear previous data
    free_triangle_data(&trigen->output);

    // Triangulate the points
    // Switches:
    // * `z` -- number everything from zero (z)
    // * `v` -- Voronoi diagram
    char command[10];
    strcpy(command, "zv");
    if (verbose == TRITET_FALSE) {
        strcat(command, "Q");
    }
    triangulate(command, &trigen->input, &trigen->output, &trigen->voronoi);

    // After triangulate (with -p switch), output.regionlist gets the content of input.regionlist and
    // output.holelist gets the content of input.holelist. Thus, these output variables must be set
    // to NULL in order to tell free_data to ignore them and avoid a double-free memory issue.
    trigen->output.regionlist = NULL;
    trigen->output.holelist = NULL;

    if (verbose == TRITET_TRUE) {
        report(&trigen->voronoi, 0, 0, 0, 0, 1, 1);
    }
    return TRITET_SUCCESS;
}

int32_t tri_run_triangulate(struct ExtTrigen *trigen, int32_t verbose, int32_t quadratic, int32_t allow_new_points_on_bry, double global_max_area, double global_min_angle) {
    if (trigen == NULL) {
        return TRITET_ERROR_NULL_DATA;
    }
    if (trigen->input.pointlist == NULL) {
        return TRITET_ERROR_NULL_POINT_LIST;
    }
    if (trigen->input.segmentlist == NULL) {
        return TRITET_ERROR_NULL_SEGMENT_LIST;
    }

    // clear previous data
    free_triangle_data(&trigen->output);

    // Generate mesh
    // Switches:
    // * `p` -- write a PSLG (p)
    // * `z` -- number everything from zero (z)
    // * `A` -- assign a regional attribute to each element (A)
    // * `Q` -- quiet mode
    // * `o2` -- generates second-order elements with six nodes each
    // * `Y` -- prohibits the insertion of Steiner points on the mesh boundary
    char command[128];
    strcpy(command, "pzA");
    if (verbose == TRITET_FALSE) {
        strcat(command, "Q");
    }
    if (quadratic == TRITET_TRUE) {
        strcat(command, "o2");
    }
    if (allow_new_points_on_bry == TRITET_FALSE) {
        strcat(command, "Y");
    }
    if (global_max_area > 0.0) {
        char buf[32];
        int32_t n = snprintf(buf, 32, "a%.15f", global_max_area);
        if (n >= 32) {
            return TRITET_ERROR_STRING_CONCAT;
        }
        strcat(command, buf);
    } else {
        strcat(command, "a");
    }
    if (global_min_angle > 0.0) {
        char buf[32];
        int32_t n = snprintf(buf, 32, "q%.15f", global_min_angle);
        if (n >= 32) {
            return TRITET_ERROR_STRING_CONCAT;
        }
        strcat(command, buf);
    } else {
        strcat(command, "q");
    }
    triangulate(command, &trigen->input, &trigen->output, NULL);

    // After triangulate (with -p switch), output.regionlist gets the content of input.regionlist and
    // output.holelist gets the content of input.holelist. Thus, these output variables must be set
    // to NULL in order to tell free_data to ignore them and avoid a double-free memory issue.
    trigen->output.regionlist = NULL;
    trigen->output.holelist = NULL;

    if (verbose == TRITET_TRUE) {
        report(&trigen->output, 1, 1, 0, 0, 0, 0);
    }
    return TRITET_SUCCESS;
}

int32_t tri_out_npoint(struct ExtTrigen *trigen) {
    if (trigen == NULL) {
        return 0;
    }
    return trigen->output.numberofpoints;
}

int32_t tri_out_nsegment(struct ExtTrigen *trigen) {
    if (trigen == NULL) {
        return 0;
    }
    return trigen->output.numberofsegments;
}

int32_t tri_out_ncell(struct ExtTrigen *trigen) {
    if (trigen == NULL) {
        return 0;
    }
    return trigen->output.numberoftriangles;
}

int32_t tri_out_cell_npoint(struct ExtTrigen *trigen) {
    if (trigen == NULL) {
        return 0;
    }
    return trigen->output.numberofcorners;
}

double tri_out_point(struct ExtTrigen *trigen, int32_t index, int32_t dim) {
    if (trigen == NULL) {
        return 0.0;
    }
    if (index < trigen->output.numberofpoints && (dim == 0 || dim == 1)) {
        return trigen->output.pointlist[index * 2 + dim];
    } else {
        return 0.0;
    }
}

int32_t tri_out_point_marker(struct ExtTrigen *trigen, int32_t index) {
    if (trigen == NULL) {
        return 0;
    }
    if (index < trigen->output.numberofpoints) {
        return trigen->output.pointmarkerlist[index];
    } else {
        return 0;
    }
}

int32_t tri_out_segment_point(struct ExtTrigen *trigen, int32_t index, int32_t side) {
    if (trigen == NULL) {
        return 0;
    }
    if (index < trigen->output.numberofsegments && (side == 0 || side == 1)) {
        return trigen->output.segmentlist[index * 2 + side];
    } else {
        return 0;
    }
}

int32_t tri_out_segment_marker(struct ExtTrigen *trigen, int32_t index) {
    if (trigen == NULL) {
        return 0;
    }
    if (index < trigen->output.numberofsegments) {
        return trigen->output.segmentmarkerlist[index];
    } else {
        return 0;
    }
}

int32_t tri_out_cell_point(struct ExtTrigen *trigen, int32_t index, int32_t corner) {
    if (trigen == NULL) {
        return 0;
    }
    if (index < trigen->output.numberoftriangles && corner < trigen->output.numberofcorners) {
        return trigen->output.trianglelist[index * trigen->output.numberofcorners + corner];
    } else {
        return 0;
    }
}

int32_t tri_out_cell_marker(struct ExtTrigen *trigen, int32_t index) {
    if (trigen == NULL) {
        return 0;
    }
    if (index < trigen->output.numberoftriangles && trigen->output.numberoftriangleattributes > 0) {
        return trigen->output.triangleattributelist[index * trigen->output.numberoftriangleattributes];
    } else {
        return 0;
    }
}

int32_t tri_out_voronoi_npoint(struct ExtTrigen *trigen) {
    if (trigen == NULL) {
        return 0;
    }
    return trigen->voronoi.numberofpoints;
}

double tri_out_voronoi_point(struct ExtTrigen *trigen, int32_t index, int32_t dim) {
    if (trigen == NULL) {
        return 0.0;
    }
    if (index < trigen->voronoi.numberofpoints && (dim == 0 || dim == 1)) {
        return trigen->voronoi.pointlist[index * 2 + dim];
    } else {
        return 0.0;
    }
}

int32_t tri_out_voronoi_nedge(struct ExtTrigen *trigen) {
    if (trigen == NULL) {
        return 0;
    }
    return trigen->voronoi.numberofedges;
}

int32_t tri_out_voronoi_edge_point(struct ExtTrigen *trigen, int32_t index, int32_t side) {
    if (trigen == NULL) {
        return 0;
    }
    if (index < trigen->voronoi.numberofedges && (side == 0 || side == 1)) {
        return trigen->voronoi.edgelist[index * 2 + side];
    } else {
        return 0;
    }
}

double tri_out_voronoi_edge_point_b_direction(struct ExtTrigen *trigen, int32_t index, int32_t dim) {
    if (trigen == NULL) {
        return 0.0;
    }
    if (index < trigen->voronoi.numberofedges && (dim == 0 || dim == 1)) {
        if (trigen->voronoi.edgelist[index * 2 + 1] == -1) {
            return trigen->voronoi.normlist[index * 2 + dim];
        } else {
            return 0.0;
        }
    } else {
        return 0.0;
    }
}