-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest3.c
More file actions
executable file
·1308 lines (1140 loc) · 34.1 KB
/
test3.c
File metadata and controls
executable file
·1308 lines (1140 loc) · 34.1 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
/* Output from p2c, the Pascal-to-C translator */
/* From input file "test3.pas" */
/****************************************************************************/
/* program : hidden lines */
/* purpose : This does hidden lines on the three dimentional package. */
/****************************************************************************/
/* NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE */
/* This has been a collaborative attempt (of debugging) by Dan Fields and */
/* Fred Meczywor. Fred did the original typing and debugging of the code. */
/* Subsequently, Dan Fields did the (real difficult debugging) of the math */
/* stuff. Therefore, this is a dual handing in of the project. */
#include "p2c.h"
#define MAXSEGS 50
#define MAX_VERTS 250
#define MAX_LINES 350
#define MAX_FACETS 150
#define MAX_SIDES 6
#define KLUDGE 0.00001
typedef enum
{
X,
Y,
Z,
W
} XYZW;
typedef double POINT[4];
typedef double MATRIX[4][4];
enum
{
LEFT,
RIGHT,
BOTTOM,
TOP,
FRONT,
BACK
};
typedef long DIRECTION;
typedef POINT POINT_TYPE[MAX_VERTS];
typedef double RM_TYPE[MAXSEGS][2];
typedef double COORD_TYPE[MAX_VERTS];
typedef uchar LINV_TYPE[MAX_LINES][2];
typedef uchar LINF_TYPE[MAX_FACETS][MAX_SIDES];
typedef uchar INDEX_TYPE[MAX_FACETS];
typedef struct OBJ_TYPE
{
uchar NOV;
POINT_TYPE POINTS;
COORD_TYPE XP, YP;
uchar NOL;
LINV_TYPE LINV;
uchar NOF;
INDEX_TYPE INDEXF;
LINF_TYPE LINF;
} OBJ_TYPE;
Static FILE *NIB;
Static POINT VRP, VPN, VUP, COP, P1, P2, VRPPRIME;
Static double D, F, B, UMIN, VMIN, UMAX, VMAX, MINDIST;
Static MATRIX TRANSMATRIX, THEMATRIX, VIEWMATRIX;
Static long NOWX, NOWY, COLOR;
Static double WX, WY, WL, WH, VH, VL;
Static OBJ_TYPE OBJ;
Static RM_TYPE RM;
/********************************************************************/
static double
stretch(double x1, double x2)
{
return (x1 + (x2 - x1) * 319 / 199);
}
/********************************************************************/
static long
tox(double x)
{
return ((long)floor(VL * (x - WX) / WL + 0.5));
}
/********************************************************************/
static long
toy(double y)
{
return ((long)floor(VH * (WY - y) / WH + VH + 0.5));
}
/********************************************************************/
static void
window(double left, double right, double bottom, double top)
{
WX = left;
WL = right - left;
WY = bottom;
WH = top - bottom;
}
/********************************************************************/
static void
viewport(double left, double right, double bottom, double top)
{
long X1, Y1, X2, Y2;
VL = (right - left) * 319;
VH = (top - bottom) * 199;
X1 = (long)floor(left * 319 + 0.5);
Y1 = (long)floor((1 - top) * 199 + 0.5);
X2 = (long)floor(right * 319 + 0.5);
Y2 = (long)floor((1 - bottom) * 199 + 0.5);
/* GRAPHWINDOW(X1, Y1, X2, Y2); */
/* p2c: test3.pas, line 103:
* Warning: Symbol 'GRAPHWINDOW' is not defined [221] */
}
/********************************************************************/
static void
drawon()
{
COLOR = 3;
}
/********************************************************************/
static void
drawoff()
{
COLOR = 0;
}
/********************************************************************/
static void
initialize()
{
/* PURPOSE : INITIALIZES THE MATRIX AND COLORS */
/* GRAPHMODE(); */
/* p2c: test3.pas, line 125:
* Warning: Symbol 'GRAPHMODE' is not defined [221] */
/* PALETTE(1); */
/* p2c: test3.pas, line 126:
* Warning: Symbol 'PALETTE' is not defined [221] */
drawon();
window(0.0, 319.0, 0.0, 199.0);
viewport(0.0, 1.0, 0.0, 1.0);
}
/********************************************************************/
static void
plotpoint(double x, double y)
{
/* PURPOSE : PLOTS A SINGLE POINT ONTO THE SCREEN */
NOWX = tox(x);
NOWY = toy(y);
/* PLOT(NOWX, NOWY, COLOR); */
/* p2c: test3.pas, line 139: Warning: Symbol 'PLOT' is not defined [221] */
}
/********************************************************************/
static void
moves(double x, double y)
{
/* PURPOSE : MOVES TO <X,Y> WITHOUT DRAWING */
NOWX = tox(x);
NOWY = toy(y);
}
/********************************************************************/
static void
draws(double x, double y)
{
/* PURPOSE : DRAWS TO <X,Y> AND MAKES IT THE CURRENT POINT */
long temp;
temp = toy(y);
/* DRAW(NOWX, NOWY, tox(X_), TEMP, COLOR); */
/* p2c: test3.pas, line 160: Warning: Symbol 'DRAW' is not defined [221] */
NOWX = tox(x);
NOWY = temp;
}
/********************************************************************/
static double
dot_prod(double *v1, double *v2)
{
/* PURPOSE : TO PERFORM THE DOT PRODUCT OF V1 AND V2 */
return (v1[(long)X] * v2[(long)X] + v1[(long)Y] * v2[(long)Y] +
v1[(long)Z] * v2[(long)Z]);
}
/*******************************************************************/
static void
cross_prod(double *v1, double *v2, double *result)
{
/* PURPOSE : TO PERFORM THE CROSS PROD WITH V1 AND V2 */
/* WITH THE RESULT IN RESULT */
result[(long)X] = v1[(long)Y] * v2[(long)Z] - v1[(long)Z] * v2[(long)Y];
result[(long)Y] = v1[(long)Z] * v2[(long)X] - v1[(long)X] * v2[(long)Z];
result[(long)Z] = v1[(long)X] * v2[(long)Y] - v1[(long)Y] * v2[(long)X];
}
/********************************************************************/
Static Void NORMALIZE(V1)
double *V1;
{
/* PURPOSE : TO NORMALIZE A VECTOR */
double LENGTH;
XYZW INDEX;
LENGTH = sqrt(dot_prod(V1, V1));
for (INDEX = X; (long)INDEX <= (long)Z; INDEX = (XYZW)((long)INDEX + 1))
V1[(long)INDEX] /= LENGTH;
}
/********************************************************************/
Static Void MULTIPLY_MAT(A, B, C) double (*A)[4], (*B)[4], (*C)[4];
{
/* PURPOSE : TO MULTIPLY TWO 4X4 MATRIX'S WITH RESULT IN C */
long I, J, K;
for (I = 0; I <= 3; I++)
{
for (J = 0; J <= 3; J++)
{
C[I][J] = 0.0;
for (K = 0; K <= 3; K++)
C[I][J] += A[I][K] * B[K][J];
}
}
}
/********************************************************************/
Static Void MULTIPLY_VEC(A, V1, V2) double (*A)[4];
double *V1, *V2;
{
/* PURPOSE : TO MULTIPLY TWO VECTORS GIVING A POINT */
XYZW I;
long J;
for (I = X; (long)I <= (long)W; I = (XYZW)((long)I + 1))
{
J = (int)I + 1;
V2[(long)I] = V1[(long)X] * A[0][J - 1] + V1[(long)Y] * A[1][J - 1] +
V1[(long)Z] * A[2][J - 1] + V1[(long)W] * A[3][J - 1];
}
}
/********************************************************************/
Static Void SUBTRACT_VEC(V1, V2, RESULT)
double *V1, *V2, *RESULT;
{
/* PURPOSE : TO SUBTRACT TWO VECTORS GIVING A VECTOR */
RESULT[(long)X] = V1[(long)X] - V2[(long)X];
RESULT[(long)Y] = V1[(long)Y] - V2[(long)Y];
RESULT[(long)Z] = V1[(long)Z] - V2[(long)Z];
}
/********************************************************************/
Static Void GET_AXIS(V)
double *V;
{
/* PURPOSE : TO CALCULATE THE AXIS'S FROM THE SETUP PROCEDURE */
double SCALAR;
POINT TEMPVECT;
XYZW I;
NORMALIZE(VPN);
SCALAR = dot_prod(VPN, VUP);
for (I = X; (long)I <= (long)Z; I = (XYZW)((long)I + 1))
TEMPVECT[(long)I] = SCALAR * VPN[(long)I];
TEMPVECT[(long)W] = 1.0;
SUBTRACT_VEC(VUP, TEMPVECT, V);
}
/********************************************************************/
Static Void FILL_MATRIX(A, A1, A2, A3, A4, B1, B2, B3, B4, C1, C2, C3, C4, D1,
D2, D3, D4) double (*A)[4];
double A1, A2, A3, A4, B1, B2, B3, B4, C1, C2, C3, C4, D1, D2, D3, D4;
{
/* PURPOSE : TO FILL A MATRIX WITH THE PROPER VALUES */
A[0][0] = A1;
A[0][1] = A2;
A[0][2] = A3;
A[0][3] = A4;
A[1][0] = B1;
A[1][1] = B2;
A[1][2] = B3;
A[1][3] = B4;
A[2][0] = C1;
A[2][1] = C2;
A[2][2] = C3;
A[2][3] = C4;
A[3][0] = D1;
A[3][1] = D2;
A[3][2] = D3;
A[3][3] = D4;
}
/********************************************************************/
Static Void RESET_TRANS()
{
/* PURPOSE : TO INITIALIZE THE MATRIX BACK TO AN IDENTITY MATRIX */
FILL_MATRIX(TRANSMATRIX, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0,
1.0, 0.0, 0.0, 0.0, 0.0, 1.0);
}
/********************************************************************/
Static Void SETUP()
{
/* PURPOSE : SETS UP THE MATRIXES AND INITIALIZES EVERYTHING */
POINT U, V, CENT;
MATRIX T, ROT, SH, SC;
double SX, SY, SZ;
GET_AXIS(V);
cross_prod(VPN, V, U);
FILL_MATRIX(T, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0,
-(VRP[(long)X] + COP[(long)X]), -(VRP[(long)Y] + COP[(long)Y]),
-(VRP[(long)Z] + COP[(long)Z]), 1.0);
FILL_MATRIX(ROT, U[(long)X], V[(long)X], VPN[(long)X], 0.0, U[(long)Y],
V[(long)Y], VPN[(long)Y], 0.0, U[(long)Z], V[(long)Z],
VPN[(long)Z], 0.0, 0.0, 0.0, 0.0, 1.0);
MULTIPLY_MAT(T, ROT, VIEWMATRIX);
MULTIPLY_VEC(VIEWMATRIX, VRP, VRPPRIME);
CENT[(long)Z] = VRPPRIME[(long)Z];
CENT[(long)X] = VRPPRIME[(long)X] + (UMAX + UMIN) / 2;
CENT[(long)Y] = VRPPRIME[(long)Y] + (VMAX + VMIN) / 2;
FILL_MATRIX(SH, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0,
-(CENT[(long)X] / CENT[(long)Z]),
-(CENT[(long)Y] / CENT[(long)Z]), 1.0, 0.0, 0.0, 0.0, 0.0, 1.0);
MULTIPLY_MAT(VIEWMATRIX, SH, VIEWMATRIX);
SZ = 1 / (VRPPRIME[(long)Z] + B);
SX = 2 * VRPPRIME[(long)Z] * SZ / (UMAX - UMIN);
SY = 2 * VRPPRIME[(long)Z] * SZ / (VMAX - VMIN);
FILL_MATRIX(SC, SX, 0.0, 0.0, 0.0, 0.0, SY, 0.0, 0.0, 0.0, 0.0, SZ, 0.0,
0.0, 0.0, 0.0, 1.0);
MULTIPLY_MAT(VIEWMATRIX, SC, VIEWMATRIX);
D = VRPPRIME[(long)Z] * SZ;
window(-D, D, -D, D);
MINDIST = (VRPPRIME[(long)Z] + F) * SZ;
}
/********************************************************************/
Static Void REGION(P, ENDPOINT)
double *P;
DIRECTION *ENDPOINT;
{
/* PURPOSE : TO DETERMINE IF A POINT IS IN THE WINDOW */
*ENDPOINT = 0;
if (P[(long)X] < -P[(long)Z] - 0.001)
*ENDPOINT = 1L << ((long)LEFT);
else
{
if (P[(long)X] > P[(long)Z] + 0.001)
*ENDPOINT = 1L << ((long)RIGHT);
}
if (P[(long)Y] < -P[(long)Z] - 0.001)
*ENDPOINT |= 1L << ((long)BOTTOM);
else
{
if (P[(long)Y] > P[(long)Z] + 0.001)
*ENDPOINT |= 1L << ((long)TOP);
}
if (P[(long)Z] < MINDIST - 0.001)
*ENDPOINT |= 1L << ((long)FRONT);
else
{
if (P[(long)Z] > 1.001)
*ENDPOINT |= 1L << ((long)BACK);
}
}
/********************************************************************/
Static Void GET_P(P, T)
double *P;
double T;
{
P[(long)X] = (P2[(long)X] - P1[(long)X]) * T + P1[(long)X];
P[(long)Y] = (P2[(long)Y] - P1[(long)Y]) * T + P1[(long)Y];
P[(long)Z] = (P2[(long)Z] - P1[(long)Z]) * T + P1[(long)Z];
}
/********************************************************************/
Static Void CLIP_LEFT(P)
double *P;
{
double T;
T = (-P1[(long)Z] - P1[(long)X]) /
(P2[(long)X] - P1[(long)X] + P2[(long)Z] - P1[(long)Z]);
GET_P(P, T);
}
/********************************************************************/
Static Void CLIP_RIGHT(P)
double *P;
{
double T;
T = (P1[(long)Z] - P1[(long)X]) /
(P2[(long)X] - P1[(long)X] + P1[(long)Z] - P2[(long)Z]);
GET_P(P, T);
}
/********************************************************************/
Static Void CLIP_TOP(P)
double *P;
{
double T;
T = (P1[(long)Z] - P1[(long)Y]) /
(P2[(long)Y] - P1[(long)Y] - P2[(long)Z] + P1[(long)Z]);
GET_P(P, T);
}
/********************************************************************/
Static Void CLIP_BOTTOM(P)
double *P;
{
double T;
T = (P1[(long)Y] + P1[(long)Z]) /
(P1[(long)Y] - P2[(long)Y] - P2[(long)Z] + P1[(long)Z]);
GET_P(P, T);
}
/********************************************************************/
Static Void CLIP_BACK(P)
double *P;
{
double T;
T = (1 - P1[(long)Z]) / (P2[(long)Z] - P1[(long)Z]);
GET_P(P, T);
}
/********************************************************************/
Static Void CLIP_FRONT(P)
double *P;
{
double T;
T = (MINDIST - P1[(long)Z]) / (P2[(long)Z] - P1[(long)Z]);
GET_P(P, T);
}
/********************************************************************/
Static Void MAKE(P, OLDP)
double *P, *OLDP;
{
P[(long)X] = OLDP[(long)X];
P[(long)Y] = OLDP[(long)Y];
P[(long)Z] = OLDP[(long)Z];
P[(long)W] = 1.0;
}
/********************************************************************/
Static Void CLIPPING(P1_, P2_)
double *P1_, *P2_;
{
/* PURPOSE : DOES THE ACTUAL CLIPPING OF A LINE IF IT IS OUTSIDE */
/* THE WINDOW AND THE DRAWING TO THE SCREEN */
POINT P1, P2, P;
DIRECTION DIR, DIR1, DIR2;
boolean INVISIBLE;
double XP, YP;
memcpy(P1, P1_, sizeof(POINT));
memcpy(P2, P2_, sizeof(POINT));
INVISIBLE = false;
REGION(P1, &DIR1);
REGION(P2, &DIR2);
while (DIR1 != 0 || DIR2 != 0)
{
if ((DIR1 & DIR2) != 0)
{
DIR1 = 0;
DIR2 = 0;
INVISIBLE = true;
continue;
}
if (DIR1 == 0)
{
DIR = DIR2;
MAKE(P, P2);
}
else
{
DIR = DIR1;
MAKE(P, P1);
}
if (((1L << ((long)LEFT)) & DIR) != 0)
CLIP_LEFT(P);
else
{
if (((1L << ((long)RIGHT)) & DIR) != 0)
CLIP_RIGHT(P);
else
{
if (((1L << ((long)TOP)) & DIR) != 0)
CLIP_TOP(P);
else
{
if (((1L << ((long)BOTTOM)) & DIR) != 0)
CLIP_BOTTOM(P);
else
{
if (((1L << ((long)BACK)) & DIR) != 0)
CLIP_BACK(P);
else
{
if (((1L << ((long)FRONT)) & DIR) != 0)
CLIP_FRONT(P);
}
}
}
}
}
if (DIR == DIR1)
{
MAKE(P1, P);
REGION(P1, &DIR1);
}
else
{
MAKE(P2, P);
REGION(P2, &DIR2);
}
}
if (INVISIBLE)
return;
XP = D * P1[(long)X] / P1[(long)Z];
YP = D * P1[(long)Y] / P1[(long)Z];
moves(XP, YP);
XP = D * P2[(long)X] / P2[(long)Z];
YP = D * P2[(long)Y] / P2[(long)Z];
draws(XP, YP);
}
/****************************************************************************/
/* procedure : update */
/* purpose : This is used to update the visible line segment array with */
/* the visible line segments for that particular line under */
/* study. */
/* It leaves the RM array filled with the Mus of the endpoints */
/* of each visible segment. */
/****************************************************************************/
Static Void UPDATE(RMIN, RMAX, NRL, RM)
double RMIN, RMAX;
uchar *NRL;
double (*RM)[2];
{
uchar MORERL, K;
double R1, R2;
uchar FORLIM;
MORERL = *NRL;
FORLIM = *NRL;
for (K = 0; K < FORLIM; K++)
{
R1 = RM[K][0];
R2 = RM[K][1];
if (R1 <= RMAX && R2 >= RMIN)
{
if (R1 >= RMIN && R2 <= RMAX)
RM[K][0] = -1.0;
else
{
if (R1 < RMIN && R2 > RMAX)
{
MORERL++;
RM[MORERL - 1][0] = RMAX;
RM[MORERL - 1][1] = R2;
RM[K][1] = RMIN;
}
else
{
if (R1 < RMIN)
RM[K][1] = RMIN;
else
RM[K][0] = RMAX;
}
}
}
} /* FOR */
*NRL = 0;
for (K = 0; K < MORERL; K++)
{
if (RM[K][0] >= -KLUDGE)
{
(*NRL)++;
RM[*NRL - 1][0] = RM[K][0];
RM[*NRL - 1][1] = RM[K][1];
}
}
}
/****************************************************************************/
/* function : eval */
/* synopsis : Believe it or not, this will convert a real number to units */
/* of integers. */
/****************************************************************************/
Static long EVAL(VAL)
double VAL;
{
if (VAL > KLUDGE)
return 1;
else
{
if (VAL < -KLUDGE)
return -1;
else
return 0;
}
}
/***************************************************************************/
/* function : G (what a pnemonic name) */
/* purpose : This guy returns the functional for a line that is perpen- */
/* dicular to the line under study. This is used by the funct- */
/* ion offend . */
/***************************************************************************/
Static long G(XX, YY, X4, Y4, XD, YD)
double XX, YY, X4, Y4, XD, YD;
{
double VAL;
VAL = (YY - Y4) * YD + (XX - X4) * XD;
return (EVAL(VAL));
}
/****************************************************************************/
/* function : visible */
/* purpose : This one will find out of the reverse projection of the RMID */
/* (that is X,Y,Z hat) is on the same or different size of the */
/* plane that the line is in intersection with. Essentially, it */
/* does this by plugging the (origin) and the X,Y,Z hat point */
/* into the functional for the plane. and testing the result. */
/* That is -1, 0, or 1. */
/****************************************************************************/
Static boolean VISIBLE(J, L1, L2, X1, Y1, X2, Y2, RMIN, RMAX)
uchar J, L1, L2;
double X1, Y1, X2, Y2, RMIN, RMAX;
{
boolean Result;
double RMID, RXX, XMID, YMID, DENOM, PHI, ZHAT, DDD, XHAT, YHAT, D1, F1, F2;
uchar I1, I2, V1, V2, V3;
POINT P, P1, P3;
Result = true;
RMID = (RMAX + RMIN) * 0.5;
RXX = 1.0 - RMID;
XMID = RXX * X1 + RMID * X2;
YMID = RXX * Y1 + RMID * Y2;
DENOM = D * (OBJ.POINTS[L2 - 1][(long)X] - OBJ.POINTS[L1 - 1][(long)X]) -
XMID * (OBJ.POINTS[L2 - 1][(long)Z] - OBJ.POINTS[L1 - 1][(long)Z]);
if (fabs(DENOM) > KLUDGE)
PHI = (XMID * OBJ.POINTS[L1 - 1][(long)Z] - D * OBJ.POINTS[L1 - 1]
[(long)X]) /
DENOM;
else
{
DENOM = D * (OBJ.POINTS[L2 - 1][(long)Y] - OBJ.POINTS[L1 - 1][(long)Y]) -
YMID * (OBJ.POINTS[L2 - 1][(long)Z] - OBJ.POINTS[L1 - 1][(long)Z]);
PHI = YMID * OBJ.POINTS[L1 - 1][(long)Z] - D * OBJ.POINTS[L1 - 1][(long)Y] / DENOM;
} /* ELSE */
ZHAT = (1.0 - PHI) * OBJ.POINTS[L1 - 1][(long)Z] + PHI * OBJ.POINTS[L2 - 1]
[(long)Z];
DDD = ZHAT / D;
XHAT = XMID * DDD;
YHAT = YMID * DDD;
I1 = OBJ.LINF[J - 1][0];
I2 = OBJ.LINF[J - 1][1];
V1 = OBJ.LINV[I1 - 1][0];
V2 = OBJ.LINV[I1 - 1][1];
V3 = OBJ.LINV[I2 - 1][0];
if (V1 == V3 || V2 == V3)
V3 = OBJ.LINV[I2 - 1][1];
SUBTRACT_VEC(OBJ.POINTS[V2 - 1], OBJ.POINTS[V1 - 1], P1);
SUBTRACT_VEC(OBJ.POINTS[V2 - 1], OBJ.POINTS[V3 - 1], P3);
cross_prod(P1, P3, P);
D1 = P[(long)X] * OBJ.POINTS[V2 - 1][(long)X] + P[(long)Y] * OBJ.POINTS[V2 - 1][(long)Y] + P[(long)Z] * OBJ.POINTS[V2 - 1][(long)Z];
F1 = P[(long)X] * XHAT + P[(long)Y] * YHAT + P[(long)Z] * ZHAT - D1;
F2 = -D1;
if (fabs(F1) < KLUDGE)
Result = false;
else
fprintf(NIB, "eval is %12ld\n", labs(EVAL(F1) - EVAL(F2)));
if (labs(EVAL(F1) - EVAL(F2)) >= 2) /* WITH */
return false;
return Result;
}
/****************************************************************************/
/* function : intersects */
/* purpose : This will tell if the particular line under study is in */
/* intersection a facet. It does this by simultaneously solving */
/* the equasions of the line under study and each side of the */
/* facet under study. If there is an intersection, the endpoint */
/* RMIN and RMAX are changed to the points on the line of the */
/* intersection with the edges. */
/****************************************************************************/
Static boolean INTERSECTS(J, X1, Y1, XD, YD, INS, RMIN, RMAX)
uchar J;
double X1, Y1, XD, YD, INS, *RMIN, *RMAX;
{
boolean ANS;
uchar K, NN, V1, V2;
double XE, YE, XF, YF, DISK, XSI, MU;
ANS = true;
*RMAX = 0.0;
*RMIN = 1.0;
K = 1;
while (K <= INS && ANS)
{
NN = OBJ.LINF[J - 1][K - 1];
V1 = OBJ.LINV[NN - 1][0];
V2 = OBJ.LINV[NN - 1][1];
XE = OBJ.XP[V1 - 1] - OBJ.XP[V2 - 1];
YE = OBJ.YP[V1 - 1] - OBJ.YP[V2 - 1];
XF = OBJ.XP[V1 - 1] - X1;
YF = OBJ.YP[V1 - 1] - Y1;
DISK = XD * YE - XE * YD;
if (fabs(DISK) > KLUDGE)
{ /* THE LINES DO NOT OVERLAP */
XSI = (XD * YF - YD * XF) / DISK; /* XSI ON FACET EDGE */
if (XSI > -KLUDGE && XSI < 1 + KLUDGE)
{
MU = (YE * XF - XE * YF) / DISK; /* MU OF SEARCH LINE */
if (*RMAX < MU)
*RMAX = MU;
if (*RMIN > MU)
*RMIN = MU;
}
}
else
{
if (fabs(XD) > KLUDGE)
{
XSI = XF / XD;
if (fabs(YF - XSI * YD) < KLUDGE)
ANS = false;
}
else
{
if (fabs(XF) < KLUDGE)
ANS = false;
}
} /* ELSE */
K++;
} /* WHILE */
/*zzz*/
if (*RMIN > 1 + KLUDGE || *RMAX < KLUDGE)
{
ANS = false;
return ANS;
}
if (*RMAX > 1.0)
*RMAX = 1.0;
if (*RMIN < 0.0)
*RMIN = 0.0;
if (*RMAX - *RMIN < KLUDGE)
ANS = false;
return ANS; /* WITH */
/* ELSE */
}
/****************************************************************************/
/* function : off_end */
/* purpose : This guy will tell if the points of the facet are off one end */
/* of the line of study (or not). It does so by finding a line */
/* that is perpendicular to the line of study and then seeing if */
/* all the endpoints of the facet are on one side of this per- */
/* pendicular. */
/****************************************************************************/
Static boolean OFF_END(J, INS, X3, Y3, X4, Y4, XD, YD)
uchar J, INS;
double X3, Y3, X4, Y4, XD, YD;
{
boolean INSEG;
uchar K, NN, VERTEX;
long GVAL;
INSEG = false;
GVAL = G(X3, Y3, X4, Y4, XD, YD);
if (GVAL == 0)
{
INSEG = true;
return (!INSEG);
} /* IF */
K = 1;
while (K <= INS && !INSEG)
{
NN = OBJ.LINF[J - 1][K - 1];
VERTEX = OBJ.LINV[NN - 1][0];
if (labs(GVAL - G(OBJ.XP[VERTEX - 1], OBJ.YP[VERTEX - 1], X4, Y4, XD, YD)) < 2)
INSEG = true;
else
{
VERTEX = OBJ.LINV[NN - 1][1];
if (labs(GVAL - G(OBJ.XP[VERTEX - 1], OBJ.YP[VERTEX - 1], X4, Y4, XD,
YD)) < 2)
INSEG = true;
}
K++;
} /* WHILE */
return (!INSEG); /* WITH */
}
/****************************************************************************/
/* function : fit */
/* purpose : This returns the functional of the points that are passed in */
/* through parameters. This tells wether (or not) the points are*/
/* on the line. */
/****************************************************************************/
Static long FIT(XX, YY, X1, Y1, XD, YD)
double XX, YY, X1, Y1, XD, YD;
{
double VAL;
VAL = (YY - Y1) * XD - (XX - X1) * YD;
return (EVAL(VAL));
}
/****************************************************************************/
/* function : diff sides */
/* purpose : This tells wether the points of a facet are on either side of */
/* a line. If they turn out to be on both sides (that is the */
/* somehow intersects the facet) the situation must be studied */
/* further. */
/* Essentially used to elliminated facets and line that absol- */
/* utly do not intersect. */
/****************************************************************************/
Static boolean DIFF_SIDE(J, INS, X1, Y1, XD, YD)
uchar J, INS;
double X1, Y1, XD, YD;
{
boolean SAME_SIDE;
uchar K, NN, VERTEX;
long FVAL;
SAME_SIDE = true;
NN = OBJ.LINF[J - 1][0];
VERTEX = OBJ.LINV[NN - 1][0];
FVAL = FIT(OBJ.XP[VERTEX - 1], OBJ.YP[VERTEX - 1], X1, Y1, XD, YD);
if (FVAL == 0)
{
SAME_SIDE = false;
return (!SAME_SIDE);
} /* IF */
K = 2;
while (K <= INS && SAME_SIDE)
{
NN = OBJ.LINF[J - 1][K - 1];
VERTEX = OBJ.LINV[NN - 1][0];
if (FVAL != FIT(OBJ.XP[VERTEX - 1], OBJ.YP[VERTEX - 1], X1, Y1, XD, YD))
SAME_SIDE = false;
else
{
VERTEX = OBJ.LINV[NN - 1][1];
if (FVAL != FIT(OBJ.XP[VERTEX - 1], OBJ.YP[VERTEX - 1], X1, Y1, XD, YD))
SAME_SIDE = false;
}
K++;
} /* WHILE */
return (!SAME_SIDE); /* WITH */
}
/****************************************************************************/
/* function : in facet */
/* purpose : This tells wether the line in a member of one of the sides of */
/* facet. It does this by checking the indexzing numbers of the */
/* line against the indexzing numbers of the lines that make up */
/* the facet. */
/****************************************************************************/
Static boolean IN_FACET(INS, I, J)
uchar INS, I, J;
{
uchar K;
boolean IN_IT;
K = 1;
IN_IT = false;
while (K <= INS && !IN_IT)
{
if (OBJ.LINF[J - 1][K - 1] == I)
IN_IT = true;
K++;
}
return IN_IT; /* WITH */
}
/*****************************************************************************/
/* procedure : findsegs */
/* purpose : The main driver for the search algorithim. This checks the */
/* interaction between the I th line and J th facet. */
/*****************************************************************************/
Static Void FIND_SEGS(I, J, L1, L2, X1, Y1, X2, Y2, XD, YD, NRL, RM)
uchar I, J, L1, L2;
double X1, Y1, X2, Y2, XD, YD;
uchar *NRL;
double (*RM)[2];
{
uchar INS;
double RMIN, RMAX;
INS = OBJ.INDEXF[J - 1];
if (IN_FACET(INS, I, J)) /* WITH */
return;
if (!DIFF_SIDE(J, INS, X1, Y1, XD, YD))
return;
if (OFF_END(J, INS, X2, Y2, X1, Y1, XD, YD))
return;
if (OFF_END(J, INS, X1, Y1, X2, Y2, XD, YD))
return;
if (INTERSECTS(J, X1, Y1, XD, YD, (double)INS, &RMIN, &RMAX))
{
if (VISIBLE(J, L1, L2, X1, Y1, X2, Y2, RMIN, RMAX))
UPDATE(RMIN, RMAX, NRL, RM);
}
}
/****************************************************************************/
/* procedure : draw segs */
/* purpose : This guy will draw the visible segements of a line from the */
/* Mus in the RM array. */
/****************************************************************************/
static void
draw_segs(X1, Y1, X2, Y2, MU1, MU2) double X1,
Y1, X2, Y2, MU1, MU2;
{
double XP1, YP1, XP2, YP2, ONE_MU1, ONE_MU2;
ONE_MU1 = 1 - MU1;
ONE_MU2 = 1 - MU2;
XP1 = X1 * ONE_MU1 + X2 * MU1;
YP1 = Y1 * ONE_MU1 + Y2 * MU1;
XP2 = X1 * ONE_MU2 + X2 * MU2;
YP2 = Y1 * ONE_MU2 + Y2 * MU2;
if (fabs(XP1 - XP2) > KLUDGE || fabs(YP1 - YP2) > KLUDGE)
{
moves(XP1, YP1);
draws(XP2, YP2);
}
}
/****************************************************************************/
/* procedure : hidden */