python环境下用open3d可视化mesh

以下是一段Open3d阻塞模式(就是不可以更新mesh)可视化mesh的代码

vis = o3d.visualization.Visualizer()
vis.create_window(width=512, height=512)
vis.add_geometry(mesh)
# while True:
vis.run()

  mesh加载的代码如下:

mesh = o3d.io.read_triangle_mesh(filename, False)

  mesh保存代码如下:

o3d.io.write_triangle_mesh(tgt_dir + obj_name[:-4] + "_right.obj", meshes[2])

    

       如果保存下的obj没有纹理,很可能是中间的操作出了问题,有了些用不到的纹理图,需要用可视化的方式看看,如果o3d可视化用彩色,则可以,否则就没有颜色。

如果要删除一些三角形面。

def mesh_clean_fcn(mesh, triangles_mask):

    triangles = np.asarray(mesh.triangles)
    triangles_clean = triangles[triangles_mask]
    ###

    triangle_uvs = np.asarray(mesh.triangle_uvs)
    triangle_uvs_clean = []
    for i, mask in enumerate(triangles_mask):
        if mask:
            triangle_uvs_clean.append(triangle_uvs[3 * i + 0, :])
            triangle_uvs_clean.append(triangle_uvs[3 * i + 1, :])
            triangle_uvs_clean.append(triangle_uvs[3 * i + 2, :])
    triangle_uvs_clean = np.asarray(triangle_uvs_clean)
    mesh.triangle_uvs = o3d.utility.Vector2dVector(triangle_uvs_clean)
    ###


    vertex_id_unique = np.sort(np.unique(triangles_clean)).tolist()
    vertices = np.asarray(mesh.vertices)
    vertices_clean = vertices[vertex_id_unique]
    for i, fac in enumerate(triangles_clean):
        triangles_clean[i, 0] = vertex_id_unique.index(fac[0])
        triangles_clean[i, 1] = vertex_id_unique.index(fac[1])
        triangles_clean[i, 2] = vertex_id_unique.index(fac[2])
    mesh.triangles = o3d.utility.Vector3iVector(triangles_clean)
    mesh.vertices = o3d.utility.Vector3dVector(vertices_clean)


    vertex_normals = np.asarray(mesh.vertex_normals)
    vertex_normals_clean = vertex_normals[vertex_id_unique]
    mesh.vertex_normals = o3d.utility.Vector3dVector(vertex_normals_clean)

    triangle_normals = np.asarray(mesh.triangle_normals)
    triangle_normals_clean = triangle_normals[triangles_mask]
    mesh.triangle_normals = o3d.utility.Vector3dVector(triangle_normals_clean)

    triangle_material_ids = np.asarray(mesh.triangle_material_ids)
    triangle_material_ids_clean = triangle_material_ids[triangles_mask]
    material_ids = np.unique(triangle_material_ids_clean).tolist()
    for i, id_ in enumerate(triangle_material_ids_clean):
        triangle_material_ids_clean[i] = material_ids.index(id_)
    mesh.triangle_material_ids = o3d.utility.IntVector(triangle_material_ids_clean)

    import pdb
    pdb.set_trace()

    backup_texture = copy.copy(mesh.textures)
    mesh.textures = [backup_texture[i] for i in material_ids] ##mesh.textures[material_ids]

    # for i, id in enumerate(material_ids):
    #     print("mesh.textures.append(backup_texture[id])")
    #     mesh.textures.append(backup_texture[id])
    #     mesh.textures.append(1)

    import pdb
    pdb.set_trace()


    vis = o3d.visualization.Visualizer()
    vis.create_window(width=512, height=512)
    vis.add_geometry(mesh)
    # while True:
    vis.run()
    return mesh

你可能感兴趣的:(计算机知识,图像,算法,三维模型处理,三维模型可视化)