File: rezone.i

package info (click to toggle)
yorick 1.4-14
  • links: PTS
  • area: main
  • in suites: potato
  • size: 5,948 kB
  • ctags: 6,609
  • sloc: ansic: 63,898; yacc: 889; makefile: 605; sh: 65; lisp: 60; fortran: 19
file content (1570 lines) | stat: -rw-r--r-- 46,978 bytes parent folder | download | duplicates (3)
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
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
/*
   REZONE.I
   Point and click rezoner, written in interpreted Yorick.

   $Id$
 */
/*    Copyright (c) 1994.  The Regents of the University of California.
                    All rights reserved.  */

require, "button.i";
require, "bowtie.i";

func toy_mesh(filename)
/* DOCUMENT toy_mesh, filename
     generates a toy mesh in the file FILENAME in order to be able to
     play with the rezone function.  (FILENAME must be a string enclosed
     in double quotes, of course.)
   SEE ALSO: rezone
 */
{
  rt= span(1., 0., 6)(,-:1:11);
  zt= span(0., 2., 11)(-:1:6,);
  rt(3,3)+= 0.15;
  zt(3,3)+= 0.15;
  rt(3,7)+= 0.05;
  zt(3,7)+= 0.25;
  save, createb(filename), rt, zt;
}

extern rez_load_hook;
/* DOCUMENT rez_load_hook
     If non-nil, filename to be included immediately after rez.i.
     This file can be used to set personalized default values for the
     many parameters.
     Some parameters may be set before loading rez.i, as well
   SEE ALSO: rez_kstyle, rez_lstyle
 */

extern rez_kstyle, rez_lstyle, rez_color;
/* DOCUMENT rez_kstyle, rez_lstyle, rez_color
     is a vector of three longs:  [type,width,color]
     type MUST be 1, 2, 3, 4, or 5,
     width MUST be 1, 2, 3, 4, 5, 6, 7, or 8,
     and color MUST be -1, -2, -3, -4, -5, -6, -7, -8, -9, or -10

     Either or both parameters may be set before or after including
     rez.i; by default:
       rez_kstyle= [4, 1, -2]    (dashdot, thin, foreground color)
       rez_lstyle= [1, 1, -2]    (solid, thin, foreground color)

     To distinguish K and L by color instead of type, try:
       rez_kstyle= [1, 1, -7]    (dashdot, thin, blue)
       rez_lstyle= [1, 1, -5]    (solid, thin, red)
     If the variable rez_color is true (non-nil and non-zero) when
     this file is included, then these will be the default.

   SEE ALSO: rez_load_hook
 */
/* This choice distinguishes by line type.  */
_old_rez_color= rez_color;
if (is_void(rez_kstyle)) rez_kstyle= rez_color? [1, 1, -7]:[4, 1, -2];
if (is_void(rez_lstyle)) rez_lstyle= rez_color? [1, 1, -5]:[1, 1, -2];

extern rez_mark;
/* DOCUMENT rez_mark
     Causes rezone functions to mark bowtied or chevroned (boomeranged)
     zones when the mesh is plotted.  MUST have one of the following values:
       0 to not mark bowtied or chevroned zones
       1 to put a B at the center of the positive triangle of bowtied zones
       2 to put a C at the center of the larger wing of chevroned zones
       3 to mark both bowtied and chevroned zones

     By default, rez_mark is 3; you may set it to a different value
     either before or after including rez.i.

   SEE ALSO: rez_mesh, rez_next, rez_prev, rez_drag
 */
if (is_void(rez_mark)) rez_mark= 3;

_rez_on_black= 0;

extern rez_palette;
/* DOCUMENT rez_palette
     3-by-ncolors array used as a palette when rez_regs region coloring
     is set.  The index in rez_regs is the second index into rez_palette.
     The length 3 first index is [r,g,b].
     The default is 15 pastels.
   SEE ALSO: rez_regs
 */
rez_palette= [[255,230,230],[230,255,230],[230,230,255],[242,242,230],
	      [230,242,242],[242,230,242],[236,249,230],[236,236,242],
	      [249,236,230],[230,236,249],[242,236,236],[230,249,236],
	      [249,230,236],[236,242,236],[236,230,249]];
_old_rez_palette= [];

func rez_chk_palette
{
  if (typeof(rez_palette)!=typeof(_old_rez_palette) ||
      dimsof(rez_palette)(1)!=dimsof(_old_rez_palette)(1) ||
      anyof(dimsof(rez_palette)!=dimsof(_old_rez_palette)) ||
      anyof(rez_palette!=_old_rez_palette)) {
    if (dimsof(rez_palette)(2)!=3)
      error, "rez_palette must be [[r1,g1,b1],[r2,g2,b2],...]";
    if (!is_array(rez_palette) || typeof(rez_palette)=="struct_instance" ||
	typeof(rez_palette)=="string" || typeof(rez_palette)=="complex" ||
	min(rez_palette)<0 || max(rez_palette)>255)
      error, "rez_palette values should be between 0 and 255";
    palette, rez_palette(1,), rez_palette(2,), rez_palette(3,);
    _old_rez_palette= rez_palette;
    fma;
  }
}

extern rez_regs;
/* DOCUMENT rez_regs
     is a vector of region numbers which are to be drawn in the colors
     specified in rez_palette.  Thus:
        rez_regs= [3,5]
     causes regions 3 and 5 to be drawn in colors.  All other regions have
     white backgrounds.  Hilighting too many regions in this way may slow
     down the redraws unacceptably.  To highlight all regions, use:
        rez_regs= indgen(max(rez_ireg))
     This won't work on monochrome monitors, of course.
     The default is rez_regs=[].
   SEE ALSO: rez_palette
 */

extern rez_adjust_mesh;
/* DOCUMENT rez_adjust_mesh
     is an optional hook function which is called after any change to the
     mesh (e.g.- by dragging points) has been made.  If it is defined, it
     may modify the external variables rez_rt and rez_zt.  This hook can
     be used, for example, to force points along some k or l line to lie
     along a symmetry line.
   SEE ALSO: rezone, rez_all, rez_mesh
 */

