alpar@209: /* -*- mode: C++; indent-tabs-mode: nil; -*-
alpar@128:  *
alpar@209:  * This file is a part of LEMON, a generic C++ optimization library.
alpar@128:  *
alpar@1092:  * Copyright (C) 2003-2013
alpar@128:  * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@128:  * (Egervary Research Group on Combinatorial Optimization, EGRES).
alpar@128:  *
alpar@128:  * Permission to use, modify and distribute this software is granted
alpar@128:  * provided that this copyright notice appears in all copies. For
alpar@128:  * precise terms see the accompanying LICENSE file.
alpar@128:  *
alpar@128:  * This software is provided "AS IS" with no warranty of any kind,
alpar@128:  * express or implied, and with no claim as to its suitability for any
alpar@128:  * purpose.
alpar@128:  *
alpar@128:  */
alpar@128: 
alpar@128: #ifndef LEMON_BEZIER_H
alpar@128: #define LEMON_BEZIER_H
alpar@128: 
kpeter@314: //\ingroup misc
kpeter@314: //\file
kpeter@314: //\brief Classes to compute with Bezier curves.
kpeter@314: //
kpeter@314: //Up to now this file is used internally by \ref graph_to_eps.h
alpar@128: 
alpar@128: #include<lemon/dim2.h>
alpar@128: 
alpar@128: namespace lemon {
alpar@128:   namespace dim2 {
alpar@128: 
alpar@128: class BezierBase {
alpar@128: public:
alpar@184:   typedef lemon::dim2::Point<double> Point;
alpar@128: protected:
alpar@128:   static Point conv(Point x,Point y,double t) {return (1-t)*x+t*y;}
alpar@128: };
alpar@128: 
alpar@128: class Bezier1 : public BezierBase
alpar@128: {
alpar@128: public:
alpar@128:   Point p1,p2;
alpar@128: 
alpar@128:   Bezier1() {}
alpar@128:   Bezier1(Point _p1, Point _p2) :p1(_p1), p2(_p2) {}
alpar@209: 
alpar@128:   Point operator()(double t) const
alpar@128:   {
alpar@128:     //    return conv(conv(p1,p2,t),conv(p2,p3,t),t);
alpar@128:     return conv(p1,p2,t);
alpar@128:   }
alpar@128:   Bezier1 before(double t) const
alpar@128:   {
alpar@128:     return Bezier1(p1,conv(p1,p2,t));
alpar@128:   }
alpar@209: 
alpar@128:   Bezier1 after(double t) const
alpar@128:   {
alpar@128:     return Bezier1(conv(p1,p2,t),p2);
alpar@128:   }
alpar@128: 
alpar@128:   Bezier1 revert() const { return Bezier1(p2,p1);}
alpar@128:   Bezier1 operator()(double a,double b) const { return before(b).after(a/b); }
alpar@128:   Point grad() const { return p2-p1; }
alpar@128:   Point norm() const { return rot90(p2-p1); }
alpar@128:   Point grad(double) const { return grad(); }
alpar@128:   Point norm(double t) const { return rot90(grad(t)); }
alpar@128: };
alpar@128: 
alpar@128: class Bezier2 : public BezierBase
alpar@128: {
alpar@128: public:
alpar@128:   Point p1,p2,p3;
alpar@128: 
alpar@128:   Bezier2() {}
alpar@128:   Bezier2(Point _p1, Point _p2, Point _p3) :p1(_p1), p2(_p2), p3(_p3) {}
alpar@128:   Bezier2(const Bezier1 &b) : p1(b.p1), p2(conv(b.p1,b.p2,.5)), p3(b.p2) {}
alpar@128:   Point operator()(double t) const
alpar@128:   {
alpar@128:     //    return conv(conv(p1,p2,t),conv(p2,p3,t),t);
alpar@128:     return ((1-t)*(1-t))*p1+(2*(1-t)*t)*p2+(t*t)*p3;
alpar@128:   }
alpar@128:   Bezier2 before(double t) const
alpar@128:   {
alpar@128:     Point q(conv(p1,p2,t));
alpar@128:     Point r(conv(p2,p3,t));
alpar@128:     return Bezier2(p1,q,conv(q,r,t));
alpar@128:   }
alpar@209: 
alpar@128:   Bezier2 after(double t) const
alpar@128:   {
alpar@128:     Point q(conv(p1,p2,t));
alpar@128:     Point r(conv(p2,p3,t));
alpar@128:     return Bezier2(conv(q,r,t),r,p3);
alpar@128:   }
alpar@128:   Bezier2 revert() const { return Bezier2(p3,p2,p1);}
alpar@128:   Bezier2 operator()(double a,double b) const { return before(b).after(a/b); }
alpar@128:   Bezier1 grad() const { return Bezier1(2.0*(p2-p1),2.0*(p3-p2)); }
alpar@128:   Bezier1 norm() const { return Bezier1(2.0*rot90(p2-p1),2.0*rot90(p3-p2)); }
alpar@128:   Point grad(double t) const { return grad()(t); }
alpar@128:   Point norm(double t) const { return rot90(grad(t)); }
alpar@128: };
alpar@128: 
alpar@128: class Bezier3 : public BezierBase
alpar@128: {
alpar@128: public:
alpar@128:   Point p1,p2,p3,p4;
alpar@128: 
alpar@128:   Bezier3() {}
alpar@128:   Bezier3(Point _p1, Point _p2, Point _p3, Point _p4)
alpar@128:     : p1(_p1), p2(_p2), p3(_p3), p4(_p4) {}
alpar@209:   Bezier3(const Bezier1 &b) : p1(b.p1), p2(conv(b.p1,b.p2,1.0/3.0)),
alpar@209:                               p3(conv(b.p1,b.p2,2.0/3.0)), p4(b.p2) {}
alpar@128:   Bezier3(const Bezier2 &b) : p1(b.p1), p2(conv(b.p1,b.p2,2.0/3.0)),
alpar@209:                               p3(conv(b.p2,b.p3,1.0/3.0)), p4(b.p3) {}
alpar@209: 
alpar@209:   Point operator()(double t) const
alpar@128:     {
alpar@128:       //    return Bezier2(conv(p1,p2,t),conv(p2,p3,t),conv(p3,p4,t))(t);
alpar@128:       return ((1-t)*(1-t)*(1-t))*p1+(3*t*(1-t)*(1-t))*p2+
alpar@209:         (3*t*t*(1-t))*p3+(t*t*t)*p4;
alpar@128:     }
alpar@128:   Bezier3 before(double t) const
alpar@128:     {
alpar@128:       Point p(conv(p1,p2,t));
alpar@128:       Point q(conv(p2,p3,t));
alpar@128:       Point r(conv(p3,p4,t));
alpar@128:       Point a(conv(p,q,t));
alpar@128:       Point b(conv(q,r,t));
alpar@128:       Point c(conv(a,b,t));
alpar@128:       return Bezier3(p1,p,a,c);
alpar@128:     }
alpar@209: 
alpar@128:   Bezier3 after(double t) const
alpar@128:     {
alpar@128:       Point p(conv(p1,p2,t));
alpar@128:       Point q(conv(p2,p3,t));
alpar@128:       Point r(conv(p3,p4,t));
alpar@128:       Point a(conv(p,q,t));
alpar@128:       Point b(conv(q,r,t));
alpar@128:       Point c(conv(a,b,t));
alpar@128:       return Bezier3(c,b,r,p4);
alpar@128:     }
alpar@128:   Bezier3 revert() const { return Bezier3(p4,p3,p2,p1);}
alpar@128:   Bezier3 operator()(double a,double b) const { return before(b).after(a/b); }
alpar@128:   Bezier2 grad() const { return Bezier2(3.0*(p2-p1),3.0*(p3-p2),3.0*(p4-p3)); }
alpar@128:   Bezier2 norm() const { return Bezier2(3.0*rot90(p2-p1),
alpar@209:                                   3.0*rot90(p3-p2),
alpar@209:                                   3.0*rot90(p4-p3)); }
alpar@128:   Point grad(double t) const { return grad()(t); }
alpar@128:   Point norm(double t) const { return rot90(grad(t)); }
alpar@128: 
alpar@128:   template<class R,class F,class S,class D>
alpar@209:   R recSplit(F &_f,const S &_s,D _d) const
alpar@128:   {
alpar@128:     const Point a=(p1+p2)/2;
alpar@128:     const Point b=(p2+p3)/2;
alpar@128:     const Point c=(p3+p4)/2;
alpar@128:     const Point d=(a+b)/2;
alpar@128:     const Point e=(b+c)/2;
alpar@997:     // const Point f=(d+e)/2;
alpar@128:     R f1=_f(Bezier3(p1,a,d,e),_d);
alpar@128:     R f2=_f(Bezier3(e,d,c,p4),_d);
alpar@128:     return _s(f1,f2);
alpar@128:   }
alpar@209: 
alpar@128: };
alpar@128: 
alpar@128: 
alpar@128: } //END OF NAMESPACE dim2
alpar@128: } //END OF NAMESPACE lemon
alpar@128: 
alpar@128: #endif // LEMON_BEZIER_H