File: util3d.c

package info (click to toggle)
gnuplot 3.5beta6.347-3
  • links: PTS
  • area: main
  • in suites: slink
  • size: 5,032 kB
  • ctags: 4,235
  • sloc: ansic: 42,086; makefile: 561; asm: 539; sh: 386; objc: 379; csh: 297; pascal: 194; perl: 138; lisp: 88
file content (1262 lines) | stat: -rw-r--r-- 32,580 bytes parent folder | download
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
#ifndef lint
static char *RCSid = "$Id: util3d.c,v 1.17 1998/06/18 14:55:20 ddenholm Exp $";
#endif


/* GNUPLOT - util3d.c */

/*[
 * Copyright 1986 - 1993, 1998   Thomas Williams, Colin Kelley
 *
 * Permission to use, copy, and distribute this software and its
 * documentation for any purpose with or without fee is hereby granted,
 * provided that the above copyright notice appear in all copies and
 * that both that copyright notice and this permission notice appear
 * in supporting documentation.
 *
 * Permission to modify the software is granted, but not the right to
 * distribute the complete modified source code.  Modifications are to
 * be distributed as patches to the released version.  Permission to
 * distribute binaries produced by compiling modified sources is granted,
 * provided you
 *   1. distribute the corresponding source modifications from the
 *    released version in the form of a patch file along with the binaries,
 *   2. add special version identification to distinguish your version
 *    in addition to the base release version number,
 *   3. provide your name and address as the primary contact for the
 *    support of your modified version, and
 *   4. retain our contact information in regard to use of the base
 *    software.
 * Permission to distribute the released version of the source code along
 * with corresponding source modifications in the form of a patch file is
 * granted with same provisions 2 through 4 for binary distributions.
 *
 * This software is provided "as is" without express or implied warranty
 * to the extent permitted by applicable law.
]*/


/*
 * 19 September 1992  Lawrence Crowl  (crowl@cs.orst.edu)
 * Added user-specified bases for log scaling.
 *
 * 3.6 - split graph3d.c into graph3d.c (graph),
 *                            util3d.c (intersections, etc)
 *                            hidden3d.c (hidden-line removal code)
 *
 */

#include "plot.h"
#include "setshow.h"

extern int xleft,xright,ybot,ytop;
extern int hidden_active; /* HBB 980324: hidden_no_update was unused here */

/* ACCESS THINGS THAT OUGHT TO BE HIDDEN IN hidden3d.c - perhaps we
 * can move the relevant code into hidden3d.c sometime
 */

/* Bitmap of the screen.  The array for each x value is malloc-ed as needed */

extern int suppressMove;

extern double min_array[], max_array[];
extern int auto_array[], log_array[];
extern double base_array[], log_base_array[];

/* for convenience while converting to use these arrays */
#define x_min3d min_array[FIRST_X_AXIS]
#define x_max3d max_array[FIRST_X_AXIS]
#define y_min3d min_array[FIRST_Y_AXIS]
#define y_max3d max_array[FIRST_Y_AXIS]
#define z_min3d min_array[FIRST_Z_AXIS]
#define z_max3d max_array[FIRST_Z_AXIS]
#define min3d_z min_array[FIRST_Z_AXIS]
#define max3d_z max_array[FIRST_Z_AXIS]

#define inrange(z,min,max) ((min<max) ? ((z>=min)&&(z<=max)) : ((z>=max)&&(z<=min)) )


typedef double transform_matrix[4][4];

void mat_unit(mat)
transform_matrix mat;
{
    int i, j;

    for (i = 0; i < 4; i++) for (j = 0; j < 4; j++)
	if (i == j)
	    mat[i][j] = 1.0;
	else
	    mat[i][j] = 0.0;
}

void mat_trans(tx, ty, tz, mat)
double tx, ty, tz;
transform_matrix mat;
{
     mat_unit(mat);                                 /* Make it unit matrix. */
     mat[3][0] = tx;
     mat[3][1] = ty;
     mat[3][2] = tz;
}

void mat_scale(sx, sy, sz, mat)
double sx, sy, sz;
transform_matrix mat;
{
     mat_unit(mat);                                 /* Make it unit matrix. */
     mat[0][0] = sx;
     mat[1][1] = sy;
     mat[2][2] = sz;
}

void mat_rot_x(teta, mat)
double teta;
transform_matrix mat;
{
    double cos_teta, sin_teta;

    teta *= Pi / 180.0;
    cos_teta = cos(teta);
    sin_teta = sin(teta);

    mat_unit(mat);                                  /* Make it unit matrix. */
    mat[1][1] = cos_teta;
    mat[1][2] = -sin_teta;
    mat[2][1] = sin_teta;
    mat[2][2] = cos_teta;
}

void mat_rot_y(teta, mat)
double teta;
transform_matrix mat;
{
    double cos_teta, sin_teta;

    teta *= Pi / 180.0;
    cos_teta = cos(teta);
    sin_teta = sin(teta);

    mat_unit(mat);                                  /* Make it unit matrix. */
    mat[0][0] = cos_teta;
    mat[0][2] = -sin_teta;
    mat[2][0] = sin_teta;
    mat[2][2] = cos_teta;
}

