% \iffalse
% --------------------------------------------------------------------
%<*hex>
% \fi
% 
% \subsubsection{Ridges}
% \label{sec:impl:hex:ridges}
%
% A hex can be decorated with up to 6 ridges --- one for each edge of
% the hexagon.  The first thing is to set up the graphics style to use
% for the ridges.  We use the \spec{wave} decoration. 
%
% If rounded corners are set for ridges, (e.g., via \spec{every hex
% ridges}), then it should be  \spec{0pt} or \spec{4pt} (roughly 2mm)
% or larger.  Otherwise, one will get a ``dimension too large''
% error. 
% 
%    \begin{macrocode}
\tikzset{%
  hex/ridges pre/.style={
    line cap=round,
    draw=pgfstrokecolor,
    solid,
    /hex/ridges/.cd,%
    radius=0.85,%
    n=4,
    R=.25,
  },
  hex/ridges/.style={
    get scale,
    decoration={
      path has corners=true,
      waves,
      radius=\wg@scale\hex@r@R,
      segment length=\wg@scale\hex@r@s,
    },
    decorate}}
%    \end{macrocode}
%
% To properly set up the ridges, we need to concatenate ridge paths in
% order.  To facilitate that, we define 6 \cs{if}s --- one for each
% edge.
% 
%    \begin{macrocode}
\newif\ifhex@r@ne
\newif\ifhex@r@n
\newif\ifhex@r@nw
\newif\ifhex@r@sw
\newif\ifhex@r@s
\newif\ifhex@r@se
%    \end{macrocode}
%
% Next is the keys for each edge.  These will set the above \cs{if}s
% to \spec{true}.  We put these into the family \spec{/hex/r} so that
% we can parse them separately. 
% 
%    \begin{macrocode}
\tikzset{%
  /hex/ridges/.search also={/tikz},
  /hex/ridges/.cd,
  north east/.is if=hex@r@ne,
  north/.is if=hex@r@n,
  north west/.is if=hex@r@nw,
  south west/.is if=hex@r@sw,
  south/.is if=hex@r@s,
  south east/.is if=hex@r@se,
  radius/.store in=\hex@r@r,
  curve radius/.store in=\hex@r@w,
  NE/.is if=hex@r@ne,
  N/.is if=hex@r@n,
  NW/.is if=hex@r@nw,
  SW/.is if=hex@r@sw,
  S/.is if=hex@r@s,
  SE/.is if=hex@r@se,
  r/.store in=\hex@r@r,
  n/.store in=\hex@r@n,
  R/.store in=\hex@r@w,
}
%    \end{macrocode}
%
% \begin{Macro}{\hex@do@ridges}
%
%   This is the macro that actually generates the ridge.  We use the
%   same \textsc{pgf} filtered key parsing trick as above.   Note that
%   the routine below is handcrafted since it is relatively simple.
%   
%    \begin{macrocode}
\newdimen\hex@r@s
\newdimen\hex@r@R
\def\hex@do@ridges{%
  \edef\hex@r@tmp{[
    hex/ridges pre,
    /tikz/every hex ridges/.try,
    \hex@ridges]}
  \expandafter\scope\hex@r@tmp%
    \hex@dbg{3}{Ridges: `\meaning\hex@ridges', `\meaning\hex@r@tmp'
      ^^Jnorth east=\ifhex@r@ne yes\else no\fi
      ^^Jnorth     =\ifhex@r@n  yes\else no\fi
      ^^Jnorth west=\ifhex@r@nw yes\else no\fi
      ^^Jsouth west=\ifhex@r@sw yes\else no\fi
      ^^Jsouth     =\ifhex@r@s  yes\else no\fi
      ^^Jsouth east=\ifhex@r@se yes\else no\fi
      ^^Jradius    =\hex@r@r
      ^^Jn         =\hex@r@n
    }
    \pgfmathparse{\hex@r@r/\hex@r@n}\xdef\hex@r@t{\pgfmathresult}
    \hex@r@s=\hex@r@t cm
    \hex@r@R=\hex@r@w cm
    \def\hex@r@p{}
    % Hand written algorithm
    \ifhex@r@ne
      \ifhex@r@se
        \xdef\hex@r@p{(0:\hex@r@r)--(60:\hex@r@r)}
      \else
        \xdef\hex@r@p{($(0:\hex@r@r)+(-60:\hex@r@t/2)$)--(60:\hex@r@r)}
      \fi
      \hex@dbg{4}{Ridge along north east edge: `\hex@r@p'}
    \fi
    \ifhex@r@n
      \ifhex@r@ne\else
        \xdef\hex@r@p{\hex@r@p ($( 60:\hex@r@r)+(0:\hex@r@t/2)$)}
      \fi
      \xdef\hex@r@p{\hex@r@p --(120:\hex@r@r)}
      \hex@dbg{4}{Ridge along north edge: `\hex@r@p'}
    \fi
    \ifhex@r@nw
      \ifhex@r@n\else
        \xdef\hex@r@p{\hex@r@p ($(120:\hex@r@r)+(60:\hex@r@t/2)$)}
      \fi
      \xdef\hex@r@p{\hex@r@p --(180:\hex@r@r)}
      \hex@dbg{4}{Ridge along north west: `\hex@r@p'}
    \fi
    \ifhex@r@sw
      \ifhex@r@nw\else
        \xdef\hex@r@p{\hex@r@p ($(180:\hex@r@r)+(120:\hex@r@t/2)$)}
      \fi
      \ifhex@r@s  
        \xdef\hex@r@p{\hex@r@p --(240:\hex@r@r)}
      \else
        \xdef\hex@r@p{\hex@r@p --(240:\hex@r@r)}
      \fi
      \hex@dbg{4}{Ridge along south west: `\hex@r@p'}
    \fi
    \ifhex@r@s
      \ifhex@r@sw\else
        \xdef\hex@r@p{\hex@r@p ($(240:\hex@r@r)+(-\hex@r@t/2,0)$)}
      \fi
      \ifhex@r@se
        \xdef\hex@r@p{\hex@r@p --(300:\hex@r@r)}
      \else
        \xdef\hex@r@p{\hex@r@p --(300.5:\hex@r@r)}
      \fi
      \hex@dbg{4}{Ridge along south: `\hex@r@p'}
    \fi
    \ifhex@r@se
      \ifhex@r@s\else
        \xdef\hex@r@p{\hex@r@p ($(300:\hex@r@r)+(-120:\hex@r@t/2)$)}
      \fi
      \ifhex@r@ne
      %\xdef\hex@r@p{\hex@r@p --cycle}
      \xdef\hex@r@p{\hex@r@p -- (0:\hex@r@r)}
      \else
        \xdef\hex@r@p{\hex@r@p --(.5:\hex@r@r)}
      \fi
      \hex@dbg{4}{Ridge along south east: `\hex@r@p'}
    \fi
    \hex@dbg{3}{ Ridges path: \hex@r@p}
    % \draw[red] \hex@r@p;
    \draw[hex/ridges] \hex@r@p;
  \endscope% End of ridges scope 
}
%    \end{macrocode}
% \end{Macro}
% 
% \iffalse
%</hex>
% --------------------------------------------------------------------
% \fi 
