从用户输入(通过鼠标)向ILPlotCube添加ILPoints

时间:2015-05-23 20:48:43

标签: c# plot ilnumerics

我是ILNumerics的新手,我对一些简单的活动有疑问。我想在用户鼠标输入上添加点到绘图。用户点击了图上的点5,4,这一点必须在这个位置上绘制。 我有一些问题要将鼠标的位置转换为绘图坐标,我在这里找到了解决方案,但它对我来说并不理想。我做了什么:

private void ilpGraph_Load(object sender, EventArgs e)
    {
        var scene = new ILScene();

        var plotCube = scene.Add(
            new ILPlotCube()
            {
                new PointsGroup()
            });

        plotCube.Limits.Set(new Vector3(0, 0, 0), new Vector3(10, 10, 0));

        plotCube.MouseClick += plotCube_MouseClick;

        ilpGraph.Scene = scene;
    }

void plotCube_MouseClick(object sender, ILMouseEventArgs e)
    {
        var pos = new Vector3(0, 0, 0);

        ILGroup plotcubeGroup = sender as ILGroup;
        ILGroup group = plotcubeGroup.Children.Where(item => item.Tag == "PointsGroup").First() as ILGroup;
        if (group != null)
        {
            // walk up to the next camera node 
            Matrix4 trans = group.Transform;
            while (!(group is ILCamera) && group != null)
            {
                group = group.Parent;
                // collect all nodes on the path up
                trans = group.Transform * trans;
            }
            if (group != null && (group is ILCamera))
            {
                // convert args.LocationF to world coords
                // The Z coord is not provided by the mouse! -> choose arbitrary value
                pos = new Vector3(e.LocationF.X * 2 - 1, e.LocationF.Y * -2 + 1, 0);
                // invert the matrix.
                trans = Matrix4.Invert(trans);
                // trans now converts from the world coord system (at the camera) to 
                // the local coord system in the 'target' group node (surface). 
                // In order to transform the mouse (viewport) position, we 
                // left multiply the transformation matrix.
                pos = trans * pos;
            }
        }

        // it's for testing now
        var pg = (sender as ILPlotCube).Plots.Children[0];
        var t = ((PointsGroup)pg).Points.Positions;
        int dc = t.DataCount;

        t.Update(dc, 1, new[] { pos.X, pos.Y, 0.0f });
        e.Refresh = true;
    }

public class PointsGroup : ILGroup
{
    private ILPoints _points;

    public PointsGroup()
        : base((object)"PointsGroup", new Vector3?(), 0.0, new Vector3?(), new Vector3?(), new RenderTarget?())
    {
    }

    internal PointsGroup(PointsGroup source) : base((ILGroup) source)
    {
    }

    public ILPoints Points
    {
        get
        {
            if (_points == null)
                return new ILPoints();
            return _points;
        }

        set { _points = value; }
    }

    protected override ILNode CreateSynchedCopy(ILNode source)
    {
        return (ILNode)new PointsGroup();
    }

    public override ILNode Copy()
    {
        return (ILNode)new PointsGroup(this);
    }

    public override ILNode Synchronize(ILNode copy, ILSyncParams syncParams)
    {
        return base.Synchronize(copy, syncParams);
    }
}

问题是什么: 如果我没有定义PointsGroup类,我无法在绘图上本地化鼠标坐标(ILPoints不是ILGroup,我不知道如何以不同方式执行)。 使用PointsGroup坐标计算良好,但坐标不会被记住并且不会在图上显示。我知道它为什么会发生,但我真的不知道如何解决这个问题。 如何扩展我的PointsGroup类以记住我的值并在绘图上显示或如何修改坐标计算以处理ILPoints对象? 谁能帮我?请

0 个答案:

没有答案
相关问题