如何防止三次Bezier曲线中的循环

时间:2015-02-27 16:47:40

标签: loops curve bezier

我有一个产生立方2D贝塞尔曲线的计算。在这种情况下,端点是固定的,我的程序计算内部控制点。大多数情况下,这些曲线在两个附近的形状之间产生简单的混合。但有时候几何形状是这样的,绘制曲线会产生一个循环,这在这个应用程序中永远不会好看。在这种情况下,我愿意修改控制点以防止循环。但要做到这一点,我需要检测给定的三次贝塞尔曲线是否会在绘制时循环。

我当然可以简单地在多个点处对曲线进行采样并寻找一个循环,但我宁愿找到基于8个变量的代数解(4个点中的每一个的x和y值)。理想情况下,该解决方案不仅会告诉我是否存在循环,还会告诉我"大"循环在某种意义上说。但即使有二进制是/否答案也会有很大帮助。

有没有人知道一种算法可以确定给定的立方2D贝塞尔曲线在绘制时是否会产生一个循环?

2 个答案:

答案 0 :(得分:2)

当然可以:你可以查看它的canonical form,看看这四个点是否会产生曲线"结束"在必须存在循环的区域。

1989年的论文"A Geometric Characterization of Parametric Cubic curves"

概述了这方面的原始理论

答案 1 :(得分:0)

首先是代码,稍后会有一些解释。该过程有点耗时,因此代码略微优化。现在它类似于一个敏锐的选择,很少有候选人通过所有测试。

type
  myType=Integer; {remember to change here to your own type, 
    an integers are good for an educational purpose}
  point_2d=record    xx,yy :myType  end;
  vector_2d=record    vx,vy :myType  end;    

function bezier_has_a_loop(p0,p1,p2,p3:point_2d):boolean;
              {-------------------------}
              function f_vec_from_2_pts(pa,pb:point_2d):vector_2d;
              begin
                with Result do begin
                  vx:=pb.xx - pa.xx;
                  vy:=pb.yy - pa.yy     end  end;
              {-------------------------}
              function f_cross_prod(va,vb : vector_2d):myType;
              begin
                 Result := va.vx*vb.vy - va.vy*vb.vx      end;
              {-------------------------}
              function f_mult(m:myType; v:vector_2d):vector_2d;
              begin
                with Result do begin
                  vx := m*v.vx;
                  vy := m*v.vy   end   end;
              {-------------------------}
              function f_sum(va,vb : vector_2d):vector_2d;
              begin
                with Result do begin
                  vx := va.vx+vb.vx;
                  vy := va.vy+vb.vy    end   end;
              {-------------------------}
              function f_rotate(v, rotor : vector_2d):vector_2d;
              var
                m_sin,m_cos:myType; {only for a readability purpose}
              begin
                m_cos:=rotor.vx {
                /sqrt(sqr(rotor.xx)+sqr(rotor.yy))  - unnecessary };
                m_sin:=rotor.vy {
                /sqrt(sqr(rotor.xx)+sqr(rotor.yy))  - unnecessary };
                with Result do begin
                  vx := -v.vx * m_cos - v.vy *m_sin;
                  vy :=  v.vx * m_sin - v.vy *m_cos   end   end;
              {-------------------------}
var
  a,b,c, c1,c2,c3  :vector_2d;
  ab,ac,bc:myType;
  bb,s1,s2,delta,shift,t_1_2 : Double;
begin

  a:=f_vec_from_2_pts(p0,p1);
  b:=f_vec_from_2_pts(p1,p2);
  c:=f_vec_from_2_pts(p2,p3);

  ab:=f_cross_prod(a,b); {on the planar, 
    myType for a cross product is just fine}}
  ac:=f_cross_prod(a,c);
  bc:=f_cross_prod(b,c);

  {==1== No inflection point(s) or cusp allowed}
  if ac*ac >= 4*ab*bc  then begin    Result:=False; exit end;

  c3:= f_sum( f_sum(a,c)  ,  f_mult( -2,b ) );
  if c3.vy<>0 then begin   {Is the bag's handle horizontal?}
    a := f_rotate(a,c3);      
    b := f_rotate(b,c3);
    c := f_rotate(c,c3);
    c3:= f_sum  ( f_sum(a,c) , f_mult(-2,b) ); 
    {Now the handle is forced to be horizontal.}
  end;

  c1:= f_mult ( 3,a );
  c2:= f_sum  ( f_mult(3,b) , f_mult(-3,a) );

  { Following 4 restrictions comes from a single caveats for a roots:
     0<= t1<t2<=1}
  bb:= -c1.vy / c2.vy;  { always  c2.vy<>0 }

  {==2== A central point (t1+t2)/2 outside the limits}
  if  (bb<0) or  (bb>2) then begin Result:=False;  exit  end;

  s1:= c1.vx/c3.vx;    { always  c3.vx<>0 }
  s2:= c2.vx/c3.vx;
  delta := -bb*(4*s2+3*bb)-4*s1;

  {==3==  delta is to big}
  if delta>1 then begin Result:=False;  exit  end;

  shift:=sqrt(delta);   { always  delta>0 }
  t_1_2:=bb-shift;    {for readability purpose only, 
    one can omit this and write below: if shift>bb }

  {==4==  t1 is to low }
  if t_1_2<0 then begin Result:=False; exit end;
  t_1_2:=bb+shift;      { no /2 here,beacause of *2 below}

  {==5==  t2 is to high}
  if t_1_2>2 then  Result:=False
              else  Result:=True  { TA DA! Thank you for your patience. }
end;

现在有些理论。

  1. 考虑4个点P0,P1,P2,P3。这些点定义(几乎总是)立方Bézier曲线。
  2. 设置点H1,例如矢量P1_H1是矢量P0_P1的一半。
  3. 设置点H2,例如矢量P2_H2是矢量P3_P2的一半。
  4. 最后,让我们创建vetor h = H1_H2。 (我个人称这个载体为“袋子的把手”,猜猜为什么。)
  5. Bezier curve and its handle

    • 这里不足为奇,当您开始各向同性缩放或P0,P1,P2,P3的旋转时,矢量 h 将相应地变换。
    • 对于某人而言,这将是一个惊喜,向量 h = [_h_x,_h_y]与从立方体的代数形式的最高系数创建的向量[c3x,c3y]成比例Bézier曲线。比例系数为(-1/2)。

    (事实上,当点H1 = H2重合时, h 向量消失,c3x = c3y = 0,因此立方Bézier曲线至少减少为由P0创建的二次Bézier曲线,H1,P3分。)

    现在的线索:正确的旋转总是可以水平(或者真实地)转动矢量 h ,并且这种旋转会将c3y(或c3x)减少为null,但是保留了循环。反过来,人们可以将所述问题简化为二次方程的平凡根发现。 (我想这是找到解决方案的决定性提示。)

    为了测量循环,我建议从上面的代码中考虑变量 delta

       0 < delta <= 1
    When delta -> 0, the loop vanishes. 
    When delta -> 1, the loop becomes really pompous.
    

    我对这个提议不太满意,但它总比没有好。