def __init__(self, args): '''Init a new View window.''' #print(args) # create the 3D scene s3d = Scene3D(display=True, ren_size=(800, 800)) if isinstance(args, list): if len(args) == 1: print( 'Please specify the file representing the 3D object to view' ) sys.exit(1) elif len(args) == 2: file_path = args[1] else: print( 'Please use only one parameter (the path to the file representing the 3D object to view)' ) sys.exit(1) (path, ext) = os.path.splitext(file_path) ext = ext.strip('.') print(ext) if ext in ['stl', 'STL']: actor = load_STL_actor(path, ext) else: print('Unrecognized file extenstion: %s' % ext) sys.exit(1) elif isinstance(args, Grain): actor = grain_3d(args) elif isinstance(args, Orientation): l = Lattice.cubic(1.0) (a, b, c) = l._lengths grid = lattice_grid(l) actor = lattice_edges(grid) actor.SetOrigin(a / 2, b / 2, c / 2) actor.AddPosition(-a / 2, -b / 2, -c / 2) apply_orientation_to_actor(actor, args) elif isinstance(args, Lattice): (a, b, c) = args._lengths actor = lattice_3d(args) actor.SetOrigin(a / 2, b / 2, c / 2) actor.AddPosition(-a / 2, -b / 2, -c / 2) elif isinstance(args, np.ndarray): actor = show_array(args) elif isinstance(args, vtk.vtkActor): actor = args else: raise ValueError('unsupported object type: {0}'.format(type(args))) bounds = actor.GetBounds() size = (bounds[1] - bounds[0], bounds[3] - bounds[2], bounds[5] - bounds[4]) # bounds[1::2] print(size) axes = axes_actor(length=np.mean(size), fontSize=60) s3d.add(axes) s3d.add(actor) cam = setup_camera(size) cam.SetFocalPoint(0.5 * (bounds[0] + bounds[1]), 0.5 * (bounds[2] + bounds[3]), 0.5 * (bounds[4] + bounds[5])) s3d.set_camera(cam) s3d.render(key_pressed_callback=True)
def __init__(self, microstructure=None, lattice=None, axis='Z', hkl='111', proj='stereo', verbose=False): """ Create an empty PoleFigure object associated with an empty Microstructure. :param microstructure: the :py:class:`~pymicro.crystal.microstructure.Microstructure` containing the collection of orientations to plot (None by default). :param lattice: the crystal :py:class:`~pymicro.crystal.lattice.Lattice`. :param str axis: the pole figure axis ('Z' by default), vertical axis in the direct pole figure and direction plotted on the inverse pole figure. .. warning:: Any crystal structure is now supported (you have to set the proper crystal lattice) but it has only really be tested for cubic. :param str hkl: slip plane family ('111' by default) :param str proj: projection type, can be either 'stereo' (default) or 'flat' :param bool verbose: verbose mode (False by default) """ self.proj = proj self.axis = axis self.map_field = None if microstructure: self.microstructure = microstructure else: self.microstructure = Microstructure() if lattice: self.lattice = lattice else: self.lattice = Lattice.cubic(1.0) self.family = None self.poles = [] self.set_hkl_poles(hkl) self.verbose = verbose self.mksize = 12 self.color_by_grain_id = False self.pflegend = False self.x = np.array([1., 0., 0.]) self.y = np.array([0., 1., 0.]) self.z = np.array([0., 0., 1.]) # list all crystal directions self.c001s = np.array([[0, 0, 1], [0, 1, 0], [1, 0, 0]], dtype=np.float) self.c011s = np.array([[0, 1, 1], [1, 0, 1], [1, 1, 0], [0, -1, 1], [-1, 0, 1], [-1, 1, 0]], dtype=np.float) / np.sqrt(2) self.c111s = np.array([[1, 1, 1], [-1, -1, 1], [1, -1, 1], [-1, 1, 1]], dtype=np.float) / np.sqrt(3)
def test_apply_orientation_to_actor(self): o = Orientation.from_rodrigues([0.0885, 0.3889, 0.3268]) Bt = o.orientation_matrix().transpose( ) # to go from crystal to lab coordinate Vl = Bt.Vc l = Lattice.cubic(1.0) (a, b, c) = l._lengths grid = lattice_grid(l) actor = lattice_edges(grid) apply_orientation_to_actor(actor, o) m = actor.GetUserTransform().GetMatrix() for i in range(3): for j in range(3): self.assertEqual(Bt[i, j], m.GetElement(i, j))
def __init__(self, arg): """Init a new View window. :param arg: a descriptor of the object to view, it can be an instance of `Grain`, `Orientation`, `Lattice`, a vtkActor, a 3D numpy array or the path to a STL file. """ # create the 3D scene s3d = Scene3D(display=True, ren_size=(800, 800)) if isinstance(arg, str): (path, ext) = os.path.splitext(arg) ext = ext.strip('.') print(ext) if ext in ['stl', 'STL']: actor = load_STL_actor(path, ext) else: print('Unrecognized file extension: %s' % ext) sys.exit(1) elif isinstance(arg, Grain): actor = grain_3d(arg) elif isinstance(arg, Orientation): l = Lattice.cubic(1.0) (a, b, c) = l._lengths grid = lattice_grid(l) actor = lattice_edges(grid) actor.SetOrigin(a / 2, b / 2, c / 2) actor.AddPosition(-a / 2, -b / 2, -c / 2) apply_orientation_to_actor(actor, arg) elif isinstance(arg, Lattice): (a, b, c) = arg._lengths actor = lattice_3d(arg) actor.SetOrigin(a / 2, b / 2, c / 2) actor.AddPosition(-a / 2, -b / 2, -c / 2) elif isinstance(arg, np.ndarray): if arg.ndim != 3: print('Only 3D arrays can be viewed with this method.') sys.exit(1) actor = show_array(arg) elif isinstance(arg, vtk.vtkActor): actor = arg else: raise ValueError('unsupported object type: {0}'.format(type(arg))) bounds = actor.GetBounds() size = (bounds[1] - bounds[0], bounds[3] - bounds[2], bounds[5] - bounds[4]) # bounds[1::2] print(size) axes = axes_actor(length=np.mean(size), fontSize=60) s3d.add(axes) s3d.add(actor) cam = setup_camera(size) cam.SetFocalPoint(0.5 * (bounds[0] + bounds[1]), 0.5 * (bounds[2] + bounds[3]), 0.5 * (bounds[4] + bounds[5])) s3d.set_camera(cam) s3d.render(key_pressed_callback=True)
def test_dct_omega_angles(self): # test with a BCC Titanium lattice lambda_keV = 30 lambda_nm = 1.2398 / lambda_keV a = 0.3306 # lattice parameter in nm Ti_bcc = Lattice.cubic(a) (h, k, l) = (0, 1, 1) hkl = HklPlane(h, k, l, lattice=Ti_bcc) o = Orientation.from_euler((103.517, 42.911, 266.452)) theta = hkl.bragg_angle(lambda_keV, verbose=False) gt = o.orientation_matrix( ) # our B (here called gt) corresponds to g^{-1} in Poulsen 2004 A = h * gt[0, 0] + k * gt[1, 0] + l * gt[2, 0] B = -h * gt[0, 1] - k * gt[1, 1] - l * gt[2, 1] C = -2 * a * np.sin( theta )**2 / lambda_nm # the minus sign comes from the main equation Delta = 4 * (A**2 + B**2 - C**2) self.assertEqual(Delta > 0, True) t1 = (B - 0.5 * np.sqrt(Delta)) / (A + C) t2 = (B + 0.5 * np.sqrt(Delta)) / (A + C) # verifying A cos(w) + B sin(w) = C:' for t in (t1, t2): x = A * (1 - t**2) / (1 + t**2) + B * 2 * t / (1 + t**2) self.assertAlmostEqual(x, C, 2) # verifying (A + C) * t**2 - 2 * B * t + (C - A) = 0' for t in (t1, t2): self.assertAlmostEqual((A + C) * t**2 - 2 * B * t + (C - A), 0.0, 2) (w1, w2) = o.dct_omega_angles(hkl, lambda_keV, verbose=False) self.assertAlmostEqual(w1, 196.709, 2) self.assertAlmostEqual(w2, 28.334, 2) # test with an FCC Aluminium-Lithium lattice a = 0.40495 # lattice parameter in nm Al_fcc = Lattice.face_centered_cubic(a) hkl = HklPlane(-1, 1, 1, Al_fcc) o = Orientation.from_rodrigues([0.0499, -0.3048, 0.1040]) w1, w2 = o.dct_omega_angles(hkl, 40, verbose=False) self.assertAlmostEqual(w1, 109.2, 1) self.assertAlmostEqual(w2, 296.9, 1)
def set_material(self, material): if material is None: material = Lattice.cubic(1.0) self.material = material
with a small offset so it is displayed nicely. ''' # create the 3D scene base_name = os.path.splitext(__file__)[0] s3d = Scene3D(display=False, ren_size=(800, 400), name=base_name) # create all the different unit lattice cells a = 1.0 b = 1.5 c = 2.0 alpha = 66 beta = 66 gamma = 66 l = Lattice.cubic(a) cubic = lattice_3d(l) apply_translation_to_actor(cubic, (0.5, 0.5, 0.0)) l = Lattice.tetragonal(a, c) tetragonal = lattice_3d(l) apply_translation_to_actor(tetragonal, (2.0, 2.0, 0.0)) l = Lattice.orthorombic(a, b, c) orthorombic = lattice_3d(l) apply_translation_to_actor(orthorombic, (3.5, 3.5, 0.0)) l = Lattice.hexagonal(a, c) hexagonal = lattice_3d(l) apply_translation_to_actor(hexagonal, (5.0, 5.0, 0.0))
def test_bragg_angle(self): l = Lattice.cubic(0.287) # FCC iron hkl = HklPlane(2, 0, 0, l) # 200 reflection at 8 keV is at 32.7 deg self.assertAlmostEqual(hkl.bragg_angle(8), 0.5704164)
def test_volume(self): l = Lattice.cubic(0.5) self.assertAlmostEqual(l.volume(), 0.125)
def test_cubic(self): a = np.array([[0.5, 0., 0.], [0., 0.5, 0.], [0., 0., 0.5]]) l = Lattice.cubic(0.5) for i in range(0, 3): for j in range(0, 3): self.assertEqual(l.matrix[i][j], a[i][j])
def test_equality(self): l1 = Lattice.cubic(0.5) a = np.array([[0.5, 0., 0.], [0., 0.5, 0.], [0., 0., 0.5]]) l2 = Lattice(a, symmetry=Symmetry.cubic) self.assertEqual(l1, l2)