DMU50_GUI_PY/funct_wp.py
Eduard Gerlitz b607afcd86 .
2025-03-11 16:00:41 +01:00

218 lines
9.1 KiB
Python

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d.art3d import Poly3DCollection
import numpy as np
def generate_picture_wp(all_group_vars, picture_path, DEBUG=False):
# try:
vars = all_group_vars['wp']
WPR_X, WPR_Y, WPR_Z, WP_CLP_OFFSET_X, WPR_GRP_OFFSET_Z, WPF_GRP_OFFSET_Y, WPF_GRP_OFFSET_Z = (float(vars[k]) for k in
['WPR_X', 'WPR_Y', 'WPR_Z', 'WP_CLP_OFFSET_X', 'WPR_GRP_OFFSET_Z', 'WPF_GRP_OFFSET_Y', 'WPF_GRP_OFFSET_Z'])
if min(WPR_X, WPR_Y, WPR_Z) <= 0:
raise ValueError("Dimensions must be positive.")
# -------------------- wpraw -------------------------
fig, ax = plt.subplots(subplot_kw={'projection': '3d'})
x_cube, y_cube = 2 * WPR_X, 2 * WPR_Y
vertices = np.array([[0, 0, 0], [x_cube, 0, 0], [x_cube, y_cube, 0], [0, y_cube, 0],
[0, 0, WPR_Z], [x_cube, 0, WPR_Z], [x_cube, y_cube, WPR_Z], [0, y_cube, WPR_Z]])
faces = [vertices[[0, 1, 2, 3]], vertices[[4, 5, 6, 7]], vertices[[0, 3, 7, 4]],
vertices[[1, 2, 6, 5]], vertices[[0, 1, 5, 4]], vertices[[2, 3, 7, 6]]]
# Polyeder hinzufügen
ax.add_collection3d(Poly3DCollection(faces, facecolors='k', linewidths=1, edgecolors='k', alpha=.15))
# Achsen-Einstellungen zusammenfassen
ax.set_box_aspect([x_cube, y_cube, WPR_Z])
ax.set(xticks=[], yticks=[], zticks=[])
# Kugel zeichnen
u, v = np.mgrid[0:2 * np.pi:20j, 0:np.pi:20j]
ball_radius = min(WPR_X, WPR_Y, WPR_Z) * 0.1
ax.plot_surface(
WPR_X + ball_radius * np.cos(u) * np.sin(v),
WPR_Y + ball_radius * np.sin(u) * np.sin(v),
WPR_Z + ball_radius * np.cos(v),
color='r'
)
# Koordinatenachsen zeichnen
axis_len = max(x_cube, y_cube, WPR_Z) * 0.3
colors = ['r', 'g', 'b']
for i, vec in enumerate(np.eye(3)):
ax.quiver(0, 0, 0, *(axis_len * vec), color=colors[i], linewidth=5)
gripper_thickness = max(x_cube, y_cube, WPR_Z) * 0.1
gripper_wide = WPR_X * 0.4
GRP_ALPHA = .25
bars = [
((x_cube - gripper_wide) * 0.5, -gripper_thickness, WPR_Z - WPR_GRP_OFFSET_Z),
((x_cube - gripper_wide) * 0.5, y_cube, WPR_Z - WPR_GRP_OFFSET_Z),
]
for (x, y, z) in bars:
ax.bar3d(
x, y, z,
gripper_wide, gripper_thickness, 2 * WPR_Z + WPR_GRP_OFFSET_Z,
color='r', alpha=GRP_ALPHA, edgecolor='k', linewidth=0.5
)
ax.view_init(elev=18, azim=-130)
ax.set_xlim([0, x_cube]); ax.set_ylim([0, y_cube]); ax.set_zlim([0, WPR_Z])
ax.set_aspect('auto')
if not DEBUG:
fig.savefig(picture_path, bbox_inches='tight', dpi=300, transparent=True)
else:
plt.show()
plt.close()
# except Exception as e:
# print(f"An error occurred: {e}")
# --------------- OLD ------------------------
def generate_picture_wpraw(all_group_vars, picture_path, DEBUG=False):
try:
# --- wpraw
vars = all_group_vars['wp_raw']
x, y, z = float(vars['WPR_X']), float(vars['WPR_Y']), float(vars['WPR_Z'])
if min(x, y, z) <= 0:
raise ValueError("Dimensions must be positive.")
fig, ax = plt.subplots(subplot_kw={'projection': '3d'})
x_cube, y_cube = 2 * x, 2 * y
vertices = np.array([[0, 0, 0], [x_cube, 0, 0], [x_cube, y_cube, 0], [0, y_cube, 0],
[0, 0, z], [x_cube, 0, z], [x_cube, y_cube, z], [0, y_cube, z]])
faces = [vertices[[0, 1, 2, 3]], vertices[[4, 5, 6, 7]], vertices[[0, 3, 7, 4]],
vertices[[1, 2, 6, 5]], vertices[[0, 1, 5, 4]], vertices[[2, 3, 7, 6]]]
ax.add_collection3d(Poly3DCollection(faces, facecolors='k', linewidths=1, edgecolors='k', alpha=.15))
ax.set_box_aspect([x_cube, y_cube, z])
ax.set_xticks([]);
ax.set_yticks([]);
ax.set_zticks([])
ball_radius = min(x, y, z) * 0.1
u, v = np.linspace(0, 2 * np.pi, 20), np.linspace(0, np.pi, 20)
x_sphere = x + ball_radius * np.outer(np.cos(u), np.sin(v))
y_sphere = y + ball_radius * np.outer(np.sin(u), np.sin(v))
z_sphere = z + ball_radius * np.outer(np.ones_like(u), np.cos(v))
ax.plot_surface(x_sphere, y_sphere, z_sphere, color='r', alpha=1)
max_size = max(x_cube, y_cube, z) * 0.3
ax.quiver(0, 0, 0, max_size, 0, 0, color='r', linewidth=5)
ax.quiver(0, 0, 0, 0, max_size, 0, color='g', linewidth=5)
ax.quiver(0, 0, 0, 0, 0, max_size, color='b', linewidth=5)
block_depth = y * 0.4
ax.bar3d(-max_size, (y_cube - block_depth) * 0.5, z * 0.8, max_size, block_depth, z * 0.8, color='k', alpha=.25)
ax.bar3d(x_cube, (y_cube - block_depth) * 0.5, z * 0.8, max_size, block_depth, z * 0.8, color='k', alpha=.25)
ax.view_init(elev=18, azim=-130)
ax.set_xlim([0, x_cube]); ax.set_ylim([0, y_cube]); ax.set_zlim([0, z])
ax.set_aspect('auto')
if not DEBUG:
fig.savefig(picture_path, bbox_inches='tight', dpi=300, transparent=True)
else:
plt.show()
plt.close()
except Exception as e:
print(f"An error occurred: {e}")
def generate_picture_wpfin(all_group_vars, picture_path, DEBUG=False):
try:
# --- wpraw
vars = all_group_vars['wp_raw']
x, y, z = float(vars['WPR_X']), float(vars['WPR_Y']), float(vars['WPR_Z'])
if min(x, y, z) <= 0:
raise ValueError("Dimensions must be positive.")
fig, ax = plt.subplots(subplot_kw={'projection': '3d'})
x_cube, y_cube = 2 * x, 2 * y
vertices = np.array([[0, 0, 0], [x_cube, 0, 0], [x_cube, y_cube, 0], [0, y_cube, 0],
[0, 0, z], [x_cube, 0, z], [x_cube, y_cube, z], [0, y_cube, z]])
faces = [vertices[[0, 1, 2, 3]], vertices[[4, 5, 6, 7]], vertices[[0, 3, 7, 4]],
vertices[[1, 2, 6, 5]], vertices[[0, 1, 5, 4]], vertices[[2, 3, 7, 6]]]
ax.add_collection3d(Poly3DCollection(faces, facecolors='k', linewidths=1, edgecolors='k', alpha=.15))
ax.set_box_aspect([x_cube, y_cube, z])
ax.set_xticks([]);
ax.set_yticks([]);
ax.set_zticks([])
ball_radius = min(x, y, z) * 0.1
u, v = np.linspace(0, 2 * np.pi, 20), np.linspace(0, np.pi, 20)
x_sphere = x + ball_radius * np.outer(np.cos(u), np.sin(v))
y_sphere = y + ball_radius * np.outer(np.sin(u), np.sin(v))
z_sphere = z + ball_radius * np.outer(np.ones_like(u), np.cos(v))
ax.plot_surface(x_sphere, y_sphere, z_sphere, color='r', alpha=1)
# --- wprfin
vars = all_group_vars['wp_fin']
x, y, z, l = float(vars['x_wpfin']), float(vars['y_wpfin']), float(vars['z_wpfin']), float(vars['l_wpfin'])
if min(x, y, z, l) <= 0:
raise ValueError("Dimensions must be positive.")
fig, ax = plt.subplots(subplot_kw={'projection': '3d'})
x_offset = x - l / 2
vertices = np.array([[0, 0, 0], [l, 0, 0], [l, 2 * y, 0], [0, 2 * y, 0],
[0, 0, z], [l, 0, z], [l, 2 * y, z], [0, 2 * y, z]]) + np.array([x_offset, 0, 0])
faces = [vertices[[0, 1, 2, 3]], vertices[[4, 5, 6, 7]], vertices[[0, 3, 7, 4]],
vertices[[1, 2, 6, 5]], vertices[[0, 1, 5, 4]], vertices[[2, 3, 7, 6]]]
ax.add_collection3d(Poly3DCollection(faces, facecolors='k', linewidths=1, edgecolors='k', alpha=.15))
ball_radius = min(x, y, z) * 0.1
u, v = np.linspace(0, 2 * np.pi, 20), np.linspace(0, np.pi, 20)
x_sphere = x + ball_radius * np.outer(np.cos(u), np.sin(v))
y_sphere = y + ball_radius * np.outer(np.sin(u), np.sin(v))
z_sphere = z + ball_radius * np.outer(np.ones_like(u), np.cos(v))
ax.plot_surface(x_sphere, y_sphere, z_sphere, color='blue', alpha=1)
max_size = max(l, 2 * y, z) * 0.3
ax.quiver(0, 0, 0, max_size, 0, 0, color='red', linewidth=5)
ax.quiver(0, 0, 0, 0, max_size, 0, color='green', linewidth=5)
ax.quiver(0, 0, 0, 0, 0, max_size, color='blue', linewidth=5)
block_depth = y * 0.4
ax.bar3d(-max_size + x_offset, (2 * y - block_depth) * 0.5, z * 0.8, max_size, block_depth, z * 0.8,
color='black', alpha=.25)
ax.bar3d(l + x_offset, (2 * y - block_depth) * 0.5, z * 0.8, max_size, block_depth, z * 0.8, color='black',
alpha=.25)
ax.view_init(elev=18, azim=-130)
ax.set_xticks([]);
ax.set_yticks([]);
ax.set_zticks([])
ax.set_xlim([0, l + x_offset]);
ax.set_ylim([0, 2 * y]);
ax.set_zlim([0, z])
ax.set_aspect('equal')
if not DEBUG:
fig.savefig(picture_path, bbox_inches='tight', dpi=300, transparent=True)
else:
plt.show()
plt.close()
except Exception as e:
print(f"An error occurred: {e}")
if __name__ == "__main__":
vars = {'WPR_X': 20, 'WPR_Y': 15, 'WPR_Z': 20, 'WP_CLP_OFFSET_X': 20, 'WPR_GRP_OFFSET_Z': 15, 'WPF_GRP_OFFSET_Y': 20, 'WPF_GRP_OFFSET_Z': 20}
generate_picture_wp({'wp': vars}, "cfg_picture/TEST_wpraw.jpg", DEBUG=True)
# vars = {'x_wpfin': 10, 'y_wpfin': 20, 'z_wpfin': 20, 'l_wpfin': 10}
# generate_picture_wpfin({'wp_fin': vars}, "cfg_picture/TEST_wpfin.jpg", DEBUG=True)