1# https://pyrocko.org - GPLv3 

2# 

3# The Pyrocko Developers, 21st Century 

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

5 

6import logging 

7import numpy as num 

8import vtk 

9 

10from vtk.util.numpy_support import \ 

11 numpy_to_vtk as numpy_to_vtk_, get_vtk_array_type 

12from vtk.util import vtkAlgorithm as va 

13 

14from pyrocko import geometry, cake, orthodrome as od 

15from pyrocko.plot import cpt as pcpt 

16from pyrocko.color import Color 

17 

18logger = logging.getLogger('pyrocko.gui.vtk_util') 

19 

20 

21def vtk_set_input(obj, source): 

22 try: 

23 obj.SetInputData(source) 

24 except AttributeError: 

25 obj.SetInput(source) 

26 

27 

28def numpy_to_vtk(a): 

29 return numpy_to_vtk_( 

30 a, deep=1, array_type=get_vtk_array_type(a.dtype)) 

31 

32 

33def numpy_to_vtk_colors(a): 

34 c = numpy_to_vtk((a*255.).astype(num.uint8)) 

35 c.SetName('Colors') 

36 return c 

37 

38 

39def cpt_to_vtk_lookuptable(cpt, n=1024): 

40 lut = vtk.vtkLookupTable() 

41 lut.Allocate(n, n) 

42 values = num.linspace(cpt.vmin, cpt.vmax, n) 

43 colors = cpt(values) 

44 lut.SetTableRange(cpt.vmin, cpt.vmax) 

45 for i in range(n): 

46 lut.SetTableValue( 

47 i, colors[i, 0]/255., colors[i, 1]/255., colors[i, 2]/255.) 

48 

49 return lut 

50 

51 

52def vtk_set_prop_interpolation(prop, name): 

53 if name == 'gouraud': 

54 prop.SetInterpolationToGouraud() 

55 elif name == 'phong': 

56 prop.SetInterpolationToPhong() 

57 elif name == 'flat': 

58 prop.SetInterpolationToFlat() 

59 elif name == 'pbr': 

60 if hasattr(prop, 'SetInterpolationToPBR'): 

61 prop.SetInterpolationToPBR() 

62 else: 

63 logger.warn( 

64 'PBR shading not available - update your VTK installation.') 

65 

66 

67def make_multi_polyline( 

68 lines_rtp=None, lines_latlon=None, depth=0.0, lines_latlondepth=None): 

69 if lines_rtp is not None: 

70 points = geometry.rtp2xyz(num.vstack(lines_rtp)) 

71 lines = lines_rtp 

72 elif lines_latlon is not None: 

73 points = geometry.latlon2xyz( 

74 num.vstack(lines_latlon), radius=1.0 - depth/cake.earthradius) 

75 lines = lines_latlon 

76 elif lines_latlondepth is not None: 

77 if not lines_latlondepth: 

78 points = num.zeros((0, 3)) 

79 else: 

80 points = geometry.latlondepth2xyz( 

81 num.vstack(lines_latlondepth), planetradius=cake.earthradius) 

82 lines = lines_latlondepth 

83 

84 vpoints = vtk.vtkPoints() 

85 vpoints.SetNumberOfPoints(points.shape[0]) 

86 vpoints.SetData(numpy_to_vtk(points)) 

87 

88 poly_data = vtk.vtkPolyData() 

89 poly_data.Allocate(len(lines), len(lines)) 

90 poly_data.SetPoints(vpoints) 

91 

92 ioff = 0 

93 celltype = vtk.vtkPolyLine().GetCellType() 

94 for iline, line in enumerate(lines): 

95 # should be faster but doesn't work (SetArray seems to be the problem) 

96 # arr = numpy_to_vtkIdTypeArray(num.arange(line.shape[0]) + ioff, 1) 

97 # pids.SetArray( 

98 # int(arr.GetPointer(0).split('_')[1], base=16), line.shape[0]) 

99 

100 poly_data.InsertNextCell( 

101 celltype, line.shape[0], range(ioff, ioff+line.shape[0])) 

