How to get midpoint/center between points in OpenCASCADE
OCCUtils provides convenience functions for getting the midpoint (also called the center) between 2 or more points:
#include <occutils/Point.hxx>
using namespace OCCUtils;
gp_Pnt p1 = /* ... */;
gp_Pnt p2 = /* ... */;
gp_Pnt midpointOfP1AndP2 = Point::Midpoint({p1, p2});
You can also call Point::Midpoint()
with a std::vector<gp_Pnt>
.
In case you can’t use *OCCUtils,*here’s the code to do it manually:
double x = 0.0, y = 0.0, z = 0.0;
for (const gp_Pnt &pnt : points) {
x += pnt.X();
y += pnt.Y();
z += pnt.Z();
}
size_t size = points.size();
gp_Pnt midpoint(x / size, y / size, z / size);