1.1 --- a/lemon/bits/bezier.h Wed Sep 06 11:39:22 2006 +0000
1.2 +++ b/lemon/bits/bezier.h Thu Sep 07 13:27:16 2006 +0000
1.3 @@ -27,26 +27,27 @@
1.4 ///
1.5 ///\author Alpar Juttner
1.6
1.7 -#include<lemon/xy.h>
1.8 +#include<lemon/dim2.h>
1.9
1.10 namespace lemon {
1.11 + namespace dim2 {
1.12
1.13 class BezierBase {
1.14 public:
1.15 - typedef xy<double> xy;
1.16 + typedef Point<double> Point;
1.17 protected:
1.18 - static xy conv(xy x,xy y,double t) {return (1-t)*x+t*y;}
1.19 + static Point conv(Point x,Point y,double t) {return (1-t)*x+t*y;}
1.20 };
1.21
1.22 class Bezier1 : public BezierBase
1.23 {
1.24 public:
1.25 - xy p1,p2;
1.26 + Point p1,p2;
1.27
1.28 Bezier1() {}
1.29 - Bezier1(xy _p1, xy _p2) :p1(_p1), p2(_p2) {}
1.30 + Bezier1(Point _p1, Point _p2) :p1(_p1), p2(_p2) {}
1.31
1.32 - xy operator()(double t) const
1.33 + Point operator()(double t) const
1.34 {
1.35 // return conv(conv(p1,p2,t),conv(p2,p3,t),t);
1.36 return conv(p1,p2,t);
1.37 @@ -63,59 +64,60 @@
1.38
1.39 Bezier1 revert() const { return Bezier1(p2,p1);}
1.40 Bezier1 operator()(double a,double b) const { return before(b).after(a/b); }
1.41 - xy grad() const { return p2-p1; }
1.42 - xy norm() const { return rot90(p2-p1); }
1.43 - xy grad(double) const { return grad(); }
1.44 - xy norm(double t) const { return rot90(grad(t)); }
1.45 + Point grad() const { return p2-p1; }
1.46 + Point norm() const { return rot90(p2-p1); }
1.47 + Point grad(double) const { return grad(); }
1.48 + Point norm(double t) const { return rot90(grad(t)); }
1.49 };
1.50
1.51 class Bezier2 : public BezierBase
1.52 {
1.53 public:
1.54 - xy p1,p2,p3;
1.55 + Point p1,p2,p3;
1.56
1.57 Bezier2() {}
1.58 - Bezier2(xy _p1, xy _p2, xy _p3) :p1(_p1), p2(_p2), p3(_p3) {}
1.59 + Bezier2(Point _p1, Point _p2, Point _p3) :p1(_p1), p2(_p2), p3(_p3) {}
1.60 Bezier2(const Bezier1 &b) : p1(b.p1), p2(conv(b.p1,b.p2,.5)), p3(b.p2) {}
1.61 - xy operator()(double t) const
1.62 + Point operator()(double t) const
1.63 {
1.64 // return conv(conv(p1,p2,t),conv(p2,p3,t),t);
1.65 return ((1-t)*(1-t))*p1+(2*(1-t)*t)*p2+(t*t)*p3;
1.66 }
1.67 Bezier2 before(double t) const
1.68 {
1.69 - xy q(conv(p1,p2,t));
1.70 - xy r(conv(p2,p3,t));
1.71 + Point q(conv(p1,p2,t));
1.72 + Point r(conv(p2,p3,t));
1.73 return Bezier2(p1,q,conv(q,r,t));
1.74 }
1.75
1.76 Bezier2 after(double t) const
1.77 {
1.78 - xy q(conv(p1,p2,t));
1.79 - xy r(conv(p2,p3,t));
1.80 + Point q(conv(p1,p2,t));
1.81 + Point r(conv(p2,p3,t));
1.82 return Bezier2(conv(q,r,t),r,p3);
1.83 }
1.84 Bezier2 revert() const { return Bezier2(p3,p2,p1);}
1.85 Bezier2 operator()(double a,double b) const { return before(b).after(a/b); }
1.86 Bezier1 grad() const { return Bezier1(2.0*(p2-p1),2.0*(p3-p2)); }
1.87 Bezier1 norm() const { return Bezier1(2.0*rot90(p2-p1),2.0*rot90(p3-p2)); }
1.88 - xy grad(double t) const { return grad()(t); }
1.89 - xy norm(double t) const { return rot90(grad(t)); }
1.90 + Point grad(double t) const { return grad()(t); }
1.91 + Point norm(double t) const { return rot90(grad(t)); }
1.92 };
1.93
1.94 class Bezier3 : public BezierBase
1.95 {
1.96 public:
1.97 - xy p1,p2,p3,p4;
1.98 + Point p1,p2,p3,p4;
1.99
1.100 Bezier3() {}
1.101 - Bezier3(xy _p1, xy _p2, xy _p3, xy _p4) :p1(_p1), p2(_p2), p3(_p3), p4(_p4) {}
1.102 + Bezier3(Point _p1, Point _p2, Point _p3, Point _p4)
1.103 + : 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 + Point operator()(double t) const
1.111 {
1.112 // return Bezier2(conv(p1,p2,t),conv(p2,p3,t),conv(p3,p4,t))(t);
1.113 return ((1-t)*(1-t)*(1-t))*p1+(3*t*(1-t)*(1-t))*p2+
1.114 @@ -123,23 +125,23 @@
1.115 }
1.116 Bezier3 before(double t) const
1.117 {
1.118 - xy p(conv(p1,p2,t));
1.119 - xy q(conv(p2,p3,t));
1.120 - xy r(conv(p3,p4,t));
1.121 - xy a(conv(p,q,t));
1.122 - xy b(conv(q,r,t));
1.123 - xy c(conv(a,b,t));
1.124 + Point p(conv(p1,p2,t));
1.125 + Point q(conv(p2,p3,t));
1.126 + Point r(conv(p3,p4,t));
1.127 + Point a(conv(p,q,t));
1.128 + Point b(conv(q,r,t));
1.129 + Point c(conv(a,b,t));
1.130 return Bezier3(p1,p,a,c);
1.131 }
1.132
1.133 Bezier3 after(double t) const
1.134 {
1.135 - xy p(conv(p1,p2,t));
1.136 - xy q(conv(p2,p3,t));
1.137 - xy r(conv(p3,p4,t));
1.138 - xy a(conv(p,q,t));
1.139 - xy b(conv(q,r,t));
1.140 - xy c(conv(a,b,t));
1.141 + Point p(conv(p1,p2,t));
1.142 + Point q(conv(p2,p3,t));
1.143 + Point r(conv(p3,p4,t));
1.144 + Point a(conv(p,q,t));
1.145 + Point b(conv(q,r,t));
1.146 + Point c(conv(a,b,t));
1.147 return Bezier3(c,b,r,p4);
1.148 }
1.149 Bezier3 revert() const { return Bezier3(p4,p3,p2,p1);}
1.150 @@ -148,18 +150,18 @@
1.151 Bezier2 norm() const { return Bezier2(3.0*rot90(p2-p1),
1.152 3.0*rot90(p3-p2),
1.153 3.0*rot90(p4-p3)); }
1.154 - xy grad(double t) const { return grad()(t); }
1.155 - xy norm(double t) const { return rot90(grad(t)); }
1.156 + Point grad(double t) const { return grad()(t); }
1.157 + Point norm(double t) const { return rot90(grad(t)); }
1.158
1.159 template<class R,class F,class S,class D>
1.160 R recSplit(F &_f,const S &_s,D _d) const
1.161 {
1.162 - const xy a=(p1+p2)/2;
1.163 - const xy b=(p2+p3)/2;
1.164 - const xy c=(p3+p4)/2;
1.165 - const xy d=(a+b)/2;
1.166 - const xy e=(b+c)/2;
1.167 - const xy f=(d+e)/2;
1.168 + const Point a=(p1+p2)/2;
1.169 + const Point b=(p2+p3)/2;
1.170 + const Point c=(p3+p4)/2;
1.171 + const Point d=(a+b)/2;
1.172 + const Point e=(b+c)/2;
1.173 + const Point f=(d+e)/2;
1.174 R f1=_f(Bezier3(p1,a,d,e),_d);
1.175 R f2=_f(Bezier3(e,d,c,p4),_d);
1.176 return _s(f1,f2);
1.177 @@ -167,6 +169,8 @@
1.178
1.179 };
1.180
1.181 -} //END OF NAMESPACE LEMON
1.182 +
1.183 +} //END OF NAMESPACE dim2
1.184 +} //END OF NAMESPACE lemon
1.185
1.186 #endif // LEMON_BEZIER_H