some more progress with box clipping and much a saner test harness

This commit is contained in:
Bill Currie 2009-03-20 03:33:03 +00:00 committed by Jeff Teunissen
parent e992edaf31
commit f59f2dc7b9
3 changed files with 289 additions and 120 deletions

View file

@ -8,7 +8,7 @@ noinst_PROGRAMS= testclip
testclip_SOURCES=testclip.c
testclip_LDADD= libQFmodels.la
testclip_DEPENDENCIES= libQFmodels.la
testclip_DEPENDENCIES=
lib_LTLIBRARIES= libQFmodels.la @VID_MODEL_TARGETS@
EXTRA_LTLIBRARIES= \

View file

@ -1,5 +1,12 @@
#include "QF/va.h"
#include "getopt.h"
#include "world.h"
#undef DIST_EPSILON
#define DIST_EPSILON 0
#include "trace.c"
dclipnode_t clipnodes1[] = {
{ 0, { 1, -2}},
{ 1, {-2, 2}},
@ -102,7 +109,94 @@ hull_t hull2 = {
{0, 0, 0},
};
static void
dclipnode_t clipnodes3[] = {
{0, {1, 12}}, // 0
{1, {2, 7}}, // 1
{2, {-2, 3}}, // 2
{3, {4, -2}}, // 3
{4, {-2, 5}}, // 4
{5, {-2, 6}}, // 5
{6, {-1, -2}}, // 6
{7, {-2, 8}}, // 7
{8, {9, -2}}, // 8
{9, {10, 11}}, // 9
{10, {-2, -1}}, // 10
{6, {-1, -2}}, // 11
{3, {13, -2}}, // 12
{11, {14, -2}}, // 13
{4, {-2, 15}}, // 14
{2, {-2, 16}}, // 15
{9, {-1, -2}}, // 16
};
mplane_t planes3[] = {
{{0, 1, 0}, -224, 1, 0, {0, 0}}, // 0
{{0, 1, 0}, -192, 1, 0, {0, 0}}, // 1
{{0, 0, 1}, 192, 2, 0, {0, 0}}, // 2
{{1, 0, 0}, 384, 0, 0, {0, 0}}, // 3
{{1, 0, 0}, 576, 0, 0, {0, 0}}, // 4
{{0, 1, 0}, -16, 1, 0, {0, 0}}, // 5
{{-0, 0.242536, 0.970142}, -3.88057, 5, 0, {0, 0}}, // 6
{{1, 0, 0}, 552, 0, 0, {0, 0}}, // 7
{{1, 0, 0}, 408, 0, 0, {0, 0}}, // 8
{{0, 0, 1}, 48, 2, 0, {0, 0}}, // 9
{{0, 0, 1}, 160, 2, 0, {0, 0}}, // 10
{{0, 1, 0}, -416, 1, 0, {0, 0}}, // 11
};
hull_t hull3 = {
clipnodes3,
planes3,
0,
6,
{0, 0, 0},
{0, 0, 0},
};
typedef struct {
const char *desc;
hull_t *hull;
vec3_t start;
vec3_t end;
struct {
float frac;
qboolean allsolid;
qboolean startsolid;
qboolean inopen;
qboolean inwater;
} expect;
} test_t;
test_t tests[] = {
{0, &hull1, {0, 47, 40}, {0, 47, 32}, {0.5, 0, 0, 1, 0}},
{0, &hull1, {0, 48, 40}, {0, 48, 32}, {1, 0, 0, 1, 0}},
{0, &hull1, {0, 52, 35}, {0, 44, 35}, {0.5, 0, 0, 1, 0}},
{0, &hull1, {0, 52, 36}, {0, 44, 36}, {1, 0, 0, 1, 0}},
{0, &hull1, {47, -32, 36}, {49, -32, 36}, {1, 0, 0, 1, 0}},
{0, &hull1, {48, -32, 36}, {50, -32, 36}, {1, 0, 1, 1, 0}},
{0, &hull1, {44, -32, 59}, {52, -32, 59}, {0.5, 0, 0, 1, 0}},
{0, &hull1, {44, -32, 60}, {52, -32, 60}, {1, 0, 0, 1, 0}},
{0, &hull1, {47, -32, 76}, {47, -32, 44}, {1, 0, 1, 1, 0}},
{0, &hull1, {48, -32, 76}, {48, -32, 44}, {0.5, 0, 1, 1, 0}},
{0, &hull2, {896, 2048, -144}, {896, 2048, -152}, {0.5, 0, 0, 1, 0}},
{0, &hull1, {96, -47.9612, 61}, {96, -47.1025, 59}, {0.5, 0, 0, 1, 0}},
{0, &hull1, {94.8916, -34.8506, 61}, {94.8146, -28.5696, 59},
{1, 0, 0, 1, 0}},
{0, &hull3, {480, -216, 76}, {480, -200, 76}, {0.5, 0, 0, 1, 0}},
};
const int num_tests = sizeof (tests) / sizeof (tests[0]);
int verbose = 0;
static trace_t
do_trace (hull_t *hull, vec3_t start, vec3_t end)
{
trace_t trace;
@ -118,96 +212,97 @@ do_trace (hull_t *hull, vec3_t start, vec3_t end)
trace.isbox = true;
VectorCopy (end, trace.endpos);
MOD_TraceLine (hull, 0, start, end, &trace);
printf ("(%g %g %g) (%g %g %g) -> %3g %d %d %d %d\n",
start[0], start[1], start[2],
trace.endpos[0], trace.endpos[1], trace.endpos[2], trace.fraction,
trace.allsolid, trace.startsolid, trace.inopen, trace.inwater);
return trace;
}
static int
run_test (test_t *test)
{
const char *desc;
vec3_t end;
int res = 0;
VectorSubtract (test->end, test->start, end);
VectorMultAdd (test->start, test->expect.frac, end, end);
if (verbose)
printf ("(%g %g %g) -> (%g %g %g) => (%g %g %g) %3g %d %d %d %d\n",
test->start[0], test->start[1], test->start[2],
test->end[0], test->end[1], test->end[2],
end[0], end[1], end[2],
test->expect.frac,
test->expect.allsolid, test->expect.startsolid,
test->expect.inopen, test->expect.inwater);
trace_t trace = do_trace (test->hull, test->start, test->end);
if (verbose)
printf ("(%g %g %g) -> (%g %g %g) => (%g %g %g) %3g %d %d %d %d\n",
test->start[0], test->start[1], test->start[2],
test->end[0], test->end[1], test->end[2],
trace.endpos[0], trace.endpos[1], trace.endpos[2],
trace.fraction,
trace.allsolid, trace.startsolid,
trace.inopen, trace.inwater);
if (VectorCompare (end, trace.endpos)
&& test->expect.frac == trace.fraction
&& test->expect.allsolid == trace.allsolid
&& test->expect.startsolid == trace.startsolid
&& test->expect.inopen == trace.inopen
&& test->expect.inwater == trace.inwater)
res = 1;
if (test->desc)
desc = va ("(%d) %s", (int)(long)(test - tests), test->desc);
else
desc = va ("test #%d", (int)(long)(test - tests));
printf ("%s: %s\n", desc, res ? "PASS" : "FAIL");
return res;
}
int
main (int argc, char **argv)
{
vec3_t start, end;
int i;
#if 1
puts ("\nexpect 0.496094, 1 (0 0 1 0), (0 0 1 0)");
for (i = 0; i < 2; i++) {
start[0] = 0;
start[1] = 47 + i;
start[2] = 40;
VectorCopy (start, end);
end[2] -= 8;
do_trace (&hull1, start, end);
int c, i;
int pass = 1;
int test = -1;
while ((c = getopt (argc, argv, "vt:")) != EOF) {
switch (c) {
case 'v':
verbose = 1;
break;
case 't':
test = atoi (optarg);
break;
default:
fprintf (stderr,
"-v (verbose) and/or -t TEST (test number)\n");
return 1;
}
}
puts ("\nexpect 0.496094, 1 (0 0 1 0), (0 0 1 0)");
for (i = 0; i < 2; i++) {
start[0] = 0;
start[1] = 52;
start[2] = 35 + i;
VectorCopy (start, end);
end[1] -= 8;
do_trace (&hull1, start, end);
if (test == -1) {
for (i = 0; i < num_tests; i++)
pass &= run_test (&tests[i]);
} else if (test >= 0 && test < num_tests) {
pass &= run_test (&tests[test]);
} else {
fprintf (stderr, "Bad test number (0 - %d)\n", num_tests);
return 1;
}
puts ("\nexpect 0.484375, 1 (0 0 1 0), (1 1 0 0)");
for (i = 0; i < 2; i++) {
start[0] = 47 + i;
start[1] = -32;
start[2] = 36;
for (i = 0; i < 0; i++) {
start[0] = 480;
start[1] = -240 + i;
start[2] = 77;
VectorCopy (start, end);
end[0] += 2;
do_trace (&hull1, start, end);
end[2] -= 16;
do_trace (&hull3, start, end);
}
puts ("\nexpect 0.496094, 1 (0 0 1 0), (0 0 1 0)");
for (i = 0; i < 2; i++) {
start[0] = 44;
start[1] = -32;
start[2] = 59 + i;
VectorCopy (start, end);
end[0] += 8;
do_trace (&hull1, start, end);
}
puts ("\nexpect 1, 0.499023 (0 1 1 0), (0 1 1 0)");
for (i = 0; i < 2; i++) {
start[0] = 47 + i;
start[1] = -32;
start[2] = 76;
VectorCopy (start, end);
end[2] -= 32;
do_trace (&hull1, start, end);
}
#endif
puts ("\nexpect 1, 0.499023 (0 1 1 0), (0 1 1 0)");
// for (i = 0; i < 30; i++) {
i = 5;
start[0] = 891 + i;
start[1] = 2048;
start[2] = -144;
VectorCopy (start, end);
end[2] -= 8;
do_trace (&hull2, start, end);
// }
#if 1
puts ("\nexpect 0 (0 0 1 0)");
start[0] = 96;
start[1] = -47.9612;
start[2] = 56.0312 + 4;
end[0] = 96;
end[1] = -47.1025;
end[2] = 55.8737 + 4;
do_trace (&hull1, start, end);
puts ("\nexpect 0 (0 0 1 0)");
start[0] = 94.8916;
start[1] = -34.8506;
start[2] = 56.0312 + 4;
end[0] = 94.8146;
end[1] = -28.5696;
end[2] = 55.5683 + 4;
do_trace (&hull1, start, end);
#endif
return 0;
// start[0] = 480;
// start[1] = -207.253967;
// start[2] = 76.03125;
// VectorCopy (start, end);
// end[1] += 16;
// do_trace (&hull3, start, end);
return !pass;
}

View file

@ -48,8 +48,6 @@ static __attribute__ ((used)) const char rcsid[] =
#include "world.h"
/* LINE TESTING IN HULLS */
//#undef DIST_EPSILON
//#define DIST_EPSILON 0
static inline void
calc_impact (trace_t *trace, const vec3_t start, const vec3_t end,
@ -86,37 +84,67 @@ calc_impact (trace_t *trace, const vec3_t start, const vec3_t end,
VectorMultAdd (start, frac, dist, trace->endpos);
}
#if 0
typedef struct {
mplane_t *plane;
int side;
} tp_t;
typedef struct {
const vec_t *start;
const vec_t *end;
const vec_t *extents;
qboolean isbox;
hull_t hull;
mplane_t *plane;
mplane_t *impact;
int impactside;
tp_t split;
tp_t impact;
float fraction;
int flags;
} tl_t;
static inline void
set_split (tl_t *tl, mplane_t *plane, int side, tp_t *s)
{
if (s)
*s = tl->split;
tl->split.plane = plane;
tl->split.side = side;
}
static inline void
restore_split (tl_t *tl, tp_t *s)
{
tl->split = *s;
}
static inline void
set_impact (tl_t *tl, mplane_t *plane, int side)
{
tl->impact.plane = plane;
tl->impact.side = side;
}
static inline vec_t
sgn (vec_t v)
{
return v < 0 ? -1 : (v > 0 ? 1 : 0);
}
static inline vec_t
select_point (mplane_t *split, int side, int axis, tl_t *tl)
static inline void
select_point (tl_t *tl, tp_t *impact, tp_t *split, vec3_t offs)
{
vec_t s = sgn (tl->impact->normal[axis]);
if (!tl->impactside)
int axis;
for (axis = 0; axis < 3; axis++) {
vec_t s = sgn (impact->plane->normal[axis]);
if (!impact->side)
s = -s;
if (!s) {
s = sgn (split->normal[axis]);
if (!side)
s = sgn (split->plane->normal[axis]);
if (!split->side)
s = -s;
}
return tl->extents[axis] * s;
offs[axis] = tl->extents[axis] * s;
}
}
static inline void
@ -154,9 +182,9 @@ impact (tl_t *tl)
float t1, t2, offset, frac;
int side = 0;
t1 = PlaneDiff (tl->start, tl->plane);
t2 = PlaneDiff (tl->end, tl->plane);
offset = calc_offset (tl, tl->plane);
t1 = PlaneDiff (tl->start, tl->split.plane);
t2 = PlaneDiff (tl->end, tl->split.plane);
offset = calc_offset (tl, tl->split.plane);
if (t1 < t2) {
side = -1;
frac = (t1 + offset) / (t1 - t2);
@ -169,27 +197,57 @@ impact (tl_t *tl)
}
if (frac >= 0) {
tl->fraction = frac;
tl->impact = tl->plane;
tl->impactside = side;
set_impact (tl, tl->split.plane, side);
}
}
static void
validate_impact (mplane_t *split, int side, tl_t *tl)
static int
validate_impact (tp_t *split, tl_t *tl)
{
vec3_t dist, point;
vec3_t dist, point, offs;
int pside;
VectorSubtract (tl->end, tl->start, dist);
VectorMultAdd (tl->start, tl->fraction, dist, point);
point[0] += select_point (split, !side, 0, tl);
point[1] += select_point (split, !side, 1, tl);
point[2] += select_point (split, !side, 2, tl);
pside = PlaneDiff (point, split) < 0;
if (pside != side) {
select_point (tl, &tl->impact, split, offs);
VectorAdd (point, offs, point);
pside = PlaneDiff (point, split->plane) < 0;
// Sys_Printf ("impact: (%g %g %g) %d %d\n", point[0], point[1], point[2], pside, split->side);
if (pside != split->side) {
tl->fraction = 1;
tl->impact = 0;
set_impact (tl, 0, 0);
return 0;
}
return 1;
}
static int
validate_solid (const vec3_t p1, const vec3_t p2, float t1, float t2,
tp_t *wall, tp_t *split, tl_t *tl)
{
float frac, offset;
int side;
vec3_t dist, point, offs;
// if (!tl->plane)
// return 1;
if (t1 == t2)
return 1;
offset = calc_offset (tl, wall->plane);
if (t1 < t2)
frac = (t1 - offset) / (t1 - t2);
else
frac = (t1 + offset) / (t1 - t2);
VectorSubtract (p2, p1, dist);
VectorMultAdd (p1, frac, dist, point);
select_point (tl, wall, split, offs);
VectorAdd (point, offs, point);
side = PlaneDiff (point, split->plane) < 0;
// Sys_Printf ("solid: (%g %g %g) %d %d\n", point[0], point[1], point[2], side, split->side);
if (side != split->side) {
return 0;
}
return 1;
}
static int
@ -202,6 +260,7 @@ traceline (int num, float p1f, float p2f, const vec3_t p1, const vec3_t p2,
int side;
vec3_t mid;
int c1, c2;
tp_t split;
// Skip past nodes that don't intersect with the line.
do {
@ -229,17 +288,30 @@ traceline (int num, float p1f, float p2f, const vec3_t p1, const vec3_t p2,
// if (t1 == t2) {
if (t1 >= -offset && t1 < offset && t2 >= -offset && t2 < offset) {
set_split (tl, plane, 0, &split);
c1 = c2 = traceline (node->children[0], p1f, p2f, p1, p2, tl);
if (tl->impact)
validate_impact (plane, 0, tl);
if (c1 != CONTENTS_SOLID)
if (c1 == CONTENTS_SOLID) {
if (!validate_solid (p1, p2, t1, t2, &tl->split, &split, tl))
c1 = CONTENTS_EMPTY;
} else {
if (tl->impact.plane)
validate_impact (&tl->split, tl);
}
if (c1 != CONTENTS_SOLID) {
set_split (tl, plane, 1, 0);
c2 = traceline (node->children[1], p1f, p2f, p1, p2, tl);
if (c2 == CONTENTS_SOLID && !validate_solid (p1, p2, t1, t2,
&tl->split, &split,
tl))
c2 = CONTENTS_EMPTY;
}
restore_split (tl, &split);
if (c1 == CONTENTS_SOLID || c2 == CONTENTS_SOLID) {
if (!p1f) {
tl->flags &= 3;
tl->flags |= 2;
}
if (tl->plane)
if (tl->split.plane)
impact (tl);
return CONTENTS_SOLID;
}
@ -270,15 +342,16 @@ traceline (int num, float p1f, float p2f, const vec3_t p1, const vec3_t p2,
frac2 = bound (0, frac2, 1);
midf = p1f + (p2f - p1f) * frac2;
if (!tl->impact || midf <= tl->fraction) {
set_split (tl, plane, side ^ 1, &split);
if (!tl->impact.plane || midf <= tl->fraction) {
VectorSubtract (p2, p1, mid);
VectorMultAdd (p1, frac2, mid, mid);
tl->plane = plane;
c2 = traceline (node->children[side ^ 1], midf, p2f, mid, p2, tl);
}
tl->plane = plane;
if (c1 != CONTENTS_SOLID && c2 == CONTENTS_SOLID)
impact (tl);
restore_split (tl, &split);
if (c1 == CONTENTS_SOLID && !(tl->flags & 0xc))
tl->flags |= 2;
if (c1 == CONTENTS_EMPTY || c2 == CONTENTS_EMPTY) {
@ -306,7 +379,8 @@ MOD_TraceLine (hull_t *hull, int num,
tl.end = end_point;
tl.hull = *hull;
tl.fraction = 1;
tl.plane = tl.impact = 0;
set_split (&tl, 0, 0, 0);
set_impact (&tl, 0, 0);
tl.flags = 1;
tl.isbox = trace->isbox;
tl.extents = trace->extents;
@ -320,7 +394,7 @@ MOD_TraceLine (hull_t *hull, int num,
tl.flags |= 8;
}
if (tl.fraction < 1) {
calc_impact (trace, start_point, end_point, tl.impact);
calc_impact (trace, start_point, end_point, tl.impact.plane);
}
trace->allsolid = (tl.flags & 1) != 0;
trace->startsolid = (tl.flags & 2) != 0;