102 

103 ioff += line.shape[0] 

104 

105 return poly_data 

106 

107 

108class ScatterPipe(object): 

109 def __init__(self, vertices): 

110 nvertices = vertices.shape[0] 

111 vpoints = vtk.vtkPoints() 

112 vpoints.SetNumberOfPoints(nvertices) 

113 i = 0 

114 for x, y, z in vertices: 

115 vpoints.InsertPoint(i, x, y, z) 

116 i += 1 

117 

118 ppd = vtk.vtkPolyData() 

119 ppd.SetPoints(vpoints) 

120 

121 vertex_filter = vtk.vtkVertexGlyphFilter() 

122 try: 

123 vertex_filter.SetInputData(ppd) 

124 except AttributeError: 

125 vertex_filter.SetInputConnection(ppd.GetProducerPort()) 

126 

127 vertex_filter.Update() 

128 

129 pd = vtk.vtkPolyData() 

130 self.polydata = pd 

131 pd.ShallowCopy(vertex_filter.GetOutput()) 

132 

133 self._colors = num.ones((nvertices, 4)) 

134 self._update_colors() 

135 

136 map = vtk.vtkPolyDataMapper() 

137 try: 

138 map.SetInputConnection(pd.GetProducerPort()) 

139 except AttributeError: 

140 map.SetInputData(pd) 

141 

142 act = vtk.vtkActor() 

143 act.SetMapper(map) 

144 

145 prop = act.GetProperty() 

146 prop.SetPointSize(10) 

147 

148 self.prop = prop 

149 self.actor = act 

150 

151 self._symbol = '' 

152 self.set_symbol('point') 

153 

154 def set_colors(self, colors): 

155 self._colors[:, :3] = colors 

156 self._update_colors() 

157 

158 def set_alpha(self, alpha): 

159 # print('colors', self._colors.shape) 

160 self._colors[:, 3] = alpha 

161 self._update_colors() 

162 

163 def _update_colors(self): 

164 vcolors = numpy_to_vtk_colors(self._colors) 

165 self.polydata.GetPointData().SetScalars(vcolors) 

166 

167 def set_size(self, size): 

168 self.prop.SetPointSize(size) 

169 

170 def set_symbol(self, symbol): 

171 assert symbol in ('point', 'sphere') 

172 

173 if self._symbol != symbol: 

174 try: 

175 self.prop.SetRenderPointsAsSpheres(symbol == 'sphere') 

176 except AttributeError: 

177 if symbol == 'sphere': 

178 logger.warn( 

179 'Cannot render points as sphere with this version of ' 

180 'VTK') 

181 

182 self._symbol = symbol 

183 

184 

185class PointDataInjector(va.VTKAlgorithm): 

186 def __init__(self, scalars): 

187 va.VTKAlgorithm.__init__( 

188 self, 

189 nInputPorts=1, inputType='vtkPolyData', 

190 nOutputPorts=1, outputType='vtkPolyData') 

191 

192 self.scalars = scalars 

193 

194 def RequestData(self, vtkself, request, inInfo, outInfo): 

195 inp = self.GetInputData(inInfo, 0, 0) 

196 out = self.GetOutputData(outInfo, 0) 

197 out.ShallowCopy(inp) 

198 out.GetPointData().SetScalars(self.scalars) 

199 

200 return 1 

201 

202 

203def lighten(c, f): 

204 return tuple(255.-(255.-x)*f for x in c) 

205 

206 

207class BeachballPipe(object): 

208 

209 def __init__( 

210 self, positions, m6s, sizes, depths, ren, 

211 level=3, 

212 face='backside', 

213 lighting=False): 

214 

215 from pyrocko import moment_tensor, icosphere 

216 

217 # cpt tricks 

218 

219 cpt = pcpt.get_cpt('global_event_depth_2') 

220 cpt2 = pcpt.CPT() 

221 for ilevel, clevel in enumerate(cpt.levels): 

222 

