Debian Codegraph @

debian/external_deps/fetk/sg/src/glu/nurbscrv.c

raw
1 /* $Id: nurbscrv.c,v 1.2 2000/11/02 00:28:54 mholst Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 2.0
6 * Copyright (C) 1995-1996 Brian Paul
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
17 *
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the Free
20 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 /*
24 * NURBS implementation written by Bogdan Sikorski ([email protected])
25 * See README2 for more info.
26 */
27
28
29 #include <math.h>
30 #include <stdlib.h>
31 #include "nurbs.h"
32
33
34 static int
35 get_curve_dim(GLenum type)
36 {
37 switch(type)
38 {
39 case GL_MAP1_VERTEX_3: return 3;
40 case GL_MAP1_VERTEX_4: return 4;
41 case GL_MAP1_INDEX: return 1;
42 case GL_MAP1_COLOR_4: return 4;
43 case GL_MAP1_NORMAL: return 3;
44 case GL_MAP1_TEXTURE_COORD_1: return 1;
45 case GL_MAP1_TEXTURE_COORD_2: return 2;
46 case GL_MAP1_TEXTURE_COORD_3: return 3;
47 case GL_MAP1_TEXTURE_COORD_4: return 4;
48 default: abort(); /* TODO: is this OK? */
49 }
50 return 0; /*never get here*/
51 }
52
53 static GLenum
54 test_nurbs_curve(GLUnurbsObj *nobj, curve_attribs *attribs)
55 {
56 GLenum err;
57 GLint tmp_int;
58
59 if(attribs->order < 0)
60 {
61 call_user_error(nobj,GLU_INVALID_VALUE);
62 return GLU_ERROR;
63 }
64 glGetIntegerv(GL_MAX_EVAL_ORDER,&tmp_int);
65 if(attribs->order > tmp_int || attribs->order < 2)
66 {
67 call_user_error(nobj,GLU_NURBS_ERROR1);
68 return GLU_ERROR;
69 }
70 if(attribs->knot_count < attribs->order +2)
71 {
72 call_user_error(nobj,GLU_NURBS_ERROR2);
73 return GLU_ERROR;
74 }
75 if(attribs->stride < 0)
76 {
77 call_user_error(nobj,GLU_NURBS_ERROR34);
78 return GLU_ERROR;
79 }
80 if(attribs->knot==NULL || attribs->ctrlarray==NULL)
81 {
82 call_user_error(nobj,GLU_NURBS_ERROR36);
83 return GLU_ERROR;
84 }
85 if((err=test_knot(attribs->knot_count,attribs->knot,attribs->order))
86 !=GLU_NO_ERROR)
87 {
88 call_user_error(nobj,err);
89 return GLU_ERROR;
90 }
91 return GLU_NO_ERROR;
92 }
93
94 static GLenum
95 test_nurbs_curves(GLUnurbsObj *nobj)
96 {
97 /* test the geometric data */
98 if(test_nurbs_curve(nobj,&(nobj->curve.geom))!=GLU_NO_ERROR)
99 return GLU_ERROR;
100 /* now test the attributive data */
101 /* color */
102 if(nobj->curve.color.type!=GLU_INVALID_ENUM)
103 if(test_nurbs_curve(nobj,&(nobj->curve.color))!=GLU_NO_ERROR)
104 return GLU_ERROR;
105 /* normal */
106 if(nobj->curve.normal.type!=GLU_INVALID_ENUM)
107 if(test_nurbs_curve(nobj,&(nobj->curve.normal))!=GLU_NO_ERROR)
108 return GLU_ERROR;
109 /* texture */
110 if(nobj->curve.texture.type!=GLU_INVALID_ENUM)
111 if(test_nurbs_curve(nobj,&(nobj->curve.texture))!=GLU_NO_ERROR)
112 return GLU_ERROR;
113 return GLU_NO_ERROR;
114 }
115
116 /* prepare the knot information structures */
117 static GLenum
118 fill_knot_structures(GLUnurbsObj *nobj,knot_str_type *geom_knot,
119 knot_str_type *color_knot, knot_str_type *normal_knot,
120 knot_str_type *texture_knot)
121 {
122 GLint order;
123 GLfloat *knot;
124 GLint nknots;
125 GLint t_min,t_max;
126
127 geom_knot->unified_knot=NULL;
128 knot=geom_knot->knot=nobj->curve.geom.knot;
129 nknots=geom_knot->nknots=nobj->curve.geom.knot_count;
130 order=geom_knot->order=nobj->curve.geom.order;
131 geom_knot->delta_nknots=0;
132 t_min=geom_knot->t_min=order-1;
133 t_max=geom_knot->t_max=nknots-order;
134 if(fabs(knot[t_min]-knot[t_max])<EPSILON)
135 {
136 call_user_error(nobj,GLU_NURBS_ERROR3);
137 return GLU_ERROR;
138 }
139 if(fabs(knot[0]-knot[t_min])<EPSILON)
140 {
141 /* knot open at beggining */
142 geom_knot->open_at_begin=GL_TRUE;
143 }
144 else
145 geom_knot->open_at_begin=GL_FALSE;
146 if(fabs(knot[t_max]-knot[nknots-1])<EPSILON)
147 {
148 /* knot open at end */
149 geom_knot->open_at_end=GL_TRUE;
150 }
151 else
152 geom_knot->open_at_end=GL_FALSE;
153 if(nobj->curve.color.type!=GLU_INVALID_ENUM)
154 {
155 color_knot->unified_knot=(GLfloat *)1;
156 knot=color_knot->knot=nobj->curve.color.knot;
157 nknots=color_knot->nknots=nobj->curve.color.knot_count;
158 order=color_knot->order=nobj->curve.color.order;
159 color_knot->delta_nknots=0;
160 t_min=color_knot->t_min=order-1;
161 t_max=color_knot->t_max=nknots-order;
162 if(fabs(knot[t_min]-knot[t_max])<EPSILON)
163 {
164 call_user_error(nobj,GLU_NURBS_ERROR3);
165 return GLU_ERROR;
166 }
167 if(fabs(knot[0]-knot[t_min])<EPSILON)
168 {
169 /* knot open at beggining */
170 color_knot->open_at_begin=GL_TRUE;
171 }
172 else
173 color_knot->open_at_begin=GL_FALSE;
174 if(fabs(knot[t_max]-knot[nknots-1])<EPSILON)
175 {
176 /* knot open at end */
177 color_knot->open_at_end=GL_TRUE;
178 }
179 else
180 color_knot->open_at_end=GL_FALSE;
181 }
182 else
183 color_knot->unified_knot=NULL;
184 if(nobj->curve.normal.type!=GLU_INVALID_ENUM)
185 {
186 normal_knot->unified_knot=(GLfloat *)1;
187 knot=normal_knot->knot=nobj->curve.normal.knot;
188 nknots=normal_knot->nknots=nobj->curve.normal.knot_count;
189 order=normal_knot->order=nobj->curve.normal.order;
190 normal_knot->delta_nknots=0;
191 t_min=normal_knot->t_min=order-1;
192 t_max=normal_knot->t_max=nknots-order;
193 if(fabs(knot[t_min]-knot[t_max])<EPSILON)
194 {
195 call_user_error(nobj,GLU_NURBS_ERROR3);
196 return GLU_ERROR;
197 }
198 if(fabs(knot[0]-knot[t_min])<EPSILON)
199 {
200 /* knot open at beggining */
201 normal_knot->open_at_begin=GL_TRUE;
202 }
203 else
204 normal_knot->open_at_begin=GL_FALSE;
205 if(fabs(knot[t_max]-knot[nknots-1])<EPSILON)
206 {
207 /* knot open at end */
208 normal_knot->open_at_end=GL_TRUE;
209 }
210 else
211 normal_knot->open_at_end=GL_FALSE;
212 }
213 else
214 normal_knot->unified_knot=NULL;
215 if(nobj->curve.texture.type!=GLU_INVALID_ENUM)
216 {
217 texture_knot->unified_knot=(GLfloat *)1;
218 knot=texture_knot->knot=nobj->curve.texture.knot;
219 nknots=texture_knot->nknots=nobj->curve.texture.knot_count;
220 order=texture_knot->order=nobj->curve.texture.order;
221 texture_knot->delta_nknots=0;
222 t_min=texture_knot->t_min=order-1;
223 t_max=texture_knot->t_max=nknots-order;
224 if(fabs(knot[t_min]-knot[t_max])<EPSILON)
225 {
226 call_user_error(nobj,GLU_NURBS_ERROR3);
227 return GLU_ERROR;
228 }
229 if(fabs(knot[0]-knot[t_min])<EPSILON)
230 {
231 /* knot open at beggining */
232 texture_knot->open_at_begin=GL_TRUE;
233 }
234 else
235 texture_knot->open_at_begin=GL_FALSE;
236 if(fabs(knot[t_max]-knot[nknots-1])<EPSILON)
237 {
238 /* knot open at end */
239 texture_knot->open_at_end=GL_TRUE;
240 }
241 else
242 texture_knot->open_at_end=GL_FALSE;
243 }
244 else
245 texture_knot->unified_knot=NULL;
246 return GLU_NO_ERROR;
247 }
248
249 /* covert the NURBS curve into a series of adjacent Bezier curves */
250 static GLenum
251 convert_curve(knot_str_type *the_knot, curve_attribs *attrib,
252 GLfloat **new_ctrl,GLint *ncontrol)
253 {
254 GLenum err;
255
256 if((err=explode_knot(the_knot))!=GLU_NO_ERROR)
257 {
258 if(the_knot->unified_knot)
259 {
260 free(the_knot->unified_knot);
261 the_knot->unified_knot=NULL;
262 }
263 return err;
264 }
265 if(the_knot->unified_knot)
266 {
267 free(the_knot->unified_knot);
268 the_knot->unified_knot=NULL;
269 }
270 if((err=calc_alphas(the_knot))!=GLU_NO_ERROR)
271 {
272 free(the_knot->new_knot);
273 return err;
274 }
275 free(the_knot->new_knot);
276 if((err=calc_new_ctrl_pts(attrib->ctrlarray,attrib->stride,the_knot,
277 attrib->dim,new_ctrl,ncontrol))
278 !=GLU_NO_ERROR)
279 {
280 free(the_knot->alpha);
281 return err;
282 }
283 free(the_knot->alpha);
284 return GLU_NO_ERROR;
285 }
286
287 /* covert curves - geometry and possible attribute ones into equivalent */
288 /* sequence of adjacent Bezier curves */
289 static GLenum
290 convert_curves(GLUnurbsObj *nobj, GLfloat **new_geom_ctrl,
291 GLint *ncontrol, GLfloat **new_color_ctrl, GLfloat **new_normal_ctrl,
292 GLfloat **new_texture_ctrl)
293 {
294 knot_str_type geom_knot,color_knot,normal_knot,texture_knot;
295 GLint junk;
296 GLenum err;
297
298 *new_color_ctrl=*new_normal_ctrl=*new_texture_ctrl=NULL;
299
300 if(fill_knot_structures(nobj,&geom_knot,&color_knot,&normal_knot,
301 &texture_knot)!=GLU_NO_ERROR)
302 return GLU_ERROR;
303
304 /* unify knots - all knots should have the same number of working */
305 /* ranges */
306 if((err=select_knot_working_range(nobj,&geom_knot,&color_knot,&normal_knot,
307 &texture_knot))!=GLU_NO_ERROR)
308 {
309 return err;
310 }
311 /* convert the geometry curve */
312 nobj->curve.geom.dim=get_curve_dim(nobj->curve.geom.type);
313 if((err=convert_curve(&geom_knot,&(nobj->curve.geom),new_geom_ctrl,
314 ncontrol))!=GLU_NO_ERROR)
315 {
316 free_unified_knots(&geom_knot,&color_knot,&normal_knot,&texture_knot);
317 call_user_error(nobj,err);
318 return err;
319 }
320 /* if additional attributive curves are given convert them as well */
321 if(color_knot.unified_knot)
322 {
323 nobj->curve.color.dim=get_curve_dim(nobj->curve.color.type);
324 if((err=convert_curve(&color_knot,&(nobj->curve.color),
325 new_color_ctrl,&junk))!=GLU_NO_ERROR)
326 {
327 free_unified_knots(&geom_knot,&color_knot,&normal_knot,&texture_knot);
328 free(*new_geom_ctrl);
329 call_user_error(nobj,err);
330 return err;
331 }
332 }
333 if(normal_knot.unified_knot)
334 {
335 nobj->curve.normal.dim=get_curve_dim(nobj->curve.normal.type);
336 if((err=convert_curve(&normal_knot,&(nobj->curve.normal),
337 new_normal_ctrl,&junk))!=GLU_NO_ERROR)
338 {
339 free_unified_knots(&geom_knot,&color_knot,&normal_knot,&texture_knot);
340 free(*new_geom_ctrl);
341 if(*new_color_ctrl)
342 free(*new_color_ctrl);
343 call_user_error(nobj,err);
344 return err;
345 }
346 }
347 if(texture_knot.unified_knot)
348 {
349 nobj->curve.texture.dim=get_curve_dim(nobj->curve.texture.type);
350 if((err=convert_curve(&texture_knot,&(nobj->curve.texture),
351 new_texture_ctrl,&junk))!=GLU_NO_ERROR)
352 {
353 free_unified_knots(&geom_knot,&color_knot,&normal_knot,&texture_knot);
354 free(*new_geom_ctrl);
355 if(*new_color_ctrl)
356 free(*new_color_ctrl);
357 if(*new_normal_ctrl)
358 free(*new_normal_ctrl);
359 call_user_error(nobj,err);
360 return err;
361 }
362 }
363 return GLU_NO_ERROR;
364 }
365
366 /* main NURBS curve procedure */
367 void do_nurbs_curve( GLUnurbsObj *nobj)
368 {
369 GLint geom_order,color_order=0,normal_order=0,texture_order=0;
370 GLenum geom_type;
371 GLint n_ctrl;
372 GLfloat *new_geom_ctrl,*new_color_ctrl,*new_normal_ctrl,*new_texture_ctrl;
373 GLfloat *geom_ctrl,*color_ctrl,*normal_ctrl,*texture_ctrl;
374 GLint *factors;
375 GLint i,j;
376 GLint geom_dim,color_dim=0,normal_dim=0,texture_dim=0;
377
378 /* test the user supplied data */
379 if(test_nurbs_curves(nobj)!=GLU_NO_ERROR)
380 return;
381
382 if(convert_curves(nobj,&new_geom_ctrl,&n_ctrl,&new_color_ctrl,
383 &new_normal_ctrl,&new_texture_ctrl)!=GLU_NO_ERROR)
384 return;
385
386 geom_order=nobj->curve.geom.order;
387 geom_type=nobj->curve.geom.type;
388 geom_dim=nobj->curve.geom.dim;
389
390 if(glu_do_sampling_2D(nobj,new_geom_ctrl,n_ctrl,geom_order,geom_dim,
391 &factors)
392 !=GLU_NO_ERROR)
393 {
394 free(new_geom_ctrl);
395 if(new_color_ctrl)
396 free(new_color_ctrl);
397 if(new_normal_ctrl)
398 free(new_normal_ctrl);
399 if(new_texture_ctrl)
400 free(new_texture_ctrl);
401 return;
402 }
403 glEnable(geom_type);
404 if(new_color_ctrl)
405 {
406 glEnable(nobj->curve.color.type);
407 color_dim=nobj->curve.color.dim;
408 color_ctrl=new_color_ctrl;
409 color_order=nobj->curve.color.order;
410 }
411 if(new_normal_ctrl)
412 {
413 glEnable(nobj->curve.normal.type);
414 normal_dim=nobj->curve.normal.dim;
415 normal_ctrl=new_normal_ctrl;
416 normal_order=nobj->curve.normal.order;
417 }
418 if(new_texture_ctrl)
419 {
420 glEnable(nobj->curve.texture.type);
421 texture_dim=nobj->curve.texture.dim;
422 texture_ctrl=new_texture_ctrl;
423 texture_order=nobj->curve.texture.order;
424 }
425 for(i=0 , j=0, geom_ctrl=new_geom_ctrl;
426 i<n_ctrl;
427 i+=geom_order , j++ , geom_ctrl+=geom_order*geom_dim)
428 {
429 if(fine_culling_test_2D(nobj,geom_ctrl,geom_order,geom_dim,geom_dim))
430 {
431 color_ctrl+=color_order*color_dim;
432 normal_ctrl+=normal_order*normal_dim;
433 texture_ctrl+=texture_order*texture_dim;
434 continue;
435 }
436 glMap1f(geom_type, 0.0, 1.0, geom_dim, geom_order, geom_ctrl);
437 if(new_color_ctrl)
438 {
439 glMap1f(nobj->curve.color.type, 0.0, 1.0, color_dim,
440 color_order,color_ctrl);
441 color_ctrl+=color_order*color_dim;
442 }
443 if(new_normal_ctrl)
444 {
445 glMap1f(nobj->curve.normal.type, 0.0, 1.0, normal_dim,
446 normal_order,normal_ctrl);
447 normal_ctrl+=normal_order*normal_dim;
448 }
449 if(new_texture_ctrl)
450 {
451 glMap1f(nobj->curve.texture.type, 0.0, 1.0, texture_dim,
452 texture_order,texture_ctrl);
453 texture_ctrl+=texture_order*texture_dim;
454 }
455 glMapGrid1f(factors[j],0.0,1.0);
456 glEvalMesh1(GL_LINE,0,factors[j]);
457 }
458 free(new_geom_ctrl);
459 free(factors);
460 if(new_color_ctrl)
461 free(new_color_ctrl);
462 if(new_normal_ctrl)
463 free(new_normal_ctrl);
464 if(new_texture_ctrl)
465 free(new_texture_ctrl);
466 }
467
468