BVH:
void occQt::makeSphere()
{
gp_Ax2 anAxis;
anAxis.SetLocation(gp_Pnt(0.0, 0.0, 0.0));
//1.创建物体
TopoDS_Shape aTopoSphere = BRepPrimAPI_MakeSphere(anAxis, 30.0).Shape();
//2.获取shape指针
Handle(AIS_Shape) anAisSphere = new AIS_Shape(aTopoSphere);
//设置颜色
anAisSphere->SetColor(Quantity_NOC_BLUE1);
//通过OccView上下文显示
myOccView->getContext()->Display(anAisSphere, Standard_True);
Handle(AIS_InteractiveContext) myAISContext = myOccView->getContext();
//构造bvh树
BRepExtrema_ShapeList faceList;
for (TopExp_Explorer exp(aTopoSphere, TopAbs_FACE); exp.More(); exp.Next()) {
faceList.Append(TopoDS::Face(exp.Current()));
}
//BRepExtrema_ShapeList aaL;
BRepExtrema_TriangleSet triangleSet(faceList);
int triangleSize = triangleSet.Size();
// TopoDS_Shape edgeShape = edge.Current();
// Handle(AIS_Shape) h1 = new AIS_Shape(edgeShape);
// myAISContext->Display(h1, true);
triangleSet.BVH();
//const NCollection_Handle<BVH_Tree<Standard_Real, 3> > aBvh = triangleSet.BVH();
const opencascade::handle<BVH_Tree<Standard_Real, 3> > aBvh = triangleSet.BVH();
int length = aBvh->Length();//节点数量
int depth = aBvh->Depth();//深度
int innNodeNum = 0;
int leavesNodeNum = 0;
int leavesNumMax = 0;
for (int i = 0; i < length; i +=1)
{
BVH_Vec4i aNodeData1 = aBvh->NodeInfoBuffer()[i];
//BVH_Vec4i aNodeData =
//std::vector<typename VectorType<T, N>::Type>
BVH_Array4i array = aBvh->NodeInfoBuffer();//BVH_Vec4i数组,数量为length
BVH_Vec4i aNodeData = array[i];//四个值为:节点类型、起始序号,结束序号、0
if (aNodeData.x() == 0)
{
innNodeNum++;
//aNodeData
int beginIndex = aBvh->BegPrimitive(i);
int endIndex = aBvh->EndPrimitive(i);
qDebug() << " inn node, num:" << endIndex-beginIndex << endl;
int aa = 0;
#if 0
// inner node.
const BVH_Vec3d& aP1 = aBvh->MinPoint(aNodeData.y());
const BVH_Vec3d& aP2 = aBvh->MaxPoint(aNodeData.y());
const BVH_Vec3d& aQ1 = aBvh->MinPoint(aNodeData.z());
const BVH_Vec3d& aQ2 = aBvh->MaxPoint(aNodeData.z());
if (aP1.x() < aP2.x() && aP1.y() < aP2.y() && aP1.z() < aP2.z())
{
BRepPrimAPI_MakeBox aBoxMaker1(gp_Pnt(aP1.x(), aP1.y(), aP1.z()), gp_Pnt(aP2.x(), aP2.y(), aP2.z()));
for (TopExp_Explorer edge(aBoxMaker1.Shape(), TopAbs_EDGE); edge.More(); edge.Next())
{
TopoDS_Shape edgeShape = edge.Current();
Handle(AIS_Shape) h1 = new AIS_Shape(edgeShape);
myAISContext->Display(h1, true);
//qDebug() << i << "-make edge " << endl;
}
}
#endif
}
else
{
leavesNodeNum++;
int beginIndex = aBvh->BegPrimitive(i);
int endIndex = aBvh->EndPrimitive(i);
if ((endIndex - beginIndex) > leavesNumMax)
leavesNumMax = endIndex - beginIndex;
qDebug() << " leaves node, num:" << endIndex - beginIndex << endl;
#if 1
qDebug() << i << “-leaves” << endl;
int xVal = aNodeData.x();
// leaves node.
const BVH_Vec3d& aP1 = aBvh->MinPoint(i);//包围盒的点1
const BVH_Vec3d& aP2 = aBvh->MaxPoint(i);//包围盒的点2
double len2 = (aP1.x() - aP2.x()) * (aP1.x() - aP2.x()) + (aP1.y() - aP2.y()) * (aP1.y() - aP2.y()) + (aP1.z() - aP2.z()) * (aP1.z() - aP2.z());
int aa = 0;
//if (aP1.x() < aP2.x() && aP1.y() < aP2.y() && aP1.z() < aP2.z())
//{
// BRepPrimAPI_MakeBox aBoxMaker1(gp_Pnt(aP1.x(), aP1.y(), aP1.z()), gp_Pnt(aP2.x(), aP2.y(), aP2.z()));
// for (TopExp_Explorer edge(aBoxMaker1.Shape(), TopAbs_EDGE); edge.More(); edge.Next())
// {
// TopoDS_Shape edgeShape = edge.Current();
// Handle(AIS_Shape) h1 = new AIS_Shape(edgeShape);
// myAISContext->Display(h1, true);
// //qDebug() << i << "-make edge " << endl;
// }
//}
#endif
}
//myAISContext->Erase(hBox3,true);
}
qDebug() << leavesNodeNum << "-leaves node," << innNodeNum << " inn node" << endl;
qDebug() << " leaves node max num:" << leavesNumMax << endl;
}