2 Copyright (C) 1999-2007 id Software, Inc. and contributors.
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
5 This file is part of GtkRadiant.
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 GtkRadiant is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
28 extern int numthreads;
30 // counters are only bumped when running single threaded,
31 // because they are an awefull coherence problem
32 int c_active_windings;
37 #define BOGUS_RANGE 8192
39 void pw( winding_t *w ){
41 for ( i = 0 ; i < w->numpoints ; i++ )
42 printf( "(%5.1f, %5.1f, %5.1f)\n",w->p[i][0], w->p[i][1],w->p[i][2] );
51 winding_t *AllocWinding( int points ){
55 if ( numthreads == 1 ) {
57 c_winding_points += points;
59 if ( c_active_windings > c_peak_windings ) {
60 c_peak_windings = c_active_windings;
63 s = sizeof( vec_t ) * 3 * points + sizeof( int );
69 void FreeWinding( winding_t *w ){
70 if ( *(unsigned *)w == 0xdeaddead ) {
71 Error( "FreeWinding: freed a freed winding" );
73 *(unsigned *)w = 0xdeaddead;
75 if ( numthreads == 1 ) {
88 void RemoveColinearPoints( winding_t *w ){
92 vec3_t p[MAX_POINTS_ON_WINDING];
95 for ( i = 0 ; i < w->numpoints ; i++ )
97 j = ( i + 1 ) % w->numpoints;
98 k = ( i + w->numpoints - 1 ) % w->numpoints;
99 VectorSubtract( w->p[j], w->p[i], v1 );
100 VectorSubtract( w->p[i], w->p[k], v2 );
101 VectorNormalize( v1,v1 );
102 VectorNormalize( v2,v2 );
103 if ( DotProduct( v1, v2 ) < 0.999 ) {
104 VectorCopy( w->p[i], p[nump] );
109 if ( nump == w->numpoints ) {
113 if ( numthreads == 1 ) {
114 c_removed += w->numpoints - nump;
117 memcpy( w->p, p, nump * sizeof( p[0] ) );
125 void WindingPlane( winding_t *w, vec3_t normal, vec_t *dist ){
128 VectorSubtract( w->p[1], w->p[0], v1 );
129 VectorSubtract( w->p[2], w->p[0], v2 );
130 CrossProduct( v2, v1, normal );
131 VectorNormalize( normal, normal );
132 *dist = DotProduct( w->p[0], normal );
141 vec_t WindingArea( winding_t *w ){
143 vec3_t d1, d2, cross;
147 for ( i = 2 ; i < w->numpoints ; i++ )
149 VectorSubtract( w->p[i - 1], w->p[0], d1 );
150 VectorSubtract( w->p[i], w->p[0], d2 );
151 CrossProduct( d1, d2, cross );
152 total += 0.5 * VectorLength( cross );
157 void WindingBounds( winding_t *w, vec3_t mins, vec3_t maxs ){
161 mins[0] = mins[1] = mins[2] = 99999;
162 maxs[0] = maxs[1] = maxs[2] = -99999;
164 for ( i = 0 ; i < w->numpoints ; i++ )
166 for ( j = 0 ; j < 3 ; j++ )
184 void WindingCenter( winding_t *w, vec3_t center ){
188 VectorCopy( vec3_origin, center );
189 for ( i = 0 ; i < w->numpoints ; i++ )
190 VectorAdd( w->p[i], center, center );
192 scale = 1.0 / w->numpoints;
193 VectorScale( center, scale, center );
201 winding_t *BaseWindingForPlane( vec3_t normal, vec_t dist ){
204 vec3_t org, vright, vup;
207 // find the major axis
211 for ( i = 0 ; i < 3; i++ )
213 v = fabs( normal[i] );
220 Error( "BaseWindingForPlane: no axis found" );
223 VectorCopy( vec3_origin, vup );
235 v = DotProduct( vup, normal );
236 VectorMA( vup, -v, normal, vup );
237 VectorNormalize( vup, vup );
239 VectorScale( normal, dist, org );
241 CrossProduct( vup, normal, vright );
243 VectorScale( vup, 8192, vup );
244 VectorScale( vright, 8192, vright );
246 // project a really big axis aligned box onto the plane
247 w = AllocWinding( 4 );
249 VectorSubtract( org, vright, w->p[0] );
250 VectorAdd( w->p[0], vup, w->p[0] );
252 VectorAdd( org, vright, w->p[1] );
253 VectorAdd( w->p[1], vup, w->p[1] );
255 VectorAdd( org, vright, w->p[2] );
256 VectorSubtract( w->p[2], vup, w->p[2] );
258 VectorSubtract( org, vright, w->p[3] );
259 VectorSubtract( w->p[3], vup, w->p[3] );
271 winding_t *CopyWinding( winding_t *w ){
275 c = AllocWinding( w->numpoints );
276 size = (int)( (winding_t *)0 )->p[w->numpoints];
277 memcpy( c, w, size );
286 winding_t *ReverseWinding( winding_t *w ){
290 c = AllocWinding( w->numpoints );
291 for ( i = 0 ; i < w->numpoints ; i++ )
293 VectorCopy( w->p[w->numpoints - 1 - i], c->p[i] );
295 c->numpoints = w->numpoints;
305 void ClipWindingEpsilon( winding_t *in, vec3_t normal, vec_t dist,
306 vec_t epsilon, winding_t **front, winding_t **back ){
307 vec_t dists[MAX_POINTS_ON_WINDING + 4];
308 int sides[MAX_POINTS_ON_WINDING + 4];
310 static vec_t dot; // VC 4.2 optimizer bug if not static
317 counts[0] = counts[1] = counts[2] = 0;
319 // determine sides for each point
320 for ( i = 0 ; i < in->numpoints ; i++ )
322 dot = DotProduct( in->p[i], normal );
325 if ( dot > epsilon ) {
326 sides[i] = SIDE_FRONT;
328 else if ( dot < -epsilon ) {
329 sides[i] = SIDE_BACK;
340 *front = *back = NULL;
343 *back = CopyWinding( in );
347 *front = CopyWinding( in );
351 maxpts = in->numpoints + 4; // cant use counts[0]+2 because
352 // of fp grouping errors
354 *front = f = AllocWinding( maxpts );
355 *back = b = AllocWinding( maxpts );
357 for ( i = 0 ; i < in->numpoints ; i++ )
361 if ( sides[i] == SIDE_ON ) {
362 VectorCopy( p1, f->p[f->numpoints] );
364 VectorCopy( p1, b->p[b->numpoints] );
369 if ( sides[i] == SIDE_FRONT ) {
370 VectorCopy( p1, f->p[f->numpoints] );
373 if ( sides[i] == SIDE_BACK ) {
374 VectorCopy( p1, b->p[b->numpoints] );
378 if ( sides[i + 1] == SIDE_ON || sides[i + 1] == sides[i] ) {
382 // generate a split point
383 p2 = in->p[( i + 1 ) % in->numpoints];
385 dot = dists[i] / ( dists[i] - dists[i + 1] );
386 for ( j = 0 ; j < 3 ; j++ )
387 { // avoid round off error when possible
388 if ( normal[j] == 1 ) {
391 else if ( normal[j] == -1 ) {
395 mid[j] = p1[j] + dot * ( p2[j] - p1[j] );
399 VectorCopy( mid, f->p[f->numpoints] );
401 VectorCopy( mid, b->p[b->numpoints] );
405 if ( f->numpoints > maxpts || b->numpoints > maxpts ) {
406 Error( "ClipWinding: points exceeded estimate" );
408 if ( f->numpoints > MAX_POINTS_ON_WINDING || b->numpoints > MAX_POINTS_ON_WINDING ) {
409 Error( "ClipWinding: MAX_POINTS_ON_WINDING" );
419 void ChopWindingInPlace( winding_t **inout, vec3_t normal, vec_t dist, vec_t epsilon ){
421 vec_t dists[MAX_POINTS_ON_WINDING + 4];
422 int sides[MAX_POINTS_ON_WINDING + 4];
424 static vec_t dot; // VC 4.2 optimizer bug if not static
432 counts[0] = counts[1] = counts[2] = 0;
434 // determine sides for each point
435 for ( i = 0 ; i < in->numpoints ; i++ )
437 dot = DotProduct( in->p[i], normal );
440 if ( dot > epsilon ) {
441 sides[i] = SIDE_FRONT;
443 else if ( dot < -epsilon ) {
444 sides[i] = SIDE_BACK;
461 return; // inout stays the same
464 maxpts = in->numpoints + 4; // cant use counts[0]+2 because
465 // of fp grouping errors
467 f = AllocWinding( maxpts );
469 for ( i = 0 ; i < in->numpoints ; i++ )
473 if ( sides[i] == SIDE_ON ) {
474 VectorCopy( p1, f->p[f->numpoints] );
479 if ( sides[i] == SIDE_FRONT ) {
480 VectorCopy( p1, f->p[f->numpoints] );
484 if ( sides[i + 1] == SIDE_ON || sides[i + 1] == sides[i] ) {
488 // generate a split point
489 p2 = in->p[( i + 1 ) % in->numpoints];
491 dot = dists[i] / ( dists[i] - dists[i + 1] );
492 for ( j = 0 ; j < 3 ; j++ )
493 { // avoid round off error when possible
494 if ( normal[j] == 1 ) {
497 else if ( normal[j] == -1 ) {
501 mid[j] = p1[j] + dot * ( p2[j] - p1[j] );
505 VectorCopy( mid, f->p[f->numpoints] );
509 if ( f->numpoints > maxpts ) {
510 Error( "ClipWinding: points exceeded estimate" );
512 if ( f->numpoints > MAX_POINTS_ON_WINDING ) {
513 Error( "ClipWinding: MAX_POINTS_ON_WINDING" );
525 Returns the fragment of in that is on the front side
526 of the cliping plane. The original is freed.
529 winding_t *ChopWinding( winding_t *in, vec3_t normal, vec_t dist ){
532 ClipWindingEpsilon( in, normal, dist, ON_EPSILON, &f, &b );
547 void CheckWinding( winding_t *w ){
551 vec3_t dir, edgenormal, facenormal;
555 if ( w->numpoints < 3 ) {
556 Error( "CheckWinding: %i points",w->numpoints );
559 area = WindingArea( w );
561 Error( "CheckWinding: %f area", area );
564 WindingPlane( w, facenormal, &facedist );
566 for ( i = 0 ; i < w->numpoints ; i++ )
570 for ( j = 0 ; j < 3 ; j++ )
571 if ( p1[j] > BOGUS_RANGE || p1[j] < -BOGUS_RANGE ) {
572 Error( "CheckFace: BUGUS_RANGE: %f",p1[j] );
575 j = i + 1 == w->numpoints ? 0 : i + 1;
577 // check the point is on the face plane
578 d = DotProduct( p1, facenormal ) - facedist;
579 if ( d < -ON_EPSILON || d > ON_EPSILON ) {
580 Error( "CheckWinding: point off plane" );
583 // check the edge isnt degenerate
585 VectorSubtract( p2, p1, dir );
587 if ( VectorLength( dir ) < ON_EPSILON ) {
588 Error( "CheckWinding: degenerate edge" );
591 CrossProduct( facenormal, dir, edgenormal );
592 VectorNormalize( edgenormal, edgenormal );
593 edgedist = DotProduct( p1, edgenormal );
594 edgedist += ON_EPSILON;
596 // all other points must be on front side
597 for ( j = 0 ; j < w->numpoints ; j++ )
602 d = DotProduct( w->p[j], edgenormal );
603 if ( d > edgedist ) {
604 Error( "CheckWinding: non-convex" );
616 int WindingOnPlaneSide( winding_t *w, vec3_t normal, vec_t dist ){
617 qboolean front, back;
623 for ( i = 0 ; i < w->numpoints ; i++ )
625 d = DotProduct( w->p[i], normal ) - dist;
626 if ( d < -ON_EPSILON ) {
633 if ( d > ON_EPSILON ) {