1 % luamesh: compute and draw meshes with lua, luamplib and tikz
3 % Originally written by Maxime Chupin <mc@melusine.eu.org>,
6 % Distributed under the terms of the GNU free documentation licence:
7 % http://www.gnu.org/licenses/fdl.html
8 % without any invariant section or cover text.
10 \documentclass{lltxdoc
}
11 \usepackage{tcolorbox
}
13 \usepackage[tikz
]{bclogo
}
17 \title{\Verb+luamesh+: compute and draw meshes with
\lualatex}
18 \author{Maxime Chupin
\email{mc@melusine.eu.org
}}
22 \definecolor{darkred
}{rgb
}{0.8,
0.1,
0.1}
25 \newcommand*
\commande{\noindent\hspace{-
30pt
}%
26 \SaveVerb[aftersave=
{%
31 \newcommand*
\textme[1]{\textcolor{black
}{\rmfamily\textit{#1}}}
32 \newcommand*
\meta[1]{% % meta
33 \textme{\ensuremath{\langle}#1\ensuremath{\rangle}}}
34 \newcommand*
\optstar{% % optional star
35 \meta{\ensuremath{*
}}\xspace}
38 \setlength{\fboxsep}{2pt
}
40 codes=
{\catcode`\«
\active \catcode`\×
\active },
41 defineactive=
{\makefancyog\makefancytimes},
42 formatcom=
\color{darkred
},
45 % rendre «...» équivalent à \meta{...}
47 \newcommandx\makefancyog[0][addprefix=
\global]{%
48 \def«#
#1»
{\meta{#
#1}}}}
49 % rendre × équivalent à \optstar
51 \newcommandx\makefancytimes[0][addprefix=
\global]{%
55 \tcbuselibrary{listings,breakable
}
57 \definecolor{vert
}{rgb
}{0.1,
0.4,
0.1}
58 \definecolor{bleu
}{rgb
}{0.1,
0.1,
0.4}
60 numberstyle=
\footnotesize\color{vert
},
61 keywordstyle=
\ttfamily\bfseries\color{blue
},
62 basicstyle=
\ttfamily\footnotesize,
63 commentstyle=
\itshape\color{vert
},
64 stringstyle=
\ttfamily,
65 showstringspaces=false,
69 defaultdialect=
[LaTeX
]TeX,
70 morekeywords=
{buildMeshBW,buildMeshBWinc,drawPointsMesh,
71 drawPointsMeshinc, meshAddPointBW, meshAddPointBWinc
}% frame=tb
74 \lstdefinelanguage{lua
}
75 {morekeywords=
{for,end,function,do,if,else,elseif,then,
76 tex.print,tex.sprint,io.read,io.open,string.find,string.explode,require
},
78 morecomment=
[s
]{--
[[}{]]},
82 \newtcblisting{Exemple
}{%
83 arc=
0pt,outer arc=
0pt,
85 colframe=red!
75!black,
87 boxsep=
0pt,left=
5pt,right=
5pt,top=
5pt,bottom=
5pt, bottomtitle =
89 boxrule=
0pt,bottomrule=
0.5pt,toprule=
0.5pt, toprule at break =
90 0pt, bottomrule at break =
0pt,
91 listing options=
{breaklines
},
94 \newtcblisting{commandshell
}{colback=black,colupper=white,colframe=black,
96 listing only,boxsep=
0pt,listing
97 options=
{style=tcblatex,language=sh
},
98 every listing line=
{\textcolor{red
}{\small\ttfamily\bfseries user \$>
}}}
100 \newtcblisting{latexcode
}{
101 arc=
0pt,outer arc=
0pt,
103 colframe=red!
75!black,
105 boxsep=
0pt,left=
5pt,right=
5pt,top=
5pt,bottom=
5pt, bottomtitle =
107 boxrule=
0pt,bottomrule=
0.5pt,toprule=
0.5pt, toprule at break =
108 0pt, bottomrule at break =
0pt,
109 listing only,boxsep=
0pt,listing
114 \newcommand\luamesh{\Verb+luamesh+
\xspace}
116 \newenvironment{optionsenum
}[1][]
117 {\begin{description
}[font=
\color{darkred
}\ttfamily]}
120 \newenvironment{warning
}{%
121 \setlength{\logowidth}{24pt
}
123 arc=
0pt,outer arc=
0pt,colback=gray!
10!white,colframe=gray!
60!white,
124 boxsep=
0pt,left=
5pt,right=
5pt,top=
5pt,bottom=
5pt, bottomtitle =
3pt, toptitle=
3pt,
125 boxrule=
0pt,bottomrule=
0.5pt,toprule=
0.5pt
}
128 \begin{wrapfigure
}[2]{L
}{17pt
}%
135 {\end{tcolorbox
}\medskip}
137 \lstset{moredelim=*
[s
][\color{red
}\rmfamily\itshape]{<
}{>
}}
138 \lstset{moredelim=*
[s
][\color{blue
}\rmfamily\itshape]{<<
}{>>
}}
139 \usepackage[colorlinks=true
]{hyperref
}
141 %% === Page de garde ===================================================
142 \thispagestyle{empty
}
143 \begin{tikzpicture
}[remember picture, overlay
]
144 \node[below right, shift=
{(-
4pt,
4pt)
}] at (current page.north west)
{%
145 \includegraphics{fond.pdf
}%
150 \includegraphics{luamesh-title
}\\
151 {\large compute and draw meshes with
\lualatex}\\
[1cm
]
152 \parbox{0.6\textwidth}{
154 mode=ext,step=badtriangles,
155 colorNew =green!
20!red,
163 \parbox{0.4\textwidth}{\Large\raggedleft
164 \textbf{Contributor
}\\
165 Maxime
\textsc{Chupin
}
169 Version
0.1,
25 novembre
2016\\
170 \url{http://melusine.eu.org/syracuse/G/delaunay/
}
172 %% == Page de garde ====================================================
178 The package
\Verb|luamesh| allows to compute and draw
2D Delaunay
179 triangulation. The algorithm is written with lua, and depending on the
180 choice of the ``engine'', the drawing is done by MetaPost (with
181 \Verb|luamplib|) or by
\Verb|tikz|.
183 The Delaunay triangulation algorithm is the Bowyer and Watson
184 algorithm. Several macros are provided to draw the global mesh, the
185 set of points, or a particular step of the algorithm.
188 I would like to thank Jean-Michel Sarlat, who hosts the development
189 with a git project on the
\Verb+melusine+ machine:
191 \url{https://melusine.eu.org/syracuse/G/delaunay/
}
193 I would also like to thank the first user, an intensive
194 \emph{test
} user, and a very kind English corrector: Nicole Spillane.
198 \section{Installation
}
201 Of course, you can just put the two files
\Verb+luamesh.lua+ and
202 \Verb+luamesh.sty+ in the working directory, but this is not
206 \subsection{With
\TeX live and Linux or Mac OSX
}
208 To install
\luamesh with
\TeX live, you have to create the local
209 \Verb+texmf+ directory in your
\Verb+home+.
215 Then place the files in the correct directories. First, the
216 \Verb+luamesh.sty+ file must be in the directory:
218 \Verb+~/texmf/tex/latex/luamesh/+
220 and secondly, the
\Verb+luamesh.lua+ must be in the directory:
222 \Verb+~/texmf/scripts/luamesh/+
225 Once you have done this,
\luamesh can be included in your
document
231 \subsection{With Mik
\TeX{} and Windows
}
233 We do not know these two systems, so we refer to the
234 documentation for integrating local additions to Mik
\TeX:
236 \url{http://docs.miktex.org/manual/localadditions.html
}
240 \subsection{A
\lualatex package
}
242 If you want to use this package, you must compile your
document with
246 lualatex mylatexfile.tex
250 \subsection{Dependencies
}
252 This package is built upon two main existing packages to draw the
255 \item \Verb+luamplib+ to use MetaPost via the
\luatex library
257 \item and
\Verb+tikz+.
259 We will see how to choose between these two
\emph{drawing engines
}.
261 Moreover, the following packages are necessary:
263 \item \Verb+xkeyval+ to manage the optional arguments;
264 \item \Verb+xcolor+ to use colors (needed by
\Verb+luamplib+);
265 \item \Verb+ifthen+ to help the programming with
\TeX.
269 \section{The Basic Macros
}
271 Let us recall that this package provides macros to draw two
272 dimensional triangulations (or meshes).
274 \subsection{Draw a Complete Mesh
}\label{sec:buildMesh
}
276 \commande|
\buildMeshBW[«options»
]{«list of points» or «file name»
}|
\medskip
278 This macro produces the Delaunay triangulation (using the Bowyer and
279 Watson algorithm) of the given
\meta{list of points
}. The list of
280 points must be given in the following way :
282 \verb+(x1,y1);(x2,y2);(x3,y3);...;(xn,yn)+
286 \buildMeshBW{(
0.3,
0.3);(
1.5,
1);(
4,
0);(
4.5,
2.5);(
1.81,
2.14);(
2.5,
0.5);(
2.8,
1.5)
}
289 \subsubsection{The Options
}
291 There are several options to customize the drawing.
293 \item[mode = int (default)
\textme{or
} ext:
] this option allows to
294 use either the previously described set of points in the argument, or
295 a file, containing, line by line (
2 columns), the points. Such a
304 \item[bbox = none (default)
\textme{or
} show:
] this option allows to draw the
305 added points to form a
\emph{bounding box
}\footnote{The bounding
306 box is defined by four points place at
15\% around the box
307 defined by $(x_
{\min},y_
{\min})$, $(x_
{\min},y_
{\max})$,
308 $(x_
{\max},y_
{\max})$, and $(x_
{\min},y_
{\max})$. It is used by
309 the algorithm and will be computed in any case.
} and the corresponding
310 triangulation. By default, these triangles are removed at the end of
312 \item[color =
\meta{value
} (default: black):
] The
color of the
314 \item[colorBbox =
\meta{value
} (default: black):
] The
color of the
315 drawing for the elements (points and triangles) belonging to the
317 \item[print = none (default)
\textme{or
} points:
] To label the vertices of the
318 triangulation. This also adds a
\emph{dot
} at each vertex.
319 \item[meshpoint =
\meta{value
} (default: P):
] The letter(s) used to
320 label the vertices of the triangulation. It is included in the math
321 mode delimiters
\Verb+$...$+. The bounding box points are labeled
322 with numbers
1 to
4 and with a star exponent.
323 \item[tikz (boolean, default:false):
] By default, this boolean is set
324 to
\Verb+false+, and MetaPost (with
\Verb+luamplib+) is used to draw
325 the picture. With this option,
\Verb+tikz+ becomes the
\textit{drawing
327 \item[scale =
\meta{value
} (default:
1cm):
] The scale option defines
328 the scale at which the picture is drawn (the same for both
329 axes). It must contain the unit of length (cm,
333 To illustrate the options, let us show you an example. We consider a
334 file
\Verb+mesh.txt+:
358 The drawing engine is not very relevant here, but it is useful to
359 understand how the drawing is made. However, the engine will be
361 the so called
\emph{inc
} macros (section~
\ref{sec:inc
}), for adding
362 code before and after the one generated by
366 \subsection{Draw the Set of Points
}
368 \commande|
\drawPointsMesh[«options»
]{«list of points» or «file name»
}|
\medskip
370 With the
\Verb+
\drawPointsMesh+, we plot the set of (user chosen) points from
371 which the Bowyer and Watson algorithm computes the triangulation.
373 The use of this macro is quite similar to
374 \Verb+
\buildMeshBW+. Here is an example of the basic uses.
376 \drawPointsMesh{(
0.3,
0.3);(
1.5,
1);(
4,
0);(
4.5,
2.5);(
1.81,
2.14);(
2.5,
0.5);(
2.8,
1.5)
}
380 \subsubsection{The Options
}
382 There are several options (exactly the same as for the
383 \Verb+
\buildMeshBW+) to customize the drawing.
385 \item[mode = int (default)
\textme{or
} ext:
] this option allows to
386 use either the previously described set of points as the argument, or
387 a file, containing, line by line (
2 columns), the points. Such a
396 \item[bbox = none (default)
\textme{or
} show:
] this option allows to draw the
397 added points to form a
\emph{bounding box
} and the corresponding
398 triangulation. By default, these triangles are removed at the end of
399 the algorithm.
\emph{Here, because we plot only the vertices of the
400 mesh, there are no triangles, only dots.
}
401 \item[color =
\meta{value
} (default: black):
] The
color of the
403 \item[colorBbox =
\meta{value
} (default: black):
] The
color of the
404 drawing for the elements (points and triangles) belonging to the
406 \item[print = none (default)
\textme{or
} points:
] To label the vertices of the
407 triangulation. This also adds a
\emph{dot
} at each vertex. Without
408 label, there is still the dot.
409 \item[meshpoint =
\meta{value
} (default: P):
] The letter(s) used to
410 label the vertices of the triangulation. It is included in the math
411 mode delimiters
\Verb+$...$+. The bounding box points are labeled
412 with numbers
1 to
4 and with a star exponent.
413 \item[tikz (boolean, default:false):
] By default, this boolean is set
414 to
\Verb+false+, and MetaPost (with
\Verb+luamplib+) is used to draw
415 the picture. With this option,
\Verb+tikz+ becomes the
\textit{drawing
417 \item[scale =
\meta{value
} (default:
1cm):
] The scale option defines
418 the scale at which the picture is drawn (the same for both
419 axes). It must contain the unit of length (cm,
422 With the same external mesh point file presented in
423 section~
\ref{sec:buildMesh
}, we illustrate the different options.
439 \subsection{Draw a Step of the Bowyer and Watson Algorithm
}
441 \commande|
\meshAddPointBW[«options»
]{«list of points» or «file name»
}{«point» or «number of line»
}|
\medskip
443 This command allows to plot the steps within the addition of a
444 point in a Delaunay triangulation, by the Bowyer and Watson
447 This macro produces the Delaunay triangulation (using the Bowyer and
448 Watson algorithm) of the given
\meta{list of points
} and shows a step
449 of the algorithm when the
\meta{point
} is added. The list of
450 points must be given in the following way:
452 \verb+(x1,y1);(x2,y2);(x3,y3);...;(xn,yn)+
454 and the point is of the form
\verb+(x,y)+. The
\meta{file name
}
455 and
\meta{number of line
} will be explained in the option
458 One can use the macro as fallows:
460 \meshAddPointBW[step=badtriangles
]{(
1.5,
1);(
4,
0);(
4.5,
2.5);(
1.81,
2.14);(
2.5,
0.5);(
2.8,
1.5)
}{(
2.2,
1.8)
}
461 \meshAddPointBW[step=cavity
]{(
1.5,
1);(
4,
0);(
4.5,
2.5);(
1.81,
2.14);(
2.5,
0.5);(
2.8,
1.5)
}{(
2.2,
1.8)
}
462 \meshAddPointBW[step=newtriangles
]{(
1.5,
1);(
4,
0);(
4.5,
2.5);(
1.81,
2.14);(
2.5,
0.5);(
2.8,
1.5)
}{(
2.2,
1.8)
}
464 The default value for
\Verb+step+ is
465 \Verb+badtriangles+. Consequently, the first
466 line is equivalent to
468 \meshAddPointBW{(
1.5,
1);(
4,
0);(
4.5,
2.5);(
1.81,
2.14);(
2.5,
0.5);(
2.8,
1.5)
}{(
2.2,
1.8)
}
471 \subsubsection{The Options
}
473 There are several options (some of them are the same as for
474 \Verb+
\buildMeshBW+) to customize the drawing.
476 \item[mode = int (default)
\textme{or
} ext:
] this option allows to
477 use either the previously described set of point in the first
479 a file containing, line by line (
2 columns), the points. Such a
488 For the second argument of the macro, if we are in the
489 \Verb+mode = ext+, the argument must be the
\emph{line number
} of the file
490 corresponding to the point we want to add. The algorithm will stop the
491 line before to build the initial triangulation for which it will add
492 the point corresponding to the line. The subsequent lines in the file are
494 \item[bbox = none (default)
\textme{or
} show:
] this option allows to draw the
495 added points to form a
\emph{bounding box
} and the corresponding
496 triangulation. By default, these triangles are removed at the end of
498 \item[color =
\meta{value
} (default: black):
] The
color of the
500 \item[colorBbox =
\meta{value
} (default: black):
] The
color of the
501 drawing for the elements (points and triangles) belonging to the
503 \item[colorNew =
\meta{value
} (default: red):
] The
color of the
504 drawing of the ``new'' elements which are the point to add, the
505 polygon of the cavity, and the new triangles.
506 \item[colorBack =
\meta{value
} (default: black!
20):
] The
color for the
507 filling of the region concerned by the addition of the new point.
508 \item[colorCircle =
\meta{value
} (default: green):
] The
color for
509 the circumcircle of the triangles containing the point to add.
510 \item[meshpoint =
\meta{value
} (default: P):
] The letter(s) used to
511 label the vertices of the triangulation. It is included in the math
512 mode delimiters
\Verb+$...$+. The bounding box points are labeled
513 with numbers
1 to
4 and with a star exponent.
514 \item[step = badtriangles (default)
\textme{or
} cavity
\textme{or
}
515 newtriangles:
] To choose the step we want to draw, corresponding to
516 the steps of the Bowyer and Watson algorithm.
517 \item[newpoint =
\meta{value
} (default: P):
] The letter(s) used to
518 label the new point of the triangulation. It is include in the math
519 mode delimiters
\Verb+$...$+.
520 \item[tikz (boolean, default:false):
] By default, this boolean is set
521 to
\Verb+false+, and MetaPost (with
\Verb+luamplib+) is used to draw
522 the picture. With this option,
\Verb+tikz+ is the
\textit{drawing
524 \item[scale =
\meta{value
} (default:
1cm):
] The scale option defines
525 the scale at which the picture is draw (the same for the two
526 axis). It must contain the unit of length (cm,
530 Here is an example of customizing the drawing. First, recall that
531 the external file
\Verb+mesh.txt+ is:
541 We draw the addition of the
6th point. The
7th line will be ignored.
550 colorNew = green!
50!red,
552 colorBbox = black!
20,
559 \section{The
\emph{inc
} Macros
}\label{sec:inc
}
561 The three macros presented in the above sections have complementary
562 macros, with the suffix
\Verb+inc+ that allow the user to add code
563 (MetaPost or
\Verb+tikz+, depending of the drawing engine) before and
564 after the code generated by
\luamesh.
566 The three macros are:
\medskip
569 \commande|
\buildMeshBWinc[«options»
]{«list of points» or «file name»
}{«code before»
}{«code after»
}|
\medskip
571 \commande|
\drawPointsMeshinc[«options»
]{«list of points» or «file name»
}{«code before»
}{«code after»
}|
\medskip
573 \commande|
\meshAddPointBWinc[«options»
]{«list of points» or «file name»
}%|
575 \commande|
{«point» or «number of line»
}{«code before»
}{«code after»
}|
\medskip
577 \subsection{With MetaPost
}
579 We consider the case where the drawing engine is MetaPost (through the
580 \Verb+luamplib+ package).
582 We describe the feature taking one macro in example but the mechanism
583 and the possibilities are exactly the same for all the macros.
585 When we use the MetaPost drawing engine, the macros previously
586 described produced a code of the form
591 <code for the drawing>
596 Then, the arguments
\meta{code before
} and
\meta{code after
} are
602 <code for the drawing>
607 With the
\emph{inc
} macros, the user has to add the
\Verb+beginfig();+
608 and
\Verb+endfig;+ commands to produce a picture. Indeed, this allows
609 to use the
\Verb+
\everymplib+ command from the
\Verb+
\luamplib+ package.
612 \subsubsection{The
\LaTeX{} Colors Inside the MetaPost Code
}
614 The configurable colors
615 of the
\LaTeX{} macro are accessible inside the MetaPost code. For
616 \Verb+
\buildMeshBWinc+ and
\Verb+
\drawPointsMeshinc+, we have
617 \Verb+
\luameshmpcolor+,
618 and
\Verb+
\luameshmpcolorBbox+.
619 For the macro
\Verb+
\meshAddPointBWinc+ we have three additional
620 colors :
\Verb+
\luameshmpcolorBack+,
\Verb+
\luameshmpcolorNew+, and
621 \Verb+
\luameshmpcolorCircle+. Of course, we can define MetoPost
622 colors as well. Finally, the
\Verb+luamplib+ mechanism of
623 \Verb+
\mpcolor+ is also available.
625 \subsubsection{The Mesh Points
}
627 At the beginning of the automatically generated code, a list of
628 MetaPost
\Verb+pair+s are defined corresponding to all the vertices of
629 the mesh (when the option
\Verb+bbox=show+, the last
4 points are the
630 \emph{bounding box points
}). The points are available with the
631 \Verb+MeshPoints
[]+ table of variables. The
\Verb+MeshPoints
[i
]+ are
632 defined using the unit length
\Verb+u+.
634 \subsubsection{Examples
}
636 Here is three examples for the different macros.
643 ]{(
0.3,
0.3);(
1.5,
1);(
4,
0);(
4.5,
2.5);(
1.81,
2.14);(
2.5,
0.5);(
2.8,
1.5)
}%
648 label(btex Mesh $
\mathbb{T
}$ etex, (
0,
2u)) withcolor
\luameshmpcolor;
658 ]{(
0.3,
0.3);(
1.5,
1);(
4,
0);(
4.5,
2.5);(
1.81,
2.14);(
2.5,
0.5);(
2.8,
1.5)
}%
663 drawdblarrow MeshPoints
[3] -- MeshPoints
[9] withpen pencircle scaled
1pt
664 withcolor (
0.3,
0.7,
0.2);
671 colorNew = green!
50!red,
673 colorBbox = black!
20,
677 {(
0.3,
0.3);(
1.5,
1);(
4,
0);(
4.5,
2.5);(
1.81,
2.14);(
2.5,
0.5)
}{(
2.8,
1.5)
}%
684 fill MeshPoints
[7]--MeshPoints
[8]--MeshPoints
[9]--MeshPoints
[10]--cycle
685 withcolor
\mpcolor{blue!
10};
691 The variables
\Verb+MeshPoints
[]+ are not defined for the argument
692 corresponding to the code to place before the code generated by
693 \luamesh. Hence, to use such variables, we have to define a
694 \Verb+picture+ as shown in the third example above.
698 \subsection{With TikZ
}
700 If we have chosen
\Verb+tikz+ as the engine drawing, the added code
701 will be written in
\Verb+tikz+. In that case, the two arguments
702 \meta{code before
} and
\meta{code after
} will be inserted as follows:
705 \begin{tikzpicture
}[x=<scale>,y=<scale>
]
712 Because the engine is
\Verb+tikz+ their is no issue with colors, the
713 \LaTeX{} colors (e.g.:
\Verb+xcolor+) can be directly used.
715 \subsubsection{The Mesh Points
}
717 The points of the mesh are defined here as
\Verb+tikz+
718 \Verb+
\coordinate+ named as follows
720 \coordinate (MeshPoints1) at (...,...);
721 \coordinate (MeshPoints2) at (...,...);
722 \coordinate (MeshPoints3) at (...,...);
726 Once again these coordinates are not yet defined for the
\meta{code
729 \subsubsection{Examples
}
738 ]{(
0.3,
0.3);(
1.5,
1);(
4,
0);(
4.5,
2.5);(
1.81,
2.14);(
2.5,
0.5);(
2.8,
1.5)
}%
742 \node[color = blue!
50] at (
0,
2)
{Mesh $
\mathbb{T
}$
} ;
752 ]{(
0.3,
0.3);(
1.5,
1);(
4,
0);(
4.5,
2.5);(
1.81,
2.14);(
2.5,
0.5);(
2.8,
1.5)
}%
756 \draw[<->,thick,
color=green
] (MeshPoints3) -- (MeshPoints9);
768 %%% ispell-local-dictionary: "american"