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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
# %%
""" """

# %%
import json
import numpy as np
from sympy import im
import glog

# from visualize_tree import visualize_tree
from scipy.spatial.transform import Rotation as R
import argparse
import copy
from src.preprocessing import scene_parser
from src.geometry.convex_hull_processor import (
    ConvexHullProcessor_2d,
)
from src.geometry.basic_geometries import Basic2DGeometry
from src.geometry.object_mesh_processor import MeshProcessor
from src.geometry.rectangle_query_processor import RectangleQuery2D
from src.geometry.ground_coverage_analyzer import GroundCoverageAnalyzer
from src.utils.string_convertor import StringConvertor
import sapien
import transforms3d
from src.utils.image_renderer import image_render_processor
import matplotlib.pyplot as plt
import matplotlib.patches as patches

# from ..image_renderer import image_render_processor

INF = 1e6
eps = 1e-6


# %%
# Function to load a JSON file from disk
def load_json_file(file_path):
    with open(file_path, "r") as file:
        data = json.load(file)
    return data


EIGHT_DIRECTIONS = [
    "rear",
    "rear-left",
    "left",
    "front-left",
    "front",
    "front-right",
    "right",
    "rear-right",
]
OPPOSITE_DIRECTIONS = {
    "front": "rear",
    "front-right": "rear-left",
    "right": "left",
    "rear-right": "front-left",
    "rear": "front",
    "rear-left": "front-right",
    "left": "right",
    "front-left": "rear-right",
}
FOUR_DIAGONAL_DIRECTIONS = ["rear-left", "front-left", "front-right", "rear-right"]


