File: miUtils.c

package info (click to toggle)
vnc4 4.1.1%2BX4.3.0-31
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 171,548 kB
  • ctags: 288,231
  • sloc: ansic: 2,205,256; cpp: 56,016; sh: 38,092; pascal: 13,773; asm: 12,656; tcl: 9,182; lisp: 7,831; perl: 3,338; makefile: 2,957; yacc: 2,902; objc: 2,698; xml: 2,614; python: 2,383; lex: 1,477; awk: 901; csh: 58; sed: 50
file content (628 lines) | stat: -rw-r--r-- 14,870 bytes parent folder | download | duplicates (6)
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
/* $Xorg: miUtils.c,v 1.4 2001/02/09 02:04:13 xorgcvs Exp $ */
/*

Copyright 1989, 1990, 1991, 1998  The Open Group

Permission to use, copy, modify, distribute, and sell this software and its
documentation for any purpose is hereby granted without fee, provided that
the above copyright notice appear in all copies and that both that
copyright notice and this permission notice appear in supporting
documentation.

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 OPEN GROUP 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 Open Group 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 Open Group.


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.

*/
/* $XFree86: xc/programs/Xserver/PEX5/ddpex/mi/shared/miUtils.c,v 1.9 2001/12/14 19:57:40 dawes Exp $ */

#include "mipex.h"
#include "PEX.h"
#include "PEXprotost.h"
#include "ddpex3.h"
#include "miRender.h"
#include "miWks.h"
#include "pexos.h"


/*++
 |
 |  Function Name:	miMatIdent
 |
 |  Function Description:
 | 	initializes 4x4 matrices to identity.
 |
 --*/

void
miMatIdent(m)
     ddFLOAT        m[4][4];
{
    int      i, j;

    for (i=0; i<4; i++) {
      for (j=0; j<4; j++) {
	m[i][j] = ((i==j) ? 1.0 : 0.0);
      }
    }
}

/*++
 |
 |  Function Name:	miMatCopy
 |
 |  Function Description:
 | 	copies 4x4 mat
 |
 --*/

void
miMatCopy(src, dest)
     ddFLOAT        src[4][4];
     ddFLOAT        dest[4][4];
{
    int      i, j;

    for (i=0; i<4; i++) {
      for (j=0; j<4; j++) {
	dest[i][j] = src[i][j];
      }
    }
}

/*++
 |
 |  Function Name:	miMatTranspose
 |
 |  Function Description:
 | 	transposes 4x4 matrices.
 |
 --*/

void
miMatTranspose(m)
     ddFLOAT        m[4][4];
{
    int      i, j;
    ddFLOAT  t;

    for (i=1; i<4; i++) {
      for (j=0; j<i; j++) {
	t = m[i][j];
	m[i][j] = m[j][i];
	m[j][i] = t;
      }
    }
}

/*++
 |
 |  Function Name:	miMatMult
 |
 |  Function Description:
 | 	implements m = b x a for 4x4 matrices.
 |
 |  Note(s):
 |
 |	NOTE the order of the multiply: BxA *NOT* AxB
 |
 --*/

void
miMatMult(m, a, b)
ddFLOAT		m[4][4];
ddFLOAT		a[4][4];
ddFLOAT		b[4][4];
{
    register	int	i,j,k;
    register	float	*col_ptr;
    register	float	*row_ptr;
    register	float	*result;

    if ((m != a) && (m != b))
    {
     result = &(m[0][0]);
     for (i = 0; i < 4; i++) {
	for (j = 0; j < 4; j++) {
		*result = 0.0;
		col_ptr = &(b[i][0]);
		row_ptr = &(a[0][j]);
		for (k = 0; k < 4; k++) {
			*result += *row_ptr * *(col_ptr++);
			row_ptr += 4;
		}
		result++;
	}
     }
    } else {
     float	t[4][4];

     result = &(t[0][0]);
     for (i = 0; i < 4; i++) {
	for (j = 0; j < 4; j++) {
		*result = 0.0;
		col_ptr = &(b[i][0]);
		row_ptr = &(a[0][j]);
		for (k = 0; k < 4; k++) {
			*result += *row_ptr * *(col_ptr++);
			row_ptr += 4;
		}
		result++;
	}
     }
     memcpy( (char *)m, (char *)t, 16*sizeof(float));
    }
}

/*++
 |
 |  Function Name:	miPrintVertList
 |
 |  Function Description: print a formatted version of the
 |			  miListHeader structure and its contents.
 |
 --*/

