pptk icon indicating copy to clipboard operation
pptk copied to clipboard

Unexpected behaviour when loading a point cloud to viewer

Open sitzikbs opened this issue 3 years ago • 0 comments

I created a viewer and loaded a point cloud to it. I want to update the viewer with new point clouds based on some input. When I do that, the view "jumps back to the initial position. I tried resetting the view parameters immediately after loading the new point cloud to the viewer but then it flickers. Is this the desired behaviour? if so, is there a way around it?

Attached bellow is a minimum working example.


import numpy as np
import pptk
import open3d.visualization.gui as gui


class PCWindow:
    def __init__(self, pc_data):
        self.pc_data = pc_data
        self.frame_ind = 0

        # create frame slider
        self.window = gui.Application.instance.create_window("Test", 400, 80)
        em = self.window.theme.font_size
        layout = gui.Vert(0, gui.Margins(0.5 * em, 0.5 * em, 0.5 * em, 0.5 * em))
        slider = gui.Slider(gui.Slider.INT)
        slider.set_limits(0, 19)
        slider.set_on_value_changed(self._on_slider)
        self._label = gui.Label("Frame selector")
        self._label.text_color = gui.Color(1.0, 0.5, 0.0)
        layout.add_child(self._label)
        layout.add_child(slider)
        self.window.add_child(layout)

        # run viewer
        self.viewer = pptk.viewer(pc_data[0][:, :3])
        self.viewer.set(r=10)

    def _on_slider(self, new_val):
        self.frame_ind = int(new_val)
        phi, theta, r = self.viewer.get("phi"), self.viewer.get("theta"), self.viewer.get("r")
        print(new_val)
        self.viewer.clear()
        self.viewer.load(self.pc_data[self.frame_ind][:, :3])
        self.viewer.set(phi=phi, theta=theta, r=r)


def main():
    # Generate random points on a sphere
    phi = np.linspace(0, np.pi, 200)
    theta = np.linspace(0, 2 * np.pi, 200)
    xx = np.outer(np.sin(theta), np.cos(phi))
    yy = np.outer(np.sin(theta), np.sin(phi))
    zz = np.outer(np.cos(theta), np.ones_like(phi))

    # Shuffle and stack the points
    pc_data = []
    for i in range(20):
        points = np.stack([xx.flatten(), yy.flatten(), zz.flatten()], 1)
        np.random.shuffle(points)
        pc_data.append(points + i / 20)


    #run gui and viewer
    gui.Application.instance.initialize()
    w = PCWindow(pc_data)
    gui.Application.instance.run()


if __name__ == "__main__":
    main()

Note: Related to #35 and #21 and #15.

sitzikbs avatar Mar 05 '21 11:03 sitzikbs