summaryrefslogtreecommitdiff
path: root/xc/programs/Xserver/PEX5/ddpex/mi/level2/miNurbs.c
blob: d7e4c24a4d536554ed0622007408fe907936a652 (plain)
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
/* $XConsortium: miNurbs.c,v 5.4 93/09/03 15:06:20 hersh Exp $ */
/*

Copyright (c) 1989, 1990, 1991  X Consortium

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

Except as contained in this notice, the name of the X Consortium shall
not be used in advertising or otherwise to promote the sale, use or
other dealings in this Software without prior written authorization
from the X Consortium.


Copyright 1989, 1990, 1991 by Sun Microsystems, Inc. 
All Rights Reserved

Permission to use, copy, modify, and distribute this software and its
documentation for any purpose and without fee is hereby granted,
provided that the above copyright notice appear in all copies and that
both that copyright notice and this permission notice appear in
supporting documentation, and that the name of Sun Microsystems
not be used in advertising or publicity pertaining to distribution
of the software without specific, written prior permission.

SUN MICROSYSTEMS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT
SHALL SUN MICROSYSTEMS BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
SOFTWARE.

*/

#include "X.h"
#include "misc.h"
#include "miscstruct.h"
#include "PEXErr.h"
#include "PEXproto.h"
#include "PEXprotost.h"
#include "ddpex.h"
#include "ddpex3.h"
#include "miRender.h"
#include "ddpex2.h"
#include "miNurbs.h"

/*
 * mtx to convert polynomial coeffs ai, to fwd basis coeffs Aj  is
 *        D   j+1     k                     i
 * Aj =	 Sum [Sum (-1) * (j!/k!(j-k)!)*(j-k)  ] * ai   where D=degree
 *       i=0  k=0
 */

#if MAXORD == 4
/* Debugging is often easier if MAXORD is made small. */

double mi_nu_ptofd[MAXORD][MAXORD] = {
{ 1.0, 0.0, 0.0, 0.0},
{ 0.0, 1.0, 1.0, 1.0},
{ 0.0, 0.0, 2.0, 6.0},
{ 0.0, 0.0, 0.0, 6.0}
};

#else

double mi_nu_ptofd[MAXORD][MAXORD] = {
{ 1.0, 0.0, 0.0, 0.0,  0.0,   0.0,    0.0,     0.0,      0.0,       0.0},
{ 0.0, 1.0, 1.0, 1.0,  1.0,   1.0,    1.0,     1.0,      1.0,       1.0},
{ 0.0, 0.0, 2.0, 6.0, 14.0,  30.0,   62.0,   126.0,    254.0,     510.0},
{ 0.0, 0.0, 0.0, 6.0, 36.0, 150.0,  540.0,  1806.0,   5796.0,   18150.0},
{ 0.0, 0.0, 0.0, 0.0, 24.0, 240.0, 1560.0,  8400.0,  40824.0,  186480.0},
{ 0.0, 0.0, 0.0, 0.0,  0.0, 120.0, 1800.0, 16800.0, 126000.0,  834120.0},
{ 0.0, 0.0, 0.0, 0.0,  0.0,   0.0,  720.0, 15120.0, 191520.0, 1905120.0},
{ 0.0, 0.0, 0.0, 0.0,  0.0,   0.0,    0.0,  5040.0, 141120.0, 2328480.0},
{ 0.0, 0.0, 0.0, 0.0,  0.0,   0.0,    0.0,     0.0,  40320.0, 1451520.0},
{ 0.0, 0.0, 0.0, 0.0,  0.0,   0.0,    0.0,     0.0,      0.0,  362880.0}
};

#endif

#define HUGE 10E30



/*++
 |
 |  Function Name:      mi_nu_preprocess_knots
 |
 |  Function Description:
 |
 |  Note(s):
 |
 --*/

void
mi_nu_preprocess_knots( order, nk, knots, rp )
    ddUSHORT		order;
    int		nk;
    ddFLOAT	*knots;
    ddFLOAT	rp[][MAXORD];	/* reciprocal of knots diff */
{
    double x;

    register	int i, j;

    for ( i = 0; i < nk; i++ )
	rp[i][0] = 1.0 ;
    for ( j = 1; j < order; j++ ) {
	for ( i = 0; i <= nk - j; i++ ) {
	    if ( (x = knots[i+j] - knots[i]) == 0.0 ) {
		rp[i][j] = HUGE;
	    } else {
	        rp[i][j] = 1.0 / x;
	    }
	}
    }
}



/*++
 |
 |  Function Name:      mi_nu_compute_nurb_basis_function
 |
 |  Function Description:
 |
 | Recursive definition of polynomial coefficients for span (0<= s <=1).
 | C(i,j,k) = a*C(i,j-1,k-1) + b*C(i,j,k-1) + c*C(i+1,j-1,k-1) + d*C(i+1,j,k-1)
 | a = (s(l+1) - s(l))/(s(i+k-1) - s(i)), b = (s(l) - s(i))/(s(i+k-1) - s(i))
 | c = -(s(l+1) - s(l))/(s(i+k) - s(i+1)), d = (s(i+k) - s(l))/(s(i+k) - s(i+1))
 |
 |  Note(s):
 |
 --*/