func rez_plm(flag, adj_check)
{
  if (adj_check && is_func(rez_adjust_mesh)) rez_adjust_mesh;

  fma;

  extern rez_kstyle, rez_lstyle;
  if (rez_color!=_old_rez_color) {
    rez_kstyle= rez_color? [1, 1, -7]:[4, 1, -2];
    rez_lstyle= rez_color? [1, 1, -5]:[1, 1, -2];
    _old_rez_color= rez_color;
  }

  if (!is_void(rez_regs)) {
    rez_chk_palette;
    n= numberof(rez_regs);
    colors= array(char, dimsof(rez_rt)(2), dimsof(rez_rt)(3));
    for (i=1 ; i<=n ; i++) {
      reg= rez_regs(i);
      if (noneof(rez_ireg==reg) || reg<=0) continue;
      colors(where(rez_ireg==reg))= i-1;
      plf, colors, rez_rt, rez_zt, rez_ireg, legend=string(0), region=reg;
    }
  } else if (_rez_on_black) {
    rez_ptexist;
    list= where(rez_ptx);
    r= rez_rt(list);
    z= rez_zt(list);
    pli, [['\0']], min(z), min(r), max(z), max(r);
  }

  plm, rez_rt, rez_zt, rez_ireg, legend=string(0), inhibit=1,
       color= rez_kstyle(3), width= rez_kstyle(2), type= rez_kstyle(1);
  plm, rez_rt, rez_zt, rez_ireg, legend=string(0), inhibit=2,
       color= rez_lstyle(3), width= rez_lstyle(2), type= rez_lstyle(1);

  if (rez_mark&3) {
    extern rez_map;
    local rr, zz;
    if (flag || is_void(flag)) {
      if (_rez_polar) {
	zt= rez_zt*cos(rez_rt);
	rt= rez_zt*sin(rez_rt);
      } else {
	zt= rez_zt;
	rt= rez_rt;
      }
      rez_map= bowtie(rt,zt,rez_ireg);
    }
    for (i=1 ; i<=2 ; i++) {
      if (!(rez_mark&i)) continue;
      rez_getrz, where(rez_map==(i-1));
      if (!is_void(rr)) {
	a1= (rr(2,2,)-rr(2,1,))*(zz(2,1,)-zz(1,1,)) -
	  (zz(2,2,)-zz(2,1,))*(rr(2,1,)-rr(1,1,));
	a2= (rr(1,1,)-rr(1,2,))*(zz(1,2,)-zz(2,2,)) -
	  (zz(1,1,)-zz(1,2,))*(rr(1,2,)-rr(2,2,));
	a3= (rr(2,1,)-rr(1,1,))*(zz(1,1,)-zz(1,2,)) -
	  (zz(2,1,)-zz(1,1,))*(rr(1,1,)-rr(1,2,));
	if (i==1) {
	  /* this is a magic trick for bowtied zones */
	  index= ((a3>=0.) | (a1>=0.)<<1 | (a2>=0.)<<2) - 1;
	  p1= [1,0,3,2](index);
	  p2= [3,1,2,0](index);
	  p3= [2,3,0,1](index);
	  p4= [0,2,1,3](index);
	  index= indgen(1:numberof(rr):4);
	  p1+= index;
	  p2+= index;
	  p3+= index;
	  p4+= index;
	  /* now p1 p2 p3 p4 are the indices of the points in (rr,zz)
	     with p1 p2 being the positive edge of the bowtied zone,
	     so that p1 p3 and p2 p4 cross */
	  r1= rr(p1);
	  z1= zz(p1);
	  r2= rr(p2);
	  z2= zz(p2);
	  r3= rr(p3);
	  z3= zz(p3);
	  r4= rr(p4);
	  z4= zz(p4);
	  fa= (r3-r2)*(z4-z1);
	  fb= -(r4-r1)*(z3-z2);
	  det= fa+fb;
	  fa/= det;
	  fb/= det;
	  rr= r1*fa + r2*fb + (r3-r2)*(r4-r1)*(z2-z1)/det;
	  zz= z2*fa + z1*fb - (z3-z2)*(z4-z1)*(r2-r1)/det;
	  rr= (rr+r1+r2)/3.0;
	  zz= (zz+z1+z2)/3.0;
	  marker= 'B';
	  legend= "B: bowtied zones";
	} else {
	  a4= (rr(1,2,)-rr(2,2,))*(zz(2,2,)-zz(2,1,)) -
	    (zz(1,2,)-zz(2,2,))*(rr(2,2,)-rr(2,1,));
	  r1= (rr(1,1,)+rr(2,1,)+rr(2,2,))/3.0;
	  z1= (zz(1,1,)+zz(2,1,)+zz(2,2,))/3.0;
	  r2= (rr(1,1,)+rr(2,2,)+rr(1,2,))/3.0;
	  z2= (zz(1,1,)+zz(2,2,)+zz(1,2,))/3.0;
	  r3= (rr(2,1,)+rr(1,1,)+rr(1,2,))/3.0;
	  z3= (zz(2,1,)+zz(1,1,)+zz(1,2,))/3.0;
	  r4= (rr(1,2,)+rr(2,2,)+rr(2,1,))/3.0;
	  z4= (zz(1,2,)+zz(2,2,)+zz(2,1,))/3.0;
	  mask= (a1>0. & a2>0.);
	  which= [a1*mask,a2*mask,a3*(1-mask),a4*(1-mask)](,mxx);
	  n= numberof(a1);
	  list= indgen(n)+n*(which-1);
	  rr= [r1,r2,r3,r4](list);
	  zz= [z1,z2,z3,z4](list);
	  marker= 'C';
	  legend= "C: chevroned zones";
	}
	plg,rr,zz,type=0,marker=marker,legend=legend;
      }
    }
  }
}

_rez_polar= 0n;

func rez_toggle
{
  zt= rez_zt;
  rt= rez_rt;
  _rez_polar= !_rez_polar;
  if (_rez_polar) {
    rez_zt= abs(rt,zt);
    rez_rt= atan(rt,zt+1.e-99);
  } else {
    rez_zt= zt*cos(rt);
    rez_rt= zt*sin(rt);
  }
}

/* These four parameters specify the viewport position, which is actually
   a function of the stylesheet being used, and potentially changes as
   a function of window or coordinate system within the window.  The numbers
   here should work decently for the work.gs and boxed.gs styles.  If you
   set them very large, the only side effect is that clicking way outside
   the coordinate system (e.g.- missing the Done or Undo button) will pick
   up the nearest mesh vertex).  This is not that unreasonable a behavior,
   especially since any miscue can be undone.  See rez_load_hook.  */
_rez_xvp=  (0.615+0.175)*0.5;
_rez_dxvp= (0.615-0.175)*0.5;
_rez_yvp=  (0.895+0.455)*0.5;
_rez_dyvp= (0.895-0.455)*0.5;
/* Following setup could be placed in rez_load_hook to allow any style:
  _rez_xvp=  0.5;
  _rez_dxvp= 0.6;
  _rez_yvp=  0.5;
  _rez_dyvp= 0.6;
 */

func rezone(filename, all=, quiet=)
/* DOCUMENT rezone, filename
       -or- rezone

     Invoke the mouse-driven rezoner.  Input is taken from variables
     rt, zt, and ireg in the file FILENAME (which is prompted for if
     omitted).  Output is written to a different file as rt, zt, and
     ireg.  The output file also contains copies rez_rt, rez_zt, and
     rez_ireg in case the mesh under a different name is desirable,
     as well as rez_rt0, rez_zt0, and rez_ireg0, which are the original
     mesh.

     If you click on [Escape], you may continue rezoning where you left
     off by typing   rezone   with no arguments.

     When you are finished, click [OK] to write the output file.

     Use [Zoom or Pan] and [Set Styles] to change the plot limits or
     line colors and styles.  The [Zoom or Pan] screen has buttons which
     "warp" you to each bowtied or chevroned "problem" zone, as well as
     a button to toggle between rectangular and polar coordinates.

     Click [Drag Points] to drag points one at a time with the mouse.

     Click [Adjust Line] to smooth the points on logical line segments.
     You first click on the endpoints P and Q, then click a button to
     smooth or equal space the poionts between P and Q, or drag any
     point between P and Q to establish equal ratio zones ("ER" operation).

     Click [Adjust Region] to smooth the points in logically rectangular
     regions.  You first click on the opposite corners P and R of the
     rectangle PQRS, then click a button to smooth the interior points,
     or click on a point on one of the four edges to transfer the spacing
     on that edge throughout the region PQRS ("EQ" operation).

     The [Undo] (or [Redo]) button removes the effect of the most
     recent drag points, adjust line, or adjust region operation.

     Hints:
     (0) After [Escape], the current mesh is in the variables
         rez_zt, rez_rt, and rez_ireg.  You can change these
	 variables "by hand" before restarting rezone to make
	 algebraic adjustments.
     (1) Switch to polar coordinates during an [Adjust Line] or
         [Adjust Region] in order to get points to lie along a
	 circular arc.  If your center point is not (0,0), you
	 will need to [Escape] and subtract your center point
	 from the rez_zt and rez_rt variables yourself, then
	 [Escape] a second time to restore the proper coordinates
	 before you write the output file.
     (2) You can select a center point as the second point during
         [Adjust Line] or [Adjust Region].  When you do this, the
	 K (or L) value of this second point will be the same as
	 that of the first point -- otherwise there is no way to
	 distinguish among all of the logical points at the center.
	 In an [Adjust Region], the center point will be labeled
	 Q instead of R as usual, and you will need to choose a
	 third point (S) to complete the rectangle PQRS.
     (3) If you set the variable rez_color=1 before you include
         rezone.i, you will get K lines and L lines drawn in different
	 colors instead of the default (which is K lines dashdot and
	 L lines solid).
     (4) If you have a color monitor, you can set rez_regs before
         invoking rezone in order to highlight a list of regions.

     The ALL keyword will be passed to the nbow function, which will
     not be called if the QUIET keyword is non-nil and non-zero.  By
     default, a short summary of bowtied and chevroned zones is printed.

     If you need help on how to use one of the screens, try getting
     help on the appropriate on of these SEE ALSO topics, e.g.-
        help, rez_style
     to get help on the [Set Styles] screen.

   SEE ALSO: rez_all, rez_drag, rez_ladj, rez_radj, rez_zoom, rez_style,
             rez_color, rez_regs, rez_adjust_mesh, toy_mesh
 */
{
  extern rez_name;
  if (is_void(filename)) {
    if (is_void(rez_name)) {
      filename= "";
      read, format="%s", prompt="Name of Z-file: ", filename;
    }
  }
  if (!is_void(filename)) {
    /* begin a new rezone */
    for (i=1 ; i<=2 ; i++) {
      if (!is_void(open(filename,"rb",1))) break;
      write, "Cannot open "+filename;
      read, format="%s", prompt="Name of Z-file: ", filename;
    }
    if (i>2) {
      write, "Quitting rezone";
      return;
    }
    rez_mesh, openb(filename), quiet=quiet, all=all;
    if (!is_void(rez_zt) && !is_void(rez_rt)) rez_name= filename;
    write, "For help, click [Escape], then type:    help, rezone";
  }

  esca= rez_all();
  if (_rez_polar) rez_toggle;
  if (esca) return;

  default= rez_name+"_rez";
  for (i=1 ; i<=3 ; i++) {
    line=
      rdline(prompt="Output filename (default "+default+", ? to escape): ");
    line= strtok(line)(1);
    if (!line) line= default;
    else if (line=="?") line= string(0);
    if (line!=rez_name) break;
  }
  if (!line || line==rez_name) {
    write, "Escaping from rezone -- to resume, type    rezone";
    return;
  }

  zt= rez_zt;
  rt= rez_rt;
  ireg= rez_ireg;
  at_pdb_close|= 1;
  save, createb(line), zt, rt, ireg, rez_zt, rez_rt, rez_ireg,
    rez_zt0, rez_rt0, rez_ireg0;

  rez_name= [];
}

