How to check if two gp_Pnt coincide in OpenCASCADE
If you have two points in OpenCASCADE:
gp_Pnt p1 = /* ... */;
gp_Pnt p2 = /* ... */;
you can’t just use ==
to check if they coincide in stock OpenCASCADE. However, OCCUtils provides operator== and operator!= for gp_Pnt among other types:
#include <occutils/Equality.hxx>
bool areTheyCoincident = p1 == p2;
If you can’t use OCCUtils, this is the way to check if they are coincident:
if (p1.Distance(p2) <= Precision::Confusion()) {
// p1 coincides with p2
}
or alternatively:
if (p1.IsEqual(p2, Precision::Confusion())) {
// p1 coincides with p2
}
The reason for this is that even though two points may represent the same point in space, depending on how you compute their coordinates exactly, their cartesian X/Y/Z coordinates might not be exactly identical. This is always the case with floating-point arithmetic, that’s the type of math computers use. Don’t confuse floating-point arithmetic with exact arithmetic which you have learnt in school or university.
The solution for this issue is to define a very small tolerance - any objects that have a distance less than that tolerance are considered equal.
In OpenCASCADE, Precision::Confusion()
in the standard tolerance value for this.
My OCCUtils library provides