-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy patharea.py
executable file
·145 lines (117 loc) · 4.72 KB
/
area.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import itertools
import numpy as np
import copy
def shift(vertical_shift, horizontal_shift):
s = np.array([[1., 0., vertical_shift],
[0., 1., horizontal_shift],
[0., 0., 1.]])
return s
def scale(vertical_scale, horizontal_scale):
s = np.diag([vertical_scale, horizontal_scale, 1.])
return s
class area:
def __init__(self, range_y=1, range_x=1, rows=3, columns=1, center_y=0, center_x=0):
self.range_y = range_y
self.range_x = range_x
self.rows = int(rows)
self.columns = int(columns)
self.center_x = center_x
self.center_y = center_y
self.start_y = center_y - range_y/2.
self.start_x = center_x - range_x/2.
self.end_y = center_y + range_y/2.
self.end_x = center_x + range_x/2.
start = np.array([self.start_y, self.start_x])
end = np.array([self.end_y, self.end_x])
self.extent = end - start
self.center = start + self.extent/2.
self.shape = (self.rows, self.columns)
def get_grid_and_points(self):
vertical = np.linspace(0, 1, self.rows)
horizontal = np.linspace(0, 1, self.columns)
positions = itertools.product(vertical, horizontal)
points = np.array([np.array(position) for position in positions])
points = np.hstack([points, np.ones((points.shape[0], 1))])
points = np.dot(shift(-0.5, -0.5), points.T).T
points = np.dot(scale(*self.extent), points.T).T
points = np.dot(shift(*self.center), points.T).T
points[:, 2] += np.arange(points.shape[0])
indexes = list(map(int, points[:, 2]))
points = dict(zip(indexes, points[:, :2]))
grid = np.reshape(indexes, (self.rows, self.columns))
return grid, points
def get_position_sequence(self, grid):
return grid.ravel()
def get_jump_sequence(self, grid, against_gravity=False):
if against_gravity:
jump_sequence = zip(grid[:, -1], grid[:, 0])
else:
jump_sequence = zip(grid[:, 0], grid[:, -1])
return jump_sequence
def get_horizontal_raster(self, grid, start=1):
g = copy.deepcopy(grid)
if np.__version__ >= '1.8.3':
g[start::2, ::] = g[start::2, ::-1]
else:
raster = []
for k, line in enumerate(g):
if k % 2 == 1:
line = line[::-1]
raster.append(line)
g = np.array(raster)
return g
def get_vertical_raster(self, grid, start=1):
g = copy.deepcopy(grid)
if np.__version__ >= '1.8.3':
g[::, start::2] = g[::-1, start::2]
else:
g = self.get_horizontal_raster(g.T).T
return g
def get_linearized_point_jumps(self, jumps, points):
return [(points[jump[0]], points[jump[1]]) for jump in jumps]
def test():
import optparse
parser = optparse.OptionParser()
parser.add_option('-y', '--range_y', default=1, type=float, help='Vertical scan range')
parser.add_option('-x', '--range_x', default=1, type=float, help='Horizontal scan range')
parser.add_option('-r', '--rows', default=5, type=int, help='Number of rows')
parser.add_option('-c', '--columns', default=3, type=int, help='Number of columns')
parser.add_option('-a', '--center_y', default=1, type=float, help='Vertical origin')
parser.add_option('-b', '--center_x', default=1, type=float, help='Horizontal origin')
options, args = parser.parse_args()
a = area(**vars(options))
grid, points = a.get_grid_and_points()
print('grid')
print(grid)
print('grid.T')
print(grid.T)
print('points')
print(points)
vr = a.get_vertical_raster(grid)
hr = a.get_horizontal_raster(grid)
print('vertical raster')
print(vr)
print('vertical raster linearized positions')
print(a.get_position_sequence(vr.T))
print('vertical raster linearized jumps')
vertical_jumps = a.get_jump_sequence(vr.T)
print(vertical_jumps)
print(a.get_linearized_point_jumps(vertical_jumps, points))
print('horizontal raster')
print(hr)
print('horizontal raster linearized positions')
print(a.get_position_sequence(hr))
print('horizontal raster linearized jumps')
horizontal_jumps = a.get_jump_sequence(hr)
print(horizontal_jumps)
print(a.get_linearized_point_jumps(horizontal_jumps, points))
def main():
import sys
if len(sys.argv) > 0:
what = sys.argv[1]
else:
print('Please specify what should I aling')
if __name__ == '__main__':
test()