/
bsplines.c
275 lines (221 loc) · 6.09 KB
/
bsplines.c
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/* B-Spline Generator */
/* Compile with gcc main.c -o bspline -lGL -lGLU -lglut -lm */
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include <stdlib.h>
#include <math.h>
#include <stdio.h>
#define SCREEN_HEIGHT 480
#define SCREEN_WIDTH 640
#define MAX_POINTS 50
/* Points */
struct Point {
int x, y;
};
/* Globals */
struct Point points[MAX_POINTS];
int left_button_down = 0;
int current_point = -1;
int num_points = 0;
float knot[MAX_POINTS];
/* Declarations */
float b_spline(int k, int m, float t);
void remove_point(int x, int y);
void mouse_input(int button, int state, int x, int y);
void keyboard_input(unsigned char key, int x, int y);
void init();
void draw_screen();
void draw_point(int x, int y);
int update_point(int x, int y);
void rebuild_array(int begin);
int build_knots(int m, int L);
void mouse_move_input(int x, int y);
/* Main generates window, initializes keys*/
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(SCREEN_WIDTH, SCREEN_HEIGHT);
glutInitWindowPosition(100, 100);
glutCreateWindow("B-Spline");
glutMouseFunc(mouse_input);
glutDisplayFunc(draw_screen);
glutKeyboardFunc(keyboard_input);
glutMotionFunc(mouse_move_input);
init();
glutMainLoop();
return 0;
}
/* Initialize graphic settings */
void init()
{
glClearColor(1.0, 1.0, 1.0, 1.0);
glColor3f(0.0, 0.0, 0.0);
glPointSize(6.0);
glLineWidth(2.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, 640.0, 0.0, 480.0);
}
/* Points */
void draw_screen()
{
int i, m = 4;
double t, sum_x, sum_y, x_old, y_old;
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0, 0.0, 1.0);
/* draw points and connecting lines */
for (i = 0; i < num_points; i++) {
draw_point(points[i].x, points[i].y);
if (i == 0) {
x_old = points[i].x;
y_old = points[i].y;
}
glBegin(GL_LINES);
glVertex2i(x_old, y_old);
glVertex2i(points[i].x, points[i].y);
glEnd();
x_old = points[i].x;
y_old = points[i].y;
}
/* exit if no knot vector */
if (!build_knots(m, num_points - 1)) {
glutSwapBuffers();
glFlush();
return;
}
/* reset last point variables */
x_old = points[0].x;
y_old = points[0].y;
glColor3f(0.0, 1.0, 0.0);
/* draw curve */
for (t = knot[m-1] ; t <= knot[num_points]; t += .05) {
sum_x = 0;
sum_y = 0;
for (i = 0; i < num_points; i++) {
sum_x += points[i].x * b_spline(i, m, t);
sum_y += points[i].y * b_spline(i, m, t);
}
if (sum_x == 0.0 || sum_y == 0.0)
continue;
glBegin(GL_LINES);
glVertex2i(x_old, y_old);
glVertex2i(sum_x, sum_y);
glEnd();
x_old = sum_x;
y_old = sum_y;
}
glutSwapBuffers();
glFlush();
}
/* remove the selected point */
void remove_point(int x, int y)
{
int i;
for (i = 0; i < num_points; i++) {
if (points[i].x >= (x - 3) && points[i].x <= (x + 3) &&
points[i].y >= (y - 3) && points[i].y <= (y + 3)) {
rebuild_array(i);
}
}
}
/* rebuild point array after removing point */
void rebuild_array(int begin)
{
int j;
num_points--;
for (j = begin; j < num_points; j++) {
points[j].x = points[j + 1].x;
points[j].y = points[j + 1].y;
}
}
/* move the selected point */
int update_point(int x, int y)
{
int i;
int point_found = 0;
for (i = 0; i < num_points; i++) {
if (points[i].x >= (x - 3) && points[i].x <= (x + 3) &&
points[i].y >= (y - 3) && points[i].y <= (y + 3)) {
points[i].x = x;
points[i].y = y;
current_point = i;
point_found = 1;
}
}
return point_found;
}
/* intercept mouse input */
void mouse_input(int button, int state, int x, int y)
{
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
int point_found = update_point(x, SCREEN_HEIGHT - y);
if (point_found)
left_button_down = 1;
if (!point_found && num_points < MAX_POINTS) {
points[num_points].x = x;
points[num_points].y = SCREEN_HEIGHT - y;
current_point = num_points;
num_points++;
}
} else if (button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN) {
remove_point(x, SCREEN_HEIGHT - y);
} else if (button==GLUT_LEFT_BUTTON && state == GLUT_UP) {
left_button_down = 0;
}
draw_screen();
}
/* intercept mouse input to move a point */
void mouse_move_input(int x, int y)
{
if (left_button_down) {
points[current_point].x = x;
points[current_point].y = SCREEN_HEIGHT - y;
draw_screen();
}
}
/* calculates b-spline location */
float b_spline(int k, int m, float t)
{
float denom1, denom2, sum = 0.0;
if (m == 1)
return (t >= knot[k] && t < knot[k+1]);
denom1 = knot[k+m -1] - knot[k];
if (denom1 != 0.0)
sum = (t - knot[k]) * b_spline(k, m - 1, t) / denom1;
denom2 = knot[k + m] - knot[k + 1];
if (denom2 != 0.0)
sum += (knot[k + m] - t) * b_spline(k + 1, m - 1, t) / denom2;
return sum;
}
/* Builds knots used to calculate splines */
int build_knots(int m, int L)
{
int i;
if (L < (m - 1)) return 0;
for (i = 1; i <= L + m; i++) {
if (i < m) knot[i] = 0.0;
else if (i <= L) knot[i] = i - m + 1;
else knot[i] = L - m + 2;
}
return 1;
}
/* Draws a point */
void draw_point(int x, int y)
{
glBegin(GL_POINTS);
glVertex2i(x, y);
glEnd();
}
/* Intercepts keyboard input to quit */
void keyboard_input(unsigned char key, int x, int y)
{
int i;
switch (key) {
case 'q':
case 'Q':
exit(0);
break;
}
}