provide a tighter accuracy (but slower) _VectorNormalize for the tools

This commit is contained in:
Bill Currie 2002-09-25 21:35:49 +00:00
parent 43d8cf9640
commit d25ee82838
14 changed files with 45 additions and 24 deletions

View file

@ -95,6 +95,7 @@ void _VectorScale (const vec3_t in, vec_t scale, vec3_t out);
void _VectorSubtract (const vec3_t veca, const vec3_t vecb, vec3_t out); void _VectorSubtract (const vec3_t veca, const vec3_t vecb, vec3_t out);
void CrossProduct (const vec3_t v1, const vec3_t v2, vec3_t cross); void CrossProduct (const vec3_t v1, const vec3_t v2, vec3_t cross);
float VectorNormalize (vec3_t v); // returns vector length float VectorNormalize (vec3_t v); // returns vector length
vec_t _VectorNormalize (vec3_t v); // returns vector length
void VectorInverse (vec3_t v); void VectorInverse (vec3_t v);
int Q_log2(int val); int Q_log2(int val);

View file

@ -425,6 +425,25 @@ VectorNormalize (vec3_t v)
return length; return length;
} }
vec_t
_VectorNormalize (vec3_t v)
{
int i;
double length;
length = 0;
for (i=0 ; i < 3; i++)
length += v[i] * v[i];
length = sqrt (length);
if (length == 0)
return 0;
for (i=0 ; i <3; i++)
v[i] /= length;
return length;
}
void void
VectorInverse (vec3_t v) VectorInverse (vec3_t v)
{ {

View file

@ -86,7 +86,7 @@ CheckFace (face_t *f)
Sys_Error ("CheckFace: degenerate edge"); Sys_Error ("CheckFace: degenerate edge");
CrossProduct (facenormal, dir, edgenormal); CrossProduct (facenormal, dir, edgenormal);
VectorNormalize (edgenormal); _VectorNormalize (edgenormal);
edgedist = DotProduct (p1, edgenormal); edgedist = DotProduct (p1, edgenormal);
edgedist += ON_EPSILON; edgedist += ON_EPSILON;
@ -569,7 +569,7 @@ AddHullEdge (vec3_t p1, vec3_t p2, int hullnum)
num_hull_edges++; num_hull_edges++;
VectorSubtract (p1, p2, edgevec); VectorSubtract (p1, p2, edgevec);
VectorNormalize (edgevec); _VectorNormalize (edgevec);
for (a = 0; a < 3; a++) { for (a = 0; a < 3; a++) {
b = (a + 1) % 3; b = (a + 1) % 3;

View file

@ -391,7 +391,8 @@ CSGFaces (brushset_t *bs)
// these faces are continued in another brush, so get rid of them // these faces are continued in another brush, so get rid of them
if (b1->contents == CONTENTS_SOLID if (b1->contents == CONTENTS_SOLID
&& b2->contents <= CONTENTS_WATER) FreeInside (b2->contents); && b2->contents <= CONTENTS_WATER)
FreeInside (b2->contents);
else else
FreeInside (CONTENTS_SOLID); FreeInside (CONTENTS_SOLID);
} }

View file

@ -323,7 +323,7 @@ ParseBrush (void)
free (f); free (f);
break; break;
} }
VectorNormalize (f->plane.normal); _VectorNormalize (f->plane.normal);
f->plane.dist = DotProduct (t3, f->plane.normal); f->plane.dist = DotProduct (t3, f->plane.normal);
// fake proper texture vectors from QuakeEd style // fake proper texture vectors from QuakeEd style

View file

@ -42,11 +42,11 @@ CheckColinear (face_t *f)
// the vector to the next point // the vector to the next point
j = (i - 1 < 0) ? f->numpoints - 1 : i - 1; j = (i - 1 < 0) ? f->numpoints - 1 : i - 1;
VectorSubtract (f->pts[i], f->pts[j], v1); VectorSubtract (f->pts[i], f->pts[j], v1);
VectorNormalize (v1); _VectorNormalize (v1);
j = (i + 1 == f->numpoints) ? 0 : i + 1; j = (i + 1 == f->numpoints) ? 0 : i + 1;
VectorSubtract (f->pts[j], f->pts[i], v2); VectorSubtract (f->pts[j], f->pts[i], v2);
VectorNormalize (v2); _VectorNormalize (v2);
if (_VectorCompare (v1, v2)) if (_VectorCompare (v1, v2))
Sys_Error ("Colinear edge"); Sys_Error ("Colinear edge");
@ -121,7 +121,7 @@ TryMerge (face_t *f1, face_t *f2)
back = f1->pts[(i + f1->numpoints - 1) % f1->numpoints]; back = f1->pts[(i + f1->numpoints - 1) % f1->numpoints];
VectorSubtract (p1, back, delta); VectorSubtract (p1, back, delta);
CrossProduct (planenormal, delta, normal); CrossProduct (planenormal, delta, normal);
VectorNormalize (normal); _VectorNormalize (normal);
back = f2->pts[(j + 2) % f2->numpoints]; back = f2->pts[(j + 2) % f2->numpoints];
VectorSubtract (back, p1, delta); VectorSubtract (back, p1, delta);
@ -133,7 +133,7 @@ TryMerge (face_t *f1, face_t *f2)
back = f1->pts[(i + 2) % f1->numpoints]; back = f1->pts[(i + 2) % f1->numpoints];
VectorSubtract (back, p2, delta); VectorSubtract (back, p2, delta);
CrossProduct (planenormal, delta, normal); CrossProduct (planenormal, delta, normal);
VectorNormalize (normal); _VectorNormalize (normal);
back = f2->pts[(j + f2->numpoints - 1) % f2->numpoints]; back = f2->pts[(j + f2->numpoints - 1) % f2->numpoints];
VectorSubtract (back, p2, delta); VectorSubtract (back, p2, delta);

