具有第四维标记特殊值的3D表面着色

时间:2013-05-31 17:10:38

标签: c# 3d ilnumerics geometry-surface

我想创建一个3D表面图,如图所示。我正在使用ILNumerics,recent RC from the website。到目前为止,我可以使用以下代码创建表面(数据仅用于测试):

using System;
using System.Drawing;
using System.Windows.Forms;
using ILNumerics; 
using ILNumerics.Drawing; 
using ILNumerics.Drawing.Plotting; 

namespace WindowsFormsApplication1 {
    public partial class Form1 : Form {
        public Form1() {
            InitializeComponent();
        }

    private void ilPanel1_Load(object sender, EventArgs e) {
        using (ILScope.Enter()) {
            ILArray<float> R = ILMath.linspace<float>(-4, 4, 100);
            ILArray<float> y = 1;
            ILArray<float> x = ILMath.meshgrid(R, R, y);

            ILArray<float> Z = ILMath.zeros<float>(x.S[0], x.S[1], 3); 
            Z[":;:;1"] = x; Z[":;:;2"] = y; 
            Z[":;:;0"] = 0.4f * x * x - 0.2f * y * y * y; 

            ilPanel1.Scene.Add(new ILPlotCube(twoDMode: false) {
                new ILSurface(Z, colormap: Colormaps.Cool) {
                    Colors = 1.4f * x * x * x + 0.13f * y * y,
                    Childs = { new ILColorbar() }
                }
            }); 
        }
    }
    }
}

surface graph without marked values

在不同颜色的数据(颜色)值为0的情况下标记所有构面的最佳方法是什么?可能使用colorbar会是要走的路?但这究竟是怎么做到的?

1 个答案:

答案 0 :(得分:3)

这可以使用自定义色彩映射来实现。但是必须要小心,要做到可靠。简单地在色彩映射的中间放置一个新的红色关键点会产生非常不正确的结果...:|

我稍微修改并记录了你的例子。基本上,colormaps包含具有位置和颜色的关键点。通常(但不一定)的位置范围为[0..1]。颜色始终在[0..1]范围内。这里的挑战是在色彩映射范围内找到'0'cdata值的正确位置。

首先,创建默认的Cool colormap。它有以下两个关键点(在行中):

colorkeys
<Single> [2,5]
    [0]: 0,00000 0,00000 1,00000 1,00000 1,00000 <- position: 0, color: RGBA
    [1]: 1,00000 1,00000 0,00000 1,00000 1,00000 <- position: 1

您必须添加至少3个新关键点:前两个关键点在标记区域的边缘处采样原始颜色。第三个使标记区域为红色。简单地添加红色关键点是行不通的,因为这会影响地图中的所有值,而不仅仅是0左右的数据值。

最后,关键点如下所示:

colorkeys
<Single> [5,5]
     [0]: 0,00000 0,00000 1,00000 1,00000 1,00000 
     [1]: 1,00000 1,00000 0,00000 1,00000 1,00000 
     [2]: 0,49150 0,49150 0,50850 1,00000 1,00000 
     [3]: 0,49702 0,49702 0,50298 1,00000 1,00000 
     [4]: 0,49426 1,00000 0,00000 0,00000 1,00000 

请注意,关键点的顺序并不重要。在创建新的色彩映射时,它们仍然会进行排序。新地图仅提供给ILSurface:

private void ilPanel1_Load(object sender, EventArgs e) {

    // create some X/Y meshgrid data 
    ILArray<float> y = 1, R = ILMath.linspace<float>(-4, 4, 100);
    ILArray<float> x = ILMath.meshgrid(R, ILMath.linspace<float>(-4, 4, 100), y);

    // precreate the surface data array
    ILArray<float> Z = ILMath.zeros<float>(x.S[0], x.S[1], 3);
    // surface expects Z, X and Y coords (in that order)
    Z[":;:;2"] = y; Z[":;:;1"] = x;
    Z[":;:;0"] = 0.4f * x * x - 0.2f * y * y * y;

    // our color data are based on another function 
    ILArray<float> cdata = 1.4f * x * x * x + 0.13f * y * y;

    // we need cdatas limits for creating a new colormap
    float min, max; cdata.GetLimits(out min, out max);

    // get default 'Cool' colormap
    var colormap = new ILColormap(Colormaps.Cool);
    // get colormap keys as array: [position,R,G,B,A]
    ILArray<float> colorkeys = colormap.Data;
    // helper function to map true values to 0..1 range 
    Func<float, float> map = a => { return (a - min) / (max - min); };

    // the value to mark and +/- tolerance width (to make it a visible strip at least)
    float markValue = 0, tolerance = 0.5f;
    // sample the colormap at the marked edges
    Vector4 key1 = colormap.Map(markValue - tolerance, new Tuple<float, float>(min, max));
    Vector4 key2 = colormap.Map(markValue + tolerance, new Tuple<float, float>(min, max));
    // create new keypoints at the edges of marked area
    colorkeys[ILMath.end + 1, ":"] = ILMath.array(map(markValue - tolerance), key1.X, key1.Y, key1.Z, 1f); 
    colorkeys[ILMath.end + 1, ":"] = ILMath.array(map(markValue + tolerance), key2.X, key2.Y, key2.Z, 1f); 
    // create new keypoint for the marked area itself; color red
    colorkeys[ILMath.end + 1, ":"] = ILMath.array(map(markValue), 1f, 0f, 0f, 1f); // red
    // make a new colormap out of it 
    colormap = new ILColormap(colorkeys);
    // create & add a plot cube 
    ilPanel1.Scene.Add(new ILPlotCube(twoDMode: false) {
        // add surface plot, give custom colormap & colormap data ...
        new ILSurface(Z, colormap: colormap, C: cdata) {
            // add colorbar
            Childs = { new ILColorbar() }
        }
    }); 
}

这会产生以下输出: enter image description here