-
Notifications
You must be signed in to change notification settings - Fork 1
/
mylib_axis.py
executable file
·277 lines (252 loc) · 9.25 KB
/
mylib_axis.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
#!/usr/bin/env python
#
# Author: Patrick Brockmann
# Contact: [email protected]
# $Date: $
# $Name: $
# $Revision: $
# History:
# Modification:
#
import vtk
import __main__
from mylib_text import *
# ********************************
axisxoffset = 0.03
axisxticksize = 0.02
axisyoffset = 0.03
axisyticksize = 0.02
axisxwidth = 720.0
axisxstart = -180.0
axisywidth = 180.0
axisystart = -90.0
# **************************************
def CreateViewportBox(colorfg, linewidth=3):
pts = vtk.vtkPoints()
pts.SetNumberOfPoints(4)
pts.SetPoint(0, 0.0, 0.0, 0.0)
pts.SetPoint(1, 0.0, 1.0, 0.0)
pts.SetPoint(2, 1.0, 1.0, 0.0)
pts.SetPoint(3, 1.0, 0.0, 0.0)
lines = vtk.vtkCellArray()
lines.InsertNextCell(5)
lines.InsertCellPoint(0)
lines.InsertCellPoint(1)
lines.InsertCellPoint(2)
lines.InsertCellPoint(3)
lines.InsertCellPoint(0)
box = vtk.vtkPolyData()
box.SetPoints(pts)
box.SetLines(lines)
coords = vtk.vtkCoordinate()
coords.SetCoordinateSystemToNormalizedViewport()
boxmapper = vtk.vtkPolyDataMapper2D()
boxmapper.SetInputData(box)
boxmapper.SetTransformCoordinate(coords)
boxactor = vtk.vtkActor2D()
boxactor.SetMapper(boxmapper)
boxactor.GetProperty().SetLineWidth(linewidth)
boxactor.GetProperty().SetColor(colorfg)
return boxactor
# **************************************
def GetPrecision(x):
x = abs(x)
fractionpart = x-int(x)
if fractionpart != 0:
precision = len(str(fractionpart))-2
else:
precision = 0
return precision
# **************************************
def CreateAxisTickActor(xstart, ystart, xend, yend, colorfg):
line = vtk.vtkLineSource()
line.SetPoint1(xstart, ystart, 0.0)
line.SetPoint2(xend, yend, 0.0)
coords = vtk.vtkCoordinate()
coords.SetCoordinateSystemToNormalizedViewport()
linemapper = vtk.vtkPolyDataMapper2D()
linemapper.SetInputConnection(line.GetOutputPort())
linemapper.SetTransformCoordinate(coords)
tick = vtk.vtkActor2D()
tick.SetMapper(linemapper)
tick.GetProperty().SetLineWidth(1)
tick.GetProperty().SetColor(colorfg)
tick.GetPositionCoordinate().SetCoordinateSystemToNormalizedViewport()
return tick
# **************************************
listlonaxistextactors = []
listlonaxistickuactors = []
listlonaxistickdactors = []
listlonaxispos = []
listlonaxisvisibility = []
def InitLonAxisTicks():
del listlonaxistextactors[:]
del listlonaxistickuactors[:]
del listlonaxistickdactors[:]
del listlonaxispos[:]
del listlonaxisvisibility[:]
def CreateLonAxisTicks(axisxticknb, textprop):
colorfg = textprop.GetColor()
xdelta = axisxwidth/(axisxticknb-1)
xdeltaprecision = GetPrecision(xdelta)
xpostextformat = '%.'+str(xdeltaprecision)+'f'
for i in range(int(axisxticknb)):
xpos = axisxstart+xdelta*i
xpostext = xpostextformat % xpos
listlonaxistextactors.append(CreateTextActor(xpostext, textprop, 0, 0,
justification=0, verticaljustification=1))
listlonaxistickuactors.append(
CreateAxisTickActor(0, 0, 0, axisxticksize, colorfg))
listlonaxistickdactors.append(
CreateAxisTickActor(0, 0, 0, axisxticksize, colorfg))
listlonaxispos.append(xpos)
listlonaxisvisibility.append(1)
def DelLonAxisTicks(ren0):
for i in range(len(listlonaxistextactors)):
ren0.RemoveActor(listlonaxistextactors[i])
ren0.RemoveActor(listlonaxistickdactors[i])
ren0.RemoveActor(listlonaxistickuactors[i])
InitLonAxisTicks()
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
listlataxistextactors = []
listlataxisticklactors = []
listlataxistickractors = []
listlataxispos = []
listlataxisvisibility = []
def InitLatAxisTicks():
del listlataxistextactors[:]
del listlataxisticklactors[:]
del listlataxistickractors[:]
del listlataxispos[:]
del listlataxisvisibility[:]
def CreateLatAxisTicks(axisyticknb, textprop):
colorfg = textprop.GetColor()
ydelta = axisywidth/(axisyticknb-1)
ydeltaprecision = GetPrecision(ydelta)
ypostextformat = '%.'+str(ydeltaprecision)+'f'
for i in range(int(axisyticknb)):
ypos = axisystart+ydelta*i
ypostext = ypostextformat % (ypos)
listlataxistextactors.append(CreateTextActor(ypostext, textprop, 0, 0,
justification=1, verticaljustification=0))
listlataxistickractors.append(
CreateAxisTickActor(0, 0, axisyticksize, 0, colorfg))
listlataxisticklactors.append(
CreateAxisTickActor(0, 0, axisyticksize, 0, colorfg))
listlataxispos.append(ypos)
listlataxisvisibility.append(1)
def DelLatAxisTicks(ren0):
for i in range(len(listlataxistextactors)):
ren0.RemoveActor(listlataxistextactors[i])
ren0.RemoveActor(listlataxisticklactors[i])
ren0.RemoveActor(listlataxistickractors[i])
InitLatAxisTicks()
# -----------------------
def HandleVisibilityLonAxisTextAndTicks(renwin, ren0, ren1, text, tickb, ticku, xpos):
xsize, ysize = renwin.GetSize()
ren1xstart, ren1ystart, ren1xend, ren1yend = ren1.GetViewport()
coords = vtk.vtkCoordinate()
coords.SetCoordinateSystemToWorld()
coords.SetValue(xpos, 0.0, 0.0)
x1, y1 = coords.GetComputedDoubleViewportValue(ren1)
renxpos = x1/xsize+ren1xstart
if renxpos > ren1xstart and renxpos < ren1xend:
ren0.AddActor(text)
text.SetPosition(renxpos, ren1ystart-axisxoffset)
ren0.AddActor(tickb)
tickb.SetPosition(renxpos, ren1ystart-axisxticksize)
ren0.AddActor(ticku)
ticku.SetPosition(renxpos, ren1yend)
visibility = 1
else:
ren0.RemoveActor(text)
ren0.RemoveActor(tickb)
ren0.RemoveActor(ticku)
visibility = 0
return visibility
# -----------------------
def HandleVisibilityLatAxisTextAndTicks(renwin, ren0, ren1, text, tickl, tickr, ypos):
xsize, ysize = renwin.GetSize()
ren1xstart, ren1ystart, ren1xend, ren1yend = ren1.GetViewport()
coords = vtk.vtkCoordinate()
coords.SetCoordinateSystemToWorld()
coords.SetValue(0.0, ypos, 0.0)
x1, y1 = coords.GetComputedDoubleViewportValue(ren1)
renypos = y1/ysize+ren1ystart
if renypos > ren1ystart and renypos < ren1yend:
ren0.AddActor(text)
text.SetPosition(ren1xstart-axisyoffset, renypos)
ren0.AddActor(tickl)
tickl.SetPosition(ren1xstart-axisyticksize, renypos)
ren0.AddActor(tickr)
tickr.SetPosition(ren1xend, renypos)
visibility = 1
else:
ren0.RemoveActor(text)
ren0.RemoveActor(tickl)
ren0.RemoveActor(tickr)
visibility = 0
return visibility
# -----------------------
def CalculNbTicks(nbticks, axiswidth, way):
# to switch delta from 30 to 10
# from 5 to 2
axisdelta_actual = axiswidth/(nbticks-1)
if way == 'closer':
if axisdelta_actual == 30:
axisdelta_new = 10
elif axisdelta_actual == 5:
axisdelta_new = 2
else:
axisdelta_new = axisdelta_actual/2
elif way == 'wider':
if axisdelta_actual == 10:
axisdelta_new = 30
elif axisdelta_actual == 2:
axisdelta_new = 5
else:
axisdelta_new = axisdelta_actual*2
nbticks = axiswidth/axisdelta_new+1
return nbticks
# -----------------------
def DisplayAxisTextAndTicks(renwin, ren0, ren1,
axisxticknb, axisyticknb, textprop, ratioxy):
if len(listlonaxistextactors) != axisxticknb:
DelLonAxisTicks(ren0)
CreateLonAxisTicks(axisxticknb, textprop)
for i in range(len(listlonaxistextactors)):
listlonaxisvisibility[i] = HandleVisibilityLonAxisTextAndTicks(
renwin, ren0, ren1,
listlonaxistextactors[i],
listlonaxistickdactors[i],
listlonaxistickuactors[i],
listlonaxispos[i])
if listlonaxisvisibility.count(1) < 4:
DelLonAxisTicks(ren0)
axisxticknb = CalculNbTicks(axisxticknb, axisxwidth, way='closer')
CreateLonAxisTicks(axisxticknb, textprop)
elif listlonaxisvisibility.count(1) > 11:
DelLonAxisTicks(ren0)
axisxticknb = CalculNbTicks(axisxticknb, axisxwidth, way='wider')
CreateLonAxisTicks(axisxticknb, textprop)
axisxticknb = len(listlonaxistextactors)
if len(listlataxistextactors) != axisyticknb:
DelLatAxisTicks(ren0)
CreateLatAxisTicks(axisyticknb, textprop)
for i in range(len(listlataxistextactors)):
listlataxisvisibility[i] = HandleVisibilityLatAxisTextAndTicks(
renwin, ren0, ren1,
listlataxistextactors[i],
listlataxisticklactors[i],
listlataxistickractors[i],
listlataxispos[i]*ratioxy)
if listlataxisvisibility.count(1) < 4:
DelLatAxisTicks(ren0)
axisyticknb = CalculNbTicks(axisyticknb, axisywidth, way='closer')
CreateLatAxisTicks(axisyticknb, textprop)
elif listlataxisvisibility.count(1) > 11:
DelLatAxisTicks(ren0)
axisyticknb = CalculNbTicks(axisyticknb, axisywidth, way='wider')
CreateLatAxisTicks(axisyticknb, textprop)
axisyticknb = len(listlataxistextactors)
return axisxticknb, axisyticknb