# source:trunk/src/matrix.h@293Tweet

Last change on this file since 293 was 293, checked in by sam, 11 years ago

Add pseudorandom functions for floats.

• Property svn:keywords set to `Id`
File size: 3.4 KB
Line
1//
2// Lol Engine
3//
4// Copyright: (c) 2010-2011 Sam Hocevar <sam@hocevar.net>
5//   This program is free software; you can redistribute it and/or
6//   modify it under the terms of the Do What The Fuck You Want To
7//   Public License, Version 2, as published by Sam Hocevar. See
8//   http://sam.zoy.org/projects/COPYING.WTFPL for more details.
9//
10
11//
12// The Matrix classes
13// ------------------
14//
15
16#if !defined __DH_MATRIX_H__
17#define __DH_MATRIX_H__
18
19#include <cmath>
20
21#define VECTOR_OP(elems, op) \
22    template<typename U> \
23    inline Vec##elems<T> operator op(Vec##elems<U> const &val) const \
24    { \
25        Vec##elems<T> ret; \
26        for (int n = 0; n < elems; n++) \
27            ret[n] = (*this)[n] op val[n]; \
28        return ret; \
29    }
30
31#define BOOL_OP(elems, op, ret) \
32    inline bool operator op(Vec##elems<T> const &val) const \
33    { \
34        for (int n = 0; n < elems; n++) \
35            if ((*this)[n] != val[n]) \
36                return ret; \
37        return !ret; \
38    }
39
40#define SCALAR_OP(elems, op) \
41    inline Vec##elems<T> operator op(T const &val) const \
42    { \
43        Vec##elems<T> ret; \
44        for (int n = 0; n < elems; n++) \
45            ret[n] = (*this)[n] op val; \
46        return ret; \
47    }
48
49#define OPERATORS(elems) \
50    inline T& operator[](int n) { return *(&x + n); } \
51    inline T const& operator[](int n) const { return *(&x + n); } \
52    \
53    VECTOR_OP(elems, -) \
54    VECTOR_OP(elems, +) \
55    VECTOR_OP(elems, *) \
56    VECTOR_OP(elems, /) \
57    \
58    BOOL_OP(elems, ==, false) \
59    BOOL_OP(elems, !=, true) \
60    \
61    SCALAR_OP(elems, -) \
62    SCALAR_OP(elems, +) \
63    SCALAR_OP(elems, *) \
64    SCALAR_OP(elems, /) \
65    \
66    template<typename U> \
67    inline operator Vec##elems<U>() const \
68    { \
69        Vec##elems<U> ret; \
70        for (int n = 0; n < elems; n++) \
71            ret[n] = static_cast<U>((*this)[n]); \
72        return ret; \
73    } \
74    \
75    inline T sqlen() const \
76    { \
77        T acc = 0; \
78        for (int n = 0; n < elems; n++) \
79            acc += (*this)[n] * (*this)[n]; \
80        return acc; \
81    } \
82    \
83    inline float len() const \
84    { \
85        return sqrtf((float)sqlen()); \
86    }
87
88template <typename T> struct Vec2
89{
90    inline Vec2() { x = y = 0; }
91    inline Vec2(T val) { x = y = val; }
92    inline Vec2(T _x, T _y) { x = _x; y = _y; }
93
94    OPERATORS(2)
95
96    union { T x; T a; T i; };
97    union { T y; T b; T j; };
98};
99
100typedef Vec2<float> Float2;
101typedef Vec2<int> Int2;
102
103template <typename T> struct Vec3
104{
105    inline Vec3() { x = y = z = 0; }
106    inline Vec3(T val) { x = y = z = val; }
107    inline Vec3(T _x, T _y, T _z) { x = _x; y = _y; z = _z; }
108
109    OPERATORS(3)
110
111    union { T x; T a; T i; };
112    union { T y; T b; T j; };
113    union { T z; T c; T k; };
114};
115
116typedef Vec3<float> Float3;
117typedef Vec3<int> Int3;
118
119#define SCALAR_GLOBAL(elems, op, U) \
120    template<typename T> \
121    static inline Vec##elems<U> operator op(U const &val, \
122                                            Vec##elems<T> const &that) \
123    { \
124        Vec##elems<U> ret; \
125        for (int n = 0; n < elems; n++) \
126            ret[n] = val op that[n]; \
127        return ret; \
128    }
129
130#define SCALAR_GLOBAL2(elems, op) \
131    SCALAR_GLOBAL(elems, op, int) \
132    SCALAR_GLOBAL(elems, op, float)
133
134#define GLOBALS(elems) \
135    SCALAR_GLOBAL2(elems, -) \
136    SCALAR_GLOBAL2(elems, +) \
137    SCALAR_GLOBAL2(elems, *) \
138    SCALAR_GLOBAL2(elems, /)
139
140GLOBALS(2)
141GLOBALS(3)
142
143#endif // __DH_MATRIX_H__
144
Note: See TracBrowser for help on using the repository browser.