-
Notifications
You must be signed in to change notification settings - Fork 0
/
BSpline.cpp
143 lines (128 loc) · 3.98 KB
/
BSpline.cpp
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include "BSpline.h"
#define GL_SILENCE_DEPRECATION
#ifdef __APPLE__
#include <OpenGL/gl.h>
#else
#include <GL/gl.h>
#endif
inline int clamp(int val, int minVal, int maxVal)
{
return std::min(std::max(val, minVal), maxVal);
}
BSpline::coefs_t BSpline::calcSplineCoef3(double t)
{
coefs_t coefs;
coefs.reserve(4);
coefs.push_back((1.0 - t) * (1.0 - t) * (1.0 - t) / 6.0);
coefs.push_back((3.0 * t * t * t - 6.0 * t * t + 4) / 6.0);
coefs.push_back((-3.0 * t * t * t + 3 * t * t + 3 * t + 1) / 6.0);
coefs.push_back(t * t * t / 6.0);
return coefs;
}
BSpline::coefs_t BSpline::calcSplineCoef2(double t)
{
coefs_t coefs;
coefs.reserve(3);
coefs.push_back((1.0 - t) * (1.0 - t) / 2.0);
coefs.push_back((-2.0 * t * t + 2.0 * t + 1) / 2.0);
coefs.push_back(t * t / 2.0);
return coefs;
}
BSpline::BSpline(const Points3& points, uint degree, bool closed, uint stepCount)
: degree(degree), stepCount(stepCount), points(points), closed(closed)
{
// Генерация коэффициентов B-сплайна
coefs = std::vector<coefs_t>();
for (uint i = 0; i < stepCount; i++) {
coefs_t coef = degree == 3
? calcSplineCoef3(i / (double)stepCount)
: calcSplineCoef2(i / (double)stepCount);
coefs.push_back(coef);
}
}
void BSpline::drawSegment3(int segNum)
{
int pNum = (int)points.size();
int p0, p1, p2, p3;
//Вычисление номеров вершин для построения сплайна
if (closed == false) {
p0 = clamp(segNum - 2, 0, pNum - 1);
p1 = clamp(segNum - 1, 0, pNum - 1);
p2 = clamp(segNum, 0, pNum - 1);
p3 = clamp(segNum + 1, 0, pNum - 1);
} else {
p0 = (segNum - 1 + pNum) % pNum;
p1 = (segNum + pNum) % pNum;
p2 = (segNum + 1 + pNum) % pNum;
p3 = (segNum + 2 + pNum) % pNum;
}
// По заранее вычисленным коэффициентам
// Вычисляем промежуточные точки сплайна
for (uint i = 0; i < stepCount; i++) {
double x = coefs.at(i)[0] * points[p0].x()
+ coefs.at(i)[1] * points[p1].x()
+ coefs.at(i)[2] * points[p2].x()
+ coefs.at(i)[3] * points[p3].x();
double y = coefs[i][0] * points[p0].y()
+ coefs.at(i)[1] * points[p1].y()
+ coefs.at(i)[2] * points[p2].y()
+ coefs.at(i)[3] * points[p3].y();
double z = coefs.at(i)[0] * points[p0].z()
+ coefs.at(i)[1] * points[p1].z()
+ coefs.at(i)[2] * points[p2].z()
+ coefs.at(i)[3] * points[p3].z();
glVertex3f(x, y, z);
}
}
void BSpline::drawSegment2(int segNum)
{
int pNum = (int)points.size();
int p0, p1, p2;
//Вычисление номеров вершин для построения сплайна
if (closed == false) {
p0 = clamp(segNum - 1, 0, pNum - 1);
p1 = clamp(segNum, 0, pNum - 1);
p2 = clamp(segNum + 1, 0, pNum - 1);
} else {
p0 = (segNum - 1 + pNum) % pNum;
p1 = (segNum + pNum) % pNum;
p2 = (segNum + 1 + pNum) % pNum;
}
// По заранее вычисленным коэффициентам
// Вычисляем промежуточные точки сплайна
for (uint i = 0; i < stepCount; i++) {
double x = coefs.at(i)[0] * points[p0].x()
+ coefs[i][1] * points[p1].x()
+ coefs[i][2] * points[p2].x();
double y = coefs[i][0] * points[p0].y()
+ coefs[i][1] * points[p1].y()
+ coefs[i][2] * points[p2].y();
double z = coefs[i][0] * points[p0].z()
+ coefs[i][1] * points[p1].z()
+ coefs[i][2] * points[p2].z();
glVertex3f(x, y, z);
}
}
void BSpline::drawSplineCurve()
{
int segmentsCount;
glLineWidth(2.0f);
glColor4f(1.0f, 1.0f, 0.33f * degree, 1.0f);
if (closed == false) {
glBegin(GL_LINE_STRIP);
segmentsCount = std::max((int)(points.size() + degree) - 2, 0);
} else {
segmentsCount = (int)points.size(); //Сегмент между первой и последней вершиной
glBegin(GL_LINE_LOOP);
}
if (degree == 3) {
for (int i = 0; i < segmentsCount; i++) {
drawSegment3(i);
}
} else {
for (int i = 0; i < segmentsCount; i++) {
drawSegment2(i);
}
}
glEnd();
}