-
Hi, first of all thank you for this great library :-) I am currently trying to animate reference frames in which forces are defined. I want to draw the axes of the frame, as well a vector representing the force. Frame position and orientation, as well as the (x,y,z) coordinates of the force vector resolved in the reference frame are given by some mapping defined as a function of time (dummy example case hard-coded in the method In order to be able to plot different objects generically, I split the problem into several classes: a class Now, to change the orientation/position of the Vedo objects, the import vedo as vd
import numpy as np
def transform(t):
"""Dummy transformation"""
# Homogeneous transformation matrix: frame of reference
H1 = np.array([[np.cos(t),-np.sin(t),0,0],
[np.sin(t),np.cos(t),0,0],
[0, 0, 1,0],
[0, 0, 0,1]])
H2 = np.array([[np.cos(t),0,np.sin(t),0],
[0, 1,0, t],
[-np.sin(t),0,np.cos(t),0],
[0, 0,0, 0]])
H = H1.dot(H2)
# Position within the frame
pos = (1 + 0.1*np.sin(t),0,0.1*np.sin(t))
return H,pos
# -----------------------------------------------------------------------------
# Concrete implementations of abstract classes representing how various
# geometrical shapes are modified when t varies
class ArrowActor():
def __init__(self):
self.vedo_instance = vd.Arrow((0,0,0),(1,0,0),c='red')
def apply_coordinates_transform(self,t):
H,m = transform(t)
# Change Arrow's frame
self.vedo_instance.apply_transform(H)
# How to change the properties of the arrow so that in the new frame,
# its tip would be (m[0],m[1],m[2]) ?
# TODO: ...
class BoxActor():
def __init__(self):
self.vedo_instance = vd.Box()
def apply_coordinates_transform(self,t):
H,_ = transform(t)
self.vedo_instance.apply_transform(H)
# -----------------------------------------------------------------------------
# Main plotting class
class Plot():
def __init__(self):
self._actors = []
self._plot = vd.Plotter(axes=True)
def add_actor(self,actor):
self._actors.append(actor)
self._plot += actor.vedo_instance
def add_sliders(self):
# one slider for all
def slider(widget, event):
t = widget.GetRepresentation().GetValue()
for actor in self._actors:
actor.apply_coordinates_transform(t)
self._plot.render()
self._plot.add_slider(
sliderfunc = slider,
xmin = -1,
xmax = 1,
value = 0,
showValue = True
)
def show(self):
self._plot.show()
# -----------------------------------------------------------------------------
# Test
if __name__ == "__main__":
a1 = ArrowActor()
b1 = BoxActor()
plot = Plot()
plot.add_actor(a1)
plot.add_actor(b1)
plot.add_sliders()
plot.show() I hope that this simplified example is clear. Thank you for any help ! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Well, I eventually found a satisfying solution, if not elegant ; just call again class ArrowActor():
def __init__(self):
self.vedo_instance = vd.Arrow((0,0,0),(1,0,0),c='red')
def apply_coordinates_transform(self,t):
H,m = transform(t)
# Change Arrow's frame
self.vedo_instance.apply_transform(H)
# Change the properties of the arrow
self.vedo_instance.__init__((0,0,0), (m[0],m[1],m[2])) |
Beta Was this translation helpful? Give feedback.
Well, I eventually found a satisfying solution, if not elegant ; just call again
__init__
: