pgf-tikz / pgfplots

pgfplots - A TeX package to draw normal and/or logarithmic plots directly in TeX in two and three dimensions with a user-friendly interface and pgfplotstable - a TeX package to round and format numerical tables. Examples in manuals and/or on web site.
http://pgfplots.sourceforge.net/
196 stars 34 forks source link

[fillbetween] `sequence` does strange things #357

Open Mo-Gul opened 4 years ago

Mo-Gul commented 4 years ago

On TeX.SX this was first reported (https://tex.stackexchange.com/q/543553/95441).

Although the intersections are found properly, when using sequence in 3 out of 4 tests the result is odd, so I guess this is a bug. Please find below a modified MWE.

% used PGFPlots v1.17
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
    \usetikzlibrary{pgfplots.fillbetween}
    \tikzset{
        my sequence style/.style={
            red,
            opacity=0.5,
            thick,
            name path=spectrum,
            intersection segments={
                of= background and resonance,
                sequence={#1},
            },
        },
    }
\begin{document}
\begin{tikzpicture}
    \draw [name path=background]
        (1,2.5) .. controls +(30:3)  and +(160:1) ..(9,2)
    ;

    \draw [name path=resonance]
        (2.5,2)    .. controls +(90:6)  and +(92:4) .. (2.8,2)
        (4.1,1.15) .. controls +(90:6)  and +(92:4) .. (4.4,1.15)
        (5.7,0.7)  .. controls +(90:6)  and +(92:4) .. (6.0,0.7)
        (7.3,0.4)  .. controls +(90:6)  and +(92:4) .. (7.6,0.4)
    ;

%    % intersections are all found properly
%    \fill [
%        name intersections={
%            of=background and resonance,
%            name=intersections,
%            total=\t,
%        },
%        green,
%        opacity=0.5,
%    ] \foreach \s in {1,...,\t} {
%        (intersections-\s) circle (1.5pt)
%    };

%    \draw [my sequence style={R1 R3 R5 R7 R9}];     % <-- fine
    \draw [my sequence style={R2 R4 R6 R8}];        % <-- "overshooting"
%    \draw [my sequence style={L1 L3 L5 L7 L9}];     % <-- very strange
%    \draw [my sequence style={L2 L4 L6 L8}];        % <-- very strange
\end{tikzpicture}
\end{document}

P

hmenke commented 4 years ago

Are you using the latest PGF? Maybe related: https://github.com/pgf-tikz/pgf/issues/480

Mo-Gul commented 4 years ago

I am using GitHub versions of PGF and PGFPlots ... (But at least regarding the quoted TikZ issue that shouldn't make a difference, because there was the release of v3.1.5b after it was closed, right?)

marmotghost commented 3 years ago

I might be too naive, but I think you are expecting a bit too much. (It is not my intention to downplay the capabilities of the library!) You need to construct subpaths of Bezier curves. Exact solutions would require solutions of cubic equations. I am not claiming that I did a careful job but it appears to me that approximate solutions are found. Since your Bezier curves are extremely stretched, the errors might add to give the effect you are describing. (While it appears to be possible to, in principle, find analytic solutions I think that in practice this will be hard. The more so since a lot of knowledge that went into the libraries will have to be rediscovered by any potential author unless of course either Mark or Christian write the code. These are masterpieces of data handling which are very advanced and sophisticated, certainly beyond the capability of a marmot who might be able to compute the analytic solutions.)

Mo-Gul commented 3 years ago

@marmotghost, I am not 100% sure if I understood everything you wrote, so please forgive me the possibly stupid statement and question.

I also thought that maybe this is because the controls only consists of 4 points and thus leads to inaccuracies finding the intersection, but no, the intersections are all found properly. So I am wondering why can't the (properly found) intersection not (simply) be used? Shouldn't this be possible because the start and end points are known? So I would have guessed that the error is "between" these two points by guessing the help point(s).

If the previous sentences don't change your answer I'll be happy to accept it. You don't need to explain it (in more detail) again. The important thing is that you (the experts, e.g. Henri and you) are on the same page.

marmotghost commented 3 years ago

@Mo-Gul I am really just guessing. However, you need to perform two steps.

  1. Find the intersections.
  2. Construct new subpaths that follow the original paths between those intersections.

The first step consists of two substeps: 1a. Find the x and y coordinates of the intersections. This works great, as you say. 1b. Find the "time" (Gauss and the pgfmanual call the curve parameter "time") at which a path hits the intersection.

I think that 1b is tricky. Given the parametrization of the Bezier curve,

Screen Shot 2020-10-17 at 1 41 19 PM

you need to solve gamma(t_*)=(x_*,y_*) for the intersection points with coordinates (x_*,y_*). These are cubic equations. Once you have these solutions, you'd need in the second step to construct the subpath. (This is actually just a linear problem.) If you do not solve the equations analytically, then in the steps 1b and potentially also 2 deviations can occur that may be hard to control.

Mo-Gul commented 3 years ago

@marmotghost Many thanks for the insight.

marmotghost commented 3 years ago

@Mo-Gul I'd like to reiterate because the above comment may sound too negative. I am not at all negative. Rather, if you look at section 5.7.7 of the pgfplots manual v1.17, you can see that pgfplots already achieves what some want to achieve for TikZ/pgf, see this issue. I just think that it might be worthwhile to rewrite parts of it to incorporate a more analytic approach. (I think that the bbox library is a proof of principle that the analytic approach works, and if someone writes the analytic routines for the path manipulations one could hope that they choose an editor the maintainers like... ;-) And, of course, it would be good to make it possible to load fillbetween without loading pgfplots, which should be possible in principle but is also hard work, I think. What I want to say is that I think that there is a chance that an analytic approach works. However, carrying this out will most likely require collaborations because it may be that the analytic approach person is not capable to reproduce what Mark and Christian did when they set up the amazing scheme of data handling that underlies the intersections and fillbetween libraries.

ilayn commented 3 years ago

This is not the place to ask but kind of related to these issues; can anyone please just summarize the current status of the old maintainer status in terms of active/inactive? I remember somebody telling me Till was not around anymore for TeX stuff (maybe Christian in an email?).

I have seen already too many that things went sour between individuals in all kinds of open source libraries, hence please skip those things. I am just curious about who is around and I do not want to stir the soup. There is also the l3draw stuff that was happening some time ago IIRC from my days where I was still TeXing. Did they move there and/or contributed? Because they would definitely have valuable input.

marmotghost commented 3 years ago

@ilayn I would be willing to make a $100 donation to the marmot recovery foundation for the information what Don Knuth, Leslie Lamport, Heiko Oberdiek and Till Tantau really think about expl3.

ilayn commented 3 years ago

I don't think they care about TeX anymore. I meant TikZ maintainers Mark, Christian, if s/he is still around cjorssen and so on.

marmotghost commented 3 years ago

@ilayn If true, this would be a pity. I am using what they have created on a daily basis. For work and for recreation. It is remarkable that there are some who create things that last for decades and just give it to the community for free.

ilayn commented 3 years ago

That's the basic definition of open-source.

marmotghost commented 3 years ago

@ilayn Yes, sure. However, it is remarkable that there are people who have the vision and skills to create something that is still state of the art for millions of people decades after it has been created is remarkable. If I were to create an open-source code, it might be used by two people and will be outdated in a month.