function[lidarResults sampleVegLidar sampleGrdLidar]=sampleLidar_forestClass(polyVertex, lidarVeg, lidarGrd,samplePt)

%this function samples lidar points along the transect within an x by x
%sampling square

%polyVertex = x and y values for polygon
% xv=xvertices of the polygon
% yv = yvertices of the polygon
% lidarVeg = lidar points (veg only)
% lidarGrd = lidar points (Ground only)

%might want to go back and clear out some variables in this code later....

%{
% ----------------------------- BEGIN code for testing only -----------
%save pn_perp_plot(:,:,j) samplePt
%save bufferSamplePoly(:,:,j) polyVertex
%%


%save samplePolyVert_temp samplePolyVert
%%
%load samplePolyVert_temp
%load bufferPolyVert_StrmCurrent 
%load samplePoly  %polyVertex
%load perpPPL %samplePt

polyVertex=(bufferSamplePoly(:,:,j));
samplePt=(pn_perp_plot(:,:,j));
%A is a temporary sample set of points to use

% will create a for loop that will go through each sample polygon in the
% matrix
%this is a chunk of lidar to use for testing
%load streamVegLidar
%load streamGrdLidar

%load Stream
%disp('lidar data loaded')

lidarVeg=streamVegLidar;
lidarGrd=streamGrdLidar;
% ----------------------------- END code for testing only ---------------

%}
totalSamples = (length(polyVertex(:,1)));
xv=zeros(totalSamples,5);
yv=zeros(totalSamples,5);

%progressbar('Calculating FC and Av Ht within Sample Boxes for Transect (width)');
%for s=1:8
for s=1:totalSamples  
 %  progressbar(s/totalSamples);
    
   % disp(s)    
    xv(s,:)=[polyVertex(s,10);polyVertex(s,8);polyVertex(s,4);polyVertex(s,6);polyVertex(s,10)];
    yv(s,:)=[polyVertex(s,11);polyVertex(s,9);polyVertex(s,5);polyVertex(s,7);polyVertex(s,11)];

if polyVertex(s,4) == 0
    %assign FC values to zeros across the board... don't calculate lidar
    %values
      maxHeight=0;
      FCSample=0;
      pctX0= [0 0 0 0];
      totalGrdPoints=0;
      totalVegPoints=0;
    
else
    
%%

% find ground and veg lidar points within the sample box
inVeg = inpolygon(lidarVeg(:,1),lidarVeg(:,2),xv(s,:),yv(s,:));
inGrd = inpolygon(lidarGrd(:,1),lidarGrd(:,2),xv(s,:),yv(s,:));

% find index values of lidar points within the sample box
closestIndexVeg=find(inVeg == 1);
closestIndexGrd=find(inGrd == 1);


%build a matrix of points that are within the sample box -
%preallocate lidar matrices and clear before loop
totalVeg=length(closestIndexVeg);
sampleVegLidar_temp=zeros(totalVeg,3);
totalGrd=length(closestIndexGrd);
sampleGrdLidar_temp=zeros(totalGrd,3);

    if isempty(closestIndexVeg)
        totalVegPoints=0;
        vegGr2m=0;
    else
        
    for pointsVeg=1:length(closestIndexVeg)
        sampleVegLidar_temp(pointsVeg,:)=(lidarVeg(closestIndexVeg(pointsVeg),1:3));
    end
        vegGr2m_index = find(sampleVegLidar_temp(:,3) >= 6.56); %find total points taller than 2m(DBH) to calculate FC
        vegGr2m = length(vegGr2m_index);
        %create matrix of lidar veg points taller than 2m to calculate
        %overstory height (percentile values)
        
        sampleVegLidar_Gr2m_temp=zeros(vegGr2m,3);
        
        for l=1:vegGr2m
        %sampleVegLidar_Gr2m_temp(l,:) = [sampleVegLidar_temp(vegGr2m_index(l),1) sampleVegLidar_temp(vegGr2m_index(l),2) sampleVegLidar_temp(vegGr2m_index(l),3)];
         sampleVegLidar_Gr2m_temp(l,:) = (sampleVegLidar_temp(vegGr2m_index(l),1:3));
        end
       
        %this still isn't working quite right... i'm getting different
        %points...
        
        totalVegPoints=length(sampleVegLidar_temp);
    end

