gpt4 book ai didi

python - 如何在 Vispy 3d 图中获取点的位置?

转载 作者:行者123 更新时间:2023-12-01 13:18:59 28 4
gpt4 key购买 nike

我是 VisPy 的初学者。

我想做的就是:

单击该点,该点将改变颜色并打印该点的位置 (x,y,z)。

但我找不到如何做到这一点。

这是我的代码。

import numpy as np
import sys
from vispy import app, visuals, scene

class Canvas(scene.SceneCanvas):
""" A simple test canvas for testing the EditLineVisual """

def __init__(self):
scene.SceneCanvas.__init__(self, keys='interactive',
size=(800, 800), show=True)

# # Create some initial points
self.unfreeze()
# Add a ViewBox to let the user zoom/rotate
self.view = self.central_widget.add_view()
self.view.camera = 'turntable'
self.view.camera.fov = 30
self.show()
self.selected_point = None
scene.visuals.GridLines(parent=self.view.scene)
self.freeze()

def on_mouse_press(self, event):
print(event.pos) # How to convert this pos to canvas position??


Scatter3D = scene.visuals.create_visual_node(visuals.MarkersVisual)
canvas = Canvas()

p1 = Scatter3D(parent=canvas.view.scene)
p1.set_gl_state('translucent', blend=True, depth_test=True)

# fake data
x = np.random.rand(100) * 10
y = np.random.rand(100) * 10
z = np.random.rand(100) * 10

# Draw it
point_list = [x, y, z]
point = np.array(point_list).transpose()
p1.set_data(point, symbol='o', size=6, edge_width=0.5, edge_color='blue')

if __name__ == "__main__":

if sys.flags.interactive != 1:
app.run()

最佳答案

我只是遇到了同样的问题。我通过使用以下代码解决了它,希望这会有所帮助。

def on_mouse_press(self, event):


#1=left, 2=right , 3=middle button
if event.button == 1:
p2 = event.pos
norm = np.mean(self.view.camera._viewbox.size)

if self.view.camera._event_value is None or len(self.view.camera._event_value) == 2:
ev_val = self.view.camera.center
else:
ev_val = self.view.camera._event_value
dist = p2 / norm * self.view.camera._scale_factor

dist[1] *= -1
# Black magic part 1: turn 2D into 3D translations
dx, dy, dz = self.view.camera._dist_to_trans(dist)
# Black magic part 2: take up-vector and flipping into account
ff = self.view.camera._flip_factors
up, forward, right = self.view.camera._get_dim_vectors()
dx, dy, dz = right * dx + forward * dy + up * dz
dx, dy, dz = ff[0] * dx, ff[1] * dy, dz * ff[2]
c = ev_val
#shift by scale_factor half
sc_half = self.view.camera._scale_factor/2
point = c[0] + dx-sc_half, c[1] + dy-sc_half, c[2] + dz+sc_half
print("final point:", point[0], point[1], point[2])

关于python - 如何在 Vispy 3d 图中获取点的位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51529879/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com