# source:lemon-0.x/lemon/bezier.h@1523:144ab0e4b09c

Last change on this file since 1523:144ab0e4b09c was 1471:11a13908b510, checked in by Balazs Dezso, 16 years ago

Minor change

File size: 3.7 KB
RevLine
[1073]1/* -*- C++ -*-
[1435]2 * lemon/bezier.h - Part of LEMON, a generic C++ optimization library
[1073]3 *
[1164]4 * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
[1359]5 * (Egervary Research Group on Combinatorial Optimization, EGRES).
[1073]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///
[1084]24///Up to now this file is used internally by \ref graph_to_eps.h
[1073]25///
26///\author Alpar Juttner
27
28#include<lemon/xy.h>
29
30namespace lemon {
31
32class BezierBase {
33public:
34  typedef xy<double> xy;
35protected:
36  static xy conv(xy x,xy y,double t) {return (1-t)*x+t*y;}
37};
38
39class Bezier1 : public BezierBase
40{
41public:
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  }
[1084]61  Bezier1 revert() { return Bezier1(p2,p1);}
62  Bezier1 operator()(double a,double b) { return before(b).after(a/b); }
63  xy grad() { return p2-p1; }
[1471]64  xy grad(double) { return grad(); }
[1084]65
[1073]66};
67
68class Bezier2 : public BezierBase
69{
70public:
71  xy p1,p2,p3;
72
73  Bezier2() {}
74  Bezier2(xy _p1, xy _p2, xy _p3) :p1(_p1), p2(_p2), p3(_p3) {}
75  Bezier2(const Bezier1 &b) : p1(b.p1), p2(conv(b.p1,b.p2,.5)), p3(b.p2) {}
76  xy operator()(double t) const
77  {
78    //    return conv(conv(p1,p2,t),conv(p2,p3,t),t);
79    return ((1-t)*(1-t))*p1+(2*(1-t)*t)*p2+(t*t)*p3;
80  }
81  Bezier2 before(double t) const
82  {
83    xy q(conv(p1,p2,t));
84    xy r(conv(p2,p3,t));
85    return Bezier2(p1,q,conv(q,r,t));
86  }
87
88  Bezier2 after(double t) const
89  {
90    xy q(conv(p1,p2,t));
91    xy r(conv(p2,p3,t));
92    return Bezier2(conv(q,r,t),r,p3);
93  }
[1084]94  Bezier2 revert() { return Bezier2(p3,p2,p1);}
[1073]95  Bezier2 operator()(double a,double b) { return before(b).after(a/b); }
[1084]96  Bezier1 grad() { return Bezier1(2.0*(p2-p1),2.0*(p3-p2)); }
97  xy grad(double t) { return grad()(t); }
[1073]98};
99
100class Bezier3 : public BezierBase
101{
102public:
103  xy p1,p2,p3,p4;
104
105  Bezier3() {}
106  Bezier3(xy _p1, xy _p2, xy _p3, xy _p4) :p1(_p1), p2(_p2), p3(_p3), p4(_p4) {}
107  Bezier3(const Bezier1 &b) : p1(b.p1), p2(conv(b.p1,b.p2,1.0/3.0)),
108                              p3(conv(b.p1,b.p2,2.0/3.0)), p4(b.p2) {}
109  Bezier3(const Bezier2 &b) : p1(b.p1), p2(conv(b.p1,b.p2,2.0/3.0)),
110                              p3(conv(b.p2,b.p3,1.0/3.0)), p4(b.p3) {}
111
112  xy operator()(double t) const
113    {
114      //    return Bezier2(conv(p1,p2,t),conv(p2,p3,t),conv(p3,p4,t))(t);
115      return ((1-t)*(1-t)*(1-t))*p1+(3*t*(1-t)*(1-t))*p2+
116        (3*t*t*(1-t))*p3+(t*t*t)*p4;
117    }
118  Bezier3 before(double t) const
119    {
120      xy p(conv(p1,p2,t));
121      xy q(conv(p2,p3,t));
122      xy r(conv(p3,p4,t));
123      xy a(conv(p,q,t));
124      xy b(conv(q,r,t));
125      xy c(conv(a,b,t));
126      return Bezier3(p1,p,a,c);
127    }
128
129  Bezier3 after(double t) const
130    {
131      xy p(conv(p1,p2,t));
132      xy q(conv(p2,p3,t));
133      xy r(conv(p3,p4,t));
134      xy a(conv(p,q,t));
135      xy b(conv(q,r,t));
136      xy c(conv(a,b,t));
137      return Bezier3(c,b,r,p4);
138    }
[1084]139  Bezier3 revert() { return Bezier3(p4,p3,p2,p1);}
[1073]140  Bezier3 operator()(double a,double b) { return before(b).after(a/b); }
[1084]141  Bezier2 grad() { return Bezier2(3.0*(p2-p1),3.0*(p3-p2),3.0*(p4-p3)); }
142  xy grad(double t) { return grad()(t); }
[1073]143};
144
145} //END OF NAMESPACE LEMON
146
147#endif // LEMON_BEZIER_H
Note: See TracBrowser for help on using the repository browser.