forked from ctu-mrs/sketch4latex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scene.c
1665 lines (1471 loc) · 44 KB
/
scene.c
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
/* scene.c
Copyright (C) 2005,2006,2007,2008,2009,2010,2011 Eugene K. Ressler, Jr.
This file is part of Sketch, a small, simple system for making
3d drawings with LaTeX and the PSTricks or TikZ package.
Sketch is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
Sketch is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Sketch; see the file COPYING.txt. If not, see
http://www.gnu.org/copyleft */
#include <stdio.h>
#include <math.h>
#include "scene.h"
#include "emit.h"
DECLARE_DYNAMIC_2D_ARRAY_FUNCS(POINT_LIST_3D, POINT_3D, FLOAT,
point_list_3d, v, n_pts, NO_OTHER_INIT);
DECLARE_DYNAMIC_2D_ARRAY_FUNCS(TRANSFORM_LIST, TRANSFORM, FLOAT,
transform_list, xf, n_xfs, NO_OTHER_INIT);
DECLARE_DYNAMIC_ARRAY_FUNCS(SPECIAL_ARG_LIST, SPECIAL_ARG,
special_arg_list, arg, n_args, NO_OTHER_INIT);
// this must match the definition of OBJECT_TYPE
char *object_type_str[] = {
"base",
"tag",
"option list",
"scalar",
"point",
"vector",
"transform",
"dots",
"line",
"curve",
"polygon",
"special",
"sweep",
"repeat",
"compound",
};
#define LAY_IN 0
#define LAY_OVER 1
#define LAY_UNDER -1
int lay_val(OPTS * opts, int lay_default)
{
char *val = opt_val(opts, "lay");
if (!val)
return lay_default;
if (strcmp(val, "over") == 0)
return LAY_OVER;
else if (strcmp(val, "under") == 0)
return LAY_UNDER;
else if (strcmp(val, "in") == 0)
return LAY_IN;
else {
warn(no_line, "lay=%s has been ignored", val);
return lay_default;
}
}
OBJECT *new_tag_def(void)
{
TAG_DEF *r = safe_malloc(sizeof *r);
r->tag = O_TAG_DEF;
r->sibling = NULL;
return (OBJECT *) r;
}
OBJECT *new_opts_def_copy(OPTS * opts)
{
OPTS_DEF *r = safe_malloc(sizeof *r);
r->tag = O_OPTS_DEF;
r->sibling = NULL;
r->opts = opts;
return (OBJECT *) r;
}
OBJECT *new_opts_def(char *opts_str, SRC_LINE line)
{
return new_opts_def_copy(new_opts(opts_str, line));
}
OBJECT *new_scalar_def(FLOAT val)
{
SCALAR_DEF *r = safe_malloc(sizeof *r);
r->tag = O_SCALAR_DEF;
r->sibling = NULL;
r->val = val;
return (OBJECT *) r;
}
OBJECT *new_point_def(POINT_3D p)
{
POINT_DEF *r = safe_malloc(sizeof *r);
r->tag = O_POINT_DEF;
r->sibling = NULL;
copy_pt_3d(r->p, p);
return (OBJECT *) r;
}
OBJECT *new_vector_def(VECTOR_3D v)
{
VECTOR_DEF *r = safe_malloc(sizeof *r);
r->tag = O_VECTOR_DEF;
r->sibling = NULL;
copy_vec_3d(r->v, v);
return (OBJECT *) r;
}
OBJECT *new_transform_def(TRANSFORM xf)
{
TRANSFORM_DEF *r = safe_malloc(sizeof *r);
r->tag = O_TRANSFORM_DEF;
r->sibling = NULL;
copy_transform(r->xf, xf);
return (OBJECT *) r;
}
void translate_points(POINT_LIST_3D * dst, OBJECT * src_obj)
{
POINT_DEF *sibling, *src = (POINT_DEF *) src_obj;
while (src) {
copy_pt_3d(pushed_point_list_3d_v(dst), src->p);
sibling = (POINT_DEF *) src->sibling;
safe_free(src);
src = sibling;
}
}
void translate_special_args(SPECIAL_ARG_LIST * dst, OBJECT * src)
{
OBJECT *sibling;
while (src) {
SPECIAL_ARG *arg = pushed_special_arg_list_arg(dst);
switch (src->tag) {
case O_SCALAR_DEF:{
SCALAR_DEF *scalar_def = (SCALAR_DEF *) src;
arg->tag = SA_SCALAR;
arg->val.flt = scalar_def->val;
}
break;
case O_POINT_DEF:{
POINT_DEF *pt_def = (POINT_DEF *) src;
arg->tag = SA_POINT;
copy_pt_3d(arg->val.pt, pt_def->p);
}
break;
case O_VECTOR_DEF:{
VECTOR_DEF *vec_def = (VECTOR_DEF *) src;
arg->tag = SA_VECTOR;
copy_vec_3d(arg->val.v, vec_def->v);
}
break;
default:
die(no_line, "unexpected special arg object %s(%d)",
object_type_str[src->tag], src->tag);
}
sibling = src->sibling;
safe_free(src);
src = sibling;
}
}
DOTS_OBJECT *raw_dots(OPTS * opts)
{
DOTS_OBJECT *r = safe_malloc(sizeof *r);
r->tag = O_DOTS;
r->sibling = NULL;
r->opts = opts;
init_point_list_3d(r->pts);
return r;
}
OBJECT *new_dots(OPTS * opts, OBJECT * pts)
{
DOTS_OBJECT *r = raw_dots(opts);
translate_points(r->pts, pts);
return (OBJECT *) r;
}
OBJECT *copy_dots(OBJECT * obj)
{
DOTS_OBJECT *org = (DOTS_OBJECT *) obj, *r = raw_dots(org->opts);
copy_point_list_3d(r->pts, org->pts);
return (OBJECT *) r;
}
LINE_OBJECT *raw_line(OPTS * opts)
{
LINE_OBJECT *r = safe_malloc(sizeof *r);
r->tag = O_LINE;
r->sibling = NULL;
r->opts = opts;
init_point_list_3d(r->pts);
return r;
}
OBJECT *new_line(OPTS * opts, OBJECT * pts)
{
LINE_OBJECT *r = raw_line(opts);
translate_points(r->pts, pts);
return (OBJECT *) r;
}
OBJECT *copy_line(OBJECT * obj)
{
LINE_OBJECT *org = (LINE_OBJECT *) obj, *r = raw_line(org->opts);
copy_point_list_3d(r->pts, org->pts);
return (OBJECT *) r;
}
CURVE_OBJECT *raw_curve(OPTS * opts)
{
CURVE_OBJECT *r = safe_malloc(sizeof *r);
r->tag = O_CURVE;
r->sibling = NULL;
r->opts = opts;
init_point_list_3d(r->pts);
return r;
}
OBJECT *new_curve(OPTS * opts, OBJECT * pts)
{
CURVE_OBJECT *r = raw_curve(opts);
translate_points(r->pts, pts);
return (OBJECT *) r;
}
OBJECT *copy_curve(OBJECT * obj)
{
CURVE_OBJECT *org = (CURVE_OBJECT *) obj, *r = raw_curve(org->opts);
copy_point_list_3d(r->pts, org->pts);
return (OBJECT *) r;
}
POLYGON_OBJECT *raw_polygon(OPTS * opts)
{
POLYGON_OBJECT *r = safe_malloc(sizeof *r);
r->tag = O_POLYGON;
r->sibling = NULL;
r->opts = opts;
init_point_list_3d(r->pts);
r->border_p = 0;
return r;
}
OBJECT *new_polygon(OPTS * opts, OBJECT * pts)
{
POLYGON_OBJECT *r = raw_polygon(opts);
translate_points(r->pts, pts);
return (OBJECT *) r;
}
OBJECT *copy_polygon(OBJECT * obj)
{
POLYGON_OBJECT *org = (POLYGON_OBJECT *) obj, *r =
raw_polygon(org->opts);
copy_point_list_3d(r->pts, org->pts);
return (OBJECT *) r;
}
static SPECIAL_OBJECT *raw_special(OPTS * opts)
{
SPECIAL_OBJECT *r = safe_malloc(sizeof *r);
r->tag = O_SPECIAL;
r->sibling = NULL;
r->code = NULL;
r->opts = opts;
init_special_arg_list(r->args);
return r;
}
/* The parser may put options anywhere in the arg list. We must
flag options in any location except the first as an error.
In the first location, we unlink from args list and install. */
OBJECT *new_special(char *code, OBJECT * args, SRC_LINE line)
{
SPECIAL_OBJECT *r;
OPTS_DEF *d;
OBJECT *p, *q;
// use the first arg as options if that's what's there
if (args && args->tag == O_OPTS_DEF) {
d = (OPTS_DEF *) args;
r = raw_special(d->opts);
args = args->sibling;
safe_free(d);
} else {
r = raw_special(NULL);
}
// ensure no options are left
q = NULL;
for (p = args; p != NULL; p = p->sibling) {
if (p->tag == O_OPTS_DEF) {
err(line, "unexpected options found in special argument list");
// unlink
if (q == NULL)
args = p->sibling;
else
q->sibling = p->sibling;
} else {
q = p;
}
}
r->code = code;
translate_special_args(r->args, args);
// syntax check
process_special(NULL, r, line);
return (OBJECT *) r;
}
OBJECT *copy_special(OBJECT * obj)
{
SPECIAL_OBJECT *org = (SPECIAL_OBJECT *) obj, *r =
raw_special(org->opts);
copy_special_arg_list(r->args, org->args);
r->code = safe_strdup(org->code);
return (OBJECT *) r;
}
SWEEP_OBJECT *raw_sweep(OPTS * opts)
{
SWEEP_OBJECT *r = safe_malloc(sizeof *r);
r->tag = O_SWEEP;
r->sibling = NULL;
r->n_slices = 0;
r->closed_p = 0;
init_transform_list(r->xforms);
r->opts = opts;
r->swept = NULL;
return r;
}
void translate_transforms(TRANSFORM_LIST * dst, OBJECT * src_obj)
{
TRANSFORM_DEF *sibling, *src = (TRANSFORM_DEF *) src_obj;
while (src) {
copy_transform(pushed_transform_list_xf(dst), src->xf);
sibling = (TRANSFORM_DEF *) src->sibling;
safe_free(src);
src = sibling;
}
}
OBJECT *new_sweep(OPTS * opts, int n_slices, int closed_p, OBJECT * xfs,
OBJECT * swept)
{
SWEEP_OBJECT *r = raw_sweep(opts);
r->n_slices = n_slices;
r->closed_p = closed_p;
translate_transforms(r->xforms, xfs);
r->swept = swept;
return (OBJECT *) r;
}
// this is a shallow copy
OBJECT *copy_sweep(OBJECT * obj)
{
SWEEP_OBJECT *org = (SWEEP_OBJECT *) obj, *r = raw_sweep(org->opts);
r->n_slices = org->n_slices;
r->closed_p = org->closed_p;
copy_transform_list(r->xforms, org->xforms);
r->swept = org->swept;
return (OBJECT *) r;
}
REPEAT_OBJECT *raw_repeat(void)
{
REPEAT_OBJECT *r = safe_malloc(sizeof *r);
r->tag = O_REPEAT;
r->sibling = NULL;
r->n = 0;
init_transform_list(r->xforms);
r->repeated = NULL;
return r;
}
OBJECT *new_repeat(int n, OBJECT * xfs, OBJECT * repeated)
{
REPEAT_OBJECT *r = raw_repeat();
r->n = n;
translate_transforms(r->xforms, xfs);
r->repeated = repeated;
return (OBJECT *) r;
}
OBJECT *copy_repeat(OBJECT * obj)
{
REPEAT_OBJECT *org = (REPEAT_OBJECT *) obj, *r = raw_repeat();
r->n = org->n;
copy_transform_list(r->xforms, org->xforms);
r->repeated = org->repeated; // shallow copy
return (OBJECT *) r;
}
OBJECT *new_compound(TRANSFORM xform, OBJECT * child)
{
COMPOUND_OBJECT *r = safe_malloc(sizeof *r);
r->tag = O_COMPOUND;
r->sibling = NULL;
copy_transform(r->xform, xform);
r->child = child;
return (OBJECT *) r;
}
// this is a shallow copy
OBJECT *copy_compound(OBJECT * obj)
{
COMPOUND_OBJECT *org = (COMPOUND_OBJECT *) obj;
return new_compound(org->xform, org->child);
}
typedef OBJECT *(*COPY_FUNC) (OBJECT *);
static COPY_FUNC copy_tbl[] = {
NULL, // O_BASE
NULL, // O_TAG_DEF
NULL, // O_OPTS_DEF
NULL, // O_SCALAR_DEF
NULL, // O_POINT_DEF
NULL, // O_VECTOR_DEF
NULL, // O_TRANSFORM_DEF
copy_dots,
copy_line,
copy_curve,
copy_polygon,
copy_special,
copy_sweep,
copy_repeat,
copy_compound,
};
OBJECT *copy_drawable_without_siblings(OBJECT * obj)
{
return (*copy_tbl[obj->tag]) (obj);
}
OBJECT *copy_drawable(OBJECT * obj)
{
OBJECT *r = NULL;
while (obj) {
if (copy_tbl[obj->tag]) {
OBJECT *copy = copy_drawable_without_siblings(obj);
copy->sibling = r;
r = copy;
} else {
die(no_line, "copy_drawable: attempt to copy non-drawable %s",
object_type_str[obj->tag]);
}
obj = obj->sibling;
}
return sibling_reverse(r);
}
OBJECT *sibling_reverse(OBJECT * obj)
{
OBJECT *p, *q, *t;
// pop from p and push onto q until p is empty
p = obj;
q = NULL;
while (p) {
t = p;
p = p->sibling; // pop
t->sibling = q;
q = t; // push
}
return q;
}
OBJECT *object_from_expr(EXPR_VAL * val)
{
switch (val->tag) {
case E_FLOAT:
return new_scalar_def(val->val.flt);
case E_POINT:
return new_point_def(val->val.pt);
case E_VECTOR:
return new_vector_def(val->val.vec);
case E_TRANSFORM:
return new_transform_def(val->val.xf);
default:
die(no_line, "object_from_expr: unknown value tag %d", val->tag);
}
return NULL; // never occurs
}
void
transform_points(POINT_LIST_3D * dst_pts, TRANSFORM xf,
POINT_LIST_3D * src_pts)
{
int i;
setup_point_list_3d(dst_pts, src_pts->n_pts);
for (i = 0; i < src_pts->n_pts; i++)
transform_pt_3d(dst_pts->v[i], xf, src_pts->v[i]);
dst_pts->n_pts = src_pts->n_pts;
}
void
transform_special_args(SPECIAL_ARG_LIST * dst_args, TRANSFORM xf,
SPECIAL_ARG_LIST * src_args)
{
int i;
setup_special_arg_list(dst_args, src_args->n_args);
for (i = 0; i < src_args->n_args; i++) {
switch (dst_args->arg[i].tag = src_args->arg[i].tag) {
case SA_SCALAR:
dst_args->arg[i].val.flt = src_args->arg[i].val.flt;
break;
case SA_POINT:
transform_pt_3d(dst_args->arg[i].val.pt, xf,
src_args->arg[i].val.pt);
break;
case SA_VECTOR:
transform_vec_3d(dst_args->arg[i].val.v, xf,
src_args->arg[i].val.v);
break;
default:
die(no_line, "unexpected special arg type");
}
}
dst_args->n_args = src_args->n_args;
}
static void
fill_transform_accum(TRANSFORM_LIST * accum, TRANSFORM_LIST * inc)
{
int i;
setup_transform_list(accum, inc->n_xfs);
accum->n_xfs = inc->n_xfs;
for (i = 0; i < inc->n_xfs; i++)
set_ident(accum->xf[i]);
}
static void
advance_transform_accum(TRANSFORM_LIST * accum, TRANSFORM_LIST * inc)
{
int i;
for (i = 0; i < accum->n_xfs; i++)
compose(accum->xf[i], accum->xf[i], inc->xf[i]);
}
static void
compose_transform_accum(TRANSFORM xf, TRANSFORM_LIST * accum,
TRANSFORM model_view_xf)
{
int i;
if (accum->n_xfs <= 0)
die(no_line, "zero size accumulator");
copy_transform(xf, accum->xf[0]);
// left-multiply because accumulator is in "then" order
for (i = 1; i < accum->n_xfs; i++)
compose(xf, accum->xf[i], xf);
if (model_view_xf)
compose(xf, model_view_xf, xf);
}
OBJECT *flat_dots(OBJECT * obj, TRANSFORM xf)
{
DOTS_OBJECT *s = (DOTS_OBJECT *) obj, *dots = raw_dots(s->opts);
transform_points(dots->pts, xf, s->pts);
return (OBJECT *) dots;
}
OBJECT *flat_line(OBJECT * obj, TRANSFORM xf)
{
LINE_OBJECT *s = (LINE_OBJECT *) obj, *line = raw_line(s->opts);
check_opts(s->opts, OPT_INTERNAL | OPT_LINE,
"unknown line option %s=%s will be ignored",
global_env->output_language, no_line);
transform_points(line->pts, xf, s->pts);
return (OBJECT *) line;
}
OBJECT *flat_curve(OBJECT * obj, TRANSFORM xf)
{
CURVE_OBJECT *s = (CURVE_OBJECT *) obj, *curve = raw_curve(s->opts);
transform_points(curve->pts, xf, s->pts);
return (OBJECT *) curve;
}
OBJECT *flat_polygon(OBJECT * obj, TRANSFORM xf)
{
POLYGON_OBJECT *s = (POLYGON_OBJECT *) obj,
*polygon = raw_polygon(s->opts);
check_opts(s->opts, OPT_INTERNAL | OPT_POLYGON | OPT_LINE,
"unknown polygon option %s=%s will be ignored",
global_env->output_language, no_line);
transform_points(polygon->pts, xf, s->pts);
return (OBJECT *) polygon;
}
OBJECT *flat_special(OBJECT * obj, TRANSFORM xf)
{
SPECIAL_OBJECT *s = (SPECIAL_OBJECT *) obj,
*special = raw_special(s->opts);
special->code = safe_strdup(s->code);
transform_special_args(special->args, xf, s->args);
return (OBJECT *) special;
}
#define MAX_WARP 1e-5
// return -1 if no split is necessary
// return 0 if best spilt is on the 0--2 line
// return 1 if best split is on the 1--3 line
static int
best_triangle_split(POINT_3D v0, POINT_3D v1, POINT_3D v2, POINT_3D v3)
{
VECTOR_3D n, d0, d1, e, e_max;
FLOAT e_len_sqr, e_max_len_sqr, warp;
sub_vecs_3d(d0, v2, v0);
sub_vecs_3d(d1, v3, v1);
cross(n, d0, d1);
// if the cross product is zero length, the polygon is degenerate and can
// be considered flat; no need to traingulate
if (!find_unit_vec_3d(n, n))
return -1;
// find the edge of maximum length; probably not necessary
sub_vecs_3d(e_max, v1, v0);
e_max_len_sqr = dot_3d(e_max, e_max);
sub_vecs_3d(e, v2, v1);
e_len_sqr = dot_3d(e, e);
if (e_len_sqr > e_max_len_sqr) {
e_max_len_sqr = e_len_sqr;
copy_vec_3d(e_max, e);
}
sub_vecs_3d(e, v3, v2);
e_len_sqr = dot_3d(e, e);
if (e_len_sqr > e_max_len_sqr) {
e_max_len_sqr = e_len_sqr;
copy_vec_3d(e_max, e);
}
sub_vecs_3d(e, v0, v3);
e_len_sqr = dot_3d(e, e);
if (e_len_sqr > e_max_len_sqr) {
e_max_len_sqr = e_len_sqr;
copy_vec_3d(e_max, e);
}
// flat if projection of edge on normal is small, else split on shortest diagonal
warp = dot_3d(e_max, n);
return
-MAX_WARP <= warp && warp <= MAX_WARP ? -1 :
dot_3d(d0, d0) < dot_3d(d1, d1) ? 0 : 1;
}
// add triangular or quadrilateral faces to object list depending on flatness
static void
make_faces(OBJECT ** r, OPTS * opts, TRANSFORM xf,
POINT_3D v0, POINT_3D v1, POINT_3D v2, POINT_3D v3, int split_p)
{
POLYGON_OBJECT *new_polygon;
if (!split_p)
goto no_split;
switch (best_triangle_split(v0, v1, v2, v3)) {
case -1:
no_split:
new_polygon = raw_polygon(opts);
setup_point_list_3d(new_polygon->pts, 4);
transform_pt_3d(new_polygon->pts->v[0], xf, v0);
transform_pt_3d(new_polygon->pts->v[1], xf, v1);
transform_pt_3d(new_polygon->pts->v[2], xf, v2);
transform_pt_3d(new_polygon->pts->v[3], xf, v3);
new_polygon->pts->n_pts = 4;
new_polygon->sibling = *r;
*r = (OBJECT *) new_polygon;
break;
case 0:
new_polygon = raw_polygon(opts);
setup_point_list_3d(new_polygon->pts, 3);
transform_pt_3d(new_polygon->pts->v[0], xf, v0);
transform_pt_3d(new_polygon->pts->v[1], xf, v1);
transform_pt_3d(new_polygon->pts->v[2], xf, v2);
new_polygon->pts->n_pts = 3;
new_polygon->sibling = *r;
*r = (OBJECT *) new_polygon;
new_polygon = raw_polygon(opts);
setup_point_list_3d(new_polygon->pts, 3);
transform_pt_3d(new_polygon->pts->v[0], xf, v2);
transform_pt_3d(new_polygon->pts->v[1], xf, v3);
transform_pt_3d(new_polygon->pts->v[2], xf, v0);
new_polygon->pts->n_pts = 3;
new_polygon->sibling = *r;
*r = (OBJECT *) new_polygon;
break;
case 1:
new_polygon = raw_polygon(opts);
setup_point_list_3d(new_polygon->pts, 3);
transform_pt_3d(new_polygon->pts->v[0], xf, v1);
transform_pt_3d(new_polygon->pts->v[1], xf, v2);
transform_pt_3d(new_polygon->pts->v[2], xf, v3);
new_polygon->pts->n_pts = 3;
new_polygon->sibling = *r;
*r = (OBJECT *) new_polygon;
new_polygon = raw_polygon(opts);
setup_point_list_3d(new_polygon->pts, 3);
transform_pt_3d(new_polygon->pts->v[0], xf, v3);
transform_pt_3d(new_polygon->pts->v[1], xf, v0);
transform_pt_3d(new_polygon->pts->v[2], xf, v1);
new_polygon->pts->n_pts = 3;
new_polygon->sibling = *r;
*r = (OBJECT *) new_polygon;
break;
}
}
OBJECT *flat_sweep(OBJECT * obj, TRANSFORM xf)
{
int i, j, jj, split_p;
POINT_LIST_3D *a, *b, *t;
OBJECT *swept, *r;
TRANSFORM sweep_xf;
POINT_LIST_3D pts_1[1], pts_2[1];
TRANSFORM_LIST sweep_accum[1];
SWEEP_OBJECT *s = (SWEEP_OBJECT *) obj;
init_point_list_3d(pts_1);
init_point_list_3d(pts_2);
init_transform_list(sweep_accum);
split_p = bool_opt_p(s->opts, "split", 1)
&& bool_opt_p(global_env->opts, "split", 1);
r = NULL;
#define ADD_TO_OUTPUT(O) do { \
(O)->sibling = r; \
r = (OBJECT*)(O); \
} while (0)
// handle definitions first; a point becomes a single line or a polygon
if (s->swept->tag == O_POINT_DEF) {
fill_transform_accum(sweep_accum, s->xforms);
for (swept = s->swept; swept; swept = swept->sibling) {
POINT_DEF *pd = (POINT_DEF *) swept;
if (s->closed_p) {
POLYGON_OBJECT *polygon = raw_polygon(s->opts);
for (i = 0; i < s->n_slices; i++) {
compose_transform_accum(sweep_xf, sweep_accum, xf);
transform_pt_3d(pushed_point_list_3d_v(polygon->pts),
sweep_xf, pd->p);
advance_transform_accum(sweep_accum, s->xforms);
}
ADD_TO_OUTPUT(polygon);
} else {
LINE_OBJECT *line = raw_line(s->opts);
for (i = 0; i < s->n_slices + 1; i++) {
compose_transform_accum(sweep_xf, sweep_accum, xf);
transform_pt_3d(pushed_point_list_3d_v(line->pts),
sweep_xf, pd->p);
advance_transform_accum(sweep_accum, s->xforms);
}
ADD_TO_OUTPUT(line);
}
}
} else {
// it's drawable; recursively flatten swept object in its own coordinates
for (swept = flat_scene(s->swept, NULL); swept;
swept = swept->sibling) {
// refill with identity for each swept object
fill_transform_accum(sweep_accum, s->xforms);
// now the different flavors of sweep depend on what's being swept and
// the setting of the closure tag
if (swept->tag == O_LINE) {
// a line becomes a surface represented by a sequence of 4-sided polygons
LINE_OBJECT *line = (LINE_OBJECT *) swept;
// a is the trail buffer and b the lead
a = pts_1;
b = pts_2;
copy_point_list_3d(a, line->pts);
if (s->closed_p) {
POLYGON_OBJECT *e1 = raw_polygon(s->opts);
POLYGON_OBJECT *e2 = raw_polygon(s->opts);
OPTS *face_opts = line->opts ? line->opts : s->opts;
// set up in advance; e1 is filled in forward, e2 in reverse
setup_point_list_3d(e1->pts, s->n_slices);
e1->pts->n_pts = s->n_slices;
setup_point_list_3d(e2->pts, s->n_slices);
e2->pts->n_pts = s->n_slices;
for (i = 0; i < s->n_slices - 1; i++) {
advance_transform_accum(sweep_accum, s->xforms);
compose_transform_accum(sweep_xf, sweep_accum, 0); // apply mv transform in make_faces
transform_points(b, sweep_xf, line->pts);
// copy first and last points for 'end caps'
transform_pt_3d(e1->pts->v[i], xf,
b->v[b->n_pts - 1]);
transform_pt_3d(e2->pts->v[s->n_slices - 1 - i],
xf, b->v[0]);
for (jj = 0, j = 1; j < a->n_pts; jj = j++)
make_faces(&r, face_opts, xf, b->v[jj],
b->v[j], a->v[j], a->v[jj],
split_p);
t = a;
a = b;
b = t; // swap a and b for next pass
}
// closure: add last point of original line. first to ends, then as faces
transform_pt_3d(e1->pts->v[i], xf,
line->pts->v[line->pts->n_pts - 1]);
transform_pt_3d(e2->pts->v[0], xf, line->pts->v[0]);
for (jj = 0, j = 1; j < a->n_pts; jj = j++)
make_faces(&r, face_opts, xf, line->pts->v[jj],
line->pts->v[j], a->v[j], a->v[jj],
split_p);
// add ends to output
ADD_TO_OUTPUT(e1);
ADD_TO_OUTPUT(e2);
} else {
for (i = 0; i < s->n_slices; i++) {
advance_transform_accum(sweep_accum, s->xforms);
compose_transform_accum(sweep_xf, sweep_accum, 0);
transform_points(b, sweep_xf, line->pts);
for (jj = 0, j = 1; j < a->n_pts; jj = j++)
make_faces(&r, s->opts, xf, b->v[jj], b->v[j],
a->v[j], a->v[jj], split_p);
t = a;
a = b;
b = t; // swap a and b for next pass
}
}
} else if (swept->tag == O_POLYGON) {
// a polygon becomes a surface represented by a sequence of 4-sided polygons (with "end caps")
POLYGON_OBJECT *new_polygon, *polygon =
(POLYGON_OBJECT *) swept;
OPTS *end_opts = polygon->opts ? polygon->opts : s->opts;
if (s->closed_p)
warn(no_line,
"closure tag on polygon sweep ignored (sorry, no line number)");
a = pts_1;
b = pts_2;
copy_point_list_3d(a, polygon->pts);
// initial end cap
new_polygon = raw_polygon(end_opts);
transform_points(new_polygon->pts, xf, a);
ADD_TO_OUTPUT(new_polygon);
for (i = 0; i < s->n_slices; i++) {
advance_transform_accum(sweep_accum, s->xforms);
compose_transform_accum(sweep_xf, sweep_accum, 0);
transform_points(b, sweep_xf, polygon->pts);
for (jj = a->n_pts - 1, j = 0; j < a->n_pts; jj = j++)
make_faces(&r, s->opts, xf, b->v[jj], b->v[j],
a->v[j], a->v[jj], split_p);
t = a;
a = b;
b = t; // swap a and b for next pass
}
// final end cap is copy of a (note reverse point order)
new_polygon = raw_polygon(end_opts);
reverse_copy_point_list_3d(new_polygon->pts, a);
transform_points(new_polygon->pts, xf, new_polygon->pts);
ADD_TO_OUTPUT(new_polygon);
} else {
warn(no_line,
"cannot sweep a %s; object ignored (sorry, no line number)",
object_type_str[swept->tag]);
}
}
}
clear_point_list_3d(pts_1);
clear_point_list_3d(pts_2);
clear_transform_list(sweep_accum);
return r;
#undef ADD_TO_OUTPUT
}
// forward declaration
static OBJECT *rev_transformed_flat_scene(OBJECT * obj, TRANSFORM xf);
OBJECT *flat_repeat(OBJECT * obj, TRANSFORM xf)
{
int i;
REPEAT_OBJECT *s = (REPEAT_OBJECT *) obj;
OBJECT *flat_repeated, *r;
TRANSFORM_LIST repeat_accum[1];
TRANSFORM child_xf;
init_transform_list(repeat_accum);
if (s->n <= 0)
return NULL;
// recursively flatten repeated object in its own coordinates
flat_repeated = flat_scene(s->repeated, NULL);
fill_transform_accum(repeat_accum, s->xforms);
r = NULL;
for (i = 0; i < s->n; i++) {
compose_transform_accum(child_xf, repeat_accum, xf);
r = cat_objects(rev_transformed_flat_scene
(flat_repeated, child_xf), r);
advance_transform_accum(repeat_accum, s->xforms);
}
// flat_repeated is a memory leak
return r;
}
OBJECT *flat_compound(OBJECT * obj, TRANSFORM xf)
{
COMPOUND_OBJECT *compound = (COMPOUND_OBJECT *) obj;
TRANSFORM child_xf;
compose(child_xf, xf, compound->xform);
return rev_transformed_flat_scene(compound->child, child_xf);
}
typedef OBJECT *(*FLATTEN_FUNC) (OBJECT *, TRANSFORM);
static FLATTEN_FUNC flatten_tbl[] = {
NULL, // O_BASE
NULL, // O_TAG_DEF
NULL, // O_OPTS_DEF
NULL, // O_SCALAR_DEF
NULL, // O_POINT_DEF
NULL, // O_VECTOR_DEF
NULL, // O_TRANSFORM_DEF
flat_dots,
flat_line,
flat_curve,
flat_polygon,
flat_special,
flat_sweep,
flat_repeat,
flat_compound,
};
OBJECT *cat_objects(OBJECT * lft, OBJECT * rgt)
{
OBJECT *p;
if (!lft)
return rgt;
for (p = lft; p->sibling; p = p->sibling)
/* skip */ ;
p->sibling = rgt;
return lft;
}
static OBJECT *rev_transformed_flat_scene(OBJECT * obj, TRANSFORM xf)
{
OBJECT *r = NULL;
while (obj) {
// flatten the object
if (flatten_tbl[obj->tag] == NULL)
die(no_line, "rev_transformed_flat_scene: bad tag %d",
obj->tag);
// join scene sibling lists
r = cat_objects((*flatten_tbl[obj->tag]) (obj, xf), r);
// on to next object
obj = obj->sibling;
}
return r;
}
// call with null env omits camera transformation
OBJECT *flat_scene(OBJECT * obj, GLOBAL_ENV * env)
{