void
miPrintVertList(vinput)
/* in */
miListHeader    *vinput;
{
    int			i, j;
    listofddPoint	*pddlist;
    int			vert_count;
    char                *pt;
    int			point_size;

    /*
     * Print each list.
     */
    ErrorF(" Number of lists: %d,  list data type: %d \n",
	   vinput->numLists, vinput->type);

    DD_VertPointSize(vinput->type, point_size);

    pddlist = vinput->ddList;
    for (i = 0; i < vinput->numLists; i++) {
 
      vert_count = pddlist->numPoints;
      ErrorF(" num points, list %d:  %d", i, vert_count);
 
      /*
       *
       */
      pt = (char *)pddlist->pts.p4Dpt;
 
      for (j = 0; j < vert_count; j++) {

         if (DD_IsVertFloat(vinput->type)) {

           if (DD_IsVert2D(vinput->type)) 
		ErrorF("      x %f, y %f \n", 
			((ddCoord4D *)pt)->x, 
			((ddCoord4D *)pt)->y );

           else if (DD_IsVert3D(vinput->type)) 
		ErrorF("      x %f, y %f, z %f \n", 
			((ddCoord4D *)pt)->x, 
			((ddCoord4D *)pt)->y,
			((ddCoord4D *)pt)->z );

           else
		ErrorF("      x %f, y %f, z %f \n", 
			((ddCoord4D *)pt)->x, 
			((ddCoord4D *)pt)->y,
			((ddCoord4D *)pt)->z,
			((ddCoord4D *)pt)->w );


          } else {

           if (DD_IsVert2D(vinput->type)) 
		ErrorF("      x %d, y %d \n", 
			((ddCoord3DS *)pt)->x, 
			((ddCoord3DS *)pt)->y );

           else if (DD_IsVert3D(vinput->type)) 
		ErrorF("      x %d, y %d, z %d \n", 
			((ddCoord3DS *)pt)->x, 
			((ddCoord3DS *)pt)->y,
			((ddCoord3DS *)pt)->z );

          }

	pt += point_size;

      }
      /* Now, ski to next input list */
      pddlist++;
    }
}

/*++
 |
 |  Function Name:	miTransformVector
 |
 |  Function Description: Transform a 3D point by the 3x3
 |  portion of a 4x4 matrix
 |  Added 4/8/91 by JSH. 
 |
 --*/
void
miTransformVector (p3, matrix, xp3)
/* in */
ddVector3D   *p3;
ddFLOAT     matrix[4][4];
/* out */
ddVector3D	*xp3;
{

    xp3->x = matrix[0][0]*p3->x;
    xp3->x += matrix[0][1]*p3->y;
    xp3->x += matrix[0][2]*p3->z;

    xp3->y =  matrix[1][0]*p3->x;
    xp3->y +=  matrix[1][1]*p3->y;
    xp3->y +=  matrix[1][2]*p3->z;

    xp3->z =  matrix[2][0]*p3->x;
    xp3->z +=  matrix[2][1]*p3->y;
    xp3->z +=  matrix[2][2]*p3->z;

}

/*++
 |
 |  Function Name:	miTransformPoint
 |
 |  Function Description: Transform a 4D point by a 4x4 matrix
 |
 --*/
void
miTransformPoint (p4, matrix, xp4)
/* in */
ddCoord4D   *p4;
ddFLOAT     matrix[4][4];
/* out */
ddCoord4D	*xp4;
{

    xp4->x = matrix[0][0]*p4->x;
    xp4->x += matrix[0][1]*p4->y;
    xp4->x += matrix[0][2]*p4->z;
    xp4->x += matrix[0][3]*p4->w;

    xp4->y =  matrix[1][0]*p4->x;
    xp4->y +=  matrix[1][1]*p4->y;
    xp4->y +=  matrix[1][2]*p4->z;
    xp4->y +=  matrix[1][3]*p4->w;

    xp4->z =  matrix[2][0]*p4->x;
    xp4->z +=  matrix[2][1]*p4->y;
    xp4->z +=  matrix[2][2]*p4->z;
    xp4->z +=  matrix[2][3]*p4->w;

    xp4->w =  matrix[3][0]*p4->x;
    xp4->w +=  matrix[3][1]*p4->y;
    xp4->w +=  matrix[3][2]*p4->z;
    xp4->w +=  matrix[3][3]*p4->w;
}

/*++
 |
 |  Function Name:	miMatInverse
 |
 |  Function Description: 
 |	miMatInverse - a fairly robust matrix inversion routine
 |	inverts a 4x4 matrix.  
 |
 |	TODO: If the matrix is singular, call a more robust routine (SVD)
 |	to find a solution. See Numerical Recipes in C
 |
 |
 --*/

