File size: 8,035 Bytes
d82ae7a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import open3d as o3d
import numpy as np
import os

def align_scene_to_z_up(pcd, save_pcd_path=None, save_transform_path=None, visualize=False, fit_ground=True):
    """
    将点云扫描转换为Z轴向上对齐,并尝试将墙壁对齐到X-Y平面
    
    参数:
        points (np.ndarray): (N, 3) 点云数据
        visualize (bool): 是否可视化结果
        
    返回:
        aligned_points (np.ndarray): 对齐后的点云
        transform_matrix (np.ndarray): 应用的4x4变换矩阵
    """
    points = np.asarray(pcd.points)
    centroid = np.mean(points, axis=0)
    distance_threshold = 0.02  # 动态距离阈值
    R_ground = np.eye(3)  # Initialize ground rotation matrix
    if fit_ground:
        plane_model, ground_inliers = pcd.segment_plane(
            distance_threshold=distance_threshold,
            ransac_n=3,
            num_iterations=1000
        )
        [a, b, c, d] = plane_model
        ground_normal = np.array([a, b, c])
        # print('ground_normal:', ground_normal)
        # 可视化地面点云
        ground_cloud = pcd.select_by_index(ground_inliers)
        ground_cloud.paint_uniform_color([1, 0, 0])  # 红色表示地面
        if visualize:
            o3d.visualization.draw_geometries([ground_cloud])
        
        # 3. 计算旋转矩阵使地面法向量对齐到Z轴
        z_axis = np.array([0, 0, 1])
        ground_normal = ground_normal / np.linalg.norm(ground_normal)
        
        # 计算旋转轴和角度
        rotation_axis = np.cross(ground_normal, z_axis)
        if np.linalg.norm(rotation_axis) < 1e-6:
            rotation_axis = np.array([0, 1, 0])  # 避免零向量
        else:
            rotation_axis = rotation_axis / np.linalg.norm(rotation_axis)
        
        cos_theta = np.dot(ground_normal, z_axis)
        angle = np.arccos(np.clip(cos_theta, -1.0, 1.0))

        # 使用罗德里格斯公式计算旋转矩阵
        K = np.array([
            [0, -rotation_axis[2], rotation_axis[1]],
            [rotation_axis[2], 0, -rotation_axis[0]],
            [-rotation_axis[1], rotation_axis[0], 0]
        ])
        R_ground = np.eye(3) + np.sin(angle) * K + (1 - np.cos(angle)) * (K @ K)
        
        # [Alternate] concise method
        # from scipy.spatial.transform import Rotation as R
        # R_ground = R.from_rotvec(rotation_axis * angle).as_matrix()

        # 4. 应用地面旋转
        centered_points = points - centroid
        ground_rotated = (R_ground @ centered_points.T).T
        
        # 5. 检测墙壁平面(垂直平面)
        all_indices = np.arange(len(points))
        non_ground_indices = np.setdiff1d(all_indices, ground_inliers)
        non_ground_points = ground_rotated[non_ground_indices]
        
        # 创建临时点云用于墙壁检测
        temp_pcd = o3d.geometry.PointCloud()
        temp_pcd.points = o3d.utility.Vector3dVector(non_ground_points)
        remaining_pcd = temp_pcd
    else:
        # already z up
        remaining_pcd = pcd
    
    wall_planes = []
    wall_directions = []
    max_points = 0
    best_direction = None
    R_walls = np.eye(3)
    # 检测多个墙壁平面
    for _ in range(6):
        if len(remaining_pcd.points) < 100:
            break
            
        plane_model, inliers = remaining_pcd.segment_plane(
            distance_threshold=distance_threshold,
            ransac_n=3,
            num_iterations=1000
        )
        
        wall_cloud = remaining_pcd.select_by_index(inliers)
        wall_cloud.paint_uniform_color([1, 0, 0])  # 红色表示地面
        if visualize:
            o3d.visualization.draw_geometries([wall_cloud])

        [a, b, c, d] = plane_model
        normal = np.array([a, b, c])
        
        # 检查是否为垂直平面(法向量的Z分量接近0)
        if abs(normal[2]) < 0.1 and np.linalg.norm(normal[:2]) > 0.5:
            # 提取水平方向
            horizontal_dir = normal[:2] / np.linalg.norm(normal[:2])
            wall_directions = horizontal_dir
            # print('wall direction',normal)
            if max_points < len(inliers):
                max_points = len(inliers)
                # print(max_points)
                best_direction = wall_directions
                 # 计算最佳方向的旋转角度
                angle = np.arctan2(best_direction[1], best_direction[0])
                
                # 创建最终的旋转矩阵
                R_walls = np.array([
                    [np.cos(-angle), -np.sin(-angle), 0],
                    [np.sin(-angle), np.cos(-angle), 0],
                    [0, 0, 1]
                ])
        
        remaining_pcd = remaining_pcd.select_by_index(inliers, invert=True)

    
    # 8. 创建变换矩阵(旋转 + 平移)
    if fit_ground:
        # Compose the transformations: first ground rotation, then wall rotation
        combined_rotation = R_walls @ R_ground
        transform_matrix = np.eye(4)
        transform_matrix[:3, :3] = combined_rotation
        transform_matrix[:3, 3] = -combined_rotation @ centroid
    else:
        # Only wall rotation
        transform_matrix = np.eye(4)
        transform_matrix[:3, :3] = R_walls
        transform_matrix[:3, 3] = -R_walls @ centroid  # 平移使中心到原点
    
    # 9. 应用变换
    aligned_points = (transform_matrix[:3, :3] @ points.T + transform_matrix[:3, 3:4]).T
    
    # 10. 创建对齐后的点云
    aligned_pcd = o3d.geometry.PointCloud()
    aligned_pcd.points = o3d.utility.Vector3dVector(aligned_points)

    if pcd.colors:
        aligned_pcd.colors = pcd.colors
    
    # 11. 保存结果
    if save_pcd_path:
        o3d.io.write_point_cloud(save_pcd_path, aligned_pcd)
    
    if save_transform_path:
        np.savetxt(save_transform_path, transform_matrix, fmt='%.8f')
    

    
    return aligned_pcd, transform_matrix

    """创建4x4变换矩阵"""
    transform = np.eye(4)
    transform[:3, :3] = rotation_matrix
    return transform

