You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

GEOS(JTS Topology Suite) OffsetCurve生成首尾额外点问题排查

GEOS线串偏移曲线生成异常说明

我使用GEOS(JTS Topology Suite的C语言移植版本)生成线串(linestring)的偏移曲线。

我已成功生成偏移曲线,但部分场景下会出现异常:当线串首尾段均为水平且终点x坐标相同、或均为垂直且终点y坐标相同时,偏移结果的首尾位置会额外生成多余点。

通过图片可以更直观说明该问题:
示例图1
示例图2

我暂无法确认是自身操作存在遗漏还是库本身存在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);
}

内容的提问来源于stack exchange,提问作者Max Peglar-Willis

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.25 14:09:03