View file

@ -93,7 +93,7 @@ MarkLeakTrail (portal_t *n2)
VectorSubtract (p2, p1, dir); VectorSubtract (p2, p1, dir);
len = VectorLength (dir); len = VectorLength (dir);
VectorNormalize (dir); _VectorNormalize (dir);
while (len > 2) { while (len > 2) {
fprintf (leakfile, "%f %f %f\n", p1[0], p1[1], p1[2]); fprintf (leakfile, "%f %f %f\n", p1[0], p1[1], p1[2]);

View file

@ -200,7 +200,7 @@ PlaneFromWinding (winding_t *w, plane_t *plane)
VectorSubtract (w->points[2], w->points[1], v1); VectorSubtract (w->points[2], w->points[1], v1);
VectorSubtract (w->points[0], w->points[1], v2); VectorSubtract (w->points[0], w->points[1], v2);
CrossProduct (v2, v1, plane->normal); CrossProduct (v2, v1, plane->normal);
VectorNormalize (plane->normal); _VectorNormalize (plane->normal);
plane->dist = DotProduct (w->points[0], plane->normal); plane->dist = DotProduct (w->points[0], plane->normal);
} }

View file

@ -104,7 +104,7 @@ BaseWindingForPlane (plane_t *p)
v = DotProduct (vup, p->normal); v = DotProduct (vup, p->normal);
VectorMA (vup, -v, p->normal, vup); VectorMA (vup, -v, p->normal, vup);
VectorNormalize (vup); _VectorNormalize (vup);
VectorScale (p->normal, p->dist, org); VectorScale (p->normal, p->dist, org);

View file

@ -246,7 +246,7 @@ HealEdges (int e1, int e2)
ed2 = &bsp->edges[e2]; ed2 = &bsp->edges[e2];
VectorSubtract (bsp->vertexes[ed->v[1]].point, VectorSubtract (bsp->vertexes[ed->v[1]].point,
bsp->vertexes[ed->v[0]].point, v1); bsp->vertexes[ed->v[0]].point, v1);
VectorNormalize (v1); _VectorNormalize (v1);
if (ed->v[0] == ed2->v[0]) if (ed->v[0] == ed2->v[0])
ed->v[0] = ed2->v[1]; ed->v[0] = ed2->v[1];
@ -261,7 +261,7 @@ HealEdges (int e1, int e2)
VectorSubtract (bsp->vertexes[ed->v[1]].point, VectorSubtract (bsp->vertexes[ed->v[1]].point,
bsp->vertexes[ed->v[0]].point, v2); bsp->vertexes[ed->v[0]].point, v2);
VectorNormalize (v2); _VectorNormalize (v2);
if (!_VectorCompare (v1, v2)) if (!_VectorCompare (v1, v2))
Sys_Error ("HealEdges: edges not colinear"); Sys_Error ("HealEdges: edges not colinear");

View file

@ -87,7 +87,7 @@ SubdivideFace (face_t *f, face_t **prevptr)
VectorCopy (tex->vecs[axis], plane.normal); VectorCopy (tex->vecs[axis], plane.normal);
v = VectorLength (plane.normal); v = VectorLength (plane.normal);
VectorNormalize (plane.normal); _VectorNormalize (plane.normal);
plane.dist = (mins + options.subdivide_size - 16) / v; plane.dist = (mins + options.subdivide_size - 16) / v;
next = f->next; next = f->next;
SplitFace (f, &plane, &front, &back); SplitFace (f, &plane, &front, &back);

View file

