simplerobotics 1.0.5.2__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- SimpleRobotics/RobotRR.py +156 -0
- SimpleRobotics/__init__.py +37 -0
- SimpleRobotics/animation.py +427 -0
- SimpleRobotics/formatting.py +65 -0
- SimpleRobotics/plotting.py +1272 -0
- SimpleRobotics/transforms.py +865 -0
- simplerobotics-1.0.5.2.dist-info/METADATA +173 -0
- simplerobotics-1.0.5.2.dist-info/RECORD +10 -0
- simplerobotics-1.0.5.2.dist-info/WHEEL +5 -0
- simplerobotics-1.0.5.2.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,1272 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
from typing import Any
|
|
3
|
+
from pathlib import Path
|
|
4
|
+
from types import SimpleNamespace
|
|
5
|
+
import numpy as np
|
|
6
|
+
from math import pi
|
|
7
|
+
from .transforms import rotx, roty, rotz, trans
|
|
8
|
+
import trimesh
|
|
9
|
+
from mpl_toolkits.mplot3d.art3d import Poly3DCollection
|
|
10
|
+
from matplotlib.colors import LightSource
|
|
11
|
+
import matplotlib.pyplot as plt
|
|
12
|
+
|
|
13
|
+
def _obtener_axes_3d(ax: Any = None) -> Any:
|
|
14
|
+
"""Obtiene un eje 3D existente sin crear figuras ni ejes nuevos."""
|
|
15
|
+
|
|
16
|
+
if ax is not None:
|
|
17
|
+
if not hasattr(ax, "get_zlim"):
|
|
18
|
+
raise TypeError("ax debe ser un eje tridimensional.")
|
|
19
|
+
return ax
|
|
20
|
+
|
|
21
|
+
import matplotlib.pyplot as plt
|
|
22
|
+
|
|
23
|
+
if not plt.get_fignums():
|
|
24
|
+
raise RuntimeError("No hay una figura activa. Crea un eje 3D o proporciona el argumento ax")
|
|
25
|
+
|
|
26
|
+
fig = plt.gcf()
|
|
27
|
+
|
|
28
|
+
ax = next(
|
|
29
|
+
(
|
|
30
|
+
eje for eje in reversed(fig.axes)
|
|
31
|
+
if hasattr(eje, "get_zlim")
|
|
32
|
+
),
|
|
33
|
+
None,
|
|
34
|
+
)
|
|
35
|
+
|
|
36
|
+
if ax is None:
|
|
37
|
+
raise RuntimeError(
|
|
38
|
+
"Primero crea un eje 3D o proporciona el argumento ax."
|
|
39
|
+
)
|
|
40
|
+
|
|
41
|
+
return ax
|
|
42
|
+
|
|
43
|
+
def eslabon(po: Any, pf: Any, c: str= "k", lw: float = 2, *, ax: Any = None,) -> Any:
|
|
44
|
+
|
|
45
|
+
# Obtener un eje 3D si no se cuenta con uno
|
|
46
|
+
|
|
47
|
+
ax = _obtener_axes_3d(ax)
|
|
48
|
+
|
|
49
|
+
#Convertir los putnso a vectores numericos
|
|
50
|
+
|
|
51
|
+
po = np.asanyarray(po, dtype=float).reshape(-1)
|
|
52
|
+
pf = np.asanyarray(pf, dtype=float).reshape(-1)
|
|
53
|
+
|
|
54
|
+
if po.size < 3 or pf.size < 3:
|
|
55
|
+
raise ValueError("po y pf deben tener almenos 3 coordenadas.")
|
|
56
|
+
|
|
57
|
+
#Equivalente a plot3 de MATLAB
|
|
58
|
+
linea, = ax.plot([po[0], pf[0]],[po[1], pf[1]],[po[2], pf[2]],color=c,linewidth=lw,)
|
|
59
|
+
|
|
60
|
+
return linea
|
|
61
|
+
|
|
62
|
+
def cilindro(K: Any, alto: float, radio: float, dif: float, *, ax: Any = None, ) -> Any:
|
|
63
|
+
|
|
64
|
+
#Dibuja un cilindro transformado por la matriz homogenea k
|
|
65
|
+
|
|
66
|
+
import matplotlib.pyplot as plt
|
|
67
|
+
|
|
68
|
+
#Obtener el eje 3D actual o crear uno si no existe
|
|
69
|
+
|
|
70
|
+
ax = _obtener_axes_3d(ax)
|
|
71
|
+
|
|
72
|
+
#Convertit la trasnformacion a una matriz numerica
|
|
73
|
+
|
|
74
|
+
K = np.asanyarray(K, dtype=float)
|
|
75
|
+
|
|
76
|
+
if K.shape != (4, 4):
|
|
77
|
+
raise ValueError("K Debe ser una matriz de transformacion 4x4")
|
|
78
|
+
|
|
79
|
+
if alto < 0 or radio < 0:
|
|
80
|
+
raise ValueError("alto y/o radio no pueden ser negativos")
|
|
81
|
+
|
|
82
|
+
angulo = np.linspace(0, 2 * pi, 21)
|
|
83
|
+
|
|
84
|
+
Xc = radio * np.cos(angulo)
|
|
85
|
+
Yc = radio * np.sin(angulo)
|
|
86
|
+
|
|
87
|
+
#Equivalente a zc = (Zc * alto) - dif.
|
|
88
|
+
|
|
89
|
+
Zc1 = np.full_like(angulo, -dif)
|
|
90
|
+
Zc2 = np.full_like(angulo, alto - dif)
|
|
91
|
+
|
|
92
|
+
#Coordenadas homogeneas de la base y la cara superior
|
|
93
|
+
|
|
94
|
+
p1 = np.vstack((Xc, Yc, Zc1, np.ones_like(angulo)))
|
|
95
|
+
p2 = np.vstack((Xc, Yc, Zc2, np.ones_like(angulo)))
|
|
96
|
+
|
|
97
|
+
#Aplicar la transformacion K
|
|
98
|
+
|
|
99
|
+
p1K = K @ p1
|
|
100
|
+
p2K = K @ p2
|
|
101
|
+
|
|
102
|
+
#Reconstruit las matrices que recibe la superficie
|
|
103
|
+
|
|
104
|
+
X = np.vstack((p1K[0, :], p2K[0, :]))
|
|
105
|
+
Y = np.vstack((p1K[1, :], p2K[1, :]))
|
|
106
|
+
Z = np.vstack((p1K[2, :], p2K[2, :]))
|
|
107
|
+
|
|
108
|
+
#Color amarillo FANUC: [255, 255, 50] / 255.
|
|
109
|
+
color = (1.0, 1.0, 50 / 255)
|
|
110
|
+
|
|
111
|
+
superficie = ax.plot_surface(X, Y, Z, color=color, linewidth = 0.3, antialiased = True,)
|
|
112
|
+
|
|
113
|
+
return superficie
|
|
114
|
+
|
|
115
|
+
def tubo(K: Any, alto: float, radio: float, color: str, *, ax: Any = None,) -> Any:
|
|
116
|
+
|
|
117
|
+
#Obtener el eje 3D actual o crear uno si no existe
|
|
118
|
+
|
|
119
|
+
ax = _obtener_axes_3d(ax)
|
|
120
|
+
|
|
121
|
+
#Convertit la trasnformacion a una matriz numerica
|
|
122
|
+
|
|
123
|
+
K = np.asanyarray(K, dtype=float)
|
|
124
|
+
|
|
125
|
+
if K.shape != (4, 4):
|
|
126
|
+
raise ValueError("K Debe ser una matriz de transformacion 4x4")
|
|
127
|
+
|
|
128
|
+
if alto < 0 or radio < 0:
|
|
129
|
+
raise ValueError("alto y/o radio no pueden ser negativos")
|
|
130
|
+
|
|
131
|
+
angulo = np.linspace(0, 2 * pi, 21)
|
|
132
|
+
|
|
133
|
+
Xc = radio * np.cos(angulo)
|
|
134
|
+
Yc = radio * np.sin(angulo)
|
|
135
|
+
|
|
136
|
+
#Equivalente a zc = (Zc * alto) - dif.
|
|
137
|
+
|
|
138
|
+
Zc1 = np.full_like(angulo, 0)
|
|
139
|
+
Zc2 = np.full_like(angulo, alto)
|
|
140
|
+
|
|
141
|
+
#Coordenadas homogeneas de la base y la cara superior
|
|
142
|
+
|
|
143
|
+
p1 = np.vstack((Xc, Yc, Zc1, np.ones_like(angulo)))
|
|
144
|
+
p2 = np.vstack((Xc, Yc, Zc2, np.ones_like(angulo)))
|
|
145
|
+
|
|
146
|
+
#Aplicar la transformacion K
|
|
147
|
+
|
|
148
|
+
p1K = K @ p1
|
|
149
|
+
p2K = K @ p2
|
|
150
|
+
|
|
151
|
+
#Reconstruit las matrices que recibe la superficie
|
|
152
|
+
|
|
153
|
+
X = np.vstack((p1K[0, :], p2K[0, :]))
|
|
154
|
+
Y = np.vstack((p1K[1, :], p2K[1, :]))
|
|
155
|
+
Z = np.vstack((p1K[2, :], p2K[2, :]))
|
|
156
|
+
|
|
157
|
+
#Color amarillo FANUC: [255, 255, 50] / 255.
|
|
158
|
+
colores = {
|
|
159
|
+
"red": (1, 0, 0),
|
|
160
|
+
"green": (0, 1, 0),
|
|
161
|
+
"blue": (0, 0, 1),
|
|
162
|
+
"yellow": (1, 1, 0),
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
color_rgb = colores.get(color.lower())
|
|
166
|
+
|
|
167
|
+
if color_rgb is not None:
|
|
168
|
+
superficie = ax.plot_surface(
|
|
169
|
+
X, Y, Z,
|
|
170
|
+
color=color_rgb,
|
|
171
|
+
linewidth=0.3,
|
|
172
|
+
antialiased=True,
|
|
173
|
+
shade=False,
|
|
174
|
+
)
|
|
175
|
+
else:
|
|
176
|
+
superficie = ax.plot_surface(
|
|
177
|
+
X, Y, Z,
|
|
178
|
+
cmap="gray",
|
|
179
|
+
linewidth=0.3,
|
|
180
|
+
antialiased=True,
|
|
181
|
+
)
|
|
182
|
+
|
|
183
|
+
return superficie
|
|
184
|
+
|
|
185
|
+
def tubo2(K: Any, alto: float, radio: float, color: str, nalpha: float, *, ax: Any = None,) -> Any:
|
|
186
|
+
|
|
187
|
+
# Validar la transparencia.
|
|
188
|
+
if not 0.0 <= nalpha <= 1.0:
|
|
189
|
+
raise ValueError(
|
|
190
|
+
"nalpha debe estar entre 0 y 1."
|
|
191
|
+
)
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
superficie = tubo(K, alto, radio, color, ax=ax)
|
|
195
|
+
|
|
196
|
+
superficie.set_alpha(nalpha)
|
|
197
|
+
|
|
198
|
+
return superficie
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
def cono(K: Any, alto: float, radio: float, color: str, *, ax: Any = None,) -> Any:
|
|
202
|
+
|
|
203
|
+
#Obtener el eje 3D actual o crear uno si no existe
|
|
204
|
+
|
|
205
|
+
ax = _obtener_axes_3d(ax)
|
|
206
|
+
|
|
207
|
+
#Convertit la trasnformacion a una matriz numerica
|
|
208
|
+
|
|
209
|
+
K = np.asanyarray(K, dtype=float)
|
|
210
|
+
|
|
211
|
+
if K.shape != (4, 4):
|
|
212
|
+
raise ValueError("K Debe ser una matriz de transformacion 4x4")
|
|
213
|
+
|
|
214
|
+
if alto < 0 or radio < 0:
|
|
215
|
+
raise ValueError("alto y/o radio no pueden ser negativos")
|
|
216
|
+
|
|
217
|
+
angulo = np.linspace(0, 2 * pi, 21)
|
|
218
|
+
|
|
219
|
+
#Circunferencia de la base
|
|
220
|
+
|
|
221
|
+
xc = radio * np.cos(angulo)
|
|
222
|
+
yc = radio * np.sin(angulo)
|
|
223
|
+
|
|
224
|
+
|
|
225
|
+
Xc = np.vstack((xc, xc))
|
|
226
|
+
Yc = np.vstack((yc, yc))
|
|
227
|
+
Zc = np.vstack((np.zeros_like(angulo), np.ones_like(angulo)))
|
|
228
|
+
|
|
229
|
+
zc = Zc * alto
|
|
230
|
+
|
|
231
|
+
xc1 = Xc[0, :]
|
|
232
|
+
yc1 = Yc[0, :]
|
|
233
|
+
zc1 = zc[0, :]
|
|
234
|
+
|
|
235
|
+
#Coordenadas homogeneas de la base y la cara superior
|
|
236
|
+
|
|
237
|
+
p1 = np.vstack((xc1, yc1, zc1, np.ones_like(angulo)))
|
|
238
|
+
p2 = np.vstack((np.zeros_like(angulo), np.zeros_like(angulo), np.full_like(angulo, alto), np.ones_like(angulo)))
|
|
239
|
+
|
|
240
|
+
#Aplicar la transformacion K
|
|
241
|
+
|
|
242
|
+
p1K = K @ p1
|
|
243
|
+
p2K = K @ p2
|
|
244
|
+
|
|
245
|
+
#Reconstruit las matrices que recibe la superficie
|
|
246
|
+
|
|
247
|
+
X = np.vstack((p1K[0, :], p2K[0, :]))
|
|
248
|
+
Y = np.vstack((p1K[1, :], p2K[1, :]))
|
|
249
|
+
Z = np.vstack((p1K[2, :], p2K[2, :]))
|
|
250
|
+
|
|
251
|
+
#Color amarillo FANUC: [255, 255, 50] / 255.
|
|
252
|
+
colores = {
|
|
253
|
+
"red": (1, 0, 0),
|
|
254
|
+
"green": (0, 1, 0),
|
|
255
|
+
"blue": (0, 0, 1),
|
|
256
|
+
"yellow": (1, 1, 0),
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
color_rgb = colores.get(color.lower())
|
|
260
|
+
|
|
261
|
+
if color_rgb is not None:
|
|
262
|
+
superficie = ax.plot_surface(
|
|
263
|
+
X, Y, Z,
|
|
264
|
+
color=color_rgb,
|
|
265
|
+
linewidth=0.3,
|
|
266
|
+
antialiased=True,
|
|
267
|
+
shade=False,
|
|
268
|
+
)
|
|
269
|
+
else:
|
|
270
|
+
superficie = ax.plot_surface(
|
|
271
|
+
X, Y, Z,
|
|
272
|
+
cmap="gray",
|
|
273
|
+
linewidth=0.3,
|
|
274
|
+
antialiased=True,
|
|
275
|
+
)
|
|
276
|
+
|
|
277
|
+
return superficie
|
|
278
|
+
|
|
279
|
+
def flecha(K: Any, alto: float, color: str, ax: Any = None) -> tuple[Any, Any]:
|
|
280
|
+
|
|
281
|
+
#Obtener el eje 3D actual o crear uno si no existe
|
|
282
|
+
|
|
283
|
+
ax = _obtener_axes_3d(ax)
|
|
284
|
+
|
|
285
|
+
#Convertit la trasnformacion a una matriz numerica
|
|
286
|
+
|
|
287
|
+
K = np.asanyarray(K, dtype=float)
|
|
288
|
+
|
|
289
|
+
if K.shape != (4, 4):
|
|
290
|
+
raise ValueError("K Debe ser una matriz de transformacion 4x4")
|
|
291
|
+
|
|
292
|
+
if alto <= 0:
|
|
293
|
+
raise ValueError("alto no pueden ser negativos")
|
|
294
|
+
|
|
295
|
+
#Proporciones de la flacha, equivalente a MATLAB
|
|
296
|
+
|
|
297
|
+
radio1 = alto * 0.018
|
|
298
|
+
radio2 = alto * 0.05
|
|
299
|
+
alto1 = alto * 0.15
|
|
300
|
+
alto2 = alto - alto1
|
|
301
|
+
|
|
302
|
+
#Traslacion lical para colocar la punta sobre el tubo
|
|
303
|
+
|
|
304
|
+
Tz = np.eye(4)
|
|
305
|
+
Tz[2, 3] = alto2
|
|
306
|
+
|
|
307
|
+
K_cono = K @ Tz
|
|
308
|
+
|
|
309
|
+
#Dibujar el cuerpo de la flecha
|
|
310
|
+
|
|
311
|
+
superficie_tubo = tubo(K, alto2, radio1, color, ax=ax)
|
|
312
|
+
|
|
313
|
+
#Dibujar la punta de la flecha
|
|
314
|
+
|
|
315
|
+
superficie_cono = cono(K_cono, alto1, radio2, color, ax=ax)
|
|
316
|
+
|
|
317
|
+
return superficie_tubo, superficie_cono
|
|
318
|
+
|
|
319
|
+
def Sistema3D(M: Any, t: float, lx: str, ly: str, lz: str, tf: float, *, ax: Any = None) -> np.ndarray:
|
|
320
|
+
#Dibuja un sistema de referencia tridimensional
|
|
321
|
+
|
|
322
|
+
#Convertir M a una matriz numerica
|
|
323
|
+
|
|
324
|
+
M = np.asanyarray(M, dtype=float)
|
|
325
|
+
|
|
326
|
+
if M.shape != (4, 4):
|
|
327
|
+
raise ValueError("K Debe ser una matriz de transformacion 4x4")
|
|
328
|
+
|
|
329
|
+
if t <= 0:
|
|
330
|
+
raise ValueError("t no pueden ser negativos o cero")
|
|
331
|
+
|
|
332
|
+
#Obtener el eje 3D actual o crear uno si no existe
|
|
333
|
+
|
|
334
|
+
ax = _obtener_axes_3d(ax)
|
|
335
|
+
|
|
336
|
+
#Dibujar los tres ejes mediante flechas
|
|
337
|
+
|
|
338
|
+
flecha(M @ roty(pi / 2), t, "red", ax = ax)
|
|
339
|
+
flecha(M @ rotx(-pi / 2), t, "green", ax = ax)
|
|
340
|
+
flecha(M, t, "blue", ax = ax)
|
|
341
|
+
|
|
342
|
+
origen = M @ np.array([
|
|
343
|
+
[0.0],
|
|
344
|
+
[0.0],
|
|
345
|
+
[0.0],
|
|
346
|
+
[1.0],
|
|
347
|
+
])
|
|
348
|
+
|
|
349
|
+
#posiciones de las etiquetas
|
|
350
|
+
tt = 1.1 * t
|
|
351
|
+
|
|
352
|
+
posletrax = M @ np.array([tt, 0, 0, 1])
|
|
353
|
+
posletray = M @ np.array([0, tt, 0, 1])
|
|
354
|
+
posletraz = M @ np.array([0, 0, tt, 1])
|
|
355
|
+
|
|
356
|
+
#Añadir las etiquetas utilizando notacion matematica
|
|
357
|
+
|
|
358
|
+
if lx != " ":
|
|
359
|
+
ax.text(
|
|
360
|
+
posletrax[0],
|
|
361
|
+
posletrax[1],
|
|
362
|
+
posletrax[2],
|
|
363
|
+
rf"${lx}$",
|
|
364
|
+
fontsize=tf,
|
|
365
|
+
)
|
|
366
|
+
|
|
367
|
+
if ly != " ":
|
|
368
|
+
ax.text(
|
|
369
|
+
posletray[0],
|
|
370
|
+
posletray[1],
|
|
371
|
+
posletray[2],
|
|
372
|
+
rf"${ly}$",
|
|
373
|
+
fontsize=tf,
|
|
374
|
+
)
|
|
375
|
+
|
|
376
|
+
if lz != " ":
|
|
377
|
+
ax.text(
|
|
378
|
+
posletraz[0],
|
|
379
|
+
posletraz[1],
|
|
380
|
+
posletraz[2],
|
|
381
|
+
rf"${lz}$",
|
|
382
|
+
fontsize=tf,
|
|
383
|
+
)
|
|
384
|
+
|
|
385
|
+
return origen
|
|
386
|
+
|
|
387
|
+
def Sistema3Dbool(M: Any, t: float, lx: str, ly: str, lz: str, aparecer: Any, tf: float, *, ax: Any = None) -> np.ndarray:
|
|
388
|
+
#Dibuja un sistema de referencia tridimensional
|
|
389
|
+
|
|
390
|
+
#Convertir M a una matriz numerica
|
|
391
|
+
|
|
392
|
+
M = np.asanyarray(M, dtype=float)
|
|
393
|
+
|
|
394
|
+
if M.shape != (4, 4):
|
|
395
|
+
raise ValueError("K Debe ser una matriz de transformacion 4x4")
|
|
396
|
+
|
|
397
|
+
if t <= 0:
|
|
398
|
+
raise ValueError("t no pueden ser negativos o cero")
|
|
399
|
+
|
|
400
|
+
#Obtener el eje 3D actual o crear uno si no existe
|
|
401
|
+
|
|
402
|
+
ax = _obtener_axes_3d(ax)
|
|
403
|
+
|
|
404
|
+
#Dibujar los tres ejes mediante flechas
|
|
405
|
+
|
|
406
|
+
if aparecer[0]:
|
|
407
|
+
flecha(M @ roty(pi / 2), t, "red", ax = ax)
|
|
408
|
+
|
|
409
|
+
if aparecer[1]:
|
|
410
|
+
flecha(M @ rotx(-pi / 2), t, "green", ax = ax)
|
|
411
|
+
|
|
412
|
+
if aparecer[2]:
|
|
413
|
+
flecha(M, t, "blue", ax = ax)
|
|
414
|
+
|
|
415
|
+
|
|
416
|
+
|
|
417
|
+
origen = M @ np.array([
|
|
418
|
+
[0.0],
|
|
419
|
+
[0.0],
|
|
420
|
+
[0.0],
|
|
421
|
+
[1.0],
|
|
422
|
+
])
|
|
423
|
+
|
|
424
|
+
#posiciones de las etiquetas
|
|
425
|
+
tt = 1.2 * t
|
|
426
|
+
|
|
427
|
+
posletrax = M @ np.array([tt, 0, 0, 1])
|
|
428
|
+
posletray = M @ np.array([0, tt, 0, 1])
|
|
429
|
+
posletraz = M @ np.array([0, 0, tt, 1])
|
|
430
|
+
|
|
431
|
+
#Añadir las etiquetas utilizando notacion matematica
|
|
432
|
+
|
|
433
|
+
if lx != " ":
|
|
434
|
+
ax.text(
|
|
435
|
+
posletrax[0],
|
|
436
|
+
posletrax[1],
|
|
437
|
+
posletrax[2],
|
|
438
|
+
rf"${lx}$",
|
|
439
|
+
fontsize=tf,
|
|
440
|
+
)
|
|
441
|
+
|
|
442
|
+
if ly != " ":
|
|
443
|
+
ax.text(
|
|
444
|
+
posletray[0],
|
|
445
|
+
posletray[1],
|
|
446
|
+
posletray[2],
|
|
447
|
+
rf"${ly}$",
|
|
448
|
+
fontsize=tf,
|
|
449
|
+
)
|
|
450
|
+
|
|
451
|
+
if lz != " ":
|
|
452
|
+
ax.text(
|
|
453
|
+
posletraz[0],
|
|
454
|
+
posletraz[1],
|
|
455
|
+
posletraz[2],
|
|
456
|
+
rf"${lz}$",
|
|
457
|
+
fontsize=tf,
|
|
458
|
+
)
|
|
459
|
+
|
|
460
|
+
return origen
|
|
461
|
+
|
|
462
|
+
def Sistema(M: Any, t: float, lx: str, ly: str, lz: str,tf: float, *, ax: Any = None) -> np.ndarray:
|
|
463
|
+
|
|
464
|
+
#Dibuja un sistema de coordenadas 3D mediante líneas
|
|
465
|
+
|
|
466
|
+
M = np.asarray(M, dtype=float)
|
|
467
|
+
|
|
468
|
+
if M.shape != (4, 4):
|
|
469
|
+
|
|
470
|
+
raise ValueError("M debe ser una matriz de transformacion 4x4")
|
|
471
|
+
|
|
472
|
+
ax = _obtener_axes_3d(ax)
|
|
473
|
+
|
|
474
|
+
po = M @ np.array([[0], [0], [0], [1]], dtype=float )
|
|
475
|
+
px = M @ np.array([[t], [0], [0], [1]], dtype=float )
|
|
476
|
+
py = M @ np.array([[0], [t], [0], [1]], dtype=float )
|
|
477
|
+
pz = M @ np.array([[0], [0], [t], [1]], dtype=float )
|
|
478
|
+
|
|
479
|
+
ax.plot([po[0, 0], px[0, 0]], [po[1, 0], px[1, 0]], [po[2, 0], px[2, 0]], color = "r", linewidth = 2)
|
|
480
|
+
|
|
481
|
+
ax.plot([po[0, 0], py[0, 0]], [po[1, 0], py[1, 0]], [po[2, 0], py[2, 0]], color = "g", linewidth = 2)
|
|
482
|
+
|
|
483
|
+
ax.plot([po[0, 0], pz[0, 0]], [po[1, 0], pz[1, 0]], [po[2, 0], pz[2, 0]], color = "b", linewidth = 2)
|
|
484
|
+
|
|
485
|
+
tt = 1.1 * t
|
|
486
|
+
|
|
487
|
+
posletrax = M @ np.array([[tt], [0], [0], [1]], dtype=float)
|
|
488
|
+
posletray = M @ np.array([[0], [tt], [0], [1]], dtype=float)
|
|
489
|
+
posletraz = M @ np.array([[0], [0], [tt], [1]], dtype=float)
|
|
490
|
+
|
|
491
|
+
ax.text(
|
|
492
|
+
posletrax[0, 0], posletrax[1, 0], posletrax[2, 0],
|
|
493
|
+
f"${lx}$", fontsize=tf
|
|
494
|
+
)
|
|
495
|
+
|
|
496
|
+
ax.text(
|
|
497
|
+
posletray[0, 0], posletray[1, 0], posletray[2, 0],
|
|
498
|
+
f"${ly}$", fontsize=tf
|
|
499
|
+
)
|
|
500
|
+
|
|
501
|
+
ax.text(
|
|
502
|
+
posletraz[0, 0], posletraz[1, 0], posletraz[2, 0],
|
|
503
|
+
f"${lz}$", fontsize=tf
|
|
504
|
+
)
|
|
505
|
+
|
|
506
|
+
return po
|
|
507
|
+
|
|
508
|
+
def prisma_rectangularC(K: Any, ancho_x: float, largo_y: float, alto_z: float, color: str, *, ax: Any = None) -> None:
|
|
509
|
+
|
|
510
|
+
# Dibuja un prisma rectangular centrado en el origen
|
|
511
|
+
|
|
512
|
+
K = np.asarray(K, dtype=float)
|
|
513
|
+
|
|
514
|
+
if K.shape != (4, 4):
|
|
515
|
+
raise ValueError("M debe ser una matriz de transformacion 4x4")
|
|
516
|
+
|
|
517
|
+
ax = _obtener_axes_3d(ax)
|
|
518
|
+
|
|
519
|
+
colores = {
|
|
520
|
+
"red": (1, 0, 0),
|
|
521
|
+
"blue": (0, 0, 1),
|
|
522
|
+
"green": (0, 1, 0),
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
rgb = colores.get(color, (0.5, 0.5, 0.5))
|
|
526
|
+
|
|
527
|
+
x1 = ancho_x /2
|
|
528
|
+
x2 = -x1
|
|
529
|
+
|
|
530
|
+
y1 = largo_y / 2
|
|
531
|
+
y2 = -y1
|
|
532
|
+
|
|
533
|
+
z1 = alto_z /2
|
|
534
|
+
z2 = -z1
|
|
535
|
+
|
|
536
|
+
XX = np.array([
|
|
537
|
+
[x1, x1, x2, x2, x1],
|
|
538
|
+
[x1, x1, x2, x2, x1]
|
|
539
|
+
], dtype=float)
|
|
540
|
+
|
|
541
|
+
YY = np.array([
|
|
542
|
+
[y1, y2, y2, y1, y1],
|
|
543
|
+
[y1, y2, y2, y1, y1]
|
|
544
|
+
], dtype=float)
|
|
545
|
+
|
|
546
|
+
ZZ = np.array([
|
|
547
|
+
[z1, z1, z1, z1, z1],
|
|
548
|
+
[z2, z2, z2, z2, z2]
|
|
549
|
+
], dtype=float)
|
|
550
|
+
|
|
551
|
+
|
|
552
|
+
p1 = np.vstack((
|
|
553
|
+
XX[0, :],
|
|
554
|
+
YY[0, :],
|
|
555
|
+
ZZ[0, :],
|
|
556
|
+
np.ones(XX.shape[1])
|
|
557
|
+
))
|
|
558
|
+
|
|
559
|
+
p2 = np.vstack((
|
|
560
|
+
XX[1, :],
|
|
561
|
+
YY[1, :],
|
|
562
|
+
ZZ[1, :],
|
|
563
|
+
np.ones(XX.shape[1])
|
|
564
|
+
))
|
|
565
|
+
|
|
566
|
+
p1K = K @ p1
|
|
567
|
+
p2K = K @ p2
|
|
568
|
+
|
|
569
|
+
Xc = np.vstack((p1K[0, :], p2K[0, :]))
|
|
570
|
+
Yc = np.vstack((p1K[1, :], p2K[1, :]))
|
|
571
|
+
Zc = np.vstack((p1K[2, :], p2K[2, :]))
|
|
572
|
+
|
|
573
|
+
|
|
574
|
+
ax.plot_surface(Xc, Yc, Zc, color=rgb, edgecolor = "none", shade = False)
|
|
575
|
+
|
|
576
|
+
x1 = ancho_x /2
|
|
577
|
+
x2 = -x1
|
|
578
|
+
|
|
579
|
+
y1 = alto_z / 2
|
|
580
|
+
y2 = -y1
|
|
581
|
+
|
|
582
|
+
z1 = largo_y /2
|
|
583
|
+
z2 = -z1
|
|
584
|
+
|
|
585
|
+
XX = np.array([
|
|
586
|
+
[x1, x1, x2, x2, x1],
|
|
587
|
+
[x1, x1, x2, x2, x1]
|
|
588
|
+
], dtype=float)
|
|
589
|
+
|
|
590
|
+
YY = np.array([
|
|
591
|
+
[y1, y2, y2, y1, y1],
|
|
592
|
+
[y1, y2, y2, y1, y1]
|
|
593
|
+
], dtype=float)
|
|
594
|
+
|
|
595
|
+
ZZ = np.array([
|
|
596
|
+
[z1, z1, z1, z1, z1],
|
|
597
|
+
[z2, z2, z2, z2, z2]
|
|
598
|
+
], dtype=float)
|
|
599
|
+
|
|
600
|
+
|
|
601
|
+
p1 = np.vstack((
|
|
602
|
+
XX[0, :],
|
|
603
|
+
YY[0, :],
|
|
604
|
+
ZZ[0, :],
|
|
605
|
+
np.ones(XX.shape[1])
|
|
606
|
+
))
|
|
607
|
+
|
|
608
|
+
p2 = np.vstack((
|
|
609
|
+
XX[1, :],
|
|
610
|
+
YY[1, :],
|
|
611
|
+
ZZ[1, :],
|
|
612
|
+
np.ones(XX.shape[1])
|
|
613
|
+
))
|
|
614
|
+
|
|
615
|
+
K2 = K @ rotx(-pi/2)
|
|
616
|
+
|
|
617
|
+
p1K = K2 @ p1
|
|
618
|
+
p2K = K2 @ p2
|
|
619
|
+
|
|
620
|
+
Xc = np.vstack((p1K[0, :], p2K[0, :]))
|
|
621
|
+
Yc = np.vstack((p1K[1, :], p2K[1, :]))
|
|
622
|
+
Zc = np.vstack((p1K[2, :], p2K[2, :]))
|
|
623
|
+
|
|
624
|
+
ax.plot_surface(Xc, Yc, Zc, color=rgb, edgecolor = "none", shade = False)
|
|
625
|
+
|
|
626
|
+
def prisma_rectangular(K: Any, ancho_x: float, largo_y: float, alto_z: float, color: str, *, ax: Any = None) -> None:
|
|
627
|
+
|
|
628
|
+
prisma_rectangularC(K @ trans(0,0,alto_z/2),ancho_x,largo_y,alto_z,color, ax = ax)
|
|
629
|
+
|
|
630
|
+
def prisma_rectangularC2(K: Any, ancho_x: float, largo_y: float, alto_z: float, color: str,nalpha: float ,*, ax: Any = None) -> None:
|
|
631
|
+
|
|
632
|
+
# Dibuja un prisma rectangular centrado en el origen
|
|
633
|
+
|
|
634
|
+
K = np.asarray(K, dtype=float)
|
|
635
|
+
|
|
636
|
+
if K.shape != (4, 4):
|
|
637
|
+
raise ValueError("M debe ser una matriz de transformacion 4x4")
|
|
638
|
+
|
|
639
|
+
ax = _obtener_axes_3d(ax)
|
|
640
|
+
|
|
641
|
+
if not 0.0 <= nalpha <= 1.0:
|
|
642
|
+
raise ValueError("nalpha debe estar entre 0 y 1.")
|
|
643
|
+
|
|
644
|
+
colores = {
|
|
645
|
+
"red": (1, 0, 0),
|
|
646
|
+
"blue": (0, 0, 1),
|
|
647
|
+
"green": (0, 1, 0),
|
|
648
|
+
}
|
|
649
|
+
|
|
650
|
+
rgb = colores.get(color, (0.5, 0.5, 0.5))
|
|
651
|
+
|
|
652
|
+
x1 = ancho_x /2
|
|
653
|
+
x2 = -x1
|
|
654
|
+
|
|
655
|
+
y1 = largo_y / 2
|
|
656
|
+
y2 = -y1
|
|
657
|
+
|
|
658
|
+
z1 = alto_z /2
|
|
659
|
+
z2 = -z1
|
|
660
|
+
|
|
661
|
+
XX = np.array([
|
|
662
|
+
[x1, x1, x2, x2, x1],
|
|
663
|
+
[x1, x1, x2, x2, x1]
|
|
664
|
+
], dtype=float)
|
|
665
|
+
|
|
666
|
+
YY = np.array([
|
|
667
|
+
[y1, y2, y2, y1, y1],
|
|
668
|
+
[y1, y2, y2, y1, y1]
|
|
669
|
+
], dtype=float)
|
|
670
|
+
|
|
671
|
+
ZZ = np.array([
|
|
672
|
+
[z1, z1, z1, z1, z1],
|
|
673
|
+
[z2, z2, z2, z2, z2]
|
|
674
|
+
], dtype=float)
|
|
675
|
+
|
|
676
|
+
|
|
677
|
+
p1 = np.vstack((
|
|
678
|
+
XX[0, :],
|
|
679
|
+
YY[0, :],
|
|
680
|
+
ZZ[0, :],
|
|
681
|
+
np.ones(XX.shape[1])
|
|
682
|
+
))
|
|
683
|
+
|
|
684
|
+
p2 = np.vstack((
|
|
685
|
+
XX[1, :],
|
|
686
|
+
YY[1, :],
|
|
687
|
+
ZZ[1, :],
|
|
688
|
+
np.ones(XX.shape[1])
|
|
689
|
+
))
|
|
690
|
+
|
|
691
|
+
p1K = K @ p1
|
|
692
|
+
p2K = K @ p2
|
|
693
|
+
|
|
694
|
+
Xc = np.vstack((p1K[0, :], p2K[0, :]))
|
|
695
|
+
Yc = np.vstack((p1K[1, :], p2K[1, :]))
|
|
696
|
+
Zc = np.vstack((p1K[2, :], p2K[2, :]))
|
|
697
|
+
|
|
698
|
+
|
|
699
|
+
s1 = ax.plot_surface(Xc, Yc, Zc, color=rgb, edgecolor = "none", shade = False)
|
|
700
|
+
|
|
701
|
+
s1.set_alpha(nalpha)
|
|
702
|
+
|
|
703
|
+
x1 = ancho_x /2
|
|
704
|
+
x2 = -x1
|
|
705
|
+
|
|
706
|
+
y1 = alto_z / 2
|
|
707
|
+
y2 = -y1
|
|
708
|
+
|
|
709
|
+
z1 = largo_y /2
|
|
710
|
+
z2 = -z1
|
|
711
|
+
|
|
712
|
+
XX = np.array([
|
|
713
|
+
[x1, x1, x2, x2, x1],
|
|
714
|
+
[x1, x1, x2, x2, x1]
|
|
715
|
+
], dtype=float)
|
|
716
|
+
|
|
717
|
+
YY = np.array([
|
|
718
|
+
[y1, y2, y2, y1, y1],
|
|
719
|
+
[y1, y2, y2, y1, y1]
|
|
720
|
+
], dtype=float)
|
|
721
|
+
|
|
722
|
+
ZZ = np.array([
|
|
723
|
+
[z1, z1, z1, z1, z1],
|
|
724
|
+
[z2, z2, z2, z2, z2]
|
|
725
|
+
], dtype=float)
|
|
726
|
+
|
|
727
|
+
|
|
728
|
+
p1 = np.vstack((
|
|
729
|
+
XX[0, :],
|
|
730
|
+
YY[0, :],
|
|
731
|
+
ZZ[0, :],
|
|
732
|
+
np.ones(XX.shape[1])
|
|
733
|
+
))
|
|
734
|
+
|
|
735
|
+
p2 = np.vstack((
|
|
736
|
+
XX[1, :],
|
|
737
|
+
YY[1, :],
|
|
738
|
+
ZZ[1, :],
|
|
739
|
+
np.ones(XX.shape[1])
|
|
740
|
+
))
|
|
741
|
+
|
|
742
|
+
K2 = K @ rotx(-pi/2)
|
|
743
|
+
|
|
744
|
+
p1K = K2 @ p1
|
|
745
|
+
p2K = K2 @ p2
|
|
746
|
+
|
|
747
|
+
Xc = np.vstack((p1K[0, :], p2K[0, :]))
|
|
748
|
+
Yc = np.vstack((p1K[1, :], p2K[1, :]))
|
|
749
|
+
Zc = np.vstack((p1K[2, :], p2K[2, :]))
|
|
750
|
+
|
|
751
|
+
s2 = ax.plot_surface(Xc, Yc, Zc, color=rgb, edgecolor = "none", shade = False)
|
|
752
|
+
|
|
753
|
+
s2.set_alpha(nalpha)
|
|
754
|
+
|
|
755
|
+
def piramide(K: Any, alto: float, lado: float, color: str, *, ax: Any = None) -> None:
|
|
756
|
+
|
|
757
|
+
# Dibuja una pirámide con el eje de simetría sobre Z,
|
|
758
|
+
|
|
759
|
+
K = np.asarray(K, dtype=float)
|
|
760
|
+
|
|
761
|
+
if K.shape != (4, 4):
|
|
762
|
+
raise ValueError("M debe ser una matriz de transformacion 4x4")
|
|
763
|
+
|
|
764
|
+
ax = _obtener_axes_3d(ax)
|
|
765
|
+
|
|
766
|
+
colores = {
|
|
767
|
+
"red": (1, 0, 0,),
|
|
768
|
+
"blue": (0, 0, 1),
|
|
769
|
+
"green": (0, 1, 0),
|
|
770
|
+
"yellow": (1, 1, 0)
|
|
771
|
+
}
|
|
772
|
+
|
|
773
|
+
rgb = colores.get(color, (.5, .5, .5))
|
|
774
|
+
|
|
775
|
+
radio = lado/np.sqrt(2)
|
|
776
|
+
|
|
777
|
+
theta = np.linspace(0, 2 * pi, 5)
|
|
778
|
+
|
|
779
|
+
Xc = np.vstack((
|
|
780
|
+
radio * np.cos(theta),
|
|
781
|
+
np.zeros_like(theta)
|
|
782
|
+
))
|
|
783
|
+
|
|
784
|
+
Yc = np.vstack((
|
|
785
|
+
radio * np.sin(theta),
|
|
786
|
+
np.zeros_like(theta)
|
|
787
|
+
))
|
|
788
|
+
|
|
789
|
+
Zc = np.vstack((
|
|
790
|
+
np.zeros_like(theta),
|
|
791
|
+
np.full_like(theta, alto)
|
|
792
|
+
))
|
|
793
|
+
|
|
794
|
+
p1 = np.vstack((
|
|
795
|
+
Xc[0, :],
|
|
796
|
+
Yc[0, :],
|
|
797
|
+
Zc[0, :],
|
|
798
|
+
np.ones(Xc.shape[1])
|
|
799
|
+
))
|
|
800
|
+
|
|
801
|
+
p2 = np.vstack((
|
|
802
|
+
Xc[1, :],
|
|
803
|
+
Yc[1, :],
|
|
804
|
+
Zc[1, :],
|
|
805
|
+
np.ones(Xc.shape[1])
|
|
806
|
+
))
|
|
807
|
+
|
|
808
|
+
T = K @ rotz(pi / 4)
|
|
809
|
+
|
|
810
|
+
p1K = T @ p1
|
|
811
|
+
p2K = T @ p2
|
|
812
|
+
|
|
813
|
+
Xc = np.vstack((p1K[0, :], p2K[0, :]))
|
|
814
|
+
Yc = np.vstack((p1K[1, :], p2K[1, :]))
|
|
815
|
+
Zc = np.vstack((p1K[2, :], p2K[2, :]))
|
|
816
|
+
|
|
817
|
+
|
|
818
|
+
ax.plot_surface(Xc, Yc, Zc, color=rgb, edgecolor = "none", shade = False)
|
|
819
|
+
|
|
820
|
+
def normaL2(e: Any, t: Any) -> float:
|
|
821
|
+
|
|
822
|
+
e = np.asarray(e, dtype=float)
|
|
823
|
+
t = np.asarray(t, dtype=float).reshape(-1)
|
|
824
|
+
|
|
825
|
+
if e.ndim != 2:
|
|
826
|
+
raise ValueError("e debe ser un vector o una matriz de señales.")
|
|
827
|
+
|
|
828
|
+
nmuestras, nsenales = e.shape
|
|
829
|
+
|
|
830
|
+
if t.size != nmuestras:
|
|
831
|
+
raise ValueError(
|
|
832
|
+
"t debe tener un elemento por cada muestra de e."
|
|
833
|
+
)
|
|
834
|
+
|
|
835
|
+
if nmuestras < 2:
|
|
836
|
+
raise ValueError("Se necesita al menos 2 muestras")
|
|
837
|
+
|
|
838
|
+
if np.any(np.diff(t) <= 0):
|
|
839
|
+
raise ValueError("Los tiempos deben ser estrictamente crecientes")
|
|
840
|
+
|
|
841
|
+
et = np.sum(e**2, axis=1)
|
|
842
|
+
|
|
843
|
+
t = t - t[0]
|
|
844
|
+
|
|
845
|
+
y1F = 0.0
|
|
846
|
+
y1Fk1 = 0.0
|
|
847
|
+
Tant = 0.0
|
|
848
|
+
|
|
849
|
+
for k in range(nmuestras):
|
|
850
|
+
Tf = t[k] - Tant
|
|
851
|
+
Tant = t[k]
|
|
852
|
+
|
|
853
|
+
y1F = Tf * et[k] + y1Fk1
|
|
854
|
+
y1Fk1 = y1F
|
|
855
|
+
|
|
856
|
+
T = t[-1] - t[0]
|
|
857
|
+
|
|
858
|
+
nL2 = np.sqrt((1 / T) * y1F)
|
|
859
|
+
|
|
860
|
+
return float(nL2)
|
|
861
|
+
|
|
862
|
+
def linea_punteada(po: float, pf: float, *, ax: Any = None) -> None:
|
|
863
|
+
|
|
864
|
+
po = np.asarray(po, dtype=float).reshape(-1)
|
|
865
|
+
pf = np.asarray(pf, dtype=float).reshape(-1)
|
|
866
|
+
|
|
867
|
+
if po.size < 3 or pf.size < 3:
|
|
868
|
+
raise ValueError("po y pf deben contener al menos 3 coordenadas")
|
|
869
|
+
|
|
870
|
+
ax = _obtener_axes_3d(ax)
|
|
871
|
+
|
|
872
|
+
ax.plot([po[0], pf[0]], [po[1], pf[1]], [po[2], pf[2]], linestyle="--", color = "black")
|
|
873
|
+
|
|
874
|
+
def Load_STL(archivo: str) -> Any:
|
|
875
|
+
|
|
876
|
+
#Lee un archivo STL y devuelve una estructura con los datos geométricos del sólido.
|
|
877
|
+
|
|
878
|
+
ruta = Path(archivo)
|
|
879
|
+
|
|
880
|
+
if not ruta.is_file():
|
|
881
|
+
raise ValueError(f"No se encuentra el archivo STL: {ruta}")
|
|
882
|
+
|
|
883
|
+
malla = trimesh.load(
|
|
884
|
+
str(ruta),
|
|
885
|
+
file_type="stl",
|
|
886
|
+
force="mesh",
|
|
887
|
+
process=True
|
|
888
|
+
)
|
|
889
|
+
|
|
890
|
+
v = np.asarray(malla.vertices, dtype=float)
|
|
891
|
+
f = np.asarray(malla.faces, dtype=np.int64)
|
|
892
|
+
n = np.asarray(malla.face_normals, dtype=float)
|
|
893
|
+
|
|
894
|
+
c = None
|
|
895
|
+
|
|
896
|
+
stltitle = ruta.name
|
|
897
|
+
|
|
898
|
+
solido = SimpleNamespace(vertices = v, faces = f, normals = n, colors = c, STLtitle = stltitle)
|
|
899
|
+
|
|
900
|
+
return solido
|
|
901
|
+
|
|
902
|
+
def gripper(H: Any, L: float, lw: float, *, ax: Any = None) -> None:
|
|
903
|
+
|
|
904
|
+
# Dibuja una pinza robótica mediante segmentos de línea.
|
|
905
|
+
|
|
906
|
+
|
|
907
|
+
H = np.asarray(H, dtype=float)
|
|
908
|
+
|
|
909
|
+
if H.shape != (4, 4):
|
|
910
|
+
raise ValueError("M debe ser una matriz de transformacion 4x4")
|
|
911
|
+
|
|
912
|
+
ax = _obtener_axes_3d(ax)
|
|
913
|
+
|
|
914
|
+
P0 = H @ np.asarray([0, 0, 0, 1], dtype=float)
|
|
915
|
+
P1 = H @ np.asarray([0, 0, L, 1], dtype=float)
|
|
916
|
+
PL1 = H @ np.asarray([0, -L * 0.4, L, 1], dtype=float)
|
|
917
|
+
PL2 = H @ np.asarray([0, -L * 0.4, L * 1.4, 1], dtype=float)
|
|
918
|
+
PR1 = H @ np.asarray([0, L * 0.4, L, 1], dtype=float)
|
|
919
|
+
PR2 = H @ np.asarray([0, L * 0.4, L * 1.4, 1], dtype=float)
|
|
920
|
+
|
|
921
|
+
eslabon(P0,P1,'k',lw, ax = ax)
|
|
922
|
+
eslabon(P1,PL1,'k',lw, ax = ax)
|
|
923
|
+
eslabon(PL1,PL2,'k',lw, ax = ax)
|
|
924
|
+
eslabon(P1,PR1,'k',lw, ax = ax)
|
|
925
|
+
eslabon(PR1,PR2,'k',lw, ax = ax)
|
|
926
|
+
|
|
927
|
+
def giroZ(K: Any, theta_min_max: Any, r_a: Any, lth: str, lposXYZ: Any, tf: float, *, ax: Any = None) -> None:
|
|
928
|
+
|
|
929
|
+
K = np.asarray(K, dtype=float)
|
|
930
|
+
|
|
931
|
+
if K.shape != (4, 4):
|
|
932
|
+
raise ValueError("M debe ser una matriz de transformacion 4x4")
|
|
933
|
+
|
|
934
|
+
ax = _obtener_axes_3d(ax)
|
|
935
|
+
|
|
936
|
+
theta = np.arange(theta_min_max[0], theta_min_max[1] + pi/360, pi/180 )
|
|
937
|
+
|
|
938
|
+
x = r_a[0]*np.cos(theta)
|
|
939
|
+
y = r_a[0]*np.sin(theta)
|
|
940
|
+
|
|
941
|
+
z = np.full_like(theta, r_a[1])
|
|
942
|
+
|
|
943
|
+
XYZ = K @ np.vstack((x, y, z, np.ones_like(theta)))
|
|
944
|
+
|
|
945
|
+
ax.plot(XYZ[0, :], XYZ[1, :], XYZ[2, :], "k", linewidth = 2)
|
|
946
|
+
|
|
947
|
+
ancho = r_a[0] / 8
|
|
948
|
+
alto = r_a[0] / 4
|
|
949
|
+
|
|
950
|
+
Kcono = K @ rotz(theta_min_max[1]) @ trans(r_a[0], 0, r_a[1]) @ rotx(-pi/2) @ trans(0, 0, -alto)
|
|
951
|
+
|
|
952
|
+
cono(Kcono, alto, ancho, "gray", ax = ax)
|
|
953
|
+
|
|
954
|
+
posletra = K @ [lposXYZ[0], lposXYZ[1], lposXYZ[2], 1]
|
|
955
|
+
|
|
956
|
+
if lth != " ":
|
|
957
|
+
ax.text(posletra[0], posletra[1], posletra[2], f"${lth}$", fontsize=tf)
|
|
958
|
+
|
|
959
|
+
def giro_z(K: Any, theta_max: Any, r: Any, lth: str, lpos: Any, tf: float, *, ax: Any = None) -> None:
|
|
960
|
+
|
|
961
|
+
K = np.asarray(K, dtype=float)
|
|
962
|
+
|
|
963
|
+
if K.shape != (4, 4):
|
|
964
|
+
raise ValueError("M debe ser una matriz de transformacion 4x4")
|
|
965
|
+
|
|
966
|
+
ax = _obtener_axes_3d(ax)
|
|
967
|
+
|
|
968
|
+
theta = np.arange(0, theta_max + pi/360, pi/180 )
|
|
969
|
+
|
|
970
|
+
x = r * np.cos(theta)
|
|
971
|
+
y = r * np.sin(theta)
|
|
972
|
+
|
|
973
|
+
z = np.zeros_like(theta)
|
|
974
|
+
|
|
975
|
+
XYZ = K @ np.vstack((x, y, z, z + 1))
|
|
976
|
+
|
|
977
|
+
ax.plot(XYZ[0, :], XYZ[1, :], XYZ[2, :], color = "black", linewidth = 2)
|
|
978
|
+
|
|
979
|
+
ancho = r / 8
|
|
980
|
+
alto = r / 4
|
|
981
|
+
|
|
982
|
+
Kcono = K @ rotz(theta_max) @ trans(r, 0, 0) @ rotx(-pi/2) @ trans(0, 0, -alto)
|
|
983
|
+
|
|
984
|
+
cono(Kcono, alto, ancho, "gray", ax = ax)
|
|
985
|
+
|
|
986
|
+
veces = 1.4
|
|
987
|
+
posletra = K @ [veces * r * np.cos(lpos), veces * r * np.sin(lpos), 0, 1]
|
|
988
|
+
|
|
989
|
+
if lth != " ":
|
|
990
|
+
ax.text(posletra[0], posletra[1], posletra[2], f"${lth}$", fontsize=tf)
|
|
991
|
+
|
|
992
|
+
def eslabon_STL(H: Any, solido: Any, RGBcolor: Any, *, ax: Any = None) -> None:
|
|
993
|
+
|
|
994
|
+
# Esta función se utiliza para dibujar un objeto sólido cargado apartir de
|
|
995
|
+
# un archivo STL en la pose indicada por la matriz de transformación H
|
|
996
|
+
|
|
997
|
+
H = np.asarray(H, dtype=float)
|
|
998
|
+
RGBcolor = np.asarray(RGBcolor, dtype=float)
|
|
999
|
+
|
|
1000
|
+
if H.shape != (4, 4):
|
|
1001
|
+
raise ValueError("M debe ser una matriz de transformacion 4x4")
|
|
1002
|
+
|
|
1003
|
+
if RGBcolor.shape != (3,):
|
|
1004
|
+
raise ValueError("RGBcolor debe contener tres valores RGB.")
|
|
1005
|
+
|
|
1006
|
+
if np.any((RGBcolor < 0) | (RGBcolor > 1)):
|
|
1007
|
+
raise ValueError("Los componentes RGB deben estar entre 0 y 1.")
|
|
1008
|
+
|
|
1009
|
+
vertices = np.asarray(solido.vertices, dtype=float)
|
|
1010
|
+
caras = np.asarray(solido.faces, dtype=np.int64)
|
|
1011
|
+
|
|
1012
|
+
if vertices.ndim != 2 or vertices.shape[1] != 3:
|
|
1013
|
+
raise ValueError("solido.vertices debe tener forma (n, 3)")
|
|
1014
|
+
|
|
1015
|
+
if caras.ndim != 2 or caras.shape[1] != 3:
|
|
1016
|
+
raise ValueError("solido.faces debe tener forma (m, 3)")
|
|
1017
|
+
|
|
1018
|
+
vn = H @ np.vstack((vertices.T, np.ones(vertices.shape[0])))
|
|
1019
|
+
|
|
1020
|
+
vertices_transformados = vn[:3, :].T
|
|
1021
|
+
|
|
1022
|
+
ax = _obtener_axes_3d(ax)
|
|
1023
|
+
|
|
1024
|
+
triangulos = vertices_transformados[caras]
|
|
1025
|
+
|
|
1026
|
+
luz = LightSource(azdeg=315, altdeg=45)
|
|
1027
|
+
|
|
1028
|
+
colores = np.tile(RGBcolor, (caras.shape[0], 1))
|
|
1029
|
+
|
|
1030
|
+
superficie = Poly3DCollection(triangulos, facecolors=colores, edgecolors=colores, linewidths=0, shade=True, lightsource=luz)
|
|
1031
|
+
|
|
1032
|
+
ax.add_collection3d(superficie)
|
|
1033
|
+
|
|
1034
|
+
ax.auto_scale_xyz(vertices_transformados[:, 0], vertices_transformados[:, 1], vertices_transformados[:, 2])
|
|
1035
|
+
|
|
1036
|
+
def esfera(K: Any, radio: float, color: str, *, ax: Any = None) -> None:
|
|
1037
|
+
|
|
1038
|
+
# Esta función se utiliza para dibujar una esfera cuyo centro coincide con
|
|
1039
|
+
# el origen del sistema relacionado con la matriz de transformación K.
|
|
1040
|
+
|
|
1041
|
+
K = np.asarray(K, dtype=float)
|
|
1042
|
+
|
|
1043
|
+
if K.shape != (4, 4):
|
|
1044
|
+
raise ValueError("M debe ser una matriz de transformacion 4x4")
|
|
1045
|
+
|
|
1046
|
+
u = np.linspace(0, 2 * pi, 21)
|
|
1047
|
+
v = np.linspace(0, pi, 21)
|
|
1048
|
+
|
|
1049
|
+
U, V = np.meshgrid(u, v)
|
|
1050
|
+
|
|
1051
|
+
Xs = np.cos(U) * np.sin(V)
|
|
1052
|
+
Ys = np.sin(U) * np.sin(V)
|
|
1053
|
+
Zs = np.cos(V)
|
|
1054
|
+
|
|
1055
|
+
Xs = Xs * radio
|
|
1056
|
+
Ys = Ys * radio
|
|
1057
|
+
Zs = Zs * radio
|
|
1058
|
+
|
|
1059
|
+
p = K @ np.array([0, 0, 0, 1], dtype=float)
|
|
1060
|
+
|
|
1061
|
+
Xs = Xs + p[0]
|
|
1062
|
+
Ys = Ys + p[1]
|
|
1063
|
+
Zs = Zs + p[2]
|
|
1064
|
+
|
|
1065
|
+
ax = _obtener_axes_3d(ax)
|
|
1066
|
+
|
|
1067
|
+
colores = {
|
|
1068
|
+
"red": (1, 0, 0),
|
|
1069
|
+
"green": (0, 1, 0),
|
|
1070
|
+
"blue": (0, 0, 1)
|
|
1071
|
+
}
|
|
1072
|
+
|
|
1073
|
+
if color in colores:
|
|
1074
|
+
|
|
1075
|
+
ax.plot_surface(Xs, Ys, Zs, color=colores[color], edgecolor="none", shade=False)
|
|
1076
|
+
|
|
1077
|
+
else:
|
|
1078
|
+
ax.plot_surface(Xs, Ys, Zs, cmap="gray", edgecolor="none", shade=False)
|
|
1079
|
+
|
|
1080
|
+
def ejes3D(Po: Any, Px: Any, Py: Any, Pz: Any, cx: str, cy: str, cz: str, lw: float, *, ax: Any = None) -> None:
|
|
1081
|
+
|
|
1082
|
+
po = np.asarray(Po, dtype=float).reshape(-1)
|
|
1083
|
+
px = np.asarray(Px, dtype=float).reshape(-1)
|
|
1084
|
+
py = np.asarray(Py, dtype=float).reshape(-1)
|
|
1085
|
+
pz = np.asarray(Pz, dtype=float).reshape(-1)
|
|
1086
|
+
|
|
1087
|
+
|
|
1088
|
+
if any(p.size < 3 for p in (po, px, py, pz)):
|
|
1089
|
+
raise ValueError("Cada punto debe contener al menos 3 coordenadas")
|
|
1090
|
+
|
|
1091
|
+
ax = _obtener_axes_3d(ax)
|
|
1092
|
+
|
|
1093
|
+
ax.plot([po[0], px[0]], [po[1], px[1]], [po[2], px[2]], color=cx, linewidth=lw)
|
|
1094
|
+
ax.plot([po[0], py[0]], [po[1], py[1]], [po[2], py[2]], color=cy, linewidth=lw)
|
|
1095
|
+
ax.plot([po[0], pz[0]], [po[1], pz[1]], [po[2], pz[2]], color=cz, linewidth=lw)
|
|
1096
|
+
|
|
1097
|
+
ax.set_xlabel("Eje X")
|
|
1098
|
+
ax.set_ylabel("Eje Y")
|
|
1099
|
+
ax.set_zlabel("Eje Z")
|
|
1100
|
+
ax.grid(True)
|
|
1101
|
+
|
|
1102
|
+
def distancia_z(K: Any, altura: float, offsetXY: Any, ld: str, off_label: Any, tf: float, *, ax: Any = None) -> None:
|
|
1103
|
+
|
|
1104
|
+
# Esta función se utiliza para dibujar una doble flecha paralela al eje Z
|
|
1105
|
+
# correspondiente al sistema coordenado que ubica K con respecto al
|
|
1106
|
+
# origen.
|
|
1107
|
+
|
|
1108
|
+
K = np.asarray(K, dtype=float)
|
|
1109
|
+
offsetXY = np.asarray(offsetXY, dtype=float).reshape(-1)
|
|
1110
|
+
off_label = np.asarray(off_label, dtype=float).reshape(-1)
|
|
1111
|
+
|
|
1112
|
+
if K.shape != (4, 4):
|
|
1113
|
+
raise ValueError("M debe ser una matriz de transformacion 4x4")
|
|
1114
|
+
|
|
1115
|
+
ax = _obtener_axes_3d(ax)
|
|
1116
|
+
|
|
1117
|
+
po = K @ np.array([offsetXY[0], offsetXY[1], 0, 1], dtype=float)
|
|
1118
|
+
pf = K @ np.array([offsetXY[0], offsetXY[1], altura, 1], dtype=float)
|
|
1119
|
+
|
|
1120
|
+
ax.plot([po[0], pf[0]], [po[1], pf[1]], [po[2], pf[2]], "k", linewidth=2)
|
|
1121
|
+
|
|
1122
|
+
|
|
1123
|
+
alto = altura * 0.08
|
|
1124
|
+
radio = altura * 0.03
|
|
1125
|
+
|
|
1126
|
+
cono(K @ trans(offsetXY[0], offsetXY[1], altura - alto), alto, radio, "gray", ax = ax)
|
|
1127
|
+
cono(K @ trans(offsetXY[0], offsetXY[1], alto) @ rotx(pi), alto, radio, "gray", ax = ax)
|
|
1128
|
+
|
|
1129
|
+
posletra = K @ np.array([off_label[0], off_label[1], off_label[2], 1], dtype =float)
|
|
1130
|
+
|
|
1131
|
+
if ld != " ":
|
|
1132
|
+
|
|
1133
|
+
ax.text(posletra[0], posletra[1], posletra[2], f"${ld}$", fontsize=tf)
|
|
1134
|
+
|
|
1135
|
+
def DibujaPlano(LX: float, LY: float, K: Any, nalpha: float, *, ax: Any = None) -> None:
|
|
1136
|
+
|
|
1137
|
+
# Esta función se utiliza para dibujar un plano cuyo origen está
|
|
1138
|
+
# relacionado con la matriz de transformación homogenea K.
|
|
1139
|
+
|
|
1140
|
+
K = np.asarray(K, dtype=float)
|
|
1141
|
+
|
|
1142
|
+
if K.shape != (4, 4):
|
|
1143
|
+
raise ValueError("M debe ser una matriz de transformacion 4x4")
|
|
1144
|
+
|
|
1145
|
+
if not 0.0 <= nalpha <= 1.0:
|
|
1146
|
+
raise ValueError("nalpha debe estar entre 0 y 1")
|
|
1147
|
+
|
|
1148
|
+
ax = _obtener_axes_3d(ax)
|
|
1149
|
+
|
|
1150
|
+
Datos=np.array([
|
|
1151
|
+
[0, LX, 0, LX, LX/2],
|
|
1152
|
+
[0, 0, LY, LY, LX/2],
|
|
1153
|
+
[0, 0, 0, 0, 0],
|
|
1154
|
+
[1, 1, 1, 1, 1]], dtype=float)
|
|
1155
|
+
|
|
1156
|
+
Datos2=np.array([
|
|
1157
|
+
[0, LX, LX, 0, 0],
|
|
1158
|
+
[0, 0, LY, LY, 0],
|
|
1159
|
+
[0, 0, 0, 0, 0],
|
|
1160
|
+
[1, 1, 1, 1, 1]], dtype=float)
|
|
1161
|
+
|
|
1162
|
+
Plano = K @ Datos
|
|
1163
|
+
|
|
1164
|
+
Contorno = K @ Datos2
|
|
1165
|
+
|
|
1166
|
+
xx = Plano[0, :]
|
|
1167
|
+
yy = Plano[1, :]
|
|
1168
|
+
zz = Plano[2, :]
|
|
1169
|
+
|
|
1170
|
+
ax.plot_trisurf(xx, yy, zz, color=(0, 0, 1), edgecolor="none", linewidth=0, shade=0, alpha=nalpha)
|
|
1171
|
+
|
|
1172
|
+
ax.plot(Contorno[0, :], Contorno[1, :], Contorno[2, :], color="k")
|
|
1173
|
+
|
|
1174
|
+
def desplazamiento_z(K: Any, altura: float, offsetXY: Any, ld: str, off_label: Any, tf: float, *, ax: Any = None) -> None:
|
|
1175
|
+
|
|
1176
|
+
# Esta función se utiliza para dibujar una flecha paralela al eje Z
|
|
1177
|
+
# correspondiente al sistema coordenado que ubica K con respecto al origen.
|
|
1178
|
+
|
|
1179
|
+
K = np.asarray(K, dtype=float)
|
|
1180
|
+
offsetXY = np.asarray(offsetXY, dtype=float).reshape(-1)
|
|
1181
|
+
off_label = np.asarray(off_label, dtype=float).reshape(-1)
|
|
1182
|
+
|
|
1183
|
+
if K.shape != (4, 4):
|
|
1184
|
+
raise ValueError("M debe ser una matriz de transformacion 4x4")
|
|
1185
|
+
|
|
1186
|
+
ax = _obtener_axes_3d(ax)
|
|
1187
|
+
|
|
1188
|
+
po = K @ np.array([offsetXY[0], offsetXY[1], 0, 1], dtype=float)
|
|
1189
|
+
pf = K @ np.array([offsetXY[0], offsetXY[1], altura, 1], dtype=float)
|
|
1190
|
+
|
|
1191
|
+
ax.plot([po[0], pf[0]], [po[1], pf[1]], [po[2], pf[2]], "k", linewidth=2)
|
|
1192
|
+
|
|
1193
|
+
|
|
1194
|
+
alto = altura * 0.08
|
|
1195
|
+
radio = altura * 0.03
|
|
1196
|
+
|
|
1197
|
+
cono(K @ trans(offsetXY[0], offsetXY[1], altura - alto), alto, radio, "gray", ax = ax)
|
|
1198
|
+
|
|
1199
|
+
posletra = K @ np.array([off_label[0], off_label[1], off_label[2], 1], dtype =float)
|
|
1200
|
+
|
|
1201
|
+
if ld != " ":
|
|
1202
|
+
|
|
1203
|
+
ax.text(posletra[0], posletra[1], posletra[2], f"${ld}$", fontsize=tf)
|
|
1204
|
+
|
|
1205
|
+
|
|
1206
|
+
def Dibujar_intermedio(Tk1: float, Tk2: float, Tki: Any, nfig: int) -> None:
|
|
1207
|
+
|
|
1208
|
+
Tk1 = np.asarray(Tk1, dtype=float)
|
|
1209
|
+
Tk2 = np.asarray(Tk2, dtype=float)
|
|
1210
|
+
Tki = np.asarray(Tki, dtype=float)
|
|
1211
|
+
|
|
1212
|
+
if Tk1.shape != (4, 4) and Tk2.shape != (4, 4) and Tki.shape != (4, 4):
|
|
1213
|
+
raise ValueError("Tki debe ser una matriz de transformacion 4x4")
|
|
1214
|
+
|
|
1215
|
+
H00 = np.eye(4)
|
|
1216
|
+
ini_fin = False
|
|
1217
|
+
|
|
1218
|
+
tf = 14
|
|
1219
|
+
tt = 1
|
|
1220
|
+
|
|
1221
|
+
fig = plt.figure(nfig)
|
|
1222
|
+
fig.clf()
|
|
1223
|
+
|
|
1224
|
+
ax = fig.add_subplot(111, projection="3d")
|
|
1225
|
+
|
|
1226
|
+
po0 = Sistema3D(H00, tt, "X_0", "y_0", "z_0", tf, ax=ax)
|
|
1227
|
+
|
|
1228
|
+
if ini_fin:
|
|
1229
|
+
|
|
1230
|
+
pTk0 = Sistema3D(Tk1, tt, "x_i", "y_i", "z_i", tf, ax=ax)
|
|
1231
|
+
|
|
1232
|
+
pTkF = Sistema3D(Tk2, tt, "x_f", "y_f", "z_f", tf, ax=ax)
|
|
1233
|
+
|
|
1234
|
+
else:
|
|
1235
|
+
|
|
1236
|
+
origen = np.array([[0], [0], [0], [1]])
|
|
1237
|
+
|
|
1238
|
+
pTk0 = Tk1 @ origen
|
|
1239
|
+
pTkF = Tk2 @ origen
|
|
1240
|
+
|
|
1241
|
+
alto = tt * 0.6
|
|
1242
|
+
radio = tt * 0.2
|
|
1243
|
+
|
|
1244
|
+
if ini_fin:
|
|
1245
|
+
|
|
1246
|
+
prisma_rectangular(Tk1, radio, radio, alto, "yellow", ax=ax)
|
|
1247
|
+
|
|
1248
|
+
prisma_rectangular(Tk2, radio, radio, alto, "yellow", ax=ax)
|
|
1249
|
+
|
|
1250
|
+
prisma_rectangular(Tki, radio, radio, alto, "yellow", ax=ax)
|
|
1251
|
+
|
|
1252
|
+
linea_punteada(pTk0, pTkF, ax=ax)
|
|
1253
|
+
|
|
1254
|
+
ax.set_xlim(-1, 5)
|
|
1255
|
+
ax.set_ylim(-1, 3)
|
|
1256
|
+
ax.set_zlim(-1, 5)
|
|
1257
|
+
|
|
1258
|
+
ax.set_box_aspect((6, 4, 6))
|
|
1259
|
+
ax.grid(True)
|
|
1260
|
+
|
|
1261
|
+
ax.view_init(elev=50, azim=80)
|
|
1262
|
+
ax.set_proj_type("persp")
|
|
1263
|
+
|
|
1264
|
+
ax.set_title("Interpolación")
|
|
1265
|
+
ax.set_xlabel("X")
|
|
1266
|
+
ax.set_ylabel("Y")
|
|
1267
|
+
ax.set_zlabel("Z")
|
|
1268
|
+
|
|
1269
|
+
fig.tight_layout()
|
|
1270
|
+
|
|
1271
|
+
return fig, ax
|
|
1272
|
+
|