223 cpt_data = [ 

224 (-1.0, ) + lighten(clevel.color_min, 0.2), 

225 (-0.05, ) + lighten(clevel.color_min, 0.2), 

226 (0.05, ) + clevel.color_min, 

227 (1.0, ) + clevel.color_min] 

228 

229 cpt2.levels.extend( 

230 pcpt.CPTLevel( 

231 vmin=a[0] + ilevel * 2.0, 

232 vmax=b[0] + ilevel * 2.0, 

233 color_min=a[1:], 

234 color_max=b[1:]) 

235 for (a, b) in zip(cpt_data[:-1], cpt_data[1:])) 

236 

237 def depth_to_ilevel(cpt, depth): 

238 for ilevel, clevel in enumerate(cpt.levels): 

239 if depth < clevel.vmax: 

240 return ilevel 

241 

242 return len(cpt.levels) - 1 

243 

244 # source 

245 

246 vertices, faces = icosphere.sphere( 

247 level, 'icosahedron', 'kind1', radius=1.0, 

248 triangulate=False) 

249 

250 p_vertices = vtk.vtkPoints() 

251 p_vertices.SetNumberOfPoints(vertices.shape[0]) 

252 p_vertices.SetData(numpy_to_vtk(vertices)) 

253 

254 pd = vtk.vtkPolyData() 

255 pd.SetPoints(p_vertices) 

256 

257 cells = faces_to_cells(faces) 

258 

259 pd.SetPolys(cells) 

260 

261 # positions 

262 

263 p_positions = vtk.vtkPoints() 

264 p_positions.SetNumberOfPoints(positions.shape[0]) 

265 p_positions.SetData(numpy_to_vtk(positions)) 

266 

267 pd_positions = vtk.vtkPolyData() 

268 pd_positions.SetPoints(p_positions) 

269 pd_positions.GetPointData().SetScalars(numpy_to_vtk(sizes)) 

270 

271 latlons = od.xyz_to_latlon(positions) 

272 

273 # glyph 

274 

275 glyph = vtk.vtkGlyph3D() 

276 glyph.ScalingOn() 

277 glyph.SetScaleModeToScaleByScalar() 

278 glyph.SetSourceData(pd) 

279 

280 if True: 

281 glyph.SetInputData(pd_positions) 

282 glyph.SetScaleFactor(0.005) 

283 else: 

284 pd_distance_scaler = vtk.vtkDistanceToCamera() 

285 pd_distance_scaler.SetInputData(pd_positions) 

286 pd_distance_scaler.SetRenderer(ren) 

287 pd_distance_scaler.SetScreenSize(10) 

288 

289 glyph.SetInputConnection(pd_distance_scaler.GetOutputPort()) 

290 glyph.SetInputArrayToProcess( 

291 0, 0, 0, 

292 vtk.vtkDataObject.FIELD_ASSOCIATION_POINTS, "DistanceToCamera") 

293 

294 self.glyph = glyph 

295 

296 nvertices = vertices.shape[0] 

297 amps = num.zeros(m6s.shape[0] * nvertices) 

298 for i, m6 in enumerate(m6s): 

299 m = moment_tensor.symmat6(*m6) 

300 m /= num.linalg.norm(m) / num.sqrt(2.0) 

301 (ep, en, et), m_evecs = num.linalg.eigh(m) 

302 if num.linalg.det(m_evecs) < 0.: 

303 m_evecs *= -1. 

304 vp, vn, vt = m_evecs.T 

305 to_e = num.vstack((vn, vt, vp)) 

306 

307 xyz_to_ned_00 = num.array([ 

308 [0., 0., 1.], 

309 [0., 1., 0.], 

310 [-1., 0., 0.]]) 

311 

312 zero_to_latlon = od.rot_to_00(*latlons[i]) 

313 

314 rot = num.dot(num.dot(to_e, xyz_to_ned_00), zero_to_latlon) 

315 

316 vecs_e = num.dot(rot, vertices.T).T 

317 

318 rtp = geometry.xyz2rtp(vecs_e) 

319 

320 atheta, aphi = rtp[:, 1], rtp[:, 2] 

