% Copyright 2022 by Qrrbrbirlbel
%
% This file may be distributed and/or modified
%
% 1. under the LaTeX Project Public License and/or
% 2. under the GNU Free Documentation License.
%
\usepgflibrary{ext.pgfkeys-plus}%

%% full arc
%% https://tex.stackexchange.com/a/144297/16595
%% The postfix operator R is added to PGFmath,
%% it allows the use of angular segments.
%% full arc = 10 makes it so that
%%   1R =  36°
%%   2R =  72°
%%   …
%%  10R = 360°
%% Calling the full arc key with an empty value 
%% makes 1R = 1°
\pgfset{full arc/.code=%
  \def\pgf@temp{#1}%
  \ifx\pgfutil@empty\pgf@temp
    \let\pgfmath@fullarc@factor\pgfutil@empty
  \else
    \pgfmathsetmacro\pgfmath@fullarc@factor{360/(#1)}%
  \fi,full arc=}
\pgfmathdeclareoperator{R}{full arc}{1}{postfix}{950}
\pgfmathdeclarefunction{full arc}{1}{%
  \begingroup
    \pgfmath@x=#1pt\relax
    \ifx\pgfmath@fullarc@factor\pgfutil@empty\else
      \pgfmath@x\pgfmath@fullarc@factor\pgfmath@x
    \fi
    \pgfmath@returnone\pgfmath@x
  \endgroup}

%% foreach
%% http://tex.stackexchange.com/a/126418/16595
%% instead of \foreach \var in {start, start + delta, ..., end}
%% this allows to use \foreach[use int={start to end step delta}] without(!) a list in {}
%% the values start, end and delta are evaluated by PGFmath at the start of the loop.
%% In addition to use int, there's also use float.
\newif\iftikzext@pgf@foreach@no@list
\pgfqkeys{/pgf/foreach}{
  use int/.code={%
    \tikzext@pgf@foreach@no@list@parse@to\pgfmathtruncatemacro#1\pgffor@stop
  },
  use float/.code={%
    \tikzext@pgf@foreach@no@list@parse@to\pgfmathsetmacro#1\pgffor@stop
  }
}

\def\tikzext@pgf@foreach@no@list@parse@to#1#2to#3\pgffor@stop{%
  \tikzext@pgf@foreach@no@listtrue
  #1\foreachStart{#2}%
  \pgfutil@in@{step}{#3}
  \ifpgfutil@in@
    \tikzext@pgf@foreach@no@list@parse@step{#1}#3\pgffor@stop
  \else
    \tikzext@pgf@foreach@no@list@parse@step{#1}#3step1\pgffor@stop
  \fi
  \edef\tikzext@pgf@foreach@no@list@list{\foreachStart,\foreachSecond,...,\foreachEnd}%
}
\def\tikzext@pgf@foreach@no@list@parse@step#1#2step#3\pgffor@stop{%
  #1\foreachEnd{#2}%
  #1\foreachSecond{\foreachStart+#3}%
}
\def\pgffor@vars{% manually extended, better etoolbox
  \pgfutil@ifnextchar i{\pgffor@@vars@end}{%
    \pgfutil@ifnextchar[{\pgffor@@vars@opt}{%]
      \pgfutil@ifnextchar/{\pgffor@@vars@slash@gobble}{%
         \iftikzext@pgf@foreach@no@list\expandafter\pgfutil@firstoftwo\else
           \expandafter\pgfutil@secondoftwo\fi
         {\tikzext@pgf@foreach@no@listfalse\pgffor@macro@list\tikzext@pgf@foreach@no@list@list}
         {\pgffor@@vars}}}}}%

%% PGFmath
%% strrepeat("x", 5) = "xxxxx"
\pgfmathdeclarefunction{strrepeat}{2}{%
  \begingroup\pgfmathint{#2}\pgfmath@count\pgfmathresult
    \let\pgfmathresult\pgfutil@empty
    \pgfutil@loop\ifnum\pgfmath@count>0\relax
      \expandafter\def\expandafter\pgfmathresult\expandafter{\pgfmathresult#1}%
      \advance\pgfmath@count-1\relax
    \pgfutil@repeat\pgfmath@smuggleone\pgfmathresult\endgroup}

%% isInString("foo", "foobar") = true (= 1)
%% isInString("boo", "foobar") = false (= 0)
\pgfmathdeclarefunction{isInString}{2}{%
  \pgfutil@in@{#1}{#2}%
  \ifpgfutil@in@\def\pgfmathresult{1}\else\def\pgfmathresult{0}\fi}

%% strcat("foo", "bar") = "foobar" (can have more than two parameters)
\pgfutil@IfUndefined{pgfmathstrcat@}{
  \pgfmathdeclarefunction{strcat}{...}{%
    \begingroup
      \let\pgfmathresult\pgfutil@empty
      \pgfmathstrcat@@#1\pgfmath@stop}
  \def\pgfmathstrcat@@#1{%
    \ifx\pgfmath@stop#1%
      \def\pgfmath@next{\pgfmath@smuggleone\pgfmathresult\endgroup}
    \else
      \expandafter\def\expandafter\pgfmathresult\expandafter{\pgfmathresult#1}%
      \let\pgfmath@next\pgfmathstrcat@@
    \fi
    \pgfmath@next}
}{}

% http://tex.stackexchange.com/questions/244569/bounding-lines-around-tax-nodes/244619#244619
\pgfmathdeclarefunction{atanXY}{2}{\pgfmathatantwo@{#2}{#1}}
\pgfmathdeclarefunction{atanYX}{2}{\pgfmathatantwo@{#1}{#2}}

%% http://tex.stackexchange.com/a/132939/16595
\tikzset{
  @edges through/.code={{{% three braces to protect \pgfeov
    \pgfutil@ifnextchar[{\pgfkeysvalueof{/tikz/@@edges through/.@cmd}}
                        {\pgfkeysvalueof{/tikz/@@edges through/.@cmd}[]}#1\pgfeov}}},
  @@edges through/.style args={[#1]#2}{/tikz/insert path={edge[#1] (#2) (#2)}},
  edges through/.style={/tikz/@edges through/.list={#1}}}
\tikzset{
  @edges to/.code={{{% three braces to protect \pgfeov
    \pgfutil@ifnextchar[{\pgfkeysvalueof{/tikz/@@edges to/.@cmd}}
                        {\pgfkeysvalueof{/tikz/@@edges to/.@cmd}[]}#1\pgfeov}}},
  @@edges to/.style args={[#1]#2}{/tikz/insert path={edge[#1] (#2)}},
  edges to/.style={/tikz/@edges to/.list={#1}}}
\tikzset{
  @tos to/.code={{{% three braces to protect \pgfeov
    \pgfutil@ifnextchar[{\pgfkeysvalueof{/tikz/@@tos to/.@cmd}}
                        {\pgfkeysvalueof{/tikz/@@tos to/.@cmd}[]}#1\pgfeov}}},
  @@tos to/.style args={[#1]#2}{/tikz/insert path={to[#1] (#2)}},
  tos to/.style={/tikz/@tos to/.list={#1}}}


\pgfmathdeclarefunction{distancebetween}{2}{% only coordinates/nodes
  \begingroup
    \pgfpointdiff{\pgfpointanchor{#1}{center}}{\pgfpointanchor{#2}{center}}%
    \edef\pgfmath@temp{{\pgf@sys@tonumber\pgf@x}{\pgf@sys@tonumber\pgf@y}}%
    \expandafter\pgfmathveclen@\pgfmath@temp
    \pgfmath@smuggleone\pgfmathresult
  \endgroup}
\pgfmathdeclarefunction{qdistancebetween}{1}{% only coordinates/nodes
  \begingroup
    \pgfpointdiff{\pgfpointorigin}{\pgfpointanchor{#1}{center}}%
    \edef\pgfmath@temp{{\pgf@sys@tonumber\pgf@x}{\pgf@sys@tonumber\pgf@y}}%
    \expandafter\pgfmathveclen@\pgfmath@temp
    \pgfmath@smuggleone\pgfmathresult
  \endgroup}

\pgfmathdeclarefunction{qanglebetween}{1}{%
  \pgfmathanglebetweenpoints{\pgfpointorigin}{\pgfpointanchor{#1}{center}}}

\pgfmathdeclarefunction{anglebetween}{2}{%
  \pgfmathanglebetweenpoints{\pgfpointanchor{#1}{center}}{\pgfpointanchor{#2}{center}}}

\pgfmathdeclarefunction{isEmpty}{1}{%
  \begingroup
    \def\pgfmath@temp{#1}%
    \pgfutil@ifxempty\pgfmath@temp{\def\pgfmathresult{1}}{\def\pgfmathresult{0}}%                           
    \pgfmath@smuggleone\pgfmathresult
  \endgroup}

\pgfset{
  declare constant/.code={%
    \let\pgfmathdeclareconstant@@@\pgfutil@empty
    \pgfkeysvalueof{/pgf/declare function/execute at begin function}%
    \pgfmathdeclareconstant@#1@=@;\pgf@stop
    \pgfkeysvalueof{/pgf/declare function/execute at end function}%
    \pgfmathdeclareconstant@@@
  }}
\def\pgfmathdeclareconstant@{%
  \pgfutil@ifnextchar x\pgfmathdeclareconstant@@\pgfmathdeclareconstant@@
}
\def\pgfmathdeclareconstant@@#1=#2;#3\pgf@stop{%
  \edef\pgfmath@local@temp{#1}%
  \pgfutil@ifx\pgfmath@local@temp\pgfmath@local@at{}{%
    \pgfutil@g@addto@macro\pgfmathdeclareconstant@@@{\pgfmathdeclarepseudoconstant{#1}{\def\pgfmathresult{#2}}}%
    \pgfmathdeclareconstant@#3\pgf@stop
  }%
}