summaryrefslogtreecommitdiffstats
path: root/demos/boxes/vector.h
blob: bb2453115844bf566715198186f677240e07dac7 (plain)
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
/****************************************************************************
**
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
** Contact: Qt Software Information (qt-info@nokia.com)
**
** This file is part of the demonstration applications of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the either Technology Preview License Agreement or the
** Beta Release License Agreement.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain
** additional rights. These rights are described in the Nokia Qt LGPL
** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
** package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at qt-sales@nokia.com.
** $QT_END_LICENSE$
**
****************************************************************************/

#ifndef VECTOR_H
#define VECTOR_H

#include <cassert>
#include <cmath>
#include <iostream>

namespace gfx
{

template<class T, int n>
struct Vector
{
    // Keep the Vector struct a plain old data (POD) struct by avoiding constructors

    static Vector vector(T x)
    {
        Vector result;
        for (int i = 0; i < n; ++i)
            result.v[i] = x;
        return result;
    }

    // Use only for 2D vectors
    static Vector vector(T x, T y)
    {
        assert(n == 2);
        Vector result;
        result.v[0] = x;
        result.v[1] = y;
        return result;
    }

    // Use only for 3D vectors
    static Vector vector(T x, T y, T z)
    {
        assert(n == 3);
        Vector result;
        result.v[0] = x;
        result.v[1] = y;
        result.v[2] = z;
        return result;
    }

    // Use only for 4D vectors
    static Vector vector(T x, T y, T z, T w)
    {
        assert(n == 4);
        Vector result;
        result.v[0] = x;
        result.v[1] = y;
        result.v[2] = z;
        result.v[3] = w;
        return result;
    }

    // Pass 'n' arguments to this function.
    static Vector vector(T *v)
    {
        Vector result;
        for (int i = 0; i < n; ++i)
            result.v[i] = v[i];
        return result;
    }

    T &operator [] (int i) {return v[i];}
    T operator [] (int i) const {return v[i];}

#define VECTOR_BINARY_OP(op, arg, rhs)      \
    Vector operator op (arg) const          \
    {                                       \
        Vector result;                      \
        for (int i = 0; i < n; ++i)         \
            result.v[i] = v[i] op rhs;      \
        return result;                      \
    }

    VECTOR_BINARY_OP(+, const Vector &u, u.v[i])
    VECTOR_BINARY_OP(-, const Vector &u, u.v[i])
    VECTOR_BINARY_OP(*, const Vector &u, u.v[i])
    VECTOR_BINARY_OP(/, const Vector &u, u.v[i])
    VECTOR_BINARY_OP(+, T s, s)
    VECTOR_BINARY_OP(-, T s, s)
    VECTOR_BINARY_OP(*, T s, s)
    VECTOR_BINARY_OP(/, T s, s)
#undef VECTOR_BINARY_OP

    Vector operator - () const
    {
        Vector result;
        for (int i = 0; i < n; ++i)
            result.v[i] = -v[i];
        return result;
    }

#define VECTOR_ASSIGN_OP(op, arg, rhs)      \
    Vector &operator op (arg)               \
    {                                       \
        for (int i = 0; i < n; ++i)         \
            v[i] op rhs;                    \
        return *this;                       \
    }

    VECTOR_ASSIGN_OP(+=, const Vector &u, u.v[i])
    VECTOR_ASSIGN_OP(-=, const Vector &u, u.v[i])
    VECTOR_ASSIGN_OP(=, T s, s)
    VECTOR_ASSIGN_OP(*=, T s, s)
    VECTOR_ASSIGN_OP(/=, T s, s)
#undef VECTOR_ASSIGN_OP

    static T dot(const Vector &u, const Vector &v)
    {
        T sum(0);
        for (int i = 0; i < n; ++i)
            sum += u.v[i] * v.v[i];
        return sum;
    }

    static Vector cross(const Vector &u, const Vector &v)
    {
        assert(n == 3);
        return vector(u.v[1] * v.v[2] - u.v[2] * v.v[1],
                      u.v[2] * v.v[0] - u.v[0] * v.v[2],
                      u.v[0] * v.v[1] - u.v[1] * v.v[0]);
    }

    T sqrNorm() const
    {
        return dot(*this, *this);
    }

    // requires floating point type T
    void normalize()
    {
        T s = sqrNorm();
        if (s != 0)
            *this /= sqrt(s);
    }

