- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我不确定如何从屏幕坐标转换为世界坐标。我正在使用 VisPy,我想在 3D 中实现光线追踪和拾取功能。
我根据立方体示例准备了一些代码。下面的代码通过更改 z 值并打印 3D 坐标(在 ''on_mouse_press '' 方法中)通过屏幕发送粗射线。但是结果不正确。如果我单击立方体右上角的某个位置,应该会打印出射线 (3,3,3),但事实并非如此。有人可以帮我解决这个问题吗?
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# vispy: gallery 50
"""
This example shows how to display 3D objects.
You should see a colored outlined spinning cube.
"""
import numpy as np
from vispy import app, gloo
from vispy.util.transforms import perspective, translate, rotate
vert = """
// Uniforms
// ------------------------------------
uniform mat4 u_model;
uniform mat4 u_view;
uniform mat4 u_projection;
uniform vec4 u_color;
// Attributes
// ------------------------------------
attribute vec3 a_position;
attribute vec4 a_color;
attribute vec3 a_normal;
// Varying
// ------------------------------------
varying vec4 v_color;
void main()
{
v_color = a_color * u_color;
gl_Position = u_projection * u_view * u_model * vec4(a_position,1.0);
}
"""
frag = """
uniform mat4 u_model;
uniform mat4 u_view;
uniform mat4 u_normal;
uniform vec3 u_light_intensity;
uniform vec3 u_light_position;
varying vec3 v_position;
varying vec3 v_normal;
varying vec4 v_color;
void main()
{
gl_FragColor = v_color;
}
"""
# -----------------------------------------------------------------------------
def cube(num_of_cubes):
"""
Build vertices for a colored cube.
V is the vertices
I1 is the indices for a filled cube (use with GL_TRIANGLES)
I2 is the indices for an outline cube (use with GL_LINES)
"""
for i in range(0,num_of_cubes):
# Vertices positions
v = np.array([[1, 1, 1], [-1, 1, 1], [-1, -1, 1], [1, -1, 1],
[1, -1, -1], [1, 1, -1], [-1, 1, -1], [-1, -1, -1]],dtype=np.float32)
v[:,0]=v[:,0]+2.
v[:,1]=v[:,1]+2.
v[:,2]=v[:,2]+2.
# Face Normals
n =np.array([[0, 0, 1], [1, 0, 0], [0, 1, 0],
[-1, 0, 1], [0, -1, 0], [0, 0, -1]],dtype=np.float32)
# Vertice colors
c = np.array([[0, 0, 1, 1], [0, 0, 1, 1], [0, 0, 1, 1], [0, 0, 1, 1],
[0, 0, 1, 1], [0, 0, 1, 1], [0, 0, 1, 1], [0, 0, 1, 1]],dtype=np.float32)
V_aux = np.array([(v[0], n[0], c[0]), (v[1], n[0], c[1]),
(v[2], n[0], c[2]), (v[3], n[0], c[3]),
(v[0], n[1], c[0]), (v[3], n[1], c[3]),
(v[4], n[1], c[4]), (v[5], n[1], c[5]),
(v[0], n[2], c[0]), (v[5], n[2], c[5]),
(v[6], n[2], c[6]), (v[1], n[2], c[1]),
(v[1], n[3], c[1]), (v[6], n[3], c[6]),
(v[7], n[3], c[7]), (v[2], n[3], c[2]),
(v[7], n[4], c[7]), (v[4], n[4], c[4]),
(v[3], n[4], c[3]), (v[2], n[4], c[2]),
(v[4], n[5], c[4]), (v[7], n[5], c[7]),
(v[6], n[5], c[6]), (v[5], n[5], c[5])]
)
I1_aux = np.resize(np.array([0, 1, 2, 0, 2, 3], dtype=np.uint32), 6 * (2 * 3))
I1_aux += np.repeat(4 * np.arange(2 * 3, dtype=np.uint32), 6)
I2_aux = np.resize(
np.array([0, 1, 1, 2, 2, 3, 3, 0], dtype=np.uint32), 6 * (2 * 4))
I2_aux += np.repeat(4 * np.arange(6, dtype=np.uint32), 8)
if i==0:
V=V_aux
I1=I1_aux
I2=I2_aux
else:
V=np.vstack((V,V_aux))
I1=np.vstack((I1,I1_aux+i*24))
I2=np.vstack((I2,I2_aux+i*24))
return V, I1, I2
# -----------------------------------------------------------------------------
class Canvas(app.Canvas):
def __init__(self):
app.Canvas.__init__(self, keys='interactive', size=(800, 600))
num_of_cubes=1 #number of cubes to draw
self.V, self.filled, self.outline = cube(num_of_cubes)
self.store_pos=np.array((0,0)) #for mouse interaction
self.vert_data=np.vstack(self.V[:,0])
self.V_buf=np.vstack(self.V[:,0])
self.V_buf.dtype=[('a_position',np.float32,3)]
self.vert_buf=gloo.VertexBuffer(self.V_buf)
self.N_buf=np.vstack(self.V[:,1])
self.N_buf.dtype=[('a_normal',np.float32,3)]
self.norm_buf=gloo.VertexBuffer(self.N_buf)
self.C_buf=np.vstack(self.V[:,2])
self.C_buf.dtype=[('a_color',np.float32,4)]
self.colo_buf=gloo.VertexBuffer(self.C_buf)
self.filled_buf=gloo.IndexBuffer(self.filled.flatten())
self.outline_buf=gloo.IndexBuffer(self.outline.flatten())
self.program = gloo.Program(vert, frag)
self.translate = 1
#self.vert_buf=gloo.VertexBuffer(self.vertices.flatten())
self.program.bind(self.vert_buf)
self.program.bind(self.norm_buf)
self.program.bind(self.colo_buf)
self.view = translate((0, 0, -10))
self.model = np.eye(4, dtype=np.float32)
gloo.set_viewport(0, 0, self.physical_size[0], self.physical_size[1])
self.projection = perspective(45.0, self.size[0] /
float(self.size[1]), 2.0, 10.0)
self.program['u_projection'] = self.projection
self.program['u_model'] = self.model
self.program['u_view'] = self.view
self.theta = 0
self.phi = 0
gloo.set_clear_color('white')
gloo.set_state('opaque')
gloo.set_polygon_offset(1, 1)
self._timer = app.Timer('auto', connect=self.on_timer, start=True)
self.show()
self.t=0
# ---------------------------------
def on_timer(self, event):
self.update()
# ---------------------------------
def print_mouse_event(self, event, what):
modifiers = ', '.join([key.name for key in event.modifiers])
print('%s - pos: %r, button: %s, modifiers: %s, delta: %r' %
(what, event.pos, event.button, modifiers, event.delta))
def on_mouse_press(self, event):
self.print_mouse_event(event, 'Mouse press')
#convert to NDC
left=event.pos[0]*2/self.size[0]-1
bottom=(self.size[1]-event.pos[1])*2/self.size[1]-1
z_clip=np.linspace(-1.,1.,100)
for val in z_clip:
aux=np.dot(np.dot(np.linalg.inv(self.view),np.linalg.inv(self.projection)),np.array((left,bottom,val,1.)))
pos3d=aux/aux[3]
print(pos3d)
def on_mouse_wheel(self, event):
self.translate -= event.delta[1]
self.translate = max(-1, self.translate)
self.view[3,2]=-self.translate
self.program['u_view'] = self.view
self.update()
def on_draw(self, event):
gloo.clear()
# Filled cube
gloo.set_state(blend=False, depth_test=True, polygon_offset_fill=True)
self.program['u_color'] = 1, 0, 1, 1
self.program.draw('triangles', self.filled_buf)
# Outline
gloo.set_state(polygon_offset_fill=False, blend=True, depth_mask=False)
gloo.set_depth_mask(False)
self.program['u_color'] = 0, 0, 0, 1
self.program.draw('lines', self.outline_buf)
gloo.set_depth_mask(True)
# -----------------------------------------------------------------------------
if __name__ == '__main__':
c = Canvas()
app.run()
最佳答案
屏幕上的一个点击点映射到场景中的一条线。
view.scene.transform
中的对象表示场景和屏幕坐标之间的映射。 .map(points)
将点从场景转换到屏幕。 .imap(points)
从屏幕坐标映射回世界坐标。
获取你的屏幕点对应的线。您可以在屏幕上映射一个点,并在 z 中从屏幕偏移另一个点:
def get_view_axis_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)
return p0[0:3],p1[0:3] # 2 point representation of view axis in 3d scene coordinates
我把它改得更接近你想要的;您需要将 screen_center 替换为单击的点。请注意,我这样做是为了正交投影;认为它也适用于透视,但尚未测试。
关于python - 如何从 Vispy 中的屏幕坐标获取世界坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33942728/
我正在处理一组标记为 160 个组的 173k 点。我想通过合并最接近的(到 9 或 10 个组)来减少组/集群的数量。我搜索过 sklearn 或类似的库,但没有成功。 我猜它只是通过 knn 聚类
我有一个扁平数字列表,这些数字逻辑上以 3 为一组,其中每个三元组是 (number, __ignored, flag[0 or 1]),例如: [7,56,1, 8,0,0, 2,0,0, 6,1,
我正在使用 pipenv 来管理我的包。我想编写一个 python 脚本来调用另一个使用不同虚拟环境(VE)的 python 脚本。 如何运行使用 VE1 的 python 脚本 1 并调用另一个 p
假设我有一个文件 script.py 位于 path = "foo/bar/script.py"。我正在寻找一种在 Python 中通过函数 execute_script() 从我的主要 Python
这听起来像是谜语或笑话,但实际上我还没有找到这个问题的答案。 问题到底是什么? 我想运行 2 个脚本。在第一个脚本中,我调用另一个脚本,但我希望它们继续并行,而不是在两个单独的线程中。主要是我不希望第
我有一个带有 python 2.5.5 的软件。我想发送一个命令,该命令将在 python 2.7.5 中启动一个脚本,然后继续执行该脚本。 我试过用 #!python2.7.5 和http://re
我在 python 命令行(使用 python 2.7)中,并尝试运行 Python 脚本。我的操作系统是 Windows 7。我已将我的目录设置为包含我所有脚本的文件夹,使用: os.chdir("
剧透:部分解决(见最后)。 以下是使用 Python 嵌入的代码示例: #include int main(int argc, char** argv) { Py_SetPythonHome
假设我有以下列表,对应于及时的股票价格: prices = [1, 3, 7, 10, 9, 8, 5, 3, 6, 8, 12, 9, 6, 10, 13, 8, 4, 11] 我想确定以下总体上最
所以我试图在选择某个单选按钮时更改此框架的背景。 我的框架位于一个类中,并且单选按钮的功能位于该类之外。 (这样我就可以在所有其他框架上调用它们。) 问题是每当我选择单选按钮时都会出现以下错误: co
我正在尝试将字符串与 python 中的正则表达式进行比较,如下所示, #!/usr/bin/env python3 import re str1 = "Expecting property name
考虑以下原型(prototype) Boost.Python 模块,该模块从单独的 C++ 头文件中引入类“D”。 /* file: a/b.cpp */ BOOST_PYTHON_MODULE(c)
如何编写一个程序来“识别函数调用的行号?” python 检查模块提供了定位行号的选项,但是, def di(): return inspect.currentframe().f_back.f_l
我已经使用 macports 安装了 Python 2.7,并且由于我的 $PATH 变量,这就是我输入 $ python 时得到的变量。然而,virtualenv 默认使用 Python 2.6,除
我只想问如何加快 python 上的 re.search 速度。 我有一个很长的字符串行,长度为 176861(即带有一些符号的字母数字字符),我使用此函数测试了该行以进行研究: def getExe
list1= [u'%app%%General%%Council%', u'%people%', u'%people%%Regional%%Council%%Mandate%', u'%ppp%%Ge
这个问题在这里已经有了答案: Is it Pythonic to use list comprehensions for just side effects? (7 个答案) 关闭 4 个月前。 告
我想用 Python 将两个列表组合成一个列表,方法如下: a = [1,1,1,2,2,2,3,3,3,3] b= ["Sun", "is", "bright", "June","and" ,"Ju
我正在运行带有最新 Boost 发行版 (1.55.0) 的 Mac OS X 10.8.4 (Darwin 12.4.0)。我正在按照说明 here构建包含在我的发行版中的教程 Boost-Pyth
学习 Python,我正在尝试制作一个没有任何第 3 方库的网络抓取工具,这样过程对我来说并没有简化,而且我知道我在做什么。我浏览了一些在线资源,但所有这些都让我对某些事情感到困惑。 html 看起来
我是一名优秀的程序员,十分优秀!