-
Notifications
You must be signed in to change notification settings - Fork 0
/
Animation.py
172 lines (120 loc) · 4.69 KB
/
Animation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# -*- coding: utf-8 -*-
"""Untitled2.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/13IMUYvp2hUsJy6SKaiXsRsyU7juamBNR
"""
!pip install numpy-stl
#Pikachu entrando na pokebola
#Igor Jensen e Sebastiao Gabriel
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import animation, rc
from stl import mesh
from math import pi,cos,sin
#Definindo as funções
def escala (sx,sy,sz):
scalling_matrix = np.array([[sx,0,0,0],[0,sy,0,0],[0,0,sz,0],[0,0,0,1]])
return scalling_matrix
def x_rotation(angle):
rotation_matrix=np.array([[1,0,0,0],[0, cos(angle),-sin(angle),0],[0, sin(angle),cos(angle),0],[0,0,0,1]])
return rotation_matrix
def y_rotation(angle):
rotation_matrix=np.array([[cos(angle),0, sin(angle),0],[0,1,0,0],[-sin(angle),0,cos(angle),0],[0,0,0,1]])
return rotation_matrix
def z_rotation(angle):
rotation_matrix=np.array([[cos(angle),-sin(angle),0,0],[sin(angle),cos(angle),0,0],[0,0,1,0],[0,0,0,1]])
return rotation_matrix
def translada(dx,dy,dz):
translation_matrix = np.array([[1,0,0,dx],[0,1,0,dy],[0,0,1,dz],[0,0,0,1]])
return translation_matrix
def set_axes_equal(ax):
x_limits = ax.get_xlim3d()
y_limits = ax.get_ylim3d()
z_limits = ax.get_zlim3d()
x_range = abs(x_limits[1] - x_limits[0])
x_middle = np.mean(x_limits)
y_range = abs(y_limits[1] - y_limits[0])
y_middle = np.mean(y_limits)
z_range = abs(z_limits[1] - z_limits[0])
z_middle = np.mean(z_limits)
plot_radius = max([x_range, y_range, z_range])
ax.set_xlim3d([x_middle - plot_radius, x_middle + plot_radius])
ax.set_ylim3d([y_middle - plot_radius, y_middle + plot_radius])
ax.set_zlim3d([z_middle - plot_radius, z_middle + plot_radius])
#Abrindo e configurando os .stl
mesh1 = mesh.Mesh.from_file('pokeball.stl')
mesh2 = mesh.Mesh.from_file('pikachu.STL')
x1 = 0.5*mesh1.x.flatten()
y1 = 0.5*mesh1.y.flatten()
z1 = 0.5*mesh1.z.flatten()
x2 = mesh2.x.flatten()
y2 = mesh2.y.flatten()
z2 = mesh2.z.flatten()
pokeball_vectors = mesh1.vectors
pikachu_vectors = mesh2.vectors
fig = plt.figure(figsize=(10,10))
ax0 = plt.axes(projection='3d')
plt.close()
ax0.set_xlim3d((-100, 100))
ax0.set_ylim3d((-200, 10))
ax0.set_zlim3d((0, 100))
# Listando os objetos que serão desenhados
obj3, = ax0.plot3D([], [], [],'yellow', lw=2)
obj2, = ax0.plot3D([], [], [],'r.', lw=2)
#Colocando os objetos em coordenadas homogeneas
pokeball = np.array([x1.T,y1.T,z1.T,np.ones(x1.size)])
pikachu = np.array([x2.T,y2.T,z2.T,np.ones(x2.size)])
pokeball = np.dot(translada(0,-180,10),pokeball)
#Função de inicialização:
def init():
return ()
# Funçãom de animação. Ela é chamada sequencialmente
def animate(i):
#Definindo a matriz de rotação R que roda o Pikachu em z
R1 = z_rotation(i)
#Definindo a matriz de rotação R que roda o Pikachu em y
R2 = y_rotation(i)
#Definindo a matriz que faz o Pikachu oscilar em x
T4 =translada(75*sin(i-100),0,0)
#Definindo a composição das duas matrizes
C = np.dot(T4,R1)
#Definindo a matriz que inicia a pokebola na segunda parte da animação
T2 = translada(10*sin(0),180, 10*cos(50))
#Definindo a matriz que move a pokebola em direção ao Pikachu
T3 = translada(10*sin(i-50),1.8*i*2, 10*cos(i))
#Primeira metade da animação
if i<=50:
#Movendo a pokebola em direção ao Pikachu
pokeball2 = np.dot(T3,pokeball)
#Fazendo a rotação R em z e a oscilação em x do Pikachu
pikachu2 = np.dot(C,pikachu)
obj3.set_data(pikachu2[0,:], pikachu2[1,:])
obj3.set_3d_properties(pikachu2[2,:])
obj2.set_data(pokeball2[0,:], pokeball2[1,:])
obj2.set_3d_properties(pokeball2[2,:])
#Segunda metade da animação
if i>50:
#Definindo a matriz de mudança de escala
E1 = escala(51**3/i**3,51**3/i**3,51**3/i**3)
#Iniciando a pokebola onde ela havia parado
pokeball2 = np.dot(T2,pokeball)
#Reduzindo a escala do Pikachu
pikachu2 = np.dot(E1,pikachu)
#Rotacionando o pikachu em y
pikachu2 = np.dot(R2,pikachu2)
#Movendo o pikachu em direção a pokebola
pikachu2 = np.dot(translada(0,0, (60*i/100)-(60*51/100)),pikachu2)
obj3.set_data(pikachu2[0,:], pikachu2[1,:])
obj3.set_3d_properties(pikachu2[2,:])
obj2.set_data(pokeball2[0,:], pokeball2[1,:])
obj2.set_3d_properties(pokeball2[2,:])
return (obj2, obj3)
#Alterando a visualização dos eixos
ax0.view_init(elev=45,azim=-45)
# Realizando a animaçãp
anim = animation.FuncAnimation(fig, animate, init_func=init,
frames=100, interval=100, blit=True)
rc('animation', html='jshtml')
anim