Example #1
0
    def main(self):
        '''
        Main function: Creates the user window and side bar with a button. This button is used to toggle between normal point cloud and downsampled point cloud
        '''
        pangolin.CreateWindowAndBind('Sample Toggle App', 1280,
                                     960)  #Set sindow size and name
        gl.glEnable(
            gl.GL_DEPTH_TEST
        )  #This is enabled to allow for user to move/rotate 3D image with mouse

        #Define render object
        SampleToggling.scam = pangolin.OpenGlRenderState(
            pangolin.ProjectionMatrix(640, 480, 420, 420, 320, 240, 0.1, 1000),
            pangolin.ModelViewLookAt(0, 0.5, -3, 0, 0, 0,
                                     pangolin.AxisDirection.AxisY))
        handler3d = pangolin.Handler3D(SampleToggling.scam)

        #Add viewpoint object
        SampleToggling.dcam = pangolin.CreateDisplay()
        SampleToggling.dcam.SetBounds(0.0, 1.0, 180 / 640., 1.0,
                                      -640.0 / 480.0)

        SampleToggling.dcam.SetHandler(pangolin.Handler3D(SampleToggling.scam))

        #Create the side panel
        panel = pangolin.CreatePanel('buttonPanel')
        panel.SetBounds(0.0, 1.0, 0.0, 180 / 640.)

        gl.glPointSize(1)  #Set the size of each point on the point cloud

        button = pangolin.VarBool('buttonPanel.Sampling Toggle Button',
                                  value=False,
                                  toggle=False)  #Create a simple button

        #Read the .ply file and convert it into a numpy array so it can be understood by pangolin
        pointCloudOrigional = o3d.io.read_point_cloud("bunny.ply")
        pointCloudOrigionalArray = np.asarray(pointCloudOrigional.points)

        #Down sample this read .ply file and then convert that into a numpy array
        pointCloudDownSampled = voxel_down_sample(pointCloudOrigional,
                                                  voxel_size=0.008)
        pointCloudDownSampledArray = np.asarray(pointCloudDownSampled.points)

        #Zoom the pointCloud so it is easy for user to see
        pointCloudOrigionalZoomed = pointCloudOrigionalArray * self.zoomCoefficient
        pointCloudSampledZoomed = pointCloudDownSampledArray * self.zoomCoefficient

        #Run a loop until the program is quit, toggling image each time button is pressed.
        while not pangolin.ShouldQuit():
            self.drawPoints(pointCloudOrigionalZoomed)
            if pangolin.Pushed(button):
                while not pangolin.Pushed(button):
                    self.drawPoints(pointCloudSampledZoomed)