%calculate lidar ground points within sample box
    if isempty(closestIndexGrd)
        totalGrdPoints=0;
    else
        for pointsGrd=1:length(closestIndexGrd)
            %sampleGrdLidar_temp(pointsGrd,:)=[lidarGrd(closestIndexGrd(pointsGrd),1) lidarGrd(closestIndexGrd(pointsGrd),2) lidarGrd(closestIndexGrd(pointsGrd),3)];
            sampleGrdLidar_temp(pointsGrd,:)=(lidarGrd(closestIndexGrd(pointsGrd),1:3));
        end
        totalGrdPoints=length(sampleGrdLidar_temp);
    end
        
    % ----------------------------BEGIN USE FOR PLOTTING SAMPLE POINTS
    if s==1
        sampleVegLidar=(sampleVegLidar_temp);
        sampleGrdLidar=(sampleGrdLidar_temp);
    else
        sampleVegLidar=[sampleVegLidar; sampleVegLidar_temp];
        sampleGrdLidar=[sampleGrdLidar; sampleGrdLidar_temp];
    end
    % ----------------------------END USE FOR PLOTTING SAMPLE POINTS
    
% heights=sampleVegLidar_Gr2m_temp(:,3);
 FCSample=(vegGr2m / (totalGrdPoints+totalVegPoints));
 maxHeight=max(sampleVegLidar_temp(:,3));
  if isempty(maxHeight)
     maxHeight=0; 
  end
  if isempty(FCSample)
      FCSample=0;
  end
 %pctX0= prctile(heights,[10,20,70,90]);
end

 %pn_perp_plot(:,3,j,Name) pn_perp_plot(:,4,j,Name)
 lidarResults(s,:)=[polyVertex(s,1:3) samplePt(s,3) samplePt(s,4) maxHeight FCSample ];

end
%disp('done processing lidar samples')

%{
%*************************************************************************
%BEGIN PLOT -------------- remove for function  --------------------


NotIn=[lidarVeg(~inVeg,1) lidarVeg(~inVeg,2)];

     for boxes=1:length(xv)
       plot(xv(boxes,:),yv(boxes,:),'-g+','markerSize',4), hold on
     end

%plot(lidarVeg(1:5:end,1),lidarVeg(1:5:end,2),'ro','MarkerFaceColor','r','markerSize',2), hold on
plot(lidarGrd(1:end,1),lidarGrd(1:end,2),'co','MarkerFaceColor','c','markerSize',2), hold on

plot(sampleVegLidar(:,1),sampleVegLidar(:,2),'bo','markerSize',2,'MarkerFaceColor','b'), hold on
plot(sampleGrdLidar(:,1),sampleGrdLidar(:,2),'k*','markerSize',6), hold on

%plot streams
%%
plot(Stream(1:10,5),Stream(1:10,6),'--bo','LineWidth',1.25), hold on
plot(Stream(11:24,5),Stream(11:24,6),'--bo','LineWidth',1.25), hold on
%%
%plot(xv,yv,'-k*','MarkerFaceColor','k','markerSize',10), hold on

%plot sample polygon boundaries
%{
plot(polyVertex(:,7),polyVertex(:,8),'k*','MarkerFaceColor','k','markerSize',5), hold on
plot(polyVertex(:,3),polyVertex(:,4),'k*','MarkerFaceColor','k','markerSize',5), hold on
plot(polyVertex(:,5),polyVertex(:,6),'k*','MarkerFaceColor','k','markerSize',5), hold on
plot(polyVertex(:,9),polyVertex(:,10),'k*','MarkerFaceColor','k','markerSize',5), hold on
%}

%plot(sampleGrdLidar_temp(:,1),sampleGrdLidar_temp(:,2),'ko','markerSize',2,'MarkerFaceColor','k'), hold on
%plot(NotIn(1:10:end,1),NotIn(1:10:end,2),'ro','markerSize',2,'MarkerFaceColor','r')
axis equal
 
% END PLOT -------------- remove for function  --------------------

%}
