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 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771
|
/* cp_io.c = input and output routines for circle packing */
#include "cp_head.h"
#include <xview/seln.h>
#include <xview/alert.h>
#include <xview/notice.h>
#define SHMODE 0666
#define MAXCMD 1000
static char *shmBlock=NULL;
static int *lockFlg=NULL;
static int *newFlag=NULL;
static int *vertNum=NULL;
static char *cmdStr=NULL;
extern void textsw_possibly_normalize(),textsw_set_selection();
extern Menu print_menu;
int dum_int;
FILE *fp,*sysfp;
int readcall(struct p_data *p,char *filename)
/* Open file ptr; assumed that overwrite has already been approved
if p is nonempty; filename has complete path included. */
{
int i;
char packname[NAME_MAX];
if ( (strlen(filename)>NAME_MAX) || ((fp=fopen(filename,"r"))==NULL) )
goto FAILED;
i=strlen(filename);
while (i>0 && filename[i-1]!='/') i--;
strcpy(packname,filename+i); /* cut off any directory names */
if (!call_read_pack(fp,p,packname) ) goto FAILED;
fclose(fp);
return 1;
FAILED:
sprintf(msgbuf,"Read of %s has failed; check name or "
"path in Config window.",filename);
emsg();
if (fp) fclose(fp);
return 0;
} /* readcall */
int call_read_pack(FILE *fp,struct p_data *p,char *packname)
/* assume fp open, call read_pack, update pack info. Return from
read_pack has bits encoding what info was read:
Use this to know what to update.
1: 0001 basic combinatorics (new pack)
2: 0002 aims
3: 0004 inv_dist
4: 0010 radii
5: 0020 centers
6: 0040 angle sums
7: 0100 vertex_map
8: 0200 lists of verts/faces/edges
9: 0400 colors (circle or face) */
{
int i,val,pnum=pack_num(p);
double halfwidth;
struct K_data *pK_ptr;
struct R_data *pR_ptr;
if (!(val=readpack(fp,p)))
{
sprintf(msgbuf,"Read for pack %d has failed.",pnum);
emsg();
return 0;
}
pK_ptr=p->packK_ptr;
pR_ptr=p->packR_ptr;
/* depending on bit coding in val, update pack info */
if (val & 0001) /* combinatoric info (ie, it's a new pack)
note: complex_count was done during reading. */
{
sprintf(msgbuf,"Read into p%d succeeded; %d circles.",
pnum,p->nodecount);
msg();
choose_alpha(p);choose_beta(p);choose_gamma(p);
facedraworder(p,0);
p->active_node=p->alpha;
if (!(val & 0010)) /* no radii read */
for (i=1;i<=p->nodecount;i++) pR_ptr[i].rad=0.5;
/* radii read but not centers */
if ((val & 0010) && !(val & 0020))
comp_pack_centers(p,0,0,2,okerr);
if (!(val & 0040)) /* no angle sums read */
fillcurves(p);
/* fix up pack name and window label */
if ((val & 0001) && p->file_name[0]=='\0')
/* no pack name read, use call */
strcpy(p->file_name,packname);
sprintf(buf,"%s",p->file_name);
if (p->hes<0) strcat(buf," (hyp)\0");
else if (p->hes>0) strcat(buf," (sph)\0");
else strcat(buf," (eucl)\0");
pmsg(pack_num(p),buf);
/* reset screen data */
p->screen->cflag_opt=1;
p->screen->fflag_opt=0;
p->screen->box=std_real_box;
halfwidth=(p->screen->box.ry-p->screen->box.ly)
*p->screen->pix_box.rx/p->screen->pix_box.ry/2.0;
p->screen->box.lx=(-1)*halfwidth;
p->screen->box.rx=halfwidth;
/* must fix so it fits current canvas pixel size */
if (p->hes>=0)
{
p->screen->unitcircle=0;
set_u_cir_flag(pnum,FALSE);
}
else
{
p->screen->unitcircle=1;
set_u_cir_flag(pnum,TRUE);
}
set_cursor(p->screen,0);
live_node[pnum]=live_face[pnum]=0;
}
else /* partial data only, not a new pack */
{
sprintf(msgbuf,"Read of data for p%d succeeded.",pnum);
msg();
}
return 1;
} /* call_read_pack */
int writecall(struct p_data *p,char *filename,int act)
/* calls routine for writing file from pack p. Assumed that
overwrite has already been approved if filename already exists.
filename includes full path.*/
{
if (p->status && open_and_write(p,filename,act))
{
write_consolidate(p,filename);
return 1;
}
sprintf(msgbuf,"Write from %d has failed.",pack_num(p));
emsg();
return 0;
} /* writecall */
int write_consolidate(struct p_data *p,char *filename)
/* Update after successful write */
{
int n,pnum=pack_num(p);
char *name;
sprintf(buf,"\nSENT pack %d to disc as %s. ",pnum,filename);
n=strlen(filename);
while (filename[n-1]!='/' && n>0) n--;
name=filename+n; /* cut off any directory names */
strcpy(p->file_name,name); /* update file_name */
sprintf(buf,"%s",p->file_name);
if (p->hes<0) strcat(buf," (hyp)\0");
else if (p->hes==0) strcat(buf," (eucl)\0");
else strcat(buf," (sph)\0");
pmsg(pnum,buf);canvmsg(pnum);
return 1;
} /* write_consolidate */
int open_and_write(struct p_data *p,char *filename,int act)
/* open input file, call write, return 1 if successful */
{
int i;
if ( (strlen(filename)>NAME_MAX) || ((fp=fopen(filename,"w"))==NULL) )
return 0;
i=writepack(fp,p,act,0);
fclose(fp);
return i;
} /* open_and_write */
int read_path_file(char *filename)
/* read a closed path from a file. */
{
int count;
if (!stat_file(filename))
{
sprintf(msgbuf,"The file `%s' cannot be found",filename);
emsg();
return 0;
}
if ((fp=fopen(filename,"r"))!=NULL)
{
count=readpath(fp,&pathlist,&pathlength);
fclose(fp);
return count;
}
else return 0;
} /* read_path_file */
int write_path_file(char *filename)
/* write a closed path to a file.*/
{
int count;
if (pathlist!=NULL && (fp=fopen(filename,"w"))!=NULL
&& strlen(filename)<=NAME_MAX)
{
count=writepath(fp,pathlist);
fclose(fp);
return count;
}
else return 0;
} /* write_path_file */
int copy_packcall(char *datastr)
/* copy p1 into p2; */
{
int p1,p2,width,height;
double midpt,halfwidth;
Pixmap xpm_hold;
XID xid_hold;
if ( sscanf(datastr,"%d %d",&p1,&p2) ==2 && p1>=0 && p2!=p1
&& p1<NUM_PACKS && p2>=0 && p2<NUM_PACKS &&
packdata[p1].status && !packdata[p2].locks
&& copy_pack(&packdata[p1],&packdata[p2]) )
{
/* save pointers */
packdata[p2].screen=&screendata[p2];
xid_hold=screendata[p2].xid;
xpm_hold=screendata[p2].xpm;
screendata[p2]=screendata[p1];
screendata[p2].xid=xid_hold;
screendata[p2].xpm=xpm_hold;
/* move screen parameters */
if (!cmd_mode) screendata[p2].pix_box.rx=width=
(int)xv_get(canvas_frame[p2],XV_WIDTH);
if (!cmd_mode) screendata[p2].pix_box.ry=height=
(int)xv_get(canvas_frame[p2],XV_HEIGHT);
midpt=(screendata[p2].box.lx+screendata[p2].box.rx)/2.0;
halfwidth=(screendata[p2].box.ry-screendata[p2].box.ly)
*width/height/2.0;
screendata[p2].box.lx=midpt-halfwidth;
screendata[p2].box.rx=midpt+halfwidth;
set_cursor(&screendata[p2],0);
sprintf(buf,"%s",packdata[p2].file_name);
if (packdata[p2].hes<0) strcat(buf," (hyp)\0");
else if (packdata[p2].hes==0) strcat(buf," (eucl)\0");
else strcat(buf," (sph)\0");
pmsg(p2,buf);
set_pack_labels();
return 1;
}
return 0;
} /* copy_packcall */
int seed_start_call(struct p_data *p,char *datastr)
/* bookkeeping */
{
int draw_flag=1,n,pnum=pack_num(p);
double halfwidth;
stripsp(datastr);
if (!strncmp(datastr,"-q",2)) /* -q flag for quiet, don't draw */
{
draw_flag=0;
datastr[0]=datastr[1]=' ';
stripsp(datastr);
}
if (!sscanf(datastr,"%d",&n) || n<3 || n>MAX_PETALS) n=6;
if (!seed_pack(p,-1,n)) return 0; /* default geom is hyp */
/* set screen stuff */
p->screen->box=std_real_box;
halfwidth=(p->screen->box.ry-p->screen->box.ly)
*p->screen->pix_box.rx/p->screen->pix_box.ry/2.0;
p->screen->box.lx=(-1)*halfwidth;
p->screen->box.rx=halfwidth;
p->screen->cflag_opt=1;
p->screen->fflag_opt=0;
p->screen->unitcircle=1;
set_cursor(p->screen,0);
live_node[pnum]=live_face[pnum]=0;
sprintf(buf,"Seed pack");
strcpy(p->file_name,buf);
sprintf(buf,"no header");
strcpy(p->fhead,buf);
set_u_cir_flag(pnum,TRUE);
sprintf(buf,"Seed pack (hyp)");
pmsg(pnum,buf);canvmsg(pnum);
/* repack/fix/draw */
sprintf(buf,"repack -p%d",pnum);
handle_cmd(buf,¤t_p);
sprintf(buf,"fix -p%d -c",pnum);
handle_cmd(buf,¤t_p);
if (draw_flag)
{
sprintf(buf,"disp -p%d -w -c",pnum);
handle_cmd(buf,¤t_p);
}
return 1;
} /* seed_start_call */
int empty_pack(struct p_data *p)
/* reset pack, default size =5000. */
{
int pnum=pack_num(p);
alloc_pack_space(p,5000,0);
sprintf(packlabels[pnum],"Pack %d: empty ",pnum);
strcpy(p->file_name,"None\0");
set_pack_labels();
return 1;
} /* empty_pack */
int confirm_action(char *message)
/* alert package for confirming a read or write */
{
int result;
Event event;
result=notice_prompt(base_frame,&event,
NOTICE_MESSAGE_STRINGS,message,0,
NOTICE_BUTTON_YES,"confirm",
NOTICE_BUTTON_NO,"Cancel",
0);
switch (result)
{
case NOTICE_YES: return 1;
case NOTICE_NO: return 0;
case NOTICE_FAILED:
{
strcpy(msgbuf,"Alert failed for some reason");
emsg();
return 0;
}
}
return 1;
} /* confirm_action */
int print_inq_data(struct p_data *p,int f,char *datastr)
/* f is code for desired action. Comes either from menu
choice or from cmd line; see cp_format/cp_action or cp_cmd. */
{
int i,j,v,count=0,n1,n2,vv[3],length=0;
int num,pnum=pack_num(p),hits,incompat_flag,*genlist;
int max_ideg,min_ideg,max_bdeg,min_bdeg,digits=1;
double dum,b_dist,nc,dumf,duml,tf,tl,tc;
double b_vert_dist,datum,rad1,rad2,inv_d,act_inv_d,diff;
double over_err,ic_err;
/* double accum_ic_err=0.0,ic_mix_max=0.0,ic_large_max=0.0,
ic_small_max=0.0, worst_as_err=0.0,accum_as_err=0.0;*/
complex ctr,e_cent;
char buff[256],buff2[256],*endptr,format_buf[256];
struct s_data *scr;
struct Vertlist *vertlist,*trace;
struct Edgelist *edgelist,*track;
struct p_data *p1,*p2;
struct K_data *pK_ptr;
struct R_data *pR_ptr;
pK_ptr=p->packK_ptr;pR_ptr=p->packR_ptr;
while ((pow(10,(double)digits))*toler < 0.1
&& digits<MAX_ACCUR) digits++; /* for output accuracy */
if (f==30) /* status of remote programs */
{
j=0;
for (i=0;i<NUM_PROC;i++)
if (remote[i].pid)
{
j++;
sprintf(msgbuf,"Remote[%d] is %s, moniker = %s.",
i,remote[i].program,remote[i].moniker);
msg();
}
if (!j)
{
sprintf(msgbuf,"No remote processes active.");
msg();
}
return 1;
}
if (f==21) /* program parameters */
{
sprintf(msgbuf,"Param: Packing: accuracy=%e "
"(okerr=%e);\n Packing algorithm cycles=%d; "
"iterations=%d; \n Misc: post_size=%d; brush=%d;"
"act pack=%d; act node=%d",
toler,okerr, totalpasses,iterates,sc_print_size,
line_thick,pnum,p->active_node);
msg();
/* give remote routine status */
for (i=0;i<NUM_PROC;i++)
if (remote[i].pid)
{
sprintf(msgbuf," Remote routine %d active: "
"pid=%d, program is '%s', moniker = %s.",
i,remote[i].pid,remote[i].program,remote[i].moniker);
msg();
}
return 1;
}
if (f==33) /* script loaded? */
{
buff[0]='\0';
if (!cmd_mode)
{
if (textsw_append_file_name(script_sw,buff))
sprintf(msgbuf,"No script file loaded.");
else sprintf(msgbuf,"Script filename: %s.",buff);
msg();
return 1;
}
if (!strlen(script_name))
sprintf(msgbuf,"No script file loaded.");
else sprintf(msgbuf,"Script filename: %s.",script_name);
msg();
return 1;
}
if (!p->status)
{
sprintf(msgbuf,"Pack %d is empty.",pnum);
emsg();
return 0;
}
scr=&screendata[pnum];
switch (f)
{
case 1: /* pack data */
{
if (sscanf(datastr,"%d",&pnum)!=1 || pnum<0 || pnum>=NUM_PACKS
|| !packdata[pnum].status)
pnum=current_p;
p1=&packdata[pnum];
max_bdeg=0;
for (v=1;v<=p1->nodecount;v++)
if (p->packK_ptr[v].bdry_flag && (p->packK_ptr[v].num+1)>max_bdeg)
max_bdeg=p->packK_ptr[v].num+1;
min_bdeg=max_bdeg;
for (v=1;v<=p1->nodecount;v++)
if (p->packK_ptr[v].bdry_flag && (p->packK_ptr[v].num+1)<min_bdeg)
min_bdeg=p->packK_ptr[v].num+1;
min_ideg=max_ideg=p->packK_ptr[p->alpha].num;
for (v=1;v<=p1->nodecount;v++)
if (!p->packK_ptr[v].bdry_flag)
{
i=p->packK_ptr[v].num;
if (i>max_ideg) max_ideg=i;
if (i<min_ideg) min_ideg=i;
}
for (i=1;p1->edge_pair[i].edge;i++)
length=i;
i=0;
for (j=1;j<=length;j++) if (p1->edge_pair[j].mate) i++;
sprintf(msgbuf,"PACK %d: nodecount=%d; (alp/gam)=(%d/%d); "
"face#1=%d; red#1=%d\n Euler=%d; "
"genus=%d; bdry segs: tot/paired=%d/%d; "
"%d bdry_comp(s);\n",
pnum,p1->nodecount,p1->alpha,p1->gamma,
p1->first_face,p1->first_red_face,
p1->euler,p1->genus,length,i,
p1->num_bdry_comp);
if (packdata[pnum].num_bdry_comp>0)
{
sprintf(msgbuf+strlen(msgbuf)," bdry_start(s): ");
for (i=1;i<=packdata[pnum].num_bdry_comp;i++)
sprintf(msgbuf+strlen(msgbuf),"%d ",
packdata[pnum].bdry_starts[i]);
sprintf(msgbuf+strlen(msgbuf),"\n");
}
if (p->hes==0)
{
dum=0.0;
for (i=1;i<=p->facecount;i++)
dum += e_area(pR_ptr[p->faces[i].vert[0]].rad,
pR_ptr[p->faces[i].vert[1]].rad,
pR_ptr[p->faces[i].vert[2]].rad);
sprintf(msgbuf+strlen(msgbuf)," eucl area=%f; ",dum);
}
sprintf(msgbuf+strlen(msgbuf)," deg(max/min): int=(%d/%d), "
"bdry=(%d/%d).",
max_ideg,min_ideg,max_bdeg,min_bdeg);
msg();
return 1;
}
case 2: /* vertex data */
{
if ( (vertlist=node_link_parse(p,datastr,&endptr,&hits)) != NULL)
{
trace=vertlist;
do
{
v=trace->v;
sprintf(msgbuf,"\nData on vert# %d, p%d: rad=% .5e, "
"ang sum=% .5ePi, aim=% .5ePi, bdry?=%d, "
"#star=%d, plot_flag? %d.",
v,pnum,radius(p,v),
p->packR_ptr[v].curv/M_PI,p->packR_ptr[v].aim/M_PI,
p->packK_ptr[v].bdry_flag,p->packK_ptr[v].num,
p->packK_ptr[v].plot_flag);
msg();
count++;
trace=trace->next;
} while (trace!=NULL && count < 5);
vert_free(&vertlist); /* free any excess */
return count;
}
else v=p->active_node;
sprintf(msgbuf,"\nData on vert# %d, p%d: rad=% .5e, "
"ang sum=% .5ePi, aim=% .5ePi, bdry?=%d, "
"#star=%d, plot_flag? %d.",
v,pnum,radius(p,v),
p->packR_ptr[v].curv/M_PI,p->packR_ptr[v].aim/M_PI,
p->packK_ptr[v].bdry_flag,p->packK_ptr[v].num,
p->packK_ptr[v].plot_flag);
msg();
return 1;
}
case 7: /* flower */
case 8: /* selected radii */
case 9: /* selected angle sums */
case 10: /* selected aims */
case 15: /* selected model curvatures */
{
if ( (vertlist=node_link_parse(p,datastr,&endptr,&hits)) != NULL)
{
if (f==7) sprintf(msgbuf,"Flowers:");
else if (f==8) sprintf(msgbuf,"Radii:");
else if (f==9) sprintf(msgbuf,"Angle sums/Pi:");
else if (f==10) sprintf(msgbuf,"Aims/Pi:");
else if (f==15) sprintf(msgbuf,"Kappa:");
trace=vertlist;
do
{
if (f==8) datum=radius(p,trace->v);
else if (f==9) datum=p->packR_ptr[trace->v].curv/M_PI;
else if (f==10) datum=p->packR_ptr[trace->v].aim/M_PI;
else if (f==15)
{
kappa_calc(p,trace->v,&datum);
datum=datum*(fabs(datum));
}
else if (f==7)
{
sprintf(buff,"\np%d,v%d,deg=%d,flower: ",
pnum,trace->v,
p->packK_ptr[trace->v].num);
strcat(msgbuf,buff);
for (i=0;i<=p->packK_ptr[trace->v].num;i++)
{
sprintf(buff,"%d ",
p->packK_ptr[trace->v].flower[i]);
strcat(msgbuf,buff);
}
*buff='\0';
}
if (f!=7) sprintf(buff,"%d % .5e; ",trace->v,datum);
strcat(msgbuf,buff);
count++;
trace=trace->next;
} while (trace!=NULL && count < 5);
vert_free(&vertlist); /* free any excess */
msg();
}
return count;
}
case 16: /* antipodal point */
{
endptr=datastr;
if (!(v=grab_one_vert(p,&endptr))) v=p->alpha;
for (i=1;i<=p->nodecount;i++) p->packK_ptr[i].util_flag=0;
p->packK_ptr[v].util_flag=1;
if (!(genlist=label_generations(p,-1,&j,&i)))
return 0;
free(genlist);
sprintf(msgbuf,"Antipodal (max comb distance) to %d is "
"vertex %d.",v,j);
msg();
return 1;
}
case 11: /* data on selected faces */
{
if ( (vertlist=face_link_parse(p,datastr,&endptr,&hits)) != NULL)
{
trace=vertlist;
do
{
incompat_flag=0.0;
for (j=0;j<3;j++) vv[j]=p->faces[trace->v].vert[j];
sprintf(msgbuf,"face %d data, p%d: verts (%d,%d,%d), "
"plot v%d, plot_flag? %d.\n",
trace->v,pnum,vv[0],vv[1],vv[2],
vv[p->faces[trace->v].index_flag],
p->faces[trace->v].plot_flag);
msg();
sprintf(msgbuf," Rad: (%f,%f,%f).\n Angles: (%f,%f,%f).\n",
radius(p,vv[0]),radius(p,vv[1]),radius(p,vv[2]),
comp_single_angle(p,vv[0],vv[1],vv[2],&incompat_flag),
comp_single_angle(p,vv[1],vv[2],vv[0],&incompat_flag),
comp_single_angle(p,vv[2],vv[0],vv[1],&incompat_flag));
msg();
if (incompat_flag)
{
sprintf(msgbuf," NOTE: radii/overlaps incompatible.\n");
emsg();
}
sprintf(msgbuf," Overlaps (0->1->2->0): assigned=");
if (p->overlap_status)
sprintf(buff,"%f,%f,%f\n errors "
"(assigned-actual)=%f,%f,%f.\n",
pK_ptr[vv[0]].overlaps[nghb(p,vv[0],vv[1])],
pK_ptr[vv[1]].overlaps[nghb(p,vv[1],vv[2])],
pK_ptr[vv[2]].overlaps[nghb(p,vv[2],vv[0])],
invdist_err(p,vv[0],vv[1]),
invdist_err(p,vv[1],vv[2]),
invdist_err(p,vv[2],vv[0]));
else sprintf(buff,"tangency\n errors "
"(assigned-actual)=%f,%f,%f.\n",
invdist_err(p,vv[0],vv[1]),
invdist_err(p,vv[1],vv[2]),
invdist_err(p,vv[2],vv[0]));
strcat(msgbuf,buff);
msg();
count++;
trace=trace->next;
} while (trace!=NULL && count < 5);
vert_free(&vertlist); /* free any excess */
}
return count;
}
case 12: /* overlaps */
{
sprintf(msgbuf,"Overlaps, p%d:",pnum);
if (!p->overlap_status)
{
strcat(msgbuf," All set to 0.0, (default=tangency case).");
msg();
return count;
}
if ((edgelist=node_pair_link(p,datastr,&endptr,&hits))!=NULL)
{
track=edgelist;
do {
inv_d=pK_ptr[track->v].
overlaps[nghb(p,track->v,track->w)];
if (inv_d>=0 && inv_d<=1) /* want overlap */
{
act_inv_d=comp_inv_dist(p,track->v,track->w);
if (act_inv_d<=1)
sprintf(buff," (%d,%d): aim=cos(% .5e Pi); "
"actual=cos(% .5e Pi).\n",
track->v,track->w,
acos(inv_d)/M_PI,acos(act_inv_d)/M_PI);
else
sprintf(buff," (%d,%d): aim=cos(% .5e Pi); "
"actual=*% .5e.\n",
track->v,track->w,
acos(inv_d)/M_PI,act_inv_d);
strcat(msgbuf,buff);
count++;
}
else if (inv_d>1) /* want separated */
{
act_inv_d=comp_inv_dist(p,track->v,track->w);
if (act_inv_d>1)
sprintf(buff," (%d,%d): aim=*% .5e; "
"actual=*% .5e.\n",
track->v,track->w,inv_d,act_inv_d);
else
sprintf(buff," (%d,%d): aim=*% .5e; "
"actual=cos(% .5e Pi).\n",
track->v,track->w,inv_d,acos(act_inv_d)/M_PI);
strcat(msgbuf,buff);
count++;
}
else /* assigned overlaps exceeding Pi/2 */
{
act_inv_d=comp_inv_dist(p,track->v,track->w);
if (act_inv_d>1)
sprintf(buff," (%d,%d): aim=cos(% .5e Pi) "
"(NOTE: > Pi/2); actual=*% .5e.\n",
track->v,track->w,acos(inv_d)/M_PI,act_inv_d);
else
sprintf(buff," (%d,%d): aim=cos(% .5e Pi) "
"(NOTE: > Pi/2); actual=cos(% .5e Pi).\n",
track->v,track->w,
acos(inv_d)/M_PI,acos(act_inv_d)/M_PI);
strcat(msgbuf,buff);
count++;
}
track=track->next;
} while (track!=NULL);
edge_free(&edgelist);
}
msg();
return count;
}
case 14: /* altn_rad alternative radii: eucl, hyp. */
{
if ( (vertlist=node_link_parse(p,datastr,&endptr,&hits)) != NULL)
{
if (p->hes<0) sprintf(msgbuf,"Radii, p%d (v eucl, hyp):",
pnum);
else if (p->hes==0) sprintf(msgbuf,"Eucl:");
else sprintf(msgbuf,"Sph:");
trace=vertlist;
do {
if (p->hes<0)
h_to_e_data(p->packR_ptr[trace->v].center,
p->packR_ptr[trace->v].rad,&e_cent,&datum);
else datum=p->packR_ptr[trace->v].rad;
sprintf(buff," %d % .5e, ",trace->v,datum);
strcat(msgbuf,buff);
if (p->hes<0)
{
datum=p->packR_ptr[trace->v].rad;
if (datum<=0) sprintf(buff,"inf ");
else {
datum=(-log(datum));
sprintf(buff,"% .5e ",datum);}
strcat(msgbuf,buff);
}
count++;
trace=trace->next;
} while (trace!=NULL && count < 5);
vert_free(&vertlist); /* free any excess */
msg();
}
return count;
}
case 13: /* selected centers */
{
sprintf(msgbuf,"Centers: ");
if ( (vertlist=node_link_parse(p,datastr,&endptr,&hits)) != NULL)
{
trace=vertlist;
do {
ctr=p->packR_ptr[trace->v].center;
sprintf(buff,"%d (% .12e % .12e),\n |.|=% .12e ; ",
trace->v,ctr.re,ctr.im,cAbs(ctr));
strcat(msgbuf,buff);
count++;
trace=trace->next;
} while (trace!=NULL && count < 3);
vert_free(&vertlist); /* free any excess */
msg();
}
return count;
}
case 20: /* ?screendata */
{
sprintf(msgbuf,
"Screen %d: lower left=(%d, %d), upper right=(%d,%d). ",
pnum,(int)(scr->box.lx),(int)(scr->box.ly),
(int)(scr->box.rx),(int)(scr->box.ry));
msg();
return 1;
}
case 25: /* ?pk_status: curvature/overlap errors */
{
/* Collect data, set vertex marks with codes:
-1 = not subject to change
0 = packed within tolerance
1 = incompat, too small in some faces
2 = incompat, too large in some faces
3 = incompat, mixed (too large in some faces, too small in others)
*/
double accum_ic_err=0.0,ic_mix_max=0.0,ic_large_max=0.0;
double ic_small_max=0.0,worst_as_err=0.0,accum_as_err=0.0;
int ic_small_vert=0,ic_large_vert=0,ic_mix_vert=0;
int free_count=0,worst_as_vert=0;
for (i=1;i<=p->nodecount;i++)
{
if (p->packR_ptr[i].aim<0) p->packK_ptr[i].mark=-1;
else
{
free_count++;
if (p->hes<0)
h_as_overlap(p,i,p->packR_ptr[i].rad,
&p->packR_ptr[i].curv,&p->packK_ptr[i].mark,&ic_err);
if (!p->packK_ptr[i].mark)
{
diff=fabs(p->packR_ptr[i].curv-p->packR_ptr[i].aim);
if (diff > worst_as_err)
{
worst_as_err=diff;
worst_as_vert=i;
}
accum_as_err += diff;
}
else if (p->packK_ptr[i].mark==1)
{
if (ic_err>ic_small_max)
{
ic_small_max=ic_err;
ic_small_vert=i;
}
accum_ic_err += ic_err;
}
else if (p->packK_ptr[i].mark==2)
{
if (ic_err>ic_large_max)
{
ic_large_max=ic_err;
ic_large_vert=i;
}
accum_ic_err += ic_err;
}
else if (p->packK_ptr[i].mark==3)
{
if (ic_err>ic_mix_max)
{
ic_mix_max=ic_err;
ic_mix_vert=i;
}
accum_ic_err += ic_err;
}
}
}
over_err=0.0;
if (p->overlap_status)
for (i=1;i<p->nodecount;i++)
for (j=0;j<p->packK_ptr[i].num;j++)
{
if (i<(v=p->packK_ptr[i].flower[j]))
{
diff=fabs(p->packK_ptr[i].overlaps[j]-
comp_inv_dist(p,i,v));
if (diff > okerr && diff > over_err)
{n1=i;n2=v;over_err=diff;}
/* worst, so far */
}
}
/* printing results */
sprintf(msgbuf,"Pack status, p%d: %d verts subject to change.",
pack_num(p),free_count);
msg();
sprintf(format_buf," %% .%de = accum ang sum err; "
"worst = %% .%de at v%%d.",digits,digits);
sprintf(msgbuf,format_buf,accum_as_err,worst_as_err,worst_as_vert);
msg();
if (p->overlap_status && accum_ic_err > 0.0)
{
sprintf(msgbuf," INCOMPATIBILITIES have occurred; verts "
"marked:\n 1 = too small,2 = too large, 3 = mixed.");
msg();
window_bell(msg_sw);
sprintf(msgbuf," %f = accum incompatibility err",accum_ic_err);
if (ic_small_vert)
{
sprintf(buff,"\n worst too small = %f at v%d",
ic_small_max,ic_small_vert);
strcat(msgbuf,buff);
}
if (ic_large_vert)
{
sprintf(buff,"\n worst too large = %f at v%d",
ic_large_max,ic_large_vert);
strcat(msgbuf,buff);
}
if (ic_mix_vert)
{
sprintf(buff,"\n worst mixed = %f at v%d.",
ic_mix_max,ic_mix_vert);
strcat(msgbuf,buff);
}
msg();
if (over_err>0.0)
{
sprintf(msgbuf," OVERLAP ERRORS: worst is %f, edge (%d,%d).",
over_err,n1,n2);
msg();
window_bell(msg_sw);
}
else
{
sprintf(msgbuf," Overlaps within tolerances.");
msg();
}
}
return 1;
}
case 26: /* distance to bdry */
{
h_to_e_data(p->packR_ptr[p->alpha].center,
p->packR_ptr[p->alpha].rad,&ctr,&dum);
bdry_dist(p,ctr,&b_dist,&b_vert_dist);
sprintf(msgbuf,"Euclidean distance from alpha to the "
"boundary polygon is % .5e.",b_dist);
msg();
return 1;
}
case 27: /* edge_pairing. Give mobius transforms */
{
n1=0;
if (p->edge_pair[2].edge)
{
sprintf(msgbuf,"Edge pairings for p%d:\n",pnum);
msg();
for (i=1;p->edge_pair[i].edge;i++)
{
if (p->edge_pair[i].mate)
{
for (j=1;j<i;j++)
if (p->edge_pair[j].edge==p->edge_pair[i].mate
&& p->edge_pair[j].edge_indx==p->edge_pair[i].mate_indx)
{
n1=j;
j=i;
}
if (!n1) /* found mate, print mobius */
{
sprintf(msgbuf," Pairing %d <--> %d as 2x2 matrix:\n",
i,n1);
msg();
sprintf(msgbuf," [ %f + %f I, %f + %f I;\n",
p->edge_pair[i].mob.a.re,p->edge_pair[i].mob.a.im,
p->edge_pair[i].mob.b.re,p->edge_pair[i].mob.b.im);
msg();
sprintf(msgbuf," %f + %f I, %f + %f I ]\n",
p->edge_pair[i].mob.c.re,p->edge_pair[i].mob.c.im,
p->edge_pair[i].mob.d.re,p->edge_pair[i].mob.d.im);
msg();
}
}
}
}
else
{
sprintf(msgbuf,"No edge pairing for p%d.",pnum);
msg();
}
return 1;
}
case 28: /* ratio_ftn <p,q>. Gives (eucl radius of alpha vert of q)
divided by (eucl rad of alpha vert of p) */
{
if (sscanf(datastr,"%d %d",&n1,&n2)!=2
|| n1< 0 || n1>=NUM_PACKS || !packdata[n1].status
|| n2< 0 || n2>=NUM_PACKS || !packdata[n2].status)
{sprintf(msgbuf,"Specify two active packs.");emsg();return 0;}
p1=&packdata[n1];p2=&packdata[n2];
if (p1->hes>0 || p2->hes>0)
{sprintf(msgbuf,"One of packs is spherical.");emsg();return 0;}
if (p1->hes<0) h_to_e_data(p1->packR_ptr[p1->alpha].center,
p1->packR_ptr[p1->alpha].rad,&e_cent,&rad1);
else rad1=p1->packR_ptr[p1->alpha].rad;
if (p2->hes<0) h_to_e_data(p2->packR_ptr[p2->alpha].center,
p2->packR_ptr[p2->alpha].rad,&e_cent,&rad2);
else rad2=p2->packR_ptr[p2->alpha].rad;
sprintf(msgbuf,"Radii comparisons (for alpha vert):\n "
"Eucl: ratio p%d/p%d = % .5e; "
"difference p%d-p%d = % .5e.",
n2,n1,rad2/rad1,n2,n1,rad2-rad1);
msg();
if (p->hes<0)
{
rad2=-log(p2->packR_ptr[p2->alpha].rad);
rad1=-log(p1->packR_ptr[p1->alpha].rad);
sprintf(msgbuf," Hyp: ratio p%d/p%d = % .5e; "
"difference p%d-p%d = % .5e.",
n2,n1,rad2/rad1,n2,n1,rad2-rad1);
msg();
}
return 1;
}
case 29: /* give conductances, transition probabilities for nodes. */
{
if (p->hes>=0) return 0; /* Only for hyp case at this time. */
if ( (vertlist=node_link_parse(p,datastr,&endptr,&hits)) != NULL)
{
trace=vertlist;
do {
v=trace->v;
num=pK_ptr[v].num;
sprintf(msgbuf,"Random walk data, pack %d, "
"vert %d: neighbors = ",
pnum,v);
for (i=0;i<=num;i++)
{
sprintf(buff," %d",pK_ptr[v].flower[i]);
strcat(msgbuf,buff);
}
msg();
sprintf(msgbuf," Conductances: total node = % .5e; "
"twin = % .5e",
nc=node_conductance(p,v),tc=twin_conductance(p,v));
msg();
dumf=Cvw(pR_ptr[v].rad,0.0,
pR_ptr[pK_ptr[v].flower[0]].rad,
pR_ptr[pK_ptr[v].flower[1]].rad,1,p->hes);
tf=dumf/nc;
duml = Cvw(pR_ptr[v].rad,
pR_ptr[pK_ptr[v].flower[num-1]].rad,
pR_ptr[pK_ptr[v].flower[num]].rad,0.0,2,p->hes);
tl=duml/nc;
if (!pK_ptr[v].bdry_flag) {dumf += duml;tf += tl;}
sprintf(msgbuf," % .5e",dumf);
sprintf(buff2," % .5e",tf);
for (i=1;i<num;i++)
{
dum=Cvw(pR_ptr[v].rad,
pR_ptr[pK_ptr[v].flower[i+1]].rad,
pR_ptr[pK_ptr[v].flower[i]].rad,
pR_ptr[pK_ptr[v].flower[i-1]].rad,0,p->hes);
tf=dum/nc;
sprintf(buff," % .5e",dum);
strcat(msgbuf,buff);
sprintf(buff," % .5e",tf);
strcat(buff2,buff);
}
if (pK_ptr[v].bdry_flag)
{
sprintf(buff," % .5e",duml);
strcat(msgbuf,buff);
sprintf(buff," % .5e",tl);
strcat(buff2,buff);
}
msg(); /* print conductances */
sprintf(msgbuf," Transition Probabilities: "
"leakage = % .5e\n",
tc/nc);
strcat(msgbuf,buff2);
msg(); /* print transition probabilities */
count++;
trace=trace->next;
} while (trace!=NULL && count < 5);
vert_free(&vertlist); /* free any excess */
}
return count;
}
case 31: /* length of bdry */
{
if ( (vertlist=node_link_parse(p,datastr,&endptr,&hits)) != NULL)
{
sprintf(msgbuf,"Bdry counts:");
trace=vertlist;
do {
if ((hits=bdry_comp_count(p,trace->v)))
{
sprintf(buff," v%d --> count=%d, ",trace->v,hits);
strcat(msgbuf,buff);
count++;
}
trace=trace->next;
} while (trace!=NULL && count < 5);
vert_free(&vertlist);
msg();
return 1;
}
sprintf(msgbuf,"Bdry length? Given vert doesn't seem to be "
"in the boundary.");
msg();
return 0;
}
} /* end of switch */
return 0;
} /* print_inq_data */
int close_script_proc(Panel_item item,int value,Event *event)
{xv_set(script_frame,WIN_SHOW,FALSE,0);return 1;}
int reset_script_proc(Panel_item item,int value,Event *event)
{reset_script();return 1;}
int reset_script()
{
Textsw_index first;
first=(Textsw_index)0;
xv_set(script_sw,TEXTSW_INSERTION_POINT,first,0);
textsw_possibly_normalize(script_sw,first);
script_file_data_mark=script_file_cmd_mark
=script_file_path_mark=(Textsw_mark)
textsw_add_mark(script_sw,first,TEXTSW_MARK_DEFAULTS);
return 1;
} /* reset_script */
int find_script_proc(Panel_item item,int value,Event *event)
{find_next_script_cmd(0,0);return 1;}
int next_script_proc(Panel_item item,int value,Event *event)
/* get next full line of commands from the script command file */
{
if (find_next_script_cmd(0,1)==(-1)) return 1;
sort_cmd(next_script_cmd,¤t_p);
free(next_script_cmd);next_script_cmd=NULL;
return 1;
} /* next_script_proc */
int read_infile(struct p_data *p,char *datastr)
/* read pack data from script file */
{
int Pid,flag,count=0;
long save_pos,pos;
char *databuf=NULL,*ptr;
char tmpfile[NAME_MAX],filename[NAME_MAX];
FILE *tmp_fp;
filename[0]='\0';
if (!cmd_mode)
{
if ((sscanf(datastr,"%s",filename)!=1)
|| (databuf=ptr=find_script_data(filename,1))==NULL )
{
sprintf(msgbuf,"No pack data in script file with "
"given label.");
emsg();
if (databuf) free(databuf);
return 0;
}
/* first have to put data in tmp file */
Pid=(int)getpid();
sprintf(tmpfile,"/tmp/%d%s",Pid,filename);
if ( (tmp_fp=fopen(tmpfile,"w"))==NULL )
{
sprintf(msgbuf,"Temporary store of script file failed.");
emsg();
if (databuf) free(databuf);
return 0;
}
while ( *ptr!='\0' && (fputc(*ptr,tmp_fp)!=EOF) ) ptr++;
fclose(tmp_fp);
if ( (tmp_fp=fopen(tmpfile,"r"))==NULL )
{
if (databuf) free(databuf);
return 0;
}
if (!call_read_pack(tmp_fp,p,filename)) return 0;
if (tmp_fp) fclose(tmp_fp);
if (databuf) free(databuf);
return 1;
}
else
{
if ((sscanf(datastr,"%s",filename)!=1)
|| (flag=find_name(script_fp,filename,&pos))<=0
|| flag>2)
{
sprintf(msgbuf,"No NODECOUNT or CHECKCOUNT "
"data in script file with label %s.",
filename);
emsg();
return 0;
}
save_pos=ftell(script_fp);
fseek(script_fp,pos,SEEK_SET);
count=call_read_pack(script_fp,p,filename);
fseek(script_fp,save_pos,SEEK_SET);
return count;
}
} /* read_infile */
int cmds_infile(char *datastr)
/* read string of cmds from script file */
{
int Pid,flag,count=0;
long save_pos,pos;
char *databuf=NULL,*ptr;
char tmpfile[NAME_MAX],filename[NAME_MAX],command[BUFSIZE];
FILE *tmp_fp;
filename[0]='\0';
if (!cmd_mode)
{
if ((sscanf(datastr,"%s",filename)!=1)
|| (databuf=ptr=find_script_data(filename,3))==NULL )
{
sprintf(msgbuf,"Didn't find labeled cmds stream "
"in script file.");
emsg();
if (databuf) free(databuf);
return 0;
}
/* first have to put cmds in tmp file */
Pid=(int)getpid();
sprintf(tmpfile,"/tmp/%d%s",Pid,filename);
if ( (tmp_fp=fopen(tmpfile,"w"))==NULL )
{
sprintf(msgbuf,"Temporary store of script data failed.");
emsg();
if (databuf) free(databuf);
return 0;
}
while ( *ptr!='\0' && (fputc(*ptr,tmp_fp)!=EOF) ) ptr++;
fclose(tmp_fp);
fexec_call(tmpfile);
if (databuf) free(databuf);
return 1;
}
else
{
if ((sscanf(datastr,"%s",filename)!=1)
|| (flag=find_name(script_fp,filename,&pos))<=0
|| flag!=4)
{
sprintf(msgbuf,"No CMDS data in script file with "
"label %s.",filename);
emsg();
return 0;
}
save_pos=ftell(script_fp);
fseek(script_fp,pos,SEEK_SET);
while (fgets(command,BUFSIZE-1,script_fp) && command!='\0')
count += handle_cmd(command,¤t_p);
fseek(script_fp,save_pos,SEEK_SET);
return count;
}
} /* cmds_infile */
int path_infile(char *datastr)
/* read path data from script file */
{
int Pid,flag,count;
long save_pos,pos;
char *databuf=NULL,*ptr;
char tmpfile[NAME_MAX],filename[NAME_MAX];
FILE *tmp_fp;
filename[0]='\0';
if (!cmd_mode)
{
if ((sscanf(datastr,"%s",filename)!=1)
|| (databuf=ptr=find_script_data(filename,2))==NULL )
{
sprintf(msgbuf,"No path data in script file with "
"given label.");
emsg();
if (databuf) free(databuf);
return 0;
}
/* first have to put data in tmp file */
Pid=(int)getpid();
sprintf(tmpfile,"/tmp/%d%s",Pid,filename);
if ( (tmp_fp=fopen(tmpfile,"w"))==NULL )
{
sprintf(msgbuf,"Temporary store of path data failed.");
emsg();
if (databuf) free(databuf);
return 0;
}
while ( *ptr!='\0' && (fputc(*ptr,tmp_fp)!=EOF) )
ptr++;
fclose(tmp_fp);
/* sysfp=popen("/bin/sh","w");
fprintf(sysfp,"chmod 666 /tmp/%s",filename);
fclose(sysfp);
*/
if ( (tmp_fp=fopen(tmpfile,"r"))==NULL )
{
if (databuf) free(databuf);
return 0;
}
readpath(tmp_fp,&pathlist,&pathlength);
fclose(tmp_fp);
if (databuf) free(databuf);
return 1;
}
else
{
if ((sscanf(datastr,"%s",filename)!=1)
|| (flag=find_name(script_fp,filename,&pos))<=0
|| flag!=3)
{
sprintf(msgbuf,"No PATH data in script file with "
"label %s.",filename);
emsg();
return 0;
}
save_pos=ftell(script_fp);
fseek(script_fp,pos,SEEK_SET);
count=readpath(script_fp,&pathlist,&pathlength);
fseek(script_fp,save_pos,SEEK_SET);
return count;
}
} /* path_infile */
int fexec_call(char *datastr)
/* open file, execute commands */
{
int count=0;
char filename[256],command[BUFSIZE];
FILE *fp;
sscanf(datastr,"%s",filename);
if ((fp=fopen(filename,"r"))==NULL)
{
sprintf(msgbuf,"fexec error opening %s.",
filename);
emsg();
return count;
}
while (fgets(command,BUFSIZE-1,fp) && command!='\0')
count += handle_cmd(command,¤t_p);
fclose(fp);
return count;
} /* fexec_call */
/* ====================== shmem routines ======================
shared memory routines from Monica Hurdal and Kurt Shaper (Minn)
to pass commands between CirclePack and IDL (or other external
program) */
int c_initmem()
{
int shmid;
key_t key;
int blockSize;
char *keyName;
/* create the key */
keyName = (char *) getenv("SHMEMKEY");
if (keyName == NULL)
keyName = "shmem.key";
key = ftok(keyName,'c');
if ((int)key < 0) {
perror("error creating key");
return(1);
}
/* create share memory block and write id to interface file */
blockSize = 3*sizeof(int) + MAXCMD*sizeof(char);
shmid = shmget(key, blockSize, IPC_CREAT|SHMODE);
if (shmid < 0) {
perror("error getting shared memory block");
return(2);
}
/* map the variable pointers to the shared memory block */
shmBlock = (char *) shmat(shmid, 0, 0);
if (!((long)shmBlock+1)) {
perror("error attaching to shared memory block");
return(3);
}
lockFlg = (int *) shmBlock;
newFlag = (int *) (shmBlock+1*sizeof(int));
vertNum = (int *) (shmBlock+2*sizeof(int));
cmdStr = (char *) (shmBlock+3*sizeof(int));
*lockFlg = 0;
return(0);
} /* c_initmem */
int c_rdmem(int *vnum,char *vstr)
{
int loopCount;
/* if not initialized, error */
if (lockFlg == NULL) {
perror("shared memory not initialized");
return(1);
}
/* if nothing new, just bail */
if (*newFlag == 0) {
*vnum = *vertNum;
strncpy(vstr,cmdStr,MAXCMD);
return(-1);
}
/* lock memory area */
loopCount = 0;
while (*lockFlg != 0) {
usleep(100);
loopCount++;
if (loopCount > 100) {
perror("error locking memory segment");
return(2);
}
}
/* ---------------------- critical section ------------------- */
*lockFlg = 1;
*vnum = *vertNum;
strncpy(vstr,cmdStr,MAXCMD);
*newFlag = 0;
*lockFlg = 0;
/* ---------------------- critical section ------------------- */
return(0);
} /* c_rdmem */
int c_wrmem(int *vnum,char *vstr)
{
int loopCount;
/* if not initialized, error */
if (lockFlg == NULL) {
perror("shared memory not initialized");
return(1);
}
/* lock memory area */
loopCount = 0;
while (*lockFlg != 0) {
usleep(100);
loopCount++;
if (loopCount > 100) {
perror("error locking memory segment");
return(2);
}
}
/* ---------------------- critical section ------------------- */
*lockFlg = 1;
*vertNum = *vnum;
strncpy(cmdStr,vstr,MAXCMD);
*newFlag = 1;
*lockFlg = 0;
/* ---------------------- critical section ------------------- */
return(0);
} /* c_wrmem */
int cmd_import(char *datastr)
/* import a string from shared memory */
{
int stat;
int vnum = 0;
if (init_shmem == 0) {
stat = c_initmem();
if (stat > 0) {
if (stat == 1) {
sprintf(msgbuf,"File shmem.key not found.");
emsg();
}
sprintf(msgbuf,"Not able to share memory with "
"external application.");
emsg();
return 0;
}
} else init_shmem = 1;
stat = c_rdmem(&vnum,buf);
if (stat > 0) {
sprintf(msgbuf,"Error importing command from "
"external application.");
emsg();
return 0;
} else if (stat < 0) {
sprintf(msgbuf,"No change - nothing to do.");
msg();
return 1;
} else {
sprintf(msgbuf,"Importing %s",buf);
msg();
return(sort_cmd(buf,¤t_p));
}
} /* cmd_import */
int cmd_export(struct p_data *p,char *datastr)
/* export a string to shared memory */
{
int stat,vnum = 0,hits;
char *dpoint,cmd[128],outstr[128],*endptr;
struct Vertlist *holdv;
if (init_shmem == 0) {
stat = c_initmem();
if (stat > 0) {
if (stat == 1) {
sprintf(msgbuf,"File shmem.key not found.");
emsg();
}
sprintf(msgbuf,"Not able to share memory with "
"external application.");
emsg();
return 0;
}
} else init_shmem = 1;
dpoint=datastr;
/* pre-processing datastr */
if (!grab_next(&dpoint,cmd)) return 0; /* string was empty */
switch(cmd[0])
{
case 'c':
{
if (!strncmp(cmd,"center_vert",11)) /* ======= center_vert */
{
if ((holdv=node_link_parse(p,dpoint,&endptr,&hits))!=NULL)
{
sprintf(outstr,"center_vert %d",holdv->v);
vnum = holdv->v;
vert_free(&holdv);
}
else goto BAIL;
break;
}
break;
}
}/* end of switch */
if ((stat = c_wrmem(&vnum,outstr))>0) goto BAIL;
sprintf(msgbuf,"Exporting %s",outstr);
msg();
return 1;
BAIL:
sprintf(msgbuf,"Error exporting command to "
"external application.");
emsg();
return 0;
} /* cmd_export */
/* ---------------------------------- some technical routines ---------- */
char *get_selection(int kill)
/* gets current primary selection; remove
highlighting in scratch subwindow if kill true. */
{
static char datastr[BUFSIZE];
Seln_holder holder;
Seln_request *buffer;
holder=seln_inquire(SELN_PRIMARY);
buffer=seln_ask(&holder, SELN_REQ_CONTENTS_ASCII,0,0);
strncpy(datastr,buffer->data+sizeof(Seln_attribute),BUFSIZE-10);
if (kill) textsw_set_selection(scratch_sw,0,0,1);
return (datastr);
} /* get_selection */
int stat_file(char *filename)
/* returns 1 if filename exists */
{
struct stat statbuf;
return( (strlen(filename)<=NAME_MAX) && (stat(filename, &statbuf)>=0) );
} /* stat_file */
int print_proc(Menu menu,Menu_item menuitem)
/* act on print_menu */
{
int pnum,cflag,fflag,act = (int)xv_get(menuitem,MENU_VALUE)-1;
char *datastr;
struct p_data *p;
char buff[128];
p=&packdata[current_p];
if (act>1 && act<11 && !custom_fp)
{
strcpy(msgbuf,"The 'custom' postscript file has "
"not been opened.");
emsg();
return 1;
}
switch (act)
{
case 1: /* open default custom file (lose previous contents) */
{
if (!cmd_mode)
strcpy(ps_file_name,(char *)xv_get(ps_file_item,PANEL_VALUE));
open_psfile(p,1,"\0",&custom_fp,ps_file_name,user_name,
CPVersion); return 1;
}
case 3: sprintf(buff," -u "); break; /* unit circle */
case 4: /* selected open circles */
{
if( strlen(datastr=get_selection(1)) )
sprintf(buff," -c %s",datastr);
else sprintf(buff," -c a");
break;
}
case 5: /* selected filled circles */
{
if ( strlen(datastr=get_selection(1)) )
sprintf(buff," -cf %s",datastr);
else sprintf(buff," -cf a");
break;
}
case 6: /* selected open faces */
{
if ( strlen(datastr=get_selection(1)) )
sprintf(buff," -f %s",datastr);
else sprintf(buff," -f a");
break;
}
case 7: /* selected filled faces */
{
if ( strlen(datastr=get_selection(1)) )
sprintf(buff," -ff %s",datastr);
else sprintf(buff," -ff a");
break;
}
case 8: /* selected edges */
{
if (!strlen(datastr=get_selection(1)))
{strcpy(msgbuf,"You must select vertices. ");emsg();break;}
else sprintf(buff," -e %s",datastr);
break;
}
case 9: /* selected vertex labels */
{
if ( strlen(datastr=get_selection(1)) )
sprintf(buff," -nc %s",datastr);
else sprintf(buff," -nc a");
break;
}
case 10: /* selected face labels */
{
if ( strlen(datastr=get_selection(1)) )
sprintf(buff," -nf %s",datastr);
else sprintf(buff," -nf a");
break;
}
case 11: /* caption */
{
if ( strlen(datastr=get_selection(1)) )
sprintf(buff," -t %s",datastr);
else sprintf(buff," -t");
break;
}
case 12: /* record display option objects from selected pack. */
{
if (!strlen(datastr=get_selection(1)) || sscanf(datastr,"%d",&pnum)!=1
|| pnum<0 || pnum>NUM_PACKS || !packdata[pnum].status)
return 1;
cflag=screendata[pnum].cflag_opt;
fflag=screendata[pnum].fflag_opt;
if (cflag & 1) /* circle options */
{
strcpy(buff,"-c");
if (cflag & 8) strcat(buff,"c");
if (!(cflag & 16) && (cflag & 4)) strcat(buff,"bg");
else if (!(cflag & 16) && (cflag & 2)) strcat(buff,"fg");
else if (cflag & 2) strcat(buff,"f");
strcat(buff," a");
if (cflag & 32) strcat(buff,"-nc a");
}
if (fflag & 1) /* face options */
{
strcat(buff," -f");
if (fflag & 8) strcat(buff,"c");
if (!(fflag & 16) && (fflag & 4)) strcat(buff,"bg");
else if (!(fflag & 16) && (fflag & 2)) strcat(buff,"fg");
else if (fflag & 2) strcat(buff,"f");
strcat(buff," a");
if (fflag & 32) strcat(buff,"-nf a");
}
break;
}
case 13: sprintf(buff," -g");break; /* print path */
case 14: /* wrap up the postscript file and close it */
{
close_psfile(&custom_fp);
return 1;
}
case 15: /* print the special postscript file */
{
print_psfile(&custom_fp);
return 1;
}
case 17: /* print the canvas postscript file. */
{
post_canvas(p);
return 1;
}
} /* end of switch */
if (!cmd_mode)
{
strcpy(ps_file_name,(char *)xv_get(print_file_item,PANEL_VALUE));
strcpy(print_cmd,(char *)get_print_cmd());
}
print_call(&custom_fp,p,ps_file_name,
buff,user_name,CPVersion,print_cmd);
return 1;
} /* print_proc */
int sel_print_proc(Panel_item item,Event *event)
/* bring up print_menu */
{if (event_action(event)==ACTION_MENU && event_is_down(event))
menu_show(print_menu,panel,event,0);return 1; }
int post_canvas(struct p_data *p)
/* circles/triangles/edges/labels/etc using standard flags. */
{
int cflag,fflag;
char buff[NAME_MAX+128];
if (!p->status) return 0;
if (post_fp)
{
fprintf(post_fp,"\ngrestore\nend\nshowpage"); /* wrap up */
fclose(post_fp);
post_fp=NULL;
}
switch (fill_mode)
{
case 0: {graylevel=.5;break;/* grey */}
case 1: {graylevel=1;break; /* white */}
case 2: {graylevel=0;break; /* black */}
}
ps_linewidth=(1+2*(line_thick-1))*PS_UNIT_LINEWIDTH;
cflag=p->screen->cflag_opt;
fflag=p->screen->fflag_opt;
if (!cmd_mode)
sprintf(print_file_name,
(char *)get_print_file());
sprintf(buff," -o %s ",print_file_name);
if (p->screen->unitcircle) strcat(buff," -u ");
if (cflag & 1) /* circle options */
{
strcat(buff," -c");
if (cflag & 8) strcat(buff,"c");
if (!(cflag & 16) && (cflag & 4)) strcat(buff,"bg");
else if (!(cflag & 16) && (cflag & 2)) strcat(buff,"fg");
else if (cflag & 2) strcat(buff,"f");
strcat(buff," a");
if (cflag & 32) strcat(buff," -nc a");
}
if (fflag & 1) /* face options */
{
strcat(buff," -f");
if (fflag & 8) strcat(buff,"c");
if (!(fflag & 16) && (fflag & 4)) strcat(buff,"bg");
else if (!(fflag & 16) && (fflag & 2)) strcat(buff,"fg");
else if (fflag & 2) strcat(buff,"f");
strcat(buff," a");
if (fflag & 32) strcat(buff," -nf a");
}
if (!cmd_mode)
strcpy(print_cmd,(char *)get_print_cmd());
print_call(&post_fp,p,print_file_name,
buff,user_name,CPVersion,print_cmd);
fprintf(post_fp,"\ngrestore\nend\nshowpage"); /* wrap up */
fclose(post_fp);
post_fp=NULL;
/* to save paper, pop up ghostview image; user can then print */
/* print_psfile(&post_fp); */
if (!cmd_mode)
strcpy(print_file_name,(char *)get_print_file());
strcpy(buff,print_file_name);
sysfp=popen("/bin/sh","w");
fprintf(sysfp," ghostview %s &\n",buff);
pclose(sysfp);
return 1;
} /* post_canvas */
int print_psfile(FILE **FP)
/* print canvas (flag=1) or custom (flag=2) file */
{
char buff[64];
FILE *sysfp;
if (FP==&post_fp)
{
if (!cmd_mode)
strcpy(print_file_name,(char *)get_print_file());
strcpy(buff,print_file_name);
}
else if (FP==&custom_fp)
{
if (custom_fp) close_psfile(&custom_fp);
if (!cmd_mode)
strcpy(ps_file_name,(char *)get_ps_file());
strcpy(buff,ps_file_name);
}
else return 0;
if (!cmd_mode)
strcpy(print_cmd,(char *)get_print_cmd());
strcpy(buf,print_cmd);
strcat(buf," ");
strcat(buf,buff);
sysfp=popen("/bin/sh","w");
fprintf(sysfp,"%s\n",buf);
pclose(sysfp);
sprintf(msgbuf,"Have sent `%s' to the printer.",buff);
msg();
return 1;
} /* print_psfile */
|