Example #2
0
def main():
    pangolin.ParseVarsFile('app.cfg')

    pangolin.CreateWindowAndBind('Main', 640, 480)
    gl.glEnable(gl.GL_DEPTH_TEST)

    scam = pangolin.OpenGlRenderState(
        pangolin.ProjectionMatrix(640, 480, 420, 420, 320, 240, 0.1, 1000),
        pangolin.ModelViewLookAt(0, 0.5, -3, 0, 0, 0,
                                 pangolin.AxisDirection.AxisY))
    handler3d = pangolin.Handler3D(scam)

    dcam = pangolin.CreateDisplay()
    dcam.SetBounds(0.0, 1.0, 180 / 640., 1.0, -640.0 / 480.0)
    # dcam.SetBounds(pangolin.Attach(0.0),     pangolin.Attach(1.0),
    # pangolin.Attach.Pix(180), pangolin.Attach(1.0), -640.0/480.0)

    dcam.SetHandler(pangolin.Handler3D(scam))

    panel = pangolin.CreatePanel('ui')
    panel.SetBounds(0.0, 1.0, 0.0, 180 / 640.)

    button = pangolin.VarBool('ui.Button', value=False, toggle=False)
    checkbox = pangolin.VarBool('ui.Checkbox', value=False, toggle=True)
    float_slider = pangolin.VarFloat('ui.Float', value=3, min=0, max=5)
    float_log_slider = pangolin.VarFloat('ui.Log_scale var',
                                         value=3,
                                         min=1,
                                         max=1e4,
                                         logscale=True)
    int_slider = pangolin.VarInt('ui.Int', value=2, min=0, max=5)
    int_slave_slider = pangolin.VarInt('ui.Int_slave', value=2, toggle=False)

    save_window = pangolin.VarBool('ui.Save_Window', value=False, toggle=False)
    save_cube = pangolin.VarBool('ui.Save_Cube', value=False, toggle=False)
    record_cube = pangolin.VarBool('ui.Record_Cube', value=False, toggle=False)

    def reset():
        #float_slider.SetVal(0.5)
        print('You typed ctrl-r or pushed reset')

    # Reset = SetVarFunctor(float_slider, 0.5)
    # reset = pangolin.VarFunc('ui.Reset', reset)
    # pangolin.RegisterKeyPressCallback(int(pangolin.PANGO_CTRL) + ord('r'), reset)      # segfault
    # pangolin.RegisterKeyPressCallback(int(pangolin.PANGO_CTRL) + ord('b'), pangolin.SetVarFunctorFloat('ui.Float', 4.5))      # segfault
    # pangolin.RegisterKeyPressCallback(int(pangolin.PANGO_CTRL) + ord('b'), SetVarFunctor(float_slider, 4.5))      # segfault

    while not pangolin.ShouldQuit():
        gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT)

        if pangolin.Pushed(button):
            print('You Pushed a button!')

        if checkbox.Get():
            int_slider.SetVal(int(float_slider))
        int_slave_slider.SetVal(int_slider)

        if pangolin.Pushed(save_window):
            pangolin.SaveWindowOnRender("window")

        if pangolin.Pushed(save_cube):
            pangolin.SaveWindowOnRender("cube")

        if pangolin.Pushed(record_cube):
            pangolin.DisplayBase().RecordOnRender(
                "ffmpeg:[fps=50,bps=8388608,unique_filename]//screencap.avi")

        dcam.Activate(scam)
        gl.glColor3f(1.0, 1.0, 1.0)
        pangolin.glDrawColouredCube()
        pangolin.FinishFrame()
