Skip to content

Commit fdde2c8

Browse files
committed
Add a new monotone interpolation plot handler
This handler converts each plot stream command into a curveto command, except for the first, which is converted to the previously specified action. The curveto control points are computed and adjusted so that the resulting curve respects the monotonicity of the points. If a point is a local extremum of the point set, it will also be a local extremum of the generated curve. See https://en.wikipedia.org/wiki/Monotone_cubic_interpolation for an explanation of the algorithm. The last part uses \pgfpathcurveto instead of using Hermitte base functions. The plot handler is also made available to datavisualization through the `smooth monotone line` visualizer option. Signed-off-by: Julien '_FrnchFrgg_' RIVAUD <[email protected]>
1 parent d43fb10 commit fdde2c8

File tree

5 files changed

+164
-0
lines changed

5 files changed

+164
-0
lines changed

doc/generic/pgf/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
1111
### Added
1212

1313
- Add `RGB` and `gray` color model support for ConTeXt #1130
14+
- Add a new monotonic interpolation plot handler #1358
1415

1516
### Fixed
1617

doc/generic/pgf/pgfmanual-en-library-plot-handlers.tex

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,38 @@ \subsection{Curve Plot Handlers}
9797
\end{command}
9898

9999

100+
\begin{command}{\pgfplothandlermonotone}
101+
This handler will issue a |\pgfpathcurveto| command for each point of the
102+
plot, \emph{except} possibly for the first. As for the line-to handler,
103+
what happens with the first point can be specified using
104+
|\pgfsetmovetofirstplotpoint| or |\pgfsetlinetofirstplotpoint|.
105+
106+
Obviously, the |\pgfpathcurveto| command needs, in addition to the points
107+
on the path, some control points. These are computed using The
108+
Fritsch-Carlson monotone cubic interpolation algorithm which roughly does
109+
the following:
110+
111+
\begin{enumerate}
112+
\item Initialize tangent slopes at each input point as the
113+
average of the slopes of the neighboring secant lines.
114+
\item If the secants have opposite signes (because this is a local
115+
extremum), set the tangent slope to~$0$ to get an horizontal
116+
tangent and prevent overshoot. Same if one of the neighboring
117+
secants is horizontal.
118+
\item Adjust the tangent slopes to ensure strict monotonicity.
119+
\item Place the control points on the computed tangent lines,
120+
at one third of the $x$~distance of a secant line: if $a$, $b$
121+
and~$c$ are three consecutive input points and $m$~is the
122+
slope computed at~$B$, then the control points around~$b$ will
123+
be $u$~and~$v$ where $x_u = x_b - \frac{1}{3}(x_b-x_a)$,
124+
$y_u = y_b - \frac{1}{3} m (x_b-x_a)$,
125+
$x_v = x_b + \frac{1}{3}(x_c-x_b)$ and
126+
$y_v = y_b + \frac{1}{3} m (x_c-x_b)$.
127+
\end{enumerate}
128+
\end{command}
129+
130+
131+
100132
\subsection{Constant Plot Handlers}
101133

102134
There are several plot handlers which produce piecewise constant interpolations

tex/generic/pgf/frontendlayer/tikz/libraries/datavisualization/tikzlibrarydatavisualization.code.tex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1538,6 +1538,7 @@
15381538
smooth cycle/.style={@set={\pgfplothandlerclosedcurve}{default label in legend closed path}},
15391539
straight line/.style={@set={\pgfplothandlerlineto}{default label in legend path}},
15401540
straight cycle/.style={@set={\pgfplothandlerpolygon}{default label in legend closed path}},
1541+
smooth monotone line/.style={@set={\pgfplothandlermonotone}{default label in legend path}},
15411542
polygon/.style={straight cycle},% alias
15421543
gap line/.style={@set={\pgfplothandlergaplineto}{default label in legend path}},
15431544
gap cycle/.style={@set={\pgfplothandlergapcycle}{gap circular label in legend line}}

