pythonmayavimayavi.mlab

How to get data/coordinates from Mayavi 3D plot by mouseclick on any point in the plot


I have used a Digital elevation model, to create a 3D model of a terrain using Mayavi mlab. My next task, is to be able to get coordinates of any point that I click on the 3D model. Once I get the coordinates, I will map them to the image coordinates and get the required data. But currently, I'm unsure of how to click and get coordinates of a point in the first place. I have done this on 2D graphs/images in matplotlib. But I'm new to Mayavi. Please help.


Solution

  • Attach mouse_picker to your surface, and use picker_function function to get point coordinates.

        my3DSurface= mlab.figure(size=(width,height), bgcolor=(0.45,0.45,0.45))
        my3DSurface.on_mouse_pick(picker_function)
    
        def picker_function(picker_obj):
            global x_pt, y_pt, maxx, miny,minx, maxy
            point2d = picker_obj.point_id
            if(point2d==-1):
                point2d=0
            else:
    
                demX_pt = np.floor((maxx - minx) / cell_size_x) +1
                demY_pt = np.floor((maxy - miny) / cell_size_y)
    
                md = point2d%demY_pt
                x_pt = md
    
                md = point2d-md
                y_pt = md/demY_pt
    
            return point2d
    

    Where: minx = Minimum Longitude value in DEM

    maxx = Maximum Longitude value in DEM

    miny = Minimum Latitude value in DEM

    maxy = Maximum Latitude value in DEM

    cell_size_x = DEM resolution in direction of X axis

    cell_size_y = DEM resolution in direction of Y axis