    // requires floating point type T
    Vector normalized() const
    {
        T s = sqrNorm();
        if (s == 0)
            return *this;
        return *this / sqrt(s);
    }

    T *bits() {return v;}
    const T *bits() const {return v;}

    T v[n];
};

#define SCALAR_VECTOR_BINARY_OP(op)                     \
template<class T, int n>                                \
Vector<T, n> operator op (T s, const Vector<T, n>& u)   \
{                                                       \
    Vector<T, n> result;                                \
    for (int i = 0; i < n; ++i)                         \
        result[i] = s op u[i];                          \
    return result;                                      \
}

SCALAR_VECTOR_BINARY_OP(+)
SCALAR_VECTOR_BINARY_OP(-)
SCALAR_VECTOR_BINARY_OP(*)
SCALAR_VECTOR_BINARY_OP(/)
#undef SCALAR_VECTOR_BINARY_OP

template<class T, int n>
std::ostream &operator << (std::ostream &os, const Vector<T, n> &v)
{
    assert(n > 0);
    os << "[" << v[0];
    for (int i = 1; i < n; ++i)
        os << ", " << v[i];
    os << "]";
    return os;
}

typedef Vector<float, 2> Vector2f;
typedef Vector<float, 3> Vector3f;
typedef Vector<float, 4> Vector4f;

template<class T, int rows, int cols>
struct Matrix
{
    // Keep the Matrix struct a plain old data (POD) struct by avoiding constructors

    static Matrix matrix(T x)
    {
        Matrix result;
        for (int i = 0; i < rows; ++i) {
            for (int j = 0; j < cols; ++j)
                result.v[i][j] = x;
        }
        return result;
    }

    static Matrix matrix(T *m)
    {
        Matrix result;
        for (int i = 0; i < rows; ++i) {
            for (int j = 0; j < cols; ++j) {
                result.v[i][j] = *m;
                ++m;
            }
        }
        return result;
    }

    T &operator () (int i, int j) {return v[i][j];}
    T operator () (int i, int j) const {return v[i][j];}
    Vector<T, cols> &operator [] (int i) {return v[i];}
    const Vector<T, cols> &operator [] (int i) const {return v[i];}

    // TODO: operators, methods

    Vector<T, rows> operator * (const Vector<T, cols> &u) const
    {
        Vector<T, rows> result;
        for (int i = 0; i < rows; ++i)
            result[i] = Vector<T, cols>::dot(v[i], u);
        return result;
    }

    template<int k>
    Matrix<T, rows, k> operator * (const Matrix<T, cols, k> &m)
    {
        Matrix<T, rows, k> result;
        for (int i = 0; i < rows; ++i)
            result[i] = v[i] * m;
        return result;
    }

    T* bits() {return reinterpret_cast<T *>(this);}
    const T* bits() const {return reinterpret_cast<const T *>(this);}

    // Simple Gauss elimination.
    // TODO: Optimize and improve stability.
    Matrix inverse(bool *ok = 0) const
    {
        assert(rows == cols);
        Matrix rhs = identity();
        Matrix lhs(*this);
        T temp;
        // Down
        for (int i = 0; i < rows; ++i) {
            // Pivoting
            int pivot = i;
            for (int j = i; j < rows; ++j) {
                if (qAbs(lhs(j, i)) > lhs(pivot, i))
                    pivot = j;
            }
			// TODO: fuzzy compare.
            if (lhs(pivot, i) == T(0)) {
                if (ok)
                    *ok = false;
                return rhs;
            }
            if (pivot != i) {
                for (int j = i; j < cols; ++j) {
                    temp = lhs(pivot, j);
                    lhs(pivot, j) = lhs(i, j);
                    lhs(i, j) = temp;
                }
                for (int j = 0; j < cols; ++j) {
                    temp = rhs(pivot, j);
                    rhs(pivot, j) = rhs(i, j);
                    rhs(i, j) = temp;
                }
            }

            // Normalize i-th row
            rhs[i] /= lhs(i, i);
            for (int j = cols - 1; j > i; --j)
                lhs(i, j) /= lhs(i, i);

			// Eliminate non-zeros in i-th column below the i-th row.
            for (int j = i + 1; j < rows; ++j) {
                rhs[j] -= lhs(j, i) * rhs[i];
                for (int k = i + 1; k < cols; ++k)
                    lhs(j, k) -= lhs(j, i) * lhs(i, k);
            }
        }
        // Up
        for (int i = rows - 1; i > 0; --i) {
            for (int j = i - 1; j >= 0; --j)
                rhs[j] -= lhs(j, i) * rhs[i];
        }
        if (ok)
            *ok = true;
        return rhs;
    }

    Matrix<T, cols, rows> transpose() const
    {
        Matrix<T, cols, rows> result;
        for (int i = 0; i < rows; ++i) {
            for (int j = 0; j < cols; ++j)
                result.v[j][i] = v[i][j];
        }
        return result;
    }

    static Matrix identity()
    {
        Matrix result = matrix(T(0));
        for (int i = 0; i < rows && i < cols; ++i)
            result.v[i][i] = T(1);
        return result;
    }

    Vector<T, cols> v[rows];
};

template<class T, int rows, int cols>
Vector<T, cols> operator * (const Vector<T, rows> &u, const Matrix<T, rows, cols> &m)
{
    Vector<T, cols> result = Vector<T, cols>::vector(T(0));
    for (int i = 0; i < rows; ++i)
        result += m[i] * u[i];
    return result;
}

template<class T, int rows, int cols>
std::ostream &operator << (std::ostream &os, const Matrix<T, rows, cols> &m)
{
    assert(rows > 0);
    os << "[" << m[0];
    for (int i = 1; i < rows; ++i)
        os << ", " << m[i];
    os << "]";
    return os;
}


typedef Matrix<float, 2, 2> Matrix2x2f;
typedef Matrix<float, 3, 3> Matrix3x3f;
typedef Matrix<float, 4, 4> Matrix4x4f;

template<class T>
struct Quaternion
{
    // Keep the Quaternion struct a plain old data (POD) struct by avoiding constructors

    static Quaternion quaternion(T s, T x, T y, T z)
    {
        Quaternion result;
        result.scalar = s;
        result.vector[0] = x;
        result.vector[1] = y;
        result.vector[2] = z;
        return result;
    }

    static Quaternion quaternion(T s, const Vector<T, 3> &v)
    {
        Quaternion result;
        result.scalar = s;
        result.vector = v;
        return result;
    }

    static Quaternion identity()
    {
        return quaternion(T(1), T(0), T(0), T(0));
    }

    // assumes that all the elements are packed tightly
    T& operator [] (int i) {return reinterpret_cast<T *>(this)[i];}
    T operator [] (int i) const {return reinterpret_cast<const T *>(this)[i];}

#define QUATERNION_BINARY_OP(op, arg, rhs)  \
    Quaternion operator op (arg) const      \
    {                                       \
        Quaternion result;                  \
        for (int i = 0; i < 4; ++i)         \
            result[i] = (*this)[i] op rhs;  \
        return result;                      \
    }

    QUATERNION_BINARY_OP(+, const Quaternion &q, q[i])
    QUATERNION_BINARY_OP(-, const Quaternion &q, q[i])
    QUATERNION_BINARY_OP(*, T s, s)
    QUATERNION_BINARY_OP(/, T s, s)
#undef QUATERNION_BINARY_OP

    Quaternion operator - () const
    {
        return Quaternion(-scalar, -vector);
    }

    Quaternion operator * (const Quaternion &q) const
    {
        Quaternion result;
        result.scalar = scalar * q.scalar - Vector<T, 3>::dot(vector, q.vector);
        result.vector = scalar * q.vector + vector * q.scalar + Vector<T, 3>::cross(vector, q.vector);
        return result;
    }

    Quaternion operator * (const Vector<T, 3> &v) const
    {
        Quaternion result;
        result.scalar = -Vector<T, 3>::dot(vector, v);
        result.vector = scalar * v + Vector<T, 3>::cross(vector, v);
        return result;
    }

    friend Quaternion operator * (const Vector<T, 3> &v, const Quaternion &q)
    {
        Quaternion result;
        result.scalar = -Vector<T, 3>::dot(v, q.vector);
        result.vector = v * q.scalar + Vector<T, 3>::cross(v, q.vector);
        return result;
    }

#define QUATERNION_ASSIGN_OP(op, arg, rhs)  \
    Quaternion &operator op (arg)           \
    {                                       \
        for (int i = 0; i < 4; ++i)         \
            (*this)[i] op rhs;              \
        return *this;                       \
    }

