限制 Mayavi 鼠标拖动以绕其轴旋转地球 [英] Constrain Mayavi mouse drag to rotating Earth around its axis

查看:24
本文介绍了限制 Mayavi 鼠标拖动以绕其轴旋转地球的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 iPython Notebook,我已经能够用如下代码创建地球仪:

Using iPython Notebook, I have been able to bring up a globe of the Earth with code like:

from mayavi import mlab
from mayavi.sources.builtin_surface import BuiltinSurface

ocean_blue = (0.4, 0.5, 1.0)
r = 6371 # km

sphere = mlab.points3d(0, 0, 0, name='Globe',
  scale_mode='none', scale_factor=r * 2.0,
  color=ocean_blue, resolution=50)

sphere.actor.property.specular = 0.20
sphere.actor.property.specular_power = 10

continents_src = BuiltinSurface(source='earth', name='Continents')
continents_src.data_source.on_ratio = 1  # detail level
continents_src.data_source.radius = r
continents = mlab.pipeline.surface(continents_src, color=(0, 0, 0))

但是当我使用鼠标与生成的 3D 窗口进行交互时,很难保持其正面朝上,因为 UI 会将鼠标向左或向右拖动解释为尝试旋转场景(或相机?)围绕窗口的当前垂直轴,而不是通过地球本身的轴.

But when I interact with the resulting 3D window using the mouse, it is very difficult to keep it right-side-up, because the UI interprets a mouse-drag to either the left or the right as an attempt to rotate the scene (or camera?) around the current vertical axis of the window, instead of through the axis of the globe itself.

有什么方法可以约束用户交互代码,以便鼠标左键或右键拖动使地球绕其轴旋转,无论轴是否指向垂直向上和向下,无论是通过设置一些Mayavi 参数,或者通过获取一些 Python 代码注册为用于鼠标拖动的 UI 事件处理程序?

Is there any way to constrain the user interaction code so that a left or right mouse-drag spins the globe around its axis, regardless of whether the axis is pointing straight-up-and-down or not, either by setting some Mayavi parameters, or by getting some Python code registered as the UI event handler for mouse dragging?

推荐答案

九个月后,我终于找到了一条线索,让我找到了解决方案!另一个堆栈溢出问答,是在 Mayavi 中可以禁止使用鼠标旋转轴吗?,让我想到了设置一个非默认的交互器"来控制鼠标点击和拖动的含义.

Nine months later, I have finally run across a clue that lead me to a solution! Another Stack Overflow question and answer, Is it possible to prohibit axes rotation with the mouse in Mayavi?, exposed me to the idea of setting up a non-default "interactor" to control what mouse clicks and drags mean.

而且确实有一个交互器可以让 Z 轴保持向上",无论用户做什么:

And there is indeed an interactor that keeps the Z-axis "up" no matter what the user does:

http://www.vtk.org/doc/nightly/html/classvtkInteractorStyleTerrain.html

要在 Mayavi 主窗口中激活此交互器,只需将以下三行添加到您的代码中(例如,您可以将它们添加到我在问题中的地球构建代码中),地球将始终保持直立!

To activate this interactor in the Mayavi main window, simply add the following three lines to your code (for example, you can add them to my globe-building code in the question), and the globe will always remain upright!

from tvtk.api import tvtk
fig = mlab.gcf()
fig.scene.interactor.interactor_style = tvtk.InteractorStyleTerrain()

这篇关于限制 Mayavi 鼠标拖动以绕其轴旋转地球的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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