321 amps_this = ep * num.cos(atheta)**2 + ( 

322 en * num.cos(aphi)**2 + 

323 et * num.sin(aphi)**2) * num.sin(atheta)**2 

324 

325 amps_this = num.clip(amps_this, -0.9, 0.9) \ 

326 + depth_to_ilevel(cpt, depths[i]) * 2 

327 

328 amps[i*nvertices:(i+1)*nvertices] = amps_this 

329 

330 vamps = numpy_to_vtk(amps) 

331 

332 glyph.Update() 

333 

334 pd_injector = PointDataInjector(vamps) 

335 

336 pd_injector_pa = vtk.vtkPythonAlgorithm() 

337 pd_injector_pa.SetPythonObject(pd_injector) 

338 

339 pd_injector_pa.SetInputConnection(glyph.GetOutputPort()) 

340 

341 mapper = vtk.vtkPolyDataMapper() 

342 mapper.ScalarVisibilityOn() 

343 mapper.InterpolateScalarsBeforeMappingOn() 

344 

345 mapper.SetInputConnection(pd_injector_pa.GetOutputPort()) 

346 mapper.Update() 

347 

348 actor = vtk.vtkActor() 

349 actor.SetMapper(mapper) 

350 

351 self.prop = actor.GetProperty() 

352 self.set_lighting(lighting) 

353 self.set_face(face) 

354 

355 lut = cpt_to_vtk_lookuptable(cpt2, n=10000) 

356 mapper.SetUseLookupTableScalarRange(True) 

357 mapper.SetLookupTable(lut) 

358 

359 self.actor = actor 

360 

361 def set_face(self, face='backside'): 

362 if face == 'backside': 

363 self.prop.FrontfaceCullingOn() 

364 self.prop.BackfaceCullingOff() 

365 elif face == 'frontside': 

366 self.prop.FrontfaceCullingOff() 

367 self.prop.BackfaceCullingOn() 

368 

369 def set_lighting(self, lighting=False): 

370 self.prop.SetLighting(lighting) 

371 

372 def set_size_factor(self, factor): 

373 self.glyph.SetScaleFactor(factor) 

374 

375 

376def faces_to_cells(faces): 

377 cells = vtk.vtkCellArray() 

378 

379 for face in faces: 

380 cells.InsertNextCell(face.size) 

381 for ivert in face: 

382 cells.InsertCellPoint(ivert) 

383 

384 return cells 

385 

386 

387class TrimeshPipe(object): 

388 def __init__( 

389 self, vertices, 

390 faces=None, 

391 cells=None, 

392 values=None, 

393 smooth=False, 

394 cpt=None, 

395 lut=None, 

396 backface_culling=True): 

397 

398 self._opacity = 1.0 

399 self._smooth = None 

400 self._lut = None 

401 

402 vpoints = vtk.vtkPoints() 

403 vpoints.SetNumberOfPoints(vertices.shape[0]) 

404 vpoints.SetData(numpy_to_vtk(vertices)) 

405 

406 pd = vtk.vtkPolyData() 

407 pd.SetPoints(vpoints) 

408 

409 if faces is not None: 

410 cells = faces_to_cells(faces) 

411 

412 pd.SetPolys(cells) 

413 

414 mapper = vtk.vtkPolyDataMapper() 

415 

416 mapper.ScalarVisibilityOff() 

417 

418 act = vtk.vtkActor() 

419 act.SetMapper(mapper) 

420 prop = act.GetProperty() 

421 self.prop = prop 

422 prop.SetColor(0.5, 0.5, 0.5) 

423 

424 if backface_culling: 

425 prop.BackfaceCullingOn() 

426 else: 

427 prop.BackfaceCullingOff() 

428 

429 # prop.EdgeVisibilityOn() 

430 # prop.SetInterpolationToGouraud() 

431 self._shading = None 

432 self.set_shading('phong') 

433 self._color = None 

434 self.set_color(Color('aluminium3')) 

435 

436 self._ambient = None 

