Skip to content

Instantly share code, notes, and snippets.

@SJongeJongeJonge
Created January 4, 2021 09:22
Show Gist options
  • Save SJongeJongeJonge/af172b6c6b970de9513cf7792a7a2f8b to your computer and use it in GitHub Desktop.
Save SJongeJongeJonge/af172b6c6b970de9513cf7792a7a2f8b to your computer and use it in GitHub Desktop.
Open3d Tests
import open3d.visualization.gui as gui
import open3d.visualization.rendering as rendering
class TestWindow:
def __init__(self, pcd_file, width, height):
self.pcd_file = pcd_file
self._window = gui.Application.instance.create_window("Test", width, height)
w = self._window
self._scene = gui.SceneWidget()
self._scene.scene = rendering.Open3DScene(w.renderer)
# self._scene.scene.set_background_color([68, 68, 68, 255])
self._scene.scene.set_background_color([200, 200, 200, 255])
# self._scene.scene.set_background_color(gui.Color(1, 1, 1))
self._scene.set_view_controls(gui.SceneWidget.Controls.ROTATE_CAMERA)
em = w.theme.font_size
separation_height = int(round(0.5 * em))
self._settings_panel = gui.Vert(0, gui.Margins(0.25*em, 0.25*em, 0.25*em, 0.25*em))
# View controls
view_ctrls = gui.CollapsableVert("View controls", 0.25 * em,
gui.Margins(em, 0, 0, 0))
# Model button
self._model_button = gui.Button("Model")
self._model_button.horizontal_padding_em = 0.5
self._model_button.vertical_padding_em = 0.25
self._model_button.set_on_clicked(self._set_mouse_mode_model)
# Pick button
self._pick_button = gui.Button("Pick")
self._pick_button.horizontal_padding_em = 0.5
self._pick_button.vertical_padding_em = 0.25
self._pick_button.set_on_clicked(self._set_mouse_mode_pick)
view_ctrls.add_child(gui.Label("Mouse controls"))
h = gui.Horiz(0.25 * em)
h.add_child(self._model_button)
h.add_child(self._pick_button)
h.add_stretch()
view_ctrls.add_child(h)
self._settings_panel.add_child(view_ctrls)
w.set_on_layout(self._on_layout2)
w.add_child(self._scene)
w.add_child(self._settings_panel)
self.load_model(pcd_file)
self.setup_camera()
def _on_layout(self, theme):
# https://github.com/intel-isl/Open3D/blob/master/examples/python/gui/vis-gui.py#L505
# The on_layout callback should set the frame (position + size) of every
# child correctly. After the callback is done the window will layout
# the grandchildren.
print('_on_layout')
r = self.window.content_rect
self._scene.frame = r
width = 17 * theme.font_size
height = min(r.height,
self._settings_panel.calc_preferred_size(theme).height)
self._settings_panel.frame = gui.Rect(r.get_right() - width, r.y, width,
height)
def _on_layout2(self, theme):
# https://github.com/intel-isl/Open3D-ML/blob/master/ml3d/vis/visualizer.py#1304
print('_on_layout')
frame = self._window.content_rect
em = theme.font_size
panel_width = 20 * em
panel_rect = gui.Rect(frame.get_right() - panel_width, frame.y,
panel_width, frame.height - frame.y)
self._settings_panel.frame = panel_rect
self._scene.frame = gui.Rect(frame.x, frame.y, panel_rect.x - frame.x,
frame.height - frame.y)
def load_model(self, pcd_file):
try:
self.geometry = o3d.io.read_point_cloud(pcd_file)
self._scene.scene.add_geometry("__model__", self.geometry, self.get_material())
except Exception as e:
print(e)
def setup_camera(self):
try:
bounds = self.geometry.get_axis_aligned_bounding_box()
self._scene.setup_camera(60, bounds, bounds.get_center())
except Exception as e:
print(e)
def setup_sun(self):
self._scene.scene.scene.set_sun_light([0.577, -0.577, -0.577], gui.Color(1, 1, 1), 45000)
self._scene.scene.scene.enable_sun_light(True)
def get_material(self):
# For shaders see: https://github.com/intel-isl/Open3D/tree/master/cpp/open3d/visualization/gui/Materials
mat = rendering.Material()
# mat.shader = "defaultUnlit"
mat.shader = "unlitSolidColor"
mat.base_color = [1.0, 0.2, 0.2, 1.0]
return mat
def _set_mouse_mode_model(self):
self._scene.set_view_controls(gui.SceneWidget.Controls.ROTATE_MODEL)
def _set_mouse_mode_pick(self):
self._scene.set_view_controls(gui.SceneWidget.Controls.PICK_POINTS)
def main():
# o3d.utility.set_verbosity_level(o3d.utility.VerbosityLevel.Debug)
print('====INITIALIZE====')
gui.Application.instance.initialize()
print('====END INITIALIZE====')
exception = None
try:
print('====TESTWINDOW====')
test = TestWindow("examples/test_data/Armadillo.ply", 1024, 800)
print('====END TESTWINDOW====')
except Exception as e:
print('====EXCEPTION====')
exception = e
raise e
# if not exception:
# print('No exceptions: running')
print('====BEGIN RUN====')
gui.Application.instance.run()
print('====END====')
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment