quakeforge/tools/qfbsp/source/merge.c

244 lines
5.7 KiB
C
Raw Normal View History

2002-09-19 18:51:19 +00:00
/*
Copyright (C) 1996-1997 Id Software, Inc.
2002-09-19 18:51:19 +00:00
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
2002-09-19 18:51:19 +00:00
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
2002-09-19 18:51:19 +00:00
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
2002-09-19 18:51:19 +00:00
See file, 'COPYING', for details.
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
static __attribute__ ((used)) const char rcsid[] =
"$Id$";
#include "QF/sys.h"
#include "brush.h"
#include "bsp5.h"
#include "csg4.h"
#include "draw.h"
#include "merge.h"
#include "surfaces.h"
/** \addtogroup qfbsp_merge
*/
//@{
#define CONTINUOUS_EPSILON 0.001
2010-08-29 11:35:43 +00:00
/** Try to merge two polygons.
2002-09-23 16:27:17 +00:00
If two polygons share a common edge and the edges that meet at the common
2010-08-29 11:35:43 +00:00
points are both inside the other polygons, merge them. The two polygons
must be on the same plane, the same side of the plane, have the same
texture and have the same contents on each side.
\param f1 The first face.
\param f2 The second face.
\return The new face or NULL if the faces could not be merged.
2010-08-29 11:35:43 +00:00
\note The originals will NOT be freed.
*/
2010-08-29 11:35:43 +00:00
static face_t *
TryMerge (const face_t *f1, const face_t *f2)
{
2002-09-19 17:14:23 +00:00
face_t *newf;
int i, j, k, l;
plane_t *plane;
qboolean keep1, keep2;
2002-09-19 18:51:19 +00:00
vec3_t normal, delta, planenormal;
vec_t dot;
vec_t *p1, *p2, *p3, *p4, *back;
2003-09-08 03:00:53 +00:00
winding_t *f1p, *f2p, *newfp;
2002-09-19 17:14:23 +00:00
2003-09-08 03:00:53 +00:00
if (!(f1p = f1->points) || !(f2p = f2->points))
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;
2002-09-19 17:14:23 +00:00
2010-08-29 11:35:43 +00:00
p1 = p2 = NULL;
2002-09-19 18:51:19 +00:00
j = 0;
2002-09-19 17:14:23 +00:00
2010-08-29 11:35:43 +00:00
// find a common edge
2003-09-08 03:00:53 +00:00
for (i = 0; i < f1p->numpoints; i++) {
p1 = f1p->points[i];
p2 = f1p->points[(i + 1) % f1p->numpoints];
for (j = 0; j < f2p->numpoints; j++) {
p3 = f2p->points[j];
p4 = f2p->points[(j + 1) % f2p->numpoints];
2002-09-19 17:14:23 +00:00
for (k = 0; k < 3; k++) {
if (fabs (p1[k] - p4[k]) > EQUAL_EPSILON)
break;
2002-09-19 17:14:23 +00:00
if (fabs (p2[k] - p3[k]) > EQUAL_EPSILON)
break;
}
2002-09-19 17:14:23 +00:00
if (k == 3)
2003-09-08 03:00:53 +00:00
goto found_edge;
}
}
2003-09-08 03:00:53 +00:00
return NULL; // no matching edges
found_edge:
2002-09-23 16:27:17 +00:00
// 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)
VectorNegate (planenormal, planenormal);
2002-09-19 17:14:23 +00:00
2003-09-08 03:00:53 +00:00
back = f1p->points[(i + f1p->numpoints - 1) % f1p->numpoints];
VectorSubtract (p1, back, delta);
CrossProduct (planenormal, delta, normal);
_VectorNormalize (normal);
2002-09-19 17:14:23 +00:00
2003-09-08 03:00:53 +00:00
back = f2p->points[(j + 2) % f2p->numpoints];
VectorSubtract (back, p1, delta);
dot = DotProduct (delta, normal);
if (dot > CONTINUOUS_EPSILON)
2002-09-19 17:14:23 +00:00
return NULL; // not a convex polygon
keep1 = dot < -CONTINUOUS_EPSILON;
2002-09-19 17:14:23 +00:00
2003-09-08 03:00:53 +00:00
back = f1p->points[(i + 2) % f1p->numpoints];
VectorSubtract (back, p2, delta);
CrossProduct (planenormal, delta, normal);
_VectorNormalize (normal);
2003-09-08 03:00:53 +00:00
back = f2p->points[(j + f2p->numpoints - 1) % f2p->numpoints];
VectorSubtract (back, p2, delta);
dot = DotProduct (delta, normal);
if (dot > CONTINUOUS_EPSILON)
2002-09-19 17:14:23 +00:00
return NULL; // not a convex polygon
keep2 = dot < -CONTINUOUS_EPSILON;
2002-09-23 16:27:17 +00:00
// build the new polygon
2003-09-08 03:00:53 +00:00
k = f1p->numpoints + f2p->numpoints - 4 + keep1 + keep2;
newf = NewFaceFromFace (f1);
2003-09-08 03:00:53 +00:00
newfp = newf->points = NewWinding (k);
2002-09-19 17:14:23 +00:00
2002-09-23 16:27:17 +00:00
// copy first polygon
2003-09-08 03:00:53 +00:00
for (k = (i + 1) % f1p->numpoints; k != i; k = (k + 1) % f1p->numpoints) {
if (k == (i + 1) % f1p->numpoints && !keep2)
continue;
2002-09-19 17:14:23 +00:00
2003-09-08 03:00:53 +00:00
VectorCopy (f1p->points[k], newfp->points[newfp->numpoints]);
newfp->numpoints++;
}
2002-09-19 17:14:23 +00:00
2002-09-23 16:27:17 +00:00
// copy second polygon
2003-09-08 03:00:53 +00:00
for (l = (j + 1) % f2p->numpoints; l != j; l = (l + 1) % f2p->numpoints) {
if (l == (j + 1) % f2p->numpoints && !keep1)
continue;
2003-09-08 03:00:53 +00:00
VectorCopy (f2p->points[l], newfp->points[newfp->numpoints]);
newfp->numpoints++;
}
return newf;
}
2002-09-19 17:14:23 +00:00
qboolean mergedebug;
2010-08-29 11:35:43 +00:00
face_t *
2002-09-19 18:51:19 +00:00
MergeFaceToList (face_t *face, face_t *list)
{
2002-09-19 17:14:23 +00:00
face_t *newf, *f;
for (f = list; f; f = f->next) {
2010-08-27 13:12:13 +00:00
// CheckColinear (f);
2002-09-19 17:14:23 +00:00
if (mergedebug) {
Draw_ClearWindow ();
Draw_DrawFace (face);
Draw_DrawFace (f);
Draw_SetBlack ();
}
newf = TryMerge (face, f);
if (!newf)
continue;
FreeFace (face);
2003-09-08 03:00:53 +00:00
FreeWinding (f->points); // merged out
f->points = 0;
return MergeFaceToList (newf, list);
}
2002-09-19 17:14:23 +00:00
2002-09-23 16:27:17 +00:00
// didn't merge, so add at start
face->next = list;
return face;
}
2010-08-29 11:35:43 +00:00
face_t *
2002-09-19 18:51:19 +00:00
FreeMergeListScraps (face_t *merged)
{
2002-09-19 17:14:23 +00:00
face_t *head, *next;
head = NULL;
2002-09-19 17:14:23 +00:00
for (; merged; merged = next) {
next = merged->next;
2003-09-08 03:00:53 +00:00
if (!merged->points)
FreeFace (merged);
2002-09-19 17:14:23 +00:00
else {
merged->next = head;
head = merged;
}
}
return head;
}
2002-09-19 17:14:23 +00:00
void
2002-09-19 18:51:19 +00:00
MergePlaneFaces (surface_t *plane)
{
2002-09-19 18:51:19 +00:00
face_t *merged, *next, *f1;
2002-09-19 17:14:23 +00:00
merged = NULL;
2002-09-19 17:14:23 +00:00
for (f1 = plane->faces; f1; f1 = next) {
next = f1->next;
merged = MergeFaceToList (f1, merged);
}
2002-09-23 16:27:17 +00:00
// chain all of the non-empty faces to the plane
plane->faces = FreeMergeListScraps (merged);
}
2002-09-19 17:14:23 +00:00
void
2010-09-02 04:01:45 +00:00
MergeAll (surface_t *surfhead)
{
2002-09-19 17:14:23 +00:00
face_t *f;
2002-09-19 18:51:19 +00:00
int mergefaces;
surface_t *surf;
2002-09-19 17:14:23 +00:00
printf ("---- MergeAll ----\n");
2002-09-19 17:14:23 +00:00
mergefaces = 0;
for (surf = surfhead; surf; surf = surf->next) {
MergePlaneFaces (surf);
2002-09-19 17:14:23 +00:00
Draw_ClearWindow ();
for (f = surf->faces; f; f = f->next) {
Draw_DrawFace (f);
mergefaces++;
}
}
2002-09-19 17:14:23 +00:00
printf ("%i mergefaces\n", mergefaces);
}
//@}