【Open3D】第二篇:GUI编程

文章目录

  • 基本控件创建
    • 创建文本框
    • 创建button
    • 创建布局
  • 绘制形状
    • 绘制线段
    • 绘制点云
  • 设置属性
    • 设置线宽
    • 设置点大小
  • 可用Shader汇总
  • GUI框架

基本控件创建

创建文本框

push_edit = gui.TextEdit()

创建button

push_button = gui.Button('...')
push_button.horizontal_padding_em = 0.5
push_button.vertical_padding_em = 0
push_button.set_on_clicked(self._on_push_button) # 设置回调函数

创建布局

# 文本框和按钮水平布局
push_button_layout = gui.Horiz()
push_button_layout.add_child(gui.Label('Push Button'))
push_button_layout.add_child(self._push_edit)
push_button_layout.add_fixed(0.25*em)
push_button_layout.add_child(push_button)

# 总体垂直布局
self.pannel = gui.Vert()
self.pannel.add_fixed(0.5*em)
self.pannel.add_child(push_button_layout)
self.window.add_child(self.pannel)

绘制形状

绘制线段

import open3d.visualization.gui as gui
points = [[0,0,0],[1,0,0],[0,1,0],[1,1,0],[0,0,1],[1,0,1],[0,1,1],[1,1,1]]
lines = [[0,1],[0,2],[1,3],[2,3],[4,5],[4,6],[5,7],[6,7],[0,4],[1,5],[2,6],[3,7]]
object_lines = o3d.geometry.LineSet(
    points=o3d.utility.Vector3dVector(points),
    lines=o3d.utility.Vector2iVector(lines),
)
# define material
material = rendering.MaterialRecord()
material.shader = 'defaultLit'

# insert into scene
my_scene = gui.SceneWidget()
my_scene.scene = rendering.Open3DScene(w.renderer)
my_scene.scene.add_geometry('lines',object_lines ,material)
bounds = object_lines.get_axis_aligned_bounding_box()
my_scene.setup_camera(60,bounds,bounds.get_center())
my_scene.force_redraw()

绘制点云

import numpy as np
import open3d as o3d
import open3d.visualization.rendering as rendering

cloud=o3d.io.read_point_cloud("your_path_to_point_cloud")
cloud_xyz=np.asarray(cloud.points)

# define material
material = rendering.MaterialRecord()
material.shader = 'defaultLit'

# define colors
ctmp=np.zeros((len(cloud_xyz),3))
ctmp[:,0]=1

# rendering
object_pts=o3d.geometry.PointCloud()
object_pts.points=o3d.utility.Vector3dVector(cloud_xyz)
object_pts.colors=o3d.utility.Vector3dVector(ctmp)

设置属性

设置线宽

material_default = rendering.MaterialRecord()
material_default.shader = 'unlitLine'
material_default.line_width = 3
self._scene.scene.add_geometry('lines', object_lines,  material_default)

设置点大小

material = o3d.visualization.rendering.Material()
material.shader = "defaultLit"
material.base_color = color
material.point_size = size
scene.add_geometry(name, cloud, material)

可用Shader汇总

参考网址

类型
适用shader
网格 defaultLit/defaultLitTransparencynormals
点云 defaultLit/defaultLitTransparencynormalsdefaultUnlit/defaultUnlitTransparency
线段 最好unlitLine,只有这个可以控制线宽

GUI框架

main_window.py

import open3d.visualization.gui as gui

class App:
    count = 0
    def __init__(self):
        # 初始化
        gui.Application.instance.initialize()
        self.window = gui.Application.instance.create_window("Event and Widget", 300, 600)
        
        # 使用相对大小避免直接设置像素,因为不同显示器像素大小可能不同
        em = self.window.theme.font_size

        # 文本框和按钮
        self._push_edit = gui.TextEdit()
        push_button = gui.Button('...')
        push_button.horizontal_padding_em = 0.5
        push_button.vertical_padding_em = 0
        push_button.set_on_clicked(self._on_push_button)

        # 文本框和按钮水平布局
        push_button_layout = gui.Horiz()
        push_button_layout.add_child(gui.Label('Push Button'))
        push_button_layout.add_child(self._push_edit)
        push_button_layout.add_fixed(0.25*em)
        push_button_layout.add_child(push_button)

        # 总体垂直布局
        self.pannel = gui.Vert()
        self.pannel.add_fixed(0.5*em)
        self.pannel.add_child(push_button_layout)

        self.window.add_child(self.pannel)

    def _on_push_button(self):
        self.count += 1
        # 设置文本框文字
        self._push_edit.text_value = f'push count {self.count}'
        # 弹出消息框
        self.window.show_message_box('Push Info', 'Hello World!')

    def run(self):
        gui.Application.instance.run()

if __name__ == "__main__":
    app = App()
    app.run()

你可能感兴趣的:(python,点云处理,GUI编程)