File: navigation_bsp_utils.cpp

package info (click to toggle)
openmohaa 0.82.1%2Bdfsg-1
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid
  • size: 34,192 kB
  • sloc: cpp: 315,720; ansic: 275,789; sh: 312; xml: 246; asm: 141; makefile: 7
file content (336 lines) | stat: -rw-r--r-- 8,119 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/*
===========================================================================
Copyright (C) 2025 the OpenMoHAA team

This file is part of OpenMoHAA source code.

OpenMoHAA source code 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.

OpenMoHAA source code 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.

You should have received a copy of the GNU General Public License
along with OpenMoHAA source code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
===========================================================================
*/

/**
 * @file navigation_bsp_utils.cpp
 * @brief Utility functions to create windings from brushes.
 * 
 */

#include "g_local.h"
#include "navigation_bsp.h"
#include "../qcommon/qfiles.h"
#include "../qcommon/container.h"
#include "../qcommon/vector.h"
#include "../qcommon/cm_polylib.h"
#include "../script/scriptexception.h"

/*
=============
AllocWinding
=============
*/
winding_t *AllocWinding(int points)
{
    winding_t *w;
    int        s;

    s = sizeof(vec_t) * 3 * points + sizeof(int);
    w = (winding_t *)gi.Malloc(s);
    Com_Memset(w, 0, s);
    return w;
}

void FreeWinding(winding_t *w)
{
    if (*(unsigned *)w == 0xdeaddead) {
        Com_Error(ERR_FATAL, "FreeWinding: freed a freed winding");
    }
    *(unsigned *)w = 0xdeaddead;

    gi.Free(w);
}

/*
=================
BaseWindingForPlane
=================
*/
winding_t *BaseWindingForPlane(vec3_t normal, vec_t dist)
{
    int        i, x;
    vec_t      max, v;
    vec3_t     org, vright, vup;
    winding_t *w;

    // find the major axis

    max = -MAP_SIZE;
    x   = -1;
    for (i = 0; i < 3; i++) {
        v = fabs(normal[i]);
        if (v > max) {
            x   = i;
            max = v;
        }
    }
    if (x == -1) {
        Com_Error(ERR_DROP, "BaseWindingForPlane: no axis found");
    }

    VectorCopy(vec3_origin, vup);
    switch (x) {
    case 0:
    case 1:
        vup[2] = 1;
        break;
    case 2:
        vup[0] = 1;
        break;
    }

    v = DotProduct(vup, normal);
    VectorMA(vup, -v, normal, vup);
    VectorNormalize(vup);

    VectorScale(normal, dist, org);

    CrossProduct(vup, normal, vright);

    VectorScale(vup, MAP_SIZE, vup);
    VectorScale(vright, MAP_SIZE, vright);

    // project a really big	axis aligned box onto the plane
    w = AllocWinding(4);

    VectorSubtract(org, vright, w->p[0]);
    VectorAdd(w->p[0], vup, w->p[0]);

    VectorAdd(org, vright, w->p[1]);
    VectorAdd(w->p[1], vup, w->p[1]);

    VectorAdd(org, vright, w->p[2]);
    VectorSubtract(w->p[2], vup, w->p[2]);

    VectorSubtract(org, vright, w->p[3]);
    VectorSubtract(w->p[3], vup, w->p[3]);

    w->numpoints = 4;

    return w;
}

