/
o_approximation_curve.c
219 lines (172 loc) · 5.55 KB
/
o_approximation_curve.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
#include "o_objet.h"
#include "t_geometrie.h"
#include "approximation_curve.h"
#include "approximation_curve_rendering.h"
#include "matrix.h"
#include "sys_lin.h"
#include "bernstein.h"
#include "casteljau.h"
#include "ne_utils.h"
#include <stdio.h>
static Table_flottant* ac_uniform_parameterization(Table_triplet* points)
{
int i;
Flottant step;
Table_flottant* params;
step = 1.f / (points->nb - 1);
params = malloc_table_flottant(points->nb);
for (i = 0; i < points->nb; ++i)
params->table[i] = i * step;
return params;
}
static Table_flottant* ac_non_uniform_parameterization(Table_triplet* points)
{
int i;
Table_flottant* params;
Flottant total_distance;
Flottant current_distance;
params = malloc_table_flottant(points->nb);
if (points->nb > 0)
params->table[0] = 0.f;
current_distance = 0;
total_distance = triplet_total_distance(points);
for (i = 0; i < (points->nb - 1); ++i)
{
current_distance += triplet_distance(&points->table[i], &points->table[i + 1]);
params->table[i + 1] = current_distance / total_distance;
}
return params;
}
static void ac_bernstein_matrix(Grille_flottant* m, Table_flottant* params)
{
int row;
int col;
int degree;
if (m->nb_colonnes != params->nb)
{
fprintf(stderr, "ac_bernstein_matrix(): la largeur de la matrice spécifié (%d) ne correspond pas au nombre de paramètres spécifiés (%d)\r\n",
m->nb_colonnes, params->nb);
EXIT;
}
degree = m->nb_lignes - 1;
for (row = 0; row < m->nb_lignes; ++row)
for (col = 0; col < m->nb_colonnes; ++col)
m->grille[row][col] = bernstein_factor(degree, row, params->table[col]);
}
static void ac_update_curve_polygon(struct approximation_curve* ac)
{
Grille_flottant* m;
Grille_flottant* mt;
Grille_flottant* mmt;
Table_flottant* p;
Table_flottant* a;
Table_flottant* mp;
Table_flottant* params;
// Allocation de la table des points de contrôle de la courbe d'approximation
if (ac->curve_polygon.nb != (ac->degree + 1))
{
free(ac->curve_polygon.table);
ac->curve_polygon.nb = ac->degree + 1;
ALLOUER(ac->curve_polygon.table, ac->curve_polygon.nb);
}
// Paramétrisation de la courbe d'approximation
if (ac->use_uniform_parameterization)
params = ac_uniform_parameterization(&ac->points);
else
params = ac_non_uniform_parameterization(&ac->points);
// Construction de la matrice de Bernstein, de sa transposée et de leur produit
m = matrix_create(ac->degree + 1, ac->points.nb);
ac_bernstein_matrix(m, params);
mt = matrix_transpose(m);
mmt = matrix_product(m, mt);
// Résolution des 3 systèmes d'équations (composantes x, y et z) des points de contrôle recherchés
p = malloc_table_flottant(ac->points.nb);
a = malloc_table_flottant(ac->degree + 1);
mp = malloc_table_flottant(m->nb_lignes);
// X
get_triplets_x_values(&ac->points, p);
matrix_vector_product(m, p, mp);
if (resolution_systeme_lineaire(mmt, mp, a))
EXIT;
set_triplets_x_values(&ac->curve_polygon, a);
// Y
get_triplets_y_values(&ac->points, p);
matrix_vector_product(m, p, mp);
if (resolution_systeme_lineaire(mmt, mp, a))
EXIT;
set_triplets_y_values(&ac->curve_polygon, a);
// Z
get_triplets_z_values(&ac->points, p);
matrix_vector_product(m, p, mp);
if (resolution_systeme_lineaire(mmt, mp, a))
EXIT;
set_triplets_z_values(&ac->curve_polygon, a);
// Libération des ressources
matrix_delete(m);
matrix_delete(mt);
matrix_delete(mmt);
free_table_flottant(a);
free_table_flottant(p);
free_table_flottant(mp);
free_table_flottant(params);
}
static void ac_update_curve_points(struct approximation_curve* ac)
{
int i;
float step;
// Réallocation de la table de points si nécessaire
if (ac->curve_points.nb != ac->curve_point_count)
{
free(ac->curve_points.table);
ac->curve_points.nb = ac->curve_point_count;
ALLOUER(ac->curve_points.table, ac->curve_points.nb);
}
step = 1.f / (ac->curve_point_count - 1);
for (i = 0; i < ac->curve_point_count; ++i)
ac->curve_points.table[i] = casteljau(&ac->curve_polygon, i * step);
}
static void ac_update_curve(struct approximation_curve* ac)
{
ac_update_curve_polygon(ac);
ac_update_curve_points(ac);
}
static void update(struct approximation_curve* ac)
{
if (!(UN_CHAMP_CHANGE(ac) || CREATION(ac)))
return;
if (CHAMP_CHANGE(ac, points)
|| CHAMP_CHANGE(ac, degree)
|| CHAMP_CHANGE(ac, curve_point_count)
|| CHAMP_CHANGE(ac, use_uniform_parameterization))
{
// Intégrité du degré d'approximation spécifié
if (ac->degree < 0)
ac->degree = 0;
else if (ac->degree >= ac->points.nb)
ac->degree = ac->points.nb - 1;
// Intégrité du nombre de points de la courbe à afficher
if (ac->curve_point_count < 0)
ac->curve_point_count = 0;
// MàJ globale de la courbe d'approximation
ac_update_curve(ac);
}
}
CLASSE(approximation_curve, struct approximation_curve,
CHAMP(points,
LABEL("Points à approximer")
L_table_point P_table_triplet
Extrait Obligatoire Affiche Edite Sauve)
CHAMP(degree,
LABEL("Degré de la courbe d'approximation")
L_entier Edite Sauve DEFAUT("10"))
CHAMP(use_uniform_parameterization,
LABEL("Paramétrage uniforme")
L_booleen Edite DEFAUT("1"))
CHAMP(curve_point_count,
LABEL("Nombre de points à afficher")
L_entier Edite Sauve DEFAUT("10"))
CHANGEMENT(update)
CHAMP_VIRTUEL(L_affiche_gl(approximation_curve_rendering))
MENU("Jo/Approximation Bézier")
EVENEMENT("Ctrl+RB")
)