~ubuntu-branches/debian/stretch/assaultcube-data/stretch

« back to all changes in this revision

Viewing changes to source/src/geom.h

  • Committer: Bazaar Package Importer
  • Author(s): Gonéri Le Bouder, Ansgar Burchardt, Gonéri Le Bouder
  • Date: 2010-04-02 23:37:55 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20100402233755-kf74fxwlu634o6vg
Tags: 1.0.4+repack1-1
[ Ansgar Burchardt ]
* debian/control: fix typo in short description

[ Gonéri Le Bouder ]
* Upgrade to 1.0.4
* bump standards-version to 3.8.4
* Add Depends: ${misc:Depends} just to avoid a lintian warning
* Add a debian/source/format file for the same reason

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
struct vec
 
2
{
 
3
    union
 
4
    {
 
5
        struct { float x, y, z; };
 
6
        float v[3];
 
7
        int i[3];
 
8
    };
 
9
 
 
10
    vec() {}
 
11
    vec(float a, float b, float c) : x(a), y(b), z(c) {}
 
12
    vec(float *v) : x(v[0]), y(v[1]), z(v[2]) {}
 
13
 
 
14
    float &operator[](int i)       { return v[i]; }
 
15
    float  operator[](int i) const { return v[i]; }
 
16
 
 
17
    bool iszero() const { return x==0 && y==0 && z==0; }
 
18
 
 
19
    bool operator==(const vec &o) const { return x == o.x && y == o.y && z == o.z; }
 
20
    bool operator!=(const vec &o) const { return x != o.x || y != o.y || z != o.z; }
 
21
    vec operator-() const { return vec(-x, -y, -z); }
 
22
 
 
23
    vec &mul(float f) { x *= f; y *= f; z *= f; return *this; }
 
24
    vec &div(float f) { x /= f; y /= f; z /= f; return *this; }
 
25
    vec &add(float f) { x += f; y += f; z += f; return *this; }
 
26
    vec &sub(float f) { x -= f; y -= f; z -= f; return *this; }
 
27
 
 
28
    vec &add(const vec &o) { x += o.x; y += o.y; z += o.z; return *this; }
 
29
    vec &sub(const vec &o) { x -= o.x; y -= o.y; z -= o.z; return *this; }
 
30
 
 
31
    float squaredlen() const { return x*x + y*y + z*z; }
 
32
    float dot(const vec &o) const { return x*o.x + y*o.y + z*o.z; }
 
33
 
 
34
    float magnitude() const { return sqrtf(squaredlen()); }
 
35
    vec &normalize() { div(magnitude()); return *this; }
 
36
 
 
37
    float dist(const vec &e) const { vec t; return dist(e, t); }
 
38
    float dist(const vec &e, vec &t) const { t = *this; t.sub(e); return t.magnitude(); }
 
39
 
 
40
    float distxy(const vec &e) const { float dx = e.x - x, dy = e.y - y; return sqrtf(dx*dx + dy*dy); }
 
41
    float magnitudexy() const { return sqrtf(x*x + y*y); }
 
42
 
 
43
    bool reject(const vec &o, float max) const { return x>o.x+max || x<o.x-max || y>o.y+max || y<o.y-max; }
 
44
 
 
45
    vec &cross(const vec &a, const vec &b) { x = a.y*b.z-a.z*b.y; y = a.z*b.x-a.x*b.z; z = a.x*b.y-a.y*b.x; return *this; }
 
46
 
 
47
    void rotate_around_z(float angle) { *this = vec(cosf(angle)*x-sinf(angle)*y, cosf(angle)*y+sinf(angle)*x, z); }
 
48
    void rotate_around_x(float angle) { *this = vec(x, cosf(angle)*y-sinf(angle)*z, cosf(angle)*z+sinf(angle)*y); }
 
49
    void rotate_around_y(float angle) { *this = vec(cosf(angle)*x-sinf(angle)*z, y, cosf(angle)*z+sinf(angle)*x); }
 
50
 
 
51
    vec &rotate(float angle, const vec &d)
 
52
    {
 
53
        float c = cosf(angle), s = sinf(angle);
 
54
        return rotate(c, s, d);
 
55
    }
 
56
 
 
57
    vec &rotate(float c, float s, const vec &d)
 
58
    {
 
59
        *this = vec(x*(d.x*d.x*(1-c)+c) + y*(d.x*d.y*(1-c)-d.z*s) + z*(d.x*d.z*(1-c)+d.y*s),
 
60
                    x*(d.y*d.x*(1-c)+d.z*s) + y*(d.y*d.y*(1-c)+c) + z*(d.y*d.z*(1-c)-d.x*s),
 
61
                    x*(d.x*d.z*(1-c)-d.y*s) + y*(d.y*d.z*(1-c)+d.x*s) + z*(d.z*d.z*(1-c)+c));
 
62
        return *this;
 
63
    }
 
64
 
 
65
    void orthogonal(const vec &d)
 
66
    {
 
67
        int i = fabs(d.x) > fabs(d.y) ? (fabs(d.x) > fabs(d.z) ? 0 : 2) : (fabs(d.y) > fabs(d.z) ? 1 : 2);
 
68
        v[i] = d[(i+1)%3];
 
69
        v[(i+1)%3] = -d[i];
 
70
        v[(i+2)%3] = 0;
 
71
    }
 
72
};
 
