function triangulation_boundary_test ( )

%*****************************************************************************80
%
%% triangulation_boundary_test tests triangulation_boundary().
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    01 September 2020
%
%  Author:
%
%    John Burkardt
%
  addpath ( '../triangulation_boundary' );

  timestamp ( );
  fprintf ( 1, '\n' );
  fprintf ( 1, 'triangulation_boundary_test:\n' );
  fprintf ( 1, '  test triangulation_boundary();\n' );

  triangulation_ijk_to_node_test ( );
  triangulation_node_to_boundary_edge_test ( );
  boundary_edge_to_path_test ( );
  node_to_ij_test ( );
%
%  Terminate.
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'triangulation_boundary_test:\n' );
  fprintf ( 1, '  Normal end of execution;\n' );
  timestamp ( );

  rmpath ( '../triangulation_boundary' );

  return
end
function triangulation_ijk_to_node_test ( )

%*****************************************************************************80
%
%% triangulation_ijk_to_node_test tests triangulation_ijk_to_node
%
%  Discussion:
%
%    Here is the triangulated region, with the temporary grid created
%    to assign numbers to the nodes:
%
%          16--17--18--19  20
%         /              \
%        11  12--13  14  15
%       /   /     \     /
%      6   7   8   9   10
%       \   \       \ /
%    1   2---3   4   5
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    28 August 2020
%
%  Author:
%
%    John Burkardt
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'triangulation_ijk_to_node_test:\n' );
  fprintf ( 1, '  triangulation_ijk_to_node receives the IJK coordinates\n' );
  fprintf ( 1, '  of triangles in a triangulation.\n' );
  fprintf ( 1, '  It creates a triangular grid, assigns a number to each\n' );
  fprintf ( 1, '  node on the grid, and returns a list of the node numbers\n' );
  fprintf ( 1, '  forming each triangle in the triangulation.\n' );
%
%  Represent the shape using the IJK parallelogram coordinates.
%
  triangulation_ijk = [ ...
    1, 1, 1; ...
    1, 2, 0; ...
    1, 2, 1; ...
    1, 3, 0; ...
    1, 3, 1; ...
    2, 1, 0; ...
    2, 3, 0; ...
    2, 3, 1; ...
    3, 2, 1; ...
    3, 3, 0; ...
    3, 3, 1; ...
    4, 1, 1; ...
    4, 2, 0; ...
    4, 2, 1; ...
    4, 3, 0 ];
%
%  triangulation_node(triangle_num,3) contains P1, P2, P3, node indices.
%  IJ index (I,J) -> P = 1 + (M+1)*(J-1) where M is the maximum I index.
%
  triangulation_node = triangulation_ijk_to_node ( triangulation_ijk );
%
%  Report.
%
  triangle_num = size ( triangulation_ijk, 1 );
  fprintf ( 1, '\n' );
  fprintf ( 1, '  Nodes making up each triangle:\n' );
  fprintf ( 1, '\n' );
  for t = 1 : triangle_num
    fprintf ( 1, '  %2d:  %2d  %2d  %2d\n', t, triangulation_node(t,1:3) );
  end
  
  return
end
function triangulation_node_to_boundary_edge_test ( )

%*****************************************************************************80
%
%% triangulation_node_to_boundary_edge_test tests triangulation_node_to_boundary_edge().
%
%  Discussion:
%
%    Here is the triangulated region, with the temporary grid created
%    to assign numbers to the nodes:
%
%          16--17--18--19  20
%         /              \
%        11  12--13  14  15
%       /   /     \     /
%      6   7   8   9   10
%       \   \       \ /
%    1   2---3   4   5
%
%    Here is the corresponding triangulation_node array:
%
%     1:  7,   6,   2
%     2:  6,   7,  11
%     3: 12,  11,   7
%     4: 11,  12,  16
%     5: 17,  16,  12
%     6:  2,   3,   7
%     7: 12,  13,  17
%     8: 18,  17,  13
%     9: 14,  13,   9
%    10: 13,  14,  18
%    11: 19,  18,  14
%    12: 10,   9,   5
%    13:  9,  10,  14
%    14: 15,  14,  10
%    15: 14,  15,  19
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    29 August 2020
%
%  Author:
%
%    John Burkardt
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'triangulation_node_to_boundary_edge_test:\n' );
  fprintf ( 1, '  triangulation_node_to_boundary_edge receives the indices\n' );
  fprintf ( 1, '  of the vertices of triangles in a triangulation.\n' );
  fprintf ( 1, '  It returns boundary edges as pairs of node indices.\n' );