Example #3
0
    def view(self):
        pangolin.CreateWindowAndBind('Viewer', 1024, 768)

        gl.glEnable(gl.GL_DEPTH_TEST)
        gl.glEnable(gl.GL_BLEND)
        gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA)

        panel = pangolin.CreatePanel('menu')
        panel.SetBounds(0.5, 1.0, 0.0, 175 / 1024.)

        # checkbox
        m_follow_camera = pangolin.VarBool('menu.Follow Camera',
                                           value=True,
                                           toggle=True)
        m_show_points = pangolin.VarBool('menu.Show Points', True, True)
        m_show_lines = pangolin.VarBool('menu.Show Lines', True, True)
        m_show_keyframes = pangolin.VarBool('menu.Show KeyFrames', True, True)
        m_show_graph = pangolin.VarBool('menu.Show Graph', True, True)
        m_show_image = pangolin.VarBool('menu.Show Image', True, True)

        # button
        m_replay = pangolin.VarBool('menu.Replay', value=False, toggle=False)
        m_refresh = pangolin.VarBool('menu.Refresh', False, False)
        m_reset = pangolin.VarBool('menu.Reset', False, False)

        if self.config is None:
            width, height = 400, 250
            viewpoint_x = 0
            viewpoint_y = -500  # -10
            viewpoint_z = -100  # -0.1
            viewpoint_f = 2000
            camera_width = 1.
        else:
            width = self.config.view_image_width
            height = self.config.view_image_height
            viewpoint_x = self.config.view_viewpoint_x
            viewpoint_y = self.config.view_viewpoint_y
            viewpoint_z = self.config.view_viewpoint_z
            viewpoint_f = self.config.view_viewpoint_f
            camera_width = self.config.view_camera_width

        proj = pangolin.ProjectionMatrix(1024, 768, viewpoint_f, viewpoint_f,
                                         512, 389, 0.1, 5000)
        look_view = pangolin.ModelViewLookAt(viewpoint_x, viewpoint_y,
                                             viewpoint_z, 0, 0, 0, 0, -1, 0)

        # Camera Render Object (for view / scene browsing)
        scam = pangolin.OpenGlRenderState(proj, look_view)

        # Add named OpenGL viewport to window and provide 3D Handler
        dcam = pangolin.CreateDisplay()
        dcam.SetBounds(0.0, 1.0, 175 / 1024., 1.0, -1024 / 768.)
        dcam.SetHandler(pangolin.Handler3D(scam))

        # image
        # width, height = 400, 130
        dimg = pangolin.Display('image')
        dimg.SetBounds(0, height / 768., 0.0, width / 1024., 1024 / 768.)
        dimg.SetLock(pangolin.Lock.LockLeft, pangolin.Lock.LockTop)

        texture = pangolin.GlTexture(width, height, gl.GL_RGB, False, 0,
                                     gl.GL_RGB, gl.GL_UNSIGNED_BYTE)
        image = np.ones((height, width, 3), 'uint8')

        pose = pangolin.OpenGlMatrix()  # identity matrix
        following = True

        active = []
        replays = []
        graph = []
        loops = []
        mappoints = DynamicArray(shape=(3, ))
        maplines = DynamicArray(shape=(6, ))
        colors = DynamicArray(shape=(3, ))
        cameras = DynamicArray(shape=(4, 4))

        active_lines = []
        line_colors = []
        gnd_mesh = None

        while not pangolin.ShouldQuit():

            if not self.q_pose.empty():
                pose.m = self.q_pose.get()

            follow = m_follow_camera.Get()
            if follow and following:
                scam.Follow(pose, True)
            elif follow and not following:
                scam.SetModelViewMatrix(look_view)
                scam.Follow(pose, True)
                following = True
            elif not follow and following:
                following = False

            gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT)
            gl.glClearColor(1.0, 1.0, 1.0, 1.0)
            dcam.Activate(scam)

            # show graph
            if not self.q_graph.empty():
                graph = self.q_graph.get()
                loops = np.array([_[0] for _ in graph if _[1] == 2])
                graph = np.array([_[0] for _ in graph if _[1] < 2])
            if m_show_graph.Get():
                if len(graph) > 0:
                    gl.glLineWidth(1)
                    gl.glColor3f(0.0, 1.0, 0.0)
                    pangolin.DrawLines(graph, 3)
                if len(loops) > 0:
                    gl.glLineWidth(2)
                    gl.glColor3f(0.0, 0.0, 0.0)
                    pangolin.DrawLines(loops, 4)

                gl.glPointSize(4)
                gl.glColor3f(1.0, 0.0, 0.0)
                gl.glBegin(gl.GL_POINTS)
                gl.glVertex3d(pose[0, 3], pose[1, 3], pose[2, 3])
                gl.glEnd()

            # if not self.q_ground.empty():
            #     gnd_mesh = self.q_ground.get()

            if gnd_mesh is not None:
                gl.glLineWidth(2)
                gl.glColor3f(0.5, 0.25, 0.5)
                pangolin.DrawLines(gnd_mesh, 2)

            # Show mappoints
            if not self.q_points.empty():
                pts, code = self.q_points.get()
                cls, code = self.q_colors.get()
                if code == 1:  # append new points
                    mappoints.extend(pts)
                    colors.extend(cls)
                elif code == 0:  # refresh all points
                    mappoints.clear()
                    mappoints.extend(pts)
                    colors.clear()
                    colors.extend(cls)

            if m_show_points.Get():
                gl.glPointSize(4)
                # easily draw millions of points
                pangolin.DrawPoints(mappoints.array(), colors.array())

                if not self.q_active.empty():
                    active = self.q_active.get()

                gl.glPointSize(5)
                gl.glBegin(gl.GL_POINTS)
                gl.glColor3f(1.0, 0.0, 0.0)
                for point in active:
                    gl.glVertex3f(*point)
                gl.glEnd()

            if not self.q_lines.empty():
                lines, code = self.q_lines.get()
                maplines.extend(lines)

            if m_show_lines.Get():

                gl.glLineWidth(1)
                gl.glColor3f(1.0, 0.0, 0.5)
                pangolin.DrawLines(maplines.array(), 2)

                if not self.q_active_lines.empty():
                    active_lines, line_colors = np.array(
                        self.q_active_lines.get())

                if active_lines is not None:
                    for act_line, color in zip(active_lines, line_colors):
                        c = (color[2] / 255, color[1] / 255, color[0] / 255)
                        act_line = act_line.reshape((1, 6))
                        gl.glLineWidth(5)
                        gl.glColor3f(*c)
                        pangolin.DrawLines(act_line, 2)

            if len(replays) > 0:
                n = 300
                gl.glPointSize(4)
                gl.glColor3f(1.0, 0.0, 0.0)
                gl.glBegin(gl.GL_POINTS)
                for point in replays[:n]:
                    gl.glVertex3f(*point)
                gl.glEnd()
                replays = replays[n:]

            # show cameras
            if not self.q_camera.empty():
                cams = self.q_camera.get()
                if len(cams) > 20:
                    cameras.clear()
                cameras.extend(cams)

            if m_show_keyframes.Get():
                gl.glLineWidth(1)
                gl.glColor3f(0.0, 0.0, 1.0)
                pangolin.DrawCameras(cameras.array(), camera_width)

            # show image
            if not self.q_image.empty():
                image = self.q_image.get()
                if image.ndim == 3:
                    image = image[::-1, :, ::-1]
                else:
                    image = np.repeat(image[::-1, :, np.newaxis], 3, axis=2)
                image = cv2.resize(image, (width, height))
            if m_show_image.Get():
                texture.Upload(image, gl.GL_RGB, gl.GL_UNSIGNED_BYTE)
                dimg.Activate()
                gl.glColor3f(1.0, 1.0, 1.0)
                texture.RenderToViewport()

            if pangolin.Pushed(m_replay):
                replays = mappoints.array()

            if pangolin.Pushed(m_reset):
                m_show_graph.SetVal(True)
                m_show_keyframes.SetVal(True)
                m_show_points.SetVal(True)
                m_show_image.SetVal(True)
                m_follow_camera.SetVal(True)
                follow_camera = True

            if pangolin.Pushed(m_refresh):
                self.q_refresh.put(True)

            pangolin.FinishFrame()
