DMU50_GUI_PY/funct_wp.py
Eduard Gerlitz 377fb1422f .
2025-03-11 15:06:20 +01:00

198 lines
8.7 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:
# --- wpraw
vars = all_group_vars['wp_raw']
x, y, z = float(vars['x_wpraw']), float(vars['y_wpraw']), float(vars['z_wpraw'])
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}")
# --------------- 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['x_wpraw']), float(vars['y_wpraw']), float(vars['z_wpraw'])
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['x_wpraw']), float(vars['y_wpraw']), float(vars['z_wpraw'])
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 = {'x_wpraw': 10, 'y_wpraw': 15, 'z_wpraw': 20}
generate_picture_wpraw({'wp_raw': 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)