File size: 30,745 Bytes
2df809d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import sys
import json
import numpy as np
from PIL import Image


from torch.amp import autocast
import torch
import copy
from torch.nn import functional as F

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d.art3d import Poly3DCollection

sys.path.append("./extern/dust3r")
from dust3r.inference import inference, load_model
from dust3r.utils.image import load_images
from dust3r.image_pairs import make_pairs
from dust3r.cloud_opt import global_aligner, GlobalAlignerMode



def visualize_surfels(
        surfels, 
        draw_normals=False, 
        normal_scale=20, 
        disk_resolution=16, 
        disk_alpha=0.5
    ):
        """
        Visualize surfels as 2D disks oriented by their normals in 3D using matplotlib.

        Args:
            surfels (list of Surfel): Each Surfel has at least:
                - position: (x, y, z)
                - normal: (nx, ny, nz)
                - radius: scalar
                - color: (R, G, B) in [0..255] (optional)
            draw_normals (bool): If True, draws the surfel normals as quiver arrows.
            normal_scale (float): Scale factor for the normal arrows.
            disk_resolution (int): Number of segments to approximate each disk.
            disk_alpha (float): Alpha (transparency) for the filled disks.
        """

        fig = plt.figure()
        ax = fig.add_subplot(111, projection='3d')
        
        # Prepare arrays for optional quiver (if draw_normals=True)
        positions = []
        normals = []

        # We'll accumulate 3D polygons in a list for Poly3DCollection
        polygons = []
        polygon_colors = []

        for s in surfels:
            # --- Extract surfel data ---
            
            position = s.position
            normal = s.normal
            radius = s.radius
            
            if isinstance(position, torch.Tensor):
                x, y, z = position.detach().cpu().numpy()
                nx, ny, nz = normal.detach().cpu().numpy()
                radius = radius.detach().cpu().numpy()
            else:
                x, y, z = position
                nx, ny, nz = normal
                radius = radius
            
            
            # Convert color from [0..255] to [0..1], or use default
            if s.color is None:
                color = (0.2, 0.6, 1.0)  # Light blue
            else:
                r, g, b = s.color
                color = (r/255.0, g/255.0, b/255.0)

            # --- Build local coordinate axes for the disk ---
            normal = np.array([nx, ny, nz], dtype=float)
            norm_len = np.linalg.norm(normal)
            # Skip degenerate normals to avoid nan
            if norm_len < 1e-12:
                continue
            normal /= norm_len

            # Pick an 'up' vector that is not too close to the normal
            # so we can build a tangent plane
            up = np.array([0, 0, 1], dtype=float)
            if abs(normal.dot(up)) > 0.9:
                up = np.array([0, 1, 0], dtype=float)

            # xAxis = normal x up
            xAxis = np.cross(normal, up)
            xAxis /= np.linalg.norm(xAxis)
            # yAxis = normal x xAxis
            yAxis = np.cross(normal, xAxis)
            yAxis /= np.linalg.norm(yAxis)

            # --- Create a circle of 'disk_resolution' segments in local 2D coords ---
            angles = np.linspace(0, 2*np.pi, disk_resolution, endpoint=False)
            circle_points_3d = []
            for theta in angles:
                # local 2D circle: (r*cosθ, r*sinθ)
                px = radius * np.cos(theta)
                py = radius * np.sin(theta)

                # transform to 3D world space: position + px*xAxis + py*yAxis
                world_pt = np.array([x, y, z]) + px * xAxis + py * yAxis
                circle_points_3d.append(world_pt)

            # We have a list of [x, y, z]. For a filled polygon, Poly3DCollection
            # wants them as a single Nx3 array.
            circle_points_3d = np.array(circle_points_3d)
            polygons.append(circle_points_3d)
            polygon_colors.append(color)

            # Collect positions and normals for quiver (if used)
            positions.append([x, y, z])
            normals.append(normal)

        # --- Draw the disks as polygons ---
        poly_collection = Poly3DCollection(
            polygons,
            facecolors=polygon_colors,
            edgecolors='k',  # black edge
            linewidths=0.5,
            alpha=disk_alpha
        )
        ax.add_collection3d(poly_collection)

        # --- Optionally draw normal vectors (quiver) ---
        if draw_normals and len(positions) > 0:
            X = [p[0] for p in positions]
            Y = [p[1] for p in positions]
            Z = [p[2] for p in positions]

            Nx = [n[0] for n in normals]
            Ny = [n[1] for n in normals]
            Nz = [n[2] for n in normals]

            # Note: If your scene is large, you may want to increase `length`.
            ax.quiver(
                X, Y, Z, 
                Nx, Ny, Nz, 
                length=normal_scale, 
                color='red', 
                normalize=True
            )

        # --- Axis labels, aspect ratio, etc. ---
        ax.set_xlabel('X')
        ax.set_ylabel('Y')
        ax.set_zlabel('Z')
        try:
            ax.set_box_aspect((1, 1, 1))
        except AttributeError:
            pass  # older MPL versions

        plt.title("Surfels as Disks (Oriented by Normal)")
        plt.show()
    


