upgraded both QuakeC VMs to use a table of negative string indices for all dynamic...
[divverent/darkplaces.git] / collision.c
1
2 #include "quakedef.h"
3 #include "polygon.h"
4
5 #define COLLISION_SNAPSCALE (8.0f)
6 #define COLLISION_SNAP (1.0f / COLLISION_SNAPSCALE)
7
8 cvar_t collision_impactnudge = {0, "collision_impactnudge", "0.03125"};
9 cvar_t collision_startnudge = {0, "collision_startnudge", "0"};
10 cvar_t collision_endnudge = {0, "collision_endnudge", "0"};
11 cvar_t collision_enternudge = {0, "collision_enternudge", "0"};
12 cvar_t collision_leavenudge = {0, "collision_leavenudge", "0"};
13
14 void Collision_Init (void)
15 {
16         Cvar_RegisterVariable(&collision_impactnudge);
17         Cvar_RegisterVariable(&collision_startnudge);
18         Cvar_RegisterVariable(&collision_endnudge);
19         Cvar_RegisterVariable(&collision_enternudge);
20         Cvar_RegisterVariable(&collision_leavenudge);
21 }
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36 void Collision_PrintBrushAsQHull(colbrushf_t *brush, const char *name)
37 {
38         int i;
39         Con_Printf("3 %s\n%i\n", name, brush->numpoints);
40         for (i = 0;i < brush->numpoints;i++)
41                 Con_Printf("%f %f %f\n", brush->points[i].v[0], brush->points[i].v[1], brush->points[i].v[2]);
42         // FIXME: optimize!
43         Con_Printf("4\n%i\n", brush->numplanes);
44         for (i = 0;i < brush->numplanes;i++)
45                 Con_Printf("%f %f %f %f\n", brush->planes[i].normal[0], brush->planes[i].normal[1], brush->planes[i].normal[2], brush->planes[i].dist);
46 }
47
48 void Collision_ValidateBrush(colbrushf_t *brush)
49 {
50         int j, k, pointsoffplanes, pointonplanes, pointswithinsufficientplanes, printbrush;
51         float d;
52         printbrush = false;
53         if (!brush->numpoints)
54         {
55                 Con_Print("Collision_ValidateBrush: brush with no points!\n");
56                 printbrush = true;
57         }
58 #if 0
59         // it's ok for a brush to have one point and no planes...
60         if (brush->numplanes == 0 && brush->numpoints != 1)
61         {
62                 Con_Print("Collision_ValidateBrush: brush with no planes and more than one point!\n");
63                 printbrush = true;
64         }
65 #endif
66         if (brush->numplanes)
67         {
68                 pointsoffplanes = 0;
69                 pointswithinsufficientplanes = 0;
70                 for (k = 0;k < brush->numplanes;k++)
71                         if (DotProduct(brush->planes[k].normal, brush->planes[k].normal) < 0.0001f)
72                                 Con_Printf("Collision_ValidateBrush: plane #%i (%f %f %f %f) is degenerate\n", k, brush->planes[k].normal[0], brush->planes[k].normal[1], brush->planes[k].normal[2], brush->planes[k].dist);
73                 for (j = 0;j < brush->numpoints;j++)
74                 {
75                         pointonplanes = 0;
76                         for (k = 0;k < brush->numplanes;k++)
77                         {
78                                 d = DotProduct(brush->points[j].v, brush->planes[k].normal) - brush->planes[k].dist;
79                                 if (d > (1.0f / 8.0f))
80                                 {
81                                         Con_Printf("Collision_ValidateBrush: point #%i (%f %f %f) infront of plane #%i (%f %f %f %f)\n", j, brush->points[j].v[0], brush->points[j].v[1], brush->points[j].v[2], k, brush->planes[k].normal[0], brush->planes[k].normal[1], brush->planes[k].normal[2], brush->planes[k].dist);
82                                         printbrush = true;
83                                 }
84                                 if (fabs(d) > 0.125f)
85                                         pointsoffplanes++;
86                                 else
87                                         pointonplanes++;
88                         }
89                         if (pointonplanes < 3)
90                                 pointswithinsufficientplanes++;
91                 }
92                 if (pointswithinsufficientplanes)
93                 {
94                         Con_Print("Collision_ValidateBrush: some points have insufficient planes, every point must be on at least 3 planes to form a corner.\n");
95                         printbrush = true;
96                 }
97                 if (pointsoffplanes == 0) // all points are on all planes
98                 {
99                         Con_Print("Collision_ValidateBrush: all points lie on all planes (degenerate, no brush volume!)\n");
100                         printbrush = true;
101                 }
102         }
103         if (printbrush)
104                 Collision_PrintBrushAsQHull(brush, "unnamed");
105 }
106
107 float nearestplanedist_float(const float *normal, const colpointf_t *points, int numpoints)
108 {
109         float dist, bestdist;
110         bestdist = DotProduct(points->v, normal);
111         points++;
112         while(--numpoints)
113         {
114                 dist = DotProduct(points->v, normal);
115                 bestdist = min(bestdist, dist);
116                 points++;
117         }
118         return bestdist;
119 }
120
121 float furthestplanedist_float(const float *normal, const colpointf_t *points, int numpoints)
122 {
123         float dist, bestdist;
124         bestdist = DotProduct(points->v, normal);
125         points++;
126         while(--numpoints)
127         {
128                 dist = DotProduct(points->v, normal);
129                 bestdist = max(bestdist, dist);
130                 points++;
131         }
132         return bestdist;
133 }
134
135
136 colbrushf_t *Collision_NewBrushFromPlanes(mempool_t *mempool, int numoriginalplanes, const mplane_t *originalplanes, int supercontents)
137 {
138         int j, k, m, w;
139         int numpointsbuf = 0, maxpointsbuf = 256, numplanesbuf = 0, maxplanesbuf = 256, numelementsbuf = 0, maxelementsbuf = 256;
140         colbrushf_t *brush;
141         colpointf_t pointsbuf[256];
142         colplanef_t planesbuf[256];
143         int elementsbuf[1024];
144         int polypointbuf[256];
145         int pmaxpoints = 64;
146         int pnumpoints;
147         double p[2][3*64];
148 #if 0
149         // enable these if debugging to avoid seeing garbage in unused data
150         memset(pointsbuf, 0, sizeof(pointsbuf));
151         memset(planesbuf, 0, sizeof(planesbuf));
152         memset(elementsbuf, 0, sizeof(elementsbuf));
153         memset(polypointbuf, 0, sizeof(polypointbuf));
154         memset(p, 0, sizeof(p));
155 #endif
156         // construct a collision brush (points, planes, and renderable mesh) from
157         // a set of planes, this also optimizes out any unnecessary planes (ones
158         // whose polygon is clipped away by the other planes)
159         for (j = 0;j < numoriginalplanes;j++)
160         {
161                 // add the plane uniquely (no duplicates)
162                 for (k = 0;k < numplanesbuf;k++)
163                         if (VectorCompare(planesbuf[k].normal, originalplanes[j].normal) && planesbuf[k].dist == originalplanes[j].dist)
164                                 break;
165                 // if the plane is a duplicate, skip it
166                 if (k < numplanesbuf)
167                         continue;
168                 // check if there are too many and skip the brush
169                 if (numplanesbuf >= maxplanesbuf)
170                 {
171                         Con_Print("Collision_NewBrushFromPlanes: failed to build collision brush: too many planes for buffer\n");
172                         return NULL;
173                 }
174
175                 // create a large polygon from the plane
176                 w = 0;
177                 PolygonD_QuadForPlane(p[w], originalplanes[j].normal[0], originalplanes[j].normal[1], originalplanes[j].normal[2], originalplanes[j].dist, 1024.0*1024.0*1024.0);
178                 pnumpoints = 4;
179                 // clip it by all other planes
180                 for (k = 0;k < numoriginalplanes && pnumpoints && pnumpoints <= pmaxpoints;k++)
181                 {
182                         if (k != j)
183                         {
184                                 // we want to keep the inside of the brush plane so we flip
185                                 // the cutting plane
186                                 PolygonD_Divide(pnumpoints, p[w], -originalplanes[k].normal[0], -originalplanes[k].normal[1], -originalplanes[k].normal[2], -originalplanes[k].dist, 1.0/32.0, pmaxpoints, p[!w], &pnumpoints, 0, NULL, NULL);
187                                 w = !w;
188                         }
189                 }
190                 // if nothing is left, skip it
191                 if (pnumpoints < 3)
192                 {
193                         //Con_Printf("Collision_NewBrushFromPlanes: warning: polygon for plane %f %f %f %f clipped away\n", originalplanes[j].normal[0], originalplanes[j].normal[1], originalplanes[j].normal[2], originalplanes[j].dist);
194                         continue;
195                 }
196
197                 for (k = 0;k < pnumpoints;k++)
198                 {
199                         int l, m;
200                         m = 0;
201                         for (l = 0;l < numoriginalplanes;l++)
202                                 if (fabs(DotProduct(&p[w][k*3], originalplanes[l].normal) - originalplanes[l].dist) < 1.0/8.0)
203                                         m++;
204                         if (m < 3)
205                                 break;
206                 }
207                 if (k < pnumpoints)
208                 {
209                         Con_Printf("Collision_NewBrushFromPlanes: warning: polygon point does not lie on at least 3 planes\n");
210                         //return NULL;
211                 }
212
213                 // check if there are too many polygon vertices for buffer
214                 if (pnumpoints > pmaxpoints)
215                 {
216                         Con_Print("Collision_NewBrushFromPlanes: failed to build collision brush: too many points for buffer\n");
217                         return NULL;
218                 }
219
220                 // check if there are too many triangle elements for buffer
221                 if (numelementsbuf + (pnumpoints - 2) * 3 > maxelementsbuf)
222                 {
223                         Con_Print("Collision_NewBrushFromPlanes: failed to build collision brush: too many triangle elements for buffer\n");
224                         return NULL;
225                 }
226
227                 for (k = 0;k < pnumpoints;k++)
228                 {
229                         // check if there is already a matching point (no duplicates)
230                         for (m = 0;m < numpointsbuf;m++)
231                                 if (VectorDistance2(&p[w][k*3], pointsbuf[m].v) < COLLISION_SNAP)
232                                         break;
233
234                         // if there is no match, add a new one
235                         if (m == numpointsbuf)
236                         {
237                                 // check if there are too many and skip the brush
238                                 if (numpointsbuf >= maxpointsbuf)
239                                 {
240                                         Con_Print("Collision_NewBrushFromPlanes: failed to build collision brush: too many points for buffer\n");
241                                         return NULL;
242                                 }
243                                 // add the new one
244                                 VectorCopy(&p[w][k*3], pointsbuf[numpointsbuf].v);
245                                 numpointsbuf++;
246                         }
247
248                         // store the index into a buffer
249                         polypointbuf[k] = m;
250                 }
251
252                 // add the triangles for the polygon
253                 // (this particular code makes a triangle fan)
254                 for (k = 0;k < pnumpoints - 2;k++)
255                 {
256                         elementsbuf[numelementsbuf++] = polypointbuf[0];
257                         elementsbuf[numelementsbuf++] = polypointbuf[k + 1];
258                         elementsbuf[numelementsbuf++] = polypointbuf[k + 2];
259                 }
260
261                 // add the new plane
262                 VectorCopy(originalplanes[j].normal, planesbuf[numplanesbuf].normal);
263                 planesbuf[numplanesbuf].dist = originalplanes[j].dist;
264                 numplanesbuf++;
265         }
266
267         // validate plane distances
268         for (j = 0;j < numplanesbuf;j++)
269         {
270                 float d = furthestplanedist_float(planesbuf[j].normal, pointsbuf, numpointsbuf);
271                 if (fabs(planesbuf[j].dist - d) > (1.0f/32.0f))
272                         Con_Printf("plane %f %f %f %f mismatches dist %f\n", planesbuf[j].normal[0], planesbuf[j].normal[1], planesbuf[j].normal[2], planesbuf[j].dist, d);
273         }
274
275         // if nothing is left, there's nothing to allocate
276         if (numelementsbuf < 12 || numplanesbuf < 4 || numpointsbuf < 4)
277         {
278                 Con_Printf("Collision_NewBrushFromPlanes: failed to build collision brush: %i triangles, %i planes (input was %i planes), %i vertices\n", numelementsbuf / 3, numplanesbuf, numoriginalplanes, numpointsbuf);
279                 return NULL;
280         }
281
282         // allocate the brush and copy to it
283         brush = Collision_AllocBrushFloat(mempool, numpointsbuf, numplanesbuf, numelementsbuf / 3, supercontents);
284         for (j = 0;j < brush->numpoints;j++)
285         {
286                 brush->points[j].v[0] = pointsbuf[j].v[0];
287                 brush->points[j].v[1] = pointsbuf[j].v[1];
288                 brush->points[j].v[2] = pointsbuf[j].v[2];
289         }
290         for (j = 0;j < brush->numplanes;j++)
291         {
292                 brush->planes[j].normal[0] = planesbuf[j].normal[0];
293                 brush->planes[j].normal[1] = planesbuf[j].normal[1];
294                 brush->planes[j].normal[2] = planesbuf[j].normal[2];
295                 brush->planes[j].dist = planesbuf[j].dist;
296         }
297         for (j = 0;j < brush->numtriangles * 3;j++)
298                 brush->elements[j] = elementsbuf[j];
299         VectorCopy(brush->points[0].v, brush->mins);
300         VectorCopy(brush->points[0].v, brush->maxs);
301         for (j = 1;j < brush->numpoints;j++)
302         {
303                 brush->mins[0] = min(brush->mins[0], brush->points[j].v[0]);
304                 brush->mins[1] = min(brush->mins[1], brush->points[j].v[1]);
305                 brush->mins[2] = min(brush->mins[2], brush->points[j].v[2]);
306                 brush->maxs[0] = max(brush->maxs[0], brush->points[j].v[0]);
307                 brush->maxs[1] = max(brush->maxs[1], brush->points[j].v[1]);
308                 brush->maxs[2] = max(brush->maxs[2], brush->points[j].v[2]);
309         }
310         brush->mins[0] -= 1;
311         brush->mins[1] -= 1;
312         brush->mins[2] -= 1;
313         brush->maxs[0] += 1;
314         brush->maxs[1] += 1;
315         brush->maxs[2] += 1;
316         Collision_ValidateBrush(brush);
317         return brush;
318 }
319
320
321
322 colbrushf_t *Collision_AllocBrushFloat(mempool_t *mempool, int numpoints, int numplanes, int numtriangles, int supercontents)
323 {
324         colbrushf_t *brush;
325         brush = Mem_Alloc(mempool, sizeof(colbrushf_t) + sizeof(colpointf_t) * numpoints + sizeof(colplanef_t) * numplanes + sizeof(int[3]) * numtriangles);
326         brush->supercontents = supercontents;
327         brush->numplanes = numplanes;
328         brush->numpoints = numpoints;
329         brush->numtriangles = numtriangles;
330         brush->planes = (void *)(brush + 1);
331         brush->points = (void *)(brush->planes + brush->numplanes);
332         brush->elements = (void *)(brush->points + brush->numpoints);
333         return brush;
334 }
335
336 void Collision_CalcPlanesForPolygonBrushFloat(colbrushf_t *brush)
337 {
338         int i;
339         float edge0[3], edge1[3], edge2[3], normal[3], dist, bestdist;
340         colpointf_t *p, *p2;
341
342         // FIXME: these probably don't actually need to be normalized if the collision code does not care
343         if (brush->numpoints == 3)
344         {
345                 // optimized triangle case
346                 TriangleNormal(brush->points[0].v, brush->points[1].v, brush->points[2].v, brush->planes[0].normal);
347                 if (DotProduct(brush->planes[0].normal, brush->planes[0].normal) < 0.0001f)
348                 {
349                         // there's no point in processing a degenerate triangle (GIGO - Garbage In, Garbage Out)
350                         brush->numplanes = 0;
351                         return;
352                 }
353                 else
354                 {
355                         brush->numplanes = 5;
356                         VectorNormalize(brush->planes[0].normal);
357                         brush->planes[0].dist = DotProduct(brush->points->v, brush->planes[0].normal);
358                         VectorNegate(brush->planes[0].normal, brush->planes[1].normal);
359                         brush->planes[1].dist = -brush->planes[0].dist;
360                         VectorSubtract(brush->points[2].v, brush->points[0].v, edge0);
361                         VectorSubtract(brush->points[0].v, brush->points[1].v, edge1);
362                         VectorSubtract(brush->points[1].v, brush->points[2].v, edge2);
363 #if 1
364                         {
365                                 float projectionnormal[3], projectionedge0[3], projectionedge1[3], projectionedge2[3];
366                                 int i, best;
367                                 float dist, bestdist;
368                                 bestdist = fabs(brush->planes[0].normal[0]);
369                                 best = 0;
370                                 for (i = 1;i < 3;i++)
371                                 {
372                                         dist = fabs(brush->planes[0].normal[i]);
373                                         if (bestdist < dist)
374                                         {
375                                                 bestdist = dist;
376                                                 best = i;
377                                         }
378                                 }
379                                 VectorClear(projectionnormal);
380                                 if (brush->planes[0].normal[best] < 0)
381                                         projectionnormal[best] = -1;
382                                 else
383                                         projectionnormal[best] = 1;
384                                 VectorCopy(edge0, projectionedge0);
385                                 VectorCopy(edge1, projectionedge1);
386                                 VectorCopy(edge2, projectionedge2);
387                                 projectionedge0[best] = 0;
388                                 projectionedge1[best] = 0;
389                                 projectionedge2[best] = 0;
390                                 CrossProduct(projectionedge0, projectionnormal, brush->planes[2].normal);
391                                 CrossProduct(projectionedge1, projectionnormal, brush->planes[3].normal);
392                                 CrossProduct(projectionedge2, projectionnormal, brush->planes[4].normal);
393                         }
394 #else
395                         CrossProduct(edge0, brush->planes->normal, brush->planes[2].normal);
396                         CrossProduct(edge1, brush->planes->normal, brush->planes[3].normal);
397                         CrossProduct(edge2, brush->planes->normal, brush->planes[4].normal);
398 #endif
399                         VectorNormalize(brush->planes[2].normal);
400                         VectorNormalize(brush->planes[3].normal);
401                         VectorNormalize(brush->planes[4].normal);
402                         brush->planes[2].dist = DotProduct(brush->points[2].v, brush->planes[2].normal);
403                         brush->planes[3].dist = DotProduct(brush->points[0].v, brush->planes[3].normal);
404                         brush->planes[4].dist = DotProduct(brush->points[1].v, brush->planes[4].normal);
405
406                         if (developer.integer)
407                         {
408                                 // validation code
409 #if 0
410                                 float temp[3];
411
412                                 VectorSubtract(brush->points[0].v, brush->points[1].v, edge0);
413                                 VectorSubtract(brush->points[2].v, brush->points[1].v, edge1);
414                                 CrossProduct(edge0, edge1, normal);
415                                 VectorNormalize(normal);
416                                 VectorSubtract(normal, brush->planes[0].normal, temp);
417                                 if (VectorLength(temp) > 0.01f)
418                                         Con_Printf("Collision_CalcPlanesForPolygonBrushFloat: TriangleNormal gave wrong answer (%f %f %f != correct answer %f %f %f)\n", brush->planes->normal[0], brush->planes->normal[1], brush->planes->normal[2], normal[0], normal[1], normal[2]);
419                                 if (fabs(DotProduct(brush->planes[1].normal, brush->planes[0].normal) - -1.0f) > 0.01f || fabs(brush->planes[1].dist - -brush->planes[0].dist) > 0.01f)
420                                         Con_Printf("Collision_CalcPlanesForPolygonBrushFloat: plane 1 (%f %f %f %f) is not opposite plane 0 (%f %f %f %f)\n", brush->planes[1].normal[0], brush->planes[1].normal[1], brush->planes[1].normal[2], brush->planes[1].dist, brush->planes[0].normal[0], brush->planes[0].normal[1], brush->planes[0].normal[2], brush->planes[0].dist);
421 #if 0
422                                 if (fabs(DotProduct(brush->planes[2].normal, brush->planes[0].normal)) > 0.01f)
423                                         Con_Printf("Collision_CalcPlanesForPolygonBrushFloat: plane 2 (%f %f %f %f) is not perpendicular to plane 0 (%f %f %f %f)\n", brush->planes[2].normal[0], brush->planes[2].normal[1], brush->planes[2].normal[2], brush->planes[2].dist, brush->planes[0].normal[0], brush->planes[0].normal[1], brush->planes[0].normal[2], brush->planes[2].dist);
424                                 if (fabs(DotProduct(brush->planes[3].normal, brush->planes[0].normal)) > 0.01f)
425                                         Con_Printf("Collision_CalcPlanesForPolygonBrushFloat: plane 3 (%f %f %f %f) is not perpendicular to plane 0 (%f %f %f %f)\n", brush->planes[3].normal[0], brush->planes[3].normal[1], brush->planes[3].normal[2], brush->planes[3].dist, brush->planes[0].normal[0], brush->planes[0].normal[1], brush->planes[0].normal[2], brush->planes[3].dist);
426                                 if (fabs(DotProduct(brush->planes[4].normal, brush->planes[0].normal)) > 0.01f)
427                                         Con_Printf("Collision_CalcPlanesForPolygonBrushFloat: plane 4 (%f %f %f %f) is not perpendicular to plane 0 (%f %f %f %f)\n", brush->planes[4].normal[0], brush->planes[4].normal[1], brush->planes[4].normal[2], brush->planes[4].dist, brush->planes[0].normal[0], brush->planes[0].normal[1], brush->planes[0].normal[2], brush->planes[4].dist);
428                                 if (fabs(DotProduct(brush->planes[2].normal, edge0)) > 0.01f)
429                                         Con_Printf("Collision_CalcPlanesForPolygonBrushFloat: plane 2 (%f %f %f %f) is not perpendicular to edge 0 (%f %f %f to %f %f %f)\n", brush->planes[2].normal[0], brush->planes[2].normal[1], brush->planes[2].normal[2], brush->planes[2].dist, brush->points[2].v[0], brush->points[2].v[1], brush->points[2].v[2], brush->points[0].v[0], brush->points[0].v[1], brush->points[0].v[2]);
430                                 if (fabs(DotProduct(brush->planes[3].normal, edge1)) > 0.01f)
431                                         Con_Printf("Collision_CalcPlanesForPolygonBrushFloat: plane 3 (%f %f %f %f) is not perpendicular to edge 1 (%f %f %f to %f %f %f)\n", brush->planes[3].normal[0], brush->planes[3].normal[1], brush->planes[3].normal[2], brush->planes[3].dist, brush->points[0].v[0], brush->points[0].v[1], brush->points[0].v[2], brush->points[1].v[0], brush->points[1].v[1], brush->points[1].v[2]);
432                                 if (fabs(DotProduct(brush->planes[4].normal, edge2)) > 0.01f)
433                                         Con_Printf("Collision_CalcPlanesForPolygonBrushFloat: plane 4 (%f %f %f %f) is not perpendicular to edge 2 (%f %f %f to %f %f %f)\n", brush->planes[4].normal[0], brush->planes[4].normal[1], brush->planes[4].normal[2], brush->planes[4].dist, brush->points[1].v[0], brush->points[1].v[1], brush->points[1].v[2], brush->points[2].v[0], brush->points[2].v[1], brush->points[2].v[2]);
434 #endif
435 #endif
436                                 if (fabs(DotProduct(brush->points[0].v, brush->planes[0].normal) - brush->planes[0].dist) > 0.01f || fabs(DotProduct(brush->points[1].v, brush->planes[0].normal) - brush->planes[0].dist) > 0.01f || fabs(DotProduct(brush->points[2].v, brush->planes[0].normal) - brush->planes[0].dist) > 0.01f)
437                                         Con_Printf("Collision_CalcPlanesForPolygonBrushFloat: edges (%f %f %f to %f %f %f to %f %f %f) off front plane 0 (%f %f %f %f)\n", brush->points[0].v[0], brush->points[0].v[1], brush->points[0].v[2], brush->points[1].v[0], brush->points[1].v[1], brush->points[1].v[2], brush->points[2].v[0], brush->points[2].v[1], brush->points[2].v[2], brush->planes[0].normal[0], brush->planes[0].normal[1], brush->planes[0].normal[2], brush->planes[0].dist);
438                                 if (fabs(DotProduct(brush->points[0].v, brush->planes[1].normal) - brush->planes[1].dist) > 0.01f || fabs(DotProduct(brush->points[1].v, brush->planes[1].normal) - brush->planes[1].dist) > 0.01f || fabs(DotProduct(brush->points[2].v, brush->planes[1].normal) - brush->planes[1].dist) > 0.01f)
439                                         Con_Printf("Collision_CalcPlanesForPolygonBrushFloat: edges (%f %f %f to %f %f %f to %f %f %f) off back plane 1 (%f %f %f %f)\n", brush->points[0].v[0], brush->points[0].v[1], brush->points[0].v[2], brush->points[1].v[0], brush->points[1].v[1], brush->points[1].v[2], brush->points[2].v[0], brush->points[2].v[1], brush->points[2].v[2], brush->planes[1].normal[0], brush->planes[1].normal[1], brush->planes[1].normal[2], brush->planes[1].dist);
440                                 if (fabs(DotProduct(brush->points[2].v, brush->planes[2].normal) - brush->planes[2].dist) > 0.01f || fabs(DotProduct(brush->points[0].v, brush->planes[2].normal) - brush->planes[2].dist) > 0.01f)
441                                         Con_Printf("Collision_CalcPlanesForPolygonBrushFloat: edge 0 (%f %f %f to %f %f %f) off front plane 2 (%f %f %f %f)\n", brush->points[2].v[0], brush->points[2].v[1], brush->points[2].v[2], brush->points[0].v[0], brush->points[0].v[1], brush->points[0].v[2], brush->planes[2].normal[0], brush->planes[2].normal[1], brush->planes[2].normal[2], brush->planes[2].dist);
442                                 if (fabs(DotProduct(brush->points[0].v, brush->planes[3].normal) - brush->planes[3].dist) > 0.01f || fabs(DotProduct(brush->points[1].v, brush->planes[3].normal) - brush->planes[3].dist) > 0.01f)
443                                         Con_Printf("Collision_CalcPlanesForPolygonBrushFloat: edge 0 (%f %f %f to %f %f %f) off front plane 2 (%f %f %f %f)\n", brush->points[0].v[0], brush->points[0].v[1], brush->points[0].v[2], brush->points[1].v[0], brush->points[1].v[1], brush->points[1].v[2], brush->planes[3].normal[0], brush->planes[3].normal[1], brush->planes[3].normal[2], brush->planes[3].dist);
444                                 if (fabs(DotProduct(brush->points[1].v, brush->planes[4].normal) - brush->planes[4].dist) > 0.01f || fabs(DotProduct(brush->points[2].v, brush->planes[4].normal) - brush->planes[4].dist) > 0.01f)
445                                         Con_Printf("Collision_CalcPlanesForPolygonBrushFloat: edge 0 (%f %f %f to %f %f %f) off front plane 2 (%f %f %f %f)\n", brush->points[1].v[0], brush->points[1].v[1], brush->points[1].v[2], brush->points[2].v[0], brush->points[2].v[1], brush->points[2].v[2], brush->planes[4].normal[0], brush->planes[4].normal[1], brush->planes[4].normal[2], brush->planes[4].dist);
446                         }
447                 }
448         }
449         else
450         {
451                 // choose best surface normal for polygon's plane
452                 bestdist = 0;
453                 for (i = 0, p = brush->points + 1;i < brush->numpoints - 2;i++, p++)
454                 {
455                         VectorSubtract(p[-1].v, p[0].v, edge0);
456                         VectorSubtract(p[1].v, p[0].v, edge1);
457                         CrossProduct(edge0, edge1, normal);
458                         //TriangleNormal(p[-1].v, p[0].v, p[1].v, normal);
459                         dist = DotProduct(normal, normal);
460                         if (i == 0 || bestdist < dist)
461                         {
462                                 bestdist = dist;
463                                 VectorCopy(normal, brush->planes->normal);
464                         }
465                 }
466                 if (bestdist < 0.0001f)
467                 {
468                         // there's no point in processing a degenerate triangle (GIGO - Garbage In, Garbage Out)
469                         brush->numplanes = 0;
470                         return;
471                 }
472                 else
473                 {
474                         brush->numplanes = brush->numpoints + 2;
475                         VectorNormalize(brush->planes->normal);
476                         brush->planes->dist = DotProduct(brush->points->v, brush->planes->normal);
477
478                         // negate plane to create other side
479                         VectorNegate(brush->planes[0].normal, brush->planes[1].normal);
480                         brush->planes[1].dist = -brush->planes[0].dist;
481                         for (i = 0, p = brush->points + (brush->numpoints - 1), p2 = brush->points;i < brush->numpoints;i++, p = p2, p2++)
482                         {
483                                 VectorSubtract(p->v, p2->v, edge0);
484                                 CrossProduct(edge0, brush->planes->normal, brush->planes[i + 2].normal);
485                                 VectorNormalize(brush->planes[i + 2].normal);
486                                 brush->planes[i + 2].dist = DotProduct(p->v, brush->planes[i + 2].normal);
487                         }
488                 }
489         }
490
491         if (developer.integer)
492         {
493                 // validity check - will be disabled later
494                 Collision_ValidateBrush(brush);
495                 for (i = 0;i < brush->numplanes;i++)
496                 {
497                         int j;
498                         for (j = 0, p = brush->points;j < brush->numpoints;j++, p++)
499                                 if (DotProduct(p->v, brush->planes[i].normal) > brush->planes[i].dist + (1.0 / 32.0))
500                                         Con_Printf("Error in brush plane generation, plane %i\n", i);
501                 }
502         }
503 }
504
505 colbrushf_t *Collision_AllocBrushFromPermanentPolygonFloat(mempool_t *mempool, int numpoints, float *points, int supercontents)
506 {
507         colbrushf_t *brush;
508         brush = Mem_Alloc(mempool, sizeof(colbrushf_t) + sizeof(colplanef_t) * (numpoints + 2));
509         brush->supercontents = supercontents;
510         brush->numpoints = numpoints;
511         brush->numplanes = numpoints + 2;
512         brush->planes = (void *)(brush + 1);
513         brush->points = (colpointf_t *)points;
514         Host_Error("Collision_AllocBrushFromPermanentPolygonFloat: FIXME: this code needs to be updated to generate a mesh...\n");
515         return brush;
516 }
517
518 // NOTE: start and end of each brush pair must have same numplanes/numpoints
519 void Collision_TraceBrushBrushFloat(trace_t *trace, const colbrushf_t *thisbrush_start, const colbrushf_t *thisbrush_end, const colbrushf_t *thatbrush_start, const colbrushf_t *thatbrush_end)
520 {
521         int nplane, nplane2, fstartsolid, fendsolid, brushsolid;
522         float enterfrac, leavefrac, d1, d2, f, imove, newimpactnormal[3], enterfrac2;
523         const colplanef_t *startplane, *endplane;
524
525         enterfrac = -1;
526         enterfrac2 = -1;
527         leavefrac = 1;
528         fstartsolid = true;
529         fendsolid = true;
530
531         for (nplane = 0;nplane < thatbrush_start->numplanes + thisbrush_start->numplanes;nplane++)
532         {
533                 nplane2 = nplane;
534                 if (nplane2 >= thatbrush_start->numplanes)
535                 {
536                         nplane2 -= thatbrush_start->numplanes;
537                         startplane = thisbrush_start->planes + nplane2;
538                         endplane = thisbrush_end->planes + nplane2;
539                         if (developer.integer)
540                         {
541                                 // any brush with degenerate planes is not worth handling
542                                 if (DotProduct(startplane->normal, startplane->normal) < 0.9f || DotProduct(endplane->normal, endplane->normal) < 0.9f)
543                                 {
544                                         Con_Print("Collision_TraceBrushBrushFloat: degenerate thisbrush plane!\n");
545                                         return;
546                                 }
547                                 f = furthestplanedist_float(startplane->normal, thisbrush_start->points, thisbrush_start->numpoints);
548                                 if (fabs(f - startplane->dist) > 0.125f)
549                                         Con_Printf("startplane->dist %f != calculated %f (thisbrush_start)\n", startplane->dist, f);
550                         }
551                         d1 = nearestplanedist_float(startplane->normal, thisbrush_start->points, thisbrush_start->numpoints) - furthestplanedist_float(startplane->normal, thatbrush_start->points, thatbrush_start->numpoints) - collision_startnudge.value;
552                         d2 = nearestplanedist_float(endplane->normal, thisbrush_end->points, thisbrush_end->numpoints) - furthestplanedist_float(endplane->normal, thatbrush_end->points, thatbrush_end->numpoints) - collision_endnudge.value;
553                 }
554                 else
555                 {
556                         startplane = thatbrush_start->planes + nplane2;
557                         endplane = thatbrush_end->planes + nplane2;
558                         if (developer.integer)
559                         {
560                                 // any brush with degenerate planes is not worth handling
561                                 if (DotProduct(startplane->normal, startplane->normal) < 0.9f || DotProduct(endplane->normal, endplane->normal) < 0.9f)
562                                 {
563                                         Con_Print("Collision_TraceBrushBrushFloat: degenerate thatbrush plane!\n");
564                                         return;
565                                 }
566                                 f = furthestplanedist_float(startplane->normal, thatbrush_start->points, thatbrush_start->numpoints);
567                                 if (fabs(f - startplane->dist) > 0.125f)
568                                         Con_Printf("startplane->dist %f != calculated %f (thatbrush_start)\n", startplane->dist, f);
569                         }
570                         d1 = nearestplanedist_float(startplane->normal, thisbrush_start->points, thisbrush_start->numpoints) - startplane->dist - collision_startnudge.value;
571                         d2 = nearestplanedist_float(endplane->normal, thisbrush_end->points, thisbrush_end->numpoints) - endplane->dist - collision_endnudge.value;
572                 }
573                 //Con_Printf("%c%i: d1 = %f, d2 = %f, d1 / (d1 - d2) = %f\n", nplane2 != nplane ? 'b' : 'a', nplane2, d1, d2, d1 / (d1 - d2));
574
575                 if(d1 > d2)
576                 {
577                         // moving into brush
578                         if(d2 > 0)
579                                 return;
580                         if(d1 > 0)
581                         {
582                                 // enter
583                                 fstartsolid = false;
584                                 imove = 1 / (d1 - d2);
585                                 f = (d1 - collision_enternudge.value) * imove;
586                                 if (enterfrac < f)
587                                 {
588                                         enterfrac = f;
589                                         enterfrac2 = f - collision_impactnudge.value * imove;
590                                         VectorLerp(startplane->normal, enterfrac, endplane->normal, newimpactnormal);
591                                 }
592                         }
593                 }
594                 else
595                 {
596                         // moving out of brush
597                         if(d1 > 0)
598                                 return;
599                         if(d2 > 0)
600                         {
601                                 // leave
602                                 fendsolid = false;
603                                 f = (d1 + collision_leavenudge.value) / (d1 - d2);
604                                 if (leavefrac > f)
605                                         leavefrac = f;
606                         }
607                 }
608         }
609
610         brushsolid = trace->hitsupercontentsmask & thatbrush_start->supercontents;
611         if (fstartsolid)
612         {
613                 trace->startsupercontents |= thatbrush_start->supercontents;
614                 if (brushsolid)
615                 {
616                         trace->startsolid = true;
617                         if (fendsolid)
618                                 trace->allsolid = true;
619                 }
620         }
621
622         // LordHavoc: we need an epsilon nudge here because for a point trace the
623         // penetrating line segment is normally zero length if this brush was
624         // generated from a polygon (infinitely thin), and could even be slightly
625         // positive or negative due to rounding errors in that case.
626         if (brushsolid && enterfrac > -1 && enterfrac < trace->realfraction && enterfrac - (1.0f / 1024.0f) <= leavefrac)
627         {
628 #if 0
629                 // broken
630                 if (thatbrush_start->ispolygon)
631                 {
632                         d1 = nearestplanedist_float(thatbrush_start->planes[0].normal, thisbrush_start->points, thisbrush_start->numpoints) - thatbrush_start->planes[0].dist - collision_startnudge.value;
633                         d2 = nearestplanedist_float(thatbrush_end->planes[0].normal, thisbrush_end->points, thisbrush_end->numpoints) - thatbrush_end->planes[0].dist - collision_endnudge.value;
634                         move = d1 - d2;
635                         if (move <= 0 || d2 > collision_enternudge.value || d1 < 0)
636                                 return;
637                         // enter
638                         imove = 1 / move;
639                         enterfrac = (d1 - collision_enternudge.value) * imove;
640                         if (enterfrac < trace->realfraction)
641                         {
642                                 enterfrac2 = enterfrac - collision_impactnudge.value * imove;
643                                 trace->realfraction = bound(0, enterfrac, 1);
644                                 trace->fraction = bound(0, enterfrac2, 1);
645                                 VectorLerp(thatbrush_start->planes[0].normal, enterfrac, thatbrush_end->planes[0].normal, trace->plane.normal);
646                         }
647                 }
648                 else
649 #endif
650                 {
651                         trace->realfraction = bound(0, enterfrac, 1);
652                         trace->fraction = bound(0, enterfrac2, 1);
653                         VectorCopy(newimpactnormal, trace->plane.normal);
654                 }
655         }
656 }
657
658 // NOTE: start and end brush pair must have same numplanes/numpoints
659 void Collision_TraceLineBrushFloat(trace_t *trace, const vec3_t linestart, const vec3_t lineend, const colbrushf_t *thatbrush_start, const colbrushf_t *thatbrush_end)
660 {
661         int nplane, fstartsolid, fendsolid, brushsolid;
662         float enterfrac, leavefrac, d1, d2, f, imove, newimpactnormal[3], enterfrac2;
663         const colplanef_t *startplane, *endplane;
664
665         enterfrac = -1;
666         enterfrac2 = -1;
667         leavefrac = 1;
668         fstartsolid = true;
669         fendsolid = true;
670
671         for (nplane = 0;nplane < thatbrush_start->numplanes;nplane++)
672         {
673                 startplane = thatbrush_start->planes + nplane;
674                 endplane = thatbrush_end->planes + nplane;
675                 d1 = DotProduct(startplane->normal, linestart) - startplane->dist - collision_startnudge.value;
676                 d2 = DotProduct(endplane->normal, lineend) - endplane->dist - collision_endnudge.value;
677                 if (developer.integer)
678                 {
679                         // any brush with degenerate planes is not worth handling
680                         if (DotProduct(startplane->normal, startplane->normal) < 0.9f || DotProduct(endplane->normal, endplane->normal) < 0.9f)
681                         {
682                                 Con_Print("Collision_TraceLineBrushFloat: degenerate plane!\n");
683                                 return;
684                         }
685                         if (thatbrush_start->numpoints)
686                         {
687                                 f = furthestplanedist_float(startplane->normal, thatbrush_start->points, thatbrush_start->numpoints);
688                                 if (fabs(f - startplane->dist) > 0.125f)
689                                         Con_Printf("startplane->dist %f != calculated %f\n", startplane->dist, f);
690                         }
691                 }
692
693                 if (d1 > d2)
694                 {
695                         // moving into brush
696                         if (d2 > 0)
697                                 return;
698                         if (d1 > 0)
699                         {
700                                 // enter
701                                 fstartsolid = false;
702                                 imove = 1 / (d1 - d2);
703                                 f = (d1 - collision_enternudge.value) * imove;
704                                 if (enterfrac < f)
705                                 {
706                                         enterfrac = f;
707                                         enterfrac2 = f - collision_impactnudge.value * imove;
708                                         VectorLerp(startplane->normal, enterfrac, endplane->normal, newimpactnormal);
709                                 }
710                         }
711                 }
712                 else
713                 {
714                         // moving out of brush
715                         if (d1 > 0)
716                                 return;
717                         if (d2 > 0)
718                         {
719                                 // leave
720                                 fendsolid = false;
721                                 f = (d1 + collision_leavenudge.value) / (d1 - d2);
722                                 if (leavefrac > f)
723                                         leavefrac = f;
724                         }
725                 }
726         }
727
728         brushsolid = trace->hitsupercontentsmask & thatbrush_start->supercontents;
729         if (fstartsolid)
730         {
731                 trace->startsupercontents |= thatbrush_start->supercontents;
732                 if (brushsolid)
733                 {
734                         trace->startsolid = true;
735                         if (fendsolid)
736                                 trace->allsolid = true;
737                 }
738         }
739
740         // LordHavoc: we need an epsilon nudge here because for a point trace the
741         // penetrating line segment is normally zero length if this brush was
742         // generated from a polygon (infinitely thin), and could even be slightly
743         // positive or negative due to rounding errors in that case.
744         if (brushsolid && enterfrac > -1 && enterfrac < trace->realfraction && enterfrac - (1.0f / 1024.0f) <= leavefrac)
745         {
746 #if 0
747                 // broken
748                 if (thatbrush_start->ispolygon)
749                 {
750                         d1 = DotProduct(thatbrush_start->planes[0].normal, linestart) - thatbrush_start->planes[0].dist - collision_startnudge.value;
751                         d2 = DotProduct(thatbrush_end->planes[0].normal, lineend) - thatbrush_end->planes[0].dist - collision_endnudge.value;
752                         move = d1 - d2;
753                         if (move <= 0 || d2 > collision_enternudge.value || d1 < 0)
754                                 return;
755                         // enter
756                         imove = 1 / move;
757                         enterfrac = (d1 - collision_enternudge.value) * imove;
758                         if (enterfrac < trace->realfraction)
759                         {
760                                 enterfrac2 = enterfrac - collision_impactnudge.value * imove;
761                                 trace->realfraction = bound(0, enterfrac, 1);
762                                 trace->fraction = bound(0, enterfrac2, 1);
763                                 VectorLerp(thatbrush_start->planes[0].normal, enterfrac, thatbrush_end->planes[0].normal, trace->plane.normal);
764                         }
765                 }
766                 else
767 #endif
768                 {
769                         trace->realfraction = bound(0, enterfrac, 1);
770                         trace->fraction = bound(0, enterfrac2, 1);
771                         VectorCopy(newimpactnormal, trace->plane.normal);
772                 }
773         }
774 }
775
776 void Collision_TracePointBrushFloat(trace_t *trace, const vec3_t point, const colbrushf_t *thatbrush)
777 {
778         int nplane;
779         const colplanef_t *plane;
780
781         for (nplane = 0, plane = thatbrush->planes;nplane < thatbrush->numplanes;nplane++, plane++)
782                 if (DotProduct(plane->normal, point) > plane->dist)
783                         return;
784
785         trace->startsupercontents |= thatbrush->supercontents;
786         if (trace->hitsupercontentsmask & thatbrush->supercontents)
787         {
788                 trace->startsolid = true;
789                 trace->allsolid = true;
790         }
791 }
792
793 static colpointf_t polyf_points[256];
794 static colplanef_t polyf_planes[256 + 2];
795 static colbrushf_t polyf_brush;
796
797 void Collision_SnapCopyPoints(int numpoints, const colpointf_t *in, colpointf_t *out, float fractionprecision, float invfractionprecision)
798 {
799         while (numpoints--)
800         {
801                 out->v[0] = floor(in->v[0] * fractionprecision + 0.5f) * invfractionprecision;
802                 out->v[1] = floor(in->v[1] * fractionprecision + 0.5f) * invfractionprecision;
803                 out->v[2] = floor(in->v[2] * fractionprecision + 0.5f) * invfractionprecision;
804         }
805 }
806
807 void Collision_TraceBrushPolygonFloat(trace_t *trace, const colbrushf_t *thisbrush_start, const colbrushf_t *thisbrush_end, int numpoints, const float *points, int supercontents)
808 {
809         if (numpoints > 256)
810         {
811                 Con_Print("Polygon with more than 256 points not supported yet (fixme!)\n");
812                 return;
813         }
814         polyf_brush.numpoints = numpoints;
815         polyf_brush.numplanes = numpoints + 2;
816         //polyf_brush.points = (colpointf_t *)points;
817         polyf_brush.planes = polyf_planes;
818         polyf_brush.supercontents = supercontents;
819         polyf_brush.points = polyf_points;
820         Collision_SnapCopyPoints(numpoints, (colpointf_t *)points, polyf_points, COLLISION_SNAPSCALE, COLLISION_SNAP);
821         Collision_CalcPlanesForPolygonBrushFloat(&polyf_brush);
822         //Collision_PrintBrushAsQHull(&polyf_brush, "polyf_brush");
823         Collision_TraceBrushBrushFloat(trace, thisbrush_start, thisbrush_end, &polyf_brush, &polyf_brush);
824 }
825
826 void Collision_TraceBrushTriangleMeshFloat(trace_t *trace, const colbrushf_t *thisbrush_start, const colbrushf_t *thisbrush_end, int numtriangles, const int *element3i, const float *vertex3f, int supercontents, const vec3_t segmentmins, const vec3_t segmentmaxs)
827 {
828         int i;
829         float facemins[3], facemaxs[3];
830         polyf_brush.numpoints = 3;
831         polyf_brush.numplanes = 5;
832         polyf_brush.points = polyf_points;
833         polyf_brush.planes = polyf_planes;
834         polyf_brush.supercontents = supercontents;
835         for (i = 0;i < numtriangles;i++, element3i += 3)
836         {
837                 VectorCopy(vertex3f + element3i[0] * 3, polyf_points[0].v);
838                 VectorCopy(vertex3f + element3i[1] * 3, polyf_points[1].v);
839                 VectorCopy(vertex3f + element3i[2] * 3, polyf_points[2].v);
840                 Collision_SnapCopyPoints(3, polyf_points, polyf_points, COLLISION_SNAPSCALE, COLLISION_SNAP);
841                 facemins[0] = min(polyf_points[0].v[0], min(polyf_points[1].v[0], polyf_points[2].v[0])) - 1;
842                 facemins[1] = min(polyf_points[0].v[1], min(polyf_points[1].v[1], polyf_points[2].v[1])) - 1;
843                 facemins[2] = min(polyf_points[0].v[2], min(polyf_points[1].v[2], polyf_points[2].v[2])) - 1;
844                 facemaxs[0] = max(polyf_points[0].v[0], max(polyf_points[1].v[0], polyf_points[2].v[0])) + 1;
845                 facemaxs[1] = max(polyf_points[0].v[1], max(polyf_points[1].v[1], polyf_points[2].v[1])) + 1;
846                 facemaxs[2] = max(polyf_points[0].v[2], max(polyf_points[1].v[2], polyf_points[2].v[2])) + 1;
847                 if (BoxesOverlap(segmentmins, segmentmaxs, facemins, facemaxs))
848                 {
849                         Collision_CalcPlanesForPolygonBrushFloat(&polyf_brush);
850                         //Collision_PrintBrushAsQHull(&polyf_brush, "polyf_brush");
851                         Collision_TraceBrushBrushFloat(trace, thisbrush_start, thisbrush_end, &polyf_brush, &polyf_brush);
852                 }
853         }
854 }
855
856 void Collision_TraceLinePolygonFloat(trace_t *trace, const vec3_t linestart, const vec3_t lineend, int numpoints, const float *points, int supercontents)
857 {
858         if (numpoints > 256)
859         {
860                 Con_Print("Polygon with more than 256 points not supported yet (fixme!)\n");
861                 return;
862         }
863         polyf_brush.numpoints = numpoints;
864         polyf_brush.numplanes = numpoints + 2;
865         //polyf_brush.points = (colpointf_t *)points;
866         polyf_brush.points = polyf_points;
867         Collision_SnapCopyPoints(numpoints, (colpointf_t *)points, polyf_points, COLLISION_SNAPSCALE, COLLISION_SNAP);
868         polyf_brush.planes = polyf_planes;
869         polyf_brush.supercontents = supercontents;
870         Collision_CalcPlanesForPolygonBrushFloat(&polyf_brush);
871         //Collision_PrintBrushAsQHull(&polyf_brush, "polyf_brush");
872         Collision_TraceLineBrushFloat(trace, linestart, lineend, &polyf_brush, &polyf_brush);
873 }
874
875 void Collision_TraceLineTriangleMeshFloat(trace_t *trace, const vec3_t linestart, const vec3_t lineend, int numtriangles, const int *element3i, const float *vertex3f, int supercontents, const vec3_t segmentmins, const vec3_t segmentmaxs)
876 {
877         int i;
878 #if 1
879         // FIXME: snap vertices?
880         for (i = 0;i < numtriangles;i++, element3i += 3)
881                 Collision_TraceLineTriangleFloat(trace, linestart, lineend, vertex3f + element3i[0] * 3, vertex3f + element3i[1] * 3, vertex3f + element3i[2] * 3);
882 #else
883         polyf_brush.numpoints = 3;
884         polyf_brush.numplanes = 5;
885         polyf_brush.points = polyf_points;
886         polyf_brush.planes = polyf_planes;
887         polyf_brush.supercontents = supercontents;
888         for (i = 0;i < numtriangles;i++, element3i += 3)
889         {
890                 float facemins[3], facemaxs[3];
891                 VectorCopy(vertex3f + element3i[0] * 3, polyf_points[0].v);
892                 VectorCopy(vertex3f + element3i[1] * 3, polyf_points[1].v);
893                 VectorCopy(vertex3f + element3i[2] * 3, polyf_points[2].v);
894                 Collision_SnapCopyPoints(numpoints, polyf_points, polyf_points, COLLISION_SNAPSCALE, COLLISION_SNAP);
895                 facemins[0] = min(polyf_points[0].v[0], min(polyf_points[1].v[0], polyf_points[2].v[0])) - 1;
896                 facemins[1] = min(polyf_points[0].v[1], min(polyf_points[1].v[1], polyf_points[2].v[1])) - 1;
897                 facemins[2] = min(polyf_points[0].v[2], min(polyf_points[1].v[2], polyf_points[2].v[2])) - 1;
898                 facemaxs[0] = max(polyf_points[0].v[0], max(polyf_points[1].v[0], polyf_points[2].v[0])) + 1;
899                 facemaxs[1] = max(polyf_points[0].v[1], max(polyf_points[1].v[1], polyf_points[2].v[1])) + 1;
900                 facemaxs[2] = max(polyf_points[0].v[2], max(polyf_points[1].v[2], polyf_points[2].v[2])) + 1;
901                 if (BoxesOverlap(segmentmins, segmentmaxs, facemins, facemaxs))
902                 {
903                         Collision_CalcPlanesForPolygonBrushFloat(&polyf_brush);
904                         //Collision_PrintBrushAsQHull(&polyf_brush, "polyf_brush");
905                         Collision_TraceLineBrushFloat(trace, linestart, lineend, &polyf_brush, &polyf_brush);
906                 }
907         }
908 #endif
909 }
910
911
912 static colpointf_t polyf_pointsstart[256], polyf_pointsend[256];
913 static colplanef_t polyf_planesstart[256 + 2], polyf_planesend[256 + 2];
914 static colbrushf_t polyf_brushstart, polyf_brushend;
915
916 void Collision_TraceBrushPolygonTransformFloat(trace_t *trace, const colbrushf_t *thisbrush_start, const colbrushf_t *thisbrush_end, int numpoints, const float *points, const matrix4x4_t *polygonmatrixstart, const matrix4x4_t *polygonmatrixend, int supercontents)
917 {
918         int i;
919         if (numpoints > 256)
920         {
921                 Con_Print("Polygon with more than 256 points not supported yet (fixme!)\n");
922                 return;
923         }
924         polyf_brushstart.numpoints = numpoints;
925         polyf_brushstart.numplanes = numpoints + 2;
926         polyf_brushstart.points = polyf_pointsstart;//(colpointf_t *)points;
927         polyf_brushstart.planes = polyf_planesstart;
928         polyf_brushstart.supercontents = supercontents;
929         for (i = 0;i < numpoints;i++)
930                 Matrix4x4_Transform(polygonmatrixstart, points + i * 3, polyf_brushstart.points[i].v);
931         polyf_brushend.numpoints = numpoints;
932         polyf_brushend.numplanes = numpoints + 2;
933         polyf_brushend.points = polyf_pointsend;//(colpointf_t *)points;
934         polyf_brushend.planes = polyf_planesend;
935         polyf_brushend.supercontents = supercontents;
936         for (i = 0;i < numpoints;i++)
937                 Matrix4x4_Transform(polygonmatrixend, points + i * 3, polyf_brushend.points[i].v);
938         Collision_SnapCopyPoints(numpoints, polyf_pointsstart, polyf_pointsstart, COLLISION_SNAPSCALE, COLLISION_SNAP);
939         Collision_SnapCopyPoints(numpoints, polyf_pointsend, polyf_pointsend, COLLISION_SNAPSCALE, COLLISION_SNAP);
940         Collision_CalcPlanesForPolygonBrushFloat(&polyf_brushstart);
941         Collision_CalcPlanesForPolygonBrushFloat(&polyf_brushend);
942
943         //Collision_PrintBrushAsQHull(&polyf_brushstart, "polyf_brushstart");
944         //Collision_PrintBrushAsQHull(&polyf_brushend, "polyf_brushend");
945
946         Collision_TraceBrushBrushFloat(trace, thisbrush_start, thisbrush_end, &polyf_brushstart, &polyf_brushend);
947 }
948
949
950
951 #define MAX_BRUSHFORBOX 16
952 static int brushforbox_index = 0;
953 static colpointf_t brushforbox_point[MAX_BRUSHFORBOX*8];
954 static colplanef_t brushforbox_plane[MAX_BRUSHFORBOX*6];
955 static colbrushf_t brushforbox_brush[MAX_BRUSHFORBOX];
956 static colbrushf_t brushforpoint_brush[MAX_BRUSHFORBOX];
957
958 void Collision_InitBrushForBox(void)
959 {
960         int i;
961         for (i = 0;i < MAX_BRUSHFORBOX;i++)
962         {
963                 brushforbox_brush[i].supercontents = SUPERCONTENTS_SOLID;
964                 brushforbox_brush[i].numpoints = 8;
965                 brushforbox_brush[i].numplanes = 6;
966                 brushforbox_brush[i].points = brushforbox_point + i * 8;
967                 brushforbox_brush[i].planes = brushforbox_plane + i * 6;
968                 brushforpoint_brush[i].supercontents = SUPERCONTENTS_SOLID;
969                 brushforpoint_brush[i].numpoints = 1;
970                 brushforpoint_brush[i].numplanes = 0;
971                 brushforpoint_brush[i].points = brushforbox_point + i * 8;
972                 brushforpoint_brush[i].planes = brushforbox_plane + i * 6;
973         }
974 }
975
976 colbrushf_t *Collision_BrushForBox(const matrix4x4_t *matrix, const vec3_t mins, const vec3_t maxs)
977 {
978         int i, j;
979         vec3_t v;
980         colbrushf_t *brush;
981         if (brushforbox_brush[0].numpoints == 0)
982                 Collision_InitBrushForBox();
983         // FIXME: these probably don't actually need to be normalized if the collision code does not care
984         if (VectorCompare(mins, maxs))
985         {
986                 // point brush
987                 brush = brushforpoint_brush + ((brushforbox_index++) % MAX_BRUSHFORBOX);
988                 VectorCopy(mins, brush->points->v);
989         }
990         else
991         {
992                 brush = brushforbox_brush + ((brushforbox_index++) % MAX_BRUSHFORBOX);
993                 // FIXME: optimize
994                 for (i = 0;i < 8;i++)
995                 {
996                         v[0] = i & 1 ? maxs[0] : mins[0];
997                         v[1] = i & 2 ? maxs[1] : mins[1];
998                         v[2] = i & 4 ? maxs[2] : mins[2];
999                         Matrix4x4_Transform(matrix, v, brush->points[i].v);
1000                 }
1001                 // FIXME: optimize!
1002                 for (i = 0;i < 6;i++)
1003                 {
1004                         VectorClear(v);
1005                         v[i >> 1] = i & 1 ? 1 : -1;
1006                         Matrix4x4_Transform3x3(matrix, v, brush->planes[i].normal);
1007                         VectorNormalize(brush->planes[i].normal);
1008                 }
1009         }
1010         for (j = 0;j < brush->numplanes;j++)
1011                 brush->planes[j].dist = furthestplanedist_float(brush->planes[j].normal, brush->points, brush->numpoints);
1012         VectorCopy(brush->points[0].v, brush->mins);
1013         VectorCopy(brush->points[0].v, brush->maxs);
1014         for (j = 1;j < brush->numpoints;j++)
1015         {
1016                 brush->mins[0] = min(brush->mins[0], brush->points[j].v[0]);
1017                 brush->mins[1] = min(brush->mins[1], brush->points[j].v[1]);
1018                 brush->mins[2] = min(brush->mins[2], brush->points[j].v[2]);
1019                 brush->maxs[0] = max(brush->maxs[0], brush->points[j].v[0]);
1020                 brush->maxs[1] = max(brush->maxs[1], brush->points[j].v[1]);
1021                 brush->maxs[2] = max(brush->maxs[2], brush->points[j].v[2]);
1022         }
1023         brush->mins[0] -= 1;
1024         brush->mins[1] -= 1;
1025         brush->mins[2] -= 1;
1026         brush->maxs[0] += 1;
1027         brush->maxs[1] += 1;
1028         brush->maxs[2] += 1;
1029         Collision_ValidateBrush(brush);
1030         return brush;
1031 }
1032
1033 void Collision_ClipTrace_BrushBox(trace_t *trace, const vec3_t cmins, const vec3_t cmaxs, const vec3_t start, const vec3_t mins, const vec3_t maxs, const vec3_t end, int hitsupercontentsmask)
1034 {
1035         colbrushf_t *boxbrush, *thisbrush_start, *thisbrush_end;
1036         matrix4x4_t identitymatrix;
1037         vec3_t startmins, startmaxs, endmins, endmaxs;
1038
1039         // create brushes for the collision
1040         VectorAdd(start, mins, startmins);
1041         VectorAdd(start, maxs, startmaxs);
1042         VectorAdd(end, mins, endmins);
1043         VectorAdd(end, maxs, endmaxs);
1044         Matrix4x4_CreateIdentity(&identitymatrix);
1045         boxbrush = Collision_BrushForBox(&identitymatrix, cmins, cmaxs);
1046         thisbrush_start = Collision_BrushForBox(&identitymatrix, startmins, startmaxs);
1047         thisbrush_end = Collision_BrushForBox(&identitymatrix, endmins, endmaxs);
1048
1049         memset(trace, 0, sizeof(trace_t));
1050         trace->hitsupercontentsmask = hitsupercontentsmask;
1051         trace->fraction = 1;
1052         trace->realfraction = 1;
1053         trace->allsolid = true;
1054         Collision_TraceBrushBrushFloat(trace, thisbrush_start, thisbrush_end, boxbrush, boxbrush);
1055 }
1056
1057 // LordHavoc: currently unused and not yet tested
1058 // note: this can be used for tracing a moving sphere vs a stationary sphere,
1059 // by simply adding the moving sphere's radius to the sphereradius parameter,
1060 // all the results are correct (impactpoint, impactnormal, and fraction)
1061 float Collision_ClipTrace_Line_Sphere(double *linestart, double *lineend, double *sphereorigin, double sphereradius, double *impactpoint, double *impactnormal)
1062 {
1063         double dir[3], scale, v[3], deviationdist, impactdist, linelength;
1064         // make sure the impactpoint and impactnormal are valid even if there is
1065         // no collision
1066         impactpoint[0] = lineend[0];
1067         impactpoint[1] = lineend[1];
1068         impactpoint[2] = lineend[2];
1069         impactnormal[0] = 0;
1070         impactnormal[1] = 0;
1071         impactnormal[2] = 0;
1072         // calculate line direction
1073         dir[0] = lineend[0] - linestart[0];
1074         dir[1] = lineend[1] - linestart[1];
1075         dir[2] = lineend[2] - linestart[2];
1076         // normalize direction
1077         linelength = sqrt(dir[0] * dir[0] + dir[1] * dir[1] + dir[2] * dir[2]);
1078         if (linelength)
1079         {
1080                 scale = 1.0 / linelength;
1081                 dir[0] *= scale;
1082                 dir[1] *= scale;
1083                 dir[2] *= scale;
1084         }
1085         // this dotproduct calculates the distance along the line at which the
1086         // sphere origin is (nearest point to the sphere origin on the line)
1087         impactdist = dir[0] * (sphereorigin[0] - linestart[0]) + dir[1] * (sphereorigin[1] - linestart[1]) + dir[2] * (sphereorigin[2] - linestart[2]);
1088         // calculate point on line at that distance, and subtract the
1089         // sphereorigin from it, so we have a vector to measure for the distance
1090         // of the line from the sphereorigin (deviation, how off-center it is)
1091         v[0] = linestart[0] + impactdist * dir[0] - sphereorigin[0];
1092         v[1] = linestart[1] + impactdist * dir[1] - sphereorigin[1];
1093         v[2] = linestart[2] + impactdist * dir[2] - sphereorigin[2];
1094         deviationdist = v[0] * v[0] + v[1] * v[1] + v[2] * v[2];
1095         // if outside the radius, it's a miss for sure
1096         // (we do this comparison using squared radius to avoid a sqrt)
1097         if (deviationdist > sphereradius*sphereradius)
1098                 return 1; // miss (off to the side)
1099         // nudge back to find the correct impact distance
1100         impactdist += (sqrt(deviationdist) - sphereradius);
1101         if (impactdist >= linelength)
1102                 return 1; // miss (not close enough)
1103         if (impactdist < 0)
1104                 return 1; // miss (linestart is past or inside sphere)
1105         // calculate new impactpoint
1106         impactpoint[0] = linestart[0] + impactdist * dir[0];
1107         impactpoint[1] = linestart[1] + impactdist * dir[1];
1108         impactpoint[2] = linestart[2] + impactdist * dir[2];
1109         // calculate impactnormal (surface normal at point of impact)
1110         impactnormal[0] = impactpoint[0] - sphereorigin[0];
1111         impactnormal[1] = impactpoint[1] - sphereorigin[1];
1112         impactnormal[2] = impactpoint[2] - sphereorigin[2];
1113         // normalize impactnormal
1114         scale = impactnormal[0] * impactnormal[0] + impactnormal[1] * impactnormal[1] + impactnormal[2] * impactnormal[2];
1115         if (scale)
1116         {
1117                 scale = 1.0 / sqrt(scale);
1118                 impactnormal[0] *= scale;
1119                 impactnormal[1] *= scale;
1120                 impactnormal[2] *= scale;
1121         }
1122         // return fraction of movement distance
1123         return impactdist / linelength;
1124 }
1125
1126 void Collision_TraceLineTriangleFloat(trace_t *trace, const vec3_t linestart, const vec3_t lineend, const float *point0, const float *point1, const float *point2)
1127 {
1128 #if 1
1129         // more optimized
1130         float d1, d2, d, f, impact[3], edgenormal[3], faceplanenormal[3], faceplanedist, faceplanenormallength2, edge01[3], edge21[3], edge02[3];
1131
1132         // this function executes:
1133         // 32 ops when line starts behind triangle
1134         // 38 ops when line ends infront of triangle
1135         // 43 ops when line fraction is already closer than this triangle
1136         // 72 ops when line is outside edge 01
1137         // 92 ops when line is outside edge 21
1138         // 115 ops when line is outside edge 02
1139         // 123 ops when line impacts triangle and updates trace results
1140
1141         // this code is designed for clockwise triangles, conversion to
1142         // counterclockwise would require swapping some things around...
1143         // it is easier to simply swap the point0 and point2 parameters to this
1144         // function when calling it than it is to rewire the internals.
1145
1146         // calculate the faceplanenormal of the triangle, this represents the front side
1147         // 15 ops
1148         VectorSubtract(point0, point1, edge01);
1149         VectorSubtract(point2, point1, edge21);
1150         CrossProduct(edge01, edge21, faceplanenormal);
1151         // there's no point in processing a degenerate triangle (GIGO - Garbage In, Garbage Out)
1152         // 6 ops
1153         faceplanenormallength2 = DotProduct(faceplanenormal, faceplanenormal);
1154         if (faceplanenormallength2 < 0.0001f)
1155                 return;
1156         // calculate the distance
1157         // 5 ops
1158         faceplanedist = DotProduct(point0, faceplanenormal);
1159
1160         // if start point is on the back side there is no collision
1161         // (we don't care about traces going through the triangle the wrong way)
1162
1163         // calculate the start distance
1164         // 6 ops
1165         d1 = DotProduct(faceplanenormal, linestart);
1166         if (d1 <= faceplanedist)
1167                 return;
1168
1169         // calculate the end distance
1170         // 6 ops
1171         d2 = DotProduct(faceplanenormal, lineend);
1172         // if both are in front, there is no collision
1173         if (d2 >= faceplanedist)
1174                 return;
1175
1176         // from here on we know d1 is >= 0 and d2 is < 0
1177         // this means the line starts infront and ends behind, passing through it
1178
1179         // calculate the recipricol of the distance delta,
1180         // so we can use it multiple times cheaply (instead of division)
1181         // 2 ops
1182         d = 1.0f / (d1 - d2);
1183         // calculate the impact fraction by taking the start distance (> 0)
1184         // and subtracting the face plane distance (this is the distance of the
1185         // triangle along that same normal)
1186         // then multiply by the recipricol distance delta
1187         // 2 ops
1188         f = (d1 - faceplanedist) * d;
1189         // skip out if this impact is further away than previous ones
1190         // 1 ops
1191         if (f > trace->realfraction)
1192                 return;
1193         // calculate the perfect impact point for classification of insidedness
1194         // 9 ops
1195         impact[0] = linestart[0] + f * (lineend[0] - linestart[0]);
1196         impact[1] = linestart[1] + f * (lineend[1] - linestart[1]);
1197         impact[2] = linestart[2] + f * (lineend[2] - linestart[2]);
1198
1199         // calculate the edge normal and reject if impact is outside triangle
1200         // (an edge normal faces away from the triangle, to get the desired normal
1201         //  a crossproduct with the faceplanenormal is used, and because of the way
1202         // the insidedness comparison is written it does not need to be normalized)
1203
1204         // first use the two edges from the triangle plane math
1205         // the other edge only gets calculated if the point survives that long
1206
1207         // 20 ops
1208         CrossProduct(edge01, faceplanenormal, edgenormal);
1209         if (DotProduct(impact, edgenormal) > DotProduct(point1, edgenormal))
1210                 return;
1211
1212         // 20 ops
1213         CrossProduct(faceplanenormal, edge21, edgenormal);
1214         if (DotProduct(impact, edgenormal) > DotProduct(point2, edgenormal))
1215                 return;
1216
1217         // 23 ops
1218         VectorSubtract(point0, point2, edge02);
1219         CrossProduct(faceplanenormal, edge02, edgenormal);
1220         if (DotProduct(impact, edgenormal) > DotProduct(point0, edgenormal))
1221                 return;
1222
1223         // 8 ops (rare)
1224
1225         // store the new trace fraction
1226         trace->realfraction = f;
1227
1228         // calculate a nudged fraction to keep it out of the surface
1229         // (the main fraction remains perfect)
1230         trace->fraction = f - collision_impactnudge.value * d;
1231
1232         // store the new trace plane (because collisions only happen from
1233         // the front this is always simply the triangle normal, never flipped)
1234         d = 1.0 / sqrt(faceplanenormallength2);
1235         VectorScale(faceplanenormal, d, trace->plane.normal);
1236         trace->plane.dist = faceplanedist * d;
1237 #else
1238         float d1, d2, d, f, fnudged, impact[3], edgenormal[3], faceplanenormal[3], faceplanedist, edge[3];
1239
1240         // this code is designed for clockwise triangles, conversion to
1241         // counterclockwise would require swapping some things around...
1242         // it is easier to simply swap the point0 and point2 parameters to this
1243         // function when calling it than it is to rewire the internals.
1244
1245         // calculate the unnormalized faceplanenormal of the triangle,
1246         // this represents the front side
1247         TriangleNormal(point0, point1, point2, faceplanenormal);
1248         // there's no point in processing a degenerate triangle
1249         // (GIGO - Garbage In, Garbage Out)
1250         if (DotProduct(faceplanenormal, faceplanenormal) < 0.0001f)
1251                 return;
1252         // calculate the unnormalized distance
1253         faceplanedist = DotProduct(point0, faceplanenormal);
1254
1255         // calculate the unnormalized start distance
1256         d1 = DotProduct(faceplanenormal, linestart) - faceplanedist;
1257         // if start point is on the back side there is no collision
1258         // (we don't care about traces going through the triangle the wrong way)
1259         if (d1 <= 0)
1260                 return;
1261
1262         // calculate the unnormalized end distance
1263         d2 = DotProduct(faceplanenormal, lineend) - faceplanedist;
1264         // if both are in front, there is no collision
1265         if (d2 >= 0)
1266                 return;
1267
1268         // from here on we know d1 is >= 0 and d2 is < 0
1269         // this means the line starts infront and ends behind, passing through it
1270
1271         // calculate the recipricol of the distance delta,
1272         // so we can use it multiple times cheaply (instead of division)
1273         d = 1.0f / (d1 - d2);
1274         // calculate the impact fraction by taking the start distance (> 0)
1275         // and subtracting the face plane distance (this is the distance of the
1276         // triangle along that same normal)
1277         // then multiply by the recipricol distance delta
1278         f = d1 * d;
1279         // skip out if this impact is further away than previous ones
1280         if (f > trace->realfraction)
1281                 return;
1282         // calculate the perfect impact point for classification of insidedness
1283         impact[0] = linestart[0] + f * (lineend[0] - linestart[0]);
1284         impact[1] = linestart[1] + f * (lineend[1] - linestart[1]);
1285         impact[2] = linestart[2] + f * (lineend[2] - linestart[2]);
1286
1287         // calculate the edge normal and reject if impact is outside triangle
1288         // (an edge normal faces away from the triangle, to get the desired normal
1289         //  a crossproduct with the faceplanenormal is used, and because of the way
1290         // the insidedness comparison is written it does not need to be normalized)
1291
1292         VectorSubtract(point2, point0, edge);
1293         CrossProduct(edge, faceplanenormal, edgenormal);
1294         if (DotProduct(impact, edgenormal) > DotProduct(point0, edgenormal))
1295                 return;
1296
1297         VectorSubtract(point0, point1, edge);
1298         CrossProduct(edge, faceplanenormal, edgenormal);
1299         if (DotProduct(impact, edgenormal) > DotProduct(point1, edgenormal))
1300                 return;
1301
1302         VectorSubtract(point1, point2, edge);
1303         CrossProduct(edge, faceplanenormal, edgenormal);
1304         if (DotProduct(impact, edgenormal) > DotProduct(point2, edgenormal))
1305                 return;
1306
1307         // store the new trace fraction
1308         trace->realfraction = bound(0, f, 1);
1309
1310         // store the new trace plane (because collisions only happen from
1311         // the front this is always simply the triangle normal, never flipped)
1312         VectorNormalize(faceplanenormal);
1313         VectorCopy(faceplanenormal, trace->plane.normal);
1314         trace->plane.dist = DotProduct(point0, faceplanenormal);
1315
1316         // calculate the normalized start and end distances
1317         d1 = DotProduct(trace->plane.normal, linestart) - trace->plane.dist;
1318         d2 = DotProduct(trace->plane.normal, lineend) - trace->plane.dist;
1319
1320         // calculate a nudged fraction to keep it out of the surface
1321         // (the main fraction remains perfect)
1322         fnudged = (d1 - collision_impactnudge.value) / (d1 - d2);
1323         trace->fraction = bound(0, fnudged, 1);
1324
1325         // store the new trace endpos
1326         // not needed, it's calculated later when the trace is finished
1327         //trace->endpos[0] = linestart[0] + fnudged * (lineend[0] - linestart[0]);
1328         //trace->endpos[1] = linestart[1] + fnudged * (lineend[1] - linestart[1]);
1329         //trace->endpos[2] = linestart[2] + fnudged * (lineend[2] - linestart[2]);
1330 #endif
1331 }
1332
1333 typedef struct colbspnode_s
1334 {
1335         mplane_t plane;
1336         struct colbspnode_s *children[2];
1337         // the node is reallocated or split if max is reached
1338         int numcolbrushf;
1339         int maxcolbrushf;
1340         colbrushf_t **colbrushflist;
1341         //int numcolbrushd;
1342         //int maxcolbrushd;
1343         //colbrushd_t **colbrushdlist;
1344 }
1345 colbspnode_t;
1346
1347 typedef struct colbsp_s
1348 {
1349         mempool_t *mempool;
1350         colbspnode_t *nodes;
1351 }
1352 colbsp_t;
1353
1354 colbsp_t *Collision_CreateCollisionBSP(mempool_t *mempool)
1355 {
1356         colbsp_t *bsp;
1357         bsp = Mem_Alloc(mempool, sizeof(colbsp_t));
1358         bsp->mempool = mempool;
1359         bsp->nodes = Mem_Alloc(bsp->mempool, sizeof(colbspnode_t));
1360         return bsp;
1361 }
1362
1363 void Collision_FreeCollisionBSPNode(colbspnode_t *node)
1364 {
1365         if (node->children[0])
1366                 Collision_FreeCollisionBSPNode(node->children[0]);
1367         if (node->children[1])
1368                 Collision_FreeCollisionBSPNode(node->children[1]);
1369         while (--node->numcolbrushf)
1370                 Mem_Free(node->colbrushflist[node->numcolbrushf]);
1371         //while (--node->numcolbrushd)
1372         //      Mem_Free(node->colbrushdlist[node->numcolbrushd]);
1373         Mem_Free(node);
1374 }
1375
1376 void Collision_FreeCollisionBSP(colbsp_t *bsp)
1377 {
1378         Collision_FreeCollisionBSPNode(bsp->nodes);
1379         Mem_Free(bsp);
1380 }
1381
1382 void Collision_BoundingBoxOfBrushTraceSegment(const colbrushf_t *start, const colbrushf_t *end, vec3_t mins, vec3_t maxs, float startfrac, float endfrac)
1383 {
1384         int i;
1385         colpointf_t *ps, *pe;
1386         float tempstart[3], tempend[3];
1387         VectorLerp(start->points[0].v, startfrac, end->points[0].v, mins);
1388         VectorCopy(mins, maxs);
1389         for (i = 0, ps = start->points, pe = end->points;i < start->numpoints;i++, ps++, pe++)
1390         {
1391                 VectorLerp(ps->v, startfrac, pe->v, tempstart);
1392                 VectorLerp(ps->v, endfrac, pe->v, tempend);
1393                 mins[0] = min(mins[0], min(tempstart[0], tempend[0]));
1394                 mins[1] = min(mins[1], min(tempstart[1], tempend[1]));
1395                 mins[2] = min(mins[2], min(tempstart[2], tempend[2]));
1396                 maxs[0] = min(maxs[0], min(tempstart[0], tempend[0]));
1397                 maxs[1] = min(maxs[1], min(tempstart[1], tempend[1]));
1398                 maxs[2] = min(maxs[2], min(tempstart[2], tempend[2]));
1399         }
1400         mins[0] -= 1;
1401         mins[1] -= 1;
1402         mins[2] -= 1;
1403         maxs[0] += 1;
1404         maxs[1] += 1;
1405         maxs[2] += 1;
1406 }
1407