lemon/bits/bezier.h
author Balazs Dezso <deba@inf.elte.hu>
Tue, 22 Apr 2008 13:50:52 +0200
changeset 144 4e626dbbe408
child 157 2ccc1afc2c52
permissions -rw-r--r--
MSVC 2005 compatible path structure (ticket #87)
     1 /* -*- C++ -*-
     2  *
     3  * This file is a part of LEMON, a generic C++ optimization library
     4  *
     5  * Copyright (C) 2003-2008
     6  * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
     7  * (Egervary Research Group on Combinatorial Optimization, EGRES).
     8  *
     9  * Permission to use, modify and distribute this software is granted
    10  * provided that this copyright notice appears in all copies. For
    11  * precise terms see the accompanying LICENSE file.
    12  *
    13  * This software is provided "AS IS" with no warranty of any kind,
    14  * express or implied, and with no claim as to its suitability for any
    15  * purpose.
    16  *
    17  */
    18 
    19 #ifndef LEMON_BEZIER_H
    20 #define LEMON_BEZIER_H
    21 
    22 ///\ingroup misc
    23 ///\file
    24 ///\brief Classes to compute with Bezier curves.
    25 ///
    26 ///Up to now this file is used internally by \ref graph_to_eps.h
    27 ///
    28 ///\author Alpar Juttner
    29 
    30 #include<lemon/dim2.h>
    31 
    32 namespace lemon {
    33   namespace dim2 {
    34 
    35 class BezierBase {
    36 public:
    37   typedef Point<double> Point;
    38 protected:
    39   static Point conv(Point x,Point y,double t) {return (1-t)*x+t*y;}
    40 };
    41 
    42 class Bezier1 : public BezierBase
    43 {
    44 public:
    45   Point p1,p2;
    46 
    47   Bezier1() {}
    48   Bezier1(Point _p1, Point _p2) :p1(_p1), p2(_p2) {}
    49   
    50   Point operator()(double t) const
    51   {
    52     //    return conv(conv(p1,p2,t),conv(p2,p3,t),t);
    53     return conv(p1,p2,t);
    54   }
    55   Bezier1 before(double t) const
    56   {
    57     return Bezier1(p1,conv(p1,p2,t));
    58   }
    59   
    60   Bezier1 after(double t) const
    61   {
    62     return Bezier1(conv(p1,p2,t),p2);
    63   }
    64 
    65   Bezier1 revert() const { return Bezier1(p2,p1);}
    66   Bezier1 operator()(double a,double b) const { return before(b).after(a/b); }
    67   Point grad() const { return p2-p1; }
    68   Point norm() const { return rot90(p2-p1); }
    69   Point grad(double) const { return grad(); }
    70   Point norm(double t) const { return rot90(grad(t)); }
    71 };
    72 
    73 class Bezier2 : public BezierBase
    74 {
    75 public:
    76   Point p1,p2,p3;
    77 
    78   Bezier2() {}
    79   Bezier2(Point _p1, Point _p2, Point _p3) :p1(_p1), p2(_p2), p3(_p3) {}
    80   Bezier2(const Bezier1 &b) : p1(b.p1), p2(conv(b.p1,b.p2,.5)), p3(b.p2) {}
    81   Point operator()(double t) const
    82   {
    83     //    return conv(conv(p1,p2,t),conv(p2,p3,t),t);
    84     return ((1-t)*(1-t))*p1+(2*(1-t)*t)*p2+(t*t)*p3;
    85   }
    86   Bezier2 before(double t) const
    87   {
    88     Point q(conv(p1,p2,t));
    89     Point r(conv(p2,p3,t));
    90     return Bezier2(p1,q,conv(q,r,t));
    91   }
    92   
    93   Bezier2 after(double t) const
    94   {
    95     Point q(conv(p1,p2,t));
    96     Point r(conv(p2,p3,t));
    97     return Bezier2(conv(q,r,t),r,p3);
    98   }
    99   Bezier2 revert() const { return Bezier2(p3,p2,p1);}
   100   Bezier2 operator()(double a,double b) const { return before(b).after(a/b); }
   101   Bezier1 grad() const { return Bezier1(2.0*(p2-p1),2.0*(p3-p2)); }
   102   Bezier1 norm() const { return Bezier1(2.0*rot90(p2-p1),2.0*rot90(p3-p2)); }
   103   Point grad(double t) const { return grad()(t); }
   104   Point norm(double t) const { return rot90(grad(t)); }
   105 };
   106 
   107 class Bezier3 : public BezierBase
   108 {
   109 public:
   110   Point p1,p2,p3,p4;
   111 
   112   Bezier3() {}
   113   Bezier3(Point _p1, Point _p2, Point _p3, Point _p4)
   114     : p1(_p1), p2(_p2), p3(_p3), p4(_p4) {}
   115   Bezier3(const Bezier1 &b) : p1(b.p1), p2(conv(b.p1,b.p2,1.0/3.0)), 
   116 			      p3(conv(b.p1,b.p2,2.0/3.0)), p4(b.p2) {}
   117   Bezier3(const Bezier2 &b) : p1(b.p1), p2(conv(b.p1,b.p2,2.0/3.0)),
   118 			      p3(conv(b.p2,b.p3,1.0/3.0)), p4(b.p3) {}
   119   
   120   Point operator()(double t) const 
   121     {
   122       //    return Bezier2(conv(p1,p2,t),conv(p2,p3,t),conv(p3,p4,t))(t);
   123       return ((1-t)*(1-t)*(1-t))*p1+(3*t*(1-t)*(1-t))*p2+
   124 	(3*t*t*(1-t))*p3+(t*t*t)*p4;
   125     }
   126   Bezier3 before(double t) const
   127     {
   128       Point p(conv(p1,p2,t));
   129       Point q(conv(p2,p3,t));
   130       Point r(conv(p3,p4,t));
   131       Point a(conv(p,q,t));
   132       Point b(conv(q,r,t));
   133       Point c(conv(a,b,t));
   134       return Bezier3(p1,p,a,c);
   135     }
   136   
   137   Bezier3 after(double t) const
   138     {
   139       Point p(conv(p1,p2,t));
   140       Point q(conv(p2,p3,t));
   141       Point r(conv(p3,p4,t));
   142       Point a(conv(p,q,t));
   143       Point b(conv(q,r,t));
   144       Point c(conv(a,b,t));
   145       return Bezier3(c,b,r,p4);
   146     }
   147   Bezier3 revert() const { return Bezier3(p4,p3,p2,p1);}
   148   Bezier3 operator()(double a,double b) const { return before(b).after(a/b); }
   149   Bezier2 grad() const { return Bezier2(3.0*(p2-p1),3.0*(p3-p2),3.0*(p4-p3)); }
   150   Bezier2 norm() const { return Bezier2(3.0*rot90(p2-p1),
   151 				  3.0*rot90(p3-p2),
   152 				  3.0*rot90(p4-p3)); }
   153   Point grad(double t) const { return grad()(t); }
   154   Point norm(double t) const { return rot90(grad(t)); }
   155 
   156   template<class R,class F,class S,class D>
   157   R recSplit(F &_f,const S &_s,D _d) const 
   158   {
   159     const Point a=(p1+p2)/2;
   160     const Point b=(p2+p3)/2;
   161     const Point c=(p3+p4)/2;
   162     const Point d=(a+b)/2;
   163     const Point e=(b+c)/2;
   164     const Point f=(d+e)/2;
   165     R f1=_f(Bezier3(p1,a,d,e),_d);
   166     R f2=_f(Bezier3(e,d,c,p4),_d);
   167     return _s(f1,f2);
   168   }
   169   
   170 };
   171 
   172 
   173 } //END OF NAMESPACE dim2
   174 } //END OF NAMESPACE lemon
   175 
   176 #endif // LEMON_BEZIER_H