-
-
Notifications
You must be signed in to change notification settings - Fork 649
Expand file tree
/
Copy pathtiny_obj_c_impl.inc
More file actions
1979 lines (1847 loc) · 70.5 KB
/
Copy pathtiny_obj_c_impl.inc
File metadata and controls
1979 lines (1847 loc) · 70.5 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
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
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* tiny_obj_c_impl.inc -- per-precision OBJ parser + scene teardown.
*
* Included twice from tiny_obj_c.c with (TOBJ_REAL, TOBJ_SUF) = (float, _f)
* and (double, _d). Every file-scope name is suffixed via TOBJ_T() so the two
* inclusions do not collide in the single translation unit. Do not include
* directly.
*/
/* ----------------------------------------------------------------------- */
/* builder */
/* ----------------------------------------------------------------------- */
typedef struct TOBJ_T(tobj_builder) {
const tobj_allocator *alloc;
tobj_arena *arena;
tobj_diag *diag;
const tobj_load_config *cfg;
tobj_caps caps;
tobj_result err;
size_t line; /* 1-based current line for diagnostics */
bool triangulate;
/* attrib temp vectors (reals / structs) */
tobj_vec av; /* vertices xyz */
tobj_vec avw; /* vertex weights (lazy) */
tobj_vec avn; /* normals xyz */
tobj_vec avt; /* texcoords uv */
tobj_vec avtw; /* texcoord w (lazy) */
tobj_vec acol; /* colors rgb (lazy) */
bool color_seen;
/* materials + name->id map (filled by the .mtl parser) */
tobj_vec materials; /* TOBJ_T(tobj_material) */
tobj_vec matmap_key; /* const char* (interned) */
tobj_vec matmap_id; /* int */
tobj_vec mat_unknown; /* tobj_pending_kv */
/* finalized shapes */
tobj_vec shapes; /* TOBJ_T(tobj_shape) */
/* current shape accumulation */
const char *cur_name;
bool cur_has_prims;
tobj_vec m_idx; /* tobj_index */
tobj_vec m_fv; /* unsigned face-vertex counts */
tobj_vec m_mid; /* int material ids */
tobj_vec m_sg; /* unsigned smoothing ids */
tobj_vec l_idx; /* tobj_index */
tobj_vec l_cnt; /* int per-line counts */
tobj_vec p_idx; /* tobj_index */
/* per-face scratch (reused) */
tobj_vec corners; /* tobj_index */
tobj_vec ringpos; /* TOBJ_REAL */
tobj_vec tris; /* uint32_t */
tobj_vec tscratch; /* uint8_t */
/* free-form geometry (only used when cfg->parse_freeform) */
tobj_vec ff_vp; /* reals (global parameter vertices) */
int ff_vp_comps;
bool ff_present; /* any free-form element in current shape */
bool ff_active; /* between curv/curv2/surf and end */
int ff_kind; /* 1=curv 2=curv2 3=surf */
tobj_cstype ff_cstype;
bool ff_rational;
int ff_degu, ff_degv;
TOBJ_REAL ff_u0, ff_u1, ff_s0, ff_s1, ff_t0, ff_t1;
tobj_vec ff_cp_int; /* int control points (curv: v idx, curv2: vp idx) */
tobj_vec ff_cp_idx; /* tobj_index control points (surf) */
tobj_vec ff_knu; /* reals (parm u) */
tobj_vec ff_knv; /* reals (parm v) */
tobj_vec ff_trim; /* TOBJ_T(tobj_trim_loop) */
tobj_vec ff_hole;
tobj_vec ff_scrv;
tobj_vec sh_curves; /* TOBJ_T(tobj_curve) */
tobj_vec sh_curves2; /* TOBJ_T(tobj_curve2) */
tobj_vec sh_surfaces; /* TOBJ_T(tobj_surface) */
/* parser state */
int cur_material;
unsigned cur_smooth;
/* multithreading pass 2: vertices already parsed; indices resolve against
* running counts instead of the (already full) attrib arrays */
bool prefilled;
size_t run_numv, run_numvt, run_numvn, run_numvp;
} TOBJ_T(tobj_builder);
/* Counts used for index fixup: running counts in MT pass 2, else live counts. */
static size_t TOBJ_T(cnt_v)(TOBJ_T(tobj_builder) * b) {
return b->prefilled ? b->run_numv : b->av.count / 3u;
}
static size_t TOBJ_T(cnt_vt)(TOBJ_T(tobj_builder) * b) {
return b->prefilled ? b->run_numvt : b->avt.count / 2u;
}
static size_t TOBJ_T(cnt_vn)(TOBJ_T(tobj_builder) * b) {
return b->prefilled ? b->run_numvn : b->avn.count / 3u;
}
static bool TOBJ_T(b_fail)(TOBJ_T(tobj_builder) * b, tobj_result e) {
if (b->err == TOBJ_OK) b->err = e;
return false;
}
static void TOBJ_T(b_warn)(TOBJ_T(tobj_builder) * b, const char *msg) {
tobj_diag_append(b->diag, b->alloc, TOBJ_SEV_WARNING, b->line, msg);
}
/* ----------------------------------------------------------------------- */
/* index fixup (1-based -> 0-based, negative -> relative) */
/* ----------------------------------------------------------------------- */
static bool TOBJ_T(fix_index)(int idx, size_t n, int *out) {
if (idx > 0) {
*out = idx - 1;
return true;
}
if (idx == 0) {
*out = -1;
return false; /* zero index: invalid per spec */
}
long long r = (long long)n + idx;
if (r < 0) {
*out = -1;
return false;
}
*out = (int)r;
return true;
}
/* Parse one corner "v[/[vt][/vn]]" against explicit counts. Returns true if a
* (possibly relative) vertex index was parsed and is in range. */
static bool TOBJ_T(parse_corner_n)(const char *tok, size_t len, size_t numv,
size_t numvt, size_t numvn,
tobj_index *out) {
out->vertex_index = -1;
out->normal_index = -1;
out->texcoord_index = -1;
tobj_cursor c;
tobj_cur_init(&c, tok, len);
int vi;
if (!tobj_scan_int(&c.p, c.end, &vi)) return false;
bool ok = TOBJ_T(fix_index)(vi, numv, &out->vertex_index);
if (c.p < c.end && *c.p == '/') {
c.p++;
if (c.p < c.end && *c.p != '/') {
int ti;
if (tobj_scan_int(&c.p, c.end, &ti))
TOBJ_T(fix_index)(ti, numvt, &out->texcoord_index);
}
if (c.p < c.end && *c.p == '/') {
c.p++;
int ni;
if (tobj_scan_int(&c.p, c.end, &ni))
TOBJ_T(fix_index)(ni, numvn, &out->normal_index);
}
}
return ok;
}
/* Parse one face/line/point corner using the builder's current counts. */
static void TOBJ_T(parse_corner)(TOBJ_T(tobj_builder) * b, const char *tok,
size_t len, tobj_index *out) {
bool ok = TOBJ_T(parse_corner_n)(tok, len, TOBJ_T(cnt_v)(b),
TOBJ_T(cnt_vt)(b), TOBJ_T(cnt_vn)(b), out);
if (!ok && out->vertex_index < 0)
TOBJ_T(b_warn)(b, "tinyobjc: invalid vertex index in face\n");
}
/* ----------------------------------------------------------------------- */
/* optional parallel-array helpers (lazy with backfill) */
/* ----------------------------------------------------------------------- */
static bool TOBJ_T(opt_push)(tobj_vec *v, size_t comps, size_t vidx,
const TOBJ_REAL *vals, const TOBJ_REAL *deflt,
bool present, const tobj_allocator *a) {
if (!present && v->count == 0) return true; /* stay empty */
/* backfill defaults up to vidx entries */
while (v->count / comps < vidx) {
if (!tobj_vec_append(v, deflt, comps, a)) return false;
}
const TOBJ_REAL *src = present ? vals : deflt;
return tobj_vec_append(v, src, comps, a);
}
/* ----------------------------------------------------------------------- */
/* directive handlers */
/* ----------------------------------------------------------------------- */
static bool TOBJ_T(handle_v)(TOBJ_T(tobj_builder) * b, tobj_cursor *c) {
double nums[8];
int n = 0;
while (n < 8) {
double d;
const char *save = c->p;
if (!tobj_scan_double(&c->p, c->end, &d)) {
c->p = save;
break;
}
nums[n++] = d;
}
if (n < 3) {
TOBJ_T(b_warn)(b, "tinyobjc: 'v' with fewer than 3 components\n");
while (n < 3) nums[n++] = 0.0;
}
if (b->av.count / 3u >= b->caps.vertices) return TOBJ_T(b_fail)(b, TOBJ_ERR_LIMIT_EXCEEDED);
size_t vidx = b->av.count / 3u;
TOBJ_REAL xyz[3] = {(TOBJ_REAL)nums[0], (TOBJ_REAL)nums[1], (TOBJ_REAL)nums[2]};
if (!tobj_vec_append(&b->av, xyz, 3, b->alloc)) return TOBJ_T(b_fail)(b, TOBJ_ERR_ALLOC);
/* weight: only the 4-number form carries a vertex weight */
bool has_w = (n == 4);
TOBJ_REAL w = has_w ? (TOBJ_REAL)nums[3] : (TOBJ_REAL)1;
TOBJ_REAL wdef = (TOBJ_REAL)1;
if (!TOBJ_T(opt_push)(&b->avw, 1, vidx, &w, &wdef, has_w, b->alloc))
return TOBJ_T(b_fail)(b, TOBJ_ERR_ALLOC);
/* color: the 6/7-number form carries r g b */
if (b->cfg->store_vertex_colors) {
bool has_c = (n >= 6);
TOBJ_REAL rgb[3];
rgb[0] = has_c ? (TOBJ_REAL)nums[3] : (TOBJ_REAL)1;
rgb[1] = has_c ? (TOBJ_REAL)nums[4] : (TOBJ_REAL)1;
rgb[2] = has_c ? (TOBJ_REAL)nums[5] : (TOBJ_REAL)1;
TOBJ_REAL white[3] = {(TOBJ_REAL)1, (TOBJ_REAL)1, (TOBJ_REAL)1};
if (has_c) b->color_seen = true;
if (!TOBJ_T(opt_push)(&b->acol, 3, vidx, rgb, white, has_c, b->alloc))
return TOBJ_T(b_fail)(b, TOBJ_ERR_ALLOC);
}
return true;
}
static bool TOBJ_T(handle_vn)(TOBJ_T(tobj_builder) * b, tobj_cursor *c) {
TOBJ_REAL xyz[3] = {0, 0, 0};
double d;
for (int i = 0; i < 3; i++)
if (tobj_scan_double(&c->p, c->end, &d)) xyz[i] = (TOBJ_REAL)d;
if (!tobj_vec_append(&b->avn, xyz, 3, b->alloc)) return TOBJ_T(b_fail)(b, TOBJ_ERR_ALLOC);
return true;
}
static bool TOBJ_T(handle_vt)(TOBJ_T(tobj_builder) * b, tobj_cursor *c) {
double d;
TOBJ_REAL uv[2] = {0, 0};
int n = 0;
for (; n < 2; n++) {
if (!tobj_scan_double(&c->p, c->end, &d)) break;
uv[n] = (TOBJ_REAL)d;
}
size_t tidx = b->avt.count / 2u;
if (!tobj_vec_append(&b->avt, uv, 2, b->alloc)) return TOBJ_T(b_fail)(b, TOBJ_ERR_ALLOC);
bool has_w = tobj_scan_double(&c->p, c->end, &d);
TOBJ_REAL w = has_w ? (TOBJ_REAL)d : (TOBJ_REAL)0;
TOBJ_REAL wdef = (TOBJ_REAL)0;
if (!TOBJ_T(opt_push)(&b->avtw, 1, tidx, &w, &wdef, has_w, b->alloc))
return TOBJ_T(b_fail)(b, TOBJ_ERR_ALLOC);
return true;
}
/* Append triangulated/raw face corners to the current shape mesh. */
static bool TOBJ_T(emit_face)(TOBJ_T(tobj_builder) * b, size_t arity) {
const tobj_index *corners = (const tobj_index *)b->corners.data;
bool do_tri = b->triangulate && arity > 3u;
if (!do_tri) {
if (b->m_idx.count + arity > b->caps.indices)
return TOBJ_T(b_fail)(b, TOBJ_ERR_LIMIT_EXCEEDED);
if (!tobj_vec_append(&b->m_idx, corners, arity, b->alloc))
return TOBJ_T(b_fail)(b, TOBJ_ERR_ALLOC);
unsigned fv = (unsigned)tobj_size_to_int(arity);
if (!tobj_vec_append(&b->m_fv, &fv, 1, b->alloc) ||
!tobj_vec_append(&b->m_mid, &b->cur_material, 1, b->alloc) ||
!tobj_vec_append(&b->m_sg, &b->cur_smooth, 1, b->alloc))
return TOBJ_T(b_fail)(b, TOBJ_ERR_ALLOC);
b->cur_has_prims = true;
return true;
}
/* triangulate */
size_t numv = TOBJ_T(cnt_v)(b);
bool pos_ok = true;
for (size_t k = 0; k < arity; k++) {
int vi = corners[k].vertex_index;
if (vi < 0 || (size_t)vi >= numv) {
pos_ok = false;
break;
}
}
uint32_t ntri = (uint32_t)(arity - 2u);
b->tris.count = 0;
if (!tobj_vec_reserve(&b->tris, (size_t)ntri * 3u, b->alloc))
return TOBJ_T(b_fail)(b, TOBJ_ERR_ALLOC);
uint32_t *tri = (uint32_t *)b->tris.data;
if (pos_ok) {
b->ringpos.count = 0;
if (!tobj_vec_reserve(&b->ringpos, arity * 3u, b->alloc))
return TOBJ_T(b_fail)(b, TOBJ_ERR_ALLOC);
TOBJ_REAL *rp = (TOBJ_REAL *)b->ringpos.data;
const TOBJ_REAL *vsrc = (const TOBJ_REAL *)b->av.data;
for (size_t k = 0; k < arity; k++) {
size_t vi = (size_t)corners[k].vertex_index;
rp[3 * k + 0] = vsrc[3 * vi + 0];
rp[3 * k + 1] = vsrc[3 * vi + 1];
rp[3 * k + 2] = vsrc[3 * vi + 2];
}
size_t need_scratch = tobj_tess_scratch_size((uint32_t)arity);
if (!tobj_vec_reserve(&b->tscratch, need_scratch, b->alloc))
return TOBJ_T(b_fail)(b, TOBJ_ERR_ALLOC);
TOBJ_T(tobj_tess_desc) desc;
tobj_memset(&desc, 0, sizeof desc);
desc.points = rp;
desc.n = (uint32_t)arity;
desc.out_indices = tri;
desc.out_cap = ntri * 3u;
desc.scratch = b->tscratch.data;
desc.scratch_size = need_scratch;
tobj_tess_result res;
TOBJ_T(tobj_tess_polygon)(&desc, &res);
ntri = res.num_triangles;
if (res.status == TOBJ_TESS_DEGENERATE_BESTEFFORT)
TOBJ_T(b_warn)(b, "tinyobjc: degenerate polygon triangulated best-effort\n");
} else {
TOBJ_T(b_warn)(b, "tinyobjc: face with invalid vertex index; fan fallback\n");
for (uint32_t t = 0; t < ntri; t++) {
tri[3 * t + 0] = 0;
tri[3 * t + 1] = t + 1;
tri[3 * t + 2] = t + 2;
}
}
if (b->m_idx.count + (size_t)ntri * 3u > b->caps.indices)
return TOBJ_T(b_fail)(b, TOBJ_ERR_LIMIT_EXCEEDED);
for (uint32_t t = 0; t < ntri; t++) {
tobj_index tidx[3];
tidx[0] = corners[tri[3 * t + 0]];
tidx[1] = corners[tri[3 * t + 1]];
tidx[2] = corners[tri[3 * t + 2]];
unsigned fv = 3;
if (!tobj_vec_append(&b->m_idx, tidx, 3, b->alloc) ||
!tobj_vec_append(&b->m_fv, &fv, 1, b->alloc) ||
!tobj_vec_append(&b->m_mid, &b->cur_material, 1, b->alloc) ||
!tobj_vec_append(&b->m_sg, &b->cur_smooth, 1, b->alloc))
return TOBJ_T(b_fail)(b, TOBJ_ERR_ALLOC);
}
b->cur_has_prims = true;
return true;
}
static bool TOBJ_T(handle_f)(TOBJ_T(tobj_builder) * b, tobj_cursor *c) {
b->corners.count = 0;
const char *tok;
size_t tlen;
size_t arity = 0;
while (tobj_next_token(c, &tok, &tlen)) {
if (arity >= b->caps.arity) return TOBJ_T(b_fail)(b, TOBJ_ERR_LIMIT_EXCEEDED);
tobj_index *slot = (tobj_index *)tobj_vec_push(&b->corners, b->alloc);
if (!slot) return TOBJ_T(b_fail)(b, TOBJ_ERR_ALLOC);
TOBJ_T(parse_corner)(b, tok, tlen, slot);
arity++;
}
if (arity < 3) {
TOBJ_T(b_warn)(b, "tinyobjc: degenerate face (<3 vertices)\n");
return true;
}
if (b->m_fv.count >= b->caps.faces) return TOBJ_T(b_fail)(b, TOBJ_ERR_LIMIT_EXCEEDED);
return TOBJ_T(emit_face)(b, arity);
}
static bool TOBJ_T(handle_l)(TOBJ_T(tobj_builder) * b, tobj_cursor *c) {
const char *tok;
size_t tlen;
size_t cnt = 0;
size_t start = b->l_idx.count;
while (tobj_next_token(c, &tok, &tlen)) {
if (cnt >= b->caps.arity) return TOBJ_T(b_fail)(b, TOBJ_ERR_LIMIT_EXCEEDED);
tobj_index idx;
TOBJ_T(parse_corner)(b, tok, tlen, &idx);
if (!tobj_vec_append(&b->l_idx, &idx, 1, b->alloc))
return TOBJ_T(b_fail)(b, TOBJ_ERR_ALLOC);
cnt++;
}
if (cnt < 2) {
b->l_idx.count = start; /* roll back */
TOBJ_T(b_warn)(b, "tinyobjc: degenerate line (<2 vertices)\n");
return true;
}
int icnt = tobj_size_to_int(cnt);
if (!tobj_vec_append(&b->l_cnt, &icnt, 1, b->alloc))
return TOBJ_T(b_fail)(b, TOBJ_ERR_ALLOC);
b->cur_has_prims = true;
return true;
}
static bool TOBJ_T(handle_p)(TOBJ_T(tobj_builder) * b, tobj_cursor *c) {
const char *tok;
size_t tlen;
while (tobj_next_token(c, &tok, &tlen)) {
tobj_index idx;
TOBJ_T(parse_corner)(b, tok, tlen, &idx);
if (!tobj_vec_append(&b->p_idx, &idx, 1, b->alloc))
return TOBJ_T(b_fail)(b, TOBJ_ERR_ALLOC);
b->cur_has_prims = true;
}
return true;
}
/* ----------------------------------------------------------------------- */
/* shape finalize */
/* ----------------------------------------------------------------------- */
#define TOBJ_FIN(dst_ptr, dst_cnt, vec, T) \
do { \
(dst_cnt) = (vec).count; \
(dst_ptr) = (T *)tobj_vec_finalize(&(vec), b->arena, 16); \
if ((vec).count && !(dst_ptr)) return TOBJ_T(b_fail)(b, TOBJ_ERR_ALLOC); \
(vec).count = 0; \
} while (0)
static bool TOBJ_T(ff_finalize_elem)(TOBJ_T(tobj_builder) * b); /* fwd */
static bool TOBJ_T(finalize_shape)(TOBJ_T(tobj_builder) * b) {
if (b->ff_active && !TOBJ_T(ff_finalize_elem)(b)) return false;
bool has_ff = b->sh_curves.count || b->sh_curves2.count ||
b->sh_surfaces.count;
if (!b->cur_has_prims && b->m_fv.count == 0 && b->l_cnt.count == 0 &&
b->p_idx.count == 0 && !has_ff) {
/* nothing to emit, but keep the name for the next group */
return true;
}
if (b->shapes.count >= b->caps.shapes) return TOBJ_T(b_fail)(b, TOBJ_ERR_LIMIT_EXCEEDED);
TOBJ_T(tobj_shape) sh;
tobj_memset(&sh, 0, sizeof sh);
sh.name = b->cur_name ? b->cur_name : tobj_intern(b->arena, "", 0);
if (!sh.name) return TOBJ_T(b_fail)(b, TOBJ_ERR_ALLOC);
size_t nf;
TOBJ_FIN(sh.mesh.indices, sh.mesh.num_indices, b->m_idx, tobj_index);
TOBJ_FIN(sh.mesh.num_face_vertices, nf, b->m_fv, unsigned);
sh.mesh.num_faces = nf;
TOBJ_FIN(sh.mesh.material_ids, nf, b->m_mid, int);
TOBJ_FIN(sh.mesh.smoothing_group_ids, nf, b->m_sg, unsigned);
TOBJ_FIN(sh.lines.indices, sh.lines.num_indices, b->l_idx, tobj_index);
TOBJ_FIN(sh.lines.num_line_vertices, sh.lines.num_lines, b->l_cnt, int);
TOBJ_FIN(sh.points.indices, sh.points.num_indices, b->p_idx, tobj_index);
/* free-form geometry, if any accumulated for this shape */
if (b->sh_curves.count || b->sh_curves2.count || b->sh_surfaces.count) {
TOBJ_T(tobj_freeform) *ff = (TOBJ_T(tobj_freeform) *)tobj_arena_alloc(
b->arena, sizeof(*ff), 16);
if (!ff) return TOBJ_T(b_fail)(b, TOBJ_ERR_ALLOC);
tobj_memset(ff, 0, sizeof *ff);
ff->vp.components = b->ff_vp_comps;
ff->vp.uvw.count = b->ff_vp.count;
ff->vp.uvw.ptr = (TOBJ_REAL *)tobj_arena_dup(
b->arena, b->ff_vp.data, b->ff_vp.count * sizeof(TOBJ_REAL), 16);
if (b->ff_vp.count && !ff->vp.uvw.ptr)
return TOBJ_T(b_fail)(b, TOBJ_ERR_ALLOC);
TOBJ_FIN(ff->curves, ff->num_curves, b->sh_curves, TOBJ_T(tobj_curve));
TOBJ_FIN(ff->curves2, ff->num_curves2, b->sh_curves2, TOBJ_T(tobj_curve2));
TOBJ_FIN(ff->surfaces, ff->num_surfaces, b->sh_surfaces,
TOBJ_T(tobj_surface));
sh.freeform = ff;
}
TOBJ_T(tobj_shape) *slot = (TOBJ_T(tobj_shape) *)tobj_vec_push(&b->shapes, b->alloc);
if (!slot) return TOBJ_T(b_fail)(b, TOBJ_ERR_ALLOC);
*slot = sh;
b->cur_has_prims = false;
return true;
}
/* Start a new shape group with the given (interned) name. */
static bool TOBJ_T(new_group)(TOBJ_T(tobj_builder) * b, const char *name) {
if (!TOBJ_T(finalize_shape)(b)) return false;
b->cur_name = name;
return true;
}
/* ----------------------------------------------------------------------- */
/* material support */
/* ----------------------------------------------------------------------- */
static void TOBJ_T(texopt_init)(TOBJ_T(tobj_texture_option) * t, tobj_arena *ar) {
tobj_memset(t, 0, sizeof *t);
t->type = TOBJ_TEXTURE_TYPE_NONE;
t->sharpness = (TOBJ_REAL)1;
t->brightness = (TOBJ_REAL)0;
t->contrast = (TOBJ_REAL)1;
t->scale[0] = t->scale[1] = t->scale[2] = (TOBJ_REAL)1;
t->texture_resolution = -1;
t->clamp = false;
t->blendu = true;
t->blendv = true;
t->imfchan = 'm';
t->bump_multiplier = (TOBJ_REAL)1;
t->colorspace = tobj_intern(ar, "", 0);
}
static void TOBJ_T(material_init)(TOBJ_T(tobj_material) * m, tobj_arena *ar) {
tobj_memset(m, 0, sizeof *m);
m->shininess = (TOBJ_REAL)1;
m->ior = (TOBJ_REAL)1;
m->dissolve = (TOBJ_REAL)1;
m->illum = 0;
const char *empty = tobj_intern(ar, "", 0);
m->name = empty;
m->ambient_texname = m->diffuse_texname = m->specular_texname = empty;
m->specular_highlight_texname = m->bump_texname = m->displacement_texname = empty;
m->alpha_texname = m->reflection_texname = empty;
m->roughness_texname = m->metallic_texname = m->sheen_texname = empty;
m->emissive_texname = m->normal_texname = empty;
TOBJ_T(texopt_init)(&m->ambient_texopt, ar);
TOBJ_T(texopt_init)(&m->diffuse_texopt, ar);
TOBJ_T(texopt_init)(&m->specular_texopt, ar);
TOBJ_T(texopt_init)(&m->specular_highlight_texopt, ar);
TOBJ_T(texopt_init)(&m->bump_texopt, ar);
TOBJ_T(texopt_init)(&m->displacement_texopt, ar);
TOBJ_T(texopt_init)(&m->alpha_texopt, ar);
TOBJ_T(texopt_init)(&m->reflection_texopt, ar);
TOBJ_T(texopt_init)(&m->roughness_texopt, ar);
TOBJ_T(texopt_init)(&m->metallic_texopt, ar);
TOBJ_T(texopt_init)(&m->sheen_texopt, ar);
TOBJ_T(texopt_init)(&m->emissive_texopt, ar);
TOBJ_T(texopt_init)(&m->normal_texopt, ar);
}
static int TOBJ_T(mat_find)(TOBJ_T(tobj_builder) * b, const char *name,
size_t len) {
const char **keys = (const char **)b->matmap_key.data;
const int *ids = (const int *)b->matmap_id.data;
for (size_t i = 0; i < b->matmap_key.count; i++) {
if (tobj_slice_eq(name, len, keys[i])) return ids[i];
}
return -1;
}
/* Read up to `n` reals from the cursor into dst (missing left untouched). */
static void TOBJ_T(read_reals)(tobj_cursor *c, TOBJ_REAL *dst, int n) {
double d;
for (int i = 0; i < n; i++) {
if (!tobj_scan_double(&c->p, c->end, &d)) break;
dst[i] = (TOBJ_REAL)d;
}
}
/* Trim leading/trailing spaces of the cursor remainder and intern it. */
static const char *TOBJ_T(intern_rest)(TOBJ_T(tobj_builder) * b, tobj_cursor *c) {
const char *p = c->p, *e = c->end;
while (p < e && tobj_is_space(*p)) p++;
while (e > p && tobj_is_space(e[-1])) e--;
return tobj_intern(b->arena, p, (size_t)(e - p));
}
static bool TOBJ_T(tok_onoff)(const char *t, size_t l, bool *out) {
if (tobj_slice_eq(t, l, "on")) {
*out = true;
return true;
}
if (tobj_slice_eq(t, l, "off")) {
*out = false;
return true;
}
return false;
}
/* Parse "[-options ...] filename" into texopt (+ interned name). Mirrors the
* C++ ParseTextureNameAndOption. The cursor is consumed up to the filename. */
static const char *TOBJ_T(parse_texopt)(TOBJ_T(tobj_builder) * b, tobj_cursor *c,
TOBJ_T(tobj_texture_option) * opt) {
const char *tok;
size_t tl;
for (;;) {
const char *save = c->p;
if (!tobj_next_token(c, &tok, &tl)) {
c->p = save;
break;
}
if (tl == 0 || tok[0] != '-') {
c->p = save; /* not an option: filename begins here */
break;
}
if (tobj_slice_eq(tok, tl, "-blendu")) {
const char *v;
size_t vl;
if (tobj_next_token(c, &v, &vl)) TOBJ_T(tok_onoff)(v, vl, &opt->blendu);
} else if (tobj_slice_eq(tok, tl, "-blendv")) {
const char *v;
size_t vl;
if (tobj_next_token(c, &v, &vl)) TOBJ_T(tok_onoff)(v, vl, &opt->blendv);
} else if (tobj_slice_eq(tok, tl, "-clamp")) {
const char *v;
size_t vl;
if (tobj_next_token(c, &v, &vl)) TOBJ_T(tok_onoff)(v, vl, &opt->clamp);
} else if (tobj_slice_eq(tok, tl, "-bm")) {
TOBJ_T(read_reals)(c, &opt->bump_multiplier, 1);
} else if (tobj_slice_eq(tok, tl, "-boost")) {
TOBJ_T(read_reals)(c, &opt->sharpness, 1);
} else if (tobj_slice_eq(tok, tl, "-o")) {
TOBJ_T(read_reals)(c, opt->origin_offset, 3);
} else if (tobj_slice_eq(tok, tl, "-s")) {
TOBJ_T(read_reals)(c, opt->scale, 3);
} else if (tobj_slice_eq(tok, tl, "-t")) {
TOBJ_T(read_reals)(c, opt->turbulence, 3);
} else if (tobj_slice_eq(tok, tl, "-mm")) {
TOBJ_T(read_reals)(c, &opt->brightness, 1);
TOBJ_T(read_reals)(c, &opt->contrast, 1);
} else if (tobj_slice_eq(tok, tl, "-texres")) {
int v = -1;
tobj_scan_int(&c->p, c->end, &v);
opt->texture_resolution = v;
} else if (tobj_slice_eq(tok, tl, "-imfchan")) {
const char *v;
size_t vl;
if (tobj_next_token(c, &v, &vl) && vl >= 1) opt->imfchan = v[0];
} else if (tobj_slice_eq(tok, tl, "-type")) {
const char *v;
size_t vl;
if (tobj_next_token(c, &v, &vl)) {
if (tobj_slice_eq(v, vl, "sphere"))
opt->type = TOBJ_TEXTURE_TYPE_SPHERE;
else if (tobj_slice_eq(v, vl, "cube_top"))
opt->type = TOBJ_TEXTURE_TYPE_CUBE_TOP;
else if (tobj_slice_eq(v, vl, "cube_bottom"))
opt->type = TOBJ_TEXTURE_TYPE_CUBE_BOTTOM;
else if (tobj_slice_eq(v, vl, "cube_front"))
opt->type = TOBJ_TEXTURE_TYPE_CUBE_FRONT;
else if (tobj_slice_eq(v, vl, "cube_back"))
opt->type = TOBJ_TEXTURE_TYPE_CUBE_BACK;
else if (tobj_slice_eq(v, vl, "cube_left"))
opt->type = TOBJ_TEXTURE_TYPE_CUBE_LEFT;
else if (tobj_slice_eq(v, vl, "cube_right"))
opt->type = TOBJ_TEXTURE_TYPE_CUBE_RIGHT;
}
} else if (tobj_slice_eq(tok, tl, "-colorspace")) {
const char *v;
size_t vl;
if (tobj_next_token(c, &v, &vl))
opt->colorspace = tobj_intern(b->arena, v, vl);
} else {
/* unknown option flag: skip just the flag token */
}
}
return TOBJ_T(intern_rest)(b, c);
}
/* Record an unrecognized "key value" .mtl line for material `mi`. */
static bool TOBJ_T(push_unknown)(TOBJ_T(tobj_builder) * b, size_t mi,
const char *key, size_t keylen,
tobj_cursor *c) {
tobj_pending_kv *kv = (tobj_pending_kv *)tobj_vec_push(&b->mat_unknown, b->alloc);
if (!kv) return TOBJ_T(b_fail)(b, TOBJ_ERR_ALLOC);
kv->mat_index = mi;
kv->key = tobj_intern(b->arena, key, keylen);
kv->value = TOBJ_T(intern_rest)(b, c);
if (!kv->key || !kv->value) return TOBJ_T(b_fail)(b, TOBJ_ERR_ALLOC);
return true;
}
/* If map_Kd was given but Kd was not, default diffuse to 0.6 (C++ parity). */
static void TOBJ_T(apply_kd_default)(TOBJ_T(tobj_material) * m, bool kd_seen) {
if (!kd_seen && m->diffuse_texname && m->diffuse_texname[0] != '\0') {
m->diffuse[0] = m->diffuse[1] = m->diffuse[2] = (TOBJ_REAL)0.6;
}
}
/* Full .mtl parser. */
static bool TOBJ_T(parse_mtl_buffer)(TOBJ_T(tobj_builder) * b,
const uint8_t *data, size_t len) {
tobj_vec lines;
tobj_vec_init(&lines, sizeof(tobj_line));
if (!tobj_build_line_index(data, len, &lines, b->alloc)) {
tobj_vec_free(&lines, b->alloc);
return TOBJ_T(b_fail)(b, TOBJ_ERR_ALLOC);
}
const char *base = (const char *)data;
int cur = -1;
bool kd_seen = false;
for (size_t i = 0; i < lines.count && b->err == TOBJ_OK; i++) {
tobj_line *ln = (tobj_line *)tobj_vec_at(&lines, i);
if (ln->len > b->caps.line_bytes) continue;
tobj_cursor c;
tobj_cur_init(&c, base + ln->pos, ln->len);
const char *tok;
size_t tl;
if (!tobj_next_token(&c, &tok, &tl)) continue;
if (tl && tok[0] == '#') continue;
if (tobj_slice_eq(tok, tl, "newmtl")) {
if (cur >= 0)
TOBJ_T(apply_kd_default)(
(TOBJ_T(tobj_material) *)tobj_vec_at(&b->materials, (size_t)cur),
kd_seen);
if (b->materials.count >= b->caps.materials) {
TOBJ_T(b_fail)(b, TOBJ_ERR_LIMIT_EXCEEDED);
break;
}
TOBJ_T(tobj_material) *m =
(TOBJ_T(tobj_material) *)tobj_vec_push(&b->materials, b->alloc);
if (!m) {
TOBJ_T(b_fail)(b, TOBJ_ERR_ALLOC);
break;
}
TOBJ_T(material_init)(m, b->arena);
m->name = TOBJ_T(intern_rest)(b, &c);
cur = (int)(b->materials.count - 1);
kd_seen = false;
const char **k = (const char **)tobj_vec_push(&b->matmap_key, b->alloc);
int *id = (int *)tobj_vec_push(&b->matmap_id, b->alloc);
if (!k || !id) {
TOBJ_T(b_fail)(b, TOBJ_ERR_ALLOC);
break;
}
*k = m->name;
*id = cur;
continue;
}
if (cur < 0) continue;
TOBJ_T(tobj_material) *m =
(TOBJ_T(tobj_material) *)tobj_vec_at(&b->materials, (size_t)cur);
if (tobj_slice_eq(tok, tl, "Ka")) {
TOBJ_T(read_reals)(&c, m->ambient, 3);
} else if (tobj_slice_eq(tok, tl, "Kd")) {
TOBJ_T(read_reals)(&c, m->diffuse, 3);
kd_seen = true;
} else if (tobj_slice_eq(tok, tl, "Ks")) {
TOBJ_T(read_reals)(&c, m->specular, 3);
} else if (tobj_slice_eq(tok, tl, "Ke")) {
TOBJ_T(read_reals)(&c, m->emission, 3);
} else if (tobj_slice_eq(tok, tl, "Kt") || tobj_slice_eq(tok, tl, "Tf")) {
TOBJ_T(read_reals)(&c, m->transmittance, 3);
} else if (tobj_slice_eq(tok, tl, "Ns")) {
TOBJ_T(read_reals)(&c, &m->shininess, 1);
} else if (tobj_slice_eq(tok, tl, "Ni")) {
TOBJ_T(read_reals)(&c, &m->ior, 1);
} else if (tobj_slice_eq(tok, tl, "d")) {
TOBJ_T(read_reals)(&c, &m->dissolve, 1);
} else if (tobj_slice_eq(tok, tl, "Tr")) {
TOBJ_REAL tr = 0;
TOBJ_T(read_reals)(&c, &tr, 1);
m->dissolve = (TOBJ_REAL)1 - tr;
} else if (tobj_slice_eq(tok, tl, "illum")) {
int v = 0;
tobj_scan_int(&c.p, c.end, &v);
m->illum = v;
} else if (tobj_slice_eq(tok, tl, "Pr")) {
TOBJ_T(read_reals)(&c, &m->roughness, 1);
} else if (tobj_slice_eq(tok, tl, "Pm")) {
TOBJ_T(read_reals)(&c, &m->metallic, 1);
} else if (tobj_slice_eq(tok, tl, "Ps")) {
TOBJ_T(read_reals)(&c, &m->sheen, 1);
} else if (tobj_slice_eq(tok, tl, "Pc")) {
TOBJ_T(read_reals)(&c, &m->clearcoat_thickness, 1);
} else if (tobj_slice_eq(tok, tl, "Pcr")) {
TOBJ_T(read_reals)(&c, &m->clearcoat_roughness, 1);
} else if (tobj_slice_eq(tok, tl, "aniso")) {
TOBJ_T(read_reals)(&c, &m->anisotropy, 1);
} else if (tobj_slice_eq(tok, tl, "anisor")) {
TOBJ_T(read_reals)(&c, &m->anisotropy_rotation, 1);
} else if (tobj_slice_eq(tok, tl, "map_Ka")) {
m->ambient_texname = TOBJ_T(parse_texopt)(b, &c, &m->ambient_texopt);
} else if (tobj_slice_eq(tok, tl, "map_Kd")) {
m->diffuse_texname = TOBJ_T(parse_texopt)(b, &c, &m->diffuse_texopt);
} else if (tobj_slice_eq(tok, tl, "map_Ks")) {
m->specular_texname = TOBJ_T(parse_texopt)(b, &c, &m->specular_texopt);
} else if (tobj_slice_eq(tok, tl, "map_Ns")) {
m->specular_highlight_texname =
TOBJ_T(parse_texopt)(b, &c, &m->specular_highlight_texopt);
} else if (tobj_slice_eq(tok, tl, "map_d")) {
m->alpha_texname = TOBJ_T(parse_texopt)(b, &c, &m->alpha_texopt);
} else if (tobj_slice_eq(tok, tl, "map_bump") ||
tobj_slice_eq(tok, tl, "map_Bump") ||
tobj_slice_eq(tok, tl, "bump")) {
m->bump_texopt.imfchan = 'l';
m->bump_texname = TOBJ_T(parse_texopt)(b, &c, &m->bump_texopt);
} else if (tobj_slice_eq(tok, tl, "disp")) {
m->displacement_texname =
TOBJ_T(parse_texopt)(b, &c, &m->displacement_texopt);
} else if (tobj_slice_eq(tok, tl, "refl")) {
m->reflection_texname = TOBJ_T(parse_texopt)(b, &c, &m->reflection_texopt);
} else if (tobj_slice_eq(tok, tl, "norm")) {
m->normal_texname = TOBJ_T(parse_texopt)(b, &c, &m->normal_texopt);
} else if (tobj_slice_eq(tok, tl, "map_Pr")) {
m->roughness_texname = TOBJ_T(parse_texopt)(b, &c, &m->roughness_texopt);
} else if (tobj_slice_eq(tok, tl, "map_Pm")) {
m->metallic_texname = TOBJ_T(parse_texopt)(b, &c, &m->metallic_texopt);
} else if (tobj_slice_eq(tok, tl, "map_Ps")) {
m->sheen_texname = TOBJ_T(parse_texopt)(b, &c, &m->sheen_texopt);
} else if (tobj_slice_eq(tok, tl, "map_Ke")) {
m->emissive_texname = TOBJ_T(parse_texopt)(b, &c, &m->emissive_texopt);
} else {
/* unknown parameter: store key -> rest of line */
if (!TOBJ_T(push_unknown)(b, (size_t)cur, tok, tl, &c)) break;
}
}
if (cur >= 0)
TOBJ_T(apply_kd_default)(
(TOBJ_T(tobj_material) *)tobj_vec_at(&b->materials, (size_t)cur),
kd_seen);
tobj_vec_free(&lines, b->alloc);
return b->err == TOBJ_OK;
}
static bool TOBJ_T(handle_mtllib)(TOBJ_T(tobj_builder) * b, tobj_cursor *c) {
if (!b->cfg->mtl_resolver) {
TOBJ_T(b_warn)(b, "tinyobjc: mtllib present but no material resolver\n");
return true;
}
const char *tok;
size_t tl;
while (tobj_next_token(c, &tok, &tl)) {
char *name = (char *)tobj_arena_alloc(b->arena, tl + 1, 1);
if (!name) return TOBJ_T(b_fail)(b, TOBJ_ERR_ALLOC);
tobj_memcpy(name, tok, tl);
name[tl] = '\0';
tobj_mtl_source src;
tobj_memset(&src, 0, sizeof src);
tobj_result r =
b->cfg->mtl_resolver(b->cfg->mtl_resolver_user_data, name, &src);
if (r != TOBJ_OK) {
TOBJ_T(b_warn)(b, "tinyobjc: failed to resolve mtllib\n");
continue;
}
TOBJ_T(parse_mtl_buffer)(b, src.data, src.len);
if (src.release) src.release(src.release_ud, src.data, src.len);
if (b->err != TOBJ_OK) return false;
}
return true;
}
/* ----------------------------------------------------------------------- */
/* free-form geometry (parse & retain; no NURBS evaluation) */
/* ----------------------------------------------------------------------- */
static void TOBJ_T(ff_reset_elem)(TOBJ_T(tobj_builder) * b) {
b->ff_active = false;
b->ff_kind = 0;
b->ff_cp_int.count = 0;
b->ff_cp_idx.count = 0;
b->ff_knu.count = 0;
b->ff_knv.count = 0;
b->ff_trim.count = 0;
b->ff_hole.count = 0;
b->ff_scrv.count = 0;
}
static bool TOBJ_T(ff_finalize_elem)(TOBJ_T(tobj_builder) * b) {
if (!b->ff_active) return true;
if (b->ff_kind == 1) { /* curv */
TOBJ_T(tobj_curve) *cv =
(TOBJ_T(tobj_curve) *)tobj_vec_push(&b->sh_curves, b->alloc);
if (!cv) return TOBJ_T(b_fail)(b, TOBJ_ERR_ALLOC);
cv->type = b->ff_cstype;
cv->rational = b->ff_rational;
cv->degree = b->ff_degu;
cv->u0 = b->ff_u0;
cv->u1 = b->ff_u1;
cv->num_control_points = b->ff_cp_int.count;
cv->control_points = (int *)tobj_vec_finalize(&b->ff_cp_int, b->arena, 16);
cv->num_knots = b->ff_knu.count;
cv->knots = (TOBJ_REAL *)tobj_vec_finalize(&b->ff_knu, b->arena, 16);
if ((cv->num_control_points && !cv->control_points) ||
(cv->num_knots && !cv->knots))
return TOBJ_T(b_fail)(b, TOBJ_ERR_ALLOC);
} else if (b->ff_kind == 2) { /* curv2 */
TOBJ_T(tobj_curve2) *cv =
(TOBJ_T(tobj_curve2) *)tobj_vec_push(&b->sh_curves2, b->alloc);
if (!cv) return TOBJ_T(b_fail)(b, TOBJ_ERR_ALLOC);
cv->type = b->ff_cstype;
cv->rational = b->ff_rational;
cv->degree = b->ff_degu;
cv->num_control_points = b->ff_cp_int.count;
cv->control_points = (int *)tobj_vec_finalize(&b->ff_cp_int, b->arena, 16);
cv->num_knots = b->ff_knu.count;
cv->knots = (TOBJ_REAL *)tobj_vec_finalize(&b->ff_knu, b->arena, 16);
if ((cv->num_control_points && !cv->control_points) ||
(cv->num_knots && !cv->knots))
return TOBJ_T(b_fail)(b, TOBJ_ERR_ALLOC);
} else if (b->ff_kind == 3) { /* surf */
TOBJ_T(tobj_surface) *sf =
(TOBJ_T(tobj_surface) *)tobj_vec_push(&b->sh_surfaces, b->alloc);
if (!sf) return TOBJ_T(b_fail)(b, TOBJ_ERR_ALLOC);
sf->type = b->ff_cstype;
sf->rational = b->ff_rational;
sf->degu = b->ff_degu;
sf->degv = b->ff_degv;
sf->s0 = b->ff_s0;
sf->s1 = b->ff_s1;
sf->t0 = b->ff_t0;
sf->t1 = b->ff_t1;
sf->num_control_points = b->ff_cp_idx.count;
sf->control_points =
(tobj_index *)tobj_vec_finalize(&b->ff_cp_idx, b->arena, 16);
sf->num_knots_u = b->ff_knu.count;
sf->knots_u = (TOBJ_REAL *)tobj_vec_finalize(&b->ff_knu, b->arena, 16);
sf->num_knots_v = b->ff_knv.count;
sf->knots_v = (TOBJ_REAL *)tobj_vec_finalize(&b->ff_knv, b->arena, 16);
sf->num_trims = b->ff_trim.count;
sf->trims = (TOBJ_T(tobj_trim_loop) *)tobj_vec_finalize(&b->ff_trim,
b->arena, 16);
sf->num_holes = b->ff_hole.count;
sf->holes = (TOBJ_T(tobj_trim_loop) *)tobj_vec_finalize(&b->ff_hole,
b->arena, 16);
sf->num_scrvs = b->ff_scrv.count;
sf->scrvs = (TOBJ_T(tobj_trim_loop) *)tobj_vec_finalize(&b->ff_scrv,
b->arena, 16);
}
b->ff_present = true;
TOBJ_T(ff_reset_elem)(b);
return true;
}
static void TOBJ_T(ff_loops)(TOBJ_T(tobj_builder) * b, tobj_cursor *c,
tobj_vec *dst) {
double u0, u1;
int idx;
while (tobj_scan_double(&c->p, c->end, &u0)) {
if (!tobj_scan_double(&c->p, c->end, &u1)) break;
if (!tobj_scan_int(&c->p, c->end, &idx)) break;
TOBJ_T(tobj_trim_loop) *t =
(TOBJ_T(tobj_trim_loop) *)tobj_vec_push(dst, b->alloc);
if (!t) {
TOBJ_T(b_fail)(b, TOBJ_ERR_ALLOC);
return;
}
t->u0 = (TOBJ_REAL)u0;
t->u1 = (TOBJ_REAL)u1;
t->curve2_index = idx;
}
}
static bool TOBJ_T(handle_freeform)(TOBJ_T(tobj_builder) * b, const char *tok,
size_t tl, tobj_cursor *c) {
if (tobj_slice_eq(tok, tl, "vp")) {
double d;
int n = 0;
TOBJ_REAL uvw[3] = {0, 0, 0};
for (; n < 3; n++) {
if (!tobj_scan_double(&c->p, c->end, &d)) break;
uvw[n] = (TOBJ_REAL)d;
}
if (n < 1) return true;
if (n > b->ff_vp_comps) b->ff_vp_comps = n;
return tobj_vec_append(&b->ff_vp, uvw, (size_t)n, b->alloc)
? true
: TOBJ_T(b_fail)(b, TOBJ_ERR_ALLOC);
}
if (tobj_slice_eq(tok, tl, "cstype")) {
const char *t2;
size_t l2;
b->ff_rational = false;
b->ff_cstype = TOBJ_CSTYPE_NONE;
while (tobj_next_token(c, &t2, &l2)) {
if (tobj_slice_eq(t2, l2, "rat"))
b->ff_rational = true;
else if (tobj_slice_eq(t2, l2, "bmatrix"))
b->ff_cstype = TOBJ_CSTYPE_BMATRIX;
else if (tobj_slice_eq(t2, l2, "bezier"))
b->ff_cstype = TOBJ_CSTYPE_BEZIER;
else if (tobj_slice_eq(t2, l2, "bspline"))
b->ff_cstype = TOBJ_CSTYPE_BSPLINE;
else if (tobj_slice_eq(t2, l2, "cardinal"))
b->ff_cstype = TOBJ_CSTYPE_CARDINAL;
else if (tobj_slice_eq(t2, l2, "taylor"))
b->ff_cstype = TOBJ_CSTYPE_TAYLOR;
}
return true;
}
if (tobj_slice_eq(tok, tl, "deg")) {
int du = 0, dv = 0;
if (tobj_scan_int(&c->p, c->end, &du)) b->ff_degu = du;
if (tobj_scan_int(&c->p, c->end, &dv)) b->ff_degv = dv;
return true;
}
if (tobj_slice_eq(tok, tl, "curv")) {
TOBJ_T(ff_reset_elem)(b);
b->ff_active = true;
b->ff_kind = 1;
double a, bb;
b->ff_u0 = tobj_scan_double(&c->p, c->end, &a) ? (TOBJ_REAL)a : (TOBJ_REAL)0;
b->ff_u1 = tobj_scan_double(&c->p, c->end, &bb) ? (TOBJ_REAL)bb : (TOBJ_REAL)0;
int vi;
size_t numv = TOBJ_T(cnt_v)(b);
while (tobj_scan_int(&c->p, c->end, &vi)) {
int r;
TOBJ_T(fix_index)(vi, numv, &r);
if (!tobj_vec_append(&b->ff_cp_int, &r, 1, b->alloc))
return TOBJ_T(b_fail)(b, TOBJ_ERR_ALLOC);
}
return true;