Skip to content Skip to sidebar Skip to footer

Get View Direction Relative To Scene In Vispy?

What is the cleanest way to get the view direction relative to your scene in vispy? view.scene.transform contains a whole chain of transforms: In [88]: view.scene.transform Out[88]

Solution 1:

Vispy transformations have a map and imap function you can use to map coordinates between scene and screen coordinates in either direction. I used them on points and threw in a lot of assertions to be safe; there are probably simpler implementations. I tested this for orthographic projection. I ~think it will work for perspective projections too as long as the center of projection is in the middle of the screen.

def get_view_direction_in_scene_coordinates(view):
    import numpy
    tform=view.scene.transform
    w,h = view.canvas.size
    screen_center = numpy.array([w/2,h/2,0,1]) # in homogeneous screen coordinates
    d1 = numpy.array([0,0,1,0]) # in homogeneous screen coordinates
    point_in_front_of_screen_center = screen_center + d1 # in homogeneous screen coordinates
    p1 = tform.imap(point_in_front_of_screen_center) # in homogeneous scene coordinates
    p0 = tform.imap(screen_center) # in homogeneous screen coordinates
    assert(abs(p1[3]-1.0) < 1e-5) # normalization necessary before subtraction
    assert(abs(p0[3]-1.0) < 1e-5)
    d2 = p1 - p0 # in homogeneous screen coordinates
    assert(abs(d2[3])< 1e-5)
    d3 = d2[0:3] # in 3D screen coordinates
    d4 = d3 / numpy.linalg.norm(d3)
    return d4

Post a Comment for "Get View Direction Relative To Scene In Vispy?"