73
 
 
74
static inline bool htcmp(const vec &x, const vec &y)
 
75
{
 
76
    return x == y;
 
77
}
 
78
 
 
79
static inline uint hthash(const vec &k)
 
80
{
 
81
    return k.i[0]^k.i[1]^k.i[2];
 
82
}
 
83
 
 
84
struct vec4
 
85
{
 
86
    union
 
87
    {
 
88
        struct { float x, y, z, w; };
 
89
        float v[4];
 
90
    };
 
91
 
 
92
    vec4() {}
 
93
    explicit vec4(const vec &p, float w = 0) : x(p.x), y(p.y), z(p.z), w(w) {}
 
94
    vec4(float x, float y, float z, float w) : x(x), y(y), z(z), w(w) {}
 
95
 
 
96
    float &operator[](int i)       { return v[i]; }
 
97
    float  operator[](int i) const { return v[i]; }
 
98
};
 
99
 
 
100
struct ivec
 
101
{
 
102
    union
 
103
    {
 
104
        struct { int x, y, z; };
 
105
        int v[3];
 
106
    };
 
107
 
 
108
    ivec() {}
 
109
    ivec(const vec &v) : x(int(v.x)), y(int(v.y)), z(int(v.z)) {}
 
110
    ivec(int a, int b, int c) : x(a), y(b), z(c) {}
 
111
 
 
112
    vec tovec() const { return vec(x, y, z); }
 
113
 
 
114
    int &operator[](int i)       { return v[i]; }
 
115
    int  operator[](int i) const { return v[i]; }
 
116
 
 
117
    bool operator==(const ivec &v) const { return x==v.x && y==v.y && z==v.z; }
 
118
    bool operator!=(const ivec &v) const { return x!=v.x || y!=v.y || z!=v.z; }
 
119
    ivec &mul(int n) { x *= n; y *= n; z *= n; return *this; }
 
120
    ivec &div(int n) { x /= n; y /= n; z /= n; return *this; }
 
121
    ivec &add(int n) { x += n; y += n; z += n; return *this; }
 
122
    ivec &sub(int n) { x -= n; y -= n; z -= n; return *this; }
 
123
    ivec &add(const ivec &v) { x += v.x; y += v.y; z += v.z; return *this; }
 
124
    ivec &sub(const ivec &v) { x -= v.x; y -= v.y; z -= v.z; return *this; }
 
125
    ivec &mask(int n) { x &= n; y &= n; z &= n; return *this; }
 
126
    ivec &cross(const ivec &a, const ivec &b) { x = a.y*b.z-a.z*b.y; y = a.z*b.x-a.x*b.z; z = a.x*b.y-a.y*b.x; return *this; }
 
127
    int dot(const ivec &o) const { return x*o.x + y*o.y + z*o.z; }
 
128
};
 
