-
Notifications
You must be signed in to change notification settings - Fork 99
Expand file tree
/
Copy pathgraph3d.c
More file actions
4591 lines (4024 loc) · 139 KB
/
graph3d.c
File metadata and controls
4591 lines (4024 loc) · 139 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
/* GNUPLOT - graph3d.c */
/*[
* Copyright 1986 - 1993, 1998, 2004 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.
]*/
/*
* AUTHORS
*
* Original Software:
* Gershon Elber and many others.
*
* 19 September 1992 Lawrence Crowl ([email protected])
* 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 "graph3d.h"
#include "alloc.h"
#include "axis.h"
#include "command.h"
#include "contour.h"
#include "gadgets.h"
#include "hidden3d.h"
#include "misc.h"
#include "term_api.h"
#include "util3d.h"
#include "util.h"
#include "vplot.h"
#include "pm3d.h"
#include "plot3d.h"
#include "color.h"
#include "plot.h"
static int key_entry_height; /* bigger of t->v_char, pointsize*t->v_tick */
static int key_title_height;
static int key_title_extra; /* allow room for subscript/superscript */
static int key_title_width;
static int key_width;
static int key_height;
static int key_font_hchar;
/* is contouring wanted ? */
t_contour_placement draw_contour = CONTOUR_NONE;
TBOOLEAN clabel_onecolor = FALSE; /* use same linetype for all contours */
int clabel_interval = 20; /* label every 20th contour segment */
int clabel_start = 5; /* starting with the 5th */
char *clabel_font = NULL; /* default to current font */
/* control parameters for contourfill */
t_contourfill contourfill = {
.mode = CFILL_AUTO,
.nslices = 5,
.tic_level = 0,
.firstlinetype = -1
};
static zslice *zslice_array; /* shared by plot3d_contourfill and zslice_callback */
static int current_slice = 0; /* shared index to zslice_array */
/* Draw the surface at all? (FALSE if only contours are wanted) */
TBOOLEAN draw_surface = TRUE;
/* Always create a gridded surface when lines are read from a data file */
TBOOLEAN implicit_surface = TRUE;
/* Was hidden3d display selected by user? */
TBOOLEAN hidden3d = FALSE;
int hidden3d_layer = LAYER_BACK;
/* Rotation and scale of the 3d view, as controlled by 'set view': */
float surface_rot_z = 30.0;
float surface_rot_x = 60.0;
float surface_scale = 1.0;
float surface_zscale = 1.0;
float surface_lscale = 0.0;
float mapview_scale = 1.0;
float azimuth = 0.0;
/* These flags indicate projection onto the xy, xz or yz plane
* as requested by 'set view map' or 'set view projection'.
* in_3d_polygon disables conversion of graph coordinates from x/y/z to
* hor/ver in projection; i.e. polygon vertices are always orthogonal x/y/z.
*/
TBOOLEAN splot_map = FALSE;
TBOOLEAN xz_projection = FALSE;
TBOOLEAN yz_projection = FALSE;
TBOOLEAN in_3d_polygon = FALSE;
TBOOLEAN zx_projection = FALSE;
/* position of the base plane, as given by 'set ticslevel' or 'set xyplane' */
t_xyplane xyplane = { 0.5, FALSE };
/* 'set isosamples' settings */
int iso_samples_1 = ISO_SAMPLES;
int iso_samples_2 = ISO_SAMPLES;
double xscale3d, yscale3d, zscale3d;
double xcenter3d = 0.0;
double ycenter3d = 0.0;
double zcenter3d = 0.0;
typedef enum { ALLGRID, FRONTGRID, BACKGRID, BORDERONLY } WHICHGRID;
static void do_3dkey_layout(legend_key *key, int *xinkey, int *yinkey);
static void plot3d_impulses(struct surface_points * plot);
static void plot3d_lines(struct surface_points * plot);
static void plot3d_points(struct surface_points * plot);
static void plot3d_polygons(struct surface_points * plot);
static void plot3d_zerrorfill(struct surface_points * plot);
static void plot3d_boxes(struct surface_points * plot);
static void plot3d_vectors(struct surface_points * plot);
static void plot3d_lines_pm3d(struct surface_points * plot);
static void plot3d_contourfill(struct surface_points * plot);
static void get_surface_cbminmax(struct surface_points *plot, double *cbmin, double *cbmax);
static void cntr3d_impulses(struct gnuplot_contours * cntr, struct lp_style_type * lp);
static void cntr3d_lines(struct gnuplot_contours * cntr, struct lp_style_type * lp);
static void cntr3d_points(struct gnuplot_contours * cntr, struct lp_style_type * lp);
static void cntr3d_labels(struct gnuplot_contours * cntr, char * leveltext,
struct text_label * label);
static void check_corner_height(struct coordinate * point,
double height[2][2], double depth[2][2]);
static void setup_3d_box_corners(void);
static void draw_3d_graphbox(struct surface_points * plot,
int plot_count,
WHICHGRID whichgrid, int current_layer);
static void xtick_callback(struct axis *, double place, char *text, int ticlevel,
struct lp_style_type grid, struct ticmark *userlabels);
static void ytick_callback(struct axis *, double place, char *text, int ticlevel,
struct lp_style_type grid, struct ticmark *userlabels);
static void ztick_callback(struct axis *, double place, char *text, int ticlevel,
struct lp_style_type grid, struct ticmark *userlabels);
static void zslice_callback(struct axis *, double place, char *text, int ticlevel,
struct lp_style_type grid, struct ticmark *userlabels);
static int find_maxl_cntr(struct gnuplot_contours * contours, int *count);
static int find_maxl_keys3d(struct surface_points *plots, int count, int *kcnt);
static void boundary3d(struct surface_points * plots, int count);
/* put entries in the key */
static void key_sample_line(int xl, int yl);
static void key_sample_point(struct surface_points *this_plot, int xl, int yl, int pointtype);
static void key_sample_line_pm3d(struct surface_points *plot, int xl, int yl);
static void key_sample_point_pm3d(struct surface_points *plot, int xl, int yl, int pointtype);
static void key_sample_fill(int xl, int yl, struct surface_points *this_plot);
static TBOOLEAN can_pm3d = FALSE;
static void key_text(int xl, int yl, char *text);
static void check3d_for_variable_color(struct surface_points *plot, struct coordinate *point);
static TBOOLEAN get_arrow3d(struct arrow_def*, double*, double*, double*, double*);
static void place_arrows3d(int);
static void place_labels3d(struct text_label * listhead, int layer);
static int map3d_getposition(struct position* pos, const char* what, double* xpos, double* ypos, double* zpos);
static void flip_projection_axis(struct axis *axis);
static void splot_map_activate(void);
static void splot_map_deactivate(void);
# define f_max(a,b) GPMAX((a),(b))
# define f_min(a,b) GPMIN((a),(b))
# define i_inrange(z,a,b) inrange((z),(a),(b))
#define apx_eq(x,y) (fabs(x-y) < 0.001)
#define ABS(x) ((x) >= 0 ? (x) : -(x))
#define SQR(x) ((x) * (x))
/* Define the boundary of the plot
* These are computed at each call to do_plot, and are constant over
* the period of one do_plot. They actually only change when the term
* type changes and when the 'set size' factors change.
*/
int xmiddle, ymiddle, xscaler, yscaler;
double xyscaler;
double radius_scaler;
static int ptitl_cnt;
static int max_ptitl_len;
static int titlelin;
static int key_sample_width, key_rows, key_cols, key_col_wth, yl_ref;
static double ktitle_lines = 0;
/* Boundary and scale factors, in user coordinates */
/* These positions assume a single linear scale encompassing the
* zrange plus extra space below for the baseplane. This was messy but
* correct before the introduction of nonlinear axes. Now - not so much.
*
* ceiling_z is the highest z in use
* floor_z is the lowest z in use
* base_z is the z of the base
* min3d_z is the lowest z of the graph area
* max3d_z is the highest z of the graph area
*
* ceiling_z is either max3d_z or base_z, and similarly for floor_z
* There should be no part of graph drawn outside
* min3d_z:max3d_z - apart from arrows, perhaps
*/
double floor_z;
double ceiling_z, base_z; /* made exportable for PM3D */
/* To handle a non-linear z axis we need to calculate these values on
* the other end of the linked linear:nonlinear axis pair.
*/
double floor_z1; /* Used also by map_z3d() */
static double ceiling_z1, base_z1;
transform_matrix trans_mat;
/* x and y input range endpoints where the three axes are to be
* displayed (left, front-left, and front-right edges of the cube) */
static double xaxis_y, yaxis_x, zaxis_x, zaxis_y;
/* ... and the same for the back, right, and front corners */
static double back_x, back_y;
static double right_x, right_y;
static double front_x, front_y;
/* The global flags splot_map, xz_projection, and yz_projection are specific views.
* These flag the more general case of looking down the x or y axis
*/
static TBOOLEAN xz_plane, yz_plane;
#ifdef USE_MOUSE
int axis3d_o_x, axis3d_o_y, axis3d_x_dx, axis3d_x_dy, axis3d_y_dx, axis3d_y_dy;
#endif
/* the penalty for convenience of using tic_gen to make callbacks
* to tick routines is that we cannot pass parameters very easily.
* We communicate with the tick_callbacks using static variables
*/
/* unit vector (terminal coords) */
static double tic_unitx, tic_unity, tic_unitz;
/* calculate the number and max-width of the keys for an splot.
* Note that a blank line is issued after each set of contours
*/
static int
find_maxl_keys3d(struct surface_points *plots, int count, int *kcnt)
{
int mlen, len, surf, cnt;
struct surface_points *this_plot;
mlen = cnt = 0;
this_plot = plots;
for (surf = 0; surf < count; this_plot = this_plot->next_sp, surf++) {
/* we draw a main entry if there is one, and we are
* drawing either surface, or unlabeled contours
*/
if (this_plot->title && *this_plot->title
&& !this_plot->title_is_suppressed && !this_plot->title_position) {
++cnt;
len = estimate_strlen(this_plot->title, NULL);
if (len > mlen)
mlen = len;
}
if (draw_contour && !clabel_onecolor && this_plot->contours != NULL
&& !this_plot->title_is_suppressed
&& this_plot->plot_style != LABELPOINTS) {
len = find_maxl_cntr(this_plot->contours, &cnt);
if (len > mlen)
mlen = len;
}
}
if (kcnt != NULL)
*kcnt = cnt;
return (mlen);
}
static int
find_maxl_cntr(struct gnuplot_contours *contours, int *count)
{
int cnt;
int mlen, len;
struct gnuplot_contours *cntrs = contours;
mlen = cnt = 0;
while (cntrs) {
if (cntrs->isNewLevel) {
len = estimate_strlen(cntrs->label, NULL)
- strspn(cntrs->label," ");
if (len)
cnt++;
if (len > mlen)
mlen = len;
}
cntrs = cntrs->next;
}
*count += cnt;
return (mlen);
}
/* borders of plotting area */
/* computed once on every call to do_plot */
static void
boundary3d(struct surface_points *plots, int count)
{
legend_key *key = &keyT;
struct termentry *t = term;
int i;
titlelin = 0;
if (key->swidth >= 0)
key_sample_width = key->swidth * t->h_char + t->h_tic;
else
key_sample_width = 0;
key_entry_height = t->v_tic * 1.25 * key->vert_factor;
if (key_entry_height < t->v_char) {
/* is this reasonable ? */
key_entry_height = t->v_char * key->vert_factor;
}
/* string lengths for internal key layout (other than the title)
* use the key font. Save it for later use.
*/
if (key->font)
t->set_font(key->font);
key_font_hchar = t->h_char;
if (key->font)
t->set_font("");
/* Approximate width of titles is used to determine number of rows, cols
* The actual widths will be recalculated later
*/
max_ptitl_len = find_maxl_keys3d(plots, count, &ptitl_cnt);
key_title_width = label_width(key->title.text, &i) * t->h_char;
ktitle_lines = i;
key_col_wth = (max_ptitl_len + 4) * key_font_hchar + key_sample_width;
if (lmargin.scalex == screen)
plot_bounds.xleft = lmargin.x * (double)t->xmax + 0.5;
else if (lmargin.x >= 0)
plot_bounds.xleft = lmargin.x * (double)t->h_char + 0.5;
else
plot_bounds.xleft = t->h_char * 2 + t->h_tic;
if (rmargin.scalex == screen)
plot_bounds.xright = rmargin.x * (double)t->xmax + 0.5;
else if (splot_map && rmargin.x >= 0)
plot_bounds.xright = xsize * t->xmax - (rmargin.x * t->h_char + 0.5);
else
plot_bounds.xright = xsize * t->xmax - t->h_char * 2 - t->h_tic;
/* New option "set key columns <ncol>" */
if (key->user_cols > 0) {
key_cols = key->user_cols;
key_rows = ceil((double)ptitl_cnt / (double)key_cols);
}
/* Automatic calculation of key columns and rows */
if (key->user_cols == 0) {
key_rows = ptitl_cnt;
key_cols = 1;
if (key_rows > key->maxrows && key->maxrows > 0) {
key_rows = key->maxrows;
key_cols = (ptitl_cnt - 1)/key_rows + 1;
}
if (key->visible)
if ((key->region == GPKEY_AUTO_EXTERIOR_MARGIN || key->region == GPKEY_AUTO_EXTERIOR_LRTBC)
&& key->margin == GPKEY_BMARGIN) {
if (ptitl_cnt > 0) {
/* calculate max no cols, limited by label-length */
key_cols = (plot_bounds.xright - plot_bounds.xleft)
/ ((max_ptitl_len + 4) * key_font_hchar + key_sample_width);
if (key_cols == 0)
key_cols = 1;
key_rows = ((ptitl_cnt - 1)/ key_cols) + 1;
/* Limit the number of rows if requested by user */
if (key_rows > key->maxrows && key->maxrows > 0)
key_rows = key->maxrows;
/* now calculate actual no cols depending on no rows */
key_cols = ((ptitl_cnt - 1)/ key_rows) + 1;
key_col_wth = (plot_bounds.xright - plot_bounds.xleft) / key_cols;
}
}
}
/* Sanity check top and bottom margins, in case the user got confused */
if (bmargin.scalex == screen && tmargin.scalex == screen)
if (bmargin.x > tmargin.x) {
double tmp = bmargin.x;
bmargin.x = tmargin.x;
tmargin.x = tmp;
}
/* this should also consider the view and number of lines in
* xformat || yformat || xlabel || ylabel */
if (bmargin.scalex == screen)
plot_bounds.ybot = bmargin.x * (double)t->ymax + 0.5;
else if (splot_map && bmargin.x >= 0)
plot_bounds.ybot = (double)t->v_char * bmargin.x;
else
plot_bounds.ybot = t->v_char * 2.5 + 1;
if (key->visible)
if (key_rows && (key->margin == GPKEY_BMARGIN)
&& (key->region == GPKEY_AUTO_EXTERIOR_MARGIN || key->region == GPKEY_AUTO_EXTERIOR_LRTBC))
plot_bounds.ybot += key_rows * key_entry_height + key_title_height;
if (title.text) {
titlelin++;
for (i = 0; i < strlen(title.text); i++) {
if (title.text[i] == '\\')
titlelin++;
}
}
if (tmargin.scalex == screen)
plot_bounds.ytop = tmargin.x * (double)t->ymax + 0.5;
else if (splot_map && tmargin.x >= 0)
plot_bounds.ytop = ysize * t->ymax - t->v_char * (titlelin + tmargin.x) - 1;
else
plot_bounds.ytop = ysize * t->ymax - t->v_char * (titlelin + 1.5) - 1;
/* Automatic calculation of key columns and rows */
if (key->visible && key->user_cols == 0) {
if (key->region == GPKEY_AUTO_INTERIOR_LRTBC
|| ((key->region == GPKEY_AUTO_EXTERIOR_LRTBC || key->region == GPKEY_AUTO_EXTERIOR_MARGIN)
&& key->margin == GPKEY_RMARGIN)) {
/* calculate max no rows, limited by plot_bounds.ytop-plot_bounds.ybot */
i = (plot_bounds.ytop - plot_bounds.ybot) / t->v_char - 1 - ktitle_lines;
if (i > key->maxrows && key->maxrows > 0)
i = key->maxrows;
if (i <= 0)
i = 1;
if (ptitl_cnt > i) {
key_cols = ((ptitl_cnt - 1)/ i) + 1;
/* now calculate actual no rows depending on no cols */
key_rows = ((ptitl_cnt - 1) / key_cols) + 1;
}
}
}
/* Calculation of key width */
if (key->visible) {
if (key->user_width.x == 0)
key_width = key_col_wth * key_cols - 2 * t->h_char;
else {
if (key->user_width.scalex == graph)
key_width = key->user_width.x * (plot_bounds.xright - plot_bounds.xleft);
else /* (key->user_width.scalex == screen) */
key_width = key->user_width.x * (double)(t->xmax-1);
key_col_wth = key_width / key_cols;
}
if ((key->region == GPKEY_AUTO_EXTERIOR_LRTBC || key->region == GPKEY_AUTO_EXTERIOR_MARGIN)
&& (key->margin == GPKEY_RMARGIN)
&& (rmargin.scalex != screen))
plot_bounds.xright -= key_width;
if ((key->region == GPKEY_AUTO_EXTERIOR_LRTBC || key->region == GPKEY_AUTO_EXTERIOR_MARGIN)
&& (key->margin == GPKEY_LMARGIN)
&& (lmargin.scalex != screen))
plot_bounds.xleft += key_width;
}
/* Make room for the colorbox to the right of the plot */
if (splot_map && is_plot_with_colorbox() && rmargin.scalex != screen) {
if ((color_box.where != SMCOLOR_BOX_NO) && (color_box.where != SMCOLOR_BOX_USER))
plot_bounds.xright -= 0.1 * (plot_bounds.xright-plot_bounds.xleft);
color_box.xoffset = 0;
}
/* Leave room for optional x2label and y2label in "set view map" mode.
* These estimates are crude compared to the effort we make in 2D plots
* but the user can adjust it with "offset" and "set rmargin".
* Later we will adjust the colorbox position by the same amount.
*/
if (splot_map && axis_array[SECOND_X_AXIS].label.text)
plot_bounds.ytop -= 1.5 * t->v_char;
if (splot_map && axis_array[SECOND_Y_AXIS].label.text)
plot_bounds.xright -= 2.5 * t->h_char;
if (!splot_map && aspect_ratio_3D > 0) {
int height = (plot_bounds.ytop - plot_bounds.ybot);
int width = (plot_bounds.xright - plot_bounds.xleft);
if (height > width) {
plot_bounds.ybot += (height-width)/2;
plot_bounds.ytop -= (height-width)/2;
} else {
plot_bounds.xleft += (width-height)/2;
plot_bounds.xright -= (width-height)/2;
}
}
if (lmargin.scalex != screen)
plot_bounds.xleft += t->xmax * xoffset;
if (rmargin.scalex != screen)
plot_bounds.xright += t->xmax * xoffset;
if (tmargin.scalex != screen)
plot_bounds.ytop += t->ymax * yoffset;
if (bmargin.scalex != screen)
plot_bounds.ybot += t->ymax * yoffset;
xmiddle = (plot_bounds.xright + plot_bounds.xleft) / 2;
ymiddle = (plot_bounds.ytop + plot_bounds.ybot) / 2;
/* HBB: Magic number alert! */
xscaler = ((plot_bounds.xright - plot_bounds.xleft) * 4L) / 7L;
yscaler = ((plot_bounds.ytop - plot_bounds.ybot) * 4L) / 7L;
/* Allow explicit control via set {}margin screen */
if (tmargin.scalex == screen && bmargin.scalex == screen)
yscaler = (plot_bounds.ytop - plot_bounds.ybot) / surface_scale;
if (rmargin.scalex == screen && lmargin.scalex == screen)
xscaler = (plot_bounds.xright - plot_bounds.xleft) / surface_scale;
/* prevent infinite loop or divide-by-zero if scaling is bad */
if (yscaler == 0) yscaler = 1;
if (xscaler == 0) xscaler = 1;
/* Let 2D 'set size {square|ratio}' affect splot_map also */
if (splot_map && (aspect_ratio != 0.0 || aspect_ratio_3D > 0)) {
double current_aspect_ratio;
if (aspect_ratio != 0)
current_aspect_ratio = aspect_ratio;
else
current_aspect_ratio = -1.0;
if (current_aspect_ratio < 0 && (X_AXIS.max - X_AXIS.min) != 0.0) {
current_aspect_ratio = - current_aspect_ratio
* fabs((Y_AXIS.max - Y_AXIS.min) /
(X_AXIS.max - X_AXIS.min));
}
/*{{{ set aspect ratio if valid and sensible */
if (current_aspect_ratio >= 0.01 && current_aspect_ratio <= 100.0) {
double current = (double)yscaler / xscaler ;
double required = current_aspect_ratio * t->v_tic / t->h_tic;
if (current > required)
/* too tall */
yscaler = xscaler * required;
else
/* too wide */
xscaler = yscaler / required;
}
}
/* For anything that really wants to be the same on x and y */
xyscaler = sqrt((double)xscaler * (double)yscaler);
/* This one is used to scale circles in 3D plots */
radius_scaler = xscaler * surface_scale / (X_AXIS.max - X_AXIS.min);
/* Set default clipping */
if (splot_map)
clip_area = &plot_bounds;
else if (term->flags & TERM_CAN_CLIP)
clip_area = NULL;
else
clip_area = &canvas;
}
static TBOOLEAN
get_arrow3d(
struct arrow_def* arrow,
double* dsx, double* dsy,
double* dex, double* dey)
{
map3d_position_double(&(arrow->start), dsx, dsy, "arrow");
if (arrow->type == arrow_end_relative) {
map3d_position_r_double(&(arrow->end), dex, dey, "arrow");
*dex += *dsx;
*dey += *dsy;
} else if (arrow->type == arrow_end_oriented) {
double aspect = effective_aspect_ratio();
double radius;
double junkw, junkh;
if (arrow->end.scalex != screen && arrow->end.scalex != character && !splot_map)
return FALSE;
map3d_position_r_double(&arrow->end, &junkw, &junkh, "arrow");
radius = junkw;
*dex = *dsx + cos(DEG2RAD * arrow->angle) * radius;
*dey = *dsy + sin(DEG2RAD * arrow->angle) * radius * aspect;
} else {
map3d_position_double(&(arrow->end), dex, dey, "arrow");
}
return TRUE;
}
static void
place_labels3d(struct text_label *listhead, int layer)
{
struct text_label *this_label;
int x, y;
term->pointsize(pointsize);
/* Hypertext labels? */
/* NB: currently svg is the only terminal that needs this extra step */
if (layer == LAYER_PLOTLABELS && listhead && listhead->hypertext
&& term->hypertext) {
term->hypertext(TERM_HYPERTEXT_FONT, listhead->font);
}
for (this_label = listhead;
this_label != NULL;
this_label = this_label->next) {
if (this_label->layer != layer)
continue;
if (layer == LAYER_PLOTLABELS) {
double xx, yy;
map3d_xy_double(this_label->place.x, this_label->place.y,
this_label->place.z, &xx, &yy);
x = xx;
y = yy;
/* Only clip in 2D */
if (splot_map && clip_point(x, y))
continue;
} else
map3d_position(&this_label->place, &x, &y, "label");
write_label(x, y, this_label);
}
}
static void
place_arrows3d(int layer)
{
struct arrow_def *this_arrow;
BoundingBox *clip_save = clip_area;
/* Allow arrows to run off the plot, so long as they are still on the canvas */
if (term->flags & TERM_CAN_CLIP)
clip_area = NULL;
else
clip_area = &canvas;
for (this_arrow = first_arrow; this_arrow != NULL;
this_arrow = this_arrow->next) {
double dsx, dsy, dex, dey;
if (this_arrow->arrow_properties.layer != layer)
continue;
if (get_arrow3d(this_arrow, &dsx, &dsy, &dex, &dey)) {
term_apply_lp_properties(&(this_arrow->arrow_properties.lp_properties));
apply_head_properties(&(this_arrow->arrow_properties));
draw_clip_arrow(dsx, dsy, dex, dey, this_arrow->arrow_properties.head);
} else {
FPRINTF((stderr,"place_arrows3d: skipping out-of-bounds arrow\n"));
}
}
clip_area = clip_save;
}
/* we precalculate features of the key, to save lots of nested
* ifs in code - x,y = user supplied or computed position of key
* taken to be inner edge of a line sample
*/
static int key_sample_left; /* offset from x for left of line sample */
static int key_sample_right; /* offset from x for right of line sample */
static int key_point_offset; /* offset from x for point sample */
static int key_text_left; /* offset from x for left-justified text */
static int key_text_right; /* offset from x for right-justified text */
static int key_size_left; /* distance from x to left edge of box */
static int key_size_right; /* distance from x to right edge of box */
void
do_3dplot(
struct surface_points *plots,
int pcount, /* count of plots in linked list */
REPLOT_TYPE replot_mode) /* replot/refresh/axes-only/quick-refresh */
{
struct termentry *t = term;
int surface;
struct surface_points *this_plot = NULL;
int xl = 0, yl = 0;
int xl_save, yl_save;
int xl_prev = 0, yl_prev = 0;
int title_x = 0, title_y = 0;
transform_matrix mat;
int key_count;
TBOOLEAN key_pass = FALSE;
legend_key *key = &keyT;
TBOOLEAN pm3d_order_depth = FALSE;
AXIS *primary_z;
/* Initiate transformation matrix using the global view variables. */
if (splot_map) {
splot_map_activate();
} else if (xz_projection) {
surface_rot_x = 270.;
surface_rot_z = 0.;
surface_scale = 1.425 * mapview_scale;
} else if (yz_projection) {
surface_rot_x = 90.;
surface_rot_z = 90.;
surface_scale = 1.425 * mapview_scale;
flip_projection_axis(&axis_array[FIRST_Z_AXIS]);
} else if (surface_rot_z == 0. && (surface_rot_x == 90. || surface_rot_x == 270.)) {
zx_projection = TRUE;
} else {
zx_projection = FALSE;
}
in_3d_polygon = FALSE; /* protects polygons from xz, yz projections */
mat_rot_z(surface_rot_z, trans_mat);
mat_rot_x(surface_rot_x, mat);
mat_mult(trans_mat, trans_mat, mat);
mat_scale(surface_scale / 2.0, surface_scale / 2.0, surface_scale / 2.0, mat);
mat_mult(trans_mat, trans_mat, mat);
/* The azimuth is applied as a rotation about the line of sight */
if (azimuth !=0 && !splot_map) {
mat_rot_z(azimuth, mat);
mat_mult(trans_mat, trans_mat, mat);
}
if (polar)
int_error(NO_CARET,"Cannot splot in polar coordinate system.");
/* In the case of a nonlinear z axis this points to the linear version */
/* that shadows it. Otherwise it just points to FIRST_Z_AXIS. */
primary_z = (nonlinear(&Z_AXIS)) ? Z_AXIS.linked_to_primary : &Z_AXIS;
/* absolute or relative placement of xyplane along z */
if (nonlinear(&Z_AXIS)) {
if (xyplane.absolute) {
if (primary_z->log && xyplane.z <= 0) {
base_z1 = eval_link_function(primary_z, Z_AXIS.min);
} else {
base_z1 = eval_link_function(primary_z, xyplane.z);
}
} else {
base_z1 = primary_z->min - (primary_z->max - primary_z->min) * xyplane.z;
}
base_z = eval_link_function(&Z_AXIS, base_z1);
} else {
if (xyplane.absolute)
base_z1 = xyplane.z;
else
base_z1 = primary_z->min - (primary_z->max - primary_z->min) * xyplane.z;
base_z = base_z1;
}
/* If we are to draw some portion of the xyplane make sure zmin is updated properly. */
if (X_AXIS.ticmode || Y_AXIS.ticmode || draw_border & 0x00F) {
if (primary_z->min > primary_z->max) {
floor_z1 = GPMAX(primary_z->min, base_z1);
ceiling_z1 = GPMIN(primary_z->max, base_z1);
} else {
floor_z1 = GPMIN(primary_z->min, base_z1);
ceiling_z1 = GPMAX(primary_z->max, base_z1);
}
} else {
floor_z1 = primary_z->min;
ceiling_z1 = primary_z->max;
}
if (nonlinear(&Z_AXIS)) {
floor_z = eval_link_function(&Z_AXIS, floor_z1);
ceiling_z = eval_link_function(&Z_AXIS, ceiling_z1);
} else {
floor_z = floor_z1;
ceiling_z = ceiling_z1;
}
if (X_AXIS.min == X_AXIS.max)
int_error(NO_CARET,"x_min3d should not equal x_max3d!");
if (Y_AXIS.min == Y_AXIS.max)
int_error(NO_CARET,"y_min3d should not equal y_max3d!");
if (Z_AXIS.min == Z_AXIS.max)
int_error(NO_CARET,"z_min3d should not equal z_max3d!");
/* Special case projections of the xz or yz plane */
/* Place x or y axis to the left of the plot */
xz_plane = yz_plane = FALSE;
if (!splot_map && (surface_rot_x == 90 || surface_rot_x == 270)) {
if (surface_rot_z == 0 || surface_rot_z == 180) {
xz_plane = TRUE;
base_z = floor_z;
}
if (surface_rot_z == 90 || surface_rot_z == 270) {
yz_plane = TRUE;
if (surface_rot_x == 270 || yz_projection)
base_z = ceiling_z;
}
}
term_start_plot();
(term->layer)(TERM_LAYER_3DPLOT);
screen_ok = FALSE;
/* Sync point for epslatex text positioning */
(term->layer)(TERM_LAYER_BACKTEXT);
/* now compute boundary for plot */
boundary3d(plots, pcount);
axis_set_scale_and_range(&axis_array[FIRST_X_AXIS], plot_bounds.xleft, plot_bounds.xright);
axis_set_scale_and_range(&axis_array[FIRST_Y_AXIS], plot_bounds.ybot, plot_bounds.ytop);
axis_set_scale_and_range(&axis_array[FIRST_Z_AXIS], floor_z, ceiling_z);
/* SCALE FACTORS */
zscale3d = 2.0 / (ceiling_z - floor_z) * surface_zscale;
yscale3d = 2.0 / (Y_AXIS.max - Y_AXIS.min);
xscale3d = 2.0 / (X_AXIS.max - X_AXIS.min);
if (nonlinear(&X_AXIS))
xscale3d = 2.0 / (X_AXIS.linked_to_primary->max - X_AXIS.linked_to_primary->min);
if (nonlinear(&Y_AXIS))
yscale3d = 2.0 / (Y_AXIS.linked_to_primary->max - Y_AXIS.linked_to_primary->min);
if (nonlinear(&Z_AXIS))
zscale3d = 2.0 / (ceiling_z1 - floor_z1) * surface_zscale;
/* Allow 'set view equal xy' to adjust rendered length of the X and/or Y axes. */
/* NB: only works correctly for terminals whose coordinate system is isotropic. */
xcenter3d = ycenter3d = zcenter3d = 0.0;
if (!splot_map && aspect_ratio_3D >= 2) {
if (yscale3d > xscale3d) {
ycenter3d = 1.0 - xscale3d/yscale3d;
yscale3d = xscale3d;
} else if (xscale3d > yscale3d) {
xcenter3d = 1.0 - yscale3d/xscale3d;
xscale3d = yscale3d;
}
if (aspect_ratio_3D >= 3)
zscale3d = xscale3d;
}
/* FIXME: I do not understand why this is correct */
if (nonlinear(&Z_AXIS))
zcenter3d = 0.0;
/* Without this the rotation center would be located at */
/* the bottom of the plot. This places it in the middle.*/
else
zcenter3d = -(ceiling_z - floor_z) / 2.0 * zscale3d + 1;
/* Needed for mousing by outboard terminal drivers */
if (splot_map) {
AXIS *X = &axis_array[FIRST_X_AXIS];
AXIS *Y = &axis_array[FIRST_Y_AXIS];
AXIS *Z = &axis_array[FIRST_Z_AXIS];
int xl, xr, yb, yt;
map3d_xy(X->min, Y->min, Z->min, &xl, &yb);
map3d_xy(X->max, Y->max, Z->min, &xr, &yt);
axis_set_scale_and_range(X, xl, xr);
axis_set_scale_and_range(Y, yb, yt);
}
/* Initialize palette */
if (replot_mode != AXIS_ONLY_ROTATE) {
can_pm3d = is_plot_with_palette() && !make_palette()
&& ((term->flags & TERM_NULL_SET_COLOR) == 0);
}
/* Give a chance for rectangles to be behind everything else */
place_objects( first_object, LAYER_BEHIND, 3);
if (replot_mode != AXIS_ONLY_ROTATE)
place_pixmaps(LAYER_BEHIND, 3);
term_apply_lp_properties(&border_lp); /* border linetype */
/* must come before using draw_3d_graphbox() the first time */
setup_3d_box_corners();
/* DRAW GRID AND BORDER */
/* Original behaviour: draw entire grid in back, if 'set grid back': */
/* HBB 20040331: but not if in hidden3d mode */
if (splot_map && border_layer != LAYER_FRONT)
draw_3d_graphbox(plots, pcount, BORDERONLY, LAYER_BACK);
else if (!hidden3d && (grid_layer == LAYER_BACK))
draw_3d_graphbox(plots, pcount, ALLGRID, LAYER_BACK);
else if (!hidden3d && (grid_layer == LAYER_BEHIND))
/* Default layering mode. Draw the back part now, but not if
* hidden3d is in use, because that relies on all isolated
* lines being output after all surfaces have been defined. */
draw_3d_graphbox(plots, pcount, BACKGRID, LAYER_BACK);
else if (hidden3d && border_layer == LAYER_BEHIND)
draw_3d_graphbox(plots, pcount, ALLGRID, LAYER_BACK);
/* Save state of plot_bounds before applying rotations, etc */
memcpy(&page_bounds, &plot_bounds, sizeof(page_bounds));
/* Clipping in 'set view map' mode should be like 2D clipping */
if (splot_map) {
int map_x1, map_y1, map_x2, map_y2;
map3d_xy(X_AXIS.min, Y_AXIS.min, base_z, &map_x1, &map_y1);
map3d_xy(X_AXIS.max, Y_AXIS.max, base_z, &map_x2, &map_y2);
plot_bounds.xleft = map_x1;
plot_bounds.xright = map_x2;
plot_bounds.ybot = map_y2;
plot_bounds.ytop = map_y1;
}
/* Define the clipping area in 3D to lie between the left-most and
* right-most graph box edges. This is introduced for the benefit of
* zooming in the canvas terminal. It may or may not make any practical
* difference for other terminals. If it causes problems, then we will need
* a separate BoundingBox structure to track the actual 3D graph box.
*/
else if (azimuth == 0) {
int xl, xb, xr, xf, yl, yb, yr, yf;
map3d_xy(zaxis_x, zaxis_y, base_z, &xl, &yl);
map3d_xy(back_x , back_y , base_z, &xb, &yb);
map3d_xy(right_x, right_y, base_z, &xr, &yr);
map3d_xy(front_x, front_y, base_z, &xf, &yf);
plot_bounds.xleft = GPMIN(xl, xb); /* Always xl? */
plot_bounds.xright = GPMAX(xb, xr); /* Always xr? */
}
/* PLACE TITLE */
if (title.text != 0) {
int x, y;
if (splot_map) { /* case 'set view map' */
int map_x1, map_y1, map_x2, map_y2;
int tics_len = 0;
if (X_AXIS.ticmode & TICS_MIRROR) {
tics_len = (X_AXIS.ticscale * (X_AXIS.tic_in ? -1 : 1) * (term->v_tic));
if (tics_len < 0) tics_len = 0; /* take care only about upward tics */
}
map3d_xy(X_AXIS.min, Y_AXIS.min, base_z, &map_x1, &map_y1);
map3d_xy(X_AXIS.max, Y_AXIS.max, base_z, &map_x2, &map_y2);
/* Distance between the title base line and graph top line or the upper part of
tics is as given by character height: */
x = ((map_x1 + map_x2) / 2);
y = (map_y1 + tics_len + (titlelin + 0.5) * (t->v_char));
if (splot_map && axis_array[SECOND_X_AXIS].ticmode)
y += 0.5 * t->v_char;
if (splot_map && axis_array[SECOND_X_AXIS].label.text)
y += 1.0 * t->v_char;
} else { /* usual 3d set view ... */
x = (plot_bounds.xleft + plot_bounds.xright) / 2;
y = (plot_bounds.ytop + titlelin * (t->h_char));
}
/* Save title position for later */
title_x = x;
title_y = y;
}
/* PLACE TIMELABEL */
if (timelabel.text) {
int x, y;
x = t->v_char;
y = timelabel_bottom
? yoffset * Y_AXIS.max + t->v_char
: plot_bounds.ytop - t->v_char;
do_timelabel(x,y);
}
/* Add 'back' color box */
if ((replot_mode != AXIS_ONLY_ROTATE)
&& can_pm3d && is_plot_with_colorbox() && color_box.layer == LAYER_BACK)
draw_color_smooth_box(MODE_SPLOT);
/* Grid walls */
place_objects(grid_wall, LAYER_BACK, 3);
/* pixmaps before objects so that a rectangle can be used as a border */
place_pixmaps(LAYER_BACK, 3);