Skip to content

Commit cecaf07

Browse files
committed
Add a new monotonic 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 monotonic line` visualizer option. Signed-off-by: Julien '_FrnchFrgg_' RIVAUD <[email protected]>
1 parent d43fb10 commit cecaf07

File tree

4 files changed

+132
-0
lines changed

4 files changed

+132
-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

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 monotonic line/.style={@set={\pgfplothandlermonotonic}{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 monotonic}[]{\let\tikz@plot@handler=\pgfplothandlermonotonic}%
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 monotonic.
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{\pgfplothandlermonotonic}{}{%
284+
point macro=\pgf@plot@monotonic@dopointone,
285+
jump macro=\pgf@plot@monotonic@dojump,
286+
end macro=\pgf@plot@monotonic@doend
287+
}%
288+
289+
\def\pgf@plot@monotonic@dojump{%
290+
\pgf@plot@monotonic@doend
291+
\global\pgf@plot@startedfalse
292+
\global\let\pgf@plotstreampoint\pgf@plot@monotonic@dopointone
293+
}%
294+
295+
\def\pgf@plot@monotonic@dopointone#1{%
296+
\pgf@process{#1}%
297+
\pgf@plot@first@action{\pgfqpoint{\pgf@x}{\pgf@y}}%
298+
\xdef\pgf@plot@monotonic@pointone{\noexpand\pgfqpoint{\the\pgf@x}{\the\pgf@y}}%
299+
\global\let\pgf@plotstreampoint=\pgf@plot@monotonic@dopointtwo
300+
}%
301+
302+
\def\pgf@plot@monotonic@dopointtwo#1{%
303+
\pgf@process{#1}%
304+
\xdef\pgf@plot@monotonic@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@monotonic@pointone}%
309+
\advance\pgf@xa by-\pgf@x
310+
\advance\pgf@ya by-\pgf@y
311+
% save left delta
312+
\xdef\pgf@plot@monotonic@deltaleft{\pgf@sys@tonumber\pgf@xa}%
313+
% compute slope
314+
\pgfmathdivide@{\the\pgf@ya}{\the\pgf@xa}%
315+
\global\let\pgf@plot@monotonic@rateleft\pgfmathresult
316+
% init tangent one
317+
\global\let\pgf@plot@monotonic@slopeone\pgf@plot@monotonic@rateleft
318+
% prepare for next step
319+
\global\let\pgf@plotstreampoint=\pgf@plot@monotonic@dopointthree
320+
\global\pgf@plot@startedtrue%
321+
}%
322+
323+
\def\pgf@plot@monotonic@dopointthree#1{%
324+
\pgf@process{#1}%
325+
\xdef\pgf@plot@monotonic@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@monotonic@pointtwo}%
330+
\advance\pgf@xa by-\pgf@x
331+
\advance\pgf@ya by-\pgf@y
332+
% save right delta
333+
\xdef\pgf@plot@monotonic@deltaright{\pgf@sys@tonumber\pgf@xa}%
334+
% compute slope
335+
\pgfmathdivide@{\the\pgf@ya}{\the\pgf@xa}%
336+
\global\let\pgf@plot@monotonic@rateright\pgfmathresult
337+
% compute tangent slope
338+
\pgfmathadd@{\pgf@plot@monotonic@rateleft}{\pgf@plot@monotonic@rateright}%
339+
\pgf@xa=\pgfmathresult pt\relax
340+
\pgf@xa=0.5\pgf@xa
341+
% Fix slopes & draw curve
342+
\pgf@plot@monotoniccurveto@fixdraw
343+
% Prepare next:
344+
\global\let\pgf@plot@monotonic@slopeone\pgf@plot@monotonic@slopetwo
345+
\global\let\pgf@plot@monotonic@pointone\pgf@plot@monotonic@pointtwo
346+
\global\let\pgf@plot@monotonic@pointtwo\pgf@plot@monotonic@pointthree
347+
\global\let\pgf@plot@monotonic@deltaleft\pgf@plot@monotonic@deltaright
348+
\global\let\pgf@plot@monotonic@rateleft\pgf@plot@monotonic@rateright
349+
}%
350+
351+
\def\pgf@plot@monotoniccurveto@fixdraw{%
352+
% fix extremums
353+
% \pgf@xa is slope two
354+
\pgf@ya=\pgf@plot@monotonic@rateleft pt\relax
355+
\pgf@yb=\pgf@plot@monotonic@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@monotonic@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@monotonic@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@monotonic@deltaleft pt\relax
378+
\pgf@ya=0.333333\pgf@ya
379+
\advance \pgf@x by -\pgf@ya
380+
\advance \pgf@y by -\pgf@plot@monotonic@slopetwo\pgf@ya
381+
\xdef\pgf@plot@monotonic@supporttwo{\noexpand\pgfqpoint{\the\pgf@x}{\the\pgf@y}}%
382+
\pgf@process{\pgf@plot@monotonic@pointone}%
383+
\advance \pgf@x by \pgf@ya
384+
\advance \pgf@y by \pgf@plot@monotonic@slopeone\pgf@ya
385+
\xdef\pgf@plot@monotonic@supportone{\noexpand\pgfqpoint{\the\pgf@x}{\the\pgf@y}}%
386+
% draw left curve:
387+
\pgfpathcurveto{\pgf@plot@monotonic@supportone}{\pgf@plot@monotonic@supporttwo}{\pgf@plot@monotonic@pointtwo}%
388+
}%
389+
390+
\def\pgf@plot@monotonic@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@monotonic@pointtwo}%
395+
\pgf@xa=\pgf@plot@monotonic@rateleft pt\relax
396+
\pgf@plot@monotoniccurveto@fixdraw
397+
\fi
398+
}%
399+
400+
272401

273402

274403

0 commit comments

Comments
 (0)