void mat_rot_z(teta, mat)
double teta;
transform_matrix mat;
{
    double cos_teta, sin_teta;

    teta *= Pi / 180.0;
    cos_teta = cos(teta);
    sin_teta = sin(teta);

    mat_unit(mat);                                  /* Make it unit matrix. */
    mat[0][0] = cos_teta;
    mat[0][1] = -sin_teta;
    mat[1][0] = sin_teta;
    mat[1][1] = cos_teta;
}

/* Multiply two transform_matrix. Result can be one of two operands. */
void mat_mult(mat_res, mat1, mat2)
transform_matrix mat_res, mat1, mat2;
{
    int i, j, k;
    transform_matrix mat_res_temp;

    for (i = 0; i < 4; i++) for (j = 0; j < 4; j++) {
        mat_res_temp[i][j] = 0;
        for (k = 0; k < 4; k++) mat_res_temp[i][j] += mat1[i][k] * mat2[k][j];
    }
    for (i = 0; i < 4; i++) for (j = 0; j < 4; j++)
	mat_res[i][j] = mat_res_temp[i][j];
}


/* Test a single point to be within the xleft,xright,ybot,ytop bbox.
 * Sets the returned integers 4 l.s.b. as follows:
 * bit 0 if to the left of xleft.
 * bit 1 if to the right of xright.
 * bit 2 if above of ytop.
 * bit 3 if below of ybot.
 * 0 is returned if inside.
 */
int clip_point(x, y)
unsigned int x, y;
{
    int ret_val = 0;

    if (x < xleft) ret_val |= 0x01;
    if (x > xright) ret_val |= 0x02;
    if (y < ybot) ret_val |= 0x04;
    if (y > ytop) ret_val |= 0x08;

    return ret_val;
}

/* Clip the given line to drawing coords defined as xleft,xright,ybot,ytop.
 *   This routine uses the cohen & sutherland bit mapping for fast clipping -
 * see "Principles of Interactive Computer Graphics" Newman & Sproull page 65.
 */
void draw_clip_line(x1, y1, x2, y2)
unsigned int x1, y1, x2, y2;
{
    int x, y, dx, dy, x_intr[4], y_intr[4], count, pos1, pos2;
    register struct termentry *t = term;

#if defined(ATARI) || defined(MTOS)
    if(x1<0||x2<0||y1<0||y2<0) return; /* temp bug fix */
#endif

    pos1 = clip_point(x1, y1);
    pos2 = clip_point(x2, y2);
    if (pos1 || pos2) {
	if (pos1 & pos2) return;		 /* segment is totally out. */

	/* Here part of the segment MAY be inside. test the intersection
	 * of this segment with the 4 boundaries for hopefully 2 intersections
	 * in. If none are found segment is totaly out.
	 * Under rare circumstances there may be up to 4 intersections (e.g.
	 * when the line passes directly through at least one corner). In
	 * this case it is sufficient to take any 2 intersections (e.g. the
	 * first two found).
	 */
	count = 0;
	dx = x2 - x1;
	dy = y2 - y1;

	/* Find intersections with the x parallel bbox lines: */
	if (dy != 0) {
	    x = (ybot - y2) * dx / dy + x2;      /* Test for ybot boundary. */
	    if (x >= xleft && x <= xright) {
		x_intr[count] = x;
		y_intr[count++] = ybot;
	    }
	    x = (ytop - y2) * dx / dy + x2;      /* Test for ytop boundary. */
	    if (x >= xleft && x <= xright) {
		x_intr[count] = x;
		y_intr[count++] = ytop;
	    }
	}

	/* Find intersections with the y parallel bbox lines: */
	if (dx != 0) {
	    y = (xleft - x2) * dy / dx + y2;    /* Test for xleft boundary. */
	    if (y >= ybot && y <= ytop) {
		x_intr[count] = xleft;
		y_intr[count++] = y;
	    }
	    y = (xright - x2) * dy / dx + y2;  /* Test for xright boundary. */
	    if (y >= ybot && y <= ytop) {
		x_intr[count] = xright;
		y_intr[count++] = y;
	    }
	}

	if (count >= 2) {
	    int x_max, x_min, y_max, y_min;

	    x_min = GPMIN(x1, x2);
	    x_max = GPMAX(x1, x2);
	    y_min = GPMIN(y1, y2);
	    y_max = GPMAX(y1, y2);

	    if (pos1 && pos2) {              /* Both were out - update both */
		x1 = x_intr[0];
		y1 = y_intr[0];
		x2 = x_intr[1];
		y2 = y_intr[1];
	    }
	    else if (pos1) {	     /* Only x1/y1 was out - update only it */
		if (dx * (x2 - x_intr[0]) + dy * (y2 - y_intr[0]) > 0) {
		    x1 = x_intr[0];
		    y1 = y_intr[0];
		}
		else {
		    x1 = x_intr[1];
		    y1 = y_intr[1];
		}
	    }
	    else {	       	     /* Only x2/y2 was out - update only it */
		if (dx * (x_intr[0] - x1) + dy * (y_intr[0] - y1) > 0) {
		    x2 = x_intr[0];
		    y2 = y_intr[0];
		}
		else {
		    x2 = x_intr[1];
		    y2 = y_intr[1];
		}
	    }

	    if (x1 < x_min || x1 > x_max ||
		x2 < x_min || x2 > x_max ||
		y1 < y_min || y1 > y_max ||
		y2 < y_min || y2 > y_max) return;
	}
	else
	    return;
    }

    
#ifndef LITE
    if(hidden3d && hidden_active && draw_surface)
      {
        draw_line_hidden(x1, y1, x2, y2);
        return;
      };
#endif /* not LITE */
    if(!suppressMove) (*t->move)(x1,y1);
    (*t->vector)(x2,y2);
}



