function cell_els = LEM_cell_els(cells,pt1,pt2,step)
% LEM_cell_els - find cell elements traversed from pt1 to pt2
% On input:
%     cells (grid struct): defines cells
%       .x_min (float): x min for cells
%       .y_min (float): y min for cells
%       .x_max (float): x max for cells
%       .y_max (float): y max for cells
%       .del_x (float): x axis spacing
%       .del_y (float): y axis spacing
%     pt1 (1x3 vector): 3D point
%     pt2 (1x3 vector): 3D point
%     step (float): dist to move along segment pt1-pt2
% On output:
%     cell_els (nx1 vector): cell elements (linearized by column)
% Call:
%     flights(f).cell_els = LEM_cell_els(grid,traj(1,1:3),traj(end,4:6),1);
% Author:
%     T. Henderson
%     UU
%     Spring 2020
%

cell_els = [];
M = cells.M;
N = cells.N;

p = pt1;
dist12 = norm(pt2-pt1);
dir12 = pt2 - pt1;
dir12 = dir12/norm(dir12);
perp = [-dir12(2),dir12(1)];
while norm(pt1-p)<dist12
    [row,col] = LEM_row_col(cells,p);
    el = (col-1)*M + row;
    if isempty(find(el==cell_els))
        cell_els = [cell_els,el];
    end
    [row1,col1] = LEM_row_col(cells,p+step*perp);
    el1 = (col1-1)*M + row1;
    if isempty(find(el1==cell_els))
        cell_els = [cell_els,el1];
    end
    [row2,col2] = LEM_row_col(cells,p+-step*perp);
    el2 = (col2-1)*M + row2;
    if isempty(find(el2==cell_els))
        cell_els = [cell_els,el2];
    end
    p = p + dir12*step;
end
tch = 0;