def visualize_pointcloud(
        points,
        colors=None,
        title='Point Cloud',
        point_size=1,
        alpha=1.0,
        bg_color=(240/255, 223/255, 223/255)  # 新增参数,默认白色 (1,1,1)
    ):
    """
    可视化3D点云,同时支持每个点的RGB或RGBA颜色,并保证x, y, z三个轴等比例缩放。
    
    参数
    ----------
    points : np.ndarray 或 torch.Tensor
        形状为 [N, 3] 的数组或张量,每行表示一个3D点 (x, y, z)。
    colors : None, str, 或 np.ndarray
        - 如果为 None,则使用默认颜色 'blue'。
        - 如果为字符串,则所有点均使用该颜色。
        - 如果为数组,则形状应为 [N, 3] 或 [N, 4],表示每个点的颜色,值的范围应为 [0, 1](若为浮点数)。
    title : str, 可选
        图像标题,默认 'Point Cloud'。
    point_size : float, 可选
        点的大小,默认 1。
    alpha : float, 可选
        点的整体透明度,默认 1.0。
    bg_color : tuple, 可选
        背景颜色,格式为 (r, g, b),每个值的范围为 [0, 1],默认为白色 (1.0, 1.0, 1.0)。

    示例
    --------
    >>> import numpy as np
    >>> pts = np.random.rand(1000, 3)
    >>> cols = np.random.rand(1000, 3)
    >>> visualize_pointcloud(pts, colors=cols, title="随机点云", bg_color=(0.2, 0.2, 0.3))
    """
    
    # 如果是 Torch 张量,则转换为 NumPy 数组
    if isinstance(points, torch.Tensor):
        points = points.detach().cpu().numpy()
    if isinstance(colors, torch.Tensor):
        colors = colors.detach().cpu().numpy()

    # 如果点云或颜色数据维度过高,则展平
    if len(points.shape) > 2:
        points = points.reshape(-1, 3)
    if colors is not None and isinstance(colors, np.ndarray) and len(colors.shape) > 2:
        colors = colors.reshape(-1, colors.shape[-1])
        
    # 验证点云形状
    if points.shape[1] != 3:
        raise ValueError("`points` array must have shape [N, 3].")
        
    # 处理颜色参数
    if colors is None:
        colors = 'blue'
    elif isinstance(colors, np.ndarray):
        colors = np.asarray(colors)
        if colors.shape[0] != points.shape[0]:
            raise ValueError("Colors array length must match the number of points.")
        if colors.shape[1] not in [3, 4]:
            raise ValueError("Colors array must have shape [N, 3] or [N, 4].")
    
    # 验证背景颜色参数
    if not isinstance(bg_color, tuple) or len(bg_color) != 3:
        raise ValueError("Background color must be a tuple of (r, g, b) with values between 0 and 1.")
    
    # 提取坐标
    x = points[:, 0]
    y = points[:, 1]
    z = points[:, 2]
    
    # 创建图像,并设置自定义背景颜色
    fig = plt.figure(figsize=(8, 6), facecolor=bg_color)
    ax = fig.add_subplot(111, projection='3d')
    ax.set_facecolor(bg_color)
    
    # 绘制散点图
    ax.scatter(x, y, z, c=colors, s=point_size, alpha=alpha)
    
    # 设置等比例缩放
    max_range = np.array([x.max() - x.min(), 
                          y.max() - y.min(),
                          z.max() - z.min()]).max() / 2.0
    mid_x = (x.max() + x.min()) * 0.5
    mid_y = (y.max() + y.min()) * 0.5
    mid_z = (z.max() + z.min()) * 0.5
    ax.set_xlim(mid_x - max_range, mid_x + max_range)
    ax.set_ylim(mid_y - max_range, mid_y + max_range)
    ax.set_zlim(mid_z - max_range, mid_z + max_range)
    
    # 隐藏刻度和标签
    ax.set_xticks([])
    ax.set_yticks([])
    ax.set_zticks([])
    ax.set_xlabel('')
    ax.set_ylabel('')
    ax.set_zlabel('')
    ax.grid(False)
    
    # 隐藏3D坐标轴的面板(pane)来去除轴的显示
    ax.xaxis.pane.set_visible(False)
    ax.yaxis.pane.set_visible(False)
    ax.zaxis.pane.set_visible(False)
    
    # 设置标题(如果需要显示标题)
    ax.set_title(title)
    
    plt.tight_layout()
    plt.show()

