我正在使用GEOS ( JTS拓扑套件的C端口)来生成一行字符串的偏移曲线。
我已经成功地产生了偏移曲线,但是对于某些情况(即起始线和结束线都是水平和结束在相同的x位置/或垂直和结束在相同的y位置),一个额外的点在偏移的开始和结束。
用图像来解释这一点是最简单的:
我无法确定是否有什么事情我失败了,或者如果这是库中的一个bug,下面是我的代码:
#include "geos_c.h"
std::vector<vec2> Geos::offsetLine(const std::vector<vec2>& points, float offset, int quadrantSegments, int joinStyle, double mitreLimit)
{
// make coord sequence from points
GEOSCoordSequence* seq = makeCoordSequence(points);
// Define line string
GEOSGeometry* lineString = GEOSGeom_createLineString(seq);
if(!lineString) return {};
// offset line
GEOSGeometry* bufferOp = GEOSOffsetCurve(lineString, offset, quadrantSegments, joinStyle, mitreLimit);
if(!bufferOp) return {};
// put coords into vector
std::vector<vec2> output = outputCoords(bufferOp, (offset < 0.0f));
// Frees memory of all as memory ownership is passed along
GEOSGeom_destroy(bufferOp);
return move(output);
}
GEOSCoordSequence* Geos::makeCoordSequence(const std::vector<vec2>& points)
{
GEOSCoordSequence* seq = GEOSCoordSeq_create(points.size(), 2);
if(!seq) return {};
for (size_t i = 0; i < points.size(); i++) {
GEOSCoordSeq_setX(seq, i, points[i].x);
GEOSCoordSeq_setY(seq, i, points[i].y);
}
return seq;
}
std::vector<vec2> Geos::outputCoords(const GEOSGeometry* points, bool reversePoints)
{
// Convert to coord sequence and draw points
const GEOSCoordSequence *coordSeq = GEOSGeom_getCoordSeq(points);
if(!coordSeq) return {};
// get number of points
int nPoints = GEOSGeomGetNumPoints(points);
if(nPoints == -1) return {};
// output onto vector to return
std::vector<vec2> output;
// build vector
for (size_t i = 0; i < (size_t)nPoints; i++) {
// points are in reverse order if negative offset
size_t index = reversePoints ? nPoints-i-1 : i;
double xCoord, yCoord;
GEOSCoordSeq_getX(coordSeq, index, &xCoord);
GEOSCoordSeq_getY(coordSeq, index, &yCoord);
output.push_back({ xCoord, yCoord });
}
return move(output);
}发布于 2022-01-21 20:47:34
Geos的最新版本现在解决了这一问题。
见此处:问题
https://stackoverflow.com/questions/70011468
复制相似问题