我有一个结构节点的大列表(如300000个元素),其中Node被定义为
public struct Node { public int nid; public double x,y,z; }
并且我想知道哪些节点彼此接近一定量(在代码中称为间隙),以便组合在一起的元素.
请注意,对于给定的节点分布,每个节点仅属于一个集合.
我试着用这种方式,循环每个节点并使用Linq查询和三维空间中两点之间的距离的经典公式寻找它的邻居,但它确实很慢.
有什么建议吗?提前致谢.
List<Node> nList; [...] for (int i = 0; i < nList.Count; i++) // cycles through all nodes in list { Node nodo_old = nList[i]; // current node List<Node> nset = nList.Where(n => (Math.Sqrt(Math.Pow((n.x - nodo_old.x),2) + Math.Pow((n.y - nodo_old.y),2) + Math.Pow((n.z - nodo_old.z),2)) <= gap)).ToList(); [...] // Now nset contains a list of neighbour nodes }
解决方法
此算法删除已检查的节点以减少处理时间.我没有尝试你的问题,但我相信它会帮助你,因为我在另一个场景测试它,它的工作完美.
// to hold sets of neighbour nodes Dictionary<int,List<Node>> relatedCollectionsDictionary = new Dictionary<int,List<Node>>(); int index = 0; List<Node> nList; while (nList.Any()) { var relatedCollection = nList.Where(n => (Math.Sqrt(Math.Pow((n.x - nList.First().x),2) + Math.Pow((n.y - nList.First().y),2) + Math.Pow((n.z - nList.First().z),2)) <= gap)); List<Node> relatedCollectionList = relatedCollection.ToList(); List<Node> relatedCollectionListForDictionary = relatedCollection.ToList(); relatedCollectionsDictionary.Add(index++,relatedCollectionListForDictionary); while (relatedCollectionList.Any()) { nList.Remove(relatedCollectionList.First()); relatedCollectionList.RemoveAt(0); } }
我们的想法是,您每次都要进行大量处理并遍历所有节点.但是使用这种情况,您不会多次遍历任何项目.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。