def visualize_alignment(pcd_orig, pcd_aligned):
    """可视化原始点云和对齐后的点云"""
    pcd_orig.paint_uniform_color([1, 0, 0])  # 红色为原始点云
    pcd_aligned.paint_uniform_color([0, 1, 0])  # 绿色为对齐后点云
    
    # 创建坐标系
    coord_frame = o3d.geometry.TriangleMesh.create_coordinate_frame(size=1.0)
    
    o3d.visualization.draw_geometries([pcd_orig, pcd_aligned, coord_frame])



npy_dir = '/media/vivo/vivo/Datasets/ScanNetpp/ScanNetpp_preprocessed/train/'
scene_names = os.listdir(npy_dir)

for scene_name in scene_names:
    if scene_name.endswith('.zip'): 
        continue

    print(scene_name)

    scene_dir = os.path.join(npy_dir, scene_name)

    npy_path = os.path.join(scene_dir, 'coord.npy')
    points = np.load(npy_path)  # (N, 3) 或 (N, 6)

    # 创建点云对象
    pcd = o3d.geometry.PointCloud()
    pcd.points = o3d.utility.Vector3dVector(points[:, :3])

    if points.shape[1] == 6:
        pcd.colors = o3d.utility.Vector3dVector(points[:, 3:6])  # 如果包含 RGB
    
    
    # 执行对齐
    transformed_pcd, transform = align_scene_to_z_up(
        pcd,
        # save_transform_path=os.path.join(scene_dir, "transform.txt"),
        visualize=False,
        fit_ground=False
    )

    aligned_points = np.asarray(transformed_pcd.points)

    # scale point cloud
    # min_z = np.min(aligned_points[:, 2])
    # max_z = np.max(aligned_points[:, 2])
    # height = max_z - min_z
    # print(f"Original height: {height}")
    # estimated_height = 2.5
    # scale = estimated_height / height
    # print(f"Scale factor: {scale}")

    # aligned_points_scaled = aligned_points * scale

    transformed_pcd.points = o3d.utility.Vector3dVector(aligned_points)

    # 保存为对齐后的 PLY 文件
    # aligned_ply_path = os.path.join(scene_dir, scene_name + ".ply")
    # o3d.io.write_point_cloud(aligned_ply_path, transformed_pcd)

    # 也可以保存为对齐后的 npy
    np.save(os.path.join(scene_dir, "coord_align.npy"), aligned_points)