epezent / implot

Immediate Mode Plotting
MIT License
4.79k stars 531 forks source link

Option to skip nan values in PlotLine #276

Closed kv-gits closed 2 years ago

kv-gits commented 3 years ago

Implemented this by adding new Item "LineTo" with simple check for nan, but may be flag for common PlotLine would be more useful?

Also used "cmath" header file. Not sure is it propper for implot architecture.

template <typename Getter, typename Transformer>
inline void RenderLineToStrip(const Getter &getter, const Transformer &transformer, ImDrawList &DrawList, float line_weight, ImU32 col) {
    ImPlotContext &gp = *GImPlot;

    if (ImHasFlag(gp.CurrentPlot->Flags, ImPlotFlags_AntiAliased) || gp.Style.AntiAliasedLines) {
        ImVec2 p1 = transformer(getter(0));

        for (int i = 1; i < getter.Count; ++i) {
            ImVec2 p2 = transformer(getter(i));

            if (gp.CurrentPlot->PlotRect.Overlaps(ImRect(ImMin(p1, p2), ImMax(p1, p2))))
                DrawList.AddLine(p1, p2, col, line_weight);

            if (std::isnan(p2.y) == false) p1 = p2;
        }
    } else {
        RenderPrimitives(LineToStripRenderer<Getter, Transformer>(getter, transformer, col, line_weight), DrawList, gp.CurrentPlot->PlotRect);
    }
}
epezent commented 3 years ago

I suspect this check has some non-trivial overhead, and we would want users to be able to opt in to it only if they need it. At some point in the near future, I want to add ImPlotLineFlags, etc. and so we could enable this with e.g. ImPlotLineFlags_SkipNan. It will be a while before I get to this, though.

epezent commented 2 years ago

This is now possible with ImPlotLineFlags_SkipNaN. In the example below, there is a y=NaN at x=0.5. The checkbox enables ImPlotLineFlags_SkipNaN

image

image