-
Notifications
You must be signed in to change notification settings - Fork 306
/
graphics.c
3498 lines (3144 loc) · 96.6 KB
/
graphics.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* R : A Computer Language for Statistical Data Analysis
* Copyright (C) 1995, 1996 Robert Gentleman and Ross Ihaka
* Copyright (C) 1997--2016 The R Core Team
* Copyright (C) 2002--2011 The R Foundation
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, a copy is available at
* https://www.R-project.org/Licenses/
* This is an extensive reworking by Paul Murrell of an original
* quick hack by Ross Ihaka designed to give a superset of the
* functionality in the AT&T Bell Laboratories GRZ library.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <Defn.h>
#include <float.h> /* for DBL_EPSILON etc */
#include <Graphics.h>
// --> R_ext/GraphicsEngine.h + Rgraphics.h
#include <GraphicsBase.h> /* setBaseDevice */
#include <Rmath.h> /* eg. fmax2() */
#ifdef ENABLE_NLS
#include <libintl.h>
#undef _
#define _(String) dgettext ("grDevices", String)
#else
#define _(String) (String)
#endif
/*--->> Documentation now in ../include/Rgraphics.h "API" ----- */
double R_Log10(double x)
{
return (R_FINITE(x) && x > 0.0) ? log10(x) : NA_REAL;
}
/*-------------------------------------------------------------------
*
* TRANSFORMATIONS
*
* There are five major regions on a device, for any
* particular figure: the outer margins, which "stick"
* to the edges of the device; the inner region, which
* is defined as the total device less the outer margins;
* the figure region, which defaults from the current
* layout (mfrow, mfcol, layout) unless the user specifies
* it directly (fig, fin); the figure margins, which
* "stick" to the edges of the plot region; and thed
* plot region, which is the figure region less the figure
* margins by default unless the user specifies it directly
* (plt, pin)
*
* COORDINATE SYSTEMS
*
* DEVICE = devices natural coordinate system
* (e.g., pixels, 1/72", ...)
* NDC = normalised device coordinates (0..1 on device)
* INCHES = inches
* OMA1..4 = outer margin coordinates
* NIC = normalised inner region coordinates
* (0..1 on inner region)
* NFC = normalised figure coordinates
* (0..1 on figure region)
* MAR1..4 = figure margin coordinates
* NPC = normalised plot coordinates
* (0..1 on plot region)
* USER = world or data coordinates
*
*
* UNITS
*
* All of the above, except OMA1..4 and MAR1..4, plus ...
*
* LINES = line coordinates (lines of margin; based on mex)
* CHARS = char coordinates (lines of text; based on cex)
*
* The function Convert(value, from, to) is provided
* to transform between any pair of coordinate systems
* (for transforming locations)
*
* The functions ConvertXUnits(value, from, to) and
* ConvertYUnits(value, from, to) are provided to transform
* between any pair of units (for transforming dimensions)
*
* IMPORTANT: if user coordinates are logged, then the
* conversion to/from USER units will not work. in this
* case it is necessary to use convert(x1) - convert(x2)
* rather than convert(x1 - x2)
*
*/
/* In interpreted R, units are as follows:
* 1 = "user"
* 2 = "figure"
* 3 = "inches"
* the function GMapUnits provides a mapping
* between interpreted units and internal units.
*/
GUnit GMapUnits(int Runits)
{
switch (Runits) {
case 1: return USER;
case 2: return NFC;
case 3: return INCHES;
default: return 0;
}
}
/* Conversions Between Units*/
/* Used to be global (non-static) -- but are nowhere declared.
* The public interface is through G[XY]ConvertUnits() */
static double xNDCtoDevUnits(double x, pGEDevDesc dd)
{
return x*fabs(gpptr(dd)->ndc2dev.bx);
}
static double yNDCtoDevUnits(double y, pGEDevDesc dd)
{
return y*fabs(gpptr(dd)->ndc2dev.by);
}
static double xNICtoDevUnits(double x, pGEDevDesc dd)
{
return x*fabs(gpptr(dd)->inner2dev.bx);
}
static double yNICtoDevUnits(double y, pGEDevDesc dd)
{
return y*fabs(gpptr(dd)->inner2dev.by);
}
static double xNFCtoDevUnits(double x, pGEDevDesc dd)
{
return x*fabs(gpptr(dd)->fig2dev.bx);
}
static double yNFCtoDevUnits(double y, pGEDevDesc dd)
{
return y*fabs(gpptr(dd)->fig2dev.by);
}
static double xNPCtoDevUnits(double x, pGEDevDesc dd)
{
return xNFCtoDevUnits(x*(gpptr(dd)->plt[1] - gpptr(dd)->plt[0]), dd);
}
static double yNPCtoDevUnits(double y, pGEDevDesc dd)
{
return yNFCtoDevUnits(y*(gpptr(dd)->plt[3] - gpptr(dd)->plt[2]), dd);
}
static double xUsrtoDevUnits(double x, pGEDevDesc dd)
{
return xNFCtoDevUnits(x*gpptr(dd)->win2fig.bx, dd);
}
static double yUsrtoDevUnits(double y, pGEDevDesc dd)
{
return yNFCtoDevUnits(y*gpptr(dd)->win2fig.by, dd);
}
static double xInchtoDevUnits(double x, pGEDevDesc dd)
{
return xNDCtoDevUnits(x*gpptr(dd)->xNDCPerInch, dd);
}
static double yInchtoDevUnits(double y, pGEDevDesc dd)
{
return yNDCtoDevUnits(y*gpptr(dd)->yNDCPerInch, dd);
}
static double xLinetoDevUnits(double x, pGEDevDesc dd)
{
return xNDCtoDevUnits(x*gpptr(dd)->xNDCPerLine, dd);
}
static double yLinetoDevUnits(double y, pGEDevDesc dd)
{
return yNDCtoDevUnits(y*gpptr(dd)->yNDCPerLine, dd);
}
static double xChartoDevUnits(double x, pGEDevDesc dd)
{
return xNDCtoDevUnits(x*gpptr(dd)->cex*gpptr(dd)->xNDCPerChar, dd);
}
static double yChartoDevUnits(double y, pGEDevDesc dd)
{
return yNDCtoDevUnits(y*gpptr(dd)->cex*gpptr(dd)->yNDCPerChar, dd);
}
static double xDevtoNDCUnits(double x, pGEDevDesc dd)
{
return x/fabs(gpptr(dd)->ndc2dev.bx);
}
static double yDevtoNDCUnits(double y, pGEDevDesc dd)
{
return y/fabs(gpptr(dd)->ndc2dev.by);
}
static double xDevtoNICUnits(double x, pGEDevDesc dd)
{
return x/fabs(gpptr(dd)->inner2dev.bx);
}
static double yDevtoNICUnits(double y, pGEDevDesc dd)
{
return y/fabs(gpptr(dd)->inner2dev.by);
}
static double xDevtoNFCUnits(double x, pGEDevDesc dd)
{
return x/fabs(gpptr(dd)->fig2dev.bx);
}
static double yDevtoNFCUnits(double y, pGEDevDesc dd)
{
return y/fabs(gpptr(dd)->fig2dev.by);
}
static double xDevtoNPCUnits(double x, pGEDevDesc dd)
{
return xDevtoNFCUnits(x, dd)/(gpptr(dd)->plt[1] - gpptr(dd)->plt[0]);
}
static double yDevtoNPCUnits(double y, pGEDevDesc dd)
{
return yDevtoNFCUnits(y, dd)/(gpptr(dd)->plt[3] - gpptr(dd)->plt[2]);
}
static double xDevtoUsrUnits(double x, pGEDevDesc dd)
{
return xDevtoNFCUnits(x, dd)/gpptr(dd)->win2fig.bx;
}
static double yDevtoUsrUnits(double y, pGEDevDesc dd)
{
return yDevtoNFCUnits(y, dd)/gpptr(dd)->win2fig.by;
}
static double xDevtoInchUnits(double x, pGEDevDesc dd)
{
return xDevtoNDCUnits(x, dd)/gpptr(dd)->xNDCPerInch;
}
static double yDevtoInchUnits(double y, pGEDevDesc dd)
{
return yDevtoNDCUnits(y, dd)/gpptr(dd)->yNDCPerInch;
}
static double xDevtoLineUnits(double x, pGEDevDesc dd)
{
return xDevtoNDCUnits(x, dd)/gpptr(dd)->xNDCPerLine;
}
static double yDevtoLineUnits(double y, pGEDevDesc dd)
{
return yDevtoNDCUnits(y, dd)/gpptr(dd)->yNDCPerLine;
}
/* NOTE that use the _current_ gpptr(dd)->cex here */
/* the conversion for lines doesn't have to worry about */
/* this because gpptr(dd)->mex can only be set once per plot */
static double xDevtoCharUnits(double x, pGEDevDesc dd)
{
return xDevtoNDCUnits(x, dd)/(gpptr(dd)->cex * gpptr(dd)->xNDCPerChar);
}
static double yDevtoCharUnits(double y, pGEDevDesc dd)
{
return yDevtoNDCUnits(y, dd)/(gpptr(dd)->cex * gpptr(dd)->yNDCPerChar);
}
static void NORET BadUnitsError(const char *where)
{
error(_("bad units specified in '%s'"), where);
}
/* GConvertXUnits() and GConvertYUnits() convert
a single value fromUnits toUnits : */
double GConvertXUnits(double x, GUnit fromUnits, GUnit toUnits, pGEDevDesc dd)
{
double dev, final;
switch (fromUnits) {
case DEVICE: dev = x; break;
case NDC: dev = xNDCtoDevUnits(x, dd); break;
case NIC: dev = xNICtoDevUnits(x, dd); break;
case NFC: dev = xNFCtoDevUnits(x, dd); break;
case NPC: dev = xNPCtoDevUnits(x, dd); break;
case USER: dev = xUsrtoDevUnits(x, dd); break;
case INCHES: dev = xInchtoDevUnits(x, dd); break;
case LINES: dev = xLinetoDevUnits(x, dd); break;
case CHARS: dev = xChartoDevUnits(x, dd); break;
default: dev = 0; BadUnitsError("GConvertXUnits");
}
switch (toUnits) {
case DEVICE: final = dev; break;
case NDC: final = xDevtoNDCUnits(dev, dd); break;
case NIC: final = xDevtoNICUnits(dev, dd); break;
case NFC: final = xDevtoNFCUnits(dev, dd); break;
case NPC: final = xDevtoNPCUnits(dev, dd); break;
case USER: final = xDevtoUsrUnits(dev, dd); break;
case INCHES: final = xDevtoInchUnits(dev, dd); break;
case LINES: final = xDevtoLineUnits(dev, dd); break;
case CHARS: final = xDevtoCharUnits(dev, dd); break;
default: final = 0; BadUnitsError("GConvertXUnits");
}
return final;
}
double GConvertYUnits(double y, GUnit fromUnits, GUnit toUnits, pGEDevDesc dd)
{
double dev, final;
switch (fromUnits) {
case DEVICE: dev = y; break;
case NDC: dev = yNDCtoDevUnits(y, dd); break;
case NIC: dev = yNICtoDevUnits(y, dd); break;
case NFC: dev = yNFCtoDevUnits(y, dd); break;
case NPC: dev = yNPCtoDevUnits(y, dd); break;
case USER: dev = yUsrtoDevUnits(y, dd); break;
case INCHES: dev = yInchtoDevUnits(y, dd); break;
case LINES: dev = yLinetoDevUnits(y, dd); break;
case CHARS: dev = yChartoDevUnits(y, dd); break;
default: dev = 0; BadUnitsError("GConvertYUnits");
}
switch (toUnits) {
case DEVICE: final = dev; break;
case NDC: final = yDevtoNDCUnits(dev, dd); break;
case NIC: final = yDevtoNICUnits(dev, dd); break;
case NFC: final = yDevtoNFCUnits(dev, dd); break;
case NPC: final = yDevtoNPCUnits(dev, dd); break;
case USER: final = yDevtoUsrUnits(dev, dd); break;
case INCHES: final = yDevtoInchUnits(dev, dd); break;
case LINES: final = yDevtoLineUnits(dev, dd); break;
case CHARS: final = yDevtoCharUnits(dev, dd); break;
default: final = 0; BadUnitsError("GConvertYUnits");
}
return final;
}
/* Functions to convert locations from one coordinate system to another */
/* OTHER coordinate systems to DEVICE */
/* Used to be global (non-static) -- but are nowhere declared.
* The public interface is GConvert(), GConvertX(), GConvertY() */
static double xNDCtoDev(double x, pGEDevDesc dd)
{
return gpptr(dd)->ndc2dev.ax + x*gpptr(dd)->ndc2dev.bx;
}
static double yNDCtoDev(double y, pGEDevDesc dd)
{
return gpptr(dd)->ndc2dev.ay + y*gpptr(dd)->ndc2dev.by;
}
static double xInchtoDev(double x, pGEDevDesc dd)
{
return xNDCtoDev(x*gpptr(dd)->xNDCPerInch, dd);
}
static double yInchtoDev(double y, pGEDevDesc dd)
{
return yNDCtoDev(y*gpptr(dd)->yNDCPerInch, dd);
}
static double xLinetoDev(double x, pGEDevDesc dd)
{
return xNDCtoDev(x*gpptr(dd)->xNDCPerLine, dd);
}
static double yLinetoDev(double y, pGEDevDesc dd)
{
return yNDCtoDev(y*gpptr(dd)->yNDCPerLine, dd);
}
static double xNICtoDev(double x, pGEDevDesc dd)
{
return gpptr(dd)->inner2dev.ax + x*gpptr(dd)->inner2dev.bx;
}
static double yNICtoDev(double y, pGEDevDesc dd)
{
return gpptr(dd)->inner2dev.ay + y*gpptr(dd)->inner2dev.by;
}
/* NOTE that an x-coordinate in OMA2 or OMA4 converts to a */
/* y-coordinate in Dev and a y-coordinate in OMA2 or OMA4 */
/* converts to an x-coordinate in Dev */
static double xOMA1toDev(double x, pGEDevDesc dd)
{
return xNICtoDev(x, dd);
}
static double yOMA1toDev(double y, pGEDevDesc dd)
{
return yLinetoDev((gpptr(dd)->oma[0] - y), dd);
}
static double xOMA2toyDev(double x, pGEDevDesc dd)
{
return yNICtoDev(x, dd);
}
static double yOMA2toxDev(double y, pGEDevDesc dd)
{
return xLinetoDev((gpptr(dd)->oma[1] - y), dd);
}
static double xOMA3toDev(double x, pGEDevDesc dd)
{
return xNICtoDev(x, dd);
}
static double yOMA3toDev(double y, pGEDevDesc dd)
{
double ndc = 1.0-yDevtoNDC(yLinetoDev((gpptr(dd)->oma[2] - y), dd), dd);
return yNDCtoDev(ndc, dd);
}
static double xOMA4toyDev(double x, pGEDevDesc dd)
{
return yNICtoDev(x, dd);
}
static double yOMA4toxDev(double y, pGEDevDesc dd)
{
double ndc = 1.0-xDevtoNDC(xLinetoDev(gpptr(dd)->oma[3]-y, dd), dd);
return xNDCtoDev(ndc, dd);
}
static double xNFCtoDev(double x, pGEDevDesc dd)
{
return gpptr(dd)->fig2dev.ax + x*gpptr(dd)->fig2dev.bx;
}
static double yNFCtoDev(double y, pGEDevDesc dd)
{
return gpptr(dd)->fig2dev.ay + y*gpptr(dd)->fig2dev.by;
}
static double xNPCtoDev(double x, pGEDevDesc dd)
{
return xNFCtoDev(gpptr(dd)->plt[0] +
x*(gpptr(dd)->plt[1] - gpptr(dd)->plt[0]), dd);
}
static double yNPCtoDev(double y, pGEDevDesc dd)
{
return yNFCtoDev(gpptr(dd)->plt[2] +
y*(gpptr(dd)->plt[3] - gpptr(dd)->plt[2]), dd);
}
static double xUsrtoDev(double x, pGEDevDesc dd)
{
if (gpptr(dd)->xlog)
x = R_Log10(x);
return xNFCtoDev(gpptr(dd)->win2fig.ax + x*gpptr(dd)->win2fig.bx, dd);
}
static double yUsrtoDev(double y, pGEDevDesc dd)
{
if (gpptr(dd)->ylog)
y = R_Log10(y);
return yNFCtoDev(gpptr(dd)->win2fig.ay + y*gpptr(dd)->win2fig.by, dd);
}
/* NOTE that an x-coordinate in MAR2 or MAR4 converts to a */
/* y-coordinate in Dev and a y-coordinate in MAR2 or MAR4 */
/* converts to an x-coordinate in Dev */
static double xMAR1toDev(double x, pGEDevDesc dd)
{
return xUsrtoDev(x, dd);
}
static double yMAR1toDev(double y, pGEDevDesc dd)
{
double nfc = GConvertYUnits(y, LINES, NFC, dd);
return yNFCtoDev(gpptr(dd)->plt[2] - nfc, dd);
}
static double xMAR2toyDev(double x, pGEDevDesc dd)
{
return yUsrtoDev(x, dd);
}
static double yMAR2toxDev(double y, pGEDevDesc dd)
{
double nfc = GConvertXUnits(y, LINES, NFC, dd);
return xNFCtoDev(gpptr(dd)->plt[0] - nfc, dd);
}
static double xMAR3toDev(double x, pGEDevDesc dd)
{
return xUsrtoDev(x, dd);
}
static double yMAR3toDev(double y, pGEDevDesc dd)
{
double nfc = GConvertYUnits(y, LINES, NFC, dd);
return yNFCtoDev(gpptr(dd)->plt[3] + nfc, dd);
}
static double xMAR4toyDev(double x, pGEDevDesc dd)
{
return yUsrtoDev(x, dd);
}
static double yMAR4toxDev(double y, pGEDevDesc dd)
{
double nfc = GConvertXUnits(y, LINES, NFC, dd);
return xNFCtoDev(gpptr(dd)->plt[1] + nfc, dd);
}
/* DEVICE coordinates to OTHER */
double xDevtoNDC(double x, pGEDevDesc dd)
{
return (x - gpptr(dd)->ndc2dev.ax)/gpptr(dd)->ndc2dev.bx;
}
double yDevtoNDC(double y, pGEDevDesc dd)
{
return (y - gpptr(dd)->ndc2dev.ay)/gpptr(dd)->ndc2dev.by;
}
static double xDevtoInch(double x, pGEDevDesc dd)
{
return xDevtoNDC(x, dd)/gpptr(dd)->xNDCPerInch;
}
static double yDevtoInch(double y, pGEDevDesc dd)
{
return yDevtoNDC(y, dd)/gpptr(dd)->yNDCPerInch;
}
static double xDevtoLine(double x, pGEDevDesc dd)
{
return xDevtoNDC(x, dd)/gpptr(dd)->xNDCPerLine;
}
static double yDevtoLine(double y, pGEDevDesc dd)
{
return yDevtoNDC(y, dd)/gpptr(dd)->yNDCPerLine;
}
static double xDevtoNIC(double x, pGEDevDesc dd)
{
return (x - gpptr(dd)->inner2dev.ax)/gpptr(dd)->inner2dev.bx;
}
static double yDevtoNIC(double y, pGEDevDesc dd)
{
return (y - gpptr(dd)->inner2dev.ay)/gpptr(dd)->inner2dev.by;
}
static double xDevtoOMA1(double x, pGEDevDesc dd)
{
return xDevtoNIC(x, dd);
}
static double yDevtoOMA1(double y, pGEDevDesc dd)
{
return gpptr(dd)->oma[0] - yDevtoLine(y, dd);
}
static double xDevtoyOMA2(double x, pGEDevDesc dd)
{
return gpptr(dd)->oma[1] - xDevtoLine(x, dd);
}
static double yDevtoxOMA2(double y, pGEDevDesc dd)
{
return yDevtoNIC(y, dd);
}
static double xDevtoOMA3(double x, pGEDevDesc dd)
{
return xDevtoNIC(x, dd);
}
static double yDevtoOMA3(double y, pGEDevDesc dd)
{
double line = (1.0 - yDevtoNDC(y, dd))/gpptr(dd)->yNDCPerLine;
return gpptr(dd)->oma[2] - line;
}
static double xDevtoyOMA4(double x, pGEDevDesc dd)
{
double line = (1.0 - xDevtoNDC(x, dd))/gpptr(dd)->xNDCPerLine;
return gpptr(dd)->oma[3] - line;
}
static double yDevtoxOMA4(double y, pGEDevDesc dd)
{
return yDevtoNIC(y, dd);
}
double xDevtoNFC(double x, pGEDevDesc dd)
{
return (x - gpptr(dd)->fig2dev.ax)/gpptr(dd)->fig2dev.bx;
}
double yDevtoNFC(double y, pGEDevDesc dd)
{
return (y - gpptr(dd)->fig2dev.ay)/gpptr(dd)->fig2dev.by;
}
double xDevtoNPC(double x, pGEDevDesc dd)
{
return (xDevtoNFC(x, dd) - gpptr(dd)->plt[0])/
(gpptr(dd)->plt[1] - gpptr(dd)->plt[0]);
}
double yDevtoNPC(double y, pGEDevDesc dd)
{
return (yDevtoNFC(y, dd) - gpptr(dd)->plt[2])/
(gpptr(dd)->plt[3] - gpptr(dd)->plt[2]);
}
/* a special case (NPC = normalised plot region coordinates) */
double xNPCtoUsr(double x, pGEDevDesc dd)
{
if (gpptr(dd)->xlog)
return Rexp10(gpptr(dd)->logusr[0] +
x*(gpptr(dd)->logusr[1] - gpptr(dd)->logusr[0]));
else
return gpptr(dd)->usr[0] + x*(gpptr(dd)->usr[1] - gpptr(dd)->usr[0]);
}
double yNPCtoUsr(double y, pGEDevDesc dd)
{
if (gpptr(dd)->ylog)
return Rexp10(gpptr(dd)->logusr[2] +
y*(gpptr(dd)->logusr[3]-gpptr(dd)->logusr[2]));
else
return gpptr(dd)->usr[2] + y*(gpptr(dd)->usr[3] - gpptr(dd)->usr[2]);
}
double xDevtoUsr(double x, pGEDevDesc dd)
{
double nfc = xDevtoNFC(x, dd);
if (gpptr(dd)->xlog)
return Rexp10((nfc - gpptr(dd)->win2fig.ax)/gpptr(dd)->win2fig.bx);
else
return (nfc - gpptr(dd)->win2fig.ax)/gpptr(dd)->win2fig.bx;
}
double yDevtoUsr(double y, pGEDevDesc dd)
{
double nfc = yDevtoNFC(y, dd);
if (gpptr(dd)->ylog)
return Rexp10((nfc - gpptr(dd)->win2fig.ay)/gpptr(dd)->win2fig.by);
else
return (nfc - gpptr(dd)->win2fig.ay)/gpptr(dd)->win2fig.by;
}
static double xDevtoMAR1(double x, pGEDevDesc dd)
{
return xDevtoUsr(x, dd);
}
static double yDevtoMAR1(double y, pGEDevDesc dd)
{
return gpptr(dd)->oma[0] + gpptr(dd)->mar[0] - yDevtoLine(y, dd);
}
static double xDevtoyMAR2(double x, pGEDevDesc dd)
{
return gpptr(dd)->oma[1] + gpptr(dd)->mar[1] - xDevtoLine(x, dd);
}
static double yDevtoxMAR2(double y, pGEDevDesc dd)
{
return yDevtoUsr(y, dd);
}
static double xDevtoMAR3(double x, pGEDevDesc dd)
{
return xDevtoUsr(x, dd);
}
static double yDevtoMAR3(double y, pGEDevDesc dd)
{
double line = GConvertYUnits(1.0 - yDevtoNFC(y, dd), NFC, LINES, dd);
return gpptr(dd)->mar[2] - line;
}
static double xDevtoyMAR4(double x, pGEDevDesc dd)
{
double line = GConvertXUnits(1.0 - xDevtoNFC(x, dd), NFC, LINES, dd);
return gpptr(dd)->mar[3] - line;
}
static double yDevtoxMAR4(double y, pGEDevDesc dd)
{
return yDevtoUsr(y, dd);
}
/* the Convert function converts a LOCATION in the FROM coordinate */
/* system to a LOCATION in the TO coordinate system */
void GConvert(double *x, double *y, GUnit from, GUnit to, pGEDevDesc dd)
{
double devx, devy;
switch (from) {
case DEVICE:
devx = *x;
devy = *y;
break;
case NDC:
devx = xNDCtoDev(*x, dd);
devy = yNDCtoDev(*y, dd);
break;
case INCHES:
devx = xInchtoDev(*x, dd);
devy = yInchtoDev(*y, dd);
break;
case OMA1:
devx = xOMA1toDev(*x, dd);
devy = yOMA1toDev(*y, dd);
break;
case OMA2:
devx = yOMA2toxDev(*y, dd);
devy = xOMA2toyDev(*x, dd);
break;
case OMA3:
devx = xOMA3toDev(*x, dd);
devy = yOMA3toDev(*y, dd);
break;
case OMA4:
devx = yOMA4toxDev(*y, dd);
devy = xOMA4toyDev(*x, dd);
break;
case NIC:
devx = xNICtoDev(*x, dd);
devy = yNICtoDev(*y, dd);
break;
case NFC:
devx = xNFCtoDev(*x, dd);
devy = yNFCtoDev(*y, dd);
break;
case MAR1:
devx = xMAR1toDev(*x, dd);
devy = yMAR1toDev(*y, dd);
break;
case MAR2:
devx = yMAR2toxDev(*y, dd);
devy = xMAR2toyDev(*x, dd);
break;
case MAR3:
devx = xMAR3toDev(*x, dd);
devy = yMAR3toDev(*y, dd);
break;
case MAR4:
devx = yMAR4toxDev(*y, dd);
devy = xMAR4toyDev(*x, dd);
break;
case NPC:
devx = xNPCtoDev(*x, dd);
devy = yNPCtoDev(*y, dd);
break;
case USER:
devx = xUsrtoDev(*x, dd);
devy = yUsrtoDev(*y, dd);
break;
default:
devx = 0; /* for -Wall */
devy = 0;
BadUnitsError("GConvert");
}
switch (to) {
case DEVICE:
*x = devx;
*y = devy;
break;
case NDC:
*x = xDevtoNDC(devx, dd);
*y = yDevtoNDC(devy, dd);
break;
case INCHES:
*x = xDevtoInch(devx, dd);
*y = yDevtoInch(devy, dd);
break;
case LINES:
*x = xDevtoLine(devx, dd);
*y = yDevtoLine(devy, dd);
break;
case NIC:
*x = xDevtoNIC(devx, dd);
*y = yDevtoNIC(devy, dd);
break;
case OMA1:
*x = xDevtoOMA1(devx, dd);
*y = yDevtoOMA1(devy, dd);
break;
case OMA2:
*x = yDevtoxOMA2(devy, dd);
*y = xDevtoyOMA2(devx, dd);
break;
case OMA3:
*x = xDevtoOMA3(devx, dd);
*y = yDevtoOMA3(devy, dd);
break;
case OMA4:
*x = yDevtoxOMA4(devy, dd);
*y = xDevtoyOMA4(devx, dd);
break;
case NFC:
*x = xDevtoNFC(devx, dd);
*y = yDevtoNFC(devy, dd);
break;
case NPC:
*x = xDevtoNPC(devx, dd);
*y = yDevtoNPC(devy, dd);
break;
case USER:
*x = xDevtoUsr(devx, dd);
*y = yDevtoUsr(devy, dd);
break;
case MAR1:
*x = xDevtoMAR1(devx, dd);
*y = yDevtoMAR1(devy, dd);
break;
case MAR2:
*x = yDevtoxMAR2(devy, dd);
*y = xDevtoyMAR2(devx, dd);
break;
case MAR3:
*x = xDevtoMAR3(devx, dd);
*y = yDevtoMAR3(devy, dd);
break;
case MAR4:
*x = yDevtoxMAR4(devy, dd);
*y = xDevtoyMAR4(devx, dd);
break;
default:
BadUnitsError("GConvert");
}
}
double GConvertX(double x, GUnit from, GUnit to, pGEDevDesc dd)
{
double devx;
switch (from) {
case DEVICE:devx = x; break;
case NDC: devx = xNDCtoDev(x, dd); break;
case INCHES:devx = xInchtoDev(x, dd); break;
case LINES: devx = xLinetoDev(x, dd); break;
case OMA1: devx = xOMA1toDev(x, dd); break;
/*case OMA2: x <--> y */
case OMA3: devx = xOMA3toDev(x, dd); break;
/*case OMA4: x <--> y */
case NIC: devx = xNICtoDev(x, dd); break;
case NFC: devx = xNFCtoDev(x, dd); break;
case MAR1: devx = xMAR1toDev(x, dd); break;
/*case MAR2: x <--> y */
case MAR3: devx = xMAR3toDev(x, dd); break;
/*case MAR4: x <--> y */
case NPC: devx = xNPCtoDev(x, dd); break;
case USER: devx = xUsrtoDev(x, dd); break;
default: devx = 0;/* for -Wall */ BadUnitsError("GConvertX");
}
switch (to) {
case DEVICE:x = devx; break;
case NDC: x = xDevtoNDC(devx, dd); break;
case INCHES:x = xDevtoInch(devx, dd); break;
case LINES: x = xDevtoLine(devx, dd); break;
case NIC: x = xDevtoNIC(devx, dd); break;
case OMA1: x = xDevtoOMA1(devx, dd); break;
/*case OMA2: x <--> y */
case OMA3: x = xDevtoOMA3(devx, dd); break;
/*case OMA4: x <--> y */
case NFC: x = xDevtoNFC(devx, dd); break;
case USER: x = xDevtoUsr(devx, dd); break;
case MAR1: x = xDevtoMAR1(devx, dd); break;
/*case MAR2: x <--> y */
case MAR3: x = xDevtoMAR3(devx, dd); break;
/*case MAR4: x <--> y */
case NPC: x = xDevtoNPC(devx, dd); break;
default: BadUnitsError("GConvertX");
}
return x;
}
double GConvertY(double y, GUnit from, GUnit to, pGEDevDesc dd)
{
double devy;
switch (from) {
case DEVICE:devy = y; break;
case NDC: devy = yNDCtoDev(y, dd); break;
case INCHES:devy = yInchtoDev(y, dd); break;
case LINES: devy = yLinetoDev(y, dd); break;
case OMA1: devy = yOMA1toDev(y, dd); break;
/*case OMA2: x <--> y */
case OMA3: devy = yOMA3toDev(y, dd); break;
/*case OMA4: x <--> y */
case NIC: devy = yNICtoDev(y, dd); break;
case NFC: devy = yNFCtoDev(y, dd); break;
case MAR1: devy = yMAR1toDev(y, dd); break;
/*case MAR2: x <--> y */
case MAR3: devy = yMAR3toDev(y, dd); break;
/*case MAR4: x <--> y */
case NPC: devy = yNPCtoDev(y, dd); break;
case USER: devy = yUsrtoDev(y, dd); break;
default: devy = 0;/* for -Wall */ BadUnitsError("GConvertY");
}
switch (to) {
case DEVICE:y = devy; break;
case NDC: y = yDevtoNDC(devy, dd); break;
case INCHES:y = yDevtoInch(devy, dd); break;
case LINES: y = yDevtoLine(devy, dd); break;
case NIC: y = yDevtoNIC(devy, dd); break;
case OMA1: y = yDevtoOMA1(devy, dd); break;
/*case OMA2: x <--> y */
case OMA3: y = yDevtoOMA3(devy, dd); break;
/*case OMA4: x <--> y */
case NFC: y = yDevtoNFC(devy, dd); break;
case USER: y = yDevtoUsr(devy, dd); break;
case MAR1: y = yDevtoMAR1(devy, dd); break;
/*case MAR2: x <--> y */
case MAR3: y = yDevtoMAR3(devy, dd); break;
/*case MAR4: x <--> y */
case NPC: y = yDevtoNPC(devy, dd); break;
default: BadUnitsError("GConvertY");
}
return y;
}
/* Code for layouts */
static double sum(double values[], int n, int cmValues[], int cmSum)
{
int i;
double s = 0;
for (i = 0; i < n; i++)
if ((cmSum && cmValues[i]) || (!cmSum && !cmValues[i]))
s = s + values[i];
return s;
}
static double sumWidths(pGEDevDesc dd)
{
return sum(gpptr(dd)->widths, gpptr(dd)->numcols, gpptr(dd)->cmWidths, 0);
}
static double sumCmWidths(pGEDevDesc dd)
{
return sum(gpptr(dd)->widths, gpptr(dd)->numcols, gpptr(dd)->cmWidths, 1);
}
static double sumHeights(pGEDevDesc dd)
{
return sum(gpptr(dd)->heights, gpptr(dd)->numrows, gpptr(dd)->cmHeights, 0);
}
static double sumCmHeights(pGEDevDesc dd)
{
return sum(gpptr(dd)->heights, gpptr(dd)->numrows, gpptr(dd)->cmHeights, 1);
}
static int tallLayout(double cmWidth, double cmHeight, pGEDevDesc dd)
{
return (cmHeight/sumHeights(dd)) > (cmWidth/sumWidths(dd));
}
static void figureExtent(int *minCol, int *maxCol, int *minRow, int *maxRow,
int figureNum, pGEDevDesc dd)
{
int minc = -1;
int maxc = -1;