277 lines
5.1 KiB
C
277 lines
5.1 KiB
C
// merge.c
|
|
|
|
#include "bsp5.h"
|
|
|
|
|
|
#define continuous_epsilon 0.001
|
|
|
|
/*
|
|
================
|
|
checkcolinear
|
|
|
|
================
|
|
*/
|
|
void checkcolinear (face_t *f)
|
|
{
|
|
int i, j;
|
|
vec3_t v1, v2;
|
|
|
|
for (i=0 ; i<f->numpoints ;i++)
|
|
{
|
|
// skip the point if the vector from the previous point is the same
|
|
// as the vector to the next point
|
|
j = (i - 1 < 0) ? f->numpoints - 1 : i - 1;
|
|
vectorsubtract (f->pts[i], f->pts[j], v1);
|
|
vectornormalize (v1);
|
|
|
|
j = (i + 1 == f->numpoints) ? 0 : i + 1;
|
|
vectorsubtract (f->pts[j], f->pts[i], v2);
|
|
vectornormalize (v2);
|
|
|
|
if (vectorcompare (v1, v2))
|
|
error ("colinear edge");
|
|
}
|
|
|
|
}
|
|
|
|
|
|
/*
|
|
=============
|
|
trymerge
|
|
|
|
if two polygons share a common edge and the edges that meet at the
|
|
common points are both inside the other polygons, merge them
|
|
|
|
returns null if the faces couldn't be merged, or the new face.
|
|
the originals will not be freed.
|
|
=============
|
|
*/
|
|
face_t *trymerge (face_t *f1, face_t *f2)
|
|
{
|
|
vec_t *p1, *p2, *p3, *p4, *back;
|
|
face_t *newf;
|
|
int i, j, k, l;
|
|
vec3_t normal, delta, planenormal;
|
|
vec_t dot;
|
|
plane_t *plane;
|
|
qboolean keep1, keep2;
|
|
|
|
if (f1->numpoints == -1 || f2->numpoints == -1)
|
|
return null;
|
|
if (f1->planeside != f2->planeside)
|
|
return null;
|
|
if (f1->texturenum != f2->texturenum)
|
|
return null;
|
|
if (f1->contents[0] != f2->contents[0])
|
|
return null;
|
|
if (f1->contents[1] != f2->contents[1])
|
|
return null;
|
|
|
|
//
|
|
// find a common edge
|
|
//
|
|
p1 = p2 = null; // stop compiler warning
|
|
j = 0; //
|
|
|
|
for (i=0 ; i<f1->numpoints ; i++)
|
|
{
|
|
p1 = f1->pts[i];
|
|
p2 = f1->pts[(i+1)%f1->numpoints];
|
|
for (j=0 ; j<f2->numpoints ; j++)
|
|
{
|
|
p3 = f2->pts[j];
|
|
p4 = f2->pts[(j+1)%f2->numpoints];
|
|
for (k=0 ; k<3 ; k++)
|
|
{
|
|
if (fabs(p1[k] - p4[k]) > equal_epsilon)
|
|
break;
|
|
if (fabs(p2[k] - p3[k]) > equal_epsilon)
|
|
break;
|
|
}
|
|
if (k==3)
|
|
break;
|
|
}
|
|
if (j < f2->numpoints)
|
|
break;
|
|
}
|
|
|
|
if (i == f1->numpoints)
|
|
return null; // no matching edges
|
|
|
|
//
|
|
// check slope of connected lines
|
|
// if the slopes are colinear, the point can be removed
|
|
//
|
|
plane = &planes[f1->planenum];
|
|
vectorcopy (plane->normal, planenormal);
|
|
if (f1->planeside)
|
|
vectorsubtract (vec3_origin, planenormal, planenormal);
|
|
|
|
back = f1->pts[(i+f1->numpoints-1)%f1->numpoints];
|
|
vectorsubtract (p1, back, delta);
|
|
crossproduct (planenormal, delta, normal);
|
|
vectornormalize (normal);
|
|
|
|
back = f2->pts[(j+2)%f2->numpoints];
|
|
vectorsubtract (back, p1, delta);
|
|
dot = dotproduct (delta, normal);
|
|
if (dot > continuous_epsilon)
|
|
return null; // not a convex polygon
|
|
keep1 = dot < -continuous_epsilon;
|
|
|
|
back = f1->pts[(i+2)%f1->numpoints];
|
|
vectorsubtract (back, p2, delta);
|
|
crossproduct (planenormal, delta, normal);
|
|
vectornormalize (normal);
|
|
|
|
back = f2->pts[(j+f2->numpoints-1)%f2->numpoints];
|
|
vectorsubtract (back, p2, delta);
|
|
dot = dotproduct (delta, normal);
|
|
if (dot > continuous_epsilon)
|
|
return null; // not a convex polygon
|
|
keep2 = dot < -continuous_epsilon;
|
|
|
|
//
|
|
// build the new polygon
|
|
//
|
|
if (f1->numpoints + f2->numpoints > maxedges)
|
|
{
|
|
// error ("trymerge: too many edges!");
|
|
return null;
|
|
}
|
|
|
|
newf = newfacefromface (f1);
|
|
|
|
// copy first polygon
|
|
for (k=(i+1)%f1->numpoints ; k != i ; k=(k+1)%f1->numpoints)
|
|
{
|
|
if (k==(i+1)%f1->numpoints && !keep2)
|
|
continue;
|
|
|
|
vectorcopy (f1->pts[k], newf->pts[newf->numpoints]);
|
|
newf->numpoints++;
|
|
}
|
|
|
|
// copy second polygon
|
|
for (l= (j+1)%f2->numpoints ; l != j ; l=(l+1)%f2->numpoints)
|
|
{
|
|
if (l==(j+1)%f2->numpoints && !keep1)
|
|
continue;
|
|
vectorcopy (f2->pts[l], newf->pts[newf->numpoints]);
|
|
newf->numpoints++;
|
|
}
|
|
|
|
return newf;
|
|
}
|
|
|
|
|
|
/*
|
|
===============
|
|
mergefacetolist
|
|
===============
|
|
*/
|
|
qboolean mergedebug;
|
|
face_t *mergefacetolist (face_t *face, face_t *list)
|
|
{
|
|
face_t *newf, *f;
|
|
|
|
for (f=list ; f ; f=f->next)
|
|
{
|
|
//checkcolinear (f);
|
|
if (mergedebug)
|
|
{
|
|
draw_clearwindow ();
|
|
draw_drawface (face);
|
|
draw_drawface (f);
|
|
draw_setblack ();
|
|
}
|
|
newf = trymerge (face, f);
|
|
if (!newf)
|
|
continue;
|
|
freeface (face);
|
|
f->numpoints = -1; // merged out
|
|
return mergefacetolist (newf, list);
|
|
}
|
|
|
|
// didn't merge, so add at start
|
|
face->next = list;
|
|
return face;
|
|
}
|
|
|
|
|
|
/*
|
|
===============
|
|
freemergelistscraps
|
|
===============
|
|
*/
|
|
face_t *freemergelistscraps (face_t *merged)
|
|
{
|
|
face_t *head, *next;
|
|
|
|
head = null;
|
|
for ( ; merged ; merged = next)
|
|
{
|
|
next = merged->next;
|
|
if (merged->numpoints == -1)
|
|
freeface (merged);
|
|
else
|
|
{
|
|
merged->next = head;
|
|
head = merged;
|
|
}
|
|
}
|
|
|
|
return head;
|
|
}
|
|
|
|
|
|
/*
|
|
===============
|
|
mergeplanefaces
|
|
===============
|
|
*/
|
|
void mergeplanefaces (surface_t *plane)
|
|
{
|
|
face_t *f1, *next;
|
|
face_t *merged;
|
|
|
|
merged = null;
|
|
|
|
for (f1 = plane->faces ; f1 ; f1 = next)
|
|
{
|
|
next = f1->next;
|
|
merged = mergefacetolist (f1, merged);
|
|
}
|
|
|
|
// chain all of the non-empty faces to the plane
|
|
plane->faces = freemergelistscraps (merged);
|
|
}
|
|
|
|
|
|
/*
|
|
============
|
|
mergeall
|
|
============
|
|
*/
|
|
void mergeall (surface_t *surfhead)
|
|
{
|
|
surface_t *surf;
|
|
int mergefaces;
|
|
face_t *f;
|
|
|
|
printf ("---- mergeall ----\n");
|
|
|
|
mergefaces = 0;
|
|
for (surf = surfhead ; surf ; surf=surf->next)
|
|
{
|
|
mergeplanefaces (surf);
|
|
draw_clearwindow ();
|
|
for (f=surf->faces ; f ; f=f->next)
|
|
{
|
|
draw_drawface (f);
|
|
mergefaces++;
|
|
}
|
|
}
|
|
|
|
printf ("%i mergefaces\n", mergefaces);
|
|
}
|