How to rotate current carrying wires in 3d space using magpylib?

Viewed 94

Im using python's Magpylib library for magnetic field computation.

i've created a current conducting wire with vertices: [(0,0,0)(1,0,0)]

Now im trying to rotate it in such a way that its new vertices will be: [(0,0,0),(0,1,0)],[(0,0,0)(-1,0,0)],[(0,0,0)(0,-1,0)]

This is where i currently stand:

wire=mag3.current.Line(current=10,vertices=[(0,0,0),(1,0,0)],position=(0,0,0))
print(wire.vertices)
wire=wire.rotate(R.from_euler('z',90, degrees=True),anchor=(0,0,0),increment=True)
print(wire.vertices)

i'm getting:

[(0,0,0)(1,0,0)],[(0,0,0)(1,0,0)]

while i'm looking to get: [(0,0,0)(1,0,0)],[(0,0,0)(0,1,0)]

1 Answers

How does it work?

It did not supposed to change, even tho it's a little unintuitive, but works like that: move and rotate method calls do not affect the vertices, but applies for position and orientation

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)])
print(wire.vertices)
# [[0 0 0]
#  [1 0 0]]
print(wire.position)
# [0. 0. 0.]
print(wire.orientation.as_euler('xyz', degrees=True))
# [0. 0. 0.]

wire.move((1, 2, 3))
print(wire.vertices)
# [[0 0 0]
#  [1 0 0]]
print(wire.position)
# [1. 2. 3.]
print(wire.orientation.as_euler('xyz', degrees=True))
# [0. 0. 0.]

wire.rotate(R.from_euler('z', 45, degrees=True))
print(wire.vertices)
# [[0 0 0]
#  [1 0 0]]
print(wire.position)
# [1. 2. 3.]
print(wire.orientation.as_euler('xyz', degrees=True))
# [ 0.  0. 45.]

All of that is for objects that do not have any vertices like Sphere, you see, there's no way to define sphere rotation other than simply store it as a separate value. (tho why we would want to store rotation of the sphere is another question :] )

Anyway, how can we achieve this?

But if you wanna see the effect of the rotation on the vertices you can do wire.orientation.apply(wire.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)])

wire.rotate(R.from_euler('z', 45, degrees=True))
print(wire.vertices)
# [[0 0 0]
#  [1 0 0]]
print(wire.orientation.apply(wire.vertices))
# [[0.         0.         0.        ]
#  [0.70710678 0.70710678 0.        ]]
print(wire.vertices)
# [[0 0 0]
#  [1 0 0]]

and if you wanna get the shift then just use wire.vertices + wire.position

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)])

wire.move((1, 2, 3))
wire.move((4, 3, 2))
print(wire.vertices)
# [[0 0 0]
#  [1 0 0]]
print(wire.vertices + wire.position)
# [[5. 5. 5.]
#  [6. 5. 5.]]
print(wire.vertices)
# [[0 0 0]
#  [1 0 0]]

I don't know how this package works with simultaneous shift and rotation, and the question is not about it, so I hope that's enough for now.

Related