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-10-04 09:52 +0000
« prev ^ index » next coverage.py v6.5.0, created at 2023-10-04 09:52 +0000
1# https://pyrocko.org - GPLv3
2#
3# The Pyrocko Developers, 21st Century
4# ---|P------/S----------~Lg----------
6import numpy as num
8from pyrocko.guts import Bool, Float
9from pyrocko.gui.qt_compat import qw, qc
11from pyrocko.dataset.active_faults import ActiveFaults
12from pyrocko.gui import vtk_util
13from pyrocko import plot
14import vtk
16from .base import Element, ElementState
18guts_prefix = 'sparrow'
20km = 1e3
23def color(x):
24 return num.array(plot.to01(plot.color(x)), dtype=num.float64)
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')}}
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)
53 def create(self):
54 element = ActiveFaultsElement()
55 element.bind_state(self)
56 return element
59class FaultlinesPipe(object):
60 def __init__(self, faults):
62 self._opacity = 1.0
63 self._line_width = 1.0
64 self._faults = faults
66 slip_types = sorted(set(f.slip_type for f in faults.active_faults))
67 self._slip_types = slip_types
69 self._actors = {}
70 for slip_type in slip_types:
71 mapper = vtk.vtkDataSetMapper()
73 lines = [
74 f.get_surface_line()
75 for f in faults.active_faults
76 if f.slip_type == slip_type]
78 grid = vtk_util.make_multi_polyline(
79 lines_latlon=lines, depth=-100.)
81 vtk_util.vtk_set_input(mapper, grid)
83 actor = vtk.vtkActor()
84 actor.SetMapper(mapper)
86 self._actors[slip_type] = actor
88 self._theme = ''
89 self.set_color_theme('uniform_light')
91 def set_color_theme(self, theme):
93 if self._theme != theme:
95 colors = fault_color_themes[theme]
96 default_color = colors[None]
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))
103 self._theme = theme
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)
111 self._opacity = opacity
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)
119 self._line_width = width
121 def get_actors(self):
122 return [self._actors[slip_type] for slip_type in self._slip_types]
125class ActiveFaultsElement(Element):
127 def __init__(self):
128 Element.__init__(self)
129 self._pipe = None
130 self._controls = None
131 self._active_faults = None
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)
140 def get_name(self):
141 return 'Active Faults'
143 def set_parent(self, parent):
144 Element.set_parent(self, parent)
145 if not self._active_faults:
146 self._active_faults = ActiveFaults()
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()])
156 self.update()
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)
165 self._pipe = None
167 if self._controls:
168 self._parent.remove_panel(self._controls)
169 self._controls = None
171 self._parent.update_view()
172 self._parent = None
174 def update(self, *args):
176 state = self._state
177 if state.visible:
178 if not self._pipe:
179 self._pipe = FaultlinesPipe(self._active_faults)
181 if state.color_by_slip_type:
182 self._pipe.set_color_theme('light')
183 else:
184 self._pipe.set_color_theme('uniform_light')
186 self._pipe.set_line_width(state.line_width)
188 if state.visible:
189 for actor in self._pipe.get_actors():
190 self._parent.add_actor(actor)
192 else:
193 for actor in self._pipe.get_actors():
194 self._parent.remove_actor(actor)
196 self._parent.update_view()
198 def _get_controls(self):
199 if self._controls is None:
200 from ..state import state_bind_checkbox, state_bind_slider
202 frame = qw.QFrame()
203 layout = qw.QGridLayout()
204 layout.setAlignment(qc.Qt.AlignTop)
205 frame.setLayout(layout)
207 layout.addWidget(qw.QLabel('Line width'), 0, 0)
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)
220 cb_color_slip_type = qw.QCheckBox('Color by slip type')
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)
226 layout.addWidget(qw.QFrame(), 2, 0, 1, 2)
228 self._controls = frame
230 return self._controls
233__all__ = [
234 'ActiveFaultsElement',
235 'ActiveFaultsState'
236]