# def visualize_pointcloud(
#         points,
#         colors=None,
#         title='Point Cloud',
#         point_size=1,
#         alpha=1.0
#     ):
#     """
#     可视化3D点云,同时支持每个点的RGB或RGBA颜色,并保证x, y, z三个轴等比例缩放。
    
#     参数
#     ----------
#     points : np.ndarray 或 torch.Tensor
#         形状为 [N, 3] 的数组或张量,每行表示一个3D点 (x, y, z)。
#     colors : None, str, 或 np.ndarray
#         - 如果为 None,则使用默认颜色 'blue'。
#         - 如果为字符串,则所有点均使用该颜色。
#         - 如果为数组,则形状应为 [N, 3] 或 [N, 4],表示每个点的颜色,值的范围应为 [0, 1](若为浮点数)。
#     title : str, 可选
#         图像标题,默认 'Point Cloud'。
#     point_size : float, 可选
#         点的大小,默认 1。
#     alpha : float, 可选
#         点的整体透明度,默认 1.0。

#     示例
#     --------
#     >>> import numpy as np
#     >>> pts = np.random.rand(1000, 3)
#     >>> cols = np.random.rand(1000, 3)
#     >>> visualize_pointcloud(pts, colors=cols, title="随机点云")
#     """
    
#     # 如果是 Torch 张量,则转换为 NumPy 数组
#     if isinstance(points, torch.Tensor):
#         points = points.detach().cpu().numpy()
#     if isinstance(colors, torch.Tensor):
#         colors = colors.detach().cpu().numpy()

#     # 如果点云或颜色数据维度过高,则展平
#     if len(points.shape) > 2:
#         points = points.reshape(-1, 3)
#     if colors is not None and isinstance(colors, np.ndarray) and len(colors.shape) > 2:
#         colors = colors.reshape(-1, colors.shape[-1])
        
#     # 验证点云形状
#     if points.shape[1] != 3:
#         raise ValueError("`points` array must have shape [N, 3].")
        
#     # 处理颜色参数
#     if colors is None:
#         colors = 'blue'
#     elif isinstance(colors, np.ndarray):
#         colors = np.asarray(colors)
#         if colors.shape[0] != points.shape[0]:
#             raise ValueError("Colors array length must match the number of points.")
#         if colors.shape[1] not in [3, 4]:
#             raise ValueError("Colors array must have shape [N, 3] or [N, 4].")
    
#     # 提取坐标
#     x = points[:, 0]
#     y = points[:, 1]
#     z = points[:, 2]
    
#     # 创建图像,并设置背景为白色
#     fig = plt.figure(figsize=(8, 6), facecolor='white')
#     ax = fig.add_subplot(111, projection='3d')
#     ax.set_facecolor('white')
    
#     # 绘制散点图
#     ax.scatter(x, y, z, c=colors, s=point_size, alpha=alpha)
    
#     # 设置等比例缩放
#     max_range = np.array([x.max() - x.min(), 
#                           y.max() - y.min(),
#                           z.max() - z.min()]).max() / 2.0
#     mid_x = (x.max() + x.min()) * 0.5
#     mid_y = (y.max() + y.min()) * 0.5
#     mid_z = (z.max() + z.min()) * 0.5
#     ax.set_xlim(mid_x - max_range, mid_x + max_range)
#     ax.set_ylim(mid_y - max_range, mid_y + max_range)
#     ax.set_zlim(mid_z - max_range, mid_z + max_range)
    
#     # 隐藏刻度和标签
#     ax.set_xticks([])
#     ax.set_yticks([])
#     ax.set_zticks([])
#     ax.set_xlabel('')
#     ax.set_ylabel('')
#     ax.set_zlabel('')
#     ax.grid(False)
    