void
miMatInverse( a )
    ddFLOAT	a[4][4];
{
    short index[4][2], ipivot[4];
    float pivot[4];
    short row, colum;
    float themax;
    short i, j, k, l;

    for (j = 0; j < 4; j++)
	ipivot[j] = 0;

    for (i = 0; i < 4; i++) {	/* do matrix inversion */
	themax = 0.0;
	for (j = 0; j < 4; j++) {	/* search for pivot element */
	    if (ipivot[j] == 1)
		continue;
	    for (k = 0; k < 4; k++) {
		if (ipivot[k] == 1)
		    continue;
		/* what does this mean? is it another singular case?
		if (ipivot[k] > 1)
		    TODO:
		*/
		if (fabs(themax) < fabs(a[j][k])) {
		    row = j;
		    colum = k;
		    themax = a[j][k];
		}
	    }
	}
	if (MI_NEAR_ZERO(themax)) {
	    /* input matrix is singular, return the an identity matrix */
	    MI_MAT_IDENTITY( a, 4 );
	   /* TODO: restore matix 'a' and call SVD routine */
	    return;
	}
	ipivot[colum] += 1;
	if (row != colum) {	/* interchange rows to put */
	    for (l = 0; l < 4; l++) {
		themax = a[row][l];
		a[row][l] = a[colum][l];
		a[colum][l] = themax;
	    }
	}
	index[i][0] = row;
	index[i][1] = colum;
	pivot[i] = a[colum][colum];
#ifndef NDEBUG
	if ((pivot[i] < 1.0e-6) && (pivot[i] > -1.0e-6) ) {
	    /* input matrix is singular, return the an identity matrix */
	    MI_MAT_IDENTITY( a, 4 );
	   /* TODO: restore matix 'a' and call SVD routine */
	}
#endif
	/* the following isn't needed if we have SVD routine */
	if (MI_NEAR_ZERO(pivot[i])) {
	   pivot[i] = MI_ZERO_TOLERANCE;
	}
	a[colum][colum] = 1.0;	/* divide pivot row by pivot element */
	for (l = 0; l < 4; l++)
	    a[colum][l] /= pivot[i];
	for (j = 0; j < 4; j++)
	    if (j != colum) {
	      themax = a[j][colum];
	      a[j][colum] = 0.0;
	      for (l = 0; l < 4; l++)
		a[j][l] -= a[colum][l] * themax;
	    }
    }

    for (i = 0; i < 4; i++) {	/* interchange columns */
	l = 4 - 1 - i;
	if (index[l][0] != index[l][1]) {
	    row = index[l][0];
	    colum = index[l][1];
	    for (k = 0; k < 4; k++) {
		themax = a[k][row];
		a[k][row] = a[k][colum];
		a[k][colum] = themax;
	    }
	}
    }
    /* determinant is

	(row == column)?1:(-1) * pivot[0] * pivot[1] * pivot[2] * pivot[3]

       if needed*/
}

/*++
 |
 |  Function Name:	miMatInverseTranspose
 |
 |  Function Description: 
 |	miMatInverseTranspose - compute the inverse transpose of a matrix.
 |
 --*/

void
miMatInverseTranspose( m )
    ddFLOAT	m[4][4];
{
	miMatInverse( m );
	miMatTranspose( m );
}

/*++
 |
 |  Function Name:	LostXResource
 |
 |  Function Description:
 |	General purpose procedure to inform ddpex when an X 
 |	resource is deleted.
 |
 |  Note(s):
 |
 --*/

void
LostXResource( pPEXResource, PEXtype, Xtype )
	diResourceHandle	pPEXResource;
	ddResourceType		PEXtype;
	ddXResourceType		Xtype;
{
	/* the only case known to use this is when a drawable is
	 * deleted and a workstation is using the drawable
	 */
    if ( (PEXtype == WORKSTATION_RESOURCE) && (Xtype == X_DRAWABLE_RESOURCE) )
    {
    	register miWksPtr   pwks = (miWksPtr)((diWKSHandle)pPEXResource)->deviceData;

	pwks->pRend->pDrawable = NULL;
	pwks->pRend->drawableId = PEXAlreadyFreed;
    }
    /* no else should ever happen */
    return;
}

/*++
 |
 |  Function Name:	mi_set_filters
 |
 |  Function Description:
 |	sets the filter flags in the ddcontext
 |
 |  Note(s):
 |
 --*/

