-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindexedMeshGrid.m
60 lines (51 loc) · 1.63 KB
/
indexedMeshGrid.m
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
function [varargout] = indexedMeshGrid(varargin)
% Create meshgrid of integer multiples of lattice vectors (2D or 3D)
% circular masking is performed to create radially symmetric grid
% input: spotcut, a1, a2, (a3)
% spotcut : determine radius of cutoff. radius = spotcut* |a1|
% a1,a2,a3 : lattice vectors, a3 is optional
% the lattice vectors are column vectors
%
% output: pos, h,k, (l)
% pos: n*3 matrix of mesh positions
% h,k,l: indices of pos
if length(varargin) == 4
a3 = varargin{4};
elseif length(varargin) == 3
a3 = [0;0;0];
end
spotcut = varargin{1};
a1 = varargin{2};
a2 = varargin{3};
if length(a1) == 2
a1 = [a1;0];
end
if length(a2) == 2
a2 = [a2;0];
end
if length(a3) ==2
a3 = [a3;0];
end
spotmax = ceil(spotcut);
[h,k,l] = ndgrid(-spotmax:spotmax,-spotmax:spotmax,-spotmax:spotmax);
cutoff = sqrt(dot(a1,a1))*spotcut*1.1; %k space cutoff
%cutoff = sqrt(dot(a1,a1))*sqrt(3)*1.1; %k space cutoff
h = reshape(h,[numel(h),1]);
k = reshape(k,[numel(k),1]);
l = reshape(l,[numel(l),1]);
x = h*a1(1)+k*a2(1)+l*a3(1);
y = h*a1(2)+k*a2(2)+l*a3(2);
z = h*a1(3)+k*a2(3)+l*a3(3);
r = sqrt(x.^2+y.^2+z.^2);
h(r> cutoff) = [];
k(r> cutoff) = [];
l(r> cutoff) = [];
x = h*a1(1)+k*a2(1)+l*a3(1);
y = h*a1(2)+k*a2(2)+l*a3(2);
z = h*a1(3)+k*a2(3)+l*a3(3);
pos = [x,y,z];
varargout{1} = pos;
varargout{2} = h;
varargout{3} = k;
varargout{4} = l;
end