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