73 lines
3.0 KiB
Python
73 lines
3.0 KiB
Python
import matplotlib.pyplot as plt
|
|
import numpy as np
|
|
|
|
def generate_picture_inlay(all_group_vars, picture_path, DEBUG=False):
|
|
try:
|
|
vars = all_group_vars['inlay']
|
|
x_num, y_num = int(vars['INL_X_NUM']), int(vars['INL_Y_NUM'])
|
|
x_offset_mm, y_offset_mm = float(vars['INL_X_OFFSET']), float(vars['INL_Y_OFFSET'])
|
|
|
|
if x_num <= 0 or y_num <= 0:
|
|
raise ValueError("INL_X_NUM and INL_Y_NUM must be positive integers.")
|
|
|
|
|
|
rect_realsize_factor = 0.90
|
|
canvas_height_mm = x_num * x_offset_mm - x_offset_mm * (1-rect_realsize_factor)
|
|
canvas_width_mm = y_num * y_offset_mm - y_offset_mm * (1-rect_realsize_factor)
|
|
|
|
x_offset = x_offset_mm / canvas_height_mm # Swapping due to axis swap
|
|
y_offset = y_offset_mm / canvas_width_mm # Swapping due to axis swap
|
|
|
|
|
|
x_wide = x_offset * rect_realsize_factor
|
|
y_wide = y_offset * rect_realsize_factor
|
|
|
|
|
|
fig, ax = plt.subplots(figsize=(12, 8))
|
|
ax.set_facecolor('black')
|
|
|
|
for xxx in range(x_num):
|
|
for yyy in range(y_num):
|
|
# Mirroring horizontally
|
|
x_start = xxx * x_offset
|
|
y_start = ((yyy) * y_offset) # Corrected vertical mirroring
|
|
|
|
rect = plt.Rectangle((y_start, x_start), y_wide, x_wide, color='white')
|
|
ax.add_patch(rect)
|
|
|
|
triangle_sizefactor = 0.2
|
|
triangle = plt.Polygon([
|
|
(y_start+y_wide, x_start), # Bottom right of the rectangle
|
|
(y_start+y_wide, x_start + triangle_sizefactor * y_wide), # Left along the bottom
|
|
(y_start+y_wide - triangle_sizefactor * y_wide, x_start ) # Up from the bottom right
|
|
], color='black')
|
|
ax.add_patch(triangle)
|
|
|
|
# Arrows with adjusted margins
|
|
ax.arrow(1.0, 0.0, -0.3, 0, head_width=0.03, head_length=0.02, fc='green', ec='green', linewidth=2, edgecolor='white')
|
|
ax.arrow(1.0, 0.0, 0, 0.3, head_width=0.02, head_length=0.03, fc='red', ec='red', linewidth=2, edgecolor='white')
|
|
|
|
margin = 0.03
|
|
ax.set_xlim(0-margin, 1+margin)
|
|
ax.set_ylim(0-margin, 1+margin)
|
|
ax.set_xticks([]), ax.set_yticks([])
|
|
# plt.subplots_adjust(left=0.01, right=0.99, top=0.99, bottom=0.01) # Adjusting the margins
|
|
# ax.set_xlim(1, 0) # Flipping the x-axis
|
|
# ax.set_ylim(1, 0) # Flipping the y-axis
|
|
|
|
if DEBUG:
|
|
plt.show()
|
|
else:
|
|
fig.savefig(picture_path, bbox_inches='tight', pad_inches=0.5, dpi=300, transparent=True) # Adjusted padding
|
|
plt.close()
|
|
|
|
except ValueError as ve:
|
|
print(f"Invalid Input: {ve}")
|
|
except Exception as e:
|
|
print(f"Error generating matrix: {e}")
|
|
|
|
if __name__ == "__main__":
|
|
vars = {'INL_X_NUM': 8, 'INL_Y_NUM': 12, 'INL_X_OFFSET': 100, 'INL_Y_OFFSET': 125}
|
|
picture_path = "cfg_picture/TEST_inlay.jpg"
|
|
generate_picture_inlay({'inlay': vars}, picture_path, DEBUG=True)
|