src/lemon/bezier.h
changeset 1073 bedab8bd915f
child 1084 320a0f083ca1
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/lemon/bezier.h	Tue Jan 11 09:15:25 2005 +0000
     1.3 @@ -0,0 +1,139 @@
     1.4 +/* -*- C++ -*-
     1.5 + * src/lemon/bezier.h - Part of LEMON, a generic C++ optimization library
     1.6 + *
     1.7 + * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
     1.8 + * (Egervary Combinatorial Optimization Research Group, EGRES).
     1.9 + *
    1.10 + * Permission to use, modify and distribute this software is granted
    1.11 + * provided that this copyright notice appears in all copies. For
    1.12 + * precise terms see the accompanying LICENSE file.
    1.13 + *
    1.14 + * This software is provided "AS IS" with no warranty of any kind,
    1.15 + * express or implied, and with no claim as to its suitability for any
    1.16 + * purpose.
    1.17 + *
    1.18 + */
    1.19 +
    1.20 +#ifndef LEMON_BEZIER_H
    1.21 +#define LEMON_BEZIER_H
    1.22 +
    1.23 +///\ingroup misc
    1.24 +///\file
    1.25 +///\brief Classes to compute with Bezier curves.
    1.26 +///
    1.27 +///Up to now this file is internally used by \ref graph_to_eps.h
    1.28 +///
    1.29 +///\author Alpar Juttner
    1.30 +
    1.31 +#include<lemon/xy.h>
    1.32 +
    1.33 +namespace lemon {
    1.34 +
    1.35 +class BezierBase {
    1.36 +public:
    1.37 +  typedef xy<double> xy;
    1.38 +protected:
    1.39 +  static xy conv(xy x,xy y,double t) {return (1-t)*x+t*y;}
    1.40 +};
    1.41 +
    1.42 +class Bezier1 : public BezierBase
    1.43 +{
    1.44 +public:
    1.45 +  xy p1,p2;
    1.46 +
    1.47 +  Bezier1() {}
    1.48 +  Bezier1(xy _p1, xy _p2) :p1(_p1), p2(_p2) {}
    1.49 +  
    1.50 +  xy operator()(double t) const
    1.51 +  {
    1.52 +    //    return conv(conv(p1,p2,t),conv(p2,p3,t),t);
    1.53 +    return conv(p1,p2,t);
    1.54 +  }
    1.55 +  Bezier1 before(double t) const
    1.56 +  {
    1.57 +    return Bezier1(p1,conv(p1,p2,t));
    1.58 +  }
    1.59 +  
    1.60 +  Bezier1 after(double t) const
    1.61 +  {
    1.62 +    return Bezier1(conv(p1,p2,t),p2);
    1.63 +  }
    1.64 +  Bezier1 operator()(double a,double b) { return before(b).after(a/b); }  
    1.65 +};
    1.66 +
    1.67 +class Bezier2 : public BezierBase
    1.68 +{
    1.69 +public:
    1.70 +  xy p1,p2,p3;
    1.71 +
    1.72 +  Bezier2() {}
    1.73 +  Bezier2(xy _p1, xy _p2, xy _p3) :p1(_p1), p2(_p2), p3(_p3) {}
    1.74 +  Bezier2(const Bezier1 &b) : p1(b.p1), p2(conv(b.p1,b.p2,.5)), p3(b.p2) {}
    1.75 +  xy operator()(double t) const
    1.76 +  {
    1.77 +    //    return conv(conv(p1,p2,t),conv(p2,p3,t),t);
    1.78 +    return ((1-t)*(1-t))*p1+(2*(1-t)*t)*p2+(t*t)*p3;
    1.79 +  }
    1.80 +  Bezier2 before(double t) const
    1.81 +  {
    1.82 +    xy q(conv(p1,p2,t));
    1.83 +    xy r(conv(p2,p3,t));
    1.84 +    return Bezier2(p1,q,conv(q,r,t));
    1.85 +  }
    1.86 +  
    1.87 +  Bezier2 after(double t) const
    1.88 +  {
    1.89 +    xy q(conv(p1,p2,t));
    1.90 +    xy r(conv(p2,p3,t));
    1.91 +    return Bezier2(conv(q,r,t),r,p3);
    1.92 +  }
    1.93 +  Bezier2 operator()(double a,double b) { return before(b).after(a/b); }
    1.94 +  
    1.95 +};
    1.96 +
    1.97 +class Bezier3 : public BezierBase
    1.98 +{
    1.99 +public:
   1.100 +  xy p1,p2,p3,p4;
   1.101 +
   1.102 +  Bezier3() {}
   1.103 +  Bezier3(xy _p1, xy _p2, xy _p3, xy _p4) :p1(_p1), p2(_p2), p3(_p3), p4(_p4) {}
   1.104 +  Bezier3(const Bezier1 &b) : p1(b.p1), p2(conv(b.p1,b.p2,1.0/3.0)), 
   1.105 +			      p3(conv(b.p1,b.p2,2.0/3.0)), p4(b.p2) {}
   1.106 +  Bezier3(const Bezier2 &b) : p1(b.p1), p2(conv(b.p1,b.p2,2.0/3.0)),
   1.107 +			      p3(conv(b.p2,b.p3,1.0/3.0)), p4(b.p3) {}
   1.108 +  
   1.109 +  xy operator()(double t) const 
   1.110 +    {
   1.111 +      //    return Bezier2(conv(p1,p2,t),conv(p2,p3,t),conv(p3,p4,t))(t);
   1.112 +      return ((1-t)*(1-t)*(1-t))*p1+(3*t*(1-t)*(1-t))*p2+
   1.113 +	(3*t*t*(1-t))*p3+(t*t*t)*p4;
   1.114 +    }
   1.115 +  Bezier3 before(double t) const
   1.116 +    {
   1.117 +      xy p(conv(p1,p2,t));
   1.118 +      xy q(conv(p2,p3,t));
   1.119 +      xy r(conv(p3,p4,t));
   1.120 +      xy a(conv(p,q,t));
   1.121 +      xy b(conv(q,r,t));
   1.122 +      xy c(conv(a,b,t));
   1.123 +      return Bezier3(p1,p,a,c);
   1.124 +    }
   1.125 +  
   1.126 +  Bezier3 after(double t) const
   1.127 +    {
   1.128 +      xy p(conv(p1,p2,t));
   1.129 +      xy q(conv(p2,p3,t));
   1.130 +      xy r(conv(p3,p4,t));
   1.131 +      xy a(conv(p,q,t));
   1.132 +      xy b(conv(q,r,t));
   1.133 +      xy c(conv(a,b,t));
   1.134 +      return Bezier3(c,b,r,p4);
   1.135 +    }
   1.136 +  Bezier3 operator()(double a,double b) { return before(b).after(a/b); }
   1.137 +  
   1.138 +};
   1.139 +
   1.140 +} //END OF NAMESPACE LEMON
   1.141 +
   1.142 +#endif // LEMON_BEZIER_H