/*
=============
ChopWindingInPlace
=============
*/
void ChopWindingInPlace(winding_t **inout, vec3_t normal, vec_t dist, vec_t epsilon)
{
    winding_t   *in;
    vec_t        dists[MAX_POINTS_ON_WINDING + 4];
    int          sides[MAX_POINTS_ON_WINDING + 4];
    int          counts[3];
    static vec_t dot; // VC 4.2 optimizer bug if not static
    int          i, j;
    vec_t       *p1, *p2;
    vec3_t       mid;
    winding_t   *f;
    int          maxpts;

    in        = *inout;
    counts[0] = counts[1] = counts[2] = 0;

    // determine sides for each point
    for (i = 0; i < in->numpoints; i++) {
        dot = DotProduct(in->p[i], normal);
        dot -= dist;
        dists[i] = dot;
        if (dot > epsilon) {
            sides[i] = SIDE_FRONT;
        } else if (dot < -epsilon) {
            sides[i] = SIDE_BACK;
        } else {
            sides[i] = SIDE_ON;
        }
        counts[sides[i]]++;
    }
    sides[i] = sides[0];
    dists[i] = dists[0];

    if (!counts[0]) {
        FreeWinding(in);
        *inout = NULL;
        return;
    }
    if (!counts[1]) {
        return; // inout stays the same
    }

    maxpts = in->numpoints + 4; // cant use counts[0]+2 because
    // of fp grouping errors

    f = AllocWinding(maxpts);

    for (i = 0; i < in->numpoints; i++) {
        p1 = in->p[i];

        if (sides[i] == SIDE_ON) {
            VectorCopy(p1, f->p[f->numpoints]);
            f->numpoints++;
            continue;
        }

        if (sides[i] == SIDE_FRONT) {
            VectorCopy(p1, f->p[f->numpoints]);
            f->numpoints++;
        }

        if (sides[i + 1] == SIDE_ON || sides[i + 1] == sides[i]) {
            continue;
        }

        // generate a split point
        p2 = in->p[(i + 1) % in->numpoints];

        dot = dists[i] / (dists[i] - dists[i + 1]);
        for (j = 0; j < 3; j++) { // avoid round off error when possible
            if (normal[j] == 1) {
                mid[j] = dist;
            } else if (normal[j] == -1) {
                mid[j] = -dist;
            } else {
                mid[j] = p1[j] + dot * (p2[j] - p1[j]);
            }
        }

        VectorCopy(mid, f->p[f->numpoints]);
        f->numpoints++;
    }

    if (f->numpoints > maxpts) {
        Com_Error(ERR_DROP, "ClipWinding: points exceeded estimate");
    }
    if (f->numpoints > MAX_POINTS_ON_WINDING) {
        Com_Error(ERR_DROP, "ClipWinding: MAX_POINTS_ON_WINDING");
    }

    FreeWinding(in);
    *inout = f;
}

#define SNAP_EPSILON 0.01

void SnapWeldVector(vec3_t a, vec3_t b, vec3_t out)
{
    int   i;
    vec_t ai, bi, outi;

    /* dummy check */
    if (a == NULL || b == NULL || out == NULL) {
        return;
    }

    /* do each element */
    for (i = 0; i < 3; i++) {
        /* round to integer */
        ai = Q_rint(a[i]);
        bi = Q_rint(a[i]);

        /* prefer exact integer */
        if (ai == a[i]) {
            out[i] = a[i];
        } else if (bi == b[i]) {
            out[i] = b[i];
        }

        /* use nearest */
        else if (fabs(ai - a[i]) < fabs(bi < b[i])) {
            out[i] = a[i];
        } else {
            out[i] = b[i];
        }

        /* snap */
        outi = Q_rint(out[i]);
        if (fabs(outi - out[i]) <= SNAP_EPSILON) {
            out[i] = outi;
        }
    }
}

#define DEGENERATE_EPSILON 0.1

qboolean FixWinding(winding_t *w)
{
    qboolean valid = qtrue;
    int      i, j, k;
    vec3_t   vec;
    float    dist;

    /* dummy check */
    if (!w) {
        return qfalse;
    }

    /* check all verts */
    for (i = 0; i < w->numpoints; i++) {
        /* don't remove points if winding is a triangle */
        if (w->numpoints == 3) {
            return valid;
        }

        /* get second point index */
        j = (i + 1) % w->numpoints;

        /* degenerate edge? */
        VectorSubtract(w->p[i], w->p[j], vec);
        dist = VectorLength(vec);
        if (dist < DEGENERATE_EPSILON) {
            valid = qfalse;
            //Sys_FPrintf( SYS_VRB, "WARNING: Degenerate winding edge found, fixing...\n" );

            /* create an average point (ydnar 2002-01-26: using nearest-integer weld preference) */
            SnapWeldVector(w->p[i], w->p[j], vec);
            VectorCopy(vec, w->p[i]);
            //VectorAdd( w->p[ i ], w->p[ j ], vec );
            //VectorScale( vec, 0.5, w->p[ i ] );

            /* move the remaining verts */
            for (k = i + 2; k < w->numpoints; k++) {
                VectorCopy(w->p[k], w->p[k - 1]);
            }
            w->numpoints--;
        }
    }

    /* one last check and return */
    if (w->numpoints < 3) {
        valid = qfalse;
    }
    return valid;
}

qboolean G_PlaneFromPoints(vec4_t plane, vec3_t a, vec3_t b, vec3_t c)
{
    vec3_t d1, d2;

    VectorSubtract(b, a, d1);
    VectorSubtract(c, a, d2);
    CrossProduct(d2, d1, plane);
    if (VectorNormalize(plane) == 0) {
        return qfalse;
    }

    plane[3] = DotProduct(a, plane);
    return qtrue;
}