C# 大型点云中大型点环的优化
我所做的就是为点云(3空间中的点集合)实现线检测Hough变换 注意,C# 大型点云中大型点环的优化,c#,multithreading,optimization,hough-transform,C#,Multithreading,Optimization,Hough Transform,我所做的就是为点云(3空间中的点集合)实现线检测Hough变换 注意,CastVote中的点云可以包含大量的点,并且VotingSpace增量变为 if (!VotingSpace.ContainsKey(index)) VotingSpace.TryAdd(index, 0); if (add) VotingSpace[index]++; else VotingSpace[index]--; 但是,有时TryAdd会失败,导致调用算法失败。我曾尝试在TryAdd上重
CastVote
中的点云可以包含大量的点,并且VotingSpace
增量变为
if (!VotingSpace.ContainsKey(index))
VotingSpace.TryAdd(index, 0);
if (add)
VotingSpace[index]++;
else
VotingSpace[index]--;
但是,有时
TryAdd
会失败,导致调用算法失败。我曾尝试在TryAdd
上重试,但这似乎无助于解决删除索引的问题。如何使该类尽可能简单地以与原始类完全相同的方式优化多线程?在处理并发集合时,通常使用它们提供的特殊原子API。在这种情况下,您可能应该使用以下方法:
啊,这可能就是问题所在。谢谢你抽出时间。非常感谢。
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));
}
if (!VotingSpace.ContainsKey(index))
VotingSpace.TryAdd(index, 0);
if (add)
VotingSpace[index]++;
else
VotingSpace[index]--;
VotingSpace.AddOrUpdate(index,
addValueFactory: (key) => add ? 1 : -1,
updateValueFactory: (key, existingValue) => existingValue + (add ? 1 : -1));