3 * This file is a part of LEMON, a generic C++ optimization library
5 * Copyright (C) 2003-2006
6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
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.
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
19 #ifndef LEMON_BEZIER_H
20 #define LEMON_BEZIER_H
24 ///\brief Classes to compute with Bezier curves.
26 ///Up to now this file is used internally by \ref graph_to_eps.h
28 ///\author Alpar Juttner
36 typedef xy<double> xy;
38 static xy conv(xy x,xy y,double t) {return (1-t)*x+t*y;}
41 class Bezier1 : public BezierBase
47 Bezier1(xy _p1, xy _p2) :p1(_p1), p2(_p2) {}
49 xy operator()(double t) const
51 // return conv(conv(p1,p2,t),conv(p2,p3,t),t);
54 Bezier1 before(double t) const
56 return Bezier1(p1,conv(p1,p2,t));
59 Bezier1 after(double t) const
61 return Bezier1(conv(p1,p2,t),p2);
64 Bezier1 revert() const { return Bezier1(p2,p1);}
65 Bezier1 operator()(double a,double b) const { return before(b).after(a/b); }
66 xy grad() const { return p2-p1; }
67 xy norm() const { return rot90(p2-p1); }
68 xy grad(double) const { return grad(); }
69 xy norm(double t) const { return rot90(grad(t)); }
72 class Bezier2 : public BezierBase
78 Bezier2(xy _p1, xy _p2, xy _p3) :p1(_p1), p2(_p2), p3(_p3) {}
79 Bezier2(const Bezier1 &b) : p1(b.p1), p2(conv(b.p1,b.p2,.5)), p3(b.p2) {}
80 xy operator()(double t) const
82 // return conv(conv(p1,p2,t),conv(p2,p3,t),t);
83 return ((1-t)*(1-t))*p1+(2*(1-t)*t)*p2+(t*t)*p3;
85 Bezier2 before(double t) const
89 return Bezier2(p1,q,conv(q,r,t));
92 Bezier2 after(double t) const
96 return Bezier2(conv(q,r,t),r,p3);
98 Bezier2 revert() const { return Bezier2(p3,p2,p1);}
99 Bezier2 operator()(double a,double b) const { return before(b).after(a/b); }
100 Bezier1 grad() const { return Bezier1(2.0*(p2-p1),2.0*(p3-p2)); }
101 Bezier1 norm() const { return Bezier1(2.0*rot90(p2-p1),2.0*rot90(p3-p2)); }
102 xy grad(double t) const { return grad()(t); }
103 xy norm(double t) const { return rot90(grad(t)); }
106 class Bezier3 : public BezierBase
112 Bezier3(xy _p1, xy _p2, xy _p3, xy _p4) :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)),
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)),
116 p3(conv(b.p2,b.p3,1.0/3.0)), p4(b.p3) {}
118 xy operator()(double t) const
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+
122 (3*t*t*(1-t))*p3+(t*t*t)*p4;
124 Bezier3 before(double t) const
132 return Bezier3(p1,p,a,c);
135 Bezier3 after(double t) const
143 return Bezier3(c,b,r,p4);
145 Bezier3 revert() const { return Bezier3(p4,p3,p2,p1);}
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)); }
148 Bezier2 norm() const { return Bezier2(3.0*rot90(p2-p1),
151 xy grad(double t) const { return grad()(t); }
152 xy norm(double t) const { return rot90(grad(t)); }
154 template<class R,class F,class S,class D>
155 R recSplit(F &_f,const S &_s,D _d) const
157 const xy a=(p1+p2)/2;
158 const xy b=(p2+p3)/2;
159 const xy c=(p3+p4)/2;
163 R f1=_f(Bezier3(p1,a,d,e),_d);
164 R f2=_f(Bezier3(e,d,c,p4),_d);
170 } //END OF NAMESPACE LEMON
172 #endif // LEMON_BEZIER_H