The provided Python script is designed to detect, regularize, and analyze the symmetry of various geometric shapes (such as circles, ellipses, rectangles, squares, stars, and straight lines) based on point data read from a CSV file. Here's a detailed explanation of how the script regularizes rectangles and squares and finds the symmetry of all detected shapes.
1. Regularizing Rectangles and Squares
Regularization involves transforming the detected shape's points into a standardized or "perfect" geometric form. For rectangles and squares, this process ensures that the shapes adhere strictly to their geometric definitions (e.g., all angles are 90 degrees, sides are equal in the case of squares).
a. Regularizing Rectangles
The regularize_rectangle function transforms the input points of a detected rectangle into a perfect axis-aligned rectangle. Here's how it works:
def regularize_rectangle(XY):
"""Regularizes the points to form a perfect rectangle."""
x_min, y_min = np.min(XY, axis=0)
x_max, y_max = np.max(XY, axis=0)
regularized = np.array([
[x_min, y_min],
[x_max, y_min],
[x_max, y_max],
[x_min, y_max],
[x_min, y_min] # Closing the rectangle
])
return regularized
Process:
-
Bounding Box Calculation:
- Minimum Coordinates:
x_min, y_min are the smallest x and y values among the points.
- Maximum Coordinates:
x_max, y_max are the largest x and y values among the points.
-
Constructing the Rectangle:
- The regularized rectangle is constructed using the bounding box corners:
- Bottom-left:
(x_min, y_min)
- Bottom-right:
(x_max, y_min)
- Top-right:
(x_max, y_max)
- Top-left:
(x_min, y_max)
- Closing back to
(x_min, y_min) to complete the shape.
Outcome: This ensures that the rectangle is perfectly axis-aligned with sides equal to the width (x_max - x_min) and height (y_max - y_min) derived from the bounding box.
b. Regularizing Squares
The regularize_square function transforms the input points of a detected square into a perfect square. Here's the implementation:
def regularize_square(XY):
"""Regularizes the points to form a perfect square."""
x_min, y_min = np.min(XY, axis=0)
x_max, y_max = np.max(XY, axis=0)
side_length = min(x_max - x_min, y_max - y_min)
reg_XY = np.array([
[x_min, y_min],
[x_min + side_length, y_min],
[x_min + side_length, y_min + side_length],
[x_min, y_min + side_length],
[x_min, y_min] # Closing the square
])
return reg_XY
Process:
-
Bounding Box Calculation:
- Similar to the rectangle, it computes
x_min, y_min, x_max, and y_max.
-
Determining Side Length:
side_length is set to the smaller of the width (x_max - x_min) or height (y_max - y_min) to ensure the square's sides are equal.
-
Constructing the Square:
- The square is built starting from
(x_min, y_min) and adding side_length to both x and y coordinates to create equal-length sides.
- Points are ordered as bottom-left, bottom-right, top-right, top-left, and back to bottom-left to complete the square.
Outcome: This guarantees that the regularized square has equal-length sides and is perfectly axis-aligned.
2. Detecting and Drawing Symmetry Lines
Symmetry detection involves identifying lines that divide the shape into mirror-image halves. The script identifies symmetry lines based on the detected and regularized shape types.
a. Symmetry Calculation Based on Shape Type
The calculate_symmetry_lines function determines the symmetry lines depending on the shape type:
def calculate_symmetry_lines(XY, shape_type):
"""Calculates symmetry lines for the given shape."""
if shape_type in ["Circle", "Ellipse", "Square", "Rectangle"]:
centroid = np.mean(XY, axis=0)
if shape_type == "Circle" or shape_type == "Square":
return [
([centroid[0], np.min(XY[:, 1])], [centroid[0], np.max(XY[:, 1])]), # Vertical
([np.min(XY[:, 0]), centroid[1]], [np.max(XY[:, 0]), centroid[1]]) # Horizontal
]
elif shape_type == "Ellipse" or shape_type == "Rectangle":
return [
([np.min(XY[:, 0]), centroid[1]], [np.max(XY[:, 0]), centroid[1]]) # Horizontal
]
elif shape_type == "Star":
centroid = np.mean(XY, axis=0)
angles = np.linspace(0, 2*np.pi, 5, endpoint=False)
lines = []
for angle in angles:
x = centroid[0] + np.cos(angle) * 1000
y = centroid[1] + np.sin(angle) * 1000
lines.append(([centroid[0], centroid[1]], [x, y]))
return lines
return []
Process:
-
For Circles and Squares:
- Vertical Symmetry Line: Passes through the centroid vertically.
- Horizontal Symmetry Line: Passes through the centroid horizontally.
-
For Ellipses and Rectangles:
- Horizontal Symmetry Line: Only one horizontal line is defined as symmetry.
-
For Stars:
- The number of symmetry lines corresponds to the number of star points (typically 5 for a five-pointed star).
- Lines radiate outward from the centroid at equal angular intervals (every 72 degrees for a 5-pointed star).
-
Other Shapes:
- If the shape doesn't fall into the predefined categories, no specific symmetry lines are drawn.
b. Drawing Symmetry Lines
The draw_symmetry_lines function visualizes the calculated symmetry lines and provides informational output about the number of symmetry lines:
def draw_symmetry_lines(ax, shape, reg_XY, color):
def plot_line(ax, start, end, color):
ax.plot([start[0], end[0]], [start[1], end[1]], color=color, linestyle='--', linewidth=1)
if shape == "Ellipse":
# Draw two symmetry lines (horizontal and vertical)
...
print(f" - {shape} has 2 lines of symmetry.")
elif shape == "Rectangle":
# Draw two symmetry lines (horizontal and vertical)
...
print(f" - {shape} has 2 lines of symmetry.")
elif shape == "Star":
# Draw multiple symmetry lines based on the number of points
...
print(f" - {shape} has {num_points} lines of symmetry.")
else:
print(f" - No specific symmetry lines defined for {shape}.")
Process:
-
Ellipse and Rectangle:
- Two Symmetry Lines: Horizontal and vertical lines through the centroid.
- Visualization: Dashed lines are plotted on the regularized shape to represent symmetry.
-
Square:
- Although not explicitly shown in the snippet, squares are treated similarly to circles regarding symmetry lines, having both horizontal and vertical lines.
-
Star:
- Multiple Symmetry Lines: Equal to the number of star points (e.g., 5 for a five-pointed star).
- Visualization: Radiating lines from the centroid at regular angular intervals, representing the star's axes of symmetry.
-
Other Shapes:
- If no predefined symmetry lines exist for the shape, the script outputs a message indicating that no specific symmetry lines are defined.
Outcome: The script not only draws the symmetry lines on the plot but also prints out the number of symmetry lines for each detected shape, providing both visual and textual information about the shape's symmetry properties.
3. Integration in the Main Workflow
Here's how regularization and symmetry detection fit into the overall script workflow:
-
Reading CSV Data:
- The script reads point data from a CSV file and organizes them into paths representing different shapes.
-
Shape Detection and Regularization:
- For each set of points (
XY), the script:
- Detects the Shape Type: Using characteristics like the number of vertices, area, and circularity.
- Regularizes the Shape: Transforms the input points into a perfect geometric shape based on the detected type (e.g., perfect rectangle, square).
-
Symmetry Calculation and Visualization:
- After regularizing, the script calculates the symmetry lines based on the shape type.
- It then plots both the original and regularized shapes, overlaying the symmetry lines on the regularized shapes for visual analysis.
-
Output:
- Detected shapes and their symmetry information are printed in bold for clarity.
- The regularized shapes along with their symmetry lines are saved back to a CSV file for further use or documentation.
Conclusion
The script effectively regularizes rectangles and squares by leveraging their geometric properties—ensuring axis alignment and equal side lengths for squares. It then detects and visualizes symmetry lines based on the regularized shapes, providing both graphical and textual insights into the symmetry characteristics of each shape. This combination of regularization and symmetry analysis makes the script a powerful tool for geometric shape processing and analysis.