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
|
# -*- coding: utf-8 -*-
##############################################################################
#
# Copyright (c) 2003-2020 by The University of Queensland
# http://www.uq.edu.au
#
# Primary Business: Queensland, Australia
# Licensed under the Apache License, version 2.0
# http://www.apache.org/licenses/LICENSE-2.0
#
# Development until 2012 by Earth Systems Science Computational Center (ESSCC)
# Development 2012-2013 by School of Earth Sciences
# Development from 2014 by Centre for Geoscience Computing (GeoComp)
# Development from 2019 by School of Earth and Environmental Sciences
#
##############################################################################
from __future__ import print_function, division
__copyright__="""Copyright (c) 2003-2020 by The University of Queensland
http://www.uq.edu.au
Primary Business: Queensland, Australia"""
__license__="""Licensed under the Apache License, version 2.0
http://www.apache.org/licenses/LICENSE-2.0"""
__url__="https://launchpad.net/escript-finley"
"""
Geometrical Primitives
the concept is inspired by gmsh and very much focused on the fact that
the classes are used to wrk with gmsh.
:var __author__: name of author
:var __copyright__: copyrights
:var __license__: licence agreement
:var __url__: url entry point on documentation
:var __version__: version
:var __date__: date of the version
"""
__author__="Lutz Gross, l.gross@uq.edu.au"
try:
import numpy
numpyImported=True
except:
numpyImported=False
import numpy
from .transformations import _TYPE, Translation, Dilation, Transformation, DEG
import math
def resetGlobalPrimitiveIdCounter():
"""
Initializes the global primitive ID counter.
"""
global global_primitive_id_counter
global_primitive_id_counter=1
def setToleranceForColocation(tol=1.e-11):
"""
Sets the global tolerance for colocation checks to ``tol``.
"""
global global_tolerance_for_colocation
global_tolerance_for_colocation=tol
def getToleranceForColocation():
"""
Returns the global tolerance for colocation checks.
"""
return global_tolerance_for_colocation
resetGlobalPrimitiveIdCounter()
setToleranceForColocation()
class PrimitiveBase(object):
"""
Template for a set of primitives.
"""
def __init__(self):
"""
Initializes the PrimitiveBase instance object.
"""
pass
# for python2
def __cmp__(self,other):
"""
Compares object with other by comparing the absolute value of the ID.
"""
if isinstance(other, PrimitiveBase):
return cmp(self.getID(),other.getID())
else:
return -1
def __lt__(self,other):
if isinstance(other, PrimitiveBase):
return self.getID()<other.getID()
else:
return False
def __eq__(self,other):
if isinstance(other, PrimitiveBase):
return self.getID()==other.getID()
else:
return False
def __hash__(self):
return self.getID()
def getConstructionPoints(self):
"""
Returns the points used to construct the primitive.
"""
out=[]
for i in self.getPrimitives():
if isinstance(i,Point): out.append(i)
return out
def getPrimitives(self):
"""
Returns a list of primitives used to construct the primitive with no
double entries.
"""
out=[]
for p in self.collectPrimitiveBases():
if not p in out: out.append(p)
return out
def copy(self):
"""
Returns a deep copy of the object.
"""
return self.substitute({})
def modifyBy(self,transformation):
"""
Modifies the coordinates by applying a transformation.
"""
for p in self.getConstructionPoints(): p.modifyBy(transformation)
def __add__(self,other):
"""
Returns a new object shifted by ``other``.
"""
return self.apply(Translation(numpy.array(other,_TYPE)))
def __sub__(self,other):
"""
Returns a new object shifted by ``-other``.
"""
return self.apply(Translation(-numpy.array(other,_TYPE)))
def __iadd__(self,other):
"""
Shifts the point inplace by ``other``.
"""
self.modifyBy(Translation(numpy.array(other,_TYPE)))
return self
def __isub__(self,other):
"""
Shifts the point inplace by ``-other``.
"""
self.modifyBy(Translation(-numpy.array(other,_TYPE)))
return self
def __imul__(self,other):
"""
Modifies object by applying `Transformation` ``other``. If ``other``
is not a `Transformation` it is first tried to be converted.
"""
if isinstance(other,int) or isinstance(other,float):
trafo=Dilation(other)
elif isinstance(other,numpy.ndarray):
trafo=Translation(other)
elif isinstance(other,Transformation):
trafo=other
else:
raise TypeError("cannot convert argument to a Transformation class object.")
self.modifyBy(trafo)
return self
def __rmul__(self,other):
"""
Applies `Transformation` ``other`` to object. If ``other`` is not a
`Transformation` it is first tried to be converted.
"""
if isinstance(other,int) or isinstance(other,float):
trafo=Dilation(other)
elif isinstance(other,numpy.ndarray):
trafo=Translation(other)
elif isinstance(other,Transformation):
trafo=other
else:
raise TypeError("cannot convert argument to Transformation class object.")
return self.apply(trafo)
def setLocalScale(self,factor=1.):
"""
Sets the local refinement factor.
"""
for p in self.getConstructionPoints(): p.setLocalScale(factor)
def apply(self,transformation):
"""
Returns a new object by applying the transformation.
"""
out=self.copy()
out.modifyBy(transformation)
return out
class Primitive(object):
"""
Class that represents a general primitive.
"""
def __init__(self):
"""
Initializes the Primitive instance object with a unique ID.
"""
global global_primitive_id_counter
self.__ID=global_primitive_id_counter
global_primitive_id_counter+=1
def getID(self):
"""
Returns the primitive ID.
"""
return self.__ID
def getDirectedID(self):
"""
Returns the primitive ID where a negative sign means that reversed
ordering is used.
"""
return self.getID()
def __repr__(self):
return "%s(%s)"%(self.__class__.__name__,self.getID())
def getUnderlyingPrimitive(self):
"""
Returns the underlying primitive.
"""
return self
def hasSameOrientation(self,other):
"""
Returns True if ``other`` is the same primitive and has the same
orientation, False otherwise.
"""
return self == other and isinstance(other,Primitive)
def __neg__(self):
"""
Returns a view onto the curve with reversed ordering.
:note: This method is overwritten by subclasses.
"""
raise NotImplementedError("__neg__ is not implemented.")
def substitute(self,sub_dict):
"""
Returns a copy of self with substitutes for the primitives used to
construct it given by the dictionary ``sub_dict``. If a substitute for
the object is given by ``sub_dict`` the value is returned, otherwise a
new instance with substituted arguments is returned.
:note: This method is overwritten by subclasses.
"""
raise NotImplementedError("substitute is not implemented.")
def collectPrimitiveBases(self):
"""
Returns a list of primitives used to construct the primitive. It may
contain primitives twice.
:note: This method is overwritten by subclasses.
"""
raise NotImplementedError("collectPrimitiveBases is not implemented.")
def isColocated(self,primitive):
"""
Returns True if the two primitives are located at the same position.
:note: This method is overwritten by subclasses.
"""
raise NotImplementedError("isCollocated is not implemented.")
def isReversed(self):
"""
returns True is the primitive is a reversed primitive.
"""
return False
class ReversePrimitive(object):
"""
A view onto a primitive creating a reverse orientation.
"""
def __init__(self,primitive):
"""
Instantiates a view onto ``primitive``.
"""
if not isinstance(primitive, Primitive):
raise ValueError("argument needs to be a Primitive class object.")
self.__primitive=primitive
def getID(self):
"""
Returns the primitive ID.
"""
return self.__primitive.getID()
def getUnderlyingPrimitive(self):
"""
Returns the underlying primitive.
"""
return self.__primitive
def hasSameOrientation(self,other):
"""
Returns True if ``other`` is the same primitive and has the same
orientation as self.
"""
return self == other and isinstance(other, ReversePrimitive)
def __repr__(self):
return "-%s(%s)"%(self.__primitive.__class__.__name__,self.getID())
def getDirectedID(self):
"""
Returns the primitive ID where a negative signs means that reversed
ordering is used.
"""
return -self.__primitive.getID()
def substitute(self,sub_dict):
"""
Returns a copy of self with substitutes for the primitives used to
construct it given by the dictionary ``sub_dict``. If a substitute for
the object is given by ``sub_dict`` the value is returned, otherwise a
new instance with substituted arguments is returned.
"""
if self not in sub_dict:
sub_dict[self]=-self.getUnderlyingPrimitive().substitute(sub_dict)
return sub_dict[self]
def __neg__(self):
"""
Returns a view onto the curve with reversed ordering.
"""
return self.__primitive
def collectPrimitiveBases(self):
"""
Returns a list of primitives used to construct the primitive. It may
contain primitives twice.
"""
return self.__primitive.collectPrimitiveBases()
def isColocated(self,primitive):
"""
Returns True if the two primitives are located at the same position.
:note: This method is overwritten by subclasses.
"""
return self.__primitive.isColocated(primitive)
def isReversed(self):
"""
returns True is the primitive is a reversed primitive.
"""
return True
class Point(Primitive, PrimitiveBase):
"""
A three-dimensional point.
"""
def __init__(self,x=0.,y=0.,z=0.,local_scale=1.):
"""
Creates a point with coordinates ``x``, ``y``, ``z`` with the local
refinement factor ``local_scale``. If ``x`` is a list or similar it needs to have
length less or equal 3. In this case ``y`` and ``z`` are overwritten by
``x[1]`` and ``x[2]``.
"""
PrimitiveBase.__init__(self)
Primitive.__init__(self)
try:
l=len(x)
if l>3:
raise ValueError("x has a lanegth bigger than 3.")
if l>1:
y=x[1]
else:
y=0.
if l>2:
z=x[2]
else:
z=0.
if l>0:
x=x[0]
else:
x=0.
except TypeError:
pass
a=numpy.array([x,y,z], _TYPE)
self.setCoordinates(a)
self.setLocalScale(local_scale)
def setLocalScale(self,factor=1.):
"""
Sets the local refinement factor.
"""
if factor<=0.:
raise ValueError("scaling factor must be positive.")
self.__local_scale=factor
def getLocalScale(self):
"""
Returns the local refinement factor.
"""
return self.__local_scale
def getCoordinates(self):
"""
Returns the coordinates of the point as a ``numpy.ndarray`` object.
"""
return self._x
def getCoordinatesAsList(self):
"""
Returns the coordinates of the point as a ``list`` object.
"""
return [self._x[0], self._x[1], self._x[2] ]
def setCoordinates(self,x):
"""
Sets the coordinates of the point from a ``numpy.ndarray`` object ``x``.
"""
if not isinstance(x, numpy.ndarray):
self._x=numpy.array(x,_TYPE)
else:
self._x=x
def collectPrimitiveBases(self):
"""
Returns primitives used to construct the primitive.
"""
return [self]
def isColocated(self,primitive):
"""
Returns True if the `Point` ``primitive`` is collocated (has the same
coordinates) with self. That is, if
*|self - primitive| <= tol * max(\|self\|,|primitive|)*.
"""
if isinstance(primitive,Point):
primitive=primitive.getCoordinates()
c=self.getCoordinates()
d=c-primitive
if numpyImported:
return numpy.dot(d,d)<=getToleranceForColocation()**2*max(numpy.dot(c,c),numpy.dot(primitive,primitive))
else:
return numpy.dot(d,d)<=getToleranceForColocation()**2*max(numpy.dot(c,c),numpy.dot(primitive,primitive))
else:
return False
def substitute(self,sub_dict):
"""
Returns a copy of self with substitutes for the primitives used to
construct it given by the dictionary ``sub_dict``. If a substitute for
the object is given by ``sub_dict`` the value is returned, otherwise a
new instance with substituted arguments is returned.
"""
if self not in sub_dict:
c=self.getCoordinates()
sub_dict[self]=Point(c[0],c[1],c[2],local_scale=self.getLocalScale())
return sub_dict[self]
def modifyBy(self,transformation):
"""
Modifies the coordinates by applying the given transformation.
"""
self.setCoordinates(transformation(self.getCoordinates()))
def __neg__(self):
"""
Returns a view of the object with reverse orientation. As a point has
no direction the object itself is returned.
"""
return self
class Manifold1D(PrimitiveBase):
"""
General one-dimensional manifold in 1D defined by a start and end point.
"""
def __init__(self):
"""
Initializes the one-dimensional manifold.
"""
PrimitiveBase.__init__(self)
self.__apply_elements=False
def getStartPoint(self):
"""
Returns the start point.
"""
raise NotImplementedError()
def getEndPoint(self):
"""
Returns the end point.
"""
raise NotImplementedError()
def getBoundary(self):
"""
Returns a list of the zero-dimensional manifolds forming the boundary
of the curve.
"""
return [ self.getStartPoint(), self.getEndPoint()]
def setElementDistribution(self,n,progression=1,createBump=False):
"""
Defines the number of elements on the line. If set it overwrites the local length setting which would be applied.
The progression factor ``progression`` defines the change of element size between neighboured elements. If ``createBump`` is set
progression is applied towards the center of the line.
:param n: number of elements on the line
:type n: ``int``
:param progression: a positive progression factor
:type progression: positive ``float``
:param createBump: of elements on the line
:type createBump: ``bool``
"""
if isinstance(self, ReversePrimitive):
self.getUnderlyingPrimitive().setElementDistribution(n,progression,createBump)
else:
if n<1:
raise ValueError("number of elements must be positive.")
if progression<=0:
raise ValueError("progression factor must be positive.")
self.__apply_elements=True
self.__n=n
self.__progression_factor=progression
self.__createBump=createBump
def resetElementDistribution(self):
"""
removes the a previously set element distribution from the line.
"""
if isinstance(self, ReversePrimitive):
self.getUnderlyingPrimitive().resetElementDistribution()
else:
self.__apply_elements=False
def getElementDistribution(self):
"""
Returns the element distribution.
:return: the tuple of the number of elements, the progression factor and the bump flag. If no element distribution is set ``None`` is returned
:rtype: ``tuple``
"""
if isinstance(self, ReversePrimitive):
return self.getUnderlyingPrimitive().getElementDistribution()
else:
if self.__apply_elements:
return (self.__n, self.__progression_factor, self.__createBump)
else:
return None
class CurveBase(Manifold1D):
"""
Base class for curves. A Curve is defined by a set of control points.
"""
def __init__(self):
"""
Initializes the curve.
"""
Manifold1D.__init__(self)
def __len__(self):
"""
Returns the number of control points.
"""
return len(self.getControlPoints())
def getStartPoint(self):
"""
Returns the start point.
"""
return self.getControlPoints()[0]
def getEndPoint(self):
"""
Returns the end point.
"""
return self.getControlPoints()[-1]
def getControlPoints(self):
"""
Returns a list of the points.
"""
raise NotImplementedError()
class Curve(CurveBase, Primitive):
"""
A curve defined through a list of control points.
"""
def __init__(self,*points):
"""
Defines a curve from control points given by ``points``.
"""
if len(points)==1:
points=points[0]
if not hasattr(points,'__iter__'): raise ValueError("Curve needs at least two points")
if len(points)<2:
raise ValueError("Curve needs at least two points")
i=0
for p in points:
i+=1
if not isinstance(p,Point): raise TypeError("%s-th argument is not a Point object."%i)
self.__points=points
CurveBase.__init__(self)
Primitive.__init__(self)
def getControlPoints(self):
"""
Returns a list of the points.
"""
return self.__points
def __neg__(self):
"""
Returns a view onto the curve with reversed ordering.
"""
return ReverseCurve(self)
def substitute(self,sub_dict):
"""
Returns a copy of self with substitutes for the primitives used to
construct it given by the dictionary ``sub_dict``. If a substitute for
the object is given by ``sub_dict`` the value is returned, otherwise a
new instance with substituted arguments is returned.
"""
if self not in sub_dict:
new_p=[]
for p in self.getControlPoints(): new_p.append(p.substitute(sub_dict))
sub_dict[self]=self.__class__(*tuple(new_p))
return sub_dict[self]
def collectPrimitiveBases(self):
"""
Returns the primitives used to construct the curve.
"""
out=[self]
for p in self.getControlPoints(): out+=p.collectPrimitiveBases()
return out
def isColocated(self,primitive):
"""
Returns True if curves are at the same position.
"""
if hasattr(primitive,"getUnderlyingPrimitive"):
if isinstance(primitive.getUnderlyingPrimitive(),self.__class__):
if len(primitive) == len(self):
cp0=self.getControlPoints()
cp1=primitive.getControlPoints()
match=True
for i in range(len(cp0)):
if not cp0[i].isColocated(cp1[i]):
match=False
break
if not match:
for i in range(len(cp0)):
if not cp0[i].isColocated(cp1[len(cp0)-1-i]):
return False
return True
return False
class ReverseCurve(CurveBase, ReversePrimitive):
"""
A curve defined through a list of control points.
"""
def __init__(self,curve):
"""
Defines a curve from control points.
"""
if not isinstance(curve, Curve):
raise TypeError("ReverseCurve needs to be an instance of Curve")
CurveBase.__init__(self)
ReversePrimitive.__init__(self,curve)
def getControlPoints(self):
"""
Returns a list of the points.
"""
out=[p for p in self.getUnderlyingPrimitive().getControlPoints()]
out.reverse()
return tuple(out)
class Spline(Curve):
"""
A spline curve defined through a list of control points.
"""
pass
class BezierCurve(Curve):
"""
A Bezier curve.
"""
pass
class BSpline(Curve):
"""
A BSpline curve. Control points may be repeated.
"""
pass
class Line(Curve):
"""
A line is defined by two points.
"""
def __init__(self,*points):
"""
Defines a line with start and end point.
"""
if len(points)!=2:
raise TypeError("Line needs two points")
Curve.__init__(self,*points)
class ArcBase(Manifold1D):
"""
Base class for arcs.
"""
def __init__(self):
"""
Initializes the arc.
"""
Manifold1D.__init__(self)
def collectPrimitiveBases(self):
"""
Returns the primitives used to construct the Arc.
"""
out=[self]
out+=self.getStartPoint().collectPrimitiveBases()
out+=self.getEndPoint().collectPrimitiveBases()
out+=self.getCenterPoint().collectPrimitiveBases()
return out
def getCenterPoint(self):
"""
Returns the center.
"""
raise NotImplementedError()
class Arc(ArcBase, Primitive):
"""
Defines an arc which is strictly smaller than pi.
"""
def __init__(self,center,start,end):
"""
Creates an arc defined by the start point, end point and center.
"""
if not isinstance(center,Point): raise TypeError("center needs to be a Point object.")
if not isinstance(end,Point): raise TypeError("end needs to be a Point object.")
if not isinstance(start,Point): raise TypeError("start needs to be a Point object.")
if center.isColocated(end): raise TypeError("center and start point are collocated.")
if center.isColocated(start): raise TypeError("center end end point are collocated.")
if start.isColocated(end): raise TypeError("start and end are collocated.")
# TODO: check length of circle.
ArcBase.__init__(self)
Primitive.__init__(self)
self.__center=center
self.__start=start
self.__end=end
def __neg__(self):
"""
Returns a view onto the curve with reversed ordering.
"""
return ReverseArc(self)
def getStartPoint(self):
"""
Returns the start point.
"""
return self.__start
def getEndPoint(self):
"""
Returns the end point.
"""
return self.__end
def getCenterPoint(self):
"""
Returns the center point.
"""
return self.__center
def substitute(self,sub_dict):
"""
Returns a copy of self with substitutes for the primitives used to
construct it given by the dictionary ``sub_dict``. If a substitute for
the object is given by ``sub_dict`` the value is returned, otherwise a
new instance with substituted arguments is returned.
"""
if self not in sub_dict:
sub_dict[self]=Arc(self.getCenterPoint().substitute(sub_dict),self.getStartPoint().substitute(sub_dict),self.getEndPoint().substitute(sub_dict))
return sub_dict[self]
def isColocated(self,primitive):
"""
Returns True if curves are at the same position.
"""
if hasattr(primitive,"getUnderlyingPrimitive"):
if isinstance(primitive.getUnderlyingPrimitive(),Arc):
return (self.getCenterPoint().isColocated(primitive.getCenterPoint())) and ( \
(self.getEndPoint().isColocated(primitive.getEndPoint()) and self.getStartPoint().isColocated(primitive.getStartPoint()) ) \
or (self.getEndPoint().isColocated(primitive.getStartPoint()) and self.getStartPoint().isColocated(primitive.getEndPoint()) ) )
return False
class ReverseArc(ArcBase, ReversePrimitive):
"""
Defines an arc which is strictly smaller than pi.
"""
def __init__(self,arc):
"""
Creates an arc defined by the start point, end point and center.
"""
if not isinstance(arc, Arc):
raise TypeError("ReverseCurve needs to be an instance of Arc")
ArcBase.__init__(self)
ReversePrimitive.__init__(self,arc)
def getStartPoint(self):
"""
Returns the start point.
"""
return self.getUnderlyingPrimitive().getEndPoint()
def getEndPoint(self):
"""
Returns the end point.
"""
return self.getUnderlyingPrimitive().getStartPoint()
def getCenterPoint(self):
"""
Returns the center point.
"""
return self.getUnderlyingPrimitive().getCenterPoint()
class EllipseBase(Manifold1D):
"""
Base class for ellipses.
"""
def __init__(self):
"""
Initializes the ellipse.
"""
Manifold1D.__init__(self)
def collectPrimitiveBases(self):
"""
Returns the primitives used to construct the ellipse.
"""
out=[self]
out+=self.getStartPoint().collectPrimitiveBases()
out+=self.getEndPoint().collectPrimitiveBases()
out+=self.getCenterPoint().collectPrimitiveBases()
out+=self.getPointOnMainAxis().collectPrimitiveBases()
return out
class Ellipse(EllipseBase, Primitive):
"""
Defines an ellipse which is strictly smaller than pi.
"""
def __init__(self,center,point_on_main_axis,start,end):
"""
Creates an ellipse defined by the start point, end point, the center
and a point on the main axis.
"""
if not isinstance(center,Point): raise TypeError("center needs to be a Point object.")
if not isinstance(end,Point): raise TypeError("end needs to be a Point object.")
if not isinstance(start,Point): raise TypeError("start needs to be a Point object.")
if not isinstance(point_on_main_axis,Point): raise TypeError("point on main axis needs to be a Point object.")
if center.isColocated(end): raise TypeError("center and start point are collocated.")
if center.isColocated(start): raise TypeError("center end end point are collocated.")
if center.isColocated(point_on_main_axis): raise TypeError("center and point on main axis are colocated.")
if start.isColocated(end): raise TypeError("start and end point are collocated.")
# TODO: check length of circle.
EllipseBase.__init__(self)
Primitive.__init__(self)
self.__center=center
self.__start=start
self.__end=end
self.__point_on_main_axis=point_on_main_axis
def __neg__(self):
"""
Returns a view onto the curve with reversed ordering.
"""
return ReverseEllipse(self)
def getStartPoint(self):
"""
Returns the start point.
"""
return self.__start
def getEndPoint(self):
"""
Returns the end point.
"""
return self.__end
def getCenterPoint(self):
"""
Returns the center.
"""
return self.__center
def getPointOnMainAxis(self):
"""
Returns a point on the main axis.
"""
return self.__point_on_main_axis
def substitute(self,sub_dict):
"""
Returns a copy of self with substitutes for the primitives used to
construct it given by the dictionary ``sub_dict``. If a substitute for
the object is given by ``sub_dict`` the value is returned, otherwise a
new instance with substituted arguments is returned.
"""
if self not in sub_dict:
sub_dict[self]=Ellipse(self.getCenterPoint().substitute(sub_dict),
self.getPointOnMainAxis().substitute(sub_dict),
self.getStartPoint().substitute(sub_dict),
self.getEndPoint().substitute(sub_dict))
return sub_dict[self]
def isColocated(self,primitive):
"""
Returns True if curves are at the same position.
"""
if hasattr(primitive,"getUnderlyingPrimitive"):
if isinstance(primitive.getUnderlyingPrimitive(),Ellipse):
self_c=self.getCenterPoint().getCoordinates()
p=self.getPointOnMainAxis().getCoordinates()-self_c
q=primitive.getPointOnMainAxis().getCoordinates()-self_c
# are p and q orthogonal or collinear?
len_p=math.sqrt(p[0]**2+p[1]**2+p[2]**2)
len_q=math.sqrt(q[0]**2+q[1]**2+q[2]**2)
p_q= abs(p[0]*q[0]+p[1]*q[1]+p[2]*q[2])
return ((p_q <= getToleranceForColocation() * len_q * p_q) or \
(abs(p_q - len_q * p_q) <= getToleranceForColocation())) and \
self.getCenterPoint().isColocated(primitive.getCenterPoint()) and \
( \
(self.getEndPoint().isColocated(primitive.getEndPoint()) and \
self.getStartPoint().isColocated(primitive.getStartPoint()) ) \
or \
(self.getEndPoint().isColocated(primitive.getStartPoint()) and \
self.getStartPoint().isColocated(primitive.getEndPoint())) \
)
return False
class ReverseEllipse(EllipseBase, ReversePrimitive):
"""
Defines an ellipse which is strictly smaller than pi.
"""
def __init__(self,arc):
"""
Creates an instance of a reverse view to an ellipse.
"""
if not isinstance(arc, Ellipse):
raise TypeError("ReverseCurve needs to be an instance of Ellipse")
EllipseBase.__init__(self)
ReversePrimitive.__init__(self,arc)
def getStartPoint(self):
"""
Returns the start point.
"""
return self.getUnderlyingPrimitive().getEndPoint()
def getEndPoint(self):
"""
Returns the end point.
"""
return self.getUnderlyingPrimitive().getStartPoint()
def getCenterPoint(self):
"""
Returns the center point.
"""
return self.getUnderlyingPrimitive().getCenterPoint()
def getPointOnMainAxis(self):
"""
Returns a point on the main axis.
"""
return self.getUnderlyingPrimitive().getPointOnMainAxis()
class CurveLoop(Primitive, PrimitiveBase):
"""
An oriented loop of one-dimensional manifolds (= curves and arcs).
The loop must be closed and the `Manifold1D` s should be oriented
consistently.
"""
def __init__(self,*curves):
"""
Creates a polygon from a list of line curves. The curves must form a
closed loop.
"""
if len(curves)==1:
curves=curves[0]
if not hasattr(curves,'__iter__'): raise ValueError("CurveLoop needs at least two points")
if len(curves)<2:
raise ValueError("At least two curves have to be given.")
for i in range(len(curves)):
if not isinstance(curves[i],Manifold1D):
raise TypeError("%s-th argument is not a Manifold1D object."%i)
# for the curves a loop:
#used=[ False for i in curves]
self.__curves=[]
for c in curves:
if not c in self.__curves: self.__curves.append(c)
Primitive.__init__(self)
PrimitiveBase.__init__(self)
def getCurves(self):
"""
Returns the curves defining the CurveLoop.
"""
return self.__curves
def __neg__(self):
"""
Returns a view onto the curve with reversed ordering.
"""
return ReverseCurveLoop(self)
def __len__(self):
"""
Returns the number of curves in the CurveLoop.
"""
return len(self.getCurves())
def collectPrimitiveBases(self):
"""
Returns primitives used to construct the CurveLoop.
"""
out=[self]
for c in self.getCurves(): out+=c.collectPrimitiveBases()
return out
def substitute(self,sub_dict):
"""
Returns a copy of self with substitutes for the primitives used to
construct it given by the dictionary ``sub_dict``. If a substitute for
the object is given by ``sub_dict`` the value is returned, otherwise a
new instance with substituted arguments is returned.
"""
if self not in sub_dict:
new_c=[]
for c in self.getCurves(): new_c.append(c.substitute(sub_dict))
sub_dict[self]=CurveLoop(*tuple(new_c))
return sub_dict[self]
def isColocated(self,primitive):
"""
Returns True if each curve is collocated with a curve in ``primitive``.
"""
if hasattr(primitive,"getUnderlyingPrimitive"):
if isinstance(primitive.getUnderlyingPrimitive(),CurveLoop):
if len(primitive) == len(self):
cp0=self.getCurves()
cp1=primitive.getCurves()
for c0 in cp0:
colocated = False
for c1 in cp1:
colocated = colocated or c0.isColocated(c1)
if not colocated: return False
return True
return False
def getPolygon(self):
"""
Returns a list of start/end points of the 1D manifold from the loop.
If not closed an exception is thrown.
"""
curves=self.getCurves()
s=[curves[0].getStartPoint(), curves[0].getEndPoint()]
found= [ curves[0], ]
restart=True
while restart:
restart=False
for k in curves:
if not k in found:
if k.getStartPoint() == s[-1]:
found.append(k)
if hasattr(k,"getControlPoints"): s+=k.getControlPoints()[1:-1]
if k.getEndPoint() == s[0]:
if len(found) == len(curves):
return s
else:
raise ValueError("loop %s is not closed."%self.getID())
s.append(k.getEndPoint())
restart=True
break
if not restart:
raise ValueError("loop %s is not closed."%self.getID())
class ReverseCurveLoop(ReversePrimitive, PrimitiveBase):
"""
An oriented loop of one-dimensional manifolds (= curves and arcs).
The loop must be closed and the one-dimensional manifolds should be
oriented consistently.
"""
def __init__(self,curve_loop):
"""
Creates a polygon from a list of line curves. The curves must form a
closed loop.
"""
if not isinstance(curve_loop, CurveLoop):
raise TypeError("arguments need to be an instance of CurveLoop.")
ReversePrimitive.__init__(self, curve_loop)
PrimitiveBase.__init__(self)
def getCurves(self):
"""
Returns the curves defining the CurveLoop.
"""
return [ -c for c in self.getUnderlyingPrimitive().getCurves() ]
def __len__(self):
return len(self.getUnderlyingPrimitive())
#=
class Manifold2D(PrimitiveBase):
"""
General two-dimensional manifold.
:note: Instance variable LEFT - left element orientation when meshing with transfinite meshing
:note: Instance variable RIGHT - right element orientation when meshing with transfinite meshing
:note: Instance variable ALTERNATE - alternate element orientation when meshing with transfinite meshing
"""
LEFT="Left"
RIGHT="Right"
ALTERNATE="Alternate"
def __init__(self):
"""
Creates a two-dimensional manifold.
"""
PrimitiveBase.__init__(self)
self.__transfinitemeshing=False
self.__recombination_angle=None
def getBoundary(self):
"""
Returns a list of the one-dimensional manifolds forming the boundary
of the surface (including holes).
"""
raise NotImplementedError()
def hasHole(self):
"""
Returns True if a hole is present.
"""
raise NotImplementedError()
def setElementDistribution(self,n,progression=1,createBump=False):
"""
Defines the number of elements on the lines
:param n: number of elements on the line
:type n: ``int``
:param progression: a positive progression factor
:type progression: positive ``float``
:param createBump: of elements on the line
:type createBump: ``bool``
"""
for i in self.getBoundary(): i.setElementDistribution(n,progression,createBump)
def getPoints(self):
"""
returns a list of points used to define the boundary
:return: list of points used to define the boundary
:rtype: ``list`` of `Point` s
"""
out=[]
boundary=self.getBoundary()
for l in boundary:
for p in l.getBoundary():
if not p in out: out.append(p)
return out
def setRecombination(self, max_deviation=45*DEG):
"""
Recombines triangular meshes on the surface into mixed triangular/quadrangular meshes.
``max_deviation`` specifies the maximum derivation of the largest angle in the quadrangle
from the right angle. Use ``max_deviation``==``None`` to switch off recombination.
:param max_deviation: maximum derivation of the largest angle in the quadrangle from the right angle.
:type max_deviation: ``float`` or ``None``.
"""
if isinstance(self, ReversePrimitive):
self.getUnderlyingPrimitive().setRecombination(max_deviation)
else:
if not max_deviation==None:
if max_deviation<=0:
raise ValueError("max_deviation must be positive.")
if max_deviation/DEG>=90:
raise ValueError("max_deviation must be smaller than 90 DEG")
self.__recombination_angle=max_deviation
def getRecombination(self):
"""
returns max deviation from right angle in the recombination algorithm
:return: max deviation from right angle in the recombination algorithm. If recombination is switched off, ``None`` is returned.
:rtype: ``float`` or ``None``
"""
if isinstance(self, ReversePrimitive):
return self.getUnderlyingPrimitive().getRecombination()
else:
return self.__recombination_angle
def setTransfiniteMeshing(self,orientation="Left"):
"""
applies 2D transfinite meshing to the surface.
:param orientation: sets the orientation of the triangles. It is only relevant if recombination is not used.
:type orientation: `Manifold2D.LEFT`, `Manifold2D.RIGHT`, `Manifold2D.ALTERNATE`
:note: Transfinite meshing can not be applied if holes are present.
"""
if isinstance(self, ReversePrimitive):
return self.getUnderlyingPrimitive().setTransfiniteMeshing(orientation)
else:
if not orientation in [ Manifold2D.LEFT, Manifold2D.RIGHT, Manifold2D.ALTERNATE]:
raise ValueError("invalid orientation %s."%orientation)
if self.hasHole():
raise ValueError("transfinite meshing cannot be appled to surfaces with a hole.")
b=self.getBoundary()
if len(b)>4 or len(b)<3:
raise ValueError("transfinite meshing permits 3 or 4 boundary lines only.")
for l in b:
if l.getElementDistribution() == None: raise ValueError("transfinite meshing requires element distribution on all boundary lines.")
start=b[0]
opposite=None
top=None
bottom=None
for l in b[1:]:
if l.getEndPoint() == start.getStartPoint():
bottom=l
elif l.getStartPoint() == start.getEndPoint():
top=l
else:
opposite=l
if top==None or bottom == None:
raise ValueError("transfinite meshing cannot be applied to boundary is not closed. Most likely the orientation of some boundray segments is wrong.")
if opposite == None: # three sides only
if not top.getElementDistribution()[0] == bottom.getElementDistribution()[0]: start, top, bottom= bottom, start, top
if not top.getElementDistribution() == bottom.getElementDistribution():
raise ValueError("transfinite meshing requires opposite faces to be have the same element distribution.")
if not opposite == None:
if not start.getElementDistribution()[0] == opposite.getElementDistribution()[0]:
raise ValueError("transfinite meshing requires oposite faces to be have the same element distribution.")
if opposite == None:
if bottom.getEndPoint == top.getStartPoint():
raise ValueError("cannot identify corner proints for transfinite meshing.")
else:
points=[ bottom.getStartPoint(), bottom.getEndPoint(), top.getStartPoint() ]
else:
points=[ bottom.getStartPoint(), bottom.getEndPoint(), top.getStartPoint(), top.getEndPoint() ]
self.__points=points
self.__orientation=orientation
self.__transfinitemeshing=True
def resetTransfiniteMeshing(self):
"""
removes the transfinite meshing from the surface
"""
if isinstance(self, ReversePrimitive):
self.getUnderlyingPrimitive().resetTransfiniteMeshing()
else:
self.__transfinitemeshing=False
def getTransfiniteMeshing(self):
"""
returns the transfinite meshing settings. If transfinite meshing is not set, ``None`` is returned.
:return: a tuple of the tuple of points used to define the transfinite meshing and the orientation. If no points are set the points tuple is returned as ``None``. If no transfinite meshing is not set, ``None`` is returned.
:rtype: ``tuple`` of a ``tuple`` of `Point` s (or ``None``) and the orientation which is one of the values `Manifold2D.LEFT` , `Manifold2D.RIGHT` , `Manifold2D.ALTERNATE`
"""
if isinstance(self, ReversePrimitive):
return self.getUnderlyingPrimitive().getTransfiniteMeshing()
else:
if self.__transfinitemeshing:
return (self.__points, self.__orientation)
else:
return None
class RuledSurface(Primitive, Manifold2D):
"""
A ruled surface, i.e. a surface that can be interpolated using transfinite
interpolation.
"""
def __init__(self,loop):
"""
Creates a ruled surface with boundary ``loop``.
:param loop: `CurveLoop` defining the boundary of the surface.
"""
if not isinstance(loop.getUnderlyingPrimitive(),CurveLoop):
raise TypeError("argument loop needs to be a CurveLoop object.")
if len(loop)<2:
raise ValueError("the loop must contain at least two Curves.")
if len(loop)>4:
raise ValueError("the loop must contain at most four Curves.")
Primitive.__init__(self)
Manifold2D.__init__(self)
self.__loop=loop
def hasHole(self):
"""
Returns True if a hole is present.
"""
return False
def __neg__(self):
"""
Returns a view onto the suface with reversed ordering.
"""
return ReverseRuledSurface(self)
def getBoundaryLoop(self):
"""
Returns the loop defining the outer boundary.
"""
return self.__loop
def getBoundary(self):
"""
Returns a list of the one-dimensional manifolds forming the boundary
of the Surface (including holes).
"""
return self.getBoundaryLoop().getCurves()
def substitute(self,sub_dict):
"""
Returns a copy of self with substitutes for the primitives used to
construct it given by the dictionary ``sub_dict``. If a substitute for
the object is given by ``sub_dict`` the value is returned, otherwise a
new instance with substituted arguments is returned.
"""
if self not in sub_dict:
sub_dict[self]=RuledSurface(self.getBoundaryLoop().substitute(sub_dict))
return sub_dict[self]
def isColocated(self,primitive):
"""
Returns True if each curve is collocated with a curve in ``primitive``.
"""
if hasattr(primitive,"getUnderlyingPrimitive"):
if isinstance(primitive.getUnderlyingPrimitive(),RuledSurface):
return self.getBoundaryLoop().isColocated(primitive.getBoundaryLoop())
return False
def collectPrimitiveBases(self):
"""
Returns primitives used to construct the Surface.
"""
return [self] + self.getBoundaryLoop().collectPrimitiveBases()
def createRuledSurface(*curves):
"""
An easier way to create a `RuledSurface` from given curves.
"""
return RuledSurface(CurveLoop(*curves))
class ReverseRuledSurface(ReversePrimitive, Manifold2D):
"""
Creates a view onto a `RuledSurface` but with reverse orientation.
"""
def __init__(self,surface):
"""
Creates a polygon from a list of line curves. The curves must form a
closed loop.
"""
if not isinstance(surface, RuledSurface):
raise TypeError("arguments need to be an instance of CurveLoop.")
ReversePrimitive.__init__(self, surface)
Manifold2D.__init__(self)
def getBoundaryLoop(self):
"""
Returns the CurveLoop defining the ReverseRuledSurface.
"""
return -self.getUnderlyingPrimitive().getBoundaryLoop()
def getBoundary(self):
"""
Returns a list of the one-dimensional manifolds forming the boundary
of the Surface (including holes).
"""
return self.getBoundaryLoop().getCurves()
def hasHole(self):
"""
Returns True if a hole is present.
"""
return False
#==============================
class PlaneSurface(Primitive, Manifold2D):
"""
A plane surface with holes.
"""
def __init__(self,loop,holes=[]):
"""
Creates a plane surface with holes.
:param loop: `CurveLoop` defining the boundary of the surface
:param holes: list of `CurveLoop` s defining holes in the surface
:note: A CurveLoop defining a hole should not have any lines in common
with the exterior CurveLoop.
:note: A CurveLoop defining a hole should not have any lines in common
with another CurveLoop defining a hole in the same surface.
"""
if not isinstance(loop.getUnderlyingPrimitive(),CurveLoop):
raise TypeError("argument loop needs to be a CurveLoop object.")
for i in range(len(holes)):
if not isinstance(holes[i].getUnderlyingPrimitive(), CurveLoop):
raise TypeError("%i-th hole needs to be a CurveLoop object.")
#TODO: check if lines and holes are in a plane
#TODO: are holes really holes?
Primitive.__init__(self)
Manifold2D.__init__(self)
self.__loop=loop
self.__holes=holes
def hasHole(self):
"""
Returns True if a hole is present.
"""
return len(self.getHoles())>0
def getHoles(self):
"""
Returns the holes.
"""
return self.__holes
def getBoundaryLoop(self):
"""
Returns the loop defining the boundary.
"""
return self.__loop
def substitute(self,sub_dict):
"""
Returns a copy of self with substitutes for the primitives used to
construct it given by the dictionary ``sub_dict``. If a substitute for
the object is given by ``sub_dict`` the value is returned, otherwise a
new instance with substituted arguments is returned.
"""
if self not in sub_dict:
sub_dict[self]=PlaneSurface(self.getBoundaryLoop().substitute(sub_dict),[ h.substitute(sub_dict) for h in self.getHoles()])
return sub_dict[self]
def isColocated(self,primitive):
"""
Returns True if each curve is collocated with a curve in ``primitive``.
"""
if hasattr(primitive,"getUnderlyingPrimitive"):
if isinstance(primitive.getUnderlyingPrimitive(),PlaneSurface):
if self.getBoundaryLoop().isColocated(primitive.getBoundaryLoop()):
hs0=self.getHoles()
hs1=primitive.getHoles()
if len(hs0) == len(hs1):
for h0 in hs0:
colocated = False
for h1 in hs1:
colocated = colocated or h0.isColocated(h1)
if not colocated: return False
return True
return False
def collectPrimitiveBases(self):
"""
Returns primitives used to construct the Surface.
"""
out=[self] + self.getBoundaryLoop().collectPrimitiveBases()
for i in self.getHoles(): out+=i.collectPrimitiveBases()
return out
def __neg__(self):
"""
Returns a view onto the curve with reversed ordering.
"""
return ReversePlaneSurface(self)
def getBoundary(self):
"""
Returns a list of the one-dimensional manifolds forming the boundary
of the Surface (including holes).
"""
out = []+ self.getBoundaryLoop().getCurves()
for h in self.getHoles(): out+=h.getCurves()
return out
class ReversePlaneSurface(ReversePrimitive, Manifold2D):
"""
Creates a view onto a `PlaneSurface` but with reverse orientation.
"""
def __init__(self,surface):
"""
Creates a polygon from a `PlaneSurface`.
"""
if not isinstance(surface, PlaneSurface):
raise TypeError("arguments need to be an instance of PlaneSurface.")
ReversePrimitive.__init__(self, surface)
Manifold2D.__init__(self)
def getBoundaryLoop(self):
"""
Returns the CurveLoop defining the ReversePlaneSurface.
"""
return -self.getUnderlyingPrimitive().getBoundaryLoop()
def getHoles(self):
"""
Returns the holes.
"""
return [ -h for h in self.getUnderlyingPrimitive().getHoles() ]
def getBoundary(self):
"""
Returns a list of the one-dimensional manifolds forming the boundary
of the Surface (including holes).
"""
out = [] + self.getBoundaryLoop().getCurves()
for h in self.getHoles(): out+=h.getCurves()
return out
def hasHole(self):
"""
Returns True if a hole is present.
"""
return len(self.getHoles())>0
#=========================================================================
class SurfaceLoop(Primitive, PrimitiveBase):
"""
A loop of 2D primitives which defines the shell of a volume.
The loop must represent a closed shell, and the primitives should be
oriented consistently.
"""
def __init__(self,*surfaces):
"""
Creates a surface loop.
"""
if len(surfaces)==1:
surfaces=surfaces[0]
if not hasattr(surfaces,'__iter__'): raise ValueError("SurfaceLoop needs at least two points")
if len(surfaces)<2:
raise ValueError("at least two surfaces have to be given.")
for i in range(len(surfaces)):
if not isinstance(surfaces[i].getUnderlyingPrimitive(),Manifold2D):
raise TypeError("%s-th argument is not a Manifold2D object."%i)
self.__surfaces=list(surfaces)
Primitive.__init__(self)
PrimitiveBase.__init__(self)
def __len__(self):
"""
Returns the number of curves in the SurfaceLoop.
"""
return len(self.__surfaces)
def __neg__(self):
"""
Returns a view onto the curve with reversed ordering.
"""
return ReverseSurfaceLoop(self)
def getSurfaces(self):
"""
Returns the surfaces defining the SurfaceLoop.
"""
return self.__surfaces
def collectPrimitiveBases(self):
"""
Returns primitives used to construct the SurfaceLoop.
"""
out=[self]
for c in self.getSurfaces(): out+=c.collectPrimitiveBases()
return out
def substitute(self,sub_dict):
"""
Returns a copy of self with substitutes for the primitives used to
construct it given by the dictionary ``sub_dict``. If a substitute for
the object is given by ``sub_dict`` the value is returned, otherwise a
new instance with substituted arguments is returned.
"""
if self not in sub_dict:
new_s=[]
for s in self.getSurfaces(): new_s.append(s.substitute(sub_dict))
sub_dict[self]=SurfaceLoop(*tuple(new_s))
return sub_dict[self]
def isColocated(self,primitive):
"""
Returns True if each surface is collocated with a curve in ``primitive``
and vice versa.
"""
if hasattr(primitive,"getUnderlyingPrimitive"):
if isinstance(primitive.getUnderlyingPrimitive(),SurfaceLoop):
if len(primitive) == len(self):
sp0=self.getSurfaces()
sp1=primitive.getSurfaces()
for s0 in sp0:
colocated = False
for s1 in sp1:
colocated = colocated or s0.isColocated(s1)
if not colocated: return False
return True
return False
class ReverseSurfaceLoop(ReversePrimitive, PrimitiveBase):
"""
A view of a SurfaceLoop with reverse orientation.
The loop must represent a closed shell and the primitives should be
oriented consistently.
"""
def __init__(self,surface_loop):
"""
Creates a polygon from a list of line surfaces. The curves must form
a closed loop.
"""
if not isinstance(surface_loop, SurfaceLoop):
raise TypeError("arguments need to be an instance of SurfaceLoop.")
ReversePrimitive.__init__(self, surface_loop)
PrimitiveBase.__init__(self)
def getSurfaces(self):
"""
Returns the surfaces defining the SurfaceLoop.
"""
return [ -s for s in self.getUnderlyingPrimitive().getSurfaces() ]
def __len__(self):
return len(self.getUnderlyingPrimitive())
#==============================
class Manifold3D(PrimitiveBase):
"""
General three-dimensional manifold.
"""
def __init__(self):
"""
Creates a three-dimensional manifold.
"""
PrimitiveBase.__init__(self)
self.__transfinitemeshing=False
def getBoundary(self):
"""
Returns a list of the 2-dimensional manifolds forming the boundary
of the volume (including holes).
"""
raise NotImplementedError()
def setElementDistribution(self,n,progression=1,createBump=False):
"""
Defines the number of elements on the lines and surfaces
:param n: number of elements on the line
:type n: ``int``
:param progression: a positive progression factor
:type progression: positive ``float``
:param createBump: of elements on the line
:type createBump: ``bool``
"""
for i in self.getBoundary(): i.setElementDistribution(n,progression,createBump)
def setRecombination(self, max_deviation=45*DEG):
"""
Recombines triangular meshes on all surface into mixed triangular/quadrangular meshes. These meshes
are then used to generate the volume mesh if possible. Recombination requires 3D transfinite meshing.
``max_deviation`` specifies the maximum derivation of the largest angle in the quadrangle
from the right angle. Use ``max_deviation``==``None`` to switch off recombination.
:param max_deviation: maximum derivation of the largest angle in the quadrangle from the right angle.
:type max_deviation: ``float`` or ``None``.
"""
if not max_deviation==None:
if max_deviation<=0:
raise ValueError("max_deviation must be positive.")
if max_deviation/DEG>=90:
raise ValueError("max_deviation must be smaller than 90 DEG")
for i in self.getBoundary(): i.setRecombination(max_deviation)
self.setTransfiniteMeshing()
def setTransfiniteMeshing(self,orientation="Left"):
"""
applies 3D transfinite meshing to the volume and all surface. It requires transfinite meshing
on all faces which will be enforced (except if ``orientation`` is equal to ``None``).
:param orientation: sets the orientation of the triangles on the surfaces. It is only relevant if recombination is not used.
If orientation is equal to ``None``, the transfinite meshing is not applied to the surfaces but must be set by the user.
:type orientation: `Manifold2D.LEFT`, `Manifold2D.RIGHT`, `Manifold2D.ALTERNATE`
:note: Transfinite meshing can not be applied if holes are present.
:note: only five or six surfaces may be used.
:warning: The functionality of transfinite meshing without recombination is not entirely clear in `gmsh`. So please apply this method with care.
"""
if isinstance(self, ReversePrimitive):
return self.getUnderlyingPrimitive().setTransfiniteMeshing(orientation)
else:
if not orientation == None:
if not orientation in [ Manifold2D.LEFT, Manifold2D.RIGHT, Manifold2D.ALTERNATE]:
raise ValueError("invalid orientation %s."%orientation)
if self.hasHole():
raise ValueError("transfinite meshing cannot be appled to surfaces with a hole.")
b=self.getBoundary()
# find a face with 3/4 Points:
if len(b) == 6 :
des_len=4
elif len(b) == 5:
des_len=3
else:
raise ValueError("transfinite meshing permits 5 or 6 surface only.")
# start_b=None
# for l in b:
# if len(l.getPolygon()) == des_len:
# start_b = l
# break
# if start_b == None:
# raise ValueError,"Expect face with %s points."%des_len
# start_poly=start_b.getPolygon()
# now we need to find the opposite face:
# opposite = None
# for l in b:
# if all( [ not k in start_poly for k in l.getPolygon() ]):
# opposite = l
# break
# if opposite == None:
# raise ValueError,"Unable to find face for transfinite interpolation."
# opposite_poly=opposite.getPolygon()
# if not len(opposite_poly) == des_len:
# raise ValueError,"Unable to find face for transfinite interpolation."
# this needs more work to find the points!!!!
points = []
self.__points=points
if not orientation == None:
for i in b: i.setTransfiniteMeshing(orientation)
self.__transfinitemeshing=True
def resetTransfiniteMeshing(self):
"""
removes the transfinite meshing from the volume but not from the surfaces
"""
if isinstance(self, ReversePrimitive):
self.getUnderlyingPrimitive().resetTransfiniteMeshing()
else:
self.__transfinitemeshing=False
def getTransfiniteMeshing(self):
"""
returns the transfinite meshing settings. If transfinite meshing is not set, ``None`` is returned.
:return: a tuple of the tuple of points used to define the transfinite meshing and the orientation. If no points are set the points tuple is returned as ``None``. If no transfinite meshing is not set, ``None`` is returned.
:rtype: ``tuple`` of a ``tuple`` of `Point` s (or ``None``) and the orientation which is one of the values `Manifold2D.LEFT` , `Manifold2D.RIGHT` , `Manifold2D.ALTERNATE`
"""
if isinstance(self, ReversePrimitive):
return self.getUnderlyingPrimitive().getTransfiniteMeshing()
else:
if self.__transfinitemeshing:
return self.__points
else:
return None
class Volume(Manifold3D, Primitive):
"""
A volume with holes.
"""
def __init__(self,loop,holes=[]):
"""
Creates a volume with holes.
:param loop: `SurfaceLoop` defining the boundary of the surface
:param holes: list of `SurfaceLoop` defining holes in the surface
:note: A SurfaceLoop defining a hole should not have any surfaces in
common with the exterior SurfaceLoop.
:note: A SurfaceLoop defining a hole should not have any surfaces in
common with another SurfaceLoop defining a hole in the same
volume.
"""
if not isinstance(loop.getUnderlyingPrimitive(), SurfaceLoop):
raise TypeError("argument loop needs to be a SurfaceLoop object.")
for i in range(len(holes)):
if not isinstance(holes[i].getUnderlyingPrimitive(), SurfaceLoop):
raise TypeError("%i th hole needs to be a SurfaceLoop object.")
Primitive.__init__(self)
Manifold3D.__init__(self)
self.__loop=loop
self.__holes=holes
self.__transfinitemeshing=False
def getHoles(self):
"""
Returns the holes in the volume.
"""
return self.__holes
def getSurfaceLoop(self):
"""
Returns the loop forming the surface.
"""
return self.__loop
def substitute(self,sub_dict):
"""
Returns a copy of self with substitutes for the primitives used to
construct it given by the dictionary ``sub_dict``. If a substitute for
the object is given by ``sub_dict`` the value is returned, otherwise a
new instance with substituted arguments is returned.
"""
if self not in sub_dict:
sub_dict[self]=Volume(self.getSurfaceLoop().substitute(sub_dict),[ h.substitute(sub_dict) for h in self.getHoles()])
return sub_dict[self]
def isColocated(self,primitive):
"""
Returns True if each curve is collocated with a curve in ``primitive``.
"""
if hasattr(primitive,"getUnderlyingPrimitive"):
if isinstance(primitive.getUnderlyingPrimitive(),Volume):
if self.getSurfaceLoop().isColocated(primitive.getSurfaceLoop()):
hs0=self.getHoles()
hs1=primitive.getHoles()
if len(hs0) == len(hs1):
for h0 in hs0:
colocated = False
for h1 in hs1:
colocated = colocated or h0.isColocated(h1)
if not colocated: return False
return True
return False
def collectPrimitiveBases(self):
"""
Returns primitives used to construct the surface.
"""
out=[self] + self.getSurfaceLoop().collectPrimitiveBases()
for i in self.getHoles(): out+=i.collectPrimitiveBases()
return out
def getBoundary(self):
"""
Returns a list of the 2-dimensional manifolds forming the surface of the Volume (including holes).
"""
out = []+ self.getSurfaceLoop().getSurfaces()
for h in self.getHoles(): out+=h.getSurfaces()
return out
def hasHole(self):
"""
Returns True if a hole is present.
"""
return len(self.getHoles())>0
class PropertySet(Primitive, PrimitiveBase):
"""
Defines a group of `Primitive` s which can be accessed through a name.
"""
def __init__(self,name,*items):
Primitive.__init__(self)
self.__dim=None
self.clearItems()
self.addItem(*items)
self.setName(name)
def getDim(self):
"""
Returns the dimensionality of the items.
"""
if self.__dim == None:
items=self.getItems()
if len(items)>0:
if isinstance(items[0] ,Manifold1D):
self.__dim=1
elif isinstance(items[0] ,Manifold2D):
self.__dim=2
elif isinstance(items[0] ,Manifold3D):
self.__dim=3
else:
self.__dim=0
return self.__dim
def __repr__(self):
"""
Returns a string representation.
"""
return "%s(%s)"%(self.getName(),self.getID())
def getManifoldClass(self):
"""
Returns the manifold class expected from items.
"""
d=self.getDim()
if d == None:
raise ValueError("undefined spatial diemnsion.")
else:
if d==0:
return Point
elif d==1:
return Manifold1D
elif d==2:
return Manifold2D
else:
return Manifold3D
def getName(self):
"""
Returns the name of the set.
"""
return self.__name
def setName(self,name):
"""
Sets the name.
"""
self.__name=str(name)
def addItems(self,*items):
"""
Adds items. An item my be any `Primitive` but no `PropertySet`.
"""
self.addItem(*items)
def addItem(self,*items):
"""
Adds items. An item my be any `Primitive` but no `PropertySet`.
"""
for i in items:
if not (isinstance(i, Manifold1D) or isinstance(i, Manifold2D) or isinstance(i, Manifold3D) ):
raise TypeError("Illegal argument type %s added to PropertySet."%(i.__class__))
for i in items:
if not i in self.__items:
if len(self.__items)>0:
m=self.getManifoldClass()
if not isinstance(i, m):
raise TypeError("argument %s is not a %s class object."%(i, m.__name__))
self.__items.append(i)
def getNumItems(self):
"""
Returns the number of items in the property set.
"""
return len(self.__items)
def getItems(self):
"""
Returns the list of items.
"""
return self.__items
def clearItems(self):
"""
Clears the list of items.
"""
self.__items=[]
def collectPrimitiveBases(self):
"""
Returns primitives used to construct the PropertySet.
"""
out=[self]
for i in self.getItems(): out+=i.collectPrimitiveBases()
return out
def getTag(self):
"""
Returns the tag used for this property set.
"""
return self.getID()
|