#     # 隐藏3D坐标轴的面板(pane)来去除轴的显示
#     ax.xaxis.pane.set_visible(False)
#     ax.yaxis.pane.set_visible(False)
#     ax.zaxis.pane.set_visible(False)
    
#     # 设置标题(如果需要显示标题)
#     ax.set_title(title)
    
#     plt.tight_layout()
#     plt.show()
class Surfel:
    def __init__(self, position, normal, radius=1.0, color=None):
        """
        position: (x, y, z)
        normal:   (nx, ny, nz)
        radius:   scalar
        color:    (r, g, b) or None
        """
        self.position = position
        self.normal = normal
        self.radius = radius
        self.color = color

    def __repr__(self):
        return (f"Surfel(position={self.position}, "
                f"normal={self.normal}, radius={self.radius}, "
                f"color={self.color})")




class Octree:
    def __init__(self, points, indices=None, bbox=None, max_points=10):
        """
        构建八叉树:
          - points: 所有点的 numpy 数组,形状为 (N, 3)
          - indices: 当前节点中点的索引列表
          - bbox: 当前节点的包围盒,形式为 (center, half_size),其中半径为正方体半边长
          - max_points: 叶子节点允许的最大点数
        """
        self.points = points
        if indices is None:
            indices = np.arange(points.shape[0])
        self.indices = indices

        # 如果没有给定包围盒,则计算所有点的包围盒,保证是一个正方体
        if bbox is None:
            min_bound = points.min(axis=0)
            max_bound = points.max(axis=0)
            center = (min_bound + max_bound) / 2
            half_size = np.max(max_bound - min_bound) / 2
            bbox = (center, half_size)
        self.center, self.half_size = bbox

        self.children = []  # 存储子节点
        self.max_points = max_points

        if len(self.indices) > self.max_points:
            self.subdivide()

    def subdivide(self):
        """将当前节点划分为8个子节点"""
        cx, cy, cz = self.center
        hs = self.half_size / 2
        # 八个象限的偏移量
        offsets = np.array([[dx, dy, dz] for dx in (-hs, hs) 
                                       for dy in (-hs, hs) 
                                       for dz in (-hs, hs)])
        for offset in offsets:
            child_center = self.center + offset
            child_indices = []
            # 检查每个点是否在子节点的包围盒内
            for idx in self.indices:
                p = self.points[idx]
                if np.all(np.abs(p - child_center) <= hs):
                    child_indices.append(idx)
            child_indices = np.array(child_indices)
            if len(child_indices) > 0:
                child = Octree(self.points, indices=child_indices, bbox=(child_center, hs), max_points=self.max_points)
                self.children.append(child)
        # 划分后,内部节点不再直接保存点索引
        self.indices = None

    def sphere_intersects_node(self, center, r):
        """
        判断以center为球心, r为半径的球是否与当前节点的轴对齐包围盒相交。
        算法:计算球心到盒子的距离(只考虑超出盒子边界的部分),若小于r,则相交。
        """
        diff = np.abs(center - self.center)
        max_diff = diff - self.half_size
        max_diff = np.maximum(max_diff, 0)
        dist_sq = np.sum(max_diff**2)
        return dist_sq <= r*r

    def query_ball_point(self, point, r):
        """
        查询距离给定点 point 小于 r 的所有点索引。
        """
        results = []
        if not self.sphere_intersects_node(point, r):
            return results
        # 如果当前节点没有子节点,则为叶子节点
        if len(self.children) == 0:
            if self.indices is not None:
                for idx in self.indices:
                    if np.linalg.norm(self.points[idx] - point) <= r:
                        results.append(idx)
            return results
        else:
            for child in self.children:
                results.extend(child.query_ball_point(point, r))
            return results
        

