Repository navigation
Expand file tree
/
Copy pathtobj_tess_impl.inc
More file actions
332 lines (304 loc) · 11.3 KB
/
Copy pathtobj_tess_impl.inc
File metadata and controls
332 lines (304 loc) · 11.3 KB
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
/*
* tobj_tess_impl.inc -- per-precision projection + dispatch.
* Included twice from tobj_tess.c. Do not include directly.
*/
/* Project the 3D ring into w->p2d (double). Returns 1 normally, 0 if the
* polygon is fully degenerate (collinear/zero-area) and a bbox fallback was
* used. Never fails. */
static int TOBJ_TESS_T(tobj_tess_project)(const TOBJ_TESS_REAL *pts, uint32_t n,
const TOBJ_TESS_REAL *given_normal,
uint32_t flags, tobj_tess_work *w) {
double cx = (double)pts[0], cy = (double)pts[1], cz = (double)pts[2];
double coordmax = 0.0;
for (uint32_t i = 0; i < 3u * n; i++) {
double a = tobj_dabs((double)pts[i]);
if (a > coordmax) coordmax = a;
}
double nx = 0, ny = 0, nz = 0;
if (flags & TOBJ_TESS_FLAG_NORMAL_GIVEN) {
nx = (double)given_normal[0];
ny = (double)given_normal[1];
nz = (double)given_normal[2];
} else {
for (uint32_t k = 0; k < n; k++) {
uint32_t j = (k + 1u) % n;
double ax = (double)pts[3 * k], ay = (double)pts[3 * k + 1],
az = (double)pts[3 * k + 2];
double bx = (double)pts[3 * j], by = (double)pts[3 * j + 1],
bz = (double)pts[3 * j + 2];
nx += (ay - by) * (az + bz);
ny += (az - bz) * (ax + bx);
nz += (ax - bx) * (ay + by);
}
}
double len2 = nx * nx + ny * ny + nz * nz;
double cm2 = coordmax * coordmax;
double thresh = cm2 * cm2 * TOBJ_TESS_REL_N;
if (tobj_is_finite(len2) && len2 > thresh && len2 > 0.0) {
/* in-plane basis (unnormalized: only orientation signs matter) */
double wx = nx, wy = ny, wz = nz;
double ax = tobj_dabs(wx), ay = tobj_dabs(wy), az = tobj_dabs(wz);
double sx = 0, sy = 0, sz = 0;
if (ax <= ay && ax <= az)
sx = 1;
else if (ay <= az)
sy = 1;
else
sz = 1;
/* u = w x s, v = w x u */
double ux = wy * sz - wz * sy;
double uy = wz * sx - wx * sz;
double uz = wx * sy - wy * sx;
double vx = wy * uz - wz * uy;
double vy = wz * ux - wx * uz;
double vz = wx * uy - wy * ux;
for (uint32_t k = 0; k < n; k++) {
double dx = (double)pts[3 * k] - cx;
double dy = (double)pts[3 * k + 1] - cy;
double dz = (double)pts[3 * k + 2] - cz;
w->p2d[2 * k] = dx * ux + dy * uy + dz * uz;
w->p2d[2 * k + 1] = dx * vx + dy * vy + dz * vz;
}
return 1;
}
/* Fallback: drop the smallest-extent axis, keep the other two raw axes. */
double mn0 = (double)pts[0], mx0 = mn0;
double mn1 = (double)pts[1], mx1 = mn1;
double mn2 = (double)pts[2], mx2 = mn2;
for (uint32_t k = 0; k < n; k++) {
double v0 = (double)pts[3 * k + 0];
if (v0 < mn0) mn0 = v0;
if (v0 > mx0) mx0 = v0;
double v1 = (double)pts[3 * k + 1];
if (v1 < mn1) mn1 = v1;
if (v1 > mx1) mx1 = v1;
double v2 = (double)pts[3 * k + 2];
if (v2 < mn2) mn2 = v2;
if (v2 > mx2) mx2 = v2;
}
double span0 = mx0 - mn0;
double span1 = mx1 - mn1;
double span2 = mx2 - mn2;
int drop = 0;
if (span1 < span0) drop = 1;
if (span2 < (drop == 0 ? span0 : (drop == 1 ? span1 : span2))) drop = 2;
int a0 = (drop == 0) ? 1 : 0;
int a1 = (drop == 2) ? 1 : 2;
for (uint32_t k = 0; k < n; k++) {
double p0 = (double)pts[3 * k + 0];
double p1 = (double)pts[3 * k + 1];
double p2 = (double)pts[3 * k + 2];
double x = (drop == 0) ? p1 : p0;
double y = (drop == 2) ? p1 : p2;
w->p2d[2 * k] = x - (drop == 0 ? cy : cx);
w->p2d[2 * k + 1] = y - ((drop == 2) ? cy : cz);
}
return 0;
}
/* Verified-friendly core: caller provides output and scratch buffers. */
static tobj_tess_status TOBJ_TESS_T(tobj_tess_polygon_core)(
const TOBJ_TESS_T(tobj_tess_desc) * desc, tobj_tess_result *result) {
result->indices = NULL;
result->num_triangles = 0;
result->indices_allocated = 0;
result->status = TOBJ_TESS_INVALID;
if (!desc || !desc->points) return TOBJ_TESS_INVALID;
uint32_t n = desc->n;
if (n < 3) return TOBJ_TESS_INVALID;
for (uint32_t i = 0; i < 3u * n; i++) {
if (!tobj_is_finite((double)desc->points[i])) return TOBJ_TESS_INVALID;
}
uint32_t tri = n - 2u;
uint32_t need_out = tri * 3u;
if (!desc->out_indices || desc->out_cap < need_out) {
result->status = TOBJ_TESS_OOM;
return TOBJ_TESS_OOM;
}
uint32_t *out = desc->out_indices;
size_t need_scratch = tobj_tess_scratch_size(n);
if (!desc->scratch || desc->scratch_size < need_scratch) {
result->status = TOBJ_TESS_OOM;
return TOBJ_TESS_OOM;
}
unsigned char *sb = (unsigned char *)desc->scratch;
tobj_tess_work *w = (tobj_tess_work *)sb;
size_t wsize = tobj_tess_align(sizeof(tobj_tess_work), 16);
tobj_tess_work_bind(w, (void *)(sb + wsize), need_scratch, n);
int proj_ok =
TOBJ_TESS_T(tobj_tess_project)(desc->points, n, desc->normal,
desc->flags, w);
tobj_tess_status status;
uint32_t k = 0;
if (n == 3) {
tobj_tess_emit(out, &k, 0, 1, 2);
double ar = tobj_dabs(tobj_orient2d(&w->p2d[0], &w->p2d[2], &w->p2d[4]));
double scale = tobj_tess_scale2d(w->p2d, n);
status = ar <= scale * scale * TOBJ_TESS_REL ? TOBJ_TESS_DEGENERATE_BESTEFFORT
: TOBJ_TESS_OK;
} else if (n == 4) {
double scale = tobj_tess_scale2d(w->p2d, n);
double eps = scale * scale * TOBJ_TESS_REL;
const double *p0 = &w->p2d[0], *p1 = &w->p2d[2], *p2 = &w->p2d[4],
*p3 = &w->p2d[6];
double o012 = tobj_orient2d(p0, p1, p2);
double o023 = tobj_orient2d(p0, p2, p3);
double o123 = tobj_orient2d(p1, p2, p3);
double o130 = tobj_orient2d(p1, p3, p0);
int d02 = (tobj_dabs(o012) > eps && tobj_dabs(o023) > eps &&
((o012 > 0) == (o023 > 0)));
int d13 = (tobj_dabs(o123) > eps && tobj_dabs(o130) > eps &&
((o123 > 0) == (o130 > 0)));
int split02;
status = TOBJ_TESS_OK;
if (d02 && !d13) {
split02 = 1;
} else if (d13 && !d02) {
split02 = 0;
} else {
const TOBJ_TESS_REAL *q = desc->points;
double e02x = (double)q[6] - (double)q[0];
double e02y = (double)q[7] - (double)q[1];
double e02z = (double)q[8] - (double)q[2];
double e13x = (double)q[9] - (double)q[3];
double e13y = (double)q[10] - (double)q[4];
double e13z = (double)q[11] - (double)q[5];
double s02 = e02x * e02x + e02y * e02y + e02z * e02z;
double s13 = e13x * e13x + e13y * e13y + e13z * e13z;
split02 = s02 <= s13;
if (!d02 && !d13) status = TOBJ_TESS_DEGENERATE_BESTEFFORT;
}
if (split02) {
tobj_tess_emit(out, &k, 0, 1, 2);
tobj_tess_emit(out, &k, 0, 2, 3);
} else {
tobj_tess_emit(out, &k, 0, 1, 3);
tobj_tess_emit(out, &k, 1, 2, 3);
}
} else {
status = tobj_tess_run2d(w, n, out, desc->flags);
}
if (!proj_ok && status == TOBJ_TESS_OK) status = TOBJ_TESS_DEGENERATE_BESTEFFORT;
result->indices = out;
result->num_triangles = tri;
result->indices_allocated = 0;
result->status = status;
return status;
}
tobj_tess_status TOBJ_TESS_T(tobj_tess_polygon)(
const TOBJ_TESS_T(tobj_tess_desc) * desc, tobj_tess_result *result) {
result->indices = NULL;
result->num_triangles = 0;
result->indices_allocated = 0;
result->status = TOBJ_TESS_INVALID;
if (!desc || !desc->points) return TOBJ_TESS_INVALID;
uint32_t n = desc->n;
if (n < 3) return TOBJ_TESS_INVALID;
/* reject non-finite coordinates */
for (uint32_t i = 0; i < 3u * n; i++) {
if (!tobj_is_finite((double)desc->points[i])) return TOBJ_TESS_INVALID;
}
uint32_t tri = n - 2u;
uint32_t need_out = tri * 3u;
/* resolve output buffer */
uint32_t *out = NULL;
int out_alloc = 0;
if (desc->out_indices && desc->out_cap >= need_out) {
out = desc->out_indices;
} else if (desc->alloc && desc->alloc->alloc) {
out = (uint32_t *)desc->alloc->alloc(desc->alloc->ud,
need_out * sizeof(uint32_t));
if (!out) {
result->status = TOBJ_TESS_OOM;
return TOBJ_TESS_OOM;
}
out_alloc = 1;
} else {
result->status = TOBJ_TESS_OOM;
return TOBJ_TESS_OOM;
}
/* resolve scratch */
size_t need_scratch = tobj_tess_scratch_size(n);
void *scratch = NULL;
int scratch_alloc = 0;
unsigned char stackbuf[TOBJ_TESS_STACK_BYTES];
if (desc->scratch && desc->scratch_size >= need_scratch) {
scratch = desc->scratch;
} else if (n <= TOBJ_TESS_STACK_N && need_scratch <= sizeof(stackbuf)) {
scratch = stackbuf;
} else if (desc->alloc && desc->alloc->alloc) {
scratch = desc->alloc->alloc(desc->alloc->ud, need_scratch);
if (!scratch) {
if (out_alloc) desc->alloc->free(desc->alloc->ud, out);
result->status = TOBJ_TESS_OOM;
return TOBJ_TESS_OOM;
}
scratch_alloc = 1;
} else {
if (out_alloc && desc->alloc) desc->alloc->free(desc->alloc->ud, out);
result->status = TOBJ_TESS_OOM;
return TOBJ_TESS_OOM;
}
tobj_tess_work w;
tobj_tess_work_bind(&w, scratch, need_scratch, n);
int proj_ok =
TOBJ_TESS_T(tobj_tess_project)(desc->points, n, desc->normal,
desc->flags, &w);
tobj_tess_status status;
uint32_t k = 0;
if (n == 3) {
tobj_tess_emit(out, &k, 0, 1, 2);
double ar = tobj_dabs(tobj_orient2d(&w.p2d[0], &w.p2d[2], &w.p2d[4]));
double scale = tobj_tess_scale2d(w.p2d, n);
status = ar <= scale * scale * TOBJ_TESS_REL ? TOBJ_TESS_DEGENERATE_BESTEFFORT
: TOBJ_TESS_OK;
} else if (n == 4) {
double scale = tobj_tess_scale2d(w.p2d, n);
double eps = scale * scale * TOBJ_TESS_REL;
const double *p0 = &w.p2d[0], *p1 = &w.p2d[2], *p2 = &w.p2d[4],
*p3 = &w.p2d[6];
double o012 = tobj_orient2d(p0, p1, p2);
double o023 = tobj_orient2d(p0, p2, p3);
double o123 = tobj_orient2d(p1, p2, p3);
double o130 = tobj_orient2d(p1, p3, p0);
int d02 = (tobj_dabs(o012) > eps && tobj_dabs(o023) > eps &&
((o012 > 0) == (o023 > 0)));
int d13 = (tobj_dabs(o123) > eps && tobj_dabs(o130) > eps &&
((o123 > 0) == (o130 > 0)));
int split02;
status = TOBJ_TESS_OK;
if (d02 && !d13) {
split02 = 1;
} else if (d13 && !d02) {
split02 = 0;
} else {
/* convex (both internal) or degenerate (neither): shorter 3D diagonal */
const TOBJ_TESS_REAL *q = desc->points;
double e02x = (double)q[6] - (double)q[0];
double e02y = (double)q[7] - (double)q[1];
double e02z = (double)q[8] - (double)q[2];
double e13x = (double)q[9] - (double)q[3];
double e13y = (double)q[10] - (double)q[4];
double e13z = (double)q[11] - (double)q[5];
double s02 = e02x * e02x + e02y * e02y + e02z * e02z;
double s13 = e13x * e13x + e13y * e13y + e13z * e13z;
split02 = s02 <= s13;
if (!d02 && !d13) status = TOBJ_TESS_DEGENERATE_BESTEFFORT;
}
if (split02) {
tobj_tess_emit(out, &k, 0, 1, 2);
tobj_tess_emit(out, &k, 0, 2, 3);
} else {
tobj_tess_emit(out, &k, 0, 1, 3);
tobj_tess_emit(out, &k, 1, 2, 3);
}
} else {
status = tobj_tess_run2d(&w, n, out, desc->flags);
}
if (!proj_ok && status == TOBJ_TESS_OK) status = TOBJ_TESS_DEGENERATE_BESTEFFORT;
if (scratch_alloc) desc->alloc->free(desc->alloc->ud, scratch);
result->indices = out;
result->num_triangles = tri;
result->indices_allocated = out_alloc;
result->status = status;
return status;
}