func rez_all(dummy)
/* DOCUMENT rez_all
     Click on [Drag Points] to drag points around with the mouse with
     rez_drag.

     Click on [Adjust Line] to adjust points along a line segment with
     rez_ladj.

     Click on [Adjust Region] to adjust points in a rectangular region
     with rez_radj.

     Click on the [Undo] button to undo the previous change.
     Click on the [Redo] button to redo the previous undo.
     Click on the [Revert] to return to the original mesh.

     When you are finished moving points around, click on [OK].
     Click on [Escape] to exit with a non-zero return value.

     Click on [Zoom or Pan] to change the plot limits (see rez_zoom).

     Click on [Set Styles] to change the line styles or colors (see
     rez_style).

   SEE ALSO: rez_ladj, rez_mesh, rez_zoom, rez_style, rez_adjust_mesh
 */
{
  done= Button(text="OK",x=.203,y=.895,dx=.020,dy=.012);
  undo= Button(text="Undo",x=.254,y=.895,dx=.025,dy=.012);
  esca= Button(text="Escape",x=.344,y=.895,dx=.035,dy=.012);
  zorp= Button(text="Zoom or Pan",x=.464,y=.895,dx=.054,dy=.012);
  sets= Button(text="Set Styles",x=.569,y=.895,dx=.045,dy=.012);
  drag= Button(text="Drag Points",x=.240,y=.375,dx=.054,dy=.012);
  ladj= Button(text="Adjust Line",x=.350,y=.375,dx=.050,dy=.012);
  radj= Button(text="Adjust Region",x=.468,y=.375,dx=.062,dy=.012);
  reve= Button(text="Revert",x=.577,y=.375,dx=.030,dy=.012);

  rez_plm, 1;
  button_plot, done, undo, esca, zorp, sets, drag, ladj, radj, reve;

  zt= zt2= rez_zt;
  rt= rt2= rez_rt;

  for (;;) {
    m= mouse(1, 2, "");
    if (is_void(m)) { rdline,prompt=""; continue; }
    x= m(5);
    y= m(6);
    if (button_test(done,x,y)) break;
    if (button_test(esca,x,y)) {
      write, "Escaping from rezone -- to resume, type    rezone";
      rez_plm,1;
      return 1;
    }
    if (button_test(undo,x,y)) {
      if (undo.text=="Undo") {
	rez_zt= zt;
	rez_rt= rt;
	undo.text= "Redo";
      } else {
	rez_zt= zt2;
	rez_rt= rt2;
	undo.text= "Undo";
      }
      rez_plm, 1;
    } else if (button_test(reve,x,y)) {
      zt= rez_zt;
      rt= rez_rt;
      undo.text= "Undo";
      rez_zt= zt2= rez_zt0;
      rez_rt= rt2= rez_rt0;
      rez_plm, 1;
    } else if (button_test(zorp,x,y)) {
      rez_zoom;
      rez_plm, 0;
    } else if (button_test(sets,x,y)) {
      rez_style;
      rez_plm, 0;
    } else if (button_test(drag,x,y)) {
      zt= rez_zt;
      rt= rez_rt;
      undo.text= "Undo";
      rez_drag;
      zt2= rez_zt;
      rt2= rez_rt;
    } else if (button_test(ladj,x,y)) {
      zt= rez_zt;
      rt= rez_rt;
      undo.text= "Undo";
      rez_ladj;
      zt2= rez_zt;
      rt2= rez_rt;
    } else if (button_test(radj,x,y)) {
      zt= rez_zt;
      rt= rez_rt;
      undo.text= "Undo";
      rez_radj;
      zt2= rez_zt;
      rt2= rez_rt;
    } else {
      rez_ding;
      continue;
    }
    button_plot, done, undo, esca, zorp, sets, drag, ladj, radj, reve;
  }

  rez_plm, 1;
  return 0;
}

func rez_drag
/* DOCUMENT rez_drag
     Allow points in the rezone mesh to be dragged around with
     the mouse.  Press mouse button near a vertex, move mouse
     to desired position of that vertex, and release button.
     If the point where you click is less than twice the distance to the
     second closest point than to the nearest point, the point will not
     be moved (you will hear a beep when you release the button).
     Click on the [Undo] button to undo the previous move; you can undo
     up to 100 previous moves by successive [Undo] clicks.

     When you are finished moving points around, click on [OK].

     Click on [Zoom or Pan] to change the plot limits (see rez_zoom).

     Click on [Set Styles] to change the line styles or colors (see
     rez_style).

   SEE ALSO: rez_ladj, rez_radj, rez_mesh, rez_zoom, rez_style
 */
{
  done= Button(text="OK",x=.203,y=.895,dx=.020,dy=.012);
  undo= Button(text="Undo",x=.254,y=.895,dx=.025,dy=.012);
  lab1= Button(text="Drag points.",x=.344,y=.895,dx=.050,dy=.012,width=-1);
  zorp= Button(text="Zoom or Pan",x=.464,y=.895,dx=.054,dy=.012);
  sets= Button(text="Set Styles",x=.569,y=.895,dx=.045,dy=.012);

  rez_plm, 1;
  button_plot, done, undo, lab1, zorp, sets;

  list= array(0.0, 3, 100);  /* keep last 100 moves for undo */

  kmax= dimsof(rez_rt)(2);
  for (;;) {
    m= mouse(1, 2, "");
    if (is_void(m)) { rdline,prompt=""; continue; }
    flag= j= 0;
    if (button_test(done,m(5),m(6))) break;
    if (button_test(undo,m(5),m(6))) {
      j= long(list(1,1));
      zt= list(2,1);
      rt= list(3,1);
      list(,1:-1)= list(,2:0);
      list(,0)= 0.0;
      flag= 1;
    } else if (button_test(zorp,m(5),m(6))) {
      rez_zoom;
      flag= j= 2;
    } else if (button_test(sets,m(5),m(6))) {
      rez_style;
      flag= j= 2;
    } else if (abs(_rez_xvp-m(5))<=_rez_dxvp ||
	       abs(_rez_yvp-m(6))<=_rez_dyvp) {
      j= rez_find(m(1), m(2));
      if (numberof(_rez_center)) j= 0;
      zt= m(3);
      rt= m(4);
    }
    if (j<1) {
      rez_ding;
    } else {
      if (flag<2) {
	if (!flag) {
	  list(,2:0)= list(,1:-1);
	  list(1,1)= j;
	  list(2,1)= rez_zt(j);
	  list(3,1)= rez_rt(j);
	}
	rez_zt(j)= zt;
	rez_rt(j)= rt;
      }
      rez_plm, (flag<2), 1;
      button_plot, done, undo, lab1, zorp, sets;
      if (!flag) {
	text= swrite(format="moved (%ld,%ld)",(j-1)%kmax+1,(j-1)/kmax+1);
	plt, text, 0.35, 0.375;
      }
    }
  }

  rez_plm, 1;
}

