2 Copyright (C) 1999-2006 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
30 extern int numthreads;
32 // counters are only bumped when running single threaded,
33 // because they are an awefull coherence problem
34 int c_active_windings;
39 #define BOGUS_RANGE WORLD_SIZE
44 for (i=0 ; i<w->numpoints ; i++)
45 Sys_Printf ("(%5.1f, %5.1f, %5.1f)\n",w->p[i][0], w->p[i][1],w->p[i][2]);
54 winding_t *AllocWinding (int points)
59 if (points >= MAX_POINTS_ON_WINDING)
60 Error ("AllocWinding failed: MAX_POINTS_ON_WINDING exceeded");
65 c_winding_points += points;
67 if (c_active_windings > c_peak_windings)
68 c_peak_windings = c_active_windings;
70 s = sizeof(vec_t)*3*points + sizeof(int);
76 void FreeWinding (winding_t *w)
78 if (*(unsigned *)w == 0xdeaddead)
79 Error ("FreeWinding: freed a freed winding");
80 *(unsigned *)w = 0xdeaddead;
94 void RemoveColinearPoints (winding_t *w)
99 vec3_t p[MAX_POINTS_ON_WINDING];
102 for (i=0 ; i<w->numpoints ; i++)
104 j = (i+1)%w->numpoints;
105 k = (i+w->numpoints-1)%w->numpoints;
106 VectorSubtract (w->p[j], w->p[i], v1);
107 VectorSubtract (w->p[i], w->p[k], v2);
108 VectorNormalize(v1,v1);
109 VectorNormalize(v2,v2);
110 if (DotProduct(v1, v2) < 0.999)
112 VectorCopy (w->p[i], p[nump]);
117 if (nump == w->numpoints)
121 c_removed += w->numpoints - nump;
123 memcpy (w->p, p, nump*sizeof(p[0]));
131 void WindingPlane (winding_t *w, vec3_t normal, vec_t *dist)
135 VectorSubtract (w->p[1], w->p[0], v1);
136 VectorSubtract (w->p[2], w->p[0], v2);
137 CrossProduct (v2, v1, normal);
138 VectorNormalize (normal, normal);
139 *dist = DotProduct (w->p[0], normal);
148 vec_t WindingArea (winding_t *w)
151 vec3_t d1, d2, cross;
155 for (i=2 ; i<w->numpoints ; i++)
157 VectorSubtract (w->p[i-1], w->p[0], d1);
158 VectorSubtract (w->p[i], w->p[0], d2);
159 CrossProduct (d1, d2, cross);
160 total += 0.5 * VectorLength ( cross );
165 void WindingBounds (winding_t *w, vec3_t mins, vec3_t maxs)
170 mins[0] = mins[1] = mins[2] = 99999;
171 maxs[0] = maxs[1] = maxs[2] = -99999;
173 for (i=0 ; i<w->numpoints ; i++)
175 for (j=0 ; j<3 ; j++)
191 void WindingCenter (winding_t *w, vec3_t center)
196 VectorCopy (vec3_origin, center);
197 for (i=0 ; i<w->numpoints ; i++)
198 VectorAdd (w->p[i], center, center);
200 scale = 1.0/w->numpoints;
201 VectorScale (center, scale, center);
209 winding_t *BaseWindingForPlane (vec3_t normal, vec_t dist)
211 // The goal in this function is to replicate the exact behavior that was in the original
212 // BaseWindingForPlane() function (see below). The only thing we're going to change is the
213 // accuracy of the operation. The original code gave a preference for the vup vector to start
214 // out as (0, 0, 1), unless the normal had a dominant Z value, in which case vup started out
215 // as (1, 0, 0). After that, vup was "bent" [along the plane defined by normal and vup] to
216 // become perpendicular to normal. After that the vright vector was computed as the cross
217 // product of vup and normal.
219 // Once these vectors are calculated, I'm constructing the winding points in exactly the same
220 // way as was done in the original function. Orientation is the same.
222 // Note that the 4 points in the returned winding_t may actually not be necessary (3 might
223 // be enough). However, I want to minimize the chance of ANY bugs popping up due to any
224 // change in behavior of this function. Therefore, behavior stays exactly the same, except
225 // for precision of math. Performance might be better in the new function as well.
229 vec3_t vright, vup, org;
234 for (i = 0; i < 3; i++) {
241 if (x == -1) Error("BaseWindingForPlane: no axis found");
244 case 0: // Fall through to next case.
246 vright[0] = -normal[1];
247 vright[1] = normal[0];
252 vright[1] = -normal[2];
253 vright[2] = normal[1];
256 // NOTE: vright is NOT a unit vector at this point.
257 VectorSetLength(vright, MAX_WORLD_COORD * 2, vright);
258 CrossProduct(normal, vright, vup);
259 VectorScale(normal, dist, org);
263 VectorSubtract(org, vright, w->p[0]);
264 VectorAdd(w->p[0], vup, w->p[0]);
266 VectorAdd(org, vright, w->p[1]);
267 VectorAdd(w->p[1], vup, w->p[1]);
269 VectorAdd(org, vright, w->p[2]);
270 VectorSubtract(w->p[2], vup, w->p[2]);
272 VectorSubtract(org, vright, w->p[3]);
273 VectorSubtract(w->p[3], vup, w->p[3]);
280 // Old function, not used but here for reference. Please do not modify it.
281 // (You may remove it at some point.)
282 winding_t *_BaseWindingForPlane_orig_(vec3_t normal, vec_t dist)
286 vec3_t org, vright, vup;
289 // find the major axis
303 Error ("BaseWindingForPlane: no axis found");
305 VectorCopy (vec3_origin, vup);
317 v = DotProduct (vup, normal);
318 VectorMA (vup, -v, normal, vup);
319 VectorNormalize (vup, vup);
321 VectorScale (normal, dist, org);
323 CrossProduct (vup, normal, vright);
325 // LordHavoc: this has to use *2 because otherwise some created points may
326 // be inside the world (think of a diagonal case), and any brush with such
327 // points should be removed, failure to detect such cases is disasterous
328 VectorScale (vup, MAX_WORLD_COORD*2, vup);
329 VectorScale (vright, MAX_WORLD_COORD*2, vright);
331 // project a really big axis aligned box onto the plane
332 w = AllocWinding (4);
334 VectorSubtract (org, vright, w->p[0]);
335 VectorAdd (w->p[0], vup, w->p[0]);
337 VectorAdd (org, vright, w->p[1]);
338 VectorAdd (w->p[1], vup, w->p[1]);
340 VectorAdd (org, vright, w->p[2]);
341 VectorSubtract (w->p[2], vup, w->p[2]);
343 VectorSubtract (org, vright, w->p[3]);
344 VectorSubtract (w->p[3], vup, w->p[3]);
356 winding_t *CopyWinding (winding_t *w)
361 c = AllocWinding (w->numpoints);
362 size = (size_t)((winding_t *)NULL)->p[w->numpoints];
372 winding_t *ReverseWinding (winding_t *w)
377 c = AllocWinding (w->numpoints);
378 for (i=0 ; i<w->numpoints ; i++)
380 VectorCopy (w->p[w->numpoints-1-i], c->p[i]);
382 c->numpoints = w->numpoints;
392 void ClipWindingEpsilon (winding_t *in, vec3_t normal, vec_t dist,
393 vec_t epsilon, winding_t **front, winding_t **back)
395 vec_t dists[MAX_POINTS_ON_WINDING+4];
396 int sides[MAX_POINTS_ON_WINDING+4];
398 static vec_t dot; // VC 4.2 optimizer bug if not static
405 counts[0] = counts[1] = counts[2] = 0;
407 // determine sides for each point
408 for (i=0 ; i<in->numpoints ; i++)
411 dot = DotProduct (in->p[i], normal);
415 sides[i] = SIDE_FRONT;
416 else if (dot < -epsilon)
417 sides[i] = SIDE_BACK;
427 *front = *back = NULL;
431 *back = CopyWinding (in);
436 *front = CopyWinding (in);
440 maxpts = in->numpoints+4; // cant use counts[0]+2 because
441 // of fp grouping errors
443 *front = f = AllocWinding (maxpts);
444 *back = b = AllocWinding (maxpts);
446 for (i=0 ; i<in->numpoints ; i++)
450 if (sides[i] == SIDE_ON)
452 VectorCopy (p1, f->p[f->numpoints]);
454 VectorCopy (p1, b->p[b->numpoints]);
459 if (sides[i] == SIDE_FRONT)
461 VectorCopy (p1, f->p[f->numpoints]);
464 if (sides[i] == SIDE_BACK)
466 VectorCopy (p1, b->p[b->numpoints]);
470 if (sides[i+1] == SIDE_ON || sides[i+1] == sides[i])
473 // generate a split point
474 p2 = in->p[(i+1)%in->numpoints];
476 dot = dists[i] / (dists[i]-dists[i+1]);
477 for (j=0 ; j<3 ; j++)
478 { // avoid round off error when possible
481 else if (normal[j] == -1)
484 mid[j] = p1[j] + dot*(p2[j]-p1[j]);
487 VectorCopy (mid, f->p[f->numpoints]);
489 VectorCopy (mid, b->p[b->numpoints]);
493 if (f->numpoints > maxpts || b->numpoints > maxpts)
494 Error ("ClipWinding: points exceeded estimate");
495 if (f->numpoints > MAX_POINTS_ON_WINDING || b->numpoints > MAX_POINTS_ON_WINDING)
496 Error ("ClipWinding: MAX_POINTS_ON_WINDING");
505 void ChopWindingInPlace (winding_t **inout, vec3_t normal, vec_t dist, vec_t epsilon)
508 vec_t dists[MAX_POINTS_ON_WINDING+4];
509 int sides[MAX_POINTS_ON_WINDING+4];
511 static vec_t dot; // VC 4.2 optimizer bug if not static
519 counts[0] = counts[1] = counts[2] = 0;
521 // determine sides for each point
522 for (i=0 ; i<in->numpoints ; i++)
524 dot = DotProduct (in->p[i], normal);
528 sides[i] = SIDE_FRONT;
529 else if (dot < -epsilon)
530 sides[i] = SIDE_BACK;
547 return; // inout stays the same
549 maxpts = in->numpoints+4; // cant use counts[0]+2 because
550 // of fp grouping errors
552 f = AllocWinding (maxpts);
554 for (i=0 ; i<in->numpoints ; i++)
558 if (sides[i] == SIDE_ON)
560 VectorCopy (p1, f->p[f->numpoints]);
565 if (sides[i] == SIDE_FRONT)
567 VectorCopy (p1, f->p[f->numpoints]);
571 if (sides[i+1] == SIDE_ON || sides[i+1] == sides[i])
574 // generate a split point
575 p2 = in->p[(i+1)%in->numpoints];
577 dot = dists[i] / (dists[i]-dists[i+1]);
578 for (j=0 ; j<3 ; j++)
579 { // avoid round off error when possible
582 else if (normal[j] == -1)
585 mid[j] = p1[j] + dot*(p2[j]-p1[j]);
588 VectorCopy (mid, f->p[f->numpoints]);
592 if (f->numpoints > maxpts)
593 Error ("ClipWinding: points exceeded estimate");
594 if (f->numpoints > MAX_POINTS_ON_WINDING)
595 Error ("ClipWinding: MAX_POINTS_ON_WINDING");
606 Returns the fragment of in that is on the front side
607 of the cliping plane. The original is freed.
610 winding_t *ChopWinding (winding_t *in, vec3_t normal, vec_t dist)
614 ClipWindingEpsilon (in, normal, dist, ON_EPSILON, &f, &b);
628 void CheckWinding (winding_t *w)
633 vec3_t dir, edgenormal, facenormal;
637 if (w->numpoints < 3)
638 Error ("CheckWinding: %i points",w->numpoints);
640 area = WindingArea(w);
642 Error ("CheckWinding: %f area", area);
644 WindingPlane (w, facenormal, &facedist);
646 for (i=0 ; i<w->numpoints ; i++)
650 for (j=0 ; j<3 ; j++)
651 if (p1[j] > MAX_WORLD_COORD || p1[j] < MIN_WORLD_COORD)
652 Error ("CheckFace: MAX_WORLD_COORD exceeded: %f",p1[j]);
654 j = i+1 == w->numpoints ? 0 : i+1;
656 // check the point is on the face plane
657 d = DotProduct (p1, facenormal) - facedist;
658 if (d < -ON_EPSILON || d > ON_EPSILON)
659 Error ("CheckWinding: point off plane");
661 // check the edge isnt degenerate
663 VectorSubtract (p2, p1, dir);
665 if (VectorLength (dir) < ON_EPSILON)
666 Error ("CheckWinding: degenerate edge");
668 CrossProduct (facenormal, dir, edgenormal);
669 VectorNormalize (edgenormal, edgenormal);
670 edgedist = DotProduct (p1, edgenormal);
671 edgedist += ON_EPSILON;
673 // all other points must be on front side
674 for (j=0 ; j<w->numpoints ; j++)
678 d = DotProduct (w->p[j], edgenormal);
680 Error ("CheckWinding: non-convex");
691 int WindingOnPlaneSide (winding_t *w, vec3_t normal, vec_t dist)
693 qboolean front, back;
699 for (i=0 ; i<w->numpoints ; i++)
701 d = DotProduct (w->p[i], normal) - dist;
728 AddWindingToConvexHull
730 Both w and *hull are on the same plane
733 #define MAX_HULL_POINTS 128
734 void AddWindingToConvexHull( winding_t *w, winding_t **hull, vec3_t normal ) {
739 int numHullPoints, numNew;
740 vec3_t hullPoints[MAX_HULL_POINTS];
741 vec3_t newHullPoints[MAX_HULL_POINTS];
742 vec3_t hullDirs[MAX_HULL_POINTS];
743 qboolean hullSide[MAX_HULL_POINTS];
747 *hull = CopyWinding( w );
751 numHullPoints = (*hull)->numpoints;
752 memcpy( hullPoints, (*hull)->p, numHullPoints * sizeof(vec3_t) );
754 for ( i = 0 ; i < w->numpoints ; i++ ) {
757 // calculate hull side vectors
758 for ( j = 0 ; j < numHullPoints ; j++ ) {
759 k = ( j + 1 ) % numHullPoints;
761 VectorSubtract( hullPoints[k], hullPoints[j], dir );
762 VectorNormalize( dir, dir );
763 CrossProduct( normal, dir, hullDirs[j] );
767 for ( j = 0 ; j < numHullPoints ; j++ ) {
768 VectorSubtract( p, hullPoints[j], dir );
769 d = DotProduct( dir, hullDirs[j] );
770 if ( d >= ON_EPSILON ) {
773 if ( d >= -ON_EPSILON ) {
776 hullSide[j] = qfalse;
780 // if the point is effectively inside, do nothing
785 // find the back side to front side transition
786 for ( j = 0 ; j < numHullPoints ; j++ ) {
787 if ( !hullSide[ j % numHullPoints ] && hullSide[ (j + 1) % numHullPoints ] ) {
791 if ( j == numHullPoints ) {
795 // insert the point here
796 VectorCopy( p, newHullPoints[0] );
799 // copy over all points that aren't double fronts
800 j = (j+1)%numHullPoints;
801 for ( k = 0 ; k < numHullPoints ; k++ ) {
802 if ( hullSide[ (j+k) % numHullPoints ] && hullSide[ (j+k+1) % numHullPoints ] ) {
805 copy = hullPoints[ (j+k+1) % numHullPoints ];
806 VectorCopy( copy, newHullPoints[numNew] );
810 numHullPoints = numNew;
811 memcpy( hullPoints, newHullPoints, numHullPoints * sizeof(vec3_t) );
814 FreeWinding( *hull );
815 w = AllocWinding( numHullPoints );
816 w->numpoints = numHullPoints;
818 memcpy( w->p, hullPoints, numHullPoints * sizeof(vec3_t) );