lemon/bits/bezier.h
author deba
Mon, 14 Aug 2006 16:14:10 +0000
changeset 2181 b2c38f4f72ff
child 2207 75a29ac69c19
permissions -rw-r--r--
Omit warnings
alpar@2178
     1
/* -*- C++ -*-
alpar@2178
     2
 *
alpar@2178
     3
 * This file is a part of LEMON, a generic C++ optimization library
alpar@2178
     4
 *
alpar@2178
     5
 * Copyright (C) 2003-2006
alpar@2178
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@2178
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
alpar@2178
     8
 *
alpar@2178
     9
 * Permission to use, modify and distribute this software is granted
alpar@2178
    10
 * provided that this copyright notice appears in all copies. For
alpar@2178
    11
 * precise terms see the accompanying LICENSE file.
alpar@2178
    12
 *
alpar@2178
    13
 * This software is provided "AS IS" with no warranty of any kind,
alpar@2178
    14
 * express or implied, and with no claim as to its suitability for any
alpar@2178
    15
 * purpose.
alpar@2178
    16
 *
alpar@2178
    17
 */
alpar@2178
    18
alpar@2178
    19
#ifndef LEMON_BEZIER_H
alpar@2178
    20
#define LEMON_BEZIER_H
alpar@2178
    21
alpar@2178
    22
///\ingroup misc
alpar@2178
    23
///\file
alpar@2178
    24
///\brief Classes to compute with Bezier curves.
alpar@2178
    25
///
alpar@2178
    26
///Up to now this file is used internally by \ref graph_to_eps.h
alpar@2178
    27
///
alpar@2178
    28
///\author Alpar Juttner
alpar@2178
    29
alpar@2178
    30
#include<lemon/xy.h>
alpar@2178
    31
alpar@2178
    32
