% Copyright 2017 The Johns Hopkins University Applied Physics Laboratory.
% Licensed under the MIT License. See LICENSE.txt in the project root for full license information.

% writes data into las format. It loads a sample las file to get all the
% secondary fields
%
% Version History
%   2017/03/28  Marc Bosch       Initial version
function [filenameLAS,testobg]=writeMeshNodes2LAS(node_xyz,filename)
resultpath=[];
lasobj=load('lasdatastruct.mat');%read las structure and fill in the fields we care
testobg=lasobj.testobj3;
testobg.scan_angle=int8(zeros(length(node_xyz(2,:)),1));
testobg.selection=logical(zeros(length(node_xyz(2,:)),1));
testobg.intensity=uint16(zeros(length(node_xyz(2,:)),1));
testobg.classification=uint16(zeros(length(node_xyz(2,:)),1));
testobg.point_source_id = zeros(length(node_xyz(2,:)),1,'uint16');
testobg.gps_time = zeros(length(node_xyz(2,:)),1,'double');
testobg.red = zeros(length(node_xyz(2,:)),1,'uint16');
testobg.green = zeros(length(node_xyz(2,:)),1,'uint16');
testobg.blue = zeros(length(node_xyz(2,:)),1,'uint16');
testobg.nir = zeros(length(node_xyz(2,:)),1,'uint16');
testobg.wave_packet_descriptor = zeros(length(node_xyz(2,:)),1,'uint8');
testobg.wave_byte_offset = zeros(length(node_xyz(2,:)),1,'uint64');
testobg.wave_packet_size = zeros(length(node_xyz(2,:)),1,'uint32');
testobg.wave_return_point = zeros(length(node_xyz(2,:)),1,'single');
testobg.Xt = zeros(length(node_xyz(2,:)),1,'single');
testobg.Yt = zeros(length(node_xyz(2,:)),1,'single');
testobg.Zt = zeros(length(node_xyz(2,:)),1,'single');   
testobg.bits = zeros(length(node_xyz(2,:)),1,'uint8');
testobg.user_data=zeros(length(node_xyz(2,:)),1,'uint8');
testobg.z=node_xyz(3,:);
testobg.y=node_xyz(2,:);
testobg.x=node_xyz(1,:);
testobg.header.number_of_point_records=length(node_xyz(:,1));
testobg.header.min_x=min(testobg.x);
testobg.header.max_x=max(testobg.x);
testobg.header.min_y=min(testobg.y);
testobg.header.max_y=max(testobg.y);
testobg.header.min_z=min(testobg.z);
testobg.header.max_z=max(testobg.z);
testobg.header.x_offset=min(testobg.x);
testobg.header.y_offset=min(testobg.y);
testobg.header.z_offset=min(testobg.z);
if strcmp(filename(end-3:end),'.las')==1
    filenameLAS=filename;
else
    filenameLAS=[filename '.las'];
end
resultpath=filenameLAS;
write_las(testobg, resultpath);