void
mi_nu_compute_nurb_basis_function( order, span, knots, kr, C )
    ddUSHORT	order;
    int		span;
    ddFLOAT	*knots;
    ddFLOAT	kr[][MAXORD]; /* reciprocal of knots diff */
    double	C[MAXORD][MAXORD]; 
{
    int		i, j, k, m, im, degree = order - 1;
    double	t0, t1, t2, t3;

    if ( order == 2 ) {
	C[0][0] =  1.0;
	C[0][1] =  0.0;
	C[1][0] = -1.0;
	C[1][1] =  1.0;
	return;
    }

    /* Compute the coefficients of Nik in polynomial basis, for the span
     * knots[i] to knots[i+1] where s goes from 0 to 1.0
     */
    t1 = knots[span+1] - knots[span];
    C[0][degree] = 1.0;			/* Ni1 = 1.0 within span */
    for ( k = 1; k < order; k++ ) {	/* recurse on order for Cj,i,k */
	t0 = t1 * kr[span-k+1][k];
	im = degree - k;
	C[0][im] = t0 * C[0][im+1];		/* top left coeff */
	for ( j = k-1; j > 0; j-- )
	    C[j][im] = t0 * ( C[j][im+1] - C[j-1][im+1] ); /*middle*/
	C[k][im] = -t0 * C[k-1][im+1];		/* top right coeff */ 
	for (m=k-1; m>0; m--) {			/* central section */
	    i = span - m;			/* right edge first */
	    im = degree - m;
	    C[k][im] = t1 * (kr[i][k] * C[k-1][im] - kr[i+1][k] * C[k-1][im+1]);
	    t2 = knots[i+k+1] - knots[span];
	    t3 = knots[span] - knots[i];
	    for ( j = k-1; j > 0; j-- ) 	/* then j down to 1 */
		C[j][im] =
		      kr[i][k] * (t1 * C[j-1][im]
		    + t3 * C[j][im])
		    + kr[i+1][k] * (t2 * C[j][im+1] - t1 * C[j-1][im+1]);
	    C[0][im] = kr[i][k] * t3 * C[0][im]
		+ kr[i+1][k] * t2 * C[0][im+1]; /* left edge */
	}
	t0 = t1 * kr[span][k];			/* bottom rt,middle coeffs */
	for ( j = k; j > 0; j-- )
	    C[j][degree] = t0 * C[j-1][degree]; 
	C[0][degree] = 0.0;			/* bottom left coeff */
    }
}



/*++
 |
 |  Function Name:      mi_nu_insert_knots
 |
 |  Function Description:
 |
 |  Note(s):
 |
 --*/

