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 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732
|
/*
See license.txt in the root of this project.
*/
/*tex
This is just an experiment by Mikael and Hans in the perspective of 3D experiments in
\METAPOST\ with the \LUA\ interface. It is also a kind of follow up on the \type {matrix}
module from which we took some of the code. So we have a mix of Jeong Dalyoung, Mikael
Sundqvist & Hans Hagen code here. More methods might be added.
This module is sort of generic but also geared at using on combination with \METAPOST, which
is why we have a few public functions. For that we have extra helpers that work on meshes.
*/
# include <luametatex.h>
# include "libraries/triangles/triangles.h"
# define VECTOR_METATABLE "vector"
# define MESH_METATABLE "mesh"
/*
todo:
-- maybe also store size (more readable, less clutter)
-- extra array for w (doubles)
-- userdata lua upvalue for whatever extras
*/
static const char *mesh_names[5] = {
[no_mesh_type] = "no",
[dot_mesh_type] = "dot",
[line_mesh_type] = "line",
[triangle_mesh_type] = "triangle",
[quad_mesh_type] = "quad"
};
# define valid_mesh(n) (n >= dot_mesh_type && n <= quad_mesh_type ? n : triangle_mesh_type)
/*tex
We need to be way above EPSILON in triangles.c because otherwise we get too many
false positives at that end!
For practical reason we now use a configurable epsilon because we need to interplay
nicely with the overlap detection. Alas.
\starttyping
const double p_epsilon = 0.000001;
const double m_epsilon = -0.000001;
static inline int iszero(double d) {
return d > m_epsilon && d < p_epsilon;
}
\stoptyping
So we now do this:
*/
# define vector_epsilon_default 1.0e-06
static double vector_epsilon = vector_epsilon_default;
# define ISZERO(d) (fabs(d) < epsilon)
static int vectorlib_setepsilon(lua_State *L)
{
vector_epsilon = lmt_optdouble(L, 1, vector_epsilon_default);
return 1;
}
static int vectorlib_getepsilon(lua_State *L)
{
lua_pushnumber(L, lua_toboolean(L, 1) ? vector_epsilon_default : vector_epsilon);
return 1;
}
static int vectorlib_iszero(lua_State *L)
{
double epsilon = lmt_optdouble(L, 2, vector_epsilon);
lua_pushboolean(L, ISZERO(lua_tonumber(L, 1)));
return 1;
}
static inline int vectorlib_aux_zero_in_column(const vector a, int c, double epsilon)
{
for (int r = 0; r < a->rows; r++) {
if (ISZERO(a->data[r * a->columns + c])) {
return 1;
}
}
return 0;
}
// static inline int vectorlib_aux_zero_in_row(const vector a, int r, double epsilon)
// {
// for (int c = 0; c < a->columns; c++) {
// if (ISZERO(a->data[r * a->columns + c])) {
// return 1;
// }
// }
// return 0;
// }
/* let the compiled deal with this: */
/* memcpy(v->data, a->data,a->rows * a->columns * sizeof(double)); */
inline static void vectorlib_aux_copy_data(vector v, const vector a)
{
for (int i = 0; i < a->rows * a->columns; i++) {
v->data[i] = a->data[i];
}
}
/*
We let \LUA\ manage the temporary vectors if needed which sometimes means that we push too
many intermediate nil's too but it's harmless.
*/
static inline vector vectorlib_aux_push(lua_State *L, int r, int c, int s)
{
if (r >= max_vector_rows || c >= max_vector_columns || r*c > max_vector) {
tex_formatted_error("vector lib", "you can have %i rows, %i columns and at most %i entries", max_vector_rows, max_vector_columns, max_vector);
return NULL;
} else {
vector v = lua_newuserdatauv(L, 6 * sizeof(int) + r * c * sizeof(double), 0);
if (v && r > 0 && c > 0) {
v->rows = r;
v->columns = c;
v->type = generic_type;
v->stacking = s;
v->index = 0;
v->padding = 0;
lua_get_metatablelua(vector_instance);
lua_setmetatable(L, -2);
}
return v;
}
}
static vector vectorlib_aux_maybe_isvector(lua_State *L, int index)
{
vector v = lua_touserdata(L, index);
if (v && lua_getmetatable(L, index)) {
lua_get_metatablelua(vector_instance);
if (! lua_rawequal(L, -1, -2)) {
v = NULL;
}
lua_pop(L, 2);
}
return v;
}
static int vectorlib_isvector(lua_State *L)
{
lua_pushboolean(L, vectorlib_aux_maybe_isvector(L, 1) ? 1 : 0);
return 1;
}
static int vectorlib_type(lua_State *L)
{
if (vectorlib_aux_maybe_isvector(L, 1)) {
lua_push_key(vector);
} else {
lua_pushnil(L);
}
return 1;
}
/* tex Only |{ { 1,2,3 }, { 4,5,6 } }| here as we can mix. */
static int vectorlib_aux_be_nice(lua_State *L, int index)
{
/* We know that we have a table. */
int rows = (int) lua_rawlen(L, index);
int columns = lua_rawgeti(L, index, 1) == LUA_TTABLE ? (int) lua_rawlen(L, -1) : 0;
lua_pop(L, 1);
if (rows && columns) {
vector v = vectorlib_aux_push(L, rows, columns, 0);
for (int r = 0; r < rows; r++) {
long target = r * columns;
if (lua_rawgeti(L, index, r + 1) == LUA_TTABLE) {
for (int c = 0; c < columns; c++) {
v->data[target++] = lua_rawgeti(L, -1, c + 1) == LUA_TNUMBER ? lua_tonumber(L, -1) : 0.0;
lua_pop(L, 1);
}
} else {
for (int c = 0; c < columns; c++) {
v->data[target++] = 0.0;
}
}
lua_pop(L, 1);
}
return 1;
}
lua_pushnil(L);
return 1;
}
inline static vector vectorlib_aux_get(lua_State *L, int index)
{
switch (lua_type(L, index)) {
case LUA_TUSERDATA:
{
vector v = lua_touserdata(L, index);
if (v && lua_getmetatable(L, index)) {
lua_get_metatablelua(vector_instance);
if (! lua_rawequal(L, -1, -2)) {
v = NULL;
}
lua_pop(L, 2);
}
return v;
}
case LUA_TTABLE:
{
vectorlib_aux_be_nice(L, index);
if (lua_type(L, -1) == LUA_TUSERDATA) {
vector v = lua_touserdata(L, -1);
lua_replace(L, index);
return v;
} else {
lua_pop(L, 1);
break;
}
}
default:
break;
}
return NULL;
}
static int vectorlib_new(lua_State *L)
{
switch (lua_type(L, 1)) {
case LUA_TNUMBER:
{
/* local v = vector.new( 2, 3 ) */
/* local v = vector.new( 2, 3, 1,2,3, 4,5,6 ) */
/* local v = vector.new( 2, 3, { 1,2,3 }, { 4,5,6 } ) */ /* maybe */
/* local v = vector.new( 2, 3, { 1,2,3 , 4,5,6 } ) */ /* maybe */
int t = lua_gettop(L) - 2;
vector v = vectorlib_aux_push(L, lmt_optinteger(L, 1, 1), lmt_optinteger(L, 2, 1), 0);
if (t) {
if (t > v->rows * v->columns) {
t = v->rows * v->columns;
}
for (int i = 0; i < t; i++) {
v->data[i] = lua_type(L, i + 3) == LUA_TNUMBER ? lua_tonumber(L, i + 3) : 0.0;
}
} else {
for (int i = 0; i < v->rows * v->columns; i++) {
v->data[i] = 0.0;
}
}
return 1;
}
case LUA_TTABLE:
{
int n = (int) lua_rawlen(L, 1);
int t = lua_rawgeti(L, 1, 1);
lua_pop(L, 1);
switch (t) {
case LUA_TNUMBER:
/* local v = vector.new( { 1,2,3 }, { 4,5,6 } ) */
{
int columns = n;
int rows = lua_gettop(L);
if (rows && columns) {
vector v = vectorlib_aux_push(L, rows, columns, 0);
for (int r = 0; r < rows; r++) {
long index = r * columns;
if (lua_type(L, r + 1) == LUA_TTABLE) {
for (int c = 0; c < columns; c++) {
if (lua_rawgeti(L, r + 1, c + 1) == LUA_TNUMBER) {
v->data[index++] = lua_tonumber(L, -1);
} else {
v->data[index++] = 0.0;
}
lua_pop(L, 1);
}
} else {
for (int c = 0; c < columns; c++) {
v->data[index++] = 0.0;
}
}
}
return 1;
} else {
break;
}
}
case LUA_TTABLE:
{
/* local v = vector.new( { { 1,2,3 }, { 4,5,6 } } ) */
int type = (int) lua_rawgeti(L, 1, 1);
int rows = (int) lua_rawlen(L, 1);
int columns = 0;
switch (type) {
case LUA_TTABLE:
columns = (int) lua_rawlen(L, -1);
break;
case LUA_TUSERDATA:
{
vector a = vectorlib_aux_get(L, 1);
if (a) {
columns = a->columns;
}
break;
}
}
lua_pop(L, 1);
if (rows && columns) {
vector v = vectorlib_aux_push(L, rows, columns, 0);
long index = 0;
for (int r = 1; r <= rows; r++) {
switch (lua_rawgeti(L, 1, r)) {
case LUA_TTABLE:
{
for (int c = 1; c <= columns; c++) {
if (lua_rawgeti(L, -1, c) == LUA_TNUMBER) {
v->data[index++] = lua_tonumber(L, -1);
} else {
v->data[index++] = 0.0;
}
lua_pop(L, 1);
}
break;
}
case LUA_TUSERDATA:
{
vector a = vectorlib_aux_get(L, 1);
if (a && a->columns * a->rows == columns) {
long source = 0;
for (int c = 0; c < columns; c++) {
v->data[index++] = a->data[source++];
}
break;
} else {
/* fall through */
}
}
default:
{
for (int c = 0; c < columns; c++) {
v->data[index++] = 0.0;
}
break;
}
}
lua_pop(L, 1);
}
return 1;
} else {
break;
}
}
default:
break;
}
break;
}
}
lua_pushnil(L);
return 0;
}
static inline int vectorlib_onerow(lua_State *L)
{
int t = lua_gettop(L);
if (t) {
vector v = vectorlib_aux_push(L, 1, t, 0);
for (int i = 0; i < t; i++) {
v->data[i] = lua_tonumber(L, i + 1);
}
} else {
lua_pushnil(L);
}
return 1;
}
static inline int vectorlib_onecolumn(lua_State *L)
{
int t = lua_gettop(L);
if (t) {
vector v = vectorlib_aux_push(L, t, 1, 0);
for (int i = 0; i < t; i++) {
v->data[i] = lua_tonumber(L, i + 1);
}
} else {
lua_pushnil(L);
}
return 1;
}
vector vectorlib_get(lua_State *L, int index)
{
if (lua_type(L, index) == LUA_TUSERDATA) {
return (vector) luaL_checkudata(L, index, VECTOR_METATABLE);
} else {
return NULL;
}
}
static int vectorlib_tostring(lua_State *L)
{
vector v = vectorlib_aux_get(L, 1);
if (v) {
lua_pushfstring(L, "<vector %d x %d : %p>", v->rows, v->columns, v);
return 1;
} else {
return 0;
}
}
static int vectorlib_totable(lua_State *L)
{
vector v = vectorlib_aux_maybe_isvector(L, 1);
if (v) {
long source = 0;
if (lua_toboolean(L, 2)) {
lua_createtable(L, v->rows * v->columns, 0);
for (int i = 1; i <= v->rows * v->columns; i++) {
lua_pushnumber(L, v->data[source++]);
lua_rawseti(L, -2, i);
}
} else {
lua_createtable(L, v->rows, 0);
for (int r = 1; r <= v->rows; r++) {
lua_createtable(L, v->columns, 0);
for (int c = 1; c <= v->columns; c++) {
lua_pushnumber(L, v->data[source++]);
lua_rawseti(L, -2, c);
}
lua_rawseti(L, -2, r);
}
}
return 1;
} else {
return 0;
}
}
static int vectorlib_aux_copy(lua_State *L, int index)
{
vector a = vectorlib_aux_get(L, index);
if (a) {
/* could be a mem copy */
vector v = vectorlib_aux_push(L, a->rows, a->columns, a->stacking);
v->type = a->type;
vectorlib_aux_copy_data(v, a);
} else {
lua_pushnil(L);
}
return 1;
}
static int vectorlib_copy(lua_State *L)
{
return vectorlib_aux_copy(L, 1);
}
static int vectorlib_eq(lua_State *L)
{
vector a = vectorlib_aux_get(L, 1);
vector b = vectorlib_aux_get(L, 2);
int result = 1;
if (a && b && a->rows == b->rows && a->columns == b->columns) {
/* we can do a memcmp but we can have negative zero etc */
for (int i = 0; i < b->rows * b->columns; i++) {
if (a->data[i] != b->data[i]) {
result = 0;
break;
}
}
} else {
result = 0;
}
lua_pushboolean(L, result);
return 1;
}
static int vectorlib_add(lua_State *L) {
if (lua_type(L, 1) == LUA_TNUMBER) {
vector b = vectorlib_aux_get(L, 2);
if (b) {
double d = lua_tonumber(L, 1);
vector v = vectorlib_aux_push(L, b->rows, b->columns, b->stacking);
for (int i = 0; i < b->rows * b->columns; i++) {
v->data[i] = b->data[i] + d;
}
} else {
lua_pushnil(L);
}
} else if (lua_type(L, 2) == LUA_TNUMBER) {
vector a = vectorlib_aux_get(L, 1);
if (a) {
double d = lua_tonumber(L, 2);
vector v = vectorlib_aux_push(L, a->rows, a->columns, a->stacking);
for (int i = 0; i < a->rows * a->columns; i++) {
v->data[i] = a->data[i] + d;
}
} else {
lua_pushnil(L);
}
} else {
vector a = vectorlib_aux_get(L, 1);
vector b = vectorlib_aux_get(L, 2);
if (a && b && a->rows == b->rows && a->columns == b->columns) {
vector v = vectorlib_aux_push(L, a->rows, a->columns, a->stacking);
for (int i = 0; i < a->rows * a->columns; i++) {
v->data[i] = a->data[i] + b->data[i];
}
} else {
lua_pushnil(L);
}
}
return 1;
}
static int vectorlib_sub(lua_State *L) {
if (lua_type(L, 1) == LUA_TNUMBER) {
vector b = vectorlib_aux_get(L, 2);
if (b) {
double d = lua_tonumber(L, 1);
vector v = vectorlib_aux_push(L, b->rows, b->columns, b->stacking);
for (int i = 0; i < b->rows * b->columns; i++) {
v->data[i] = b->data[i] - d;
}
} else {
lua_pushnil(L);
}
} else if (lua_type(L, 2) == LUA_TNUMBER) {
vector a = vectorlib_aux_get(L, 1);
if (a) {
double d = lua_tonumber(L, 2);
vector v = vectorlib_aux_push(L, a->rows, a->columns, a->stacking);
for (int i = 0; i < a->rows * a->columns; i++) {
v->data[i] = a->data[i] - d;
}
} else {
lua_pushnil(L);
}
} else {
vector a = vectorlib_aux_get(L, 1);
vector b = vectorlib_aux_get(L, 2);
if (a && b && a->rows == b->rows && a->columns == b->columns) {
vector v = vectorlib_aux_push(L, a->rows, a->columns, a->stacking);
for (int i = 0; i < a->rows * a->columns; i++) {
v->data[i] = a->data[i] - b->data[i];
}
} else {
lua_pushnil(L);
}
}
return 1;
}
static int vectorlib_product(lua_State *L)
{
vector a = vectorlib_aux_get(L, 1);
vector b = vectorlib_aux_get(L, 2);
if (a && b && a->columns == b->rows) {
vector v = vectorlib_aux_push(L, a->rows, b->columns, b->stacking); /* not a->stacking */
for (int ra = 0; ra < a->rows; ra++) {
/* loop over rows a : a[r] */
for (int cb = 0; cb < b->columns; cb++) {
/* loop over columns b */
double d = 0;
for (int i = 0; i < b->rows; i++) {
/* loop over rows b */
d = d + a->data[ra * a->columns + i] * b->data[i * b->columns + cb];
}
/* result [ra][cb] */
v->data[ra * b->columns + cb] = d;
}
}
} else {
lua_pushnil(L);
}
return 1;
}
static int vectorlib_mul(lua_State *L) {
if (lua_type(L, 1) == LUA_TNUMBER) {
vector b = vectorlib_aux_get(L, 2);
if (b) {
double d = lua_tonumber(L, 1);
if (d == 1.0) {
vectorlib_aux_copy(L, 2);
} else {
vector v = vectorlib_aux_push(L, b->rows, b->columns, b->stacking);
for (int i = 0; i < b->rows * b->columns; i++) {
v->data[i] = b->data[i] * d;
}
}
} else {
lua_pushnil(L);
}
} else if (lua_type(L, 2) == LUA_TNUMBER) {
vector a = vectorlib_aux_get(L, 1);
if (a) {
double d = lua_tonumber(L, 2);
if (d == 1.0) {
vectorlib_aux_copy(L, 1);
} else {
vector v = vectorlib_aux_push(L, a->rows, a->columns, a->stacking);
for (int i = 0; i < a->rows * a->columns; i++) {
v->data[i] = a->data[i] * d;
}
}
} else {
lua_pushnil(L);
}
} else {
return vectorlib_product(L);
}
return 1;
}
static int vectorlib_div(lua_State *L) {
if (lua_type(L, 1) == LUA_TNUMBER) {
lua_pushnil(L);
} else if (lua_type(L, 2) == LUA_TNUMBER) {
vector a = vectorlib_aux_get(L, 1);
if (a) {
double d = lua_tonumber(L, 2);
vector v = vectorlib_aux_push(L, a->rows, a->columns, a->stacking);
for (int i = 0; i < a->rows * a->columns; i++) {
/* Should we check for d == 0.0 or not here? */
v->data[i] = a->data[i] / d;
}
} else {
lua_pushnil(L);
}
} else {
lua_pushnil(L);
}
return 1;
}
static int vectorlib_neg(lua_State *L) {
vector a = vectorlib_aux_get(L, 1);
if (a) {
vector v = vectorlib_aux_push(L, a->rows, a->columns, a->stacking);
for (int i = 0; i < a->rows * a->columns; i++) {
v->data[i] = - a->data[i];
}
} else {
lua_pushnil(L);
}
return 1;
}
static int vectorlib_getrow(lua_State *L)
{
vector v = vectorlib_aux_get(L, 1);
if (v) {
int row = lmt_tointeger(L, 2) - 1;
if (row >= 0 && row < v->rows) {
long source = row * v->columns;
if (lua_toboolean(L, 3)) {
long target = 1;
lua_createtable(L, v->columns, 0);
for (int c = 0; c < v->columns; c++) {
lua_pushnumber(L, v->data[source++]);
lua_rawseti(L, -2, target++);
}
return 1;
} else {
for (int c = 0; c < v->columns; c++) {
lua_pushnumber(L, v->data[source++]);
}
return v->columns;
}
}
} else {
lua_pushnil(L);
}
return 1;
}
static int vectorlib_getvalue(lua_State *L)
{
vector v = vectorlib_aux_get(L, 1);
if (v) {
long index = lmt_tolong(L, 2);
lua_pushnumber(L, index > 0 && index <= v->rows * v->columns ? v->data[index-1] : 0);
} else {
lua_pushnil(L);
}
return 1;
}
static int vectorlib_setvalue(lua_State *L)
{
vector v = vectorlib_aux_get(L, 1);
if (v) {
long index = lmt_tolong(L, 2);
if (index > 0 && index <= v->rows * v->columns) {
v->data[index-1] = lua_type(L, 3) == LUA_TNUMBER ? lua_tonumber(L, 3) : 0;
}
} else {
lua_pushnil(L);
}
return 1;
}
static int vectorlib_getlength(lua_State *L)
{
vector v = vectorlib_aux_get(L, 1);
lua_pushinteger(L, v ? v->rows : 0);
return 1;
}
static int vectorlib_setnext(lua_State *L)
{
vector a = vectorlib_aux_get(L, 1);
if (a && a->index < (a->rows * a->columns)) {
int value = 1;
for (int c = 0; c < a->columns; c++) {
a->data[a->index++] = lua_type(L, ++value) == LUA_TNUMBER ? lua_tonumber(L, value) : 0;
}
} else {
lua_pushnil(L);
}
return 1;
}
static int vectorlib_getvaluerc(lua_State *L)
{
vector a = vectorlib_aux_get(L, 1);
if (a) {
int row = lmt_tointeger(L, 2) - 1;
int column = lmt_tointeger(L, 3) - 1;
if (row >= 0 && row < a->rows && column >= 0 && column < a->columns) {
lua_pushnumber(L, a->data[row * a->columns + column]);
return 1;
}
}
lua_pushnil(L);
return 1;
}
static int vectorlib_setvaluerc(lua_State *L)
{
vector a = vectorlib_aux_get(L, 1);
if (a) {
int row = lmt_tointeger(L, 2) - 1;
int column = lmt_tointeger(L, 3) - 1;
if (row >= 0 && row < a->rows && column >= 0 && column < a->columns) {
a->data[row * a->columns] = lua_type(L, 4) == LUA_TNUMBER ? lua_tonumber(L, 4) : 0.0;
}
} else {
lua_pushnil(L);
}
return 1;
}
/* round floor ceil */
static int vectorlib_identity(lua_State *L)
{
int n = lmt_tointeger(L, 1);
if (n > 0) {
vector v = vectorlib_aux_push(L, n, n, 0);
v->type = identity_type;
for (int i = 0; i < v->rows * v->columns; i++) {
v->data[i] = 0.0;
}
for (int i = 0; i < n; i++) {
v->data[i * v->columns + i] = 1.0;
}
} else {
lua_pushnil(L);
}
return 1;
}
static int vectorlib_transpose(lua_State *L)
{
vector a = vectorlib_aux_get(L, 1);
if (a) {
if (a->type == identity_type) {
vectorlib_aux_copy(L, 1);
} else {
// if one column or one row just swap
vector v = vectorlib_aux_push(L, a->columns, a->rows, a->stacking);
for (int r = 0; r < a->rows; r++) {
for (int c = 0; c < a->columns; c++) {
v->data[c * a->rows + r] = a->data[r * a->columns + c];
}
}
}
} else {
lua_pushnil(L);
}
return 1;
}
static int vectorlib_inner(lua_State *L)
{
vector a = vectorlib_aux_get(L, 1);
vector b = vectorlib_aux_get(L, 2);
if (a && b && a->rows == b->rows) {
double d = 0.0;
for (int i = 0; i < a->rows; i++) {
d = d + a->data[i] * b->data[i];
}
lua_pushnumber(L, d);
} else {
lua_pushnil(L);
}
return 1;
}
static int vectorlib_round(lua_State *L)
{
vector a = vectorlib_aux_get(L, 1);
if (a) {
vector v = vectorlib_aux_push(L, a->rows, a->columns, a->stacking);
for (int i = 0; i < a->rows * a->columns; i++) {
v->data[i] = round(a->data[i]);
}
} else {
lua_pushnil(L);
}
return 1;
}
static int vectorlib_floor(lua_State *L)
{
vector a = vectorlib_aux_get(L, 1);
if (a) {
vector v = vectorlib_aux_push(L, a->rows, a->columns, a->stacking);
for (int i = 0; i < a->rows * a->columns; i++) {
v->data[i] = floor(a->data[i]);
}
} else {
lua_pushnil(L);
}
return 1;
}
static int vectorlib_ceiling(lua_State *L)
{
vector a = vectorlib_aux_get(L, 1);
if (a) {
vector v = vectorlib_aux_push(L, a->rows, a->columns, a->stacking);
for (int i = 0; i < a->rows * a->columns; i++) {
v->data[i] = ceil(a->data[i]);
}
} else {
lua_pushnil(L);
}
return 1;
}
static int vectorlib_truncate(lua_State *L)
{
vector a = vectorlib_aux_get(L, 1);
if (a) {
if (lua_toboolean(L, 2)) {
double epsilon = lmt_optdouble(L, 3, vector_epsilon);
/* in place */
for (int i = 0; i < a->rows * a->columns; i++) {
if (ISZERO(a->data[i])) {
a->data[i] = 0.0;
}
}
} else {
vector v = vectorlib_aux_push(L, a->rows, a->columns, a->stacking);
double epsilon = lmt_optdouble(L, 3, vector_epsilon);
for (int i = 0; i < a->rows * a->columns; i++) {
v->data[i] = ISZERO(a->data[i]) ? 0.0 : a->data[i];
}
}
} else {
lua_pushnil(L);
}
return 1;
}
static int vectorlib_aux_uppertriangle(vector v, int sign, double epsilon)
{
for (int i = 0; i < v->rows - 1; i++) {
double pivot = v->data[i * v->columns + i];
if (ISZERO(pivot)) {
int p = i + 1;
// while (! (v->data[p * v->columns + i])) {
while (ISZERO(v->data[p * v->columns + i])) {
p++;
if (p > v->rows) {
return sign;
}
}
// for (int k = 0; k < v->columns; k++) {
// double d1 = v->data[p * v->columns + k];
// double d2 = v->data[i * v->columns + k];
// v->data[p * v->columns + k] = d2;
// v->data[i * v->columns + k] = d1;
// }
p = p * v->columns;
i = i * v->columns;
for (int k = 0; k < v->columns; k++) {
double d1 = v->data[p];
double d2 = v->data[i];
v->data[p++] = d2;
v->data[i++] = d1;
}
sign = -sign;
}
for (int k = i + 1; k < v->rows; k++) {
double factor = - v->data[k * v->columns + i] / v->data[i * v->columns + i];
long target = k * v->columns;
if (ISZERO(factor)) {
for (int l = i; l < v->columns; l++) {
v->data[target++] += 0.0;
}
} else {
long source = i * v->columns;
for (int l = i; l < v->columns; l++) {
v->data[target++] += factor * v->data[source++];
}
}
}
}
return sign;
}
static int vectorlib_aux_determinant(lua_State *L, int singular, double *d, double epsilon)
{
vector a = vectorlib_aux_get(L, 1);
(void) singular;
if (a && a->rows == a->columns) {
if (a->type == identity_type) {
*d = 1.0;
return 1;
} else {
switch (a->columns) {
case 1:
/* { { a } } : a */
{
*d =
a->data[0];
}
break;
case 2:
/* { { a, b }, { c, d } } : ad - bc */
{
*d =
a->data[0] * a->data[3] -
a->data[1] * a->data[2];
}
break;
case 3:
/* {{ a, b, c }, { d, e, f }. { g, g, i } } : a(ei-fh) - b(di-gf) + c(dh-eg) */
/* see gems article about pos / neg separation, appendix v2.5 */
{
*d =
a->data[0] * (a->data[4] * a->data[8] - a->data[5] * a->data[7]) -
a->data[1] * (a->data[3] * a->data[8] - a->data[6] * a->data[5]) +
a->data[2] * (a->data[3] * a->data[7] - a->data[4] * a->data[6]);
}
break;
default:
vectorlib_aux_copy(L, 1);
{
vector a = vectorlib_aux_get(L, -1);
double epsilon = lmt_optdouble(L, 2, vector_epsilon);
int sign = vectorlib_aux_uppertriangle(a, 1, epsilon);
*d = 1.0;
for (int i = 0; i < a->rows; i++) {
double dd = a->data[i * a->columns + i];
if (ISZERO(dd)) {
/*tex
This is also an attempt to avoid the -0.0 case. But even this
doesn't catches all it seems.
*/
*d = 0.0;
goto DONE;
} else {
*d = *d * a->data[i * a->columns + i];
}
}
*d = sign * *d;
}
break;
}
/*tex
These -0.0 result in random tiny number, is this a compiler issue? The next test doesn't
always catch is. We also have this issue in \METAPOST\ double mode. A |-d == 0.0| is more
reliable than a |d == -0.0| test it seems.
*/
// if (-d == 0.0) {
// d = 0.0;
// }
DONE:
if (ISZERO(*d)) {
*d = 0.0;
}
return 1;
}
} else {
return 0;
}
}
static int vectorlib_determinant(lua_State *L)
{
double determinant = 0.0;
double epsilon = lmt_optdouble(L, 2, vector_epsilon);
if (vectorlib_aux_determinant(L, 0, &determinant, epsilon)) {
if (ISZERO(determinant)) {
lua_pushinteger(L, 0);
} else {
lua_pushnumber(L, determinant);
}
} else {
lua_pushnil(L);
}
return 1;
}
static int vectorlib_issingular(lua_State *L)
{
double d = 0.0;
double epsilon = lmt_optdouble(L, 2, vector_epsilon);
if (vectorlib_aux_determinant(L, 0, &d, epsilon)) {
double e = lua_type(L, 2) == LUA_TNUMBER ? lua_tonumber(L, 2) : 0.001;
lua_pushboolean(L, d <= e || d >= -e);
} else {
lua_pushnil(L);
}
return 1;
}
static int vectorlib_aux_rowechelon(vector v, int reduce, int augmented, double epsilon)
{
int pRow = 0;
int pCol = 0;
int nRow = v->rows;
int nCol = v->columns;
while (pRow < nRow) {
double pivot = v->data[pRow * nCol + pCol];
if (ISZERO(pivot)) {
int i = pRow;
int n = nRow;
while (ISZERO(v->data[i * nCol + pCol])) {
i++;
if (i > n) {
pCol = pCol + 1;
if (pCol >= nCol) {
if (augmented) {
/* we have a serious problem */
}
return 1;
} else {
i = pRow;
}
}
}
for (int k = 0; k < v->columns; k++) {
double d1 = v->data[pRow * v->columns + k];
double d2 = v->data[i * v->columns + k];
v->data[pRow * v->columns + k] = d2;
v->data[i * v->columns + k] = d1;
}
}
pivot = v->data[pRow * nCol + pCol];
for (int l = pCol; l < nCol; l++) {
v->data[pRow * nCol + l] /= pivot;
}
if (reduce) {
for (int k = 0; k < pRow; k++) {
double factor = - v->data[k * nCol + pCol];
for (int l = pCol; l < nCol; l++) {
v->data[k * nCol + l] += factor * v->data[pRow * nCol + l];
}
}
}
for (int k = pRow + 1; k < nRow; k++) {
double factor = - v->data[k * nCol + pCol];
for (int l = pCol; l < nCol; l++) { // + 1
v->data[k * nCol + l] += factor * v->data[pRow * nCol + l];
}
}
pRow = pRow + 1;
pCol = pCol + 1;
if (pRow > nRow || pCol > nCol) {
pRow = nRow + 1;
}
}
return 0;
}
static int vectorlib_inverse(lua_State *L)
{
vector a = vectorlib_aux_get(L, 1);
if (a) {
switch (a->type) {
case identity_type:
vectorlib_aux_copy(L, 1);
break;
default:
if (a->rows == a->columns) {
double epsilon = lmt_optdouble(L, 2, vector_epsilon);
switch (a->rows) {
case 1:
{
vector w = vectorlib_aux_push(L, a->rows, a->columns, a->stacking);
if (ISZERO(a->data[0])) {
goto BAD;
} else {
w->data[0] = 1.0 / a->data[0];
}
}
break;
case 2:
{
double d = 0.0;
if (vectorlib_aux_determinant(L, 0, &d, epsilon)) {
if (ISZERO(d)) {
goto BAD;
} else {
vector w = vectorlib_aux_push(L, a->rows, a->columns, a->stacking);
w->data[0] = a->data[3] / d;
w->data[1] = - a->data[1] / d;
w->data[2] = - a->data[2] / d;
w->data[3] = a->data[0] / d;
}
}
}
break;
case 3:
{
double d = 0.0;
if (vectorlib_aux_determinant(L, 0, &d, epsilon)) {
if (ISZERO(d)) {
goto BAD;
} else {
vector w = vectorlib_aux_push(L, a->rows, a->columns, a->stacking);
w->data[0] = (a->data[4] * a->data[8] - a->data[5] * a->data[7]) / d;
w->data[1] = (a->data[2] * a->data[7] - a->data[1] * a->data[8]) / d;
w->data[2] = (a->data[1] * a->data[5] - a->data[2] * a->data[4]) / d;
w->data[3] = (a->data[5] * a->data[6] - a->data[3] * a->data[8]) / d;
w->data[4] = (a->data[0] * a->data[8] - a->data[2] * a->data[6]) / d;
w->data[5] = (a->data[2] * a->data[3] - a->data[0] * a->data[5]) / d;
w->data[6] = (a->data[3] * a->data[7] - a->data[4] * a->data[6]) / d;
w->data[7] = (a->data[1] * a->data[6] - a->data[0] * a->data[7]) / d;
w->data[8] = (a->data[0] * a->data[4] - a->data[1] * a->data[3]) / d;
}
}
}
break;
default:
{
// vectorlib_copy(L);
// vectorlib_identity(L);
// vectorlib_concat(L);
vector v = vectorlib_aux_push(L, a->rows, a->columns * 2, a->stacking);
vector w = vectorlib_aux_push(L, a->rows, a->columns, a->stacking);
for (int r = 0; r < a->rows; r++) {
long target = r * v->columns;
long source = r * a->columns;
for (int c = 0; c < a->columns; c++) {
v->data[target++] = a->data[source++];
}
// v->data[r * v->columns + a->columns + r] = 1.0;
v->data[target + r] = 1.0;
}
vectorlib_aux_rowechelon(v, 1, 1, epsilon);
for (int r = 0; r < a->rows; r++) {
if (ISZERO(v->data[r * v->columns + r])) {
goto BAD;
}
}
for (int r = 0; r < a->rows; r++) {
long target = r * w->columns;
long source = r * v->columns + a->columns;
for (int c = 0; c < a->columns; c++) {
w->data[target++] = v->data[source++];
}
}
}
break;
}
}
}
return 1;
}
BAD:
lua_pushnil(L);
return 1;
}
static int vectorlib_rowechelon(lua_State *L)
{
vector a = vectorlib_aux_get(L, 1);
int reduce = ! lua_toboolean(L, 2);
if (a && a->rows == a->columns) {
double epsilon = lmt_optdouble(L, 3, vector_epsilon); /* now, as we push */
vectorlib_aux_copy(L, 1);
switch (a->type) {
case identity_type:
break;
default:
{
a = vectorlib_aux_get(L, -1);
vectorlib_aux_rowechelon(a, reduce, 0, epsilon);
break;
}
}
} else {
lua_pushnil(L);
}
return 1;
}
static int vectorlib_crossproduct(lua_State *L)
{
vector a = vectorlib_aux_get(L, 1);
vector b = vectorlib_aux_get(L, 2);
if (a && b) {
if (a->columns == b->columns && a->rows == b->rows && (b->columns == 1 || b->rows == 1)) {
switch (a->rows) {
case 1:
switch (a->columns) {
case 1:
lua_pushinteger(L, 0);
return 1;
case 2:
lua_pushnumber(L, a->data[0] * b->data[1] - b->data[1] * a->data[0]);
return 1;
case 3:
/*u \times v = (u2*v3 - u3*v2, u3*v1 - u1*v3, u1*v2 - u2*v1) */
{
vector v = vectorlib_aux_push(L, 1, a->columns, a->stacking);
v->data[0] = a->data[1] * b->data[2] - a->data[2] * b->data[1];
v->data[1] = a->data[2] * b->data[0] - a->data[0] * b->data[2];
v->data[2] = a->data[0] * b->data[1] - a->data[1] * b->data[0];
return 1;
}
default:
break; /* maybe todo */
}
break;
case 2:
/* u \times v = u1*v2 - u2*v1 */
lua_pushnumber(L, a->data[0] * b->data[1] - b->data[1] * a->data[0]);
return 1;
case 3:
/*u \times v = (u2*v3 - u3*v2, u3*v1 - u1*v3, u1*v2 - u2*v1) */
{
vector v = vectorlib_aux_push(L, a->rows, 1, a->stacking);
v->data[0] = a->data[1] * b->data[2] - a->data[2] * b->data[1];
v->data[1] = a->data[2] * b->data[0] - a->data[0] * b->data[2];
v->data[2] = a->data[0] * b->data[1] - a->data[1] * b->data[0];
return 1;
}
default:
break; /* maybe todo */
}
}
}
lua_pushnil(L);
return 1;
}
static int vectorlib_normalize(lua_State *L)
{
vector a = vectorlib_aux_get(L, 1);
if (a) {
double d = 0.0;
for (int i = 0; i < a->rows * a->columns; i++) {
d += a->data[i] * a->data[i];
}
if (d > 0.0) {
vector v = vectorlib_aux_push(L, a->rows, a->columns, a->stacking);
d = sqrt(d);
for (int i = 0; i < a->rows * a->columns; i++) {
v->data[i] = a->data[i] / d;
}
return 1;
}
}
lua_pushnil(L);
return 1;
}
static int vectorlib_homogenize(lua_State *L)
{
vector a = vectorlib_aux_get(L, 1);
if (a && a->columns > 1) {
double epsilon = lmt_optdouble(L, 2, vector_epsilon);
if (! vectorlib_aux_zero_in_column(a, a->columns - 1, epsilon)) {
vector v = vectorlib_aux_push(L, a->rows, a->columns, a->stacking);
for (int r = 0; r < a->rows; r++) {
double d = a->data[r * a->columns + a->columns - 1];
long target = r * a->columns;
long source = r * a->columns;
for (int c = 0; c < a->columns - 1; c++) {
v->data[target++] = a->data[source++] / d;
}
v->data[target] = 1.0;
}
return 1;
}
}
lua_pushnil(L);
return 1;
}
static int vectorlib_concat(lua_State *L)
{
vector a = vectorlib_aux_get(L, 1);
vector b = vectorlib_aux_get(L, 2);
int vertical = lua_toboolean(L, 3);
if (a && b) {
if (vertical) {
if (b->columns == a->columns) {
vector v = vectorlib_aux_push(L, a->rows + b->rows, a->columns, a->stacking);
long index = a->rows * a->columns;
vectorlib_aux_copy_data(v, a);
for (int i = 0; i < b->rows * b->columns; i++) {
v->data[index++] = b->data[i];
}
return 1;
}
} else {
if (b->rows == a->rows) {
vector v = vectorlib_aux_push(L, a->rows, a->columns + b->columns, a->stacking);
for (int r = 0; r < a->rows; r++) {
long target = r * v->columns;
long source = r * a->columns;
for (int c = 0; c < a->columns; c++) {
v->data[target++] = a->data[source++];
}
}
for (int r = 0; r < b->rows; r++) {
long target = r * v->columns + a->columns;
long source = r * b->columns;
for (int c = 0; c < b->columns; c++) {
v->data[target++ ] = b->data[source++];
}
}
return 1;
}
}
}
lua_pushnil(L);
return 1;
}
static int vectorlib_slice(lua_State *L)
{
vector a = vectorlib_aux_get(L, 1);
if (a) {
int fr = lmt_tointeger(L, 2);
int fc = lmt_tointeger(L, 3);
int nr = lmt_tointeger(L, 4);
int nc = lmt_tointeger(L, 5);
int tr = --fr + nr - 1;
int tc = --fc + nc - 1;
if ( (fr >= 0 && fr < a->rows) && (fc >= 0 && fc < a->columns) &&
(tr >= fr && tr < a->rows) && (tc >= fc && tc < a->columns)) {
vector v = vectorlib_aux_push(L, nr, nc, a->stacking);
long target = 0;
for (int r = fr; r <= tr; r++) {
long source = r * a->columns;
for (int c = fc; c <= tc; c++) {
v->data[target++] = a->data[source++];
}
}
return 1;
}
}
lua_pushnil(L);
return 1;
}
static int vectorlib_replace(lua_State *L)
{
vector a = vectorlib_aux_get(L, 1);
vector b = vectorlib_aux_get(L, 2);
if (a && b) {
int fr = lmt_tointeger(L, 3);
int fc = lmt_tointeger(L, 4);
int nr = b->rows;
int nc = b->columns;
int tr = --fr + nr - 1;
int tc = --fc + nc - 1;
if ( (fr >= 0 && fr < a->rows) && (fc >= 0 && fc < a->columns) &&
(tr >= fr && tr < a->rows) && (tc >= fc && tc < a->columns)) {
vector v = vectorlib_aux_push(L, a->rows, a->columns, a->stacking);
long source = 0;
vectorlib_aux_copy_data(v, a);
for (int r = fr; r <= tr; r++) {
long target = r * v->columns + fc;
for (int c = fc; c <= tc; c++) {
v->data[target++] = b->data[source++];
}
}
return 1;
}
}
lua_pushnil(L);
return 1;
}
static int vectorlib_delete(lua_State *L)
{
vector a = vectorlib_aux_get(L, 1);
if (a) {
int f = lmt_tointeger(L, 2);
int n = lmt_tointeger(L, 3);
int t = --f + n - 1;
int vertical = lua_toboolean(L, 4);
if (vertical) {
if ((n <= a->rows) && (f >= 0 && f < a->rows) && (t >= f && t < a->rows)) {
vector v = vectorlib_aux_push(L, a->rows - n, a->columns, a->stacking);
long target = 0;
for (int r = 0; r < a->rows; r++) {
if (r < f || r > t) {
long source = r * a->columns;
for (int c = 0; c < a->columns; c++) {
v->data[target++] = a->data[source++];
}
}
}
return 1;
}
} else {
if ((n <= a->columns) && (f >= 0 && f < a->columns) && (t >= f && t < a->columns)) {
vector v = vectorlib_aux_push(L, a->rows, a->columns - n, a->stacking);
long target = 0;
for (int r = 0; r < a->rows; r++) {
int base = r * a->columns;
for (int c = 0; c < a->columns; c++) {
if (c < f || c > t) {
v->data[target++] = a->data[base + c];
}
}
}
return 1;
}
}
}
lua_pushnil(L);
return 1;
}
static int vectorlib_remove(lua_State *L)
{
vector a = vectorlib_aux_get(L, 1);
if (a) {
int f = lmt_tointeger(L, 2);
int n = lmt_tointeger(L, 3);
int t = --f + n - 1;
int vertical = lua_toboolean(L, 4);
if (vertical) {
if ((n <= a->rows) && (f >= 0 && f < a->rows) && (t >= f && t < a->rows)) {
vector v = vectorlib_aux_push(L, a->rows - n, a->columns, a->stacking);
vector w = vectorlib_aux_push(L, n, a->columns, a->stacking);
long vtarget = 0;
long wtarget = 0;
for (int r = 0; r < a->rows; r++) {
long source = r * a->columns;
if (r < f || r > t) {
for (int c = 0; c < a->columns; c++) {
v->data[vtarget++] = a->data[source++];
}
} else {
for (int c = 0; c < a->columns; c++) {
w->data[wtarget++] = a->data[source++];
}
}
}
return 2;
}
} else {
if ((n <= a->columns) && (f >= 0 && f < a->columns) && (t >= f && t < a->columns)) {
vector v = vectorlib_aux_push(L, a->rows, a->columns - n, a->stacking);
vector w = vectorlib_aux_push(L, a->rows, n, a->stacking);
long vtarget = 0;
long wtarget = 0;
for (int r = 0; r < a->rows; r++) {
long source = r * a->columns;
for (int c = 0; c < a->columns; c++) {
if (c < f || c > t) {
v->data[vtarget++] = a->data[source++];
} else {
w->data[wtarget++] = a->data[source++];
}
}
}
return 2;
}
}
}
lua_pushnil(L);
lua_pushnil(L);
return 2;
}
static int vectorlib_insert(lua_State *L)
{
vector a = vectorlib_aux_get(L, 1);
vector b = vectorlib_aux_get(L, 2);
if (a && b) {
int p = lmt_tointeger(L, 3);
int vertical = lua_toboolean(L, 4);
if (vertical) {
if (a->columns == b->columns) {
vector v = vectorlib_aux_push(L, a->rows + b->rows, a->columns, a->stacking);
long target = 0;
for (int r = 0; r < p; r++) {
long source = r * a->columns;
for (int c = 0; c < a->columns; c++) {
v->data[target++] = a->data[source++];
}
}
for (int r = 0; r < b->rows; r++) {
long source = r * b->columns;
for (int c = 0; c < b->columns; c++) {
v->data[target++] = b->data[source++];
}
}
for (int r = p; r < a->rows; r++) {
long source = r * a->columns;
for (int c = 0; c < a->columns; c++) {
v->data[target++] = a->data[source++];
}
}
return 1;
}
} else {
if (a->rows == b->rows) {
vector v = vectorlib_aux_push(L, a->rows, a->columns + b->columns, a->stacking);
long target = 0;
for (int r = 0; r < a->rows; r++) {
long source = r * a->columns;
for (int c = 0; c < p; c++) {
v->data[target++] = a->data[source++];
}
source = r * b->columns;
for (int c = 0; c < b->columns; c++) {
v->data[target++] = b->data[source++];
}
source = r * a->columns + p;
for (int c = p; c < a->columns; c++) {
v->data[target++] = a->data[source++];
}
}
return 1;
}
}
}
lua_pushnil(L);
return 1;
}
static int vectorlib_append(lua_State *L)
{
vector a = vectorlib_aux_get(L, 1);
if (a) {
double d = lua_tonumber(L, 2);
int vertical = lua_toboolean(L, 3);
if (vertical) {
int row = a->rows + 1;
vector v = vectorlib_aux_push(L, row, a->columns, a->stacking);
vectorlib_aux_copy_data(v, a);
for (int c = 0; c < a->columns; c++) {
v->data[row + c] = d;
}
return 1;
} else {
vector v = vectorlib_aux_push(L, a->rows, a->columns + 1, a->stacking);
long target = 0;
long source = 0;
for (int r = 0; r < a->rows; r++) {
for (int c = 0; c < a->columns; c++) {
v->data[target++] = a->data[source++];
}
v->data[target++] = d;
}
return 1;
}
}
lua_pushnil(L);
return 1;
}
static int vectorlib_exchange(lua_State *L)
{
vector a = vectorlib_aux_get(L, 1);
if (a) {
int p = lmt_tointeger(L, 2) - 1;
int q = lmt_tointeger(L, 3) - 1;
int vertical = lua_toboolean(L, 4);
if (vertical) {
if (p >= 0 && p < a->rows && q >= 0 && q < a->rows) {
vector v = vectorlib_aux_push(L, a->rows , a->columns, a->stacking);
vectorlib_aux_copy_data(v, a);
p *= a->columns;
q *= a->columns;
for (int c = 0; c < a->columns; c++) {
v->data[p] = a->data[q];
v->data[q] = a->data[p];
p++;
q++;
}
return 1;
}
} else {
if (p >= 0 && p < a->columns && q >= 0 && q < a->columns) {
vector v = vectorlib_aux_push(L, a->rows , a->columns, a->stacking);
vectorlib_aux_copy_data(v, a);
for (int i = 0; i < a->rows * a->columns; i++) {
v->data[i] = a->data[i];
}
for (int r = 0; r < a->rows; r++) {
v->data[p] = a->data[q];
v->data[q] = a->data[p];
p += a->columns;
q += a->columns;
}
return 1;
}
}
}
lua_pushnil(L);
return 1;
}
static int vectorlib_swap(lua_State *L)
{
vector a = vectorlib_aux_get(L, 1);
if (a && (a->columns == 1 || a->rows == 1)) {
vector v = vectorlib_aux_push(L, a->rows, a->columns, a->stacking);
v->rows = a->columns;
v->columns = a->rows;
vectorlib_aux_copy_data(v, a);
} else {
lua_pushnil(L);
}
return 1;
}
static int vectorlib_gettype(lua_State *L)
{
vector v = vectorlib_aux_get(L, 1);
if (v) {
lua_pushinteger(L, v->type);
} else {
lua_pushnil(L);
}
return 1;
}
static int vectorlib_getdimensions(lua_State *L)
{
vector v = vectorlib_aux_get(L, 1);
if (v) {
lua_pushinteger(L, v->rows);
lua_pushinteger(L, v->columns);
return 2;
} else {
return 0;
}
}
static int vectorlib_setstacking(lua_State *L)
{
vector a = vectorlib_aux_get(L, 1);
if (a) {
a->stacking = lmt_tointeger(L, 2);
}
return 0;
}
static int vectorlib_getstacking(lua_State *L)
{
vector a = vectorlib_aux_get(L, 1);
if (a) {
lua_pushinteger(L, a->stacking);
} else {
lua_pushnil(L);
}
return 1;
}
/*
Meshes:
These we use for some experiments with alternative approaches to contour graphics in the
\LUAMETAFUN\ subsystem. They are for now not official.
*/
static inline unsigned short vectorlib_valid_point(lua_State *L, int index)
{
int p = lmt_tointeger(L, index);
return p < 0 ? 0 : p > max_mesh ? max_mesh : p;
}
static mesh vectorlib_aux_maybe_ismesh(lua_State *L, int index)
{
mesh m = lua_touserdata(L, index);
if (m && lua_getmetatable(L, index)) {
lua_get_metatablelua(mesh_instance);
if (! lua_rawequal(L, -1, -2)) {
m = NULL;
}
lua_pop(L, 2);
}
return m;
}
static int vectorlib_ismesh(lua_State *L)
{
lua_pushboolean(L, vectorlib_aux_maybe_ismesh(L, 1) ? 1 : 0);
return 1;
}
static int vectorlib_mesh_type(lua_State *L)
{
if (vectorlib_aux_maybe_ismesh(L, 1)) {
lua_push_key(mesh);
} else {
lua_pushnil(L);
}
return 1;
}
static inline void vectorlib_mesh_wipe_entry(meshentry *entry)
{
for (int i = 0; i < 4; i++) {
entry->points[i] = 0;
}
entry->average = 0.0;
}
static inline mesh vectorlib_mesh_aux_push(lua_State *L, int r)
{
mesh m = lua_newuserdatauv(L, 2 * sizeof(int) + r * sizeof(meshentry), 0);
if (m && r > 0) {
m->rows = r;
m->type = triangle_mesh_type;
lua_get_metatablelua(mesh_instance);
lua_setmetatable(L, -2);
}
return m;
}
static int vectorlib_mesh_new(lua_State *L)
{
switch (lua_type(L, 1)) {
case LUA_TNUMBER:
{
mesh m = vectorlib_mesh_aux_push(L, lmt_optinteger(L, 1, 1));
for (int i = 0; i < m->rows; i++) {
vectorlib_mesh_wipe_entry(&(m->data[i]));
}
return 1;
}
case LUA_TTABLE:
{
int t = lua_rawgeti(L, 1, 1);
lua_pop(L, 1);
switch (t) {
case LUA_TNUMBER:
/* local v = vector.new( { 1,2,3 }, { 4,5,6 } ) */ /* n can determine quads */
{
int rows = lua_gettop(L);
if (rows) {
mesh m = vectorlib_mesh_aux_push(L, rows);
for (int i = 0; i < rows; i++) {
meshentry *entry = &(m->data[i]);
if (lua_type(L, i + 1) == LUA_TTABLE) {
for (int j = 0; j < 4; j++) {
entry->points[j] = lua_rawgeti(L, i + 1, j + 1) == LUA_TNUMBER ? vectorlib_valid_point(L, -1) : 0;
lua_pop(L, 1);
}
entry->average = 0.0;
} else {
vectorlib_mesh_wipe_entry(entry);
}
}
return 1;
} else {
break;
}
}
case LUA_TTABLE:
{
/* local v = vector.new( { { 1,2,3 }, { 4,5,6 } } ) */
int rows = (int) lua_rawlen(L, 1);
if (rows) {
mesh m = vectorlib_mesh_aux_push(L, rows);
for (int i = 0; i < rows; i++) {
meshentry *entry = &(m->data[i]);
if (lua_rawgeti(L, 1, i + 1) == LUA_TTABLE) {
for (int j = 0; j < 4; j++) {
entry->points[j] = lua_rawgeti(L, -1, j + 1) == LUA_TNUMBER ? vectorlib_valid_point(L, -1) : 0;
lua_pop(L, 1);
}
entry->average = 0.0;
} else {
vectorlib_mesh_wipe_entry(entry);
}
lua_pop(L, 1);
}
return 1;
} else {
break;
}
}
default:
break;
}
break;
}
}
lua_pushnil(L);
return 0;
}
inline static mesh vectorlib_mesh_aux_get(lua_State *L, int index)
{
mesh m = lua_touserdata(L, index);
if (m && lua_getmetatable(L, index)) {
lua_get_metatablelua(mesh_instance);
if (! lua_rawequal(L, -1, -2)) {
m = NULL;
}
lua_pop(L, 2);
}
return m;
}
mesh vectorlib_get_mesh(lua_State *L, int index)
{
if (lua_type(L, index) == LUA_TUSERDATA) {
return (mesh) luaL_checkudata(L, index, MESH_METATABLE);
} else {
return NULL;
}
}
static int vectorlib_mesh_tostring(lua_State *L)
{
mesh v = vectorlib_mesh_aux_get(L, 1);
if (v) {
lua_pushfstring(L, "<mesh %d %s : %p>", v->rows, mesh_names[valid_mesh(v->type)], v);
return 1;
} else {
return 0;
}
}
static int vectorlib_mesh_totable(lua_State *L)
{
mesh m = vectorlib_aux_maybe_ismesh(L, 1);
if (m) {
long target = 1;
int size = m->type == quad_mesh_type ? 4 : 3;
if (lua_toboolean(L, 2)) {
lua_createtable(L, m->rows * (size + 1), 0);
for (int r = 0; r < m->rows; r++) {
meshentry *triangle = &(m->data[r]);
for (int i = 0; i < size; i++) {
lua_pushinteger(L, triangle->points[i]);
lua_rawseti(L, -2, target++);
}
lua_pushnumber(L, triangle->average);
lua_rawseti(L, -2, target++);
}
} else {
lua_createtable(L, m->rows, 0);
for (int r = 0; r < m->rows; r++) {
meshentry *triangle = &(m->data[r]);
long index = 1;
lua_createtable(L, size + 1, 0);
for (int i = 0; i < size; i++) {
lua_pushinteger(L, triangle->points[i]);
lua_rawseti(L, -2, index++);
}
lua_pushnumber(L, triangle->average);
lua_rawseti(L, -2, index);
lua_rawseti(L, -2, target++);
}
}
return 1;
} else {
return 0;
}
}
static int vectorlib_mesh_index(lua_State *L)
{
mesh m = vectorlib_mesh_aux_get(L, 1);
if (m) {
int index = lmt_tointeger(L, 2);
if (index > 0 && index <= m->rows) {
lua_pushnumber(L, m->data[index - 1].average);
return 1;
}
}
return 0;
}
static int vectorlib_mesh_getvalue(lua_State *L)
{
mesh m = vectorlib_mesh_aux_get(L, 1);
if (m) {
int index = lmt_tointeger(L, 2);
if (index > 0 && index <= m->rows) {
meshentry *triangle = &(m->data[index-1]);
int size = m->type == quad_mesh_type ? 4 : 3;
if (lua_toboolean(L, 3)) {
long target = 1;
lua_createtable(L, size + 1, 0);
for (int i = 0; i < size; i++) {
lua_pushinteger(L, triangle->points[i]);
lua_rawseti(L, -2, target++);
}
lua_pushnumber(L, triangle->average);
lua_rawseti(L, -2, target);
return 1;
} else {
for (int i = 0; i < size; i++) {
lua_pushinteger(L, triangle->points[i]);
}
lua_pushnumber(L, triangle->average);
return size + 1;
}
}
}
return 0;
}
static int vectorlib_mesh_setvalue(lua_State *L)
{
mesh m = vectorlib_mesh_aux_get(L, 1);
if (m) {
int index = lmt_tointeger(L, 2);
if (index > 0 && index <= m->rows) {
meshentry *triangle = &(m->data[index-1]);
switch (lua_type(L, 3)) {
case LUA_TNUMBER:
{
triangle->average = lua_tonumber(L, 3);
break;
}
case LUA_TTABLE:
{
int size = m->type == quad_mesh_type ? 4 : 3;
for (int i = 1; i <= size; i++) {
if (lua_rawgeti(L, 3, i) == LUA_TNUMBER) {
if (i < size) {
triangle->points[i-1] = vectorlib_valid_point(L, -1);
} else {
triangle->average = lua_tonumber(L, -1);
}
}
lua_pop(L, 1);
}
break;
}
}
}
}
return 0;
}
static int vectorlib_mesh_getlength(lua_State *L)
{
mesh m = vectorlib_mesh_aux_get(L, 1);
lua_pushinteger(L, m ? m->rows : 0);
return 1;
}
static int vectorlib_mesh_getdimensions(lua_State *L)
{
mesh m = vectorlib_mesh_aux_get(L, 1);
if (m) {
lua_pushinteger(L, m->rows);
} else {
lua_pushnil(L);
}
return 1;
}
/*
Contours:
These we use for some experiments with alternative approaches to contour graphics in the
\LUAMETAFUN\ subsystem. They are for now not official.
*/
static int vectorlib_contour_aux_okay(int condition, const char *where, const char *message)
{
if (! condition) {
tex_formatted_error("vector lib", "error in vector.%s: %s\n", where, message);
}
return condition;
}
static int vectorlib_contour_aux_is_valid(lua_State *L, vector *v, mesh *l, const char *what)
{
*v = vectorlib_get(L, 1);
if (vectorlib_contour_aux_okay(*v && (*v)->rows > 1 && (*v)->columns > 2, what, "point list expected (x,y,z,...)")) {
*l = vectorlib_get_mesh(L, 2);
if (vectorlib_contour_aux_okay(*l && valid_mesh((*l)->type), what, "mesh list expected ((p1 .. pN),average)")) {
return 1;
}
}
return 0;
}
static int vectorlib_contour_getmesh(lua_State *L)
{
vector v = NULL;
mesh l = NULL;
if (vectorlib_contour_aux_is_valid(L, &v, &l, "getmesh")) {
int i = lmt_tointeger(L, 3) - 1; /* triangle index */
if (i >= 0 && i < l->rows) {
int done = 0;
int okay = 0;
int size = valid_mesh(l->type);
meshentry *triangle = &(l->data[i]);
for (int j = 0; j < size; j++) {
int r = triangle->points[j] - 1;
if (r >= 0 && r < v->rows) {
int k = r * v->columns;
if (! okay) {
lua_createtable(L, 2 * size, 0);
okay = 1;
}
lua_pushnumber(L, v->data[k++]);
lua_rawseti(L, -2, ++done);
lua_pushnumber(L, v->data[k]);
lua_rawseti(L, -2, ++done);
} else {
break; /* error */
}
}
if (okay) {
lua_pushnumber(L, triangle->average);
return 2;
} else {
/*tex We signal that we have a zero entry. */
lua_pushboolean(L, 0);
return 1;
}
}
}
return 0;
}
static int vectorlib_contour_getarea(lua_State *L)
{
vector v = NULL;
mesh l = NULL;
if (vectorlib_contour_aux_is_valid(L, &v, &l, "getarea")) {
int i = lmt_tointeger(L, 3) - 1; /* triangle index */
if (i >= 0 && i < l->rows) {
meshentry *entry = &(l->data[i]);
switch (l->type) {
case line_mesh_type:
lua_pushnumber(L, 0.0);
return 1;
case quad_mesh_type:
{
int p1 = entry->points[0] - 1;
int p3 = entry->points[2] - 1;
if (p1 >= 0 && p1 < v->rows && p3 >= 0 && p3 < v->rows) {
double x1 = v->data[p1 * v->columns];
double y1 = v->data[p1 * v->columns + 1];
double x3 = v->data[p3 * v->columns];
double y3 = v->data[p3 * v->columns + 1];
lua_pushnumber(L, fabs(x3 - x1) * fabs(y3 - y1));
return 1;
} else {
break;
}
}
case triangle_mesh_type: /* from wikipedia */
{
int p1 = entry->points[0] - 1;
int p2 = entry->points[1] - 1;
int p3 = entry->points[2] - 1;
if (p1 >= 0 && p1 < v->rows && p2 >= 0 && p2 < v->rows && p3 >= 0 && p3 < v->rows) {
double x1 = v->data[p1 * v->columns];
double y1 = v->data[p1 * v->columns + 1];
double x2 = v->data[p2 * v->columns];
double y2 = v->data[p2 * v->columns + 1];
double x3 = v->data[p3 * v->columns];
double y3 = v->data[p3 * v->columns + 1];
double ab = sqrt(pow(x1 - x2, 2) + pow(y1 - y2, 2));
double bc = sqrt(pow(x2 - x3, 2) + pow(y2 - y3, 2));
double ca = sqrt(pow(x3 - x1, 2) + pow(y3 - y1, 2));
lua_pushnumber(L, sqrt (
( ab + bc + ca) *
(- ab + bc + ca) *
( ab - bc + ca) *
( ab + bc - ca)
) / 4);
return 1;
} else {
break;
}
}
}
}
}
return 0;
}
/* see below, todo: optimize */
static int vectorlib_contour_aux_checkoverlap(vector v, mesh l, int U, int V, int method, double epsilon)
{
int u0 = l->data[U].points[0] - 1;
int u1 = l->data[U].points[1] - 1;
int u2 = l->data[U].points[2] - 1;
int v0 = l->data[V].points[0] - 1;
int v1 = l->data[V].points[1] - 1;
int v2 = l->data[V].points[2] - 1;
if (
u0 >= 0 && u1 >= 0 && u2 >= 0 &&
v0 >= 0 && v1 >= 0 && v2 >= 0 &&
u0 < v->rows && u1 < v->rows && u2 < v->rows &&
v0 < v->rows && v1 < v->rows && v2 < v->rows
) {
if (
u0 == v0 || u1 == v0 || u2 == v0 ||
u0 == v1 || u1 == v1 || u2 == v1 ||
u0 == v2 || u1 == v2 || u2 == v2
) {
return triangles_intersection_nop_same_points;
} else {
double *U0 = &(v->data[u0 * v->columns]);
double *U1 = &(v->data[u1 * v->columns]);
double *U2 = &(v->data[u2 * v->columns]);
double *V0 = &(v->data[v0 * v->columns]);
double *V1 = &(v->data[v1 * v->columns]);
double *V2 = &(v->data[v2 * v->columns]);
if (
/* we can't memcmp due to small differences (epsilon) */
(ISZERO(U0[0] - V0[0]) && ISZERO(U0[1] - V0[1]) && ISZERO(U0[2] - V0[2])) ||
(ISZERO(U1[0] - V0[0]) && ISZERO(U1[1] - V0[1]) && ISZERO(U1[2] - V0[2])) ||
(ISZERO(U2[0] - V0[0]) && ISZERO(U2[1] - V0[1]) && ISZERO(U2[2] - V0[2])) ||
(ISZERO(U0[0] - V1[0]) && ISZERO(U0[1] - V1[1]) && ISZERO(U0[2] - V1[2])) ||
(ISZERO(U1[0] - V1[0]) && ISZERO(U1[1] - V1[1]) && ISZERO(U1[2] - V1[2])) ||
(ISZERO(U2[0] - V1[0]) && ISZERO(U2[1] - V1[1]) && ISZERO(U2[2] - V1[2])) ||
(ISZERO(U0[0] - V2[0]) && ISZERO(U0[1] - V2[1]) && ISZERO(U0[2] - V2[2])) ||
(ISZERO(U1[0] - V2[0]) && ISZERO(U1[1] - V2[1]) && ISZERO(U1[2] - V2[2])) ||
(ISZERO(U2[0] - V2[0]) && ISZERO(U2[1] - V2[1]) && ISZERO(U2[2] - V2[2]))
) {
return triangles_intersection_nop_same_values;
} else if (method == 2) {
return triangles_intersect_gd(U0, U1, U2, V0, V1, V2, epsilon);
} else {
return triangles_intersect(U0, U1, U2, V0, V1, V2, epsilon);
}
}
}
return -1;
}
static int vectorlib_contour_checkoverlap(lua_State *L)
{
vector v = NULL;
mesh l = NULL;
if (vectorlib_contour_aux_is_valid(L, &v, &l, "checkoverlap")) {
if (l->type == triangle_mesh_type) {
int U = lmt_tointeger(L, 3) - 1;
int V = lmt_tointeger(L, 4) - 1;
if (U >= 0 && U < l->rows && V >= 0 && V < l->rows) {
int method = lmt_optinteger(L, 5, 1);
double epsilon = lmt_optdouble(L, 6, vector_epsilon);
int overlapping = vectorlib_contour_aux_checkoverlap(v, l, U, V, method, epsilon);
if (overlapping >= 0) {
lua_pushinteger(L, overlapping);
return 1;
}
}
} else {
/* we only check triangles */
}
}
return 0;
}
static int vectorlib_contour_collectoverlap(lua_State *L)
{
vector v = NULL;
mesh l = NULL;
if (vectorlib_contour_aux_is_valid(L, &v, &l, "collectoverlap")) {
if (l->type == triangle_mesh_type) {
int details = lua_toboolean(L, 3);
int method = lmt_optinteger(L, 4, 1);
double epsilon = lmt_optdouble(L, 5, vector_epsilon);
int vr = v->rows;
int vc = v->columns;
int lr = l->rows;
int m = 0;
for (int i = 0; i < lr; i++) {
meshentry *triangle = &(l->data[i]);
int u0 = triangle->points[0];
int u1 = triangle->points[1];
int u2 = triangle->points[2];
if (
u0 >= 1 && u1 >= 1 && u2 >= 1 &&
u0 <= vr && u1 <= vr && u2 <= vr
) {
} else {
vectorlib_contour_aux_okay(0, "collectoverlap", "triangle list entries has invalid point references");
return 0;
}
}
for (int i = 0; i < lr - 1; i++) {
meshentry *triangle = &(l->data[i]);
int u0 = triangle->points[0] - 1;
int u1 = triangle->points[1] - 1;
int u2 = triangle->points[2] - 1;
double *U0 = &(v->data[u0 * vc]);
double *U1 = &(v->data[u1 * vc]);
double *U2 = &(v->data[u2 * vc]);
for (int j = i + 1; j < lr; j++) {
meshentry *triangle = &(l->data[j]);
int v0 = triangle->points[0] - 1;
if (
u0 == v0 || u1 == v0 || u2 == v0
) {
continue; /* triangles_intersection_nop_same_points */
}
int v1 = triangle->points[1] - 1;
if (
u0 == v1 || u1 == v1 || u2 == v1
) {
continue; /* triangles_intersection_nop_same_points */
}
int v2 = triangle->points[2] - 1;
if (
u0 == v2 || u1 == v2 || u2 == v2
) {
continue; /* triangles_intersection_nop_same_points */
}
double *V0 = &(v->data[v0 * vc]);
if (
(ISZERO(U0[0] - V0[0]) && ISZERO(U0[1] - V0[1]) && ISZERO(U0[2] - V0[2])) ||
(ISZERO(U1[0] - V0[0]) && ISZERO(U1[1] - V0[1]) && ISZERO(U1[2] - V0[2])) ||
(ISZERO(U2[0] - V0[0]) && ISZERO(U2[1] - V0[1]) && ISZERO(U2[2] - V0[2]))
) {
continue; /* triangles_intersection_nop_same_values */
}
double *V1 = &(v->data[v1 * vc]);
if (
(ISZERO(U0[0] - V1[0]) && ISZERO(U0[1] - V1[1]) && ISZERO(U0[2] - V1[2])) ||
(ISZERO(U1[0] - V1[0]) && ISZERO(U1[1] - V1[1]) && ISZERO(U1[2] - V1[2])) ||
(ISZERO(U2[0] - V1[0]) && ISZERO(U2[1] - V1[1]) && ISZERO(U2[2] - V1[2]))
) {
continue; /* triangles_intersection_nop_same_values */
}
double *V2 = &(v->data[v2 * vc]);
if (
(ISZERO(U0[0] - V2[0]) && ISZERO(U0[1] - V2[1]) && ISZERO(U0[2] - V2[2])) ||
(ISZERO(U1[0] - V2[0]) && ISZERO(U1[1] - V2[1]) && ISZERO(U1[2] - V2[2])) ||
(ISZERO(U2[0] - V2[0]) && ISZERO(U2[1] - V2[1]) && ISZERO(U2[2] - V2[2]))
) {
continue; /* triangles_intersection_nop_same_values */
}
if (details) {
triangles_three Utimes, Vtimes;
int state = method == 2
? triangles_intersect_with_line_gd(U0, U1, U2, V0, V1, V2, Utimes, Vtimes, epsilon)
: triangles_intersect_with_line (U0, U1, U2, V0, V1, V2, Utimes, Vtimes, epsilon);
if (state > triangles_intersection_yes_bound) {
long index = 1;
if (! m) {
lua_createtable(L, 8, 0); /* main table, how many makes sense */
}
lua_createtable(L, 8, 0);
// lua_createtable(L, 9, 0);
lua_pushinteger(L, i + 1);
lua_rawseti(L, -2, index++);
lua_pushinteger(L, j + 1);
lua_rawseti(L, -2, index++);
for (int k = 0; k < 3; k++) {
lua_pushnumber(L, Utimes[k]);
lua_rawseti(L, -2, index++);
}
for (int k = 0; k < 3; k++) {
lua_pushnumber(L, Vtimes[k]);
lua_rawseti(L, -2, index++);
}
// lua_pushinteger(L, state);
// lua_rawseti(L, -2, index++);
lua_rawseti(L, -2, ++m);
}
} else {
int state = method == 2
? triangles_intersect_gd(U0, U1, U2, V0, V1, V2, epsilon)
: triangles_intersect (U0, U1, U2, V0, V1, V2, epsilon);
if (state > triangles_intersection_yes_bound) {
if (! m) {
lua_createtable(L, 8, 0);
}
lua_createtable(L, 2, 0);
lua_pushinteger(L, i + 1);
lua_rawseti(L, -2, 1);
lua_pushinteger(L, j + 1);
lua_rawseti(L, -2, 2);
lua_rawseti(L, -2, ++m);
/* maybe also store state */
}
}
}
}
return m ? 1 : 0;
} else {
/* we only check triangles */
}
}
return 0;
}
static int vectorlib_contour_average(lua_State *L)
{
vector v = NULL;
mesh l = NULL;
if (vectorlib_contour_aux_is_valid(L, &v, &l, "average")) {
double minx = 0.0;
double maxx = 0.0;
double miny = 0.0;
double maxy = 0.0;
double minz = 0.0;
double maxz = 0.0;
int okay = 0;
int done = 0;
int size = valid_mesh(l->type);
int tolerant = lua_toboolean(L, 3);
int method = lmt_optinteger(L, 4, 1);
// int first = lmt_optinteger(L, 5, 1);
// int last = lmt_optinteger(L, 6, l->rows);
// if (first <= 0) {
// first = 0;
// } else {
// --first;
// }
// if (last == 0) {
// last = l->rows;
// } else if (last > l->rows) {
// last = l->rows;
// } else {
// --last;
// }
// for (int i = first; i < last; i++) {
for (int i = 0; i < l->rows; i++) {
meshentry *entry = &(l->data[i]);
double average = 0.0;
double amin = 0;
int a = 0;
for (int j = 0; j < size; j++) {
int r = entry->points[j];
if (r > 0 && r <= v->rows) {
int k = (r - 1) * v->columns;
double x = v->data[k++];
double y = v->data[k++];
double z = v->data[k];
if (! a || z < amin) {
amin = z;
}
average += z;
if (done) {
if (x < minx) { minx = x; } else if (x > maxx) { maxx = x; }
if (y < miny) { miny = y; } else if (y > maxy) { maxy = y; }
} else {
minx = x; miny = y;
maxx = x; maxy = y;
done = 1;
}
a++;
} else if (! tolerant) {
tex_formatted_error("vector lib", "error in vector.average, invalid point index");
a = 0;
break;
}
}
if (method == 2) {
average = amin;
} else {
average = a > 0 ? (average / a) : 0.0;
if (method == 3) {
average += amin;
}
}
entry->average = average;
if (okay) {
if (average < minz) { minz = average; }
if (average > maxz) { maxz = average; }
} else {
minz = average;
maxz = average;
}
okay++;
}
lua_createtable(L, 0, 7);
lua_set_integer_by_key(L, "okay", okay);
lua_set_number_by_key (L, "minx", minx);
lua_set_number_by_key (L, "miny", miny);
lua_set_number_by_key (L, "maxx", maxx);
lua_set_number_by_key (L, "maxy", maxy);
lua_set_number_by_key (L, "minz", minz);
lua_set_number_by_key (L, "maxz", maxz);
return 1;
} else {
return 0;
}
}
static int vectorlib_contour_bounds(lua_State *L)
{
vector v = NULL;
mesh l = NULL;
if (vectorlib_contour_aux_is_valid(L, &v, &l, "bounds")) {
double minx = 0.0;
double maxx = 0.0;
double miny = 0.0;
double maxy = 0.0;
int okay = 0;
int size = valid_mesh(l->type);
int first = lmt_optinteger(L, 3, 1);
int last = lmt_optinteger(L, 4, l->rows);
if (first <= 0) {
first = 0;
} else {
--first;
}
if (last == 0) {
last = l->rows;
} else if (last > l->rows) {
last = l->rows;
} else {
--last;
}
for (int i = first; i < last; i++) {
meshentry *entry = &(l->data[i]);
for (int j = 0; j < size; j++) {
int r = entry->points[j];
if (r > 0 && r <= v->rows) {
int k = (r - 1) * v->columns;
double x = v->data[k++];
double y = v->data[k];
if (okay) {
if (x < minx) { minx = x; } else if (x > maxx) { maxx = x; }
if (y < miny) { miny = y; } else if (y > maxy) { maxy = y; }
} else {
minx = x; miny = y;
maxx = x; maxy = y;
}
okay++;
}
}
}
lua_createtable(L, 0, 5);
lua_set_integer_by_key(L, "okay", okay);
lua_set_number_by_key (L, "minx", minx);
lua_set_number_by_key (L, "miny", miny);
lua_set_number_by_key (L, "maxx", maxx);
lua_set_number_by_key (L, "maxy", maxy);
return 1;
}
return 0;
}
int vectorlib_contour_compare_mesh_average(const void *entry_1, const void *entry_2)
{
const meshentry *value_1 = (const meshentry *) entry_1;
const meshentry *value_2 = (const meshentry *) entry_2;
if (value_1->average > value_2->average) {
return 1;
} else if (value_1->average < value_2->average) {
return -1;
} else {
return 0;
}
}
static int vectorlib_contour_sort(lua_State *L)
{
mesh m = vectorlib_get_mesh(L, 1);
if (m && m->rows > 1) {
qsort(m->data, m->rows, sizeof(meshentry), vectorlib_contour_compare_mesh_average);
}
return 0;
}
/* we can combine these two */
static int vectorlib_contour_makemesh(lua_State *L)
{
int columns = lmt_tointeger(L, 1);
int rows = lmt_tointeger(L, 2);
int size = columns * rows;
int type = valid_mesh(lmt_optinteger(L, 3, triangle_mesh_type));
if (size > 0 && size <= max_mesh) {
switch (type) {
case triangle_mesh_type:
size *= 2;
break;
case quad_mesh_type:
break;
case line_mesh_type:
break;
default:
/*tex We silently recover. */
type = triangle_mesh_type;
size *= 2;
break;
}
/*tex When we arrived here we're okay. */
{
mesh m = vectorlib_mesh_aux_push(L, size);
int index = 0;
int offset = columns + 1;
m->type = type;
for (int r = 1; r <= rows; r++) {
for (int c = 1; c <= columns; c++) {
int p1 = (r - 1) * offset + c; // left point of current row
int p2 = r * offset + c; // left point of next row
int p11 = p1 + 1;
int p21 = p2 + 1;
if (p11 > max_mesh || p21 > max_mesh) {
/* we clip */
} else {
switch (type) {
case triangle_mesh_type:
/* first */
m->data[index ].points[0] = (unsigned short) p1;
m->data[index ].points[1] = (unsigned short) p11;
m->data[index ].points[2] = (unsigned short) p21;
m->data[index ].points[3] = 0;
m->data[index++].average = 0.0;
/* second */
m->data[index ].points[0] = (unsigned short) p1;
m->data[index ].points[1] = (unsigned short) p21;
m->data[index ].points[2] = (unsigned short) p2;
m->data[index ].points[3] = 0;
m->data[index++].average = 0.0;
break;
case quad_mesh_type:
m->data[index ].points[0] = (unsigned short) p1;
m->data[index ].points[1] = (unsigned short) p11;
m->data[index ].points[2] = (unsigned short) p21;
m->data[index ].points[3] = (unsigned short) p2;
m->data[index++].average = 0.0;
break;
case line_mesh_type:
m->data[index ].points[0] = (unsigned short) p1;
m->data[index ].points[1] = (unsigned short) p2;
m->data[index ].points[2] = 0;
m->data[index ].points[3] = 0;
m->data[index++].average = 0.0;
break;
}
}
}
}
}
} else {
/* message */
lua_pushnil(L);
}
return 1;
}
/* */
static const luaL_Reg vectorlib_vector_function_list[] =
{
/* management */
{ "new", vectorlib_new },
{ "isvector", vectorlib_isvector },
{ "ismesh", vectorlib_ismesh },
{ "onerow", vectorlib_onerow },
{ "onecolumn", vectorlib_onecolumn },
{ "copy", vectorlib_copy },
{ "type", vectorlib_type },
{ "tostring", vectorlib_tostring },
{ "totable", vectorlib_totable },
{ "get", vectorlib_getvalue },
{ "set", vectorlib_setvalue },
{ "getrow", vectorlib_getrow }, /* table */
{ "getrc", vectorlib_getvaluerc },
{ "setrc", vectorlib_setvaluerc },
{ "setnext", vectorlib_setnext },
/* info */
{ "gettype", vectorlib_gettype },
{ "setstacking", vectorlib_setstacking },
{ "getstacking", vectorlib_getstacking },
{ "getdimensions", vectorlib_getdimensions },
{ "setepsilon", vectorlib_setepsilon },
{ "getepsilon", vectorlib_getepsilon },
{ "iszero", vectorlib_iszero },
/* { "isidentity", vectorlib_isidentity }, */ /* todo */
/* operators */
{ "add", vectorlib_add },
{ "div", vectorlib_div },
{ "mul", vectorlib_mul },
{ "sub", vectorlib_sub },
{ "negate", vectorlib_neg },
{ "equal", vectorlib_eq },
/* functions */
{ "round", vectorlib_round },
{ "floor", vectorlib_floor },
{ "ceiling", vectorlib_ceiling },
{ "truncate", vectorlib_truncate },
{ "inverse", vectorlib_inverse },
{ "rowechelon", vectorlib_rowechelon },
{ "identity", vectorlib_identity },
{ "transpose", vectorlib_transpose },
{ "normalize", vectorlib_normalize },
{ "homogenize", vectorlib_homogenize },
{ "determinant", vectorlib_determinant },
{ "issingular", vectorlib_issingular },
{ "inner", vectorlib_inner }, /* maybe: innerproduct */
{ "product", vectorlib_product },
{ "crossproduct", vectorlib_crossproduct },
/* manipulations */
{ "concat", vectorlib_concat },
{ "slice", vectorlib_slice },
{ "delete", vectorlib_delete },
{ "remove", vectorlib_remove },
{ "insert", vectorlib_insert },
{ "replace", vectorlib_replace },
{ "swap", vectorlib_swap },
{ "exchange", vectorlib_exchange },
{ "append", vectorlib_append },
/* nothing more */
{ NULL, NULL },
};
static const luaL_Reg vectorlib_mesh_function_list[] =
{
{ "new", vectorlib_mesh_new },
{ "ismesh", vectorlib_ismesh },
{ "type", vectorlib_mesh_type },
{ "tostring", vectorlib_mesh_tostring },
{ "totable", vectorlib_mesh_totable },
{ "getdimensions", vectorlib_mesh_getdimensions },
{ "get", vectorlib_mesh_getvalue }, /* table when third is true */
{ "set", vectorlib_mesh_setvalue },
/* nothing more */
{ NULL, NULL },
};
static const luaL_Reg vectorlib_contour_function_list[] =
{
/* for experiments with mp */
{ "average", vectorlib_contour_average },
{ "bounds", vectorlib_contour_bounds },
{ "makemesh", vectorlib_contour_makemesh },
{ "sort", vectorlib_contour_sort },
{ "getmesh", vectorlib_contour_getmesh },
{ "getarea", vectorlib_contour_getarea },
{ "checkoverlap", vectorlib_contour_checkoverlap },
{ "collectoverlap", vectorlib_contour_collectoverlap },
/* nothing more */
{ NULL, NULL },
};
static const luaL_Reg vectorlib_vector_metatable[] =
{
{ "__len", vectorlib_getlength },
{ "__index", vectorlib_getvalue },
{ "__newindex", vectorlib_setvalue },
{ "__tostring", vectorlib_tostring },
{ "__add", vectorlib_add },
{ "__div", vectorlib_div },
{ "__mul", vectorlib_mul },
{ "__sub", vectorlib_sub },
{ "__unm", vectorlib_neg },
{ "__eq", vectorlib_eq },
{ "__concat", vectorlib_concat },
{ "__call", vectorlib_setnext },
{ NULL, NULL },
};
static const luaL_Reg vectorlib_mesh_metatable[] =
{
{ "__len", vectorlib_mesh_getlength },
{ "__index", vectorlib_mesh_index },
{ "__newindex", vectorlib_mesh_setvalue },
{ "__tostring", vectorlib_mesh_tostring },
{ NULL, NULL },
};
int luaopen_vector(lua_State *L)
{
luaL_newmetatable(L, VECTOR_METATABLE_INSTANCE);
luaL_setfuncs(L, vectorlib_vector_metatable, 0);
luaL_newmetatable(L, MESH_METATABLE_INSTANCE);
luaL_setfuncs(L, vectorlib_mesh_metatable, 0);
/* vector.* */
lua_newtable(L);
luaL_setfuncs(L, vectorlib_vector_function_list, 0);
/* vector.mesh */
// lua_pushliteral(L, "mesh");
lua_pushstring(L, lua_key(mesh));
lua_newtable(L);
luaL_setfuncs(L, vectorlib_mesh_function_list, 0);
lua_rawset(L, -3);
/* vector.contour */
// lua_pushstring(L, lua_key(contour));
lua_pushliteral(L, "contour");
lua_newtable(L);
luaL_setfuncs(L, vectorlib_contour_function_list, 0);
lua_rawset(L, -3);
return 1;
}
|