437 self.set_ambient(0.0) 

438 self._diffuse = None 

439 self.set_diffuse(1.0) 

440 self._specular = None 

441 self.set_specular(0.0) 

442 

443 self.polydata = pd 

444 self.mapper = mapper 

445 self.actor = act 

446 

447 self.set_smooth(smooth) 

448 

449 if values is not None: 

450 mapper.ScalarVisibilityOn() 

451 self.set_values(values) 

452 

453 if cpt is not None: 

454 self.set_cpt(cpt) 

455 

456 if lut is not None: 

457 self.set_lookuptable(lut) 

458 

459 def set_color(self, color): 

460 if self._color is None or color != self._color: 

461 self.prop.SetColor(*color.rgb) 

462 self._color = color 

463 

464 def set_ambient(self, ambient): 

465 if self._ambient is None or ambient != self._ambient: 

466 self.prop.SetAmbient(ambient) 

467 self._ambient = ambient 

468 

469 def set_diffuse(self, diffuse): 

470 if self._diffuse is None or diffuse != self._diffuse: 

471 self.prop.SetDiffuse(diffuse) 

472 self._diffuse = diffuse 

473 

474 def set_specular(self, specular): 

475 if self._specular is None or specular != self._specular: 

476 self.prop.SetSpecular(specular) 

477 self._specular = specular 

478 

479 def set_shading(self, shading): 

480 if self._shading is None or self._shading != shading: 

481 vtk_set_prop_interpolation(self.prop, shading) 

482 self._shading = shading 

483 

484 def set_smooth(self, smooth): 

485 if self._smooth is None or self._smooth != smooth: 

486 if not smooth: 

487 # stripper = vtk.vtkStripper() 

488 # stripper.SetInputData(self.polydata) 

489 # stripper.Update() 

490 # self.mapper.SetInputConnection(stripper.GetOutputPort()) 

491 

492 vtk_set_input(self.mapper, self.polydata) 

493 else: 

494 # stripper = vtk.vtkStripper() 

495 # stripper.SetInputData(self.polydata) 

496 # stripper.Update() 

497 

498 normals = vtk.vtkPolyDataNormals() 

499 normals.SetFeatureAngle(60.) 

500 normals.ConsistencyOff() 

501 normals.SplittingOff() 

502 # normals.SetInputConnection(stripper.GetOutputPort()) 

503 normals.SetInputData(self.polydata) 

504 

505 self.mapper.SetInputConnection(normals.GetOutputPort()) 

506 

507 self._smooth = smooth 

508 

509 def set_opacity(self, opacity): 

510 opacity = float(opacity) 

511 if self._opacity != opacity: 

512 self.prop.SetOpacity(opacity) 

513 self._opacity = opacity 

514 

515 def set_vertices(self, vertices): 

516 self.polydata.GetPoints().SetData(numpy_to_vtk(vertices)) 

517 

518 def set_values(self, values): 

519 values = num.ascontiguousarray(values, dtype=num.float64) 

520 vvalues = numpy_to_vtk(values) 

521 self.polydata.GetCellData().SetScalars(vvalues) 

522 # self.mapper.SetScalarRange(values.min(), values.max()) 

523 

524 def set_lookuptable(self, lut): 

525 if self._lut is not lut: 

526 self.mapper.SetUseLookupTableScalarRange(True) 

527 self.mapper.SetLookupTable(lut) 

528 self._lut = lut 

529 

530 def set_cpt(self, cpt): 

531 self.set_lookuptable(cpt_to_vtk_lookuptable(cpt)) 

532 

533 

534class OutlinesPipe(object): 

535 

536 def __init__(self, geom, color, cs='latlon'): 

537 

538 self._polyline_grid = None 

539 self.actors = None 

540 

541 lines = [] 

542 for outline in geom.outlines: 

543 latlon = outline.get_col('latlon') 

544 depth = outline.get_col('depth') 

545 

546 points = num.concatenate( 

547 (latlon, depth.reshape(len(depth), 1)), 

548 axis=1) 

