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

129 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2023-11-03 12:47 +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.guts import Bool, Float 

9from pyrocko.gui.qt_compat import qw, qc 

10 

11from pyrocko.dataset.active_faults import ActiveFaults 

12from pyrocko.gui import vtk_util 

13from pyrocko import plot 

14import vtk 

15 

16from .base import Element, ElementState 

17 

18guts_prefix = 'sparrow' 

19 

20km = 1e3 

21 

22 

23def color(x): 

24 return num.array(plot.to01(plot.color(x)), dtype=num.float64) 

25 

26 

27fault_color_themes = { 

28 'light': { 

29 'Normal': color('skyblue1'), 

30 'Reverse': color('scarletred1'), 

31 'SS': color('chameleon1'), 

32 'Sinistral': color('plum1'), 

33 'Dextral': color('plum1'), 

34 None: color('chocolate1')}, 

35 'dark': { 

36 'Normal': color('skyblue3'), 

37 'Reverse': color('scarletred3'), 

38 'SS': color('chameleon3'), 

39 'Sinistral': color('plum3'), 

40 'Dextral': color('plum3'), 

41 None: color('chocolate3')}, 

42 'uniform_light': { 

43 None: color('chocolate1')}, 

44 'uniform_dark': { 

45 None: color('chocolate3')}} 

46 

47 

48class ActiveFaultsState(ElementState): 

49 visible = Bool.T(default=True) 

50 line_width = Float.T(default=1.0) 

51 color_by_slip_type = Bool.T(default=False) 

52 

53 def create(self): 

54 element = ActiveFaultsElement() 

55 element.bind_state(self) 

56 return element 

57 

58 

59class FaultlinesPipe(object): 

60 def __init__(self, faults): 

61 

62 self._opacity = 1.0 

63 self._line_width = 1.0 

64 self._faults = faults 

65 

66 slip_types = sorted(set(f.slip_type for f in faults.active_faults)) 

67 self._slip_types = slip_types 

68 

69 self._actors = {} 

70 for slip_type in slip_types: 

71 mapper = vtk.vtkDataSetMapper() 

72 

73 lines = [ 

74 f.get_surface_line() 

75 for f in faults.active_faults 

76 if f.slip_type == slip_type] 

77 

78 grid = vtk_util.make_multi_polyline( 

79 lines_latlon=lines, depth=-100.) 

80 

81 vtk_util.vtk_set_input(mapper, grid) 

82 

83 actor = vtk.vtkActor() 

84 actor.SetMapper(mapper) 

85 

86 self._actors[slip_type] = actor 

87 

88 self._theme = '' 

89 self.set_color_theme('uniform_light') 

90 

91 def set_color_theme(self, theme): 

92 

93 if self._theme != theme: 

94 

95 colors = fault_color_themes[theme] 

96 default_color = colors[None] 

97 

98 for slip_type in self._slip_types: 

99 actor = self._actors[slip_type] 

100 prop = actor.GetProperty() 

101 prop.SetDiffuseColor(*colors.get(slip_type, default_color)) 

102 

103 self._theme = theme 

104 

105 def set_opacity(self, opacity): 

106 opacity = float(opacity) 

107 if self._opacity != opacity: 

108 for actor in self._actors.values(): 

109 actor.GetProperty().SetOpacity(opacity) 

110 

111 self._opacity = opacity 

112 

113 def set_line_width(self, width): 

114 width = float(width) 

115 if self._line_width != width: 

116 for actor in self._actors.values(): 

117 actor.GetProperty().SetLineWidth(width) 

118 

119 self._line_width = width 

120 

121 def get_actors(self): 

122 return [self._actors[slip_type] for slip_type in self._slip_types] 

123 

124 

125class ActiveFaultsElement(Element): 

126 

127 def __init__(self): 

128 Element.__init__(self) 

129 self._pipe = None 

130 self._controls = None 

131 self._active_faults = None 

132 

133 def bind_state(self, state): 

134 Element.bind_state(self, state) 

135 self.talkie_connect( 

136 state, 

137 ['visible', 'line_width', 'color_by_slip_type'], 

138 self.update) 

139 

140 def get_name(self): 

141 return 'Active Faults' 

142 

143 def set_parent(self, parent): 

144 Element.set_parent(self, parent) 

145 if not self._active_faults: 

146 self._active_faults = ActiveFaults() 

147 

148 self._parent.add_panel( 

149 self.get_title_label(), 

150 self._get_controls(), 

151 visible=True, 

152 title_controls=[ 

153 self.get_title_control_remove(), 

154 self.get_title_control_visible()]) 

155 

156 self.update() 

157 

158 def unset_parent(self): 

159 self.unbind_state() 

160 if self._parent: 

161 if self._pipe: 

162 for actor in self._pipe.get_actors(): 

163 self._parent.remove_actor(actor) 

164 

165 self._pipe = None 

166 

167 if self._controls: 

168 self._parent.remove_panel(self._controls) 

169 self._controls = None 

170 

171 self._parent.update_view() 

172 self._parent = None 

173 

174 def update(self, *args): 

175 

176 state = self._state 

177 if state.visible: 

178 if not self._pipe: 

179 self._pipe = FaultlinesPipe(self._active_faults) 

180 

181 if state.color_by_slip_type: 

182 self._pipe.set_color_theme('light') 

183 else: 

184 self._pipe.set_color_theme('uniform_light') 

185 

186 self._pipe.set_line_width(state.line_width) 

187 

188 if state.visible: 

189 for actor in self._pipe.get_actors(): 

190 self._parent.add_actor(actor) 

191 

192 else: 

193 for actor in self._pipe.get_actors(): 

194 self._parent.remove_actor(actor) 

195 

196 self._parent.update_view() 

197 

198 def _get_controls(self): 

199 if self._controls is None: 

200 from ..state import state_bind_checkbox, state_bind_slider 

201 

202 frame = qw.QFrame() 

203 layout = qw.QGridLayout() 

204 layout.setAlignment(qc.Qt.AlignTop) 

205 frame.setLayout(layout) 

206 

207 layout.addWidget(qw.QLabel('Line width'), 0, 0) 

208 

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

210 slider.setSizePolicy( 

211 qw.QSizePolicy( 

212 qw.QSizePolicy.Expanding, qw.QSizePolicy.Fixed)) 

213 slider.setMinimum(0) 

214 slider.setMaximum(10) 

215 slider.setSingleStep(1) 

216 slider.setPageStep(1) 

217 layout.addWidget(slider, 0, 1) 

218 state_bind_slider(self, self._state, 'line_width', slider) 

219 

220 cb_color_slip_type = qw.QCheckBox('Color by slip type') 

221 

222 layout.addWidget(cb_color_slip_type, 1, 0) 

223 state_bind_checkbox(self, self._state, 'color_by_slip_type', 

224 cb_color_slip_type) 

225 

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

227 

228 self._controls = frame 

229 

230 return self._controls 

231 

232 

233__all__ = [ 

234 'ActiveFaultsElement', 

235 'ActiveFaultsState' 

236]