\NeedsTeXFormat{LaTeX2e}%
\def\PackageName{luamesh}%
\def\fileversion{v0.1}%
\def\filedate{2016/11/20}%
\ProvidesPackage{luamesh}[\filedate\space\fileversion]%
%
% package to load
\RequirePackage{xkeyval,xcolor,ifthen}%
%% one global option: mp or tikz
%\newif\ifluameshmp%
%\define@key{luamesh.sty}{mp}[]{\luameshmptrue}%
%\define@key{luamesh.sty}{tikz}[]{\luameshmpfalse}%
%
\RequirePackage{etoolbox} % pour robustifier les commandes
%
%
%\ifluameshmp%
   \RequirePackage{luamplib}%
%\else%
   \RequirePackage{tikz}%
%\fi%
%
% load the lua code
\directlua{dofile("luamesh.lua")}%
%
%%
% for tikz (default value)
% default color for the plot of a global mesh
\definecolor{TeXCluaMeshTikZ}{rgb}{0.,0.,0.}
% default color for a new element
\definecolor{TeXCluaMeshNewTikZ}{rgb}{0.5,0.1,0.1}
% defaut color for the background of new element
\definecolor{TeXCluaMeshBackTikZ}{rgb}{0.99,0.85,0.85}
% default color for circoncircle
\definecolor{TeXCluaMeshCircleTikZ}{rgb}{0.1,0.6,0.1}


