RefBlock / data_gen.py
marstin's picture
[Martin-dev] Add data generation script
d9154fb verified
import matplotlib.pyplot as plt
import matplotlib.patches as patches
import numpy as np
def data_generation(rounding_size=0.00, gray_value=0.5, rotation_angle=0, shape_size=0.2):
"""
Draws two symmetric upright squares on a square canvas, positioned symmetrically and rotated around center.
Parameters:
- rounding_size: float (0 to ~0.5), roundness of the soft-corner square
- gray_value: float (0 to 1), grayscale color of the squares (1=white, 0=black)
- rotation_angle: float, rotation angle in degrees (affects position only, not shape orientation)
- shape_size: float, size of the squares (0 to 1)
"""
fig, ax = plt.subplots(figsize=(6, 6))
# Colors
# color_sharp = (0.5, 0.5, 0.5)
color_sharp = (0.75, 0.75, 0.75)
color_rounded = (gray_value, gray_value, gray_value)
# Center point
cx, cy = 0.5, 0.5
# offset = 0.3
offset = 0.25
# Rotation matrix
theta = np.radians(rotation_angle)
cos_t, sin_t = np.cos(theta), np.sin(theta)
def rotate_point(x, y, cx, cy):
"""Rotate a point (x, y) around center (cx, cy) by theta radians."""
x_shifted = x - cx
y_shifted = y - cy
x_new = cos_t * x_shifted - sin_t * y_shifted + cx
y_new = sin_t * x_shifted + cos_t * y_shifted + cy
return x_new, y_new
# Left (sharp) square position
shape_size_sharp = 0.2
left_center = rotate_point(cx - offset, cy, cx, cy)
square_sharp = patches.Rectangle(
(left_center[0] - shape_size_sharp/2, left_center[1] - shape_size_sharp/2),
shape_size_sharp, shape_size_sharp,
linewidth=1, edgecolor=color_sharp, facecolor=color_sharp
)
ax.add_patch(square_sharp)
# Right (rounded) square position
right_center = rotate_point(cx + offset, cy, cx, cy)
square_rounded = patches.FancyBboxPatch(
(right_center[0] - shape_size/2, right_center[1] - shape_size/2),
shape_size, shape_size,
boxstyle=f"round,pad=0.00, rounding_size={rounding_size}",
linewidth=1, edgecolor=color_rounded, facecolor=color_rounded
)
ax.add_patch(square_rounded)
# Set limits and aspect
ax.set_xlim(0, 1)
ax.set_ylim(0, 1)
ax.set_aspect('equal')
ax.axis('off')
# Save and show
plt.savefig(
f"test_{rounding_size:.3f}_{gray_value:.2f}_{rotation_angle:.1f}_{shape_size:.2f}.png",
dpi=300, bbox_inches='tight'
)
plt.show()
for r in [0.00, 0.03, 0.06, 0.09, 0.12]:
for g in [0.75, 0.65, 0.55, 0.45, 0.35]:
for a in [-45, -22.5, 0, 22.5, 45]:
for s in [0.2, 0.25, 0.30, 0.35, 0.40]:
data_generation(
rounding_size=r,
gray_value=g,
rotation_angle=a,
shape_size=s
)