Skip to content

Commit b6028ec

Browse files
committed
modernize
1 parent 6eec2b7 commit b6028ec

File tree

2 files changed

+84
-91
lines changed

2 files changed

+84
-91
lines changed

implot.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -872,8 +872,10 @@ IMPLOT_TMP void PlotShaded(const char* label_id, const T* xs, const T* ys1, cons
872872
IMPLOT_API void PlotShadedG(const char* label_id, ImPlotGetter getter1, void* data1, ImPlotGetter getter2, void* data2, int count, ImPlotShadedFlags flags=0);
873873

874874
// 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);
875+
IMPLOT_TMP void PlotShadedClip(const char* label_id, const T* values, int count, double yref=0, double xscale=1, double xstart=0, ImPlotShadedFlags flags=0, int offset=0, int stride=sizeof(T));
876+
IMPLOT_TMP void PlotShadedClip(const char* label_id, const T* xs, const T* ys, int count, double yref=0, ImPlotShadedFlags flags=0, int offset=0, int stride=sizeof(T));
877+
IMPLOT_TMP void PlotShadedClip(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));
878+
IMPLOT_API void PlotShadedClipG(const char* label_id, ImPlotGetter getter1, void* data1, ImPlotGetter getter2, void* data2, int count, ImPlotShadedFlags flags=0);
877879

878880
// Plots a bar graph. Vertical by default. #bar_size and #shift are in plot units.
879881
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));

implot_items.cpp

Lines changed: 80 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -1351,65 +1351,49 @@ struct RendererRectC : RendererBase {
13511351
mutable ImVec2 UV;
13521352
};
13531353

