\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")}%
%
% global def (default value)
\newcommand{\MeshPoint}{P}
\newcommand{\NewPoint}{P}
% for MetaPost (MP) (default value)
% default scale u:=1cm;
\newcommand\luaMeshScaleMP{1cm}
% default color for the plot of a global mesh
\newcommand\TeXCluaMeshMP{(0.,0.,0.)}
% default color for a new element
\newcommand\TeXCluaMeshNewMP{(0.5,0.1,0.1)}
% defaut color for the background of new element
\newcommand\TeXCluaMeshBackMP{(0.99,0.85,0.85)}
% default color for circoncircle
\newcommand\TeXCluaMeshCircleMP{(0.1,0.6,0.1)}
%%
% 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
%% engine of drawing
\newif\ifluameshengineMP%
\define@boolkey{buildMesh}{tikz}[true]{}%
%% show the bounding box for delaunay
\define@choicekey*{buildMesh}{bbox}[\val\nr]{none, show}{%
  \ifcase\nr\relax%
  \def\luameshval@bbox{none}%
  \or%
  \def\luameshval@bbox{bbox}%
  \fi%
}%
%% the scale
\define@key{buildMesh}{scale}[1cm]{\def\luameshval@scale{#1}}%
%% print ponits ?
\define@choicekey*{buildMesh}{print}[\val\nr]{none, points}{%
  \ifcase\nr\relax%
  \def\luameshval@print{none}%
  \or%
  \def\luameshval@print{points}%
  \fi%
}%
%% the name of the point
\define@key{buildMesh}{meshpoint}[P]{\def\luameshval@meshpoint{#1}}%
%% the mode for reading the points
\define@choicekey*{buildMesh}{mode}[\val\nr]{int, ext}{%
  \ifcase\nr\relax%
  \def\luameshval@mode{int}%
  \or%
  \def\luameshval@mode{ext}%
  \fi%
}%
%
%% a complete picture or some code of the engine
\define@choicekey*{buildMesh}{picture}[\val\nr]{full, simple}{%
  \ifcase\nr\relax%
  \def\luameshval@picture{full}%
  \or%
  \def\luameshval@picture{simple}%
  \fi%
}%
%color
%% the name of the color of drawing
\define@key{buildMesh}{color}[black]{\def\luameshval@color{#1}}%

\presetkeys{buildMesh}{tikz=false,bbox=none,scale, meshpoint,mode=int,print=none,picture=full,color}{}%
%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% the difinition
\newcommand{\buildMesh}[2][]{%
  % #1 : the string containing the list of points
  % (x1,y1);(x2,y2);... or the name file containing the points
  \setkeys{buildMesh}{#1} %
  \def\MeshPoint{\luameshval@meshpoint}%
  \ifKV@buildMesh@tikz% if we are using tikz
  \directlua{%
    buildMeshTikZ("#2","\luameshval@mode","\luameshval@print","\luameshval@bbox","full","\luameshval@scale","\luameshval@color")%
  }%
  \else % we are using MP
  \directlua{%
    buildMeshMP("#2","\luameshval@mode","\luameshval@print","\luameshval@bbox","full","\luameshval@scale","\luameshval@color")%
  }%
  %
  \fi%
}%
%
%%
%
%%the meshAddOnePoint commande
%%engine of drawing
\newif\ifluameshengineMP%
\define@boolkey{MeshAddOne}{tikz}[true]{}%
%% the scale
\define@key{MeshAddOne}{scale}[1cm]{\def\luameshval@scale{#1}}%
%% the name of the points
\define@key{MeshAddOne}{meshpoint}[P]{\def\luameshval@meshpoint{#1}}%
%% the name of the new point
\define@key{MeshAddOne}{newpoint}[P]{\def\luameshval@newpoint{#1}}%
%% a complete picture or some code of the engine
\define@choicekey*{MeshAddOne}{picture}[\val\nr]{full, simple}{%
  \ifcase\nr\relax%
  \def\luameshval@picture{full}%
  \or%
  \def\luameshval@picture{simple}%
  \fi%
}%
%% a complete picture or some code of the engine
\define@choicekey*{MeshAddOne}{step}[\val\nr]{badtriangles, cavity, newtriangles}{%
  \ifcase\nr\relax%
  \def\luameshval@step{badT}%
  \or%
  \def\luameshval@step{cavity}%
  \or%
  \def\luameshval@step{newT}%
  \fi%
}%
% color
%% the color of drawing
\define@key{MeshAddOne}{color}[black]{\def\luameshval@color{#1}}%
%% the color of background of new element
\define@key{MeshAddOne}{colorBack}[black!20]{\def\luameshval@colorback{#1}}%
%% the color of new element
\define@key{MeshAddOne}{colorNew}[red]{\def\luameshval@colornew{#1}}%
%% the color of circoncircle
\define@key{MeshAddOne}{colorCircle}[green]{\def\luameshval@colorcircle{#1}}%
%
%% a complete picture or some code of the engine
\define@choicekey*{MeshAddOne}{mode}[\val\nr]{int, ext}{%
  \ifcase\nr\relax%
  \def\luameshval@mode{int}%
  \or%
  \def\luameshval@mode{ext}%
  \fi%
}%
\presetkeys{MeshAddOne}{tikz=false,scale, meshpoint,newpoint,color,colorBack,colorNew,colorCircle,step=badtriangles,mode=int}{}%
%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% the difinition
\newcommand{\meshAddOnePoint}[3][]{%
  % #1 : the string containing the list of points
  % (x1,y1);(x2,y2);... or the name file containing the points
  \setkeys{MeshAddOne}{#1} %
  \def\MeshPoint{\luameshval@meshpoint}%
  \def\NewPoint{\luameshval@newpoint}%
  \ifKV@MeshAddOne@tikz% if we are using tikz
  \directlua{%
    buildMeshTikZ("#2","\luameshval@mode","\luameshval@print","\luameshval@bbox","full","\luameshval@scale","\luameshval@color")%
  }%
  \else % we are using MP
  \directlua{%
    TeXFullOnePointMP("#2","#3","\luameshval@step","\luameshval@color","\luameshval@colorback","\luameshval@colornew","\luameshval@colorcircle","\luameshval@scale","\luameshval@mode")%
  }%
  %
  \fi%
}%
%