Creating an OBB using BRepBndLib::AddOBB() yields poor results.

jianbaoxia

CAD master
Hi Bros, I'm JianBaoXia.;)

As title, I attempted to use BRepBndLib::AddOBB() to generate an OBB for four points that are not coplanar, but the results were confusing. Am I using it incorrectly?

There is my demo:
Code:
bool WriteStepFile(const TopoDS_Shape& shape, const std::string& filepath)
{
    bool output_flag = true;
    STEPControl_Writer writer;
    Interface_Static::SetCVal("write.step.schema", "AP203");
    IFSelect_ReturnStatus status = writer.Transfer(shape, STEPControl_AsIs);
    if (status != IFSelect_RetDone) output_flag = false;
    status = writer.Write(filepath.c_str());
    if (status != IFSelect_RetDone) output_flag = false;

    return output_flag;
}

TopoDS_Shape GetEdges_of_obb(const Bnd_OBB& obb)
{
    gp_Pnt centerPnt = obb.Center();
    double xh = obb.XHSize();    // x_half_size
    double yh = obb.YHSize();
    double zh = obb.ZHSize();

    // 
    gp_XYZ p0(xh, yh, zh);
    gp_XYZ p1(-xh, yh, zh);
    gp_XYZ p2(-xh, -yh, zh);
    gp_XYZ p3(xh, -yh, zh);

    gp_XYZ p4(xh, yh, -zh);
    gp_XYZ p5(-xh, yh, -zh);
    gp_XYZ p6(-xh, -yh, -zh);
    gp_XYZ p7(xh, -yh, -zh);

    //
    gp_Pnt pnt0 = centerPnt.XYZ() + p0;
    gp_Pnt pnt1 = centerPnt.XYZ() + p1;
    gp_Pnt pnt2 = centerPnt.XYZ() + p2;
    gp_Pnt pnt3 = centerPnt.XYZ() + p3;
    gp_Pnt pnt4 = centerPnt.XYZ() + p4;
    gp_Pnt pnt5 = centerPnt.XYZ() + p5;
    gp_Pnt pnt6 = centerPnt.XYZ() + p6;
    gp_Pnt pnt7 = centerPnt.XYZ() + p7;

    //
    TopoDS_Edge e01 = BRepBuilderAPI_MakeEdge(pnt0, pnt1);
    TopoDS_Edge e12 = BRepBuilderAPI_MakeEdge(pnt1, pnt2);
    TopoDS_Edge e23 = BRepBuilderAPI_MakeEdge(pnt2, pnt3);
    TopoDS_Edge e30 = BRepBuilderAPI_MakeEdge(pnt3, pnt0);

    TopoDS_Edge e45 = BRepBuilderAPI_MakeEdge(pnt4, pnt5);
    TopoDS_Edge e56 = BRepBuilderAPI_MakeEdge(pnt5, pnt6);
    TopoDS_Edge e67 = BRepBuilderAPI_MakeEdge(pnt6, pnt7);
    TopoDS_Edge e74 = BRepBuilderAPI_MakeEdge(pnt7, pnt4);

    TopoDS_Edge e04 = BRepBuilderAPI_MakeEdge(pnt0, pnt4);
    TopoDS_Edge e15 = BRepBuilderAPI_MakeEdge(pnt1, pnt5);
    TopoDS_Edge e26 = BRepBuilderAPI_MakeEdge(pnt2, pnt6);
    TopoDS_Edge e37 = BRepBuilderAPI_MakeEdge(pnt3, pnt7);

    //
    TopoDS_Compound com;
    BRep_Builder bb;
    bb.MakeCompound(com);
    bb.Add(com, e01);
    bb.Add(com, e12);
    bb.Add(com, e23);
    bb.Add(com, e30);

    bb.Add(com, e45);
    bb.Add(com, e56);
    bb.Add(com, e67);
    bb.Add(com, e74);

    bb.Add(com, e04);
    bb.Add(com, e15);
    bb.Add(com, e26);
    bb.Add(com, e37);

    return com;
}

int main()
{
    gp_Pnt p0(-124.989, 36.9042, 30);
    gp_Pnt p1(-130.293, 40.0692, 0);
    gp_Pnt p2(-127.948, 48.6445, 0);
    gp_Pnt p3(-123.193, 44.6424, 30);

    TopoDS_Compound com1;
    BRep_Builder bb1;
    bb1.MakeCompound(com1);
    bb1.Add(com1, BRepBuilderAPI_MakeVertex(p0));
    bb1.Add(com1, BRepBuilderAPI_MakeVertex(p1));
    bb1.Add(com1, BRepBuilderAPI_MakeVertex(p2));
    bb1.Add(com1, BRepBuilderAPI_MakeVertex(p3));

    Bnd_OBB obb;
    BRepBndLib::AddOBB(com1, obb);
    TopoDS_Shape obb_wireFrame = GetEdges_of_obb(obb);

    TopoDS_Compound com;
    BRep_Builder bb;
    bb.MakeCompound(com);
    bb.Add(com, obb_wireFrame);
    bb.Add(com, BRepBuilderAPI_MakeVertex(p0));
    bb.Add(com, BRepBuilderAPI_MakeVertex(p1));
    bb.Add(com, BRepBuilderAPI_MakeVertex(p2));
    bb.Add(com, BRepBuilderAPI_MakeVertex(p3));
    // bb.Add(com, ruleFace_sec);
    // bb.Add(com, ruledFace);

    WriteStepFile(com, "../obb.step");
    
    return 0;
}
1687192210342.png
It is evident that this is an erroneous result, and I would greatly appreciate any assistance in resolving this issue.
 