%
%  Here are the triangulation nodes as computed from the previous test.
%
  triangulation_node = [ ...
     7,   6,   2; ...
     6,   7,  11; ...
    12,  11,   7; ...
    11,  12,  16; ...
    17,  16,  12; ...
     2,   3,   7; ...
    12,  13,  17; ...
    18,  17,  13; ...
    14,  13,   9; ...
    13,  14,  18; ...
    19,  18,  14; ...
    10,   9,   5; ...
     9,  10,  14; ...
    15,  14,  10; ...
    14,  15,  19 ];

  boundary_edge = triangulation_node_to_boundary_edge ( triangulation_node );
%
%  Report.
%
  boundary_edge_num = size ( boundary_edge, 1 );
  fprintf ( 1, '\n' );
  fprintf ( 1, '  Boundary edges (pairs of nodes):\n' );
  fprintf ( 1, '\n' );
  for e = 1 : boundary_edge_num
    fprintf ( 1, '  %2d:  %2d  %2d\n', e, boundary_edge(e,1:2) );
  end
  
  return
end
function boundary_edge_to_path_test ( )

%*****************************************************************************80
%
%% boundary_edge_to_path_test tests boundary_edge_to_path().
%
%  Discussion:
%
%    Here is the triangulated region, with the temporary grid created
%    to assign numbers to the nodes:
%
%          16--17--18--19  20
%         /              \
%        11  12--13  14  15
%       /   /     \     /
%      6   7   8   9   10
%       \   \       \ /
%    1   2---3   4   5
%
%    Here is the corresponding triangulation_node array:
%
%     1:  7,   6,   2
%     2:  6,   7,  11
%     3: 12,  11,   7
%     4: 11,  12,  16
%     5: 17,  16,  12
%     6:  2,   3,   7
%     7: 12,  13,  17
%     8: 18,  17,  13
%     9: 14,  13,   9
%    10: 13,  14,  18
%    11: 19,  18,  14
%    12: 10,   9,   5
%    13:  9,  10,  14
%    14: 15,  14,  10
%    15: 14,  15,  19
%
%    Here is the list of boundary edges (pairs of nodes):
%
%    1:   2   3
%    2:   6   2
%    3:   3   7
%    4:   9   5
%    5:   5  10
%    6:  11   6
%    7:   7  12
%    8:  13   9
%    9:  10  15
%   10:  16  11
%   11:  12  13
%   12:  15  19
%   13:  17  16
%   14:  18  17
%   15:  19  18
%
%    We want to reorder these edges to form the path around the triangulation.
%
%    1:   2
%    2:   3
%    3:   7
%    4:  12
%    5:  13
%    6:   9
%    7:   5
%    8:  10
%    9:  15
%   10:  19
%   11:  18
%   12:  17
%   13:  16
%   14:  11
%   15:   6
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    31 August 2020
%
%  Author:
%
%    John Burkardt
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'boundary_edge_to_path_test:\n' );
  fprintf ( 1, '  boundary_edge_to_path receives a list of pairs\n' );
  fprintf ( 1, '  of nodes constituting boundary edges of the triangulation.\n' );
  fprintf ( 1, '  It returns a list of nodes that form a counterclockwise\n' );
  fprintf ( 1, '  path around the boundary.  It also reorders the edge list.\n' );
%
%  Here are the boundary edges as computed from the previous test.
%
  boundary_edge = [ ...
     2,   3; ...
     6,   2; ...
     3,   7; ...
     9,   5; ...
     5,  10; ...
    11,   6; ...
     7,  12; ...
    13,   9; ...
    10,  15; ...
    16,  11; ...
    12,  13; ...
    15,  19; ...
    17,  16; ...
    18,  17; ...
    19,  18 ];

  [ boundary_path, boundary_edge ] = boundary_edge_to_path ( boundary_edge );
