-
Notifications
You must be signed in to change notification settings - Fork 5
/
param.py
329 lines (284 loc) · 9.07 KB
/
param.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
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
from PyQt5.QtCore import QPointF, QSize, QRectF
import numpy as np
ORIENT0 = 0
ORIENT0F = 1
ORIENT90 = 2
ORIENT90F = 3
ORIENT180 = 4
ORIENT180F = 5
ORIENT270 = 6
ORIENT270F = 7
orient2idx = {ORIENT0: 0,
ORIENT90: 1,
ORIENT180: 2,
ORIENT270: 3,
ORIENT0F: 4,
ORIENT90F: 5,
ORIENT180F: 6,
ORIENT270F: 7}
idx2orient = { 0: ORIENT0,
1: ORIENT90,
2: ORIENT180,
3: ORIENT270,
4: ORIENT0F,
5: ORIENT90F,
6: ORIENT180F,
7: ORIENT270F}
orientation = 0
# These are all *image* variables.
x = 640
y = 480
ypad = (x - y) / 2
xpad = 0
maxd = x
# Convience indexes for plotting.
x_fwd = np.arange(x)
x_rev = np.arange(x - 1, -1, -1)
y_fwd = np.arange(y)
y_rev = np.arange(y - 1, -1, -1)
# These aren't.
projsize = 300
zoom = 1.0
def setImageSize(newx, newy):
global x, y, xpad, ypad, maxd, x_fwd, x_rev, y_fwd, y_rev
x = int(newx)
y = int(newy)
x_fwd = np.arange(x)
x_rev = np.arange(x - 1, -1, -1)
y_fwd = np.arange(y)
y_rev = np.arange(y - 1, -1, -1)
if newx >= newy:
ypad = (newx - newy) / 2
xpad = 0
maxd = newx
else:
ypad = 0
xpad = (newy - newx) / 2
maxd = newy
# Get the desired QImage size (oriented!)
def getSize():
global x, y, orientation
if orientation & 2 == 2:
return QSize(y, x)
else:
return QSize(x, y)
def getSizeTuple():
global x, y, orientation
if orientation & 2 == 2:
return (y, x)
else:
return (x, y)
def isRotated():
global orientation
return orientation & 2 == 2
def width():
global x, y, orientation
if orientation & 2 == 2:
return y
else:
return x
def height():
global x, y, orientation
if orientation & 2 == 2:
return x
else:
return y
def xpad_oriented():
global xpad, ypad, orientation
if orientation & 2 == 2:
return ypad
else:
return xpad
def ypad_oriented():
global x, y, orientation
if orientation & 2 == 2:
return xpad
else:
return ypad
class Point(object):
# Create with absolute image coordinates by default.
#
# x, y are absolute image coordinates of the point, abs() is the absolute image QPointF, and
# oriented() is the correctly oriented QPointF.
def __init__(self, xx, yy, rel=False):
self._abs = None
self._rel = None
self.orientation = -1
if rel:
self.setRel(xx, yy)
else:
self.setAbs(xx, yy)
def calcAbs(self, xx, yy):
global x, y, orientation
if orientation == ORIENT0:
self._abs = QPointF(xx, yy)
elif orientation == ORIENT0F:
self._abs = QPointF(x - 1 - xx, yy)
elif orientation == ORIENT90:
self._abs = QPointF(x - 1 - yy, xx)
elif orientation == ORIENT90F:
self._abs = QPointF(yy, xx)
elif orientation == ORIENT180:
self._abs = QPointF(x - 1 - xx, y - 1 - yy)
elif orientation == ORIENT180F:
self._abs = QPointF(xx, y - 1 - yy)
elif orientation == ORIENT270:
self._abs = QPointF(yy, y - 1 - xx)
elif orientation == ORIENT270F:
self._abs = QPointF(x - 1 - yy, y - 1 - xx)
self.x = self._abs.x()
self.y = self._abs.y()
def setRel(self, xx, yy):
global orientation
self._rel = QPointF(xx, yy)
self.orientation = orientation
self.calcAbs(xx, yy)
def setAbs(self, xx, yy):
self.x = xx
self.y = yy
self._abs = QPointF(xx, yy)
self._rel = None
def abs(self):
return self._abs
def oriented(self):
global orientation, x, y
if self.orientation == orientation and self._rel is not None:
return self._rel
self.orientation = orientation
if orientation == ORIENT0:
self._rel = QPointF(self.x, self.y)
elif orientation == ORIENT0F:
self._rel = QPointF(x - 1 - self.x, self.y)
elif orientation == ORIENT90:
self._rel = QPointF(self.y, x - 1 - self.x)
elif orientation == ORIENT90F:
self._rel = QPointF(self.y, self.x)
elif orientation == ORIENT180:
self._rel = QPointF(x - 1 - self.x, y - 1 - self.y)
elif orientation == ORIENT180F:
self._rel = QPointF(self.x, y - 1 - self.y)
elif orientation == ORIENT270:
self._rel = QPointF(y - 1 - self.y, self.x)
elif orientation == ORIENT270F:
self._rel = QPointF(y - 1 - self.y, x - 1 - self.x)
return self._rel
def pr(self, text=""):
pt = self.oriented()
print("%sabs(%g,%g) rel(%g,%g)" % (text, self.x, self.y, pt.x(), pt.y()))
class Rect(object):
# Create with absolute image coordinates by default
def __init__(self, xx, yy, ww, hh, rel=False):
self._abs = None
self._rel = None
self.orientation = -1
if rel:
self.setRel(xx, yy, ww, hh)
else:
self.setAbs(xx, yy, ww, hh)
def setRel(self, xx, yy, ww, hh):
global orientation
if ww < 0:
xx = xx + ww + 1
ww = -ww
if hh < 0:
yy = yy + hh + 1
hh = hh
self._rel = QRectF(xx, yy, ww, hh)
self.orientation = orientation
self.calcAbs(xx, yy, ww, hh)
def setAbs(self, xx, yy, ww, hh):
if ww >= 0:
self.x = xx
self.w = ww
else:
self.x = xx + ww + 1
self.w = -ww
if hh >= 0:
self.y = yy
self.h = hh
else:
self.y = yy + hh + 1
self.h = hh
self._abs = QRectF(self.x, self.y, self.w, self.h)
self._rel = None
def calcAbs(self, xx, yy, ww, hh):
global orientation
if orientation == ORIENT0:
self._abs = QRectF(xx, yy, ww, hh)
elif orientation == ORIENT0F:
self._abs = QRectF(x - xx - ww, yy, ww, hh)
elif orientation == ORIENT90:
self._abs = QRectF(x - yy - hh, xx, hh, ww)
elif orientation == ORIENT90F:
self._abs = QRectF(yy, xx, hh, ww)
elif orientation == ORIENT180:
self._abs = QRectF(x - xx - ww, y - yy - hh, ww, hh)
elif orientation == ORIENT180F:
self._abs = QRectF(xx, y - yy - hh, ww, hh)
elif orientation == ORIENT270:
self._abs = QRectF(yy, y - xx - ww, hh, ww)
elif orientation == ORIENT270F:
self._abs = QRectF(x - yy - hh, y - xx - ww, hh, ww)
self.x = self._abs.x()
self.y = self._abs.y()
self.w = self._abs.width()
self.h = self._abs.height()
def abs(self):
return self._abs
def oriented(self):
global orientation, x, y
if self.orientation == orientation and self._rel is not None:
return self._rel
self.orientation = orientation
if orientation == ORIENT0:
self._rel = QRectF(self.x, self.y, self.w, self.h)
elif orientation == ORIENT0F:
self._rel = QRectF(x - self.x - self.w, self.y, self.w, self.h)
elif orientation == ORIENT90:
self._rel = QRectF(self.y, x - self.x - self.w, self.h, self.w)
elif orientation == ORIENT90F:
self._rel = QRectF(self.y, self.x, self.h, self.w)
elif orientation == ORIENT180:
self._rel = QRectF(x - self.x - self.w, y - self.y - self.h, self.w, self.h)
elif orientation == ORIENT180F:
self._rel = QRectF(self.x, y - self.y - self.h, self.w, self.h)
elif orientation == ORIENT270:
self._rel = QRectF(y - self.y - self.h, self.x, self.h, self.w)
elif orientation == ORIENT270F:
self._rel = QRectF(y - self.y - self.h, x - self.x - self.w, self.h, self.w)
return self._rel
# These all set in *oriented* coordinates!
def setLeft(self, xx):
pt = self.oriented()
self.setRel(xx, pt.y(), pt.width(), pt.height())
def setRight(self, xx):
pt = self.oriented()
self.setRel(pt.x(), pt.y(), xx - pt.x() + 1, pt.height())
def setTop(self, yy):
pt = self.oriented()
self.setRel(pt.x(), yy, pt.width(), pt.height())
def setBottom(self, yy):
pt = self.oriented()
self.setRel(pt.x(), pt.y(), pt.width(), yy - pt.y() + 1)
def setWidth(self, ww):
pt = self.oriented()
self.setRel(pt.x(), pt.y(), ww, pt.height())
def setHeight(self, hh):
pt = self.oriented()
self.setRel(pt.x(), pt.y(), pt.width(), hh)
def pr(self, text=""):
pt = self.oriented()
print(
"%sabs(%g,%g,%g,%g) rel(%g,%g,%g,%g)"
% (
text,
self.x,
self.y,
self.w,
self.h,
pt.x(),
pt.y(),
pt.width(),
pt.height(),
)
)