Example #4
0
    def view(self):
        pangolin.CreateWindowAndBind('Viewer', 1024, 768)

        gl.glEnable(gl.GL_DEPTH_TEST)
        gl.glEnable(gl.GL_BLEND)
        gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA)

        panel = pangolin.CreatePanel('menu')
        panel.SetBounds(0.5, 1.0, 0.0, 175 / 1024.)

        # checkbox
        if self.view_point_cloud:
            m_show_points = pangolin.VarBool('menu.Show Points', True, True)
        m_show_keyframes = pangolin.VarBool('menu.Show KeyFrames', True, True)
        m_show_graph = pangolin.VarBool('menu.Show Graph', True, True)
        m_show_image = pangolin.VarBool('menu.Show Image', True, True)

        # button
        m_next_frame = pangolin.VarBool('menu.Next', False, False)

        if self.config is None:
            viewpoint_x = 0
            viewpoint_y = -500  # -10
            viewpoint_z = -100  # -0.1
            viewpoint_f = 2000
            camera_width = 1.
            width, height = 350, 250
        else:
            viewpoint_x = self.config.view_viewpoint_x
            viewpoint_y = self.config.view_viewpoint_y
            viewpoint_z = self.config.view_viewpoint_z
            viewpoint_f = self.config.view_viewpoint_f
            camera_width = self.config.view_camera_width
            width = self.config.view_image_width * 2
            height = self.config.view_image_height

        proj = pangolin.ProjectionMatrix(1024, 768, viewpoint_f, viewpoint_f,
                                         512, 389, 0.1, 5000)
        look_view = pangolin.ModelViewLookAt(viewpoint_x, viewpoint_y,
                                             viewpoint_z, 0, 0, 0, 0, -1, 0)

        # Camera Render Object (for view / scene browsing)
        scam = pangolin.OpenGlRenderState(proj, look_view)

        # Add named OpenGL viewport to window and provide 3D Handler
        dcam = pangolin.CreateDisplay()
        dcam.SetBounds(0.0, 1.0, 175 / 1024., 1.0, -1024 / 768.)
        dcam.SetHandler(pangolin.Handler3D(scam))

        # Dilay image
        dimg = pangolin.Display('image')
        dimg.SetBounds(0, height / 768., 0.0, width / 1024., 1024 / 768.)
        dimg.SetLock(pangolin.Lock.LockLeft, pangolin.Lock.LockTop)

        texture = pangolin.GlTexture(width, height, gl.GL_RGB, False, 0,
                                     gl.GL_RGB, gl.GL_UNSIGNED_BYTE)
        image = np.ones((height, width, 3), 'uint8')

        pose = pangolin.OpenGlMatrix()  # identity matrix

        active = []
        graph = []
        loops = []
        loops_local = []
        mappoints = DynamicArray(shape=(3, ))
        colors = DynamicArray(shape=(3, ))
        cameras = DynamicArray(shape=(4, 4))

        while not pangolin.ShouldQuit():

            if not self.q_pose.empty():
                pose.m = self.q_pose.get()

            gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT)
            gl.glClearColor(1.0, 1.0, 1.0, 1.0)
            dcam.Activate(scam)

            # Show graph
            if not self.q_graph.empty():
                graph = self.q_graph.get()
                loops = np.array([_[0] for _ in graph if _[1] == 2])
                loops_local = np.array([_[0] for _ in graph if _[1] == 1])
                graph = np.array([_[0] for _ in graph if _[1] == 0])

            if m_show_graph.Get():
                if len(graph) > 0:
                    gl.glLineWidth(1)
                    gl.glColor3f(0.0, 1.0, 0.0)
                    pangolin.DrawLines(graph, 3)

                if len(loops) > 0:
                    gl.glLineWidth(2)
                    gl.glColor3f(1.0, 0.0, 1.0)
                    pangolin.DrawLines(loops, 4)

                if len(loops_local) > 0:
                    gl.glLineWidth(2)
                    gl.glColor3f(1.0, 1.0, 0.0)
                    pangolin.DrawLines(loops_local, 4)

            if self.view_point_cloud:
                # Show mappoints
                if not self.q_points.empty():
                    pts, code = self.q_points.get()
                    cls, code = self.q_colors.get()
                    if code == 1:  # append new points
                        mappoints.extend(pts)
                        colors.extend(cls)
                    elif code == 0:  # refresh all points
                        mappoints.clear()
                        mappoints.extend(pts)
                        colors.clear()
                        colors.extend(cls)

                if m_show_points.Get():
                    gl.glPointSize(2)
                    # easily draw millions of points
                    pangolin.DrawPoints(mappoints.array(), colors.array())

                    if not self.q_active.empty():
                        active = self.q_active.get()

                    gl.glPointSize(3)
                    gl.glBegin(gl.GL_POINTS)
                    gl.glColor3f(1.0, 0.0, 0.0)
                    for point in active:
                        gl.glVertex3f(*point)
                    gl.glEnd()

            # Show cameras
            if not self.q_camera.empty():
                cams = self.q_camera.get()
                if len(cams) > 20:
                    cameras.clear()
                cameras.extend(cams)

            if m_show_keyframes.Get():

                if cameras.array().shape[0] > 0:

                    gl.glLineWidth(1)
                    gl.glColor3f(0.0, 0.0, 1.0)
                    pangolin.DrawCameras(cameras.array()[:-1], camera_width)

                    gl.glLineWidth(1)
                    gl.glColor3f(1.0, 0.0, 0.0)
                    pangolin.DrawCameras(
                        np.expand_dims(cameras.array()[-1], axis=0),
                        camera_width)

            # Show image
            if not self.q_image.empty():
                image = self.q_image.get()
                if image.ndim == 3:
                    image = image[::-1, :, ::-1]
                else:
                    image = np.repeat(image[::-1, :, np.newaxis], 3, axis=2)
                image = cv2.resize(image, (width, height))

            if m_show_image.Get():
                texture.Upload(image, gl.GL_RGB, gl.GL_UNSIGNED_BYTE)
                dimg.Activate()
                gl.glColor3f(1.0, 1.0, 1.0)
                texture.RenderToViewport()

            if pangolin.Pushed(m_next_frame):
                self.q_next.put(True)

            pangolin.FinishFrame()