How to add a Colorbar for Custom Objects in ILNume

2019-08-12 04:46发布

问题:

I want to use a colorbar with custom objects. The objects are colored according to a specific colormap. I want to show this colormap in a colorbar at runtime.

I already tried adding it to scene by:

        ILColorbar cb = new ILColorbar();
        scene.Add(cb);

or to the cube:

        plotCube.Add(cb);

or even plotCube.Children.Add(cb);

but it still doesn't work. What is the correct way to display a colorbar for custom objects?

Here is my code:

private void OKInputBodyListButton_Click(object sender, EventArgs e)
    {
        try
        {
            var sceneBody = new ILScene();
            var plotCubeBody = sceneBody.Add(new ILPlotCube(twoDMode: false));

            foreach (BlockBody item in ObjectList)
            {
                createBlockBody(item, sceneBody, plotCubeBody);
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

private void createBlockBody(BlockBody BlockBody, ILScene scene, ILPlotCube plotCube)
    {
        var box = new ILTriangles("tri")
        {
            ...
            ...
        }

        plotCube.Add(box);
        var colormap = new ILColormap(Colormaps.Jet);
        Vector4 key1 = colormap.Map((float)BlockBody.Rho, new Tuple<float, float>(-1, 1));
        var test = key1.ToColor();
        box.Color = test;

        SliceilPanel.Scene = scene;
        SliceilPanel.Refresh();
    }

And this are my figures:

回答1:

The colorbar needs some data: the colormap used and the upper/lower limits for the colorbar axis. Normally, it takes those information from the corresponding plot object (contour plot, surface plot) in the scene at runtime. 'At runtime' because it must be able to react to changes to these plot objects due to interactivity.

If you want to use colorbars for your custom objects you must provide those data yourself. ILColorbar provides the ColormapProvider property. Assign an object to it which provides the information at runtime. You can take the predefined ILNumerics.Drawing.Plotting.ILStaticColormapProvider or provide your own implementation of the 'IILColormapProvider' interface.

This is not working with the Community Edition from nuget!

private void ilPanel1_Load(object sender, EventArgs e) {

    // We need to provide colormap data to the colorbar at runtime. 
    // We simply take a static colormap provider here:
    var cmProv = new ILStaticColormapProvider(Colormaps.ILNumerics, 0f, 1f);
    // Position data for plotting
    ILArray<float> A = ILMath.tosingle(ILMath.rand(3, 1000));
    // create the points
    ilPanel1.Scene.Add(
        new ILPlotCube(twoDMode: false) {
            new ILPoints("myPoints") {
                Positions = A,
                // since we want to show a colorbar, we need to put the points colors under colormap control
                Colors = cmProv.Colormap.Map(A["1;:"]).T,
                // deactivate single color rendering
                Color = null
            },
            // add the colorbar (somewhere) and give it the colormap provider
            new ILColorbar() {
                ColormapProvider = cmProv
            }
        }); 
}