tex/generic/pgf/frontendlayer/tikz/tikz.code.tex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1232,6 +1232,7 @@
12321232
% Plot options
12331233
\tikzoption{smooth}[]{\let\tikz@plot@handler=\pgfplothandlercurveto}%
12341234
\tikzoption{smooth cycle}[]{\let\tikz@plot@handler=\pgfplothandlerclosedcurve}%
1235+
\tikzoption{smooth monotone}[]{\let\tikz@plot@handler=\pgfplothandlermonotone}%
12351236
\tikzoption{sharp plot}[]{\let\tikz@plot@handler\pgfplothandlerlineto}%
12361237
\tikzoption{sharp cycle}[]{\let\tikz@plot@handler\pgfplothandlerpolygon}%
12371238

tex/generic/pgf/libraries/pgflibraryplothandlers.code.tex

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,135 @@
269269
}%
270270

271271

272+
% This handler converts each plot stream command into a curveto
273+
% command, except for the first, which is converted to the previously
274+
% specified action. The curveto control points are computed and adjusted
275+
% so that the resulting curve respects the monotonicity of the points.
276+
% If a point is a local extremum of the point set, it will also be
277+
% a local extremum of the generated curve.
278+
% This handler treats the x and y direction differently, and expects
279+
% x values to be monotone.
280+
% See https://en.wikipedia.org/wiki/Monotone_cubic_interpolation for an
281+
% explanation of the algorithm. The last part uses \pgfpathcurveto
282+
% instead of using Hermitte base functions.
283+
\pgfdeclareplothandler{\pgfplothandlermonotone}{}{%
284+
point macro=\pgf@plot@monotone@dopointone,
285+
jump macro=\pgf@plot@monotone@dojump,
286+
end macro=\pgf@plot@monotone@doend
287+
}%
288+
289+
\def\pgf@plot@monotone@dojump{%
290+
\pgf@plot@monotone@doend
291+
\global\pgf@plot@startedfalse
292+
\global\let\pgf@plotstreampoint\pgf@plot@monotone@dopointone
293+
}%
294+
295+
\def\pgf@plot@monotone@dopointone#1{%
296+
\pgf@process{#1}%
297+
\pgf@plot@first@action{\pgfqpoint{\pgf@x}{\pgf@y}}%
298+
\xdef\pgf@plot@monotone@pointone{\noexpand\pgfqpoint{\the\pgf@x}{\the\pgf@y}}%
299+
\global\let\pgf@plotstreampoint=\pgf@plot@monotone@dopointtwo
300+
}%
301+
302+
\def\pgf@plot@monotone@dopointtwo#1{%
303+
\pgf@process{#1}%
304+
\xdef\pgf@plot@monotone@pointtwo{\noexpand\pgfqpoint{\the\pgf@x}{\the\pgf@y}}%
305+
% compute vector:
306+
\pgf@xa=\pgf@x%
307+
\pgf@ya=\pgf@y%
308+
\pgf@process{\pgf@plot@monotone@pointone}%
309+
\advance\pgf@xa by-\pgf@x
310+
\advance\pgf@ya by-\pgf@y
311+
% save left delta
312+
\xdef\pgf@plot@monotone@deltaleft{\pgf@sys@tonumber\pgf@xa}%
313+
% compute slope
314+
\pgfmathdivide@{\the\pgf@ya}{\the\pgf@xa}%
315+
\global\let\pgf@plot@monotone@rateleft\pgfmathresult
316+
% init tangent one
317+
\global\let\pgf@plot@monotone@slopeone\pgf@plot@monotone@rateleft
318+
% prepare for next step
319+
\global\let\pgf@plotstreampoint=\pgf@plot@monotone@dopointthree
320+
\global\pgf@plot@startedtrue%
321+
}%
322+
323+
\def\pgf@plot@monotone@dopointthree#1{%
324+
\pgf@process{#1}%
325+
\xdef\pgf@plot@monotone@pointthree{\noexpand\pgfqpoint{\the\pgf@x}{\the\pgf@y}}%
326+
% compute vector:
327+
\pgf@xa=\pgf@x%
328+
\pgf@ya=\pgf@y%
329+
\pgf@process{\pgf@plot@monotone@pointtwo}%
330+
\advance\pgf@xa by-\pgf@x
331+
\advance\pgf@ya by-\pgf@y
332+
% save right delta
333+
\xdef\pgf@plot@monotone@deltaright{\pgf@sys@tonumber\pgf@xa}%
334+
% compute slope
335+
\pgfmathdivide@{\the\pgf@ya}{\the\pgf@xa}%
336+
\global\let\pgf@plot@monotone@rateright\pgfmathresult
337+
% compute tangent slope
338+
\pgfmathadd@{\pgf@plot@monotone@rateleft}{\pgf@plot@monotone@rateright}%
339+
\pgf@xa=\pgfmathresult pt\relax
340+
\pgf@xa=0.5\pgf@xa
341+
% Fix slopes & draw curve
342+
\pgf@plot@monotonecurveto@fixdraw
343+
% Prepare next:
344+
\global\let\pgf@plot@monotone@slopeone\pgf@plot@monotone@slopetwo
345+
\global\let\pgf@plot@monotone@pointone\pgf@plot@monotone@pointtwo
346+
\global\let\pgf@plot@monotone@pointtwo\pgf@plot@monotone@pointthree
347+
\global\let\pgf@plot@monotone@deltaleft\pgf@plot@monotone@deltaright
348+
\global\let\pgf@plot@monotone@rateleft\pgf@plot@monotone@rateright
349+
}%
350+
351+
\def\pgf@plot@monotonecurveto@fixdraw{%
352+
% fix extremums
353+
% \pgf@xa is slope two
354+
\pgf@ya=\pgf@plot@monotone@rateleft pt\relax
355+
\pgf@yb=\pgf@plot@monotone@rateright\pgf@ya
356+
\ifdim\pgf@yb<0.0001pt\relax
357+
\pgf@xa=0pt\relax
358+
\else
359+
% fix motonicity
360+
\pgf@ya=3\pgf@ya
361+
\ifdim\pgf@xa>0pt\relax
362+
\ifdim\pgf@xa>\pgf@ya \pgf@xa=\pgf@ya\fi
363+
\else
364+
\ifdim\pgf@xa<\pgf@ya \pgf@xa=\pgf@ya\fi
365+
\fi
366+
\pgf@ya=\pgf@plot@monotone@rateright pt\relax
367+
\pgf@ya=3\pgf@ya
368+
\ifdim\pgf@xa>0pt\relax
369+
\ifdim\pgf@xa>\pgf@ya \pgf@xa=\pgf@ya\fi
370+
\else
371+
\ifdim\pgf@xa<\pgf@ya \pgf@xa=\pgf@ya\fi
372+
\fi
373+
\fi
374+
\xdef\pgf@plot@monotone@slopetwo{\pgf@sys@tonumber\pgf@xa}%
375+
% compute points for left bezier
376+
% (\pgf@x,\pgf@y) is point two
377+
\pgf@ya=\pgf@plot@monotone@deltaleft pt\relax
378+
\pgf@ya=0.333333\pgf@ya
379+
\advance \pgf@x by -\pgf@ya
380+
\advance \pgf@y by -\pgf@plot@monotone@slopetwo\pgf@ya
381+
\xdef\pgf@plot@monotone@supporttwo{\noexpand\pgfqpoint{\the\pgf@x}{\the\pgf@y}}%
382+
\pgf@process{\pgf@plot@monotone@pointone}%
383+
\advance \pgf@x by \pgf@ya
384+
\advance \pgf@y by \pgf@plot@monotone@slopeone\pgf@ya
385+
\xdef\pgf@plot@monotone@supportone{\noexpand\pgfqpoint{\the\pgf@x}{\the\pgf@y}}%
386+
% draw left curve:
387+
\pgfpathcurveto{\pgf@plot@monotone@supportone}{\pgf@plot@monotone@supporttwo}{\pgf@plot@monotone@pointtwo}%
388+
}%
389+
390+
\def\pgf@plot@monotone@doend{%
391+
\ifpgf@plot@started
392+
% fixdraw needs (\pgf@x,\pgf@y) as point two
393+
% and \pgf@xa as slope two
394+
\pgf@process{\pgf@plot@monotone@pointtwo}%
395+
\pgf@xa=\pgf@plot@monotone@rateleft pt\relax
396+
\pgf@plot@monotonecurveto@fixdraw
397+
\fi
398+
}%
399+
400+
272401

273402

274403

0 commit comments

Comments
 (0)