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