Skip to content

Commit 1e130ea

Browse files
committed
Add PlotShadedClip
1 parent d875123 commit 1e130ea

File tree

2 files changed

+117
-0
lines changed

2 files changed

+117
-0
lines changed

implot.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -871,6 +871,10 @@ IMPLOT_TMP void PlotShaded(const char* label_id, const T* xs, const T* ys, int c
871871
IMPLOT_TMP void PlotShaded(const char* label_id, const T* xs, const T* ys1, const T* ys2, int count, ImPlotShadedFlags flags=0, int offset=0, int stride=sizeof(T));
872872
IMPLOT_API void PlotShadedG(const char* label_id, ImPlotGetter getter1, void* data1, ImPlotGetter getter2, void* data2, int count, ImPlotShadedFlags flags=0);
873873

874+
// Plots a shaded (filled) region between two lines, only if the second line is higher than the first one
875+
IMPLOT_TMP void PlotShadedClip(const char* label_id, const T* xs, const T* ys1, const T* ys2, int count, int offset=0, int stride=sizeof(T));
876+
IMPLOT_API void PlotShadedClipG(const char* label_id, ImPlotPoint (*getter1)(void* data, int idx), void* data1, ImPlotPoint (*getter2)(void* data, int idx), void* data2, int count, int offset=0);
877+
874878
// Plots a bar graph. Vertical by default. #bar_size and #shift are in plot units.
875879
IMPLOT_TMP void PlotBars(const char* label_id, const T* values, int count, double bar_size=0.67, double shift=0, ImPlotBarsFlags flags=0, int offset=0, int stride=sizeof(T));
876880
IMPLOT_TMP void PlotBars(const char* label_id, const T* xs, const T* ys, int count, double bar_size, ImPlotBarsFlags flags=0, int offset=0, int stride=sizeof(T));

implot_items.cpp

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1351,6 +1351,66 @@ struct RendererRectC : RendererBase {
13511351
mutable ImVec2 UV;
13521352
};
13531353

1354+
//WIP
1355+
template <typename TGetter1, typename TGetter2, typename TTransformer>
1356+
struct ShadedClipRenderer {
1357+
ShadedClipRenderer(const TGetter1& getter1, const TGetter2& getter2, const TTransformer& transformer, ImU32 col) :
1358+
Getter1(getter1),
1359+
Getter2(getter2),
1360+
Transformer(transformer),
1361+
Prims(ImMin(Getter1.Count, Getter2.Count) - 1),
1362+
Col(col)
1363+
{
1364+
P11 = Transformer(Getter1(0));
1365+
P12 = Transformer(Getter2(0));
1366+
}
1367+
1368+
inline bool operator()(ImDrawList& DrawList, const ImRect& /*cull_rect*/, const ImVec2& uv, int prim) const {
1369+
// TODO: Culling
1370+
ImVec2 P21 = Transformer(Getter1(prim+1));
1371+
ImVec2 P22 = Transformer(Getter2(prim+1));
1372+
const int intersect = (P11.y > P12.y && P22.y > P21.y) || (P12.y > P11.y && P21.y > P22.y);
1373+
ImVec2 intersection = Intersection(P11,P21,P12,P22);
1374+
DrawList._VtxWritePtr[0].pos = P11;
1375+
DrawList._VtxWritePtr[0].uv = uv;
1376+
DrawList._VtxWritePtr[0].col = Col;
1377+
DrawList._VtxWritePtr[1].pos = P21;
1378+
DrawList._VtxWritePtr[1].uv = uv;
1379+
DrawList._VtxWritePtr[1].col = Col;
1380+
DrawList._VtxWritePtr[2].pos = intersection;
1381+
DrawList._VtxWritePtr[2].uv = uv;
1382+
DrawList._VtxWritePtr[2].col = Col;
1383+
DrawList._VtxWritePtr[3].pos = P12;
1384+
DrawList._VtxWritePtr[3].uv = uv;
1385+
DrawList._VtxWritePtr[3].col = Col;
1386+
DrawList._VtxWritePtr[4].pos = P22;
1387+
DrawList._VtxWritePtr[4].uv = uv;
1388+
DrawList._VtxWritePtr[4].col = Col;
1389+
DrawList._VtxWritePtr += 5;
1390+
DrawList._IdxWritePtr[0] = (ImDrawIdx)(DrawList._VtxCurrentIdx);
1391+
DrawList._IdxWritePtr[1] = (ImDrawIdx)(DrawList._VtxCurrentIdx + 1 + intersect);
1392+
DrawList._IdxWritePtr[2] = (ImDrawIdx)(DrawList._VtxCurrentIdx + 0 + 3 * (P11.y >= P12.y) );
1393+
DrawList._IdxWritePtr[3] = (ImDrawIdx)(DrawList._VtxCurrentIdx + 1);
1394+
DrawList._IdxWritePtr[4] = (ImDrawIdx)(DrawList._VtxCurrentIdx + 3 - intersect);
1395+
DrawList._IdxWritePtr[5] = (ImDrawIdx)(DrawList._VtxCurrentIdx + 1 + 3 * (P21.y >= P22.y) );
1396+
DrawList._IdxWritePtr += 6;
1397+
DrawList._VtxCurrentIdx += 5;
1398+
P11 = P21;
1399+
P12 = P22;
1400+
return true;
1401+
}
1402+
const TGetter1& Getter1;
1403+
const TGetter2& Getter2;
1404+
const TTransformer& Transformer;
1405+
const int Prims;
1406+
const ImU32 Col;
1407+
mutable ImVec2 P11;
1408+
mutable ImVec2 P12;
1409+
static const int IdxConsumed = 6;
1410+
static const int VtxConsumed = 5;
1411+
};
1412+
1413+
13541414
//-----------------------------------------------------------------------------
13551415
// [SECTION] RenderPrimitives
13561416
//-----------------------------------------------------------------------------
@@ -1798,7 +1858,60 @@ void PlotShadedG(const char* label_id, ImPlotGetter getter_func1, void* data1, I
17981858
PlotShadedEx(label_id, getter1, getter2, flags);
17991859
}
18001860

1861+
// [SECTION] PlotShadedClip
18011862
//-----------------------------------------------------------------------------
1863+
1864+
template <typename Getter1, typename Getter2>
1865+
inline void PlotShadedClipEx(const char* label_id, const Getter1& getter1, const Getter2& getter2) {
1866+
if (BeginItem(label_id, ImPlotCol_Fill)) {
1867+
if (FitThisFrame()) {
1868+
for (int i = 0; i < ImMin(getter1.Count, getter2.Count); ++i) {
1869+
ImPlotPoint p1 = getter1(i);
1870+
ImPlotPoint p2 = getter2(i);
1871+
FitPoint(p1);
1872+
FitPoint(p2);
1873+
}
1874+
}
1875+
const ImPlotNextItemData& s = GetItemData();
1876+
ImDrawList & DrawList = *GetPlotDrawList();
1877+
if (s.RenderFill) {
1878+
ImU32 col = ImGui::GetColorU32(s.Colors[ImPlotCol_Fill]);
1879+
switch (GetCurrentScale()) {
1880+
case ImPlotScale_LinLin: RenderPrimitives(ShadedClipRenderer<Getter1,Getter2,TransformerLinLin>(getter1,getter2,TransformerLinLin(), col), DrawList, GImPlot->CurrentPlot->PlotRect); break;
1881+
case ImPlotScale_LogLin: RenderPrimitives(ShadedClipRenderer<Getter1,Getter2,TransformerLogLin>(getter1,getter2,TransformerLogLin(), col), DrawList, GImPlot->CurrentPlot->PlotRect); break;
1882+
case ImPlotScale_LinLog: RenderPrimitives(ShadedClipRenderer<Getter1,Getter2,TransformerLinLog>(getter1,getter2,TransformerLinLog(), col), DrawList, GImPlot->CurrentPlot->PlotRect); break;
1883+
case ImPlotScale_LogLog: RenderPrimitives(ShadedClipRenderer<Getter1,Getter2,TransformerLogLog>(getter1,getter2,TransformerLogLog(), col), DrawList, GImPlot->CurrentPlot->PlotRect); break;
1884+
}
1885+
}
1886+
EndItem();
1887+
}
1888+
}
1889+
1890+
template <typename T>
1891+
void PlotShadedClip(const char* label_id, const T* xs, const T* ys1, const T* ys2, int count, int offset, int stride) {
1892+
GetterXsYs<T> getter1(xs, ys1, count, offset, stride);
1893+
GetterXsYs<T> getter2(xs, ys2, count, offset, stride);
1894+
PlotShadedClipEx(label_id, getter1, getter2);
1895+
}
1896+
1897+
template IMPLOT_API void PlotShadedClip<ImS8>(const char* label_id, const ImS8* xs, const ImS8* ys1, const ImS8* ys2, int count, int offset, int stride);
1898+
template IMPLOT_API void PlotShadedClip<ImU8>(const char* label_id, const ImU8* xs, const ImU8* ys1, const ImU8* ys2, int count, int offset, int stride);
1899+
template IMPLOT_API void PlotShadedClip<ImS16>(const char* label_id, const ImS16* xs, const ImS16* ys1, const ImS16* ys2, int count, int offset, int stride);
1900+
template IMPLOT_API void PlotShadedClip<ImU16>(const char* label_id, const ImU16* xs, const ImU16* ys1, const ImU16* ys2, int count, int offset, int stride);
1901+
template IMPLOT_API void PlotShadedClip<ImS32>(const char* label_id, const ImS32* xs, const ImS32* ys1, const ImS32* ys2, int count, int offset, int stride);
1902+
template IMPLOT_API void PlotShadedClip<ImU32>(const char* label_id, const ImU32* xs, const ImU32* ys1, const ImU32* ys2, int count, int offset, int stride);
1903+
template IMPLOT_API void PlotShadedClip<ImS64>(const char* label_id, const ImS64* xs, const ImS64* ys1, const ImS64* ys2, int count, int offset, int stride);
1904+
template IMPLOT_API void PlotShadedClip<ImU64>(const char* label_id, const ImU64* xs, const ImU64* ys1, const ImU64* ys2, int count, int offset, int stride);
1905+
template IMPLOT_API void PlotShadedClip<float>(const char* label_id, const float* xs, const float* ys1, const float* ys2, int count, int offset, int stride);
1906+
template IMPLOT_API void PlotShadedClip<double>(const char* label_id, const double* xs, const double* ys1, const double* ys2, int count, int offset, int stride);
1907+
1908+
// custom
1909+
void PlotShadedClipG(const char* label_id, ImPlotPoint (*g1)(void* data, int idx), void* data1, ImPlotPoint (*g2)(void* data, int idx), void* data2, int count, int offset) {
1910+
GetterFuncPtr getter1(g1, data1, count, offset);
1911+
GetterFuncPtr getter2(g2, data2, count, offset);
1912+
PlotShadedClipEx(label_id, getter1, getter2);
1913+
}
1914+
18021915
// [SECTION] PlotBars
18031916
//-----------------------------------------------------------------------------
18041917

0 commit comments

Comments
 (0)