Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Bezier.cpp « geometry « intern « freestyle « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4eec3d72db9624888383d185b7494917c240e0a6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

/** \file
 * \ingroup freestyle
 * \brief Class to define a Bezier curve of order 4.
 */

#include "Bezier.h"
#include "FitCurve.h"

using namespace std;

namespace Freestyle {

BezierCurveSegment::BezierCurveSegment()
{
}

BezierCurveSegment::~BezierCurveSegment()
{
}

void BezierCurveSegment::AddControlPoint(const Vec2d &iPoint)
{
  _ControlPolygon.push_back(iPoint);
  if (_ControlPolygon.size() == 4) {
    Build();
  }
}

void BezierCurveSegment::Build()
{
  if (_ControlPolygon.size() != 4) {
    return;
  }

  // Compute the rightmost part of the matrix:
  vector<Vec2d>::const_iterator p0, p1, p2, p3;
  p0 = _ControlPolygon.begin();
  p1 = p0;
  ++p1;
  p2 = p1;
  ++p2;
  p3 = p2;
  ++p3;
  float x[4], y[4];

  x[0] = -p0->x() + 3 * p1->x() - 3 * p2->x() + p3->x();
  x[1] = 3 * p0->x() - 6 * p1->x() + 3 * p2->x();
  x[2] = -3 * p0->x() + 3 * p1->x();
  x[3] = p0->x();

  y[0] = -p0->y() + 3 * p1->y() - 3 * p2->y() + p3->y();
  y[1] = 3 * p0->y() - 6 * p1->y() + 3 * p2->y();
  y[2] = -3 * p0->y() + 3 * p1->y();
  y[3] = p0->y();

  int nvertices = 12;
  float increment = 1.0 / (float)nvertices;
  float t = 0.0f;
  for (int i = 0; i <= nvertices; ++i) {
    _Vertices.emplace_back((x[3] + t * (x[2] + t * (x[1] + t * x[0]))),
                           (y[3] + t * (y[2] + t * (y[1] + t * y[0]))));
    t += increment;
  }
}

BezierCurve::BezierCurve()
{
  _currentSegment = new BezierCurveSegment;
}

BezierCurve::BezierCurve(vector<Vec2d> &iPoints, double error)
{
  FitCurveWrapper fitcurve;
  _currentSegment = new BezierCurveSegment;
  vector<Vec2d> curve;

  fitcurve.FitCurve(iPoints, curve, error);
  int i = 0;
  vector<Vec2d>::iterator v, vend;
  for (v = curve.begin(), vend = curve.end(); v != vend; ++v) {
    if ((i == 0) || (i % 4 != 0)) {
      AddControlPoint(*v);
    }
    ++i;
  }
}

BezierCurve::~BezierCurve()
{
  if (!_Segments.empty()) {
    vector<BezierCurveSegment *>::iterator v, vend;
    for (v = _Segments.begin(), vend = _Segments.end(); v != vend; ++v) {
      delete *v;
    }
  }
  delete _currentSegment;
}

void BezierCurve::AddControlPoint(const Vec2d &iPoint)
{
  _ControlPolygon.push_back(iPoint);
  _currentSegment->AddControlPoint(iPoint);
  if (_currentSegment->size() == 4) {
    _Segments.push_back(_currentSegment);
    _currentSegment = new BezierCurveSegment;
    _currentSegment->AddControlPoint(iPoint);
  }
}

} /* namespace Freestyle */