/* And text clipping routine. */
void clip_put_text(x, y, str)
unsigned int x,y;
char *str;
{
    register struct termentry *t = term;

    if (clip_point(x, y)) return;

    (*t->put_text)(x,y,str);
}

/* seems sensible to put the justification in here too..? */
void clip_put_text_just(x,y,str,just)
unsigned int x,y;
char *str;
enum JUSTIFY just;
{
        register struct termentry *t = term;
	if (clip_point(x,y)) return;
	if (!(*t->justify_text)(just)) {
		assert(CENTRE==1 && RIGHT==2);
		x -= (t->h_char * strlen(str) * just)/2;
	}
	(*t->put_text)(x,y,str);
}



/* Clip the given line to drawing coords defined as xleft,xright,ybot,ytop.
 *   This routine uses the cohen & sutherland bit mapping for fast clipping -
 * see "Principles of Interactive Computer Graphics" Newman & Sproull page 65.
 */

int clip_line(x1, y1, x2, y2)
     int *x1, *y1, *x2, *y2;
{
  int x, y, dx, dy, x_intr[2], y_intr[2], count, pos1, pos2;
  int x_max, x_min, y_max, y_min;
  pos1 = clip_point(*x1, *y1);
  pos2 = clip_point(*x2, *y2);
  if (!pos1 && !pos2) return 1; /* segment is totally in */ 
  if (pos1 & pos2) return 0;	/* segment is totally out. */ 
  /* Here part of the segment MAY be inside. test the intersection
   * of this segment with the 4 boundaries for hopefully 2 intersections
   * in. If non found segment is totaly out.
   */ 
  count = 0;
  dx = *x2 - *x1;  
  dy = *y2 - *y1;
  /* Find intersections with the x parallel bbox lines: */ 
  if (dy != 0) {
    x = (ybot - *y2) * dx / dy + *x2; /* Test for ybot boundary. */ 
    if (x >= xleft && x <= xright) {
      x_intr[count] = x;       
      y_intr[count++] = ybot;
    }
    x = (ytop - *y2) * dx / dy + *x2; /* Test for ytop boundary. */ 
    if (x >= xleft && x <= xright) {
      x_intr[count] = x;       
      y_intr[count++] = ytop;
    }
  }
  /* Find intersections with the y parallel bbox lines: */ 
  if (dx != 0) {
    y = (xleft - *x2) * dy / dx + *y2; /* Test for xleft boundary. */
    if (y >= ybot && y <= ytop) {
      x_intr[count] = xleft;   
      y_intr[count++] = y;
    }
    y = (xright - *x2) * dy / dx + *y2; /* Test for xright boundary. */
    if (y >= ybot && y <= ytop) {
      x_intr[count] = xright;  
      y_intr[count++] = y;
    }
  }
  if (count != 2) return 0;
  if (*x1 < *x2)
    x_min=*x1, x_max=*x2;
  else
    x_min=*x2, x_max=*x1;
  if (*y1 < *y2)
    y_min=*y1, y_max=*y2;
  else
    y_min=*y2, y_max=*y1;
  if (pos1 && pos2) {		/* Both were out - update both */
    *x1 = x_intr[0];     
    *y1 = y_intr[0];
    *x2 = x_intr[1];     
    *y2 = y_intr[1];
  }
  else if (pos1) 
    {				/* Only x1/y1 was out - update only it */
      if (dx * (*x2 - x_intr[0]) + dy * (*y2 - y_intr[0]) > 0) 
	{    
	  *x1 = x_intr[0];   
	  *y1 = y_intr[0];  
	}
      else 
	{ 
	  *x1 = x_intr[1];   
	  *y1 = y_intr[1];  
	}
    }
  else 
    {				/* Only x2/y2 was out - update only it */
      if (dx * (x_intr[0] - *x1) + dy * (y_intr[0] - *x1) > 0) 
	{    
	  *x2 = x_intr[0];   
	  *y2 = y_intr[0];  
	}
      else 
	{ 
	  *x2 = x_intr[1];   
	  *y2 = y_intr[1];  
	}
    }

  if (*x1 < x_min || *x1 > x_max ||  
      *x2 < x_min || *x2 > x_max ||
      *y1 < y_min || *y1 > y_max ||  
      *y2 < y_min || *y2 > y_max) 
    return 0;
  return 1;
}


/* single edge intersection algorithm */
/* Given two points, one inside and one outside the plot, return
 * the point where an edge of the plot intersects the line segment defined 
 * by the two points.
 */