func rez_ladj
/* DOCUMENT rez_ladj
     Adjust the points along a logical line segment PQ:

     Click on [Set P] or [Set Q] to set the endpoints of the segment.
     When P and Q have been set, click [OK]; this button will
     read [Cancel] if P and Q do not have either the same K or the
     same L index.

     Click on [Smooth] to smooth the points along PQ.

     Click on [Equal Space] to make the points along PQ equally spaced.
     This forces PQ to be a straight line.

     Drag any point between P and Q to get equal ratio spacing of all
     the points between P and Q.  This also forces PQ to be a straight
     line; if PQ is initially curved, you may want to click on
     [Equal Space] before you attempt to drag points.

     Click on the [Undo] button to undo the previous adjustment; you can
     undo up to 20 previous adjustments by successive [Undo] clicks.

     When you are finished adjusting lines, click on [OK].

     Click on [Zoom or Pan] to change the plot limits (see rez_zoom).

     Click on [Set Styles] to change the line styles or colors (see
     rez_style).

     Hint:
         You can select a center point as the second point during
         rez_ladj.  When you do this, the K (or L) value of this second
	 point will be the same as that of the first point --
	 otherwise there is no way to distinguish among all of the
	 logical points at the center.

   SEE ALSO: rez_drag, rez_radj, rez_mesh, rez_zoom, rez_style
 */
{
  done= Button(text="OK",x=.203,y=.895,dx=.020,dy=.012);
  undo= Button(text="Undo",x=.254,y=.895,dx=.025,dy=.012);
  lab1= Button(text="Adjust line.",x=.344,y=.895,dx=.050,dy=.012,width=-1);
  zorp= Button(text="Zoom or Pan",x=.464,y=.895,dx=.054,dy=.012);
  sets= Button(text="Set Styles",x=.569,y=.895,dx=.045,dy=.012);

  setp= Button(text="Set P",x=.208,y=.375,dx=.027,dy=.012);
  setq= Button(text="Set Q",x=.268,y=.375,dx=.027,dy=.012);
  drag= Button(text="Drag for ER",x=.355,y=.375,
	       dx=.035,dy=.012,width=-1);
  smth= Button(text="Smooth",x=.454,y=.375,dx=.035,dy=.012);
  eqsp= Button(text="Equal Space",x=.550,y=.375,dx=.054,dy=.012);

  mode= 1;
  jP= jQ= oldjP= oldjQ= 0;
  rez_plm, 1;
  kmax= dimsof(rez_rt)(2);
  lab1.text= "Select P.";
  setp.text= "Set Q";
  done.text= "Cancel";
  done.dx= 0.033;
  button_plot, done, lab1, zorp, sets, setp;

  for (;;) {
    m= mouse(1, 2, "");
    if (is_void(m)) { rdline,prompt=""; continue; }
    x= m(5);
    y= m(6);
    if (button_test(zorp,x,y)) {
      rez_zoom;
    } else if (button_test(sets,x,y)) {
      rez_style;
    } else if (button_test(setp,x,y)) {
      mode= 1 + (setp.text!="Set P");
      if (mode==1) {
	lab1.text= "Select P.";
	setp.text= "Set Q";
      } else {
	lab1.text= "Select Q.";
	setp.text= "Set P";
      }

    } else if (mode) {
      if (button_test(done,x,y)) {
	if (done.text=="Cancel") {
	  if (!oldjP || !oldjQ) break;
	  jP= oldjP;
	  jQ= oldjQ;
	} else {
	  if (kP==kQ) {
	    k= kP;
	    l= rez_range(min(lP,lQ):max(lP,lQ));
	  } else {
	    l= lP;
	    k= rez_range(min(kP,kQ):max(kP,kQ));
	  }
	  zt= rez_zt(k,l);
	  rt= rez_rt(k,l);
	  list= array([zt,rt], 20);  /* keep last 20 for undo */
	}
	lab1.text= "Adjust line.";
	setp.text= "Set P";
	mode= 0;
      } else if (abs(_rez_xvp-x)<=_rez_dxvp ||
	       abs(_rez_yvp-y)<=_rez_dyvp) {
	j= rez_find(m(1), m(2));
	if (numberof(_rez_center)>1) {
	  kc= (_rez_center-1)%kmax + 1;
	  lc= (_rez_center-1)/kmax + 1;
	  if (noneof(kc-kc(1))) kc= kc(1);
	  else if (noneof(lc-lc(1))) lc= lc(1);
	  if (mode==1 && jQ>0) {
	    kd= (jQ-1)%kmax + 1;
	    ld= (jQ-1)/kmax + 1;
	    if (numberof(kc)==1 && kc!=kd && anyof(lc==ld))
	      j= kc + (ld-1)*kmax;
	    else if (numberof(lc)==1 && lc!=ld && anyof(kc==kd))
	      j= kd + (lc-1)*kmax;
	  } else if (mode!=1 && jP>0) {
	    kd= (jP-1)%kmax + 1;
	    ld= (jP-1)/kmax + 1;
	    if (numberof(kc)==1 && kc!=kd && anyof(lc==ld))
	      j= kc + (ld-1)*kmax;
	    else if (numberof(lc)==1 && lc!=ld && anyof(kc==kd))
	      j= kd + (lc-1)*kmax;
	  }
	}
	if (j<1) {
	  rez_ding;
	  continue;
	} else {
	  if (mode==1) jP= j;
	  else jQ= j;
	  kP= (jP-1)%kmax + 1;
	  lP= (jP-1)/kmax + 1;
	  kQ= (jQ-1)%kmax + 1;
	  lQ= (jQ-1)/kmax + 1;
	}
      } else {
	rez_ding;
	continue;
      }

    } else {
      if (button_test(done,x,y)) break;
      if (button_test(undo,x,y)) {
	rez_zt(k,l)= list(,1,1);
	rez_rt(k,l)= list(,2,1);
	list(,,1:-1)= list(,,2:0);
      } else if (button_test(setq,x,y)) {
	oldjP= jP;
	oldjQ= jQ;
	lab1.text= "Select Q.";
	mode= 2;
      } else if (button_test(smth,x,y)) {
	zt= rez_zt(k,l);
	if (numberof(zt)>2) {
	  rt= rez_rt(k,l);
	  list(,,2:0)= list(,,1:-1);
	  list(,1,1)= zt;
	  list(,2,1)= rt;
	  zt(2:-1)= zt(zcen)(zcen);
	  rt(2:-1)= rt(zcen)(zcen);
	  rez_zt(k,l)= zt;
	  rez_rt(k,l)= rt;
	}
      } else if (button_test(eqsp,x,y)) {
	zt= rez_zt(k,l);
	n= numberof(zt);
	if (n>2) {
	  rt= rez_rt(k,l);
	  list(,,2:0)= list(,,1:-1);
	  list(,1,1)= zt;
	  list(,2,1)= rt;
	  rez_zt(k,l)= span(zt(1), zt(0), n);
	  rez_rt(k,l)= span(rt(1), rt(0), n);
	}
      } else if (abs(_rez_xvp-x)<=_rez_dxvp ||
	       abs(_rez_yvp-y)<=_rez_dyvp) {
	j= rez_find(m(1), m(2));
	if (j<=min(jP,jQ) || j>=max(jP,jQ) ||
	    (kP==kQ && abs(j-jP)%kmax)) {
	  rez_ding;
	  continue;
	} else {
	  zP= rez_zt(jP);
	  rP= rez_rt(jP);
	  zQ= rez_zt(jQ);
	  rQ= rez_rt(jQ);
	  zx= m(3)-zP;
	  rx= m(4)-rP;
	  za= zQ-zP;
	  ra= rQ-rP;
	  /* set sx to projection of new point onto PQ, normalized to
	     lie between 0 (at P) and 1 (at Q) */
	  sx= (za*zx+ra*rx)/abs(za,ra,1.e-35)^2;
	  if (sx>0. && sx<1.) {
	    zt= rez_zt(k,l);
	    rt= rez_rt(k,l);
	    list(,,2:0)= list(,,1:-1);
	    list(,1,1)= zt;
	    list(,2,1)= rt;
	    n= numberof(zt)-1;  /* already guaranteed >1 */
	    if (kP==kQ) m= abs((j-1)/kmax + 1 - lP);
	    else m= abs((j-1)%kmax + 1 - kP);
	    m= 1./m;
	    r= rez_solve(1./sx, n*m)^m;
	    sa= spanl(1., r^(n-1), n)(cum);
	    sx= [0.0, sa(0)];
	    zt= interp([zP,zQ], sx, sa);
	    rt= interp([rP,rQ], sx, sa);
	    if (kP>kQ || lP>lQ) {
	      zt= zt(0:1:-1);
	      rt= rt(0:1:-1);
	    }
	    rez_zt(k,l)= zt;
	    rez_rt(k,l)= rt;
	  } else {
	    rez_ding;
	    continue;
	  }
	}
      } else {
	rez_ding;
	continue;
      }
    }
    rez_plm, !mode, 1;
    if (!mode || (jP && jQ && ((kQ==kP) ~ (lQ==lP)))) {
      done.text= "OK";
      done.dx= 0.020;
    } else {
      done.text= "Cancel";
      done.dx= 0.033;
    }
    button_plot, done, lab1, zorp, sets, setp;
    if (jP) plg, rez_rt(kP,lP), rez_zt(kP,lP),
      marker='P', type=0, legend=string(0);
    if (jQ) plg, rez_rt(kQ,lQ), rez_zt(kQ,lQ),
      marker='Q', type=0, legend=string(0);
    if (!mode) button_plot, undo, setq, drag, smth, eqsp;
    else if (jP || jQ) {
      text= jP? swrite(format="P=(%ld,%ld)",kP,lP) : "";
      if (jQ) text+= swrite(format="    Q=(%ld,%ld)",kQ,lQ);
      plt, text, 0.35, 0.375;
    }
  }

  rez_plm, 1;
}

