Changeset 239
 Timestamp:
 Jan 19, 2011, 9:33:36 PM (10 years ago)
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

trunk/src/matrix.h
r237 r239 17 17 #define __DH_MATRIX_H__ 18 18 19 #define OPERATORS(elems) \ 20 inline Vec##elems<T> operator+(Vec##elems<T> const &op) const \ 19 #include <cmath> 20 21 #define VECTOR_OP(elems, op) \ 22 inline Vec##elems<T> operator op(Vec##elems<T> const &val) const \ 21 23 { \ 22 24 Vec##elems<T> ret; \ 23 25 for (int n = 0; n < elems; n++) \ 24 ret[n] = (*this)[n] + op[n]; \26 ret[n] = (*this)[n] op val[n]; \ 25 27 return ret; \ 26 } \ 27 \ 28 inline Vec##elems<T> operator(Vec##elems<T> const &op) const \ 28 } 29 30 #define SCALAR_OP(elems, op) \ 31 inline Vec##elems<T> operator op(T const &val) const \ 29 32 { \ 30 33 Vec##elems<T> ret; \ 31 34 for (int n = 0; n < elems; n++) \ 32 ret[n] = (*this)[n]  op[n]; \35 ret[n] = (*this)[n] op val; \ 33 36 return ret; \ 37 } 38 39 #define OPERATORS(elems) \ 40 T& operator[](int n) { return *(&x + n); } \ 41 T const& operator[](int n) const { return *(&x + n); } \ 42 \ 43 VECTOR_OP(elems, ) \ 44 VECTOR_OP(elems, +) \ 45 VECTOR_OP(elems, *) \ 46 VECTOR_OP(elems, /) \ 47 \ 48 SCALAR_OP(elems, ) \ 49 SCALAR_OP(elems, +) \ 50 SCALAR_OP(elems, *) \ 51 SCALAR_OP(elems, /) \ 52 \ 53 inline float len() const \ 54 { \ 55 T acc = 0; \ 56 for (int n = 0; n < elems; n++) \ 57 acc += (*this)[n] * (*this)[n]; \ 58 return sqrtf((float)acc); \ 34 59 } 35 60 … … 38 63 Vec2() { x = y = 0; } 39 64 Vec2(T _x, T _y) { x = _x; y = _y; } 40 T& operator[](int n) { return *(&x + n); }41 T const& operator[](int n) const { return *(&x + n); }42 65 43 66 OPERATORS(2) … … 54 77 Vec3() { x = y = z = 0; } 55 78 Vec3(T _x, T _y, T _z) { x = _x; y = _y; z = _z; } 56 T& operator[](int n) { return *(&x + n); }57 79 58 80 OPERATORS(3)
Note: See TracChangeset
for help on using the changeset viewer.