OCCT 使用 BVH 树加速 bounding box 查找遍历

Demo code

Demo code 1

#include <BVH_Tree.hxx>
#include <TopoDS_Shape.hxx>
#include <vector>

// Assuming you have a vector of TopoDS_Shape objects called 'shapes'

// Create a BVH tree
Handle(BVH_Tree<Standard_Real, 3>) bvhTree = new BVH_Tree<Standard_Real, 3>();

// Insert the shapes into the BVH tree
for (const auto& shape : shapes) {
  // Compute the bounding box for the shape
  Bnd_Box box;
  BRepBndLib::Add(shape, box);

  // Insert the bounding box into the BVH tree
  bvhTree->Insert(box, shape);
}

// a query on the BVH tree to find possible interference
bvhTree->Select([](const TopoDS_Shape& shape1, const TopoDS_Shape& shape2) {
  // Check for interference between shape1 and shape2
  // If there is interference, process it accordingly
});

Demo code 2

#include <SpatialHash.hxx>
#include <TopoDS_Shape.hxx>

int main()
{
  // Create a spatial hash from the shapes.
  SpatialHash hash(shapes.size());
  for (TopoDS_Shape& shape : shapes)
  {
    hash.Add(shape);
  }

  // Find possibly interfering objects.
  for (TopoDS_Shape& shape : shapes)
  {
    std::vector<TopoDS_Shape> interferingObjects = hash.Find(shape);
    for (TopoDS_Shape& otherShape : interferingObjects)
    {
      // Check if the shapes interfere.
    }
  }

  return 0;
}

References




    Enjoy Reading This Article?

    Here are some more articles you might like to read next:

  • al-folio 本地部署记录(Ubuntu 24.04)
  • C++ Traits
  • 道格拉斯-普克算法(Douglas–Peucker algorithm)
  • CMake支持库收集
  • QGC代码架构解析:飞行前检查(起飞条件)