Debian Codegraph @

tools/quake3/q3map2/brush.c

raw symbols SCIP
1 /* -------------------------------------------------------------------------------
2
3 Copyright (C) 1999-2007 id Software, Inc. and contributors.
4 For a list of contributors, see the accompanying CONTRIBUTORS file.
5
6 This file is part of GtkRadiant.
7
8 GtkRadiant is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 GtkRadiant 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
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GtkRadiant; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21
22 ----------------------------------------------------------------------------------
23
24 This code has been altered significantly from its original form, to support
25 several games based on the Quake III Arena engine, in the form of "Q3Map2."
26
27 ------------------------------------------------------------------------------- */
28
29
30
31 /* marker */
32 #define BRUSH_C
33
34
35
36 /* dependencies */
37 #include "q3map2.h"
38
39
40
41 /* -------------------------------------------------------------------------------
42
43 functions
44
45 ------------------------------------------------------------------------------- */
46
47 /*
48 AllocSideRef() - ydnar
49 allocates and assigns a brush side reference
50 */
51
52 sideRef_t *AllocSideRef( side_t *side, sideRef_t *next ){
53 sideRef_t *sideRef;
54
55
56 /* dummy check */
57 if ( side == NULL ) {
58 return next;
59 }
60
61 /* allocate and return */
62 sideRef = safe_malloc( sizeof( *sideRef ) );
63 sideRef->side = side;
64 sideRef->next = next;
65 return sideRef;
66 }
67
68
69
70 /*
71 CountBrushList()
72 counts the number of brushes in a brush linked list
73 */
74
75 int CountBrushList( brush_t *brushes ){
76 int c = 0;
77
78
79 /* count brushes */
80 for ( ; brushes != NULL; brushes = brushes->next )
81 c++;
82 return c;
83 }
84
85
86
87 /*
88 AllocBrush()
89 allocates a new brush
90 */
91
92 brush_t *AllocBrush( int numSides ){
93 brush_t *bb;
94 size_t c;
95
96 /* allocate and clear */
97 /*if ( numSides <= 0 ) {
98 Error( "AllocBrush called with numsides = %d", numSides );
99 }
100 c = (size_t)&( ( (brush_t*) 0 )->sides[ numSides ] );*/
101 c = sizeof(*bb) + (numSides > 6 ? sizeof(side_t)*(numSides - 6) : 0);
102 bb = safe_malloc0( c );
103 if ( numthreads == 1 ) {
104 numActiveBrushes++;
105 }
106
107 /* return it */
108 return bb;
109 }
110
111
112
113 /*
114 FreeBrush()
115 frees a single brush and all sides/windings
116 */
117
118 void FreeBrush( brush_t *b ){
119 int i;
120
121
122 /* error check */
123 if ( *( (unsigned int*) b ) == 0xFEFEFEFE ) {
124 Sys_FPrintf( SYS_VRB, "WARNING: Attempt to free an already freed brush!\n" );
125 return;
126 }
127
128 /* free brush sides */
129 for ( i = 0; i < b->numsides; i++ )
130 if ( b->sides[i].winding != NULL ) {
131 FreeWinding( b->sides[ i ].winding );
132 }
133
134 /* ydnar: overwrite it */
135 memset( b, 0xFE, (size_t)&( ( (brush_t*) 0 )->sides[ b->numsides ] ) );
136 *( (unsigned int*) b ) = 0xFEFEFEFE;
137
138 /* free it */
139 free( b );
140 if ( numthreads == 1 ) {
141 numActiveBrushes--;
142 }
143 }
144
145
146
147 /*
148 FreeBrushList()
149 frees a linked list of brushes
150 */
151
152 void FreeBrushList( brush_t *brushes ){
153 brush_t *next;
154
155
156 /* walk brush list */
157 for ( ; brushes != NULL; brushes = next )
158 {
159 next = brushes->next;
160 FreeBrush( brushes );
161 }
162 }
163
164
165
166 /*
167 CopyBrush()
168 duplicates the brush, sides, and windings
169 */
170
171 brush_t *CopyBrush( brush_t *brush ){
172 brush_t *newBrush;
173 size_t size;
174 int i;
175
176
177 /* copy brush */
178 size = (size_t)&( ( (brush_t*) 0 )->sides[ brush->numsides ] );
179 newBrush = AllocBrush( brush->numsides );
180 memcpy( newBrush, brush, size );
181
182 /* ydnar: nuke linked list */
183 newBrush->next = NULL;
184
185 /* copy sides */
186 for ( i = 0; i < brush->numsides; i++ )
187 {
188 if ( brush->sides[ i ].winding != NULL ) {
189 newBrush->sides[ i ].winding = CopyWinding( brush->sides[ i ].winding );
190 }
191 }
192
193 /* return it */
194 return newBrush;
195 }
196
197
198
199
200 /*
201 BoundBrush()
202 sets the mins/maxs based on the windings
203 returns false if the brush doesn't enclose a valid volume
204 */
205
206 qboolean BoundBrush( brush_t *brush ){
207 int i, j;
208 winding_t *w;
209
210
211 ClearBounds( brush->mins, brush->maxs );
212 for ( i = 0; i < brush->numsides; i++ )
213 {
214 w = brush->sides[ i ].winding;
215 if ( w == NULL ) {
216 continue;
217 }
218 for ( j = 0; j < w->numpoints; j++ )
219 AddPointToBounds( w->p[ j ], brush->mins, brush->maxs );
220 }
221
222 for ( i = 0; i < 3; i++ )
223 {
224 if ( brush->mins[ i ] < MIN_WORLD_COORD || brush->maxs[ i ] > MAX_WORLD_COORD || brush->mins[i] >= brush->maxs[ i ] ) {
225 return qfalse;
226 }
227 }
228
229 return qtrue;
230 }
231
232
233
234
235 /*
236 SnapWeldVector() - ydnar
237 welds two vec3_t's into a third, taking into account nearest-to-integer
238 instead of averaging
239 */
240
241 #define SNAP_EPSILON 0.01
242
243 void SnapWeldVector( vec3_t a, vec3_t b, vec3_t out ){
244 int i;
245 vec_t ai, bi, outi;
246
247
248 /* dummy check */
249 if ( a == NULL || b == NULL || out == NULL ) {
250 return;
251 }
252
253 /* do each element */
254 for ( i = 0; i < 3; i++ )
255 {
256 /* round to integer */
257 ai = Q_rint( a[ i ] );
258 bi = Q_rint( b[ i ] );
259
260 /* prefer exact integer */
261 if ( ai == a[ i ] ) {
262 out[ i ] = a[ i ];
263 }
264 else if ( bi == b[ i ] ) {
265 out[ i ] = b[ i ];
266 }
267
268 /* use nearest */
269 else if ( fabs( ai - a[ i ] ) < fabs( bi - b[ i ] ) ) {
270 out[ i ] = a[ i ];
271 }
272 else{
273 out[ i ] = b[ i ];
274 }
275
276 /* snap */
277 outi = Q_rint( out[ i ] );
278 if ( fabs( outi - out[ i ] ) <= SNAP_EPSILON ) {
279 out[ i ] = outi;
280 }
281 }
282 }
283
284
285
286 /*
287 ==================
288 SnapWeldVectorAccu
289
290 Welds two vectors into a third, taking into account nearest-to-integer
291 instead of averaging.
292 ==================
293 */
294 void SnapWeldVectorAccu( vec3_accu_t a, vec3_accu_t b, vec3_accu_t out ){
295 // I'm just preserving what I think was the intended logic of the original
296 // SnapWeldVector(). I'm not actually sure where this function should even
297 // be used. I'd like to know which kinds of problems this function addresses.
298
299 // TODO: I thought we're snapping all coordinates to nearest 1/8 unit?
300 // So what is natural about snapping to the nearest integer? Maybe we should
301 // be snapping to the nearest 1/8 unit instead?
302
303 int i;
304 vec_accu_t ai, bi, ad, bd;
305
306 if ( a == NULL || b == NULL || out == NULL ) {
307 Error( "SnapWeldVectorAccu: NULL argument" );
308 }
309
310 for ( i = 0; i < 3; i++ )
311 {
312 ai = Q_rintAccu( a[i] );
313 bi = Q_rintAccu( b[i] );
314 ad = fabs( ai - a[i] );
315 bd = fabs( bi - b[i] );
316
317 if ( ad < bd ) {
318 if ( ad < SNAP_EPSILON ) {
319 out[i] = ai;
320 }
321 else{out[i] = a[i]; }
322 }
323 else
324 {
325 if ( bd < SNAP_EPSILON ) {
326 out[i] = bi;
327 }
328 else{out[i] = b[i]; }
329 }
330 }
331 }
332
333
334
335 /*
336 FixWinding() - ydnar
337 removes degenerate edges from a winding
338 returns qtrue if the winding is valid
339 */
340
341 #define DEGENERATE_EPSILON 0.1
342
343 qboolean FixWinding( winding_t *w ){
344 qboolean valid = qtrue;
345 int i, j, k;
346 vec3_t vec;
347 float dist;
348
349
350 /* dummy check */
351 if ( !w ) {
352 return qfalse;
353 }
354
355 /* check all verts */
356 for ( i = 0; i < w->numpoints; i++ )
357 {
358 /* don't remove points if winding is a triangle */
359 if ( w->numpoints == 3 ) {
360 return valid;
361 }
362
363 /* get second point index */
364 j = ( i + 1 ) % w->numpoints;
365
366 /* degenerate edge? */
367 VectorSubtract( w->p[ i ], w->p[ j ], vec );
368 dist = VectorLength( vec );
369 if ( dist < DEGENERATE_EPSILON ) {
370 valid = qfalse;
371 //Sys_FPrintf( SYS_VRB, "WARNING: Degenerate winding edge found, fixing...\n" );
372
373 /* create an average point (ydnar 2002-01-26: using nearest-integer weld preference) */
374 SnapWeldVector( w->p[ i ], w->p[ j ], vec );
375 VectorCopy( vec, w->p[ i ] );
376 //VectorAdd( w->p[ i ], w->p[ j ], vec );
377 //VectorScale( vec, 0.5, w->p[ i ] );
378
379 /* move the remaining verts */
380 for ( k = i + 2; k < w->numpoints; k++ )
381 {
382 VectorCopy( w->p[ k ], w->p[ k - 1 ] );
383 }
384 w->numpoints--;
385 }
386 }
387
388 /* one last check and return */
389 if ( w->numpoints < 3 ) {
390 valid = qfalse;
391 }
392 return valid;
393 }
394
395 /*
396 ==================
397 FixWindingAccu
398
399 Removes degenerate edges (edges that are too short) from a winding.
400 Returns qtrue if the winding has been altered by this function.
401 Returns qfalse if the winding is untouched by this function.
402
403 It's advised that you check the winding after this function exits to make
404 sure it still has at least 3 points. If that is not the case, the winding
405 cannot be considered valid. The winding may degenerate to one or two points
406 if the some of the winding's points are close together.
407 ==================
408 */
409 qboolean FixWindingAccu( winding_accu_t *w ){
410 int i, j, k;
411 vec3_accu_t vec;
412 vec_accu_t dist;
413 qboolean done, altered;
414
415 if ( w == NULL ) {
416 Error( "FixWindingAccu: NULL argument" );
417 }
418
419 altered = qfalse;
420
421 while ( qtrue )
422 {
423 if ( w->numpoints < 2 ) {
424 break; // Don't remove the only remaining point.
425 }
426 done = qtrue;
427 for ( i = 0; i < w->numpoints; i++ )
428 {
429 j = ( ( ( i + 1 ) == w->numpoints ) ? 0 : ( i + 1 ) );
430
431 VectorSubtractAccu( w->p[i], w->p[j], vec );
432 dist = VectorLengthAccu( vec );
433 if ( dist < DEGENERATE_EPSILON ) {
434 // TODO: I think the "snap weld vector" was written before
435 // some of the math precision fixes, and its purpose was
436 // probably to address math accuracy issues. We can think
437 // about changing the logic here. Maybe once plane distance
438 // gets 64 bits, we can look at it then.
439 SnapWeldVectorAccu( w->p[i], w->p[j], vec );
440 VectorCopyAccu( vec, w->p[i] );
441 for ( k = j + 1; k < w->numpoints; k++ )
442 {
443 VectorCopyAccu( w->p[k], w->p[k - 1] );
444 }
445 w->numpoints--;
446 altered = qtrue;
447 // The only way to finish off fixing the winding consistently and
448 // accurately is by fixing the winding all over again. For example,
449 // the point at index i and the point at index i-1 could now be
450 // less than the epsilon distance apart. There are too many special
451 // case problems we'd need to handle if we didn't start from the
452 // beginning.
453 done = qfalse;
454 break; // This will cause us to return to the "while" loop.
455 }
456 }
457 if ( done ) {
458 break;
459 }
460 }
461
462 return altered;
463 }
464
465
466 /*
467 CreateBrushWindings()
468 makes basewindigs for sides and mins/maxs for the brush
469 returns false if the brush doesn't enclose a valid volume
470 */
471
472 qboolean CreateBrushWindings( brush_t *brush ){
473 int i, j;
474 #if Q3MAP2_EXPERIMENTAL_HIGH_PRECISION_MATH_FIXES
475 winding_accu_t *w;
476 #else
477 winding_t *w;
478 #endif
479 side_t *side;
480 plane_t *plane;
481
482
483 /* walk the list of brush sides */
484 for ( i = 0; i < brush->numsides; i++ )
485 {
486 /* get side and plane */
487 side = &brush->sides[ i ];
488
489 /* HACK: Workaround, skip planes with bad normals:
490
491 > WARNING: triangle ( 1664 2560 256) ( 1536 2688 256) ( 1584 2640 256) of models/map_catharsis/terrain.ase was not autoclipped
492 > FloatPlane: bad normal
493
494 See: https://gitlab.com/xonotic/netradiant/-/issues/186 */
495 if ( side->planenum == -1 )
496 {
497 Sys_FPrintf( SYS_WRN, "WARNING: UGLY WORKAROUND: Skipping invalid side. This should not happen.\n");
498 Sys_FPrintf( SYS_WRN, "WARNING: FIX ME FOR REAL: https://gitlab.com/xonotic/netradiant/-/issues/186\n" );
499 continue;
500 }
501
502 plane = &mapplanes[ side->planenum ];
503
504 /* make huge winding */
505 #if Q3MAP2_EXPERIMENTAL_HIGH_PRECISION_MATH_FIXES
506 w = BaseWindingForPlaneAccu( plane->normal, plane->dist );
507 #else
508 w = BaseWindingForPlane( plane->normal, plane->dist );
509 #endif
510
511 /* walk the list of brush sides */
512 for ( j = 0; j < brush->numsides && w != NULL; j++ )
513 {
514 if ( i == j ) {
515 continue;
516 }
517 if ( brush->sides[ j ].planenum == ( brush->sides[ i ].planenum ^ 1 ) ) {
518 continue; /* back side clipaway */
519 }
520 if ( brush->sides[ j ].bevel ) {
521 continue;
522 }
523 plane = &mapplanes[ brush->sides[ j ].planenum ^ 1 ];
524 #if Q3MAP2_EXPERIMENTAL_HIGH_PRECISION_MATH_FIXES
525 ChopWindingInPlaceAccu( &w, plane->normal, plane->dist, 0 );
526 #else
527 ChopWindingInPlace( &w, plane->normal, plane->dist, 0 ); // CLIP_EPSILON );
528 #endif
529
530 /* ydnar: fix broken windings that would generate trifans */
531 #if Q3MAP2_EXPERIMENTAL_HIGH_PRECISION_MATH_FIXES
532 // I think it's better to FixWindingAccu() once after we chop with all planes
533 // so that error isn't multiplied. There is nothing natural about welding
534 // the points unless they are the final endpoints. ChopWindingInPlaceAccu()
535 // is able to handle all kinds of degenerate windings.
536 #else
537 FixWinding( w );
538 #endif
539 }
540
541 /* set side winding */
542 #if Q3MAP2_EXPERIMENTAL_HIGH_PRECISION_MATH_FIXES
543 if ( w != NULL ) {
544 FixWindingAccu( w );
545 if ( w->numpoints < 3 ) {
546 FreeWindingAccu( w );
547 w = NULL;
548 }
549 }
550 side->winding = ( w ? CopyWindingAccuToRegular( w ) : NULL );
551 if ( w ) {
552 FreeWindingAccu( w );
553 }
554 #else
555 side->winding = w;
556 #endif
557 }
558
559 /* find brush bounds */
560 return BoundBrush( brush );
561 }
562
563
564
565
566 /*
567 ==================
568 BrushFromBounds
569
570 Creates a new axial brush
571 ==================
572 */
573 brush_t *BrushFromBounds( vec3_t mins, vec3_t maxs ){
574 brush_t *b;
575 int i;
576 vec3_t normal;
577 vec_t dist;
578
579 b = AllocBrush( 6 );
580 b->numsides = 6;
581 for ( i = 0 ; i < 3 ; i++ )
582 {
583 VectorClear( normal );
584 normal[i] = 1;
585 dist = maxs[i];
586 b->sides[i].planenum = FindFloatPlane( normal, dist, 1, (vec3_t*) &maxs );
587
588 normal[i] = -1;
589 dist = -mins[i];
590 b->sides[3 + i].planenum = FindFloatPlane( normal, dist, 1, (vec3_t*) &mins );
591 }
592
593 CreateBrushWindings( b );
594
595 return b;
596 }
597
598 /*
599 ==================
600 BrushVolume
601
602 ==================
603 */
604 vec_t BrushVolume( brush_t *brush ){
605 int i;
606 winding_t *w;
607 vec3_t corner;
608 vec_t d, area, volume;
609 plane_t *plane;
610
611 if ( !brush ) {
612 return 0;
613 }
614
615 // grab the first valid point as the corner
616
617 w = NULL;
618 for ( i = 0 ; i < brush->numsides ; i++ )
619 {
620 w = brush->sides[i].winding;
621 if ( w ) {
622 break;
623 }
624 }
625 if ( !w ) {
626 return 0;
627 }
628 VectorCopy( w->p[0], corner );
629
630 // make tetrahedrons to all other faces
631
632 volume = 0;
633 for ( ; i < brush->numsides ; i++ )
634 {
635 w = brush->sides[i].winding;
636 if ( !w ) {
637 continue;
638 }
639 plane = &mapplanes[brush->sides[i].planenum];
640 d = -( DotProduct( corner, plane->normal ) - plane->dist );
641 area = WindingArea( w );
642 volume += d * area;
643 }
644
645 volume /= 3;
646 return volume;
647 }
648
649
650
651 /*
652 WriteBSPBrushMap()
653 writes a map with the split bsp brushes
654 */
655
656 void WriteBSPBrushMap( char *name, brush_t *list ){
657 FILE *f;
658 side_t *s;
659 int i;
660 winding_t *w;
661
662
663 /* note it */
664 Sys_Printf( "Writing %s\n", name );
665
666 /* open the map file */
667 f = fopen( name, "wb" );
668 if ( f == NULL ) {
669 Error( "Can't write %s\b", name );
670 }
671
672 fprintf( f, "{\n\"classname\" \"worldspawn\"\n" );
673
674 for ( ; list ; list = list->next )
675 {
676 fprintf( f, "{\n" );
677 for ( i = 0,s = list->sides ; i < list->numsides ; i++,s++ )
678 {
679 // TODO: See if we can use a smaller winding to prevent resolution loss.
680 // Is WriteBSPBrushMap() used only to decompile maps?
681 w = BaseWindingForPlane( mapplanes[s->planenum].normal, mapplanes[s->planenum].dist );
682
683 fprintf( f,"( %i %i %i ) ", (int)w->p[0][0], (int)w->p[0][1], (int)w->p[0][2] );
684 fprintf( f,"( %i %i %i ) ", (int)w->p[1][0], (int)w->p[1][1], (int)w->p[1][2] );
685 fprintf( f,"( %i %i %i ) ", (int)w->p[2][0], (int)w->p[2][1], (int)w->p[2][2] );
686
687 fprintf( f, "notexture 0 0 0 1 1\n" );
688 FreeWinding( w );
689 }
690 fprintf( f, "}\n" );
691 }
692 fprintf( f, "}\n" );
693
694 fclose( f );
695
696 }
697
698
699
700 /*
701 FilterBrushIntoTree_r()
702 adds brush reference to any intersecting bsp leafnode
703 */
704
705 int FilterBrushIntoTree_r( brush_t *b, node_t *node ){
706 brush_t *front, *back;
707 int c;
708
709
710 /* dummy check */
711 if ( b == NULL ) {
712 return 0;
713 }
714
715 /* add it to the leaf list */
716 if ( node->planenum == PLANENUM_LEAF ) {
717 /* something somewhere is hammering brushlist */
718 b->next = node->brushlist;
719 node->brushlist = b;
720
721 /* classify the leaf by the structural brush */
722 if ( !b->detail ) {
723 if ( b->opaque ) {
724 node->opaque = qtrue;
725 node->areaportal = qfalse;
726 }
727 else if ( b->compileFlags & C_AREAPORTAL ) {
728 if ( !node->opaque ) {
729 node->areaportal = qtrue;
730 }
731 }
732 }
733
734 return 1;
735 }
736
737 /* split it by the node plane */
738 c = b->numsides;
739 SplitBrush( b, node->planenum, &front, &back );
740 FreeBrush( b );
741
742 c = 0;
743 c += FilterBrushIntoTree_r( front, node->children[ 0 ] );
744 c += FilterBrushIntoTree_r( back, node->children[ 1 ] );
745
746 return c;
747 }
748
749
750
751 /*
752 FilterDetailBrushesIntoTree
753 fragment all the detail brushes into the structural leafs
754 */
755
756 void FilterDetailBrushesIntoTree( entity_t *e, tree_t *tree ){
757 brush_t *b, *newb;
758 int r;
759 int c_unique, c_clusters;
760 int i;
761
762
763 /* note it */
764 Sys_FPrintf( SYS_VRB, "--- FilterDetailBrushesIntoTree ---\n" );
765
766 /* walk the list of brushes */
767 c_unique = 0;
768 c_clusters = 0;
769 for ( b = e->brushes; b; b = b->next )
770 {
771 if ( !b->detail ) {
772 continue;
773 }
774 c_unique++;
775 newb = CopyBrush( b );
776 r = FilterBrushIntoTree_r( newb, tree->headnode );
777 c_clusters += r;
778
779 /* mark all sides as visible so drawsurfs are created */
780 if ( r ) {
781 for ( i = 0; i < b->numsides; i++ )
782 {
783 if ( b->sides[ i ].winding ) {
784 b->sides[ i ].visible = qtrue;
785 }
786 }
787 }
788 }
789
790 /* emit some statistics */
791 Sys_FPrintf( SYS_VRB, "%9d detail brushes\n", c_unique );
792 Sys_FPrintf( SYS_VRB, "%9d cluster references\n", c_clusters );
793 }
794
795 /*
796 =====================
797 FilterStructuralBrushesIntoTree
798
799 Mark the leafs as opaque and areaportals
800 =====================
801 */
802 void FilterStructuralBrushesIntoTree( entity_t *e, tree_t *tree ) {
803 brush_t *b, *newb;
804 int r;
805 int c_unique, c_clusters;
806 int i;
807
808 Sys_FPrintf( SYS_VRB, "--- FilterStructuralBrushesIntoTree ---\n" );
809
810 c_unique = 0;
811 c_clusters = 0;
812 for ( b = e->brushes ; b ; b = b->next ) {
813 if ( b->detail ) {
814 continue;
815 }
816 c_unique++;
817 newb = CopyBrush( b );
818 r = FilterBrushIntoTree_r( newb, tree->headnode );
819 c_clusters += r;
820
821 // mark all sides as visible so drawsurfs are created
822 if ( r ) {
823 for ( i = 0 ; i < b->numsides ; i++ ) {
824 if ( b->sides[i].winding ) {
825 b->sides[i].visible = qtrue;
826 }
827 }
828 }
829 }
830
831 /* emit some statistics */
832 Sys_FPrintf( SYS_VRB, "%9d structural brushes\n", c_unique );
833 Sys_FPrintf( SYS_VRB, "%9d cluster references\n", c_clusters );
834 }
835
836
837
838 /*
839 ================
840 AllocTree
841 ================
842 */
843 tree_t *AllocTree( void ){
844 tree_t *tree;
845
846 tree = safe_malloc0( sizeof( *tree ) );
847 ClearBounds( tree->mins, tree->maxs );
848
849 return tree;
850 }
851
852 /*
853 ================
854 AllocNode
855 ================
856 */
857 node_t *AllocNode( void ){
858 node_t *node;
859
860 node = safe_malloc0( sizeof( *node ) );
861
862 return node;
863 }
864
865
866 /*
867 ================
868 WindingIsTiny
869
870 Returns true if the winding would be crunched out of
871 existance by the vertex snapping.
872 ================
873 */
874 #define EDGE_LENGTH 0.2
875 qboolean WindingIsTiny( winding_t *w ){
876 /*
877 if (WindingArea (w) < 1)
878 return qtrue;
879 return qfalse;
880 */
881 int i, j;
882 vec_t len;
883 vec3_t delta;
884 int edges;
885
886 edges = 0;
887 for ( i = 0 ; i < w->numpoints ; i++ )
888 {
889 j = i == w->numpoints - 1 ? 0 : i + 1;
890 VectorSubtract( w->p[j], w->p[i], delta );
891 len = VectorLength( delta );
892 if ( len > EDGE_LENGTH ) {
893 if ( ++edges == 3 ) {
894 return qfalse;
895 }
896 }
897 }
898 return qtrue;
899 }
900
901 /*
902 ================
903 WindingIsHuge
904
905 Returns true if the winding still has one of the points
906 from basewinding for plane
907 ================
908 */
909 qboolean WindingIsHuge( winding_t *w ){
910 int i, j;
911
912 for ( i = 0 ; i < w->numpoints ; i++ )
913 {
914 for ( j = 0 ; j < 3 ; j++ )
915 if ( w->p[i][j] <= MIN_WORLD_COORD || w->p[i][j] >= MAX_WORLD_COORD ) {
916 return qtrue;
917 }
918 }
919 return qfalse;
920 }
921
922 //============================================================
923
924 /*
925 ==================
926 BrushMostlyOnSide
927
928 ==================
929 */
930 int BrushMostlyOnSide( brush_t *brush, plane_t *plane ){
931 int i, j;
932 winding_t *w;
933 vec_t d, max;
934 int side;
935
936 max = 0;
937 side = PSIDE_FRONT;
938 for ( i = 0 ; i < brush->numsides ; i++ )
939 {
940 w = brush->sides[i].winding;
941 if ( !w ) {
942 continue;
943 }
944 for ( j = 0 ; j < w->numpoints ; j++ )
945 {
946 d = DotProduct( w->p[j], plane->normal ) - plane->dist;
947 if ( d > max ) {
948 max = d;
949 side = PSIDE_FRONT;
950 }
951 if ( -d > max ) {
952 max = -d;
953 side = PSIDE_BACK;
954 }
955 }
956 }
957 return side;
958 }
959
960
961
962 /*
963 SplitBrush()
964 generates two new brushes, leaving the original unchanged
965 */
966
967 void SplitBrush( brush_t *brush, int planenum, brush_t **front, brush_t **back ){
968 brush_t *b[2];
969 int i, j;
970 winding_t *w, *cw[2], *midwinding;
971 plane_t *plane, *plane2;
972 side_t *s, *cs;
973 float d, d_front, d_back;
974
975
976 *front = NULL;
977 *back = NULL;
978 plane = &mapplanes[planenum];
979
980 // check all points
981 d_front = d_back = 0;
982 for ( i = 0 ; i < brush->numsides ; i++ )
983 {
984 w = brush->sides[i].winding;
985 if ( !w ) {
986 continue;
987 }
988 for ( j = 0 ; j < w->numpoints ; j++ )
989 {
990 d = DotProduct( w->p[j], plane->normal ) - plane->dist;
991 if ( d > 0 && d > d_front ) {
992 d_front = d;
993 }
994 if ( d < 0 && d < d_back ) {
995 d_back = d;
996 }
997 }
998 }
999
1000 if ( d_front < 0.1 ) { // PLANESIDE_EPSILON)
1001 // only on back
1002 *back = CopyBrush( brush );
1003 return;
1004 }
1005
1006 if ( d_back > -0.1 ) { // PLANESIDE_EPSILON)
1007 // only on front
1008 *front = CopyBrush( brush );
1009 return;
1010 }
1011
1012 // create a new winding from the split plane
1013 w = BaseWindingForPlane( plane->normal, plane->dist );
1014 for ( i = 0 ; i < brush->numsides && w ; i++ )
1015 {
1016 plane2 = &mapplanes[brush->sides[i].planenum ^ 1];
1017 ChopWindingInPlace( &w, plane2->normal, plane2->dist, 0 ); // PLANESIDE_EPSILON);
1018 }
1019
1020 if ( !w || WindingIsTiny( w ) ) { // the brush isn't really split
1021 int side;
1022
1023 side = BrushMostlyOnSide( brush, plane );
1024 if ( side == PSIDE_FRONT ) {
1025 *front = CopyBrush( brush );
1026 }
1027 if ( side == PSIDE_BACK ) {
1028 *back = CopyBrush( brush );
1029 }
1030 return;
1031 }
1032
1033 if ( WindingIsHuge( w ) ) {
1034 Sys_FPrintf( SYS_VRB,"WARNING: huge winding\n" );
1035 }
1036
1037 midwinding = w;
1038
1039 // split it for real
1040
1041 for ( i = 0 ; i < 2 ; i++ )
1042 {
1043 b[i] = AllocBrush( brush->numsides + 1 );
1044 memcpy( b[i], brush, sizeof( brush_t ) - sizeof( brush->sides ) );
1045 b[i]->numsides = 0;
1046 b[i]->next = NULL;
1047 b[i]->original = brush->original;
1048 }
1049
1050 // split all the current windings
1051
1052 for ( i = 0 ; i < brush->numsides ; i++ )
1053 {
1054 s = &brush->sides[i];
1055 w = s->winding;
1056 if ( !w ) {
1057 continue;
1058 }
1059 /* strict, in parallel case we get the face back because it also is the midwinding */
1060 ClipWindingEpsilonStrict( w, plane->normal, plane->dist,
1061 0 /*PLANESIDE_EPSILON*/, &cw[0], &cw[1] );
1062 for ( j = 0 ; j < 2 ; j++ )
1063 {
1064 if ( !cw[j] ) {
1065 continue;
1066 }
1067 cs = &b[j]->sides[b[j]->numsides];
1068 b[j]->numsides++;
1069 *cs = *s;
1070 cs->winding = cw[j];
1071 }
1072 }
1073
1074
1075 // see if we have valid polygons on both sides
1076 for ( i = 0 ; i < 2 ; i++ )
1077 {
1078 if ( b[i]->numsides < 3 || !BoundBrush( b[i] ) ) {
1079 if ( b[i]->numsides >= 3 ) {
1080 Sys_FPrintf( SYS_VRB,"bogus brush after clip\n" );
1081 }
1082 FreeBrush( b[i] );
1083 b[i] = NULL;
1084 }
1085 }
1086
1087 if ( !( b[0] && b[1] ) ) {
1088 if ( !b[0] && !b[1] ) {
1089 Sys_FPrintf( SYS_VRB,"split removed brush\n" );
1090 }
1091 else{
1092 Sys_FPrintf( SYS_VRB,"split not on both sides\n" );
1093 }
1094 if ( b[0] ) {
1095 FreeBrush( b[0] );
1096 *front = CopyBrush( brush );
1097 }
1098 if ( b[1] ) {
1099 FreeBrush( b[1] );
1100 *back = CopyBrush( brush );
1101 }
1102 return;
1103 }
1104
1105 // add the midwinding to both sides
1106 for ( i = 0 ; i < 2 ; i++ )
1107 {
1108 cs = &b[i]->sides[b[i]->numsides];
1109 b[i]->numsides++;
1110
1111 cs->planenum = planenum ^ i ^ 1;
1112 cs->shaderInfo = NULL;
1113 if ( i == 0 ) {
1114 cs->winding = CopyWinding( midwinding );
1115 }
1116 else{
1117 cs->winding = midwinding;
1118 }
1119 }
1120
1121 {
1122 vec_t v1;
1123 int i;
1124
1125
1126 for ( i = 0 ; i < 2 ; i++ )
1127 {
1128 v1 = BrushVolume( b[i] );
1129 if ( v1 < 1.0 ) {
1130 FreeBrush( b[i] );
1131 b[i] = NULL;
1132 // Sys_FPrintf (SYS_VRB,"tiny volume after clip\n");
1133 }
1134 }
1135 }
1136
1137 *front = b[0];
1138 *back = b[1];
1139 }