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

# https://pyrocko.org - GPLv3 

# 

# The Pyrocko Developers, 21st Century 

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

 

from __future__ import absolute_import, print_function, division 

 

import logging 

 

import numpy as num 

 

from pyrocko import util 

from pyrocko.guts import StringChoice, Float, List, Bool, Timestamp, Tuple, \ 

get_elements, set_elements, path_to_str, clone 

 

from pyrocko.gui import talkie 

from . import common, light 

 

guts_prefix = 'sparrow' 

 

logger = logging.getLogger('pyrocko.gui.sparrow.state') 

 

 

class FocalPointChoice(StringChoice): 

choices = ['center', 'target'] 

 

 

class ShadingChoice(StringChoice): 

choices = ['flat', 'gouraud', 'phong', 'pbr'] 

 

 

class LightingChoice(StringChoice): 

choices = light.get_lighting_theme_names() 

 

 

class ViewerGuiState(talkie.TalkieRoot): 

panels_visible = Bool.T(default=True) 

size = Tuple.T(2, Float.T(), default=(100., 100.)) 

focal_point = FocalPointChoice.T(default='center') 

 

def next_focal_point(self): 

choices = FocalPointChoice.choices 

ii = choices.index(self.focal_point) 

self.focal_point = choices[(ii+1) % len(choices)] 

 

 

class ViewerState(talkie.TalkieRoot): 

lat = Float.T(default=0.0) 

lon = Float.T(default=0.0) 

depth = Float.T(default=0.0) 

strike = Float.T(default=90.0) 

dip = Float.T(default=0.0) 

distance = Float.T(default=3.0) 

elements = List.T(talkie.Talkie.T()) 

tmin = Timestamp.T(optional=True) 

tmax = Timestamp.T(optional=True) 

lighting = LightingChoice.T(default=LightingChoice.choices[0]) 

 

 

def state_bind( 

owner, state, paths, update_state, 

widget, signals, update_widget, attribute=None): 

 

def make_wrappers(widget): 

def wrap_update_widget(*args): 

if attribute: 

update_widget(state, attribute, widget) 

else: 

update_widget(state, widget) 

common.de_errorize(widget) 

 

def wrap_update_state(*args): 

try: 

if attribute: 

update_state(widget, state, attribute) 

else: 

update_state(widget, state) 

common.de_errorize(widget) 

except Exception as e: 

logger.warn('caught exception: %s' % e) 

common.errorize(widget) 

 

return wrap_update_widget, wrap_update_state 

 

wrap_update_widget, wrap_update_state = make_wrappers(widget) 

 

for sig in signals: 

sig.connect(wrap_update_state) 

 

for path in paths: 

owner.register_state_listener(wrap_update_widget) 

state.add_listener(wrap_update_widget, path) 

 

wrap_update_widget() 

 

 

def state_bind_slider(owner, state, path, widget, factor=1., dtype=float): 

 

def make_funcs(): 

def update_state(widget, state): 

state.set(path, dtype(widget.value() * factor)) 

 

def update_widget(state, widget): 

widget.blockSignals(True) 

widget.setValue(state.get(path) * 1. / factor) 

widget.blockSignals(False) 

 

return update_state, update_widget 

 

update_state, update_widget = make_funcs() 

 

state_bind( 

owner, state, [path], update_state, widget, [widget.valueChanged], 

update_widget) 

 

 

def state_bind_combobox(owner, state, path, widget): 

 

def make_funcs(): 

def update_state(widget, state): 

state.set(path, str(widget.currentText())) 

 

def update_widget(state, widget): 

widget.blockSignals(True) 

val = state.get(path) 

for i in range(widget.count()): 

if str(widget.itemText(i)) == val: 

widget.setCurrentIndex(i) 

widget.blockSignals(False) 

 

return update_state, update_widget 

 

update_state, update_widget = make_funcs() 

 

state_bind( 

owner, state, [path], update_state, widget, [widget.activated], 

update_widget) 

 

 

def state_bind_checkbox(owner, state, path, widget): 

 

def make_funcs(): 

def update_state(widget, state): 

state.set(path, bool(widget.isChecked())) 

 

def update_widget(state, widget): 

widget.blockSignals(True) 

widget.setChecked(state.get(path)) 

widget.blockSignals(False) 

 

return update_state, update_widget 

 

update_state, update_widget = make_funcs() 

 