129
 
 
130
static inline bool htcmp(const ivec &x, const ivec &y)
 
131
{
 
132
    return x == y;
 
133
}
 
134
 
 
135
static inline uint hthash(const ivec &k)
 
136
{
 
137
    return k.x^k.y^k.z;
 
138
}
 
139
 
 
140
struct bvec
 
141
{
 
142
    union
 
143
    {
 
144
        struct { uchar x, y, z; };
 
145
        uchar v[3];
 
146
    };
 
147
 
 
148
    bvec() {}
 
149
    bvec(uchar x, uchar y, uchar z) : x(x), y(y), z(z) {}
 
150
    bvec(const vec &v) : x((uchar)((v.x+1)*255/2)), y((uchar)((v.y+1)*255/2)), z((uchar)((v.z+1)*255/2)) {}
 
151
 
 
152
    uchar &operator[](int i)       { return v[i]; }
 
153
    uchar  operator[](int i) const { return v[i]; }
 
154
 
 
155
    bool operator==(const bvec &v) const { return x==v.x && y==v.y && z==v.z; }
 
156
    bool operator!=(const bvec &v) const { return x!=v.x || y!=v.y || z!=v.z; }
 
157
 
 
158
    bool iszero() const { return x==0 && y==0 && z==0; }
 
159
 
 
160
    vec tovec() const { return vec(x*(2.0f/255.0f)-1.0f, y*(2.0f/255.0f)-1.0f, z*(2.0f/255.0f)-1.0f); }
 
161
};
 
162
 
 
163
struct glmatrixf
 