%%%%%%%%%%%%%%%% the buildMesh command
%% the user can write code (tikz or MP)
%% before and after the generated code
\define@boolkey{buildMeshInc}{tikz}[true]{}%
%% show the bounding box for delaunay
\define@choicekey*{buildMeshInc}{bbox}[\val\nr]{none, show}{%
  \ifcase\nr\relax%
  \def\luameshval@bmi@bbox{none}%
  \or%
  \def\luameshval@bmi@bbox{bbox}%
  \fi%
}%
%% the scale
\define@key{buildMeshInc}{scale}[1cm]{\def\luameshval@bmi@scale{#1}}%
%% print ponits ?
\define@choicekey*{buildMeshInc}{print}[\val\nr]{none, points}{%
  \ifcase\nr\relax%
  \def\luameshval@bmi@print{none}%
  \or%
  \def\luameshval@bmi@print{points}%
  \fi%
}%
%% the name of the point
\define@key{buildMeshInc}{meshpoint}[P]{\def\luameshval@bmi@meshpoint{#1}}%
%% the mode for reading the points
\define@choicekey*{buildMeshInc}{mode}[\val\nr]{int, ext}{%
  \ifcase\nr\relax%
  \def\luameshval@bmi@mode{int}%
  \or%
  \def\luameshval@bmi@mode{ext}%
  \fi%
}%
%% the name of the color of drawing
\define@key{buildMeshInc}{color}[black]{\def\luameshval@bmi@color{#1}}%
%% the name of the color of drawing the bbox
\define@key{buildMeshInc}{colorBbox}[black]{\def\luameshval@bmi@colorbbox{#1}}%
%
\presetkeys{buildMeshInc}{tikz=false,bbox=none,scale, meshpoint,mode=int,print=none,color,colorBbox}{}%
%
\newcommand*{\buildMeshBWinc}[4][]{%
  % #1 : optionnal arguments
  % #2 : the string containing the list of points
  %      (x1,y1);(x2,y2);... or the name file containing the points
  % #3 : the code to place before the generated one
  % #4 : the code to place after the generated one
  \setkeys{buildMeshInc}{#1} %
  \def\MeshPoint{\luameshval@bmi@meshpoint}%
  \ifKV@buildMeshInc@tikz% if we are using tikz
  \directlua{%
    buildMeshTikZBWinc("#2","\luaescapestring{\unexpanded{#3}}","\luaescapestring{\unexpanded{#4}}","\luameshval@bmi@mode","\luameshval@bmi@print","\luameshval@bmi@bbox","\luameshval@bmi@scale","\luameshval@bmi@color","\luameshval@bmi@colorbbox")%
  }%
  \else % we are using MP
  \mplibcolor{\luameshmpcolor}{\luameshval@bmi@color}
  \mplibcolor{\luameshmpcolorBbox}{\luameshval@bmi@colorbbox}
  \directlua{%
    buildMeshMPBWinc("#2","\luaescapestring{\unexpanded{#3}}","\luaescapestring{\unexpanded{#4}}","\luameshval@bmi@mode","\luameshval@bmi@print","\luameshval@bmi@bbox","\luameshval@bmi@scale")%
  }%
  %
  \fi%
}%
%
%%%%%%%%%%%%%%%% the buildMeshBW command
%% the code is complete and we don't have any callbacks
\define@boolkey{buildMesh}{tikz}[true]{}%
%% show the bounding box for delaunay
\define@choicekey*{buildMesh}{bbox}[\val\nr]{none, show}{%
  \ifcase\nr\relax%
  \def\luameshval@bm@bbox{none}%
  \or%
  \def\luameshval@bm@bbox{bbox}%
  \fi%
}%
%% the scale
\define@key{buildMesh}{scale}[1cm]{\def\luameshval@bm@scale{#1}}%
%% print points
\define@choicekey*{buildMesh}{print}[\val\nr]{none, points}{%
  \ifcase\nr\relax%
  \def\luameshval@bm@print{none}%
  \or%
  \def\luameshval@bm@print{points}%
  \fi%
}%
%% the name of the point
\define@key{buildMesh}{meshpoint}[P]{\def\luameshval@bm@meshpoint{#1}}%
%% the mode for reading the points
\define@choicekey*{buildMesh}{mode}[\val\nr]{int, ext}{%
  \ifcase\nr\relax%
  \def\luameshval@bm@mode{int}%
  \or%
  \def\luameshval@bm@mode{ext}%
  \fi%
}%
%% the name of the color of drawing
\define@key{buildMesh}{color}[black]{\def\luameshval@bm@color{#1}}%
%% the name of the color of drawing the bbox
\define@key{buildMesh}{colorBbox}[black]{\def\luameshval@bm@colorbbox{#1}}%
%
\presetkeys{buildMesh}{tikz=false,bbox=none,scale, meshpoint,mode=int,print=none,color,colorBbox}{}%
%
\newcommand{\buildMeshBW}[2][]{%
  % #1 : optionnal arguments
  % #2 : the string containing the list of points
  % (x1,y1);(x2,y2);... or the name file containing the points
  \setkeys{buildMesh}{#1} %
  \def\MeshPoint{\luameshval@bm@meshpoint}%
  \ifKV@buildMesh@tikz% if we are using tikz
  \directlua{%
    buildMeshTikZBW("#2","\luameshval@bm@mode","\luameshval@bm@print","\luameshval@bm@bbox","\luameshval@bm@scale","\luameshval@bm@color","\luameshval@bm@colorbbox")%
  }%
  \else % we are using MP
  \mplibcolor{\luameshmpcolor}{\luameshval@bm@color}
  \mplibcolor{\luameshmpcolorBbox}{\luameshval@bm@colorbbox}
  \directlua{%
    buildMeshMPBW("#2","\luameshval@bm@mode","\luameshval@bm@print","\luameshval@bm@bbox","\luameshval@bm@scale")%
  }%
  %
  \fi%
}%
%
%
%%%%%%%%%%%%%%%%% the trace Points macro
%% plot the list of points given in argument
% the engine
\define@boolkey{tracePoints}{tikz}[true]{}%
%% show the bounding box for delaunay
\define@choicekey*{tracePoints}{bbox}[\val\nr]{none, show}{%
  \ifcase\nr\relax%
  \def\luameshval@tp@bbox{none}%
  \or%
  \def\luameshval@tp@bbox{bbox}%
  \fi%
}%
%% the scale
\define@key{tracePoints}{scale}[1cm]{\def\luameshval@tp@scale{#1}}%
%% print points
\define@choicekey*{tracePoints}{print}[\val\nr]{none, points}{%
  \ifcase\nr\relax%
  \def\luameshval@tp@print{none}%
  \or%
  \def\luameshval@tp@print{points}%
  \fi%
}%
%% the name of the point
\define@key{tracePoints}{meshpoint}[P]{\def\luameshval@tp@meshpoint{#1}}%
%% the mode for reading the points
\define@choicekey*{tracePoints}{mode}[\val\nr]{int, ext}{%
  \ifcase\nr\relax%
  \def\luameshval@tp@mode{int}%
  \or%
  \def\luameshval@tp@mode{ext}%
  \fi%
}%
%% the name of the color of drawing
\define@key{tracePoints}{color}[black]{\def\luameshval@tp@color{#1}}%
%% the name of the color of drawing the bbox
\define@key{tracePoints}{colorBbox}[black]{\def\luameshval@tp@colorbbox{#1}}%
%
\presetkeys{tracePoints}{tikz=false,bbox=none,scale, meshpoint,mode=int,print=none,color,colorBbox}{}%
%
\newcommand{\tracePointsMesh}[2][]{%
  % #1 : optionnal arguments
  % #2 : the string containing the list of points
  % (x1,y1);(x2,y2);... or the name file containing the points
  \setkeys{tracePoints}{#1} %
  \def\MeshPoint{\luameshval@tp@meshpoint}%
  \ifKV@tracePoints@tikz% if we are using tikz
  \directlua{%
    printPointsTikZ("#2","\luameshval@tp@mode","\luameshval@tp@print","\luameshval@tp@bbox","\luameshval@tp@scale","\luameshval@tp@color","\luameshval@tp@colorbbox")%
  }%
  \else % we are using MP
  \mplibcolor{\luameshmpcolor}{\luameshval@tp@color}
  \mplibcolor{\luameshmpcolorBbox}{\luameshval@tp@colorbbox}
  \directlua{%
    printPointsMP("#2","\luameshval@tp@mode","\luameshval@tp@print","\luameshval@tp@bbox","\luameshval@tp@scale")%
  }%
  %
  \fi%
}%
%

%%%%%%%%%%%%%%%% the trace Points macro inc with callbacks
%% engine of drawing
\define@boolkey{tracePointsInc}{tikz}[true]{}%
%% show the bounding box for delaunay
\define@choicekey*{tracePointsInc}{bbox}[\val\nr]{none, show}{%
  \ifcase\nr\relax%
  \def\luameshval@tpi@bbox{none}%
  \or%
  \def\luameshval@tpi@bbox{bbox}%
  \fi%
}%
%% the scale
\define@key{tracePointsInc}{scale}[1cm]{\def\luameshval@tpi@scale{#1}}%
%% print points
\define@choicekey*{tracePointsInc}{print}[\val\nr]{none, points}{%
  \ifcase\nr\relax%
  \def\luameshval@tpi@print{none}%
  \or%
  \def\luameshval@tpi@print{points}%
  \fi%
}%
%% the name of the point
\define@key{tracePointsInc}{meshpoint}[P]{\def\luameshval@tpi@meshpoint{#1}}%
%% the mode for reading the points
\define@choicekey*{tracePointsInc}{mode}[\val\nr]{int, ext}{%
  \ifcase\nr\relax%
  \def\luameshval@tpi@mode{int}%
  \or%
  \def\luameshval@tpi@mode{ext}%
  \fi%
}%
%
% color
%% the name of the color of drawing
\define@key{tracePointsInc}{color}[black]{\def\luameshval@tpi@color{#1}}%
%% the name of the color of drawing the bbox
\define@key{tracePointsInc}{colorBbox}[black]{\def\luameshval@tpi@colorbbox{#1}}%
%
\presetkeys{tracePointsInc}{tikz=false,bbox=none,scale, meshpoint,mode=int,print=none,color,colorBbox}{}%
%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcommand*{\tracePointsMeshinc}[4][]{%
  % #1 : optionnal arguments
  % #2 : the string containing the list of points
  % (x1,y1);(x2,y2);... or the name file containing the points
  % #3 : the code to place before the generated one
  % #4 : the code to place after the generated one
  \setkeys{tracePointsInc}{#1} %
  \def\MeshPoint{\luameshval@tpi@meshpoint}%
  \ifKV@tracePointsInc@tikz% if we are using tikz
  \directlua{%
    buildMeshTikZ("#2","\luameshval@tpi@mode","\luameshval@tpi@print","\luameshval@tpi@bbox","full","\luameshval@tpi@sc%ale","\luameshval@tpi@color")%
  }%
  \else % we are using MP
  \mplibcolor{\luameshmpcolor}{\luameshval@tpi@color}
  \mplibcolor{\luameshmpcolorBbox}{\luameshval@tpi@colorbbox}
  \directlua{%
    printPointsMPinc("#2","\luaescapestring{\unexpanded{#3}}","\luaescapestring{\unexpanded{#4}}","\luameshval@tpi@mode","\luameshval@tpi@print","\luameshval@tpi@bbox","\luameshval@tpi@scale")%
  }%
  \fi%
}%
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% the meshAddOnePointBW commande
%%engine of drawing
\define@boolkey{MeshAddOneBW}{tikz}[true]{}%
%% the scale
\define@key{MeshAddOneBW}{scale}[1cm]{\def\luameshval@ap@scale{#1}}%
%% the name of the points
\define@key{MeshAddOneBW}{meshpoint}[P]{\def\luameshval@ap@meshpoint{#1}}%
%% the name of the new point
\define@key{MeshAddOneBW}{newpoint}[P]{\def\luameshval@ap@newpoint{#1}}%
%% a complete picture or some code of the engine
\define@choicekey*{MeshAddOneBW}{step}[\val\nr]{badtriangles, cavity, newtriangles}{%
  \ifcase\nr\relax%
  \def\luameshval@ap@step{badT}%
  \or%
  \def\luameshval@ap@step{cavity}%
  \or%
  \def\luameshval@ap@step{newT}%
  \fi%
}%
% color
%% the color of drawing
\define@key{MeshAddOneBW}{color}[black]{\def\luameshval@ap@color{#1}}%
%% the color of background of new element
\define@key{MeshAddOneBW}{colorBack}[black!20]{\def\luameshval@ap@colorback{#1}}%
%% the color of new element
\define@key{MeshAddOneBW}{colorNew}[red]{\def\luameshval@ap@colornew{#1}}%
%% the color of circoncircle
\define@key{MeshAddOneBW}{colorCircle}[green]{\def\luameshval@ap@colorcircle{#1}}%
%% the name of the color of drawing the bbox
\define@key{MeshAddOneBW}{colorBbox}[black]{\def\luameshval@ap@colorbbox{#1}}%
%
%% a complete picture or some code of the engine
\define@choicekey*{MeshAddOneBW}{mode}[\val\nr]{int, ext}{%
  \ifcase\nr\relax%
  \def\luameshval@ap@mode{int}%
  \or%
  \def\luameshval@ap@mode{ext}%
  \fi%
}%
\define@choicekey*{MeshAddOneBW}{bbox}[\val\nr]{none, show}{%
  \ifcase\nr\relax%
  \def\luameshval@ap@bbox{none}%
  \or%
  \def\luameshval@ap@bbox{bbox}%
  \fi%
}%
\presetkeys{MeshAddOneBW}{tikz=false,scale, meshpoint,newpoint,color,colorBack,colorNew,colorCircle,step=badtriangles,mode=int,bbox=none,colorBbox}{}%
%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcommand*{\meshAddPointBW}[3][]{%
  % #1 : optionnal arguments
  % #2 : the string containing the list of points
  % (x1,y1);(x2,y2);... or the name file containing the points
  \setkeys{MeshAddOneBW}{#1} %
  \def\MeshPoint{\luameshval@ap@meshpoint}%
  \def\NewPoint{\luameshval@ap@newpoint}%
  \ifKV@MeshAddOneBW@tikz% if we are using tikz
  \directlua{%
    TeXOnePointTikZBW("#2","#3","\luameshval@ap@step","\luameshval@ap@scale","\luameshval@ap@mode","\luameshval@ap@bbox","\luameshval@ap@color","\luameshval@ap@colorback","\luameshval@ap@colornew","\luameshval@ap@colorcircle","\luameshval@ap@colorbbox")%
  }%
  \else % we are using MP
  \mplibcolor{\luameshmpcolor}{\luameshval@ap@color}
  \mplibcolor{\luameshmpcolorBack}{\luameshval@ap@colorback}
  \mplibcolor{\luameshmpcolorNew}{\luameshval@ap@colornew}
  \mplibcolor{\luameshmpcolorCircle}{\luameshval@ap@colorcircle}
  \mplibcolor{\luameshmpcolorBbox}{\luameshval@ap@colorbbox}
  \directlua{%
    TeXOnePointMPBW("#2","#3","\luameshval@ap@step","\luameshval@ap@scale","\luameshval@ap@mode","\luameshval@ap@bbox")%
  }%
  %
  \fi%
}%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% the meshAddPointBWinc commande with callbacks
%% engine of drawing
\define@boolkey{MeshAddOneBWinc}{tikz}[true]{}%
%% the scale
\define@key{MeshAddOneBWinc}{scale}[1cm]{\def\luameshval@api@scale{#1}}%
%% the name of the points
\define@key{MeshAddOneBWinc}{meshpoint}[P]{\def\luameshval@api@meshpoint{#1}}%
%% the name of the new point
\define@key{MeshAddOneBWinc}{newpoint}[P]{\def\luameshval@api@newpoint{#1}}%
%% a complete picture or some code of the engine
\define@choicekey*{MeshAddOneBWinc}{step}[\val\nr]{badtriangles, cavity, newtriangles}{%
  \ifcase\nr\relax%
  \def\luameshval@api@step{badT}%
  \or%
  \def\luameshval@api@step{cavity}%
  \or%
  \def\luameshval@api@step{newT}%
  \fi%
}%
% color
%% the color of drawing
\define@key{MeshAddOneBWinc}{color}[black]{\def\luameshval@api@color{#1}}%
%% the color of background of new element
\define@key{MeshAddOneBWinc}{colorBack}[black!20]{\def\luameshval@api@colorback{#1}}%
%% the color of new element
\define@key{MeshAddOneBWinc}{colorNew}[red]{\def\luameshval@api@colornew{#1}}%
%% the color of circoncircle
\define@key{MeshAddOneBWinc}{colorCircle}[green]{\def\luameshval@api@colorcircle{#1}}%
\define@key{MeshAddOneBWinc}{colorBbox}[black]{\def\luameshval@api@colorbbox{#1}}%
%
%% a complete picture or some code of the engine
\define@choicekey*{MeshAddOneBWinc}{mode}[\val\nr]{int, ext}{%
  \ifcase\nr\relax%
  \def\luameshval@api@mode{int}%
  \or%
  \def\luameshval@api@mode{ext}%
  \fi%
}%
\define@choicekey*{MeshAddOneBWinc}{bbox}[\val\nr]{none, show}{%
  \ifcase\nr\relax%
  \def\luameshval@api@bbox{none}%
  \or%
  \def\luameshval@api@bbox{bbox}%
  \fi%
}%
\presetkeys{MeshAddOneBWinc}{tikz=false,scale, meshpoint,newpoint,color,colorBack,colorNew,colorCircle,step=badtriangles,mode=int,bbox=none,colorBbox}{}%
%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcommand*{\meshAddPointBWinc}[5][]{%
  % #1 : optionnal arguments
  % #2 : the string containing the list of points
  % (x1,y1);(x2,y2);... or the name file containing the points
  % #3 : the code to place before the generated one
  % #4 : the code to place after the generated one
  \setkeys{MeshAddOneBWinc}{#1} %
  \def\MeshPoint{\luameshval@api@meshpoint}%
  \def\NewPoint{\luameshval@api@newpoint}%
  \ifKV@MeshAddOneBWinc@tikz% if we are using tikz
  \directlua{%
    TeXOnePointTikZBWinc("#2","#3","\luaescapestring{\unexpanded{#4}}","\luaescapestring{\unexpanded{#5}}","\luameshval@ap@step","\luameshval@ap@scale","\luameshval@ap@mode","\luameshval@ap@bbox","\luameshval@ap@color","\luameshval@ap@colorback","\luameshval@ap@colornew","\luameshval@ap@colorcircle","\luameshval@ap@colorbbox")%
  }%
  \else % we are using MP
  \mplibcolor{\luameshmpcolor}{\luameshval@api@color}
  \mplibcolor{\luameshmpcolorBack}{\luameshval@api@colorback}
  \mplibcolor{\luameshmpcolorNew}{\luameshval@api@colornew}
  \mplibcolor{\luameshmpcolorCircle}{\luameshval@api@colorcircle}
  \mplibcolor{\luameshmpcolorBbox}{\luameshval@api@colorbbox}
  \directlua{%
    TeXOnePointMPBWinc("#2","#3","\luaescapestring{\unexpanded{#4}}","\luaescapestring{\unexpanded{#5}}","\luameshval@api@step","\luameshval@api@scale","\luameshval@api@mode","\luameshval@api@bbox")%
  }%
  %
  \fi%
}%
%