Skip to content

Commit

Permalink
Measure: handle potential exceptions in MeasureToolBRep::brepMinDista…
Browse files Browse the repository at this point in the history
…nce()

Relates to GitHub #288
  • Loading branch information
HuguesDelorme committed Aug 5, 2024
1 parent e23a037 commit daba3e8
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 14 deletions.
38 changes: 24 additions & 14 deletions src/measure/measure_tool_brep.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ gp_Pnt computeShapeCenter(const TopoDS_Shape& shape)
const MeasureCircle circle = MeasureToolBRep::brepCircle(shape);
return circle.value.Location();
}
catch (const BRepMeasureError<ErrorCode::NotCircularEdge>&) {
catch (const IMeasureError&) {
BRepGProp::LinearProperties(shape, shapeProps);
}
}
Expand Down Expand Up @@ -276,10 +276,10 @@ MeasureCircle MeasureToolBRep::brepCircleFromGeometricEdge(const TopoDS_Edge& ed
const GCPnts_QuasiUniformAbscissa pnts(curve, 4); // More points to avoid confusion
throwErrorIf<ErrorCode::NotCircularEdge>(!pnts.IsDone() || pnts.NbPoints() < 3);
const GC_MakeCircle makeCirc(
GeomUtils::d0(curve, pnts.Parameter(1)),
GeomUtils::d0(curve, pnts.Parameter(2)),
GeomUtils::d0(curve, pnts.Parameter(3))
);
GeomUtils::d0(curve, pnts.Parameter(1)),
GeomUtils::d0(curve, pnts.Parameter(2)),
GeomUtils::d0(curve, pnts.Parameter(3))
);
throwErrorIf<ErrorCode::NotCircularEdge>(!makeCirc.IsDone());
circle = makeCirc.Value()->Circ();
}
Expand Down Expand Up @@ -312,10 +312,10 @@ MeasureCircle MeasureToolBRep::brepCircleFromPolygonEdge(const TopoDS_Edge& edge
throwErrorIf<ErrorCode::NotGeometricOrPolygonEdge>(polyline.IsNull() || polyline->NbNodes() < 7);
// Try to create a circle from 3 sample points
const GC_MakeCircle makeCirc(
polyline->Nodes().First(),
polyline->Nodes().Value(1 + polyline->NbNodes() / 3),
polyline->Nodes().Value(1 + 2 * polyline->NbNodes() / 3)
);
polyline->Nodes().First(),
polyline->Nodes().Value(1 + polyline->NbNodes() / 3),
polyline->Nodes().Value(1 + 2 * polyline->NbNodes() / 3)
);
throwErrorIf<ErrorCode::NotCircularEdge>(!makeCirc.IsDone());
const gp_Circ circle = makeCirc.Value()->Circ();

Expand Down Expand Up @@ -345,12 +345,21 @@ MeasureCircle MeasureToolBRep::brepCircle(const TopoDS_Shape& shape)
}

MeasureDistance MeasureToolBRep::brepMinDistance(
const TopoDS_Shape& shape1, const TopoDS_Shape& shape2)
const TopoDS_Shape& shape1, const TopoDS_Shape& shape2
)
{
throwErrorIf<ErrorCode::NotBRepShape>(shape1.IsNull());
throwErrorIf<ErrorCode::NotBRepShape>(shape2.IsNull());

const BRepExtrema_DistShapeShape dist(shape1, shape2);
BRepExtrema_DistShapeShape dist;
try {
dist.LoadS1(shape1);
dist.LoadS2(shape2);
dist.Perform();
} catch (...) {
throw BRepMeasureError<ErrorCode::MinDistanceFailure>();
}

throwErrorIf<ErrorCode::MinDistanceFailure>(!dist.IsDone());

MeasureDistance distResult;
Expand All @@ -362,7 +371,8 @@ MeasureDistance MeasureToolBRep::brepMinDistance(
}

MeasureDistance MeasureToolBRep::brepCenterDistance(
const TopoDS_Shape& shape1, const TopoDS_Shape& shape2)
const TopoDS_Shape& shape1, const TopoDS_Shape& shape2
)
{
throwErrorIf<ErrorCode::NotBRepShape>(shape1.IsNull());
throwErrorIf<ErrorCode::NotBRepShape>(shape2.IsNull());
Expand Down Expand Up @@ -507,8 +517,8 @@ MeasureArea MeasureToolBRep::brepArea(const TopoDS_Shape& shape)

const BRepAdaptor_Surface surface(face);
areaResult.middlePnt = surface.Value(
(surface.FirstUParameter() + surface.LastUParameter()) / 2.,
(surface.FirstVParameter() + surface.LastVParameter()) / 2.
(surface.FirstUParameter() + surface.LastUParameter()) / 2.,
(surface.FirstVParameter() + surface.LastVParameter()) / 2.
);
}
else {
Expand Down
13 changes: 13 additions & 0 deletions tests/test_measure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <BRepAdaptor_Curve.hxx>
#include <BRepBuilderAPI_MakeVertex.hxx>
#include <BRepBuilderAPI_MakeEdge.hxx>
#include <BRepBuilderAPI_MakeFace.hxx>
#include <BRepPrimAPI_MakeBox.hxx>
#include <BRepPrimAPI_MakeSphere.hxx>
#include <Geom_BSplineCurve.hxx>
Expand Down Expand Up @@ -146,6 +147,18 @@ void TestMeasure::BRepMinDistance_TwoBoxes_test()
QCOMPARE(UnitSystem::millimeters(minDist.value).value, minDist.pnt1.Distance(minDist.pnt2));
}

void TestMeasure::BRepMinDistance_TwoConfusedFaces_test()
{
const TopoDS_Face face1 = BRepBuilderAPI_MakeFace(gp_Pln(gp::XOY()));
const TopoDS_Face face2 = BRepBuilderAPI_MakeFace(gp_Pln(gp::XOY()));
try {
const MeasureDistance minDist = MeasureToolBRep::brepMinDistance(face1, face2);
QCOMPARE(minDist.value.value(), 0.);
} catch (const IMeasureError& err) {
qDebug() << QString::fromUtf8(err.message().data(), err.message().length());
}
}

void TestMeasure::BRepAngle_TwoLinesIntersect_test()
{
const TopoDS_Shape shape1 = BRepBuilderAPI_MakeEdge(gp_Lin(gp::Origin(), gp::DX()));
Expand Down
1 change: 1 addition & 0 deletions tests/test_measure.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ private slots:

void BRepMinDistance_TwoPoints_test();
void BRepMinDistance_TwoBoxes_test();
void BRepMinDistance_TwoConfusedFaces_test();

void BRepAngle_TwoLinesIntersect_test();
void BRepAngle_TwoLinesParallelError_test();
Expand Down

0 comments on commit daba3e8

Please sign in to comment.