class TreePlatform:
    def __init__(

        self,

        name="",

        children=[],

        convex_hull_2d=None,

        heading=None,

        avl_height=3.1,

        bottom=0,

        bel_object=None,

        visible_directions={

            "front": False,

            "rear": False,

            "right": False,

            "left": False,

        },

    ):
        self.name = name
        self.children = children
        self.convex_hull_2d = convex_hull_2d
        self.heading = heading
        self.avl_height = avl_height
        self.bottom = bottom
        self.bel_object = bel_object
        self.placement_dict = {}
        self.bbox = convex_hull_2d.get_headed_bbox_instance()
        self.rect_list = []
        self.visible_directions = visible_directions
        self.free_space = None
        self.standing_point_list = []

        if self.free_space is None and self.name != "GROUND":
            cos_theta, sin_theta = heading[0], heading[1]
            points = self.convex_hull_2d.get_headed_bbox_instance_with_heading(heading)

            freespace_seg_list = [
                [points[0], points[1]],
                [points[1], points[2]],
                [points[2], points[3]],
                [points[3], points[0]],
            ]

            # for i, freespace_seg in enumerate(freespace_seg_list):
            #     if np.linalg.norm(freespace_seg[0] - freespace_seg[1]) < 0.6:
            #         freespace_seg_list[i] = [
            #             (freespace_seg[0] + freespace_seg[1]) / 2 + (freespace_seg[0] - freespace_seg[1]) / 2 * 0.6 ,
            #             (freespace_seg[0] + freespace_seg[1]) / 2 - (freespace_seg[0] - freespace_seg[1]) / 2 * 0.6,
            #         ]
            #         continue
            def extend_point(p, direction):
                if direction == "right":
                    normal = np.array([cos_theta, sin_theta])
                elif direction == "left":
                    normal = np.array([-cos_theta, -sin_theta])
                elif direction == "front":
                    normal = np.array([-sin_theta, cos_theta])
                elif direction == "rear":
                    normal = np.array([sin_theta, -cos_theta])
                elif direction == "rear-left":
                    normal = np.array([sin_theta, -cos_theta]) + np.array(
                        [-cos_theta, -sin_theta]
                    )
                elif direction == "front-left":
                    normal = np.array([-sin_theta, cos_theta]) + np.array(
                        [-cos_theta, -sin_theta]
                    )
                elif direction == "front-right":
                    normal = np.array([-sin_theta, cos_theta]) + np.array(
                        [cos_theta, sin_theta]
                    )
                elif direction == "rear-right":
                    normal = np.array([sin_theta, -cos_theta]) + np.array(
                        [cos_theta, sin_theta]
                    )
                else:
                    return np.array(p)
                return np.array(p) + 0.5 * np.array(normal)

            self.free_space = [
                {
                    "Objects": [],
                    "Available_space": [
                        extend_point(freespace_seg_list[3][1], "rear"),
                        freespace_seg_list[3][1],
                        freespace_seg_list[3][0],
                        extend_point(freespace_seg_list[3][0], "rear"),
                    ],
                    "Critical_space": [
                        extend_point(freespace_seg_list[3][1], "rear"),
                        freespace_seg_list[3][1],
                        freespace_seg_list[3][0],
                        extend_point(freespace_seg_list[3][0], "rear"),
                    ],
                },
                {
                    "Objects": [],
                    "Available_space": [
                        extend_point(points[0], "rear-left"),
                        extend_point(points[0], "left"),
                        points[0],
                        extend_point(points[0], "rear"),
                    ],
                    "Critical_space": [
                        extend_point(points[0], "rear-left"),
                        extend_point(points[0], "left"),
                        points[0],
                        extend_point(points[0], "rear"),
                    ],
                },
                {
                    "Objects": [],
                    "Available_space": [
                        extend_point(freespace_seg_list[0][0], "left"),
                        extend_point(freespace_seg_list[0][1], "left"),
                        freespace_seg_list[0][1],
                        freespace_seg_list[0][0],
                    ],
                    "Critical_space": [
                        extend_point(freespace_seg_list[0][0], "left"),
                        extend_point(freespace_seg_list[0][1], "left"),
                        freespace_seg_list[0][1],
                        freespace_seg_list[0][0],
                    ],
                },
                {
                    "Objects": [],
                    "Available_space": [
                        extend_point(points[1], "left"),
                        extend_point(points[1], "front-left"),
                        extend_point(points[1], "front"),
                        points[1],
                    ],
                    "Critical_space": [
                        extend_point(points[1], "left"),
                        extend_point(points[1], "front-left"),
                        extend_point(points[1], "front"),
                        points[1],
                    ],
                },
                {
                    "Objects": [],
                    "Available_space": [
                        freespace_seg_list[1][0],
                        extend_point(freespace_seg_list[1][0], "front"),
                        extend_point(freespace_seg_list[1][1], "front"),
                        freespace_seg_list[1][1],
                    ],
                    "Critical_space": [
                        freespace_seg_list[1][0],
                        extend_point(freespace_seg_list[1][0], "front"),
                        extend_point(freespace_seg_list[1][1], "front"),
                        freespace_seg_list[1][1],
                    ],
                },
                {
                    "Objects": [],
                    "Available_space": [
                        points[2],
                        extend_point(points[2], "front"),
                        extend_point(points[2], "front-right"),
                        extend_point(points[2], "right"),
                    ],
                    "Critical_space": [
                        points[2],
                        extend_point(points[2], "front"),
                        extend_point(points[2], "front-right"),
                        extend_point(points[2], "right"),
                    ],
                },
                {
                    "Objects": [],
                    "Available_space": [
                        freespace_seg_list[2][1],
                        freespace_seg_list[2][0],
                        extend_point(freespace_seg_list[2][0], "right"),
                        extend_point(freespace_seg_list[2][1], "right"),
                    ],
                    "Critical_space": [
                        freespace_seg_list[2][1],
                        freespace_seg_list[2][0],
                        extend_point(freespace_seg_list[2][0], "right"),
                        extend_point(freespace_seg_list[2][1], "right"),
                    ],
                },
                {
                    "Objects": [],
                    "Available_space": [
                        extend_point(points[3], "rear"),
                        points[3],
                        extend_point(points[3], "right"),
                        extend_point(points[3], "rear-right"),
                    ],
                    "Critical_space": [
                        extend_point(points[3], "rear"),
                        points[3],
                        extend_point(points[3], "right"),
                        extend_point(points[3], "rear-right"),
                    ],
                },
            ]

    def get_bbox_line(self, dir):
        if dir % 2 != 0 or dir < 0 or dir > 7:
            return None

        bbox = self.convex_hull_2d.get_headed_bbox_instance()
        return [bbox[dir // 2], bbox[(dir // 2 + 3) % 4]]

    #        glog.info(f"platform name: {self.name}, visible_directions: {self.visible_directions}")

    def get_name_for_interaction(self):
        name_for_interaction = self.name
        if "Object/" in name_for_interaction:
            name_for_interaction = name_for_interaction.split("Object/")[-1]
        if "RoboTHOR_" in name_for_interaction:
            name_for_interaction = name_for_interaction.split("RoboTHOR_")[-1]
        if "frl_apartment_" in name_for_interaction:
            name_for_interaction = name_for_interaction.split("frl_apartment_")[-1]

        return f"object_{StringConvertor.get_noslash_name_wo_id(name_for_interaction)}_platform_{StringConvertor.get_id(self.name)}"

    def get_categories_and_objects(self):
        """

        get the number of categories of children and the number of children.

        """
        major_category = set()
        minor_category = set()
        child_info_list = []
        for child in self.children:

            major_category.add(StringConvertor.get_category(child.name))
            minor_category.add(StringConvertor.get_name_wo_id(child.name))

        major_category_list = list(major_category)
        minor_category_list = list(minor_category)
        children_name_list = [
            child.get_name_for_interaction() for child in self.children
        ]

        return major_category_list, minor_category_list, children_name_list

    def single_freespace_available(self, object_node, single_type="all"):
        object_node_convex = ConvexHullProcessor_2d(
            vertices=object_node.object.convex_hull_2d.vertices, heading=self.heading
        )
        if single_type == "empty":
            if len(self.children) > 0:
                return False, np.array([-1e5, -1e5])
            platform_bbox = self.convex_hull_2d.get_headed_bbox_instance()
            platform_13_bbox = np.array(
                [
                    (platform_bbox[i] - platform_bbox[0]) / 3 + platform_bbox[0]
                    for i in range(4)
                ]
            )
            if object_node_convex.can_fit_in(platform_13_bbox):
                return True, object_node_convex.get_fit_in_translation(platform_13_bbox)
            return False, np.array([-1e6, -1e6])

        if len(self.children) == 0:
            return False, np.array([-1e5, -1e5])

        if single_type == "all":
            freespace_list = self.get_freespace_list()
        else:
            freespace_id_list = []
            if len(single_type) == 1:
                child_name = single_type[0]
                child_id = self.get_child_id(child_name)
                if child_id == -1:
                    return False, np.array([-1e6, -1e6])
                freespace_id_list = [(child_id, i) for i in range(8)]
            elif len(single_type) == 2:
                if isinstance(single_type[-1], str):
                    freespace_id_list_a = []
                    freespace_id_list_b = []
                    child_name_a = single_type[0]
                    child_name_b = single_type[1]
                    child_id_a = self.get_child_id(child_name_a)
                    child_id_b = self.get_child_id(child_name_b)
                    for i in range(8):
                        freespace_id_list_a.append((child_id_a, i))
                    for i in range(8):
                        freespace_id_list_b.append((child_id_b, i))

                    for i in range(8):
                        if object_node_convex.can_fit_in(
                            self.children[child_id_a].free_space[i]["Critical_space"]
                        ) and object_node_convex.can_fit_in(
                            self.children[child_id_b].free_space[(i + 4) % 8][
                                "Critical_space"
                            ]
                        ):
                            return True, object_node_convex.get_fit_in_translation(
                                self.children[child_id_a].free_space[i][
                                    "Critical_space"
                                ]
                            )
                    return False, np.array([-1e6, -1e6])
                else:
                    child_name_a = single_type[0]
                    child_id_a = self.get_child_id(child_name_a)
                    freespace_id_list = [(child_id_a, single_type[1])]

            freespace_list = self.get_freespace_list(
                freespace_id_list=freespace_id_list
            )
        for freespace in freespace_list:
            if object_node_convex.can_fit_in(freespace):
                return True, object_node_convex.get_fit_in_translation(freespace)
        return False, np.array([-1e6, -1e6])

    def rename_with_name_map(self, name_map):
        """



        rename the platform and all its children with the name map.



        Args:

            name_map (dict): A dictionary mapping old names to new names.



        Returns:

            None. The function modifies all the features of the platform according to the name_map.



        """
        self.name = StringConvertor.rename_with_map(self.name, name_map)
        for child in self.children:
            child.rename_with_name_map(name_map)
        self.bel_object = StringConvertor.rename_with_map(self.bel_object, name_map)
        return self

    @staticmethod
    def sort_platforms(platforms):
        """



        sort the platforms according to the bottom height.



        Args:

            platforms (list): A list of TreePlatform objects.



        Returns:

            sorted_platforms (list): A list of TreePlatform objects sorted by their bottom height.

        """
        sorted_platforms = sorted(platforms, key=lambda x: x.bottom)
        return sorted_platforms

    def bfs_children(self):
        queue = []
        for child in self.children:
            queue.append(child)
        while len(queue) > 0:
            cur_node = queue.pop(0)
            for child in cur_node.children:
                queue.append(child)

        self.children = queue
        return queue

    def freespace_is_visible(self, dir):
        dir = EIGHT_DIRECTIONS[dir]
        if dir not in self.visible_directions:
            return False
        return self.visible_directions[dir]

    def get_freespace_list(self, freespace_id_list=None):

        freespace_list = []
        if freespace_id_list is None:
            for child in self.children:
                for dir in range(8):
                    freespace_list.append(child.free_space[dir]["Critical_space"])
        else:
            for object_id, freespace_id in freespace_id_list:
                if object_id >= len(self.children):
                    continue
                child = self.children[object_id]
                if freespace_id >= 8:
                    continue
                freespace_list.append(child.free_space[freespace_id]["Critical_space"])

        return freespace_list

    def get_child_id(self, child_name):
        for i, child in enumerate(self.children):
            if child.name == child_name:
                return i
        return -1

    def get_child_name_list(self):
        return [child.name for child in self.children]

    def get_dir_point(self, item_node=None, dir="center"):
        object_node_convex = ConvexHullProcessor_2d(
            vertices=item_node.object.convex_hull_2d.vertices, heading=self.heading
        )

        platform_bbox = self.convex_hull_2d.get_headed_bbox_instance()
        object_node_bbox = object_node_convex.get_headed_bbox_instance()
        if dir == "center":
            return np.mean(platform_bbox, axis=0)

        direction = EIGHT_DIRECTIONS.index(dir)
        platform_point = (
            platform_bbox[direction // 2]
            if direction % 2 == 1
            else (
                platform_bbox[direction // 2] + platform_bbox[(3 + direction // 2) % 4]
            )
            / 2
        )
        if direction in [7, 0, 1]:
            platform_point += (object_node_bbox[1] - object_node_bbox[0]) * 0.5
        if direction in [1, 2, 3]:
            platform_point += (object_node_bbox[2] - object_node_bbox[1]) * 0.5
        if direction in [3, 4, 5]:
            platform_point += (object_node_bbox[3] - object_node_bbox[2]) * 0.5
        if direction in [5, 6, 7]:
            platform_point += (object_node_bbox[0] - object_node_bbox[3]) * 0.5

        return platform_point

        pass

    def take_picture_with_marked_rect(

        self,

        scene,

        view="human_full",  # top

        camera_xy=None,

        need_mark_rectangle_list=[],

        same_color=False,

        width=1920,

        height=1080,

        focus_ratio=0.6,

        save_path=None,

    ):

        highest_child = (
            max([child.top for child in self.children])
            if self.children != []
            else self.bottom + 0.15
        )

        z_range = [highest_child, self.bottom + self.avl_height + 0.3]
        platform_2d_bbox = self.convex_hull_2d.get_headed_bbox_instance()

        platform_3d_bbox = [
            np.append(point, self.bottom) for point in platform_2d_bbox
        ] + [
            np.append(point, self.bottom + self.avl_height)
            for point in platform_2d_bbox
        ]

        might_mark_freespace_list = []

        for rect in need_mark_rectangle_list:
            rect_3d = [np.append(point, self.bottom) for point in rect]
            might_mark_freespace_list.append(rect_3d)

        optimal_pose, optimal_fovy = (
            image_render_processor.auto_get_optimal_camera_pose_for_object(
                view=view,
                camera_xy=camera_xy,
                z_range=z_range,
                object_bbox=platform_3d_bbox,
                platform_rect=platform_3d_bbox[:4],
                width=width,
                height=height,
                focus_ratio=focus_ratio,
            )
        )

        img = image_render_processor.auto_render_image_refactored(
            scene,
            pose=optimal_pose,
            fovy=optimal_fovy,
            width=width,
            height=height,
            might_mark_object_cuboid_list=[],
            might_mark_freespace_list=might_mark_freespace_list,
            rectangle_grey=same_color,
            save_path=save_path,
        )

        pass
        return img

    def find_available_places(

        self,

        obstacle_list,

        freespace_list,

        target,

        min_step=0.02,

    ):
        """



        Args:

            obstacle_list: list of obstacles, each obstacle is a list of vertices

            freespace_list: list of freespace, each freespace is a list of vertices

            target: target rectangle to place, a list of vertices

            min_step: minimum step size to consider for placing the target



        """
        table_vertices = self.convex_hull_2d.get_headed_bbox_instance()
        rotation_angle = np.arctan2(self.heading[1], self.heading[0])

        transformed_table = np.array(
            [
                Basic2DGeometry.rotate_point_counterclockwise(
                    table_vertice, rotation_angle
                )
                for table_vertice in table_vertices
            ]
        )

        transformed_obstacles = np.array(
            [
                np.array(
                    [
                        Basic2DGeometry.rotate_point_counterclockwise(
                            obstacle_vertice, rotation_angle
                        )
                        for obstacle_vertice in obstacle
                    ]
                )
                for obstacle in obstacle_list
            ]
        )
        transformed_freespaces = np.array(
            [
                np.array(
                    [
                        Basic2DGeometry.rotate_point_counterclockwise(
                            freespace_vertice, rotation_angle
                        )
                        for freespace_vertice in freespace
                    ]
                )
                for freespace in freespace_list
            ]
        )

        #        glog.info(f"transformed_obstacles: {transformed_obstacles}")

        transformed_target = np.array(
            [
                Basic2DGeometry.rotate_point_counterclockwise(
                    target_vertice, rotation_angle
                )
                for target_vertice in target
            ]
        )

        x_min, x_max, y_min, y_max = (
            np.min(transformed_table[:, 0]),
            np.max(transformed_table[:, 0]),
            np.min(transformed_table[:, 1]),
            np.max(transformed_table[:, 1]),
        )
        target_x_min, target_x_max, target_y_min, target_y_max = (
            np.min(transformed_target[:, 0]),
            np.max(transformed_target[:, 0]),
            np.min(transformed_target[:, 1]),
            np.max(transformed_target[:, 1]),
        )
        target_x_len, target_y_len = (
            target_x_max - target_x_min,
            target_y_max - target_y_min,
        )

        if len(obstacle_list) == 0:
            result = ""
            if (
                target_x_len < (x_max - x_min) * 0.9
                and target_y_len < (y_max - y_min) * 0.9
            ):
                result = "wholetable"
            if (
                target_x_len < (x_max - x_min) / 3 * 2
                and target_y_len < (y_max - y_min) / 3 * 2
            ):
                result += ",center"
            return result, [], [], [], [], {}, {}, {}

        prefix_points, prefix_values = [], []
        sensible_xs, sensible_ys = [], []
        obstacle_query = RectangleQuery2D(dimension=2)

        for obs in transformed_obstacles:
            obs_x_min, obs_x_max, obs_y_min, obs_y_max = (
                np.min(obs[:, 0]),
                np.max(obs[:, 0]),
                np.min(obs[:, 1]),
                np.max(obs[:, 1]),
            )
            obstacle_query.add_rectangle(obs)

            sensible_xs.append(obs_x_min)
            sensible_xs.append(obs_x_max)
            sensible_ys.append(obs_y_min)
            sensible_ys.append(obs_y_max)

        sensible_xs.extend([x_min, x_max])
        sensible_ys.extend([y_min, y_max])

        sensible_xs = [x + min_step * 2 for x in sensible_xs] + [
            x - min_step * 2 for x in sensible_xs
        ]
        sensible_ys = [y + min_step * 2 for y in sensible_ys] + [
            y - min_step * 2 for y in sensible_ys
        ]

        freespace_query = RectangleQuery2D(dimension=2)
        for freespace in transformed_freespaces:
            freespace_query.add_rectangle(freespace)

        available_positions = []

        for op in range(4):
            op_offset = transformed_target[0] - transformed_target[op]
            for x in sensible_xs:
                for y in sensible_ys:

                    x1, x2, y1, y2 = x, x + target_x_len, y, y + target_y_len
                    x1, x2, y1, y2 = (
                        x1 + op_offset[0],
                        x2 + op_offset[0],
                        y1 + op_offset[1],
                        y2 + op_offset[1],
                    )
                    if x1 < x_min or x2 > x_max or y1 < y_min or y2 > y_max:
                        continue

                    tmp_rect = np.array(
                        [
                            np.array([x1, y1]),
                            np.array([x1, y2]),
                            np.array([x2, y2]),
                            np.array([x2, y1]),
                        ]
                    )

                    results, ids = obstacle_query.query_rectangle(tmp_rect)
                    if len(results) == 0:
                        available_positions.append(tmp_rect)

        put_on_platform = len(available_positions) > 0
        put_around_object_set = set()
        put_on_object_dir_set = set()
        put_between_2_object_set = set()
        put_around_object_dict = {}
        put_on_object_dir_dict = {}
        put_between_2_object_dict = {}

        for avl_rect in available_positions:
            at_freespace_id, at_freespace = freespace_query.query_rectangle(avl_rect)
            at_freespace_object_id = [id // 8 for id in at_freespace_id]
            at_freespace_dir_id = [[] for i in range(8)]
            avl_rect_center = np.mean(avl_rect, axis=0)
            original_avl_rect_center = Basic2DGeometry.rotate_point_counterclockwise(
                avl_rect_center, -rotation_angle
            )
            for id in at_freespace_id:
                at_freespace_dir_id[id % 8].append(id // 8)

            counts = {}
            for object_id in at_freespace_object_id:
                if object_id not in counts:
                    counts[object_id] = 0
                counts[object_id] += 1

            for freespace_id in at_freespace_id:
                if counts[freespace_id // 8] >= 1:
                    put_on_object_dir_dict[(freespace_id // 8, freespace_id % 8)] = (
                        original_avl_rect_center
                    )
                    put_on_object_dir_set.add((freespace_id // 8, freespace_id % 8))

            for dir in range(0, 4):
                for item_ida in at_freespace_dir_id[dir]:
                    for item_idb in at_freespace_dir_id[dir + 4]:
                        put_between_2_object_set.add((item_ida, item_idb))
                        put_between_2_object_dict[(item_ida, item_idb)] = (
                            original_avl_rect_center
                        )

            for object_id in at_freespace_object_id:
                put_around_object_dict[object_id] = original_avl_rect_center
                put_around_object_set.add(object_id)

        put_around_object_list = list(put_around_object_set)
        put_on_object_dir_list = list(put_on_object_dir_set)
        put_between_2_object_list = list(put_between_2_object_set)

        return (
            "non_empty",
            put_on_platform,
            put_around_object_list,
            put_on_object_dir_list,
            put_between_2_object_list,
            put_around_object_dict,
            put_on_object_dir_dict,
            put_between_2_object_dict,
        )

    def get_fitable_tasks_any_number(self, item, all_standing_directions=[0, 2, 4, 6]):
        """

             def find_available_places(

            self, obstacle_list, freespace_2d_list, target, min_step=0.02, solve_mode="all_intensive"

        ):

        """

        target_convex = item.object.convex_hull_2d
        target_bbox = ConvexHullProcessor_2d.get_headed_bbox(
            vertices=target_convex.vertices, heading=self.heading
        )
        obstacle_list = []
        freespace_list = []
        child_name_list = [
            child.name
            for child in self.children
            if child.name != item.name and child.parent.name == self.bel_object
        ]
        for child in self.children:
            if not (child.name != item.name and child.parent.name == self.bel_object):
                continue
            child_bbox = ConvexHullProcessor_2d.get_headed_bbox(
                vertices=child.object.convex_hull_2d.vertices, heading=self.heading
            )
            obstacle_list.append(child_bbox)
            for dir in range(8):
                freespace_list.append(child.free_space[dir]["Critical_space"])

        (
            result,
            put_on_platform,
            put_around_object_list,
            put_on_object_dir_list,
            put_between_2_object_list,
            put_around_object_dict,
            put_on_object_dir_dict,
            put_between_2_object_dict,
        ) = self.find_available_places(
            obstacle_list=obstacle_list,
            freespace_list=freespace_list,
            target=target_bbox,
            min_step=0.01,
        )

        put_around_object_task_dict = {}
        put_on_object_dir_task_dict = {}
        put_between_2_object_task_dict = {}

        if all_standing_directions is None or len(all_standing_directions) == 0:
            return ("non_empty", False, [], [], [], {}, {}, {})

        for i, put_around_object_task in enumerate(put_around_object_list):
            put_around_object_task_dict[child_name_list[put_around_object_task]] = (
                put_around_object_dict.get(put_around_object_task)
            )
            put_around_object_list[i] = child_name_list[put_around_object_task]
        for i, put_on_object_task in enumerate(put_on_object_dir_list):
            for standing_directions in all_standing_directions:
                new_key = (
                    child_name_list[put_on_object_task[0]],
                    EIGHT_DIRECTIONS[
                        (put_on_object_task[1] - standing_directions + 8) % 8
                    ],
                )

                put_on_object_dir_task_dict[new_key] = put_on_object_dir_dict.get(
                    put_on_object_task
                )

                put_on_object_dir_list[i] = new_key

        for i, put_between_2_object_task in enumerate(put_between_2_object_list):
            new_key = (
                child_name_list[put_between_2_object_task[0]],
                child_name_list[put_between_2_object_task[1]],
            )
            put_between_2_object_task_dict[new_key] = put_between_2_object_dict.get(
                put_between_2_object_task
            )
            put_between_2_object_list[i] = new_key
            pass

        put_on_object_dir_list = list(set(put_on_object_dir_list))
        put_between_2_object_list = list(set(put_between_2_object_list))
        # import ipdb
        # ipdb.set_trace()
        return (
            result,
            put_on_platform,
            put_around_object_list,
            put_on_object_dir_list,
            put_between_2_object_list,
            put_around_object_task_dict,
            put_on_object_dir_task_dict,
            put_between_2_object_task_dict,
        )


class TreeNode:
    robot_fit_threshold = 0.35
    minimal_freespace_width = 0.02

    def __init__(

        self,

        name,

        entity_config=None,

        removed=False,

        heading=None,

        free_space=None,

        parent=None,

        children=None,

        convex_hull_2d=None,

        bottom=None,

        top=None,

        bbox=None,

        mesh=None,

        centroid_translation=None,

    ):
        self.name = name
        self.entity_config = entity_config
        self.object = scene_parser.SceneElement(
            name=name,
            heading=heading,
            convex_hull_2d=convex_hull_2d,
            bbox=bbox,
            instancetype="object",
        )
        self.heading = heading
        self.bottom = bottom
        self.top = top
        self.free_space = free_space
        self.free_space_height = [bottom, INF]
        self.critical_free_space = free_space
        self.removed = removed
        self.mesh = mesh
        self.centroid_translation = centroid_translation
        self.mesh.apply_offset(centroid_translation)

        if self.free_space is None and self.name != "GROUND":
            cos_theta, sin_theta = heading[0], heading[1]
            points = self.object.convex_hull_2d.get_headed_bbox_instance_with_heading(
                heading
            )

            def extend_point(p, direction):
                if direction == "right":
                    normal = np.array([cos_theta, sin_theta])
                elif direction == "left":
                    normal = np.array([-cos_theta, -sin_theta])
                elif direction == "front":
                    normal = np.array([-sin_theta, cos_theta])
                elif direction == "rear":
                    normal = np.array([sin_theta, -cos_theta])
                elif direction == "rear-left":
                    normal = np.array([sin_theta, -cos_theta]) + np.array(
                        [-cos_theta, -sin_theta]
                    )
                elif direction == "front-left":
                    normal = np.array([-sin_theta, cos_theta]) + np.array(
                        [-cos_theta, -sin_theta]
                    )
                elif direction == "front-right":
                    normal = np.array([-sin_theta, cos_theta]) + np.array(
                        [cos_theta, sin_theta]
                    )
                elif direction == "rear-right":
                    normal = np.array([sin_theta, -cos_theta]) + np.array(
                        [cos_theta, sin_theta]
                    )
                else:
                    return np.array(p)
                return np.array(p) + 10 * np.array(normal)

            # point older: rear_left, front_left, front_right, rear_right
            self.free_space = [
                {
                    "Objects": [],
                    "Available_space": [
                        extend_point(points[0], "rear"),
                        points[0],
                        points[3],
                        extend_point(points[3], "rear"),
                    ],
                    "Critical_space": [
                        extend_point(points[0], "rear"),
                        points[0],
                        points[3],
                        extend_point(points[3], "rear"),
                    ],
                },
                {
                    "Objects": [],
                    "Available_space": [
                        extend_point(points[0], "rear-left"),
                        extend_point(points[0], "left"),
                        points[0],
                        extend_point(points[0], "rear"),
                    ],
                    "Critical_space": [
                        extend_point(points[0], "rear-left"),
                        extend_point(points[0], "left"),
                        points[0],
                        extend_point(points[0], "rear"),
                    ],
                },
                {
                    "Objects": [],
                    "Available_space": [
                        extend_point(points[0], "left"),
                        extend_point(points[1], "left"),
                        points[1],
                        points[0],
                    ],
                    "Critical_space": [
                        extend_point(points[0], "left"),
                        extend_point(points[1], "left"),
                        points[1],
                        points[0],
                    ],
                },
                {
                    "Objects": [],
                    "Available_space": [
                        extend_point(points[1], "left"),
                        extend_point(points[1], "front-left"),
                        extend_point(points[1], "front"),
                        points[1],
                    ],
                    "Critical_space": [
                        extend_point(points[1], "left"),
                        extend_point(points[1], "front-left"),
                        extend_point(points[1], "front"),
                        points[1],
                    ],
                },
                {
                    "Objects": [],
                    "Available_space": [
                        points[1],
                        extend_point(points[1], "front"),
                        extend_point(points[2], "front"),
                        points[2],
                    ],
                    "Critical_space": [
                        points[1],
                        extend_point(points[1], "front"),
                        extend_point(points[2], "front"),
                        points[2],
                    ],
                },
                {
                    "Objects": [],
                    "Available_space": [
                        points[2],
                        extend_point(points[2], "front"),
                        extend_point(points[2], "front-right"),
                        extend_point(points[2], "right"),
                    ],
                    "Critical_space": [
                        points[2],
                        extend_point(points[2], "front"),
                        extend_point(points[2], "front-right"),
                        extend_point(points[2], "right"),
                    ],
                },
                {
                    "Objects": [],
                    "Available_space": [
                        points[3],
                        points[2],
                        extend_point(points[2], "right"),
                        extend_point(points[3], "right"),
                    ],
                    "Critical_space": [
                        points[3],
                        points[2],
                        extend_point(points[2], "right"),
                        extend_point(points[3], "right"),
                    ],
                },
                {
                    "Objects": [],
                    "Available_space": [
                        extend_point(points[3], "rear"),
                        points[3],
                        extend_point(points[3], "right"),
                        extend_point(points[3], "rear-right"),
                    ],
                    "Critical_space": [
                        extend_point(points[3], "rear"),
                        points[3],
                        extend_point(points[3], "right"),
                        extend_point(points[3], "rear-right"),
                    ],
                },
            ]

        self.depth = 0
        self.parent = parent
        self.children = children if children is not None else []

        self.on_platform = None
        self.own_platform = []
        self.bel_ground_object = None
        self.bel_ground_platform = None
        self.all_children = children if children is not None else []
        self.children_per_platform = []
        self.offspring_per_platform = []

    @property
    def is_ambiguous(self):
        if self.bel_ground_platform is None:
            self.get_bel_ground_platform()
        if self.bel_ground_object is None:
            self.get_bel_ground_object()
        if self.depth <= 1:
            return False
        sibling = self.bel_ground_platform.children
        cnt = 0
        for child in sibling:
            if child.name.split("_")[0] == self.name.split("_")[0]:
                cnt += 1
        return cnt > 1

    def rename_with_name_map(self, name_map):
        self.name = StringConvertor.rename_with_map(self.name, name_map)
        self.object.name = StringConvertor.rename_with_map(self.object.name, name_map)
        if self.bel_ground_object is not None:
            self.bel_ground_object.name = StringConvertor.rename_with_map(
                self.bel_ground_object.name, name_map
            )
        if self.bel_ground_platform is not None:
            self.bel_ground_platform.name = StringConvertor.rename_with_map(
                self.bel_ground_platform.name, name_map
            )
        for child in self.children:
            child.name = StringConvertor.rename_with_map(child.name, name_map)
        for child in self.all_children:
            child.name = StringConvertor.rename_with_map(child.name, name_map)
        for platform_child in self.children_per_platform:
            for child in platform_child:
                child.name = StringConvertor.rename_with_map(child.name, name_map)
        for platform_child in self.offspring_per_platform:
            for child in platform_child:
                child.name = StringConvertor.rename_with_map(child.name, name_map)
        for platform in self.own_platform:
            platform.name = StringConvertor.rename_with_map(platform.name, name_map)

        return self

    def get_info(self):
        major_category = StringConvertor.get_category(self.name)

    def get_bel_ground_object(self):
        #   if self.bel_ground_object is not None or self.depth <= 1:
        #        return self.bel_ground_object

        tmp_node = self.parent
        tmp_platform = self.on_platform
        while tmp_node.depth > 1:
            tmp_platform = tmp_node.on_platform
            tmp_node = tmp_node.parent

        self.bel_ground_platform = tmp_platform
        self.bel_ground_object = tmp_node
        return self.bel_ground_object

    def get_name_for_interaction(self):
        name_for_interaction = self.name

        return f"{StringConvertor.get_noslash_name_wo_id(name_for_interaction)}"

    def get_bel_ground_platform(self):
        #       if self.bel_ground_platform is not None or self.depth <= 1:
        #         return self.bel_ground_platform
        tmp_node = self.parent
        tmp_platform = self.on_platform
        while tmp_node.depth > 1:
            tmp_platform = tmp_node.on_platform
            tmp_node = tmp_node.parent

        self.bel_ground_platform = tmp_platform
        self.bel_ground_object = tmp_node
        return self.bel_ground_platform

    def get_child_name_list(self):
        return [child.name for child in self.children]

    def get_children_per_platform(self):
        if len(self.children_per_platform) == 0:
            self.update_children_belong_platform()
        return self.children_per_platform

    def get_object_id_on_platform(self):
        source_item_id = [
            id
            for id, _ in enumerate(self.get_bel_ground_platform().children)
            if _.name == self.name
        ]
        if len(source_item_id) == 0:
            glog.warning("get_object_id_on_platform: object not found")
            return -1
        return source_item_id[0] + 1

    def get_object_id(self):
        if self.bel_ground_object is None:
            self.get_bel_ground_object()
        return self.bel_ground_object.object_id

    def is_freespace_big_enough(self, freespace_id, min_width=0.03):
        # return xth freespace with enough width
        # freespace_dir: 0-7
        if self.free_space[freespace_id]["Critical_space"] == []:
            return False
        width = np.linalg.norm(
            self.free_space[freespace_id]["Critical_space"][0]
            - self.free_space[freespace_id]["Critical_space"][1]
        )
        height = np.linalg.norm(
            self.free_space[freespace_id]["Critical_space"][1]
            - self.free_space[freespace_id]["Critical_space"][2]
        )
        if width < min_width or height < min_width:
            return False
        return True

    def get_on_picture_freespace_id(self, dir, min_width=0.03):
        # return xth freespace with enough width
        # picture_id: 1-8
        res = 0
        for i in range(0, dir + 1):
            if self.is_freespace_big_enough(i, min_width):
                res += 1

        return -1 if not self.is_freespace_big_enough(dir) else res

    def get_freespace_id_on_picture(self, freespace_dir, min_width=0.03):
        # return xth freespace with enough width
        # freespace_dir: 1-8
        for dir in range(8):
            if self.is_freespace_big_enough(dir, min_width):
                freespace_dir -= 1
                if freespace_dir == 0:
                    return dir

        glog.warning("freespace dir might be illegal")
        return -1

    def update_children_belong_platform(self) -> None:
        self.children_per_platform = []
        self.offspring_per_platform = []
        for platform_id, platform in enumerate(self.own_platform):
            child_per_platform = []
            offspring_per_platform = []
            for child in self.children:
                if child.on_platform.name == platform.name:
                    child_per_platform.append(child)
                    child_queue = []
                    child_queue.append(child)
                    while len(child_queue) > 0:
                        cur_child = child_queue.pop(0)
                        offspring_per_platform.append(cur_child)
                        child_queue.extend(cur_child.children)
            self.children_per_platform.append(child_per_platform)
            self.offspring_per_platform.append(offspring_per_platform)
            self.own_platform[platform_id].children = offspring_per_platform
        return None

    def is_multilayer_object(self):

        real_layers = 0
        for platform in self.own_platform:
            real_layers += (
                len(
                    [
                        dir
                        for dir in range(0, 8, 2)
                        if self.freespace_is_standable(dir)
                        and platform.freespace_is_visible(dir)
                    ]
                )
                > 0
            )

        return self.depth == 1 and real_layers > 1

    def get_categories_and_objects_for_mlo(self):
        major_category = set()
        minor_category = set()
        for platform in self.own_platform:
            major_category_list, minor_category_list, _ = (
                platform.get_categories_and_objects()
            )
            major_category.update(major_category_list)
            minor_category.update(minor_category_list)

        major_category = list(major_category)
        minor_category = list(minor_category)
        return major_category, minor_category

    def renew_heading(self, heading):
        self.heading = heading
        self.object.convex_hull_2d.heading = heading
        if self.name != "GROUND":
            cos_theta, sin_theta = float(heading[0]), float(heading[1])
            points = self.object.convex_hull_2d.get_headed_bbox_instance()

            def extend_point(p, direction):
                if direction == "right":
                    normal = np.array([cos_theta, sin_theta])
                elif direction == "left":
                    normal = np.array([-cos_theta, -sin_theta])
                elif direction == "front":
                    normal = np.array([-sin_theta, cos_theta])
                elif direction == "rear":
                    normal = np.array([sin_theta, -cos_theta])
                elif direction == "rear-left":
                    normal = np.array([sin_theta, -cos_theta]) + np.array(
                        [-cos_theta, -sin_theta]
                    )
                elif direction == "front-left":
                    normal = np.array([-sin_theta, cos_theta]) + np.array(
                        [-cos_theta, -sin_theta]
                    )
                elif direction == "front-right":
                    normal = np.array([-sin_theta, cos_theta]) + np.array(
                        [cos_theta, sin_theta]
                    )
                elif direction == "rear-right":
                    normal = np.array([sin_theta, -cos_theta]) + np.array(
                        [cos_theta, sin_theta]
                    )
                else:
                    return np.array(p)

                return np.array(p) + 10 * np.array(normal)

            # point older: rear_left, front_left, front_right, rear_right
            self.free_space = [
                {
                    "Objects": [],
                    "Available_space": [
                        extend_point(points[0], "rear"),
                        points[0],
                        points[3],
                        extend_point(points[3], "rear"),
                    ],
                    "Critical_space": [
                        extend_point(points[0], "rear"),
                        points[0],
                        points[3],
                        extend_point(points[3], "rear"),
                    ],
                },
                {
                    "Objects": [],
                    "Available_space": [
                        extend_point(points[0], "rear-left"),
                        extend_point(points[0], "left"),
                        points[0],
                        extend_point(points[0], "rear"),
                    ],
                    "Critical_space": [
                        extend_point(points[0], "rear-left"),
                        extend_point(points[0], "left"),
                        points[0],
                        extend_point(points[0], "rear"),
                    ],
                },
                {
                    "Objects": [],
                    "Available_space": [
                        extend_point(points[0], "left"),
                        extend_point(points[1], "left"),
                        points[1],
                        points[0],
                    ],
                    "Critical_space": [
                        extend_point(points[0], "left"),
                        extend_point(points[1], "left"),
                        points[1],
                        points[0],
                    ],
                },
                {
                    "Objects": [],
                    "Available_space": [
                        extend_point(points[1], "left"),
                        extend_point(points[1], "front-left"),
                        extend_point(points[1], "front"),
                        points[1],
                    ],
                    "Critical_space": [
                        extend_point(points[1], "left"),
                        extend_point(points[1], "front-left"),
                        extend_point(points[1], "front"),
                        points[1],
                    ],
                },
                {
                    "Objects": [],
                    "Available_space": [
                        points[1],
                        extend_point(points[1], "front"),
                        extend_point(points[2], "front"),
                        points[2],
                    ],
                    "Critical_space": [
                        points[1],
                        extend_point(points[1], "front"),
                        extend_point(points[2], "front"),
                        points[2],
                    ],
                },
                {
                    "Objects": [],
                    "Available_space": [
                        points[2],
                        extend_point(points[2], "front"),
                        extend_point(points[2], "front-right"),
                        extend_point(points[2], "right"),
                    ],
                    "Critical_space": [
                        points[2],
                        extend_point(points[2], "front"),
                        extend_point(points[2], "front-right"),
                        extend_point(points[2], "right"),
                    ],
                },
                {
                    "Objects": [],
                    "Available_space": [
                        points[3],
                        points[2],
                        extend_point(points[2], "right"),
                        extend_point(points[3], "right"),
                    ],
                    "Critical_space": [
                        points[3],
                        points[2],
                        extend_point(points[2], "right"),
                        extend_point(points[3], "right"),
                    ],
                },
                {
                    "Objects": [],
                    "Available_space": [
                        extend_point(points[3], "rear"),
                        points[3],
                        extend_point(points[3], "right"),
                        extend_point(points[3], "rear-right"),
                    ],
                    "Critical_space": [
                        extend_point(points[3], "rear"),
                        points[3],
                        extend_point(points[3], "right"),
                        extend_point(points[3], "rear-right"),
                    ],
                },
            ]

    def reset_free_space(self):
        cos_theta, sin_theta = self.heading[0], self.heading[1]
        points = self.object.convex_hull_2d.get_headed_bbox_instance()

        def extend_point(p, direction):
            if direction == "right":
                normal = np.array([cos_theta, sin_theta])
            elif direction == "left":
                normal = np.array([-cos_theta, -sin_theta])
            elif direction == "front":
                normal = np.array([-sin_theta, cos_theta])
            elif direction == "rear":
                normal = np.array([sin_theta, -cos_theta])
            elif direction == "rear-left":
                normal = np.array([sin_theta, -cos_theta]) + np.array(
                    [-cos_theta, -sin_theta]
                )
            elif direction == "front-left":
                normal = np.array([-sin_theta, cos_theta]) + np.array(
                    [-cos_theta, -sin_theta]
                )
            elif direction == "front-right":
                normal = np.array([-sin_theta, cos_theta]) + np.array(
                    [cos_theta, sin_theta]
                )
            elif direction == "rear-right":
                normal = np.array([sin_theta, -cos_theta]) + np.array(
                    [cos_theta, sin_theta]
                )
            else:
                return np.array(p)
            return np.array(p) + 10 * np.array(normal)

        # point older: rear_left, front_left, front_right, rear_right
        self.free_space = [
            {
                "Objects": [],
                "Available_space": [
                    extend_point(points[0], "rear"),
                    points[0],
                    points[3],
                    extend_point(points[3], "rear"),
                ],
                "Critical_space": [
                    extend_point(points[0], "rear"),
                    points[0],
                    points[3],
                    extend_point(points[3], "rear"),
                ],
            },
            {
                "Objects": [],
                "Available_space": [
                    extend_point(points[0], "rear-left"),
                    extend_point(points[0], "left"),
                    points[0],
                    extend_point(points[0], "rear"),
                ],
                "Critical_space": [
                    extend_point(points[0], "rear-left"),
                    extend_point(points[0], "left"),
                    points[0],
                    extend_point(points[0], "rear"),
                ],
            },
            {
                "Objects": [],
                "Available_space": [
                    extend_point(points[0], "left"),
                    extend_point(points[1], "left"),
                    points[1],
                    points[0],
                ],
                "Critical_space": [
                    extend_point(points[0], "left"),
                    extend_point(points[1], "left"),
                    points[1],
                    points[0],
                ],
            },
            {
                "Objects": [],
                "Available_space": [
                    extend_point(points[1], "left"),
                    extend_point(points[1], "front-left"),
                    extend_point(points[1], "front"),
                    points[1],
                ],
                "Critical_space": [
                    extend_point(points[1], "left"),
                    extend_point(points[1], "front-left"),
                    extend_point(points[1], "front"),
                    points[1],
                ],
            },
            {
                "Objects": [],
                "Available_space": [
                    points[1],
                    extend_point(points[1], "front"),
                    extend_point(points[2], "front"),
                    points[2],
                ],
                "Critical_space": [
                    points[1],
                    extend_point(points[1], "front"),
                    extend_point(points[2], "front"),
                    points[2],
                ],
            },
            {
                "Objects": [],
                "Available_space": [
                    points[2],
                    extend_point(points[2], "front"),
                    extend_point(points[2], "front-right"),
                    extend_point(points[2], "right"),
                ],
                "Critical_space": [
                    points[2],
                    extend_point(points[2], "front"),
                    extend_point(points[2], "front-right"),
                    extend_point(points[2], "right"),
                ],
            },
            {
                "Objects": [],
                "Available_space": [
                    points[3],
                    points[2],
                    extend_point(points[2], "right"),
                    extend_point(points[3], "right"),
                ],
                "Critical_space": [
                    points[3],
                    points[2],
                    extend_point(points[2], "right"),
                    extend_point(points[3], "right"),
                ],
            },
            {
                "Objects": [],
                "Available_space": [
                    extend_point(points[3], "rear"),
                    points[3],
                    extend_point(points[3], "right"),
                    extend_point(points[3], "rear-right"),
                ],
                "Critical_space": [
                    extend_point(points[3], "rear"),
                    points[3],
                    extend_point(points[3], "right"),
                    extend_point(points[3], "rear-right"),
                ],
            },
        ]

    def update_free_space(self, other_node_name, direction):
        assert (
            len(self.free_space[direction]["Available_space"]) == 4
            and direction >= 0
            and direction < 8
        )
        # print(self.name, direction, other_node.name, self.free_space[EIGHT_DIRECTIONS.index(direction)]["Available_space"])
        self.free_space[direction]["Objects"].append(other_node_name)

    def get_critical_space_len(self, direction):
        return [
            np.linalg.norm(
                self.free_space[direction]["Critical_space"][0]
                - self.free_space[direction]["Critical_space"][1]
            ),
            np.linalg.norm(
                self.free_space[direction]["Critical_space"][1]
                - self.free_space[direction]["Critical_space"][2]
            ),
        ]

    def get_multiple_standing_point(

        self, platform_rect, standing_direction, max_visclear_width=0.8

    ):
        bbox_line = self.get_bbox_line(standing_direction)
        perpendicular_line = (
            self.get_bbox_line((standing_direction + 2) % 8)[1]
            - self.get_bbox_line((standing_direction + 2) % 8)[0]
        )
        perpendicular_line_normalized = (
            perpendicular_line
            / np.linalg.norm(perpendicular_line)
            * self.robot_fit_threshold
        )
        length = int(
            np.ceil(np.linalg.norm(bbox_line[1] - bbox_line[0]) / max_visclear_width)
        )
        return [
            bbox_line[1]
            + (i * 2 + 1) / (length * 2) * (bbox_line[0] - bbox_line[1])
            + perpendicular_line_normalized
            for i in range(length)
        ]

    def get_standing_point(self, direction):
        if direction == 0:
            return (
                self.free_space[0]["Critical_space"][1]
                + self.free_space[0]["Critical_space"][2]
            ) / 2 + (
                self.free_space[0]["Available_space"][0]
                - self.free_space[0]["Available_space"][1]
            ) * TreeNode.robot_fit_threshold / np.linalg.norm(
                self.free_space[0]["Available_space"][0]
                - self.free_space[0]["Available_space"][1]
            )
        elif direction == 2:
            return (
                self.free_space[2]["Critical_space"][2]
                + self.free_space[2]["Critical_space"][3]
            ) / 2 + (
                self.free_space[2]["Available_space"][1]
                - self.free_space[2]["Available_space"][2]
            ) * TreeNode.robot_fit_threshold / np.linalg.norm(
                self.free_space[2]["Available_space"][1]
                - self.free_space[2]["Available_space"][2]
            )
        elif direction == 4:
            return (
                self.free_space[4]["Critical_space"][3]
                + self.free_space[4]["Critical_space"][0]
            ) / 2 + (
                self.free_space[4]["Available_space"][2]
                - self.free_space[4]["Available_space"][3]
            ) * TreeNode.robot_fit_threshold / np.linalg.norm(
                self.free_space[4]["Available_space"][2]
                - self.free_space[4]["Available_space"][3]
            )
        elif direction == 6:
            return (
                self.free_space[6]["Critical_space"][0]
                + self.free_space[6]["Critical_space"][1]
            ) / 2 + (
                self.free_space[6]["Available_space"][3]
                - self.free_space[6]["Available_space"][0]
            ) * TreeNode.robot_fit_threshold / np.linalg.norm(
                self.free_space[6]["Available_space"][3]
                - self.free_space[6]["Available_space"][0]
            )
        else:
            return None

    def get_center(self):
        return (
            self.free_space[0]["Critical_space"][1]
            + self.free_space[4]["Critical_space"][3]
        ) / 2

    def get_bbox(self):
        return [
            self.free_space[0]["Critical_space"][1],
            self.free_space[2]["Critical_space"][2],
            self.free_space[4]["Critical_space"][3],
            self.free_space[6]["Critical_space"][0],
        ]

    def get_bbox_line(self, dir):
        if dir % 2 != 0 or dir < 0 or dir > 7:
            return None

        bbox = self.get_bbox()
        return [bbox[dir // 2], bbox[(dir // 2 + 3) % 4]]

    def get_bbox_line_length(self, dir):
        if dir % 2 != 0 or dir < 0 or dir > 7:
            return None

        bbox = self.get_bbox()
        return np.linalg.norm(bbox[dir // 2] - bbox[(dir // 2 + 3) % 4])

    def get_critical_space_center(self, direction):
        assert (
            len(self.free_space[direction]["Critical_space"]) == 4
            and direction >= 0
            and direction < 8
        )
        return np.mean(self.free_space[direction]["Critical_space"], axis=0)

    def get_critical_space_left(self, direction):
        assert (
            len(self.free_space[direction]["Critical_space"]) == 4
            and direction >= 0
            and direction < 8
        )
        return (
            np.mean(self.free_space[direction]["Critical_space"][:2], axis=0)
            + np.mean(self.free_space[direction]["Critical_space"], axis=0)
        ) / 2

    def get_critical_space_right(self, direction):
        assert (
            len(self.free_space[direction]["Critical_space"]) == 4
            and direction >= 0
            and direction < 8
        )
        return (
            np.mean(self.free_space[direction]["Critical_space"][2:], axis=0)
            + np.mean(self.free_space[direction]["Critical_space"], axis=0)
        ) / 2

    def get_num_of_critical_space(self):
        cnt = 0
        for dir in range(8):
            if self.is_freespace_big_enough(dir):
                cnt += 1
        return cnt

    def freespace_is_visible(self, standing_direction, platform_id):
        assert (
            standing_direction >= 0
            and standing_direction < 8
            and standing_direction % 2 == 0
        )
        dir = EIGHT_DIRECTIONS[standing_direction]
        plat = self.own_platform[platform_id]
        if dir not in plat.visible_directions:
            return False
        return plat.visible_directions[dir]

    def freespace_is_standable(self, direction):
        if self.free_space is None:
            return False
        assert (
            len(self.free_space[direction]["Critical_space"]) == 4
            and direction >= 0
            and direction < 8
        )

        if direction == 0 or direction == 4:
            return (
                np.linalg.norm(
                    self.free_space[direction]["Critical_space"][0]
                    - self.free_space[direction]["Critical_space"][1]
                )
                > TreeNode.robot_fit_threshold
            )
        elif direction == 2 or direction == 6:
            return (
                np.linalg.norm(
                    self.free_space[direction]["Critical_space"][1]
                    - self.free_space[direction]["Critical_space"][2]
                )
                > TreeNode.robot_fit_threshold
            )
        else:
            return 0

    def get_critical_space_bbox(self):
        return [
            self.free_space[1]["Critical_space"][0],
            self.free_space[3]["Critical_space"][1],
            self.free_space[5]["Critical_space"][2],
            self.free_space[7]["Critical_space"][3],
        ]

    def sweep_platform(self):
        max_size = -INF
        for platform in self.own_platform:
            max_size = max(
                max_size,
                (platform.bbox[1][0] - platform.bbox[0][0])
                * (platform.bbox[1][1] - platform.bbox[0][1]),
            )
        self.own_platform = [
            platform
            for platform in self.own_platform
            if (platform.bbox[1][0] - platform.bbox[0][0])
            * (platform.bbox[1][1] - platform.bbox[0][1])
            > max_size * 0.25
            and platform.convex_hull_2d.vertices.shape[0] >= 4
        ]

    def at_which_part(self):
        platform_bbox = self.on_platform.convex_hull_2d.get_headed_bbox_instance()
        vertices = self.object.convex_hull_2d.vertices
        item_bbox = self.object.convex_hull_2d.get_headed_bbox_instance()
        vertices = np.append(vertices, item_bbox, axis=0)
        direction_mappings = {
            (0, 0): "rear-left",
            (0, 1): "rear",
            (0, 2): "rear-right",
            (1, 0): "left",
            (1, 1): "center",
            (1, 2): "right",
            (2, 0): "front-left",
            (2, 1): "front",
            (2, 2): "front-right",
        }
        result_direction_list = []
        for i in range(3):
            for j in range(3):
                rect = [
                    platform_bbox[0]
                    + i * (platform_bbox[1] - platform_bbox[0]) / 3
                    + j * (platform_bbox[3] - platform_bbox[0]) / 3,
                    platform_bbox[0]
                    + (i + 1) * (platform_bbox[1] - platform_bbox[0]) / 3
                    + j * (platform_bbox[3] - platform_bbox[0]) / 3,
                    platform_bbox[0]
                    + (i + 1) * (platform_bbox[1] - platform_bbox[0]) / 3
                    + (j + 1) * (platform_bbox[3] - platform_bbox[0]) / 3,
                    platform_bbox[0]
                    + i * (platform_bbox[1] - platform_bbox[0]) / 3
                    + (j + 1) * (platform_bbox[3] - platform_bbox[0]) / 3,
                ]
                for vertex in vertices:
                    if Basic2DGeometry.is_inside_rectangle(vertex, rect):
                        result_direction_list.append(direction_mappings[(i, j)])
                        break

        return result_direction_list

    # def get_fitable_pose(self, avl_height, place_bbox):
    #     if self.top - self.bottom > avl_height:
    #         return None
    #     return self.object.convex_hull_2d.get_fit_in_translation(place_bbox)

    # def get_fitable_pose_for_list(self, avl_height, rectangle_list, heading=None):
    #     if self.top - self.bottom > avl_height:
    #         return None
    #     if heading is not None:
    #         self.object.convex_hull_2d.heading = heading
    #     target_rect = self.object.convex_hull_2d.get_headed_bbox_instance()
    #     placement = Basic2DGeometry.find_optimal_placement_with_rotation(
    #         self.object.convex_hull_2d, rectangle_list
    #     )
    #     pass

    def rotate_free_space(self, front):
        if front == 0:
            return
        elif front == 4:
            self.heading = [-self.heading[0], -self.heading[1]]
            self.free_space = self.free_space[4:] + self.free_space[:4]
            for platform in self.own_platform:
                platform.heading = self.heading
                platform.visible_directions = (
                    platform.visible_directions[2:] + platform.visible_directions[:2]
                )

            for i in range(8):
                self.free_space[i]["Available_space"] = (
                    self.free_space[i]["Available_space"][2:]
                    + self.free_space[i]["Available_space"][:2]
                )
                self.free_space[i]["Critical_space"] = (
                    self.free_space[i]["Critical_space"][2:]
                    + self.free_space[i]["Critical_space"][:2]
                )
        elif front == 2:
            self.heading = [self.heading[1], -self.heading[0]]
            self.free_space = self.free_space[2:] + self.free_space[:2]

            for platform in self.own_platform:
                platform.heading = self.heading
                platform.visible_directions = (
                    platform.visible_directions[1:] + platform.visible_directions[:1]
                )

            for i in range(8):
                self.free_space[i]["Available_space"] = (
                    self.free_space[i]["Available_space"][1:]
                    + self.free_space[i]["Available_space"][:1]
                )
                self.free_space[i]["Critical_space"] = (
                    self.free_space[i]["Critical_space"][1:]
                    + self.free_space[i]["Critical_space"][:1]
                )
        elif front == 6:
            self.heading = [-self.heading[1], self.heading[0]]
            self.free_space = self.free_space[6:] + self.free_space[:6]

            for platform in self.own_platform:
                platform.heading = self.heading
                platform.visible_directions = (
                    platform.visible_directions[3:] + platform.visible_directions[:3]
                )
            for i in range(8):
                self.free_space[i]["Available_space"] = (
                    self.free_space[i]["Available_space"][3:]
                    + self.free_space[i]["Available_space"][:3]
                )
                self.free_space[i]["Critical_space"] = (
                    self.free_space[i]["Critical_space"][3:]
                    + self.free_space[i]["Critical_space"][:3]
                )

    def rotate_free_space_for_all_children(self, front):
        self.rotate_free_space(front)
        for child in self.children:
            child.rotate_free_space_for_all_children(front)

    def set_free_space_to_platform(self, platform_hull):
        platform_hull.heading = self.heading
        platform_bbox = platform_hull.get_headed_bbox_instance()

        platform_left_side = [platform_bbox[0], platform_bbox[1]]
        platform_right_side = [platform_bbox[2], platform_bbox[3]]
        platform_front_side = [platform_bbox[1], platform_bbox[2]]
        platform_rear_side = [platform_bbox[3], platform_bbox[0]]
        for left_part in range(1, 4):

            space_front_side = [
                self.free_space[left_part]["Available_space"][1],
                self.free_space[left_part]["Available_space"][2],
            ]
            space_rear_side = [
                self.free_space[left_part]["Available_space"][0],
                self.free_space[left_part]["Available_space"][3],
            ]

            intersect_left_front = Basic2DGeometry.intersection_of_line(
                platform_left_side, space_front_side
            )
            intersect_left_rear = Basic2DGeometry.intersection_of_line(
                platform_left_side, space_rear_side
            )
            #    if self.name == 'frl_apartment_choppingboard_02_81':
            #        print(intersect_left_front, platform_left_side, space_front_side)
            if intersect_left_front is not None and intersect_left_rear is not None:
                if Basic2DGeometry.is_on_segment(
                    intersect_left_front, space_front_side
                ) and Basic2DGeometry.is_on_segment(
                    intersect_left_rear, space_rear_side
                ):
                    (
                        self.free_space[left_part]["Available_space"][0],
                        self.free_space[left_part]["Available_space"][1],
                    ) = (intersect_left_rear, intersect_left_front)
                elif Basic2DGeometry.is_on_segment(
                    space_front_side[1], [space_front_side[0], intersect_left_front]
                ):
                    (
                        self.free_space[left_part]["Available_space"][0],
                        self.free_space[left_part]["Available_space"][1],
                    ) = (
                        self.free_space[left_part]["Available_space"][3],
                        self.free_space[left_part]["Available_space"][2],
                    )

        for right_part in range(5, 8):

            space_front_side = [
                self.free_space[right_part]["Available_space"][1],
                self.free_space[right_part]["Available_space"][2],
            ]
            space_rear_side = [
                self.free_space[right_part]["Available_space"][0],
                self.free_space[right_part]["Available_space"][3],
            ]
            intersect_right_front = Basic2DGeometry.intersection_of_line(
                platform_right_side, space_front_side
            )
            intersect_right_rear = Basic2DGeometry.intersection_of_line(
                platform_right_side, space_rear_side
            )
            if intersect_left_front is not None and intersect_left_rear is not None:
                if Basic2DGeometry.is_on_segment(
                    intersect_right_front, space_front_side
                ) and Basic2DGeometry.is_on_segment(
                    intersect_right_rear, space_rear_side
                ):
                    (
                        self.free_space[right_part]["Available_space"][3],
                        self.free_space[right_part]["Available_space"][2],
                    ) = (intersect_right_rear, intersect_right_front)
                elif Basic2DGeometry.is_on_segment(
                    space_front_side[0], [space_front_side[1], intersect_right_front]
                ):
                    (
                        self.free_space[right_part]["Available_space"][2],
                        self.free_space[right_part]["Available_space"][3],
                    ) = (
                        self.free_space[right_part]["Available_space"][1],
                        self.free_space[right_part]["Available_space"][0],
                    )

        for front_part in range(3, 6):

            space_left_side = [
                self.free_space[front_part]["Available_space"][0],
                self.free_space[front_part]["Available_space"][1],
            ]
            space_right_side = [
                self.free_space[front_part]["Available_space"][3],
                self.free_space[front_part]["Available_space"][2],
            ]
            intersect_front_left = Basic2DGeometry.intersection_of_line(
                platform_front_side, space_left_side
            )
            intersect_front_right = Basic2DGeometry.intersection_of_line(
                platform_front_side, space_right_side
            )
            if intersect_front_left is not None and intersect_front_right is not None:
                if Basic2DGeometry.is_on_segment(
                    intersect_front_left, space_left_side
                ) and Basic2DGeometry.is_on_segment(
                    intersect_front_right, space_right_side
                ):
                    (
                        self.free_space[front_part]["Available_space"][1],
                        self.free_space[front_part]["Available_space"][2],
                    ) = (intersect_front_left, intersect_front_right)
                elif Basic2DGeometry.is_on_segment(
                    space_left_side[0], [space_left_side[1], intersect_front_left]
                ):
                    (
                        self.free_space[front_part]["Available_space"][1],
                        self.free_space[front_part]["Available_space"][2],
                    ) = (
                        self.free_space[front_part]["Available_space"][0],
                        self.free_space[front_part]["Available_space"][3],
                    )

        for rear_part in [7, 0, 1]:

            space_left_side = [
                self.free_space[rear_part]["Available_space"][0],
                self.free_space[rear_part]["Available_space"][1],
            ]
            space_right_side = [
                self.free_space[rear_part]["Available_space"][3],
                self.free_space[rear_part]["Available_space"][2],
            ]
            intersect_rear_left = Basic2DGeometry.intersection_of_line(
                platform_rear_side, space_left_side
            )
            intersect_rear_right = Basic2DGeometry.intersection_of_line(
                platform_rear_side, space_right_side
            )
            if intersect_rear_left is not None and intersect_rear_right is not None:
                if Basic2DGeometry.is_on_segment(
                    intersect_rear_left, space_left_side
                ) and Basic2DGeometry.is_on_segment(
                    intersect_rear_right, space_right_side
                ):
                    (
                        self.free_space[rear_part]["Available_space"][0],
                        self.free_space[rear_part]["Available_space"][3],
                    ) = (intersect_rear_left, intersect_rear_right)
                elif Basic2DGeometry.is_on_segment(
                    space_left_side[1], [space_left_side[0], intersect_rear_left]
                ):
                    (
                        self.free_space[rear_part]["Available_space"][0],
                        self.free_space[rear_part]["Available_space"][3],
                    ) = (
                        self.free_space[rear_part]["Available_space"][1],
                        self.free_space[rear_part]["Available_space"][2],
                    )

        pass

    def sync_critical_free_space(self):
        for dir in range(8):
            self.free_space[dir]["Critical_space"] = copy.deepcopy(
                self.free_space[dir]["Available_space"]
            )

    def clean_free_space(self):
        if self.free_space == None:
            return

        def rotate_point_clockwise(point, theta):
            rotation_matrix = np.array(
                [[np.cos(theta), np.sin(theta)], [-np.sin(theta), np.cos(theta)]]
            )
            return np.dot(rotation_matrix, point)

        def has_area(points, heading):
            left_rear, left_front, right_front, right_rear = points
            cos_theta, sin_theta = heading[0], heading[1]

            # calculate the angle between the x-axis and the heading
            theta = np.arctan2(sin_theta, cos_theta)

            left_rear_rotated = rotate_point_clockwise(left_rear, theta)
            left_front_rotated = rotate_point_clockwise(left_front, theta)
            right_rear_rotated = rotate_point_clockwise(right_rear, theta)
            right_front_rotated = rotate_point_clockwise(right_front, theta)

            if (
                right_rear_rotated[0] < left_rear_rotated[0]
                or right_front_rotated[0] < left_front_rotated[0]
            ):
                return False
            if (
                right_rear_rotated[1] > left_rear_rotated[1]
                or right_front_rotated[1] < left_front_rotated[1]
            ):
                return False

            return True

        for direction in range(len(self.free_space)):
            if self.free_space[direction]["Available_space"] != "not available":
                if not has_area(
                    self.free_space[direction]["Available_space"], self.heading
                ):
                    self.free_space[direction]["Objects"] = []
                    self.free_space[direction]["Available_space"] = "not available"
            if self.free_space[direction]["Critical_space"] != "not available":
                if not has_area(
                    self.free_space[direction]["Critical_space"], self.heading
                ):
                    self.free_space[direction]["Objects"] = []
                    self.free_space[direction]["Critical_space"] = "not available"

    def add_child(self, child_node):
        child_node.parent = self
        self.children.append(child_node)

    def get_child(self, child_name):
        for child in self.children:
            if child.name == child_name:
                return child

    def update_all_child(self):
        self.all_children = []

        que = [self]
        while que:
            current_node = que.pop(0)
            self.all_children.append(current_node)
            que.extend(current_node.children)

        self.all_children = self.all_children[1:]  # Exclude the root node itself

    def get_available_place_for_object(self, object_node):
        self.update_children_belong_platform()
        result = []
        for i, platform_children in enumerate(self.own_platform):
            platform_result = []
            obstacle_obj_list = []
            for child in self.children_per_platform[i]:
                if child.name != object_node.name:
                    obstacle_obj_list.append(child)
            platform_result = (
                platform_children.get_all_available_freespace_combinations(
                    obstacle_obj_list, object_node.object, min_step=0.01
                )
            )
            result.append(platform_result)

        return result

    def judge_critical_direction(self, node_name, mild=True):
        dir_list = []
        for dir in range(8):
            for id, dir_node in enumerate(self.free_space[dir]["Objects"]):
                if dir_node.name == node_name:
                    if mild:
                        dir_list.append(dir)
                        break
                    tmp_freespace = self.free_space[dir]["Available_space"]
                    all_siblings = self.free_space[dir]["Objects"]
                    all_siblings[id], all_siblings[-1] = (
                        all_siblings[-1],
                        all_siblings[id],
                    )

                    if dir in [1, 2, 3]:
                        for id, object_node in enumerate(all_siblings):
                            near_side = [
                                tmp_freespace[3],
                                tmp_freespace[2],
                            ]
                            far_side = [
                                tmp_freespace[0],
                                tmp_freespace[1],
                            ]
                            new_near_side, new_far_side = (
                                object_node.object.convex_hull_2d.cut_free_space_with_convex(
                                    near_side, far_side
                                )
                            )
                            if id == len(self.free_space[dir]["Objects"]) - 1:
                                if np.allclose(new_far_side, far_side) == False:
                                    dir_list.append(dir)

                            tmp_freespace = [
                                new_far_side[0],
                                new_far_side[1],
                                new_near_side[1],
                                new_near_side[0],
                            ]
                    if dir in [3, 4, 5]:
                        for id, object_node in enumerate(all_siblings):
                            near_side = [
                                tmp_freespace[0],
                                tmp_freespace[3],
                            ]
                            far_side = [
                                tmp_freespace[1],
                                tmp_freespace[2],
                            ]
                            new_near_side, new_far_side = (
                                object_node.object.convex_hull_2d.cut_free_space_with_convex(
                                    near_side, far_side
                                )
                            )
                            if id == len(self.free_space[dir]["Objects"]) - 1:
                                if np.allclose(new_far_side, far_side) == False:
                                    dir_list.append(dir)

                            tmp_freespace = [
                                new_near_side[0],
                                new_far_side[0],
                                new_far_side[1],
                                new_near_side[1],
                            ]
                    if dir in [5, 6, 7]:
                        for id, object_node in enumerate(all_siblings):
                            near_side = [
                                tmp_freespace[0],
                                tmp_freespace[1],
                            ]
                            far_side = [
                                tmp_freespace[3],
                                tmp_freespace[2],
                            ]
                            new_near_side, new_far_side = (
                                object_node.object.convex_hull_2d.cut_free_space_with_convex(
                                    near_side, far_side
                                )
                            )
                            if id == len(self.free_space[dir]["Objects"]) - 1:
                                if np.allclose(new_far_side, far_side) == False:
                                    dir_list.append(dir)

                            tmp_freespace = [
                                new_near_side[0],
                                new_near_side[1],
                                new_far_side[1],
                                new_far_side[0],
                            ]
                    if dir in [7, 0, 1]:
                        for id, object_node in enumerate(all_siblings):
                            near_side = [
                                tmp_freespace[1],
                                tmp_freespace[2],
                            ]
                            far_side = [
                                tmp_freespace[0],
                                tmp_freespace[3],
                            ]
                            new_near_side, new_far_side = (
                                object_node.object.convex_hull_2d.cut_free_space_with_convex(
                                    near_side, far_side
                                )
                            )
                            if id == len(self.free_space[dir]["Objects"]) - 1:
                                if np.allclose(new_far_side, far_side) == False:
                                    dir_list.append(dir)

                            tmp_freespace = [
                                new_far_side[0],
                                new_near_side[0],
                                new_near_side[1],
                                new_far_side[1],
                            ]

                    break
        dir_list = list(set(dir_list))
        return dir_list

    def remove_child(self, child_node):
        for i in range(len(self.children)):
            if self.children[i].name == child_node.name:
                self.children[i].parent = None
                self.children.pop(i)
                break

    def display_unique_information(self, ambiguous=False):
        if self.depth == 0:
            return ""
        else:
            str_buffer = ""
            str_buffer += f"object: {self.name}\n"
            str_buffer += f"belonged_to_platform: {self.on_platform.name if self.on_platform is not None and self.depth > 1 else None}\n"
            if self.is_ambiguous:
                str_buffer += f"Its surroundings\n"
                for dir in range(8):
                    if (
                        self.free_space[dir]["Objects"] is not None
                        and len(self.free_space[dir]["Objects"]) > 0
                    ):
                        str_buffer += f'{EIGHT_DIRECTIONS[dir]}, {[objs.name for objs in self.free_space[dir]["Objects"]]}\n'
            if self.depth > 1:
                str_buffer += f"Its direct parent:\n"
            str_buffer += self.parent.display_unique_information(ambiguous) + "\n"

        return str_buffer

    def auto_take_non_ground_object_picture(

        self,

        scene,

        view="human_full",  # 'human_focus', 'human_full', 'top_focus', 'top_full'

        mark_object=False,  # if True, mark all the object on the same platform with cuboid.

        only_mark_itself=False,  # if True, only mark itself

        mark_freespace=False,

        standing_direction=None,

        width=640,

        height=480,

        focus_ratio=0.8,

        fovy_range=[np.deg2rad(5), np.deg2rad(60)],

        save_path=None,

    ):
        if self.depth == 1:
            glog.error("This function is only for non-ground object")
            return None
        self.update_children_belong_platform()
        bel_ground_node = self.get_bel_ground_object()
        bel_ground_node.update_children_belong_platform()
        bel_ground_platform = self.get_bel_ground_platform()
        if bel_ground_node is None:
            glog.error("This non-ground object has no ground node")
            return None

        bel_ground_platform_id = int(bel_ground_platform.name.split("_")[-1])

        if standing_direction is None:
            standing_direction = None
            for dir in range(0, 8, 2):
                if (
                    bel_ground_node.is_freespace_big_enough(dir)
                    and bel_ground_node.freespace_is_standable(dir)
                    and bel_ground_node.freespace_is_visible(
                        dir, bel_ground_platform_id
                    )
                ):
                    standing_direction = dir
                    break
            if standing_direction is None:
                glog.error(
                    f"This object {self.name} has no available standing direction"
                )

                standing_direction = 0
        # import ipdb; ipdb.set_trace()

        self_center = self.get_center()
        border = bel_ground_platform.get_bbox_line(standing_direction)

        perp_intersection = Basic2DGeometry.get_perpendicular_intersection(
            self_center, border
        )

        camera_xy = (
            perp_intersection
            + (perp_intersection - self_center)
            / np.linalg.norm(perp_intersection - self_center)
            * 0.35
            if "top" not in view
            else self.get_center()
        )

        if "top" in view:
            z_range = [
                bel_ground_platform.bottom + 0.1,
                min(3.08, bel_ground_platform.bottom + bel_ground_platform.avl_height),
            ]
        else:
            z_range = [
                self.top + 0.1,
                min(
                    self.top + 0.3,
                    bel_ground_platform.bottom + bel_ground_platform.avl_height,
                ),
            ]
        object_2d_bbox = self.object.convex_hull_2d.get_headed_bbox_instance()
        object_2d_bbox_extended = [
            Basic2DGeometry.get_perpendicular_intersection(point, border)
            for point in object_2d_bbox
        ]

        object_2d_bbox[standing_direction // 2] = object_2d_bbox_extended[
            standing_direction // 2
        ]
        object_2d_bbox[(standing_direction // 2 + 3) % 4] = object_2d_bbox_extended[
            (standing_direction // 2 + 3) % 4
        ]

        object_3d_bbox = [np.append(point, self.bottom) for point in object_2d_bbox] + [
            np.append(point, self.top) for point in object_2d_bbox
        ]
        platform_2d_rect = bel_ground_platform.bbox
        platform_3d_rect = [
            np.append(point, bel_ground_platform.bottom) for point in platform_2d_rect
        ]
        might_mark_cuboid_list = []
        might_mark_freespace_rect_list = []
        might_mark_freespace_id_list = []

        if mark_object:
            bel_ground_platform_idx = int(bel_ground_platform.name.split("_")[-1])
            glog.info(f"bel_ground_platform_idx: {bel_ground_platform_idx}")
            glog.info(
                f"bel_ground_node_per_platform: {(bel_ground_node.children_per_platform)}"
            )
            for child in bel_ground_platform.children:
                child_2d_bbox = child.object.convex_hull_2d.get_headed_bbox_instance()
                child_3d_bbox = [
                    np.append(point, child.bottom) for point in child_2d_bbox
                ] + [np.append(point, child.top) for point in child_2d_bbox]
                might_mark_cuboid_list.append(child_3d_bbox)

        elif only_mark_itself:
            child_2d_bbox = self.object.convex_hull_2d.get_headed_bbox_instance()
            child_3d_bbox = [
                np.append(point, self.bottom) for point in child_2d_bbox
            ] + [np.append(point, self.top) for point in child_2d_bbox]
            might_mark_cuboid_list.append(child_3d_bbox)

        if mark_freespace:
            for dir in range(0, 8, 1):
                if not self.is_freespace_big_enough(dir):
                    continue
                might_mark_freespace_rect_list.append(
                    self.free_space[dir]["Critical_space"]
                )
                might_mark_freespace_id_list.append(dir)

        for i, rect in enumerate(might_mark_freespace_rect_list):
            might_mark_freespace_rect_list[i] = [
                np.append(point, self.bottom) for point in rect
            ]

        # if 'human' in view:
        #     import ipdb
        #     ipdb.set_trace()

        optimal_pose, optimal_fovy = (
            image_render_processor.auto_get_optimal_camera_pose_for_object(
                view=view,
                camera_xy=camera_xy,
                z_range=z_range,
                object_bbox=object_3d_bbox,
                platform_rect=platform_3d_rect,
                width=width,
                height=height,
                fovy_range=fovy_range,
                focus_ratio=focus_ratio,
            )
        )
        rectangle_grey = not mark_freespace

        import os

        current_path = os.path.dirname(os.path.abspath(__file__))
        # glog.info(
        #     [
        #         child.name
        #         for child in self.bel_ground_platform.children
        #         if child.name != self.name
        #     ]
        # )
        img = image_render_processor.auto_render_image_refactored(
            scene,
            name=self.name,
            transparent_item_list=[
                child.name
                for child in self.bel_ground_platform.children
                if child.name != self.name
            ],
            pose=optimal_pose,
            fovy=optimal_fovy,
            width=width,
            height=height,
            might_mark_object_cuboid_list=might_mark_cuboid_list,
            might_mark_freespace_list=might_mark_freespace_rect_list,
            rectangle_grey=rectangle_grey,
            save_path=save_path,
        )

        return img

    def auto_take_ground_object_picture(

        self,

        scene,

        view="human_full",  # 'human_full', 'overall_top',

        mark_object=False,

        mark_freespace=False,

        need_afford_rect=None,  # If not none, only mark the freespaces with size larger than it.

        platform_id=0,

        camera_xy=None,

        standing_direction=0,

        width=1920,

        height=1080,

        focus_ratio=0.5,

        save_path=None,

    ):
        if self.depth != 1:
            glog.warning("This function is only for ground object")
            return None

        self.update_children_belong_platform()

        platform = self.own_platform[platform_id]

        camera_xy = camera_xy if "top" not in view else self.get_center()
        camera_xy_list = []

        if len(platform.standing_point_list) == 0 or not any(
            len(point) > 0 for point in platform.standing_point_list
        ):
            glog.warning(
                f"This ground object {self.name} has no standing_points in direction {standing_direction}"
            )
            return None
        standing_direction //= 2
        if camera_xy is None:
            standing_direction = (
                standing_direction
                if standing_direction < len(platform.standing_point_list)
                else 0
            )
            while len(platform.standing_point_list[standing_direction]) == 0:
                standing_direction = (standing_direction + 1) % len(
                    platform.standing_point_list
                )
            glog.info(
                f"Using standing direction {standing_direction} for platform {platform_id} with name {platform.name}"
            )
            camera_xy = (
                platform.standing_point_list[standing_direction][0]
                + platform.standing_point_list[standing_direction][-1]
            ) / 2
            camera_xy_list = platform.standing_point_list[standing_direction]

        platform_children = platform.children
        platform_children_top_max = (
            max([child.top for child in platform_children])
            if len(platform_children) > 0
            else platform.bottom + 0.35
        )
        z_range = [platform_children_top_max + 0.1, platform_children_top_max + 0.3]

        platform_cuboid_bbox = platform.convex_hull_2d.get_headed_bbox_instance()
        platform_cuboid_bbox_bottom = [
            np.append(point, platform.bottom) for point in platform_cuboid_bbox
        ]
        platform_cuboid_bbox_top = [
            np.append(point, platform.bottom + platform_children_top_max)
            for point in platform_cuboid_bbox
        ]
        platform_cuboid_bbox = platform_cuboid_bbox_bottom + platform_cuboid_bbox_top

        platform_rect = platform_cuboid_bbox[:4]

        might_mark_cuboid_list = []
        might_mark_freespace_rect_list = []
        might_mark_freespace_id_list = []

        if mark_object:
            for child in self.offspring_per_platform[platform_id]:

                child_bbox_2d = child.object.convex_hull_2d.get_headed_bbox_instance()
                child_bbox_3d = [
                    np.append(point, child.bottom) for point in child_bbox_2d
                ] + [np.append(point, child.top) for point in child_bbox_2d]
                might_mark_cuboid_list.append(child_bbox_3d)

        if mark_freespace:

            if need_afford_rect is None:
                for i, j in [
                    (0, 1),
                    (0, 0),
                    (1, 0),
                    (2, 0),
                    (2, 1),
                    (2, 2),
                    (1, 2),
                    (0, 2),
                    (1, 1),
                ]:
                    rect_part_3d = [
                        platform_cuboid_bbox[0]
                        + i * (platform_cuboid_bbox[1] - platform_cuboid_bbox[0]) / 3
                        + j * (platform_cuboid_bbox[3] - platform_cuboid_bbox[0]) / 3,
                        platform_cuboid_bbox[0]
                        + (i + 1)
                        * (platform_cuboid_bbox[1] - platform_cuboid_bbox[0])
                        / 3
                        + j * (platform_cuboid_bbox[3] - platform_cuboid_bbox[0]) / 3,
                        platform_cuboid_bbox[0]
                        + (i + 1)
                        * (platform_cuboid_bbox[1] - platform_cuboid_bbox[0])
                        / 3
                        + (j + 1)
                        * (platform_cuboid_bbox[3] - platform_cuboid_bbox[0])
                        / 3,
                        platform_cuboid_bbox[0]
                        + i * (platform_cuboid_bbox[1] - platform_cuboid_bbox[0]) / 3
                        + (j + 1)
                        * (platform_cuboid_bbox[3] - platform_cuboid_bbox[0])
                        / 3,
                    ]
                    might_mark_freespace_rect_list.append(rect_part_3d)
                    might_mark_freespace_id_list.append(i * 3 + j)
        # import ipdb
        # ipdb.set_trace()
        optimal_pose, optimal_fovy = (
            image_render_processor.auto_get_optimal_camera_pose_for_object(
                view=view,
                camera_xy=camera_xy,
                z_range=z_range,
                object_bbox=platform_cuboid_bbox,
                platform_rect=platform_rect,
                width=width,
                height=height,
                focus_ratio=focus_ratio,
            )
        )

        img = None
        img = image_render_processor.auto_render_image_refactored(
            scene,
            pose=optimal_pose,
            fovy=optimal_fovy,
            width=width,
            height=height,
            might_mark_object_cuboid_list=might_mark_cuboid_list,
            might_mark_freespace_list=might_mark_freespace_rect_list,
            rectangle_grey=True,
            save_path=save_path,
        )

        pose_2d_list = camera_xy_list

        pose_list = [optimal_pose for i in range(len(pose_2d_list))]
        for i, pose_2d in enumerate(pose_2d_list):
            pose_list[i] = sapien.Pose(
                p=np.append(pose_2d, pose_list[i].p[2]), q=optimal_pose.q
            )

        img_list = [
            image_render_processor.auto_render_image_refactored(
                scene,
                pose=pose,
                fovy=optimal_fovy,
                width=width,
                height=height,
                might_mark_object_cuboid_list=might_mark_cuboid_list,
                might_mark_freespace_list=might_mark_freespace_rect_list,
                rectangle_grey=True,
                save_path=save_path.replace(
                    ".png", f"_{i+1}_out_of_{len(pose_list)}.png"
                ),
            )
            for i, pose in enumerate(pose_list)
        ]

        color_list = image_render_processor.get_high_contrast_colors()

        if len(might_mark_cuboid_list) > 0 and len(might_mark_freespace_rect_list) == 0:
            need_mark_rect_list = [rect[:4] for rect in might_mark_cuboid_list]
            platform_bbox = platform_cuboid_bbox[:4]
            camera_pose_list = pose_2d_list

            fig, ax = plt.subplots()

            # Plot platform bbox
            ax.plot(
                [platform_bbox[0][0], platform_bbox[1][0]],
                [platform_bbox[0][1], platform_bbox[1][1]],
                "k-",
                label="Platform",
            )
            ax.plot(
                [platform_bbox[1][0], platform_bbox[2][0]],
                [platform_bbox[1][1], platform_bbox[2][1]],
                "k-",
            )
            ax.plot(
                [platform_bbox[2][0], platform_bbox[3][0]],
                [platform_bbox[2][1], platform_bbox[3][1]],
                "k-",
            )
            ax.plot(
                [platform_bbox[3][0], platform_bbox[0][0]],
                [platform_bbox[3][1], platform_bbox[0][1]],
                "k-",
            )

            # Plot need_mark_rect_list
            for i, rect in enumerate(need_mark_rect_list):
                color = color_list[i % len(color_list)]
                color = [c / 255.0 for c in color]  # Normalize color values
                rect = [point[:2] for point in rect]
                ax.add_patch(
                    patches.Polygon(
                        rect,
                        closed=True,
                        facecolor=color,
                        alpha=1,
                        label=f"Item {i+1}",
                    )
                )

            # Plot camera poses
            camera_x = [pose[0] for pose in camera_pose_list]
            camera_y = [pose[1] for pose in camera_pose_list]
            ax.plot(camera_x, camera_y, "ro", label="CameraPoses")
            plt.subplots_adjust(left=0.01, right=0.5, top=0.91, bottom=0.01)

            # ax.set_xlabel("X")
            # ax.set_ylabel("Y")
            ax.set_title("A top down view")
            ax.axis("equal")
            ax.legend(
                loc="upper left",
                bbox_to_anchor=(1.05, 1),
                ncol=int(len(might_mark_cuboid_list) / 15 + 1),
            )
            ax.set_xticks([])  # Remove x axis ticks
            ax.set_yticks([])  # Remove y axis ticks

            plt.savefig(save_path.replace(".png", "_2d_visualization.png"))
            plt.close(fig)
            pass

        return img, img_list


class Tree:

    def __init__(self):
        self.nodes = {}  # Dictionary to access nodes by name
        self.edges = {}
        self.platforms = {}
        self.corresponding_scene = None
        self.mesh_checker = GroundCoverageAnalyzer()

    def get_node_categories_in_scene(self):
        major_categories = set()
        minor_categories = set()
        for node_name, node in self.nodes.items():
            if node.depth > 1:
                major_category = StringConvertor.get_category(node_name)
                minor_category = StringConvertor.get_name_wo_id(node_name)
                major_categories.add(major_category)
                minor_categories.add(minor_category)

        major_categories = list(major_categories)
        minor_categories = list(minor_categories)
        return major_categories, minor_categories

    def rename_all_features(self, new_name_map):
        """

        Args:





        """

        new_nodes_dict = {}
        new_platform_dict = {}
        for node_name, node in self.nodes.items():

            new_node = node.rename_with_name_map(new_name_map)
            assert new_node is not None
            new_node_name = StringConvertor.rename_with_map(node_name, new_name_map)
            new_nodes_dict[new_node_name] = new_node

        self.nodes = new_nodes_dict

        for platform_name, platform in self.platforms.items():
            new_platform_name = StringConvertor.rename_with_map(
                platform_name, new_name_map
            )
            new_platform = platform.rename_with_name_map(new_name_map)
            new_platform_dict[new_platform_name] = new_platform

        self.platforms = new_platform_dict
        self.edges = {
            (
                StringConvertor.rename_with_map(edge[0], new_name_map),
                StringConvertor.rename_with_map(edge[1], new_name_map),
            ): platform
            for edge, platform in self.edges.items()
        }

        if self.corresponding_scene is not None:
            for entity in self.corresponding_scene.entities:
                new_entity_name = StringConvertor.rename_with_map(
                    entity.name, new_name_map
                )
                entity.set_name(new_entity_name)

        return

    def load_corresponding_scene(self, scene):
        self.corresponding_scene = scene
        # Dictionary to access edges by name tuple(name1, name2, platform), edge[(name1, name2)]=platform means name1 is on a platform named platform, and platform is in name2

        # (self, name, heading=None, free_space=None, parent=None, children=None,convex_hull_2d=None, top = None, bbox=None):

    def get_non_ground_object_list(self):
        return [node for node in self.nodes.values() if node.depth > 1]

    def get_ground_object_list(self):
        return [node for node in self.nodes.values() if node.depth == 1]

    def get_ground_object_mesh_list(self):
        ground_object_list = self.get_ground_object_list()
        mesh_list = []
        for ground_object in ground_object_list:
            if ground_object.mesh is not None and ground_object.mesh.mesh is not None:
                mesh_list.append(ground_object.mesh.mesh)
        return mesh_list

    def cal_standable_area_for_platforms(self):

        ground_object_mesh_list = self.get_ground_object_mesh_list()
        for ground_mesh in ground_object_mesh_list:
            self.mesh_checker.add_ground_mesh(ground_mesh)

        self.mesh_checker.precompute_free_regions()

        for platform_name, platform in self.platforms.items():
            if self.nodes[platform.bel_object].depth != 1:
                continue

            standing_points_list = []

            for dir in range(0, 8, 2):
                standing_points, rectangles = self.mesh_checker.analyze_coverage(
                    platform.free_space[dir]["Available_space"],
                )

                standing_points_list.append(standing_points)

            self.platforms[platform_name].standing_point_list = standing_points_list
            # for dir in range(0, 8, 2):
            #     print(platform.free_space[dir]["Available_space"])
            # print(
            #     platform.convex_hull_2d.get_headed_bbox_instance(), standing_points_list
            # )
            # ipdb.set_trace()

        pass

    def get_sensible_platform_list(self):

        platform_list = []
        ground_object_list = self.get_ground_object_list()
        for ground_object in ground_object_list:
            for platform in ground_object.own_platform:
                if (len(platform.children) > 0 or platform.avl_height > 0.1) and any(
                    [
                        len(platform.standing_point_list[dir // 2])
                        and platform.freespace_is_visible(dir)
                        for dir in range(0, 8, 2)
                    ]
                ):
                    platform_list.append(platform)
        return platform_list

    def auto_take_platform_picture(

        self,

        platform_name,

        view,

        mark_object=False,

        mark_freespace=False,

        standing_direction=0,

        camera_xy=None,

        width=1920,

        height=1080,

        focus_ratio=0.8,

        save_path="image.png",

    ):
        """



        take platform picture for a certain platform.



        This function is dependent on " auto_take_ground_object_picture".



        Args:

            platform_name: the name of the platform, which is a string.

            view: the view of the camera, which is a string. 'human_full', 'human_focus', 'top_full', 'top_focus'

            mark_object: if True, mark all the object on the same platform with cuboid.

            mark_freespace: if True, mark all the free space on the platform with cuboid.

            standing_direction: the direction of the camera, which is a int. 0, 1, 2, 3, 4, 5, 6, 7

            width: the width of the image, which is a int.

            height: the height of the image, which is a int.

            focus_ratio: the ratio of the image, which is a float.

            save_path: the path to save the image, which is a string.



        Returns:

            img: the image of the platform, which is a numpy array.

            img_list: the image list of the platform, which is a list of numpy array.





        """
        platform_bel_object_name = StringConvertor.get_name_wo_id(platform_name)
        platform_id_object = int(StringConvertor.get_id(platform_name))
        platform_bel_object = self.nodes[platform_bel_object_name]
        img, img_list = platform_bel_object.auto_take_ground_object_picture(
            scene=self.corresponding_scene,
            view=view,
            mark_object=mark_object,
            mark_freespace=mark_freespace,
            platform_id=platform_id_object,
            standing_direction=standing_direction,
            camera_xy=camera_xy,
            width=width,
            height=height,
            focus_ratio=focus_ratio,
            save_path=save_path,
        )
        return img, img_list

    def update_platform_children(self):
        self.platforms = {}
        for node_name, node in self.nodes.items():
            if node.depth == 1:
                self.nodes[node_name].update_children_belong_platform()
                for platform_id, platform in enumerate(node.own_platform):
                    self.platforms[platform.name] = node.own_platform[platform_id]

    def from_scene_platform_list(self, object_platform_list, contacts):
        for item in object_platform_list:
            if item.belong == None:
                item_node = TreeNode(
                    name=item.name,
                    heading=item.heading,
                    free_space=None,
                    bbox=item.bbox,
                    convex_hull_2d=item.convex_hull_2d,
                    bottom=item.height,
                    top=item.top,
                    mesh=item.mesh,
                    centroid_translation=item.centroid_translation,
                )
                self.set_node(item_node)
            else:
                # self.nodes[item.belong].own_platform.append(item)
                self.nodes[item.belong].own_platform.append(
                    TreePlatform(
                        name=item.name,
                        children=[],
                        heading=item.heading,
                        convex_hull_2d=item.convex_hull_2d,
                        bottom=item.height,
                        avl_height=item.avl_height,
                        bel_object=item.belong,
                        visible_directions=item.visible_directions,
                    )
                )

        # sort platform_list by height and rename them.
        platform_name_mapping = {}
        for node_name in self.nodes:
            platform_list = self.nodes[node_name].own_platform
            platform_list = TreePlatform.sort_platforms(platform_list)

            self.nodes[node_name].own_platform = platform_list
            for i, platform in enumerate(platform_list):
                new_name = node_name + "_" + str(i)
                platform_name_mapping[platform.name] = new_name
                self.platforms[new_name] = platform
                self.nodes[node_name].own_platform[i].name = new_name

        #   glog.info('contacts: %s', contacts)

        for contact in contacts:

            if contact[0].belong is None:
                print("contact[0] should be a platform but belong is None")
                continue
            if contact[1].belong is not None:
                print("contact[1] should be a object but belong is not None")
                continue
            if (
                self.nodes[self.nodes[contact[0].belong].name].parent
                == self.nodes[contact[1].name]
            ):
                continue
            self.edges[(self.nodes[contact[0].belong].name, contact[1].name)] = contact[
                0
            ].name
            self.nodes[self.nodes[contact[0].belong].name].add_child(
                self.nodes[contact[1].name]
            )
            self.nodes[contact[1].name].parent = self.nodes[
                self.nodes[contact[0].belong].name
            ]

            contact_platform_name = platform_name_mapping[contact[0].name]
            self.nodes[contact[1].name].on_platform = self.platforms[
                contact_platform_name
            ]
            self.nodes[contact[1].name].free_space_height = [
                contact[0].height,
                contact[0].height + contact[0].avl_height,
            ]

            if contact_platform_name in self.platforms:

                self.platforms[contact_platform_name].children.append(
                    self.nodes[contact[1].name]
                )
            else:
                glog.warning(
                    f"contact[0].name not in platforms: {contact_platform_name}, {contact[1].name}"
                )

        on_platform_dict = {}
        ambiguous_items = set()
        for node in self.nodes.values():
            if node.on_platform is not None:
                node_category = StringConvertor.get_category(node.name)
                if node_category not in on_platform_dict:
                    on_platform_dict[node_category] = [node.on_platform]
                else:
                    for platform_have_this_item in on_platform_dict[node_category]:
                        if platform_have_this_item.name == node.on_platform.name:
                            ambiguous_items.add((node_category, node.on_platform.name))
                    on_platform_dict[node_category].append(node.on_platform)

        #   print("ambiguous items:", ambiguous_items)

        pass

        for node in self.nodes.values():
            node.update_children_belong_platform()

    def dfs_for_freespace(self, node):

        for child in node.children:
            child.depth = node.depth + 1
            self.nodes[child.name] = child
            if node.name == "GROUND":
                self.dfs_for_freespace(child)
            else:
                child.renew_heading(node.heading)

                self.dfs_for_freespace(child)

        if node.depth >= 1:
            for i in range(len(node.children)):
                child = node.children[i]
                child.reset_free_space()
                child.set_free_space_to_platform(child.on_platform.convex_hull_2d)
                child.sync_critical_free_space()

        for i in range(len(node.children)):
            for j in range(len(node.children)):
                if i == j:
                    continue
                child1, child2 = node.children[i], node.children[j]
                child1_obj, child2_obj = child1.object, child2.object

                # if 'lamp_02' in child1.name:
                #     import ipdb
                #     ipdb.set_trace()

                # Must belong to the same platform.
                if child1.on_platform.name != child2.on_platform.name:
                    continue

                surface_directions = []
                for k in range(8):
                    rect = child1.free_space[k]["Available_space"]
                    #    if 'picture_03' in child1.name:
                    #        print(child1.name, child2.name, rect, child2_obj.convex_hull_2d.get_headed_bbox_instance())
                    #        print(child2_obj.convex_hull_2d.is_intersected_with_rectangle(rect))
                    if child2_obj.convex_hull_2d.is_intersected_with_rectangle(rect):
                        surface_directions.append(k)

                for k in range(0, 8, 2):
                    if (
                        k not in surface_directions
                        and (k + 1) % 8 in surface_directions
                        and (k + 7) % 8 in surface_directions
                    ):
                        surface_directions.append(k)

                # if child1.name == 'frl_apartment_wall_cabinet_01_5':
                #    print(child2.name, surface_directions)

                for direction in surface_directions:
                    #          print(child1.name, child2.name, direction)
                    child1.update_free_space(child2, direction)

        for i in range(len(node.children)):
            child = node.children[i]
            for left_part in range(1, 4):

                for object_node in child.free_space[left_part]["Objects"]:
                    near_side = [
                        child.free_space[left_part]["Critical_space"][3],
                        child.free_space[left_part]["Critical_space"][2],
                    ]
                    far_side = [
                        child.free_space[left_part]["Critical_space"][0],
                        child.free_space[left_part]["Critical_space"][1],
                    ]

                    # if child.name == 'frl_apartment_wall_cabinet_01_5':
                    #    print(object_node.name, EIGHT_DIRECTIONS[left_part])
                    #    print('near',near_side,'far', far_side)
                    #    print('convexhull', object_node.object.convex_hull_2d.get_vertices_on_convex_hull())
                    new_near_side, new_far_side = (
                        object_node.object.convex_hull_2d.cut_free_space_with_convex(
                            near_side, far_side
                        )
                    )
                    child.free_space[left_part]["Critical_space"] = [
                        new_far_side[0],
                        new_far_side[1],
                        new_near_side[1],
                        new_near_side[0],
                    ]
                    # if child.name == 'frl_apartment_wall_cabinet_01_5':
                    #    print('new near far', new_near_side, new_far_side)

            for right_part in range(5, 8):

                for object_node in child.free_space[right_part]["Objects"]:
                    near_side = [
                        child.free_space[right_part]["Critical_space"][0],
                        child.free_space[right_part]["Critical_space"][1],
                    ]
                    far_side = [
                        child.free_space[right_part]["Critical_space"][3],
                        child.free_space[right_part]["Critical_space"][2],
                    ]

                    # if child.name == 'frl_apartment_wall_cabinet_01_5':
                    #    print(object_node.name, EIGHT_DIRECTIONS[right_part])
                    #    print(near_side, far_side)
                    #    print(object_node.object.convex_hull_2d.get_vertices_on_convex_hull())
                    new_near_side, new_far_side = (
                        object_node.object.convex_hull_2d.cut_free_space_with_convex(
                            near_side, far_side
                        )
                    )
                    child.free_space[right_part]["Critical_space"] = [
                        new_near_side[0],
                        new_near_side[1],
                        new_far_side[1],
                        new_far_side[0],
                    ]
                    # if child.name == 'frl_apartment_wall_cabinet_01_5':
                    #    print('new near far', new_near_side, new_far_side)
            for front_part in range(3, 6):

                for object_node in child.free_space[front_part]["Objects"]:
                    near_side = [
                        child.free_space[front_part]["Critical_space"][0],
                        child.free_space[front_part]["Critical_space"][3],
                    ]
                    far_side = [
                        child.free_space[front_part]["Critical_space"][1],
                        child.free_space[front_part]["Critical_space"][2],
                    ]

                    new_near_side, new_far_side = (
                        object_node.object.convex_hull_2d.cut_free_space_with_convex(
                            near_side, far_side
                        )
                    )
                    child.free_space[front_part]["Critical_space"] = [
                        new_near_side[0],
                        new_far_side[0],
                        new_far_side[1],
                        new_near_side[1],
                    ]

            for rear_part in [7, 0, 1]:

                for object_node in child.free_space[rear_part]["Objects"]:
                    near_side = [
                        child.free_space[rear_part]["Critical_space"][1],
                        child.free_space[rear_part]["Critical_space"][2],
                    ]
                    far_side = [
                        child.free_space[rear_part]["Critical_space"][0],
                        child.free_space[rear_part]["Critical_space"][3],
                    ]

                    new_near_side, new_far_side = (
                        object_node.object.convex_hull_2d.cut_free_space_with_convex(
                            near_side, far_side
                        )
                    )

                    child.free_space[rear_part]["Critical_space"] = [
                        new_far_side[0],
                        new_near_side[0],
                        new_near_side[1],
                        new_far_side[1],
                    ]

        return None

    def calculate_free_space(self):
        for node in self.nodes.values():
            # print(node.name, node.parent.name if node.parent is not None else None, node.on_platform)
            if node.depth == 0:
                if "GROUND" in node.name:
                    self.dfs_for_freespace(node)
        return None

    def clean_zero_area_free_space(self):
        for node in self.nodes.values():
            node.clean_free_space()

    def cut_free_space_with_stage(self, stage):

        min_intersection_height = INF
        max_stage_height = -INF
        for stage_obj in stage:

            geometry = stage_obj.mesh
            max_stage_height = max(max_stage_height, geometry.mesh.bounds[1][2])

            for node_name, node in self.nodes.items():

                if node.free_space is None:
                    continue
                bottom, top = node.bottom, node.top
                if bottom > top:
                    continue

                node_bbox = node.object.convex_hull_2d.get_headed_bbox_instance()

                node_bbox_8_directions = [
                    node_bbox[0],
                    (node_bbox[0] + node_bbox[1]) * 0.5,
                    node_bbox[1],
                    (node_bbox[1] + node_bbox[2]) * 0.5,
                    node_bbox[2],
                    (node_bbox[2] + node_bbox[3]) * 0.5,
                    node_bbox[3],
                    (node_bbox[3] + node_bbox[0]) * 0.5,
                ]

                for left_part in range(1, 4):
                    rect = node.free_space[left_part]["Critical_space"]
                    if isinstance(rect, str):
                        continue

                    cuboid_vertices = [
                        [rect[0][0], rect[0][1], bottom],
                        [rect[1][0], rect[1][1], bottom],
                        [rect[2][0], rect[2][1], bottom],
                        [rect[3][0], rect[3][1], bottom],
                        [rect[0][0], rect[0][1], top],
                        [rect[1][0], rect[1][1], top],
                        [rect[2][0], rect[2][1], top],
                        [rect[3][0], rect[3][1], top],
                    ]
                    # cuboid_faces = [[0, 1, 2, 3], [4, 5, 6, 7], [0, 1, 5, 4], [1, 2, 6, 5], [2, 3, 7, 6], [3, 0, 4, 7]]

                    if (
                        np.min(np.array(rect)) < -1e10
                        or np.max(np.array(rect)) > 1e10
                        or bottom < -1e10
                        or top > 1e10
                    ):
                        glog.warning(
                            f"Free space of {node_name} in direction {EIGHT_DIRECTIONS[left_part]} is invalid, skip cutting with stage {stage_obj.name}"
                        )

                    cuboid = MeshProcessor.create_cuboid_from_vertices(cuboid_vertices)
                    # print(bottom, top, rect,    cuboid.mesh.volume)
                    intersect = geometry.intersection_with_cuboid(cuboid)

                    if (
                        intersect is not None
                        and intersect.mesh.bounds[1][2] - intersect.mesh.bounds[0][2]
                        > 1e-1
                    ):
                        #     if node_name == 'frl_apartment_table_02_49':
                        #         print(node_name, stage_obj.name, 'left',EIGHT_DIRECTIONS[left_part], intersect.mesh.bounds)
                        intersect_2d_convex = intersect.cal_convex_hull_2d()
                        near_side = [rect[3], rect[2]]
                        far_side = [rect[0], rect[1]]
                        new_near_side, new_far_side = (
                            intersect_2d_convex.cut_free_space_with_point_cloud(
                                near_side,
                                far_side,
                                node_bbox_8_directions[left_part],
                            )
                        )
                        self.nodes[node_name].free_space[left_part][
                            "Critical_space"
                        ] = [
                            new_far_side[0],
                            new_far_side[1],
                            new_near_side[1],
                            new_near_side[0],
                        ]
                        self.nodes[node_name].free_space[left_part][
                            "Available_space"
                        ] = [
                            new_far_side[0],
                            new_far_side[1],
                            new_near_side[1],
                            new_near_side[0],
                        ]
                        #      if node_name == 'frl_apartment_table_02_49':
                        #           print('near',near_side,'far', far_side)
                        #          print('newnear', new_near_side,'newfar', new_far_side)
                        min_intersection_height = min(
                            min_intersection_height, intersect.mesh.bounds[0][2]
                        )
                for right_part in range(5, 8):
                    rect = node.free_space[right_part]["Critical_space"]
                    if isinstance(rect, str):
                        continue

                    cuboid_vertices = [
                        [rect[0][0], rect[0][1], bottom],
                        [rect[1][0], rect[1][1], bottom],
                        [rect[2][0], rect[2][1], bottom],
                        [rect[3][0], rect[3][1], bottom],
                        [rect[0][0], rect[0][1], top],
                        [rect[1][0], rect[1][1], top],
                        [rect[2][0], rect[2][1], top],
                        [rect[3][0], rect[3][1], top],
                    ]
                    cuboid = MeshProcessor.create_cuboid_from_vertices(cuboid_vertices)
                    intersect = geometry.intersection_with_cuboid(cuboid)
                    #  if node_name == 'frl_apartment_table_04_14':
                    #         print(node_name, stage_obj.name, EIGHT_DIRECTIONS[right_part], intersect.mesh.bounds if intersect is not None else None)

                    if (
                        intersect is not None
                        and intersect.mesh.bounds[1][2] - intersect.mesh.bounds[0][2]
                        > 1e-1
                    ):
                        intersect_2d_convex = intersect.cal_convex_hull_2d()
                        near_side = [rect[0], rect[1]]
                        far_side = [rect[3], rect[2]]
                        new_near_side, new_far_side = (
                            intersect_2d_convex.cut_free_space_with_point_cloud(
                                near_side,
                                far_side,
                                node_bbox_8_directions[right_part],
                            )
                        )
                        self.nodes[node_name].free_space[right_part][
                            "Critical_space"
                        ] = [
                            new_near_side[0],
                            new_near_side[1],
                            new_far_side[1],
                            new_far_side[0],
                        ]
                        self.nodes[node_name].free_space[right_part][
                            "Available_space"
                        ] = [
                            new_near_side[0],
                            new_near_side[1],
                            new_far_side[1],
                            new_far_side[0],
                        ]
                        min_intersection_height = min(
                            min_intersection_height, intersect.mesh.bounds[0][2]
                        )
                for front_part in range(3, 6):
                    rect = node.free_space[front_part]["Critical_space"]
                    if isinstance(rect, str):
                        continue

                    cuboid_vertices = [
                        [rect[0][0], rect[0][1], bottom],
                        [rect[1][0], rect[1][1], bottom],
                        [rect[2][0], rect[2][1], bottom],
                        [rect[3][0], rect[3][1], bottom],
                        [rect[0][0], rect[0][1], top],
                        [rect[1][0], rect[1][1], top],
                        [rect[2][0], rect[2][1], top],
                        [rect[3][0], rect[3][1], top],
                    ]
                    cuboid = MeshProcessor.create_cuboid_from_vertices(cuboid_vertices)
                    intersect = geometry.intersection_with_cuboid(cuboid)
                    if (
                        intersect is not None
                        and intersect.mesh.bounds[1][2] - intersect.mesh.bounds[0][2]
                        > 1e-1
                    ):
                        intersect_2d_convex = intersect.cal_convex_hull_2d()
                        near_side = [rect[0], rect[3]]
                        far_side = [rect[1], rect[2]]
                        new_near_side, new_far_side = (
                            intersect_2d_convex.cut_free_space_with_point_cloud(
                                near_side,
                                far_side,
                                node_bbox_8_directions[front_part],
                            )
                        )
                        self.nodes[node_name].free_space[front_part][
                            "Critical_space"
                        ] = [
                            new_near_side[0],
                            new_far_side[0],
                            new_far_side[1],
                            new_near_side[1],
                        ]
                        self.nodes[node_name].free_space[front_part][
                            "Available_space"
                        ] = [
                            new_near_side[0],
                            new_far_side[0],
                            new_far_side[1],
                            new_near_side[1],
                        ]
                        min_intersection_height = min(
                            min_intersection_height, intersect.mesh.bounds[0][2]
                        )
                for rear_part in [7, 0, 1]:
                    rect = node.free_space[rear_part]["Critical_space"]
                    if isinstance(rect, str):
                        continue

                    cuboid_vertices = [
                        [rect[0][0], rect[0][1], bottom],
                        [rect[1][0], rect[1][1], bottom],
                        [rect[2][0], rect[2][1], bottom],
                        [rect[3][0], rect[3][1], bottom],
                        [rect[0][0], rect[0][1], top],
                        [rect[1][0], rect[1][1], top],
                        [rect[2][0], rect[2][1], top],
                        [rect[3][0], rect[3][1], top],
                    ]
                    cuboid = MeshProcessor.create_cuboid_from_vertices(cuboid_vertices)
                    intersect = geometry.intersection_with_cuboid(cuboid)
                    if (
                        intersect is not None
                        and intersect.mesh.bounds[1][2] - intersect.mesh.bounds[0][2]
                        > 1e-1
                    ):
                        intersect_2d_convex = intersect.cal_convex_hull_2d()
                        near_side = [rect[1], rect[2]]
                        far_side = [rect[0], rect[3]]
                        new_near_side, new_far_side = (
                            intersect_2d_convex.cut_free_space_with_point_cloud(
                                near_side,
                                far_side,
                                node_bbox_8_directions[rear_part],
                            )
                        )
                        self.nodes[node_name].free_space[rear_part][
                            "Critical_space"
                        ] = [
                            new_far_side[0],
                            new_near_side[0],
                            new_near_side[1],
                            new_far_side[1],
                        ]
                        self.nodes[node_name].free_space[rear_part][
                            "Available_space"
                        ] = [
                            new_far_side[0],
                            new_near_side[0],
                            new_near_side[1],
                            new_far_side[1],
                        ]
                        min_intersection_height = min(
                            min_intersection_height, intersect.mesh.bounds[0][2]
                        )

        for node_name, node in self.nodes.items():
            self.nodes[node_name].free_space_height[1] = min(
                self.nodes[node_name].free_space_height[1], max_stage_height
            )

    def fix_heading_for_all_ground_objects(self):
        for ground_object_name, ground_object in self.nodes.items():
            if ground_object.depth == 1:
                standable_heading = 0
                for i in range(0, 8, 2):
                    if ground_object.freespace_is_standable(i):
                        standable_heading = i
                        break
                self.nodes[ground_object_name].rotate_free_space_for_all_children(
                    standable_heading
                )

    def set_node(self, node):
        self.nodes[node.name] = node

    def get_node(self, name):
        return self.nodes.get(name, None)

    def remove_node_from_scene(self, name):
        node = self.get_node(name)
        # import ipdb
        # ipdb.set_trace()
        if node is not None and self.corresponding_scene is not None:
            for entity in self.corresponding_scene.entities:
                if node.name in entity.get_name():
                    entity.remove_from_scene()

    def add_node_to_scene(self, name):
        node = self.get_node(name)
        # import ipdb

        # ipdb.set_trace()
        if node is not None:
            obj = node.entity_config
            object_file_path = obj["visual_path"]
            collision_path = obj.get("collision_path", None)
            position = [
                obj["centroid_translation"]["x"],
                obj["centroid_translation"]["y"],
                obj["centroid_translation"]["z"],
            ]

            quaternion = [
                obj["quaternion"]["w"],
                obj["quaternion"]["x"],
                obj["quaternion"]["y"],
                obj["quaternion"]["z"],
            ]

            if "cushion_03" in object_file_path:
                position[2] += 0.1

            rpy = transforms3d.euler.quat2euler(quaternion, axes="sxyz")
            quaternion = transforms3d.euler.euler2quat(
                rpy[0] + np.deg2rad(90), rpy[1], rpy[2], axes="sxyz"
            )

            if self.corresponding_scene is not None:
                builder = self.corresponding_scene.create_actor_builder()
                builder.add_visual_from_file(filename=object_file_path)
                if collision_path is not None:
                    builder.add_multiple_convex_collisions_from_file(
                        filename=collision_path
                    )
                else:
                    builder.add_convex_collision_from_file(filename=object_file_path)
                # import ipdb

                # ipdb.set_trace()
                mesh = builder.build_static(name=name)
                mesh.set_pose(sapien.Pose(p=position, q=quaternion))

    def remove_node(self, name):
        node = self.get_node(name)

        if node is not None:
            parent_name = node.parent.name if node.parent is not None else None

            self.nodes[name].removed = True
            if node.parent is not None:
                self.nodes[parent_name].remove_child(node)

            for dir in range(8):
                self.nodes[name].free_space[dir]["Objects"] = []
            platform_name = (
                node.on_platform.name if node.on_platform is not None else None
            )
            for child in node.all_children:
                # child.parent = node.parent
                # child.on_platform = node.on_platform
                child.free_space_height = node.free_space_height.copy()
                self.nodes[parent_name].add_child(child)

                self.platforms[platform_name] = node.on_platform

            for platform in node.own_platform:
                platform.children = []

                if platform.name in self.platforms:
                    self.platforms[platform.name] = platform

            self.dfs_for_freespace(self.nodes[parent_name])
            node.parent = None

            self.remove_node_from_scene(name)
            node.update_all_child()
            # import ipdb

            # ipdb.set_trace()

            for child in node.all_children:
                self.remove_node_from_scene(child.name)

            self.update_platform_children()

    def add_node(self, node_name, parent_name, platform_name, translation):

        if parent_name not in self.nodes.keys():
            print("Parent node not found", parent_name, platform_name)
            return
        node = self.nodes[node_name]
        parent = self.nodes[parent_name]
        platform = self.platforms[platform_name]

        if platform is None:
            print("Platform not found", parent_name, platform_name)
            return
        node.update_all_child()
        node_add_list = [node] + node.all_children
        relative_translation = np.array(translation[:2]) - np.array(
            [
                node.entity_config["centroid_translation"]["x"],
                node.entity_config["centroid_translation"]["y"],
            ]
        )
        relative_bottom_translation = platform.bottom - node.bottom

        node.parent = parent
        node.on_platform = platform
        node.free_space_height = [
            platform.bottom,
            platform.bottom + platform.avl_height,
        ]

        for n in node_add_list:
            n.removed = False

            n.heading = n.object.heading = n.object.convex_hull_2d.heading = (
                parent.heading
            )

            n.object.convex_hull_2d.vertices += np.array(relative_translation)
            new_bottom = platform.bottom
            n.entity_config["centroid_translation"]["x"] += relative_translation[0]
            n.entity_config["centroid_translation"]["y"] += relative_translation[1]
            n.entity_config["centroid_translation"]["z"] += relative_bottom_translation
            n.top += relative_bottom_translation
            n.bottom += relative_bottom_translation

            self.nodes[n.name] = n
        # node.removed = False
        # node.parent = parent
        # node.on_platform = platform
        # node.free_space_height = [
        #     platform.bottom,
        #     platform.bottom + platform.avl_height,
        # ]

        # node.heading = node.object.heading = node.object.convex_hull_2d.heading = (
        #     parent.heading
        # )
        # node.object.convex_hull_2d.vertices += np.array(translation) - np.array(
        #     node.get_center()[:2]
        # )

        # new_bottom = platform.bottom
        # node.entity_config["centroid_translation"]["x"] = translation[0]
        # node.entity_config["centroid_translation"]["y"] = translation[1]
        # node.entity_config["centroid_translation"]["z"] += new_bottom - node.bottom

        # node.top = node.top - node.bottom + new_bottom
        # node.bottom = new_bottom

        # self.nodes[node_name] = node
        parent.add_child(self.nodes[node_name])
        platform.children.append(self.nodes[node_name])
        self.dfs_for_freespace(self.nodes[parent_name])
        # import ipdb
        # ipdb.set_trace()
        node.update_all_child()

        self.add_node_to_scene(node.name)
        for child in node.all_children:
            self.add_node_to_scene(child.name)
        self.nodes[node_name].display_unique_information()
        pass

    def move_node(self, node_name, parent_name, platform_name, translation):
        self.remove_node(node_name)
        self.add_node(node_name, parent_name, platform_name, translation)

        pass


def print_tree(node, depth=0):
    print("  " * depth + f"Object Name: {node.name}")

    all_free = True
    for direction in node.free_space.keys():
        if node.free_space[direction]["Empty"] == False:
            print(
                "  " * depth
                + f'*Occupied Direction: {direction}, Objects: {node.free_space[direction]["Objects"]}'
            )
            all_free = False
    if all_free:
        print("  " * depth + f"*All Directions are Free")

    for child in node.children:
        print_tree(child, depth + 1)


# Function to generate the tree structure starting from ground objects
# key function
def gen_multi_layer_graph_with_free_space(json_data):

    all_objects = [obj for obj in json_data["object_instances"]]

    scene_platform_list = []
    stage = [
        {
            "name": "stage",
            "visual_path": json_data["background_file_path"],
            "centroid_translation": {"x": 0.0, "y": 0.0, "z": 0.0},
            "quaternion": {"w": 1.0, "x": 0.0, "y": 0.0, "z": 0.0},
            "bbox": "deprecated",
        }
    ]

    ground = scene_parser.SceneObject(name="GROUND")
    ground.set_ground()
    scene_platform_list.append(
        scene_parser.SceneElement(
            name="GROUND_0",
            heading=(1, 0),
            height=0,
            avl_height=3.08,
            bbox=[[-INF, -INF], [INF, INF]],
            instancetype="platform",
            convex_hull_2d=ConvexHullProcessor_2d(
                vertices=[[-INF, -INF], [INF, -INF], [INF, INF], [-INF, INF]],
                heading=(1, 0),
            ),
            belong="GROUND",
        )
    )
    object_list = scene_parser.create_object_list(
        all_objects, calculate_affordable_platforms=True
    )
    object_list.append(ground)

    stage_list = scene_parser.create_object_list(
        stage, calculate_affordable_platforms=False
    )
    for object in object_list:
        # the height of object is the height of the object's bottom.
        # Note: for object, get_bounding_box has already calculated the bbox_min and bbox_max in the world coordinate system, added the centroid_translation.
        bbox_min, bbox_max = object.get_bounding_box()
        scene_platform_list.append(
            scene_parser.SceneElement(
                name=object.name,
                heading=object.heading,
                height=bbox_min[2],
                top=bbox_max[2],
                bbox=[bbox_min[:2], bbox_max[:2]],
                bounding_points=object.bounding_points,
                centroid_translation=object.centroid_translation,
                convex_hull_2d=object.convex_hull_2d,
                mesh=object.mesh,
                instancetype="object",
            )
        )
        # the height of platform is the height of the platform's top.
        geometry = object.mesh
        for platform in geometry.affordable_platforms:
            scene_platform_list.append(
                scene_parser.SceneElement(
                    name=object.name + platform.name,
                    heading=object.heading,
                    bbox=platform.bbox
                    + np.array(
                        [
                            object.centroid_translation[:2],
                            object.centroid_translation[:2],
                        ]
                    ),
                    height=platform.get_height()[1] + object.centroid_translation[2],
                    avl_height=platform.available_height,
                    convex_hull_2d=platform.get_convex_hull_2d()
                    + object.centroid_translation[:2],
                    instancetype="platform",
                    visible_directions=platform.visible_directions,
                    belong=object.name,
                )
            )
            # print(platform.get_convex_hull_2d() + object.centroid_translation[:2])

    sorted_scene_platform_list, contacts_id = (
        scene_parser.SceneElement.calculate_contact_conditions(scene_platform_list)
    )

    contacts = [
        (sorted_scene_platform_list[i[0]], sorted_scene_platform_list[i[1]])
        for i in contacts_id
    ]

    scene_graph = Tree()
    scene_graph.from_scene_platform_list(sorted_scene_platform_list, contacts)

    # import ipdb
    # ipdb.set_trace()

    scene_graph.calculate_free_space()

    # scene_graph.clean_zero_area_free_space()
    scene_graph.cal_standable_area_for_platforms()
    scene_graph.cut_free_space_with_stage(stage_list)

    scene_graph.update_platform_children()

    for obj_instance in all_objects:
        if "name" not in obj_instance and "template_name" in obj_instance:
            obj_instance["name"] = obj_instance["template_name"]
        if scene_graph.get_node(obj_instance["name"]) is None:
            print(obj_instance["name"], "not in the tree")
        else:
            scene_graph.nodes[obj_instance["name"]].entity_config = obj_instance

    # scene_graph.clean_zero_area_free_space()

    return scene_graph


def main():
    # Load JSON data and generate the tree structure
    # input_file_path = './replica_apt_0.json'
    # input_file_path = './toy_scene_2.json'
    parser = argparse.ArgumentParser(
        description="Generate scene graph with free space information."
    )
    parser.add_argument(
        "--input",
        type=str,
        default="entities_apt_0.json",
        required=False,
        help="Path to the input JSON file.",
    )
    parser.add_argument(
        "--output",
        type=str,
        default="./result/replica_apt_0.txt",
        required=False,
        help="Path to the output text file.",
    )
    parser.add_argument(
        "--output_scene_graph",
        type=str,
        default="./result/scene_graph_0.txt",
        required=False,
        help="Path to the output text file.",
    )
    args = parser.parse_args()

    input_file_path = args.input
    output_file_path = args.output
    output_scene_graph_path = args.output_scene_graph
    input_json = load_json_file(input_file_path)

    with open(output_file_path, "w") as output_file:
        output_file.write("----------------------------------\n")
        output_file.write("Establishing the tree structure:\n")
        result_tree = gen_multi_layer_graph_with_free_space(input_json)

        output_file.write("\n----------------------------------\n")
        output_file.write("Result tree:\n")
        for root_node in result_tree.nodes.values():
            if (
                root_node.parent is None
            ):  # Only print root nodes (those without a parent)
                output_file.write("---\n")
                output_file.write(f"Object Name: {root_node.name}\n")
                all_free = True
                if root_node.free_space is None:
                    output_file.write(f"*No Free Space Information\n")
                    continue
                for direction in range(len(root_node.free_space)):
                    if len(root_node.free_space[direction]["Objects"]):
                        output_file.write(
                            f'*Occupied Direction : {direction}, Objects: {root_node.free_space[direction]["Objects"]}\n'
                        )
                        all_free = False
                if all_free:
                    output_file.write(f"*All Directions are Free\n")
                output_file.write("---\n")

        output_file.write("\n----------------------------------\n")
        output_file.write("Node details in result tree:\n")
        for obj in input_json["object_instances"]:
            node = result_tree.get_node(obj["name"])
            if node is None:
                continue
            output_file.write("---\n")
            output_file.write(
                f"Name: {node.name}, Parent: {node.parent.name if node.parent else 'None'}, Children: {[child.name for child in node.children]}\n"
            )
            output_file.write(
                f"convex_bbox: {node.object.convex_hull_2d.get_headed_bbox_instance()}\n"
            )
            output_file.write(
                f"convex: {node.object.convex_hull_2d.get_vertices_on_convex_hull()}\n"
            )
            output_file.write(f"Heading: {node.heading}\n")
            output_file.write(f"Top: {node.top}\n")
            output_file.write(f"Belong to platform: {node.on_platform.name}\n")
            for direction in range(len(root_node.free_space)):
                free_space_info = node.free_space[direction]["Available_space"]
                critical_space_info = node.free_space[direction]["Critical_space"]

                output_file.write("-\n")
                output_file.write(
                    f"Direction: {EIGHT_DIRECTIONS[direction]},  Objects: {[object.name for object in node.free_space[direction]['Objects']]}\n"
                )
                output_file.write(f"Available Space: \n[{free_space_info}]\n")
                if isinstance(critical_space_info, str):
                    output_file.write(f"Critical Available Space: \nNot Available\n")
                else:
                    output_file.write(
                        f"Critical Available Space: \n[{critical_space_info}]\n"
                    )
                output_file.write(
                    f"Free Space Height: \n[{float(node.free_space_height[0])},{float(node.free_space_height[1])}]\n"
                )
            output_file.write("-\n")
            output_file.write(f"Own Platform: \n")
            for platform in node.own_platform:
                output_file.write(f"{platform.name}\n")
                output_file.write(
                    f"bbox: \n[[{platform.bbox[0][0]},{platform.bbox[0][1]}],[{platform.bbox[1][0]},{platform.bbox[1][1]}]] \n"
                )
                output_file.write(f"height: \n{platform.height}\n")
                output_file.write(f"available height: \n{platform.avl_height}\n")
            output_file.write("\n")


if __name__ == "__main__":
    main()


"""





"""
# %%