ImGuiNET / ImGui.NET

An ImGui wrapper for .NET.
MIT License
1.85k stars 300 forks source link

PlotHistogram: Unexpected Values. #56

Closed se5a closed 6 years ago

se5a commented 6 years ago

I'm getting strange values for the histogram bars, with a really simple example:

float[] _testArray = new float[11] { 0, 0.1f, 0.2f, 0.3f, 0.4f, 0.5f, 0.6f, 0.7f, 0.8f, 0.9f, 1.0f };
ImGui.PlotHistogram("Stride1", _testArray, _testArray.Length, "", 0.0f, 1.0f, new System.Numerics.Vector2(160, 40), 1);
ImGui.PlotHistogram("Stride2", _testArray, _testArray.Length, "", 0.0f, 1.0f, new System.Numerics.Vector2(160, 40), 2);

plothistogram

The values where the bars are not showing (ie from tooltip text) are really strange values like: -1.342e+08 even the third visable bar on the top histogram in the screenshot has a strange value of 1.074e+08 as you can see from the _testArray these values are not in the array at all. in the stride2 example each of the odd numbered bars are the correct values of: 0, 0.1, 0.2, 0.3, 0.4, 0.5 every other value is negative.

mellinoe commented 6 years ago

The final parameter is actually a stride in bytes, rather than elements. So if you change 1 to 1 * sizeof(float) and 2 to 2 * sizeof(float), then you will get more reasonable results.

Additionally, the third parameter is actually supposed to be an "offset" for the data points. It turns out that the native code will wrap around if it reaches the end of the data (actually, it will trigger an assertion, but those are disabled in the version of cimgui being used). Since you're using the length of the array, it's just wrapping back around to the beginning.

Aside from that, there is actually a genuine issue with the current wrapper method, because there's no way to specify the number of elements to plot yourself -- the full array size is always used. There should probably be an overload where you can pass in the length yourself, in addition to the "start index" (that's effectively the third parameter in the current version). If you wanted to use a stride of 2-floats, then you'd want to only draw half as many elements, otherwise it'll end up reading values past the end of the array.

@se5a Let me know if that makes sense.

se5a commented 6 years ago

size in bytes... ok that.. that would make more sense. yeah I think I understand the offset, it's just most examples I saw appeared to use the length of the array for some odd reason. off to try this size in bytes now!

mellinoe commented 6 years ago

@se5a I pushed a commit with a new PlotHistogram overload:

void PlotHistogram(
    string label,
    float[] values,
    int startIndex,
    int count,
    string overlayText = null,
    float scaleMin = float.MaxValue,
    float scaleMax = float.MaxValue,
    Vector2 graphSize = default(Vector2),
    int elementStride = 1)

This one should be easier to understand, and has default parameter values that match the native API.

se5a commented 6 years ago

My histograms are now with 100% less giberish. thanks!

cool, that should help future confused people. /// xml comments could go a long way to helping make things less confusing too, but I guess that'd be a lot of work to go through and do everything.