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
|
// SPDX-FileCopyrightText: 2003 Dominique Devriese <devriese@kde.org>
// SPDX-License-Identifier: GPL-2.0-or-later
#include "special_constructors.h"
#include <math.h>
#include "calcpaths.h"
#include "common.h"
#include "conic-common.h"
#include "guiaction.h"
#include "kigpainter.h"
#include "../kig/kig_document.h"
#include "../kig/kig_part.h"
#include "../modes/construct_mode.h"
#include "../objects/bezier_imp.h"
#include "../objects/bezier_type.h"
#include "../objects/bogus_imp.h"
#include "../objects/centerofcurvature_type.h"
#include "../objects/circle_imp.h"
#include "../objects/conic_imp.h"
#include "../objects/conic_types.h"
#include "../objects/cubic_imp.h"
#include "../objects/intersection_types.h"
#include "../objects/inversion_type.h"
#include "../objects/line_imp.h"
#include "../objects/line_type.h"
#include "../objects/locus_imp.h"
#include "../objects/object_calcer.h"
#include "../objects/object_drawer.h"
#include "../objects/object_factory.h"
#include "../objects/object_holder.h"
#include "../objects/object_imp.h"
#include "../objects/object_type.h"
#include "../objects/other_imp.h"
#include "../objects/other_type.h"
#include "../objects/point_imp.h"
#include "../objects/point_type.h"
#include "../objects/polygon_imp.h"
#include "../objects/polygon_type.h"
#include "../objects/special_imptypes.h"
#include "../objects/tangent_type.h"
#include "../objects/text_imp.h"
#include "../objects/transform_types.h"
#include <QPen>
#include <algorithm>
#include <functional>
#include <iterator>
/*
* conic-line and circle-circle intersection (with search for already computed
* intersections)
* the previous "ConicLineIntersectionConstructor" is now
* dead code, which could be remove in the future
*/
TwoOrOneIntersectionConstructor::TwoOrOneIntersectionConstructor(const ArgsParserObjectType *t_std,
const ArgsParserObjectType *t_special,
const char *iconfile,
const struct ArgsParser::spec argsspecv[])
: StandardConstructorBase({}, {}, iconfile, margsparser)
, mtype_std(t_std)
, mtype_special(t_special)
, margsparser(argsspecv, 2)
{
}
TwoOrOneIntersectionConstructor::~TwoOrOneIntersectionConstructor()
{
}
void TwoOrOneIntersectionConstructor::drawprelim(const ObjectDrawer &drawer,
KigPainter &p,
const std::vector<ObjectCalcer *> &parents,
const KigDocument &doc) const
{
Args args;
if (parents.size() != 2)
return;
transform(parents.begin(), parents.end(), back_inserter(args), std::mem_fn(&ObjectCalcer::imp));
for (int i = -1; i <= 1; i += 2) {
IntImp param(i);
args.push_back(¶m);
ObjectImp *data = mtype_std->calc(args, doc);
drawer.draw(*data, p, true);
delete data;
args.pop_back();
}
}
std::vector<ObjectCalcer *> removeDuplicatedPoints(std::vector<ObjectCalcer *> points)
{
std::vector<ObjectCalcer *> ret;
for (std::vector<ObjectCalcer *>::iterator i = points.begin(); i != points.end(); ++i) {
for (std::vector<ObjectCalcer *>::iterator j = ret.begin(); j != ret.end(); ++j) {
if (coincidentPoints((*i)->imp(), (*j)->imp()))
break;
}
ret.push_back(*i);
}
return ret;
}
bool coincidentPoints(const ObjectImp *p1, const ObjectImp *p2)
{
const PointImp *pt1 = dynamic_cast<const PointImp *>(p1);
if (!pt1)
return false;
const PointImp *pt2 = dynamic_cast<const PointImp *>(p2);
if (!pt2)
return false;
Coordinate diff = pt1->coordinate() - pt2->coordinate();
if (diff.squareLength() < 1e-12)
return true;
return false;
}
std::vector<ObjectHolder *> TwoOrOneIntersectionConstructor::build(const std::vector<ObjectCalcer *> &parents, KigDocument &doc, KigWidget &) const
{
std::vector<ObjectHolder *> ret;
assert(parents.size() == 2);
std::vector<ObjectCalcer *> points = doc.findIntersectionPoints(parents[0], parents[1]);
std::vector<ObjectCalcer *> uniquepoints = removeDuplicatedPoints(points);
if (uniquepoints.size() == 1) {
std::vector<ObjectCalcer *> args(parents);
args.push_back(uniquepoints[0]);
ret.push_back(new ObjectHolder(new ObjectTypeCalcer(mtype_special, args)));
return ret;
}
for (int i = -1; i <= 1; i += 2) {
ObjectConstCalcer *d = new ObjectConstCalcer(new IntImp(i));
std::vector<ObjectCalcer *> args(parents);
args.push_back(d);
ret.push_back(new ObjectHolder(new ObjectTypeCalcer(mtype_std, args)));
}
return ret;
}
void TwoOrOneIntersectionConstructor::plug(KigPart *, KigGUIAction *)
{
}
bool TwoOrOneIntersectionConstructor::isTransform() const
{
return false;
}
ThreeTwoOneIntersectionConstructor::ThreeTwoOneIntersectionConstructor(const ArgsParserObjectType *t_std,
const ArgsParserObjectType *t_special,
const char *iconfile,
const struct ArgsParser::spec argsspecv[])
: StandardConstructorBase({}, {}, iconfile, margsparser)
, mtype_std(t_std)
, mtype_special(t_special)
, margsparser(argsspecv, 2)
{
}
ThreeTwoOneIntersectionConstructor::~ThreeTwoOneIntersectionConstructor()
{
}
void ThreeTwoOneIntersectionConstructor::drawprelim(const ObjectDrawer &drawer,
KigPainter &p,
const std::vector<ObjectCalcer *> &parents,
const KigDocument &doc) const
{
Args args;
if (parents.size() != 2)
return;
transform(parents.begin(), parents.end(), back_inserter(args), std::mem_fn(&ObjectCalcer::imp));
for (int i = 1; i <= 3; i += 1) {
IntImp param(i);
args.push_back(¶m);
ObjectImp *data = mtype_std->calc(args, doc);
drawer.draw(*data, p, true);
delete data;
args.pop_back();
}
}
std::vector<ObjectHolder *> ThreeTwoOneIntersectionConstructor::build(const std::vector<ObjectCalcer *> &parents, KigDocument &doc, KigWidget &) const
{
std::vector<ObjectHolder *> ret;
assert(parents.size() == 2);
std::vector<ObjectCalcer *> points = doc.findIntersectionPoints(parents[0], parents[1]);
std::vector<ObjectCalcer *> uniquepoints = removeDuplicatedPoints(points);
if (uniquepoints.size() == 2) {
std::vector<ObjectCalcer *> args(parents);
args.push_back(uniquepoints[0]);
args.push_back(uniquepoints[1]);
ret.push_back(new ObjectHolder(new ObjectTypeCalcer(mtype_special, args)));
return ret;
}
if (uniquepoints.size() == 1) {
for (int i = -1; i <= 1; i += 2) {
std::vector<ObjectCalcer *> args(parents);
args.push_back(uniquepoints[0]);
ObjectConstCalcer *d = new ObjectConstCalcer(new IntImp(i));
args.push_back(d);
ret.push_back(new ObjectHolder(new ObjectTypeCalcer(CubicLineTwoIntersectionType::instance(), args)));
args.clear();
}
return ret;
}
for (int i = 1; i <= 3; i += 1) {
ObjectConstCalcer *d = new ObjectConstCalcer(new IntImp(i));
std::vector<ObjectCalcer *> args(parents);
args.push_back(d);
ret.push_back(new ObjectHolder(new ObjectTypeCalcer(mtype_std, args)));
}
return ret;
}
void ThreeTwoOneIntersectionConstructor::plug(KigPart *, KigGUIAction *)
{
}
bool ThreeTwoOneIntersectionConstructor::isTransform() const
{
return false;
}
/*
* conic-conic intersection
*/
class ConicConicIntersectionConstructor : public StandardConstructorBase
{
protected:
ArgsParser mparser;
public:
ConicConicIntersectionConstructor();
~ConicConicIntersectionConstructor();
void drawprelim(const ObjectDrawer &drawer, KigPainter &p, const std::vector<ObjectCalcer *> &parents, const KigDocument &) const override;
std::vector<ObjectHolder *> build(const std::vector<ObjectCalcer *> &os, KigDocument &d, KigWidget &w) const override;
void plug(KigPart *doc, KigGUIAction *kact) override;
bool isTransform() const override;
};
class ConicLineIntersectionConstructor : public MultiObjectTypeConstructor
{
public:
ConicLineIntersectionConstructor();
~ConicLineIntersectionConstructor();
};
class ArcLineIntersectionConstructor : public MultiObjectTypeConstructor
{
public:
ArcLineIntersectionConstructor();
~ArcLineIntersectionConstructor();
};
ConicRadicalConstructor::ConicRadicalConstructor()
: StandardConstructorBase(i18n("Radical Lines for Conics"),
i18n("The lines constructed through the intersections "
"of two conics. This is also defined for "
"non-intersecting conics."),
"conicsradicalline",
mparser)
, mtype(ConicRadicalType::instance())
, mparser(mtype->argsParser().without(IntImp::stype()))
{
}
ConicRadicalConstructor::~ConicRadicalConstructor()
{
}
void ConicRadicalConstructor::drawprelim(const ObjectDrawer &drawer, KigPainter &p, const std::vector<ObjectCalcer *> &parents, const KigDocument &doc) const
{
if (parents.size() == 2 && parents[0]->imp()->inherits(ConicImp::stype()) && parents[1]->imp()->inherits(ConicImp::stype())) {
Args args;
std::transform(parents.begin(), parents.end(), std::back_inserter(args), std::mem_fn(&ObjectCalcer::imp));
for (int i = -1; i < 2; i += 2) {
IntImp root(i);
IntImp zeroindex(1);
args.push_back(&root);
args.push_back(&zeroindex);
ObjectImp *data = mtype->calc(args, doc);
drawer.draw(*data, p, true);
delete data;
data = nullptr;
args.pop_back();
args.pop_back();
};
};
}
std::vector<ObjectHolder *> ConicRadicalConstructor::build(const std::vector<ObjectCalcer *> &os, KigDocument &, KigWidget &) const
{
using namespace std;
std::vector<ObjectHolder *> ret;
ObjectCalcer *zeroindexcalcer = new ObjectConstCalcer(new IntImp(1));
for (int i = -1; i < 2; i += 2) {
std::vector<ObjectCalcer *> args;
std::copy(os.begin(), os.end(), back_inserter(args));
args.push_back(new ObjectConstCalcer(new IntImp(i)));
// we use only one zeroindex dataobject, so that if you switch one
// radical line around, then the other switches along..
args.push_back(zeroindexcalcer);
ret.push_back(new ObjectHolder(new ObjectTypeCalcer(mtype, args)));
};
return ret;
}
static const struct ArgsParser::spec argsspecpp[] = {
{PointImp::stype(), kli18n("Moving Point"), kli18n("Select the moving point, which will be moved around while drawing the locus..."), false},
{PointImp::stype(), kli18n("Following Point"), kli18n("Select the following point, whose locations the locus will be drawn through..."), true}};
LocusConstructor::LocusConstructor()
: StandardConstructorBase(i18n("Locus"), i18n("A locus"), "locus", margsparser)
, margsparser(argsspecpp, 2)
{
}
LocusConstructor::~LocusConstructor()
{
}
void LocusConstructor::drawprelim(const ObjectDrawer &drawer, KigPainter &p, const std::vector<ObjectCalcer *> &parents, const KigDocument &) const
{
// this function is rather ugly, but it is necessary to do it this
// way in order to play nice with Kig's design..
if (parents.size() != 2)
return;
const ObjectTypeCalcer *constrained = dynamic_cast<ObjectTypeCalcer *>(parents.front());
const ObjectCalcer *moving = parents.back();
if (!constrained || !constrained->type()->inherits(ObjectType::ID_ConstrainedPointType)) {
// moving is in fact the constrained point. swap them.
moving = parents.front();
constrained = dynamic_cast<const ObjectTypeCalcer *>(parents.back());
assert(constrained);
};
assert(constrained->type()->inherits(ObjectType::ID_ConstrainedPointType));
const ObjectImp *oimp = constrained->parents().back()->imp();
if (!oimp->inherits(CurveImp::stype()))
oimp = constrained->parents().front()->imp();
assert(oimp->inherits(CurveImp::stype()));
const CurveImp *cimp = static_cast<const CurveImp *>(oimp);
ObjectHierarchy hier(constrained, moving);
LocusImp limp(cimp->copy(), hier);
drawer.draw(limp, p, true);
}
int LocusConstructor::wantArgs(const std::vector<ObjectCalcer *> &os, const KigDocument &, const KigWidget &) const
{
int ret = margsparser.check(os);
if (ret == ArgsParser::Invalid)
return ret;
else if (os.size() != 2)
return ret;
if (dynamic_cast<ObjectTypeCalcer *>(os.front()) && static_cast<ObjectTypeCalcer *>(os.front())->type()->inherits(ObjectType::ID_ConstrainedPointType)) {
std::set<ObjectCalcer *> children = getAllChildren(os.front());
return children.find(os.back()) != children.end() ? ret : ArgsParser::Invalid;
}
if (dynamic_cast<ObjectTypeCalcer *>(os.back()) && static_cast<ObjectTypeCalcer *>(os.back())->type()->inherits(ObjectType::ID_ConstrainedPointType)) {
std::set<ObjectCalcer *> children = getAllChildren(os.back());
return children.find(os.front()) != children.end() ? ret : ArgsParser::Invalid;
}
return ArgsParser::Invalid;
}
std::vector<ObjectHolder *> LocusConstructor::build(const std::vector<ObjectCalcer *> &parents, KigDocument &, KigWidget &) const
{
std::vector<ObjectHolder *> ret;
assert(parents.size() == 2);
ObjectTypeCalcer *constrained = dynamic_cast<ObjectTypeCalcer *>(parents.front());
ObjectCalcer *moving = parents.back();
if (!constrained || !constrained->type()->inherits(ObjectType::ID_ConstrainedPointType)) {
// moving is in fact the constrained point. swap them.
moving = parents.front();
constrained = dynamic_cast<ObjectTypeCalcer *>(parents.back());
assert(constrained);
};
assert(constrained->type()->inherits(ObjectType::ID_ConstrainedPointType));
ret.push_back(ObjectFactory::instance()->locus(constrained, moving));
return ret;
}
QString LocusConstructor::useText(const ObjectCalcer &o, const std::vector<ObjectCalcer *> &os, const KigDocument &, const KigWidget &) const
{
if (dynamic_cast<const ObjectTypeCalcer *>(&o) && static_cast<const ObjectTypeCalcer &>(o).type()->inherits(ObjectType::ID_ConstrainedPointType)
&& (os.empty() || !dynamic_cast<ObjectTypeCalcer *>(os[0])
|| !static_cast<const ObjectTypeCalcer *>(os[0])->type()->inherits(ObjectType::ID_ConstrainedPointType)))
return i18n("Moving Point");
else
return i18n("Dependent Point");
}
void ConicRadicalConstructor::plug(KigPart *, KigGUIAction *)
{
}
void LocusConstructor::plug(KigPart *, KigGUIAction *)
{
}
bool ConicRadicalConstructor::isTransform() const
{
return mtype->isTransform();
}
bool LocusConstructor::isTransform() const
{
return false;
}
/*
* generic sequence of points constructor
*/
PointSequenceConstructor::PointSequenceConstructor(const QString &descname, const QString &desc, const QString &iconfile, const ObjectType *type)
: mdescname(descname)
, mdesc(desc)
, miconfile(iconfile)
, mtype(type)
{
}
const QString PointSequenceConstructor::descriptiveName() const
{
return mdescname;
}
const QString PointSequenceConstructor::description() const
{
return mdesc;
}
const QString PointSequenceConstructor::iconFileName(const bool) const
{
return miconfile;
}
void PointSequenceConstructor::handleArgs(const std::vector<ObjectCalcer *> &os, KigPart &d, KigWidget &v) const
{
std::vector<ObjectHolder *> bos = build(os, d.document(), v);
for (std::vector<ObjectHolder *>::iterator i = bos.begin(); i != bos.end(); ++i) {
(*i)->calc(d.document());
}
d.addObjects(bos);
}
void PointSequenceConstructor::handlePrelim(KigPainter &p, const std::vector<ObjectCalcer *> &os, const KigDocument &d, const KigWidget &) const
{
uint count = os.size();
if (count < 2)
return;
for (uint i = 0; i < count; i++) {
assert(os[i]->imp()->inherits(PointImp::stype()));
}
std::vector<ObjectCalcer *> args = os;
p.setBrushStyle(Qt::NoBrush);
p.setBrushColor(Qt::red);
p.setPen(QPen(Qt::red, 1));
p.setWidth(-1); // -1 means the default width for the object being
// drawn..
ObjectDrawer drawer(Qt::red);
drawprelim(drawer, p, args, d);
}
std::vector<ObjectHolder *> PointSequenceConstructor::build(const std::vector<ObjectCalcer *> &parents, KigDocument &, KigWidget &) const
{
uint count = parents.size() - 1;
assert(count >= 3);
std::vector<ObjectCalcer *> args;
for (uint i = 0; i < count; ++i)
args.push_back(parents[i]);
ObjectTypeCalcer *calcer = new ObjectTypeCalcer(mtype, args);
ObjectHolder *h = new ObjectHolder(calcer);
std::vector<ObjectHolder *> ret;
ret.push_back(h);
return ret;
}
void PointSequenceConstructor::plug(KigPart *, KigGUIAction *)
{
}
bool PointSequenceConstructor::isTransform() const
{
return false;
}
/*
* generic polygon constructor
*/
PolygonBNPTypeConstructor::PolygonBNPTypeConstructor()
: PointSequenceConstructor(i18n("Polygon by Its Vertices"), i18n("Construct a polygon by giving its vertices"), "kig_polygon", PolygonBNPType::instance())
{
}
PolygonBNPTypeConstructor::~PolygonBNPTypeConstructor()
{
}
bool PolygonBNPTypeConstructor::isAlreadySelectedOK(const std::vector<ObjectCalcer *> &os, const uint &pos) const
{
if (pos == 0 && os.size() >= 3)
return true;
return false;
}
int PolygonBNPTypeConstructor::wantArgs(const std::vector<ObjectCalcer *> &os, const KigDocument &, const KigWidget &) const
{
int count = os.size() - 1;
for (int i = 0; i <= count; i++) {
if (!(os[i]->imp()->inherits(PointImp::stype())))
return ArgsParser::Invalid;
}
if (count < 3)
return ArgsParser::Valid;
if (os[0] == os[count])
return ArgsParser::Complete;
return ArgsParser::Valid;
}
QString PolygonBNPTypeConstructor::useText(const ObjectCalcer &, const std::vector<ObjectCalcer *> &os, const KigDocument &, const KigWidget &) const
{
if (os.size() > 3)
return i18n("... with this vertex (click on the first vertex to terminate construction)");
else
return i18n("Construct a polygon with this vertex");
}
QString PolygonBNPTypeConstructor::selectStatement(const std::vector<ObjectCalcer *> &, const KigDocument &, const KigWidget &) const
{
return i18n("Select a point to be a vertex of the new polygon...");
}
void PolygonBNPTypeConstructor::drawprelim(const ObjectDrawer &drawer, KigPainter &p, const std::vector<ObjectCalcer *> &parents, const KigDocument &) const
{
if (parents.size() < 2)
return;
std::vector<Coordinate> points;
for (uint i = 0; i < parents.size(); ++i) {
const Coordinate vertex = static_cast<const PointImp *>(parents[i]->imp())->coordinate();
points.push_back(vertex);
}
if (parents.size() == 2) {
SegmentImp segment = SegmentImp(points[0], points[1]);
drawer.draw(segment, p, true);
} else {
FilledPolygonImp polygon = FilledPolygonImp(points);
drawer.draw(polygon, p, true);
}
}
/*
* open polygon (polyline) constructor
*/
OpenPolygonTypeConstructor::OpenPolygonTypeConstructor()
: PointSequenceConstructor(i18n("Open Polygon (Polygonal Line)"), i18n("Construct an open polygon"), "openpolygon", OpenPolygonType::instance())
{
}
OpenPolygonTypeConstructor::~OpenPolygonTypeConstructor()
{
}
bool OpenPolygonTypeConstructor::isAlreadySelectedOK(const std::vector<ObjectCalcer *> &os, const uint &pos) const
{
if (pos == os.size() - 1 && os.size() >= 2)
return true;
return false;
}
int OpenPolygonTypeConstructor::wantArgs(const std::vector<ObjectCalcer *> &os, const KigDocument &, const KigWidget &) const
{
int count = os.size() - 1;
for (int i = 0; i <= count; i++) {
if (!(os[i]->imp()->inherits(PointImp::stype())))
return ArgsParser::Invalid;
}
if (count < 2)
return ArgsParser::Valid;
if (os[count] == os[count - 1])
return ArgsParser::Complete;
return ArgsParser::Valid;
}
QString OpenPolygonTypeConstructor::useText(const ObjectCalcer &, const std::vector<ObjectCalcer *> &os, const KigDocument &, const KigWidget &) const
{
if (os.size() > 2)
return i18n("... with this vertex (click again on the last vertex to terminate construction)");
else
return i18n("Construct a polygonal line with this vertex");
}
QString OpenPolygonTypeConstructor::selectStatement(const std::vector<ObjectCalcer *> &, const KigDocument &, const KigWidget &) const
{
return i18n("Select a point to be a vertex of the new polygonal line...");
}
void OpenPolygonTypeConstructor::drawprelim(const ObjectDrawer &drawer, KigPainter &p, const std::vector<ObjectCalcer *> &parents, const KigDocument &) const
{
if (parents.size() < 2)
return;
std::vector<Coordinate> points;
for (uint i = 0; i < parents.size(); ++i) {
const Coordinate vertex = static_cast<const PointImp *>(parents[i]->imp())->coordinate();
points.push_back(vertex);
}
if (parents.size() == 2) {
SegmentImp segment = SegmentImp(points[0], points[1]);
drawer.draw(segment, p, true);
} else {
OpenPolygonalImp polygon = OpenPolygonalImp(points);
drawer.draw(polygon, p, true);
}
}
/*
* construction of polygon vertices
*/
static const struct ArgsParser::spec argsspecpv[] = {
{FilledPolygonImp::stype(), kli18n("Polygon"), kli18n("Construct the vertices of this polygon..."), true}};
PolygonVertexTypeConstructor::PolygonVertexTypeConstructor()
: StandardConstructorBase(i18n("Vertices of a Polygon"), i18n("The vertices of a polygon."), "polygonvertices", margsparser)
, mtype(PolygonVertexType::instance())
, margsparser(argsspecpv, 1)
{
}
PolygonVertexTypeConstructor::~PolygonVertexTypeConstructor()
{
}
void PolygonVertexTypeConstructor::drawprelim(const ObjectDrawer &drawer, KigPainter &p, const std::vector<ObjectCalcer *> &parents, const KigDocument &) const
{
if (parents.size() != 1)
return;
const FilledPolygonImp *polygon = dynamic_cast<const FilledPolygonImp *>(parents.front()->imp());
const std::vector<Coordinate> points = polygon->points();
int sides = points.size();
for (int i = 0; i < sides; ++i) {
PointImp point = PointImp(points[i]);
drawer.draw(point, p, true);
}
}
std::vector<ObjectHolder *> PolygonVertexTypeConstructor::build(const std::vector<ObjectCalcer *> &parents, KigDocument &, KigWidget &) const
{
std::vector<ObjectHolder *> ret;
assert(parents.size() == 1);
const FilledPolygonImp *polygon = dynamic_cast<const FilledPolygonImp *>(parents.front()->imp());
const std::vector<Coordinate> points = polygon->points();
int sides = points.size();
for (int i = 0; i < sides; ++i) {
ObjectConstCalcer *d = new ObjectConstCalcer(new IntImp(i));
std::vector<ObjectCalcer *> args(parents);
args.push_back(d);
ret.push_back(new ObjectHolder(new ObjectTypeCalcer(mtype, args)));
}
return ret;
}
void PolygonVertexTypeConstructor::plug(KigPart *, KigGUIAction *)
{
}
bool PolygonVertexTypeConstructor::isTransform() const
{
return false;
}
/*
* construction of polygon sides
*/
static const struct ArgsParser::spec argsspecps[] = {
{FilledPolygonImp::stype(), kli18n("Polygon"), kli18n("Construct the sides of this polygon..."), false}};
PolygonSideTypeConstructor::PolygonSideTypeConstructor()
: StandardConstructorBase(i18n("Sides of a Polygon"), i18n("The sides of a polygon."), "polygonsides", margsparser)
, mtype(PolygonSideType::instance())
, margsparser(argsspecps, 1)
{
}
PolygonSideTypeConstructor::~PolygonSideTypeConstructor()
{
}
void PolygonSideTypeConstructor::drawprelim(const ObjectDrawer &drawer, KigPainter &p, const std::vector<ObjectCalcer *> &parents, const KigDocument &) const
{
if (parents.size() != 1)
return;
const FilledPolygonImp *polygon = dynamic_cast<const FilledPolygonImp *>(parents.front()->imp());
const std::vector<Coordinate> points = polygon->points();
uint sides = points.size();
for (uint i = 0; i < sides; ++i) {
uint nexti = (i + 1 < sides) ? (i + 1) : 0;
SegmentImp segment = SegmentImp(points[i], points[nexti]);
drawer.draw(segment, p, true);
}
}
std::vector<ObjectHolder *> PolygonSideTypeConstructor::build(const std::vector<ObjectCalcer *> &parents, KigDocument &, KigWidget &) const
{
std::vector<ObjectHolder *> ret;
assert(parents.size() == 1);
const FilledPolygonImp *polygon = dynamic_cast<const FilledPolygonImp *>(parents.front()->imp());
const std::vector<Coordinate> points = polygon->points();
uint sides = points.size();
for (uint i = 0; i < sides; ++i) {
ObjectConstCalcer *d = new ObjectConstCalcer(new IntImp(i));
std::vector<ObjectCalcer *> args(parents);
args.push_back(d);
ret.push_back(new ObjectHolder(new ObjectTypeCalcer(mtype, args)));
}
return ret;
}
void PolygonSideTypeConstructor::plug(KigPart *, KigGUIAction *)
{
}
bool PolygonSideTypeConstructor::isTransform() const
{
return false;
}
/*
* polygon by center and vertex
*/
PolygonBCVConstructor::PolygonBCVConstructor()
: mtype(PolygonBCVType::instance())
{
}
PolygonBCVConstructor::~PolygonBCVConstructor()
{
}
const QString PolygonBCVConstructor::descriptiveName() const
{
return i18n("Regular Polygon with Given Center");
}
const QString PolygonBCVConstructor::description() const
{
return i18n("Construct a regular polygon with a given center and vertex");
}
const QString PolygonBCVConstructor::iconFileName(const bool) const
{
return QStringLiteral("hexagonbcv");
}
bool PolygonBCVConstructor::isAlreadySelectedOK(const std::vector<ObjectCalcer *> &, const uint &) const
{
return false;
}
int PolygonBCVConstructor::wantArgs(const std::vector<ObjectCalcer *> &os, const KigDocument &, const KigWidget &) const
{
if (os.size() > 3)
return ArgsParser::Invalid;
uint imax = (os.size() <= 2) ? os.size() : 2;
for (uint i = 0; i < imax; ++i)
if (!(os[i]->imp()->inherits(PointImp::stype())))
return ArgsParser::Invalid;
if (os.size() < 3)
return ArgsParser::Valid;
if (!(os[2]->imp()->inherits(BogusPointImp::stype())))
return ArgsParser::Invalid;
return ArgsParser::Complete;
}
void PolygonBCVConstructor::handleArgs(const std::vector<ObjectCalcer *> &os, KigPart &d, KigWidget &v) const
{
std::vector<ObjectHolder *> bos = build(os, d.document(), v);
for (std::vector<ObjectHolder *>::iterator i = bos.begin(); i != bos.end(); ++i) {
(*i)->calc(d.document());
}
d.addObjects(bos);
}
void PolygonBCVConstructor::handlePrelim(KigPainter &p, const std::vector<ObjectCalcer *> &os, const KigDocument &d, const KigWidget &) const
{
if (os.size() < 2)
return;
for (uint i = 0; i < 2; i++) {
assert(os[i]->imp()->inherits(PointImp::stype()));
}
Coordinate c = static_cast<const PointImp *>(os[0]->imp())->coordinate();
Coordinate v = static_cast<const PointImp *>(os[1]->imp())->coordinate();
int nsides = 6;
bool moreinfo = false;
int winding = 0; // 0 means allow winding > 1
if (os.size() == 3) {
assert(os[2]->imp()->inherits(BogusPointImp::stype()));
Coordinate cntrl = static_cast<const PointImp *>(os[2]->imp())->coordinate();
nsides = computeNsides(c, v, cntrl, winding);
moreinfo = true;
}
std::vector<ObjectCalcer *> args;
args.push_back(os[0]);
args.push_back(os[1]);
ObjectConstCalcer *ns = new ObjectConstCalcer(new IntImp(nsides));
args.push_back(ns);
if (winding > 1) {
ns = new ObjectConstCalcer(new IntImp(winding));
args.push_back(ns);
}
p.setBrushStyle(Qt::NoBrush);
p.setBrushColor(Qt::red);
p.setPen(QPen(Qt::red, 1));
p.setWidth(-1); // -1 means the default width for the object being
// drawn..
ObjectDrawer drawer(Qt::red);
drawprelim(drawer, p, args, d);
if (moreinfo) {
p.setPointStyle(Kig::RoundEmpty);
p.setWidth(6);
double ro = 1.0 / (2.5);
Coordinate where = getRotatedCoord(c, (1 - ro) * c + ro * v, 4 * M_PI / 5.0);
PointImp ptn = PointImp(where);
TextImp text = TextImp(QStringLiteral("(5,2)"), where, false);
ptn.draw(p);
text.draw(p);
for (int i = 3; i < 9; ++i) {
where = getRotatedCoord(c, v, 2.0 * M_PI / i);
ptn = PointImp(where);
ptn.draw(p);
if (i > 5)
continue;
text = TextImp(QStringLiteral("(%1)").arg(i), where, false);
text.draw(p);
}
p.setStyle(Qt::DotLine);
p.setWidth(1);
double radius = (v - c).length();
CircleImp circle = CircleImp(c, radius);
circle.draw(p);
for (int i = 2; i < 5; i++) {
ro = 1.0 / (i + 0.5);
CircleImp circle = CircleImp(c, ro * radius);
circle.draw(p);
}
}
delete_all(args.begin() + 2, args.end());
}
std::vector<ObjectHolder *> PolygonBCVConstructor::build(const std::vector<ObjectCalcer *> &parents, KigDocument &, KigWidget &) const
{
assert(parents.size() == 3);
std::vector<ObjectCalcer *> args;
Coordinate c = static_cast<const PointImp *>(parents[0]->imp())->coordinate();
Coordinate v = static_cast<const PointImp *>(parents[1]->imp())->coordinate();
Coordinate cntrl = static_cast<const PointImp *>(parents[2]->imp())->coordinate();
args.push_back(parents[0]);
args.push_back(parents[1]);
int winding = 0;
int nsides = computeNsides(c, v, cntrl, winding);
ObjectConstCalcer *d = new ObjectConstCalcer(new IntImp(nsides));
args.push_back(d);
if (winding > 1) {
d = new ObjectConstCalcer(new IntImp(winding));
args.push_back(d);
}
ObjectTypeCalcer *calcer = new ObjectTypeCalcer(mtype, args);
ObjectHolder *h = new ObjectHolder(calcer);
std::vector<ObjectHolder *> ret;
ret.push_back(h);
return ret;
}
QString PolygonBCVConstructor::useText(const ObjectCalcer &, const std::vector<ObjectCalcer *> &os, const KigDocument &, const KigWidget &) const
{
switch (os.size()) {
case 1:
return i18n("Construct a regular polygon with this center");
break;
case 2:
return i18n("Construct a regular polygon with this vertex");
break;
case 3:
Coordinate c = static_cast<const PointImp *>(os[0]->imp())->coordinate();
Coordinate v = static_cast<const PointImp *>(os[1]->imp())->coordinate();
Coordinate cntrl = static_cast<const PointImp *>(os[2]->imp())->coordinate();
int winding = 0;
int nsides = computeNsides(c, v, cntrl, winding);
if (winding > 1) {
QString result = i18n("Adjust the number of sides (%1/%2)", nsides, winding);
return result;
} else {
QString result = i18n("Adjust the number of sides (%1)", nsides);
return result;
}
break;
}
return QLatin1String("");
}
QString PolygonBCVConstructor::selectStatement(const std::vector<ObjectCalcer *> &os, const KigDocument &, const KigWidget &) const
{
switch (os.size()) {
case 1:
return i18n("Select the center of the new polygon...");
break;
case 2:
return i18n("Select a vertex for the new polygon...");
break;
case 3:
return i18n("Move the cursor to get the desired number of sides...");
break;
}
return QLatin1String("");
}
void PolygonBCVConstructor::drawprelim(const ObjectDrawer &drawer, KigPainter &p, const std::vector<ObjectCalcer *> &parents, const KigDocument &doc) const
{
if (parents.size() < 3 || parents.size() > 4)
return;
assert(parents[0]->imp()->inherits(PointImp::stype()) && parents[1]->imp()->inherits(PointImp::stype()) && parents[2]->imp()->inherits(IntImp::stype()));
if (parents.size() == 4)
assert(parents[3]->imp()->inherits(IntImp::stype()));
Args args;
std::transform(parents.begin(), parents.end(), std::back_inserter(args), std::mem_fn(&ObjectCalcer::imp));
ObjectImp *data = mtype->calc(args, doc);
drawer.draw(*data, p, true);
delete data;
data = nullptr;
}
void PolygonBCVConstructor::plug(KigPart *, KigGUIAction *)
{
}
bool PolygonBCVConstructor::isTransform() const
{
return false;
}
Coordinate PolygonBCVConstructor::getRotatedCoord(const Coordinate &c, const Coordinate &v, double alpha) const
{
double cosalpha = cos(alpha);
double sinalpha = sin(alpha);
double dx = v.x - c.x;
double dy = v.y - c.y;
return c + Coordinate(cosalpha * dx - sinalpha * dy, sinalpha * dx + cosalpha * dy);
}
int PolygonBCVConstructor::computeNsides(const Coordinate &c, const Coordinate &v, const Coordinate &cntrl, int &winding) const
{
Coordinate lvect = v - c;
Coordinate rvect = cntrl - c;
double angle = atan2(rvect.y, rvect.x) - atan2(lvect.y, lvect.x);
angle = fabs(angle / (2 * M_PI));
while (angle > 1)
angle -= 1;
if (angle > 0.5)
angle = 1 - angle;
double realsides = 1.0 / angle; // this is bigger that 2
if (angle == 0.)
realsides = 3;
if (winding <= 0) // free to compute winding
{
winding = 1;
double ratio = lvect.length() / rvect.length();
winding = int(ratio);
if (winding < 1)
winding = 1;
if (winding > 50)
winding = 50;
}
int nsides = int(winding * realsides + 0.5); // nsides/winding should be reduced!
if (nsides > 100)
nsides = 100; // well, 100 seems large enough!
if (nsides < 3)
nsides = 3;
while (!relativePrimes(nsides, winding))
++nsides;
return nsides;
}
/*
* generic Bézier curve constructor
*/
BezierCurveTypeConstructor::BezierCurveTypeConstructor()
: PointSequenceConstructor(i18n("Bézier Curve by its Control Points"),
i18n("Construct a Bézier curve by giving its control points"),
"bezierN",
BezierCurveType::instance())
{
}
BezierCurveTypeConstructor::~BezierCurveTypeConstructor()
{
}
bool BezierCurveTypeConstructor::isAlreadySelectedOK(const std::vector<ObjectCalcer *> &os, const uint &pos) const
{
if (pos == os.size() - 1 && os.size() >= 3)
return true;
return false;
}
int BezierCurveTypeConstructor::wantArgs(const std::vector<ObjectCalcer *> &os, const KigDocument &, const KigWidget &) const
{
int count = os.size() - 1;
for (int i = 0; i <= count; i++) {
if (!(os[i]->imp()->inherits(PointImp::stype())))
return ArgsParser::Invalid;
}
if (count < 3)
return ArgsParser::Valid;
if (os[count] == os[count - 1])
return ArgsParser::Complete;
return ArgsParser::Valid;
}
QString BezierCurveTypeConstructor::useText(const ObjectCalcer &, const std::vector<ObjectCalcer *> &os, const KigDocument &, const KigWidget &) const
{
if (os.size() > 3)
return i18n("... with this control point (click again on the last control point to terminate construction)");
else
return i18n("Construct a Bézier curve with this control point");
}
QString BezierCurveTypeConstructor::selectStatement(const std::vector<ObjectCalcer *> &, const KigDocument &, const KigWidget &) const
{
return i18n("Select a point to be a control point of the new Bézier curve...");
}
void BezierCurveTypeConstructor::drawprelim(const ObjectDrawer &, KigPainter &p, const std::vector<ObjectCalcer *> &parents, const KigDocument &) const
{
if (parents.size() < 2)
return;
std::vector<Coordinate> points;
for (uint i = 0; i < parents.size(); ++i) {
const Coordinate vertex = static_cast<const PointImp *>(parents[i]->imp())->coordinate();
points.push_back(vertex);
}
BezierImp B = BezierImp(points);
B.draw(p);
}
/*
* generic rational Bézier curve constructor
*/
RationalBezierCurveTypeConstructor::RationalBezierCurveTypeConstructor()
{
}
RationalBezierCurveTypeConstructor::~RationalBezierCurveTypeConstructor()
{
}
const QString RationalBezierCurveTypeConstructor::descriptiveName() const
{
return i18n("Rational Bézier Curve by its Control Points");
}
const QString RationalBezierCurveTypeConstructor::description() const
{
return i18n("Construct a Bézier curve by giving its control points and positive weights");
}
const QString RationalBezierCurveTypeConstructor::iconFileName(const bool) const
{
return QStringLiteral("rbezierN");
}
bool RationalBezierCurveTypeConstructor::isAlreadySelectedOK(const std::vector<ObjectCalcer *> &os, const uint &pos) const
{
if (pos % 2 == 1)
return true;
if (pos == os.size() - 2 && os.size() >= 3)
return true;
return false;
}
int RationalBezierCurveTypeConstructor::wantArgs(const std::vector<ObjectCalcer *> &os, const KigDocument &, const KigWidget &) const
{
int count = os.size() - 1;
for (int i = 0; i <= count; i++) {
if (!(os[i]->imp()->inherits(i % 2 == 0 ? PointImp::stype() : &weightimptypeinstance)))
return ArgsParser::Invalid;
}
if (count < 6)
return ArgsParser::Valid;
if (count % 2 == 0 && (os[count] == os[count - 2]))
return ArgsParser::Complete;
return ArgsParser::Valid;
}
std::vector<ObjectHolder *> RationalBezierCurveTypeConstructor::build(const std::vector<ObjectCalcer *> &parents, KigDocument &, KigWidget &) const
{
uint count = parents.size() - 1;
assert(count >= 3);
std::vector<ObjectCalcer *> args;
for (uint i = 0; i < count; ++i)
args.push_back(parents[i]);
ObjectTypeCalcer *calcer = new ObjectTypeCalcer(RationalBezierCurveType::instance(), args);
ObjectHolder *h = new ObjectHolder(calcer);
std::vector<ObjectHolder *> ret;
ret.push_back(h);
return ret;
}
void RationalBezierCurveTypeConstructor::handleArgs(const std::vector<ObjectCalcer *> &os, KigPart &d, KigWidget &v) const
{
std::vector<ObjectHolder *> bos = build(os, d.document(), v);
for (std::vector<ObjectHolder *>::iterator i = bos.begin(); i != bos.end(); ++i) {
(*i)->calc(d.document());
}
d.addObjects(bos);
}
QString RationalBezierCurveTypeConstructor::useText(const ObjectCalcer &, const std::vector<ObjectCalcer *> &os, const KigDocument &, const KigWidget &) const
{
if (os.size() % 2 == 0)
return i18n("... assign this weight to last selected control point");
if (os.size() > 6)
return i18n("... with this control point (click again on the last control point or weight to terminate construction)");
else
return i18n("Construct a rational Bézier curve with this control point");
}
QString RationalBezierCurveTypeConstructor::selectStatement(const std::vector<ObjectCalcer *> &os, const KigDocument &, const KigWidget &) const
{
if (os.size() % 2 == 0)
return i18n("Select a point to be a control point of the new rational Bézier curve...");
else
return i18n("Select a numeric label to be a weight of last selected point...");
}
void RationalBezierCurveTypeConstructor::drawprelim(const ObjectDrawer &, KigPainter &p, const std::vector<ObjectCalcer *> &parents, const KigDocument &) const
{
if (parents.size() < 5)
return;
std::vector<Coordinate> points;
std::vector<double> weights;
uint count = parents.size();
for (uint i = 0; i < count; i += 2) {
bool valid;
assert(parents[i]->imp()->inherits(PointImp::stype()));
const Coordinate vertex = static_cast<const PointImp *>(parents[i]->imp())->coordinate();
points.push_back(vertex);
if (i + 1 >= count)
break;
assert(parents[i + 1]->imp()->inherits(&weightimptypeinstance));
const double weight = getDoubleFromImp(parents[i + 1]->imp(), valid);
assert(valid);
weights.push_back(weight);
}
if (count % 2 == 1) {
// point was selected, we
weights.push_back(1); // don't have its weight so far
}
assert(points.size() == weights.size());
RationalBezierImp rB = RationalBezierImp(points, weights);
rB.draw(p);
}
void RationalBezierCurveTypeConstructor::handlePrelim(KigPainter &p, const std::vector<ObjectCalcer *> &os, const KigDocument &d, const KigWidget &) const
{
uint count = os.size();
if (count < 5)
return;
for (uint i = 0; i < count; i += 2) {
assert(os[i]->imp()->inherits(PointImp::stype()));
if (i + 1 >= count)
break;
assert(os[i + 1]->imp()->inherits(&weightimptypeinstance));
}
std::vector<ObjectCalcer *> args = os;
p.setBrushStyle(Qt::NoBrush);
p.setBrushColor(Qt::red);
p.setPen(QPen(Qt::red, 1));
p.setWidth(-1); // -1 means the default width for the object being
// drawn..
ObjectDrawer drawer(Qt::red);
drawprelim(drawer, p, args, d);
}
void RationalBezierCurveTypeConstructor::plug(KigPart *, KigGUIAction *)
{
}
bool RationalBezierCurveTypeConstructor::isTransform() const
{
return false;
}
/*
* ConicConic intersection...
*/
static const ArgsParser::spec argsspectc[] = {{ConicImp::stype(), {}, {}, true},
{ConicImp::stype(), {}, {}, true}};
ConicConicIntersectionConstructor::ConicConicIntersectionConstructor()
: StandardConstructorBase({}, {}, "curvelineintersection", mparser)
, mparser(argsspectc, 2)
{
}
ConicConicIntersectionConstructor::~ConicConicIntersectionConstructor()
{
}
void ConicConicIntersectionConstructor::drawprelim(const ObjectDrawer &drawer,
KigPainter &p,
const std::vector<ObjectCalcer *> &parents,
const KigDocument &) const
{
if (parents.size() != 2)
return;
assert(parents[0]->imp()->inherits(ConicImp::stype()) && parents[1]->imp()->inherits(ConicImp::stype()));
const ConicCartesianData conica = static_cast<const ConicImp *>(parents[0]->imp())->cartesianData();
const ConicCartesianData conicb = static_cast<const ConicImp *>(parents[1]->imp())->cartesianData();
bool ok = true;
for (int wr = -1; wr < 2; wr += 2) {
LineData radical = calcConicRadical(conica, conicb, wr, 1, ok);
if (ok) {
for (int wi = -1; wi < 2; wi += 2) {
Coordinate c = calcConicLineIntersect(conica, radical, 0.0, wi);
if (c.valid()) {
PointImp pi(c);
drawer.draw(pi, p, true);
}
};
};
};
}
std::vector<ObjectHolder *> ConicConicIntersectionConstructor::build(const std::vector<ObjectCalcer *> &os, KigDocument &doc, KigWidget &) const
{
assert(os.size() == 2);
std::vector<ObjectHolder *> ret;
ObjectCalcer *conica = os[0];
ObjectConstCalcer *zeroindexdo = new ObjectConstCalcer(new IntImp(1));
for (int wr = -1; wr < 2; wr += 2) {
std::vector<ObjectCalcer *> args = os;
args.push_back(new ObjectConstCalcer(new IntImp(wr)));
args.push_back(zeroindexdo);
ObjectTypeCalcer *radical = new ObjectTypeCalcer(ConicRadicalType::instance(), args);
radical->calc(doc);
for (int wi = -1; wi < 2; wi += 2) {
args.clear();
args.push_back(conica);
args.push_back(radical);
args.push_back(new ObjectConstCalcer(new IntImp(wi)));
ret.push_back(new ObjectHolder(new ObjectTypeCalcer(ConicLineIntersectionType::instance(), args)));
};
};
return ret;
}
void ConicConicIntersectionConstructor::plug(KigPart *, KigGUIAction *)
{
}
bool ConicConicIntersectionConstructor::isTransform() const
{
return false;
}
ConicLineIntersectionConstructor::ConicLineIntersectionConstructor()
: MultiObjectTypeConstructor(ConicLineIntersectionType::instance(), {}, {}, "curvelineintersection", -1, 1)
{
}
ConicLineIntersectionConstructor::~ConicLineIntersectionConstructor()
{
}
ArcLineIntersectionConstructor::ArcLineIntersectionConstructor()
: MultiObjectTypeConstructor(ArcLineIntersectionType::instance(), {}, {}, "curvelineintersection", -1, 1)
{
}
ArcLineIntersectionConstructor::~ArcLineIntersectionConstructor()
{
}
QString ConicRadicalConstructor::useText(const ObjectCalcer &o, const std::vector<ObjectCalcer *> &, const KigDocument &, const KigWidget &) const
{
if (o.imp()->inherits(CircleImp::stype()))
return i18n("Construct the Radical Lines of This Circle");
else
return i18n("Construct the Radical Lines of This Conic");
}
/*
* generic affinity and generic projectivity. A unique affinity can be
* obtained by specifying the image of three points (four for projectivity)
* in the end we need, besides the object to be transformed, a total of
* six point or (alternatively) two triangles; our affinity will map the
* first triangle onto the second with corresponding ordering of their
* vertices. Since we allow for two different ways of specifying the six
* points we shall use a Generic constructor, like that for intersections.
*/
GenericAffinityConstructor::GenericAffinityConstructor()
: MergeObjectConstructor(i18n("Generic Affinity"),
i18n("The unique affinity that maps three points (or a triangle) onto three other points (or a triangle)"),
"genericaffinity")
{
SimpleObjectTypeConstructor *b2tr = new SimpleObjectTypeConstructor(AffinityB2TrType::instance(), {}, {}, "genericaffinity");
SimpleObjectTypeConstructor *gi3p = new SimpleObjectTypeConstructor(AffinityGI3PType::instance(), {}, {}, "genericaffinity");
merge(b2tr);
merge(gi3p);
}
GenericAffinityConstructor::~GenericAffinityConstructor()
{
}
bool GenericAffinityConstructor::isAlreadySelectedOK(const std::vector<ObjectCalcer *> &, const uint &) const
{
return true;
}
GenericProjectivityConstructor::GenericProjectivityConstructor()
: MergeObjectConstructor(
i18n("Generic Projective Transformation"),
i18n("The unique projective transformation that maps four points (or a quadrilateral) onto four other points (or a quadrilateral)"),
"genericprojectivity")
{
SimpleObjectTypeConstructor *b2qu =
new SimpleObjectTypeConstructor(ProjectivityB2QuType::instance(), {}, {}, "genericprojectivity");
SimpleObjectTypeConstructor *gi4p =
new SimpleObjectTypeConstructor(ProjectivityGI4PType::instance(), {}, {}, "genericprojectivity");
merge(b2qu);
merge(gi4p);
}
GenericProjectivityConstructor::~GenericProjectivityConstructor()
{
}
bool GenericProjectivityConstructor::isAlreadySelectedOK(const std::vector<ObjectCalcer *> &, const uint &) const
{
return true;
}
/*
* inversion of points, lines with respect to a circle
*/
InversionConstructor::InversionConstructor()
: MergeObjectConstructor(i18n("Inversion of Point, Line or Circle"), i18n("The inversion of a point, line or circle with respect to a circle"), "inversion")
{
SimpleObjectTypeConstructor *pointobj = new SimpleObjectTypeConstructor(InvertPointType::instance(), {}, {}, "inversion");
SimpleObjectTypeConstructor *curveobj =
new SimpleObjectTypeConstructor(CircularInversionType::instance(), {}, {}, "inversion");
// SimpleObjectTypeConstructor* lineobj =
// new SimpleObjectTypeConstructor(
// InvertLineType::instance(),
// {}, {},
// "inversion" );
//
// SimpleObjectTypeConstructor* segmentobj =
// new SimpleObjectTypeConstructor(
// InvertSegmentType::instance(),
// {}, {},
// "inversion" );
//
// SimpleObjectTypeConstructor* circleobj =
// new SimpleObjectTypeConstructor(
// InvertCircleType::instance(),
// {}, {},
// "inversion" );
//
// SimpleObjectTypeConstructor* arcobj =
// new SimpleObjectTypeConstructor(
// InvertArcType::instance(),
// {}, {},
// "inversion" );
// merge( arcobj );
// merge( circleobj );
merge(curveobj);
merge(pointobj);
// merge( segmentobj );
// merge( lineobj );
}
InversionConstructor::~InversionConstructor()
{
}
/*
* Transport of Measure
*/
MeasureTransportConstructor::MeasureTransportConstructor()
: mtype(MeasureTransportType::instance())
{
}
MeasureTransportConstructor::~MeasureTransportConstructor()
{
}
const QString MeasureTransportConstructor::descriptiveName() const
{
return i18n("Measure Transport");
}
const QString MeasureTransportConstructor::description() const
{
return i18n("Transport the measure of a segment or arc over a line or circle.");
}
const QString MeasureTransportConstructor::iconFileName(const bool) const
{
return QStringLiteral("measuretransport");
}
bool MeasureTransportConstructor::isAlreadySelectedOK(const std::vector<ObjectCalcer *> &, const uint &) const
{
return false;
}
/*
* we want the arguments in the exact order, this makes
* the code simpler, but I guess it is also less confusing
* to the user
*/
int MeasureTransportConstructor::wantArgs(const std::vector<ObjectCalcer *> &os, const KigDocument &doc, const KigWidget &) const
{
if (os.size() == 0)
return ArgsParser::Valid;
if (!os[0]->imp()->inherits(&lengthimptypeinstance))
return ArgsParser::Invalid;
if (os.size() == 1)
return ArgsParser::Valid;
if (!os[1]->imp()->inherits(LineImp::stype()) && !os[1]->imp()->inherits(CircleImp::stype()))
return ArgsParser::Invalid;
const CurveImp *c = static_cast<const CurveImp *>(os[1]->imp());
if (os.size() == 2)
return ArgsParser::Valid;
if (!os[2]->imp()->inherits(PointImp::stype()))
return ArgsParser::Invalid;
const PointImp *p = static_cast<const PointImp *>(os[2]->imp());
// we have two choices:
// - using "isPointOnCurve" produces a "by construction" incidence
// test. This would be fine, but doesn't always work; e.g. if we
// have two points A, B, the segment s = AB and we construct the
// support line of the segment (property of segments), then kig
// is not able to understand that A is "by construction" on the
// constructed line.
// Moreover there are problems when hovering the cursor over points
// that are on both a segment and its support line.
// if ( ! isPointOnCurve( os[2], os[1] ) )
// - using "containsPoint", which is actually the test performed
// when calc-ing the TransportOfMeasure; the risk here is to
// be able to select points that are only coincidentally on the line.
if (!c->containsPoint(p->coordinate(), doc))
return ArgsParser::Invalid;
if (os.size() == 3)
return ArgsParser::Complete;
return ArgsParser::Invalid;
}
void MeasureTransportConstructor::handleArgs(const std::vector<ObjectCalcer *> &os, KigPart &d, KigWidget &v) const
{
std::vector<ObjectHolder *> bos = build(os, d.document(), v);
for (std::vector<ObjectHolder *>::iterator i = bos.begin(); i != bos.end(); ++i) {
(*i)->calc(d.document());
}
d.addObjects(bos);
}
void MeasureTransportConstructor::handlePrelim(KigPainter &p, const std::vector<ObjectCalcer *> &os, const KigDocument &d, const KigWidget &) const
{
p.setBrushStyle(Qt::NoBrush);
p.setBrushColor(Qt::red);
p.setPen(QPen(Qt::red, 1));
p.setWidth(-1); // -1 means the default width for the object being
// drawn..
ObjectDrawer drawer(Qt::red);
drawprelim(drawer, p, os, d);
}
void MeasureTransportConstructor::drawprelim(const ObjectDrawer &drawer,
KigPainter &p,
const std::vector<ObjectCalcer *> &parents,
const KigDocument &doc) const
{
Args args;
using namespace std;
transform(parents.begin(), parents.end(), back_inserter(args), std::mem_fn(&ObjectCalcer::imp));
ObjectImp *data = mtype->calc(args, doc);
drawer.draw(*data, p, true);
delete data;
}
QString MeasureTransportConstructor::useText(const ObjectCalcer &o, const std::vector<ObjectCalcer *> &os, const KigDocument &, const KigWidget &) const
{
if (o.imp()->inherits(SegmentImp::stype()))
return i18n("Segment to transport");
if (o.imp()->inherits(ArcImp::stype()))
return i18n("Arc to transport");
if (o.imp()->inherits(NumericTextImp::stype()))
return i18n("Value to transport");
if (o.imp()->inherits(LineImp::stype()))
return i18n("Transport a measure on this line");
if (o.imp()->inherits(CircleImp::stype()))
return i18n("Transport a measure on this circle");
if (o.imp()->inherits(PointImp::stype())) {
if (os[1]->imp()->inherits(CircleImp::stype()))
return i18n("Start transport from this point of the circle");
if (os[1]->imp()->inherits(LineImp::stype()))
return i18n("Start transport from this point of the line");
else
return i18n("Start transport from this point of the curve");
// well, this isn't impemented yet, should never get here
}
return QLatin1String("");
}
QString MeasureTransportConstructor::selectStatement(const std::vector<ObjectCalcer *> &os, const KigDocument &, const KigWidget &) const
{
switch (os.size()) {
case 0:
return i18n("Select a segment, arc or numeric label to be transported...");
break;
case 1:
return i18n("Select a destination line or circle...");
break;
case 2:
return i18n("Choose a starting point on the line/circle...");
break;
}
return QLatin1String("");
}
std::vector<ObjectHolder *> MeasureTransportConstructor::build(const std::vector<ObjectCalcer *> &parents, KigDocument &, KigWidget &) const
{
assert(parents.size() == 3);
// std::vector<ObjectCalcer*> args;
// for ( uint i = 0; i < count; ++i ) args.push_back( parents[i] );
ObjectTypeCalcer *calcer = new ObjectTypeCalcer(mtype, parents);
ObjectHolder *h = new ObjectHolder(calcer);
std::vector<ObjectHolder *> ret;
ret.push_back(h);
return ret;
}
void MeasureTransportConstructor::plug(KigPart *, KigGUIAction *)
{
}
bool MeasureTransportConstructor::isTransform() const
{
return false;
}
/*
* Generic intersection
*/
/*
* these two argsparser spec vectors are used for the special
* construction of conic-line and circle-circle constructions
*/
static const struct ArgsParser::spec argsspeccli[] = {{ConicImp::stype(), kli18n("Intersect with this conic"), {}, true},
{AbstractLineImp::stype(), kli18n("Intersect with this line"), {}, true}};
static const struct ArgsParser::spec argsspeccbli[] = {{CubicImp::stype(), kli18n("Intersect with this cubic"), {}, true},
{AbstractLineImp::stype(), kli18n("Intersect with this line"), {}, true}};
static const struct ArgsParser::spec argsspeccci[] = {{CircleImp::stype(), kli18n("Intersect with this circle"), {}, true},
{CircleImp::stype(), kli18n("Intersect with this circle"), {}, true}};
GenericIntersectionConstructor::GenericIntersectionConstructor()
: MergeObjectConstructor(i18n("Intersect"), i18n("The intersection of two objects"), "curvelineintersection")
{
// intersection type..
// There is one "toplevel" object_constructor, that is composed
// of multiple subconstructors. First we build the
// subconstructors:
SimpleObjectTypeConstructor *lineline =
new SimpleObjectTypeConstructor(LineLineIntersectionType::instance(), {}, {}, "curvelineintersection");
ObjectConstructor *lineconic =
// new ConicLineIntersectionConstructor();
new TwoOrOneIntersectionConstructor(ConicLineIntersectionType::instance(),
ConicLineOtherIntersectionType::instance(),
"curvelineintersection",
argsspeccli);
ObjectConstructor *arcline = new ArcLineIntersectionConstructor();
ObjectConstructor *linecubic = new ThreeTwoOneIntersectionConstructor(CubicLineIntersectionType::instance(),
CubicLineOtherIntersectionType::instance(),
"curvelineintersection",
argsspeccbli);
ObjectConstructor *conicconic = new ConicConicIntersectionConstructor();
// MultiObjectTypeConstructor* circlecircle =
// new MultiObjectTypeConstructor(
// CircleCircleIntersectionType::instance(),
// {}, {},
// "circlecircleintersection", -1, 1 );
ObjectConstructor *circlecircle = new TwoOrOneIntersectionConstructor(CircleCircleIntersectionType::instance(),
CircleCircleOtherIntersectionType::instance(),
"circlecircleintersection",
argsspeccci);
SimpleObjectTypeConstructor *polygonline =
new SimpleObjectTypeConstructor(PolygonLineIntersectionType::instance(), {}, {}, "curvelineintersection");
SimpleObjectTypeConstructor *polygonpolygon =
new SimpleObjectTypeConstructor(PolygonPolygonIntersectionType::instance(), {}, {}, "curvelineintersection");
MultiObjectTypeConstructor *opolygonalline =
new MultiObjectTypeConstructor(OPolygonalLineIntersectionType::instance(), {}, {}, "curvelineintersection", -1, 1);
MultiObjectTypeConstructor *cpolygonalline =
new MultiObjectTypeConstructor(CPolygonalLineIntersectionType::instance(), {}, {}, "curvelineintersection", -1, 1);
merge(lineline);
merge(circlecircle);
merge(lineconic);
merge(linecubic);
merge(conicconic);
merge(arcline);
merge(polygonline);
merge(opolygonalline);
merge(cpolygonalline);
merge(polygonpolygon);
}
GenericIntersectionConstructor::~GenericIntersectionConstructor()
{
}
bool GenericIntersectionConstructor::isIntersection() const
{
return true;
}
QString GenericIntersectionConstructor::useText(const ObjectCalcer &o, const std::vector<ObjectCalcer *> &os, const KigDocument &, const KigWidget &) const
{
QString preamble;
switch (os.size()) {
case 1:
if (o.imp()->inherits(CircleImp::stype()))
return i18n("Intersect this Circle");
else if (o.imp()->inherits(ConicImp::stype()))
return i18n("Intersect this Conic");
else if (o.imp()->inherits(SegmentImp::stype()))
return i18n("Intersect this Segment");
else if (o.imp()->inherits(RayImp::stype()))
return i18n("Intersect this Half-line");
else if (o.imp()->inherits(LineImp::stype()))
return i18n("Intersect this Line");
else if (o.imp()->inherits(CubicImp::stype()))
return i18n("Intersect this Cubic Curve");
else if (o.imp()->inherits(ArcImp::stype()))
return i18n("Intersect this Arc");
else if (o.imp()->inherits(FilledPolygonImp::stype()))
return i18n("Intersect this Polygon");
else if (o.imp()->inherits(AbstractPolygonImp::stype()))
return i18n("Intersect this Polygonal");
else
assert(false);
break;
case 2:
if (o.imp()->inherits(CircleImp::stype()))
return i18n("with this Circle");
else if (o.imp()->inherits(ConicImp::stype()))
return i18n("with this Conic");
else if (o.imp()->inherits(SegmentImp::stype()))
return i18n("with this Segment");
else if (o.imp()->inherits(RayImp::stype()))
return i18n("with this Half-line");
else if (o.imp()->inherits(LineImp::stype()))
return i18n("with this Line");
else if (o.imp()->inherits(CubicImp::stype()))
return i18n("with this Cubic Curve");
else if (o.imp()->inherits(ArcImp::stype()))
return i18n("with this Arc");
else if (o.imp()->inherits(FilledPolygonImp::stype()))
return i18n("with this Polygon");
else if (o.imp()->inherits(AbstractPolygonImp::stype()))
return i18n("with this Polygonal");
else
assert(false);
break;
}
return QString();
}
static const ArgsParser::spec argsspecMidPointOfTwoPoints[] = {{PointImp::stype(),
kli18n("Construct midpoint of this point and another one"),
kli18n("Select the first of the points of which you want to construct the midpoint..."),
false},
{PointImp::stype(),
kli18n("Construct the midpoint of this point and another one"),
kli18n("Select the other of the points of which to construct the midpoint..."),
false}};
MidPointOfTwoPointsConstructor::MidPointOfTwoPointsConstructor()
: StandardConstructorBase("Mid Point", "Construct the midpoint of two points", "bisection", mparser)
, mparser(argsspecMidPointOfTwoPoints, 2)
{
}
MidPointOfTwoPointsConstructor::~MidPointOfTwoPointsConstructor()
{
}
void MidPointOfTwoPointsConstructor::drawprelim(const ObjectDrawer &drawer,
KigPainter &p,
const std::vector<ObjectCalcer *> &parents,
const KigDocument &) const
{
if (parents.size() != 2)
return;
assert(parents[0]->imp()->inherits(PointImp::stype()));
assert(parents[1]->imp()->inherits(PointImp::stype()));
const Coordinate m = (static_cast<const PointImp *>(parents[0]->imp())->coordinate() + static_cast<const PointImp *>(parents[1]->imp())->coordinate()) / 2;
drawer.draw(PointImp(m), p, true);
}
std::vector<ObjectHolder *> MidPointOfTwoPointsConstructor::build(const std::vector<ObjectCalcer *> &os, KigDocument &d, KigWidget &) const
{
ObjectTypeCalcer *seg = new ObjectTypeCalcer(SegmentABType::instance(), os);
seg->calc(d);
// int index = seg->imp()->propertiesInternalNames().indexOf( "mid-point" );
// assert( index != -1 );
ObjectPropertyCalcer *prop = new ObjectPropertyCalcer(seg, "mid-point");
prop->calc(d);
std::vector<ObjectHolder *> ret;
ret.push_back(new ObjectHolder(prop));
return ret;
}
void MidPointOfTwoPointsConstructor::plug(KigPart *, KigGUIAction *)
{
}
bool MidPointOfTwoPointsConstructor::isTransform() const
{
return false;
}
static const ArgsParser::spec argsspecGoldenPointOfTwoPoints[] = {
{PointImp::stype(),
kli18n("Construct golden ratio point of this point and another one"),
kli18n("Select the first of the points of which you want to construct the golden ratio point..."),
false},
{PointImp::stype(),
kli18n("Construct the golden ratio point of this point and another one"),
kli18n("Select the other of the points of which to construct the golden ratio point..."),
false}};
GoldenPointOfTwoPointsConstructor::GoldenPointOfTwoPointsConstructor()
: StandardConstructorBase("Golden Ratio Point", "Construct the golden ratio point of two points", "bisection", mparser)
, mparser(argsspecGoldenPointOfTwoPoints, 2)
{
}
GoldenPointOfTwoPointsConstructor::~GoldenPointOfTwoPointsConstructor()
{
}
void GoldenPointOfTwoPointsConstructor::drawprelim(const ObjectDrawer &drawer,
KigPainter &p,
const std::vector<ObjectCalcer *> &parents,
const KigDocument &) const
{
if (parents.size() != 2)
return;
assert(parents[0]->imp()->inherits(PointImp::stype()));
assert(parents[1]->imp()->inherits(PointImp::stype()));
const Coordinate m =
(static_cast<const PointImp *>(parents[0]->imp())->coordinate()
+ (sqrt(5) - 1) / 2
* (static_cast<const PointImp *>(parents[1]->imp())->coordinate() - static_cast<const PointImp *>(parents[0]->imp())->coordinate()));
drawer.draw(PointImp(m), p, true);
}
std::vector<ObjectHolder *> GoldenPointOfTwoPointsConstructor::build(const std::vector<ObjectCalcer *> &os, KigDocument &d, KigWidget &) const
{
ObjectTypeCalcer *seg = new ObjectTypeCalcer(SegmentABType::instance(), os);
seg->calc(d);
// int index = seg->imp()->propertiesInternalNames().indexOf( "golden-point" );
// assert( index != -1 );
ObjectPropertyCalcer *prop = new ObjectPropertyCalcer(seg, "golden-point");
prop->calc(d);
std::vector<ObjectHolder *> ret;
ret.push_back(new ObjectHolder(prop));
return ret;
}
void GoldenPointOfTwoPointsConstructor::plug(KigPart *, KigGUIAction *)
{
}
bool GoldenPointOfTwoPointsConstructor::isTransform() const
{
return false;
}
TestConstructor::TestConstructor(const ArgsParserObjectType *type, const QString &descname, const QString &desc, const QString &iconfile)
: StandardConstructorBase(descname, desc, iconfile, type->argsParser())
, mtype(type)
{
}
TestConstructor::~TestConstructor()
{
}
void TestConstructor::drawprelim(const ObjectDrawer &, KigPainter &, const std::vector<ObjectCalcer *> &, const KigDocument &) const
{
// not used, only here because of the wrong
// ObjectConstructor-GUIAction design. See the TODO
}
std::vector<ObjectHolder *> TestConstructor::build(const std::vector<ObjectCalcer *> &, KigDocument &, KigWidget &) const
{
// not used, only here because of the wrong
// ObjectConstructor-GUIAction design. See the TODO
std::vector<ObjectHolder *> ret;
return ret;
}
void TestConstructor::plug(KigPart *, KigGUIAction *)
{
}
bool TestConstructor::isTransform() const
{
return false;
}
bool TestConstructor::isTest() const
{
return true;
}
BaseConstructMode *TestConstructor::constructMode(KigPart &doc)
{
return new TestConstructMode(doc, mtype);
}
int TestConstructor::wantArgs(const std::vector<ObjectCalcer *> &os, const KigDocument &d, const KigWidget &v) const
{
int ret = StandardConstructorBase::wantArgs(os, d, v);
if (ret == ArgsParser::Complete)
ret = ArgsParser::Valid;
return ret;
}
QString GenericIntersectionConstructor::selectStatement(const std::vector<ObjectCalcer *> &sel, const KigDocument &, const KigWidget &) const
{
if (sel.size() == 0)
return i18n("Select the first object to intersect...");
else
return i18n("Select the second object to intersect...");
}
TangentConstructor::TangentConstructor()
: MergeObjectConstructor(i18n("Tangent"), i18n("The line tangent to a curve"), "tangent")
{
SimpleObjectTypeConstructor *conic = new SimpleObjectTypeConstructor(TangentConicType::instance(), {}, {}, "tangentconic");
SimpleObjectTypeConstructor *arc = new SimpleObjectTypeConstructor(TangentArcType::instance(), {}, {}, "tangentarc");
SimpleObjectTypeConstructor *cubic = new SimpleObjectTypeConstructor(TangentCubicType::instance(), {}, {}, "tangentcubic");
SimpleObjectTypeConstructor *curve = new SimpleObjectTypeConstructor(TangentCurveType::instance(), {}, {}, "tangentcurve");
merge(conic);
merge(arc);
merge(cubic);
merge(curve);
}
TangentConstructor::~TangentConstructor()
{
}
QString TangentConstructor::useText(const ObjectCalcer &o, const std::vector<ObjectCalcer *> &, const KigDocument &, const KigWidget &) const
{
if (o.imp()->inherits(CircleImp::stype()))
return i18n("Tangent to This Circle");
else if (o.imp()->inherits(ConicImp::stype()))
return i18n("Tangent to This Conic");
else if (o.imp()->inherits(ArcImp::stype()))
return i18n("Tangent to This Arc");
else if (o.imp()->inherits(CubicImp::stype()))
return i18n("Tangent to This Cubic Curve");
else if (o.imp()->inherits(CurveImp::stype()))
return i18n("Tangent to This Curve");
else if (o.imp()->inherits(PointImp::stype()))
return i18n("Tangent at This Point");
// else assert( false );
return QString();
}
// QString TangentConstructor::selectStatement(
// const std::vector<ObjectCalcer*>& sel, const KigDocument&,
// const KigWidget& ) const
//{
// if ( sel.size() == 0 )
// return i18n( "Select the object..." );
// else
// return i18n( "Select the point for the tangent to go through..." );
// }
/*
* center of curvature of a curve
*/
CocConstructor::CocConstructor()
: MergeObjectConstructor(i18n("Center Of Curvature"), i18n("The center of the osculating circle to a curve"), "centerofcurvature")
{
SimpleObjectTypeConstructor *conic = new SimpleObjectTypeConstructor(CocConicType::instance(), {}, {}, "cocconic");
SimpleObjectTypeConstructor *cubic = new SimpleObjectTypeConstructor(CocCubicType::instance(), {}, {}, "coccubic");
SimpleObjectTypeConstructor *curve = new SimpleObjectTypeConstructor(CocCurveType::instance(), {}, {}, "coccurve");
merge(conic);
merge(cubic);
merge(curve);
}
CocConstructor::~CocConstructor()
{
}
QString CocConstructor::useText(const ObjectCalcer &o, const std::vector<ObjectCalcer *> &, const KigDocument &, const KigWidget &) const
{
if (o.imp()->inherits(ConicImp::stype()))
return i18n("Center of Curvature of This Conic");
else if (o.imp()->inherits(CubicImp::stype()))
return i18n("Center of Curvature of This Cubic Curve");
else if (o.imp()->inherits(CurveImp::stype()))
return i18n("Center of Curvature of This Curve");
else if (o.imp()->inherits(PointImp::stype()))
return i18n("Center of Curvature at This Point");
return QString();
}
bool relativePrimes(int n, int p)
{
if (p > n)
return relativePrimes(p, n);
assert(p >= 0);
if (p == 0)
return false;
if (p == 1)
return true;
int d = int(n / p);
return relativePrimes(p, n - d * p);
}
// QString CocConstructor::selectStatement(
// const std::vector<ObjectCalcer*>& sel, const KigDocument&,
// const KigWidget& ) const
//{
// if ( sel.size() == 0 )
// return i18n( "Select the object..." );
// else
// return i18n( "Select the point where to compute the center of curvature..." );
// }
|