164
{
 
165
    float v[16];
 
166
 
 
167
    float operator[](int i) const { return v[i]; }
 
168
    float &operator[](int i) { return v[i]; }
 
169
 
 
170
    #define ROTVEC(A, B) \
 
171
    { \
 
172
        float a = A, b = B; \
 
173
        A = a*c + b*s; \
 
174
        B = b*c - a*s; \
 
175
    }
 
176
 
 
177
    void rotate_around_x(float angle)
 
178
    {
 
179
        float c = cosf(angle), s = sinf(angle);
 
180
        ROTVEC(v[4], v[8]);
 
181
        ROTVEC(v[5], v[9]);
 
182
        ROTVEC(v[6], v[10]);
 
183
    }
 
184
 
 
185
    void rotate_around_y(float angle)
 
186
    {
 
187
        float c = cosf(angle), s = sinf(angle);
 
188
        ROTVEC(v[8], v[0]);
 
189
        ROTVEC(v[9], v[1]);
 
190
        ROTVEC(v[10], v[2]);
 
191
    }
 
192
 
 
193
    void rotate_around_z(float angle)
 
194
    {
 
195
        float c = cosf(angle), s = sinf(angle);
 
196
        ROTVEC(v[0], v[4]);
 
197
        ROTVEC(v[1], v[5]);
 
198
        ROTVEC(v[2], v[6]);
 
199
    }
 
200
 
 
201
    #undef ROTVEC
 
202
 
 
203
    #define MULMAT(row, col) \
 
204
       v[col + row] = x[row]*y[col] + x[row + 4]*y[col + 1] + x[row + 8]*y[col + 2] + x[row + 12]*y[col + 3];
 
205
 
 
206
    template<class XT, class YT>
 
207
    void mul(const XT x[16], const YT y[16])
 
208
    {
 
209
        MULMAT(0, 0); MULMAT(1, 0); MULMAT(2, 0); MULMAT(3, 0);
 
210
        MULMAT(0, 4); MULMAT(1, 4); MULMAT(2, 4); MULMAT(3, 4);
 
211
        MULMAT(0, 8); MULMAT(1, 8); MULMAT(2, 8); MULMAT(3, 8);
 
212
        MULMAT(0, 12); MULMAT(1, 12); MULMAT(2, 12); MULMAT(3, 12);
 
213
    }
 
214
 
 
215
    #undef MULMAT
 
216
 
 
217
    void mul(const glmatrixf &x, const glmatrixf &y)
 
218
    {
 
219
        mul(x.v, y.v);
 
220
    }
 
221
 
 
222
    void identity()
 
223
    {
 
224
        static const float m[16] =
 
225
        {
 
226
            1, 0, 0, 0,
 
227
            0, 1, 0, 0,
 
228
            0, 0, 1, 0,
 
229
            0, 0, 0, 1
 
230
        };
 
231
        memcpy(v, m, sizeof(v));
 
232
    }
 
233
 
 
234
    void translate(float x, float y, float z)
 
235
    {
 
236
        v[12] += x;
 
237
        v[13] += y;
 
238
        v[14] += z;
 
239
    }
 
240
 
 
241
    void translate(const vec &o)
 
242
    {
 
243
        translate(o.x, o.y, o.z);
 
244
    }
 
245
 
 
246
    void scale(float x, float y, float z)
 
247
    {
 
248
        v[0] *= x; v[1] *= x; v[2] *= x; v[3] *= x;
 
249
        v[4] *= y; v[5] *= y; v[6] *= y; v[7] *= y;
 
250
        v[8] *= z; v[9] *= z; v[10] *= z; v[11] *= z;
 
251
    }
 
252
 
 
253
    void projective()
 
254
    {
 
255
        loopi(2) loopj(4) v[i + j*4] = 0.5f*(v[i + j*4] + v[3 + j*4]);
 
256
    }
 
257
 
 
258
    void invertnormal(vec &dir) const
 
259
    {
 
260
        vec n(dir);
 
261
        dir.x = n.x*v[0] + n.y*v[1] + n.z*v[2];
 
262
        dir.y = n.x*v[4] + n.y*v[5] + n.z*v[6];
 
263
        dir.z = n.x*v[8] + n.y*v[9] + n.z*v[10];
 
264
    }
 
265
 
 
266
    void invertvertex(vec &pos) const
 
267
    {
 
268
        vec p(pos);
 
269
        p.x -= v[12];
 
270
        p.y -= v[13];
 
271
        p.z -= v[14];
 
272
        pos.x = p.x*v[0] + p.y*v[1] + p.z*v[2];
 
273
        pos.y = p.x*v[4] + p.y*v[5] + p.z*v[6];
 
274
        pos.z = p.x*v[8] + p.y*v[9] + p.z*v[10];
 
275
    }
 
276
 
 
277
    float transformx(const vec &p) const
 
278
    {
 
279
        return p.x*v[0] + p.y*v[4] + p.z*v[8] + v[12];
 
280
    }
 
281
 
 
282
    float transformy(const vec &p) const
 
283
    {
 
284
        return p.x*v[1] + p.y*v[5] + p.z*v[9] + v[13];
 
285
    }
 
286
 
 
287
    float transformz(const vec &p) const
 
288
    {
 
289
        return p.x*v[2] + p.y*v[6] + p.z*v[10] + v[14];
 
290
    }
 
291
 
 
292
    float transformw(const vec &p) const
 
293
    {
 
294
        return p.x*v[3] + p.y*v[7] + p.z*v[11] + v[15];
 
295
    }
 
296
 
 
297
    void transform(const vec &in, vec4 &out) const
 
298
    {
 
299
        out.x = transformx(in);
 
300
        out.y = transformy(in);
 
301
        out.z = transformz(in);
 
302
        out.w = transformw(in);
 
303
    }
 
304
 
 
305
    vec gettranslation() const
 
306
    {
 
307
        return vec(v[12], v[13], v[14]);
 
308
    }
 
309
    
 
310
    float determinant() const;
 
311
    void adjoint(const glmatrixf &m);
 
312
    bool invert(const glmatrixf &m, float mindet = 1.0e-10f);
 
313
};
 
314