function image_mesh2d ( filename )

%*****************************************************************************80
%
%% image_mesh2d() meshes a region given its boundary curve.
%
%  Discussion:
%
%    Read the coordinates of a sequence of boundary points.
%    Use mesh2d() to create a mesh in the interior.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    28 February 2024
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    string filename: the name of a file containing a list of (x,y) 
%    coordinates of points bounding some region.
%
  addpath ( '../mesh2d' );

  fprintf ( 1, '\n' );
  fprintf ( 1, 'image_mesh2d():\n' );
  fprintf ( 1, '  Read points which bound a region.\n' );
  fprintf ( 1, '  Call mesh2d() to mesh the interior.\n' );
%
%  If filename not supplied, get it.
%
  if ( nargin < 1 )
    filename = input ( '  Enter the name of the boundary data file.' );
  end
%
%  Break the filename up into pieces.
%
  [ path, name, extension ] = fileparts ( filename );
%
%  Read the vertices that trace the boundary of the region.
%  These may have been generated by using the code "image_sample.m".
%
  v = load ( filename );

  v_num = length ( v );
  fprintf ( 1, '\n' );
  fprintf ( 1, '  %d vertex coordinates read from "%s"\n', filename );
%
%  Display the vertices.
%
  figure ( 1 );
  clf ( );
  hold ( 'on' );
    plot ( v(:,1), v(:,2), 'r-' );
    plot ( v(:,1), v(:,2), 'b.', 'markersize', 25 );
  hold ( 'off' );
  grid ( 'on' );
  title ( '  Boundary vertices' );
  axis ( 'equal' );
  filename = strcat ( name, '_vertices.png' );
  print ( '-dpng', filename );
  fprintf ( 1, '  Graphics saved as "%s"\n', filename );
%
%  Set up the edge array.
%
  e = zeros ( v_num, 2 );
  e(1:v_num,1) = ( 1 : v_num )';
  e(1:v_num-1,2) = ( 2 : v_num )';
  e(v_num,2) = 1;
%
%  Create the hdata structure for mesh2d().
%
  hdata = [];
%
%  Where desirable, you can set the hdata.hmax field 
%  to specify the maximum element size.
%
  if ( false )
    hdata.hmax = 100.00;
  end
%
%  Call mesh2d() to determine the triangulation.
%
  [ p, t ] = mesh2d ( v, e, hdata );
  filename = strcat ( name, '_mesh.png' );
  print ( '-dpng', filename );
  fprintf ( 1, '  Graphics saved as "%s"\n', filename );
%
%  Count and report the vertices, nodes and elements.
%
  nv = size ( v, 1 );
  np = size ( p, 1 );
  nt = size ( t, 1 );
  fprintf ( 1, ...
    '  %d boundary vertices input, %d nodes and %d triangles created\n', ...
    nv, np, nt );
%
%  Write the node and element data to files.
%
  node_filename = strcat ( name, '_nodes.txt' );
  save ( node_filename, 'p', '-ascii' );
  fprintf ( 1, '  Nodes saved as "%s"\n', node_filename );

  element_filename = strcat ( name, '_elements.txt' );
  save ( element_filename, 't', '-ascii' );
  fprintf ( 1, '  Elements saved as "%s"\n', element_filename );
  
  rmpath ( '../mesh2d' )

  return
end

