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
|
from __future__ import annotations
import importlib
from math import pi
import pathlib
from pathlib import Path
import re
from unittest.mock import patch
import warnings
import numpy as np
import pytest
import pyvista as pv
from pyvista import examples
from pyvista.core.errors import CellSizeError
from pyvista.core.errors import NotAllTrianglesError
from pyvista.core.errors import PyVistaDeprecationWarning
from pyvista.core.errors import PyVistaFutureWarning
radius = 0.5
@pytest.fixture
def sphere():
# this shadows the main sphere fixture from conftest!
return pv.Sphere(radius=radius, theta_resolution=10, phi_resolution=10)
@pytest.fixture
def sphere_shifted():
return pv.Sphere(center=[0.5, 0.5, 0.5], theta_resolution=10, phi_resolution=10)
@pytest.fixture
def sphere_dense():
return pv.Sphere(radius=radius, theta_resolution=100, phi_resolution=100)
@pytest.fixture
def cube_dense():
return pv.Cube()
test_path = str(Path(__file__).resolve().parent)
def is_binary(filename):
"""Return ``True`` when a file is binary."""
textchars = bytearray({7, 8, 9, 10, 12, 13, 27} | set(range(0x20, 0x100)) - {0x7F})
with Path(filename).open('rb') as f:
data = f.read(1024)
return bool(data.translate(None, textchars))
def test_init():
mesh = pv.PolyData()
assert not mesh.n_points
assert not mesh.n_cells
def test_init_from_pdata(sphere):
mesh = pv.PolyData(sphere, deep=True)
assert mesh.n_points
assert mesh.n_cells
mesh.points[0] += 1
assert not np.allclose(sphere.points[0], mesh.points[0])
@pytest.mark.parametrize('faces_is_cell_array', [False, True])
def test_init_from_arrays(faces_is_cell_array):
vertices = np.array([[0, 0, 0], [1, 0, 0], [1, 1, 0], [0, 1, 0], [0.5, 0.5, -1]])
# mesh faces
faces = np.hstack([[4, 0, 1, 2, 3], [3, 0, 1, 4], [3, 1, 2, 4]]).astype(np.int8)
mesh = pv.PolyData(vertices, pv.CellArray(faces) if faces_is_cell_array else faces)
assert mesh.n_points == 5
assert mesh.n_cells == 3
mesh = pv.PolyData(vertices, pv.CellArray(faces) if faces_is_cell_array else faces, deep=True)
vertices[0] += 1
assert not np.allclose(vertices[0], mesh.points[0])
# ensure that polydata raises a warning when inputting non-float dtype
with pytest.warns(Warning, match=r'Points is not a float type\. This can cause issues'):
mesh = pv.PolyData(vertices.astype(np.int32), faces)
# array must be immutable
with pytest.raises(ValueError): # noqa: PT011
mesh.faces[0] += 1
# attribute is mutable
faces = [4, 0, 1, 2, 3]
mesh.faces = pv.CellArray(faces) if faces_is_cell_array else faces
assert np.allclose(faces, mesh.faces)
@pytest.mark.parametrize('faces_is_cell_array', [False, True])
def test_init_from_arrays_with_vert(faces_is_cell_array):
vertices = np.array(
[
[0, 0, 0],
[1, 0, 0],
[1, 1, 0],
[0, 1, 0],
[0.5, 0.5, -1],
[0, 1.5, 1.5],
]
)
# mesh faces
faces = np.hstack(
[
[4, 0, 1, 2, 3],
[3, 0, 1, 4],
[3, 1, 2, 4],
[1, 5],
], # [quad, triangle, triangle, vertex]
).astype(np.int8)
if faces_is_cell_array:
faces = pv.CellArray(faces)
mesh = pv.PolyData(vertices, faces)
assert mesh.n_points == 6
assert mesh.n_cells == 4
@pytest.mark.parametrize('faces_is_cell_array', [False, True])
def test_init_from_arrays_triangular(faces_is_cell_array):
vertices = np.array([[0, 0, 0], [1, 0, 0], [1, 1, 0], [0, 1, 0], [0.5, 0.5, -1]])
# mesh faces
faces = np.vstack([[3, 0, 1, 2], [3, 0, 1, 4], [3, 1, 2, 4]])
if faces_is_cell_array:
faces = pv.CellArray(faces)
mesh = pv.PolyData(vertices, faces)
assert mesh.n_points == 5
assert mesh.n_cells == 3
mesh = pv.PolyData(vertices, faces, deep=True)
assert mesh.n_points == 5
assert mesh.n_cells == 3
def test_init_as_points():
vertices = np.array([[0, 0, 0], [1, 0, 0], [1, 1, 0], [0, 1, 0], [0.5, 0.5, -1]])
mesh = pv.PolyData(vertices)
assert mesh.n_points == vertices.shape[0]
assert mesh.n_cells == vertices.shape[0]
assert len(mesh.verts) == vertices.shape[0] * 2
vertices = np.array([[0, 0, 0], [1, 0, 0], [0, 1, 0]])
cells = np.array([1, 0, 1, 1, 1, 2], np.int16)
to_check = pv.PolyData._make_vertex_cells(len(vertices)).ravel()
assert np.allclose(to_check, cells)
# from list
mesh.verts = [[1, 0], [1, 1], [1, 2]]
to_check = pv.PolyData._make_vertex_cells(len(vertices)).ravel()
assert np.allclose(to_check, cells)
mesh = pv.PolyData()
mesh.points = vertices
mesh.verts = cells
assert mesh.n_points == vertices.shape[0]
assert mesh.n_cells == vertices.shape[0]
assert np.allclose(mesh.verts, cells)
def test_init_as_points_from_list():
points = [[0.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]]
mesh = pv.PolyData(points)
assert np.allclose(mesh.points, points)
def test_invalid_init():
with pytest.raises(ValueError): # noqa: PT011
pv.PolyData(np.array([1.0]))
with pytest.raises(TypeError):
pv.PolyData([1.0, 2.0, 3.0], 'woa')
with pytest.raises(ValueError): # noqa: PT011
pv.PolyData('woa', 'woa')
poly = pv.PolyData()
with pytest.raises(ValueError): # noqa: PT011
pv.PolyData(poly, 'woa')
with pytest.raises(TypeError):
pv.PolyData({'woa'})
def test_invalid_file():
with pytest.raises(FileNotFoundError):
pv.PolyData('file.bad')
filename = str(Path(test_path) / 'test_polydata.py')
with pytest.raises(IOError): # noqa: PT011
pv.PolyData(filename)
@pytest.mark.parametrize(
('arr', 'value'),
[
('faces', [3, 1, 2, 3, 3, 0, 1]),
('strips', np.array([5, 4, 3, 2, 0])),
('lines', [4, 0, 1, 2, 2, 3, 4]),
('verts', [1, 0, 1]),
('faces', [[3, 0, 1], [3, 2, 1], [4, 0, 1]]),
('faces', [[2, 0, 1], [2, 2, 1], [1, 0, 1]]),
],
)
def test_invalid_connectivity_arrays(arr, value):
generator = np.random.default_rng(seed=None)
points = generator.random((10, 3))
mesh = pv.PolyData(points)
with pytest.raises(CellSizeError, match='Cell array size is invalid'):
setattr(mesh, arr, value)
with pytest.raises(CellSizeError, match=f'`{arr}` cell array size is invalid'):
_ = pv.PolyData(points, **{arr: value})
@pytest.mark.parametrize('lines_is_cell_array', [False, True])
def test_lines_on_init(lines_is_cell_array):
points = np.random.default_rng().random((5, 3))
lines = [2, 0, 1, 3, 2, 3, 4]
pd = pv.PolyData(points, lines=pv.CellArray(lines) if lines_is_cell_array else lines)
assert not pd.faces.size
assert np.array_equal(pd.lines, lines)
assert np.array_equal(pd.points, points)
def _assert_verts_equal(
mesh: pv.PolyData,
verts: list[int],
n_verts: int,
cell_types: dict[int, pv.CellType],
):
assert np.array_equal(mesh.verts, verts)
assert mesh.n_verts == n_verts
for i, expected_typ in cell_types.items():
assert mesh.get_cell(i).type == expected_typ
@pytest.mark.parametrize('verts_is_cell_array', [False, True])
def test_verts(verts_is_cell_array):
vertices = np.array([[0, 0, 0], [1, 0, 0], [1, 1, 0], [0, 1, 0], [0.5, 0.5, -1]])
verts = [1, 0, 1, 1, 1, 2, 1, 3, 1, 4]
if not verts_is_cell_array:
mesh = pv.PolyData(vertices)
_assert_verts_equal(mesh, verts, n_verts=5, cell_types={0: pv.CellType.VERTEX})
mesh = pv.PolyData(vertices, verts=pv.CellArray(verts) if verts_is_cell_array else verts)
_assert_verts_equal(mesh, verts, n_verts=5, cell_types={0: pv.CellType.VERTEX})
verts = [1, 0]
mesh = pv.PolyData(vertices, verts=pv.CellArray(verts) if verts_is_cell_array else verts)
_assert_verts_equal(mesh, verts, n_verts=1, cell_types={0: pv.CellType.VERTEX})
verts = [2, 0, 1, 1, 2]
mesh = pv.PolyData(vertices, verts=pv.CellArray(verts) if verts_is_cell_array else verts)
_assert_verts_equal(
mesh,
verts,
n_verts=2,
cell_types={0: pv.CellType.POLY_VERTEX, 1: pv.CellType.VERTEX},
)
@pytest.mark.parametrize('verts', [([1, 0]), (pv.CellArray([1, 0]))])
@pytest.mark.parametrize('lines', [([2, 1, 2]), (pv.CellArray([2, 1, 2]))])
@pytest.mark.parametrize('faces', [([3, 3, 4, 5]), (pv.CellArray([3, 3, 4, 5]))])
@pytest.mark.parametrize('strips', [([4, 6, 7, 8, 9]), (pv.CellArray([4, 6, 7, 8, 9]))])
def test_mixed_cell_polydata(verts, lines, faces, strips):
points = np.zeros((10, 3))
points[:, 0] = np.linspace(0, 9, 10)
a = pv.PolyData(points, verts=verts, lines=lines, faces=faces, strips=strips)
assert np.array_equal(a.verts, [1, 0])
assert np.array_equal(a.lines, [2, 1, 2])
assert np.array_equal(a.faces, [3, 3, 4, 5])
assert np.array_equal(a.strips, [4, 6, 7, 8, 9])
def test_polydata_repr_str():
pd = pv.PolyData()
assert repr(pd) == str(pd)
assert 'N Cells' in str(pd)
assert 'N Points' in str(pd)
assert 'X Bounds' in str(pd)
assert 'N Arrays' in str(pd)
def test_geodesic(sphere):
start, end = 0, sphere.n_points - 1
geodesic = sphere.geodesic(start, end)
assert isinstance(geodesic, pv.PolyData)
assert 'vtkOriginalPointIds' in geodesic.array_names
ids = geodesic.point_data['vtkOriginalPointIds']
assert np.allclose(geodesic.points, sphere.points[ids])
# check keep_order
geodesic_legacy = sphere.geodesic(start, end, keep_order=False)
assert geodesic_legacy['vtkOriginalPointIds'][0] == end
geodesic_ordered = sphere.geodesic(start, end, keep_order=True)
assert geodesic_ordered['vtkOriginalPointIds'][0] == start
# finally, inplace
geodesic_inplace = sphere.geodesic(start, end, inplace=True)
assert geodesic_inplace is sphere
assert np.allclose(geodesic.points, sphere.points)
def test_geodesic_fail(sphere, plane):
with pytest.raises(IndexError):
sphere.geodesic(-1, -1)
with pytest.raises(IndexError):
sphere.geodesic(sphere.n_points, 0)
with pytest.raises(NotAllTrianglesError):
plane.geodesic(0, 10)
def test_geodesic_distance(sphere):
distance = sphere.geodesic_distance(0, sphere.n_points - 1)
assert isinstance(distance, float)
# Use scalar weights
distance_use_scalar_weights = sphere.geodesic_distance(
0,
sphere.n_points - 1,
use_scalar_weights=True,
)
assert isinstance(distance_use_scalar_weights, float)
def test_ray_trace(sphere):
points, ind = sphere.ray_trace([0, 0, 0], [1, 1, 1])
assert np.any(points)
assert np.any(ind)
def test_ray_trace_origin():
# https://github.com/pyvista/pyvista/issues/5372
plane = pv.Plane(i_resolution=1, j_resolution=1)
pts, cells = plane.ray_trace([0, 0, 1], [0, 0, -1])
assert len(cells) == 1
assert cells[0] == 0
def test_multi_ray_trace(sphere):
trimesh = pytest.importorskip('trimesh')
if not trimesh.ray.has_embree:
pytest.skip('Requires Embree')
origins = [[1, 0, 1], [0.5, 0, 1], [0.25, 0, 1], [0, 0, 5]]
directions = [[0, 0, -1]] * 4
points, ind_r, ind_t = sphere.multi_ray_trace(origins, directions)
assert np.any(points)
assert np.any(ind_r)
assert np.any(ind_t)
# patch embree to test retry
with patch.object(
trimesh.ray.ray_pyembree.RayMeshIntersector,
'intersects_location',
return_value=[np.array([])] * 3,
):
points, ind_r, ind_t = sphere.multi_ray_trace(origins, directions, retry=True)
known_points = np.array(
[[0.25, 0, 0.42424145], [0.25, 0, -0.42424145], [0, 0, 0.5], [0, 0, -0.5]],
)
known_ind_r = np.array([2, 2, 3, 3])
np.testing.assert_allclose(points, known_points)
np.testing.assert_allclose(ind_r, known_ind_r)
assert len(ind_t) == 4
# check non-triangulated
mesh = pv.Cylinder()
with pytest.raises(NotAllTrianglesError):
mesh.multi_ray_trace(origins, directions)
def test_edge_mask(sphere):
_ = sphere.edge_mask(10, progress_bar=True)
def test_boolean_union_intersection(sphere, sphere_shifted):
union = sphere.boolean_union(sphere_shifted, progress_bar=True)
intersection = sphere.boolean_intersection(sphere_shifted, progress_bar=True)
# union is volume of sphere + sphere_shifted minus the part intersecting
expected_volume = sphere.volume + sphere_shifted.volume - intersection.volume
assert np.isclose(union.volume, expected_volume, atol=1e-3)
# intersection volume is the volume of both isolated meshes minus the union
expected_volume = sphere.volume + sphere_shifted.volume - union.volume
assert np.isclose(intersection.volume, expected_volume, atol=1e-3)
def test_bitwise_and_or(sphere, sphere_shifted):
union = sphere | sphere_shifted
intersection = sphere & sphere_shifted
# union is volume of sphere + sphere_shifted minus the part intersecting
expected_volume = sphere.volume + sphere_shifted.volume - intersection.volume
assert np.isclose(union.volume, expected_volume, atol=1e-3)
# intersection volume is the volume of both isolated meshes minus the union
expected_volume = sphere.volume + sphere_shifted.volume - union.volume
assert np.isclose(intersection.volume, expected_volume, atol=1e-3)
def test_boolean_difference(sphere, sphere_shifted):
difference = sphere.boolean_difference(sphere_shifted, progress_bar=True)
intersection = sphere.boolean_intersection(sphere_shifted, progress_bar=True)
expected_volume = sphere.volume - intersection.volume
assert np.isclose(difference.volume, expected_volume, atol=1e-3)
def test_boolean_difference_fail(plane, sphere):
with pytest.raises(NotAllTrianglesError):
plane - sphere
def test_subtract(sphere, sphere_shifted):
sub_mesh = sphere - sphere_shifted
assert sub_mesh.n_points == sphere.boolean_difference(sphere_shifted).n_points
def test_isubtract(sphere, sphere_shifted):
sub_mesh = sphere.copy()
sub_mesh -= sphere_shifted
assert sub_mesh.n_points == sphere.boolean_difference(sphere_shifted).n_points
def test_append(
sphere: pv.PolyData,
sphere_shifted: pv.PolyData,
sphere_dense: pv.PolyData,
):
# 1/ Single argument
merged = sphere.append_polydata(sphere_shifted)
assert merged.n_points == (sphere.n_points + sphere_shifted.n_points)
assert isinstance(merged, pv.PolyData)
# test point order is kept
np.testing.assert_array_equal(merged.points[: sphere.n_points], sphere.points)
np.testing.assert_array_equal(merged.points[sphere.n_points :], sphere_shifted.points)
# 2/ Multiple arguments
merged = sphere.append_polydata(sphere_shifted, sphere_dense)
assert merged.n_points == (sphere.n_points + sphere_shifted.n_points + sphere_dense.n_points)
assert isinstance(merged, pv.PolyData)
# test point order is kept
np.testing.assert_array_equal(merged.points[: sphere.n_points], sphere.points)
mid = sphere.n_points + sphere_shifted.n_points
np.testing.assert_array_equal(merged.points[sphere.n_points : mid], sphere_shifted.points)
np.testing.assert_array_equal(merged.points[mid:], sphere_dense.points)
# 3/ test in-place merge
mesh = sphere.copy()
merged = mesh.append_polydata(sphere_shifted, inplace=True)
assert merged is mesh
def test_append_raises(sphere: pv.PolyData):
with pytest.raises(TypeError, match='All meshes need to be of PolyData type'):
sphere.append_polydata(sphere.cast_to_unstructured_grid())
def test_merge(sphere, sphere_shifted, hexbeam):
merged = sphere.merge(hexbeam, merge_points=False, progress_bar=True)
assert merged.n_points == (sphere.n_points + hexbeam.n_points)
assert isinstance(merged, pv.UnstructuredGrid)
assert merged.active_scalars_name is None
# list with unstructuredgrid case
merged = sphere.merge([hexbeam, hexbeam], merge_points=False, progress_bar=True)
assert merged.n_points == (sphere.n_points + hexbeam.n_points * 2)
assert isinstance(merged, pv.UnstructuredGrid)
assert merged.active_scalars_name is None
# with polydata
merged = sphere.merge(sphere_shifted, progress_bar=True)
assert isinstance(merged, pv.PolyData)
assert merged.n_points == sphere.n_points + sphere_shifted.n_points
assert merged.active_scalars_name is None
# with polydata list (no merge)
merged = sphere.merge([sphere_shifted, sphere_shifted], merge_points=False, progress_bar=True)
assert isinstance(merged, pv.PolyData)
assert merged.n_points == sphere.n_points + sphere_shifted.n_points * 2
assert merged.active_scalars_name is None
# with polydata list (merge)
merged = sphere.merge([sphere_shifted, sphere_shifted], progress_bar=True)
assert isinstance(merged, pv.PolyData)
assert merged.n_points == sphere.n_points + sphere_shifted.n_points
assert merged.active_scalars_name is None
# test in-place merge
mesh = sphere.copy()
merged = mesh.merge(sphere_shifted, inplace=True)
assert merged is mesh
assert merged.active_scalars_name is None
# test merge with lines
arc_1 = pv.CircularArc(
pointa=[0, 0, 0], pointb=[10, 10, 0], center=[10, 0, 0], negative=False, resolution=3
)
arc_2 = pv.CircularArc(
pointa=[10, 10, 0], pointb=[20, 0, 0], center=[10, 0, 0], negative=False, resolution=3
)
merged = arc_1 + arc_2
assert merged.n_lines == 2
assert merged.active_scalars_name == 'Distance'
# test merge with lines as iterable
merged = arc_1.merge((arc_2, arc_2))
assert merged.n_lines == 3
assert merged.active_scalars_name == 'Distance'
@pytest.mark.parametrize('input_', [examples.load_hexbeam(), pv.Sphere()])
def test_merge_active_scalars(input_):
mesh1 = input_.copy()
mesh1['foo'] = np.arange(mesh1.n_points)
mesh2 = mesh1.copy()
a = mesh1.copy()
b = mesh2.copy()
a.active_scalars_name = None
b.active_scalars_name = None
merged = a.merge(b)
assert merged.active_scalars_name is None
merged = b.merge(a)
assert merged.active_scalars_name is None
a = mesh1.copy()
b = mesh2.copy()
a.active_scalars_name = 'foo'
b.active_scalars_name = None
merged = a.merge(b)
assert merged.active_scalars_name is None
merged = b.merge(a)
assert merged.active_scalars_name is None
a = mesh1.copy()
b = mesh2.copy()
a.active_scalars_name = None
b.active_scalars_name = 'foo'
merged = a.merge(b)
assert merged.active_scalars_name is None
merged = b.merge(a)
assert merged.active_scalars_name is None
a = mesh1.copy()
b = mesh2.copy()
a.active_scalars_name = 'foo'
b.active_scalars_name = 'foo'
merged = a.merge(b)
assert merged.active_scalars_name == 'foo'
merged = b.merge(a)
assert merged.active_scalars_name == 'foo'
@pytest.mark.parametrize(
'input_', [examples.load_hexbeam(), pv.Plane(i_resolution=1, j_resolution=1)]
)
@pytest.mark.parametrize('main_has_priority', [True, False])
def test_merge_main_has_priority(input_, main_has_priority):
mesh = input_.copy()
data_main = np.arange(mesh.n_points, dtype=float)
mesh.point_data['present_in_both'] = data_main
mesh.set_active_scalars('present_in_both')
other = mesh.copy()
data_other = -data_main
other.point_data['present_in_both'] = data_other
other.set_active_scalars('present_in_both')
# note: order of points can change after point merging
def matching_point_data(this, that, scalars_name):
"""Return True if scalars on two meshes only differ by point order."""
return all(
new_val == this.point_data[scalars_name][j]
for point, new_val in zip(that.points, that.point_data[scalars_name])
for j in (this.points == point).all(-1).nonzero()
)
if pv.vtk_version_info >= (9, 5, 0):
merged = mesh.merge(other)
expected_to_match = mesh
else:
with pytest.warns(
pv.PyVistaDeprecationWarning,
match="The keyword 'main_has_priority' is deprecated and should not be used",
):
merged = mesh.merge(other, main_has_priority=main_has_priority)
expected_to_match = mesh if main_has_priority else other
assert matching_point_data(merged, expected_to_match, 'present_in_both')
assert merged.active_scalars_name == 'present_in_both'
@pytest.mark.parametrize('main_has_priority', [True, False])
def test_merge_main_has_priority_deprecated(sphere, main_has_priority):
match = (
"The keyword 'main_has_priority' is deprecated and should not be used.\n"
'The main mesh will always have priority in a future version.'
)
if main_has_priority is False and pv.vtk_version_info >= (9, 5, 0):
with pytest.raises(ValueError, match=match):
sphere.merge(sphere, main_has_priority=main_has_priority)
else:
with pytest.warns(pv.PyVistaDeprecationWarning, match=match):
sphere.merge(sphere, main_has_priority=main_has_priority)
@pytest.mark.parametrize('main_has_priority', [True, False])
@pytest.mark.parametrize('mesh', [pv.UnstructuredGrid(), pv.PolyData()])
def test_merge_field_data(mesh, main_has_priority):
key = 'data'
data_main = [1, 2, 3]
data_other = [4, 5, 6]
mesh.field_data[key] = data_main
other = mesh.copy()
other.field_data[key] = data_other
match = (
"The keyword 'main_has_priority' is deprecated and should not be used.\n"
'The main mesh will always have priority in a future version, and this '
'keyword will be removed.'
)
if main_has_priority is False and pv.vtk_version_info >= (9, 5, 0):
match += '\nIts value cannot be False for vtk>=9.5.0.'
with pytest.raises(ValueError, match=re.escape(match)):
mesh.merge(other, main_has_priority=main_has_priority)
return
else:
with pytest.warns(pv.PyVistaDeprecationWarning, match=match):
merged = mesh.merge(other, main_has_priority=main_has_priority)
actual = merged.field_data[key]
expected = data_main if main_has_priority else data_other
assert np.array_equal(actual, expected)
def test_add(sphere, sphere_shifted):
merged = sphere + sphere_shifted
assert isinstance(merged, pv.PolyData)
assert merged.n_points == sphere.n_points + sphere_shifted.n_points
assert merged.n_faces_strict == sphere.n_faces_strict + sphere_shifted.n_faces_strict
def test_intersection(sphere, sphere_shifted):
intersection, first, second = sphere.intersection(
sphere_shifted,
split_first=True,
split_second=True,
progress_bar=True,
)
assert intersection.n_points
assert first.n_points > sphere.n_points
assert second.n_points > sphere_shifted.n_points
intersection, first, second = sphere.intersection(
sphere_shifted,
split_first=False,
split_second=False,
progress_bar=True,
)
assert intersection.n_points
assert first.n_points == sphere.n_points
assert second.n_points == sphere_shifted.n_points
@pytest.mark.parametrize('curv_type', ['mean', 'gaussian', 'maximum', 'minimum'])
def test_curvature(sphere, curv_type):
curv = sphere.curvature(curv_type)
assert np.any(curv)
assert curv.size == sphere.n_points
def test_invalid_curvature(sphere):
with pytest.raises(ValueError): # noqa: PT011
sphere.curvature('not valid')
@pytest.mark.parametrize('binary', [True, False])
@pytest.mark.parametrize('extension', pv.core.pointset.PolyData._WRITERS)
def test_save(sphere, extension, binary, tmpdir):
filename = str(tmpdir.mkdir('tmpdir').join(f'tmp{extension}'))
if extension == '.vtkhdf' and not binary:
with pytest.raises(ValueError, match='.vtkhdf files can only be written in binary format'):
sphere.save(filename, binary=binary)
return
sphere.save(filename, binary=binary)
if binary:
if extension == '.vtp':
with Path(filename).open() as f:
assert 'binary' in f.read(1000)
else:
is_binary(filename)
else:
with Path(filename).open() as f:
fst = f.read(100).lower()
assert (
'ascii' in fst
or 'xml' in fst
or 'solid' in fst
or 'pgeometry' in fst
or '# generated' in fst
or '#inventor' in fst
)
if extension not in ('.geo', '.iv'):
mesh = pv.PolyData(filename)
assert mesh.faces.shape == sphere.faces.shape
assert mesh.points.shape == sphere.points.shape
def test_pathlib_read_write(tmpdir, sphere):
path = pathlib.Path(str(tmpdir.mkdir('tmpdir').join('tmp.vtk')))
sphere.save(path)
assert path.is_file()
mesh = pv.PolyData(path)
assert mesh.faces.shape == sphere.faces.shape
assert mesh.points.shape == sphere.points.shape
mesh = pv.read(path)
assert isinstance(mesh, pv.PolyData)
assert mesh.faces.shape == sphere.faces.shape
assert mesh.points.shape == sphere.points.shape
def test_invalid_save(sphere):
with pytest.raises(ValueError): # noqa: PT011
sphere.save('file.abc')
def test_triangulate_filter(plane):
assert not plane.is_all_triangles
plane.triangulate(inplace=True)
assert plane.is_all_triangles
# Make a point cloud and assert false
assert not pv.PolyData(plane.points).is_all_triangles
# Extract lines and make sure false
assert not plane.extract_all_edges().is_all_triangles
@pytest.mark.parametrize('pass_lines', [True, False])
def test_triangulate_filter_pass_lines(sphere: pv.PolyData, plane: pv.PolyData, pass_lines: bool):
merge: pv.PolyData = plane + (lines := sphere.extract_all_edges())
tri: pv.PolyData = merge.triangulate(pass_lines=pass_lines, inplace=False)
assert tri.n_lines == (lines.n_cells if pass_lines else 0)
assert tri.is_all_triangles if not pass_lines else (not tri.is_all_triangles)
@pytest.mark.parametrize('pass_verts', [True, False])
def test_triangulate_filter_pass_verts(plane: pv.PolyData, pass_verts: bool):
merge: pv.PolyData = plane + (verts := pv.PolyData([0.0, 1.0, 2.0]))
tri: pv.PolyData = merge.triangulate(pass_verts=pass_verts, inplace=False)
assert tri.n_verts == (verts.n_cells if pass_verts else 0)
assert tri.is_all_triangles if not pass_verts else (not tri.is_all_triangles)
@pytest.mark.parametrize('subfilter', ['butterfly', 'loop', 'linear'])
def test_subdivision(sphere, subfilter):
mesh = sphere.subdivide(1, subfilter, progress_bar=True)
assert mesh.n_points > sphere.n_points
assert mesh.n_faces_strict > sphere.n_faces_strict
mesh = sphere.copy()
mesh.subdivide(1, subfilter, inplace=True)
assert mesh.n_points > sphere.n_points
assert mesh.n_faces_strict > sphere.n_faces_strict
def test_invalid_subdivision(sphere):
with pytest.raises(ValueError): # noqa: PT011
sphere.subdivide(1, 'not valid')
# check non-triangulated
mesh = pv.Cylinder()
with pytest.raises(NotAllTrianglesError):
mesh.subdivide(1)
def test_extract_feature_edges(sphere):
# Test extraction of NO edges
edges = sphere.extract_feature_edges(90)
assert not edges.n_points
mesh = pv.Cube() # use a mesh that actually has strongly defined edges
more_edges = mesh.extract_feature_edges(10)
assert more_edges.n_points
def test_extract_feature_edges_no_data():
mesh = pv.Wavelet()
edges = mesh.extract_feature_edges(90, clear_data=True)
assert edges is not None
assert isinstance(edges, pv.PolyData)
assert edges.n_arrays == 0
def test_decimate(sphere):
mesh = sphere.decimate(0.5, progress_bar=True)
assert mesh.n_points < sphere.n_points
assert mesh.n_faces_strict < sphere.n_faces_strict
mesh.decimate(0.5, inplace=True, progress_bar=True)
assert mesh.n_points < sphere.n_points
assert mesh.n_faces_strict < sphere.n_faces_strict
# check non-triangulated
mesh = pv.Cylinder()
with pytest.raises(NotAllTrianglesError):
mesh.decimate(0.5)
def test_decimate_pro(sphere):
mesh = sphere.decimate_pro(0.5, progress_bar=True, max_degree=10)
assert mesh.n_points < sphere.n_points
assert mesh.n_faces_strict < sphere.n_faces_strict
mesh.decimate_pro(0.5, inplace=True, progress_bar=True)
assert mesh.n_points < sphere.n_points
assert mesh.n_faces_strict < sphere.n_faces_strict
# check non-triangulated
mesh = pv.Cylinder()
with pytest.raises(NotAllTrianglesError):
mesh.decimate_pro(0.5)
def test_compute_normals(sphere):
sphere_normals = sphere
sphere_normals.compute_normals(inplace=True)
point_normals = sphere_normals.point_data['Normals']
cell_normals = sphere_normals.cell_data['Normals']
assert point_normals.shape[0] == sphere.n_points
assert cell_normals.shape[0] == sphere.n_cells
def test_compute_normals_raises(sphere):
msg = (
'Normals cannot be computed for PolyData containing only vertex cells (e.g. point clouds)'
'\nand/or line cells. The PolyData cells must be polygons (e.g. triangle cells).'
)
point_cloud = pv.PolyData(sphere.points)
assert point_cloud.n_verts == point_cloud.n_cells
with pytest.raises(TypeError, match=re.escape(msg)):
point_cloud.compute_normals()
lines = pv.MultipleLines()
assert lines.n_lines == lines.n_cells
with pytest.raises(TypeError, match=re.escape(msg)):
lines.compute_normals()
def test_compute_normals_inplace(sphere):
sphere.point_data['numbers'] = np.arange(sphere.n_points)
sphere2 = sphere.copy(deep=False)
sphere['numbers'] *= -1 # sphere2 'numbers' are also modified
assert np.array_equal(sphere['numbers'], sphere2['numbers'])
assert np.shares_memory(sphere['numbers'], sphere2['numbers'])
sphere.compute_normals(inplace=True)
sphere[
'numbers'
] *= -1 # sphere2 'numbers' are also modified after adding to Plotter. (See issue #2461)
assert np.array_equal(sphere['numbers'], sphere2['numbers'])
assert np.shares_memory(sphere['numbers'], sphere2['numbers'])
def test_compute_normals_split_vertices(cube):
# verify edge splitting occurs and point IDs are tracked
cube_split_norm = cube.compute_normals(split_vertices=True)
assert cube_split_norm.n_points == 24
assert 'pyvistaOriginalPointIds' in cube_split_norm.point_data
assert len(set(cube_split_norm.point_data['pyvistaOriginalPointIds'])) == 8
@pytest.fixture
def ant_with_normals(ant):
ant['Scalars'] = range(ant.n_points)
point_normals = [[0, 0, 1]] * ant.n_points
ant.point_data['PointNormals'] = point_normals
ant.point_data.active_normals_name = 'PointNormals'
cell_normals = [[1, 0, 0]] * ant.n_cells
ant.cell_data['CellNormals'] = cell_normals
ant.cell_data.active_normals_name = 'CellNormals'
return ant
def test_point_normals_returns_active_normals(ant_with_normals):
ant = ant_with_normals
expected_point_normals = ant['PointNormals']
actual_point_normals = ant.point_normals
assert actual_point_normals.shape[0] == ant.n_points
assert np.array_equal(actual_point_normals, ant.point_data.active_normals)
assert np.shares_memory(actual_point_normals, ant.point_data.active_normals)
assert np.array_equal(actual_point_normals, expected_point_normals)
def test_point_normals_computes_new_normals(ant):
expected_point_normals = ant.copy().compute_normals().point_data['Normals']
ant.point_data.clear()
assert ant.array_names == []
assert ant.point_data.active_normals is None
actual_point_normals = ant.point_normals
assert actual_point_normals.shape[0] == ant.n_points
assert np.array_equal(actual_point_normals, expected_point_normals)
def test_cell_normals_returns_active_normals(ant_with_normals):
ant = ant_with_normals
expected_cell_normals = ant['CellNormals']
actual_cell_normals = ant.cell_normals
assert actual_cell_normals.shape[0] == ant.n_cells
assert np.array_equal(actual_cell_normals, ant.cell_data.active_normals)
assert np.shares_memory(actual_cell_normals, ant.cell_data.active_normals)
assert np.array_equal(actual_cell_normals, expected_cell_normals)
def test_cell_normals_computes_new_normals(ant):
expected_cell_normals = ant.copy().compute_normals().cell_data['Normals']
ant.cell_data.clear()
assert ant.array_names == []
assert ant.cell_data.active_normals is None
actual_cell_normals = ant.cell_normals
assert actual_cell_normals.shape[0] == ant.n_cells
assert np.array_equal(actual_cell_normals, expected_cell_normals)
def test_face_normals(sphere):
assert sphere.face_normals.shape[0] == sphere.n_faces_strict
def test_clip_plane(sphere):
clipped_sphere = sphere.clip(
origin=[0, 0, 0],
normal=[0, 0, -1],
invert=False,
progress_bar=True,
)
faces = clipped_sphere.faces.reshape(-1, 4)[:, 1:]
assert np.all(clipped_sphere.points[faces, 2] <= 0)
sphere.clip(
origin=[0, 0, 0],
normal=[0, 0, -1],
inplace=True,
invert=False,
progress_bar=True,
)
faces = clipped_sphere.faces.reshape(-1, 4)[:, 1:]
assert np.all(clipped_sphere.points[faces, 2] <= 0)
def test_extract_largest(sphere):
mesh = sphere + pv.Sphere(radius=0.1, theta_resolution=5, phi_resolution=5)
largest = mesh.extract_largest()
assert largest.n_faces_strict == sphere.n_faces_strict
mesh.extract_largest(inplace=True)
assert mesh.n_faces_strict == sphere.n_faces_strict
def test_clean(sphere):
mesh = sphere.merge(sphere, merge_points=False).extract_surface()
assert mesh.n_points > sphere.n_points
cleaned = mesh.clean(merge_tol=1e-5)
assert cleaned.n_points == sphere.n_points
mesh.clean(merge_tol=1e-5, inplace=True)
assert mesh.n_points == sphere.n_points
cleaned = mesh.clean(point_merging=False)
assert cleaned.n_points == mesh.n_points
# test with points but no cells
mesh = pv.PolyData()
mesh.points = (0, 0, 0)
cleaned = mesh.clean()
assert cleaned.n_points == 0
def test_area(sphere_dense, cube_dense):
radius = 0.5
ideal_area = 4 * pi * radius**2
assert np.isclose(sphere_dense.area, ideal_area, rtol=1e-3)
ideal_area = 6 * np.cbrt(cube_dense.volume) ** 2
assert np.isclose(cube_dense.area, ideal_area, rtol=1e-3)
def test_volume(sphere_dense):
ideal_volume = (4 / 3.0) * pi * radius**3
assert np.isclose(sphere_dense.volume, ideal_volume, rtol=1e-3)
def test_remove_points_any(sphere):
remove_mask = np.zeros(sphere.n_points, np.bool_)
remove_mask[:3] = True
sphere_mod, ind = sphere.remove_points(remove_mask, inplace=False, mode='any')
assert (sphere_mod.n_points + remove_mask.sum()) == sphere.n_points
assert np.allclose(sphere_mod.points, sphere.points[ind])
def test_remove_points_all(sphere):
sphere_copy = sphere.copy()
sphere_copy.cell_data['ind'] = np.arange(sphere_copy.n_faces_strict)
remove = sphere.faces[1:4]
sphere_copy.remove_points(remove, inplace=True, mode='all')
assert sphere_copy.n_points == sphere.n_points
assert sphere_copy.n_faces_strict == sphere.n_faces_strict - 1
def test_remove_points_fail(sphere, plane):
# not triangles:
with pytest.raises(NotAllTrianglesError):
plane.remove_points([0])
# invalid bool mask size
with pytest.raises(ValueError): # noqa: PT011
sphere.remove_points(np.ones(10, np.bool_))
# invalid mask type
with pytest.raises(TypeError):
sphere.remove_points([0.0])
def test_vertice_cells_on_read(tmpdir):
point_cloud = pv.PolyData(np.random.default_rng().random((100, 3)))
filename = str(tmpdir.mkdir('tmpdir').join('foo.ply'))
point_cloud.save(filename)
recovered = pv.read(filename)
assert recovered.n_cells == 100
recovered = pv.PolyData(filename)
assert recovered.n_cells == 100
def test_center_of_mass(sphere):
assert np.allclose(sphere.center_of_mass(), [0, 0, 0])
cloud = pv.PolyData(np.random.default_rng().random((100, 3)))
assert len(cloud.center_of_mass()) == 3
cloud['weights'] = np.random.default_rng().random(cloud.n_points)
center = cloud.center_of_mass(scalars_weight=True)
assert len(center) == 3
def test_project_points_to_plane():
# Define a simple Gaussian surface
n = 20
x = np.linspace(-200, 200, num=n) + np.random.default_rng().uniform(-5, 5, size=n)
y = np.linspace(-200, 200, num=n) + np.random.default_rng().uniform(-5, 5, size=n)
xx, yy = np.meshgrid(x, y)
A, b = 100, 100
zz = A * np.exp(-0.5 * ((xx / b) ** 2.0 + (yy / b) ** 2.0))
poly = pv.StructuredGrid(xx, yy, zz).extract_geometry(progress_bar=True)
poly['elev'] = zz.ravel(order='f')
# Wrong normal length
with pytest.raises(TypeError):
poly.project_points_to_plane(normal=(0, 0, 1, 1))
# allow Sequence but not Iterable
with pytest.raises(TypeError):
poly.project_points_to_plane(normal={0, 1, 2})
# Test the filter
projected = poly.project_points_to_plane(origin=poly.center, normal=(0, 0, 1))
assert np.allclose(projected.points[:, -1], poly.center[-1])
projected = poly.project_points_to_plane(normal=(0, 1, 1))
assert projected.n_points
# finally, test inplace
poly.project_points_to_plane(normal=(0, 1, 1), inplace=True)
assert np.allclose(poly.points, projected.points)
def test_tube(spline):
# Simple
line = pv.Line()
tube = line.tube(n_sides=2, progress_bar=True)
assert tube.n_points
assert tube.n_cells
# inplace
line.tube(n_sides=2, inplace=True, progress_bar=True)
assert np.allclose(line.points, tube.points)
# Complicated
tube = spline.tube(radius=0.5, scalars='arc_length', progress_bar=True)
assert tube.n_points
assert tube.n_cells
# Complicated with absolute radius
tube = spline.tube(radius=0.5, scalars='arc_length', absolute=True, progress_bar=True)
assert tube.n_points
assert tube.n_cells
with pytest.raises(TypeError):
spline.tube(scalars=range(10))
def test_smooth_inplace(sphere):
orig_pts = sphere.points.copy()
sphere.smooth(inplace=True, progress_bar=True)
assert not np.allclose(orig_pts, sphere.points)
def test_delaunay_2d():
n = 20
x = np.linspace(-200, 200, num=n) + np.random.default_rng().uniform(-5, 5, size=n)
y = np.linspace(-200, 200, num=n) + np.random.default_rng().uniform(-5, 5, size=n)
xx, yy = np.meshgrid(x, y)
A, b = 100, 100
zz = A * np.exp(-0.5 * ((xx / b) ** 2.0 + (yy / b) ** 2.0))
# Get the points as a 2D NumPy array (N by 3)
points = np.c_[xx.reshape(-1), yy.reshape(-1), zz.reshape(-1)]
pdata = pv.PolyData(points)
surf = pdata.delaunay_2d(progress_bar=True)
# Make sure we have an all triangle mesh now
assert np.all(surf.faces.reshape((-1, 4))[:, 0] == 3)
# test inplace
pdata.delaunay_2d(inplace=True, progress_bar=True)
assert np.allclose(pdata.points, surf.points)
def test_lines():
theta = np.linspace(-4 * np.pi, 4 * np.pi, 100)
z = np.linspace(-2, 2, 100)
r = z**2 + 1
x = r * np.sin(theta)
y = r * np.cos(theta)
points = np.column_stack((x, y, z))
# Create line segments
poly = pv.PolyData()
poly.points = points
cells = np.full((len(points) - 1, 3), 2, dtype=np.int_)
cells[:, 1] = np.arange(0, len(points) - 1, dtype=np.int_)
cells[:, 2] = np.arange(1, len(points), dtype=np.int_)
poly.lines = cells
assert poly.n_points == len(points)
assert poly.n_cells == len(points) - 1
# Create a poly line
poly = pv.PolyData()
poly.points = points
the_cell = np.arange(0, len(points), dtype=np.int_)
the_cell = np.insert(the_cell, 0, len(points))
poly.lines = the_cell
assert poly.n_points == len(points)
assert poly.n_cells == 1
def test_strips():
# init with strips test
vertices = np.array([[0, 0, 0], [1, 0, 0], [1, 0.5, 0], [0, 0.5, 0]])
strips = np.array([4, 0, 1, 3, 2])
strips_init = pv.PolyData(vertices, strips=strips)
assert len(strips_init.strips) == len(strips)
# add strips using the setter
strips_setter = pv.PolyData(vertices)
strips_setter.strips = strips
assert len(strips_setter.strips) == len(strips)
# test n_strips function
strips = np.array([[4, 0, 1, 3, 2], [4, 1, 2, 3, 0]])
strips_stack = np.hstack(strips)
n_strips_test = pv.PolyData(vertices, strips=strips_stack)
assert n_strips_test.n_strips == len(strips)
def test_ribbon_filter():
line = examples.load_spline().compute_arc_length(progress_bar=True)
ribbon = line.ribbon(width=0.5, scalars='arc_length')
assert ribbon.n_points
for tcoords in [True, 'lower', 'normalized', False]:
ribbon = line.ribbon(width=0.5, tcoords=tcoords)
assert ribbon.n_points
def test_is_all_triangles():
# mesh points
vertices = np.array([[0, 0, 0], [1, 0, 0], [1, 1, 0], [0, 1, 0], [0.5, 0.5, -1]])
# mesh faces
faces = np.hstack(
[
[4, 0, 1, 2, 3],
[3, 0, 1, 4],
[3, 1, 2, 4],
]
) # [square, triangle, triangle]
mesh = pv.PolyData(vertices, faces)
assert not mesh.is_all_triangles
mesh = mesh.triangulate()
assert mesh.is_all_triangles
def test_extrude():
arc = pv.CircularArc(pointa=[-1, 0, 0], pointb=[1, 0, 0], center=[0, 0, 0])
poly = arc.extrude([0, 0, 1], progress_bar=True, capping=True)
assert poly.n_points
assert poly.n_cells
assert np.any(poly.strips)
n_points_old = arc.n_points
arc.extrude([0, 0, 1], inplace=True, capping=True)
assert arc.n_points != n_points_old
def test_extrude_capping_warnings():
arc = pv.CircularArc(pointa=[-1, 0, 0], pointb=[1, 0, 0], center=[0, 0, 0])
with pytest.warns(PyVistaFutureWarning, match='default value of the ``capping`` keyword'):
arc.extrude([0, 0, 1])
with pytest.warns(PyVistaFutureWarning, match='default value of the ``capping`` keyword'):
arc.extrude_rotate()
@pytest.mark.xfail(importlib.util.find_spec("paraview"),
reason="paraview provides inconsistent vtk")
def test_flip_normals(sphere):
with pytest.warns(
PyVistaDeprecationWarning, match='`flip_normals` is deprecated. Use `flip_faces` instead'
):
sphere.flip_normals()
@pytest.mark.parametrize('mesh', [pv.Sphere(), pv.Plane()])
def test_flip_normal_vectors(mesh):
mesh = mesh.compute_normals()
flipped = mesh.flip_normal_vectors(inplace=True, progress_bar=True)
assert flipped is mesh
flipped = mesh.flip_normal_vectors()
assert flipped is not mesh
assert np.allclose(flipped.point_data['Normals'], -mesh.point_data['Normals'])
assert np.allclose(flipped.cell_data['Normals'], -mesh.cell_data['Normals'])
# Test ordering is unaffected
assert np.allclose(flipped.faces, mesh.faces)
@pytest.mark.parametrize('mesh', [pv.Sphere(), pv.Plane()])
def test_flip_faces(mesh):
flipped = mesh.flip_faces(inplace=True, progress_bar=True)
assert flipped is mesh
flipped = mesh.flip_faces()
assert flipped is not mesh
assert np.allclose(flipped.regular_faces[0], mesh.regular_faces[0][::-1])
# Test normals are unaffected
assert np.allclose(flipped.point_data['Normals'], mesh.point_data['Normals'])
def test_n_verts():
mesh = pv.PolyData([[1.0, 0.0, 0.0], [1.0, 1.0, 1.0]])
assert mesh.n_verts == 2
def test_n_lines():
mesh = pv.Line()
assert mesh.n_lines == 1
def test_n_faces_strict():
# Mesh with one face and one line
mesh = pv.PolyData(
[(0.0, 0.0, 0.0), (1.0, 0.0, 0.0), (0.0, 1.0, 0.0)],
faces=[3, 0, 1, 2],
lines=[2, 0, 1],
)
assert mesh.n_cells == 2 # n_faces + n_lines
assert mesh.n_faces_strict == 1
@pytest.fixture
def default_n_faces():
pv.PolyData._USE_STRICT_N_FACES = False
yield
pv.PolyData._USE_STRICT_N_FACES = False
def test_n_faces():
if pv._version.version_info[:2] >= (0, 46):
# At version 0.46, n_faces should raise an error instead of warning
mesh = pv.PolyData(
[(0.0, 0.0, 0.0), (1.0, 0.0, 0.0), (0.0, 1.0, 0.0)],
faces=[3, 0, 1, 2],
lines=[2, 0, 1],
)
# Should raise an AttributeError
with pytest.raises(
AttributeError,
match='The non-strict behavior of `pv.PolyData.n_faces` has been removed',
):
_ = mesh.n_faces
else:
# Pre-0.46 behavior: warning
mesh = pv.PolyData(
[(0.0, 0.0, 0.0), (1.0, 0.0, 0.0), (0.0, 1.0, 0.0)],
faces=[3, 0, 1, 2],
lines=[2, 0, 1],
)
# Should raise a warning the first time
with pytest.warns(
pv.PyVistaDeprecationWarning,
match='The current behavior of `pv.PolyData.n_faces` has been deprecated',
):
nf = mesh.n_faces
# Current (deprecated) behavior is that n_faces is aliased to n_cells
assert nf == mesh.n_cells
# Shouldn't raise deprecation warning the second time
with warnings.catch_warnings():
warnings.simplefilter('error')
nf1 = mesh.n_faces
assert nf1 == nf
if pv._version.version_info[:2] > (0, 49):
msg = 'Convert default n_faces behavior to strict'
raise RuntimeError(msg)
def test_opt_in_n_faces_strict():
pv.PolyData.use_strict_n_faces(True)
mesh = pv.PolyData(
[(0.0, 0.0, 0.0), (1.0, 0.0, 0.0), (0.0, 1.0, 0.0)],
faces=[3, 0, 1, 2],
lines=[2, 0, 1],
)
assert mesh.n_faces == mesh.n_faces_strict
def test_geodesic_disconnected(sphere, sphere_shifted):
# the sphere and sphere_shifted are disconnected - no path between them
combined = sphere + sphere_shifted
start_vertex = 0
end_vertex = combined.n_points - 1
match = f'There is no path between vertices {start_vertex} and {end_vertex}.'
with pytest.raises(ValueError, match=match):
combined.geodesic(start_vertex, end_vertex)
with pytest.raises(ValueError, match=match):
combined.geodesic_distance(start_vertex, end_vertex)
def test_tetrahedron_regular_faces():
tetra = pv.Tetrahedron()
assert np.array_equal(tetra.faces.reshape(-1, 4)[:, 1:], tetra.regular_faces)
@pytest.mark.parametrize('deep', [False, True])
def test_regular_faces(deep):
points = np.array([[1, 1, 1], [-1, 1, -1], [1, -1, -1], [-1, -1, 1]], dtype=float)
faces = np.array([[0, 1, 2], [1, 3, 2], [0, 2, 3], [0, 3, 1]])
mesh = pv.PolyData.from_regular_faces(points, faces, deep=deep)
expected_faces = np.hstack([np.full((len(faces), 1), 3), faces]).astype(pv.ID_TYPE).flatten()
assert np.array_equal(mesh.faces, expected_faces)
assert np.array_equal(mesh.regular_faces, faces)
def test_set_regular_faces():
mesh = pv.Tetrahedron()
flipped_faces = mesh.regular_faces[:, ::-1]
mesh.regular_faces = flipped_faces
assert np.array_equal(mesh.regular_faces, flipped_faces)
def test_empty_regular_faces():
mesh = pv.PolyData()
assert np.array_equal(mesh.regular_faces, np.array([], dtype=pv.ID_TYPE))
def test_regular_faces_mutable():
points = [[1.0, 1.0, 1.0], [-1.0, 1.0, -1.0], [1.0, -1.0, -1.0], [-1.0, -1.0, 1.0]]
faces = [[0, 1, 2]]
mesh = pv.PolyData.from_regular_faces(points, faces)
mesh.regular_faces[0, 2] = 3
assert np.array_equal(mesh.faces, [3, 0, 1, 3])
def _assert_irregular_faces_equal(faces, expected):
assert len(faces) == len(expected)
assert all(map(np.array_equal, faces, expected))
def test_irregular_faces():
points = [(1, 1, 0), (-1, 1, 0), (-1, -1, 0), (1, -1, 0), (0, 0, 1.61)]
faces = [(0, 1, 2, 3), (0, 3, 4), (0, 4, 1), (3, 2, 4), (2, 1, 4)]
expected_faces = [4, 0, 1, 2, 3, 3, 0, 3, 4, 3, 0, 4, 1, 3, 3, 2, 4, 3, 2, 1, 4]
mesh = pv.PolyData.from_irregular_faces(points, faces)
assert np.array_equal(mesh.faces, expected_faces)
_assert_irregular_faces_equal(mesh.irregular_faces, expected=faces)
def test_set_irregular_faces():
mesh = pv.Pyramid().extract_surface()
flipped_faces = tuple(f[::-1] for f in mesh.irregular_faces)
mesh.irregular_faces = flipped_faces
_assert_irregular_faces_equal(mesh.irregular_faces, flipped_faces)
def test_empty_irregular_faces():
mesh = pv.PolyData()
assert mesh.irregular_faces == ()
def test_irregular_faces_mutable():
points = [(1, 1, 0), (-1, 1, 0), (-1, -1, 0), (1, -1, 0), (0, 0, 1.61)]
faces = [(0, 1, 2, 3), (0, 3, 4), (0, 4, 1), (3, 2, 4), (2, 1, 4)]
mesh = pv.PolyData.from_irregular_faces(points, faces)
mesh.irregular_faces[0][0] = 4
expected = [(4, 1, 2, 3), *faces[1:]]
_assert_irregular_faces_equal(mesh.irregular_faces, expected)
@pytest.mark.parametrize('cells', ['faces', 'lines', 'strips', 'verts'])
def test_n_faces_etc_deprecated(cells: str):
n_cells = 'n_' + cells
kwargs = {cells: [3, 0, 1, 2], n_cells: 1} # e.g. specify faces and n_faces
with pytest.warns(
pv.PyVistaDeprecationWarning,
match=f'`PolyData` constructor parameter `{n_cells}` is deprecated and no longer used',
):
_ = pv.PolyData(np.zeros((3, 3)), **kwargs)
if pv._version.version_info[:2] > (0, 47):
msg = f'Convert `PolyData` `{n_cells}` deprecation warning to error'
raise RuntimeError(msg)
if pv._version.version_info[:2] > (0, 48):
msg = f'Remove `PolyData` `{n_cells} constructor kwarg'
raise RuntimeError(msg)
@pytest.mark.parametrize('inplace', [True, False])
def test_merge_points(inplace):
mesh = pv.Cylinder(resolution=4)
assert mesh.n_points == 8 * 2
output = mesh.merge_points(inplace=inplace)
assert output.n_points == 8
assert isinstance(mesh, pv.PolyData)
assert (mesh is output) == inplace
|