root / ase / visualize / vtk / cell.py @ 4
Historique | Voir | Annoter | Télécharger (3,96 ko)
| 1 |
|
|---|---|
| 2 |
import numpy as np |
| 3 |
|
| 4 |
from ase import Atoms |
| 5 |
|
| 6 |
from vtk import vtkOutlineSource, vtkAxesActor, vtkProperty2D, vtkTextProperty |
| 7 |
from ase.visualize.vtk.module import vtkModule, vtkPolyDataModule |
| 8 |
|
| 9 |
# -------------------------------------------------------------------
|
| 10 |
|
| 11 |
class vtkUnitCellModule(vtkPolyDataModule): |
| 12 |
def __init__(self, atoms): |
| 13 |
|
| 14 |
assert isinstance(atoms, Atoms) |
| 15 |
self.pbc = atoms.get_pbc()
|
| 16 |
|
| 17 |
cell = atoms.get_cell() |
| 18 |
|
| 19 |
"""
|
| 20 |
if not isinstance(cell, np.ndarray):
|
| 21 |
cell = np.array(cell)
|
| 22 |
|
| 23 |
if cell.shape == (3,):
|
| 24 |
cell = np.diag(cell)
|
| 25 |
|
| 26 |
assert cell.dtype == float and cell.shape == (3, 3)
|
| 27 |
"""
|
| 28 |
|
| 29 |
self.vtk_outline = vtkOutlineSource()
|
| 30 |
|
| 31 |
if (cell - np.diag(cell.diagonal())).any():
|
| 32 |
corners = np.empty((8,3), dtype=float) |
| 33 |
# edges = [map(int,[(i-1)%2==0,i%4>=2,i>=4]) for i in range(8)]
|
| 34 |
for c,a in enumerate([(0, 0, 0), (1, 0, 0), (0, 1, 0), (1, 1, 0), \ |
| 35 |
(0, 0, 1), (1, 0, 1), (0, 1, 1), (1, 1, 1)]): |
| 36 |
corners[c] = np.dot(a, cell) |
| 37 |
self.bbox = np.array(zip(np.min(corners, axis=0), \ |
| 38 |
np.max(corners, axis=0))).ravel()
|
| 39 |
self.vtk_outline.SetCorners(corners.ravel())
|
| 40 |
self.vtk_outline.SetBoxTypeToOriented()
|
| 41 |
else:
|
| 42 |
self.bbox = np.array(zip(np.zeros(3),cell.diagonal())).ravel() |
| 43 |
self.vtk_outline.SetBounds(self.bbox) |
| 44 |
|
| 45 |
vtkPolyDataModule.__init__(self, self.vtk_outline) |
| 46 |
|
| 47 |
def get_bounding_box(self): |
| 48 |
return self.bbox |
| 49 |
|
| 50 |
def get_size(self): |
| 51 |
return self.bbox[1::2]-self.bbox[0::2] |
| 52 |
|
| 53 |
def get_pbc(self): |
| 54 |
return self.pbc |
| 55 |
|
| 56 |
def get_characteristic_length(self): |
| 57 |
return np.prod(self.get_size())**(1.0/3.0) |
| 58 |
|
| 59 |
# -------------------------------------------------------------------
|
| 60 |
|
| 61 |
class vtkAxesModule(vtkModule): |
| 62 |
def __init__(self, cell): |
| 63 |
|
| 64 |
assert isinstance(cell, vtkUnitCellModule) |
| 65 |
self.cell = cell
|
| 66 |
|
| 67 |
l0 = self.cell.get_characteristic_length()
|
| 68 |
|
| 69 |
# Create VTK axes actor (not really a VTK actor though)
|
| 70 |
self.vtk_ax = vtkAxesActor()
|
| 71 |
self.vtk_ax.SetTipTypeToCone()
|
| 72 |
self.vtk_ax.SetConeRadius(5e-2*l0) |
| 73 |
self.vtk_ax.SetShaftTypeToCylinder()
|
| 74 |
self.vtk_ax.SetCylinderRadius(5e-3*l0) |
| 75 |
|
| 76 |
# Create VTK two-dimensional property
|
| 77 |
p2d = vtkProperty2D() |
| 78 |
p2d.SetDisplayLocationToBackground() |
| 79 |
|
| 80 |
vtkModule.__init__(self, self.vtk_ax, p2d) |
| 81 |
|
| 82 |
# Create VTK text property and apply to axes
|
| 83 |
vtk_textproperty = vtkTextProperty() |
| 84 |
vtk_textproperty.SetFontSize(14)
|
| 85 |
vtk_textproperty.SetBold(True)
|
| 86 |
vtk_textproperty.SetItalic(True)
|
| 87 |
vtk_textproperty.SetShadow(True)
|
| 88 |
vtk_textproperty.SetJustificationToRight() |
| 89 |
vtk_textproperty.SetVerticalJustificationToCentered() |
| 90 |
|
| 91 |
self.set_text_property(vtk_textproperty)
|
| 92 |
|
| 93 |
def set_actor(self, vtk_act): |
| 94 |
assert isinstance(vtk_act, vtkAxesActor) #fix for non-vtkActor actor |
| 95 |
self.vtk_act = vtk_act
|
| 96 |
|
| 97 |
def set_property(self, vtk_property): |
| 98 |
assert isinstance(vtk_property, vtkProperty2D) |
| 99 |
for vtk_cap in [self.vtk_ax.GetXAxisCaptionActor2D(), |
| 100 |
self.vtk_ax.GetYAxisCaptionActor2D(),
|
| 101 |
self.vtk_ax.GetZAxisCaptionActor2D()]:
|
| 102 |
#vtk_cap.ThreeDimensionalLeaderOn()
|
| 103 |
#vtk_cap.LeaderOn()
|
| 104 |
vtk_cap.SetProperty(vtk_property) |
| 105 |
vtk_txt = vtk_cap.GetTextActor() |
| 106 |
vtk_txt.SetProperty(vtk_property) |
| 107 |
|
| 108 |
def set_text_property(self, vtk_textproperty, scaled=False): |
| 109 |
assert isinstance(vtk_textproperty, vtkTextProperty) |
| 110 |
for vtk_cap in [self.vtk_ax.GetXAxisCaptionActor2D(), |
| 111 |
self.vtk_ax.GetYAxisCaptionActor2D(),
|
| 112 |
self.vtk_ax.GetZAxisCaptionActor2D()]:
|
| 113 |
vtk_txt = vtk_cap.GetTextActor() |
| 114 |
vtk_txt.SetScaledText(scaled) |
| 115 |
vtk_txt.SetTextProperty(vtk_textproperty) |
| 116 |
|