How to add a Colorbar for Custom Objects in ILNumerics?

£可爱£侵袭症+ 提交于 2019-12-11 09:41:28

问题


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
            }
        }); 
}



来源:https://stackoverflow.com/questions/24545293/how-to-add-a-colorbar-for-custom-objects-in-ilnumerics

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!