-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
167 lines (131 loc) · 4.77 KB
/
main.py
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
import os.path
import matplotlib.pyplot as plt
import toml
import roads.curve_find as curve_find
from roads.curve_fit import fitCurves2
from roads.curves import saveCurves, loadCurves, saveReport, printCurves
from roads.svgread import loadSvg
# load config from file into parameters
def load_config(config_file_name, parameters=None):
if not os.path.exists(config_file_name):
print(f"No config file {config_file_name} found, using defaults")
return
with open(config_file_name, 'rt') as fi:
data = toml.load(fi)
if not parameters:
parameters = {}
if 'finding' not in parameters:
parameters['finding'] = {}
if 'fitting' not in parameters:
parameters['fitting'] = {}
try:
parameters['finding'].update(data['finding'])
except:
pass
try:
parameters['fitting'].update(data['fitting'])
except:
pass
if 'sequence' in data:
try:
parameters['sequence'].update(data['sequence'])
except:
parameters['sequence'] = data['sequence']
return parameters
def main(filename, parameters, try_load=True, plot_curvature=False, show=False):
curves_file_name = filename.replace('.svg', '-curves.json')
report_file_name = filename.replace('.svg', '-report.csv')
config_file_name = filename.replace('.svg', '.cfg')
image_file_name = filename.replace('.svg', '-plots.png')
curvature_image_name = filename.replace('.svg', '-curvature.png')
path = loadSvg(filename)
load_config(config_file_name, parameters)
path.makeContinuousCurvature()
print(parameters)
fig1, ax1 = plt.subplots(2 if plot_curvature else 1, 1)
fig2, ax2 = plt.subplots(2 if plot_curvature else 1, 1)
# if not plot_curvature:
ax = [ax1, ax2]
# ax[0].invert_yaxis()
ax[0].axis('equal')
path.plot('black', sampler_fn="position", show=False, axis=ax[0])
# if plot_curvature:
path.plot("r:", sampler_fn="curvature", show=False, axis=ax[1])
curves = None
loaded = False
if try_load:
try:
curves = loadCurves(path, curves_file_name)
loaded = True
except:
pass
if "sequence" in parameters:
pattern = parameters['sequence']['pattern']
print("Using pattern: ", pattern)
curves = curve_find.fromPattern(path, pattern, parameters['finding'])
else:
curves = curve_find.buildCurves(path, parameters['finding'])
print("Before fitting")
printCurves(curves)
if not loaded:
fitCurves2(curves, parameters['fitting'])
if "sequence" not in parameters:
curve_find.simplifyCurves(curves, 1, 0.5, 1)
fitCurves2(curves, parameters['fitting'])
# if not loaded:
# growArcs(curves)
saveCurves(curves, curves_file_name)
saveReport(curves, report_file_name, path)
print("After fitting")
printCurves(curves)
tot_len = 0
for i, c in enumerate(curves):
c.plot("-", axis=ax[0], label=c.legend)
tot_len += c.length
# do not display legend
# ax[0].plot([], [], ' ', label=f"Total length={tot_len:.2f}m")
# ax[0].legend()
font = {'family': 'Times New Roman',
'weight': 'normal',
'size': 16,
}
ax[0].set_xlabel('Coordinate real value', font)
ax[0].set_ylabel('Coordinate image value', font)
# if plot_curvature:
for i, c in enumerate(curves):
c.plot(axis=ax[1], plot_curvature=True, color='black')
# set legend
ax[1].plot([], [], color='black', linewidth=2.0, label='Linear fit')
ax[1].plot([], [], color='red', linewidth=2.0, linestyle='--', label='Curvature result')
ax[1].legend(fontsize=20)
font2 = {'family': 'Times New Roman',
'weight': 'normal',
'size': 20,
}
ax[1].set_xlabel('Curvature real value (1/m)', font2)
ax[1].set_ylabel('Curvature image value (1/m)', font2)
for c in curves:
cname = c.__class__.__name__
err = c.getErrorTally()
F = plt.gcf()
Size = F.get_size_inches()
F.set_size_inches(Size[0] * 2, Size[1] * 2,
forward=True) # Set forward to True to resize window along with plot in figure
fig1.savefig(image_file_name)
fig2.savefig(curvature_image_name)
if show:
plt.show()
if __name__ == '__main__':
parameters = load_config("default.cfg")
ramps = [
"./traced/ramp1/ramp1.svg",
"./traced/ramp2/ramp2.svg",
"./traced/ramp3/ramp3.svg",
"./traced/ramp4/ramp4.svg",
"./traced/ramp5/ramp5.svg",
"./traced/ramp6/ramp6.svg",
]
for f in ramps:
name = os.path.split(f)[1]
print(f"========== Processing: {name} =========")
main(f, parameters, try_load=False, plot_curvature=False, show=False)