float WaterElevationLayer::WaterElevationCurveData::getCapLength(NodePtr p, vec2d q)
{
    vec2d o = p->getPos();
    float capLength = 0;
    bool largeRiver = false;
    for (int i = 0; i < p->getCurveCount(); ++i) {
        CurvePtr ipath = p->getCurve(i);
        if ((ipath->getAncestor()->getId() == id) == false) {
            if (ipath->getType() == LAKE && ipath->getWidth() > 12) {
                largeRiver = true;
            }
            vec2d r = ipath->getXY(p, 1);
            if (abs(angle(q - o, r - o) - M_PI) < 0.01) {
                continue;
            }
            float pw = flattenCurve->getType() == RIVER ? 2 * flattenCurve->getWidth() : flattenCurve->getWidth();
            float ipw = ipath->getType() == RIVER ? 2 * ipath->getWidth() : ipath->getWidth();
            vec2d corner = proland::corner(o, q, r, (double) pw, (double) ipw);
            float dot = (q - o).dot(corner - o);
            capLength = max((double) capLength, dot / (o - q).length());
        }
    }
    if (largeRiver && flattenCurve->getType() == RIVER) {
        capLength = (q - o).length();
    }
    return ceil(capLength);
}