549 points = num.concatenate( 

550 (points, points[0].reshape(1, -1)), axis=0) 

551 

552 lines.append(points) 

553 

554 mapper = vtk.vtkDataSetMapper() 

555 if cs == 'latlondepth': 

556 self._polyline_grid = make_multi_polyline( 

557 lines_latlondepth=lines) 

558 elif cs == 'latlon': 

559 self._polyline_grid = make_multi_polyline( 

560 lines_latlon=lines) 

561 else: 

562 raise ValueError('cs=%s is not supported!' % cs) 

563 

564 vtk_set_input(mapper, self._polyline_grid) 

565 

566 actor = vtk.vtkActor() 

567 actor.SetMapper(mapper) 

568 

569 prop = actor.GetProperty() 

570 prop.SetDiffuseColor(color) 

571 prop.SetOpacity(1.) 

572 

573 self.actor = actor 

574 

575 

576class PolygonPipe(object): 

577 def __init__(self, vertices, faces, values=None, cpt=None, lut=None): 

578 vpoints = vtk.vtkPoints() 

579 vpoints.SetNumberOfPoints(vertices.shape[0]) 

580 vpoints.SetData(numpy_to_vtk(vertices)) 

581 

582 pd = vtk.vtkPolyData() 

583 pd.SetPoints(vpoints) 

584 

585 cells = vtk.vtkCellArray() 

586 for face in faces: 

587 cells.InsertNextCell(face.size) 

588 for ivert in face: 

589 cells.InsertCellPoint(ivert) 

590 

591 pd.SetPolys(cells) 

592 

593 mapper = vtk.vtkPolyDataMapper() 

594 vtk_set_input(mapper, pd) 

595 

596 act = vtk.vtkActor() 

597 act.SetMapper(mapper) 

598 

599 prop = act.GetProperty() 

600 self.prop = prop 

601 

602 self.polydata = pd 

603 self.mapper = mapper 

604 self.actor = act 

605 

606 self._colors = num.ones((faces.shape[0], 4)) 

607 

608 if values is not None: 

609 self.set_values(values) 

610 

611 if cpt is not None: 

612 self.set_cpt(cpt) 

613 

614 self._lut = None 

615 if lut is not None: 

616 self.set_lookuptable(lut) 

617 self._lut = lut 

618 

619 def set_colors(self, colors): 

620 self._colors[:, :3] = colors 

621 self._update_colors() 

622 

623 def set_uniform_color(self, color): 

624 npolys = self.polydata.GetNumberOfCells() 

625 

626 colors = num.ones((npolys, 4)) 

627 colors[:, :3] *= color 

628 self._colors = colors 

629 

630 self._update_colors() 

631 

632 def set_alpha(self, alpha): 

633 self._colors[:, 3] = alpha 

634 self._update_colors() 

635 

636 def _update_colors(self): 

637 vcolors = numpy_to_vtk_colors(self._colors) 

638 self.polydata.GetCellData().SetScalars(vcolors) 

639 

640 def set_opacity(self, value): 

641 self.prop.SetOpacity(value) 

642 

643 def set_vertices(self, vertices): 

644 vpoints = vtk.vtkPoints() 

645 vpoints.SetNumberOfPoints(vertices.shape[0]) 

646 vpoints.SetData(numpy_to_vtk(vertices)) 

647 self.polydata.SetPoints(vpoints) 

648 

649 def set_values(self, values): 

650 vvalues = numpy_to_vtk(values.astype(num.float64)) 

651 self.polydata.GetCellData().SetScalars(vvalues) 

652 self.mapper.SetScalarRange(values.min(), values.max()) 

653 

654 def set_lookuptable(self, lut): 

655 if self._lut is not lut: 

656 self.mapper.SetUseLookupTableScalarRange(True) 

657 self.mapper.SetLookupTable(lut) 

658 self._lut = lut 

659 

660 def set_cpt(self, cpt): 

661 self.set_lookuptable(cpt_to_vtk_lookuptable(cpt)) 

662 

663 

664class ColorbarPipe(object): 

