-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathvisualize_irregular.py
More file actions
155 lines (137 loc) · 4.5 KB
/
visualize_irregular.py
File metadata and controls
155 lines (137 loc) · 4.5 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
import argparse
import json
import plotly.graph_objects as go
import plotly.express as px
import plotly.subplots
import numpy as np
import math
def shape_path(path_x, path_y, shape, is_hole=False):
# How to draw a filled circle segment?
# https://community.plotly.com/t/how-to-draw-a-filled-circle-segment/59583
# https://stackoverflow.com/questions/70965145/can-plotly-for-python-plot-a-polygon-with-one-or-multiple-holes-in-it
for element in (shape if not is_hole else reversed(shape)):
t = element["type"]
xs = element["xs"]
ys = element["ys"]
xe = element["xe"]
ye = element["ye"]
if t == "CircularArc":
xc = element["xc"]
yc = element["yc"]
anticlockwise = 1 if element["anticlockwise"] else 0
rc = math.sqrt((xc - xs)**2 + (yc - ys)**2)
if is_hole:
xs, ys, xe, ye = xe, ye, xs, ys
if len(path_x) == 0 or path_x[-1] is None:
path_x.append(xs)
path_y.append(ys)
if t == "LineSegment":
path_x.append(xe)
path_y.append(ye)
elif t == "CircularArc":
start_cos = (xs - xc) / rc
start_sin = (ys - yc) / rc
start_angle = math.atan2(start_sin, start_cos)
end_cos = (xe - xc) / rc
end_sin = (ye - yc) / rc
end_angle = math.atan2(end_sin, end_cos)
if anticlockwise and end_angle <= start_angle:
end_angle += 2 * math.pi
if not anticlockwise and end_angle >= start_angle:
end_angle -= 2 * math.pi
t = np.linspace(start_angle, end_angle, 1024)
x = xc + rc * np.cos(t)
y = yc + rc * np.sin(t)
for xa, ya in zip(x[1:], y[1:]):
path_x.append(xa)
path_y.append(ya)
path_x.append(None)
path_y.append(None)
parser = argparse.ArgumentParser(description='')
parser.add_argument('csvpath', help='path to JSON file')
args = parser.parse_args()
bins_x = []
bins_y = []
defects_x = []
defects_y = []
items_x = []
items_y = []
with open(args.csvpath, 'r') as f:
j = json.load(f)
for bin_pos, solution_bin in enumerate(j["bins"]):
bins_x.append([])
bins_y.append([])
defects_x.append([])
defects_y.append([])
items_x.append([])
items_y.append([])
shape_path(bins_x[bin_pos], bins_y[bin_pos], solution_bin["shape"])
for defect in (solution_bin["defects"]
if "defects" in solution_bin else []):
shape_path(defects_x[bin_pos], defects_y[bin_pos], defect["shape"])
for hole in (defect["holes"]
if "holes" in defect else []):
shape_path(defects_x[bin_pos], defects_y[bin_pos], hole, True)
for solution_item in solution_bin["items"]:
for item_shape in solution_item["item_shapes"]:
shape_path(items_x[bin_pos],
items_y[bin_pos],
item_shape["shape"])
for hole in (item_shape["holes"]
if "holes" in item_shape else []):
shape_path(items_x[bin_pos], items_y[bin_pos], hole, True)
m = len(bins_x)
colors = px.colors.qualitative.Plotly
fig = plotly.subplots.make_subplots(
rows=m,
cols=1,
shared_xaxes=True,
vertical_spacing=0.001)
for i in range(0, m):
fig.add_trace(go.Scatter(
x=bins_x[i],
y=bins_y[i],
name="Bins",
legendgroup="bins",
showlegend=(i == 0),
marker=dict(
color='black',
size=1)),
row=i + 1,
col=1)
fig.add_trace(go.Scatter(
x=defects_x[i],
y=defects_y[i],
name="Defects",
legendgroup="defects",
showlegend=(i == 0),
fillcolor="crimson",
fill="toself",
marker=dict(
color='black',
size=1)),
row=i + 1,
col=1)
fig.add_trace(go.Scatter(
x=items_x[i],
y=items_y[i],
name="Items",
legendgroup="items",
showlegend=(i == 0),
fillcolor="cornflowerblue",
fill="toself",
marker=dict(
color='black',
size=1)),
row=i + 1,
col=1)
# Plot.
fig.update_layout(
autosize=True,
height=m*1000)
fig.update_xaxes(
rangeslider=dict(visible=False))
fig.update_yaxes(
scaleanchor="x",
scaleratio=1)
fig.show()