线性插值,这可以优化以更快地运行吗?

时间:2013-07-04 00:01:13

标签: c#

我有一个简单的程序循环一组点数据并进行线性插值以“填写”数据,我想知道是否有人可以帮助我提供一些关于我的代码的性能提示并向我展示我可以在哪些区域优化代码,谢谢!

这里代码:

    static internal Point3D[] _data;
    static internal Point3dTree _kd;

    static internal int _interpolation_count = 0;
    static internal int _iteration_count = 0;
    static Dictionary<int, Point3D> Interpolated_Values = new Dictionary<int, Point3D>();

    static internal int _threasindex;
    static internal double[] _threasholds = new double[]
    {
        0.5,
        1.0,
        1.5,
        20.5
    };

    static internal double Interpolate(double x, double x0, double x1, double y0, double y1)
    {
        if ((x1 - x0) == 0)
            return (y0 + y1) / 2;

        return y0 + (x - x0) * (y1 - y0) / (x1 - x0);
    }

    static void Main(string[] args)
    {
        using (new ProceduralTimer("Loading data"))
            _data = LasReader.GetData(@"C:\WindowsLP\SAMPLE_PROJECT\brisport2\area_cov.las");
        using (new ProceduralTimer("Bulding Kd tree"))
            _kd = new Point3dTree(_data, false);

        List<Point3D> InterpolatedData = _data.ToList();
        _data = null;

        using (new ProceduralTimer("Processing"))
        {
            int i = 0;
            var neighbours = new List<Point3D>();
            for (; i < InterpolatedData.Count; i++)
            {
            @rescan:
                neighbours = _kd.NearestNeighbours(new KdTreeNode<Point3D>(InterpolatedData[i]), _threasholds[_threasindex % _threasholds.Length]);

                if (neighbours.Count < 4 && _threasindex < _threasholds.Length)
                {
                    _threasindex++;
                    _iteration_count++;
                    goto rescan;
                }
                else
                {
                    if (neighbours.Count >= 4)
                    {
                        double[] xvalues = neighbours.Select(_ => _.X).ToArray();
                        double[] yvalues = neighbours.Select(_ => _.Y).ToArray();
                        double[] zvalues = neighbours.Select(_ => _.Z).ToArray();

                        Point3D pt = new Point3D();
                        pt.X = Math.Round(Interpolate(InterpolatedData[i].X, xvalues[0], xvalues[1], xvalues[2], xvalues[3]), 2);
                        pt.Y = Math.Round(Interpolate(InterpolatedData[i].Y, yvalues[0], yvalues[1], yvalues[2], yvalues[3]), 2);
                        pt.Z = Math.Round(Interpolate(InterpolatedData[i].Z, zvalues[0], zvalues[1], zvalues[2], zvalues[3]), 2);

                        Interpolated_Values[i] = pt;

                        _interpolation_count++;
                    }

                    _threasindex = 0;
                }
            }
        }

1 个答案:

答案 0 :(得分:1)

除了用于提取x y z值的linq之外,您使用的所有语言功能都非常低级。

double[] xvalues = neighbours.Select(_ => _.X).ToArray();
double[] yvalues = neighbours.Select(_ => _.Y).ToArray();
double[] zvalues = neighbours.Select(_ => _.Z).ToArray();

尝试通过提取X Y Z值的neighbours集合进行一次迭代:

List<double> xvalues = new List<double>();
List<double> yvalues = new List<double>();
List<double> zvalues = new List<double>();

foreach(var neighbour in neighbours)
{
    xvalues.Add(neighbour.X);
    yvalues.Add(neighbour.Y);
    zvalues.Add(neighbour.Z);
}