获取相对于vispy中场景的视图方向? [英] Get view direction relative to scene in vispy?

查看:125
本文介绍了获取相对于vispy中场景的视图方向?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在vispy中获取相对于场景的视图方向的最干净方法是什么?

What is the cleanest way to get the view direction relative to your scene in vispy?

view.scene.transform包含整个转换链:

In [88]: view.scene.transform
Out[88]: 
<ChainTransform [<STTransform scale=[ 960. -540.    1.    1.] translate=[ 960.  540.    0.    0.] at 0x139757309901840>,
                 MatrixTransform(matrix=[[26.44507, 0.0, 0.0, 0.0],
                        [0.0, 47.013458, 0.0, 0.0],
                        [0.0, 0.0, -1e-06, 0.0],
                        [-0.0, -0.0, -0.0, 1.0]] at 0x7f1bc8d526d0),
                 <Inverse of '<ChainTransform [MatrixTransform(matrix=[[0.64390097845776273, -0.18562251042644023, -0.74225050593726238, 0.0],\n                        [0.74851597030808681, 0.35377196489238, 0.56086472437650059, 0.0],\n                        [0.15847830177938896, -0.91672770247177038, 0.36673552784799862, 0.0],\n                        [0.002241050448888897, 0.013296952664039196, 0.015024409939918581, 1.0]] at 0x7f1bc8c81710)] at 0x7f1bc8cb7e90>'>] at 0x7f1bc8e75490>

我可以写一些东西来解析各种类型的转换列表并进行组合,然后从组合的转换中提取视图方向,但是我怀疑自己在上游游泳.

I could write something to parse lists of transforms of varous types and compose them, and extract the view direction from the composed transform, but I suspect I'm swimming upstream.

推荐答案

Vispy转换具有mapimap函数,可用于在任一方向上在场景和屏幕坐标之间映射坐标.为了保证安全,我在观点上使用了它们,并提出了很多主张.可能有更简单的实现.我对此进行了正射投影测试.我〜认为只要透视中心在屏幕中间,它就可以用于透视投影.

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

这篇关于获取相对于vispy中场景的视图方向?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