jianbaoxia

CAD master
There is no error of "BRepBndLib::AddOBB()", it's my function "GetEdges_of_obb" exit bugs.:ROFLMAO:

The new GetEdges_of_obb is:
Code:
TopoDS_Shape GetEdges_of_obb(const Bnd_OBB& obb)
{
    gp_Pnt centerPnt = obb.Center();
    double xh = obb.XHSize();    // x_half_size
    double yh = obb.YHSize();
    double zh = obb.ZHSize();

    gp_Dir dx = obb.XDirection();
    gp_Dir dy = obb.YDirection();
    gp_Dir dz = obb.ZDirection();

    //  OBB 八个顶点同中心点的xyz增量
    /*gp_XYZ p0(xh, yh, zh);
    gp_XYZ p1(-xh, yh, zh);
    gp_XYZ p2(-xh, -yh, zh);
    gp_XYZ p3(xh, -yh, zh);

    gp_XYZ p4(xh, yh, -zh);
    gp_XYZ p5(-xh, yh, -zh);
    gp_XYZ p6(-xh, -yh, -zh);
    gp_XYZ p7(xh, -yh, -zh);*/
    gp_XYZ p0(xh * dx.XYZ() + yh * dy.XYZ() + zh * dz.XYZ());
    gp_XYZ p1(-xh * dx.XYZ() + yh * dy.XYZ() + zh * dz.XYZ());
    gp_XYZ p2(-xh * dx.XYZ() - yh * dy.XYZ() + zh * dz.XYZ());
    gp_XYZ p3(xh * dx.XYZ() - yh * dy.XYZ() + zh * dz.XYZ());

    gp_XYZ p4(xh * dx.XYZ() + yh * dy.XYZ() - zh * dz.XYZ());
    gp_XYZ p5(-xh * dx.XYZ() + yh * dy.XYZ() - zh * dz.XYZ());
    gp_XYZ p6(-xh * dx.XYZ() - yh * dy.XYZ() - zh * dz.XYZ());
    gp_XYZ p7(xh * dx.XYZ() - yh * dy.XYZ() - zh * dz.XYZ());


    // OBB 八个顶点
    gp_Pnt pnt0 = centerPnt.XYZ() + p0;
    gp_Pnt pnt1 = centerPnt.XYZ() + p1;
    gp_Pnt pnt2 = centerPnt.XYZ() + p2;
    gp_Pnt pnt3 = centerPnt.XYZ() + p3;
    gp_Pnt pnt4 = centerPnt.XYZ() + p4;
    gp_Pnt pnt5 = centerPnt.XYZ() + p5;
    gp_Pnt pnt6 = centerPnt.XYZ() + p6;
    gp_Pnt pnt7 = centerPnt.XYZ() + p7;

    // 包围盒12条边
    TopoDS_Edge e01 = BRepBuilderAPI_MakeEdge(pnt0, pnt1);
    TopoDS_Edge e12 = BRepBuilderAPI_MakeEdge(pnt1, pnt2);
    TopoDS_Edge e23 = BRepBuilderAPI_MakeEdge(pnt2, pnt3);
    TopoDS_Edge e30 = BRepBuilderAPI_MakeEdge(pnt3, pnt0);

    TopoDS_Edge e45 = BRepBuilderAPI_MakeEdge(pnt4, pnt5);
    TopoDS_Edge e56 = BRepBuilderAPI_MakeEdge(pnt5, pnt6);
    TopoDS_Edge e67 = BRepBuilderAPI_MakeEdge(pnt6, pnt7);
    TopoDS_Edge e74 = BRepBuilderAPI_MakeEdge(pnt7, pnt4);

    TopoDS_Edge e04 = BRepBuilderAPI_MakeEdge(pnt0, pnt4);
    TopoDS_Edge e15 = BRepBuilderAPI_MakeEdge(pnt1, pnt5);
    TopoDS_Edge e26 = BRepBuilderAPI_MakeEdge(pnt2, pnt6);
    TopoDS_Edge e37 = BRepBuilderAPI_MakeEdge(pnt3, pnt7);

    //
    TopoDS_Compound com;
    BRep_Builder bb;
    bb.MakeCompound(com);
    bb.Add(com, e01);
    bb.Add(com, e12);
    bb.Add(com, e23);
    bb.Add(com, e30);

    bb.Add(com, e45);
    bb.Add(com, e56);
    bb.Add(com, e67);
    bb.Add(com, e74);

    bb.Add(com, e04);
    bb.Add(com, e15);
    bb.Add(com, e26);
    bb.Add(com, e37);

    return com;
}
 
Last edited:
Top