665 

666 def __init__(self, parent_pipe=None, cbar_title=None, cpt=None, lut=None): 

667 act = vtk.vtkScalarBarActor() 

668 

669 act.SetMaximumHeightInPixels(500) 

670 act.SetMaximumWidthInPixels(50) 

671 

672 try: 

673 act.SetUnconstrainedFontSize(True) 

674 except AttributeError: 

675 pass 

676 

677 self.prop = act.GetProperty() 

678 self.actor = act 

679 

680 self._format_text() 

681 self._set_position(0.95, 0.05) 

682 

683 if cbar_title is not None: 

684 self.set_title(cbar_title) 

685 

686 if cpt is not None: 

687 self.set_cpt(cpt) 

688 

689 if lut is not None: 

690 self.set_lookuptable(lut) 

691 

692 def set_lookuptable(self, lut): 

693 lut.Build() 

694 self.actor.SetLookupTable(lut) 

695 

696 def set_title(self, cbar_title): 

697 self.actor.SetTitle(cbar_title) 

698 

699 def _format_text(self): 

700 

701 prop_title = vtk.vtkTextProperty() 

702 prop_title.SetFontFamilyToArial() 

703 prop_title.SetColor(.8, .8, .8) 

704 prop_title.SetFontSize(int(prop_title.GetFontSize() * 1.3)) 

705 prop_title.BoldOn() 

706 self.actor.SetTitleTextProperty(prop_title) 

707 try: 

708 self.actor.SetVerticalTitleSeparation(20) 

709 except AttributeError: 

710 pass 

711 

712 prop_label = vtk.vtkTextProperty() 

713 prop_label.SetFontFamilyToArial() 

714 prop_label.SetColor(.8, .8, .8) 

715 prop_label.SetFontSize(int(prop_label.GetFontSize() * 1.1)) 

716 self.actor.SetLabelTextProperty(prop_label) 

717 

718 def _set_position(self, xpos, ypos): 

719 pos = self.actor.GetPositionCoordinate() 

720 pos.SetCoordinateSystemToNormalizedViewport() 

721 pos.SetValue(xpos, ypos) 

722 

723 

724class ArrowPipe(object): 

725 def __init__(self, start, end, value=None): 

726 from vtk import vtkMath as vm 

727 

728 arrow = vtk.vtkArrowSource() 

729 arrow.SetTipResolution(31) 

730 arrow.SetShaftResolution(21) 

731 arrow.Update() 

732 

733 normalized_x = [0.0] * 3 

734 normalized_y = [0.0] * 3 

735 normalized_z = [0.0] * 3 

736 

737 vm.Subtract(end, start, normalized_x) 

738 length = vm.Norm(normalized_x) 

739 vm.Normalize(normalized_x) 

740 

741 arbitrary = [0.0] * 3 

742 arbitrary[0] = vm.Random(-10, 10) 

743 arbitrary[1] = vm.Random(-10, 10) 

744 arbitrary[2] = vm.Random(-10, 10) 

745 vm.Cross(normalized_x, arbitrary, normalized_z) 

746 vm.Normalize(normalized_z) 

747 

748 vm.Cross(normalized_z, normalized_x, normalized_y) 

749 

750 matrix = vtk.vtkMatrix4x4() 

751 

752 matrix.Identity() 

753 for i in range(0, 3): 

754 matrix.SetElement(i, 0, normalized_x[i]) 

755 matrix.SetElement(i, 1, normalized_y[i]) 

756 matrix.SetElement(i, 2, normalized_z[i]) 

757 

758 transform = vtk.vtkTransform() 

759 transform.Translate(start) 

760 transform.Concatenate(matrix) 

761 transform.Scale(length, length, length) 

762 

763 transform_filter = vtk.vtkTransformPolyDataFilter() 

764 transform_filter.SetTransform(transform) 

765 transform_filter.SetInputConnection(arrow.GetOutputPort()) 

766 

767 mapper = vtk.vtkPolyDataMapper() 

768 mapper.SetInputConnection(transform_filter.GetOutputPort()) 

