Prevent IncrementalMesh crash

Say, I want to perform a cut operation on two boxes. How can I detect that a cut returns an empty region before I pass the constructed object to BRepMesh_IncrementalMesh? For a concrete case consider the following code that crashes. There the tool (second box) is larger the the object (first box):

-----------

#include
#include
#include
#include

int main()
{
gp_Pnt gp1 = gp_Pnt(-1,-1,-1);
gp_Pnt gp2 = gp_Pnt(1,1,1);
gp_Pnt gp3 = gp_Pnt(-2,-2,-2);
gp_Pnt gp4 = gp_Pnt(2,2,2);

TopoDS_Shape s1 = BRepPrimAPI_MakeBox(gp1, gp2).Shape();
TopoDS_Shape s2 = BRepPrimAPI_MakeBox(gp3, gp4).Shape();

int type = s1.ShapeType();

BRepAlgoAPI_Cut aCut(s1, s2);

if (!aCut.IsDone())
return 0;

if (aCut.HasErrors())
return 0;

TopoDS_Shape shape = aCut.Shape();

if (shape.IsNull())
return 0;

type = shape.ShapeType();

/* what to do before this call? */
BRepMesh_IncrementalMesh Mesh( shape, 0.01 );
Mesh.Perform();
const Standard_Integer ok = !Mesh.GetStatusFlags();
return ok;
}

--------
The shape type is a COMPOUND (type 0) how can I check that the computed aCut can not be meshed?
There are many Boolean operations that can return empty regions but how to detect that? I have looked through the QABugs/QABugs_XYZ.cxx sources but could not find anything. Do I need to use the TopExp_Explorer Ex and look for something specific, if so what should I test for?
Thanks

Kenji Hara's picture

IncrementalMesh works for face and edge.

So you may check that the 'shape' contains at least one face like as follows:

TopExp_Explorer ex(shape, TopAbs_FACE);

if (!ex.More())

    return 0;

Oliver R's picture

Thanks for you suggestion Kenji. I added a test based on TopAbs_EDGE in the spirit you suggested and it indeed does prevent the crash. The logic being that if there is no edge then there can not be a face.