lemon/bits/bezier.h
changeset 215 17c644f5f98d
parent 184 716b220697a0
child 314 2cc60866a0c9
equal deleted inserted replaced
2:cf01821a92c0 3:ee090d35e399
     1 /* -*- C++ -*-
     1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
     2  *
     2  *
     3  * This file is a part of LEMON, a generic C++ optimization library
     3  * This file is a part of LEMON, a generic C++ optimization library.
     4  *
     4  *
     5  * Copyright (C) 2003-2008
     5  * Copyright (C) 2003-2008
     6  * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
     6  * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
     7  * (Egervary Research Group on Combinatorial Optimization, EGRES).
     7  * (Egervary Research Group on Combinatorial Optimization, EGRES).
     8  *
     8  *
    42 public:
    42 public:
    43   Point p1,p2;
    43   Point p1,p2;
    44 
    44 
    45   Bezier1() {}
    45   Bezier1() {}
    46   Bezier1(Point _p1, Point _p2) :p1(_p1), p2(_p2) {}
    46   Bezier1(Point _p1, Point _p2) :p1(_p1), p2(_p2) {}
    47   
    47 
    48   Point operator()(double t) const
    48   Point operator()(double t) const
    49   {
    49   {
    50     //    return conv(conv(p1,p2,t),conv(p2,p3,t),t);
    50     //    return conv(conv(p1,p2,t),conv(p2,p3,t),t);
    51     return conv(p1,p2,t);
    51     return conv(p1,p2,t);
    52   }
    52   }
    53   Bezier1 before(double t) const
    53   Bezier1 before(double t) const
    54   {
    54   {
    55     return Bezier1(p1,conv(p1,p2,t));
    55     return Bezier1(p1,conv(p1,p2,t));
    56   }
    56   }
    57   
    57 
    58   Bezier1 after(double t) const
    58   Bezier1 after(double t) const
    59   {
    59   {
    60     return Bezier1(conv(p1,p2,t),p2);
    60     return Bezier1(conv(p1,p2,t),p2);
    61   }
    61   }
    62 
    62 
    85   {
    85   {
    86     Point q(conv(p1,p2,t));
    86     Point q(conv(p1,p2,t));
    87     Point r(conv(p2,p3,t));
    87     Point r(conv(p2,p3,t));
    88     return Bezier2(p1,q,conv(q,r,t));
    88     return Bezier2(p1,q,conv(q,r,t));
    89   }
    89   }
    90   
    90 
    91   Bezier2 after(double t) const
    91   Bezier2 after(double t) const
    92   {
    92   {
    93     Point q(conv(p1,p2,t));
    93     Point q(conv(p1,p2,t));
    94     Point r(conv(p2,p3,t));
    94     Point r(conv(p2,p3,t));
    95     return Bezier2(conv(q,r,t),r,p3);
    95     return Bezier2(conv(q,r,t),r,p3);
   108   Point p1,p2,p3,p4;
   108   Point p1,p2,p3,p4;
   109 
   109 
   110   Bezier3() {}
   110   Bezier3() {}
   111   Bezier3(Point _p1, Point _p2, Point _p3, Point _p4)
   111   Bezier3(Point _p1, Point _p2, Point _p3, Point _p4)
   112     : p1(_p1), p2(_p2), p3(_p3), p4(_p4) {}
   112     : p1(_p1), p2(_p2), p3(_p3), p4(_p4) {}
   113   Bezier3(const Bezier1 &b) : p1(b.p1), p2(conv(b.p1,b.p2,1.0/3.0)), 
   113   Bezier3(const Bezier1 &b) : p1(b.p1), p2(conv(b.p1,b.p2,1.0/3.0)),
   114 			      p3(conv(b.p1,b.p2,2.0/3.0)), p4(b.p2) {}
   114                               p3(conv(b.p1,b.p2,2.0/3.0)), p4(b.p2) {}
   115   Bezier3(const Bezier2 &b) : p1(b.p1), p2(conv(b.p1,b.p2,2.0/3.0)),
   115   Bezier3(const Bezier2 &b) : p1(b.p1), p2(conv(b.p1,b.p2,2.0/3.0)),
   116 			      p3(conv(b.p2,b.p3,1.0/3.0)), p4(b.p3) {}
   116                               p3(conv(b.p2,b.p3,1.0/3.0)), p4(b.p3) {}
   117   
   117 
   118   Point operator()(double t) const 
   118   Point operator()(double t) const
   119     {
   119     {
   120       //    return Bezier2(conv(p1,p2,t),conv(p2,p3,t),conv(p3,p4,t))(t);
   120       //    return Bezier2(conv(p1,p2,t),conv(p2,p3,t),conv(p3,p4,t))(t);
   121       return ((1-t)*(1-t)*(1-t))*p1+(3*t*(1-t)*(1-t))*p2+
   121       return ((1-t)*(1-t)*(1-t))*p1+(3*t*(1-t)*(1-t))*p2+
   122 	(3*t*t*(1-t))*p3+(t*t*t)*p4;
   122         (3*t*t*(1-t))*p3+(t*t*t)*p4;
   123     }
   123     }
   124   Bezier3 before(double t) const
   124   Bezier3 before(double t) const
   125     {
   125     {
   126       Point p(conv(p1,p2,t));
   126       Point p(conv(p1,p2,t));
   127       Point q(conv(p2,p3,t));
   127       Point q(conv(p2,p3,t));
   129       Point a(conv(p,q,t));
   129       Point a(conv(p,q,t));
   130       Point b(conv(q,r,t));
   130       Point b(conv(q,r,t));
   131       Point c(conv(a,b,t));
   131       Point c(conv(a,b,t));
   132       return Bezier3(p1,p,a,c);
   132       return Bezier3(p1,p,a,c);
   133     }
   133     }
   134   
   134 
   135   Bezier3 after(double t) const
   135   Bezier3 after(double t) const
   136     {
   136     {
   137       Point p(conv(p1,p2,t));
   137       Point p(conv(p1,p2,t));
   138       Point q(conv(p2,p3,t));
   138       Point q(conv(p2,p3,t));
   139       Point r(conv(p3,p4,t));
   139       Point r(conv(p3,p4,t));
   144     }
   144     }
   145   Bezier3 revert() const { return Bezier3(p4,p3,p2,p1);}
   145   Bezier3 revert() const { return Bezier3(p4,p3,p2,p1);}
   146   Bezier3 operator()(double a,double b) const { return before(b).after(a/b); }
   146   Bezier3 operator()(double a,double b) const { return before(b).after(a/b); }
   147   Bezier2 grad() const { return Bezier2(3.0*(p2-p1),3.0*(p3-p2),3.0*(p4-p3)); }
   147   Bezier2 grad() const { return Bezier2(3.0*(p2-p1),3.0*(p3-p2),3.0*(p4-p3)); }
   148   Bezier2 norm() const { return Bezier2(3.0*rot90(p2-p1),
   148   Bezier2 norm() const { return Bezier2(3.0*rot90(p2-p1),
   149 				  3.0*rot90(p3-p2),
   149                                   3.0*rot90(p3-p2),
   150 				  3.0*rot90(p4-p3)); }
   150                                   3.0*rot90(p4-p3)); }
   151   Point grad(double t) const { return grad()(t); }
   151   Point grad(double t) const { return grad()(t); }
   152   Point norm(double t) const { return rot90(grad(t)); }
   152   Point norm(double t) const { return rot90(grad(t)); }
   153 
   153 
   154   template<class R,class F,class S,class D>
   154   template<class R,class F,class S,class D>
   155   R recSplit(F &_f,const S &_s,D _d) const 
   155   R recSplit(F &_f,const S &_s,D _d) const
   156   {
   156   {
   157     const Point a=(p1+p2)/2;
   157     const Point a=(p1+p2)/2;
   158     const Point b=(p2+p3)/2;
   158     const Point b=(p2+p3)/2;
   159     const Point c=(p3+p4)/2;
   159     const Point c=(p3+p4)/2;
   160     const Point d=(a+b)/2;
   160     const Point d=(a+b)/2;
   162     const Point f=(d+e)/2;
   162     const Point f=(d+e)/2;
   163     R f1=_f(Bezier3(p1,a,d,e),_d);
   163     R f1=_f(Bezier3(p1,a,d,e),_d);
   164     R f2=_f(Bezier3(e,d,c,p4),_d);
   164     R f2=_f(Bezier3(e,d,c,p4),_d);
   165     return _s(f1,f2);
   165     return _s(f1,f2);
   166   }
   166   }
   167   
   167 
   168 };
   168 };
   169 
   169 
   170 
   170 
   171 } //END OF NAMESPACE dim2
   171 } //END OF NAMESPACE dim2
   172 } //END OF NAMESPACE lemon
   172 } //END OF NAMESPACE lemon