func rez_radj
/* DOCUMENT rez_radj
     Adjust the points in a logical rectangle PQRS:

     Click on [Set P] or [Set R] to set the endpoints of the segment.
     When P and R have been set, click [OK]; this button will
     read [Cancel] if P and R have the same K or the same L index.

     Click on a point on any edge of PQRS (but not one of the corners)
     to copy the spacings on that edge throughout the entire region.
     All of the lines of the same type (K-line or L-line) as the edge
     you clicked on will become straight lines.

     Click on [Smooth] to smooth the points interior to PQRS.
     Click on [Smooth 4] to perform the smooth operation four times.

     Click on the [Undo] button to undo the previous adjustment; you can
     undo up to 10 previous adjustments by successive [Undo] clicks.

     When you are finished adjusting regions, click on [OK].

     Click on [Zoom or Pan] to change the plot limits (see rez_zoom).

     Click on [Set Styles] to change the line styles or colors (see
     rez_style).

     Hint:
         You can select a center point as the second point during
         rez_radj.  When you do this, the K (or L) value of this second
	 point will be the same as that of the first point --
	 otherwise there is no way to distinguish among all of the
	 logical points at the center.  The center point will be labeled
	 Q instead of R as usual, and you will need to choose a third
	 point (S) to complete the rectangle PQRS.

   SEE ALSO: rez_drag, rez_ladj, rez_mesh, rez_zoom, rez_style
 */
{
  done= Button(text="OK",x=.203,y=.895,dx=.020,dy=.012);
  undo= Button(text="Undo",x=.254,y=.895,dx=.025,dy=.012);
  lab1= Button(text="Adjust region.",x=.344,y=.895,dx=.050,dy=.012,width=-1);
  zorp= Button(text="Zoom or Pan",x=.464,y=.895,dx=.054,dy=.012);
  sets= Button(text="Set Styles",x=.569,y=.895,dx=.045,dy=.012);

  setp= Button(text="Set P",x=.208,y=.375,dx=.027,dy=.012);
  setr= Button(text="Set R",x=.268,y=.375,dx=.027,dy=.012);
  drag= Button(text="Click edge for EQ",x=.371,y=.375,
	       dx=.035,dy=.012,width=-1);
  smth= Button(text="Smooth",x=.480,y=.375,dx=.035,dy=.012);
  eqsp= Button(text="Smooth 4",x=.563,y=.375,dx=.041,dy=.012);

  mode= 1;
  jP= jR= jQ= oldjP= oldjR= 0;
  rez_plm, 1;
  kmax= dimsof(rez_rt)(2);
  lab1.text= "Select P.";
  setp.text= "Set R";
  done.text= "Cancel";
  done.dx= 0.033;
  button_plot, done, lab1, zorp, sets, setp;

  for (;;) {
    m= mouse(1, 2, "");
    if (is_void(m)) { rdline,prompt=""; continue; }
    x= m(5);
    y= m(6);
    if (button_test(zorp,x,y)) {
      rez_zoom;
    } else if (button_test(sets,x,y)) {
      rez_style;
    } else if (button_test(setp,x,y)) {
      mode= 1 + (setp.text!="Set P");
      if (mode==1) {
	lab1.text= "Select P.";
	setp.text= "Set R";
      } else {
	lab1.text= "Select R.";
	setp.text= "Set P";
      }
      jQ= 0;

    } else if (mode) {
      if (button_test(done,x,y)) {
	if (done.text=="Cancel") {
	  if (!oldjP || !oldjR) break;
	  jP= oldjP;
	  jR= oldjR;
	} else {
	  k0= min(kP,kR);
	  k1= max(kP,kR);
	  l0= min(lP,lR);
	  l1= max(lP,lR);
	  zt= rez_zt(k0:k1,l0:l1);
	  rt= rez_rt(k0:k1,l0:l1);
	  list= array([zt,rt], 10);  /* keep last 10 for undo */
	}
	lab1.text= "Adjust region.";
	setp.text= "Set P";
	mode= jQ= 0;
      } else if (abs(_rez_xvp-x)<=_rez_dxvp ||
		 abs(_rez_yvp-y)<=_rez_dyvp) {
	j= rez_find(m(1), m(2));
	mold= mode;
	if (numberof(_rez_center)>1) {
	  if (mode==2 && jP>0) {
	    kc= (_rez_center-1)%kmax + 1;
	    lc= (_rez_center-1)/kmax + 1;
	    if (noneof(kc-kc(1))) kc= kc(1);
	    else if (noneof(lc-lc(1))) lc= lc(1);
	    kd= (jP-1)%kmax + 1;
	    ld= (jP-1)/kmax + 1;
	    if (numberof(kc)==1 && kc!=kd && anyof(lc==ld))
	      j= kc + (ld-1)*kmax;
	    else if (numberof(lc)==1 && lc!=ld && anyof(kc==kd))
	      j= kd + (lc-1)*kmax;
	    lab1.text= "Select S.";
	    mode= 3;
	  } else {
	    j= 0;
	  }
	}
	if (j<1) {
	  rez_ding;
	  continue;
	} else {
	  if (mode==1) jP= j;
	  else if (mode==2) jR= j;
	  else if (mode!=mold) jQ= j;
	  else if (kP==kQ) jR= (j-1)%kmax + 1 + (lQ-1)*kmax;
	  else jR= kQ + ((j-1)/kmax)*kmax;
	  kP= (jP-1)%kmax + 1;
	  lP= (jP-1)/kmax + 1;
	  kR= (jR-1)%kmax + 1;
	  lR= (jR-1)/kmax + 1;
	  kQ= (jQ-1)%kmax + 1;
	  lQ= (jQ-1)/kmax + 1;
	}
      } else {
	rez_ding;
	continue;
      }

    } else {
      if (button_test(done,x,y)) break;
      if (button_test(undo,x,y)) {
	rez_zt(k0:k1,l0:l1)= list(,,1,1);
	rez_rt(k0:k1,l0:l1)= list(,,2,1);
	list(,,,1:-1)= list(,,,2:0);
      } else if (button_test(setr,x,y)) {
	oldjP= jP;
	oldjR= jR;
	lab1.text= "Select R.";
	mode= 2;
      } else if (button_test(smth,x,y)) {
	zt= rez_zt(k0:k1,l0:l1);
	if (dimsof(zt)(2)>2 && dimsof(zt)(3)>2) {
	  rt= rez_rt(k0:k1,l0:l1);
	  list(,,,2:0)= list(,,,1:-1);
	  list(,,1,1)= zt;
	  list(,,2,1)= rt;
	  zt(2:-1,2:-1)= zt(zcen,zcen)(zcen,zcen);
	  rt(2:-1,2:-1)= rt(zcen,zcen)(zcen,zcen);
	  rez_zt(k0:k1,l0:l1)= zt;
	  rez_rt(k0:k1,l0:l1)= rt;
	}
      } else if (button_test(eqsp,x,y)) {
	zt= rez_zt(k0:k1,l0:l1);
	if (dimsof(zt)(2)>2 && dimsof(zt)(3)>2) {
	  rt= rez_rt(k0:k1,l0:l1);
	  list(,,,2:0)= list(,,,1:-1);
	  list(,,1,1)= zt;
	  list(,,2,1)= rt;
	  for (ii=0 ; ii<4 ; ii++) {
	    zt(2:-1,2:-1)= zt(zcen,zcen)(zcen,zcen);
	    rt(2:-1,2:-1)= rt(zcen,zcen)(zcen,zcen);
	  }
	  rez_zt(k0:k1,l0:l1)= zt;
	  rez_rt(k0:k1,l0:l1)= rt;
	}
      } else if (abs(_rez_xvp-x)<=_rez_dxvp ||
		 abs(_rez_yvp-y)<=_rez_dyvp) {
	j= rez_find(m(1), m(2));
	kj= (j-1)%kmax + 1;
	lj= (j-1)/kmax + 1;
	inl= (lj>l0 && lj<l1);
	ink= (kj>k0 && kj<k1);
	zt= rez_zt(k0:k1,l0:l1);
	rt= rez_rt(k0:k1,l0:l1);
	if (j>0 && (kj==k0 || kj==k1) && inl) {
	  list(,,,2:0)= list(,,,1:-1);
	  list(,,1,1)= zt;
	  list(,,2,1)= rt;
	  ds= abs(zt(kj==k0,dif),rt(kj==k0,dif))(-,cum);
	  ds/= ds(1,0);
	  rez_zt(k0:k1,l0:l1)= zt(,1) + (zt(,0)-zt(,1))*ds
	  rez_rt(k0:k1,l0:l1)= rt(,1) + (rt(,0)-rt(,1))*ds
	} else if (j>0 && (lj==l0 || lj==l1) && ink) {
	  list(,,,2:0)= list(,,,1:-1);
	  list(,,1,1)= zt;
	  list(,,2,1)= rt;
	  ds= abs(zt(dif,lj==l0),rt(dif,lj==l0))(cum);
	  ds/= ds(0);
	  rez_zt(k0:k1,l0:l1)= zt(1,-,) + (zt(0,-,)-zt(1,-,))*ds
	  rez_rt(k0:k1,l0:l1)= rt(1,-,) + (rt(0,-,)-rt(1,-,))*ds
	} else {
	  rez_ding;
	  continue;
	}
      } else {
	rez_ding;
	continue;
      }
    }
    rez_plm, !mode, 1;
    if (!mode || (jP && jR && kR!=kP && lR!=lP)) {
      done.text= "OK";
      done.dx= 0.020;
    } else {
      done.text= "Cancel";
      done.dx= 0.033;
    }
    button_plot, done, lab1, zorp, sets, setp;
    if (jP) plg, rez_rt(kP,lP), rez_zt(kP,lP),
      marker='P', type=0, legend=string(0);
    if (jR) plg, rez_rt(kR,lR), rez_zt(kR,lR),
      marker='R', type=0, legend=string(0);
    if (jQ) plg, rez_rt(kQ,lQ), rez_zt(kQ,lQ),
      marker='Q', type=0, legend=string(0);
    if (done.text=="OK") {
      if (!jQ) plg, rez_rt(kP,lR), rez_zt(kP,lR),
                 marker='Q', type=0, legend=string(0);
      plg, rez_rt(kR,lP), rez_zt(kR,lP),
        marker='S', type=0, legend=string(0);
    }
    if (!mode) button_plot, undo, setr, drag, smth, eqsp;
    else if (jP || jR) {
      text= jP? swrite(format="P=(%ld,%ld)",kP,lP) : "";
      if (jR) text+= swrite(format="    R=(%ld,%ld)",kR,lR);
      else if (jQ) text+= swrite(format="    Q=(%ld,%ld)",kQ,lQ);
      plt, text, 0.35, 0.375;
    }
  }

  rez_plm, 1;
}