@ -110,7 +110,7 @@ HashVec (vec3_t vec)
void void
CanonicalVector (vec3_t vec) CanonicalVector (vec3_t vec)
{ {
VectorNormalize (vec); _VectorNormalize (vec);
if (vec[0] > EQUAL_EPSILON) if (vec[0] > EQUAL_EPSILON)
return; return;
else if (vec[0] < -EQUAL_EPSILON) { else if (vec[0] < -EQUAL_EPSILON) {
@ -285,10 +285,10 @@ SplitFaceForTjunc (face_t *f, face_t *original)
restart: restart:
// find the last corner // find the last corner
VectorSubtract (f->pts[f->numpoints - 1], f->pts[0], dir); VectorSubtract (f->pts[f->numpoints - 1], f->pts[0], dir);
VectorNormalize (dir); _VectorNormalize (dir);
for (lastcorner = f->numpoints - 1; lastcorner > 0; lastcorner--) { for (lastcorner = f->numpoints - 1; lastcorner > 0; lastcorner--) {
VectorSubtract (f->pts[lastcorner - 1], f->pts[lastcorner], test); VectorSubtract (f->pts[lastcorner - 1], f->pts[lastcorner], test);
VectorNormalize (test); _VectorNormalize (test);
v = DotProduct (test, dir); v = DotProduct (test, dir);
if (v < 0.9999 || v > 1.00001) if (v < 0.9999 || v > 1.00001)
break; break;
@ -296,11 +296,11 @@ SplitFaceForTjunc (face_t *f, face_t *original)
// find the first corner // find the first corner
VectorSubtract (f->pts[1], f->pts[0], dir); VectorSubtract (f->pts[1], f->pts[0], dir);
VectorNormalize (dir); _VectorNormalize (dir);
for (firstcorner = 1; firstcorner < f->numpoints - 1; firstcorner++) { for (firstcorner = 1; firstcorner < f->numpoints - 1; firstcorner++) {
VectorSubtract (f->pts[firstcorner + 1], f->pts[firstcorner], VectorSubtract (f->pts[firstcorner + 1], f->pts[firstcorner],
test); test);
VectorNormalize (test); _VectorNormalize (test);
v = DotProduct (test, dir); v = DotProduct (test, dir);
if (v < 0.9999 || v > 1.00001) if (v < 0.9999 || v > 1.00001)
break; break;

View file

@ -157,7 +157,7 @@ CalcFaceVectors (lightinfo_t *l)
tex->vecs[1][0] * tex->vecs[0][2]; tex->vecs[1][0] * tex->vecs[0][2];
texnormal[2] = tex->vecs[1][0] * tex->vecs[0][1] - texnormal[2] = tex->vecs[1][0] * tex->vecs[0][1] -
tex->vecs[1][1] * tex->vecs[0][0]; tex->vecs[1][1] * tex->vecs[0][0];
VectorNormalize (texnormal); _VectorNormalize (texnormal);
// flip it towards plane normal // flip it towards plane normal
distscale = DotProduct (texnormal, l->facenormal); distscale = DotProduct (texnormal, l->facenormal);
@ -328,7 +328,7 @@ CalcPoints (lightinfo_t *l)
// move surf 8 pixels towards the center // move surf 8 pixels towards the center
VectorSubtract (facemid, surf, move); VectorSubtract (facemid, surf, move);
VectorNormalize (move); _VectorNormalize (move);
VectorMA (surf, 8, move, surf); VectorMA (surf, 8, move, surf);
} }
if (i == 2) if (i == 2)
@ -361,7 +361,7 @@ SingleLightFace (entity_t *light, lightinfo_t *l)
if (light->targetent) { if (light->targetent) {
VectorSubtract (light->targetent->origin, light->origin, spotvec); VectorSubtract (light->targetent->origin, light->origin, spotvec);
VectorNormalize (spotvec); _VectorNormalize (spotvec);
if (!light->angle) if (!light->angle)
falloff = -cos (20 * M_PI / 180); falloff = -cos (20 * M_PI / 180);
else else
@ -395,7 +395,7 @@ SingleLightFace (entity_t *light, lightinfo_t *l)
continue; // light doesn't reach continue; // light doesn't reach
VectorSubtract (light->origin, surf, incoming); VectorSubtract (light->origin, surf, incoming);
VectorNormalize (incoming); _VectorNormalize (incoming);
angle = DotProduct (incoming, l->facenormal); angle = DotProduct (incoming, l->facenormal);
if (light->targetent) { if (light->targetent) {
// spotlight cutoff // spotlight cutoff

View file

@ -101,7 +101,7 @@ PlaneFromWinding (winding_t *winding, plane_t *plane)
VectorSubtract (winding->points[2], winding->points[1], v1); VectorSubtract (winding->points[2], winding->points[1], v1);
VectorSubtract (winding->points[0], winding->points[1], v2); VectorSubtract (winding->points[0], winding->points[1], v2);
CrossProduct (v2, v1, plane->normal); CrossProduct (v2, v1, plane->normal);
VectorNormalize (plane->normal); _VectorNormalize (plane->normal);
plane->dist = DotProduct (winding->points[0], plane->normal); plane->dist = DotProduct (winding->points[0], plane->normal);
} }