void
mi_set_filters(pRend, pddc, namesets)
	ddRendererPtr	pRend;
	miDDContext	*pddc;
	ddBitmask	namesets;
{
	ddUSHORT	isempty;
	ddUSHORT	incl_match, excl_match;
	ddUSHORT	invert_incl_match, invert_excl_match;
	ddNamePtr	pnames;

	/* TODO: look at bitmask to smartly change filter_flags
	 * instead of always reseting everything
	 */
	pddc->Dynamic->filter_flags = 0;
	MINS_IS_NAMESET_EMPTY(pddc->Dynamic->currentNames, isempty);

	/* do search first */
	if (pRend->render_mode == MI_REND_SEARCHING)
	{
	    /* filters pass if they are empty, regardless of
	     * whether there are names in the current name set 
	     */
	    pnames = pddc->Static.search.norm_inclusion;
	    MINS_IS_NAMESET_EMPTY( pnames, incl_match );

	    pnames = pddc->Static.search.norm_exclusion;
	    MINS_IS_NAMESET_EMPTY( pnames, excl_match );

	    if (incl_match && excl_match) {
		/* norm list is empty, then all elements pass,
		 * so fake it to pass
		 */
		incl_match = 1;
		excl_match = 0;
	    } else {
		/* norm list is not empty */
	    	pnames = pddc->Static.search.norm_inclusion;
		MINS_MATCH_NAMESETS(pnames, pddc->Dynamic->currentNames, incl_match);
		pnames = pddc->Static.search.norm_exclusion;
		MINS_MATCH_NAMESETS(pnames, pddc->Dynamic->currentNames, excl_match);
	    }

	    pnames = pddc->Static.search.invert_inclusion;
	    MINS_IS_NAMESET_EMPTY( pnames, invert_incl_match );

	    pnames = pddc->Static.search.invert_exclusion;
	    MINS_IS_NAMESET_EMPTY( pnames, invert_excl_match );

	    if (invert_incl_match && invert_excl_match) {
		/* invert list is empty, then all elements rejected,
		 * so fake it to reject
		 */
		invert_incl_match = 0;
		invert_excl_match = 1;
	    } else {
		/* invert list is not empty */
		pnames = pddc->Static.search.invert_inclusion;
		MINS_MATCH_NAMESETS(pnames, pddc->Dynamic->currentNames, invert_incl_match);
		pnames = pddc->Static.search.invert_exclusion;
		MINS_MATCH_NAMESETS(pnames, pddc->Dynamic->currentNames, invert_excl_match);
	    }
	    if ((incl_match && !excl_match) &&
		    !(invert_incl_match && !invert_excl_match))
		MI_DDC_SET_DETECTABLE(pddc);
	}

	/* now, go on to other filters */
	if (isempty)
		/* current name set is empty. no filters pass */
		return;

	/* highlight */
	if ( pRend->ns[DD_HIGH_INCL_NS] )
	{
		pnames = ((miNSHeader *)pRend->ns[DD_HIGH_INCL_NS]->deviceData)->names;
		MINS_MATCH_NAMESETS(pnames, pddc->Dynamic->currentNames, incl_match);
		if ( pRend->ns[DD_HIGH_EXCL_NS] )
		{
			pnames = ((miNSHeader *)pRend->ns[DD_HIGH_EXCL_NS]->deviceData)->names;
			MINS_MATCH_NAMESETS(pnames, pddc->Dynamic->currentNames, excl_match);
		}
		else
			excl_match = 0;
		if (incl_match && !excl_match)
			MI_DDC_SET_HIGHLIGHT(pddc);
	}
	/* else inclusion set is empty; filter does not pass */ 

	/* invisibility */
	if ( pRend->ns[DD_INVIS_INCL_NS] )
	{
		pnames = ((miNSHeader *)pRend->ns[DD_INVIS_INCL_NS]->deviceData)->names;
		MINS_MATCH_NAMESETS(pnames, pddc->Dynamic->currentNames, incl_match);
		if ( pRend->ns[DD_INVIS_EXCL_NS] )
		{
			pnames = ((miNSHeader *)pRend->ns[DD_INVIS_EXCL_NS]->deviceData)->names;
			MINS_MATCH_NAMESETS(pnames, pddc->Dynamic->currentNames, excl_match);
		}
		else
			excl_match = 0;
		if (incl_match && !excl_match)
			MI_DDC_SET_INVISIBLE(pddc);
	}
	/* else inclusion set is empty; filter does not pass */ 

	if (pRend->render_mode == MI_REND_PICKING)
	{
		pnames = pddc->Static.pick.inclusion;
		MINS_MATCH_NAMESETS(pnames, pddc->Dynamic->currentNames, incl_match);
		pnames = pddc->Static.pick.exclusion;
		MINS_MATCH_NAMESETS(pnames, pddc->Dynamic->currentNames, excl_match);
		if (incl_match && !excl_match)
			MI_DDC_SET_DETECTABLE(pddc);
	} 

	if (pRend->render_mode == MI_REND_DRAWING)
		MI_DDC_SET_DETECTABLE(pddc);

	return;
}