def estimate_normal_from_pointmap(pointmap: torch.Tensor) -> torch.Tensor:
    """
    Estimate surface normals from a 3D point map by computing cross products of
    neighboring points, using PyTorch tensors.

    Parameters
    ----------
    pointmap : torch.Tensor
        A PyTorch tensor of shape [H, W, 3] containing 3D points in camera coordinates.
        Each point is represented as (X, Y, Z). This tensor can be on CPU or GPU.

    Returns
    -------
    torch.Tensor
        A PyTorch tensor of shape [H, W, 3] containing estimated surface normals.
        Each normal is a unit vector (X, Y, Z).
        Points where normals cannot be computed (e.g. boundaries) will be zero vectors.
    """
    # pointmap is shape (H, W, 3)
    h, w = pointmap.shape[:2]
    device = pointmap.device  # Keep the device (CPU/GPU) consistent
    dtype = pointmap.dtype
    
    # Initialize the normal map
    normal_map = torch.zeros((h, w, 3), device=device, dtype=dtype)
    
    for y in range(h):
        for x in range(w):
            # Check if neighbors are within bounds
            if x+1 >= w or y+1 >= h:
                continue
            
            p_center = pointmap[y, x]
            p_right  = pointmap[y, x+1]
            p_down   = pointmap[y+1, x]
            
            # Compute vectors
            v1 = p_right - p_center
            v2 = p_down - p_center
            
            v1 = v1 / torch.linalg.norm(v1)
            v2 = v2 / torch.linalg.norm(v2)
            
            # Cross product in camera coordinates
            n_c = torch.cross(v1, v2)
            # n_c *= 1e10
            
            # Compute norm of the normal vector
            norm_len = torch.linalg.norm(n_c)
            
            if norm_len < 1e-8:
                continue
            
            # Normalize and store
            normal_map[y, x] = n_c / norm_len
    
    return normal_map


def load_multiple_images(image_names, image_size=512, dtype=torch.float32):
    images = load_images(image_names, size=image_size, force_1024=True, dtype=dtype)
    img_ori = (images[0]['img_ori'].squeeze(0).permute(1,2,0)+1.)/2. # Just for reference
    return images, img_ori


def load_initial_images(image_name):
    images = load_images([image_name], size=512, force_1024=True)
    img_ori = (images[0]['img_ori'].squeeze(0).permute(1,2,0)+1.)/2. # [H, W, 3], range [0,1]
    if len(images) == 1:
        images = [images[0], copy.deepcopy(images[0])]
        images[1]['idx'] = 1
    return images, img_ori


def merge_surfels(
    new_surfels: list,
    current_timestamp: str,
    existing_surfels: list,
    existing_surfel_to_timestamp: dict,
    position_threshold: float = 0.025,
    normal_threshold: float = 0.7,
    max_points_per_node: int = 10  # 八叉树叶子节点允许的最大点数
):
    """
    将新的 surfel 合并到已有 surfel 列表中,使用八叉树来加速空间查找。
    
    Args:
        new_surfels (list[Surfel]): 待合并的新 surfel 列表。
        current_timestamp (str): 当前的时间戳。
        existing_surfels (list[Surfel]): 已存在的 surfel 列表。
        existing_surfel_to_timestamp (dict): 每个 surfel 索引到时间戳的映射。
        position_threshold (float): 判断两个 surfel 空间距离是否足够近的阈值。
        normal_threshold (float): 判断两个 surfel 法向是否对齐的阈值。
        max_points_per_node (int): 构建八叉树时,每个叶子节点最大允许的点数。
        
    Returns:
        (list[Surfel], dict): 
            - 未能匹配的 surfel 列表,需要追加到已有 surfel 列表中。
            - 更新后的 existing_surfel_to_timestamp 映射。
    """
    # 安全检查
    assert len(existing_surfels) == len(existing_surfel_to_timestamp), (
        "existing_surfels 和 existing_surfel_to_timestamp 长度不匹配。"
    )
    
    # 构造已有 surfel 的位置和法向数组
    positions = np.array([s.position for s in existing_surfels])  # Shape: (N, 3)
    normals = np.array([s.normal for s in existing_surfels])      # Shape: (N, 3)
    
    
    # 用于存储未匹配到已有 surfel 的新 surfel
    filtered_surfels = []
    
    merge_count = 0
    for new_surfel in new_surfels:
        is_merged = False
        for idx in range(len(positions)):
            if np.linalg.norm(positions[idx] - new_surfel.position) < position_threshold:
                if np.dot(normals[idx], new_surfel.normal) > normal_threshold:
                    existing_surfel_to_timestamp[idx].append(current_timestamp)
                    is_merged = True
                    merge_count += 1
                    break
        
        
        if not is_merged:
            filtered_surfels.append(new_surfel)
    
    # 返回未匹配的 surfel 列表及更新后的时间戳映射
    print(f"merge_count: {merge_count}")
    return filtered_surfels, existing_surfel_to_timestamp

