% plottri.m
% plots the triangulation for a 3D KRAKEN run

fid = fopen( 'SL.flp' );
title = fgetl( fid );
opt   = fgetl( fid );
m     = fscanf( fid, '%i', 1 );
temp  = fgetl( fid );

xs  = fscanf( fid, '%f', 1 );
ys  = fscanf( fid, '%f', 1 );
temp  = fgetl( fid );

% receiver ranges

temp = fgetl( fid );
temp = fgetl( fid );
temp = fscanf( fid, '%f', 3 );

rmin = temp( 1 );
rmax = temp( 2 );
nr   = temp( 3 );
temp = fgetl( fid );

% angles

temp = fscanf( fid, '%f', 3 );
ntheta = temp( 1 );
theta = linspace( temp( 2), temp( 3 ), ntheta );
temp = fgetl( fid );

% nodes
nnodes = fscanf( fid, '%i', 1 );
for inode =1:nnodes
   temp = fscanf( fid, '%f', 2 );
   x( inode ) = temp( 1 );
   y( inode ) = temp( 2 );
   temp = fgetl( fid );
end

figure
hold on

% elements

nelts = fscanf( fid, '%i', 1 );
for ielt = 1:nelts
   temp = fscanf( fid, '%i', 3 );
   xv = [ x( temp( 1 ) ) x( temp( 2 ) ) x( temp( 3 ) ) ];
   yv = [ y( temp( 1 ) ) y( temp( 2 ) ) y( temp( 3 ) ) ];
   plot( xv, yv )
end

% Plot the bearing lines

clear xv yv

for ith = 1:ntheta
   xv( 1 ) = xs + rmin * cos( deg2rad( theta( ith ) ) );
   yv( 1 ) = ys + rmin * sin( deg2rad( theta( ith ) ) );
   xv( 2 ) = xs + rmax * cos( deg2rad( theta( ith ) ) );
   yv( 2 ) = ys + rmax * sin( deg2rad( theta( ith ) ) );
   bearing = plot( xv, yv, 'r' );
   set( bearing, 'LineWidth', 2 )

end

axis equal
xlabel( 'x (km)' );
ylabel( 'y (km)' );