forked from kashif/evolver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpainter.c
3100 lines (2737 loc) · 92.9 KB
/
painter.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
/*************************************************************
* This file is part of the Surface Evolver source code. *
* Programmer: Ken Brakke, [email protected] *
*************************************************************/
/*****************************************************************
*
* File: painter.c
*
* Contents: Routines to accumulate and sort facets for display
* back to front. Not device specific, but calls
* device specific routines for actual display.
* Also does transformations on edges.
* painter_end() uses Newell-Newell-Sancha algorithm
* to depth sort and display facets.
* This version uses a separate quadtree of depth-ordered
* lists from the drawing-order list to minimize comparisons
* needed for large elements.
*/
/* Some timings with version 2.18j on starfish31.dmp (2560 facets)
Without visibility test:
Shape Fund. time,sec filesize
Fund 1 0.093 190,829
Cubelet 12 1.08 2,276,025
Cube 48 4.64 9,100,293
Rhomb 96 9.625 18,199,633
With visibility test:
Shape Fund. time,sec filesize
Fund 1 0.165 168,332
Cubelet 12 1.89 1,444,778
Cube 48 9.56 4,456,944
Rhomb 96 24.7 7,162,980
*/
#include "include.h"
static size_t count; /* number of facets */
static size_t maxcount; /* number allocated */
struct tsort *trilist; /* list for depth sorting triangles */
static int gdim = 3; /* dimension doing graphics in */
static int painter_multiple_sweep_flag; // for really big surfaces
#define MULTIPLE_SWEEP_PREP 2
#define MULTIPLE_SWEEP_GO 3
#define MULTIPLE_SWEEP_DONE 4
#define SWEEPBINS 1000
static size_t sweep_mins[SWEEPBINS], sweep_maxs[SWEEPBINS];
static REAL sweep_low = -10.0;
static REAL sweep_high = 10.0;
static REAL sweep_delta;
static REAL sweep_top; // current cutoff min
static REAL sweep_bottom; // previous cutoff
static size_t sweep_k; // keeping track in sweep_mins[]
static size_t sweep_total,sweep_done; // for reporting progress
/* results of comparing depth of facets */
#define DISJOINT 1
#define FIRST_BACK 2
#define SECOND_BACK 4
#define ASPLITTINGB 8
#define BSPLITTINGA 16
#define NOTKNOWN 32
#define COPLANAR 64
#define TEXTRA 100
static struct tsort textra[TEXTRA]; /* for triangle fragments */
/* for debugging; just displays list as is after given number of facets */
size_t debug_k = 0x7FFFFFFF;
REAL tableau[7][3]; /* simplex tableau */
void pivot(int ,int );
int newell_split(struct tsort *,struct tsort *,struct tsort *,struct tsort *);
int backstamp; /* for timestamping being at back of list */
int plane_test(struct tsort *,struct tsort *);
int setquadcode(struct tsort *);
void find_bbox(struct tsort *);
int separating_line(struct tsort*,struct tsort*);
int separating_plane(struct tsort*,struct tsort*,int);
static int ttcompare( /* depth comparison for sort */
struct tsort **t1,
struct tsort **t2
)
{
/* First, compare back z */
if ( t1[0]->mins[2] > t2[0]->mins[2] ) return 1;
if ( t1[0]->mins[2] < t2[0]->mins[2] ) return -1;
/* Break ties with front z */
if ( t1[0]->maxs[2] > t2[0]->maxs[2] ) return 1;
if ( t1[0]->maxs[2] < t2[0]->maxs[2] ) return -1;
/* Finally, break ties with id to get consistent ordering,
independent of how many elements are being sorted */
if ( t1[0]->f_id > t2[0]->f_id ) return 1;
if ( t1[0]->f_id < t2[0]->f_id ) return -1;
return 0;
} // end ttcompare()
/* quadtree of depth lists */
struct qtree_t { struct tsort *depthhead;
float maxdepth; /* of all in subtree */
} *qtree;
int maxquaddepth = 8; /* maximum depth of quadtree */
int get_quadindex(unsigned int);
void qdepth_insert(struct tsort *);
struct tsort *search_subtree(int,struct tsort *,int *);
/* visibility stuff */
void visibility_stage(struct tsort *);
void visibility_end(void);
size_t vis_count; /* used structures */
size_t vis_max; /* allocated structures */
struct tsort *vis_list; /* storage */
/*****************************************************************
*
* Function: find_bbox()
*
* Purpose: find 3D bounding box for edge or facet.
*
*/
void find_bbox(struct tsort *t)
{ int n;
REAL dx,dy,len;
if ( (t->flag & 0xF) == EDGE )
{
if ( t->flag & EDGE_ARC )
{ REAL w1[MAXCOORD],w2[MAXCOORD],mag1,mag2,w1w2,center[2],radius;
REAL det,angle1,angle2;
int i;
for (i = 0 ; i < SDIM ; i++ )
{ w1[i] = t->x[1][i] - t->x[0][i];
w2[i] = t->x[2][i] - t->x[0][i];
}
det = w1[0]*w2[1] - w1[1]*w2[0];
mag1 = SDIM_dot(w1,w1); mag2 = SDIM_dot(w2,w2);
w1w2 = w1[0]*w2[0] + w1[1]*w2[1];
for ( n = 0 ; n < gdim ; n++ )
{ /* endpoints first */
if ( t->x[0][n] < t->x[2][n] )
{ t->maxs[n] = t->x[2][n];
t->mins[n] = t->x[0][n];
}
else
{ t->maxs[n] = t->x[0][n];
t->mins[n] = t->x[2][n];
}
}
if ( 4000*det*det > mag1*mag1*mag2 + mag1*mag2*mag2 - 2*mag1*w1w2*mag2 )
{ /* circle rather that straight line */
center[0] = t->x[0][0] + 0.5*(w2[1]*mag1 - w1[1]*mag2)/det;
center[1] = t->x[0][1] + 0.5*(-w2[0]*mag1 + w1[0]*mag2)/det;
radius = sqrt((mag1*mag1*mag2+mag1*mag2*mag2-2*mag1*w1w2*mag2)
/4/det/det);
angle1 = atan2(t->x[0][1]-center[1],t->x[0][0]-center[0]);
angle2 = atan2(t->x[2][1]-center[1],t->x[2][0]-center[0]);
if ( det < 0 )
{ REAL temp = angle1; angle1 = angle2; angle2 = temp; }
if ( angle2 < angle1 ) angle2 += 2*M_PI;
if ( (angle1 < 0.0 && angle2 > 0.0 ) ||
(angle1 < 2*M_PI && angle2 > 2*M_PI) )
t->maxs[0] = (float)(center[0] + radius);
if ( (angle1 < M_PI && angle2 > M_PI ) ||
(angle1 < 3*M_PI && angle2 > 3*M_PI) )
t->mins[0] = (float)(center[0] - radius);
if ( (angle1 < M_PI/2 && angle2 > M_PI/2 ) ||
(angle1 < 5*M_PI/2 && angle2 > 5*M_PI/2) )
t->maxs[1] = (float)(center[1] + radius);
if ( (angle1 < -M_PI/2 && angle2 > -M_PI/2 ) ||
(angle1 < 3*M_PI/2 && angle2 > 3*M_PI/2) )
t->mins[1] = (float)(center[1] - radius);
return;
}
} /* end EDGE_ARC */
else /* just a straight segment */
for ( n = 0 ; n < gdim ; n++ )
{ if ( t->x[0][n] < t->x[1][n] )
{ t->maxs[n] = t->x[1][n];
t->mins[n] = t->x[0][n];
}
else
{ t->maxs[n] = t->x[0][n];
t->mins[n] = t->x[1][n];
}
}
/* adjust extents for thickness */
dx = fabs(t->x[1][0] - t->x[0][0]);
dy = fabs(t->x[1][1] - t->x[0][1]);
len = sqrt(dx*dx+dy*dy);
if ( len > 0.0 )
{ t->maxs[0] += (float)(dy/len*t->width/2);
t->mins[0] -= (float)(dy/len*t->width/2);
t->maxs[1] += (float)(dx/len*t->width/2);
t->mins[1] -= (float)(dx/len*t->width/2);
}
return;
} /* end EDGE */
/* facet */
for ( n = 0 ; n < gdim ; n++ )
{ if ( t->x[0][n] < t->x[1][n] )
{ if ( t->x[2][n] < t->x[0][n] )
{ t->maxs[n] = t->x[1][n];
t->mins[n] = t->x[2][n];
}
else if ( t->x[1][n] < t->x[2][n] )
{ t->maxs[n] = t->x[2][n];
t->mins[n] = t->x[0][n];
}
else
{ t->maxs[n] = t->x[1][n];
t->mins[n] = t->x[0][n];
}
}
else
{ if ( t->x[2][n] < t->x[1][n] )
{ t->maxs[n] = t->x[0][n];
t->mins[n] = t->x[2][n];
}
else if ( t->x[0][n] < t->x[2][n] )
{ t->maxs[n] = t->x[2][n];
t->mins[n] = t->x[1][n];
}
else
{ t->maxs[n] = t->x[0][n];
t->mins[n] = t->x[1][n];
}
}
}
} // end find_bbox()
/*************************************************************************
* Function: setquadcode()
* Purpose: For setting quadtree code and checking if in bounding box.
* Coded for 32 bit ints.
*/
#define OUTOFBOX 0
#define INTHEBOX 1
int setquadcode(struct tsort *t)
{ unsigned int q = 0; /* the quadcode */
unsigned int bit = 1; /* for shifting to quad bit position */
int n;
REAL midx = (minclipx+maxclipx)/2;
REAL midy = (minclipy+maxclipy)/2;
REAL deltax = (maxclipx-minclipx)/4;
REAL deltay = (maxclipy-minclipy)/4;
if ( t->maxs[0] < minclipx || t->mins[0] > maxclipx || t->maxs[1] < minclipy
|| t->mins[1] > maxclipy ) return OUTOFBOX;
for ( n = 0 ; n < 8 ; n++, deltax /= 2, deltay /= 2 )
{
if ( t->maxs[0] <= midx )
{ q |= bit; midx -= deltax; }
else if ( t->mins[0] >= midx )
{ q |= bit<<1; midx += deltax; }
else break;
bit <<= 2;
if ( t->maxs[1] <= midy )
{ q |= bit; midy -= deltay; }
else if ( t->mins[1] >= midy )
{ q |= bit<<1; midy += deltay; }
else break;
bit <<= 2;
}
t->quadcode = q;
return INTHEBOX;
} // end setquadcode()
/*************************************************************************
* Function: multiple_sweep_prep()
*
* Purpose: Generate graphics items once to count and find bins.
*/
void multiple_sweep_prep()
{ size_t total;
int k;
bbox_minx = bbox_miny = 1e20;
bbox_maxx = bbox_maxy = -1e20;
memset((void*)sweep_mins,0,sizeof(sweep_mins));
memset((void*)sweep_maxs,0,sizeof(sweep_maxs));
sweep_delta = (sweep_high - sweep_low)/SWEEPBINS;
painter_multiple_sweep_flag = MULTIPLE_SWEEP_PREP;
raw_generate();
// see if we are ever going to have more than maxcount
// items in a sweep
for ( total = 0, k = 0 ; k < SWEEPBINS ; k++ )
{ total += sweep_mins[k] - (k ? sweep_maxs[k-1] : 0);
if ( total > maxcount )
kb_error(5532,"depth sort: too many items in sweep.\n",
RECOVERABLE);
sweep_total += sweep_mins[k];
}
// Now set up for first sweep
count = 0;
sweep_done = 0;
for ( total = 0, k = 0 ; k < SWEEPBINS ; k++ )
{ if ( total + sweep_mins[k] > maxcount )
break;
total += sweep_mins[k];
}
sweep_k = k;
sweep_done = total;
sweep_top = sweep_low + (k-1)*sweep_delta;
sweep_bottom = -1e30;
painter_multiple_sweep_flag = MULTIPLE_SWEEP_GO;
(*init_graphics)(); // print out postscript header
erroutstring("Starting first sweep.\n");
} // end multiple_sweep_prep()
/*************************************************************************
* Function: sweep_prep_one()
*
* Purpose: tally one tsort structure in initial sweep
*/
void sweep_prep_one(struct tsort *t)
{
int bin = (int)((t->mins[SDIM-1] - sweep_low)/sweep_delta);
if ( bin < 0 )
bin = 0;
if ( bin >= SWEEPBINS )
bin = SWEEPBINS - 1;
sweep_mins[bin]++;
bin = (int)((t->maxs[SDIM-1] - sweep_low)/sweep_delta);
if ( bin < 0 )
bin = 0;
if ( bin >= SWEEPBINS )
bin = SWEEPBINS - 1;
sweep_maxs[bin]++;
/* find bounding box */
if ( need_bounding_box )
{
{ if ( t->mins[0] < bbox_minx ) bbox_minx = (REAL)t->mins[0];
if ( t->mins[1] < bbox_miny ) bbox_miny = (REAL)t->mins[1];
if ( t->maxs[0] > bbox_maxx ) bbox_maxx = (REAL)t->maxs[0];
if ( t->maxs[1] > bbox_maxy ) bbox_maxy = (REAL)t->maxs[1];
}
}
} // end sweep_prep_one()
/*************************************************************************
* Function: reset_multiple_sweep()
*
* Purpose: reset trilist in preparation for next sweep of multiple sweep
*/
void reset_multiple_sweep()
{ size_t k,j,total;
sprintf(msg,"Sweep completed. Done %lld of %lld items.\n",(long long)sweep_done,
(long long)sweep_total);
erroutstring(msg);
if ( sweep_k >= SWEEPBINS )
{ painter_multiple_sweep_flag = MULTIPLE_SWEEP_DONE;
return;
}
// compactify left-over trilist, with extra ones tacked on at end
for ( k = 0, j = 0 ; k < count ; k++ )
if ( trilist[k].flag )
trilist[j++] = trilist[k];
for ( k = 0 ; k < TEXTRA ; k++ )
if ( textra[k].flag )
trilist[j++] = textra[k];
memset((void*)(trilist+j),0,(maxcount-j)*sizeof(struct tsort));
memset((void*)(textra),0,TEXTRA*sizeof(struct tsort));
count = j-TEXTRA;
// Now get bounds for next sweep
for ( total = count, k = sweep_k ; k < SWEEPBINS ; k++ )
{ if ( total + sweep_mins[k] > maxcount )
break;
total += sweep_mins[k];
sweep_done += sweep_mins[k];
}
sweep_k = k;
sweep_bottom = sweep_top;
if ( sweep_k >= SWEEPBINS )
sweep_top = 1e30;
else
sweep_top = sweep_low + (k-1)*sweep_delta;
} // end reset_multiple_sweep()
/*************************************************************************
* Function: painter_start()
* Purpose: Start routine called by graphgen()
*/
void painter_start()
{ int dummy;
size_t allocsize;
gdim = (SDIM <= 3) ? SDIM : 3;
/* allocate space for depth sort list */
if ( web.representation == STRING )
{ maxcount = web.skel[EDGE].count + 5;
if ( web.torus_flag ) maxcount *= 2;
}
else
{ if ( web.torus_flag )
if ( torus_display_mode == TORUS_CLIPPED_MODE )
maxcount = 5*web.skel[FACET].count+ bare_edge_count + 5;
else maxcount = 2*web.skel[FACET].count+ bare_edge_count + 5;
else
maxcount = web.skel[FACET].count + bare_edge_count + 5;
}
if ( transforms_flag ) maxcount *= transform_count;
if ( web.dimension > 2 )
maxcount *= web.dimension+1; /* each simplex face becomes facet */
allocsize = (long)maxcount*sizeof(struct tsort);
painter_multiple_sweep_flag = 0;
#ifdef WIN32
{ MEMORYSTATUSEX statex;
statex.dwLength = sizeof (statex);
GlobalMemoryStatusEx (&statex);
if ( allocsize > statex.ullAvailPhys/4 )
{ painter_multiple_sweep_flag = 1;
maxcount = (size_t)(statex.ullAvailPhys/4/sizeof(struct tsort));
}
}
#elif defined(LINUX) && !defined(MAC_OS_X)
{ struct sysinfo s;
sysinfo(&s);
if ( allocsize > (size_t)(s.freeram)*s.mem_unit/4 )
{ painter_multiple_sweep_flag = 1;
maxcount = (size_t)(s.freeram)*s.mem_unit/4;
}
}
#else
if ( maxcount >= 1000000 )
{ painter_multiple_sweep_flag = 1;
maxcount = 1000000;
}
#endif
trilist = (struct tsort *)temp_calloc(maxcount,sizeof(struct tsort));
count = 0;
if ( painter_multiple_sweep_flag )
{ erroutstring("Very big surface, so painter algorithm doing multiple sweeps.\n");
multiple_sweep_prep();
}
vis_count = 0;
vis_list = NULL;
vis_max = 0;
backstamp = 0;
ps_widthattr = find_extra(PS_WIDTHNAME,&dummy);
} // end painter_start()
/****************************************************************************
*
* Function: painter_edge()
*
* Purpose: Per-edge function called by graphgen()
*/
void painter_edge(
struct graphdata *gdata,
edge_id e_id
)
{
struct tsort *t;
int i,j;
REAL a[MAXCOORD+1],b[MAXCOORD+1];
REAL dx,dy,dz,mag,width;
int ctrl_pts = gdata[0].flags & EDGE_ARC ? 3 : 2;
if ( gdata->color == CLEAR ) return;
if ( count >= maxcount-2 )
{ trilist = (struct tsort *)temp_realloc((char*)trilist,
(maxcount+200)*sizeof(struct tsort));
maxcount += 200;
}
t = trilist + count;
t->flag = EDGE;
t->flag |= (gdata->flags & (EDGE_ARC|LABEL_EDGE|LABEL_HEAD|LABEL_TAIL));
for ( j = SDIM ; j < HOMDIM-1 ; j++ ) a[j] = 0.0; /* filler */
for ( i = 0 ; i < ctrl_pts ; i++ )
{
for ( j = 0 ; (j < SDIM) && (j < HOMDIM-1) ; j++ )
a[j] = gdata[i].x[j];
a[HOMDIM-1] = 1.0;
matvec_mul(view,a,b,HOMDIM,HOMDIM); /* transform */
if ( SDIM <= 2 )
for ( j = 0 ; j < 3 ; j++ ) t->x[i][j] = (float)b[j];
else for ( j = 0 ; j < 3 ; j++ ) t->x[i][j] = (float)b[(j+1)%3];
t->x[i][2] += (float).0001; /* bias edges in front of facets */
}
/* width of edge, in descending order of thickness */
if ( ps_widthattr >= 0 )
width = *EREAL(t->f_id,ps_widthattr);
else if ( gdata->etype & BARE_EDGE ) width = ps_bareedgewidth;
else if ( gdata->etype & FIXED_EDGE ) width = ps_fixededgewidth;
else if ( gdata->etype & CONSTRAINT_EDGE ) width = ps_conedgewidth;
else if ( gdata->etype & BOUNDARY_EDGE ) width = ps_conedgewidth;
else if ( gdata->etype & SINGLE_EDGE ) width = ps_stringwidth;
else if ( gdata->etype & TRIPLE_EDGE ) width = ps_tripleedgewidth;
else width = ps_gridedgewidth; /* regular grid interior edge */
t->width = (float)width;
t->f_id = e_id;
t->color = t->ecolor[0] = gdata->ecolor;
t->etype[0] = gdata->etype;
/* find extents */
find_bbox(t);
if ( painter_multiple_sweep_flag == MULTIPLE_SWEEP_PREP )
{ sweep_prep_one(t);
return;
}
else if ( painter_multiple_sweep_flag == MULTIPLE_SWEEP_GO )
{ if ( t->mins[SDIM-1] > sweep_top || t->mins[SDIM-1] <= sweep_bottom )
return;
}
/* normal vector, closest to z axis */
dx = t->x[1][0] - t->x[0][0];
dy = t->x[1][1] - t->x[0][1];
dz = t->x[1][2] - t->x[0][2];
t->normal[0] = (float)(dx*dz);
t->normal[1] = (float)(dy*dz);
t->normal[2] = -(float)(dx*dx+dy*dy);
mag = sqrt(dotf(t->normal,t->normal,3));
if ( mag != 0.0 )
for ( i = 0 ; i < 3; i++ ) t->normal[i] /= (float)mag;
if ( setquadcode(t) == OUTOFBOX ) return;
count++;
} // end painter_edge()
/********************************************************************
*
* Function: painter_facet()
*
* Purpose: Per-facet function called by graphgen()
*/
void painter_facet(
struct graphdata *gdata,
facet_id f_id
)
{
int i,j;
REAL a[MAXCOORD+1],b[FACET_VERTS][MAXCOORD+1];
struct tsort *t;
REAL normal[MAXCOORD],mag;
if ( gdata[0].color == UNSHOWN )
{ /* just do edges */
struct graphdata ggdata[2];
facetedge_id fe=NULLID;
if ( valid_id(f_id) )
fe = get_facet_fe(f_id);
for ( i = 0 ; i < FACET_EDGES ; i++ )
{ if ( (gdata[i].etype&EBITS) == INVISIBLE_EDGE ) continue;
ggdata[0] = gdata[i];
ggdata[0].color = gdata[i].ecolor;
ggdata[1] = gdata[i==2 ? 0 : i+1];
if ( valid_id(fe) )
{ painter_edge(ggdata,get_fe_edge(fe));
fe = get_next_edge(fe);
}
else painter_edge(ggdata,NULLID);
}
return;
}
if ( count >= maxcount )
{ trilist = (struct tsort *)temp_realloc((char*)trilist,
(maxcount+200)*sizeof(struct tsort));
maxcount += 200;
}
t = trilist + count;
t->flag = FACET | (gdata->flags&LABEL_FACET);
t->f_id = f_id;
t->color = gdata[0].backcolor; /* not sure why, but works */
for ( i = 0 ; i < FACET_EDGES ; i++ )
{ t->ecolor[i] = gdata[i].ecolor;
t->etype[i] = gdata[i].etype;
t->v_id[i] = gdata[i].v_id;
}
/* accumulate list of triangles to display */
for ( j = SDIM ; j < HOMDIM-1 ; j++ ) a[j] = 0.0;
for ( i = 0 ; i < FACET_VERTS ; i++ )
{
for ( j = 0 ; (j < SDIM) && (j < HOMDIM-1) ; j++ )
a[j] = gdata[i].x[j];
a[HOMDIM-1] = 1.0;
matvec_mul(view,a,b[i],HOMDIM,HOMDIM); /* transform */
if ( SDIM <= 2 )
{ t->x[i][0] = (float)b[i][0];
t->x[i][1] = (float)b[i][1];
}
else
{ t->x[i][0] = (float)b[i][1];
t->x[i][1] = (float)b[i][2];
t->x[i][2] = (float)b[i][0];
}
}
if ( SDIM <= 2 )
{ t->normal[0] = t->normal[1] = 0.0;
t->normal[2] = 1.0;
}
else
{ vnormal(b[0],b[1],b[2],normal);
mag = sqrt(SDIM_dot(normal,normal));
if ( mag > 0.0 )
{ t->normal[0] = (float)(normal[1]/mag);
t->normal[1] = (float)(normal[2]/mag);
t->normal[2] = (float)(normal[0]/mag);
if ( fabs(t->normal[2]) < 1e-6 ) t->normal[2] = 0.0;
} else
{ t->normal[0] = 0.0f;
t->normal[1] = 0.0f;
t->normal[2] = 1.0f;
}
}
if ( t->normal[2] > (float)0.0 ) /* frontward normal */
{ int c;
vertex_id tv;
for ( i = 0 ; i < gdim ; i++ )
{ float temp = (float)t->x[1][i];
t->x[1][i] = t->x[2][i];
t->x[2][i] = temp;
t->normal[i] = -t->normal[i];
}
c = t->ecolor[0];
t->ecolor[0] = t->ecolor[2];
t->ecolor[2] = c;
c = t->etype[0] ^ LABEL_REVERSED;
t->etype[0] = t->etype[2] ^ LABEL_REVERSED;
t->etype[2] = (short)c;
t->etype[1] ^= LABEL_REVERSED;
t->color = gdata[0].color;
tv = t->v_id[1];
t->v_id[1] = t->v_id[2];
t->v_id[2] = tv;
t->flag |= FLIPPED_FACET;
}
else
{
if ( backcull_flag && (gdata[0].color == gdata[0].backcolor) ) return;
}
/* find extents */
find_bbox(t);
if ( painter_multiple_sweep_flag == MULTIPLE_SWEEP_PREP )
{ sweep_prep_one(t);
return;
}
else if ( painter_multiple_sweep_flag == MULTIPLE_SWEEP_GO )
{ if ( t->mins[SDIM-1] > sweep_top || t->mins[SDIM-1] <= sweep_bottom )
return;
}
if ( setquadcode(t) == OUTOFBOX ) return;
count++;
} // end painter_facet()
/* stats for analyzing performance; REAL to handle large counts */
REAL in_back_calls;
REAL box_overlaps;
REAL facetfacet;
REAL facetedge;
REAL edgeedge;
REAL crossings;
REAL swaps;
REAL done;
REAL loopbailouts;
REAL sep_line_calls;
REAL sep_plane_calls;
struct tsort **tlist;
/*struct tsort *depthhead;*/
/*************************************************************************
*
* function: search_subtree()
*
* purpose: search node and subtree of quadtree depth lists for an
* element obscured by given element.
* return: pointer to obscured element, or NULL if none found.
* also retval to indicate type of relationship
*/
struct tsort *search_subtree(
int qinx, /* index of node in quadtree */
struct tsort *tk, /* given element */
int *retval
)
{ struct tsort *tj;
*retval = 0;
/* check overall subtree max depth */
if ( tk->maxs[2] <= qtree[qinx].maxdepth )
return NULL;
/* the node itself */
for ( tj = qtree[qinx].depthhead ; tj != NULL ; tj = tj->next )
{
if ( tj == tk ) continue;
if ( tk->maxs[2] <= tj->mins[2] )
break;
*retval = in_back(tk,tj);
if ( *retval & (FIRST_BACK|COPLANAR|DISJOINT) ) continue;
return tj;
}
/* one child */
tj = search_subtree(2*qinx,tk,retval);
if ( tj ) return tj;
/* other child */
return search_subtree(2*qinx+1,tk,retval);
} // end search_subtree()
/**************************************************************************
*
* Function: painter_process_trilist()
*
* Purpose: depth-sort and display trilist. Separated out for benefit
* of large surfaces that re-populate trilist in stages.
*/
void painter_process_trilist()
{ size_t k;
size_t loopcount; /* for emergency loop bailout */
struct tsort **ll,*tt;
size_t quadalloc;
size_t k_top; /* top of tlist */
if ( SDIM == 2 ) /* don't bother with depth */
{ for ( k = 0 ; k < count ; k++ )
visibility_stage(trilist+k);
return;
}
/* now sort on min z, moving pointers instead of structures */
/* leaving room at front of list for extra fragments */
tlist = (struct tsort **)temp_calloc(count+TEXTRA,sizeof(struct tsort *));
for ( k = 0, ll=tlist+TEXTRA, tt=trilist ; k < count ; k++ ) *(ll++) = tt++;
qsort((char *)(tlist+TEXTRA),count,sizeof(struct tsort *),FCAST ttcompare);
for ( k = 0 ; k < TEXTRA ; k++ )
{ tlist[k] = textra+k; tlist[k]->spot = k; tlist[k]->flag = 0; }
for ( k = TEXTRA ; k < TEXTRA+count ; k++ )
tlist[k]->spot = k;
/* quadtree of depth lists */
maxquaddepth = 8; /* maybe make this adjustable later */
quadalloc = 2 << (2*maxquaddepth + 1);
qtree = (struct qtree_t *)temp_calloc(quadalloc,sizeof(struct qtree_t));
for ( k = 0 ; k < quadalloc ; k++ )
qtree[k].maxdepth = 1e30f;
for ( k = count+TEXTRA-1 ; k >= TEXTRA ; k-- )
qdepth_insert(tlist[k]);
/* display */
loopcount = 0;
k_top = count+TEXTRA;
for ( k = TEXTRA ; k < k_top ; )
{ struct tsort *tk = tlist[k];
struct tsort *tj;
int sinx,qinx=0;
int retval;
if ( breakflag ) break;
if ( !tk->flag ) { k++; continue; }
if ( painter_multiple_sweep_flag && tk->maxs[2] > sweep_top )
return; // time to get the next sweep
/* for debugging and testing */
if ( k > debug_k )
goto draw_it;
/* tk is current candidate back facet */
/* search quadtree list for any z overlap */
/* First, node to root */
qinx = get_quadindex(tk->quadcode);
for ( sinx = qinx >> 1 ; sinx != 0 ; sinx >>= 1 )
{ for ( tj = qtree[sinx].depthhead ; tj != NULL ; tj = tj->next )
{
if ( tj == tk ) continue;
if ( tk->maxs[2] <= tj->mins[2] )
break;
retval = in_back(tk,tj);
if ( retval & (FIRST_BACK|COPLANAR|DISJOINT) ) continue;
goto have_conflict;
}
}
/* now search subtree for conflicts */
tj = search_subtree(qinx,tk,&retval);
if ( tj==NULL ) goto draw_it;
have_conflict:
/* Now have conflict, tk obscuring tj */
/* test for possible looping, and if found, split tk */
if ( tj->backstamp == backstamp )
{ int ret;
crossings++;
if ( ++loopcount > count )
{ loopbailouts++; goto draw_it; }
/* need to split */
if ( k < 2 )
{ /* not enough room, so expand tlist allocation, with free at start */
size_t newsize = 2*k_top;
size_t n;
struct tsort *more = (struct tsort*)temp_calloc(k_top,sizeof(struct tsort));
tlist = (struct tsort**)temp_realloc((char*)tlist,newsize*sizeof(struct tsort*));
for ( n = 0 ; n < k_top ; n++ )
{ tlist[n+k_top] = tlist[n];
tlist[n] = more+n;
}
for ( n = 0 ; n < newsize ; n++ )
tlist[n]->spot = n;
k += k_top;
k_top = newsize;
}
if( retval & BSPLITTINGA )
{
ret = newell_split(tk,tj,tlist[k-1],tlist[k-2]);
if ( ret )
{
k -= ret;
goto repeat_tests; /* might not have split */
}
}
else if ( retval & ASPLITTINGB )
{
/* try splitting the other way */
ret = newell_split(tj,tk,tlist[k-1],tlist[k-2]);
if ( ret )
{ k -= ret;
goto repeat_tests;
}
}
else if ( ((tk->flag & 0xF) == EDGE) && ((tj->flag & 0xF) == EDGE) )
{
ret = newell_split(tk,tj,tlist[k-1],tlist[k-2]);
if ( ret )
{
k -= ret;
goto repeat_tests; /* might not have split */
}
}
}
tk->backstamp = backstamp;
/* swap tj and tk */
tlist[k] = tj;
tlist[tj->spot] = tk;
tk->spot = tj->spot;
tj->spot = k;
swaps++;
goto repeat_tests;
draw_it:
visibility_stage(tk);
loopcount = 0;
tk->flag = 0; /* to indicate empty structure */
/* remove from depth list */
if ( tk == qtree[qinx].depthhead )
qtree[qinx].depthhead = tk->next;
sinx = qinx;
/* fix up subtree maxdepths */
while ( sinx && ( tk->mins[2] <= qtree[sinx].maxdepth ) )
{ float maxd = 1e30f;
if ( qtree[sinx].depthhead )
maxd = qtree[sinx].depthhead->mins[2];
if ( sinx < (1 << (2*maxquaddepth)) )
{ if ( maxd > qtree[2*sinx].maxdepth )
maxd = qtree[2*sinx].maxdepth;
if ( maxd > qtree[2*sinx+1].maxdepth )
maxd = qtree[2*sinx+1].maxdepth;
}
qtree[sinx].maxdepth = maxd;
sinx >>= 1;
}
if ( tk->prev ) tk->prev->next = tk->next;
if ( tk->next ) tk->next->prev = tk->prev;
repeat_tests:
continue;
}
if ( tlist ) temp_free((char *)tlist);
if ( qtree ) temp_free((char *)qtree);
} // end painter_process_trilist()
/**************************************************************************
*
* function: painter_end()
*
* purpose: sort and display facets and edges from trilist.
*/
void painter_end()
{
size_t k;
in_back_calls = box_overlaps = facetfacet = facetedge = edgeedge =
crossings = sep_plane_calls = sep_line_calls = 0;
loopbailouts = 0;
if ( count > maxcount ) count = maxcount; /* in case there was excess */
/* find bounding box */
if ( need_bounding_box && !painter_multiple_sweep_flag )
{ struct tsort *t;
bbox_minx = bbox_miny = 1e20;
bbox_maxx = bbox_maxy = -1e20;
for ( k = 0, t = trilist ; k < count ; k++,t++ )
{ if ( t->mins[0] < bbox_minx ) bbox_minx = (REAL)t->mins[0];
if ( t->mins[1] < bbox_miny ) bbox_miny = (REAL)t->mins[1];
if ( t->maxs[0] > bbox_maxx ) bbox_maxx = (REAL)t->maxs[0];
if ( t->maxs[1] > bbox_maxy ) bbox_maxy = (REAL)t->maxs[1];
}
}
if ( !painter_multiple_sweep_flag )
(*init_graphics)(); // print out postscript header
painter_process_trilist();
while ( painter_multiple_sweep_flag )
{ reset_multiple_sweep();
if ( painter_multiple_sweep_flag == MULTIPLE_SWEEP_DONE )
break;
raw_generate();
painter_process_trilist();
}
if ( verbose_flag )
{
printf("in_back_calls: %g\n",(DOUBLE)in_back_calls);
printf(" facetfacet: %g\n",(DOUBLE)facetfacet);
printf(" facetedge: %g\n",(DOUBLE)facetedge);
printf(" edgeedge: %g\n",(DOUBLE)edgeedge);
printf("box_overlaps: %g\n",(DOUBLE)box_overlaps);
printf("sep_line_calls: %g\n",(DOUBLE)sep_line_calls);
printf("sep_plane_calls: %g\n",(DOUBLE)sep_plane_calls);
printf("crossings: %g\n",(DOUBLE)crossings);
printf("swaps: %g\n",(DOUBLE)swaps);
printf("loop bailouts: %g\n",(DOUBLE)loopbailouts);
}
temp_free((char *)trilist); trilist = NULL;
if ( visibility_test )
visibility_end();
(*finish_graphics)();
} /* end old painter_end() */
/*********************************************************************
*
* function: in_back()
*
* purpose: see if one facet or edge obscures another.
*
* returns DISJOINT, FIRST_BACK, SECOND_BACK, ASPLITTINGB, BSPLITTINGA,
* or COPLANAR (possibly bitwise OR)
*/
int in_back(
struct tsort *ta,
struct tsort *tb
)
{