def pointmap_to_surfels(pointmap: torch.Tensor,
                        focal_lengths: torch.Tensor,
                        depth_map: torch.Tensor,
                        poses: torch.Tensor, # shape: (4, 4)
                        radius_scale: float = 0.5,
                        depth_threshold: float = 1.0,
                        estimate_normals: bool = True):
    surfels = []
    if len(focal_lengths) == 2:
        focal_lengths = torch.mean(focal_lengths, dim=0)
    H, W = pointmap.shape[:2]
    # 1) Estimate normals
    if estimate_normals:
        normal_map = estimate_normal_from_pointmap(pointmap)
    else:
        normal_map = torch.zeros_like(pointmap)
    depth_remove_count = 0
    for v in range(H-1):
        for u in range(W-1):
            if depth_map[v, u] > depth_threshold:
                depth_remove_count += 1
                continue
            position = pointmap[v, u].detach().cpu().numpy() # in global coords
            normal = normal_map[v, u].detach().cpu().numpy() # in global coords
            depth = depth_map[v, u].detach().cpu().numpy() # in local coords
            view_direction = position - poses[0:3, 3].detach().cpu().numpy()
            view_direction = view_direction / np.linalg.norm(view_direction)
            if np.dot(view_direction, normal) < 0:
                normal = -normal
            adjustment_value = 0.2 + 0.8 * np.abs(np.dot(view_direction, normal))
            radius = (radius_scale * depth/focal_lengths/adjustment_value).detach().cpu().numpy()
            surfels.append(Surfel(position, normal, radius))
    print(f"depth_remove_count: {depth_remove_count}")
    return surfels



def run_dust3r(input_images,
               dust3r,
               batch_size = 1,
               niter = 1000,
               lr = 0.01,
               schedule = 'linear',
               clean_pc = False,
               focal_lengths = None,
               poses = None,
               device = 'cuda',
               background_mask = None,
               use_amp = False  # <<< AMP CHANGE: add a flag to enable/disable AMP
               ):

    # We wrap the entire inference and alignment in autocast so that
    # forward passes and any internal backward passes happen in mixed precision.
    with autocast(device_type='cuda', dtype=torch.float16, enabled=use_amp):
        pairs = make_pairs(input_images, scene_graph='complete', prefilter=None, symmetrize=True)
        output = inference(pairs, dust3r, device, batch_size=batch_size)

        mode = GlobalAlignerMode.PointCloudDifferentFocalOptimizer 
        scene = global_aligner(output, device=device, mode=mode)
        if focal_lengths is not None:
            scene.preset_focal(focal_lengths)
        if poses is not None:
            scene.preset_pose(poses)
        if mode == GlobalAlignerMode.PointCloudDifferentFocalOptimizer:
            # Depending on how dust3r internally does optimization,
            # it may or may not require gradient scaling.
            # If you need it, you can do something more manual with GradScaler.
            loss = scene.compute_global_alignment(init='mst', niter=niter, schedule=schedule, lr=lr)
        else:
            loss = None

        # If you want to clean up the pointcloud after alignment
        if clean_pc:
            scene = scene.clean_pointcloud()

    return scene, loss


