COPASI API  4.16.103
BezierCurve.cpp
Go to the documentation of this file.
1 // Begin CVS Header
2 // $Source: /Volumes/Home/Users/shoops/cvs/copasi_dev/copasi/layoutUI/BezierCurve.cpp,v $
3 // $Revision: 1.5 $
4 // $Name: $
5 // $Author: ssahle $
6 // $Date: 2012/04/22 14:51:17 $
7 // End CVS Header
8 
9 // Copyright (C) 2012 - 2010 by Pedro Mendes, Virginia Tech Intellectual
10 // Properties, Inc., University of Heidelberg, and The University
11 // of Manchester.
12 // All rights reserved.
13 
14 // Copyright (C) 2008 by Pedro Mendes, Virginia Tech Intellectual
15 // Properties, Inc., EML Research, gGmbH, University of Heidelberg,
16 // and The University of Manchester.
17 // All rights reserved.
18 
19 // Copyright (C) 2001 - 2007 by Pedro Mendes, Virginia Tech Intellectual
20 // Properties, Inc. and EML Research, gGmbH.
21 // All rights reserved.
22 
23 /*!
24  \file BezierCurve.cpp
25  \brief Implementation file of class BezierCurve
26  */
27 
28 #include <cmath>
29 
30 #include "BezierCurve.h"
31 
33 {
34  init();
35 }
36 
37 BezierCurve::BezierCurve(size_t numberOfStepsOnCurve)
38 {
39  steps = numberOfStepsOnCurve;
40  t = 1.0 / steps;
41 }
42 
44 {
45  steps = 100;
46  dt = 0.01;
47 }
48 
49 std::vector<CLPoint> BezierCurve::curvePts(const std::vector<CLPoint>& pts)
50 {
51  C_FLOAT64 x;
52  C_FLOAT64 y;
53  std::vector<CLPoint>points;
54  points = std::vector<CLPoint>();
55 
56  C_FLOAT64 t = 0.0;
57 
58  C_INT32 i;
59  C_INT32 n = (C_INT32) pts.size() - 1;
60 
61  while (t <= 1.0000001)
62  {
63  x = 0.0;
64  y = 0.0;
65 
66  for (i = 0; i <= n; i++)
67  {
68  x += bernstein(i, n, t) * pts[i].getX();
69  y += bernstein(i, n, t) * pts[i].getY();
70  }
71 
72  points.push_back(CLPoint(x, y));
73  t += dt;
74  }
75 
76  return points;
77 }
78 
80 {
81 
82  // first compute n over i
83  C_FLOAT64 top = multiplyFromTo(n - i + 1, n);
84  C_FLOAT64 bottom = multiplyFromTo(1, i);
85 
86  return (top / bottom * pow(t, (C_FLOAT64)i) * pow(1.0 - t, (C_FLOAT64)n - i));
87 }
88 
89 CLPoint BezierCurve::bezierPt(C_FLOAT64 t, std::vector<CLPoint> pts)
90 {
91  C_INT32 i;
92  C_FLOAT64 x = 0.0;
93  C_FLOAT64 y = 0.0;
94 
95  C_INT32 n = (C_INT32) pts.size() - 1;
96 
97  for (i = 0; i <= n; i++)
98  {
99  x += bernstein(i, n, t) * pts[i].getX();
100  y += bernstein(i, n, t) * pts[i].getY();
101  }
102 
103  return CLPoint(x, y);
104 }
105 
106 // compute product n1 * (n1+1) * ... * n2
107 // n2 > n1 !!! else returns 1
109 {
110  C_INT32 i;
111  C_FLOAT64 prod = 1;
112 
113  if (n2 >= n1)
114  {
115  for (i = n1; i <= n2; i++)
116  prod *= (C_FLOAT64)i;
117  }
118 
119  return prod;
120 }
C_FLOAT64 t
Definition: BezierCurve.h:45
C_FLOAT64 bernstein(C_INT32 i, C_INT32 n, C_FLOAT64 t)
Definition: BezierCurve.cpp:79
C_FLOAT64 multiplyFromTo(C_INT32 n1, C_INT32 n2)
#define C_INT32
Definition: copasi.h:90
Definition: CLBase.h:54
C_FLOAT64 dt
Definition: BezierCurve.h:47
Header file of class BezierCurve.
#define C_FLOAT64
Definition: copasi.h:92
std::vector< CLPoint > curvePts(const std::vector< CLPoint > &pts)
Definition: BezierCurve.cpp:49
size_t steps
Definition: BezierCurve.h:46
CLPoint bezierPt(C_FLOAT64 t, std::vector< CLPoint > pts)
Definition: BezierCurve.cpp:89