Spaces:
Running on Zero
Running on Zero
File size: 128,991 Bytes
be88765 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 | #!/usr/bin/env python3
"""
Visualize Registered Point Clouds using Estimated Poses
This script loads point clouds from an input directory and transforms them using
estimated poses from a result directory. Visualizes one sample at a time with
keyboard navigation (spacebar for next sample).
Usage:
python visualize_registered_pointclouds.py \
--input ./dataset/lidar_rpf_training_data/scannet_test_v1 \
--results ./dataset/lidar_rpf_training_data/scannet_test_v1_result \
--sequence scene0025_01
# Visualize all sequences
python visualize_registered_pointclouds.py \
--input ./dataset/lidar_rpf_training_data/scannet_test_v1 \
--results ./dataset/lidar_rpf_training_data/scannet_test_v1_result
"""
import argparse
import os
import sys
# Add project root so "dataset_process" can be imported when run from any directory
_script_dir = os.path.dirname(os.path.abspath(__file__))
_project_root = os.path.dirname(_script_dir)
if _project_root not in sys.path:
sys.path.insert(0, _project_root)
import glob
import re
import logging
import json
import numpy as np
import open3d as o3d
import natsort
from pathlib import Path
from typing import List, Tuple, Optional, Dict, Union
from dataset_process.utils.io_utils import CMAP_DEFAULT
# Logger will be configured in main() after parsing arguments
logger = logging.getLogger(__name__)
class RegisteredPointCloudVisualizer:
"""Visualizer for registered point clouds using estimated poses."""
def __init__(self,
input_dir: str,
poses_dir: str,
point_size: float = 3.0,
background_color: List[float] = [1.0, 1.0, 1.0],
no_coordinate_frame: bool = True,
estimate_normals: bool = False,
normal_estimation_radius: float = 0.1,
generation: str = "generation00",
max_points_per_fragment: int = 10000,
remove_outliers: bool = False,
outlier_nb_neighbors: int = 20,
outlier_std_ratio: float = 2.0):
"""
Initialize the registered point cloud visualizer.
Args:
input_dir: Root directory containing point cloud files
poses_dir: Root directory containing pose files
point_size: Size of points in the visualization
background_color: Background color as [R, G, B] in range [0, 1]
no_coordinate_frame: Whether to hide coordinate frame
estimate_normals: If True, estimate normals for point clouds that don't have them
normal_estimation_radius: Radius for normal estimation
generation: Generation subdirectory name (default: "generation00")
max_points_per_fragment: Maximum points to load per fragment
remove_outliers: If True, apply stochastic outlier removal to input point clouds
outlier_nb_neighbors: Number of neighbors for outlier removal (default: 20)
outlier_std_ratio: Standard deviation ratio for outlier removal (default: 2.0)
"""
self.input_dir = input_dir
self.poses_dir = poses_dir
self.point_size = point_size
self.background_color = background_color
self.no_coordinate_frame = no_coordinate_frame
self.estimate_normals = estimate_normals
self.normal_estimation_radius = normal_estimation_radius
self.generation = generation
self.max_points_per_fragment = max_points_per_fragment
self.remove_outliers = remove_outliers
self.outlier_nb_neighbors = outlier_nb_neighbors
self.outlier_std_ratio = outlier_std_ratio
# Navigation state
self.current_sequence = None
self.current_sample_idx = None
self.sequence_samples_cache = {} # Cache for sequence sample lists
self.vis = None
self.colored_pcds = []
self.original_pcds = [] # Store original (untransformed) point clouds
self.registered_pcds = [] # Store registered (transformed) point clouds
self.generated_pcds = [] # Store generated point clouds from processed directory
self.generated_gt_pcd = None # Store merged GT point cloud
self.random_yaw_pcds = [] # Store input point clouds with random yaw rotations
self.view_mode = "registered" # "original", "registered", "generated", "gt", or "random_yaw"
# Generation cycling state
self.cycle_generations = False # Whether to cycle through generations instead of samples
self.current_generation_idx = 0 # Current generation index when cycling
self.available_generations = [] # List of available generation names for current sample
# Background toggle state
self.use_white_background = True
self.white_background = [1.0, 1.0, 1.0]
self.black_background = [0.0, 0.0, 0.0]
if np.mean(self.background_color) > 0.5:
self.use_white_background = True
else:
self.use_white_background = False
def compute_adaptive_normal_radius(self, points: np.ndarray) -> float:
"""
Compute adaptive normal estimation radius based on point cloud scale.
Uses 2% of average extent, with a minimum of 0.05.
Args:
points: Point cloud points as numpy array (N, 3)
Returns:
Adaptive radius value
"""
if len(points) == 0:
return self.normal_estimation_radius
# Compute extent (bounding box size)
min_coords = np.min(points, axis=0)
max_coords = np.max(points, axis=0)
extent = max_coords - min_coords
# Use adaptive radius based on point cloud scale (2% of average extent)
adaptive_radius = max(np.mean(extent) * 0.02, 0.05)
return adaptive_radius
def apply_outlier_removal(self, pcd: o3d.geometry.PointCloud) -> o3d.geometry.PointCloud:
"""
Apply stochastic outlier removal to a point cloud.
Args:
pcd: Open3D point cloud
Returns:
Point cloud with outliers removed
"""
if not self.remove_outliers:
return pcd
points = np.asarray(pcd.points)
if len(points) < self.outlier_nb_neighbors:
# Not enough points for outlier removal
logger.debug(f"Skipping outlier removal: only {len(points)} points (need at least {self.outlier_nb_neighbors})")
return pcd
try:
# Apply statistical outlier removal
# This returns (cleaned_pcd, indices) where indices are the inlier indices
pcd_cleaned, ind = pcd.remove_statistical_outlier(
nb_neighbors=self.outlier_nb_neighbors,
std_ratio=self.outlier_std_ratio
)
num_removed = len(points) - len(np.asarray(pcd_cleaned.points))
if num_removed > 0:
logger.debug(f"Removed {num_removed} outliers ({num_removed/len(points)*100:.1f}%)")
# Colors and normals are automatically preserved by remove_statistical_outlier
# as it filters by indices, so we don't need to manually copy them
return pcd_cleaned
except Exception as e:
logger.warning(f"Failed to apply outlier removal: {e}, returning original point cloud")
return pcd
def center_pcd(self, pcd: np.ndarray) -> Tuple[np.ndarray, np.ndarray]:
"""Center point cloud at origin."""
center = np.mean(pcd, axis=0)
pcd = pcd - center
return pcd, center
def translate_point_cloud(self, pcd: o3d.geometry.PointCloud, translation: np.ndarray) -> o3d.geometry.PointCloud:
"""
Translate a point cloud by a given translation vector.
Args:
pcd: Open3D point cloud
translation: Translation vector (3D)
Returns:
Translated point cloud
"""
points = np.asarray(pcd.points)
translated_points = points - translation # Subtract to move towards origin
translated_pcd = o3d.geometry.PointCloud()
translated_pcd.points = o3d.utility.Vector3dVector(translated_points)
# Copy normals if available
if pcd.has_normals():
translated_pcd.normals = pcd.normals
# Copy colors if available
if pcd.has_colors():
translated_pcd.colors = pcd.colors
return translated_pcd
def apply_random_yaw_rotation(self, pcd: o3d.geometry.PointCloud, skip_rotation: bool = False) -> o3d.geometry.PointCloud:
"""
Apply random yaw rotation (around z-axis) with small roll and pitch perturbations to a point cloud.
First centers the point cloud at origin, then applies random rotations:
- Yaw: random rotation around z-axis (0 to 2π)
- Roll: random rotation around x-axis (±15 degrees)
- Pitch: random rotation around y-axis (±15 degrees)
Args:
pcd: Open3D point cloud
skip_rotation: If True, only center the point cloud without applying rotation
Returns:
Point cloud with random rotations applied (or just centered if skip_rotation=True)
"""
points = np.asarray(pcd.points)
if len(points) == 0:
return pcd
# Center the point cloud at origin
center = np.mean(points, axis=0)
centered_points = points - center
# If skip_rotation is True, just return centered point cloud
if skip_rotation:
centered_pcd = o3d.geometry.PointCloud()
centered_pcd.points = o3d.utility.Vector3dVector(centered_points)
# Copy normals if available
if pcd.has_normals():
centered_pcd.normals = pcd.normals
# Copy colors if available
if pcd.has_colors():
centered_pcd.colors = pcd.colors
return centered_pcd
# Generate random rotation angles
yaw_angle = np.random.uniform(0, 2 * np.pi) # Full yaw rotation
roll_angle = np.random.uniform(-15, 15) * np.pi / 180 # ±15 degrees roll
pitch_angle = np.random.uniform(-15, 15) * np.pi / 180 # ±15 degrees pitch
# Create rotation matrices
# Roll rotation around x-axis
cos_roll = np.cos(roll_angle)
sin_roll = np.sin(roll_angle)
R_roll = np.array([
[1, 0, 0],
[0, cos_roll, -sin_roll],
[0, sin_roll, cos_roll]
])
# Pitch rotation around y-axis
cos_pitch = np.cos(pitch_angle)
sin_pitch = np.sin(pitch_angle)
R_pitch = np.array([
[cos_pitch, 0, sin_pitch],
[0, 1, 0],
[-sin_pitch, 0, cos_pitch]
])
# Yaw rotation around z-axis
cos_yaw = np.cos(yaw_angle)
sin_yaw = np.sin(yaw_angle)
R_yaw = np.array([
[cos_yaw, -sin_yaw, 0],
[sin_yaw, cos_yaw, 0],
[0, 0, 1]
])
# Combine rotations: R = R_yaw * R_pitch * R_roll
# This applies roll first, then pitch, then yaw
rotation_matrix = R_yaw @ R_pitch @ R_roll
# Apply rotation to centered points
rotated_points = (rotation_matrix @ centered_points.T).T
# Create new point cloud
rotated_pcd = o3d.geometry.PointCloud()
rotated_pcd.points = o3d.utility.Vector3dVector(rotated_points)
# Transform normals if available
if pcd.has_normals():
normals = np.asarray(pcd.normals)
if len(normals) == len(points):
# Only rotate normals (no translation for normals)
rotated_normals = (rotation_matrix @ normals.T).T
rotated_pcd.normals = o3d.utility.Vector3dVector(rotated_normals)
# Copy colors if available
if pcd.has_colors():
rotated_pcd.colors = pcd.colors
return rotated_pcd
def find_max_points_generated_part_index(self, generated_pcds: List[o3d.geometry.PointCloud]) -> Optional[int]:
"""
Find the index of the generated part with the most points.
Args:
generated_pcds: List of generated point clouds
Returns:
Index of the part with the most points, or None if list is empty
"""
if not generated_pcds:
return None
max_points = -1
max_idx = None
for idx, pcd in enumerate(generated_pcds):
points = np.asarray(pcd.points)
num_points = len(points)
if num_points > max_points:
max_points = num_points
max_idx = idx
return max_idx
def create_random_yaw_point_clouds(self, input_pcds: List[o3d.geometry.PointCloud],
generated_pcds: Optional[List[o3d.geometry.PointCloud]] = None) -> List[o3d.geometry.PointCloud]:
"""
Create random rotated versions of input point clouds.
Each part is centered at origin and has random rotations applied:
- Yaw: random rotation around z-axis (0 to 2π)
- Roll: random rotation around x-axis (±15 degrees)
- Pitch: random rotation around y-axis (±15 degrees)
If generated_pcds are provided, the input part corresponding to the generated part
with the most points will NOT be rotated (only centered).
Args:
input_pcds: List of original input point clouds
generated_pcds: Optional list of generated point clouds to determine which part to skip rotation
Returns:
List of point clouds with random rotations applied
"""
# Find which part should not be rotated (if generated_pcds are available)
skip_rotation_idx = None
if generated_pcds:
max_points_idx = self.find_max_points_generated_part_index(generated_pcds)
if max_points_idx is not None and max_points_idx < len(input_pcds):
skip_rotation_idx = max_points_idx
logger.info(f"Skipping rotation for input part {skip_rotation_idx} (corresponds to generated part with most points: {len(np.asarray(generated_pcds[max_points_idx].points))} points)")
random_yaw_pcds = []
for idx, pcd in enumerate(input_pcds):
skip_rotation = (skip_rotation_idx == idx)
rotated_pcd = self.apply_random_yaw_rotation(pcd, skip_rotation=skip_rotation)
random_yaw_pcds.append(rotated_pcd)
return random_yaw_pcds
def merge_input_point_clouds(self, input_pcds: List[o3d.geometry.PointCloud]) -> Optional[np.ndarray]:
"""
Merge input point clouds and return all points as a numpy array.
Used as fallback for center of mass calculation when GT is not available.
Args:
input_pcds: List of input point clouds
Returns:
Merged points as numpy array, or None if no points available
"""
if not input_pcds:
return None
all_points = []
for pcd in input_pcds:
points = np.asarray(pcd.points)
if len(points) > 0:
all_points.append(points)
if not all_points:
return None
merged_points = np.concatenate(all_points, axis=0)
return merged_points
def find_sequence_directories(self, input_dir: str, sequence: str) -> Tuple[Optional[str], Optional[str]]:
"""
Find input and result sequence directories.
Returns:
Tuple of (input_sequence_dir, result_sequence_dir) or (None, None) if not found
If result directory doesn't exist separately, result_sequence_dir may be the same as input_sequence_dir
"""
sequence_parts = sequence.split('/')
search_sequence = sequence_parts[-1] if sequence_parts else sequence
# Find input sequence directory
input_seq_dir = None
candidate = os.path.join(self.input_dir, sequence)
if os.path.exists(candidate):
input_seq_dir = candidate
if input_seq_dir is None and len(sequence_parts) > 1:
candidate = self.input_dir
for part in sequence_parts:
candidate = os.path.join(candidate, part)
if not os.path.exists(candidate):
candidate = None
break
if candidate:
input_seq_dir = candidate
if input_seq_dir is None:
def find_seq_dir(root_dir: str, target: str, max_depth: int = 3) -> Optional[str]:
if max_depth <= 0:
return None
if os.path.basename(root_dir) == target or target in os.path.basename(root_dir):
return root_dir
if os.path.isdir(root_dir):
try:
for item in os.listdir(root_dir):
item_path = os.path.join(root_dir, item)
if os.path.isdir(item_path):
result = find_seq_dir(item_path, target, max_depth - 1)
if result:
return result
except PermissionError:
pass
return None
input_seq_dir = find_seq_dir(self.input_dir, search_sequence)
# Find result sequence directory
result_seq_dir = None
# First, try to find a separate result directory
candidate = os.path.join(self.poses_dir, sequence)
if os.path.exists(candidate):
result_seq_dir = candidate
if result_seq_dir is None and len(sequence_parts) > 1:
candidate = self.poses_dir
for part in sequence_parts:
candidate = os.path.join(candidate, part)
if not os.path.exists(candidate):
candidate = None
break
if candidate:
result_seq_dir = candidate
if result_seq_dir is None:
def find_result_seq_dir(root_dir: str, target: str, max_depth: int = 3) -> Optional[str]:
if max_depth <= 0:
return None
if os.path.basename(root_dir) == target or target in os.path.basename(root_dir):
return root_dir
if os.path.isdir(root_dir):
try:
for item in os.listdir(root_dir):
item_path = os.path.join(root_dir, item)
if os.path.isdir(item_path):
result = find_result_seq_dir(item_path, target, max_depth - 1)
if result:
return result
except PermissionError:
pass
return None
result_seq_dir = find_result_seq_dir(self.poses_dir, search_sequence)
# If no separate result directory found, use input directory (for cases where transforms are alongside PLY files)
if result_seq_dir is None and input_seq_dir is not None:
# Check if transform files exist in the input directory
# This handles cases like nss_ss_v1 where transforms are in the same directory as PLY files
result_seq_dir = input_seq_dir
return input_seq_dir, result_seq_dir
def find_sample_directories(self, sequence_dir: str) -> List[str]:
"""
Find all sample directories in a sequence directory.
Samples can be:
1. Directories starting with 'sample_' (original format)
2. Directories containing PLY files directly (modelnet format)
"""
sample_dirs = []
if not os.path.exists(sequence_dir):
return sample_dirs
# First, look for sample_* directories (original format)
for item in os.listdir(sequence_dir):
item_path = os.path.join(sequence_dir, item)
if os.path.isdir(item_path) and item.startswith('sample_'):
sample_dirs.append(item_path)
# If no sample_* directories found, look for directories containing PLY files (modelnet format)
if not sample_dirs:
for item in os.listdir(sequence_dir):
item_path = os.path.join(sequence_dir, item)
if os.path.isdir(item_path):
# Check if this directory contains PLY files
ply_files = glob.glob(os.path.join(item_path, "*.ply"))
if ply_files:
sample_dirs.append(item_path)
return sorted(sample_dirs)
def get_available_generations(self, result_sample_dir: str) -> List[str]:
"""
Find all available generation names for a sample directory.
Returns a list of generation names (e.g., ['generation_selected', 'generation00', 'generation01', ...])
sorted by priority: generation_selected first, then generation00, generation01, etc.
"""
if not os.path.exists(result_sample_dir):
return []
generations = set()
# Find all transform files with generation patterns
transform_patterns = [
"*generation*_transform.txt",
"*_generation*transform.txt",
]
for pattern in transform_patterns:
matches = glob.glob(os.path.join(result_sample_dir, pattern))
for match in matches:
basename = os.path.basename(match)
# Extract generation name
# Try to match patterns like: *generation_selected*transform.txt, *generation00*transform.txt
gen_match = re.search(r'generation(_selected|\d+)', basename)
if gen_match:
gen_name = gen_match.group(0) # e.g., 'generation_selected' or 'generation00'
generations.add(gen_name)
# Sort: generation_selected first, then generation00, generation01, etc.
def sort_key(gen_name):
if gen_name == "generation_selected":
return (0, "") # Highest priority
else:
# Extract number from generation00, generation01, etc.
num_match = re.search(r'generation(\d+)', gen_name)
if num_match:
return (1, int(num_match.group(1))) # Sort by number
return (2, gen_name) # Other patterns last
sorted_generations = sorted(generations, key=sort_key)
return sorted_generations
def find_matching_transform_file(self, ply_filename: str, result_sample_dir: str, generation: Optional[str] = None) -> Optional[str]:
"""
Find matching transform file for a PLY file.
If generation is specified, uses that generation. Otherwise, first tries to find
*generation_selected*.txt files, then falls back to generation00.
"""
basename = os.path.splitext(os.path.basename(ply_filename))[0]
part_match = None
submap_match = re.search(r'submap_(\d+)', basename)
if submap_match:
part_num = int(submap_match.group(1))
part_match = f"part{part_num:02d}"
if part_match is None:
part_match_obj = re.search(r'part_(\d+)', basename)
if part_match_obj:
part_num = int(part_match_obj.group(1))
part_match = f"part{part_num:02d}"
# Also handle simple "part(\d+)" pattern without underscore (e.g., "part1.ply")
if part_match is None:
part_match_obj = re.search(r'part(\d+)', basename)
if part_match_obj:
part_num = int(part_match_obj.group(1))
part_match = f"part{part_num:02d}"
# If generation is specified, use it directly
if generation:
if part_match:
# Try multiple patterns for the specified generation
patterns = [
os.path.join(result_sample_dir, f"*{generation}*{part_match}_transform.txt"),
os.path.join(result_sample_dir, f"*{generation}_{part_match}_transform.txt"),
os.path.join(result_sample_dir, f"*_{generation}_*{part_match}_transform.txt"),
os.path.join(result_sample_dir, f"*{part_match}_{generation}*transform.txt"),
]
for pattern in patterns:
matches = glob.glob(pattern)
if matches:
return matches[0]
sample_match = re.search(r'sample_(\d+)', basename)
if sample_match:
sample_id = sample_match.group(1)
patterns = [
os.path.join(result_sample_dir, f"*sample{sample_id}*{generation}*transform.txt"),
os.path.join(result_sample_dir, f"*sample{sample_id}*_{generation}_*transform.txt"),
]
for pattern in patterns:
matches = glob.glob(pattern)
if matches:
if part_match:
for match in matches:
if part_match in match:
return match
if matches:
return matches[0]
return None
# If no generation specified, use default priority: generation_selected first, then generation00
if part_match:
# Try generation_selected pattern first (more flexible pattern)
patterns_selected = [
os.path.join(result_sample_dir, f"*generation_selected*{part_match}_transform.txt"),
os.path.join(result_sample_dir, f"*_generation_selected_*{part_match}_transform.txt"),
os.path.join(result_sample_dir, f"*{part_match}_generation_selected*transform.txt"),
]
for pattern_selected in patterns_selected:
matches_selected = glob.glob(pattern_selected)
if matches_selected:
return matches_selected[0]
# Fall back to generation00
pattern = os.path.join(result_sample_dir, f"*generation00_{part_match}_transform.txt")
matches = glob.glob(pattern)
if matches:
return matches[0]
sample_match = re.search(r'sample_(\d+)', basename)
if sample_match:
sample_id = sample_match.group(1)
# Try generation_selected pattern first (more flexible pattern)
patterns_selected = [
os.path.join(result_sample_dir, f"*sample{sample_id}*generation_selected*transform.txt"),
os.path.join(result_sample_dir, f"*sample{sample_id}*_generation_selected_*transform.txt"),
]
for pattern_selected in patterns_selected:
matches_selected = glob.glob(pattern_selected)
if matches_selected:
if part_match:
for match in matches_selected:
if part_match in match:
return match
if matches_selected:
return matches_selected[0]
# Fall back to generation00
pattern = os.path.join(result_sample_dir, f"*sample{sample_id}*generation00*transform.txt")
matches = glob.glob(pattern)
if matches:
if part_match:
for match in matches:
if part_match in match:
return match
return matches[0]
return None
def find_matching_transform_file_by_index(self, part_index: int, result_sample_dir: str, generation: Optional[str] = None) -> Optional[str]:
"""
Find matching transform file by part index (for PLY files without part numbers in their names).
Args:
part_index: Zero-based index of the part (0, 1, 2, ...)
result_sample_dir: Directory containing transform files
generation: Optional generation name to use (e.g., 'generation00', 'generation_selected')
Returns:
Path to matching transform file, or None if not found
"""
part_match = f"part{part_index:02d}"
# If generation is specified, use it directly
if generation:
patterns = [
os.path.join(result_sample_dir, f"*{generation}*{part_match}_transform.txt"),
os.path.join(result_sample_dir, f"*{generation}_{part_match}_transform.txt"),
os.path.join(result_sample_dir, f"*_{generation}_*{part_match}_transform.txt"),
os.path.join(result_sample_dir, f"*{part_match}_{generation}*transform.txt"),
]
for pattern in patterns:
matches = glob.glob(pattern)
if matches:
return matches[0]
return None
# If no generation specified, use default priority: generation_selected first, then generation00
# Try generation_selected pattern first
patterns_selected = [
os.path.join(result_sample_dir, f"*generation_selected*{part_match}_transform.txt"),
os.path.join(result_sample_dir, f"*_generation_selected_*{part_match}_transform.txt"),
os.path.join(result_sample_dir, f"*{part_match}_generation_selected*transform.txt"),
]
for pattern_selected in patterns_selected:
matches_selected = glob.glob(pattern_selected)
if matches_selected:
return matches_selected[0]
# Fall back to generation00
pattern = os.path.join(result_sample_dir, f"*generation00*{part_match}_transform.txt")
matches = glob.glob(pattern)
if matches:
return matches[0]
return None
def load_pose_from_file(self, pose_file: str) -> Optional[np.ndarray]:
"""Load 4x4 pose matrix from text file."""
try:
pose = np.loadtxt(pose_file)
if pose.shape != (4, 4):
if pose.shape == (16,):
pose = pose.reshape(4, 4)
else:
logger.warning(f"Invalid pose shape {pose.shape} in {pose_file}")
return None
return pose
except Exception as e:
logger.warning(f"Failed to load pose from {pose_file}: {e}")
return None
def load_json_metadata(self, result_sample_dir: str) -> Optional[Dict]:
"""
Load JSON metadata file from result sample directory.
Tries multiple patterns to find metadata files, prioritizing generation_selected.
"""
# Priority order: generation_selected > generation00 > other generation files > metadata.json > any *.json
# 1. Try generation_selected JSON files first (matching transform file priority)
pattern_selected = os.path.join(result_sample_dir, "*generation_selected.json")
matches_selected = glob.glob(pattern_selected)
if matches_selected:
try:
with open(matches_selected[0], 'r') as f:
metadata = json.load(f)
logger.debug(f"Loaded metadata from generation_selected file: {os.path.basename(matches_selected[0])}")
return metadata
except Exception as e:
logger.debug(f"Failed to load generation_selected metadata file {matches_selected[0]}: {e}")
# 2. Try generation00 JSON files
pattern_generation00 = os.path.join(result_sample_dir, "*generation00.json")
matches_generation00 = glob.glob(pattern_generation00)
if matches_generation00:
try:
with open(matches_generation00[0], 'r') as f:
metadata = json.load(f)
logger.debug(f"Loaded metadata from generation00 file: {os.path.basename(matches_generation00[0])}")
return metadata
except Exception as e:
logger.debug(f"Failed to load generation00 metadata file {matches_generation00[0]}: {e}")
# 3. Try any generation*.json files
pattern_generation = os.path.join(result_sample_dir, "*generation*.json")
matches_generation = glob.glob(pattern_generation)
if matches_generation:
# Sort to get the highest generation number
matches_generation_sorted = sorted(matches_generation, reverse=True)
try:
with open(matches_generation_sorted[0], 'r') as f:
metadata = json.load(f)
logger.debug(f"Loaded metadata from generation file: {os.path.basename(matches_generation_sorted[0])}")
return metadata
except Exception as e:
logger.debug(f"Failed to load generation metadata file {matches_generation_sorted[0]}: {e}")
# 4. Try exact metadata.json
metadata_path = os.path.join(result_sample_dir, "metadata.json")
if os.path.exists(metadata_path):
try:
with open(metadata_path, 'r') as f:
metadata = json.load(f)
logger.debug(f"Loaded metadata from metadata.json")
return metadata
except Exception as e:
logger.debug(f"Failed to load metadata.json: {e}")
# 5. Try metadata_sample_*.json pattern
pattern_metadata_sample = os.path.join(result_sample_dir, "metadata_sample_*.json")
matches_metadata_sample = glob.glob(pattern_metadata_sample)
if matches_metadata_sample:
try:
with open(matches_metadata_sample[0], 'r') as f:
metadata = json.load(f)
logger.debug(f"Loaded metadata from metadata_sample file: {os.path.basename(matches_metadata_sample[0])}")
return metadata
except Exception as e:
logger.debug(f"Failed to load metadata_sample file {matches_metadata_sample[0]}: {e}")
# 6. Last resort: try any *.json file (but skip transform files)
pattern_any_json = os.path.join(result_sample_dir, "*.json")
matches_any_json = glob.glob(pattern_any_json)
# Filter out transform-related files
matches_any_json = [m for m in matches_any_json if "transform" not in os.path.basename(m).lower()]
if matches_any_json:
try:
with open(matches_any_json[0], 'r') as f:
metadata = json.load(f)
logger.debug(f"Loaded metadata from JSON file: {os.path.basename(matches_any_json[0])}")
return metadata
except Exception as e:
logger.debug(f"Failed to load JSON file {matches_any_json[0]}: {e}")
return None
def print_metadata(self, metadata: Dict):
"""Print metadata in a formatted way."""
if not metadata:
return
logger.info("=" * 60)
logger.info("METADATA:")
logger.info("=" * 60)
def print_dict(d, indent=0):
"""Recursively print dictionary with indentation."""
for key, value in d.items():
if isinstance(value, dict):
logger.info(" " * indent + f"{key}:")
print_dict(value, indent + 1)
elif isinstance(value, list):
if len(value) > 0 and isinstance(value[0], dict):
logger.info(" " * indent + f"{key}: [")
for i, item in enumerate(value[:5]): # Limit to first 5 items
logger.info(" " * (indent + 1) + f"Item {i}:")
print_dict(item, indent + 2)
if len(value) > 5:
logger.info(" " * (indent + 1) + f"... ({len(value) - 5} more items)")
logger.info(" " * indent + "]")
else:
logger.info(" " * indent + f"{key}: {value}")
else:
logger.info(" " * indent + f"{key}: {value}")
print_dict(metadata)
logger.info("=" * 60)
def load_sample_point_clouds(self, input_sample_dir: str, result_sample_dir: str, return_transforms: bool = False, generation: Optional[str] = None) -> Union[Tuple[List[o3d.geometry.PointCloud], List[o3d.geometry.PointCloud], List[str]], Tuple[List[o3d.geometry.PointCloud], List[o3d.geometry.PointCloud], List[str], List[np.ndarray]]]:
"""
Load both original and transformed point clouds for a single sample.
Args:
input_sample_dir: Directory containing input PLY files
result_sample_dir: Directory containing transform files
return_transforms: Whether to return transform matrices
generation: Optional generation name to use (e.g., 'generation00', 'generation_selected')
Returns:
If return_transforms=False: Tuple of (list of original point clouds, list of registered point clouds, list of part names)
If return_transforms=True: Tuple of (list of original point clouds, list of registered point clouds, list of part names, list of transforms)
Returns empty lists if result directory doesn't exist or no valid point clouds found
"""
sample_name = os.path.basename(input_sample_dir)
# Handle case where result_sample_dir might be the same as input_sample_dir
# (for datasets like nss_ss_v1 where transforms are alongside PLY files)
if result_sample_dir == input_sample_dir:
# Transforms are in the same directory as PLY files
result_sample_dir = input_sample_dir
elif not os.path.exists(result_sample_dir):
# If separate result directory doesn't exist, check if transforms are in input directory
# This handles cases where transforms are alongside PLY files
if os.path.exists(input_sample_dir):
# Check if transform files exist in input directory
transform_files = glob.glob(os.path.join(input_sample_dir, "*transform.txt"))
if transform_files:
# Transforms are in the same directory as PLY files
result_sample_dir = input_sample_dir
else:
logger.warning(f"Result sample directory not found: {result_sample_dir}")
logger.warning(f" Skipping sample {sample_name} - no registered results available")
if return_transforms:
return [], [], [], []
return [], [], []
else:
# Separate result directory exists (normal case)
pass
if not os.path.exists(input_sample_dir):
logger.warning(f"Input sample directory not found: {input_sample_dir}")
if return_transforms:
return [], [], [], []
return [], [], []
# Load and print metadata
metadata = self.load_json_metadata(result_sample_dir)
if metadata:
self.print_metadata(metadata)
else:
logger.debug(f"No metadata JSON file found in {result_sample_dir}")
ply_files = natsort.natsorted(glob.glob(os.path.join(input_sample_dir, "*.ply")))
if not ply_files:
logger.warning(f"No PLY files found in {input_sample_dir}")
if return_transforms:
return [], [], [], []
return [], [], []
original_point_clouds = []
registered_point_clouds = []
part_names = []
transforms_list = [] # Store transforms for potential reapplication
part_colors = self.generate_part_colors(len(ply_files))
skipped_count = 0
for idx, ply_file in enumerate(ply_files):
ply_filename = os.path.basename(ply_file)
part_name = os.path.splitext(ply_filename)[0]
# Try to find transform file by filename pattern first
transform_file = self.find_matching_transform_file(ply_filename, result_sample_dir, generation=generation)
# If not found by filename pattern, try matching by index (for files without part numbers in name)
if transform_file is None:
transform_file = self.find_matching_transform_file_by_index(idx, result_sample_dir, generation=generation)
if transform_file is None:
logger.debug(f"No transform file found for {ply_filename} (index {idx}), skipping")
skipped_count += 1
continue
# Log which type of transform file was found
if "generation_selected" in transform_file:
logger.debug(f"Using generation_selected transform file: {os.path.basename(transform_file)}")
else:
logger.debug(f"Using generation00 transform file: {os.path.basename(transform_file)}")
transform = self.load_pose_from_file(transform_file)
if transform is None:
skipped_count += 1
continue
try:
pcd = o3d.io.read_point_cloud(ply_file)
points = np.asarray(pcd.points)
if len(points) == 0:
logger.debug(f"Empty point cloud for {part_name}, skipping")
skipped_count += 1
continue
# Create original point cloud (before transformation)
pcd_original = o3d.geometry.PointCloud()
pcd_original.points = o3d.utility.Vector3dVector(points.copy())
# Copy colors if available (before outlier removal)
if pcd.has_colors():
pcd_original.colors = pcd.colors
# Apply outlier removal if enabled
pcd_original = self.apply_outlier_removal(pcd_original)
points = np.asarray(pcd_original.points) # Update points after outlier removal
if len(points) == 0:
logger.debug(f"Point cloud for {part_name} is empty after outlier removal, skipping")
skipped_count += 1
continue
# Add normals to original if available (after outlier removal)
normals_orig = None
if pcd_original.has_normals():
normals_orig = np.asarray(pcd_original.normals)
if len(normals_orig) == len(points):
pcd_original.normals = o3d.utility.Vector3dVector(normals_orig.copy())
normals_orig = normals_orig.copy() # Keep a copy for later use
else:
normals_orig = None
# Estimate normals for original if requested
if normals_orig is None and self.estimate_normals:
pcd_temp = o3d.geometry.PointCloud()
pcd_temp.points = o3d.utility.Vector3dVector(points)
# Use adaptive radius based on point cloud scale
adaptive_radius = self.compute_adaptive_normal_radius(points)
pcd_temp.estimate_normals(
search_param=o3d.geometry.KDTreeSearchParamHybrid(
radius=adaptive_radius,
max_nn=30
)
)
pcd_temp.orient_normals_consistent_tangent_plane(k=15)
normals_orig = np.asarray(pcd_temp.normals)
pcd_original.normals = o3d.utility.Vector3dVector(normals_orig)
# Transform points
points_homogeneous = np.hstack([points, np.ones((len(points), 1))])
points_transformed = (transform @ points_homogeneous.T).T[:, :3]
# Create registered point cloud (after transformation)
pcd_transformed = o3d.geometry.PointCloud()
pcd_transformed.points = o3d.utility.Vector3dVector(points_transformed)
# Transform normals if available
normals_transformed = None
if normals_orig is not None:
# Transform normals (only rotation, no translation)
normals_transformed = (transform[:3, :3] @ normals_orig.T).T
pcd_transformed.normals = o3d.utility.Vector3dVector(normals_transformed)
# Estimate normals for transformed if requested and not already available
if normals_transformed is None and self.estimate_normals:
pcd_temp = o3d.geometry.PointCloud()
pcd_temp.points = o3d.utility.Vector3dVector(points_transformed)
# Use adaptive radius based on point cloud scale
adaptive_radius = self.compute_adaptive_normal_radius(points_transformed)
pcd_temp.estimate_normals(
search_param=o3d.geometry.KDTreeSearchParamHybrid(
radius=adaptive_radius,
max_nn=30
)
)
pcd_temp.orient_normals_consistent_tangent_plane(k=15)
normals_transformed = np.asarray(pcd_temp.normals)
pcd_transformed.normals = o3d.utility.Vector3dVector(normals_transformed)
# Set color for both
color = part_colors[idx % len(part_colors)]
pcd_original.paint_uniform_color(color)
pcd_transformed.paint_uniform_color(color)
original_point_clouds.append(pcd_original)
registered_point_clouds.append(pcd_transformed)
part_names.append(part_name)
transforms_list.append(transform) # Store transform for potential reapplication
except Exception as e:
logger.warning(f"Error processing {part_name}: {e}, skipping")
skipped_count += 1
continue
if skipped_count > 0:
logger.info(f" Skipped {skipped_count} parts (missing transforms or errors)")
if return_transforms:
return original_point_clouds, registered_point_clouds, part_names, transforms_list
else:
return original_point_clouds, registered_point_clouds, part_names
def load_generated_point_clouds(self, result_sample_dir: str, generation: Optional[str] = None) -> List[o3d.geometry.PointCloud]:
"""
Load generated point cloud PLY files from the processed directory.
If generation is specified, uses that generation. Otherwise, prioritizes generation_selected files,
then falls back to generation00.
NOTE: Generated point clouds are loaded as-is without any transformation or shift.
They are already in the center of mass frame and should not be modified.
Args:
result_sample_dir: Directory containing generated PLY files
generation: Optional generation name to use (e.g., 'generation00', 'generation_selected')
Returns:
List of generated point clouds (already in center of mass frame, no transformation applied)
"""
if not os.path.exists(result_sample_dir):
return []
generated_pcds = []
# If generation is specified, use it directly
if generation:
patterns = [
f"*{generation}_part*.ply",
f"*{generation}*part*.ply",
]
else:
# Priority: generation_selected > generation00 > other generation files
patterns = [
"*generation_selected_part*.ply",
"*generation00_part*.ply",
"*generation*_part*.ply",
]
for pattern in patterns:
ply_files = sorted(glob.glob(os.path.join(result_sample_dir, pattern)))
if ply_files:
logger.debug(f"Found {len(ply_files)} generated PLY files matching pattern: {pattern}")
# Extract part numbers and sort by part number
ply_files_with_parts = []
for ply_file in ply_files:
basename = os.path.basename(ply_file)
# Extract part number from filename (e.g., part00, part01, etc.)
part_match = re.search(r'part(\d+)', basename)
if part_match:
part_num = int(part_match.group(1))
ply_files_with_parts.append((part_num, ply_file))
else:
# If no part number found, use index
ply_files_with_parts.append((len(ply_files_with_parts), ply_file))
# Sort by part number
ply_files_with_parts.sort(key=lambda x: x[0])
# Generate colors using the same scheme as original/registered
num_parts = len(ply_files_with_parts)
part_colors = self.generate_part_colors(num_parts)
for idx, (part_num, ply_file) in enumerate(ply_files_with_parts):
try:
pcd = o3d.io.read_point_cloud(ply_file)
points = np.asarray(pcd.points)
if len(points) == 0:
logger.debug(f"Empty generated point cloud: {os.path.basename(ply_file)}")
continue
# These are already transformed, so use them as-is
# Add normals if available
if pcd.has_normals():
normals = np.asarray(pcd.normals)
if len(normals) == len(points):
pcd.normals = o3d.utility.Vector3dVector(normals)
# Estimate normals if requested and not available
if not pcd.has_normals() and self.estimate_normals:
# Use adaptive radius based on point cloud scale
adaptive_radius = self.compute_adaptive_normal_radius(points)
pcd.estimate_normals(
search_param=o3d.geometry.KDTreeSearchParamHybrid(
radius=adaptive_radius,
max_nn=30
)
)
pcd.orient_normals_consistent_tangent_plane(k=15)
# Use the same part-wise color scheme as original/registered
# Use the index in the sorted list to get the correct color
color = part_colors[idx % len(part_colors)]
pcd.paint_uniform_color(color)
generated_pcds.append(pcd)
except Exception as e:
logger.warning(f"Error loading generated point cloud {ply_file}: {e}")
continue
# Only use the first pattern that matches
if generated_pcds:
break
return generated_pcds
def load_and_merge_gt_point_clouds(self, result_sample_dir: str) -> Optional[o3d.geometry.PointCloud]:
"""
Load and merge GT (ground truth) point cloud PLY files from the processed directory.
NOTE: GT point clouds are loaded and merged as-is without any transformation or shift.
They are already in the center of mass frame and should not be modified.
Returns:
Merged GT point cloud (already in center of mass frame, no transformation applied), or None if no GT files found
"""
if not os.path.exists(result_sample_dir):
return None
# Look for GT files
gt_pattern = "*gt_part*.ply"
gt_files = sorted(glob.glob(os.path.join(result_sample_dir, gt_pattern)))
if not gt_files:
logger.debug(f"No GT PLY files found matching pattern: {gt_pattern}")
return None
logger.debug(f"Found {len(gt_files)} GT PLY files")
# Extract part numbers and sort by part number
gt_files_with_parts = []
for gt_file in gt_files:
basename = os.path.basename(gt_file)
# Extract part number from filename (e.g., part00, part01, etc.)
part_match = re.search(r'part(\d+)', basename)
if part_match:
part_num = int(part_match.group(1))
gt_files_with_parts.append((part_num, gt_file))
else:
# If no part number found, use index
gt_files_with_parts.append((len(gt_files_with_parts), gt_file))
# Sort by part number
gt_files_with_parts.sort(key=lambda x: x[0])
# Load and merge all GT point clouds
all_points = []
all_normals = []
all_colors = []
# Generate colors using the same scheme as original/registered
num_parts = len(gt_files_with_parts)
part_colors = self.generate_part_colors(num_parts)
for idx, (part_num, gt_file) in enumerate(gt_files_with_parts):
try:
pcd = o3d.io.read_point_cloud(gt_file)
points = np.asarray(pcd.points)
if len(points) == 0:
logger.debug(f"Empty GT point cloud: {os.path.basename(gt_file)}")
continue
# Get normals if available
normals = None
if pcd.has_normals():
normals_array = np.asarray(pcd.normals)
if len(normals_array) == len(points):
normals = normals_array
# Estimate normals if requested and not available
if normals is None and self.estimate_normals:
pcd_temp = o3d.geometry.PointCloud()
pcd_temp.points = o3d.utility.Vector3dVector(points)
# Use adaptive radius based on point cloud scale
adaptive_radius = self.compute_adaptive_normal_radius(points)
pcd_temp.estimate_normals(
search_param=o3d.geometry.KDTreeSearchParamHybrid(
radius=adaptive_radius,
max_nn=30
)
)
pcd_temp.orient_normals_consistent_tangent_plane(k=15)
normals = np.asarray(pcd_temp.normals)
# Get colors if available, otherwise use part color
if pcd.has_colors():
colors_array = np.asarray(pcd.colors)
if len(colors_array) == len(points):
colors = colors_array
else:
colors = np.tile(part_colors[idx % len(part_colors)], (len(points), 1))
else:
colors = np.tile(part_colors[idx % len(part_colors)], (len(points), 1))
all_points.append(points)
if normals is not None:
all_normals.append(normals)
all_colors.append(colors)
except Exception as e:
logger.warning(f"Error loading GT point cloud {gt_file}: {e}")
continue
if not all_points:
return None
# Merge all point clouds
merged_pcd = o3d.geometry.PointCloud()
merged_points = np.concatenate(all_points, axis=0)
merged_pcd.points = o3d.utility.Vector3dVector(merged_points)
if all_normals:
merged_normals = np.concatenate(all_normals, axis=0)
merged_pcd.normals = o3d.utility.Vector3dVector(merged_normals)
merged_colors = np.concatenate(all_colors, axis=0)
merged_pcd.colors = o3d.utility.Vector3dVector(merged_colors)
logger.info(f"Merged {len(gt_files_with_parts)} GT parts into single point cloud with {len(merged_points)} points")
return merged_pcd
def generate_part_colors(self, num_parts: int) -> List[List[float]]:
"""Generate distinct colors for each part."""
colors = []
for i in range(num_parts):
color_idx = i % len(CMAP_DEFAULT)
colors.append(CMAP_DEFAULT[color_idx])
return colors
def get_sequence_samples(self, sequence: str) -> List[str]:
"""Get list of sample directory names for a sequence."""
if sequence in self.sequence_samples_cache:
return self.sequence_samples_cache[sequence]
input_seq_dir, _ = self.find_sequence_directories(self.input_dir, sequence)
if input_seq_dir is None:
return []
sample_dirs = self.find_sample_directories(input_seq_dir)
sample_names = [os.path.basename(d) for d in sample_dirs]
self.sequence_samples_cache[sequence] = sample_names
return sample_names
def get_available_sequences(self) -> List[str]:
"""Get list of available sequences."""
sequences = []
def has_direct_samples(dir_path: str) -> bool:
"""Check if directory directly contains sample directories (not nested)."""
sample_dirs = self.find_sample_directories(dir_path)
return len(sample_dirs) > 0
def find_sequences_recursive(root_dir: str, current_path: str = "", max_depth: int = 5) -> List[str]:
"""Recursively find sequences (directories containing samples)."""
if max_depth <= 0:
return []
found_sequences = []
try:
if not os.path.isdir(root_dir):
return []
# Check if current directory contains samples
if has_direct_samples(root_dir):
if current_path:
found_sequences.append(current_path)
else:
# Use basename if no path built yet
found_sequences.append(os.path.basename(root_dir))
# Recursively search subdirectories
for item in os.listdir(root_dir):
item_path = os.path.join(root_dir, item)
if os.path.isdir(item_path):
# Skip common non-sequence directories
if item in ['.', '..', '__pycache__', '.git', 'data_split']:
continue
# Build path for nested structure
if current_path:
new_path = f"{current_path}/{item}"
else:
new_path = item
# Recursively search
sub_sequences = find_sequences_recursive(item_path, new_path, max_depth - 1)
found_sequences.extend(sub_sequences)
except PermissionError:
pass
except Exception as e:
logger.debug(f"Error searching {root_dir}: {e}")
return found_sequences
# First, check direct subdirectories for sequences (directly contain samples)
for item in os.listdir(self.input_dir):
item_path = os.path.join(self.input_dir, item)
if os.path.isdir(item_path) and has_direct_samples(item_path):
sequences.append(item)
# If no sequences found, check one level deeper (for nested structure like scannet_test_v1/scannet_test_v1/)
if not sequences:
for item in os.listdir(self.input_dir):
item_path = os.path.join(self.input_dir, item)
if os.path.isdir(item_path):
# Check subdirectories one level deeper
try:
for subitem in os.listdir(item_path):
subitem_path = os.path.join(item_path, subitem)
if os.path.isdir(subitem_path) and has_direct_samples(subitem_path):
# Use nested path as sequence name
sequence_name = f"{item}/{subitem}" if item != subitem else subitem
if sequence_name not in sequences:
sequences.append(sequence_name)
except PermissionError:
pass
# If still no sequences found, do recursive search (for deeply nested structures like modelnet/modelnet/category/object_id)
if not sequences:
sequences = find_sequences_recursive(self.input_dir)
# Remove duplicates while preserving order
seen = set()
unique_sequences = []
for seq in sequences:
if seq not in seen:
seen.add(seq)
unique_sequences.append(seq)
sequences = unique_sequences
return sorted(sequences)
def find_next_sample(self) -> Tuple[Optional[str], Optional[int]]:
"""Find the next sample to load."""
if self.current_sequence is None or self.current_sample_idx is None:
return None, None
samples = self.get_sequence_samples(self.current_sequence)
if not samples:
return None, None
if self.current_sample_idx + 1 < len(samples):
return self.current_sequence, self.current_sample_idx + 1
# Move to next sequence
sequences = self.get_available_sequences()
try:
current_seq_idx = sequences.index(self.current_sequence)
if current_seq_idx + 1 < len(sequences):
next_sequence = sequences[current_seq_idx + 1]
next_samples = self.get_sequence_samples(next_sequence)
if next_samples:
return next_sequence, 0
# Loop back to first
first_sequence = sequences[0]
first_samples = self.get_sequence_samples(first_sequence)
if first_samples:
return first_sequence, 0
except ValueError:
pass
return None, None
def find_next_sequence(self) -> Tuple[Optional[str], Optional[int]]:
"""Find the first sample of the next sequence to load."""
if self.current_sequence is None:
return None, None
sequences = self.get_available_sequences()
if not sequences:
return None, None
try:
current_seq_idx = sequences.index(self.current_sequence)
if current_seq_idx + 1 < len(sequences):
next_sequence = sequences[current_seq_idx + 1]
else:
# If we're at the last sequence, loop back to first
next_sequence = sequences[0]
# Get first sample of next sequence
next_samples = self.get_sequence_samples(next_sequence)
if next_samples:
return next_sequence, 0
except ValueError:
pass
return None, None
def reload_current_sample_with_generation(self, generation: str) -> bool:
"""
Reload the current sample with a specific generation.
Args:
generation: Generation name to load (e.g., 'generation00', 'generation_selected')
Returns:
True if successfully loaded, False otherwise
"""
if self.current_sequence is None or self.current_sample_idx is None:
return False
try:
samples = self.get_sequence_samples(self.current_sequence)
if not samples or self.current_sample_idx >= len(samples):
return False
sample_name = samples[self.current_sample_idx]
input_seq_dir, result_seq_dir = self.find_sequence_directories(self.input_dir, self.current_sequence)
if input_seq_dir is None or result_seq_dir is None:
return False
input_sample_dir = os.path.join(input_seq_dir, sample_name)
# Determine result sample directory name
if sample_name.startswith('sample_'):
result_sample_dir = os.path.join(result_seq_dir, f"{sample_name}_processed")
if not os.path.exists(result_sample_dir) and result_seq_dir == input_seq_dir:
result_sample_dir = input_sample_dir
else:
result_sample_dir = os.path.join(result_seq_dir, f"{sample_name}_processed")
if not os.path.exists(result_sample_dir):
result_sample_dir = os.path.join(result_seq_dir, sample_name)
if not os.path.exists(result_sample_dir) and result_seq_dir == input_seq_dir:
result_sample_dir = input_sample_dir
# Load point clouds with the specified generation
original_pcds, registered_pcds, part_names, transforms_list = self.load_sample_point_clouds(
input_sample_dir, result_sample_dir, return_transforms=True, generation=generation
)
if not registered_pcds:
return False
# Load generated point clouds if enabled
if hasattr(self, 'show_generated') and self.show_generated:
self.generated_gt_pcd = self.load_and_merge_gt_point_clouds(result_sample_dir)
self.generated_pcds = self.load_generated_point_clouds(result_sample_dir, generation=generation)
else:
self.generated_pcds = []
self.generated_gt_pcd = None
# Create random yaw rotated versions
original_pcds_before_translation = []
for pcd in original_pcds:
pcd_copy = o3d.geometry.PointCloud()
pcd_copy.points = o3d.utility.Vector3dVector(np.asarray(pcd.points).copy())
if pcd.has_normals():
pcd_copy.normals = o3d.utility.Vector3dVector(np.asarray(pcd.normals).copy())
if pcd.has_colors():
pcd_copy.colors = pcd.colors
original_pcds_before_translation.append(pcd_copy)
self.random_yaw_pcds = self.create_random_yaw_point_clouds(
original_pcds_before_translation,
generated_pcds=self.generated_pcds if hasattr(self, 'show_generated') and self.show_generated else None
)
# Compute center of mass and translate
center_of_mass = None
merged_input_points = self.merge_input_point_clouds(original_pcds)
if merged_input_points is not None:
_, center_of_mass = self.center_pcd(merged_input_points)
if center_of_mass is not None:
self.original_pcds = [self.translate_point_cloud(pcd, center_of_mass) for pcd in original_pcds]
self.registered_pcds = []
for idx, (pcd_original, transform) in enumerate(zip(self.original_pcds, transforms_list)):
points = np.asarray(pcd_original.points)
points_homogeneous = np.hstack([points, np.ones((len(points), 1))])
points_transformed = (transform @ points_homogeneous.T).T[:, :3]
pcd_registered = o3d.geometry.PointCloud()
pcd_registered.points = o3d.utility.Vector3dVector(points_transformed)
if pcd_original.has_normals():
normals_orig = np.asarray(pcd_original.normals)
normals_transformed = (transform[:3, :3] @ normals_orig.T).T
pcd_registered.normals = o3d.utility.Vector3dVector(normals_transformed)
if pcd_original.has_colors():
pcd_registered.colors = pcd_original.colors
else:
color = self.generate_part_colors(len(self.original_pcds))[idx % len(self.original_pcds)]
pcd_registered.paint_uniform_color(color)
self.registered_pcds.append(pcd_registered)
else:
self.original_pcds = original_pcds
self.registered_pcds = registered_pcds
# Select point clouds based on view mode
if self.view_mode == "generated" and self.generated_pcds:
point_clouds = self.generated_pcds.copy()
elif self.view_mode == "gt" and self.generated_gt_pcd is not None:
point_clouds = [self.generated_gt_pcd]
elif self.view_mode == "random_yaw" and self.random_yaw_pcds:
point_clouds = self.random_yaw_pcds.copy()
elif self.view_mode == "original":
point_clouds = self.original_pcds
else:
point_clouds = self.registered_pcds
# Update visualization
if self.vis is not None:
self.vis.clear_geometries()
for pcd in point_clouds:
self.vis.add_geometry(pcd, False)
self.vis.update_geometry(pcd)
if not self.no_coordinate_frame:
coordinate_frame = o3d.geometry.TriangleMesh.create_coordinate_frame(size=1.0)
self.vis.add_geometry(coordinate_frame, False)
self.vis.update_geometry(coordinate_frame)
self._center_and_fit_view(point_clouds)
self.vis.update_renderer()
self.colored_pcds = point_clouds
logger.info(f"Reloaded sample with generation: {generation}")
return True
except Exception as e:
logger.warning(f"Error reloading sample with generation {generation}: {e}")
return False
def cycle_to_next_generation(self) -> bool:
"""
Cycle to the next available generation for the current sample.
If we've cycled through all generations, moves to the next sample.
Returns:
True if successfully cycled, False otherwise
"""
if not self.cycle_generations or not self.available_generations:
return False
# Check if we're at the last generation
if self.current_generation_idx + 1 >= len(self.available_generations):
# Move to next sample instead
logger.info("Cycled through all generations, moving to next sample...")
return self.load_next_sample()
# Move to next generation
self.current_generation_idx += 1
generation = self.available_generations[self.current_generation_idx]
logger.info(f"Cycling to generation {self.current_generation_idx + 1}/{len(self.available_generations)}: {generation}")
return self.reload_current_sample_with_generation(generation)
def load_next_sample(self, max_attempts: int = 10):
"""Load and visualize the next sample."""
if max_attempts <= 0:
logger.error("Maximum attempts reached. Could not find a valid sample to load.")
return False
next_sequence, next_sample_idx = self.find_next_sample()
if next_sequence is None or next_sample_idx is None:
logger.warning("No next sample available")
return False
try:
self.current_sequence = next_sequence
self.current_sample_idx = next_sample_idx
samples = self.get_sequence_samples(next_sequence)
sample_name = samples[next_sample_idx]
input_seq_dir, result_seq_dir = self.find_sequence_directories(self.input_dir, next_sequence)
if input_seq_dir is None or result_seq_dir is None:
logger.warning(f"Could not find sequence directories for {next_sequence}")
# Try next sample
return self.load_next_sample(max_attempts - 1)
input_sample_dir = os.path.join(input_seq_dir, sample_name)
# Determine result sample directory name
# For sample_* format: use sample_*_processed
# For other formats (like fracture_*): try both with and without _processed suffix
# Also handle case where transforms are in the same directory as PLY files
if sample_name.startswith('sample_'):
result_sample_dir = os.path.join(result_seq_dir, f"{sample_name}_processed")
# If _processed doesn't exist, check if transforms are in the same directory
if not os.path.exists(result_sample_dir) and result_seq_dir == input_seq_dir:
result_sample_dir = input_sample_dir
else:
# Try with _processed suffix first, then without
result_sample_dir = os.path.join(result_seq_dir, f"{sample_name}_processed")
if not os.path.exists(result_sample_dir):
result_sample_dir = os.path.join(result_seq_dir, sample_name)
# If still doesn't exist and result_seq_dir == input_seq_dir, use input_sample_dir
if not os.path.exists(result_sample_dir) and result_seq_dir == input_seq_dir:
result_sample_dir = input_sample_dir
# Initialize available generations if cycling is enabled
if self.cycle_generations:
self.available_generations = self.get_available_generations(result_sample_dir)
if self.available_generations:
self.current_generation_idx = 0
logger.info(f"Found {len(self.available_generations)} available generation(s): {self.available_generations}")
else:
logger.warning(f"No generations found for sample {sample_name}, disabling generation cycling")
self.cycle_generations = False
# Load point clouds (both original and registered) and get transforms
# Use current generation if cycling is enabled
current_generation = None
if self.cycle_generations and self.available_generations:
current_generation = self.available_generations[self.current_generation_idx]
original_pcds, registered_pcds, part_names, transforms_list = self.load_sample_point_clouds(
input_sample_dir, result_sample_dir, return_transforms=True, generation=current_generation
)
if not registered_pcds:
logger.warning(f"No point clouds loaded for sample {sample_name}")
# Try to load next sample automatically
logger.info("Attempting to load next available sample...")
return self.load_next_sample(max_attempts - 1) # Recursively try next sample with limit
# Load generated point clouds if enabled
# NOTE: Generated and GT point clouds are loaded as-is without any transformation
# They are already in center of mass frame and will be used directly for visualization
if hasattr(self, 'show_generated') and self.show_generated:
# Load GT point cloud (for visualization only, not for center of mass calculation)
self.generated_gt_pcd = self.load_and_merge_gt_point_clouds(result_sample_dir)
if self.generated_gt_pcd is not None:
logger.info(f"Loaded GT point cloud (for visualization only, NOT used for center of mass)")
# Load generated point clouds (no transformation applied)
self.generated_pcds = self.load_generated_point_clouds(result_sample_dir, generation=current_generation)
if self.generated_pcds:
logger.info(f"Loaded {len(self.generated_pcds)} generated point cloud(s) (no transformation applied)")
else:
self.generated_pcds = []
self.generated_gt_pcd = None
# Create random yaw rotated versions of input point clouds
# Clone original point clouds BEFORE translation for random yaw view
# (These are the untranslated original point clouds from load_sample_point_clouds)
original_pcds_before_translation = []
for pcd in original_pcds:
# Create a deep copy
pcd_copy = o3d.geometry.PointCloud()
pcd_copy.points = o3d.utility.Vector3dVector(np.asarray(pcd.points).copy())
if pcd.has_normals():
pcd_copy.normals = o3d.utility.Vector3dVector(np.asarray(pcd.normals).copy())
if pcd.has_colors():
pcd_copy.colors = pcd.colors
original_pcds_before_translation.append(pcd_copy)
# Create random yaw rotated point clouds (centered at origin, random yaw per part)
# Skip rotation for the part corresponding to the generated part with most points
self.random_yaw_pcds = self.create_random_yaw_point_clouds(
original_pcds_before_translation,
generated_pcds=self.generated_pcds if hasattr(self, 'show_generated') and self.show_generated else None
)
# Always use merged input point clouds for center of mass computation
center_of_mass = None
merged_input_points = self.merge_input_point_clouds(original_pcds)
if merged_input_points is not None:
_, center_of_mass = self.center_pcd(merged_input_points)
logger.info(f"Using merged input point clouds center of mass: {center_of_mass}")
# Translate original point clouds to center of mass frame first
# Then re-apply transforms to get registered point clouds in center of mass frame
# Generated and GT are already in center of mass frame, so don't translate them
if center_of_mass is not None:
# Translate original point clouds to center of mass frame
self.original_pcds = [self.translate_point_cloud(pcd, center_of_mass) for pcd in original_pcds]
# Re-apply transforms to translated original point clouds to get registered in center of mass frame
self.registered_pcds = []
for idx, (pcd_original, transform) in enumerate(zip(self.original_pcds, transforms_list)):
# Apply transform to the translated original point cloud
points = np.asarray(pcd_original.points)
points_homogeneous = np.hstack([points, np.ones((len(points), 1))])
points_transformed = (transform @ points_homogeneous.T).T[:, :3]
pcd_registered = o3d.geometry.PointCloud()
pcd_registered.points = o3d.utility.Vector3dVector(points_transformed)
# Transform normals if available
if pcd_original.has_normals():
normals_orig = np.asarray(pcd_original.normals)
normals_transformed = (transform[:3, :3] @ normals_orig.T).T
pcd_registered.normals = o3d.utility.Vector3dVector(normals_transformed)
# Copy colors
if pcd_original.has_colors():
pcd_registered.colors = pcd_original.colors
else:
# Use same color as original
color = self.generate_part_colors(len(self.original_pcds))[idx % len(self.original_pcds)]
pcd_registered.paint_uniform_color(color)
self.registered_pcds.append(pcd_registered)
else:
# If no center available, keep original registered point clouds
self.original_pcds = original_pcds
self.registered_pcds = registered_pcds
# Select point clouds based on view mode (use translated versions)
if self.view_mode == "generated" and self.generated_pcds:
point_clouds = self.generated_pcds.copy() # Only generated, no GT
elif self.view_mode == "gt" and self.generated_gt_pcd is not None:
point_clouds = [self.generated_gt_pcd] # Only GT
elif self.view_mode == "random_yaw" and self.random_yaw_pcds:
point_clouds = self.random_yaw_pcds.copy() # Random yaw rotated input point clouds
elif self.view_mode == "original":
point_clouds = self.original_pcds # Use translated original point clouds
else:
point_clouds = self.registered_pcds # Use translated registered point clouds
# Clear current visualization
if self.vis is not None:
self.vis.clear_geometries()
# Add new geometries
if self.vis is not None:
for pcd in point_clouds:
self.vis.add_geometry(pcd, False)
self.vis.update_geometry(pcd)
if not self.no_coordinate_frame:
coordinate_frame = o3d.geometry.TriangleMesh.create_coordinate_frame(size=1.0)
self.vis.add_geometry(coordinate_frame, False)
self.vis.update_geometry(coordinate_frame)
self._center_and_fit_view(point_clouds)
self.vis.update_renderer()
self.colored_pcds = point_clouds
logger.info("=" * 60)
logger.info(f"LOADED NEXT SAMPLE: Sequence {next_sequence}, Sample {sample_name}")
logger.info(f" Parts: {len(part_names)}")
view_name_map = {
"original": "Original (untransformed)",
"registered": "Registered (transformed)",
"generated": "Generated (from processed directory)",
"gt": "GT (ground truth)",
"random_yaw": "Input with Random Yaw Rotation"
}
logger.info(f" View: {view_name_map.get(self.view_mode, 'Unknown')}")
logger.info("=" * 60)
return True
except FileNotFoundError as e:
logger.warning(f"File not found: {e}")
# Try next sample
return self.load_next_sample(max_attempts - 1)
except ValueError as e:
logger.warning(f"Value error: {e}")
# Try next sample
return self.load_next_sample(max_attempts - 1)
except Exception as e:
logger.warning(f"Error loading sample: {e}")
import traceback
logger.debug(f"Full traceback:\n{traceback.format_exc()}")
# Try next sample
return self.load_next_sample(max_attempts - 1)
def load_next_sequence(self, max_attempts: int = 50):
"""
Load and visualize the first sample of the next sequence.
Tries multiple samples in the target sequence before moving to the next sequence.
"""
if max_attempts <= 0:
logger.error("Maximum attempts reached. Could not find a valid sequence to load.")
return False
next_sequence, next_sample_idx = self.find_next_sequence()
if next_sequence is None or next_sample_idx is None:
logger.warning("No next sequence available")
return False
# Try multiple samples in the target sequence before giving up
samples = self.get_sequence_samples(next_sequence)
if not samples:
logger.warning(f"No samples found in sequence {next_sequence}, trying next sequence...")
# Update current sequence to skip this empty sequence
self.current_sequence = next_sequence
self.current_sample_idx = 0
return self.load_next_sequence(max_attempts - 1)
# Try up to all samples in the target sequence (or max_attempts, whichever is smaller)
max_samples_to_try = min(len(samples), max_attempts)
for attempt in range(max_samples_to_try):
sample_idx = (next_sample_idx + attempt) % len(samples)
sample_name = samples[sample_idx]
try:
input_seq_dir, result_seq_dir = self.find_sequence_directories(self.input_dir, next_sequence)
if input_seq_dir is None or result_seq_dir is None:
logger.warning(f"Could not find sequence directories for {next_sequence}")
# Try next sequence
self.current_sequence = next_sequence
self.current_sample_idx = sample_idx
return self.load_next_sequence(max_attempts - max_samples_to_try)
input_sample_dir = os.path.join(input_seq_dir, sample_name)
# Determine result sample directory name
# For sample_* format: use sample_*_processed
# For other formats (like fracture_*): try both with and without _processed suffix
if sample_name.startswith('sample_'):
result_sample_dir = os.path.join(result_seq_dir, f"{sample_name}_processed")
else:
# Try with _processed suffix first, then without
result_sample_dir = os.path.join(result_seq_dir, f"{sample_name}_processed")
if not os.path.exists(result_sample_dir):
result_sample_dir = os.path.join(result_seq_dir, sample_name)
# Check if result directory exists before trying to load
if not os.path.exists(result_sample_dir):
logger.debug(f"Result sample directory not found: {result_sample_dir}, trying next sample...")
continue # Try next sample in this sequence
# Initialize available generations if cycling is enabled
if self.cycle_generations:
self.available_generations = self.get_available_generations(result_sample_dir)
if self.available_generations:
self.current_generation_idx = 0
logger.info(f"Found {len(self.available_generations)} available generation(s): {self.available_generations}")
else:
logger.warning(f"No generations found for sample {sample_name}, disabling generation cycling")
self.cycle_generations = False
# Load point clouds (both original and registered) and get transforms
# Use current generation if cycling is enabled
current_generation = None
if self.cycle_generations and self.available_generations:
current_generation = self.available_generations[self.current_generation_idx]
original_pcds, registered_pcds, part_names, transforms_list = self.load_sample_point_clouds(
input_sample_dir, result_sample_dir, return_transforms=True, generation=current_generation
)
if not registered_pcds:
logger.debug(f"No point clouds loaded for sample {sample_name}, trying next sample...")
continue # Try next sample in this sequence
# Successfully loaded point clouds - update state and proceed with visualization
self.current_sequence = next_sequence
self.current_sample_idx = sample_idx
# Load generated point clouds if enabled
# NOTE: Generated and GT point clouds are loaded as-is without any transformation
# They are already in center of mass frame and will be used directly for visualization
if hasattr(self, 'show_generated') and self.show_generated:
# Load GT point cloud (for visualization only, not for center of mass calculation)
self.generated_gt_pcd = self.load_and_merge_gt_point_clouds(result_sample_dir)
if self.generated_gt_pcd is not None:
logger.info(f"Loaded GT point cloud (for visualization only, NOT used for center of mass)")
# Load generated point clouds (no transformation applied)
self.generated_pcds = self.load_generated_point_clouds(result_sample_dir, generation=current_generation)
if self.generated_pcds:
logger.info(f"Loaded {len(self.generated_pcds)} generated point cloud(s) (no transformation applied)")
else:
self.generated_pcds = []
self.generated_gt_pcd = None
# Always use merged input point clouds for center of mass computation
center_of_mass = None
merged_input_points = self.merge_input_point_clouds(original_pcds)
if merged_input_points is not None:
_, center_of_mass = self.center_pcd(merged_input_points)
logger.info(f"Using merged input point clouds center of mass: {center_of_mass}")
# Translate original point clouds to center of mass frame first
# Then re-apply transforms to get registered point clouds in center of mass frame
# Generated and GT are already in center of mass frame, so don't translate them
if center_of_mass is not None:
# Translate original point clouds to center of mass frame
self.original_pcds = [self.translate_point_cloud(pcd, center_of_mass) for pcd in original_pcds]
# Re-apply transforms to translated original point clouds to get registered in center of mass frame
self.registered_pcds = []
for idx, (pcd_original, transform) in enumerate(zip(self.original_pcds, transforms_list)):
# Apply transform to the translated original point cloud
points = np.asarray(pcd_original.points)
points_homogeneous = np.hstack([points, np.ones((len(points), 1))])
points_transformed = (transform @ points_homogeneous.T).T[:, :3]
pcd_registered = o3d.geometry.PointCloud()
pcd_registered.points = o3d.utility.Vector3dVector(points_transformed)
# Transform normals if available
if pcd_original.has_normals():
normals_orig = np.asarray(pcd_original.normals)
normals_transformed = (transform[:3, :3] @ normals_orig.T).T
pcd_registered.normals = o3d.utility.Vector3dVector(normals_transformed)
# Copy colors
if pcd_original.has_colors():
pcd_registered.colors = pcd_original.colors
else:
# Use same color as original
color = self.generate_part_colors(len(self.original_pcds))[idx % len(self.original_pcds)]
pcd_registered.paint_uniform_color(color)
self.registered_pcds.append(pcd_registered)
else:
# If no center available, keep original registered point clouds
self.original_pcds = original_pcds
self.registered_pcds = registered_pcds
# Create random yaw rotated versions of input point clouds
# Clone original point clouds before translation for random yaw view
original_pcds_before_translation = []
for pcd in original_pcds:
# Create a deep copy
pcd_copy = o3d.geometry.PointCloud()
pcd_copy.points = o3d.utility.Vector3dVector(np.asarray(pcd.points).copy())
if pcd.has_normals():
pcd_copy.normals = o3d.utility.Vector3dVector(np.asarray(pcd.normals).copy())
if pcd.has_colors():
pcd_copy.colors = pcd.colors
original_pcds_before_translation.append(pcd_copy)
# Create random yaw rotated point clouds (centered at origin, random yaw per part)
# Skip rotation for the part corresponding to the generated part with most points
self.random_yaw_pcds = self.create_random_yaw_point_clouds(
original_pcds_before_translation,
generated_pcds=self.generated_pcds if hasattr(self, 'show_generated') and self.show_generated else None
)
# Select point clouds based on view mode (use translated versions)
if self.view_mode == "generated" and self.generated_pcds:
point_clouds = self.generated_pcds.copy() # Only generated, no GT
elif self.view_mode == "gt" and self.generated_gt_pcd is not None:
point_clouds = [self.generated_gt_pcd] # Only GT
elif self.view_mode == "random_yaw" and self.random_yaw_pcds:
point_clouds = self.random_yaw_pcds.copy() # Random yaw rotated input point clouds
elif self.view_mode == "original":
point_clouds = self.original_pcds # Use translated original point clouds
else:
point_clouds = self.registered_pcds # Use translated registered point clouds
# Clear current visualization
if self.vis is not None:
self.vis.clear_geometries()
# Add new geometries
if self.vis is not None:
for pcd in point_clouds:
self.vis.add_geometry(pcd, False)
self.vis.update_geometry(pcd)
if not self.no_coordinate_frame:
coordinate_frame = o3d.geometry.TriangleMesh.create_coordinate_frame(size=1.0)
self.vis.add_geometry(coordinate_frame, False)
self.vis.update_geometry(coordinate_frame)
self._center_and_fit_view(point_clouds)
self.vis.update_renderer()
self.colored_pcds = point_clouds
logger.info("=" * 60)
logger.info(f"JUMPED TO NEXT SEQUENCE: Sequence {next_sequence}, Sample {sample_name}")
logger.info(f" Parts: {len(part_names)}")
view_name_map = {
"original": "Original (untransformed)",
"registered": "Registered (transformed)",
"generated": "Generated (from processed directory)",
"gt": "GT (ground truth)"
}
logger.info(f" View: {view_name_map.get(self.view_mode, 'Unknown')}")
logger.info("=" * 60)
return True
except Exception as e:
logger.debug(f"Error loading sample {sample_name}: {e}")
import traceback
logger.debug(f"Full traceback:\n{traceback.format_exc()}")
continue # Try next sample in this sequence
# If we've exhausted all samples in this sequence, try the next sequence
logger.info(f"Tried {max_samples_to_try} sample(s) in sequence {next_sequence} but none had valid results. Moving to next sequence...")
self.current_sequence = next_sequence
self.current_sample_idx = len(samples) - 1 # Mark as having tried all samples
return self.load_next_sequence(max_attempts - max_samples_to_try)
def toggle_transform_view(self):
"""Cycle between original, registered, generated, GT, and random_yaw point cloud views."""
# Cycle through: original -> registered -> generated -> gt -> random_yaw -> original
if self.view_mode == "original":
if self.registered_pcds:
self.view_mode = "registered"
point_clouds = self.registered_pcds
view_name = "Registered (transformed)"
elif self.generated_pcds:
self.view_mode = "generated"
point_clouds = self.generated_pcds.copy()
view_name = "Generated (from processed directory)"
elif self.generated_gt_pcd is not None:
self.view_mode = "gt"
point_clouds = [self.generated_gt_pcd]
view_name = "GT (ground truth)"
elif self.random_yaw_pcds:
self.view_mode = "random_yaw"
point_clouds = self.random_yaw_pcds.copy()
view_name = "Input with Random Yaw Rotation"
else:
logger.warning("No other views available")
return
elif self.view_mode == "registered":
if self.generated_pcds:
self.view_mode = "generated"
point_clouds = self.generated_pcds.copy()
view_name = "Generated (from processed directory)"
elif self.generated_gt_pcd is not None:
self.view_mode = "gt"
point_clouds = [self.generated_gt_pcd]
view_name = "GT (ground truth)"
elif self.random_yaw_pcds:
self.view_mode = "random_yaw"
point_clouds = self.random_yaw_pcds.copy()
view_name = "Input with Random Yaw Rotation"
elif self.original_pcds:
self.view_mode = "original"
point_clouds = self.original_pcds
view_name = "Original (untransformed)"
else:
logger.warning("No other views available")
return
elif self.view_mode == "generated":
if self.generated_gt_pcd is not None:
self.view_mode = "gt"
point_clouds = [self.generated_gt_pcd]
view_name = "GT (ground truth)"
elif self.random_yaw_pcds:
self.view_mode = "random_yaw"
point_clouds = self.random_yaw_pcds.copy()
view_name = "Input with Random Yaw Rotation"
elif self.original_pcds:
self.view_mode = "original"
point_clouds = self.original_pcds
view_name = "Original (untransformed)"
elif self.registered_pcds:
self.view_mode = "registered"
point_clouds = self.registered_pcds
view_name = "Registered (transformed)"
else:
logger.warning("No other views available")
return
elif self.view_mode == "gt":
if self.random_yaw_pcds:
self.view_mode = "random_yaw"
point_clouds = self.random_yaw_pcds.copy()
view_name = "Input with Random Yaw Rotation"
elif self.original_pcds:
self.view_mode = "original"
point_clouds = self.original_pcds
view_name = "Original (untransformed)"
elif self.registered_pcds:
self.view_mode = "registered"
point_clouds = self.registered_pcds
view_name = "Registered (transformed)"
elif self.generated_pcds:
self.view_mode = "generated"
point_clouds = self.generated_pcds.copy()
view_name = "Generated (from processed directory)"
else:
logger.warning("No other views available")
return
else: # random_yaw
if self.original_pcds:
self.view_mode = "original"
point_clouds = self.original_pcds
view_name = "Original (untransformed)"
elif self.registered_pcds:
self.view_mode = "registered"
point_clouds = self.registered_pcds
view_name = "Registered (transformed)"
elif self.generated_pcds:
self.view_mode = "generated"
point_clouds = self.generated_pcds.copy()
view_name = "Generated (from processed directory)"
elif self.generated_gt_pcd is not None:
self.view_mode = "gt"
point_clouds = [self.generated_gt_pcd]
view_name = "GT (ground truth)"
else:
logger.warning("No other views available")
return
logger.info(f"Switched to {view_name} view")
# Update visualization if visualizer exists
if self.vis is not None:
# Clear all geometries
self.vis.clear_geometries()
# Add new point clouds
for pcd in point_clouds:
self.vis.add_geometry(pcd, False)
self.vis.update_geometry(pcd)
# Add coordinate frame if enabled
if not self.no_coordinate_frame:
coordinate_frame = o3d.geometry.TriangleMesh.create_coordinate_frame(size=1.0)
self.vis.add_geometry(coordinate_frame, False)
self.vis.update_geometry(coordinate_frame)
# Don't reset view - preserve current camera position
self.vis.update_renderer()
self.colored_pcds = point_clouds
def toggle_background_color(self):
"""Toggle between white and black background colors."""
self.use_white_background = not self.use_white_background
if self.use_white_background:
new_background = self.white_background
logger.info("Switched to white background")
else:
new_background = self.black_background
logger.info("Switched to black background")
self.background_color = new_background
if self.vis is not None:
render_option = self.vis.get_render_option()
render_option.background_color = np.array(new_background)
def _center_and_fit_view(self, point_clouds: List[o3d.geometry.PointCloud]):
"""Center and fit the view to show all point clouds properly."""
if not point_clouds:
return
view_control = self.vis.get_view_control()
all_points = []
for pcd in point_clouds:
points = np.asarray(pcd.points)
if len(points) > 0:
all_points.append(points)
if not all_points:
return
all_points = np.concatenate(all_points, axis=0)
center = np.mean(all_points, axis=0)
min_coords = np.min(all_points, axis=0)
max_coords = np.max(all_points, axis=0)
extent = max_coords - min_coords
max_extent = np.max(extent)
front = np.array([0.0, 0.0, 1.0])
up = np.array([0.0, 1.0, 0.0])
lookat = center
view_control.set_front(front)
view_control.set_lookat(lookat)
view_control.set_up(up)
view_control.set_zoom(0.8)
def visualize_sample(self, sequence: str, sample_idx: int = 0):
"""Visualize a specific sample."""
self.current_sequence = sequence
self.current_sample_idx = sample_idx
samples = self.get_sequence_samples(sequence)
if not samples:
raise ValueError(f"No samples found for sequence {sequence}")
if sample_idx >= len(samples):
raise ValueError(f"Sample index {sample_idx} out of range for sequence {sequence}")
sample_name = samples[sample_idx]
input_seq_dir, result_seq_dir = self.find_sequence_directories(self.input_dir, sequence)
if input_seq_dir is None or result_seq_dir is None:
raise ValueError(f"Could not find sequence directories for {sequence}")
input_sample_dir = os.path.join(input_seq_dir, sample_name)
# Determine result sample directory name
# For sample_* format: use sample_*_processed
# For other formats (like fracture_*): try both with and without _processed suffix
# Also handle case where transforms are in the same directory as PLY files
if sample_name.startswith('sample_'):
result_sample_dir = os.path.join(result_seq_dir, f"{sample_name}_processed")
# If _processed doesn't exist, check if transforms are in the same directory
if not os.path.exists(result_sample_dir) and result_seq_dir == input_seq_dir:
result_sample_dir = input_sample_dir
else:
# Try with _processed suffix first, then without
result_sample_dir = os.path.join(result_seq_dir, f"{sample_name}_processed")
if not os.path.exists(result_sample_dir):
result_sample_dir = os.path.join(result_seq_dir, sample_name)
# If still doesn't exist and result_seq_dir == input_seq_dir, use input_sample_dir
if not os.path.exists(result_sample_dir) and result_seq_dir == input_seq_dir:
result_sample_dir = input_sample_dir
# Initialize available generations if cycling is enabled
if self.cycle_generations:
self.available_generations = self.get_available_generations(result_sample_dir)
if self.available_generations:
self.current_generation_idx = 0
logger.info(f"Found {len(self.available_generations)} available generation(s): {self.available_generations}")
else:
logger.warning(f"No generations found for sample {sample_name}, disabling generation cycling")
self.cycle_generations = False
# Load point clouds (both original and registered) and get transforms
# Use current generation if cycling is enabled
current_generation = None
if self.cycle_generations and self.available_generations:
current_generation = self.available_generations[self.current_generation_idx]
original_pcds, registered_pcds, part_names, transforms_list = self.load_sample_point_clouds(
input_sample_dir, result_sample_dir, return_transforms=True, generation=current_generation
)
if not registered_pcds:
logger.warning(f"No point clouds loaded for sample {sample_name}")
logger.warning("This may be because:")
logger.warning(" - Result sample directory doesn't exist")
logger.warning(" - No transform files found")
logger.warning(" - All PLY files failed to load")
logger.info("Attempting to find next available sample...")
# Try to find and load next sample
next_sequence, next_sample_idx = self.find_next_sample()
if next_sequence and next_sample_idx is not None:
logger.info(f"Found next sample: {next_sequence}, index {next_sample_idx}")
return self.visualize_sample(next_sequence, next_sample_idx)
else:
raise ValueError(f"No valid samples found. Could not load sample {sample_name} and no next sample available.")
# Load generated point clouds if enabled
# NOTE: Generated and GT point clouds are loaded as-is without any transformation
# They are already in center of mass frame and will be used directly for visualization
if hasattr(self, 'show_generated') and self.show_generated:
# Load GT point cloud (for visualization only, not for center of mass calculation)
self.generated_gt_pcd = self.load_and_merge_gt_point_clouds(result_sample_dir)
if self.generated_gt_pcd is not None:
logger.info(f"Loaded GT point cloud (for visualization only, NOT used for center of mass)")
# Load generated point clouds (no transformation applied)
self.generated_pcds = self.load_generated_point_clouds(result_sample_dir, generation=current_generation)
if self.generated_pcds:
logger.info(f"Loaded {len(self.generated_pcds)} generated point cloud(s) (no transformation applied)")
else:
self.generated_pcds = []
self.generated_gt_pcd = None
# Create random yaw rotated versions of input point clouds
# Clone original point clouds BEFORE translation for random yaw view
# (These are the untranslated original point clouds from load_sample_point_clouds)
original_pcds_before_translation = []
for pcd in original_pcds:
# Create a deep copy
pcd_copy = o3d.geometry.PointCloud()
pcd_copy.points = o3d.utility.Vector3dVector(np.asarray(pcd.points).copy())
if pcd.has_normals():
pcd_copy.normals = o3d.utility.Vector3dVector(np.asarray(pcd.normals).copy())
if pcd.has_colors():
pcd_copy.colors = pcd.colors
original_pcds_before_translation.append(pcd_copy)
# Create random yaw rotated point clouds (centered at origin, random yaw per part)
# Skip rotation for the part corresponding to the generated part with most points
self.random_yaw_pcds = self.create_random_yaw_point_clouds(
original_pcds_before_translation,
generated_pcds=self.generated_pcds if hasattr(self, 'show_generated') and self.show_generated else None
)
# Always use merged input point clouds for center of mass computation
center_of_mass = None
merged_input_points = self.merge_input_point_clouds(original_pcds)
if merged_input_points is not None:
_, center_of_mass = self.center_pcd(merged_input_points)
logger.info(f"Using merged input point clouds center of mass: {center_of_mass}")
# Translate original point clouds to center of mass frame first
# Then re-apply transforms to get registered point clouds in center of mass frame
# Generated and GT are already in center of mass frame, so don't translate them
if center_of_mass is not None:
# Translate original point clouds to center of mass frame
self.original_pcds = [self.translate_point_cloud(pcd, center_of_mass) for pcd in original_pcds]
# Re-apply transforms to translated original point clouds to get registered in center of mass frame
self.registered_pcds = []
for idx, (pcd_original, transform) in enumerate(zip(self.original_pcds, transforms_list)):
# Apply transform to the translated original point cloud
points = np.asarray(pcd_original.points)
points_homogeneous = np.hstack([points, np.ones((len(points), 1))])
points_transformed = (transform @ points_homogeneous.T).T[:, :3]
pcd_registered = o3d.geometry.PointCloud()
pcd_registered.points = o3d.utility.Vector3dVector(points_transformed)
# Transform normals if available
if pcd_original.has_normals():
normals_orig = np.asarray(pcd_original.normals)
normals_transformed = (transform[:3, :3] @ normals_orig.T).T
pcd_registered.normals = o3d.utility.Vector3dVector(normals_transformed)
# Copy colors
if pcd_original.has_colors():
pcd_registered.colors = pcd_original.colors
else:
# Use same color as original
color = self.generate_part_colors(len(self.original_pcds))[idx % len(self.original_pcds)]
pcd_registered.paint_uniform_color(color)
self.registered_pcds.append(pcd_registered)
else:
# If no center available, keep original registered point clouds
self.original_pcds = original_pcds
self.registered_pcds = registered_pcds
# Create random yaw rotated versions of input point clouds
# Clone original point clouds before translation for random yaw view
original_pcds_before_translation = []
for pcd in original_pcds:
# Create a deep copy
pcd_copy = o3d.geometry.PointCloud()
pcd_copy.points = o3d.utility.Vector3dVector(np.asarray(pcd.points).copy())
if pcd.has_normals():
pcd_copy.normals = o3d.utility.Vector3dVector(np.asarray(pcd.normals).copy())
if pcd.has_colors():
pcd_copy.colors = pcd.colors
original_pcds_before_translation.append(pcd_copy)
# Create random yaw rotated point clouds (centered at origin, random yaw per part)
# Skip rotation for the part corresponding to the generated part with most points
self.random_yaw_pcds = self.create_random_yaw_point_clouds(
original_pcds_before_translation,
generated_pcds=self.generated_pcds if hasattr(self, 'show_generated') and self.show_generated else None
)
# Select point clouds based on view mode (use translated versions)
if self.view_mode == "generated" and self.generated_pcds:
point_clouds = self.generated_pcds.copy() # Only generated, no GT
elif self.view_mode == "gt" and self.generated_gt_pcd is not None:
point_clouds = [self.generated_gt_pcd] # Only GT
elif self.view_mode == "random_yaw" and self.random_yaw_pcds:
point_clouds = self.random_yaw_pcds.copy() # Random yaw rotated input point clouds
elif self.view_mode == "original":
point_clouds = self.original_pcds # Use translated original point clouds
else:
point_clouds = self.registered_pcds # Use translated registered point clouds
logger.info("=" * 60)
logger.info(f"SAMPLE POINT COUNT SUMMARY:")
logger.info(f" Sample: {sample_name}")
logger.info(f" Number of Parts: {len(point_clouds)}")
total_points = sum(len(np.asarray(pcd.points)) for pcd in point_clouds)
logger.info(f" TOTAL POINTS: {total_points:6d}")
logger.info("=" * 60)
# Create visualizer
try:
self.vis = o3d.visualization.VisualizerWithKeyCallback()
has_key_callback = True
except AttributeError:
self.vis = o3d.visualization.Visualizer()
has_key_callback = False
self.vis.create_window(window_name="Registered Point Cloud Visualizer")
# Add point clouds
for pcd in point_clouds:
self.vis.add_geometry(pcd)
# Add coordinate frame
if not self.no_coordinate_frame:
coordinate_frame = o3d.geometry.TriangleMesh.create_coordinate_frame(size=1.0)
self.vis.add_geometry(coordinate_frame)
# Set render options
render_option = self.vis.get_render_option()
render_option.background_color = np.array(self.background_color)
render_option.point_size = self.point_size
# Center and fit view
self._center_and_fit_view(point_clouds)
self.colored_pcds = point_clouds
# Register keyboard callbacks
if has_key_callback:
try:
def background_callback(vis):
self.toggle_background_color()
return False
def next_sample_callback(vis):
# If generation cycling is enabled, cycle through generations instead of samples
if self.cycle_generations and self.available_generations:
success = self.cycle_to_next_generation()
if success:
logger.info(f"Spacebar pressed: Cycled to generation {self.available_generations[self.current_generation_idx]}")
else:
logger.warning("Spacebar pressed: Failed to cycle to next generation")
else:
success = self.load_next_sample()
if success:
logger.info("Spacebar pressed: Loaded next sample")
else:
logger.warning("Spacebar pressed: Failed to load next sample")
return False
def toggle_transform_callback(vis):
self.toggle_transform_view()
return False
def next_sequence_callback(vis):
success = self.load_next_sequence()
if success:
logger.info("N pressed: Jumped to next sequence")
else:
logger.warning("N pressed: Failed to jump to next sequence")
return False
self.vis.register_key_callback(ord('B'), background_callback)
self.vis.register_key_callback(ord(' '), next_sample_callback)
self.vis.register_key_callback(ord('T'), toggle_transform_callback)
self.vis.register_key_callback(ord('N'), next_sequence_callback)
logger.info("Keyboard callbacks registered successfully")
except Exception as e:
logger.warning(f"Could not register keyboard callback: {e}")
has_key_callback = False
logger.info("Visualization ready. Press 'Q' to quit.")
logger.info("Controls:")
logger.info(" - Mouse: Rotate view")
logger.info(" - Scroll: Zoom")
logger.info(" - Shift+Mouse: Pan")
logger.info(" - R: Reset view")
logger.info(" - H: Print help")
if has_key_callback:
logger.info(" - B: Toggle background color")
logger.info(" - T: Cycle between original, registered, generated, GT, and random_yaw views")
if self.cycle_generations:
logger.info(" - Spacebar: Cycle through generations for current sample")
else:
logger.info(" - Spacebar: Load next sample")
logger.info(" - N: Jump to next sequence (first sample of next sequence)")
view_name_map = {
"original": "Original (untransformed)",
"registered": "Registered (transformed)",
"generated": "Generated (from processed directory)",
"gt": "GT (ground truth)"
}
logger.info(f" Current view: {view_name_map.get(self.view_mode, 'Unknown')}")
logger.info("=" * 60)
logger.info(f"CURRENT SAMPLE: Sequence {sequence}, Sample {sample_name}")
logger.info("=" * 60)
# Run visualizer
self.vis.run()
self.vis.destroy_window()
self.vis = None
def main():
parser = argparse.ArgumentParser(
description="Visualize registered point clouds using estimated poses"
)
parser.add_argument("--input", "-i", type=str, required=True,
help="Input directory containing point cloud files")
parser.add_argument("--results", "-r", type=str, required=True,
help="Directory containing result files (poses and generated point clouds)")
parser.add_argument("--sequence", "-s", type=str, default=None,
help="Specific sequence to visualize (default: all sequences)")
parser.add_argument("--sample_idx", type=int, default=0,
help="Sample index to start with (default: 0)")
parser.add_argument("--max_points_per_fragment", type=int, default=10000,
help="Maximum points to load per fragment (default: 10000)")
parser.add_argument("--generation", type=str, default="generation00",
help="Generation subdirectory name (default: generation00)")
parser.add_argument("--point_size", type=float, default=3.0,
help="Point size in visualization (default: 3.0)")
parser.add_argument("--background_color", type=float, nargs=3, default=[1.0, 1.0, 1.0],
help="Background color as R G B values in [0,1] (default: 1.0 1.0 1.0 - white)")
parser.add_argument("--show_coordinate_frame", action='store_true',
help="Show coordinate frame (hidden by default)")
parser.add_argument("--estimate_normals", "-n", action='store_true', default=False,
help="Estimate normals for point clouds that don't have them")
parser.add_argument("--normal_estimation_radius", type=float, default=1.0,
help="Radius for normal estimation (default: 1.0)")
parser.add_argument("--log_level", type=str, default="INFO",
choices=["DEBUG", "INFO", "WARNING", "ERROR"],
help="Set the logging level (default: INFO)")
parser.add_argument("--show_generated", action='store_true', default=False,
help="Load and show generated point clouds from processed directory (generation_selected PLY files)")
parser.add_argument("--remove_outliers", action='store_true', default=False,
help="Apply stochastic outlier removal to input point clouds")
parser.add_argument("--outlier_nb_neighbors", type=int, default=20,
help="Number of neighbors for outlier removal (default: 20)")
parser.add_argument("--outlier_std_ratio", type=float, default=2.0,
help="Standard deviation ratio for outlier removal (default: 2.0)")
parser.add_argument("--cycle_generations", action='store_true', default=False,
help="Cycle through different generations for each sample (press spacebar to cycle)")
args = parser.parse_args()
# Configure logging based on argument
log_level = getattr(logging, args.log_level.upper(), logging.INFO)
logging.basicConfig(
level=log_level,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
# Expand user paths
input_dir = os.path.expanduser(args.input)
results_dir = os.path.expanduser(args.results)
if not os.path.exists(input_dir):
logger.error(f"Input directory not found: {input_dir}")
return
if not os.path.exists(results_dir):
logger.error(f"Results directory not found: {results_dir}")
return
# Create visualizer
visualizer = RegisteredPointCloudVisualizer(
input_dir=input_dir,
poses_dir=results_dir,
point_size=args.point_size,
background_color=args.background_color,
no_coordinate_frame=not args.show_coordinate_frame,
estimate_normals=args.estimate_normals,
normal_estimation_radius=args.normal_estimation_radius,
generation=args.generation,
max_points_per_fragment=args.max_points_per_fragment,
remove_outliers=args.remove_outliers,
outlier_nb_neighbors=args.outlier_nb_neighbors,
outlier_std_ratio=args.outlier_std_ratio
)
# Set flag for showing generated point clouds
visualizer.show_generated = args.show_generated
# Set flag for cycling through generations
visualizer.cycle_generations = args.cycle_generations
# Get list of all available sequences from input directory
available_sequences = visualizer.get_available_sequences()
if not available_sequences:
logger.error(f"No sequences found in input directory: {input_dir}")
return
logger.info(f"Found {len(available_sequences)} sequence(s) in input directory:")
for seq in available_sequences:
logger.info(f" - {seq}")
# If sequence not specified, randomly select one
if args.sequence is None:
import random
args.sequence = random.choice(available_sequences)
logger.info(f"No sequence specified, randomly selected: {args.sequence}")
elif args.sequence not in available_sequences:
logger.warning(f"Specified sequence '{args.sequence}' not found in available sequences")
logger.warning(f"Available sequences: {available_sequences}")
import random
args.sequence = random.choice(available_sequences)
logger.info(f"Randomly selected instead: {args.sequence}")
# Visualize first sample
try:
visualizer.visualize_sample(args.sequence, args.sample_idx)
except Exception as e:
logger.error(f"Visualization failed: {e}")
import traceback
logger.error(traceback.format_exc())
logger.info("Visualization complete!")
if __name__ == "__main__":
main()
|