function xyz_display ( xyz_filename )

%*****************************************************************************80
%
%% xyz_display() plots a set of points in 3D.
%
%  Discussion:
%
%    The data is stored in a text file with N rows and 3 columns.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    09 July 2024
%
%  Author:
%
%    John Burkardt
%
%  Usage:
%
%    xyz_display ( 'filename.xyz' )
%
%  Input:
%
%    string xyz_filename: the name of the data file.
%

%
%  First argument is the point file.
%
  if ( nargin < 1 )
    fprintf ( 1, '\n' );
    fprintf ( 1, 'xyz_display():\n' );
    xyz_filename = input ( '  Enter the name of the XYZ file (in ''quotes''!).\n' );
  end
%
%  Read the data.
%
  xyz = load ( xyz_filename );
  [ point_num, dim ] = size ( xyz );

  fprintf ( 1, '\n' );
  fprintf ( 1, '  Number of points POINT_NUM  = %d\n', point_num );
  fprintf ( 1, '\n' );
  fprintf ( 1, '  First 5 nodes:\n' );
  disp ( xyz(1:min(point_num,5),1:3) );

  clf ( );
  scatter3 ( xyz(:,1), xyz(:,2), xyz(:,3), 'filled', 'b' );

  xlabel ( '--X axis--' )
  ylabel ( '--Y axis--' )
  zlabel ( '--Z axis--' )
%
%  The TITLE function will interpret underscores in the title.
%  We need to unescape such escape sequences!
%
  title_string = s_escape_tex ( xyz_filename );
  title ( title_string )
%
%  Adjust the plot ranges.
%
  xyz_min(1) = min ( xyz(:,1) );
  xyz_max(1) = max ( xyz(:,1) );

  xyz_min(2) = min ( xyz(:,2) );
  xyz_max(2) = max ( xyz(:,2) );

  xyz_min(3) = min ( xyz(:,3) );
  xyz_max(3) = max ( xyz(:,3) );

  xyz_range(1:3) = xyz_max(1:3) - xyz_min(1:3);

  margin = 0.025 * max ( xyz_range(1), ...
                   max ( xyz_range(2), xyz_range(3) ) );

  x_min = xyz_min(1) - margin;
  x_max = xyz_max(1) + margin;
  y_min = xyz_min(2) - margin;
  y_max = xyz_max(2) + margin;
  z_min = xyz_min(3) - margin;
  z_max = xyz_max(3) + margin;

  axis ( [ x_min, x_max, y_min, y_max, z_min, z_max ] );
  axis ( 'equal' );
%
%  Save a copy of the image.
%
  filename = 'xyz_display.png';
  print ( '-dpng', filename );
  fprintf ( 1, '  Graphics saved as "%s"\n', filename );

  return
end
function s2 = s_escape_tex ( s1 )

%*****************************************************************************80
%
%% s_escape_tex() de-escapes TeX escape sequences.
%
%  Discussion:
%
%    In particular, every occurrence of the characters '\', '_',
%    '^', '{' and '}' will be replaced by '\\', '\_', '\^',
%    '\{' and '\}'.  A TeX interpreter, on seeing these character
%    strings, is then likely to return the original characters.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    19 January 2007
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    string S1, the string to be de-escaped.
%
%  Output:
%
%    string S2, a copy of the string, modified to avoid TeX escapes.
%
  s1_length = length ( s1 );

  s1_pos = 0;
  s2_pos = 0;
  s2 = [];

  while ( s1_pos < s1_length )

    s1_pos = s1_pos + 1;

    if ( s1(s1_pos) == '\' || ...
         s1(s1_pos) == '_' || ...
         s1(s1_pos) == '^' || ...
         s1(s1_pos) == '{' || ...
         s1(s1_pos) == '}' )
      s2_pos = s2_pos + 1;
      s2 = strcat ( s2, '\' );
    end

    s2_pos = s2_pos + 1;
    s2 = strcat ( s2, s1(s1_pos) );

  end

  return
end