%
%  Report.
%
  boundary_path_length = length ( boundary_path );
  fprintf ( 1, '\n' );
  fprintf ( 1, '  Nodes in the boundary path:\n' );
  fprintf ( 1, '\n' );
  for node = 1 : boundary_path_length
    fprintf ( 1, '  %2d:  %2d\n', node, boundary_path(node) );
  end
 
  boundary_edge_num = size ( boundary_edge, 1 );
  fprintf ( 1, '\n' );
  fprintf ( 1, '  Reordered boundary edge list (pairs of nodes):\n' );
  fprintf ( 1, '\n' );
  for e = 1 : boundary_edge_num
    fprintf ( 1, '  %2d:  %2d  %2d\n', e, boundary_edge(e,1:2) );
  end

  return
end
function node_to_ij_test ( )

%*****************************************************************************80
%
%% node_to_ij_test tests node_to_ij().
%
%  Discussion:
%
%    Here is the triangulated region, with the (I,J) parallelogram
%    node indices:
%
%              (0,3)-(1,3)-(2,3)-(3,3) (4,3)
%               /                    \
%           (0,2) (1,2)-(2,2) (3,2) (4,2)
%            /     /        \        /
%        (0,1) (1,1) (2,1) (3,1) (4,1)
%           \     \           \   /
%    (0,0) (1,0)-(2,0) (3,0) (4,0)
%
%    Here is the triangulated region, with the temporary grid created
%    to assign numbers to the nodes:
%
%          16--17--18--19  20
%         /              \
%        11  12--13  14  15
%       /   /     \     /
%      6   7   8   9   10
%       \   \       \ /
%    1   2---3   4   5
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    01 September 2020
%
%  Author:
%
%    John Burkardt
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'node_to_ij_test\n' );
  fprintf ( 1, '  node_to_ij() takes a temporary scalar node index\n' );
  fprintf ( 1, '  and computes the (I,J) parallelogram indices.\n' );
%
%  Pass in a single value.
%
  fprintf ( 1, '\n' );
  fprintf ( 1, '  Enter a single node\n' );
  fprintf ( 1, '\n' );

  node = 13;
  imax = 4;
  [ i, j ] = node_to_ij ( node, imax );
  k = 1;
  fprintf ( 1, '  %2d:  %2d,  (%2d,%2d)\n', k, node, i(k), j(k) );
%
%  Pass in a vector.
%
  path = [ ...
      2; ...
      3; ...
      7; ...
     12; ...
     13; ...
      9; ...
      5; ...
     10; ...
     15; ...
     19; ...
     18; ...
     17; ...
     16; ...
     11; ...
      6 ];

  imax = 4;
  [ i, j ] = node_to_ij ( path, imax );
  path_num = length ( path );

  fprintf ( 1, '\n' );
  fprintf ( 1, '  Enter a vector of nodes\n' );
  fprintf ( 1, '\n' );
  for k = 1 : path_num
    fprintf ( 1, '  %2d:  %2d,  (%2d,%2d)\n', ...
      k, path(k), i(k), j(k) );
  end
%
%  Pass in a MxN array.
%
  boundary_edge = [ ...
     2,   3; ...
     3,   7; ...
     7,  12; ...
    12,  13; ...
    13,   9; ...
     9,   5; ...
     5,  10; ...
    10,  15; ...
    15,  19; ...
    19,  18; ...
    18,  17; ...
    17,  16; ...
    16,  11; ...
    11,   6; ...
     6,   2 ];

  imax = 4;
  [ i, j ] = node_to_ij ( boundary_edge, imax );

  boundary_edge_num = size ( boundary_edge, 1 );
  fprintf ( 1, '\n' );
  fprintf ( 1, '  Enter an MxN array of nodes\n' );
  fprintf ( 1, '\n' );
  for l = 1 : 2
    for k = 1 : boundary_edge_num
      fprintf ( 1, '  %2d:  %2d,  (%2d,%2d)\n', ...
        k+(l-1)*boundary_edge_num, boundary_edge(k,l), i(k), j(k) );
    end
  end

  return
end

