-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathchip.py
193 lines (176 loc) · 5.57 KB
/
chip.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
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
import numpy as np
class chip:
def __init__(self):
# origin
self._home_x = 0.0
self._home_y = 0.0
# max value for X Y
self._end_x = 0.0
self._end_y = 0.0
# steps
self.steps = 0.0
def __len__(self):
X, Y = self._generate_mesh()
return len(X) * len(Y)
def _generate_mesh(self):
"""
Generate the base mesh. Based on the relative positions and the steps
"""
if self._home_x > self._end_x:
x_steps = -self.steps
else:
x_steps = self.steps
x = np.arange(self._home_x, self._end_x + x_steps, x_steps)
if self._home_y > self._end_y:
y_steps = -self.steps
else:
y_steps = self.steps
y = np.arange(self._home_y, self._end_y + y_steps, y_steps)
X, Y = np.meshgrid(x, y)
for i in range(len(X)):
if i % 2:
X[i] = X[i][::-1]
return X, Y
def get_conf(self):
"""
Return the current chip configuration
"""
return f"""[chip]
home_x = {self._home_x}
home_y = {self._home_y}
end_x = {self._end_x}
end_y = {self._end_y}
steps = {self.steps}"""
def set_home(self, x, y):
"""
Set home X and Y coordinates
"""
self._home_x = x
self._home_y = y
def set_end(self, x, y):
"""
Set end (ie. opposite corner) X and Y coordinates
"""
self._end_x = x
self._end_y = y
def random(self):
"""
Randomly test all positions
"""
X, Y = self._generate_mesh()
coordinates = np.vstack([X.ravel(), Y.ravel()]).T
coordinates = np.random.permutation(coordinates)
for x, y in coordinates:
yield (x, y)
def horizontal(self):
"""
Scan region horizontally (X) first
"""
###############
# default mode#
# 0 >->->->-v #
# 1 v-<-<-<-< #
# 2 >->->->-> #
###############
it = 0
y_start = self._home_y
while y_start <= self._end_y:
if it % 2:
x_start = self._end_x
while x_start >= self._home_x:
yield (x_start, y_start)
x_start -= self.steps
else:
x_start = self._home_x
while x_start <= self._end_x:
yield (x_start, y_start)
x_start += self.steps
y_start += self.steps
it += 1
def vertical(self):
"""
Scan region vertically (Y) first
"""
################
# vertical mode#
# 0 .>v|.>v|^>.#
# 1 ^|v|^|v|^|v#
# 2 ^|.>^|.>^|v#
################
it = 0
x_start = self._home_x
while x_start <= self._end_x:
if it % 2:
y_start = self._end_y
while y_start >= self._home_y:
yield (x_start, y_start)
y_start -= self.steps
else:
y_start = self._home_y
while y_start <= self._end_y:
yield (x_start, y_start)
y_start += self.steps
x_start += self.steps
it += 1
def spiral_inward(self):
"""
Scan region in spiral from border to the center
"""
x_start = self._home_x
y_start = self._home_y
direction = 0
x_steps = int(self._end_x / self.steps)
y_steps = int(self._end_y / self.steps)
while x_steps > 0 or y_steps > 0:
if direction % 4 == 0:
for x in range(x_steps):
yield (x_start, y_start)
x_start += self.steps
if direction != 0:
y_steps -= 1
elif direction % 4 == 1:
for y in range(y_steps):
yield (x_start, y_start)
y_start += self.steps
if direction != 1:
x_steps -= 1
elif direction % 4 == 2:
for x in range(x_steps):
yield (x_start, y_start)
x_start -= self.steps
y_steps -= 1
elif direction % 4 == 3:
for y in range(y_steps):
yield (x_start, y_start)
y_start -= self.steps
x_steps -= 1
direction += 1
yield (x_start, y_start)
def spiral_outward(self):
"""
Scan region in spiral from the center to the outside
"""
x_start = round(self._end_x / 2.0)
y_start = round(self._end_y / 2.0)
direction = 0
nb_steps = 1
while x_start <= self._end_x and y_start <= self._end_y:
if direction % 4 == 0:
for x in range(nb_steps):
yield (x_start, y_start)
x_start -= self.steps
elif direction % 4 == 1:
for y in range(nb_steps):
yield (x_start, y_start)
y_start -= self.steps
nb_steps += 1
elif direction % 4 == 2:
for x in range(nb_steps):
yield (x_start, y_start)
x_start += self.steps
elif direction % 4 == 3:
for y in range(nb_steps):
yield (x_start, y_start)
y_start += self.steps
nb_steps += 1
direction += 1