【问题标题】:ILNumerics plot a plane at specific locationILNumerics 在特定位置绘制平面
【发布时间】:2013-11-06 04:34:19
【问题描述】:

我目前正在使用 ILNumerics API 并开始在立方体中绘制几个点。 然后我通过这些点计算了一个回归平面。 现在我想在同一个场景图中绘制平面,但大小与点云相同。

我得到了平面的参数 (a,b,c): f(x,y) = a*x + b*y + c; 我知道只有 z 对绘制平面很有趣,但我不知道如何将正确的坐标传递给场景,以使平面大小与点的最大和最小面积大致相同。

你们能否给我一个简单的绘制平面的例子,以及如何正确设置该平面的边界的一些建议?

这是我目前得到的:

        private void ilPanel1_Load(object sender, EventArgs e)
    {
           // get the X and Y bounds and calculate Z with parameters

           // plot it!
            var scene = new ILScene {
              new ILPlotCube(twoDMode: false) {
                new ILSurface( ??? ) {
                }
              }
            };

           // view angle etc
            scene.First<ILPlotCube>().Rotation = Matrix4.Rotation(
            new Vector3(1f, 0.23f, 1), 0.7f);

        ilPanel1.Scene = scene; 
    }

我希望有人可以帮助我... 提前致谢 !!!

【问题讨论】:

    标签: c# plot plane ilnumerics


    【解决方案1】:

    您可以使用 plotcube.Plots 组的 Limits 并从中获取边界框的坐标。这为您提供了平面的最小和最大 x 和 y 坐标。使用它们通过评估您的平面方程来获得相应的 z 值。

    获得平面的 x、y 和 z 后,将它们与 ILSurface 一起使用来绘制平面。

    如果您需要更多帮助,我可以尝试添加示例。

    @Edit:以下示例通过 3 个任意点绘制平面。平面方向和位置是通过平面函数 zEval 计算的。它的系数 a,b,c 是从 3 个(具体)点计算出来的。您必须在此处计算自己的方程系数。

    平面是用一个曲面来实现的。不妨采用在“P”中计算的 4 个坐标并使用 ILTriangleFan 和 ILLineStrip 来创建平面和边界。但是表面已经带有填充和线框,因此我们将其作为一种快速解决方案。

    private void ilPanel1_Load(object sender, EventArgs e) {
        // 3 arbitrary points 
        float[,] A = new float[3, 3] { 
            { 1.0f, 2.0f, 3.0f }, 
            { 2.0f, 2.0f, 4.0f }, 
            { 2.0f, -2.0f, 2.0f } 
        };
        // construct a new plotcube and plot the points
        var scene = new ILScene {
            new ILPlotCube(twoDMode: false) {
                new ILPoints {
                    Positions = A,
                    Size = 4,
                }
            }
        };
        // Plane equation: this is derived from the concrete example points. In your 
        // real world app you will have to adopt the weights a,b and c to your points. 
        Func<float, float, float> zEval = (x, y) => {
            float a = 1, b = 0.5f, c = 1;
            return a * x + b * y + c; 
        }; 
        // find bounding box of the plot contents 
        scene.Configure(); 
        var limits = scene.First<ILPlotCube>().Plots.Limits; 
    
        // Construct the surface / plane to draw
        // The 'plane' will be a surface constructed from a 2x2 mesh only. 
        // The x/y coordinates of the corners / grid points of the surface are taken from 
        // the limits of the plots /points. The corresponding Z coordinates are computed 
        // by the zEval function. So we give the ILSurface constructor not only Z coordinates 
        // as 2x2 matrix - but an Z,X,Y Array of size 2x2x3
        ILArray<float> P = ILMath.zeros<float>(2, 2, 3);
        Vector3 min = limits.Min, max = limits.Max; 
        P[":;:;1"] = new float[,] { { min.X, min.X }, { max.X, max.X } };
        P[":;:;2"] = new float[,] { { max.Y, min.Y }, { max.Y, min.Y } };
        P[":;:;0"] = new float[,] { 
            { zEval(min.X, max.Y) , zEval(min.X, min.Y) }, 
            { zEval(max.X, max.Y) , zEval(max.X, min.Y) }, 
        };
        // create the surface, make it semitransparent and modify the colormap
        scene.First<ILPlotCube>().Add(new ILSurface(P) {
            Alpha = 0.6f, 
            Colormap = Colormaps.Prism
        }); 
        // give the scene to the panel
        ilPanel1.Scene = scene;
    }
    

    这将创建一个类似于此的图像:

    @Edit2:你问,如何在添加曲面时禁用绘图立方体的自动缩放:

    // before adding the surface: 
    var plotCube = scene.First<ILPlotCube>(); 
    plotCube.AutoScaleOnAdd = false; 
    

    或者,您可以手动设置立方体的限制:

    plotCube.Limits.Set(min,max); 
    

    您可能还想禁用一些鼠标交互,因为它们会允许用户以类似(不需要的?)方式重新缩放立方体:

    plotCube.AllowZoom = false;    // disables the mouse wheel zoom
    plotCube.MouseDoubleClick += (_,arg) => {
            arg.Cancel = true;     // disable the double click - resetting for the plot cube
    };
    

    【讨论】:

    • 我真的很感激一个例子......我知道如何绘制点,但我仍然不知道如何将正确的数组传递给 ILSurface() 方法......我试过了它使用简单的数据,但它从来没有按照我想要的方式工作。在 Matlab 中,我需要生成 x 和 y 坐标,例如 x=-1:0.01:1 等,而 z 只需要上面的公式和 x 和 y 范围坐标。你介意举个简单的例子吗?
    • 由于您只想绘制平面,因此到表面的数据将仅为 2x2x3:每个 2x2 切片给出 Z、X 和 Y 的 4 个坐标值(按此顺序)。文档可在此处找到:ilnumerics.net/surface-plots.html
    • 对不起Haymo,但我不明白。为什么每个坐标轴需要 4 个坐标?也就是说我需要 3 个 2x2 切片在 ona 数组中?像 {{z z},{z z},{x x},{x x},{y y},{y y}} ???这对我没有任何意义=D对不起。对我来说这样的事情: {{minRangeX maxRangeX}, {minRangeY maxRangeY}, {Z(XY)}} 会有意义......你只有一个有 12 个坐标的小例子吗?抱歉打扰你了:D
    • 这真是太棒了Haymo!!!但我有最后一个问题=)。一旦我绘制了表面,立方体的关系就会变成一个未理解的输出。一旦我将点绘制到立方体上,我想保持立方体关系。这有什么大不了的吗?或者只是将其设置为无关系调整之类的?
    • 它工作得很好 =)。你帮了我很多!继续努力!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多