if __name__ == "__main__":
    load_image_size = 512
    load_dtype = torch.float16
    device = 'cuda'
    model_path = "checkpoints/DUSt3R_ViTLarge_BaseDecoder_512_dpt.pth"
    selected_frame_paths = ["assets/jesus/jesus_0.jpg",
                            "assets/jesus/jesus_1.jpg",
                            "assets/jesus/jesus_2.jpg"
                            ]
    
    # pil_image = Image.open("./assets/radcliffe_camera_bg.png").resize((512, 288))
    # r, g, b, a = pil_image.split()
    # background_mask = a
    # background_mask = (1 - torch.tensor(np.array(background_mask))).unsqueeze(0).repeat(2, 1, 1).bool()
    
    all_surfels = []
    surfel_to_timestamp = {}
    
    dust3r = load_model(model_path, device=device)
    dust3r.eval()
    dust3r = dust3r.to(device)
    dust3r = dust3r.half()
    if len(selected_frame_paths) == 1:
        selected_frame_paths = selected_frame_paths * 2
    frame_images, frame_img_ori = load_multiple_images(selected_frame_paths, 
                                                       image_size=load_image_size, 
                                                       dtype=load_dtype)
    
    
    scene, loss = run_dust3r(frame_images, dust3r, device=device, use_amp=True)
    # --- 1) Extract outputs ---
    # pointcloud shape: [N, H, W, 3]
    shrink_factor = 0.15
    pointcloud = torch.stack(scene.get_pts3d())
    # poses shape: [N, 4, 4]
    # optimized_poses = scene.get_im_poses()
    # focal_lengths shape: [N]
    focal_lengths = scene.get_focals()
    

    # adjustion_transformation_matrix = SpatialConstructor.estimate_pose_alignment(optimized_poses, original_camera_poses) # optimized_poses -> original_camera_poses matrix
    # adjusted_optimized_poses = adjustion_transformation_matrix @ optimized_poses


    # --- 2) Resize pointcloud ---
    # Permute for resizing -> [N, 3, H, W]
    pointcloud = pointcloud.permute(0, 3, 1, 2)

    # Resize using bilinear interpolation
    pointcloud = F.interpolate(
        pointcloud, 
        scale_factor=shrink_factor, 
        mode='bilinear'
    )
    # Permute back -> [N, H', W', 3]
    pointcloud = pointcloud.permute(0, 2, 3, 1)[-1:]
    # transform pointcloud
    # pointcloud = torch.stack([SpatialConstructor.transform_pointmap(pointcloud[i], adjustion_transformation_matrix) for i in range(pointcloud.shape[0])])



    rgbs = scene.imgs
    rgbs = torch.tensor(np.array(rgbs))
    rgbs = rgbs.permute(0, 3, 1, 2)
    rgbs = F.interpolate(rgbs, scale_factor=shrink_factor, mode='bilinear')
    rgbs = rgbs.permute(0, 2, 3, 1)[-1:]
    visualize_pointcloud(pointcloud, rgbs, point_size=4)
    # --- 3) Resize depth map ---
    # depth_map shape: [N, H, W]
    depth_map = torch.stack(scene.get_depthmaps())

    # Add channel dimension -> [N, 1, H, W]
    depth_map = depth_map.unsqueeze(1)

    depth_map = F.interpolate(
        depth_map, 
        scale_factor=shrink_factor, 
        mode='bilinear'
    )
    
    poses = scene.get_im_poses()[-1:]

    # Remove channel dimension -> [N, H', W']
    depth_map = depth_map.squeeze(1)[-1:]

    for frame_idx in range(len(pointcloud)):
        # if frame_idx > 1:
        #     break
    # Create surfels for the current frame
        surfels = pointmap_to_surfels(
            pointmap=pointcloud[frame_idx],
            focal_lengths=focal_lengths[frame_idx] * shrink_factor,
            depth_map=depth_map[frame_idx],
            poses=poses[frame_idx],
            estimate_normals=True,
            radius_scale=0.5,
            depth_threshold=0.48
        )

        # Merge with existing surfels if not the first frame
        if frame_idx > 0:
            surfels, surfel_to_timestamp = merge_surfels(
                new_surfels=surfels,
                current_timestamp=frame_idx,
                existing_surfels=all_surfels,
                existing_surfel_to_timestamp=surfel_to_timestamp,
                position_threshold=0.01,
                normal_threshold=0.7
                    )
        
                    # Update timestamp mapping
        num_surfels = len(surfels)
        surfel_start_index = len(all_surfels)
        for surfel_index in range(num_surfels):
            # Each newly created surfel gets mapped to this frame index
            # surfel_to_timestamp[surfel_start_index + surfel_index] = [frame_idx]
            surfel_to_timestamp[surfel_start_index + surfel_index] = [2]
        all_surfels.extend(surfels)
    
    positions = np.array([s.position for s in all_surfels], dtype=np.float32)
    normals   = np.array([s.normal   for s in all_surfels], dtype=np.float32)
    radii     = np.array([s.radius   for s in all_surfels], dtype=np.float32)
    colors    = np.array([s.color    for s in all_surfels], dtype=np.float32)
    
    
    visualize_surfels(all_surfels)
    

    # np.savez(f"./surfels_added_first2.npz",
    #         positions=positions,
    #         normals=normals,
    #         radii=radii,
    #         colors=colors)
    # with open("surfel_to_timestamp_first2.json", "w") as f:
    #     json.dump(surfel_to_timestamp, f)
    
    
    np.savez(f"./surfels_added_only3.npz",
            positions=positions,
            normals=normals,
            radii=radii,
            colors=colors)
    with open("surfel_to_timestamp_only3.json", "w") as f:
        json.dump(surfel_to_timestamp, f)
    
    stop = 1