[1073] | 1 | /* -*- C++ -*- |
---|
| 2 | * src/lemon/bezier.h - Part of LEMON, a generic C++ optimization library |
---|
| 3 | * |
---|
| 4 | * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
---|
| 5 | * (Egervary Combinatorial Optimization Research Group, EGRES). |
---|
| 6 | * |
---|
| 7 | * Permission to use, modify and distribute this software is granted |
---|
| 8 | * provided that this copyright notice appears in all copies. For |
---|
| 9 | * precise terms see the accompanying LICENSE file. |
---|
| 10 | * |
---|
| 11 | * This software is provided "AS IS" with no warranty of any kind, |
---|
| 12 | * express or implied, and with no claim as to its suitability for any |
---|
| 13 | * purpose. |
---|
| 14 | * |
---|
| 15 | */ |
---|
| 16 | |
---|
| 17 | #ifndef LEMON_BEZIER_H |
---|
| 18 | #define LEMON_BEZIER_H |
---|
| 19 | |
---|
| 20 | ///\ingroup misc |
---|
| 21 | ///\file |
---|
| 22 | ///\brief Classes to compute with Bezier curves. |
---|
| 23 | /// |
---|
| 24 | ///Up to now this file is internally used by \ref graph_to_eps.h |
---|
| 25 | /// |
---|
| 26 | ///\author Alpar Juttner |
---|
| 27 | |
---|
| 28 | #include<lemon/xy.h> |
---|
| 29 | |
---|
| 30 | namespace lemon { |
---|
| 31 | |
---|
| 32 | class BezierBase { |
---|
| 33 | public: |
---|
| 34 | typedef xy<double> xy; |
---|
| 35 | protected: |
---|
| 36 | static xy conv(xy x,xy y,double t) {return (1-t)*x+t*y;} |
---|
| 37 | }; |
---|
| 38 | |
---|
| 39 | class Bezier1 : public BezierBase |
---|
| 40 | { |
---|
| 41 | public: |
---|
| 42 | xy p1,p2; |
---|
| 43 | |
---|
| 44 | Bezier1() {} |
---|
| 45 | Bezier1(xy _p1, xy _p2) :p1(_p1), p2(_p2) {} |
---|
| 46 | |
---|
| 47 | xy operator()(double t) const |
---|
| 48 | { |
---|
| 49 | // return conv(conv(p1,p2,t),conv(p2,p3,t),t); |
---|
| 50 | return conv(p1,p2,t); |
---|
| 51 | } |
---|
| 52 | Bezier1 before(double t) const |
---|
| 53 | { |
---|
| 54 | return Bezier1(p1,conv(p1,p2,t)); |
---|
| 55 | } |
---|
| 56 | |
---|
| 57 | Bezier1 after(double t) const |
---|
| 58 | { |
---|
| 59 | return Bezier1(conv(p1,p2,t),p2); |
---|
| 60 | } |
---|
| 61 | Bezier1 operator()(double a,double b) { return before(b).after(a/b); } |
---|
| 62 | }; |
---|
| 63 | |
---|
| 64 | class Bezier2 : public BezierBase |
---|
| 65 | { |
---|
| 66 | public: |
---|
| 67 | xy p1,p2,p3; |
---|
| 68 | |
---|
| 69 | Bezier2() {} |
---|
| 70 | Bezier2(xy _p1, xy _p2, xy _p3) :p1(_p1), p2(_p2), p3(_p3) {} |
---|
| 71 | Bezier2(const Bezier1 &b) : p1(b.p1), p2(conv(b.p1,b.p2,.5)), p3(b.p2) {} |
---|
| 72 | xy operator()(double t) const |
---|
| 73 | { |
---|
| 74 | // return conv(conv(p1,p2,t),conv(p2,p3,t),t); |
---|
| 75 | return ((1-t)*(1-t))*p1+(2*(1-t)*t)*p2+(t*t)*p3; |
---|
| 76 | } |
---|
| 77 | Bezier2 before(double t) const |
---|
| 78 | { |
---|
| 79 | xy q(conv(p1,p2,t)); |
---|
| 80 | xy r(conv(p2,p3,t)); |
---|
| 81 | return Bezier2(p1,q,conv(q,r,t)); |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | Bezier2 after(double t) const |
---|
| 85 | { |
---|
| 86 | xy q(conv(p1,p2,t)); |
---|
| 87 | xy r(conv(p2,p3,t)); |
---|
| 88 | return Bezier2(conv(q,r,t),r,p3); |
---|
| 89 | } |
---|
| 90 | Bezier2 operator()(double a,double b) { return before(b).after(a/b); } |
---|
| 91 | |
---|
| 92 | }; |
---|
| 93 | |
---|
| 94 | class Bezier3 : public BezierBase |
---|
| 95 | { |
---|
| 96 | public: |
---|
| 97 | xy p1,p2,p3,p4; |
---|
| 98 | |
---|
| 99 | Bezier3() {} |
---|
| 100 | Bezier3(xy _p1, xy _p2, xy _p3, xy _p4) :p1(_p1), p2(_p2), p3(_p3), p4(_p4) {} |
---|
| 101 | Bezier3(const Bezier1 &b) : p1(b.p1), p2(conv(b.p1,b.p2,1.0/3.0)), |
---|
| 102 | p3(conv(b.p1,b.p2,2.0/3.0)), p4(b.p2) {} |
---|
| 103 | Bezier3(const Bezier2 &b) : p1(b.p1), p2(conv(b.p1,b.p2,2.0/3.0)), |
---|
| 104 | p3(conv(b.p2,b.p3,1.0/3.0)), p4(b.p3) {} |
---|
| 105 | |
---|
| 106 | xy operator()(double t) const |
---|
| 107 | { |
---|
| 108 | // return Bezier2(conv(p1,p2,t),conv(p2,p3,t),conv(p3,p4,t))(t); |
---|
| 109 | return ((1-t)*(1-t)*(1-t))*p1+(3*t*(1-t)*(1-t))*p2+ |
---|
| 110 | (3*t*t*(1-t))*p3+(t*t*t)*p4; |
---|
| 111 | } |
---|
| 112 | Bezier3 before(double t) const |
---|
| 113 | { |
---|
| 114 | xy p(conv(p1,p2,t)); |
---|
| 115 | xy q(conv(p2,p3,t)); |
---|
| 116 | xy r(conv(p3,p4,t)); |
---|
| 117 | xy a(conv(p,q,t)); |
---|
| 118 | xy b(conv(q,r,t)); |
---|
| 119 | xy c(conv(a,b,t)); |
---|
| 120 | return Bezier3(p1,p,a,c); |
---|
| 121 | } |
---|
| 122 | |
---|
| 123 | Bezier3 after(double t) const |
---|
| 124 | { |
---|
| 125 | xy p(conv(p1,p2,t)); |
---|
| 126 | xy q(conv(p2,p3,t)); |
---|
| 127 | xy r(conv(p3,p4,t)); |
---|
| 128 | xy a(conv(p,q,t)); |
---|
| 129 | xy b(conv(q,r,t)); |
---|
| 130 | xy c(conv(a,b,t)); |
---|
| 131 | return Bezier3(c,b,r,p4); |
---|
| 132 | } |
---|
| 133 | Bezier3 operator()(double a,double b) { return before(b).after(a/b); } |
---|
| 134 | |
---|
| 135 | }; |
---|
| 136 | |
---|
| 137 | } //END OF NAMESPACE LEMON |
---|
| 138 | |
---|
| 139 | #endif // LEMON_BEZIER_H |
---|