int
mi_nu_insert_knots( order, pt_type, 
		    numinknots, oknots, opoints, 
		    numoutknots, nknots, npoints )
    ddUSHORT		order;
    ddPointType		pt_type;
    ddUSHORT		numinknots;
    ddFLOAT	*oknots;	/* original knots */
    ddFLOAT	*opoints;	/* original control points */
    int		*numoutknots;
    ddFLOAT	*nknots;	/* new knots */
    ddFLOAT	*npoints;	/* new control points */
{
    /* 
     * Assumptions: - inserted knots are within range of original knots.
     */
    int		i, k, iok, ink, mult, num_pts;
    int		numtmpknots;
    ddFLOAT	*tmpknots;
    ddFLOAT	alpha, alph1;
    ddCoord2D	*npts2;
    ddCoord3D	*npts3;
    ddCoord4D	*npts4;

    /* Check to see if new knots needed. Copy and return if not. */
    if ( *numoutknots <= 0 ) {
	*numoutknots = numinknots;
	memcpy( (char *)nknots, (char *)oknots, (int)numinknots * sizeof(ddFLOAT) );
	return 1;
    }

    /* Copy old control points into new space. */
    num_pts = numinknots - order;
    if ( DD_IsVert2D(pt_type) ) {
	memcpy( (char *)npoints, (char *)opoints, num_pts * sizeof(ddCoord2D));
	npts2 = (ddCoord2D *)npoints;
    } else if ( DD_IsVert3D(pt_type) ) {
	memcpy( (char *)npoints, (char *)opoints, num_pts * sizeof(ddCoord3D));
	npts3 = (ddCoord3D *)npoints;
    } else if ( DD_IsVert4D(pt_type) ) {
	memcpy( (char *)npoints, (char *)opoints, num_pts * sizeof(ddCoord4D));
	npts4 = (ddCoord4D *)npoints;
    } else return (1);

    if ( !(tmpknots = (ddFLOAT *)
	Xalloc( (numinknots + *numoutknots) * sizeof(float))) )
	return 0;

    /* Insert new knots and control points, starting from the end of the
     * original lists.
     */
    memcpy( (char *)tmpknots, (char *)oknots, (int)numinknots * sizeof(ddFLOAT) );
    numtmpknots = numinknots;
    ink = *numoutknots;
    iok = numinknots - 1;

    while ( ink > 0 ) {

	mult = 1;
	--ink;
	/* Count mutiplicity of the new knot to be inserted. */
	while ( ink > 0 && nknots[ink] == nknots[ink-1] ) {
	    ++mult;
	    --ink;
	}

	/* Find position of knot(s) to insert. */
	while ( iok >= 0 && tmpknots[iok] >= nknots[ink] )
	    --iok;

	/* Move control points down to make space for inserted ones.
	 * Use memove so that the overlap is handled.
	 */
	 /* note that the funky &blah[...] notation is equivalent
	    to blah+...  since blah is a pointer. JSH 4-10-91
	 */
	if ( DD_IsVert2D(pt_type) )
	    memmove((char *)(&npts2[iok + 1 + mult]),(char *)(&npts2[iok + 1]), 
		((num_pts - iok) - 1) * sizeof(ddCoord2D) );
	else if ( DD_IsVert3D(pt_type) )
	    memmove((char *)(&npts3[iok + 1 + mult]),(char *)(&npts3[iok + 1]),
		((num_pts - iok) - 1) * sizeof(ddCoord3D) );
	else
	    memmove((char *)(&npts4[iok + 1 + mult]),(char *)(&npts4[iok + 1]),
		((num_pts - iok) - 1) * sizeof(ddCoord4D) );

	/* Do de Boor to insert new knot with multiplicity `mult'. */
	if ( DD_IsVert2D(pt_type) ) {
	    for ( k = 1; k <= mult; k++ ) { 
	    /* Move pts down recursively. */
		for ( i = iok + k; i > iok; i-- ) {
		    npts2[i].x = npts2[i-1].x;
		    npts2[i].y = npts2[i-1].y;
/********************************************************
		    if ( rat == PRATIONAL )
			npts2[i].z = npts2[i-1].z;
********************************************************/
		}
		for ( i = iok; i > iok - order + k; i-- ) { 
		    alpha = (nknots[ink] - tmpknots[i])
			/ (tmpknots[i + order - k] - tmpknots[i]);
		    alph1 = 1.0 - alpha;
		    npts2[i].x = alpha * npts2[i].x + alph1 * npts2[i-1].x;
		    npts2[i].y = alpha * npts2[i].y + alph1 * npts2[i-1].y;
/********************************************************
		    if ( rat == PRATIONAL )
			npts2[i].z = alpha * npts2[i].z + alph1 * npts2[i-1].z;
********************************************************/
		}
	    }

	} else if ( DD_IsVert3D(pt_type) ) { /* dim is 3 */
	    for ( k = 1; k <= mult; k++ ) { 
		for ( i = iok + k; i > iok; i-- ) {
		    npts3[i].x = npts3[i-1].x;
		    npts3[i].y = npts3[i-1].y;
		    npts3[i].z = npts3[i-1].z;
		}
		for ( i = iok; i > iok - order + k; i-- ) { 
		    alpha = (nknots[ink] - tmpknots[i])
			/ (tmpknots[i + order - k] - tmpknots[i]);
		    alph1 = 1.0 - alpha;
		    npts3[i].x = alpha * npts3[i].x + alph1 * npts3[i-1].x;
		    npts3[i].y = alpha * npts3[i].y + alph1 * npts3[i-1].y;
		    npts3[i].z = alpha * npts3[i].z + alph1 * npts3[i-1].z;
		}
	    }
	} else /* if ( DD_IsVert4D(pt_type) ) */ { /* dim is 4 */
	    for ( k = 1; k <= mult; k++ ) { 
		for ( i = iok + k; i > iok; i-- ) {
		    npts4[i].x = npts4[i-1].x;
		    npts4[i].y = npts4[i-1].y;
		    npts4[i].z = npts4[i-1].z;
		    npts4[i].w = npts4[i-1].w;
		}
		for ( i = iok; i > iok - order + k; i-- ) { 
		    alpha = (nknots[ink] - tmpknots[i])
			/ (tmpknots[i + order - k] - tmpknots[i]);
		    alph1 = 1.0 - alpha;
		    npts4[i].x = alpha * npts4[i].x + alph1 * npts4[i-1].x;
		    npts4[i].y = alpha * npts4[i].y + alph1 * npts4[i-1].y;
		    npts4[i].z = alpha * npts4[i].z + alph1 * npts4[i-1].z;
		    npts4[i].w = alpha * npts4[i].w + alph1 * npts4[i-1].w;
		}
	    }
	}

	/* Total number of points and knots increased by `mult'. */
	for ( k = numtmpknots - 1; k > iok; k-- )
	    tmpknots[k + mult] = tmpknots[k];
	for ( k = 1; k <= mult; k++ )
	    tmpknots[iok + k] = nknots[ink];
	numtmpknots += mult;
	num_pts +=mult; 
    }


    /* copy results into output buffers */
    *numoutknots = numtmpknots; /* resulting total knots */
    memcpy( (char *)nknots, (char *)tmpknots, numtmpknots * sizeof(ddFLOAT) );

    free( (char *)tmpknots );
    return 1;
}