-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path3dHier.c
792 lines (716 loc) · 18.7 KB
/
3dHier.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
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
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
/*****************************************************************************
* File: 3dHierarchy.c
*
* Purpose: Internal hierarchical structures support code.
*
* © 1989 Mark M. Owen. All rights reserved.
*****************************************************************************
*/
#include "3dHier.h"
#if XVT_CC_PROTO
char * NewHandleClr(long);
static int copyObject (char **, long);
static int copyHObject (char _huge **, long);
#else
char * NewHandleClr();
static int copyObject ();
static int copyHObject ();
#endif
/**********************************************************************************
*
* Function: copyObject
*
* Purpose: copy an object to a new one (duplicating all data)
* Noye **object is changed in the process
*
**********************************************************************************
*/
static int
#if XVT_CC_PROTO
copyObject (char **object, long size)
#else
copyObject (object, size)
char **object;
long size;
#endif
{
char *origObject;
char *newObject;
origObject = *object;
newObject = (char *) xvt_mem_alloc ( (size_t) size );
*object = newObject;
if (!newObject)
return (FALSE);
memcpy (newObject, origObject, (size_t) size);
return (TRUE);
}
static int
#if XVT_CC_PROTO
copyHObject (char _huge **object, long size)
#else
copyHObject (object, size)
char _huge **object;
long size;
#endif
{
char _huge *origObject;
char _huge *newObject;
origObject = *object;
newObject = (char _huge *) xvt_mem_halloc ( size, 1 );
*object = newObject;
if (!newObject)
return (FALSE);
hmemcpy (newObject, origObject, size);
return (TRUE);
}
/**********************************************************************************
*
* Function: NewHandleClr
*
* Purpose: Allocate a zero filled relocatable block of memory (a Handle).
*
**********************************************************************************
*/
char *
#if XVT_CC_PROTO
NewHandleClr(long size)
#else
NewHandleClr(size)
long size;
#endif
{
return ((char *) xvt_mem_zalloc ( (size_t) size ));
}
/*****************************************************************************
*
* Function: MakePolyFacet(É)
*
* Purpose: Copies an array of vertex indices to a facet, calculates and
* records the facets centoid. Supports virtually any arbitrary
* construct, including points, lines, triangles, pentagons, etc.
* For efficiency, use MakeRectFacet for four sided facets.
*
* Returns: INVALID_REQUEST if < 1 or > MAXFACETV points are requested
* noErr if successfull
*
*****************************************************************************
*/
int MakePolyFacet(pF,pFacetIndex,nIx,pIx,pV)
Facet __huge *pF;
int pFacetIndex;
int nIx;
int *pIx;
Vrtx __huge *pV;
{
register int i;
register FPType sumX, sumY, sumZ, r;
register Point3d *p3d;
if( (nIx < 1) || (nIx > MAXFACETV) )
return INVALID_REQUEST;
sumX = sumY = sumZ = 0.0;
i = nIx;
while( i-- )
{
pF[pFacetIndex].ixVrtx[i] = pIx[i];
p3d = &pV[pIx[i]].p;
sumX += p3d->x;
sumY += p3d->y;
sumZ += p3d->z;
}
pF[pFacetIndex].nIx = nIx;
r = 1.0 / nIx;
pF[pFacetIndex].centroid.x = (Fixed)(sumX * r);
pF[pFacetIndex].centroid.y = (Fixed)(sumY * r);
pF[pFacetIndex].centroid.z = (Fixed)(sumZ * r);
return TRUE;
}
/*****************************************************************************
*
* Function: MakeRectFacet(É)
*
* Purpose: Copies the four vertex indices to a facet, calculates and
* records the facets centoid.
*
*****************************************************************************
*/
void MakeRectFacet(pF,pFacetIndex,ix0,ix1,ix2,ix3,pV)
Facet __huge *pF;
int pFacetIndex;
int ix0,ix1,ix2,ix3;
Vrtx __huge *pV;
{
pF[pFacetIndex].nIx = 4;
pF[pFacetIndex].ixVrtx[0] = ix0;
pF[pFacetIndex].ixVrtx[1] = ix1;
pF[pFacetIndex].ixVrtx[2] = ix2;
pF[pFacetIndex].ixVrtx[3] = ix3;
/* for some reason all this in one line is too
** complicated for mac parsing so done over 3 lines */
pF[pFacetIndex].centroid.x = pV[ix0].p.x + pV[ix1].p.x;
pF[pFacetIndex].centroid.x = pF[pFacetIndex].centroid.x
+ pV[ix2].p.x + pV[ix3].p.x;
pF[pFacetIndex].centroid.x = pF[pFacetIndex].centroid.x * 0.25;
pF[pFacetIndex].centroid.y = pV[ix0].p.y + pV[ix1].p.y;
pF[pFacetIndex].centroid.y = pF[pFacetIndex].centroid.y
+ pV[ix2].p.y + pV[ix3].p.y;
pF[pFacetIndex].centroid.y = pF[pFacetIndex].centroid.y * 0.25;
pF[pFacetIndex].centroid.z = pV[ix0].p.z + pV[ix1].p.z;
pF[pFacetIndex].centroid.z = pF[pFacetIndex].centroid.z
+ pV[ix2].p.z + pV[ix3].p.z;
pF[pFacetIndex].centroid.z = pF[pFacetIndex].centroid.z * 0.25;
}
/*****************************************************************************
*
* Function: NewPatch(É)
*
* Purpose: Given an array of facets and an array of vertices, allocates
* handles for both and copies them to a specific patch index
* within a group.
*
* Returns: INVALID_REQUEST if patch index is bad or hGroup is nil
* noErr if successfull
*
*****************************************************************************
*/
int NewPatch(pG,ix,nV,pV,nF,pF,raOptions,xf)
pGroup pG; /* handle to group */
int ix; /* patch index(within group) */
int nV; /* number of vertices */
Vrtx __huge *pV; /* vertex array */
int nF; /* number of facets */
Facet __huge *pF; /* facet array */
RendAttr raOptions;/* rendering information */
Matrix3D *xf; /* instancing transform */
{
pPatch pP;
if ( ( !pG ) || ( ix<0 ) || ( ix>=(*pG).nP ) )
return INVALID_REQUEST;
pP = (*pG).pP;
memcpy(&(pP[ix].raOptions), &raOptions, sizeof(RendAttr));
pP[ix].instanced = (xf != 0);
if ( xf )
pP[ix].instanceXf = *xf;
pP[ix].refCon = 0L;
if( ( nV>0 ) && pV )
{
pP[ix].nV = nV;
pP[ix].pV = (Vrtx __huge *) xvt_mem_zhalloc(nV, sizeof(Vrtx));
if( pV )
{
hmemcpy((char __huge *) pP[ix].pV, (char __huge *) pV, sizeof(Vrtx)*nV);
}
}
if( ( nF>0 ) && pF )
{
pP[ix].nF = nF;
pP[ix].pF = (Facet __huge *) xvt_mem_zhalloc(nF , sizeof(Facet));
if( pF )
{
hmemcpy((char __huge *) pP[ix].pF, (char __huge *) pF, sizeof(Facet) * nF);
}
}
/* make sure memory was allocaed correctly */
if( (nV && !(pP[ix].pV)) || (nF && !(pP[ix].pF)) )
{ if( pP[ix].pV )
xvt_mem_hfree ((char __huge *) pV);
if( pP[ix].pF )
xvt_mem_hfree ((char __huge *) pF);
return MEMORY_ALLOC_ERROR;
}
return TRUE;
}
/*****************************************************************************
*
* Function: DisposePatch(É)
*
* Purpose: Releases the memory associated with the vertex and facet
* elements of a patch;
*
*****************************************************************************
*/
void DisposePatch(pP,ixP)
pPatch pP;
int ixP;
{
if( pP[ixP].pV )
{
xvt_mem_hfree ( (char *) pP[ixP].pV );
pP[ixP].pV = 0L;
}
if( pP[ixP].pF )
{
xvt_mem_hfree ( (char *) pP[ixP].pF );
pP[ixP].pF = 0L;
}
pP[ixP].nF = 0;
pP[ixP].nV = 0;
}
/*****************************************************************************
*
* Function: ClonePatch(É)
*
* Purpose: Makes a duplicate copy of the vertex and facet elements of a
* patch, allocating new handles for both. The handles to the
* new copies of these items is returned in place of the original
* handles present.
*
* Returns: MEMORY_ALLOC_ERROR if any memory error occurs
* noErr if successful
*
*****************************************************************************
*/
int ClonePatch(pP,ixP)
pPatch pP;
int ixP;
{
if( pP[ixP].pV )
if ( copyHObject((char _huge **) &(pP[ixP].pV),
(long) sizeof(Vrtx)*pP[ixP].nV) != TRUE )
{
pP[ixP].pV = 0L;
return MEMORY_ALLOC_ERROR;
}
if( pP[ixP].pF )
if( copyHObject((char _huge **) &(pP[ixP].pF),
(long) sizeof(Facet)*pP[ixP].nF ) != TRUE )
{
pP[ixP].pF = 0L;
return MEMORY_ALLOC_ERROR;
}
return TRUE;
}
/*****************************************************************************
*
* Function: NewGroup(É)
*
* Purpose: Allocates a handle to an array of N patches.
*
* Returns: INVALID_REQUEST if <= 0 patches are requested
* nil if the handle could not be allocated
* hGroup if successful
*
*****************************************************************************
*/
pGroup NewGroup(nPatches)
int nPatches;
{
pGroup pG;
if(nPatches<=0)
return (pGroup) INVALID_REQUEST;
pG = (pGroup) xvt_mem_alloc ( sizeof(Group) );
if ( pG )
{
(*pG).nP=nPatches;
(*pG).pP=(pPatch) xvt_mem_zalloc( sizeof(Patch) * nPatches );
if(!(*pG).pP)
{
xvt_mem_free ( (char *) pG );
pG = 0L;
}
}
return pG;
}
/*****************************************************************************
*
* Function: DisposeGroup(É)
*
* Purpose: Releases the memory associated with the subordinate elements
* of a group, and the group itself;
*
*****************************************************************************
*/
void DisposeGroup(pG)
pGroup pG;
{
int nP;
if ( pG )
{
if( (*pG).pP )
{
nP = (*pG).nP;
while( nP-- )
DisposePatch( (*pG).pP, nP );
xvt_mem_free( (char *) (*pG).pP );
}
xvt_mem_free ( (char *) pG );
}
}
/*****************************************************************************
*
* Function: CloneGroup(É)
*
* Purpose: Makes a duplicate copy of a group, and all of its subordinate
* elements, allocating new handles for all. The handle to the
* new copy of the group is returned in place of the original
* handle supplied.
*
* Returns: INVALID_REQUEST if group handle is nil
* MEMORY_ALLOC_ERROR if any memory error occurs
* noErr if successful
*
*****************************************************************************
*/
int CloneGroup(ppG)
pGroup *ppG;
{
pGroup pG;
pPatch pP;
int nP;
if( !*ppG )
return INVALID_REQUEST;
pG = *ppG;
if ( copyObject( (char **) &pG, sizeof(Group) )!=TRUE)
return MEMORY_ALLOC_ERROR;
if ( (*pG).pP )
{
if ( copyObject( (char **) &((*pG).pP), sizeof(Patch)*((*pG).nP))!=TRUE)
{
(*pG).pP = 0L;
DisposeGroup( pG );
return MEMORY_ALLOC_ERROR;
}
pP = (*pG).pP;
nP = (*pG).nP;
while( nP-- )
if( ClonePatch( pP, nP ) != TRUE )
{
DisposeGroup( pG );
return MEMORY_ALLOC_ERROR;
}
}
*ppG = pG;
return TRUE;
}
/*****************************************************************************
*
* Function: NewCollection(É)
*
* Purpose: Allocates a handle to a collection of N groups.
*
* Returns: INVALID_REQUEST if <= 0 groups are requested
* nil if the handle could not be allocated
* hCollection if successful
*
*****************************************************************************
*/
pCollection NewCollection(nG)
int nG;
{
pCollection pC;
if( nG<=0 )
return (pCollection)INVALID_REQUEST;
pC = (pCollection) xvt_mem_zalloc
( sizeof(Collection) + nG*sizeof(pGroup) );
if( pC )
{
(*pC).nG = nG;
(*pC).pG = (pGroup *) (pC + 1);
}
return pC;
}
/*****************************************************************************
*
* Function: DisposeCollection(É)
*
* Purpose: Releases the memory associated with all subordinate elements
* of a collection, and the collection itself;
*
*****************************************************************************
*/
void DisposeCollection(pC)
pCollection pC;
{
int nG;
if( pC )
{
nG = (*pC).nG;
while( nG-- )
DisposeGroup( (*pC).pG[nG] );
xvt_mem_free ( (char *) pC );
}
}
/*****************************************************************************
*
* Function: CloneCollection(É)
*
* Purpose: Makes a duplicate copy of a collection, and all its subordinate
* elements, allocating new handles for all. The handle to the
* new copy of the collection is returned in place of the original
* handle supplied.
*
* Returns: INVALID_REQUEST if group handle is nil
* MEMORY_ALLOC_ERROR if any memory error occurs
* noErr if successful
*
*****************************************************************************
*/
int CloneCollection(ppC)
pCollection *ppC;
{
int nG;
pCollection pC;
if( !*ppC )
return INVALID_REQUEST;
pC = *ppC;
if( copyObject( (char **) &pC, sizeof(Collection) ) != TRUE )
return MEMORY_ALLOC_ERROR;
nG = (*pC).nG;
while ( nG--)
if( CloneGroup( &(*pC).pG[nG] ) != TRUE )
{
DisposeCollection( pC );
return MEMORY_ALLOC_ERROR;
}
*ppC = pC;
return TRUE;
}
/*****************************************************************************
*
* Function: SetPatchTransform(É)
*
* Purpose: Sets the instance transform matrix for a patch. A nil matrix
* address will reset the instanced boolean for the patch, with
* the result equivalent to setting the matrix to identity since
* no nested instance transform will be applied.
*
* Returns: INVALID_REQUEST if nil in either hG, or if index is
* out of bounds for the group.
* noErr otherwise
*
*****************************************************************************
*/
int SetPatchTransform(pG,ix,xf)
pGroup pG;
int ix;
Matrix3D *xf;
{
pPatch pP;
if( ( !pG ) || ( ix<0 ) || ( ix>=(*pG).nP ) )
return INVALID_REQUEST;
if( !(*pG).pP )
return INVALID_REQUEST;
pP = (*pG).pP;
pP[ix].instanced = ( xf != 0 );
if( xf )
pP[ix].instanceXf= *xf;
return TRUE;
}
/*****************************************************************************
*
* Function: SetGroupTransform(É)
*
* Purpose: Sets the instance transform for all elements of a given group.
* A matrix address of nil resets the instanced booleans for all
* of the group's elements.
*
* Returns: INVALID_REQUEST if nil group handle
* noErr otherwise
*
*****************************************************************************
*/
int SetGroupTransform(pG,xf)
pGroup pG;
Matrix3D *xf;
{
pPatch pP;
int nP;
if( !pG )
return INVALID_REQUEST;
if( (*pG).pP )
{
pP = (*pG).pP;
nP = (*pG).nP;
while( nP-- )
SetPatchTransform( pG, nP, xf );
}
return TRUE;
}
/*****************************************************************************
*
* Function: SetCollectionTransform(É)
*
* Purpose: Sets the instance transform matrix for all elements of a given
* collection (surface patch level). Passing nil as the matrix
* address resets the instanced boolean for all elements.
*
* Returns: INVALID_REQUEST if nil collection handle
* noErr otherwise
*
*****************************************************************************
*/
int SetCollectionTransform (pC, xf)
pCollection pC;
Matrix3D *xf;
{
int nG;
if( !pC )
return INVALID_REQUEST;
nG = (*pC).nG;
while( nG-- )
SetGroupTransform( (*pC).pG[nG], xf );
return TRUE;
}
/*****************************************************************************
*
* Function: CatPatchTransform(É)
*
* Purpose: Concatenates (multiplies) an instancing transform matrix with
* the existing instance transform matrix of a patch.
*
* Returns: INVALID_REQUEST if nil in either hG, or if index is
* out of bounds for the group.
* noErr otherwise
*
*****************************************************************************
*/
int CatPatchTransform(pG,ix,xf)
pGroup pG;
int ix;
Matrix3D *xf;
{
pPatch pP;
if( ( !pG ) || ( ix<0 ) || ( ix>=(*pG).nP ) || !xf )
return INVALID_REQUEST;
if( !(*pG).pP )
return INVALID_REQUEST;
pP = (*pG).pP;
if( pP[ix].instanced )
{
mMult4x4( &(pP[ix].instanceXf), xf, &(pP[ix].instanceXf) );
}
else
pP[ix].instanceXf= *xf;
pP[ix].instanced = TRUE;
return TRUE;
}
/*****************************************************************************
*
* Function: CatGroupTransform(É)
*
* Purpose: Concatenates (multiplies) an instancing transform matrix with
* the existing instance transform matrices of all elements of a
* given group.
*
* Returns: INVALID_REQUEST if nil group handle
* noErr otherwise
*
*****************************************************************************
*/
int CatGroupTransform(pG, xf)
pGroup pG;
Matrix3D*xf;
{
pPatch pP;
int nP;
if( !pG )
return INVALID_REQUEST;
if( (*pG).pP )
{
pP = (*pG).pP;
nP = (*pG).nP;
while( nP-- )
CatPatchTransform( pG, nP, xf );
}
return TRUE;
}
/*****************************************************************************
*
* Function: CatCollectionTransform(É)
*
* Purpose: Concatenates (multiplies) an instancing transform matrix with
* the existing instance transform matrices of all elements of a
* given collection.
*
* Returns: INVALID_REQUEST if nil collection handle
* noErr otherwise
*
*****************************************************************************
*/
int CatCollectionTransform(pC, xf)
pCollection pC;
Matrix3D *xf;
{
int nG;
if( !pC )
return INVALID_REQUEST;
nG = (*pC).nG;
while( nG-- )
CatGroupTransform( (*pC).pG[nG], xf );
return TRUE;
}
/*****************************************************************************
*
* Function: SetPatchRenderingAttribs(É)
*
* Purpose: Sets the rendering attributes for a patch.
*
* Returns: INVALID_REQUEST if nil in either hG, or if index is
* out of bounds for the group.
* noErr otherwise
*
*****************************************************************************
*/
int SetPatchRenderingAttribs(pG,ix,RA)
pGroup pG;
int ix;
RendAttr RA;
{
pPatch pP;
if( ( !pG ) || ( ix<0 ) || ( ix >= (*pG).nP ) )
return INVALID_REQUEST;
if( !(*pG).pP )
return INVALID_REQUEST;
pP = (*pG).pP;
pP[ix].raOptions = RA;
return TRUE;
}
/*****************************************************************************
*
* Function: SetGroupRenderingAttribs(É)
*
* Purpose: Sets the rendering attributes for all elements of a group.
*
* Returns: INVALID_REQUEST if nil group handle
* noErr otherwise
*
*****************************************************************************
*/
int SetGroupRenderingAttribs(pG,RA)
pGroup pG;
RendAttr RA;
{
int nP;
if( !pG )
return INVALID_REQUEST;
if( (*pG).pP )
{
nP = (*pG).nP;
while( nP-- )
SetPatchRenderingAttribs( pG, nP, RA );
}
return TRUE;
}
/*****************************************************************************
*
* Function: SetCollectionRenderingAttribs(É)
*
* Purpose: Sets the rendering attributes for all elements of a given
* collection (surface patch level).
*
* Returns: INVALID_REQUEST if nil collection handle
* noErr otherwise
*
*****************************************************************************
*/
int SetCollectionRenderingAttribs(pC, RA)
pCollection pC;
RendAttr RA;
{
int nG;
if( !pC )
return INVALID_REQUEST;
nG = (*pC).nG;
while( nG-- )
SetGroupRenderingAttribs( (*pC).pG[nG], RA );
return TRUE;
}