-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.py
More file actions
266 lines (219 loc) · 8.72 KB
/
code.py
File metadata and controls
266 lines (219 loc) · 8.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
import numpy as np
import matplotlib.pyplot as plt
import svgwrite
import cairosvg
import cv2
import os
from sklearn.linear_model import LinearRegression
from scipy.spatial import distance
from scipy.optimize import minimize
from skimage import filters, measure, morphology, util
from skimage.feature import canny
from sklearn.cluster import DBSCAN
from IPython.display import display, Image, SVG
# Provided functions
def read_csv(csv_path):
np_path_XYs = np.genfromtxt(csv_path, delimiter=',')
path_XYs = []
for i in np.unique(np_path_XYs[:, 0]):
npXYs = np_path_XYs[np_path_XYs[:, 0] == i][:, 1:]
XYs = []
for j in np.unique(npXYs[:, 0]):
XY = npXYs[npXYs[:, 0] == j][:, 1:]
XYs.append(XY)
path_XYs.append(XYs)
return path_XYs
def plot(paths_XYs, output_path=None):
fig, ax = plt.subplots(tight_layout=True, figsize=(8, 8))
colours = ['r', 'g', 'b', 'c', 'm', 'y', 'k']
for i, XYs in enumerate(paths_XYs):
c = colours[i % len(colours)]
for XY in XYs:
ax.plot(XY[:, 0], XY[:, 1], c=c, linewidth=2)
ax.set_aspect('equal')
if output_path:
plt.savefig(output_path)
plt.close()
display(Image(filename=output_path))
else:
plt.show()
def polylines2svg(paths_XYs, svg_path):
W, H = 0, 0
for path_XYs in paths_XYs:
for XY in path_XYs:
W, H = max(W, np.max(XY[:, 0])), max(H, np.max(XY[:, 1]))
padding = 0.1
W, H = int(W + padding * W), int(H + padding * H)
W = max(W, 1)
H = max(H, 1)
dwg = svgwrite.Drawing(svg_path, profile='tiny', shape_rendering='crispEdges')
group = dwg.g()
colours = ['red', 'green', 'blue', 'cyan', 'magenta', 'yellow', 'black']
for i, path in enumerate(paths_XYs):
path_data = []
c = colours[i % len(colours)]
for XY in path:
path_data.append(("M", (XY[0, 0], XY[0, 1])))
for j in range(1, len(XY)):
path_data.append(("L", (XY[j, 0], XY[j, 1])))
if not np.allclose(XY[0], XY[-1]):
path_data.append(("Z", None))
group.add(dwg.path(d=path_data, fill='none', stroke=c, stroke_width=2))
dwg.add(group)
dwg.save()
png_path = svg_path.replace('.svg', '.png')
fact = max(1, 1024 // min(H, W))
cairosvg.svg2png(url=svg_path, write_to=png_path,
parent_width=W, parent_height=H,
output_width=fact*W, output_height=fact*H,
background_color='white')
display(SVG(filename=svg_path))
display(Image(filename=png_path))
def is_closed(path):
return np.allclose(path[0], path[-1])
def path_length(path):
return np.sum(np.sqrt(np.sum(np.diff(path, axis=0)**2, axis=1)))
def is_circle(path, tolerance=0.1):
if not is_closed(path):
return False
center = np.mean(path, axis=0)
radii = np.sqrt(np.sum((path - center)**2, axis=1))
return np.std(radii) / np.mean(radii) < tolerance
def is_rectangle(path, tolerance=0.1):
if not is_closed(path) or len(path) != 5:
return False
angles = []
for i in range(4):
v1 = path[i] - path[i-1]
v2 = path[(i+1)%4] - path[i]
angle = np.abs(np.degrees(np.arctan2(np.cross(v1, v2), np.dot(v1, v2))))
angles.append(angle)
return np.all(np.abs(np.array(angles) - 90) < tolerance)
def find_symmetry_axis(path):
center = np.mean(path, axis=0)
angles = np.linspace(0, np.pi, 180)
best_score = float('inf')
best_angle = None
for angle in angles:
rot_matrix = np.array([[np.cos(angle), -np.sin(angle)],
[np.sin(angle), np.cos(angle)]])
rotated = np.dot(path - center, rot_matrix) + center
score = np.sum(np.min(distance.cdist(path, rotated[::-1]), axis=1))
if score < best_score:
best_score = score
best_angle = angle
return best_angle
def image_to_polylines(image_path):
img = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
edges = canny(img, sigma=2)
edges = morphology.dilation(edges, morphology.disk(1))
contours = measure.find_contours(edges, 0.8)
polylines = []
for contour in contours:
contour = measure.approximate_polygon(contour, tolerance=2.0)
polylines.append([contour])
return polylines
def regularize_isolated(paths_XYs):
regularized = []
for path in paths_XYs:
if is_circle(path[0]):
center = np.mean(path[0], axis=0)
radius = np.mean(np.sqrt(np.sum((path[0] - center)**2, axis=1)))
theta = np.linspace(0, 2*np.pi, 100)
circle = np.column_stack([radius*np.cos(theta), radius*np.sin(theta)]) + center
regularized.append([circle])
elif is_rectangle(path[0]):
corners = path[0][:4]
regularized.append([np.vstack([corners, corners[0]])])
else:
regularized.append(path)
return regularized
def regularize_fragmented(paths_XYs):
return regularize_isolated(paths_XYs)
def complete_curve(start, end, points):
X = points[:, 0]
y = points[:, 1]
poly = np.poly1d(np.polyfit(X, y, 2))
new_X = np.linspace(start[0], end[0], 100)
new_y = poly(new_X)
new_points = np.column_stack([new_X, new_y])
return new_points
def complete_connected_occlusion(paths_XYs):
completed = []
for path in paths_XYs:
if is_closed(path[0]):
completed.append(path)
else:
start = path[0][0]
end = path[0][-1]
points = path[0]
completed_path = np.vstack([points, complete_curve(start, end, points)])
completed.append([completed_path])
return completed
def complete_disconnected_occlusion(paths_XYs):
completed = []
for i in range(0, len(paths_XYs), 2):
if i+1 < len(paths_XYs):
path1, path2 = paths_XYs[i][0], paths_XYs[i+1][0]
start = path1[-1]
end = path2[0]
completed_path = np.vstack([path1, complete_curve(start, end, np.vstack([path1, path2])), path2])
completed.append([completed_path])
else:
completed.append(paths_XYs[i])
return completed
def process_curvetopia(input_type, input_paths):
results = []
if input_type == 'isolated':
for path in input_paths:
if path.lower().endswith(('.png', '.jpg', '.jpeg')):
paths_XYs = image_to_polylines(path)
else:
paths_XYs = read_csv(path)
result = regularize_isolated(paths_XYs)
results.append(result)
output_base = os.path.splitext(path)[0]
plot(result, f"{output_base}_output.png")
polylines2svg(result, f"{output_base}_output.svg")
elif input_type == 'fragmented':
for path in input_paths:
paths_XYs = read_csv(path)
# print(f"Paths_XYs from {path}: {paths_XYs}") # Debug print
result = regularize_fragmented(paths_XYs)
# print(f"Regularized result: {result}") # Debug print
results.append(result)
output_base = os.path.splitext(path)[0]
plot(result, f"{output_base}_output.png")
polylines2svg(result, f"{output_base}_output.svg")
elif input_type == 'connected_occlusion':
for path in input_paths:
paths_XYs = read_csv(path)
result = complete_connected_occlusion(paths_XYs)
results.append(result)
output_base = os.path.splitext(path)[0]
plot(result, f"{output_base}_output.png")
polylines2svg(result, f"{output_base}_output.svg")
elif input_type == 'disconnected_occlusion':
for path in input_paths:
paths_XYs = read_csv(path)
result = complete_disconnected_occlusion(paths_XYs)
results.append(result)
output_base = os.path.splitext(path)[0]
plot(result, f"{output_base}_output.png")
polylines2svg(result, f"{output_base}_output.svg")
else:
raise ValueError("Invalid input type")
return results
# Example usage
if __name__ == "__main__":
isolated_input = ['/content/isolated.csv']
process_curvetopia('isolated', isolated_input)
fragmented_inputs = ['/content/frag0.csv', '/content/frag1.csv', '/content/frag2.csv']
process_curvetopia('fragmented', fragmented_inputs)
connected_occlusion_input = ['/content/occlusion1.csv']
process_curvetopia('connected_occlusion', connected_occlusion_input)
disconnected_occlusion_input = ['/content/occlusion2.csv']
process_curvetopia('disconnected_occlusion', disconnected_occlusion_input)
# Example with image input
image_input = ['/content/image.png']
process_curvetopia('isolated', image_input)