state_bind( 

owner, state, [path], update_state, widget, [widget.toggled], 

update_widget) 

 

 

def state_bind_lineedit(owner, state, path, widget): 

 

def make_funcs(): 

 

def update_state(widget, state): 

state.set(path, str(widget.text())) 

 

def update_widget(state, widget): 

widget.blockSignals(True) 

widget.setText(state.get(path)) 

widget.blockSignals(False) 

 

return update_state, update_widget 

 

update_state, update_widget = make_funcs() 

 

state_bind( 

owner, 

state, [path], update_state, 

widget, [widget.editingFinished, widget.returnPressed], update_widget) 

 

 

def interpolateables(state_a, state_b): 

 

animate = [] 

for tag, path, values in state_a.diff(state_b): 

if tag == 'set': 

ypath = path_to_str(path) 

v_old = get_elements(state_a, ypath)[0] 

v_new = values 

if isinstance(v_old, float) and isinstance(v_new, float): 

animate.append((ypath, v_old, v_new)) 

 

return animate 

 

 

def interpolate(times, states, times_inter): 

 

assert len(times) == len(states) 

 

states_inter = [] 

for i in range(len(times) - 1): 

 

state_a = states[i] 

state_b = states[i+1] 

time_a = times[i] 

time_b = times[i+1] 

 

animate = interpolateables(state_a, state_b) 

 

if i == 0: 

times_inter_this = times_inter[num.logical_and( 

time_a <= times_inter, times_inter <= time_b)] 

else: 

times_inter_this = times_inter[num.logical_and( 

time_a < times_inter, times_inter <= time_b)] 

 

for time_inter in times_inter_this: 

state = clone(state_b) 

if time_b == time_a: 

blend = 0. 

else: 

blend = (time_inter - time_a) / (time_b - time_a) 

 

for ypath, v_old, v_new in animate: 

if isinstance(v_old, float) and isinstance(v_new, float): 

if ypath == 'strike': 

if v_new - v_old > 180.: 

v_new -= 360. 

elif v_new - v_old < -180.: 

v_new += 360. 

 

if ypath != 'distance': 

v_inter = v_old + blend * (v_new - v_old) 

else: 

v_old = num.log(v_old) 

v_new = num.log(v_new) 

v_inter = v_old + blend * (v_new - v_old) 

v_inter = num.exp(v_inter) 

 

set_elements(state, ypath, v_inter) 

else: 

set_elements(state, ypath, v_new) 

 

states_inter.append(state) 

 

return states_inter 

 

 

class Interpolator(object): 

 

def __init__(self, times, states, fps=25.): 

 

assert len(times) == len(states) 

 

self.dt = 1.0 / fps 

self.tmin = times[0] 

self.tmax = times[-1] 

times_inter = util.arange2(self.tmin, self.tmax, self.dt) 

times_inter[-1] = times[-1] 

 

states_inter = [] 

for i in range(len(times) - 1): 

 

state_a = states[i] 

state_b = states[i+1] 

time_a = times[i] 

time_b = times[i+1] 

 

animate = interpolateables(state_a, state_b) 

 

if i == 0: 

times_inter_this = times_inter[num.logical_and( 

time_a <= times_inter, times_inter <= time_b)] 

else: 

times_inter_this = times_inter[num.logical_and( 

time_a < times_inter, times_inter <= time_b)] 

 

for time_inter in times_inter_this: 

state = clone(state_b) 

 

if time_b == time_a: 

blend = 0. 

else: 

blend = (time_inter - time_a) / (time_b - time_a) 

 

for ypath, v_old, v_new in animate: 

if isinstance(v_old, float) and isinstance(v_new, float): 

if ypath == 'strike': 

if v_new - v_old > 180.: 

v_new -= 360. 

elif v_new - v_old < -180.: 

v_new += 360. 

 

if ypath != 'distance': 

v_inter = v_old + blend * (v_new - v_old) 

else: 

v_old = num.log(v_old) 

v_new = num.log(v_new) 

v_inter = v_old + blend * (v_new - v_old) 

v_inter = num.exp(v_inter) 

 

set_elements(state, ypath, v_inter) 

else: 

set_elements(state, ypath, v_new) 

 

states_inter.append(state) 

 

self._states_inter = states_inter 

 

def __call__(self, t): 

itime = int(round((t - self.tmin) / self.dt)) 

itime = min(max(0, itime), len(self._states_inter)-1) 

return self._states_inter[itime]