3

我想知道如果路径上的节点变得“平滑”, Inkscape 使用什么算法(或公式)来计算控制点。

也就是说,如果我有一个包含五个节点的路径,其d属性为

M 115.85065,503.57451
  49.653441,399.52543 
  604.56143,683.48319 
  339.41126,615.97628 
  264.65997,729.11336

我将节点更改为平滑,d属性更改为

M 115.85065,503.57451 
C                     115.85065,503.57451 24.747417,422.50451
  49.653441,399.52543 192.62243,267.61777 640.56491,558.55577
  604.56143,683.48319 580.13686,768.23328 421.64047,584.07809
  339.41126,615.97628 297.27039,632.32348 264.65997,729.11336
  264.65997,729.11336

显然,Inkscape 计算控制点坐标(在 或之后的行上的倒数第二和最后一个坐标对C)。我对 Inkscape 使用的算法很感兴趣。

4

2 回答 2

5

我在 Inkscape 的源代码树中找到了相应的代码 src/ui/tool/node.cpp,方法Node::_updateAutoHandles如下:

void Node::_updateAutoHandles()
{

    // Recompute the position of automatic handles.
    // For endnodes, retract both handles. (It's only possible to create an end auto node
    // through the XML editor.)
    if (isEndNode()) {
        _front.retract();
        _back.retract();
        return;
    }

    // Auto nodes automaticaly adjust their handles to give an appearance of smoothness,
    // no matter what their surroundings are.
    Geom::Point vec_next = _next()->position() - position();
    Geom::Point vec_prev = _prev()->position() - position();
    double len_next = vec_next.length(), len_prev = vec_prev.length();
    if (len_next > 0 && len_prev > 0) {
        // "dir" is an unit vector perpendicular to the bisector of the angle created
        // by the previous node, this auto node and the next node.
        Geom::Point dir = Geom::unit_vector((len_prev / len_next) * vec_next - vec_prev);
        // Handle lengths are equal to 1/3 of the distance from the adjacent node.
        _back.setRelativePos(-dir * (len_prev / 3));
        _front.setRelativePos(dir * (len_next / 3));
    } else {
        // If any of the adjacent nodes coincides, retract both handles.
        _front.retract();
        _back.retract();
    }
}
于 2012-11-16T22:10:35.757 回答
0

我不能 100% 确定这些信息的质量。但至少在某个时间点计算某些曲线,inkscape 似乎使用了 >>spiro<<。

http://www.levien.com/spiro/

快速浏览一下这个页面,他提供了一个指向他的博士论文的链接:http: //www.levien.com/phd/thesis.pdf ,他在其中介绍了理论/算法......

干杯

编辑:

我目前正在出于类似目的对此事进行一些调查,所以我偶然发现了... http://www.w3.org/TR/SVG11/paths.html#PathDataCurveCommands ... SVG 曲线的规范. 所以曲线,不像圆或弧,是三次或二次贝塞尔曲线......看看维基百科的贝塞尔公式: http ://en.wikipedia.org/wiki/B-spline#Uniform_quadratic_B-spline

于 2012-11-13T15:12:26.510 回答