1354-
template <typename TGetter1, typename TGetter2, typename TTransformer>
1355-
struct ShadedClipRenderer {
1356-
ShadedClipRenderer(const TGetter1& getter1, const TGetter2& getter2, const TTransformer& transformer, ImU32 col) :
1357-
Getter1(getter1),
1358-
Getter2(getter2),
1359-
Transformer(transformer),
1360-
Prims(ImMin(Getter1.Count, Getter2.Count) - 1),
1361-
Col(col)
1362-
{
1363-
P11 = Transformer(Getter1(0));
1364-
P12 = Transformer(Getter2(0));
1365-
}
1366-
1367-
inline bool operator()(ImDrawList& DrawList, const ImRect& /*cull_rect*/, const ImVec2& uv, int prim) const {
1368-
// TODO: Culling
1369-
ImVec2 P21 = Transformer(Getter1(prim+1));
1370-
ImVec2 P22 = Transformer(Getter2(prim+1));
1354+
template <typename _Getter>
1355+
struct RendererShadedClip : RendererShaded {
1356+
IMPLOT_INLINE bool Render(ImDrawList& draw_list, const ImRect& cull_rect, int prim) const {
1357+
ImVec2 P21 = this->Transformer(Getter1(prim+1));
1358+
ImVec2 P22 = this->Transformer(Getter2(prim+1));
1359+
ImRect rect(ImMin(ImMin(ImMin(P11,P12),P21),P22), ImMax(ImMax(ImMax(P11,P12),P21),P22));
1360+
if (!cull_rect.Overlaps(rect)) {
1361+
P11 = P21;
1362+
P12 = P22;
1363+
return false;
1364+
}
13711365
const int intersect = (P11.y > P12.y && P22.y > P21.y) || (P12.y > P11.y && P21.y > P22.y);
13721366
ImVec2 intersection = Intersection(P11,P21,P12,P22);
1373-
DrawList._VtxWritePtr[0].pos = P11;
1374-
DrawList._VtxWritePtr[0].uv = uv;
1375-
DrawList._VtxWritePtr[0].col = Col;
1376-
DrawList._VtxWritePtr[1].pos = P21;
1377-
DrawList._VtxWritePtr[1].uv = uv;
1378-
DrawList._VtxWritePtr[1].col = Col;
1379-
DrawList._VtxWritePtr[2].pos = intersection;
1380-
DrawList._VtxWritePtr[2].uv = uv;
1381-
DrawList._VtxWritePtr[2].col = Col;
1382-
DrawList._VtxWritePtr[3].pos = P12;
1383-
DrawList._VtxWritePtr[3].uv = uv;
1384-
DrawList._VtxWritePtr[3].col = Col;
1385-
DrawList._VtxWritePtr[4].pos = P22;
1386-
DrawList._VtxWritePtr[4].uv = uv;
1387-
DrawList._VtxWritePtr[4].col = Col;
1388-
DrawList._VtxWritePtr += 5;
1389-
DrawList._IdxWritePtr[0] = (ImDrawIdx)(DrawList._VtxCurrentIdx);
1390-
DrawList._IdxWritePtr[1] = (ImDrawIdx)(DrawList._VtxCurrentIdx + 1 + intersect);
1391-
DrawList._IdxWritePtr[2] = (ImDrawIdx)(DrawList._VtxCurrentIdx + 0 + 3 * (P11.y >= P12.y) );
1392-
DrawList._IdxWritePtr[3] = (ImDrawIdx)(DrawList._VtxCurrentIdx + 1);
1393-
DrawList._IdxWritePtr[4] = (ImDrawIdx)(DrawList._VtxCurrentIdx + 3 - intersect);
1394-
DrawList._IdxWritePtr[5] = (ImDrawIdx)(DrawList._VtxCurrentIdx + 1 + 3 * (P21.y >= P22.y) );
1395-
DrawList._IdxWritePtr += 6;
1396-
DrawList._VtxCurrentIdx += 5;
1367+
draw_list._VtxWritePtr[0].pos = P11;
1368+
draw_list._VtxWritePtr[0].uv = UV;
1369+
draw_list._VtxWritePtr[0].col = Col;
1370+
draw_list._VtxWritePtr[1].pos = P21;
1371+
draw_list._VtxWritePtr[1].uv = UV;
1372+
draw_list._VtxWritePtr[1].col = Col;
1373+
draw_list._VtxWritePtr[2].pos = intersection;
1374+
draw_list._VtxWritePtr[2].uv = UV;
1375+
draw_list._VtxWritePtr[2].col = Col;
1376+
draw_list._VtxWritePtr[3].pos = P12;
1377+
draw_list._VtxWritePtr[3].uv = UV;
1378+
draw_list._VtxWritePtr[3].col = Col;
1379+
draw_list._VtxWritePtr[4].pos = P22;
1380+
draw_list._VtxWritePtr[4].uv = UV;
1381+
draw_list._VtxWritePtr[4].col = Col;
1382+
draw_list._VtxWritePtr += 5;
1383+
draw_list._IdxWritePtr[0] = (ImDrawIdx)(draw_list._VtxCurrentIdx);
1384+
draw_list._IdxWritePtr[1] = (ImDrawIdx)(draw_list._VtxCurrentIdx + 1 + intersect);
1385+
draw_list._IdxWritePtr[2] = (ImDrawIdx)(draw_list._VtxCurrentIdx + 0 + 3 * (P11.y >= P12.y) );
1386+
draw_list._IdxWritePtr[3] = (ImDrawIdx)(draw_list._VtxCurrentIdx + 1);
1387+
draw_list._IdxWritePtr[4] = (ImDrawIdx)(draw_list._VtxCurrentIdx + 3 - intersect);
1388+
draw_list._IdxWritePtr[5] = (ImDrawIdx)(draw_list._VtxCurrentIdx + 1 + 3 * (P21.y >= P22.y) );
1389+
draw_list._IdxWritePtr += 6;
1390+
draw_list._VtxCurrentIdx += 5;
13971391
P11 = P21;
13981392
P12 = P22;
13991393
return true;
14001394
}
1401-
const TGetter1& Getter1;
1402-
const TGetter2& Getter2;
1403-
const TTransformer& Transformer;
1404-
const int Prims;
1405-
const ImU32 Col;
1406-
mutable ImVec2 P11;
1407-
mutable ImVec2 P12;
1408-
static const int IdxConsumed = 6;
1409-
static const int VtxConsumed = 5;
14101395
};
14111396

1412-
14131397
//-----------------------------------------------------------------------------
14141398
// [SECTION] RenderPrimitives
14151399
//-----------------------------------------------------------------------------
@@ -1850,6 +1834,7 @@ void PlotShaded(const char* label_id, const T* xs, const T* ys1, const T* ys2, i
18501834
CALL_INSTANTIATE_FOR_NUMERIC_TYPES()
18511835
#undef INSTANTIATE_MACRO
18521836

1837+
// custom
18531838
void PlotShadedG(const char* label_id, ImPlotGetter getter_func1, void* data1, ImPlotGetter getter_func2, void* data2, int count, ImPlotShadedFlags flags) {
18541839
GetterFuncPtr getter1(getter_func1, data1, count);
18551840
GetterFuncPtr getter2(getter_func2, data2, count);
@@ -1860,55 +1845,61 @@ void PlotShadedG(const char* label_id, ImPlotGetter getter_func1, void* data1, I
18601845
// [SECTION] PlotShadedClip
18611846
//-----------------------------------------------------------------------------
18621847

1848+
// NEW
18631849
template <typename Getter1, typename Getter2>
1864-
inline void PlotShadedClipEx(const char* label_id, const Getter1& getter1, const Getter2& getter2) {
1865-
if (BeginItem(label_id, ImPlotCol_Fill)) {
1866-
if (FitThisFrame()) {
1867-
for (int i = 0; i < ImMin(getter1.Count, getter2.Count); ++i) {
1868-
ImPlotPoint p1 = getter1(i);
1869-
ImPlotPoint p2 = getter2(i);
1870-
FitPoint(p1);
1871-
FitPoint(p2);
1872-
}
1873-
}
1850+
void PlotShadedClipEx(const char* label_id, const Getter1& getter1, const Getter2& getter2, ImPlotShadedFlags flags) {
1851+
if (BeginItemEx(label_id, Fitter2<Getter1,Getter2>(getter1,getter2), flags, ImPlotCol_Fill)) {
18741852
const ImPlotNextItemData& s = GetItemData();
1875-
ImDrawList & DrawList = *GetPlotDrawList();
18761853
if (s.RenderFill) {
1877-
ImU32 col = ImGui::GetColorU32(s.Colors[ImPlotCol_Fill]);
1878-
switch (GetCurrentScale()) {
1879-
case ImPlotScale_LinLin: RenderPrimitives(ShadedClipRenderer<Getter1,Getter2,TransformerLinLin>(getter1,getter2,TransformerLinLin(), col), DrawList, GImPlot->CurrentPlot->PlotRect); break;
1880-
case ImPlotScale_LogLin: RenderPrimitives(ShadedClipRenderer<Getter1,Getter2,TransformerLogLin>(getter1,getter2,TransformerLogLin(), col), DrawList, GImPlot->CurrentPlot->PlotRect); break;
1881-
case ImPlotScale_LinLog: RenderPrimitives(ShadedClipRenderer<Getter1,Getter2,TransformerLinLog>(getter1,getter2,TransformerLinLog(), col), DrawList, GImPlot->CurrentPlot->PlotRect); break;
1882-
case ImPlotScale_LogLog: RenderPrimitives(ShadedClipRenderer<Getter1,Getter2,TransformerLogLog>(getter1,getter2,TransformerLogLog(), col), DrawList, GImPlot->CurrentPlot->PlotRect); break;
1883-
}
1854+
const ImU32 col = ImGui::GetColorU32(s.Colors[ImPlotCol_Fill]);
1855+
RenderPrimitives2<RendererShadedClip>(getter1,getter2,col);
18841856
}
18851857
EndItem();
18861858
}
18871859
}
18881860

18891861
template <typename T>
1890-
void PlotShadedClip(const char* label_id, const T* xs, const T* ys1, const T* ys2, int count, int offset, int stride) {
1891-
GetterXsYs<T> getter1(xs, ys1, count, offset, stride);
1892-
GetterXsYs<T> getter2(xs, ys2, count, offset, stride);
1893-
PlotShadedClipEx(label_id, getter1, getter2);
1894-
}
1895-
1896-
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);
1897-
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);
1898-
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);
1899-
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);
1900-
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);
1901-
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);
1902-
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);
1903-
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);
1904-
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);
1905-
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);
1862+
void PlotShadedClip(const char* label_id, const T* values, int count, double y_ref, double xscale, double x0, ImPlotShadedFlags flags, int offset, int stride) {
1863+
if (!(y_ref > -DBL_MAX))
1864+
y_ref = GetPlotLimits(IMPLOT_AUTO,IMPLOT_AUTO).Y.Min;
1865+
if (!(y_ref < DBL_MAX))
1866+
y_ref = GetPlotLimits(IMPLOT_AUTO,IMPLOT_AUTO).Y.Max;
1867+
GetterXY<IndexerLin,IndexerIdx<T>> getter1(IndexerLin(xscale,x0),IndexerIdx<T>(values,count,offset,stride),count);
1868+
GetterXY<IndexerLin,IndexerConst> getter2(IndexerLin(xscale,x0),IndexerConst(y_ref),count);
1869+
PlotShadedClipEx(label_id, getter1, getter2, flags);
1870+
}
1871+
1872+
template <typename T>
1873+
void PlotShadedClip(const char* label_id, const T* xs, const T* ys, int count, double y_ref, ImPlotShadedFlags flags, int offset, int stride) {
1874+
if (y_ref == -HUGE_VAL)
1875+
y_ref = GetPlotLimits(IMPLOT_AUTO,IMPLOT_AUTO).Y.Min;
1876+
if (y_ref == HUGE_VAL)
1877+
y_ref = GetPlotLimits(IMPLOT_AUTO,IMPLOT_AUTO).Y.Max;
1878+
GetterXY<IndexerIdx<T>,IndexerIdx<T>> getter1(IndexerIdx<T>(xs,count,offset,stride),IndexerIdx<T>(ys,count,offset,stride),count);
1879+
GetterXY<IndexerIdx<T>,IndexerConst> getter2(IndexerIdx<T>(xs,count,offset,stride),IndexerConst(y_ref),count);
1880+
PlotShadedClipEx(label_id, getter1, getter2, flags);
1881+
}
1882+
1883+
1884+
template <typename T>
1885+
void PlotShadedClip(const char* label_id, const T* xs, const T* ys1, const T* ys2, int count, ImPlotShadedFlags flags, int offset, int stride) {
1886+
GetterXY<IndexerIdx<T>,IndexerIdx<T>> getter1(IndexerIdx<T>(xs,count,offset,stride),IndexerIdx<T>(ys1,count,offset,stride),count);
1887+
GetterXY<IndexerIdx<T>,IndexerIdx<T>> getter2(IndexerIdx<T>(xs,count,offset,stride),IndexerIdx<T>(ys2,count,offset,stride),count);
1888+
PlotShadedClipEx(label_id, getter1, getter2, flags);
1889+
}
1890+
1891+
#define INSTANTIATE_MACRO(T) \
1892+
template IMPLOT_API void PlotShadedClip<T>(const char* label_id, const T* values, int count, double y_ref, double xscale, double x0, ImPlotShadedFlags flags, int offset, int stride); \
1893+
template IMPLOT_API void PlotShadedClip<T>(const char* label_id, const T* xs, const T* ys, int count, double y_ref, ImPlotShadedFlags flags, int offset, int stride); \
1894+
template IMPLOT_API void PlotShadedClip<T>(const char* label_id, const T* xs, const T* ys1, const T* ys2, int count, ImPlotShadedFlags flags, int offset, int stride);
1895+
CALL_INSTANTIATE_FOR_NUMERIC_TYPES()
1896+
#undef INSTANTIATE_MACRO
19061897

19071898
// custom
1908-
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) {
1909-
GetterFuncPtr getter1(g1, data1, count, offset);
1910-
GetterFuncPtr getter2(g2, data2, count, offset);
1911-
PlotShadedClipEx(label_id, getter1, getter2);
1899+
void PlotShadedClipG(const char* label_id, ImPlotGetter getter_func1, void* data1, ImPlotGetter getter_func2, void* data2, int count, ImPlotShadedFlags flags) {
1900+
GetterFuncPtr getter1(getter_func1, data1, count);
1901+
GetterFuncPtr getter2(getter_func2, data2, count);
1902+
PlotShadedClipEx(label_id, getter1, getter2, flags);
19121903
}
19131904

19141905
//-----------------------------------------------------------------------------

0 commit comments

Comments
 (0)