namespace lemon {
alpar@2178
    33
alpar@2178
    34
class BezierBase {
alpar@2178
    35
public:
alpar@2178
    36
  typedef xy<double> xy;
alpar@2178
    37
protected:
alpar@2178
    38
  static xy conv(xy x,xy y,double t) {return (1-t)*x+t*y;}
alpar@2178
    39
};
alpar@2178
    40
alpar@2178
    41
class Bezier1 : public BezierBase
alpar@2178
    42
{
alpar@2178
    43
public:
alpar@2178
    44
  xy p1,p2;
alpar@2178
    45
alpar@2178
    46
  Bezier1() {}
alpar@2178
    47
  Bezier1(xy _p1, xy _p2) :p1(_p1), p2(_p2) {}
alpar@2178
    48
  
alpar@2178
    49
  xy operator()(double t) const
alpar@2178
    50
  {
alpar@2178
    51
    //    return conv(conv(p1,p2,t),conv(p2,p3,t),t);
alpar@2178
    52
    return conv(p1,p2,t);
alpar@2178
    53
  }
alpar@2178
    54
  Bezier1 before(double t) const
alpar@2178
    55
  {
alpar@2178
    56
    return Bezier1(p1,conv(p1,p2,t));
alpar@2178
    57
  }
alpar@2178
    58
  
alpar@2178
    59
  Bezier1 after(double t) const
alpar@2178
    60
  {
alpar@2178
    61
    return Bezier1(conv(p1,p2,t),p2);
alpar@2178
    62
  }
alpar@2178
    63
alpar@2178
    64
  Bezier1 revert() const { return Bezier1(p2,p1);}
alpar@2178
    65
  Bezier1 operator()(double a,double b) const { return before(b).after(a/b); }
alpar@2178
    66
  xy grad() const { return p2-p1; }
alpar@2178
    67
  xy norm() const { return rot90(p2-p1); }
alpar@2178
    68
  xy grad(double) const { return grad(); }
alpar@2178
    69
  xy norm(double t) const { return rot90(grad(t)); }
alpar@2178
    70
};
alpar@2178
    71
alpar@2178
    72
class Bezier2 : public BezierBase
alpar@2178
    73
{
alpar@2178
    74
public:
alpar@2178
    75
  xy p1,p2,p3;
alpar@2178
    76
alpar@2178
    77
  Bezier2() {}
alpar@2178
    78
  Bezier2(xy _p1, xy _p2, xy _p3) :p1(_p1), p2(_p2), p3(_p3) {}
alpar@2178
    79
  Bezier2(const Bezier1 &b) : p1(b.p1), p2(conv(b.p1,b.p2,.5)), p3(b.p2) {}
alpar@2178
    80
  xy operator()(double t) const
alpar@2178
    81
  {
alpar@2178
    82
    //    return conv(conv(p1,p2,t),conv(p2,p3,t),t);
alpar@2178
    83
    return ((1-t)*(1-t))*p1+(2*(1-t)*t)*p2+(t*t)*p3;
alpar@2178
    84
  }
alpar@2178
    85
  Bezier2 before(double t) const
alpar@2178
    86
  {
alpar@2178
    87
    xy q(conv(p1,p2,t));
alpar@2178
    88
    xy r(conv(p2,p3,t));
alpar@2178
    89
    return Bezier2(p1,q,conv(q,r,t));
alpar@2178
    90
  }
alpar@2178
    91
  
alpar@2178
    92
  Bezier2 after(double t) const
alpar@2178
    93
  {
alpar@2178
    94
    xy q(conv(p1,p2,t));
alpar@2178
    95
    xy r(conv(p2,p3,t));
alpar@2178
    96
    return Bezier2(conv(q,r,t),r,p3);
alpar@2178
    97
  }
alpar@2178
    98
  Bezier2 revert() const { return Bezier2(p3,p2,p1);}
alpar@2178
    99
  Bezier2 operator()(double a,double b) const { return before(b).after(a/b); }
alpar@2178
   100
  Bezier1 grad() const { return Bezier1(2.0*(p2-p1),2.0*(p3-p2)); }
alpar@2178
   101
  Bezier1 norm() const { return Bezier1(2.0*rot90(p2-p1),2.0*rot90(p3-p2)); }
alpar@2178
   102
  xy grad(double t) const { return grad()(t); }
alpar@2178
   103
  xy norm(double t) const { return rot90(grad(t)); }
alpar@2178
   104
};
alpar@2178
   105
alpar@2178
   106
class Bezier3 : public BezierBase
alpar@2178
   107
{
alpar@2178
   108
public:
alpar@2178
   109
  xy p1,p2,p3,p4;
alpar@2178
   110
alpar@2178
   111
  Bezier3() {}
alpar@2178
   112
  Bezier3(xy _p1, xy _p2, xy _p3, xy _p4) :p1(_p1), p2(_p2), p3(_p3), p4(_p4) {}
alpar@2178
   113
  Bezier3(const Bezier1 &b) : p1(b.p1), p2(conv(b.p1,b.p2,1.0/3.0)), 
alpar@2178
   114
			      p3(conv(b.p1,b.p2,2.0/3.0)), p4(b.p2) {}
alpar@2178
   115
  Bezier3(const Bezier2 &b) : p1(b.p1), p2(conv(b.p1,b.p2,2.0/3.0)),
alpar@2178
   116
			      p3(conv(b.p2,b.p3,1.0/3.0)), p4(b.p3) {}
alpar@2178
   117
  
alpar@2178
   118
  xy operator()(double t) const 
alpar@2178
   119
    {
alpar@2178
   120
      //    return Bezier2(conv(p1,p2,t),conv(p2,p3,t),conv(p3,p4,t))(t);
alpar@2178
   121
      return ((1-t)*(1-t)*(1-t))*p1+(3*t*(1-t)*(1-t))*p2+
alpar@2178
   122
	(3*t*t*(1-t))*p3+(t*t*t)*p4;
alpar@2178
   123
    }
alpar@2178
   124
  Bezier3 before(double t) const
alpar@2178
   125
    {
alpar@2178
   126
      xy p(conv(p1,p2,t));
alpar@2178
   127
      xy q(conv(p2,p3,t));
alpar@2178
   128
      xy r(conv(p3,p4,t));
alpar@2178
   129
      xy a(conv(p,q,t));
alpar@2178
   130
      xy b(conv(q,r,t));
alpar@2178
   131
      xy c(conv(a,b,t));
alpar@2178
   132
      return Bezier3(p1,p,a,c);
alpar@2178
   133
    }
alpar@2178
   134
  
alpar@2178
   135
  Bezier3 after(double t) const
alpar@2178
   136
    {
alpar@2178
   137
      xy p(conv(p1,p2,t));
alpar@2178
   138
      xy q(conv(p2,p3,t));
alpar@2178
   139
      xy r(conv(p3,p4,t));
alpar@2178
   140
      xy a(conv(p,q,t));
alpar@2178
   141
      xy b(conv(q,r,t));
alpar@2178
   142
      xy c(conv(a,b,t));
alpar@2178
   143
      return Bezier3(c,b,r,p4);
alpar@2178
   144
    }
alpar@2178
   145
  Bezier3 revert() const { return Bezier3(p4,p3,p2,p1);}
alpar@2178
   146
  Bezier3 operator()(double a,double b) const { return before(b).after(a/b); }
alpar@2178
   147
  Bezier2 grad() const { return Bezier2(3.0*(p2-p1),3.0*(p3-p2),3.0*(p4-p3)); }
alpar@2178
   148
  Bezier2 norm() const { return Bezier2(3.0*rot90(p2-p1),
alpar@2178
   149
				  3.0*rot90(p3-p2),
alpar@2178
   150
				  3.0*rot90(p4-p3)); }
alpar@2178
   151
  xy grad(double t) const { return grad()(t); }
alpar@2178
   152
  xy norm(double t) const { return rot90(grad(t)); }
alpar@2178
   153
alpar@2178
   154
  template<class R,class F,class S,class D>
alpar@2178
   155
  R recSplit(F &_f,const S &_s,D _d) const 
alpar@2178
   156
  {
alpar@2178
   157
    const xy a=(p1+p2)/2;
alpar@2178
   158
    const xy b=(p2+p3)/2;
alpar@2178
   159
    const xy c=(p3+p4)/2;
alpar@2178
   160
    const xy d=(a+b)/2;
alpar@2178
   161
    const xy e=(b+c)/2;
alpar@2178
   162
    const xy f=(d+e)/2;
alpar@2178
   163
    R f1=_f(Bezier3(p1,a,d,e),_d);
alpar@2178
   164
    R f2=_f(Bezier3(e,d,c,p4),_d);
alpar@2178
   165
    return _s(f1,f2);
alpar@2178
   166
  }
alpar@2178
   167
  
alpar@2178
   168
};
alpar@2178
   169
alpar@2178
   170
} //END OF NAMESPACE LEMON
alpar@2178
   171
alpar@2178
   172
#endif // LEMON_BEZIER_H