Blender  V2.93
Bezier.cpp
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16 
22 #include "Bezier.h"
23 #include "FitCurve.h"
24 
25 using namespace std;
26 
27 namespace Freestyle {
28 
29 BezierCurveSegment::~BezierCurveSegment() = default;
30 
31 void BezierCurveSegment::AddControlPoint(const Vec2d &iPoint)
32 {
33  _ControlPolygon.push_back(iPoint);
34  if (_ControlPolygon.size() == 4) {
35  Build();
36  }
37 }
38 
39 void BezierCurveSegment::Build()
40 {
41  if (_ControlPolygon.size() != 4) {
42  return;
43  }
44 
45  // Compute the rightmost part of the matrix:
46  vector<Vec2d>::const_iterator p0, p1, p2, p3;
47  p0 = _ControlPolygon.begin();
48  p1 = p0;
49  ++p1;
50  p2 = p1;
51  ++p2;
52  p3 = p2;
53  ++p3;
54  float x[4], y[4];
55 
56  x[0] = -p0->x() + 3 * p1->x() - 3 * p2->x() + p3->x();
57  x[1] = 3 * p0->x() - 6 * p1->x() + 3 * p2->x();
58  x[2] = -3 * p0->x() + 3 * p1->x();
59  x[3] = p0->x();
60 
61  y[0] = -p0->y() + 3 * p1->y() - 3 * p2->y() + p3->y();
62  y[1] = 3 * p0->y() - 6 * p1->y() + 3 * p2->y();
63  y[2] = -3 * p0->y() + 3 * p1->y();
64  y[3] = p0->y();
65 
66  int nvertices = 12;
67  float increment = 1.0 / (float)nvertices;
68  float t = 0.0f;
69  for (int i = 0; i <= nvertices; ++i) {
70  _Vertices.emplace_back((x[3] + t * (x[2] + t * (x[1] + t * x[0]))),
71  (y[3] + t * (y[2] + t * (y[1] + t * y[0]))));
72  t += increment;
73  }
74 }
75 
77 {
78  _currentSegment = new BezierCurveSegment;
79 }
80 
81 BezierCurve::BezierCurve(vector<Vec2d> &iPoints, double error)
82 {
83  FitCurveWrapper fitcurve;
84  _currentSegment = new BezierCurveSegment;
85  vector<Vec2d> curve;
86 
87  fitcurve.FitCurve(iPoints, curve, error);
88  int i = 0;
89  vector<Vec2d>::iterator v, vend;
90  for (v = curve.begin(), vend = curve.end(); v != vend; ++v) {
91  if ((i == 0) || (i % 4 != 0)) {
92  AddControlPoint(*v);
93  }
94  ++i;
95  }
96 }
97 
98 BezierCurve::~BezierCurve()
99 {
100  if (!_Segments.empty()) {
101  vector<BezierCurveSegment *>::iterator v, vend;
102  for (v = _Segments.begin(), vend = _Segments.end(); v != vend; ++v) {
103  delete *v;
104  }
105  }
106  delete _currentSegment;
107 }
108 
109 void BezierCurve::AddControlPoint(const Vec2d &iPoint)
110 {
111  _ControlPolygon.push_back(iPoint);
112  _currentSegment->AddControlPoint(iPoint);
113  if (_currentSegment->size() == 4) {
114  _Segments.push_back(_currentSegment);
115  _currentSegment = new BezierCurveSegment;
116  _currentSegment->AddControlPoint(iPoint);
117  }
118 }
119 
120 } /* namespace Freestyle */
typedef float(TangentPoint)[2]
Class to define a Bezier curve of order 4.
An Algorithm for Automatically Fitting Digitized Curves by Philip J. Schneider,.
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint y
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint GLsizei GLsizei GLenum type _GL_VOID_RET _GL_VOID GLsizei GLenum GLenum const void *pixels _GL_VOID_RET _GL_VOID const void *pointer _GL_VOID_RET _GL_VOID GLdouble v _GL_VOID_RET _GL_VOID GLfloat v _GL_VOID_RET _GL_VOID GLint GLint i2 _GL_VOID_RET _GL_VOID GLint j _GL_VOID_RET _GL_VOID GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble GLdouble GLdouble zFar _GL_VOID_RET _GL_UINT GLdouble *equation _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLenum GLfloat *v _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLfloat *values _GL_VOID_RET _GL_VOID GLushort *values _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLenum GLdouble *params _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_BOOL GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLushort pattern _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble u2 _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLdouble GLdouble v2 _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLdouble GLdouble nz _GL_VOID_RET _GL_VOID GLfloat GLfloat nz _GL_VOID_RET _GL_VOID GLint GLint nz _GL_VOID_RET _GL_VOID GLshort GLshort nz _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const GLfloat *values _GL_VOID_RET _GL_VOID GLsizei const GLushort *values _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID const GLuint const GLclampf *priorities _GL_VOID_RET _GL_VOID GLdouble y _GL_VOID_RET _GL_VOID GLfloat y _GL_VOID_RET _GL_VOID GLint y _GL_VOID_RET _GL_VOID GLshort y _GL_VOID_RET _GL_VOID GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLfloat GLfloat z _GL_VOID_RET _GL_VOID GLint GLint z _GL_VOID_RET _GL_VOID GLshort GLshort z _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble w _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat w _GL_VOID_RET _GL_VOID GLint GLint GLint w _GL_VOID_RET _GL_VOID GLshort GLshort GLshort w _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble y2 _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat y2 _GL_VOID_RET _GL_VOID GLint GLint GLint y2 _GL_VOID_RET _GL_VOID GLshort GLshort GLshort y2 _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLuint *buffer _GL_VOID_RET _GL_VOID GLdouble t _GL_VOID_RET _GL_VOID GLfloat t _GL_VOID_RET _GL_VOID GLint t _GL_VOID_RET _GL_VOID GLshort t _GL_VOID_RET _GL_VOID GLdouble t
ATTR_WARN_UNUSED_RESULT const BMVert * v
void AddControlPoint(const Vec2d &iPoint)
Definition: Bezier.cpp:31
void FitCurve(std::vector< Vec2d > &data, std::vector< Vec2d > &oCurve, double error)
Definition: FitCurve.cpp:477
Curve curve
static void error(const char *str)
Definition: meshlaplacian.c:65
inherits from class Rep
Definition: AppCanvas.cpp:32
Vector2 * BezierCurve
Definition: FitCurve.cpp:33