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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
|
/*
** License Applicability. Except to the extent portions of this file are
** made subject to an alternative license as permitted in the SGI Free
** Software License B, Version 1.1 (the "License"), the contents of this
** file are subject only to the provisions of the License. You may not use
** this file except in compliance with the License. You may obtain a copy
** of the License at Silicon Graphics, Inc., attn: Legal Services, 1600
** Amphitheatre Parkway, Mountain View, CA 94043-1351, or at:
**
** http://oss.sgi.com/projects/FreeB
**
** Note that, as provided in the License, the Software is distributed on an
** "AS IS" basis, with ALL EXPRESS AND IMPLIED WARRANTIES AND CONDITIONS
** DISCLAIMED, INCLUDING, WITHOUT LIMITATION, ANY IMPLIED WARRANTIES AND
** CONDITIONS OF MERCHANTABILITY, SATISFACTORY QUALITY, FITNESS FOR A
** PARTICULAR PURPOSE, AND NON-INFRINGEMENT.
**
** Original Code. The Original Code is: OpenGL Sample Implementation,
** Version 1.2.1, released January 26, 2000, developed by Silicon Graphics,
** Inc. The Original Code is Copyright (c) 1991-2000 Silicon Graphics, Inc.
** Copyright in any portions created by third parties is as indicated
** elsewhere herein. All Rights Reserved.
**
** Additional Notice Provisions: The application programming interfaces
** established by SGI in conjunction with the Original Code are The
** OpenGL(R) Graphics System: A Specification (Version 1.2.1), released
** April 1, 1999; The OpenGL(R) Graphics System Utility Library (Version
** 1.3), released November 4, 1998; and OpenGL(R) Graphics with the X
** Window System(R) (Version 1.3), released October 19, 1998. This software
** was created using the OpenGL(R) version 1.2.1 Sample Implementation
** published by SGI, but has not been independently verified as being
** compliant with the OpenGL(R) version 1.2.1 Specification.
*/
/*
* tobezier.c++
*
* $Date: 2001/03/17 00:25:41 $ $Revision: 1.1 $
* $Header: /home/krh/git/sync/mesa-cvs-repo/Mesa/src/glu/sgi/libnurbs/internals/tobezier.cc,v 1.1 2001/03/17 00:25:41 brianp Exp $
*/
#include "glimports.h"
#include "myassert.h"
#include "mystdio.h"
#include "mystring.h"
#include "quilt.h"
#include "knotvector.h"
/* local type definitions */
struct Breakpt { /* breakpoints */
Knot value; /* value */
int multi; /* multiplicity */
int def; /* deficit */
};
struct Knotspec { /* knotvector format */
long order; /* order of spline */
Knot_ptr inkbegin; /* input knot sequence */
Knot_ptr inkend; /* location after last knot */
Knot_ptr outkbegin; /* in-process knot subsequence */
Knot_ptr outkend; /* location after last knot */
Knot_ptr kleft; /* */
Knot_ptr kright; /* */
Knot_ptr kfirst; /* */
Knot_ptr klast; /* */
Knot_ptr sbegin; /* conversion factor values */
Breakpt * bbegin; /* in-process breakpoints */
Breakpt * bend; /* last breakpoint */
int ncoords; /* coordinates per control point */
int prestride; /* stride between input points */
int poststride; /* stride between output points */
int preoffset; /* scaled point offset */
int postoffset; /* scaled point offset */
int prewidth; /* width of dimension */
int postwidth; /* width of dimension */
int istransformed; /* was dimension transformed */
Knotspec * next; /* next knotspec */
Knotspec * kspectotrans; /* knotspec in transformation direction */
Knotspec( void );
~Knotspec( void );
void factors( void );
void insert( REAL * );
void preselect();
void select( void );
void copy( INREAL *, REAL * );
void breakpoints( void );
void knots( void );
void transform( REAL * );
void showpts( REAL * );
void pt_io_copy( REAL *, INREAL * );
void pt_oo_copy( REAL *, REAL * );
void pt_oo_sum( REAL*, REAL*, REAL*, Knot, Knot );
};
struct Splinespec { /* a non-uniform tensor element */
Splinespec( int );
~Splinespec(void);
Knotspec *kspec; /* format of each param. dir. */
int dim; /* domain dimension */
REAL * outcpts; /* Bezier control points */
void kspecinit( Knotvector & );
void kspecinit( Knotvector &, Knotvector & );
void select( void );
void layout( long );
void setupquilt( Quilt_ptr );
void copy( INREAL * );
void transform( void );
};
/*-----------------------------------------------------------------------------
* Quilt::toBezier - convert from NURBS to rational Bezier
*-----------------------------------------------------------------------------
*/
void
Quilt::toBezier(
Knotvector& knotvector, /* a knot vector */
INREAL *ctlpts, /* input contol points */
long ncoords ) /* number of coordinates per control point */
{
Splinespec spline( 1 );
spline.kspecinit( knotvector );
spline.select();
spline.layout( ncoords );
spline.setupquilt( this );
spline.copy( ctlpts );
spline.transform();
}
void
Quilt::toBezier(
Knotvector& sknotvector, /* a knot vector */
Knotvector& tknotvector, /* a knot vector */
INREAL *ctlpts, /* input contol points */
long ncoords ) /* number of coordinates per control point */
{
Splinespec spline( 2 );
spline.kspecinit( sknotvector, tknotvector );
spline.select();
spline.layout( ncoords );
spline.setupquilt( this );
spline.copy( ctlpts );
spline.transform();
}
Splinespec::Splinespec( int dimen )
{
dim = dimen;
}
Splinespec::~Splinespec( void )
{
/* Note: do NOT delete 'outcpts' here since its address (not contents)
* is copied in 'cpts' in this file in function Splinespec::setupquilt().
* This block of memory will eventually be deleted in file quilt.c++ in
* function Quilt::deleteMe() through 'cpts' so do NOT delete it here!
*/
Knotspec *ktrav= kspec; //start at beginning of list
while (ktrav != 0) { //any items to delete?
Knotspec *deleteThis= ktrav; //remember to delete this
ktrav= ktrav->next; //go to next item if any
delete deleteThis; //delete it
}
} /* ~Splinespec() */
/*-----------------------------------------------------------------------------
* Splinespec::kspecinit - initialize Splinespec structure
*
* Client: Quilt::toBezier
*-----------------------------------------------------------------------------
*/
void
Splinespec::kspecinit( Knotvector& knotvector )
{
kspec = new Knotspec;
kspec->inkbegin = knotvector.knotlist;
kspec->inkend = knotvector.knotlist + knotvector.knotcount;
kspec->prestride = (int) knotvector.stride;
kspec->order = knotvector.order;
kspec->next = NULL;
}
void
Splinespec::kspecinit( Knotvector& sknotvector, Knotvector& tknotvector )
{
kspec = new Knotspec;
Knotspec *tkspec = new Knotspec;
kspec->inkbegin = sknotvector.knotlist;
kspec->inkend = sknotvector.knotlist + sknotvector.knotcount;
kspec->prestride = (int) sknotvector.stride;
kspec->order = sknotvector.order;
kspec->next = tkspec;
tkspec->inkbegin = tknotvector.knotlist;
tkspec->inkend = tknotvector.knotlist + tknotvector.knotcount;
tkspec->prestride = (int) tknotvector.stride;
tkspec->order = tknotvector.order;
tkspec->next = NULL;
}
/*-----------------------------------------------------------------------------
* Splinespec::select - select the subsegments to copy
*
* Client: gl_quilt_to_bezier
*-----------------------------------------------------------------------------
*/
void
Splinespec::select( )
{
for( Knotspec *knotspec = kspec; knotspec; knotspec = knotspec->next ) {
knotspec->preselect();
knotspec->select();
}
}
/*-----------------------------------------------------------------------------
* Splinespec::layout -
*
* Client: gl_quilt_to_bezier
*-----------------------------------------------------------------------------
*/
void
Splinespec::layout( long ncoords )
{
long stride = ncoords;
for( Knotspec *knotspec = kspec; knotspec; knotspec=knotspec->next ) {
knotspec->poststride = (int) stride;
stride *= ((knotspec->bend-knotspec->bbegin)*knotspec->order + knotspec->postoffset);
knotspec->preoffset *= knotspec->prestride;
knotspec->prewidth *= knotspec->poststride;
knotspec->postwidth *= knotspec->poststride;
knotspec->postoffset *= knotspec->poststride;
knotspec->ncoords = (int) ncoords;
}
outcpts = new REAL[stride];
assert( outcpts != 0 );
}
/*-----------------------------------------------------------------------------
* Splinespec::copy - copy the control points of current subobject
*
* Client: gl_quilt_to_bezier
*-----------------------------------------------------------------------------
*/
void
Splinespec::copy( INREAL *incpts )
{
kspec->copy( incpts, outcpts );
}
/*-----------------------------------------------------------------------------
* Splinespec::setupquilt - assign all quilt variables from knotspec
*
* Client: gl_quilt_to_bezier
*-----------------------------------------------------------------------------
*/
void
Splinespec::setupquilt( Quilt_ptr quilt )
{
Quiltspec_ptr qspec = quilt->qspec;
quilt->eqspec = qspec + dim;
for( Knotspec *knotspec = kspec; knotspec; knotspec=knotspec->next, qspec++ ) {
qspec->stride = knotspec->poststride;
qspec->width = knotspec->bend - knotspec->bbegin;
qspec->order = (int) knotspec->order;
qspec->offset = knotspec->postoffset;
qspec->index = 0;
qspec->bdry[0] = (knotspec->kleft == knotspec->kfirst) ? 1 : 0;
qspec->bdry[1] = (knotspec->kright == knotspec->klast) ? 1 : 0;
qspec->breakpoints = new Knot[qspec->width+1];
Knot_ptr k = qspec->breakpoints;
for( Breakpt *bk = knotspec->bbegin; bk <= knotspec->bend; bk++ )
*(k++) = bk->value;
}
quilt->cpts = outcpts;
quilt->next = 0;
}
/*-----------------------------------------------------------------------------
* Splinespec::transform - convert a spline to Bezier format
*
* Client: gl_quilt_to_bezier
*-----------------------------------------------------------------------------
*/
void
Splinespec::transform( void )
{
Knotspec *knotspec;
for( knotspec = kspec; knotspec; knotspec=knotspec->next )
knotspec->istransformed = 0;
for( knotspec = kspec; knotspec; knotspec=knotspec->next ) {
for( Knotspec *kspec2 = kspec; kspec2; kspec2=kspec2->next )
kspec2->kspectotrans = knotspec;
kspec->transform( outcpts );
knotspec->istransformed = 1;
}
}
/*-----------------------------------------------------------------------------
* Knotspec::Knotspec - constuct a knot spec
*-----------------------------------------------------------------------------
*/
Knotspec::Knotspec( void )
{
bbegin = 0;
sbegin = 0;
outkbegin = 0;
}
/*-----------------------------------------------------------------------------
* Knotspec::copy - copy the control points along minor direction
*
* Client: Splinespec::copy
*-----------------------------------------------------------------------------
*/
void
Knotspec::copy( INREAL *inpt, REAL *outpt )
{
inpt = (INREAL *) (((char *) inpt) + preoffset);
if( next ) {
for( REAL *lpt=outpt+prewidth; outpt != lpt; outpt += poststride ) {
next->copy( inpt, outpt );
inpt = (INREAL *) (((char *) inpt) + prestride);
}
} else {
for( REAL *lpt=outpt+prewidth; outpt != lpt; outpt += poststride ) {
pt_io_copy( outpt, inpt );
inpt = (INREAL *) (((char *) inpt) + prestride);
}
}
}
/*-----------------------------------------------------------------------------
* Knotspec::showpts - print out points before transformation
*
* Client: Knotspec::select
*-----------------------------------------------------------------------------
*/
void
Knotspec::showpts( REAL *outpt )
{
if( next ) {
for( REAL *lpt=outpt+prewidth; outpt != lpt; outpt += poststride )
next->showpts( outpt );
} else {
for( REAL *lpt=outpt+prewidth; outpt != lpt; outpt += poststride )
dprintf( "show %g %g %g\n", outpt[0], outpt[1], outpt[2] );
}
}
/*-----------------------------------------------------------------------------
* Knotspec::factors - precompute scale factors
* - overwrites knot vector, actual new knot vector is NOT produced
*
* Client: Knotspec::select
*-----------------------------------------------------------------------------
*/
void
Knotspec::factors( void )
{
Knot *mid = (outkend - 1) - order + bend->multi;
Knot_ptr fptr = sbegin;
for( Breakpt *bpt = bend; bpt >= bbegin; bpt-- ) {
mid -= bpt->multi; // last knot less than knot to insert
int def = bpt->def - 1; // number of knots to insert
if( def <= 0 ) continue;
Knot kv = bpt->value; // knot to insert
Knot *kf = (mid-def) + (order-1);
for( Knot *kl = kf + def; kl != kf; kl-- ) {
Knot *kh, *kt;
for( kt=kl, kh=mid; kt != kf; kh--, kt-- )
*(fptr++) = (kv - *kh) / (*kt - *kh);
*kl = kv;
}
}
}
/*-----------------------------------------------------------------------------
* Knotspec::insert - convert subobject in direction of kspec into Bezier
*
* Client: Knotspec::transform
*-----------------------------------------------------------------------------
*/
void
Knotspec::insert( REAL *p )
{
Knot_ptr fptr = sbegin;
REAL *srcpt = p + prewidth - poststride;
REAL *dstpt = p + postwidth + postoffset - poststride;
Breakpt *bpt = bend;
for( REAL *pend = srcpt - poststride*bpt->def; srcpt != pend; pend +=poststride ) {
REAL *p1 = srcpt;
for( REAL *p2 = srcpt-poststride; p2 != pend; p1 = p2, p2 -= poststride ) {
pt_oo_sum( p1, p1, p2, *fptr, 1.0-*fptr );
fptr++;
}
}
for( --bpt; bpt >= bbegin; bpt-- ) {
for( int multi = bpt->multi; multi > 0; multi-- ) {
pt_oo_copy( dstpt, srcpt );
dstpt -= poststride;
srcpt -= poststride;
}
for( REAL *pend = srcpt - poststride*bpt->def; srcpt != pend; pend +=poststride, dstpt-=poststride ) {
pt_oo_copy( dstpt, srcpt );
REAL *p1 = srcpt;
for( REAL *p2 = srcpt-poststride; p2 != pend; p1=p2, p2 -= poststride ) {
pt_oo_sum( p1, p1, p2, *fptr, 1.0-*fptr );
fptr++;
}
}
}
}
/*-----------------------------------------------------------------------------
* Knotspec::preselect - initialize kspec for processing
*
* Client: Splinespec::select
*-----------------------------------------------------------------------------
*/
void
Knotspec::preselect( void )
{
Knot kval;
/* position klast after last knot of "last" breakpoint */
for( klast = inkend - order, kval = *klast; klast != inkend; klast++ )
if( ! identical( *klast, kval ) ) break;
/* position kfirst after last knot of "first" breakpoint */
for( kfirst = inkbegin+order-1, kval= *kfirst; kfirst != inkend; kfirst++ )
if( ! identical( *kfirst, kval ) ) break;
/* compute multiplicity of first breakpoint */
Knot_ptr k;
for( k = kfirst - 1; k >= inkbegin; k-- )
if( ! identical( kval, *k ) ) break;
k++;
/* allocate space for breakpoints -
use worst case estimate on number of breakpoints */
bbegin = new Breakpt[(klast - kfirst)+1];
/* record multiplicity and value of first breakpoint */
bbegin->multi = kfirst - k;
bbegin->value = kval;
bend = bbegin;
kleft = kright = kfirst;
}
/*-----------------------------------------------------------------------------
* Knotspec::select - Knotspec::select segments and precompute scale factors
*
* Client: Splinespec::select
*-----------------------------------------------------------------------------
*/
void
Knotspec::select( void )
{
breakpoints();
knots();
factors();
preoffset = kleft - (inkbegin + order);
postwidth = (int)((bend - bbegin) * order);
prewidth = (int)((outkend - outkbegin) - order);
postoffset = (bbegin->def > 1) ? (bbegin->def-1) : 0;
}
/*-----------------------------------------------------------------------------
* Knotspec::breakpoints - compute breakpoints for knotspec
*
* Client: Knotspec::select
*-----------------------------------------------------------------------------
*/
void
Knotspec::breakpoints( void )
{
Breakpt *ubpt = bbegin;
Breakpt *ubend = bend;
long nfactors = 0;
ubpt->value = ubend->value;
ubpt->multi = ubend->multi;
kleft = kright;
for( ; kright != klast; kright++ ) {
if ( identical(*kright,ubpt->value) ) {
(ubpt->multi)++;
} else {
ubpt->def = (int) (order - ubpt->multi);
nfactors += (ubpt->def * (ubpt->def - 1)) / 2;
(++ubpt)->value = *kright;
ubpt->multi = 1;
}
}
ubpt->def = (int) (order - ubpt->multi);
nfactors += (ubpt->def * (ubpt->def - 1)) / 2;
bend = ubpt;
if( nfactors ) {
sbegin = new Knot[nfactors];
} else {
sbegin = NULL;
}
}
/*-----------------------------------------------------------------------------
* Knotspec::knots - copy relevant subsequence of knots into temporary area
*
* Client: Knotspec::select
*-----------------------------------------------------------------------------
*/
void
Knotspec::knots( void )
{
Knot_ptr inkpt = kleft - order;
Knot_ptr inkend = kright + bend->def;
/* allocate space for knots and factors */
outkbegin = new Knot[inkend-inkpt];
Knot_ptr outkpt;
for( outkpt = outkbegin; inkpt != inkend; inkpt++, outkpt++ )
*outkpt = *inkpt;
outkend = outkpt;
}
/*-----------------------------------------------------------------------------
* Knotspec::transform - convert a spline along a given direction
*
* Client: Splienspec::transform
*-----------------------------------------------------------------------------
*/
void
Knotspec::transform( REAL *p )
{
if( next ) {
if( this == kspectotrans ) {
next->transform( p );
} else {
if( istransformed ) {
p += postoffset;
for( REAL *pend = p + postwidth; p != pend; p += poststride )
next->transform( p );
} else {
REAL *pend = p + prewidth;
for( ; p != pend; p += poststride )
next->transform( p );
}
}
} else {
if( this == kspectotrans ) {
insert( p );
} else {
if( istransformed ) {
p += postoffset;
for( REAL *pend = p + postwidth; p != pend; p += poststride )
kspectotrans->insert( p );
} else {
REAL *pend = p + prewidth;
for( ; p != pend; p += poststride )
kspectotrans->insert( p );
}
}
}
}
/*-----------------------------------------------------------------------------
* Knotspec::~Knotspec - free space alocated for knotspec
*-----------------------------------------------------------------------------
*/
Knotspec::~Knotspec( void )
{
if( bbegin ) delete[] bbegin;
if( sbegin ) delete[] sbegin;
if( outkbegin ) delete[] outkbegin;
}
/*-----------------------------------------------------------------------------
* pt_io_copy - make internal copy of input cntrl pt. of x coords
*-----------------------------------------------------------------------------
*/
void
Knotspec::pt_io_copy( REAL *topt, INREAL *frompt )
{
switch( ncoords ) {
case 4:
topt[3] = (REAL) frompt[3];
case 3:
topt[2] = (REAL) frompt[2];
case 2:
topt[1] = (REAL) frompt[1];
case 1:
topt[0] = (REAL) frompt[0];
break;
default: {
for( int i = 0; i < ncoords; i++ )
*topt++ = (REAL) *frompt++;
}
}
}
/*-----------------------------------------------------------------------------
* pt_oo_copy - make internal copy of internal cntrl pt. of x coords
*-----------------------------------------------------------------------------
*/
void
Knotspec::pt_oo_copy( REAL *topt, REAL *frompt )
{
switch( ncoords ) {
case 4:
topt[3] = frompt[3];
case 3:
topt[2] = frompt[2];
case 2:
topt[1] = frompt[1];
case 1:
topt[0] = frompt[0];
break;
default:
memcpy( topt, frompt, ncoords * sizeof( REAL ) );
}
}
/*-----------------------------------------------------------------------------
* pt_oo_sum - compute affine combination of internal cntrl pts
*-----------------------------------------------------------------------------
*/
void
Knotspec::pt_oo_sum( REAL *x, REAL *y, REAL *z, Knot a, Knot b )
{
switch( ncoords ) {
case 4:
x[3] = a * y[3] + b * z[3];
case 3:
x[2] = a * y[2] + b * z[2];
case 2:
x[1] = a * y[1] + b * z[1];
case 1:
x[0] = a * y[0] + b * z[0];
break;
default: {
for( int i = 0; i < ncoords; i++ )
*x++ = a * *y++ + b * *z++;
}
}
}
|