Blender V4.3
Bezier.cpp
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2012-2022 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
10#include "Bezier.h"
11#include "FitCurve.h"
12
13using namespace std;
14
15namespace Freestyle {
16
18
20{
21 _ControlPolygon.push_back(iPoint);
22 if (_ControlPolygon.size() == 4) {
23 Build();
24 }
25}
26
28{
29 if (_ControlPolygon.size() != 4) {
30 return;
31 }
32
33 // Compute the rightmost part of the matrix:
34 vector<Vec2d>::const_iterator p0, p1, p2, p3;
35 p0 = _ControlPolygon.begin();
36 p1 = p0;
37 ++p1;
38 p2 = p1;
39 ++p2;
40 p3 = p2;
41 ++p3;
42 float x[4], y[4];
43
44 x[0] = -p0->x() + 3 * p1->x() - 3 * p2->x() + p3->x();
45 x[1] = 3 * p0->x() - 6 * p1->x() + 3 * p2->x();
46 x[2] = -3 * p0->x() + 3 * p1->x();
47 x[3] = p0->x();
48
49 y[0] = -p0->y() + 3 * p1->y() - 3 * p2->y() + p3->y();
50 y[1] = 3 * p0->y() - 6 * p1->y() + 3 * p2->y();
51 y[2] = -3 * p0->y() + 3 * p1->y();
52 y[3] = p0->y();
53
54 int nvertices = 12;
55 float increment = 1.0 / float(nvertices);
56 float t = 0.0f;
57 for (int i = 0; i <= nvertices; ++i) {
58 _Vertices.emplace_back((x[3] + t * (x[2] + t * (x[1] + t * x[0]))),
59 (y[3] + t * (y[2] + t * (y[1] + t * y[0]))));
60 t += increment;
61 }
62}
63
64BezierCurve::BezierCurve()
65{
66 _currentSegment = new BezierCurveSegment;
67}
68
69BezierCurve::BezierCurve(vector<Vec2d> &iPoints, double error)
70{
71 FitCurveWrapper fitcurve;
72 _currentSegment = new BezierCurveSegment;
73 vector<Vec2d> curve;
74
75 fitcurve.FitCurve(iPoints, curve, error);
76 int i = 0;
77 vector<Vec2d>::iterator v, vend;
78 for (v = curve.begin(), vend = curve.end(); v != vend; ++v) {
79 if ((i == 0) || (i % 4 != 0)) {
81 }
82 ++i;
83 }
84}
85
86BezierCurve::~BezierCurve()
87{
88 if (!_Segments.empty()) {
89 vector<BezierCurveSegment *>::iterator v, vend;
90 for (v = _Segments.begin(), vend = _Segments.end(); v != vend; ++v) {
91 delete *v;
92 }
93 }
94 delete _currentSegment;
95}
96
97void BezierCurve::AddControlPoint(const Vec2d &iPoint)
98{
99 _ControlPolygon.push_back(iPoint);
100 _currentSegment->AddControlPoint(iPoint);
101 if (_currentSegment->size() == 4) {
102 _Segments.push_back(_currentSegment);
103 _currentSegment = new BezierCurveSegment;
104 _currentSegment->AddControlPoint(iPoint);
105 }
106}
107
108} /* namespace Freestyle */
Class to define a Bezier curve of order 4.
An Algorithm for Automatically Fitting Digitized Curves by Philip J. Schneider,.
ATTR_WARN_UNUSED_RESULT const BMVert * v
void AddControlPoint(const Vec2d &iPoint)
Definition Bezier.cpp:19
void AddControlPoint(const Vec2d &iPoint)
Definition Bezier.cpp:97
void FitCurve(std::vector< Vec2d > &data, std::vector< Vec2d > &oCurve, double error)
Definition FitCurve.cpp:467
draw_view in_light_buf[] float
static void error(const char *str)
inherits from class Rep
Definition AppCanvas.cpp:20