我在3D空间中有一组边界框(矩形)。计算每个框的边界并将其存储在名为“RegionBounds”的字典中。此外,在名为“PointsToCategorize”的List中填充一组点。给定填充的List中的点(x,y,z)坐标和要检入的边界框,我可以检查该点是否在框内或不。问题是,这是一个很大的数据集。要检查的点数是1000,而边界框的数量是250-300。所以,如果我遍历每个给定点的每个边界框;所需的总时间为5-6分钟。有没有有效的方法可以更快地完成这个过程?如果可能的话,这样做的小代码会很棒
public struct iBounds {
public double x1, x2;
public double y1, y2;
public double z1, z2;
}
public struct iPoint {
public double x,y,z
}
Dictionary<String, iBounds> RegionBounds = new Dictionary<String, iBounds>();
List<iPoint> PointsToCategorize = new List<iPoint>();
int no_of_bounding_boxes = 300;
int no_of_points_to_categorize = 1000;
for (int i = 1; i <= no_of_bounding_boxes; i++)
{
String boundingBoxName = "bound_" + i;
iBounds boundingBox = new iBounds
{
x1 = Computed By Some Other method and Formulas,
x2 = Computed By Some Other method and Formulas,
y1 = Computed By Some Other method and Formulas,
y2 = Computed By Some Other method and Formulas,
z1 = Computed By Some Other method and Formulas,
z2 = Computed By Some Other method and Formulas
};
RegionBounds.Add(boundingBoxName, boundingBox);
}
////////////Start of Output section /////////////////////////
for(int i= 1; i < = PointsToCategorize.Count; i++){
foreach(var pair in RegionBounds)
{
String myboxNmame = pair.Key;
iBounds myboxBounds = pair.Value;
Console.WriteLine(PointInside(PointsToCategorize[i],myboxBounds).ToString());
}
}
////////////// End of Output section //////////////////
private bool PointInside(iPoint mypoint, iBounds boxToBeCheckedIn)
{
if (mypoint.x > boxToBeCheckedIn.x1) && (mypoint.x < boxToBeCheckedIn.x2){
if (mypoint.y > boxToBeCheckedIn.y1) && (mypoint.y < boxToBeCheckedIn.y2){
if (mypoint.z > boxToBeCheckedIn.z1) && (mypoint.z < boxToBeCheckedIn.z2){
return true;
}
}
}else{
return false;
}
}
答案 0 :(得分:0)
您可能希望使用OcTree或kD-tree数据结构,这比迭代所有框更有效。
另请参阅 2-D正交范围搜索部分中的this article,它具有非常好的可用技术和算法简历,可轻松扩展到3D