void edge3d_intersect(points, i, ex, ey, ez)
	struct coordinate GPHUGE *points; /* the points array */
	int i;				/* line segment from point i-1 to point i */
	double *ex, *ey, *ez;		/* the point where it crosses an edge */
{
    /* global x_min3d, x_max3d, y_min3d, y_max3d, min3d_z, max3d_z */
    int count;
    double ix = points[i-1].x;
    double iy = points[i-1].y;
    double iz = points[i-1].z;
    double ox = points[i].x;
    double oy = points[i].y;
    double oz = points[i].z;
    double x, y, z;			/* possible intersection point */

    if(points[i].type == INRANGE)
      {
	/* swap points around so that ix/ix/iz are INRANGE and ox/oy/oz are OUTRANGE */
	x = ix;ix = ox;ox = x;
	y = iy;iy = oy;oy = y;
	z = iz;iz = oz;oz = z;
      }

    /* nasty degenerate cases, effectively drawing to an infinity point (?)
       cope with them here, so don't process them as a "real" OUTRANGE point 

       If more than one coord is -VERYLARGE, then can't ratio the "infinities"
       so drop out by returning the INRANGE point.

       Obviously, only need to test the OUTRANGE point (coordinates) */

    /* nasty degenerate cases, effectively drawing to an infinity point (?)
       cope with them here, so don't process them as a "real" OUTRANGE point 

       If more than one coord is -VERYLARGE, then can't ratio the "infinities"
       so drop out by returning FALSE */
    
    count = 0;
    if(ox == -VERYLARGE) count++;
    if(oy == -VERYLARGE) count++;
    if(oz == -VERYLARGE) count++;

    /* either doesn't pass through 3D volume *or* 
       can't ratio infinities to get a direction to draw line, so return the INRANGE point */
    if(count > 1){
      *ex = ix;
      *ey = iy;
      *ez = iz;

      return;
    }

    if(count == 1)
      {
	*ex = ix;
	*ey = iy;
	*ez = iz;

	if(ox == -VERYLARGE)
	  {
	    *ex = x_min3d;
	    return;
	  }

	if(oy == -VERYLARGE)
	  {
	    *ey = y_min3d;
	    return;
	  }

	/* obviously oz is -VERYLARGE and (ox != -VERYLARGE && oy != -VERYLARGE) */
	*ez = min3d_z;
	return;
      }

    /*
     * Can't have case (ix == ox && iy == oy && iz == oz) as one point
     * is INRANGE and one point is OUTRANGE.
     */
    if(ix == ox) {
      if(iy == oy) {
	/* line parallel to z axis */

	/* assume inrange(iy, y_min3d, y_max3d) && inrange(ix, x_min3d, x_max3d) */
	*ex = ix;		/* == ox */
	*ey = iy;		/* == oy */
	
	if (inrange(max3d_z, iz, oz))
	  *ez = max3d_z;
	else if (inrange(min3d_z, iz, oz))
	  *ez = min3d_z;
	else {
	  graph_error("error in edge3d_intersect");
        }

	return;
      }
      
      if(iz == oz) {
	/* line parallel to y axis */
	
	/* assume inrange(iz, min3d_z, max3d_z) && inrange(ix, x_min3d, x_max3d) */
	*ex = ix;		/* == ox */
	*ez = iz;		/* == oz */
	
	if (inrange(y_max3d, iy, oy))
	  *ey = y_max3d;
	else if (inrange(y_min3d, iy, oy))
	  *ey = y_min3d;
	else {
	  graph_error("error in edge3d_intersect");
	}
	
	return;
      }

      /* nasty 2D slanted line in a yz plane */
      
      /* does it intersect y_min3d edge */
      if (inrange(y_min3d, iy, oy) && y_min3d != iy && y_min3d != oy) {
	z = iz + (y_min3d-iy) * ((oz-iz) / (oy-iy));
	if (inrange(z, min3d_z, max3d_z)) {
	  *ex = ix;
	  *ey = y_min3d;
	  *ez = z;
	  return;
	}
      }

      /* does it intersect y_max3d edge */
      if (inrange(y_max3d, iy, oy) && y_max3d != iy && y_max3d != oy) {
	z = iz + (y_max3d-iy) * ((oz-iz) / (oy-iy));
	if (inrange(z, min3d_z, max3d_z)) {
	  *ex = ix;
	  *ey = y_max3d;
	  *ez = z;
	  return;
	}
      }

      /* does it intersect min3d_z edge */
      if (inrange(min3d_z, iz, oz) && min3d_z != iz && min3d_z != oz) {
	y = iy + (min3d_z-iz) * ((oy-iy) / (oz-iz));
	if (inrange(y, y_min3d, y_max3d)) {
	  *ex = ix;
	  *ey = y;
	  *ez = min3d_z;
	  return;
	}
      }

      /* does it intersect max3d_z edge */
      if (inrange(max3d_z, iz, oz) && max3d_z != iz && max3d_z != oz) {
	y = iy + (max3d_z-iz) * ((oy-iy) / (oz-iz));
	if (inrange(y, y_min3d, y_max3d)) {
	  *ex = ix;
	  *ey = y;
	  *ez = max3d_z;
	  return;
	}
      }
    }
      
    if(iy == oy) {
      /* already checked case (ix == ox && iy == oy) */
      if(oz ==  iz) {
	  /* line parallel to x axis */

	  /* assume inrange(iz, min3d_z, max3d_z) && inrange(iy, y_min3d, y_max3d) */
	  *ey = iy;		/* == oy */
	  *ez = iz;		/* == oz */
	  
	  if (inrange(x_max3d, ix, ox))
	    *ex = x_max3d;
	  else if (inrange(x_min3d, ix, ox))
	    *ex = x_min3d;
	  else {
	    graph_error("error in edge3d_intersect");
	  }

	  return;
      }

      /* nasty 2D slanted line in an xz plane */

      /* does it intersect x_min3d edge */
      if (inrange(x_min3d, ix, ox) && x_min3d != ix && x_min3d != ox) {
	z = iz + (x_min3d-ix) * ((oz-iz) / (ox-ix));
	if (inrange(z, min3d_z, max3d_z)) {
	  *ex = x_min3d;
	  *ey = iy;
	  *ez = z;
	  return;
	}
      }

      /* does it intersect x_max3d edge */
      if (inrange(x_max3d, ix, ox) && x_max3d != ix && x_max3d != ox) {
	z = iz + (x_max3d-ix) * ((oz-iz) / (ox-ix));
	if (inrange(z, min3d_z, max3d_z)) {
	  *ex = x_max3d;
	  *ey = iy;
	  *ez = z;
	  return;
	}
      }

      /* does it intersect min3d_z edge */
      if (inrange(min3d_z, iz, oz) && min3d_z != iz && min3d_z != oz) {
	x = ix + (min3d_z-iz) * ((ox-ix) / (oz-iz));
	if (inrange(x, x_min3d, x_max3d)) {
	  *ex = x;
	  *ey = iy;
	  *ez = min3d_z;
	  return;
	}
      }
      
      /* does it intersect max3d_z edge */
      if (inrange(max3d_z, iz, oz) && max3d_z != iz && max3d_z != oz) {
	x = ix + (max3d_z-iz) * ((ox-ix) / (oz-iz));
	if (inrange(x, x_min3d, x_max3d)) {
	  *ex = x;
	  *ey = iy;
	  *ez = max3d_z;
	  return;
	}
      }      
    }
    
    if(iz == oz) {
      /* already checked cases (ix == ox && iz == oz) and (iy == oy && iz == oz) */

      /* nasty 2D slanted line in an xy plane */

      /* assume inrange(oz, min3d_z, max3d_z) */

      /* does it intersect x_min3d edge */
      if (inrange(x_min3d, ix, ox) && x_min3d != ix && x_min3d != ox) {
	y = iy + (x_min3d-ix) * ((oy-iy) / (ox-ix));
	if (inrange(y, y_min3d, y_max3d)) {
	  *ex = x_min3d;
	  *ey = y;
	  *ez = iz;
	  return;
	}
      }

      /* does it intersect x_max3d edge */
      if (inrange(x_max3d, ix, ox) && x_max3d != ix && x_max3d != ox) {
	y = iy + (x_max3d-ix) * ((oy-iy) / (ox-ix));
	if (inrange(y, y_min3d, y_max3d)) {
	  *ex = x_max3d;
	  *ey = y;
	  *ez = iz;
	  return;
	}
      }    
      
      /* does it intersect y_min3d edge */
      if (inrange(y_min3d, iy, oy) && y_min3d != iy && y_min3d != oy) {
	x = ix + (y_min3d-iy) * ((ox-ix) / (oy-iy));
	if (inrange(x, x_min3d, x_max3d)) {
	  *ex = x;
	  *ey = y_min3d;
	  *ez = iz;
	  return;
	}
      }
      
      /* does it intersect y_max3d edge */
      if (inrange(y_max3d, iy, oy) && y_max3d != iy && y_max3d != oy) {
	x = ix + (y_max3d-iy) * ((ox-ix) / (oy-iy));
	if (inrange(x, x_min3d, x_max3d)) {
	  *ex = x;
	  *ey = y_max3d;
	  *ez = iz;
	  return;
	}
      }
    }
  
    /* really nasty general slanted 3D case */

    /* does it intersect x_min3d edge */
    if (inrange(x_min3d, ix, ox) && x_min3d != ix && x_min3d != ox) {
      y = iy + (x_min3d-ix) * ((oy-iy) / (ox-ix));
      z = iz + (x_min3d-ix) * ((oz-iz) / (ox-ix));
      if (inrange(y, y_min3d, y_max3d) && inrange(z, min3d_z, max3d_z)) {
	*ex = x_min3d;
	*ey = y;
	*ez = z;
	return;
      }
    }

    /* does it intersect x_max3d edge */
    if (inrange(x_max3d, ix, ox) && x_max3d != ix && x_max3d != ox) {
      y = iy + (x_max3d-ix) * ((oy-iy) / (ox-ix));
      z = iz + (x_max3d-ix) * ((oz-iz) / (ox-ix));
      if (inrange(y, y_min3d, y_max3d) && inrange(z, min3d_z, max3d_z)) {
	*ex = x_max3d;
	*ey = y;
	*ez = z;
	return;
      }
    }

    /* does it intersect y_min3d edge */
    if (inrange(y_min3d, iy, oy) && y_min3d != iy && y_min3d != oy) {
      x = ix + (y_min3d-iy) * ((ox-ix) / (oy-iy));
      z = iz + (y_min3d-iy) * ((oz-iz) / (oy-iy));
      if (inrange(x, x_min3d, x_max3d) && inrange(z, min3d_z, max3d_z)) {
	*ex = x;
	*ey = y_min3d;
	*ez = z;
	return;
      }
    }

    /* does it intersect y_max3d edge */
    if (inrange(y_max3d, iy, oy) && y_max3d != iy && y_max3d != oy) {
      x = ix + (y_max3d-iy) * ((ox-ix) / (oy-iy));
      z = iz + (y_max3d-iy) * ((oz-iz) / (oy-iy));
      if (inrange(x, x_min3d, x_max3d) && inrange(z, min3d_z, max3d_z)) {
	*ex = x;
	*ey = y_max3d;
	*ez = z;
	return;
      }
    }

    /* does it intersect min3d_z edge */
    if (inrange(min3d_z, iz, oz) && min3d_z != iz && min3d_z != oz) {
      x = ix + (min3d_z-iz) * ((ox-ix) / (oz-iz));
      y = iy + (min3d_z-iz) * ((oy-iy) / (oz-iz));
      if (inrange(x, x_min3d, x_max3d) && inrange(y, y_min3d, y_max3d)) {
	*ex = x;
	*ey = y;
	*ez = min3d_z;
	return;
      }
    }

    /* does it intersect max3d_z edge */
    if (inrange(max3d_z, iz, oz) && max3d_z != iz && max3d_z != oz) {
      x = ix + (max3d_z-iz) * ((ox-ix) / (oz-iz));
      y = iy + (max3d_z-iz) * ((oy-iy) / (oz-iz));
      if (inrange(x, x_min3d, x_max3d) && inrange(y, y_min3d, y_max3d)) {
	*ex = x;
	*ey = y;
	*ez = max3d_z;
	return;
      }
    }

    /* If we reach here, the inrange point is on the edge, and
     * the line segment from the outrange point does not cross any 
     * other edges to get there. In this case, we return the inrange 
     * point as the 'edge' intersection point. This will basically draw
     * line.
     */
    *ex = ix;
    *ey = iy;
    *ez = iz;
    return;
}

