大点云中大点循环的优化

问题描述 投票:0回答:1

我所拥有的以下检测线实现了针对点云的霍夫变换的检测(3空间中点的集合)

internal sealed class LineHoughTransform : ILineHoughTransform
{
    private readonly double _dX;
    private readonly double _maxX;

    private readonly long _countX;
    private readonly long _countB;

    private readonly IDiscreetSphere _sphere;

    public LineHoughTransform(Vector3 minParameterVector, Vector3 maxParameterVector, double dX, int sphereGranularity)
    {
        _dX = dX;

        _sphere = new Icosahedron();
        _sphere.Create(sphereGranularity);

        _countB = _sphere.Points.Count;
        _maxX = Math.Max(maxParameterVector.Norm(), minParameterVector.Norm());

        var rangeX = 2 * _maxX;
        if (_dX == 0.0)
            _dX = rangeX / 64.0;

        _countX = (long)(rangeX / _dX).RoundToNearest();

        VotingSpace = new Dictionary<long, int>();
    }

    public int GetLine(ref Vector3 a, ref Vector3 b)
    {
        int votes = 0;
        long index = 0;

        foreach (var storedVote in VotingSpace)
        {
            if (storedVote.Value > votes)
            {
                votes = storedVote.Value;
                index = storedVote.Key;
            }
        }

        // Retrieve x' coordinate from VotingSpace[_countX * _countX * _countB].
        double x = index / (_countX * _countB);
        index -= (long)(x * _countX * _countB);
        x = x * _dX - _maxX;

        // Retrieve y' coordinate from VotingSpace[_countX * _countX * _countB].
        double y = index / _countB;
        index -= (long)y * _countB;
        y = y * _dX - _maxX;

        // Retrieve directional vector and Compute anchor point according to Eq. (3).
        b = _sphere.Points[(int)index];

        a.X = (float)(x * (1 - ((b.X * b.X) / (1 + b.Z))) - y * ((b.X * b.Y) / (1 + b.Z)));
        a.Y = (float)(x * (-((b.X * b.Y) / (1 + b.Z))) + y * (1 - ((b.Y * b.Y) / (1 + b.Z))));
        a.Z = (float)(-x * b.X - y * b.Y);

        return votes;
    }

    public void Add(IPointCloud pointCloud)
    {
        CastVote(pointCloud, true);
    }

    public void Subtract(IPointCloud pointCloud)
    {
        CastVote(pointCloud, false);
    }

    private void CastVote(IPointCloud pointCloud, bool add)
    {
        if (pointCloud == null || pointCloud.Vertices == null)
            return;

        foreach (var vertex in pointCloud.Vertices)
            PointVote(vertex.Point, add);
    }

    private void PointVote(Vector3 point, bool add)
    {
        // Loop over directions B.
        for (int j = 0; j < _sphere.Points.Count; ++j)
        {
            // Denominator in Eq. (2).
            Vector3 b = _sphere.Points[j];
            double beta = 1 / (1 + b.Z);

            // Compute x' and y' according to Eq. (2).
            double newX = ((1 - (beta * (b.X * b.X))) * point.X) - (beta * (b.X * b.Y) * point.Y) - (b.X * point.Z);
            double newY = (-beta * (b.X * b.Y) * point.X) + ((1 - (beta * (b.Y * b.Y))) * point.Y) - (b.Y * point.Z);

            long x_i = (long)((newX + _maxX) / _dX).RoundToNearest();
            long y_i = (long)((newY + _maxX) / _dX).RoundToNearest();

            // Compute one-dimensional index from three indices.
            // x_i * <number of planes> * <number of direction vectors> + y_i * <number of direction vectors> + <loop index>
            long index = (x_i * _countX * _countB) + (y_i * _countB) + j;

            if (!VotingSpace.ContainsKey(index))
                VotingSpace.Add(index, 0);

            if (add)
                VotingSpace[index]++;
            else
                VotingSpace[index]--;
        }
    }

    public Dictionary<long, int> VotingSpace { get; private set; }
}

我想提高这段代码的速度,所以我尝试使用

public ConcurrentDictionary<long, int> VotingSpace { get; private set; }

private void CastVote(IPointCloud pointCloud, bool add)
{
    if (pointCloud == null || pointCloud.Vertices == null)
        return;

    Parallel.ForEach(pointCloud.Vertices, vertex => PointVote(vertex.Point, add));
}

[注意,CastVote中的pointCloud可以包含大量点,,并且VotingSpace增量变为

if (!VotingSpace.ContainsKey(index))
    VotingSpace.TryAdd(index, 0);

if (add)
    VotingSpace[index]++;
else
    VotingSpace[index]--;

但是,有时TryAdd失败,导致我的调用算法失败。我尝试对TryAdd进行重试,但这似乎无助于删除索引的问题。如何使此类尽可能多地成为最佳多线程类,并以与原始类完全相同的方式工作?

c# multithreading optimization hough-transform
1个回答
0
投票

使用并发集合时,通常使用它们提供的特殊原子API。在这种情况下,您可能应该使用ConcurrentDictionary.AddOrUpdate方法:

ConcurrentDictionary.AddOrUpdate
© www.soinside.com 2019 - 2024. All rights reserved.