1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467
|
"""Module containing the Transform class."""
from __future__ import annotations
from collections.abc import Sequence
from typing import TYPE_CHECKING
from typing import Literal
from typing import overload
import numpy as np
import pyvista
from pyvista.core import _validation
from pyvista.core import _vtk_core as _vtk
from pyvista.core.utilities.arrays import array_from_vtkmatrix
from pyvista.core.utilities.arrays import vtkmatrix_from_array
from pyvista.core.utilities.misc import _NoNewAttrMixin
from pyvista.core.utilities.misc import assert_empty_kwargs
from pyvista.core.utilities.transformations import apply_transformation_to_points
from pyvista.core.utilities.transformations import axis_angle_rotation
from pyvista.core.utilities.transformations import decomposition
from pyvista.core.utilities.transformations import reflection
if TYPE_CHECKING: # pragma: no cover
from scipy.spatial.transform import Rotation
from pyvista import DataSet
from pyvista import MultiBlock
from pyvista import Prop3D
from pyvista.core._typing_core import MatrixLike
from pyvista.core._typing_core import NumpyArray
from pyvista.core._typing_core import RotationLike
from pyvista.core._typing_core import TransformLike
from pyvista.core._typing_core import VectorLike
from pyvista.core._typing_core import _DataSetOrMultiBlockType
class Transform(
_NoNewAttrMixin,
_vtk.DisableVtkSnakeCase,
_vtk.vtkPyVistaOverride,
_vtk.vtkTransform,
):
"""Describes linear transformations via a 4x4 matrix.
A :class:`Transform` can be used to describe the full range of linear (also known
as affine) coordinate transformations in three dimensions, which are internally
represented as a 4x4 homogeneous transformation matrix.
The transformation methods (e.g. :meth:`translate`, :meth:`rotate`,
:meth:`compose`) can operate in either :meth:`pre_multiply` or
:meth:`post_multiply` mode. In pre-multiply mode, any additional transformations
will occur *before* any transformations represented by the current :attr:`matrix`.
In post-multiply mode (the default), the additional transformation will occur
*after* any transformations represented by the current matrix.
.. note::
This class performs all of its operations in a right-handed coordinate system
with right-handed rotations. Some other graphics libraries use left-handed
coordinate systems and rotations.
.. versionadded:: 0.45
Parameters
----------
trans : TransformLike | Sequence[TransformLike], optional
Initialize the transform with a transformation or sequence of transformations.
By default, the transform is initialized as the identity matrix.
point : VectorLike[float], optional
Point to use when composing some transformations such as scale, rotation, etc.
If set, two additional transformations are composed and added to
the :attr:`matrix_list`:
- :meth:`translate` to ``point`` before the transformation
- :meth:`translate` away from ``point`` after the transformation
By default, this value is ``None``, which means that the scale, rotation, etc.
transformations are performed about the origin ``(0, 0, 0)``.
multiply_mode : 'pre' | 'post', optional
Multiplication mode to use when composing. Set this to ``'pre'`` for
pre-multiplication or ``'post'`` for post-multiplication.
See Also
--------
pyvista.DataObjectFilters.transform
Apply a transformation to a mesh.
pyvista.Prop3D.transform
Transform an actor.
Examples
--------
Create a transformation and use ``+`` to compose a translation.
>>> import numpy as np
>>> import pyvista as pv
>>> position = (-0.6, -0.8, 2.1)
>>> translation_T = pv.Transform() + position
>>> translation_T.matrix
array([[ 1. , 0. , 0. , -0.6],
[ 0. , 1. , 0. , -0.8],
[ 0. , 0. , 1. , 2.1],
[ 0. , 0. , 0. , 1. ]])
Using ``+`` performs the same concatenation as calling :meth:`translate`.
>>> np.array_equal(
... translation_T.matrix, pv.Transform().translate(position).matrix
... )
True
Create a transformation and use ``*`` to compose a scaling matrix.
>>> scale_factor = 2.0
>>> scaling_T = pv.Transform() * scale_factor
>>> scaling_T.matrix
array([[2., 0., 0., 0.],
[0., 2., 0., 0.],
[0., 0., 2., 0.],
[0., 0., 0., 1.]])
Using ``*`` performs the same concatenation as calling :meth:`scale`.
>>> np.array_equal(scaling_T.matrix, pv.Transform().scale(scale_factor).matrix)
True
Compose the two transformations using ``*``. This will compose with
post-multiplication such that the transformations are applied in order from left to
right, i.e. translate first, then scale.
>>> transform_post = translation_T * scaling_T
>>> transform_post.matrix
array([[ 2. , 0. , 0. , -1.2],
[ 0. , 2. , 0. , -1.6],
[ 0. , 0. , 2. , 4.2],
[ 0. , 0. , 0. , 1. ]])
Post-multiplication is equivalent to using matrix multiplication on the
arrays directly but with the arguments reversed:
>>> mat_mul = scaling_T.matrix @ translation_T.matrix
>>> np.array_equal(transform_post.matrix, mat_mul)
True
Alternatively, compose the transformations by chaining the methods with a
single :class:`Transform` instance. Note that post-multiply is used by default.
>>> transform_post = pv.Transform()
>>> transform_post.multiply_mode
'post'
>>> _ = transform_post.translate(position).scale(scale_factor)
>>> transform_post.matrix
array([[ 2. , 0. , 0. , -1.2],
[ 0. , 2. , 0. , -1.6],
[ 0. , 0. , 2. , 4.2],
[ 0. , 0. , 0. , 1. ]])
Use :attr:`n_transformations` to check that there are two transformations.
>>> transform_post.n_transformations
2
Use :attr:`matrix_list` to get a list of the transformations. Since
post-multiplication is used, the translation matrix is first in the list since
it was applied first, and the scale matrix is second.
>>> transform_post.matrix_list[0] # translation
array([[ 1. , 0. , 0. , -0.6],
[ 0. , 1. , 0. , -0.8],
[ 0. , 0. , 1. , 2.1],
[ 0. , 0. , 0. , 1. ]])
>>> transform_post.matrix_list[1] # scaling
array([[2., 0., 0., 0.],
[0., 2., 0., 0.],
[0., 0., 2., 0.],
[0., 0., 0., 1.]])
Create a similar transform but use pre-multiplication this time. Compose the
transformations in the same order as before using :meth:`translate` and :meth:`scale`.
>>> transform_pre = pv.Transform().pre_multiply()
>>> _ = transform_pre.translate(position).scale(scale_factor)
This is equivalent to using matrix multiplication directly on the arrays:
>>> mat_mul = translation_T.matrix @ scaling_T.matrix
>>> np.array_equal(transform_pre.matrix, mat_mul)
True
Show the matrix list again. Note how the order with pre-multiplication is the
reverse of post-multiplication.
>>> transform_pre.matrix_list[0] # scaling
array([[2., 0., 0., 0.],
[0., 2., 0., 0.],
[0., 0., 2., 0.],
[0., 0., 0., 1.]])
>>> transform_pre.matrix_list[1] # translation
array([[ 1. , 0. , 0. , -0.6],
[ 0. , 1. , 0. , -0.8],
[ 0. , 0. , 1. , 2.1],
[ 0. , 0. , 0. , 1. ]])
Apply the two post- and pre-multiplied transformations to a dataset and plot them.
Note how the meshes have different positions since post- and pre-multiplication
produce different transformations.
>>> mesh_post = pv.Sphere().transform(transform_post, inplace=False)
>>> mesh_pre = pv.Cone().transform(transform_pre, inplace=False)
>>> pl = pv.Plotter()
>>> _ = pl.add_mesh(mesh_post, color='goldenrod')
>>> _ = pl.add_mesh(mesh_pre, color='teal')
>>> _ = pl.add_axes_at_origin()
>>> pl.show()
Get the composed inverse transformation matrix of the pre-multiplication case.
>>> inverse_matrix = transform_pre.inverse_matrix
>>> inverse_matrix
array([[ 0.5 , 0. , 0. , 0.3 ],
[ 0. , 0.5 , 0. , 0.4 ],
[ 0. , 0. , 0.5 , -1.05],
[ 0. , 0. , 0. , 1. ]])
Similar to using :attr:`matrix_list`, we can inspect the individual transformation
inverses with :attr:`inverse_matrix_list`.
>>> transform_pre.inverse_matrix_list[0] # inverse scaling
array([[0.5, 0. , 0. , 0. ],
[0. , 0.5, 0. , 0. ],
[0. , 0. , 0.5, 0. ],
[0. , 0. , 0. , 1. ]])
>>> transform_pre.inverse_matrix_list[1] # inverse translation
array([[ 1. , 0. , 0. , 0.6],
[ 0. , 1. , 0. , 0.8],
[ 0. , 0. , 1. , -2.1],
[ 0. , 0. , 0. , 1. ]])
Transform the mesh by its inverse to restore it to its original un-scaled state
and positioning at the origin.
>>> mesh_pre_inverted = mesh_pre.transform(inverse_matrix, inplace=False)
>>> pl = pv.Plotter()
>>> _ = pl.add_mesh(mesh_pre_inverted, color='teal')
>>> _ = pl.add_axes_at_origin()
>>> pl.show()
"""
def __init__(
self: Transform,
trans: TransformLike | Sequence[TransformLike] | None = None,
*,
point: VectorLike[float] | None = None,
multiply_mode: Literal['pre', 'post'] = 'post',
) -> None:
super().__init__()
self.multiply_mode = multiply_mode
self.point = point
self.check_finite = True
if trans is not None:
if isinstance(trans, Sequence):
if all(isinstance(item, Sequence) for item in trans):
# Init from a nested sequence array
self.compose(trans)
else:
# Init from sequence of transformations
[self.compose(t) for t in trans]
else:
self.matrix = trans
def __add__(self: Transform, other: VectorLike[float]) -> Transform:
""":meth:`translate` this transform using post-multiply semantics."""
try:
return self.copy().translate(other, multiply_mode='post')
except TypeError:
msg = (
f"Unsupported operand type(s) for +: '{self.__class__.__name__}' "
f"and '{type(other).__name__}'\n"
f'The right-side argument must be a length-3 vector.'
)
raise TypeError(msg)
except ValueError:
msg = (
f"Unsupported operand value(s) for +: '{self.__class__.__name__}' "
f"and '{type(other).__name__}'\n"
f'The right-side argument must be a length-3 vector.'
)
raise ValueError(msg)
def __radd__(self: Transform, other: VectorLike[float]) -> Transform:
""":meth:`translate` this transform using pre-multiply semantics."""
try:
return self.copy().translate(other, multiply_mode='pre')
except TypeError:
msg = (
f"Unsupported operand type(s) for +: '{type(other).__name__}' "
f"and '{self.__class__.__name__}'\n"
f'The left-side argument must be a length-3 vector.'
)
raise TypeError(msg)
except ValueError:
msg = (
f"Unsupported operand value(s) for +: '{type(other).__name__}' "
f"and '{self.__class__.__name__}'\n"
f'The left-side argument must be a length-3 vector.'
)
raise ValueError(msg)
def __mul__(self: Transform, other: float | VectorLike[float] | TransformLike) -> Transform:
""":meth:`compose` this transform using post-multiply semantics.
Use :meth:`scale` for single numbers and length-3 vector inputs, and
:meth:`compose` otherwise for transform-like inputs.
"""
copied = self.copy()
try:
transform = copied.scale(other, multiply_mode='post') # type: ignore[arg-type]
except (ValueError, TypeError):
try:
transform = copied.compose(other, multiply_mode='post')
except TypeError:
msg = (
f"Unsupported operand type(s) for *: '{self.__class__.__name__}' "
f"and '{type(other).__name__}'\n"
f'The right-side argument must be transform-like.'
)
raise TypeError(msg)
except ValueError:
msg = (
f"Unsupported operand value(s) for *: '{self.__class__.__name__}' "
f"and '{type(other).__name__}'\n"
f'The right-side argument must be a single number or a length-3 vector '
f'or have 3x3 or 4x4 shape.'
)
raise ValueError(msg)
return transform
def __rmul__(self: Transform, other: float | VectorLike[float]) -> Transform:
""":meth:`scale` this transform using pre-multiply semantics."""
try:
return self.copy().scale(other, multiply_mode='pre')
except TypeError:
msg = (
f"Unsupported operand type(s) for *: '{type(other).__name__}' "
f"and '{self.__class__.__name__}'\n"
f'The left-side argument must be a single number or a length-3 vector.'
)
raise TypeError(msg)
except ValueError:
msg = (
f"Unsupported operand value(s) for *: '{type(other).__name__}' "
f"and '{self.__class__.__name__}'\n"
f'The left-side argument must be a single number or a length-3 vector.'
)
raise ValueError(msg)
def copy(self: Transform) -> Transform:
"""Return a deep copy of the transform.
Returns
-------
Transform
Deep copy of this transform.
Examples
--------
Create a scaling transform.
>>> import pyvista as pv
>>> transform = pv.Transform().scale(1, 2, 3)
>>> transform.matrix
array([[1., 0., 0., 0.],
[0., 2., 0., 0.],
[0., 0., 3., 0.],
[0., 0., 0., 1.]])
Copy the transform.
>>> copied = transform.copy()
>>> copied.matrix
array([[1., 0., 0., 0.],
[0., 2., 0., 0.],
[0., 0., 3., 0.],
[0., 0., 0., 1.]])
>>> copied is transform
False
"""
new_transform = Transform()
new_transform.DeepCopy(self)
# Need to copy other props not stored by vtkTransform
new_transform.multiply_mode = self.multiply_mode
return new_transform
def __repr__(self: Transform) -> str:
"""Representation of the transform."""
def _matrix_repr() -> str:
repr_ = np.array_repr(self.matrix)
return repr_.replace('array(', ' ').replace(')', '').replace(' [', '[')
matrix_repr_lines = _matrix_repr().split('\n')
lines = [
f'{type(self).__name__} ({hex(id(self))})',
f' Num Transformations: {self.n_transformations}',
f' Matrix: {matrix_repr_lines[0]}',
f' {matrix_repr_lines[1]}',
f' {matrix_repr_lines[2]}',
f' {matrix_repr_lines[3]}',
]
return '\n'.join(lines)
@property
def point(self: Transform) -> tuple[float, float, float] | None: # numpydoc ignore=RT01
"""Point to use when composing some transformations such as scale, rotation, etc.
If set, two additional transformations are composed and added to
the :attr:`matrix_list`:
- :meth:`translate` to ``point`` before the transformation
- :meth:`translate` away from ``point`` after the transformation
By default, this value is ``None``, which means that the scale, rotation, etc.
transformations are performed about the origin ``(0, 0, 0)``.
"""
return self._point
@point.setter
def point(self: Transform, point: VectorLike[float] | None) -> None:
self._point = (
None
if point is None
else _validation.validate_array3(point, dtype_out=float, to_tuple=True, name='point')
)
@property
def multiply_mode(self: Transform) -> Literal['pre', 'post']: # numpydoc ignore=RT01
"""Set or get the multiplication mode.
Set this to ``'pre'`` to set the multiplication mode to :meth:`pre_multiply`.
Set this to ``'post'`` to set it to :meth:`post_multiply`.
In pre-multiply mode, any additional transformations (e.g. using
:meth:`translate`, :meth:`compose`, etc.) will occur *before* any
transformations represented by the current :attr:`matrix`.
In post-multiply mode, the additional transformation will occur *after* any
transformations represented by the current matrix.
"""
return self._multiply_mode
@multiply_mode.setter
def multiply_mode(self: Transform, multiply_mode: Literal['pre', 'post']) -> None:
_validation.check_contains(
['pre', 'post'], must_contain=multiply_mode, name='multiply mode'
)
self.pre_multiply() if multiply_mode == 'pre' else self.post_multiply()
def pre_multiply(self: Transform) -> Transform: # numpydoc ignore=RT01
"""Set the multiplication mode to pre-multiply.
In pre-multiply mode, any additional transformations (e.g. using
:meth:`translate`, :meth:`compose`, etc.) will occur *before* any
transformations represented by the current :attr:`matrix`.
Examples
--------
>>> import pyvista as pv
>>> transform = pv.Transform().pre_multiply()
>>> transform.multiply_mode
'pre'
"""
self._multiply_mode: Literal['pre', 'post'] = 'pre'
self.PreMultiply()
return self
def post_multiply(self: Transform) -> Transform: # numpydoc ignore=RT01
"""Set the multiplication mode to post-multiply.
In post-multiply mode, any additional transformations (e.g. using
:meth:`translate`, :meth:`compose`, etc.) will occur *after* any
transformations represented by the current :attr:`matrix`.
Examples
--------
>>> import pyvista as pv
>>> transform = pv.Transform().post_multiply()
>>> transform.multiply_mode
'post'
"""
self._multiply_mode = 'post'
self.PostMultiply()
return self
def scale(
self: Transform,
*factor: float | VectorLike[float],
point: VectorLike[float] | None = None,
multiply_mode: Literal['pre', 'post'] | None = None,
) -> Transform: # numpydoc ignore=RT01
"""Compose a scale matrix.
Create a scale matrix and :meth:`compose` it with the current
transformation :attr:`matrix` according to pre-multiply or post-multiply
semantics.
Internally, the matrix is stored in the :attr:`matrix_list`.
Parameters
----------
*factor : float | VectorLike[float]
Scale factor(s) to use. Use a single number for uniform scaling or
three numbers for non-uniform scaling. The three factors may be
passed as a single vector (one arg) or an unpacked vector (three args).
point : VectorLike[float], optional
Point to scale from. By default, the object's :attr:`point` is used,
but this can be overridden.
If set, two additional transformations are composed and added to
the :attr:`matrix_list`:
- :meth:`translate` to ``point`` before the scaling
- :meth:`translate` away from ``point`` after the scaling
multiply_mode : 'pre' | 'post', optional
Multiplication mode to use when composing the matrix. By default, the
object's :attr:`multiply_mode` is used, but this can be overridden. Set this
to ``'pre'`` for pre-multiplication or ``'post'`` for post-multiplication.
See Also
--------
pyvista.DataObjectFilters.scale
Scale a mesh.
pyvista.DataObjectFilters.resize
Resize a mesh.
Examples
--------
Compose a scale matrix.
>>> import pyvista as pv
>>> transform = pv.Transform().scale(1, 2, 3)
>>> transform.matrix
array([[1., 0., 0., 0.],
[0., 2., 0., 0.],
[0., 0., 3., 0.],
[0., 0., 0., 1.]])
Compose a second scale matrix using ``*``.
>>> transform = transform * 2
>>> transform.matrix
array([[2., 0., 0., 0.],
[0., 4., 0., 0.],
[0., 0., 6., 0.],
[0., 0., 0., 1.]])
Scale from a point. Check the :attr:`matrix_list` to see that a translation
is added before and after the scaling.
>>> transform = pv.Transform().scale(7, point=(1, 2, 3))
>>> translation_to_origin = transform.matrix_list[0]
>>> translation_to_origin
array([[ 1., 0., 0., -1.],
[ 0., 1., 0., -2.],
[ 0., 0., 1., -3.],
[ 0., 0., 0., 1.]])
>>> scale = transform.matrix_list[1]
>>> scale
array([[7., 0., 0., 0.],
[0., 7., 0., 0.],
[0., 0., 7., 0.],
[0., 0., 0., 1.]])
>>> translation_from_origin = transform.matrix_list[2]
>>> translation_from_origin
array([[1., 0., 0., 1.],
[0., 1., 0., 2.],
[0., 0., 1., 3.],
[0., 0., 0., 1.]])
"""
valid_factor = _validation.validate_array3(
factor, # type: ignore[arg-type]
broadcast=True,
dtype_out=float,
name='scale factor',
)
transform = _vtk.vtkTransform()
transform.Scale(valid_factor)
return self._compose_with_translations(transform, point=point, multiply_mode=multiply_mode)
def reflect(
self: Transform,
*normal: float | VectorLike[float],
point: VectorLike[float] | None = None,
multiply_mode: Literal['pre', 'post'] | None = None,
) -> Transform: # numpydoc ignore=RT01
"""Compose a reflection matrix.
Create a reflection matrix and :meth:`compose` it with the current
transformation :attr:`matrix` according to pre-multiply or post-multiply
semantics.
Internally, the matrix is stored in the :attr:`matrix_list`.
Parameters
----------
*normal : float | VectorLike[float]
Normal direction for reflection. May be a single vector (one arg) or
unpacked vector (three args).
point : VectorLike[float], optional
Point to reflect about. By default, the object's :attr:`point` is used,
but this can be overridden.
If set, two additional transformations are composed and added to
the :attr:`matrix_list`:
- :meth:`translate` to ``point`` before the reflection
- :meth:`translate` away from ``point`` after the reflection
multiply_mode : 'pre' | 'post', optional
Multiplication mode to use when composing the matrix. By default, the
object's :attr:`multiply_mode` is used, but this can be overridden. Set this
to ``'pre'`` for pre-multiplication or ``'post'`` for post-multiplication.
See Also
--------
pyvista.DataObjectFilters.reflect
Reflect a mesh.
Examples
--------
Compose a reflection matrix.
>>> import pyvista as pv
>>> transform = pv.Transform()
>>> _ = transform.reflect(0, 0, 1)
>>> transform.matrix
array([[ 1., 0., 0., 0.],
[ 0., 1., 0., 0.],
[ 0., 0., -1., 0.],
[ 0., 0., 0., 1.]])
Compose a second reflection matrix.
>>> _ = transform.reflect((1, 0, 0))
>>> transform.matrix
array([[-1., 0., 0., 0.],
[ 0., 1., 0., 0.],
[ 0., 0., -1., 0.],
[ 0., 0., 0., 1.]])
"""
valid_normal = _validation.validate_array3(
normal, # type: ignore[arg-type]
dtype_out=float,
name='reflection normal',
)
transform = reflection(valid_normal)
return self._compose_with_translations(transform, point=point, multiply_mode=multiply_mode)
def flip_x(
self: Transform,
*,
point: VectorLike[float] | None = None,
multiply_mode: Literal['pre', 'post'] | None = None,
) -> Transform: # numpydoc ignore=RT01
"""Compose a reflection about the x-axis.
Create a reflection about the x-axis and :meth:`compose` it
with the current transformation :attr:`matrix` according to pre-multiply or
post-multiply semantics.
Internally, the matrix is stored in the :attr:`matrix_list`.
Parameters
----------
point : VectorLike[float], optional
Point to reflect about. By default, the object's :attr:`point` is used,
but this can be overridden.
If set, two additional transformations are composed and added to
the :attr:`matrix_list`:
- :meth:`translate` to ``point`` before the reflection
- :meth:`translate` away from ``point`` after the reflection
multiply_mode : 'pre' | 'post', optional
Multiplication mode to use when composing the matrix. By default, the
object's :attr:`multiply_mode` is used, but this can be overridden. Set this
to ``'pre'`` for pre-multiplication or ``'post'`` for post-multiplication.
See Also
--------
pyvista.DataObjectFilters.flip_x
Flip a mesh about the x-axis.
Examples
--------
Compose a reflection about the x-axis.
>>> import pyvista as pv
>>> transform = pv.Transform()
>>> _ = transform.flip_x()
>>> transform.matrix
array([[-1., 0., 0., 0.],
[ 0., 1., 0., 0.],
[ 0., 0., 1., 0.],
[ 0., 0., 0., 1.]])
Compose a second reflection, but this time about a point.
>>> _ = transform.flip_x(point=(4, 5, 6))
>>> transform.matrix
array([[1., 0., 0., 8.],
[0., 1., 0., 0.],
[0., 0., 1., 0.],
[0., 0., 0., 1.]])
"""
return self.reflect((1, 0, 0), point=point, multiply_mode=multiply_mode)
def flip_y(
self: Transform,
*,
point: VectorLike[float] | None = None,
multiply_mode: Literal['pre', 'post'] | None = None,
) -> Transform: # numpydoc ignore=RT01
"""Compose a reflection about the y-axis.
Create a reflection about the y-axis and :meth:`compose` it
with the current transformation :attr:`matrix` according to pre-multiply or
post-multiply semantics.
Internally, the matrix is stored in the :attr:`matrix_list`.
Parameters
----------
point : VectorLike[float], optional
Point to reflect about. By default, the object's :attr:`point` is used,
but this can be overridden.
If set, two additional transformations are composed and added to
the :attr:`matrix_list`:
- :meth:`translate` to ``point`` before the reflection
- :meth:`translate` away from ``point`` after the reflection
multiply_mode : 'pre' | 'post', optional
Multiplication mode to use when composing the matrix. By default, the
object's :attr:`multiply_mode` is used, but this can be overridden. Set this
to ``'pre'`` for pre-multiplication or ``'post'`` for post-multiplication.
See Also
--------
pyvista.DataObjectFilters.flip_y
Flip a mesh about the y-axis.
Examples
--------
Compose a reflection about the y-axis.
>>> import pyvista as pv
>>> transform = pv.Transform()
>>> _ = transform.flip_y()
>>> transform.matrix
array([[ 1., 0., 0., 0.],
[ 0., -1., 0., 0.],
[ 0., 0., 1., 0.],
[ 0., 0., 0., 1.]])
Compose a second reflection, but this time about a point.
>>> _ = transform.flip_y(point=(4, 5, 6))
>>> transform.matrix
array([[ 1., 0., 0., 0.],
[ 0., 1., 0., 10.],
[ 0., 0., 1., 0.],
[ 0., 0., 0., 1.]])
"""
return self.reflect((0, 1, 0), point=point, multiply_mode=multiply_mode)
def flip_z(
self: Transform,
*,
point: VectorLike[float] | None = None,
multiply_mode: Literal['pre', 'post'] | None = None,
) -> Transform: # numpydoc ignore=RT01
"""Compose a reflection about the z-axis.
Create a reflection about the z-axis and :meth:`compose` it
with the current transformation :attr:`matrix` according to pre-multiply or
post-multiply semantics.
Internally, the matrix is stored in the :attr:`matrix_list`.
Parameters
----------
point : VectorLike[float], optional
Point to reflect about. By default, the object's :attr:`point` is used,
but this can be overridden.
If set, two additional transformations are composed and added to
the :attr:`matrix_list`:
- :meth:`translate` to ``point`` before the reflection
- :meth:`translate` away from ``point`` after the reflection
multiply_mode : 'pre' | 'post', optional
Multiplication mode to use when composing the matrix. By default, the
object's :attr:`multiply_mode` is used, but this can be overridden. Set this
to ``'pre'`` for pre-multiplication or ``'post'`` for post-multiplication.
See Also
--------
pyvista.DataObjectFilters.flip_z
Flip a mesh about the z-axis.
Examples
--------
Compose a reflection about the z-axis.
>>> import pyvista as pv
>>> transform = pv.Transform()
>>> _ = transform.flip_z()
>>> transform.matrix
array([[ 1., 0., 0., 0.],
[ 0., 1., 0., 0.],
[ 0., 0., -1., 0.],
[ 0., 0., 0., 1.]])
Compose a second reflection, but this time about a point.
>>> _ = transform.flip_z(point=(4, 5, 6))
>>> transform.matrix
array([[ 1., 0., 0., 0.],
[ 0., 1., 0., 0.],
[ 0., 0., 1., 12.],
[ 0., 0., 0., 1.]])
"""
return self.reflect((0, 0, 1), point=point, multiply_mode=multiply_mode)
def translate(
self: Transform,
*vector: float | VectorLike[float],
multiply_mode: Literal['pre', 'post'] | None = None,
) -> Transform: # numpydoc ignore=RT01
"""Compose a translation matrix.
Create a translation matrix and :meth:`compose` it with the current
transformation :attr:`matrix` according to pre-multiply or post-multiply
semantics.
Internally, the matrix is stored in the :attr:`matrix_list`.
Parameters
----------
*vector : float | VectorLike[float]
Vector to use for translation. May be a single vector (one arg) or
unpacked vector (three args).
multiply_mode : 'pre' | 'post', optional
Multiplication mode to use when composing the matrix. By default, the
object's :attr:`multiply_mode` is used, but this can be overridden. Set this
to ``'pre'`` for pre-multiplication or ``'post'`` for post-multiplication.
See Also
--------
pyvista.DataObjectFilters.translate
Translate a mesh.
Examples
--------
Compose a translation matrix.
>>> import pyvista as pv
>>> transform = pv.Transform().translate(1, 2, 3)
>>> transform.matrix
array([[1., 0., 0., 1.],
[0., 1., 0., 2.],
[0., 0., 1., 3.],
[0., 0., 0., 1.]])
Compose a second translation matrix using ``+``.
>>> transform = transform + (1, 1, 1)
>>> transform.matrix
array([[1., 0., 0., 2.],
[0., 1., 0., 3.],
[0., 0., 1., 4.],
[0., 0., 0., 1.]])
"""
valid_vector = _validation.validate_array3(
vector, # type: ignore[arg-type]
dtype_out=float,
name='translation vector',
)
transform = _vtk.vtkTransform()
transform.Translate(valid_vector)
return self.compose(transform, multiply_mode=multiply_mode)
def rotate(
self: Transform,
rotation: RotationLike,
*,
point: VectorLike[float] | None = None,
multiply_mode: Literal['pre', 'post'] | None = None,
) -> Transform: # numpydoc ignore=RT01
"""Compose a rotation matrix.
Create a rotation matrix and :meth:`compose` it with the current
transformation :attr:`matrix` according to pre-multiply or post-multiply
semantics. The rotation may be right-handed or left-handed.
Internally, the matrix is stored in the :attr:`matrix_list`.
Parameters
----------
rotation : RotationLike
3x3 rotation matrix or a SciPy ``Rotation`` object.
point : VectorLike[float], optional
Point to rotate about. By default, the object's :attr:`point` is used,
but this can be overridden.
If set, two additional transformations are composed and added to
the :attr:`matrix_list`:
- :meth:`translate` to ``point`` before the rotation
- :meth:`translate` away from ``point`` after the rotation
multiply_mode : 'pre' | 'post', optional
Multiplication mode to use when composing the matrix. By default, the
object's :attr:`multiply_mode` is used, but this can be overridden. Set this
to ``'pre'`` for pre-multiplication or ``'post'`` for post-multiplication.
See Also
--------
as_rotation
Get this transform's rotation component.
pyvista.DataObjectFilters.rotate
Rotate a mesh.
Examples
--------
Compose a rotation matrix. In this case the rotation rotates about the
z-axis by 90 degrees.
>>> import pyvista as pv
>>> rotation_z_90 = [[0, -1, 0], [1, 0, 0], [0, 0, 1]]
>>> transform = pv.Transform().rotate(rotation_z_90)
>>> transform.matrix
array([[ 0., -1., 0., 0.],
[ 1., 0., 0., 0.],
[ 0., 0., 1., 0.],
[ 0., 0., 0., 1.]])
Compose a second rotation matrix. In this case we use the same rotation as
before.
>>> _ = transform.rotate(rotation_z_90)
The result is a matrix that rotates about the z-axis by 180 degrees.
>>> transform.matrix
array([[-1., 0., 0., 0.],
[ 0., -1., 0., 0.],
[ 0., 0., 1., 0.],
[ 0., 0., 0., 1.]])
Rotate about a point. Check the :attr:`matrix_list` to see that a translation
is added before and after the rotation.
>>> transform = pv.Transform().rotate(rotation_z_90, point=(1, 2, 3))
>>> translation_to_origin = transform.matrix_list[0]
>>> translation_to_origin
array([[ 1., 0., 0., -1.],
[ 0., 1., 0., -2.],
[ 0., 0., 1., -3.],
[ 0., 0., 0., 1.]])
>>> rotation = transform.matrix_list[1]
>>> rotation
array([[ 0., -1., 0., 0.],
[ 1., 0., 0., 0.],
[ 0., 0., 1., 0.],
[ 0., 0., 0., 1.]])
>>> translation_from_origin = transform.matrix_list[2]
>>> translation_from_origin
array([[1., 0., 0., 1.],
[0., 1., 0., 2.],
[0., 0., 1., 3.],
[0., 0., 0., 1.]])
"""
valid_rotation = _validation.validate_rotation(rotation)
return self._compose_with_translations(
valid_rotation, point=point, multiply_mode=multiply_mode
)
def rotate_x(
self: Transform,
angle: float,
*,
point: VectorLike[float] | None = None,
multiply_mode: Literal['pre', 'post'] | None = None,
) -> Transform: # numpydoc ignore=RT01
"""Compose a rotation about the x-axis.
Create a matrix for rotation about the x-axis and :meth:`compose`
it with the current transformation :attr:`matrix` according to pre-multiply or
post-multiply semantics.
Internally, the matrix is stored in the :attr:`matrix_list`.
Parameters
----------
angle : float
Angle in degrees to rotate about the x-axis.
point : VectorLike[float], optional
Point to rotate about. By default, the object's :attr:`point` is used,
but this can be overridden.
If set, two additional transformations are composed and added to
the :attr:`matrix_list`:
- :meth:`translate` to ``point`` before the rotation
- :meth:`translate` away from ``point`` after the rotation
multiply_mode : 'pre' | 'post', optional
Multiplication mode to use when composing the matrix. By default, the
object's :attr:`multiply_mode` is used, but this can be overridden. Set this
to ``'pre'`` for pre-multiplication or ``'post'`` for post-multiplication.
See Also
--------
as_rotation
Get this transform's rotation component.
pyvista.DataObjectFilters.rotate_x
Rotate a mesh about the x-axis.
Examples
--------
Compose a rotation about the x-axis.
>>> import pyvista as pv
>>> transform = pv.Transform().rotate_x(90)
>>> transform.matrix
array([[ 1., 0., 0., 0.],
[ 0., 0., -1., 0.],
[ 0., 1., 0., 0.],
[ 0., 0., 0., 1.]])
Compose a second rotation about the x-axis.
>>> _ = transform.rotate_x(45)
The result is a matrix that rotates about the x-axis by 135 degrees.
>>> transform.matrix
array([[ 1. , 0. , 0. , 0. ],
[ 0. , -0.70710678, -0.70710678, 0. ],
[ 0. , 0.70710678, -0.70710678, 0. ],
[ 0. , 0. , 0. , 1. ]])
"""
transform = axis_angle_rotation((1, 0, 0), angle, deg=True)
return self._compose_with_translations(transform, point=point, multiply_mode=multiply_mode)
def rotate_y(
self: Transform,
angle: float,
*,
point: VectorLike[float] | None = None,
multiply_mode: Literal['pre', 'post'] | None = None,
) -> Transform: # numpydoc ignore=RT01
"""Compose a rotation about the y-axis.
Create a matrix for rotation about the y-axis and :meth:`compose`
it with the current transformation :attr:`matrix` according to pre-multiply or
post-multiply semantics.
Internally, the matrix is stored in the :attr:`matrix_list`.
Parameters
----------
angle : float
Angle in degrees to rotate about the y-axis.
point : VectorLike[float], optional
Point to rotate about. By default, the object's :attr:`point` is used,
but this can be overridden.
If set, two additional transformations are composed and added to
the :attr:`matrix_list`:
- :meth:`translate` to ``point`` before the rotation
- :meth:`translate` away from ``point`` after the rotation
multiply_mode : 'pre' | 'post', optional
Multiplication mode to use when composing the matrix. By default, the
object's :attr:`multiply_mode` is used, but this can be overridden. Set this
to ``'pre'`` for pre-multiplication or ``'post'`` for post-multiplication.
See Also
--------
as_rotation
Get this transform's rotation component.
pyvista.DataObjectFilters.rotate_y
Rotate a mesh about the y-axis.
Examples
--------
Compose a rotation about the y-axis.
>>> import pyvista as pv
>>> transform = pv.Transform().rotate_y(90)
>>> transform.matrix
array([[ 0., 0., 1., 0.],
[ 0., 1., 0., 0.],
[-1., 0., 0., 0.],
[ 0., 0., 0., 1.]])
Compose a second rotation about the y-axis.
>>> _ = transform.rotate_y(45)
The result is a matrix that rotates about the y-axis by 135 degrees.
>>> transform.matrix
array([[-0.70710678, 0. , 0.70710678, 0. ],
[ 0. , 1. , 0. , 0. ],
[-0.70710678, 0. , -0.70710678, 0. ],
[ 0. , 0. , 0. , 1. ]])
"""
transform = axis_angle_rotation((0, 1, 0), angle, deg=True)
return self._compose_with_translations(transform, point=point, multiply_mode=multiply_mode)
def rotate_z(
self: Transform,
angle: float,
*,
point: VectorLike[float] | None = None,
multiply_mode: Literal['pre', 'post'] | None = None,
) -> Transform: # numpydoc ignore=RT01
"""Compose a rotation about the z-axis.
Create a matrix for rotation about the z-axis and :meth:`compose`
it with the current transformation :attr:`matrix` according to pre-multiply or
post-multiply semantics.
Internally, the matrix is stored in the :attr:`matrix_list`.
Parameters
----------
angle : float
Angle in degrees to rotate about the z-axis.
point : VectorLike[float], optional
Point to rotate about. By default, the object's :attr:`point` is used,
but this can be overridden.
If set, two additional transformations are composed and added to
the :attr:`matrix_list`:
- :meth:`translate` to ``point`` before the rotation
- :meth:`translate` away from ``point`` after the rotation
multiply_mode : 'pre' | 'post', optional
Multiplication mode to use when composing the matrix. By default, the
object's :attr:`multiply_mode` is used, but this can be overridden. Set this
to ``'pre'`` for pre-multiplication or ``'post'`` for post-multiplication.
See Also
--------
as_rotation
Get this transform's rotation component.
pyvista.DataObjectFilters.rotate_z
Rotate a mesh about the z-axis.
Examples
--------
Compose a rotation about the z-axis.
>>> import pyvista as pv
>>> transform = pv.Transform().rotate_z(90)
>>> transform.matrix
array([[ 0., -1., 0., 0.],
[ 1., 0., 0., 0.],
[ 0., 0., 1., 0.],
[ 0., 0., 0., 1.]])
Compose a second rotation about the z-axis.
>>> _ = transform.rotate_z(45)
The result is a matrix that rotates about the z-axis by 135 degrees.
>>> transform.matrix
array([[-0.70710678, -0.70710678, 0. , 0. ],
[ 0.70710678, -0.70710678, 0. , 0. ],
[ 0. , 0. , 1. , 0. ],
[ 0. , 0. , 0. , 1. ]])
"""
transform = axis_angle_rotation((0, 0, 1), angle, deg=True)
return self._compose_with_translations(transform, point=point, multiply_mode=multiply_mode)
def rotate_vector(
self: Transform,
vector: VectorLike[float],
angle: float,
*,
point: VectorLike[float] | None = None,
multiply_mode: Literal['pre', 'post'] | None = None,
) -> Transform: # numpydoc ignore=RT01
"""Compose a rotation about a vector.
Create a matrix for rotation about the vector and :meth:`compose`
it with the current transformation :attr:`matrix` according to pre-multiply or
post-multiply semantics.
Internally, the matrix is stored in the :attr:`matrix_list`.
Parameters
----------
vector : VectorLike[float]
Vector to rotate about.
angle : float
Angle in degrees to rotate about the vector.
point : VectorLike[float], optional
Point to rotate about. By default, the object's :attr:`point` is used,
but this can be overridden.
If set, two additional transformations are composed and added to
the :attr:`matrix_list`:
- :meth:`translate` to ``point`` before the rotation
- :meth:`translate` away from ``point`` after the rotation
multiply_mode : 'pre' | 'post', optional
Multiplication mode to use when composing the matrix. By default, the
object's :attr:`multiply_mode` is used, but this can be overridden. Set this
to ``'pre'`` for pre-multiplication or ``'post'`` for post-multiplication.
See Also
--------
as_rotation
Get this transform's rotation component.
pyvista.DataObjectFilters.rotate_vector
Rotate a mesh about a vector.
Examples
--------
Compose a rotation of 30 degrees about the ``(1, 1, 1)`` axis.
>>> import pyvista as pv
>>> transform = pv.Transform().rotate_vector((1, 1, 1), 30)
>>> transform.matrix
array([[ 0.9106836 , -0.24401694, 0.33333333, 0. ],
[ 0.33333333, 0.9106836 , -0.24401694, 0. ],
[-0.24401694, 0.33333333, 0.9106836 , 0. ],
[ 0. , 0. , 0. , 1. ]])
Compose a second rotation of 45 degrees about the ``(1, 2, 3)`` axis.
>>> _ = transform.rotate_vector((1, 2, 3), 45)
>>> transform.matrix
array([[ 0.38042304, -0.50894634, 0.77217351, 0. ],
[ 0.83349512, 0.55045308, -0.04782562, 0. ],
[-0.40070461, 0.66179682, 0.63360933, 0. ],
[ 0. , 0. , 0. , 1. ]])
"""
transform = axis_angle_rotation(vector, angle, deg=True)
return self._compose_with_translations(transform, point=point, multiply_mode=multiply_mode)
def compose(
self: Transform,
transform: TransformLike,
*,
multiply_mode: Literal['pre', 'post'] | None = None,
) -> Transform: # numpydoc ignore=RT01
"""Compose a transformation matrix.
Create a 4x4 matrix from any transform-like input and compose it with the
current transformation :attr:`matrix` according to pre-multiply or post-multiply
semantics.
Internally, the matrix is stored in the :attr:`matrix_list`.
Parameters
----------
transform : TransformLike
Any transform-like input such as a 3x3 or 4x4 array or matrix.
multiply_mode : 'pre' | 'post', optional
Multiplication mode to use when composing the matrix. By default, the
object's :attr:`multiply_mode` is used, but this can be overridden. Set this
to ``'pre'`` for pre-multiplication or ``'post'`` for post-multiplication.
See Also
--------
decompose
Examples
--------
Define an arbitrary 4x4 affine transformation matrix and compose it.
>>> import pyvista as pv
>>> array = [
... [0.707, -0.707, 0, 0],
... [0.707, 0.707, 0, 0],
... [0, 0, 1, 1.5],
... [0, 0, 0, 2],
... ]
>>> transform = pv.Transform().compose(array)
>>> transform.matrix
array([[ 0.707, -0.707, 0. , 0. ],
[ 0.707, 0.707, 0. , 0. ],
[ 0. , 0. , 1. , 1.5 ],
[ 0. , 0. , 0. , 2. ]])
Define a second transformation and use ``+`` to compose it.
>>> array = [[1, 0, 0], [0, 0, -1], [0, -1, 0]]
>>> transform = transform * array
>>> transform.matrix
array([[ 0.707, -0.707, 0. , 0. ],
[ 0. , 0. , -1. , -1.5 ],
[-0.707, -0.707, 0. , 0. ],
[ 0. , 0. , 0. , 2. ]])
"""
# Make sure we have a vtkTransform
if isinstance(transform, _vtk.vtkTransform):
vtk_transform = transform
else:
array = _validation.validate_transform4x4(
transform, must_be_finite=self.check_finite, name='matrix'
)
vtk_transform = _vtk.vtkTransform()
vtk_transform.SetMatrix(vtkmatrix_from_array(array))
if multiply_mode is not None:
# Override multiply mode
original_mode = self.multiply_mode
self.multiply_mode = multiply_mode
self.Concatenate(vtk_transform)
if multiply_mode is not None:
self.multiply_mode = original_mode
return self
@property
def matrix(self: Transform) -> NumpyArray[float]:
"""Return or set the current transformation matrix.
Notes
-----
This matrix is a single 4x4 matrix computed from composing all
transformations. Use :attr:`matrix_list` instead to get a list of the
individual transformations.
See Also
--------
inverse_matrix
Return the inverse of the current transformation.
Returns
-------
NDArray[float]
Current transformation matrix.
"""
array = array_from_vtkmatrix(self.GetMatrix())
if self.check_finite:
_validation.check_finite(array, name='matrix')
return array
@matrix.setter
def matrix(self: Transform, trans: TransformLike) -> None:
self.identity()
self.compose(trans)
@property
def inverse_matrix(self: Transform) -> NumpyArray[float]:
"""Return the inverse of the current transformation :attr:`matrix`.
Notes
-----
This matrix is a single 4x4 matrix computed from composing the inverse of
all transformations. Use :attr:`inverse_matrix_list` instead to get a list of
the individual inverse transformations.
See Also
--------
matrix
Return the current transformation matrix.
Returns
-------
NDArray[float]
Current inverse transformation matrix.
"""
array = array_from_vtkmatrix(self.GetInverse().GetMatrix()) # type: ignore[attr-defined]
if self.check_finite:
_validation.check_finite(array, name='matrix')
return array
@property
def matrix_list(self: Transform) -> list[NumpyArray[float]]:
"""Return a list of all current transformation matrices.
Notes
-----
The list comprises all 4x4 transformation matrices. Use :attr:`matrix` instead
to get the composed result as a single 4x4 matrix.
See Also
--------
inverse_matrix_list
Return the current transformation matrix.
Returns
-------
list[NDArray[float]]
List of all current transformation matrices.
"""
return [
array_from_vtkmatrix(self.GetConcatenatedTransform(i).GetMatrix())
for i in range(self.n_transformations)
]
@property
def inverse_matrix_list(self: Transform) -> list[NumpyArray[float]]:
"""Return a list of all inverse transformations applied by this :class:`Transform`.
Notes
-----
The list comprises all 4x4 inverse transformation matrices. Use
:attr:`inverse_matrix` instead to get the composed result as a single
4x4 matrix.
See Also
--------
matrix_list
Return a list of all transformation matrices.
Returns
-------
list[NDArray[float]]
List of all current inverse transformation matrices.
"""
return [
array_from_vtkmatrix(self.GetConcatenatedTransform(i).GetInverse().GetMatrix()) # type: ignore[attr-defined]
for i in range(self.n_transformations)
]
@property
def n_transformations(self: Transform) -> int: # numpydoc ignore: RT01
"""Return the current number of composed transformations."""
return self.GetNumberOfConcatenatedTransforms()
@overload
def apply(
self: Transform,
obj: VectorLike[float] | MatrixLike[float],
/,
mode: Literal['points', 'vectors'] | None = ...,
*,
inverse: bool = ...,
copy: bool = ...,
) -> NumpyArray[float]: ...
@overload
def apply(
self: Transform,
obj: _DataSetOrMultiBlockType,
/,
mode: Literal['active_vectors', 'all_vectors'] = ...,
*,
inverse: bool = ...,
copy: bool = ...,
) -> _DataSetOrMultiBlockType: ...
@overload
def apply(
self: Transform,
obj: Prop3D,
/,
mode: Literal['replace', 'pre-multiply', 'post-multiply'] = ...,
*,
inverse: bool = ...,
copy: bool = ...,
) -> Prop3D: ...
def apply(
self: Transform,
obj: VectorLike[float] | MatrixLike[float] | DataSet | MultiBlock | Prop3D,
/,
mode: Literal[
'points',
'vectors',
'active_vectors',
'all_vectors',
'replace',
'pre-multiply',
'post-multiply',
]
| None = None,
*,
inverse: bool = False,
copy: bool = True,
):
"""Apply the current transformation :attr:`matrix` to points, vectors, a dataset, or actor.
.. note::
Points with integer values are cast to a float type before the
transformation is applied. A similar casting is also performed when
transforming datasets. See also the notes at
:func:`~pyvista.DataObjectFilters.transform`
which is used by this filter under the hood.
Parameters
----------
obj : VectorLike[float] | MatrixLike[float] | DataSet | MultiBlock | Prop3D
Object to apply the transformation to.
mode : str, optional
Define how to apply the transformation. Different modes may be used depending
on the input type.
#. For array inputs:
- ``'points'`` transforms point arrays.
- ``'vectors'`` transforms vector arrays. The translation component of
the transformation is removed for vectors.
By default, ``'points'`` mode is used for array inputs.
#. For dataset inputs:
The dataset's points are always transformed, and its vectors are
transformed based on the mode:
- ``'active_vectors'`` transforms active normals and active vectors
arrays only.
- ``'all_vectors'`` transforms `all` input vectors, i.e. all arrays
with three components. This mode is equivalent to setting
``transform_all_input_vectors=True``
with :meth:`pyvista.DataObjectFilters.transform`.
By default, only ``'active_vectors'`` are transformed.
#. For actor inputs:
- ``'pre-multiply'`` pre-multiplies this transform with the actor's
:attr:`~pyvista.Prop3D.user_matrix`.
- ``'post-multiply'`` post-multiplies this transform with the actor's
user-matrix.
- ``'replace'`` replaces the actor's user-matrix with this transform's
:attr:`matrix`.
By default, ``'post-multiply'`` mode is used for actors.
inverse : bool, default: False
Apply the transformation using the :attr:`inverse_matrix` instead of the
:attr:`matrix`.
copy : bool, default: True
Return a copy of the input with the transformation applied. Set this to
``False`` to transform the input directly and return it. Setting this to
``False`` only applies to NumPy float arrays, datasets, and actors; a copy
is always returned for tuple and list inputs or arrays with integers.
Returns
-------
np.ndarray | DataSet | MultiBlock | Prop3D
Transformed array, dataset, or actor.
See Also
--------
apply_to_points
Equivalent to ``apply(obj, 'points')`` for point-array inputs.
apply_to_vectors
Equivalent to ``apply(obj, 'vectors')`` for vector-array inputs.
apply_to_dataset
Equivalent to ``apply(obj, mode)`` for dataset inputs where ``mode`` may be
``'active_vectors'`` or ``'all_vectors'``.
apply_to_actor
Equivalent to ``apply(obj, mode)`` for actor inputs where ``mode`` may be
``'pre-multiply'``, ``'post-multiply'``, or ``'replace'``.
pyvista.DataObjectFilters.transform
Transform a dataset.
pyvista.Prop3D.transform
Transform an actor.
Examples
--------
Apply a transformation to a point.
>>> import numpy as np
>>> import pyvista as pv
>>> point = (1.0, 2.0, 3.0)
>>> transform = pv.Transform().scale(2).translate((0.0, 0.0, 1.0))
>>> transformed = transform.apply(point)
>>> transformed
array([2., 4., 7.])
Apply a transformation to a points array.
>>> array = np.array([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]])
>>> transformed = transform.apply(array)
>>> transformed
array([[ 2., 4., 7.],
[ 8., 10., 13.]])
Apply a transformation to a vector array. Use the same array as before but
use the ``'vectors'`` mode. Note how the translation component is not applied
to vectors.
>>> transformed = transform.apply(array, 'vectors')
>>> transformed
array([[ 2., 4., 6.],
[ 8., 10., 12.]])
Apply a transformation to a dataset.
>>> dataset = pv.PolyData(array)
>>> transformed = transform.apply(dataset)
>>> transformed.points
pyvista_ndarray([[ 2., 4., 6.],
[ 8., 10., 12.]])
Apply the inverse.
>>> transformed = transform.apply(dataset, inverse=True)
>>> transformed.points
pyvista_ndarray([[0.5, 1. , 1. ],
[2. , 2.5, 2.5]])
Apply a transformation to an actor.
>>> actor = pv.Actor()
>>> transformed_actor = transform.apply(actor)
>>> transformed_actor.user_matrix
array([[2., 0., 0., 0.],
[0., 2., 0., 0.],
[0., 0., 2., 0.],
[0., 0., 0., 1.]])
"""
def _check_mode(kind: str, mode_: str | None, allowed_modes: list[str | None]) -> None:
if mode_ not in allowed_modes:
msg = (
f"Transformation mode '{mode_}' is not supported for {kind}. "
'Mode must be one of'
f'\n{allowed_modes}'
)
raise ValueError(msg)
_validation.check_contains(
[
'points',
'vectors',
'active_vectors',
'all_vectors',
'replace',
'pre-multiply',
'post-multiply',
None,
],
must_contain=mode,
name='mode',
)
_validation.check_instance(
obj,
(
np.ndarray,
Sequence,
pyvista.DataSet,
pyvista.MultiBlock,
pyvista.Prop3D,
),
)
inplace = not copy
# Transform dataset
if isinstance(obj, (pyvista.DataSet, pyvista.MultiBlock)):
allowed = ['active_vectors', 'all_vectors', None]
_check_mode('datasets', mode, allowed)
if mode in ['active_vectors', None]:
mode = None
return obj.transform(
self.copy().invert() if inverse else self,
inplace=inplace,
transform_all_input_vectors=bool(mode),
)
matrix = self.inverse_matrix if inverse else self.matrix
# Transform actor
if isinstance(obj, pyvista.Prop3D):
allowed = ['replace', 'pre-multiply', 'post-multiply', None]
_check_mode('actors', mode, allowed)
if mode in ['post-multiply', None]:
return obj.transform(matrix, 'post', inplace=inplace)
elif mode == 'pre-multiply':
return obj.transform(matrix, 'pre', inplace=inplace)
else:
actor = obj.copy() if copy else obj
actor.user_matrix = matrix
return actor
# Transform array
allowed = ['points', 'vectors', None]
_check_mode('arrays', mode, allowed)
if mode == 'vectors':
# Remove translation
matrix[:3, 3] = 0
# Validate array - make sure we have floats
array: NumpyArray[float] = _validation.validate_array(obj, must_have_shape=[(3,), (-1, 3)])
array = array if np.issubdtype(array.dtype, np.floating) else array.astype(float)
# Transform a 1D array
out: NumpyArray[float] | None
if array.shape == (3,):
out = (matrix @ (*array, 1))[:3]
if inplace:
array[:] = out
out = array
return out
# Transform a 2D array
out = apply_transformation_to_points(matrix, array, inplace=inplace)
return out if out is not None else array
def apply_to_points(
self,
points: VectorLike[float] | MatrixLike[float],
/,
*,
inverse: bool = False,
copy: bool = True,
) -> NumpyArray[float]:
"""Apply the current transformation :attr:`matrix` to a point or points.
This is equivalent to ``apply(points, 'points')``. See :meth:`apply` for
details and examples.
Parameters
----------
points : VectorLike[float] | MatrixLike[float]
Single point or ``Nx3`` points array to apply the transformation to.
inverse : bool, default: False
Apply the transformation using the :attr:`inverse_matrix` instead of the
:attr:`matrix`.
copy : bool, default: True
Return a copy of the input with the transformation applied. Set this to
``False`` to transform the input directly and return it. Only applies to
NumPy arrays. A copy is always returned for tuple and list
inputs or point arrays with integers.
Returns
-------
np.ndarray
Transformed points.
See Also
--------
apply
Apply this transformation to any input.
apply_to_vectors
Apply this transformation to vectors.
apply_to_dataset
Apply this transformation to a dataset.
apply_to_actor
Apply this transformation to an actor.
"""
return self.apply(points, 'points', inverse=inverse, copy=copy)
def apply_to_vectors(
self,
vectors: VectorLike[float] | MatrixLike[float],
/,
*,
inverse: bool = False,
copy: bool = True,
) -> NumpyArray[float]:
"""Apply the current transformation :attr:`matrix` to a vector or vectors.
This is equivalent to ``apply(vectors, 'vectors')``. See :meth:`apply` for
details and examples.
Parameters
----------
vectors : VectorLike[float] | MatrixLike[float]
Single vector or ``Nx3`` vectors array to apply the transformation to.
inverse : bool, default: False
Apply the transformation using the :attr:`inverse_matrix` instead of the
:attr:`matrix`.
copy : bool, default: True
Return a copy of the input with the transformation applied. Set this to
``False`` to transform the input directly and return it. Only applies to
NumPy arrays. A copy is always returned for tuple and list
inputs or point arrays with integers.
Returns
-------
np.ndarray
Transformed vectors.
See Also
--------
apply
Apply this transformation to any input.
apply_to_points
Apply this transformation to points.
apply_to_dataset
Apply this transformation to a dataset.
apply_to_actor
Apply this transformation to an actor.
"""
return self.apply(vectors, 'vectors', inverse=inverse, copy=copy)
def apply_to_dataset(
self,
dataset: _DataSetOrMultiBlockType,
/,
mode: Literal['active_vectors', 'all_vectors'] = 'active_vectors',
*,
copy: bool = True,
inverse: bool = False,
) -> _DataSetOrMultiBlockType:
"""Apply the current transformation :attr:`matrix` to a dataset.
This is equivalent to ``apply(dataset, mode)``. See :meth:`apply` for details
and examples.
Parameters
----------
dataset : DataSet | MultiBlock
Object to apply the transformation to.
mode : 'active_vectors' | 'all_vectors', default: 'active_vectors'
Mode for transforming the dataset's vectors:
- ``'active_vectors'`` transforms active normals and active vectors arrays
only.
- ``'all_vectors'`` transforms `all` input vectors, i.e. all arrays with
three components. This mode is equivalent to setting
``transform_all_input_vectors=True``
with :meth:`pyvista.DataObjectFilters.transform`.
inverse : bool, default: False
Apply the transformation using the :attr:`inverse_matrix` instead of the
:attr:`matrix`.
copy : bool, default: True
Return a copy of the input with the transformation applied. Set this to
``False`` to transform the input directly and return it.
Returns
-------
DataSet | MultiBlock
Transformed dataset.
See Also
--------
apply
Apply this transformation to any input.
apply_to_points
Apply this transformation to points.
apply_to_vectors
Apply this transformation to vectors.
apply_to_actor
Apply this transformation to an actor.
pyvista.DataObjectFilters.transform
Transform a dataset.
"""
return self.apply(dataset, mode, inverse=inverse, copy=copy)
def apply_to_actor(
self,
actor: Prop3D,
/,
mode: Literal['pre-multiply', 'post-multiply', 'replace'] = 'post-multiply',
*,
copy: bool = True,
inverse: bool = False,
) -> Prop3D:
"""Apply the current transformation :attr:`matrix` to an actor.
This is equivalent to ``apply(actor, mode)``. See :meth:`apply` for details and
examples.
Parameters
----------
actor : Prop3D
Actor to apply the transformation to.
mode : 'pre-multiply', 'post-multiply', 'replace', default: 'post-multiply'
Mode for transforming the actor:
- ``'pre-multiply'`` pre-multiplies this transform with the actor's
:attr:`~pyvista.Prop3D.user_matrix`.
- ``'post-multiply'`` post-multiplies this transform with the actor's
user-matrix.
- ``'replace'`` replaces the actor's user-matrix with this transform's
:attr:`matrix`.
By default, ``'post-multiply'`` mode is used.
inverse : bool, default: False
Apply the transformation using the :attr:`inverse_matrix` instead of the
:attr:`matrix`.
copy : bool, default: True
Return a copy of the input with the transformation applied. Set this to
``False`` to transform the input directly and return it.
Returns
-------
Prop3D
Transformed actor.
See Also
--------
apply
Apply this transformation to any input.
apply_to_points
Apply this transformation to points.
apply_to_vectors
Apply this transformation to vectors.
apply_to_dataset
Apply this transformation to a dataset.
pyvista.Prop3D.transform
Transform an actor.
"""
return self.apply(actor, mode, inverse=inverse, copy=copy)
def decompose(
self: Transform,
*,
homogeneous: bool = False,
) -> tuple[
NumpyArray[float],
NumpyArray[float],
NumpyArray[float],
NumpyArray[float],
NumpyArray[float],
]:
"""Decompose the current transformation into its components.
Decompose the :attr:`matrix` ``M`` into
- translation ``T``
- rotation ``R``
- reflection ``N``
- scaling ``S``
- shearing ``K``
such that, when represented as 4x4 matrices, ``M = TRNSK``. The decomposition is
unique and is computed with polar matrix decomposition.
By default, compact representations of the transformations are returned (e.g. as a
3-element vector or a 3x3 matrix). Optionally, 4x4 matrices may be returned instead.
.. note::
- The rotation is orthonormal and right-handed with positive determinant.
- The scaling factors are positive.
- The reflection is either ``1`` (no reflection) or ``-1`` (has reflection)
and can be used like a scaling factor.
Parameters
----------
homogeneous : bool, default: False
If ``True``, return the components (translation, rotation, etc.) as 4x4
homogeneous matrices. By default, reflection is a scalar, translation and
scaling are length-3 vectors, and rotation and shear are 3x3 matrices.
Returns
-------
numpy.ndarray
Translation component ``T``. Returned as a 3-element vector (or a 4x4
translation matrix if ``homogeneous`` is ``True``).
numpy.ndarray
Rotation component ``R``. Returned as a 3x3 orthonormal rotation matrix of row
vectors (or a 4x4 rotation matrix if ``homogeneous`` is ``True``).
numpy.ndarray
Reflection component ``N``. Returned as a NumPy scalar (or a 4x4 reflection
matrix if ``homogeneous`` is ``True``).
numpy.ndarray
Scaling component ``S``. Returned as a 3-element vector (or a 4x4 scaling matrix
if ``homogeneous`` is ``True``).
numpy.ndarray
Shear component ``K``. Returned as a 3x3 matrix with ones on the diagonal and
shear values in the off-diagonals (or as a 4x4 shearing matrix if ``homogeneous``
is ``True``).
See Also
--------
compose
Compose a transformation.
as_rotation
Get this transform's rotation component.
Examples
--------
Create a transform by composing scaling, rotation, and translation
matrices.
>>> import numpy as np
>>> import pyvista as pv
>>> transform = pv.Transform()
>>> _ = transform.scale(1, 2, 3)
>>> _ = transform.rotate_z(90)
>>> _ = transform.translate(4, 5, 6)
>>> transform
Transform (...)
Num Transformations: 3
Matrix: [[ 0., -2., 0., 4.],
[ 1., 0., 0., 5.],
[ 0., 0., 3., 6.],
[ 0., 0., 0., 1.]]
Decompose the matrix.
>>> T, R, N, S, K = transform.decompose()
Since the input has no shear this component is the identity matrix.
Similarly, there are no reflections so its value is ``1``. All other components
are recovered perfectly and match the input.
>>> K # shear
array([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]])
>>> S # scale
array([1., 2., 3.])
>>> N # reflection
array(1.)
>>> R # rotation
array([[ 0., -1., 0.],
[ 1., 0., 0.],
[ 0., 0., 1.]])
>>> T # translation
array([4., 5., 6.])
Compose a shear component using pre-multiplication so that shearing is
the first transformation.
>>> shear = np.eye(4)
>>> shear[0, 1] = 0.1 # xy shear
>>> _ = transform.compose(shear, multiply_mode='pre')
Repeat the decomposition and show its components. Note how the decomposed shear
does not perfectly match the input shear matrix values. The values of the
scaling and rotation components are also affected and do not exactly match the
input. This is expected, because the shear can be partially factored as a
combination of rotation and scaling.
>>> T, R, N, S, K = transform.decompose()
>>> K # shear
array([[1. , 0.03333333, 0. ],
[0.01663894, 1. , 0. ],
[0. , 0. , 1. ]])
>>> S # scale
array([0.99944491, 2.0022213 , 3. ])
>>> N # reflection
array(1.)
>>> R # rotation
array([[ 0.03331483, -0.99944491, 0. ],
[ 0.99944491, 0.03331483, 0. ],
[ 0. , 0. , 1. ]])
>>> T # translation
array([4., 5., 6.])
Although the values may not match the input exactly, the decomposition is
nevertheless valid and can be used to re-compose the original transformation.
>>> T, R, N, S, K = transform.decompose(homogeneous=True)
>>> T @ R @ N @ S @ K
array([[-5.76153045e-17, -2.00000000e+00, 0.00000000e+00,
4.00000000e+00],
[ 1.00000000e+00, 1.00000000e-01, 0.00000000e+00,
5.00000000e+00],
[ 0.00000000e+00, 0.00000000e+00, 3.00000000e+00,
6.00000000e+00],
[ 0.00000000e+00, 0.00000000e+00, 0.00000000e+00,
1.00000000e+00]])
Alternatively, re-compose the transformation as a new
:class:`~pyvista.Transform` with pre-multiplication.
>>> recomposed = pv.Transform([T, R, N, S, K], multiply_mode='pre')
>>> np.allclose(recomposed.matrix, transform.matrix)
True
Compose a reflection and decompose the transform again.
>>> _ = transform.flip_x()
>>> T, R, N, S, K = transform.decompose()
The reflection component is now ``-1``.
>>> N # reflection
array(-1.)
The decomposition may be simplified to a ``TRSK`` decomposition by combining
the reflection component with either the rotation or the scaling term.
Multiplying the reflection with the rotation will make it a left-handed rotation
with negative determinant:
>>> R = R * N
>>> np.linalg.det(R) < 0
np.True_
Alternatively, keep the rotation right-handed but make the scaling factors negative:
>>> S = S * N
>>> S # scale
array([-0.99944491, -2.0022213 , -3. ])
"""
return decomposition(
self.matrix,
homogeneous=homogeneous,
)
def invert(self: Transform) -> Transform: # numpydoc ignore: RT01
"""Invert the current transformation.
The current transformation :attr:`matrix` (including all matrices in the
:attr:`matrix_list`) is inverted every time :meth:`invert` is called.
Use :attr:`is_inverted` to check if the transformations are currently inverted.
Examples
--------
Compose an arbitrary transformation.
>>> import pyvista as pv
>>> transform = pv.Transform().scale(2.0)
>>> transform.matrix
array([[2., 0., 0., 0.],
[0., 2., 0., 0.],
[0., 0., 2., 0.],
[0., 0., 0., 1.]])
Check if the transformation is inverted.
>>> transform.is_inverted
False
Invert the transformation and show the matrix.
>>> _ = transform.invert()
>>> transform.matrix
array([[0.5, 0. , 0. , 0. ],
[0. , 0.5, 0. , 0. ],
[0. , 0. , 0.5, 0. ],
[0. , 0. , 0. , 1. ]])
Check that the transformation is inverted.
>>> transform.is_inverted
True
Invert it again to restore it back to its original state.
>>> _ = transform.invert()
>>> transform.matrix
array([[2., 0., 0., 0.],
[0., 2., 0., 0.],
[0., 0., 2., 0.],
[0., 0., 0., 1.]])
>>> transform.is_inverted
False
"""
self.Inverse()
return self
def identity(self: Transform) -> Transform: # numpydoc ignore: RT01
"""Set the transformation to the identity transformation.
This can be used to "reset" the transform.
Examples
--------
Compose an arbitrary transformation.
>>> import pyvista as pv
>>> transform = pv.Transform().scale(2.0)
>>> transform.matrix
array([[2., 0., 0., 0.],
[0., 2., 0., 0.],
[0., 0., 2., 0.],
[0., 0., 0., 1.]])
Reset the transformation to the identity matrix.
>>> _ = transform.identity()
>>> transform.matrix
array([[1., 0., 0., 0.],
[0., 1., 0., 0.],
[0., 0., 1., 0.],
[0., 0., 0., 1.]])
"""
self.Identity()
return self
@property
def is_inverted(self: Transform) -> bool: # numpydoc ignore: RT01
"""Get the inverse flag of the transformation.
This flag is modified whenever :meth:`invert` is called.
"""
return bool(self.GetInverseFlag())
def _compose_with_translations(
self: Transform,
transform: TransformLike,
point: VectorLike[float] | None = None,
multiply_mode: Literal['pre', 'post'] | None = None,
) -> Transform:
translate_before, translate_after = self._get_point_translations(
point=point, multiply_mode=multiply_mode
)
if translate_before:
self.compose(translate_before, multiply_mode=multiply_mode)
self.compose(transform, multiply_mode=multiply_mode)
if translate_after:
self.compose(translate_after, multiply_mode=multiply_mode)
return self
def _get_point_translations(
self: Transform,
point: VectorLike[float] | None,
multiply_mode: Literal['pre', 'post'] | None,
) -> tuple[None | Transform, None | Transform]:
point = point if point is not None else self.point
if point is not None:
point_array = _validation.validate_array3(point, dtype_out=float, name='point')
translate_away = Transform().translate(-point_array)
translate_toward = Transform().translate(point_array)
if multiply_mode == 'post' or self._multiply_mode == 'post':
return translate_away, translate_toward
else:
return translate_toward, translate_away
return None, None
@property
def check_finite(self: Transform) -> bool: # numpydoc ignore: RT01
"""Check that the :attr:`matrix` and :attr:`inverse_matrix` have finite values.
If ``True``, all transformations are checked to ensure they only contain
finite values (i.e. no ``NaN`` or ``Inf`` values) and a ``ValueError`` is raised
otherwise. This is useful to catch cases where the transformation(s) are poorly
defined and/or are numerically unstable.
This flag is enabled by default.
"""
return self._check_finite
@check_finite.setter
def check_finite(self: Transform, value: bool) -> None:
self._check_finite = bool(value)
def as_rotation(
self,
representation: Literal['quat', 'matrix', 'rotvec', 'mrp', 'euler', 'davenport']
| None = None,
*args,
**kwargs,
) -> Rotation | NumpyArray[float]:
"""Return the rotation component as a SciPy ``Rotation`` or any of its representations.
The current :attr:`matrix` is first decomposed to extract the rotation component
and then returned with the specified representation.
.. note::
This method depends on the ``scipy`` package which must be installed to use it.
Parameters
----------
representation : str, optional
Representation of the rotation.
- ``'quat'``: Represent as a quaternion using
:meth:`~scipy.spatial.transform.Rotation.as_quat`. Returns a length-4 vector.
- ``'matrix'``: Represent as a 3x3 matrix using
:meth:`~scipy.spatial.transform.Rotation.as_matrix`.
- ``'rotvec'``: Represent as a rotation vector using
:meth:`~scipy.spatial.transform.Rotation.as_rotvec`.
- ``'mrp'``: Represent as a Modified Rodrigues Parameters (MRPs) vector using
:meth:`~scipy.spatial.transform.Rotation.as_mrp`.
- ``'euler'``: Represent as Euler angles using
:meth:`~scipy.spatial.transform.Rotation.as_euler`.
- ``'davenport'``: Represent as Davenport angles using
:meth:`~scipy.spatial.transform.Rotation.as_davenport`.
If no representation is given, then an instance of
:class:`scipy.spatial.transform.Rotation` is returned by default.
*args
Arguments passed to the ``Rotation`` method for the specified
representation.
**kwargs
Keyword arguments passed to the ``Rotation`` method for the specified
representation.
Returns
-------
scipy.spatial.transform.Rotation | np.ndarray
Rotation object or array depending on the representation.
See Also
--------
decompose
Alternative method for obtaining the rotation component (and others).
rotate, rotate_x, rotate_y, rotate_z, rotate_vector
Compose a rotation matrix.
Examples
--------
Create a rotation matrix and initialize a :class:`Transform` from it.
>>> import numpy as np
>>> import pyvista as pv
>>> matrix = [[0, -1, 0], [1, 0, 0], [0, 0, 1]]
>>> transform = pv.Transform(matrix)
Represent the rotation as :class:`scipy.spatial.transform.Rotation` instance.
>>> rot = transform.as_rotation()
>>> rot
Rotation.from_matrix(array([[ 0., -1., 0.],
[ 1., 0., 0.],
[ 0., 0., 1.]]))
Represent the rotation as a quaternion.
>>> rot = transform.as_rotation('quat')
>>> rot
array([0. , 0. , 0.70710678, 0.70710678])
Represent the rotation as a rotation vector. The vector has a direction
``(0, 0, 1)`` and magnitude of ``pi/2``.
>>> rot = transform.as_rotation('rotvec')
>>> rot
array([0. , 0. , 1.57079633])
Represent the rotation as a Modified Rodrigues Parameters vector.
>>> rot = transform.as_rotation('mrp')
>>> rot
array([0. , 0. , 0.41421356])
Represent the rotation as x-y-z Euler angles in degrees.
>>> rot = transform.as_rotation('euler', 'xyz', degrees=True)
>>> rot
array([ 0., 0., 90.])
Represent the rotation as extrinsic x-y-z Davenport angles in degrees.
>>> rot = transform.as_rotation(
... 'davenport', np.eye(3), 'extrinsic', degrees=True
... )
>>> rot
array([-1.27222187e-14, 0.00000000e+00, 9.00000000e+01])
"""
try:
from scipy.spatial.transform import Rotation # noqa: PLC0415
except ImportError:
msg = "The 'scipy' package must be installed to use `as_rotation`"
raise ImportError(msg)
if isinstance(representation, str):
representation = representation.lower() # type: ignore[assignment]
_validation.check_contains(
['quat', 'matrix', 'rotvec', 'mrp', 'euler', 'davenport', None],
must_contain=representation,
name='representation',
)
if representation in ['rotation', 'matrix']:
assert_empty_kwargs(**kwargs)
_, R, _, _, _ = self.decompose()
if representation == 'matrix':
out = R
else:
rotation = Rotation.from_matrix(R)
if representation is None:
out = rotation
elif representation == 'quat':
out = rotation.as_quat(*args, **kwargs)
elif representation == 'rotvec':
out = rotation.as_rotvec(*args, **kwargs)
elif representation == 'mrp':
out = rotation.as_mrp(*args, **kwargs)
elif representation == 'euler':
out = rotation.as_euler(*args, **kwargs)
elif representation == 'davenport':
out = rotation.as_davenport(*args, **kwargs)
else: # pragma: no cover
msg = f"Unexpected rotation type '{representation}'" # type: ignore[unreachable]
raise RuntimeError(msg)
return out
|