You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Magpylib实现三维空间内载流导线的旋转操作

问题原因

Magpylib中Line类的vertices属性存储的是局部坐标系下的顶点坐标,默认调用rotate()方法修改的是对象的全局姿态偏移,不会修改局部坐标系下的vertices值,因此你打印vertices始终显示初始值。

解决方法

方案1:用apply_rotation()直接修改局部顶点

该方法会直接把旋转矩阵作用到局部vertices上,符合你的使用需求,代码示例如下:

import magpylib as mag3
from scipy.spatial.transform import Rotation as R

# 创建初始导线
wire = mag3.current.Line(current=10, vertices=[(0,0,0), (1,0,0)], position=(0,0,0))
print(wire.vertices) # 输出 [(0. 0. 0.) (1. 0. 0.)]

# 直接对局部顶点应用旋转
wire.apply_rotation(R.from_euler('z', 90, degrees=True), anchor=(0,0,0))
print(wire.vertices) # 输出 [(0. 0. 0.) (0. 1. 0.)]

方案2:批量生成所有需要的导线

如果要一次性生成4个方向的导线,可以通过循环旋转角度实现:

import magpylib as mag3
from scipy.spatial.transform import Rotation as R

wires = []
base_vertices = [(0,0,0), (1,0,0)]
# 依次传入0°、90°、180°、270°四个旋转角度
for angle in [0, 90, 180, 270]:
    rot = R.from_euler('z', angle, degrees=True)
    # 对原始顶点应用旋转
    rotated_vertices = rot.apply(base_vertices)
    wire = mag3.current.Line(current=10, vertices=rotated_vertices)
    wires.append(wire)

# 打印所有导线顶点验证结果
for w in wires:
    print(w.vertices)

运行后将依次输出四个方向的顶点:

[(0. 0. 0.) (1. 0. 0.)]
[(0. 0. 0.) (0. 1. 0.)]
[(0. 0. 0.) (-1. 0. 0.)]
[(0. 0. 0.) (0. -1. 0.)]

内容的提问来源于stack exchange,提问作者Yoav Tarazy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.02 03:48:03