    QUATERNION_ASSIGN_OP(+=, const Quaternion &q, q[i])
    QUATERNION_ASSIGN_OP(-=, const Quaternion &q, q[i])
    QUATERNION_ASSIGN_OP(=, T s, s)
    QUATERNION_ASSIGN_OP(*=, T s, s)
    QUATERNION_ASSIGN_OP(/=, T s, s)
#undef QUATERNION_ASSIGN_OP

    Quaternion& operator *= (const Quaternion &q)
    {
        Quaternion result;
        result.scalar = scalar * q.scalar - Vector<T, 3>::dot(vector, q.vector);
        result.vector = scalar * q.vector + vector * q.scalar + Vector<T, 3>::cross(vector, q.vector);
        return (*this = result);
    }

    Quaternion& operator *= (const Vector<T, 3> &v)
    {
        Quaternion result;
        result.scalar = -Vector<T, 3>::dot(vector, v);
        result.vector = scalar * v + Vector<T, 3>::cross(vector, v);
        return (*this = result);
    }

    Quaternion conjugate() const
    {
        return quaternion(scalar, -vector);
    }

    T sqrNorm() const
    {
        return scalar * scalar + vector.sqrNorm();
    }

    Quaternion inverse() const
    {
        return conjugate() / sqrNorm();
    }

    // requires floating point type T
    Quaternion normalized() const
    {
        T s = sqrNorm();
        if (s == 0)
            return *this;
        return *this / sqrt(s);
    }

    void matrix(Matrix<T, 3, 3>& m) const
    {
        T bb = vector[0] * vector[0];
        T cc = vector[1] * vector[1];
        T dd = vector[2] * vector[2];
        T diag = scalar * scalar - bb - cc - dd;
        T ab = scalar * vector[0];
        T ac = scalar * vector[1];
        T ad = scalar * vector[2];
        T bc = vector[0] * vector[1];
        T cd = vector[1] * vector[2];
        T bd = vector[2] * vector[0];
        m(0, 0) = diag + 2 * bb;
        m(0, 1) = 2 * (bc - ad);
        m(0, 2) = 2 * (ac + bd);
        m(1, 0) = 2 * (ad + bc);
        m(1, 1) = diag + 2 * cc;
        m(1, 2) = 2 * (cd - ab);
        m(2, 0) = 2 * (bd - ac);
        m(2, 1) = 2 * (ab + cd);
        m(2, 2) = diag + 2 * dd;
    }

    void matrix(Matrix<T, 4, 4>& m) const
    {
        T bb = vector[0] * vector[0];
        T cc = vector[1] * vector[1];
        T dd = vector[2] * vector[2];
        T diag = scalar * scalar - bb - cc - dd;
        T ab = scalar * vector[0];
        T ac = scalar * vector[1];
        T ad = scalar * vector[2];
        T bc = vector[0] * vector[1];
        T cd = vector[1] * vector[2];
        T bd = vector[2] * vector[0];
        m(0, 0) = diag + 2 * bb;
        m(0, 1) = 2 * (bc - ad);
        m(0, 2) = 2 * (ac + bd);
        m(0, 3) = 0;
        m(1, 0) = 2 * (ad + bc);
        m(1, 1) = diag + 2 * cc;
        m(1, 2) = 2 * (cd - ab);
        m(1, 3) = 0;
        m(2, 0) = 2 * (bd - ac);
        m(2, 1) = 2 * (ab + cd);
        m(2, 2) = diag + 2 * dd;
        m(2, 3) = 0;
        m(3, 0) = 0;
        m(3, 1) = 0;
        m(3, 2) = 0;
        m(3, 3) = 1;
    }

    // assumes that 'this' is normalized
    Vector<T, 3> transform(const Vector<T, 3> &v) const
    {
        Matrix<T, 3, 3> m;
        matrix(m);
        return v * m;
    }

    // assumes that all the elements are packed tightly
    T* bits() {return reinterpret_cast<T *>(this);}
    const T* bits() const {return reinterpret_cast<const T *>(this);}

    // requires floating point type T
    static Quaternion rotation(T angle, const Vector<T, 3> &unitAxis)
    {
        T s = sin(angle / 2);
        T c = cos(angle / 2);
        return quaternion(c, unitAxis * s);
    }

    T scalar;
    Vector<T, 3> vector;
};

template<class T>
Quaternion<T> operator * (T s, const Quaternion<T>& q)
{
    return Quaternion<T>::quaternion(s * q.scalar, s * q.vector);
}

typedef Quaternion<float> Quaternionf;

} // end namespace gfx

#endif