_rez_zoom= 0n;

func rez_zoom
/* DOCUMENT rez_zoom
     Change limits after setting rez_mesh.

     There are two modes of operation:
     (1) P-mode:
         Click on a point to magnify or demagnify about that point.
	 Drag the point to move it to a different position in the
	 viewport.  The left button zooms in (magnifies by 1.5), the
	 right button zooms out (demagnifies by 1.5), and the middle
	 button pans (no magnification change).  If you click just
	 below or just above the tick marks, only the x coordinate
	 is changed; clicking just to the left or just to the right
	 of the viewport changes only the y coordinate.
     (2) R-mode:
         Left drag out a rectangle to set the limits to that rectangle.
	 Right drag out a rectangle to cram the entire current viewport
	 into that rectangle (an analog to zooming out).
	 Middle drag to pan as in P-mode (the rubber band rectangle is
	 meaningless in this case).

      Click the [R-mode] (or [P-mode]) button to change modes.

      Click the [OK] button when you are satisfied with the limits.

      Click the [Undo] button to remove the effect of the previous
      zoom-click.  Up to 100 successive undo operations are allowed.

      Click the [Polar] button to toggle polar coordinates.  The
      vertical axis will be theta, and the horizontal axis radius.
      The polar coordinate origin will be at (zt,rt) = (0,0).

      Click the [Next], [Prev], or [First] buttons to focus in on
      zones which are bowtied or chevroned.  These zones are marked
      by a B or a C, respectively (if rez_mark is set).

   SEE ALSO: rez_mesh
 */
{
  done= Button(text="OK",x=.203,y=.895,dx=.020,dy=.012);
  undo= Button(text="Undo",x=.254,y=.895,dx=.025,dy=.012);
  lab2= Button(text="Left=in  Middle=pan  Right=out",
	       x=.413,y=.895,dx=.128,dy=.012,width=-1);
  mode= Button(text=(_rez_zoom?"P-Mode":"R-Mode"),
	       x=.584,y=.895,dx=.037,dy=.012);

  full= Button(text="Full Mesh",x=.230,y=.375,dx=.046,dy=.012);
  polar= Button(text="Polar",x=.307,y=.375,dx=.025,dy=.012);
  lab3= Button(text="Go To Bowtie:",
	       x=.402,y=.375,dx=.040,dy=.012,width=-1);
  next= Button(text="Next",x=.486,y=.375,dx=.025,dy=.012);
  prev= Button(text="Prev",x=.536,y=.375,dx=.025,dy=.012);
  first= Button(text="First",x=.587,y=.375,dx=.026,dy=.012);

  rez_plm, 1;
  button_plot, done, undo, lab2, mode, full, polar, lab3, next, prev, first;

  list= array(0.0, 5, 100);  /* keep last 100 limits for undo */
  len= 0;
  for (;;) {
    m= mouse(1, _rez_zoom, "");
    if (is_void(m)) { rdline,prompt=""; continue; }
    x= m(5);
    y= m(6);
    if (button_test(done,x,y)) break;
    if (button_test(undo,x,y)) {
      if (len) {
	limits, list(,1);
	list(,1:-1)= list(,2:0);
	len--;
      } else {
	rez_ding;
	continue;
      }
    } else if (button_test(mode,x,y)) {
      _rez_zoom= !_rez_zoom;
      mode.text= (_rez_zoom?"P-Mode":"R-Mode");
    } else if (button_test(full,x,y)) {
      limits;  redraw;
      continue;
    } else if (button_test(polar,x,y)) {
      rez_toggle;
      limits;
    } else if (button_test(next,x,y)) {
      rez_next;  redraw;
      continue;
    } else if (button_test(prev,x,y)) {
      rez_prev;  redraw;
      continue;
    } else if (button_test(first,x,y)) {
      rez_next, 1;  redraw;
      continue;
    } else if (abs(_rez_xvp-x)<=_rez_dxvp ||
	       abs(_rez_yvp-y)<=_rez_dyvp) {
      x= m(1);
      y= m(2);
      x1= m(3);
      y1= m(4);
      button= long(m(10));
      lims= limits();
      if (!_rez_zoom || button==2) {  /* point mode */
	if (button==1) f= 1.5;
	else if (button==2) f= 1.0;
	else f= 1.0/1.5;
	x0= x - (x1-lims(1))/f;
	x1= x - (x1-lims(2))/f;
	y0= y - (y1-lims(3))/f;
	y1= y - (y1-lims(4))/f;
	if (x!=lims(1) && x!=lims(2)) {
	  if (y!=lims(3) && y!=lims(4)) limits, x0,x1,y0,y1;
	  else limits, x0,x1;
	} else {
	  if (y!=lims(3) && y!=lims(4)) limits, ,,y0,y1;
	  else limits, x0,x1,y0,y1;
	}
	list(,2:0)= list(,1:-1);
	list(,1)= lims;
	len++;
      } else if (x!=x1 && y!=y1) {  /* rectangle mode */
	x0= min(x,x1);
	x1= max(x,x1);
	y0= min(y,y1);
	y1= max(y,y1);
	if (button!=1) {
	  f= (lims(2)-lims(1))/(x1-x0);
	  x0= lims(1) - f*(x0-lims(1));
	  x1= lims(2) + f*(lims(2)-x1);
	  f= (lims(4)-lims(3))/(y1-y0);
	  y0= lims(3) - f*(y0-lims(3));
	  y1= lims(4) + f*(lims(4)-y1);
	}
	limits, x0,x1,y0,y1;
	list(,2:0)= list(,1:-1);
	list(,1)= lims;
	len++;
      } else {
	rez_ding;
	continue;
      }
    }
    rez_plm, 0;
    button_plot, done, undo, lab2, mode, full, polar, lab3, next, prev, first;
  }
}