769 

770 act = vtk.vtkActor() 

771 act.SetMapper(mapper) 

772 

773 prop = act.GetProperty() 

774 self.prop = prop 

775 self.mapper = mapper 

776 self.actor = act 

777 

778 

779class Glyph3DPipe(object): 

780 def __init__(self, vertices, vectors, sizefactor=1.): 

781 assert len(vertices) == len(vectors) 

782 

783 if isinstance(vectors, list): 

784 vectors = num.array(vectors) 

785 

786 assert vectors.shape[1] == 3 

787 

788 vectors = vectors 

789 vpoints = vtk.vtkPoints() 

790 vpoints.SetNumberOfPoints(vertices.shape[0]) 

791 vpoints.SetData(numpy_to_vtk(vertices)) 

792 

793 vvectors = vtk.vtkDoubleArray() 

794 vvectors.SetNumberOfComponents(3) 

795 vvectors.SetNumberOfTuples(vectors.shape[0]) 

796 

797 for iv, vec in enumerate(vectors): 

798 for ic, comp in enumerate(vec): 

799 vvectors.SetComponent(iv, ic, comp) 

800 

801 pd = vtk.vtkPolyData() 

802 pd.SetPoints(vpoints) 

803 pd.GetPointData().SetVectors(vvectors) 

804 

805 arrow = vtk.vtkArrowSource() 

806 arrow.SetTipResolution(31) 

807 arrow.SetShaftResolution(21) 

808 arrow.Update() 

809 

810 glyph = vtk.vtkGlyph3D() 

811 if vtk.vtkVersion.GetVTKMajorVersion() > 5: 

812 glyph.SetSourceData(arrow.GetOutput()) 

813 glyph.SetInputData(pd) 

814 else: 

815 glyph.SetSource(arrow.GetOutput()) 

816 glyph.SetInput(pd) 

817 

818 glyph.ScalingOn() 

819 glyph.SetVectorModeToUseVector() 

820 glyph.OrientOn() 

821 glyph.SetScaleModeToScaleByVector() 

822 glyph.SetScaleFactor(10**sizefactor) 

823 glyph.Update() 

824 

825 mapper = vtk.vtkPolyDataMapper() 

826 mapper.SetInputConnection(glyph.GetOutputPort()) 

827 

828 act = vtk.vtkActor() 

829 act.SetMapper(mapper) 

830 

831 prop = act.GetProperty() 

832 self.prop = prop 

833 

834 self.polydata = pd 

835 self.mapper = mapper 

836 

837 # if scale_bar: 

838 # self.actor = [act, self.scale_bar_actor(glyph.GetScaleFactor())] 

839 # else: 

840 self.actor = act 

841 

842 def scale_bar_actor(self, ScalingFactor): 

843 leader = vtk.vtkLeaderActor2D() 

844 

845 pos = leader.GetPositionCoordinate() 

846 pos2c = leader.GetPosition2Coordinate() 

847 pos.SetCoordinateSystemToNormalizedViewport() 

848 pos2c.SetCoordinateSystemToNormalizedViewport() 

849 pos.SetValue(0.8, 0.12) 

850 pos2c.SetValue(0.9, 0.12) 

851 leader.SetArrowStyleToFilled() 

852 leader.SetLabel('Disp. = %.2f m' % 10.) 

853 leader.SetArrowPlacementToPoint1() 

854 

855 try: 

856 leader.SetUnconstrainedFontSize(True) 

857 except AttributeError: 

858 pass 

859 

860 prop_label = vtk.vtkTextProperty() 

861 prop_label.SetFontFamilyToArial() 

862 prop_label.BoldOn() 

863 prop_label.SetColor(.8, .8, .8) 

864 prop_label.SetJustificationToCentered() 

865 prop_label.SetVerticalJustificationToBottom() 

866 leader.SetLabelTextProperty(prop_label) 

867 leader.SetLabelFactor(0.5) 

868 leader.GetProperty().SetColor(1., 1., 0.69) 

869 

870 return leader