/* double edge intersection algorithm */
/* Given two points, both outside the plot, return
 * the points where an edge of the plot intersects the line segment defined 
 * by the two points. There may be zero, one, two, or an infinite number
 * of intersection points. (One means an intersection at a corner, infinite
 * means overlaying the edge itself). We return FALSE when there is nothing
 * to draw (zero intersections), and TRUE when there is something to 
 * draw (the one-point case is a degenerate of the two-point case and we do 
 * not distinguish it - we draw it anyway).
 */
TBOOLEAN                                /* any intersection? */
two_edge3d_intersect(points, i, lx, ly, lz)
	struct coordinate GPHUGE *points; /* the points array */
	int i;				/* line segment from point i-1 to point i */
	double *lx, *ly, *lz;		/* lx[2], ly[2], lz[2]: points where it crosses edges */
{
    int count;
    /* global x_min3d, x_max3d, y_min3d, y_max3d, min3d_z, max3d_z */
    double ix = points[i-1].x;
    double iy = points[i-1].y;
    double iz = points[i-1].z;
    double ox = points[i].x;
    double oy = points[i].y;
    double oz = points[i].z;
    double t[6];
    double swap;
    double x, y, z;			/* possible intersection point */
    double t_min, t_max;

    /* nasty degenerate cases, effectively drawing to an infinity point (?)
       cope with them here, so don't process them as a "real" OUTRANGE point 

       If more than one coord is -VERYLARGE, then can't ratio the "infinities"
       so drop out by returning FALSE */
    
    count = 0;
    if(ix == -VERYLARGE) count++;
    if(ox == -VERYLARGE) count++;
    if(iy == -VERYLARGE) count++;
    if(oy == -VERYLARGE) count++;
    if(iz == -VERYLARGE) count++;
    if(oz == -VERYLARGE) count++;

    /* either doesn't pass through 3D volume *or* 
       can't ratio infinities to get a direction to draw line, so simply return(FALSE) */
    if(count > 1){
      return(FALSE);
    }

    if(ox == -VERYLARGE || ix == -VERYLARGE)
      {
	if(ix == -VERYLARGE)
	  {
	    /* swap points so ix/iy/iz don't have a -VERYLARGE component */
	    x = ix;ix = ox;ox = x;
	    y = iy;iy = oy;oy = y;
	    z = iz;iz = oz;oz = z;
	  }

	/* check actually passes through the 3D graph volume */
	if(ix > x_max3d && inrange(iy, y_min3d, y_max3d) && inrange(iz, min3d_z, max3d_z))
	  {
	    lx[0] = x_min3d;
	    ly[0] = iy;
	    lz[0] = iz;

	    lx[1] = x_max3d;
	    ly[1] = iy;
	    lz[1] = iz;

	    return(TRUE);
	  }
	else {
	  return(FALSE);
	}
      }

    if(oy == -VERYLARGE || iy == -VERYLARGE)
      {
	if(iy == -VERYLARGE)
	  {
	    /* swap points so ix/iy/iz don't have a -VERYLARGE component */
	    x = ix;ix = ox;ox = x;
	    y = iy;iy = oy;oy = y;
	    z = iz;iz = oz;oz = z;
	  }

	/* check actually passes through the 3D graph volume */
	if(iy > y_max3d && inrange(ix, x_min3d, x_max3d) && inrange(iz, min3d_z, max3d_z))
	  {
	    lx[0] = ix;
	    ly[0] = y_min3d;
	    lz[0] = iz;

	    lx[1] = ix;
	    ly[1] = y_max3d;
	    lz[1] = iz;

	    return(TRUE);
	  }
	else {
	  return(FALSE);
	}
      }

    if(oz == -VERYLARGE || iz == -VERYLARGE)
      {
	if(iz == -VERYLARGE)
	  {
	    /* swap points so ix/iy/iz don't have a -VERYLARGE component */
	    x = ix;ix = ox;ox = x;
	    y = iy;iy = oy;oy = y;
	    z = iz;iz = oz;oz = z;
	  }

	/* check actually passes through the 3D graph volume */
	if(iz > max3d_z && inrange(ix, x_min3d, x_max3d) && inrange(iy, y_min3d, y_max3d))
	  {
	    lx[0] = ix;
	    ly[0] = iy;
	    lz[0] = min3d_z;

	    lx[1] = ix;
	    ly[1] = iy;
	    lz[1] = max3d_z;

	    return(TRUE);
	  }
	else {
	  return(FALSE);
	}
      }

    /*
     * Quick outcode tests on the 3d graph volume
     */

    /* 
     * test z coord first --- most surface OUTRANGE points generated between
     * min3d_z and z_min3d (i.e. when ticslevel is non-zero)
     */
    if(GPMAX(iz,oz) < min3d_z || GPMIN(iz,oz) > max3d_z)
      return(FALSE);

    if(GPMAX(ix,ox) < x_min3d || GPMIN(ix,ox) > x_max3d)
      return(FALSE);

    if(GPMAX(iy,oy) < y_min3d || GPMIN(iy,oy) > y_max3d)
      return(FALSE);

    /*
     * Special horizontal/vertical, etc. cases are checked and remaining
     * slant lines are checked separately.
     *
     * The slant line intersections are solved using the parametric form
     * of the equation for a line, since if we test x/y/z min/max planes explicitly
     * then e.g. a  line passing through a corner point (x_min,y_min,z_min) 
     * actually intersects all 3 planes and hence further tests would be required 
     * to anticipate this and similar situations.
     */

    /*
     * Can have case (ix == ox && iy == oy && iz == oz) as both points OUTRANGE
     */
    if(ix == ox && iy == oy && iz == oz)
      {
	/* but as only define single outrange point, can't intersect 3D graph volume */
	return(FALSE);
      }

    if(ix == ox) {
      if(iy == oy) {
	/* line parallel to z axis */

	/* x and y coords must be in range, and line must span both min3d_z and max3d_z */
	/* note that spanning min3d_z implies spanning max3d_z as both points OUTRANGE */
	if(!inrange(ix, x_min3d, x_max3d) || !inrange(iy, y_min3d, y_max3d))
	  {
	    return(FALSE);
	  }

	if (inrange(min3d_z, iz, oz)) {
	  lx[0] = ix;
	  ly[0] = iy;
	  lz[0] = min3d_z;

	  lx[1] = ix;
	  ly[1] = iy;
	  lz[1] = max3d_z;

	  return(TRUE);
	} else
	  return(FALSE);
      }
      
      if(iz == oz) {
	/* line parallel to y axis */
	
	/* x and z coords must be in range, and line must span both y_min3d and y_max3d */
	/* note that spanning y_min3d implies spanning y_max3d, as both points OUTRANGE */
	if(!inrange(ix, x_min3d, x_max3d) || !inrange(iz, min3d_z, max3d_z))
	  {
	    return(FALSE);
	  }

	if (inrange(y_min3d, iy, oy)) {
	  lx[0] = ix;
	  ly[0] = y_min3d;
	  lz[0] = iz;

	  lx[1] = ix;
	  ly[1] = y_max3d;
	  lz[1] = iz;

	  return(TRUE);
	} else
	  return(FALSE);
      }

      /* nasty 2D slanted line in a yz plane */
      
      if(!inrange(ox, x_min3d, x_max3d))
	return(FALSE);

      t[0] = (y_min3d - iy)/(oy - iy);
      t[1] = (y_max3d - iy)/(oy - iy);
      
      if(t[0] > t[1]) {
	swap = t[0];t[0] = t[1];t[1] = swap;
      }

      t[2] = (min3d_z - iz)/(oz - iz);
      t[3] = (max3d_z - iz)/(oz - iz);
      
      if(t[2] > t[3]) {
	swap = t[2];t[2] = t[3];t[3] = swap;
      }

      t_min = GPMAX(GPMAX(t[0],t[2]),0.0);
      t_max = GPMIN(GPMIN(t[1],t[3]),1.0);
      
      if(t_min > t_max)
	return(FALSE);

      lx[0] = ix;
      ly[0] = iy + t_min * (oy - iy);
      lz[0] = iz + t_min * (oz - iz);
      
      lx[1] = ix;
      ly[1] = iy + t_max * (oy - iy);
      lz[1] = iz + t_max * (oz - iz);
      
      /*
       * Can only have 0 or 2 intersection points -- only need test one coord
       */
      if(inrange(ly[0], y_min3d, y_max3d) && 
	 inrange(lz[0], min3d_z, max3d_z))
	{
	  return(TRUE);
	}
      
      return(FALSE);
    }
      
    if(iy == oy) {
      /* already checked case (ix == ox && iy == oy) */
      if(oz ==  iz) {
	/* line parallel to x axis */
	
	/* y and z coords must be in range, and line must span both x_min3d and x_max3d */
	/* note that spanning x_min3d implies spanning x_max3d, as both points OUTRANGE */
	if(!inrange(iy, y_min3d, y_max3d) || !inrange(iz, min3d_z, max3d_z))
	  {
	    return(FALSE);
	  }

	if (inrange(x_min3d, ix, ox)) {
	  lx[0] = x_min3d;
	  ly[0] = iy;
	  lz[0] = iz;

	  lx[1] = x_max3d;
	  ly[1] = iy;
	  lz[1] = iz;

	  return(TRUE);
	} else
	  return(FALSE);
      }

      /* nasty 2D slanted line in an xz plane */

      if(!inrange(oy, y_min3d, y_max3d))
	return(FALSE);

      t[0] = (x_min3d - ix)/(ox - ix);
      t[1] = (x_max3d - ix)/(ox - ix);
      
      if(t[0] > t[1]) {
	swap = t[0];t[0] = t[1];t[1] = swap;
      }

      t[2] = (min3d_z - iz)/(oz - iz);
      t[3] = (max3d_z - iz)/(oz - iz);
      
      if(t[2] > t[3]) {
	swap = t[2];t[2] = t[3];t[3] = swap;
      }

      t_min = GPMAX(GPMAX(t[0],t[2]),0.0);
      t_max = GPMIN(GPMIN(t[1],t[3]),1.0);
      
      if(t_min > t_max)
	return(FALSE);

      lx[0] = ix + t_min * (ox - ix);
      ly[0] = iy;
      lz[0] = iz + t_min * (oz - iz);
      
      lx[1] = ix + t_max * (ox - ix);
      ly[1] = iy;
      lz[1] = iz + t_max * (oz - iz);
      
      /*
       * Can only have 0 or 2 intersection points -- only need test one coord
       */
      if(inrange(lx[0], x_min3d, x_max3d) && 
	 inrange(lz[0], min3d_z, max3d_z))
	{
	  return(TRUE);
	}
      
      return(FALSE);
    }
    
    if(iz == oz) {
      /* already checked cases (ix == ox && iz == oz) and (iy == oy && iz == oz) */

      /* nasty 2D slanted line in an xy plane */

      if(!inrange(oz, min3d_z, max3d_z))
	return(FALSE);

      t[0] = (x_min3d - ix)/(ox - ix);
      t[1] = (x_max3d - ix)/(ox - ix);
      
      if(t[0] > t[1]) {
	swap = t[0];t[0] = t[1];t[1] = swap;
      }

      t[2] = (y_min3d - iy)/(oy - iy);
      t[3] = (y_max3d - iy)/(oy - iy);
      
      if(t[2] > t[3]) {
	swap = t[2];t[2] = t[3];t[3] = swap;
      }

      t_min = GPMAX(GPMAX(t[0],t[2]),0.0);
      t_max = GPMIN(GPMIN(t[1],t[3]),1.0);
      
      if(t_min > t_max)
	return(FALSE);

      lx[0] = ix + t_min * (ox - ix);
      ly[0] = iy + t_min * (oy - iy);
      lz[0] = iz;
      
      lx[1] = ix + t_max * (ox - ix);
      ly[1] = iy + t_max * (oy - iy);
      lz[1] = iz;
      
      /*
       * Can only have 0 or 2 intersection points -- only need test one coord
       */
      if(inrange(lx[0], x_min3d, x_max3d) && 
	 inrange(ly[0], y_min3d, y_max3d))
	{
	  return(TRUE);
	}
      
      return(FALSE);
    }
  
    /* really nasty general slanted 3D case */

    /*
      Solve parametric equation

      (ix, iy, iz) + t (diff_x, diff_y, diff_z)

      where 0.0 <= t <= 1.0 and

      diff_x = (ox - ix);
      diff_y = (oy - iy);
      diff_z = (oz - iz);
     */

    t[0] = (x_min3d - ix)/(ox - ix);
    t[1] = (x_max3d - ix)/(ox - ix);

    if(t[0] > t[1]) {
      swap = t[0];t[0] = t[1];t[1] = swap;
    }

    t[2] = (y_min3d - iy)/(oy - iy);
    t[3] = (y_max3d - iy)/(oy - iy);
    
    if(t[2] > t[3]) {
      swap = t[2];t[2] = t[3];t[3] = swap;
    }
    
    t[4] = (iz == oz) ? 0.0 : (min3d_z - iz)/(oz - iz);
    t[5] = (iz == oz) ? 1.0 : (max3d_z - iz)/(oz - iz);
    
    if(t[4] > t[5]) {
      swap = t[4];t[4] = t[5];t[5] = swap;
    }

    t_min = GPMAX(GPMAX(t[0],t[2]),GPMAX(t[4],0.0));
    t_max = GPMIN(GPMIN(t[1],t[3]),GPMIN(t[5],1.0));

    if(t_min > t_max)
      return(FALSE);

    lx[0] = ix + t_min * (ox - ix);
    ly[0] = iy + t_min * (oy - iy);
    lz[0] = iz + t_min * (oz - iz);

    lx[1] = ix + t_max * (ox - ix);
    ly[1] = iy + t_max * (oy - iy);
    lz[1] = iz + t_max * (oz - iz);

    /*
     * Can only have 0 or 2 intersection points -- only need test one coord
     */
    if(inrange(lx[0], x_min3d, x_max3d) && 
       inrange(ly[0], y_min3d, y_max3d) &&
       inrange(lz[0], min3d_z, max3d_z))
      {
	return(TRUE);
      }
    
    return(FALSE);
  }