Coverage for /usr/local/lib/python3.11/dist-packages/pyrocko/gui/sparrow/elements/plates.py: 24%

139 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2023-10-06 15:01 +0000

1# https://pyrocko.org - GPLv3 

2# 

3# The Pyrocko Developers, 21st Century 

4# ---|P------/S----------~Lg---------- 

5 

6import numpy as num 

7 

8from pyrocko import table, geometry, cake 

9from pyrocko.guts import Bool, Float 

10from pyrocko.gui.qt_compat import qw, qc 

11 

12from pyrocko.dataset.tectonics import PeterBird2003 

13from pyrocko.gui import vtk_util 

14import vtk 

15from matplotlib.pyplot import cm 

16 

17from .base import Element, ElementState 

18 

19guts_prefix = 'sparrow' 

20 

21km = 1e3 

22COLOR_PLATES = (0.1, 0.0, .0) 

23 

24 

25def plates_to_points(plates): 

26 num_all_nodes = 0 

27 for plate in plates: 

28 num_all_nodes = num_all_nodes + len(plate.points) 

29 coords = num.zeros((num_all_nodes, 3)) 

30 x = [] 

31 y = [] 

32 

33 for plate in plates: 

34 num_nodes = len(plate.points) 

35 for i in range(0, num_nodes): 

36 x.append(plate.points[i][0]) 

37 y.append(plate.points[i][1]) 

38 for i in range(0, num_all_nodes): 

39 coords[i, :] = x[i], y[i], -10*km 

40 station_table = table.Table() 

41 

42 station_table.add_col(('coords', '', ('lat', 'lon', 'depth')), coords) 

43 

44 return geometry.latlondepth2xyz( 

45 station_table.get_col('coords'), 

46 planetradius=cake.earthradius) 

47 

48 

49def plates_to_color(plates): 

50 colors = [] 

51 colors_iter_map = iter(cm.terrain(num.linspace(0, 1, len(plates)))) 

52 for plate in plates: 

53 color = next(colors_iter_map) 

54 colors.append(color) 

55 return num.array(colors) 

56 

57 

58class PlatesBoundsState(ElementState): 

59 visible = Bool.T(default=True) 

60 opacity = Float.T(default=1.0) 

61 color_by_slip_type = Bool.T(default=False) 

62 lines = Bool.T(default=True) 

63 

64 def create(self): 

65 element = PlatesBoundsElement() 

66 return element 

67 

68 

69class PlatelinesPipe(object): 

70 def __init__(self, plates=None): 

71 

72 self.mapper = vtk.vtkDataSetMapper() 

73 self._polyline_grid = {} 

74 self._opacity = 1.0 

75 self.plates = plates 

76 

77 actor = vtk.vtkActor() 

78 actor.SetMapper(self.mapper) 

79 

80 prop = actor.GetProperty() 

81 prop.SetDiffuseColor(1, 1, 1) 

82 

83 self.prop = prop 

84 self.actor = actor 

85 

86 def plate_to_lines(self, plate): 

87 lines = [] 

88 poly = [] 

89 num_nodes = len(plate.points) 

90 for i in range(0, num_nodes): 

91 poly.append((plate.points[i][0], plate.points[i][1])) 

92 lines.append(num.asarray(poly)) 

93 

94 self._polyline_grid[0] = vtk_util.make_multi_polyline( 

95 lines_latlon=lines, depth=-100.) 

96 vtk_util.vtk_set_input(self.mapper, self._polyline_grid[0]) 

97 

98 def set_opacity(self, opacity): 

99 opacity = float(opacity) 

100 if self._opacity != opacity: 

101 self.prop.SetOpacity(opacity) 

102 self._opacity = opacity 

103 

104 

105class PlatesBoundsElement(Element): 

106 

107 def __init__(self): 

108 Element.__init__(self) 

109 self._parent = None 

110 self._state = None 

111 self._pipe = None 

112 self._controls = None 

113 self._plates = None 

114 self._plate_line = None 

115 self._plate_lines = [] 

116 

117 def bind_state(self, state): 

118 Element.bind_state(self, state) 

119 self.talkie_connect(state, ['visible', 'opacity'], self.update) 

120 

121 def get_name(self): 

122 return 'Plate bounds' 

123 

124 def set_parent(self, parent): 

125 self._parent = parent 

126 if not self._plates: 

127 PB = PeterBird2003() 

128 self._plates = PB.get_plates() 

129 

130 self._parent.add_panel( 

131 self.get_title_label(), 

132 self._get_controls(), 

133 visible=True, 

134 title_controls=[ 

135 self.get_title_control_remove(), 

136 self.get_title_control_visible()]) 

137 

138 self.update() 

139 

140 def unset_parent(self): 

141 self.unbind_state() 

142 if self._plate_lines: 

143 for i, plate in enumerate(self._plate_lines): 

144 self._parent.remove_actor(plate.actor) 

145 

146 self._pipe = None 

147 

148 if self._controls: 

149 self._parent.remove_panel(self._controls) 

150 self._controls = None 

151 

152 self._parent.update_view() 

153 self._parent = None 

154 

155 def update(self, *args): 

156 

157 state = self._state 

158 if state.visible: 

159 colors = plates_to_color(self._plates) 

160 for i, plate in enumerate(self._plates): 

161 self._plate_line = PlatelinesPipe(plates=plate) 

162 self._plate_line.plate_to_lines(plate) 

163 self._parent.add_actor(self._plate_line.actor) 

164 prop = self._plate_line.actor.GetProperty() 

165 prop.SetDiffuseColor(colors[i][0:3]) 

166 self._plate_line.set_opacity(state.opacity) 

167 self._plate_lines.append(self._plate_line) 

168 if not state.visible and self._plate_lines: 

169 for i, plate in enumerate(self._plate_lines): 

170 self._parent.remove_actor(plate.actor) 

171 

172 self._parent.update_view() 

173 

174 def _get_controls(self): 

175 if self._controls is None: 

176 from ..state import state_bind_slider 

177 

178 frame = qw.QFrame() 

179 layout = qw.QGridLayout() 

180 layout.setAlignment(qc.Qt.AlignTop) 

181 frame.setLayout(layout) 

182 

183 layout.addWidget(qw.QLabel('Opacity'), 0, 0) 

184 

185 slider = qw.QSlider(qc.Qt.Horizontal) 

186 slider.setSizePolicy( 

187 qw.QSizePolicy( 

188 qw.QSizePolicy.Expanding, qw.QSizePolicy.Fixed)) 

189 slider.setMinimum(0) 

190 slider.setMaximum(10) 

191 slider.setSingleStep(1) 

192 slider.setPageStep(1) 

193 layout.addWidget(slider, 0, 1) 

194 state_bind_slider(self, self._state, 'opacity', slider) 

195 

196 layout.addWidget(qw.QFrame(), 1, 0, 1, 2) 

197 

198 self._controls = frame 

199 

200 return self._controls 

201 

202 

203__all__ = [ 

204 'PlatesBoundsElement', 

205 'PlatesBoundsState' 

206]