func rez_style
{
  done= Button(text="OK",x=.203,y=.895,dx=.020,dy=.012);
  klin= Button(text="K-line",x=.290,y=.895,dx=.038,dy=.012,width=6.);
  llin= Button(text="L-line",x=.366,y=.895,dx=.038,dy=.012);
  type= Button(text="Type",x=.476,y=.895,dx=.025,dy=.012,width=6.);
  wdth= Button(text="Width",x=.530,y=.895,dx=.029,dy=.012);
  colr= Button(text="Color",x=.586,y=.895,dx=.027,dy=.012);

  bar1= ["1", "2", "3", "4", "5", "6", "7", "8"];
  bar2= ["bg", "fg", "blk", "wht",
	 "red", "grn", "blu", "cyn", "mag", "yel"];
  bar= array(Button, 10);
  bar.dx= bardx= .022;
  bar.x= barx= .644;
  bar.dy= bardy= .012;
  bar.y= .895-(indgen(10)-1)*2.*bardy;
  bary= avg(bar.y);

  bar(1:5).text= bar1(1:5);
  barn= 5;
  bar(rez_kstyle(1)).width= 6.;
  change= 1;

  for (;;) {
    if (change) {
      i= where(bar.width)(1);
      if (type.width) {
	if (klin.width) rez_kstyle(1)= i;
	else rez_lstyle(1)= i;
      } else if (wdth.width) {
	if (klin.width) rez_kstyle(2)= i;
	else rez_lstyle(2)= i;
      } else if (colr.width) {
	if (klin.width) rez_kstyle(3)= -i;
	else rez_lstyle(3)= -i;
      }
      rez_plm, 0;
      button_plot, done, klin, llin, type, wdth, colr, bar(1:barn);
      change= 0;
    }
    m= mouse(0, _rez_zoom, "");
    if (is_void(m)) { rdline,prompt=""; continue; }
    x= m(5);
    y= m(6);
    if (button_test(done,x,y)) break;
    if (button_test(klin,x,y)) {
      if (klin.width) continue;
      klin.width= 6.;
      llin.width= 0.;
      bar.width= 0.;
      if (type.width) bar(rez_kstyle(1)).width= 6.;
      else if (wdth.width) bar(rez_kstyle(2)).width= 6.;
      else if (colr.width) bar(-rez_kstyle(3)).width= 6.;
    } else if (button_test(llin,x,y)) {
      if (llin.width) continue;
      llin.width= 6.;
      klin.width= 0.;
      bar.width= 0.;
      if (type.width) bar(rez_lstyle(1)).width= 6.;
      else if (wdth.width) bar(rez_lstyle(2)).width= 6.;
      else if (colr.width) bar(-rez_lstyle(3)).width= 6.;
    } else if (button_test(type,x,y)) {
      if (type.width) continue;
      type.width= 6.;
      wdth.width= colr.width= 0.;
      bar(1:5).text= bar1(1:5);
      barn= 5;
      bar.width= 0.;
      if (klin.width) bar(rez_kstyle(1)).width= 6.;
      else bar(rez_lstyle(1)).width= 6.;
    } else if (button_test(wdth,x,y)) {
      if (wdth.width) continue;
      wdth.width= 6.;
      type.width= colr.width= 0.;
      bar(1:8).text= bar1;
      barn= 8;
      bar.width= 0.;
      if (klin.width) bar(rez_kstyle(2)).width= 6.;
      else bar(rez_lstyle(2)).width= 6.;
    } else if (button_test(colr,x,y)) {
      if (colr.width) continue;
      colr.width= 6.;
      type.width= wdth.width= 0.;
      bar.text= bar2;
      barn= 10;
      bar.width= 0.;
      if (klin.width) bar(-rez_kstyle(3)).width= 6.;
      else bar(-rez_lstyle(3)).width= 6.;
    } else if (abs(barx-x)<bardx && abs(bary-y)<2.*barn*bardy) {
      i= long((bar(1).y+bardy - y)/(2.0*bardy)) + 1;
      if (i>0 && i<=barn) {
	bar.width= 0.;
	bar(i).width= 6.;
      } else {
	rez_ding;
	continue;
      }
    } else {
      rez_ding;
      continue;
    }
    change= 1;
  }
}

