近日遇到了求取二维多边形中心点(形心)的需求,虽然自己写代码也能实现,但是我认为能用开源库实现的功能就不重复做工,所以还是选择用已有开源库实现,新建一个函数调用geos的接口实现该功能,函数的参数一是多边形顶点坐标(自定义结构体,请忽略此处的结构体定义),二是计算出的中心点坐标,函数代码如下:
bool calDetectResultCentroid(const st_AIDetectorResult &stDetectResult, st_2DPt &st2DPtCenter)
{
static const geos::geom::GeometryFactory* pGeomFactory = geos::geom::GeometryFactory::getDefaultInstance();
//构建点序列
geos::geom::CoordinateArraySequence geoCoordSequence;// = new geos::geom::CoordinateArraySequence();
geoCoordSequence.add(geos::geom::Coordinate(stDetectResult.fPt1X, stDetectResult.fPt1Y));
geoCoordSequence.add(geos::geom::Coordinate(stDetectResult.fPt2X, stDetectResult.fPt2Y));
geoCoordSequence.add(geos::geom::Coordinate(stDetectResult.fPt3X, stDetectResult.fPt3Y));
geoCoordSequence.add(geos::geom::Coordinate(stDetectResult.fPt4X, stDetectResult.fPt4Y));
geoCoordSequence.add(geos::geom::Coordinate(stDetectResult.fPt1X, stDetectResult.fPt1Y)); //与第一个点相等
geos::geom::LinearRing* pGeomLinearRing = pGeomFactory->createLinearRing(geoCoordSequence);
if (!pGeomLinearRing->isSimple())
{
pGeomFactory->destroyGeometry(pGeomLinearRing);
EASYLOG_PUSHLOG(ERROR_LOG_LEVEL, "LinearRing is not simple ");
return false;
}
std::unique_ptr<geos::geom::Point> pPtCenter = pGeomLinearRing->getCentroid();
st2DPtCenter.dX = pPtCenter->getX();
st2DPtCenter.dY = pPtCenter->getY();
pGeomFactory->destroyGeometry(pGeomLinearRing);
return true;
}


5883

被折叠的 条评论
为什么被折叠?



