alpar@1073: /* -*- C++ -*-
alpar@1073:  * src/lemon/bezier.h - Part of LEMON, a generic C++ optimization library
alpar@1073:  *
alpar@1164:  * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@1073:  * (Egervary Combinatorial Optimization Research Group, EGRES).
alpar@1073:  *
alpar@1073:  * Permission to use, modify and distribute this software is granted
alpar@1073:  * provided that this copyright notice appears in all copies. For
alpar@1073:  * precise terms see the accompanying LICENSE file.
alpar@1073:  *
alpar@1073:  * This software is provided "AS IS" with no warranty of any kind,
alpar@1073:  * express or implied, and with no claim as to its suitability for any
alpar@1073:  * purpose.
alpar@1073:  *
alpar@1073:  */
alpar@1073: 
alpar@1073: #ifndef LEMON_BEZIER_H
alpar@1073: #define LEMON_BEZIER_H
alpar@1073: 
alpar@1073: ///\ingroup misc
alpar@1073: ///\file
alpar@1073: ///\brief Classes to compute with Bezier curves.
alpar@1073: ///
alpar@1084: ///Up to now this file is used internally by \ref graph_to_eps.h
alpar@1073: ///
alpar@1073: ///\author Alpar Juttner
alpar@1073: 
alpar@1073: #include<lemon/xy.h>
alpar@1073: 
alpar@1073: namespace lemon {
alpar@1073: 
alpar@1073: class BezierBase {
alpar@1073: public:
alpar@1073:   typedef xy<double> xy;
alpar@1073: protected:
alpar@1073:   static xy conv(xy x,xy y,double t) {return (1-t)*x+t*y;}
alpar@1073: };
alpar@1073: 
alpar@1073: class Bezier1 : public BezierBase
alpar@1073: {
alpar@1073: public:
alpar@1073:   xy p1,p2;
alpar@1073: 
alpar@1073:   Bezier1() {}
alpar@1073:   Bezier1(xy _p1, xy _p2) :p1(_p1), p2(_p2) {}
alpar@1073:   
alpar@1073:   xy operator()(double t) const
alpar@1073:   {
alpar@1073:     //    return conv(conv(p1,p2,t),conv(p2,p3,t),t);
alpar@1073:     return conv(p1,p2,t);
alpar@1073:   }
alpar@1073:   Bezier1 before(double t) const
alpar@1073:   {
alpar@1073:     return Bezier1(p1,conv(p1,p2,t));
alpar@1073:   }
alpar@1073:   
alpar@1073:   Bezier1 after(double t) const
alpar@1073:   {
alpar@1073:     return Bezier1(conv(p1,p2,t),p2);
alpar@1073:   }
alpar@1084:   Bezier1 revert() { return Bezier1(p2,p1);}
alpar@1084:   Bezier1 operator()(double a,double b) { return before(b).after(a/b); }
alpar@1084:   xy grad() { return p2-p1; }
alpar@1084:   xy grad(double t) { return grad(); }
alpar@1084: 
alpar@1073: };
alpar@1073: 
alpar@1073: class Bezier2 : public BezierBase
alpar@1073: {
alpar@1073: public:
alpar@1073:   xy p1,p2,p3;
alpar@1073: 
alpar@1073:   Bezier2() {}
alpar@1073:   Bezier2(xy _p1, xy _p2, xy _p3) :p1(_p1), p2(_p2), p3(_p3) {}
alpar@1073:   Bezier2(const Bezier1 &b) : p1(b.p1), p2(conv(b.p1,b.p2,.5)), p3(b.p2) {}
alpar@1073:   xy operator()(double t) const
alpar@1073:   {
alpar@1073:     //    return conv(conv(p1,p2,t),conv(p2,p3,t),t);
alpar@1073:     return ((1-t)*(1-t))*p1+(2*(1-t)*t)*p2+(t*t)*p3;
alpar@1073:   }
alpar@1073:   Bezier2 before(double t) const
alpar@1073:   {
alpar@1073:     xy q(conv(p1,p2,t));
alpar@1073:     xy r(conv(p2,p3,t));
alpar@1073:     return Bezier2(p1,q,conv(q,r,t));
alpar@1073:   }
alpar@1073:   
alpar@1073:   Bezier2 after(double t) const
alpar@1073:   {
alpar@1073:     xy q(conv(p1,p2,t));
alpar@1073:     xy r(conv(p2,p3,t));
alpar@1073:     return Bezier2(conv(q,r,t),r,p3);
alpar@1073:   }
alpar@1084:   Bezier2 revert() { return Bezier2(p3,p2,p1);}
alpar@1073:   Bezier2 operator()(double a,double b) { return before(b).after(a/b); }
alpar@1084:   Bezier1 grad() { return Bezier1(2.0*(p2-p1),2.0*(p3-p2)); }
alpar@1084:   xy grad(double t) { return grad()(t); }  
alpar@1073: };
alpar@1073: 
alpar@1073: class Bezier3 : public BezierBase
alpar@1073: {
alpar@1073: public:
alpar@1073:   xy p1,p2,p3,p4;
alpar@1073: 
alpar@1073:   Bezier3() {}
alpar@1073:   Bezier3(xy _p1, xy _p2, xy _p3, xy _p4) :p1(_p1), p2(_p2), p3(_p3), p4(_p4) {}
alpar@1073:   Bezier3(const Bezier1 &b) : p1(b.p1), p2(conv(b.p1,b.p2,1.0/3.0)), 
alpar@1073: 			      p3(conv(b.p1,b.p2,2.0/3.0)), p4(b.p2) {}
alpar@1073:   Bezier3(const Bezier2 &b) : p1(b.p1), p2(conv(b.p1,b.p2,2.0/3.0)),
alpar@1073: 			      p3(conv(b.p2,b.p3,1.0/3.0)), p4(b.p3) {}
alpar@1073:   
alpar@1073:   xy operator()(double t) const 
alpar@1073:     {
alpar@1073:       //    return Bezier2(conv(p1,p2,t),conv(p2,p3,t),conv(p3,p4,t))(t);
alpar@1073:       return ((1-t)*(1-t)*(1-t))*p1+(3*t*(1-t)*(1-t))*p2+
alpar@1073: 	(3*t*t*(1-t))*p3+(t*t*t)*p4;
alpar@1073:     }
alpar@1073:   Bezier3 before(double t) const
alpar@1073:     {
alpar@1073:       xy p(conv(p1,p2,t));
alpar@1073:       xy q(conv(p2,p3,t));
alpar@1073:       xy r(conv(p3,p4,t));
alpar@1073:       xy a(conv(p,q,t));
alpar@1073:       xy b(conv(q,r,t));
alpar@1073:       xy c(conv(a,b,t));
alpar@1073:       return Bezier3(p1,p,a,c);
alpar@1073:     }
alpar@1073:   
alpar@1073:   Bezier3 after(double t) const
alpar@1073:     {
alpar@1073:       xy p(conv(p1,p2,t));
alpar@1073:       xy q(conv(p2,p3,t));
alpar@1073:       xy r(conv(p3,p4,t));
alpar@1073:       xy a(conv(p,q,t));
alpar@1073:       xy b(conv(q,r,t));
alpar@1073:       xy c(conv(a,b,t));
alpar@1073:       return Bezier3(c,b,r,p4);
alpar@1073:     }
alpar@1084:   Bezier3 revert() { return Bezier3(p4,p3,p2,p1);}
alpar@1073:   Bezier3 operator()(double a,double b) { return before(b).after(a/b); }
alpar@1084:   Bezier2 grad() { return Bezier2(3.0*(p2-p1),3.0*(p3-p2),3.0*(p4-p3)); }
alpar@1084:   xy grad(double t) { return grad()(t); }
alpar@1073: };
alpar@1073: 
alpar@1073: } //END OF NAMESPACE LEMON
alpar@1073: 
alpar@1073: #endif // LEMON_BEZIER_H