extern rez_qrt, rez_qzt, rez_qireg;
/* DOCUMENT rez_qrt, rez_qzt, rez_qireg
     are the names of the mesh variables for rez_mesh, "rt", "zt", and
     "ireg" by default.  Note that ireg is optional.
 */
rez_qzt= "zt";
rez_qrt= "rt";
rez_qireg= "ireg";

func rez_mesh(rt, zt, ireg, all=, quiet=)
/* DOCUMENT rez_mesh, rt, zt, ireg
       -or- rez_mesh, rt, zt
       -or- rez_mesh, zfile
     set mesh for rez_... commands.  If ZFILE is specified, it must be a
     binary file containing variables RT, ZT, and (optionally) IREG.
   SEE ALSO: rez_all, rez_adjust_mesh
 */
{
  extern rez_rt0, rez_zt0, rez_ireg0, rez_rt, rez_zt, rez_ireg;
  extern rez_mapi;

  if (is_stream(rt)) {
    f= rt;
    vars= get_vars(f);
    ireg= rez_getvar(f,vars, rez_qireg);
    zt= rez_getvar(f,vars, rez_qzt);
    rt= rez_getvar(f,vars, rez_qrt);
  }
  if (is_void(ireg)) {
    ireg= array(1, dimsof(rt));
    ireg(1,)= 0;
    ireg(,1)= 0;
  }

  rez_rt0= rez_rt= rt;
  rez_zt0= rez_zt= zt;
  rez_ireg0= rez_ireg= ireg;
  if (is_void(zt) || is_void(rt)) return;
  rez_ptexist;

  rez_mapi= 0;
  if (!quiet) nbow, rez_rt, rez_zt, rez_ireg, all=all;

  rez_plm, 1;
}

func rez_next(which)
/* DOCUMENT rez_next
     zoom in on next bowtied (or chevroned) zone (after rez_mesh).
     With numeric argument, zooms to that bowtie (or chevron).  With
     numeric argument 0, zooms to current bowtie (or chevron).
   SEE ALSO: rez_prev, rez_mark, rez_drag
 */
{
  list= where(rez_map==0);
  n= where(rez_map==1);
  if (!numberof(list)) list= [];
  if (!numberof(n)) n= [];
  list= grow(list,n);
  n= numberof(list);
  if (is_void(which)) which= rez_mapi+1;
  else if (!which) which= rez_mapi? rez_mapi : 1;
  else if (which<0) which= rez_mapi>1? rez_mapi-1 : n+1;
  if (!n || which>n) { rez_ding; return; }
  local rr, zz;
  rez_getrz, list(which);
  rn= min(rr);  rx= max(rr);
  zn= min(zz);  zx= max(zz);
  rr= rx-rn;
  zz= zx-zn;
  zn-= 0.3*zz;
  zx+= 0.3*zz;
  rn-= 0.3*rr;
  rx+= 0.3*rr;
  limits, zn, zx, rn, rx;
  rez_mapi= which;
}

func rez_prev
/* DOCUMENT rez_prev
     zoom in on previous bowtied (or chevroned) zone (after rez_mesh).
   SEE ALSO: rez_next, rez_mark, rez_drag
 */
{
  rez_next, -1;
}

func rez_getrz(zlist)
{
  extern rr, zz;   /* 2-by-2-by-dimsof(zlist) result */
  if (!numberof(zlist)) {
    rr= zz= [];
    return;
  }
  dims= dimsof(rez_rt);
  kmax= dims(2);
  klist= (zlist-1)%(kmax-1);
  llist= (zlist-1)/(kmax-1);
  list= llist*kmax + klist + 1;  /* lower left corner index */
  rr= zz= array(0.0, 2, 2, dimsof(list));
  rr(1,1,..)= rez_rt(list);
  zz(1,1,..)= rez_zt(list);
  rr(2,1,..)= rez_rt(list+1);
  zz(2,1,..)= rez_zt(list+1);
  rr(1,2,..)= rez_rt(list+kmax);
  zz(1,2,..)= rez_zt(list+kmax);
  rr(2,2,..)= rez_rt(list+kmax+1);
  zz(2,2,..)= rez_zt(list+kmax+1);
}

func rez_ptexist
{
  extern rez_ptx, rez_ptlist, rez_ireg;
  rez_ireg(1,)= 0;
  rez_ireg(,1)= 0;
  d= dimsof(rez_ireg);
  ix= d(2);
  jx= d(3);
  n= numberof(rez_ireg);
  ireg= array(0, n+ix+1);
  ireg(1:n)= rez_ireg(*);
  rez_ptx= array(0, dimsof(rez_ireg));
  rez_ptx(*)=
    (ireg(1:n) | ireg(2:n+1) | ireg(ix+1:n+ix) | ireg(ix+2:n+ix+1));
  rez_ptlist= where(rez_ptx);
}

extern _rez_center;

func rez_find(x, y)
{
  extern _rez_center;
  d= abs(rez_zt-x, rez_rt-y)(rez_ptlist);
  j= d(mnx);
  d0= d(j);
  j0= rez_ptlist(j);
  j= where(!(d-d0));  /* detect center points */
  if (numberof(j)>1) _rez_center= rez_ptlist(j);
  else _rez_center= [];
  return j0;
}

func rez_getvar(f, vars, name)
{
  v= vars(2);
  if (v && anyof(*v == name)) return get_member(f,name);
  v= vars(1);
  if (v && anyof(*v == name)) return get_member(f,name);
  return [];
}

func rez_range(x) { return x; }  /* parser quirk */

func rez_ding
{
  write, format="%s", "\a\a";
}

func rez_solve(ss, nn)
/* xxDOCUMENT adapted from gseries_r(s, n) in series.i
     returns the ratio r of the finite geometric series, given the sum s:
        1 + r + r^2 + r^3 + ... + r^(nn-1) = ss,
	or (r^nn-1)/(r-1) = ss

     Altered to solve only nn>0.0, but allows any real nn.
 */
{
  /* compute an approximate result which has exact values and
     derivatives at s==1, s==n, and s->infinity --
     different approximations apply for s>n and s<n */
  if (nn==2) return ss-1.0;
  if (ss>nn) {
    pow= 1.0/(nn-1.0);
    npow= nn^pow - 1.0;
    n2r= 1.0/(nn-2.0);
    A= (2.0-nn*npow)*n2r;
    B= (2.0*npow-nn*pow)*nn*n2r;
    r= ss^pow - pow + A*(nn/ss)^pow + B/ss;
  } else {
    sn= (ss-1.0)/(nn-1.0);
    n2r= 1.0/(nn*nn);
    r= 1.0 - 1.0/ss + n2r*sn*sn*(nn+1.0 - sn);
  }

  /* Polish the approximation using Newton-Raphson iterations.  */
  if (ss!=nn) {
    for (;;) {
      rr= r-1.0;
      rn= r^(nn-1);
      rrss= rr*ss;
      delta= rrss - (r*rn-1.0);
      if (abs(delta)<=1.e-9*abs(rrss)) break;
      r+= delta/(nn*rn-ss);
    }
    /* try to get it to machine precision */
    if (delta) r+= delta/(nn*rn-ss);
  }

  return r;
}

func rez_test
{
  extern rez_rt, rez_zt;
  x= span(0,1,10)(,-:1:7);  y= span(0,.5,7)(-:1:10,);
  rez_mesh, x+y, x-y+.5;
}

if (!is_void(rez_load_hook)) include, rez_load_hook;