An incomplete quadcopter control programme.

Dependencies:   mbed

Committer:
Gurvan
Date:
Wed Jul 17 15:58:25 2013 +0000
Revision:
0:9cb9445a11f0
Pour Zobson, fi(r)st commit.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Gurvan 0:9cb9445a11f0 1 /*
Gurvan 0:9cb9445a11f0 2 Copyright (c) 2007, Markus Trenkwalder
Gurvan 0:9cb9445a11f0 3
Gurvan 0:9cb9445a11f0 4 All rights reserved.
Gurvan 0:9cb9445a11f0 5
Gurvan 0:9cb9445a11f0 6 Redistribution and use in source and binary forms, with or without
Gurvan 0:9cb9445a11f0 7 modification, are permitted provided that the following conditions are met:
Gurvan 0:9cb9445a11f0 8
Gurvan 0:9cb9445a11f0 9 * Redistributions of source code must retain the above copyright notice,
Gurvan 0:9cb9445a11f0 10 this list of conditions and the following disclaimer.
Gurvan 0:9cb9445a11f0 11
Gurvan 0:9cb9445a11f0 12 * Redistributions in binary form must reproduce the above copyright notice,
Gurvan 0:9cb9445a11f0 13 this list of conditions and the following disclaimer in the documentation
Gurvan 0:9cb9445a11f0 14 and/or other materials provided with the distribution.
Gurvan 0:9cb9445a11f0 15
Gurvan 0:9cb9445a11f0 16 * Neither the name of the library's copyright owner nor the names of its
Gurvan 0:9cb9445a11f0 17 contributors may be used to endorse or promote products derived from this
Gurvan 0:9cb9445a11f0 18 software without specific prior written permission.
Gurvan 0:9cb9445a11f0 19
Gurvan 0:9cb9445a11f0 20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
Gurvan 0:9cb9445a11f0 21 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
Gurvan 0:9cb9445a11f0 22 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
Gurvan 0:9cb9445a11f0 23 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
Gurvan 0:9cb9445a11f0 24 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
Gurvan 0:9cb9445a11f0 25 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
Gurvan 0:9cb9445a11f0 26 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
Gurvan 0:9cb9445a11f0 27 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
Gurvan 0:9cb9445a11f0 28 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
Gurvan 0:9cb9445a11f0 29 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
Gurvan 0:9cb9445a11f0 30 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Gurvan 0:9cb9445a11f0 31 */
Gurvan 0:9cb9445a11f0 32
Gurvan 0:9cb9445a11f0 33 #ifndef VECTOR_MATH_H
Gurvan 0:9cb9445a11f0 34 #define VECTOR_MATH_H
Gurvan 0:9cb9445a11f0 35
Gurvan 0:9cb9445a11f0 36 //#include <cmath>
Gurvan 0:9cb9445a11f0 37
Gurvan 0:9cb9445a11f0 38 // "minor" can be defined from GCC and can cause problems
Gurvan 0:9cb9445a11f0 39 #undef minor
Gurvan 0:9cb9445a11f0 40
Gurvan 0:9cb9445a11f0 41 #ifndef M_PI
Gurvan 0:9cb9445a11f0 42 #define M_PI 3.14159265358979323846
Gurvan 0:9cb9445a11f0 43 #endif
Gurvan 0:9cb9445a11f0 44
Gurvan 0:9cb9445a11f0 45 namespace vmath {
Gurvan 0:9cb9445a11f0 46
Gurvan 0:9cb9445a11f0 47 //using std::sin;
Gurvan 0:9cb9445a11f0 48 //using std::cos;
Gurvan 0:9cb9445a11f0 49 //using std::acos;
Gurvan 0:9cb9445a11f0 50 //using std::sqrt;
Gurvan 0:9cb9445a11f0 51
Gurvan 0:9cb9445a11f0 52 template <typename T>
Gurvan 0:9cb9445a11f0 53 inline T rsqrt(T x)
Gurvan 0:9cb9445a11f0 54 {
Gurvan 0:9cb9445a11f0 55 return T(1) / sqrt(x);
Gurvan 0:9cb9445a11f0 56 }
Gurvan 0:9cb9445a11f0 57
Gurvan 0:9cb9445a11f0 58 template <typename T>
Gurvan 0:9cb9445a11f0 59 inline T inv(T x)
Gurvan 0:9cb9445a11f0 60 {
Gurvan 0:9cb9445a11f0 61 return T(1) / x;
Gurvan 0:9cb9445a11f0 62 }
Gurvan 0:9cb9445a11f0 63
Gurvan 0:9cb9445a11f0 64 namespace detail {
Gurvan 0:9cb9445a11f0 65 // This function is used heavily in this library. Here is a generic
Gurvan 0:9cb9445a11f0 66 // implementation for it. If you can provide a faster one for your specific
Gurvan 0:9cb9445a11f0 67 // types this can speed up things considerably.
Gurvan 0:9cb9445a11f0 68 template <typename T>
Gurvan 0:9cb9445a11f0 69 inline T multiply_accumulate(int count, const T *a, const T *b)
Gurvan 0:9cb9445a11f0 70 {
Gurvan 0:9cb9445a11f0 71 T result = T(0);
Gurvan 0:9cb9445a11f0 72 for (int i = 0; i < count; ++i)
Gurvan 0:9cb9445a11f0 73 result += a[i] * b[i];
Gurvan 0:9cb9445a11f0 74 return result;
Gurvan 0:9cb9445a11f0 75 }
Gurvan 0:9cb9445a11f0 76 }
Gurvan 0:9cb9445a11f0 77
Gurvan 0:9cb9445a11f0 78 #define MOP_M_CLASS_TEMPLATE(CLASS, OP, COUNT) \
Gurvan 0:9cb9445a11f0 79 CLASS & operator OP (const CLASS& rhs) \
Gurvan 0:9cb9445a11f0 80 { \
Gurvan 0:9cb9445a11f0 81 for (int i = 0; i < (COUNT); ++i ) \
Gurvan 0:9cb9445a11f0 82 (*this)[i] OP rhs[i]; \
Gurvan 0:9cb9445a11f0 83 return *this; \
Gurvan 0:9cb9445a11f0 84 }
Gurvan 0:9cb9445a11f0 85
Gurvan 0:9cb9445a11f0 86 #define MOP_M_TYPE_TEMPLATE(CLASS, OP, COUNT) \
Gurvan 0:9cb9445a11f0 87 CLASS & operator OP (const T & rhs) \
Gurvan 0:9cb9445a11f0 88 { \
Gurvan 0:9cb9445a11f0 89 for (int i = 0; i < (COUNT); ++i ) \
Gurvan 0:9cb9445a11f0 90 (*this)[i] OP rhs; \
Gurvan 0:9cb9445a11f0 91 return *this; \
Gurvan 0:9cb9445a11f0 92 }
Gurvan 0:9cb9445a11f0 93
Gurvan 0:9cb9445a11f0 94 #define MOP_COMP_TEMPLATE(CLASS, COUNT) \
Gurvan 0:9cb9445a11f0 95 bool operator == (const CLASS & rhs) \
Gurvan 0:9cb9445a11f0 96 { \
Gurvan 0:9cb9445a11f0 97 bool result = true; \
Gurvan 0:9cb9445a11f0 98 for (int i = 0; i < (COUNT); ++i) \
Gurvan 0:9cb9445a11f0 99 result = result && (*this)[i] == rhs[i]; \
Gurvan 0:9cb9445a11f0 100 return result; \
Gurvan 0:9cb9445a11f0 101 } \
Gurvan 0:9cb9445a11f0 102 bool operator != (const CLASS & rhs) \
Gurvan 0:9cb9445a11f0 103 { return !((*this) == rhs); }
Gurvan 0:9cb9445a11f0 104
Gurvan 0:9cb9445a11f0 105 #define MOP_G_UMINUS_TEMPLATE(CLASS, COUNT) \
Gurvan 0:9cb9445a11f0 106 CLASS operator - () const \
Gurvan 0:9cb9445a11f0 107 { \
Gurvan 0:9cb9445a11f0 108 CLASS result; \
Gurvan 0:9cb9445a11f0 109 for (int i = 0; i < (COUNT); ++i) \
Gurvan 0:9cb9445a11f0 110 result[i] = -(*this)[i]; \
Gurvan 0:9cb9445a11f0 111 return result; \
Gurvan 0:9cb9445a11f0 112 }
Gurvan 0:9cb9445a11f0 113
Gurvan 0:9cb9445a11f0 114 #define COMMON_OPERATORS(CLASS, COUNT) \
Gurvan 0:9cb9445a11f0 115 MOP_M_CLASS_TEMPLATE(CLASS, +=, COUNT) \
Gurvan 0:9cb9445a11f0 116 MOP_M_CLASS_TEMPLATE(CLASS, -=, COUNT) \
Gurvan 0:9cb9445a11f0 117 /*no *= as this is not the same for vectors and matrices */ \
Gurvan 0:9cb9445a11f0 118 MOP_M_CLASS_TEMPLATE(CLASS, /=, COUNT) \
Gurvan 0:9cb9445a11f0 119 MOP_M_TYPE_TEMPLATE(CLASS, +=, COUNT) \
Gurvan 0:9cb9445a11f0 120 MOP_M_TYPE_TEMPLATE(CLASS, -=, COUNT) \
Gurvan 0:9cb9445a11f0 121 MOP_M_TYPE_TEMPLATE(CLASS, *=, COUNT) \
Gurvan 0:9cb9445a11f0 122 MOP_M_TYPE_TEMPLATE(CLASS, /=, COUNT) \
Gurvan 0:9cb9445a11f0 123 MOP_G_UMINUS_TEMPLATE(CLASS, COUNT) \
Gurvan 0:9cb9445a11f0 124 MOP_COMP_TEMPLATE(CLASS, COUNT)
Gurvan 0:9cb9445a11f0 125
Gurvan 0:9cb9445a11f0 126 #define VECTOR_COMMON(CLASS, COUNT) \
Gurvan 0:9cb9445a11f0 127 COMMON_OPERATORS(CLASS, COUNT) \
Gurvan 0:9cb9445a11f0 128 MOP_M_CLASS_TEMPLATE(CLASS, *=, COUNT) \
Gurvan 0:9cb9445a11f0 129 operator const T* () const { return &x; } \
Gurvan 0:9cb9445a11f0 130 operator T* () { return &x; }
Gurvan 0:9cb9445a11f0 131
Gurvan 0:9cb9445a11f0 132 #define FOP_G_SOURCE_TEMPLATE(OP, CLASS) \
Gurvan 0:9cb9445a11f0 133 { CLASS<T> r = lhs; r OP##= rhs; return r; }
Gurvan 0:9cb9445a11f0 134
Gurvan 0:9cb9445a11f0 135 #define FOP_G_CLASS_TEMPLATE(OP, CLASS) \
Gurvan 0:9cb9445a11f0 136 template <typename T> \
Gurvan 0:9cb9445a11f0 137 inline CLASS<T> operator OP (const CLASS<T> &lhs, const CLASS<T> &rhs) \
Gurvan 0:9cb9445a11f0 138 FOP_G_SOURCE_TEMPLATE(OP, CLASS)
Gurvan 0:9cb9445a11f0 139
Gurvan 0:9cb9445a11f0 140 #define FOP_G_TYPE_TEMPLATE(OP, CLASS) \
Gurvan 0:9cb9445a11f0 141 template <typename T> \
Gurvan 0:9cb9445a11f0 142 inline CLASS<T> operator OP (const CLASS<T> &lhs, const T &rhs) \
Gurvan 0:9cb9445a11f0 143 FOP_G_SOURCE_TEMPLATE(OP, CLASS)
Gurvan 0:9cb9445a11f0 144
Gurvan 0:9cb9445a11f0 145 // forward declarations
Gurvan 0:9cb9445a11f0 146 template <typename T> struct vec2;
Gurvan 0:9cb9445a11f0 147 template <typename T> struct vec3;
Gurvan 0:9cb9445a11f0 148 template <typename T> struct vec4;
Gurvan 0:9cb9445a11f0 149 template <typename T> struct mat2;
Gurvan 0:9cb9445a11f0 150 template <typename T> struct mat3;
Gurvan 0:9cb9445a11f0 151 template <typename T> struct mat4;
Gurvan 0:9cb9445a11f0 152 template <typename T> struct quat;
Gurvan 0:9cb9445a11f0 153
Gurvan 0:9cb9445a11f0 154 #define FREE_MODIFYING_OPERATORS(CLASS) \
Gurvan 0:9cb9445a11f0 155 FOP_G_CLASS_TEMPLATE(+, CLASS) \
Gurvan 0:9cb9445a11f0 156 FOP_G_CLASS_TEMPLATE(-, CLASS) \
Gurvan 0:9cb9445a11f0 157 FOP_G_CLASS_TEMPLATE(*, CLASS) \
Gurvan 0:9cb9445a11f0 158 FOP_G_CLASS_TEMPLATE(/, CLASS) \
Gurvan 0:9cb9445a11f0 159 FOP_G_TYPE_TEMPLATE(+, CLASS) \
Gurvan 0:9cb9445a11f0 160 FOP_G_TYPE_TEMPLATE(-, CLASS) \
Gurvan 0:9cb9445a11f0 161 FOP_G_TYPE_TEMPLATE(*, CLASS) \
Gurvan 0:9cb9445a11f0 162 FOP_G_TYPE_TEMPLATE(/, CLASS)
Gurvan 0:9cb9445a11f0 163
Gurvan 0:9cb9445a11f0 164 FREE_MODIFYING_OPERATORS(vec2)
Gurvan 0:9cb9445a11f0 165 FREE_MODIFYING_OPERATORS(vec3)
Gurvan 0:9cb9445a11f0 166 FREE_MODIFYING_OPERATORS(vec4)
Gurvan 0:9cb9445a11f0 167 FREE_MODIFYING_OPERATORS(mat2)
Gurvan 0:9cb9445a11f0 168 FREE_MODIFYING_OPERATORS(mat3)
Gurvan 0:9cb9445a11f0 169 FREE_MODIFYING_OPERATORS(mat4)
Gurvan 0:9cb9445a11f0 170 FREE_MODIFYING_OPERATORS(quat)
Gurvan 0:9cb9445a11f0 171
Gurvan 0:9cb9445a11f0 172 #define FREE_OPERATORS(CLASS) \
Gurvan 0:9cb9445a11f0 173 template <typename T> \
Gurvan 0:9cb9445a11f0 174 inline CLASS<T> operator + (const T& a, const CLASS<T>& b) \
Gurvan 0:9cb9445a11f0 175 { CLASS<T> r = b; r += a; return r; } \
Gurvan 0:9cb9445a11f0 176 \
Gurvan 0:9cb9445a11f0 177 template <typename T> \
Gurvan 0:9cb9445a11f0 178 inline CLASS<T> operator * (const T& a, const CLASS<T>& b) \
Gurvan 0:9cb9445a11f0 179 { CLASS<T> r = b; r *= a; return r; } \
Gurvan 0:9cb9445a11f0 180 \
Gurvan 0:9cb9445a11f0 181 template <typename T> \
Gurvan 0:9cb9445a11f0 182 inline CLASS<T> operator - (const T& a, const CLASS<T>& b) \
Gurvan 0:9cb9445a11f0 183 { return -b + a; } \
Gurvan 0:9cb9445a11f0 184 \
Gurvan 0:9cb9445a11f0 185 template <typename T> \
Gurvan 0:9cb9445a11f0 186 inline CLASS<T> operator / (const T& a, const CLASS<T>& b) \
Gurvan 0:9cb9445a11f0 187 { CLASS<T> r(a); r /= b; return r; }
Gurvan 0:9cb9445a11f0 188
Gurvan 0:9cb9445a11f0 189 FREE_OPERATORS(vec2)
Gurvan 0:9cb9445a11f0 190 FREE_OPERATORS(vec3)
Gurvan 0:9cb9445a11f0 191 FREE_OPERATORS(vec4)
Gurvan 0:9cb9445a11f0 192 FREE_OPERATORS(mat2)
Gurvan 0:9cb9445a11f0 193 FREE_OPERATORS(mat3)
Gurvan 0:9cb9445a11f0 194 FREE_OPERATORS(mat4)
Gurvan 0:9cb9445a11f0 195 FREE_OPERATORS(quat)
Gurvan 0:9cb9445a11f0 196
Gurvan 0:9cb9445a11f0 197 template <typename T>
Gurvan 0:9cb9445a11f0 198 struct vec2 {
Gurvan 0:9cb9445a11f0 199 T x, y;
Gurvan 0:9cb9445a11f0 200
Gurvan 0:9cb9445a11f0 201 vec2() {};
Gurvan 0:9cb9445a11f0 202 explicit vec2(const T i) : x(i), y(i) {}
Gurvan 0:9cb9445a11f0 203 explicit vec2(const T ix, const T iy) : x(ix), y(iy) {}
Gurvan 0:9cb9445a11f0 204 explicit vec2(const vec3<T>& v);
Gurvan 0:9cb9445a11f0 205 explicit vec2(const vec4<T>& v);
Gurvan 0:9cb9445a11f0 206
Gurvan 0:9cb9445a11f0 207 VECTOR_COMMON(vec2, 2)
Gurvan 0:9cb9445a11f0 208 };
Gurvan 0:9cb9445a11f0 209
Gurvan 0:9cb9445a11f0 210 template <typename T>
Gurvan 0:9cb9445a11f0 211 struct vec3 {
Gurvan 0:9cb9445a11f0 212 T x, y, z;
Gurvan 0:9cb9445a11f0 213
Gurvan 0:9cb9445a11f0 214 vec3() {};
Gurvan 0:9cb9445a11f0 215 explicit vec3(const T i) : x(i), y(i), z(i) {}
Gurvan 0:9cb9445a11f0 216 explicit vec3(const T ix, const T iy, const T iz) : x(ix), y(iy), z(iz) {}
Gurvan 0:9cb9445a11f0 217 explicit vec3(const vec2<T>& xy, const T iz) : x(xy.x), y(xy.y), z(iz) {}
Gurvan 0:9cb9445a11f0 218 explicit vec3(const T ix, const vec2<T>& yz) : x(ix), y(yz.y), z(yz.z) {}
Gurvan 0:9cb9445a11f0 219 explicit vec3(const vec4<T>& v);
Gurvan 0:9cb9445a11f0 220
Gurvan 0:9cb9445a11f0 221 VECTOR_COMMON(vec3, 3)
Gurvan 0:9cb9445a11f0 222 };
Gurvan 0:9cb9445a11f0 223
Gurvan 0:9cb9445a11f0 224 template <typename T>
Gurvan 0:9cb9445a11f0 225 struct vec4 {
Gurvan 0:9cb9445a11f0 226 T x, y, z, w;
Gurvan 0:9cb9445a11f0 227
Gurvan 0:9cb9445a11f0 228 vec4() {};
Gurvan 0:9cb9445a11f0 229 explicit vec4(const T i) : x(i), y(i), z(i), w(i) {}
Gurvan 0:9cb9445a11f0 230 explicit vec4(const T ix, const T iy, const T iz, const T iw) : x(ix), y(iy), z(iz), w(iw) {}
Gurvan 0:9cb9445a11f0 231 explicit vec4(const vec3<T>& xyz,const T iw) : x(xyz.x), y(xyz.y), z(xyz.z), w(iw) {}
Gurvan 0:9cb9445a11f0 232 explicit vec4(const T ix, const vec3<T>& yzw) : x(ix), y(yzw.x), z(yzw.y), w(yzw.z) {}
Gurvan 0:9cb9445a11f0 233 explicit vec4(const vec2<T>& xy, const vec2<T>& zw) : x(xy.x), y(xy.y), z(zw.x), w(zw.y) {}
Gurvan 0:9cb9445a11f0 234
Gurvan 0:9cb9445a11f0 235 VECTOR_COMMON(vec4, 4)
Gurvan 0:9cb9445a11f0 236 };
Gurvan 0:9cb9445a11f0 237
Gurvan 0:9cb9445a11f0 238 // additional constructors that omit the last element
Gurvan 0:9cb9445a11f0 239 template <typename T> inline vec2<T>::vec2(const vec3<T>& v) : x(v.x), y(v.y) {}
Gurvan 0:9cb9445a11f0 240 template <typename T> inline vec2<T>::vec2(const vec4<T>& v) : x(v.x), y(v.y) {}
Gurvan 0:9cb9445a11f0 241 template <typename T> inline vec3<T>::vec3(const vec4<T>& v) : x(v.x), y(v.y), z(v.z) {}
Gurvan 0:9cb9445a11f0 242
Gurvan 0:9cb9445a11f0 243 #define VEC_QUAT_FUNC_TEMPLATE(CLASS, COUNT) \
Gurvan 0:9cb9445a11f0 244 template <typename T> \
Gurvan 0:9cb9445a11f0 245 inline T dot(const CLASS & u, const CLASS & v) \
Gurvan 0:9cb9445a11f0 246 { \
Gurvan 0:9cb9445a11f0 247 const T *a = u; \
Gurvan 0:9cb9445a11f0 248 const T *b = v; \
Gurvan 0:9cb9445a11f0 249 using namespace detail; \
Gurvan 0:9cb9445a11f0 250 return multiply_accumulate(COUNT, a, b); \
Gurvan 0:9cb9445a11f0 251 } \
Gurvan 0:9cb9445a11f0 252 template <typename T> \
Gurvan 0:9cb9445a11f0 253 inline T length(const CLASS & v) \
Gurvan 0:9cb9445a11f0 254 { \
Gurvan 0:9cb9445a11f0 255 return sqrt(dot(v, v)); \
Gurvan 0:9cb9445a11f0 256 } \
Gurvan 0:9cb9445a11f0 257 template <typename T> inline CLASS normalize(const CLASS & v) \
Gurvan 0:9cb9445a11f0 258 { \
Gurvan 0:9cb9445a11f0 259 return v * rsqrt(dot(v, v)); \
Gurvan 0:9cb9445a11f0 260 } \
Gurvan 0:9cb9445a11f0 261 template <typename T> inline CLASS lerp(const CLASS & u, const CLASS & v, const T x) \
Gurvan 0:9cb9445a11f0 262 { \
Gurvan 0:9cb9445a11f0 263 return u * (T(1) - x) + v * x; \
Gurvan 0:9cb9445a11f0 264 }
Gurvan 0:9cb9445a11f0 265
Gurvan 0:9cb9445a11f0 266 VEC_QUAT_FUNC_TEMPLATE(vec2<T>, 2)
Gurvan 0:9cb9445a11f0 267 VEC_QUAT_FUNC_TEMPLATE(vec3<T>, 3)
Gurvan 0:9cb9445a11f0 268 VEC_QUAT_FUNC_TEMPLATE(vec4<T>, 4)
Gurvan 0:9cb9445a11f0 269 VEC_QUAT_FUNC_TEMPLATE(quat<T>, 4)
Gurvan 0:9cb9445a11f0 270
Gurvan 0:9cb9445a11f0 271 #define VEC_FUNC_TEMPLATE(CLASS) \
Gurvan 0:9cb9445a11f0 272 template <typename T> inline CLASS reflect(const CLASS & I, const CLASS & N) \
Gurvan 0:9cb9445a11f0 273 { \
Gurvan 0:9cb9445a11f0 274 return I - T(2) * dot(N, I) * N; \
Gurvan 0:9cb9445a11f0 275 } \
Gurvan 0:9cb9445a11f0 276 template <typename T> inline CLASS refract(const CLASS & I, const CLASS & N, T eta) \
Gurvan 0:9cb9445a11f0 277 { \
Gurvan 0:9cb9445a11f0 278 const T d = dot(N, I); \
Gurvan 0:9cb9445a11f0 279 const T k = T(1) - eta * eta * (T(1) - d * d); \
Gurvan 0:9cb9445a11f0 280 if ( k < T(0) ) \
Gurvan 0:9cb9445a11f0 281 return CLASS(T(0)); \
Gurvan 0:9cb9445a11f0 282 else \
Gurvan 0:9cb9445a11f0 283 return eta * I - (eta * d + static_cast<T>(sqrt(k))) * N; \
Gurvan 0:9cb9445a11f0 284 }
Gurvan 0:9cb9445a11f0 285
Gurvan 0:9cb9445a11f0 286 VEC_FUNC_TEMPLATE(vec2<T>)
Gurvan 0:9cb9445a11f0 287 VEC_FUNC_TEMPLATE(vec3<T>)
Gurvan 0:9cb9445a11f0 288 VEC_FUNC_TEMPLATE(vec4<T>)
Gurvan 0:9cb9445a11f0 289
Gurvan 0:9cb9445a11f0 290 template <typename T> inline T lerp(const T & u, const T & v, const T x)
Gurvan 0:9cb9445a11f0 291 {
Gurvan 0:9cb9445a11f0 292 return dot(vec2<T>(u, v), vec2<T>((T(1) - x), x));
Gurvan 0:9cb9445a11f0 293 }
Gurvan 0:9cb9445a11f0 294
Gurvan 0:9cb9445a11f0 295 template <typename T> inline vec3<T> cross(const vec3<T>& u, const vec3<T>& v)
Gurvan 0:9cb9445a11f0 296 {
Gurvan 0:9cb9445a11f0 297 return vec3<T>(
Gurvan 0:9cb9445a11f0 298 dot(vec2<T>(u.y, -v.y), vec2<T>(v.z, u.z)),
Gurvan 0:9cb9445a11f0 299 dot(vec2<T>(u.z, -v.z), vec2<T>(v.x, u.x)),
Gurvan 0:9cb9445a11f0 300 dot(vec2<T>(u.x, -v.x), vec2<T>(v.y, u.y)));
Gurvan 0:9cb9445a11f0 301 }
Gurvan 0:9cb9445a11f0 302
Gurvan 0:9cb9445a11f0 303
Gurvan 0:9cb9445a11f0 304 #define MATRIX_COL4(SRC, C) \
Gurvan 0:9cb9445a11f0 305 vec4<T>(SRC.elem[0][C], SRC.elem[1][C], SRC.elem[2][C], SRC.elem[3][C])
Gurvan 0:9cb9445a11f0 306
Gurvan 0:9cb9445a11f0 307 #define MATRIX_ROW4(SRC, R) \
Gurvan 0:9cb9445a11f0 308 vec4<T>(SRC.elem[R][0], SRC.elem[R][1], SRC.elem[R][2], SRC.elem[R][3])
Gurvan 0:9cb9445a11f0 309
Gurvan 0:9cb9445a11f0 310 #define MATRIX_COL3(SRC, C) \
Gurvan 0:9cb9445a11f0 311 vec3<T>(SRC.elem[0][C], SRC.elem[1][C], SRC.elem[2][C])
Gurvan 0:9cb9445a11f0 312
Gurvan 0:9cb9445a11f0 313 #define MATRIX_ROW3(SRC, R) \
Gurvan 0:9cb9445a11f0 314 vec3<T>(SRC.elem[R][0], SRC.elem[R][1], SRC.elem[R][2])
Gurvan 0:9cb9445a11f0 315
Gurvan 0:9cb9445a11f0 316 #define MATRIX_COL2(SRC, C) \
Gurvan 0:9cb9445a11f0 317 vec2<T>(SRC.elem[0][C], SRC.elem[1][C])
Gurvan 0:9cb9445a11f0 318
Gurvan 0:9cb9445a11f0 319 #define MATRIX_ROW2(SRC, R) \
Gurvan 0:9cb9445a11f0 320 vec2<T>(SRC.elem[R][0], SRC.elem[R][1])
Gurvan 0:9cb9445a11f0 321
Gurvan 0:9cb9445a11f0 322 #define MOP_M_MATRIX_MULTIPLY(CLASS, SIZE) \
Gurvan 0:9cb9445a11f0 323 CLASS & operator *= (const CLASS & rhs) \
Gurvan 0:9cb9445a11f0 324 { \
Gurvan 0:9cb9445a11f0 325 CLASS result; \
Gurvan 0:9cb9445a11f0 326 for (int r = 0; r < SIZE; ++r) \
Gurvan 0:9cb9445a11f0 327 for (int c = 0; c < SIZE; ++c) \
Gurvan 0:9cb9445a11f0 328 result.elem[r][c] = dot( \
Gurvan 0:9cb9445a11f0 329 MATRIX_ROW ## SIZE((*this), r), \
Gurvan 0:9cb9445a11f0 330 MATRIX_COL ## SIZE(rhs, c)); \
Gurvan 0:9cb9445a11f0 331 return (*this) = result; \
Gurvan 0:9cb9445a11f0 332 }
Gurvan 0:9cb9445a11f0 333
Gurvan 0:9cb9445a11f0 334 #define MATRIX_CONSTRUCTOR_FROM_T(CLASS, SIZE) \
Gurvan 0:9cb9445a11f0 335 explicit CLASS(const T v) \
Gurvan 0:9cb9445a11f0 336 { \
Gurvan 0:9cb9445a11f0 337 for (int r = 0; r < SIZE; ++r) \
Gurvan 0:9cb9445a11f0 338 for (int c = 0; c < SIZE; ++c) \
Gurvan 0:9cb9445a11f0 339 if (r == c) elem[r][c] = v; \
Gurvan 0:9cb9445a11f0 340 else elem[r][c] = T(0); \
Gurvan 0:9cb9445a11f0 341 }
Gurvan 0:9cb9445a11f0 342
Gurvan 0:9cb9445a11f0 343 #define MATRIX_CONSTRUCTOR_FROM_LOWER(CLASS1, CLASS2, SIZE1, SIZE2) \
Gurvan 0:9cb9445a11f0 344 explicit CLASS1(const CLASS2<T>& m) \
Gurvan 0:9cb9445a11f0 345 { \
Gurvan 0:9cb9445a11f0 346 for (int r = 0; r < SIZE1; ++r) \
Gurvan 0:9cb9445a11f0 347 for (int c = 0; c < SIZE1; ++c) \
Gurvan 0:9cb9445a11f0 348 if (r < SIZE2 && c < SIZE2) elem[r][c] = m.elem[r][c]; \
Gurvan 0:9cb9445a11f0 349 else elem[r][c] = r == c ? T(1) : T(0); \
Gurvan 0:9cb9445a11f0 350 }
Gurvan 0:9cb9445a11f0 351
Gurvan 0:9cb9445a11f0 352 #define MATRIX_COMMON(CLASS, SIZE) \
Gurvan 0:9cb9445a11f0 353 COMMON_OPERATORS(CLASS, SIZE*SIZE) \
Gurvan 0:9cb9445a11f0 354 MOP_M_MATRIX_MULTIPLY(CLASS, SIZE) \
Gurvan 0:9cb9445a11f0 355 MATRIX_CONSTRUCTOR_FROM_T(CLASS, SIZE) \
Gurvan 0:9cb9445a11f0 356 operator const T* () const { return (const T*) elem; } \
Gurvan 0:9cb9445a11f0 357 operator T* () { return (T*) elem; }
Gurvan 0:9cb9445a11f0 358
Gurvan 0:9cb9445a11f0 359 template <typename T> struct mat2;
Gurvan 0:9cb9445a11f0 360 template <typename T> struct mat3;
Gurvan 0:9cb9445a11f0 361 template <typename T> struct mat4;
Gurvan 0:9cb9445a11f0 362
Gurvan 0:9cb9445a11f0 363 template <typename T>
Gurvan 0:9cb9445a11f0 364 struct mat2 {
Gurvan 0:9cb9445a11f0 365 T elem[2][2];
Gurvan 0:9cb9445a11f0 366
Gurvan 0:9cb9445a11f0 367 mat2() {}
Gurvan 0:9cb9445a11f0 368
Gurvan 0:9cb9445a11f0 369 explicit mat2(
Gurvan 0:9cb9445a11f0 370 const T m00, const T m01,
Gurvan 0:9cb9445a11f0 371 const T m10, const T m11)
Gurvan 0:9cb9445a11f0 372 {
Gurvan 0:9cb9445a11f0 373 elem[0][0] = m00; elem[0][1] = m01;
Gurvan 0:9cb9445a11f0 374 elem[1][0] = m10; elem[1][1] = m11;
Gurvan 0:9cb9445a11f0 375 }
Gurvan 0:9cb9445a11f0 376
Gurvan 0:9cb9445a11f0 377 explicit mat2(const vec2<T>& v0, const vec2<T>& v1)
Gurvan 0:9cb9445a11f0 378 {
Gurvan 0:9cb9445a11f0 379 elem[0][0] = v0[0];
Gurvan 0:9cb9445a11f0 380 elem[1][0] = v0[1];
Gurvan 0:9cb9445a11f0 381 elem[0][1] = v1[0];
Gurvan 0:9cb9445a11f0 382 elem[1][1] = v1[1];
Gurvan 0:9cb9445a11f0 383 }
Gurvan 0:9cb9445a11f0 384
Gurvan 0:9cb9445a11f0 385 explicit mat2(const mat3<T>& m);
Gurvan 0:9cb9445a11f0 386
Gurvan 0:9cb9445a11f0 387 MATRIX_COMMON(mat2, 2)
Gurvan 0:9cb9445a11f0 388 };
Gurvan 0:9cb9445a11f0 389
Gurvan 0:9cb9445a11f0 390 template <typename T>
Gurvan 0:9cb9445a11f0 391 struct mat3 {
Gurvan 0:9cb9445a11f0 392 T elem[3][3];
Gurvan 0:9cb9445a11f0 393
Gurvan 0:9cb9445a11f0 394 mat3() {}
Gurvan 0:9cb9445a11f0 395
Gurvan 0:9cb9445a11f0 396 explicit mat3(
Gurvan 0:9cb9445a11f0 397 const T m00, const T m01, const T m02,
Gurvan 0:9cb9445a11f0 398 const T m10, const T m11, const T m12,
Gurvan 0:9cb9445a11f0 399 const T m20, const T m21, const T m22)
Gurvan 0:9cb9445a11f0 400 {
Gurvan 0:9cb9445a11f0 401 elem[0][0] = m00; elem[0][1] = m01; elem[0][2] = m02;
Gurvan 0:9cb9445a11f0 402 elem[1][0] = m10; elem[1][1] = m11; elem[1][2] = m12;
Gurvan 0:9cb9445a11f0 403 elem[2][0] = m20; elem[2][1] = m21; elem[2][2] = m22;
Gurvan 0:9cb9445a11f0 404 }
Gurvan 0:9cb9445a11f0 405
Gurvan 0:9cb9445a11f0 406 explicit mat3(const vec3<T>& v0, const vec3<T>& v1, const vec3<T>& v2)
Gurvan 0:9cb9445a11f0 407 {
Gurvan 0:9cb9445a11f0 408 elem[0][0] = v0[0];
Gurvan 0:9cb9445a11f0 409 elem[1][0] = v0[1];
Gurvan 0:9cb9445a11f0 410 elem[2][0] = v0[2];
Gurvan 0:9cb9445a11f0 411 elem[0][1] = v1[0];
Gurvan 0:9cb9445a11f0 412 elem[1][1] = v1[1];
Gurvan 0:9cb9445a11f0 413 elem[2][1] = v1[2];
Gurvan 0:9cb9445a11f0 414 elem[0][2] = v2[0];
Gurvan 0:9cb9445a11f0 415 elem[1][2] = v2[1];
Gurvan 0:9cb9445a11f0 416 elem[2][2] = v2[2];
Gurvan 0:9cb9445a11f0 417 }
Gurvan 0:9cb9445a11f0 418
Gurvan 0:9cb9445a11f0 419 explicit mat3(const mat4<T>& m);
Gurvan 0:9cb9445a11f0 420
Gurvan 0:9cb9445a11f0 421 MATRIX_CONSTRUCTOR_FROM_LOWER(mat3, mat2, 3, 2)
Gurvan 0:9cb9445a11f0 422 MATRIX_COMMON(mat3, 3)
Gurvan 0:9cb9445a11f0 423 };
Gurvan 0:9cb9445a11f0 424
Gurvan 0:9cb9445a11f0 425 template <typename T>
Gurvan 0:9cb9445a11f0 426 struct mat4 {
Gurvan 0:9cb9445a11f0 427 T elem[4][4];
Gurvan 0:9cb9445a11f0 428
Gurvan 0:9cb9445a11f0 429 mat4() {}
Gurvan 0:9cb9445a11f0 430
Gurvan 0:9cb9445a11f0 431 explicit mat4(
Gurvan 0:9cb9445a11f0 432 const T m00, const T m01, const T m02, const T m03,
Gurvan 0:9cb9445a11f0 433 const T m10, const T m11, const T m12, const T m13,
Gurvan 0:9cb9445a11f0 434 const T m20, const T m21, const T m22, const T m23,
Gurvan 0:9cb9445a11f0 435 const T m30, const T m31, const T m32, const T m33)
Gurvan 0:9cb9445a11f0 436 {
Gurvan 0:9cb9445a11f0 437 elem[0][0] = m00; elem[0][1] = m01; elem[0][2] = m02; elem[0][3] = m03;
Gurvan 0:9cb9445a11f0 438 elem[1][0] = m10; elem[1][1] = m11; elem[1][2] = m12; elem[1][3] = m13;
Gurvan 0:9cb9445a11f0 439 elem[2][0] = m20; elem[2][1] = m21; elem[2][2] = m22; elem[2][3] = m23;
Gurvan 0:9cb9445a11f0 440 elem[3][0] = m30; elem[3][1] = m31; elem[3][2] = m32; elem[3][3] = m33;
Gurvan 0:9cb9445a11f0 441 }
Gurvan 0:9cb9445a11f0 442
Gurvan 0:9cb9445a11f0 443 explicit mat4(const vec4<T>& v0, const vec4<T>& v1, const vec4<T>& v2, const vec4<T>& v3)
Gurvan 0:9cb9445a11f0 444 {
Gurvan 0:9cb9445a11f0 445 elem[0][0] = v0[0];
Gurvan 0:9cb9445a11f0 446 elem[1][0] = v0[1];
Gurvan 0:9cb9445a11f0 447 elem[2][0] = v0[2];
Gurvan 0:9cb9445a11f0 448 elem[3][0] = v0[3];
Gurvan 0:9cb9445a11f0 449 elem[0][1] = v1[0];
Gurvan 0:9cb9445a11f0 450 elem[1][1] = v1[1];
Gurvan 0:9cb9445a11f0 451 elem[2][1] = v1[2];
Gurvan 0:9cb9445a11f0 452 elem[3][1] = v1[3];
Gurvan 0:9cb9445a11f0 453 elem[0][2] = v2[0];
Gurvan 0:9cb9445a11f0 454 elem[1][2] = v2[1];
Gurvan 0:9cb9445a11f0 455 elem[2][2] = v2[2];
Gurvan 0:9cb9445a11f0 456 elem[3][2] = v2[3];
Gurvan 0:9cb9445a11f0 457 elem[0][3] = v3[0];
Gurvan 0:9cb9445a11f0 458 elem[1][3] = v3[1];
Gurvan 0:9cb9445a11f0 459 elem[2][3] = v3[2];
Gurvan 0:9cb9445a11f0 460 elem[3][3] = v3[3];
Gurvan 0:9cb9445a11f0 461 }
Gurvan 0:9cb9445a11f0 462
Gurvan 0:9cb9445a11f0 463 MATRIX_CONSTRUCTOR_FROM_LOWER(mat4, mat3, 4, 3)
Gurvan 0:9cb9445a11f0 464 MATRIX_COMMON(mat4, 4)
Gurvan 0:9cb9445a11f0 465 };
Gurvan 0:9cb9445a11f0 466
Gurvan 0:9cb9445a11f0 467 #define MATRIX_CONSTRUCTOR_FROM_HIGHER(CLASS1, CLASS2, SIZE) \
Gurvan 0:9cb9445a11f0 468 template <typename T> \
Gurvan 0:9cb9445a11f0 469 inline CLASS1<T>::CLASS1(const CLASS2<T>& m) \
Gurvan 0:9cb9445a11f0 470 { \
Gurvan 0:9cb9445a11f0 471 for (int r = 0; r < SIZE; ++r) \
Gurvan 0:9cb9445a11f0 472 for (int c = 0; c < SIZE; ++c) \
Gurvan 0:9cb9445a11f0 473 elem[r][c] = m.elem[r][c]; \
Gurvan 0:9cb9445a11f0 474 }
Gurvan 0:9cb9445a11f0 475
Gurvan 0:9cb9445a11f0 476 MATRIX_CONSTRUCTOR_FROM_HIGHER(mat2, mat3, 2)
Gurvan 0:9cb9445a11f0 477 MATRIX_CONSTRUCTOR_FROM_HIGHER(mat3, mat4, 3)
Gurvan 0:9cb9445a11f0 478
Gurvan 0:9cb9445a11f0 479 #define MAT_FUNC_TEMPLATE(CLASS, SIZE) \
Gurvan 0:9cb9445a11f0 480 template <typename T> \
Gurvan 0:9cb9445a11f0 481 inline CLASS transpose(const CLASS & m) \
Gurvan 0:9cb9445a11f0 482 { \
Gurvan 0:9cb9445a11f0 483 CLASS result; \
Gurvan 0:9cb9445a11f0 484 for (int r = 0; r < SIZE; ++r) \
Gurvan 0:9cb9445a11f0 485 for (int c = 0; c < SIZE; ++c) \
Gurvan 0:9cb9445a11f0 486 result.elem[r][c] = m.elem[c][r]; \
Gurvan 0:9cb9445a11f0 487 return result; \
Gurvan 0:9cb9445a11f0 488 } \
Gurvan 0:9cb9445a11f0 489 template <typename T> \
Gurvan 0:9cb9445a11f0 490 inline CLASS identity ## SIZE() \
Gurvan 0:9cb9445a11f0 491 { \
Gurvan 0:9cb9445a11f0 492 CLASS result; \
Gurvan 0:9cb9445a11f0 493 for (int r = 0; r < SIZE; ++r) \
Gurvan 0:9cb9445a11f0 494 for (int c = 0; c < SIZE; ++c) \
Gurvan 0:9cb9445a11f0 495 result.elem[r][c] = r == c ? T(1) : T(0); \
Gurvan 0:9cb9445a11f0 496 return result; \
Gurvan 0:9cb9445a11f0 497 } \
Gurvan 0:9cb9445a11f0 498 template <typename T> \
Gurvan 0:9cb9445a11f0 499 inline T trace(const CLASS & m) \
Gurvan 0:9cb9445a11f0 500 { \
Gurvan 0:9cb9445a11f0 501 T result = T(0); \
Gurvan 0:9cb9445a11f0 502 for (int i = 0; i < SIZE; ++i) \
Gurvan 0:9cb9445a11f0 503 result += m.elem[i][i]; \
Gurvan 0:9cb9445a11f0 504 return result; \
Gurvan 0:9cb9445a11f0 505 }
Gurvan 0:9cb9445a11f0 506
Gurvan 0:9cb9445a11f0 507 MAT_FUNC_TEMPLATE(mat2<T>, 2)
Gurvan 0:9cb9445a11f0 508 MAT_FUNC_TEMPLATE(mat3<T>, 3)
Gurvan 0:9cb9445a11f0 509 MAT_FUNC_TEMPLATE(mat4<T>, 4)
Gurvan 0:9cb9445a11f0 510
Gurvan 0:9cb9445a11f0 511 #define MAT_FUNC_MINOR_TEMPLATE(CLASS1, CLASS2, SIZE) \
Gurvan 0:9cb9445a11f0 512 template <typename T> \
Gurvan 0:9cb9445a11f0 513 inline CLASS2 minor(const CLASS1 & m, int _r = SIZE, int _c = SIZE) { \
Gurvan 0:9cb9445a11f0 514 CLASS2 result; \
Gurvan 0:9cb9445a11f0 515 for (int r = 0; r < SIZE - 1; ++r) \
Gurvan 0:9cb9445a11f0 516 for (int c = 0; c < SIZE - 1; ++c) { \
Gurvan 0:9cb9445a11f0 517 int rs = r >= _r ? 1 : 0; \
Gurvan 0:9cb9445a11f0 518 int cs = c >= _c ? 1 : 0; \
Gurvan 0:9cb9445a11f0 519 result.elem[r][c] = m.elem[r + rs][c + cs]; \
Gurvan 0:9cb9445a11f0 520 } \
Gurvan 0:9cb9445a11f0 521 return result; \
Gurvan 0:9cb9445a11f0 522 }
Gurvan 0:9cb9445a11f0 523
Gurvan 0:9cb9445a11f0 524 MAT_FUNC_MINOR_TEMPLATE(mat3<T>, mat2<T>, 3)
Gurvan 0:9cb9445a11f0 525 MAT_FUNC_MINOR_TEMPLATE(mat4<T>, mat3<T>, 4)
Gurvan 0:9cb9445a11f0 526
Gurvan 0:9cb9445a11f0 527 template <typename T>
Gurvan 0:9cb9445a11f0 528 inline T det(const mat2<T>& m)
Gurvan 0:9cb9445a11f0 529 {
Gurvan 0:9cb9445a11f0 530 return dot(
Gurvan 0:9cb9445a11f0 531 vec2<T>(m.elem[0][0], -m.elem[0][1]),
Gurvan 0:9cb9445a11f0 532 vec2<T>(m.elem[1][1], m.elem[1][0]));
Gurvan 0:9cb9445a11f0 533 }
Gurvan 0:9cb9445a11f0 534
Gurvan 0:9cb9445a11f0 535 template <typename T>
Gurvan 0:9cb9445a11f0 536 inline T det(const mat3<T>& m)
Gurvan 0:9cb9445a11f0 537 {
Gurvan 0:9cb9445a11f0 538 return dot(cross(MATRIX_COL3(m, 0), MATRIX_COL3(m, 1)), MATRIX_COL3(m, 2));
Gurvan 0:9cb9445a11f0 539 }
Gurvan 0:9cb9445a11f0 540
Gurvan 0:9cb9445a11f0 541 template <typename T>
Gurvan 0:9cb9445a11f0 542 inline T det(const mat4<T>& m)
Gurvan 0:9cb9445a11f0 543 {
Gurvan 0:9cb9445a11f0 544 vec4<T> b;
Gurvan 0:9cb9445a11f0 545 for (int i = 0; i < 4; ++i)
Gurvan 0:9cb9445a11f0 546 b[i] = (i & 1 ? -1 : 1) * det(minor(m, 0, i));
Gurvan 0:9cb9445a11f0 547 return dot(MATRIX_ROW4(m, 0), b);
Gurvan 0:9cb9445a11f0 548 }
Gurvan 0:9cb9445a11f0 549
Gurvan 0:9cb9445a11f0 550 #define MAT_ADJOINT_TEMPLATE(CLASS, SIZE) \
Gurvan 0:9cb9445a11f0 551 template <typename T> \
Gurvan 0:9cb9445a11f0 552 inline CLASS adjoint(const CLASS & m) \
Gurvan 0:9cb9445a11f0 553 { \
Gurvan 0:9cb9445a11f0 554 CLASS result; \
Gurvan 0:9cb9445a11f0 555 for (int r = 0; r < SIZE; ++r) \
Gurvan 0:9cb9445a11f0 556 for (int c = 0; c < SIZE; ++c) \
Gurvan 0:9cb9445a11f0 557 result.elem[r][c] = ((r + c) & 1 ? -1 : 1) * det(minor(m, c, r)); \
Gurvan 0:9cb9445a11f0 558 return result; \
Gurvan 0:9cb9445a11f0 559 }
Gurvan 0:9cb9445a11f0 560
Gurvan 0:9cb9445a11f0 561 MAT_ADJOINT_TEMPLATE(mat3<T>, 3)
Gurvan 0:9cb9445a11f0 562 MAT_ADJOINT_TEMPLATE(mat4<T>, 4)
Gurvan 0:9cb9445a11f0 563
Gurvan 0:9cb9445a11f0 564 template <typename T>
Gurvan 0:9cb9445a11f0 565 inline mat2<T> adjoint(const mat2<T> & m)
Gurvan 0:9cb9445a11f0 566 {
Gurvan 0:9cb9445a11f0 567 return mat2<T>(
Gurvan 0:9cb9445a11f0 568 m.elem[1][1], -m.elem[0][1],
Gurvan 0:9cb9445a11f0 569 -m.elem[1][0], m.elem[0][0]
Gurvan 0:9cb9445a11f0 570 );
Gurvan 0:9cb9445a11f0 571 }
Gurvan 0:9cb9445a11f0 572
Gurvan 0:9cb9445a11f0 573 #define MAT_INVERSE_TEMPLATE(CLASS) \
Gurvan 0:9cb9445a11f0 574 template <typename T> \
Gurvan 0:9cb9445a11f0 575 inline CLASS inverse(const CLASS & m) \
Gurvan 0:9cb9445a11f0 576 { \
Gurvan 0:9cb9445a11f0 577 return adjoint(m) * inv(det(m)); \
Gurvan 0:9cb9445a11f0 578 }
Gurvan 0:9cb9445a11f0 579
Gurvan 0:9cb9445a11f0 580 MAT_INVERSE_TEMPLATE(mat2<T>)
Gurvan 0:9cb9445a11f0 581 MAT_INVERSE_TEMPLATE(mat3<T>)
Gurvan 0:9cb9445a11f0 582 MAT_INVERSE_TEMPLATE(mat4<T>)
Gurvan 0:9cb9445a11f0 583
Gurvan 0:9cb9445a11f0 584 #define MAT_VEC_FUNCS_TEMPLATE(MATCLASS, VECCLASS, SIZE) \
Gurvan 0:9cb9445a11f0 585 template <typename T> \
Gurvan 0:9cb9445a11f0 586 inline VECCLASS operator * (const MATCLASS & m, const VECCLASS & v) \
Gurvan 0:9cb9445a11f0 587 { \
Gurvan 0:9cb9445a11f0 588 VECCLASS result; \
Gurvan 0:9cb9445a11f0 589 for (int i = 0; i < SIZE; ++i) {\
Gurvan 0:9cb9445a11f0 590 result[i] = dot(MATRIX_ROW ## SIZE(m, i), v); \
Gurvan 0:9cb9445a11f0 591 } \
Gurvan 0:9cb9445a11f0 592 return result; \
Gurvan 0:9cb9445a11f0 593 } \
Gurvan 0:9cb9445a11f0 594 template <typename T> \
Gurvan 0:9cb9445a11f0 595 inline VECCLASS operator * (const VECCLASS & v, const MATCLASS & m) \
Gurvan 0:9cb9445a11f0 596 { \
Gurvan 0:9cb9445a11f0 597 VECCLASS result; \
Gurvan 0:9cb9445a11f0 598 for (int i = 0; i < SIZE; ++i) \
Gurvan 0:9cb9445a11f0 599 result[i] = dot(v, MATRIX_COL ## SIZE(m, i)); \
Gurvan 0:9cb9445a11f0 600 return result; \
Gurvan 0:9cb9445a11f0 601 }
Gurvan 0:9cb9445a11f0 602
Gurvan 0:9cb9445a11f0 603 MAT_VEC_FUNCS_TEMPLATE(mat2<T>, vec2<T>, 2)
Gurvan 0:9cb9445a11f0 604 MAT_VEC_FUNCS_TEMPLATE(mat3<T>, vec3<T>, 3)
Gurvan 0:9cb9445a11f0 605 MAT_VEC_FUNCS_TEMPLATE(mat4<T>, vec4<T>, 4)
Gurvan 0:9cb9445a11f0 606
Gurvan 0:9cb9445a11f0 607 // Returns the inverse of a 4x4 matrix. It is assumed that the matrix passed
Gurvan 0:9cb9445a11f0 608 // as argument describes a rigid-body transformation.
Gurvan 0:9cb9445a11f0 609 template <typename T>
Gurvan 0:9cb9445a11f0 610 inline mat4<T> fast_inverse(const mat4<T>& m)
Gurvan 0:9cb9445a11f0 611 {
Gurvan 0:9cb9445a11f0 612 const vec3<T> t = MATRIX_COL3(m, 3);
Gurvan 0:9cb9445a11f0 613 const T tx = -dot(MATRIX_COL3(m, 0), t);
Gurvan 0:9cb9445a11f0 614 const T ty = -dot(MATRIX_COL3(m, 1), t);
Gurvan 0:9cb9445a11f0 615 const T tz = -dot(MATRIX_COL3(m, 2), t);
Gurvan 0:9cb9445a11f0 616
Gurvan 0:9cb9445a11f0 617 return mat4<T>(
Gurvan 0:9cb9445a11f0 618 m.elem[0][0], m.elem[1][0], m.elem[2][0], tx,
Gurvan 0:9cb9445a11f0 619 m.elem[0][1], m.elem[1][1], m.elem[2][1], ty,
Gurvan 0:9cb9445a11f0 620 m.elem[0][2], m.elem[1][2], m.elem[2][2], tz,
Gurvan 0:9cb9445a11f0 621 T(0), T(0), T(0), T(1)
Gurvan 0:9cb9445a11f0 622 );
Gurvan 0:9cb9445a11f0 623 }
Gurvan 0:9cb9445a11f0 624
Gurvan 0:9cb9445a11f0 625 // Transformations for points and vectors. Potentially faster than a full
Gurvan 0:9cb9445a11f0 626 // matrix * vector multiplication
Gurvan 0:9cb9445a11f0 627
Gurvan 0:9cb9445a11f0 628 #define MAT_TRANFORMS_TEMPLATE(MATCLASS, VECCLASS, VECSIZE) \
Gurvan 0:9cb9445a11f0 629 /* computes vec3<T>(m * vec4<T>(v, 0.0)) */ \
Gurvan 0:9cb9445a11f0 630 template <typename T> \
Gurvan 0:9cb9445a11f0 631 inline VECCLASS transform_vector(const MATCLASS & m, const VECCLASS & v) \
Gurvan 0:9cb9445a11f0 632 { \
Gurvan 0:9cb9445a11f0 633 VECCLASS result; \
Gurvan 0:9cb9445a11f0 634 for (int i = 0; i < VECSIZE; ++i) \
Gurvan 0:9cb9445a11f0 635 result[i] = dot(MATRIX_ROW ## VECSIZE(m, i), v); \
Gurvan 0:9cb9445a11f0 636 return result;\
Gurvan 0:9cb9445a11f0 637 } \
Gurvan 0:9cb9445a11f0 638 /* computes vec3(m * vec4(v, 1.0)) */ \
Gurvan 0:9cb9445a11f0 639 template <typename T> \
Gurvan 0:9cb9445a11f0 640 inline VECCLASS transform_point(const MATCLASS & m, const VECCLASS & v) \
Gurvan 0:9cb9445a11f0 641 { \
Gurvan 0:9cb9445a11f0 642 /*return transform_vector(m, v) + MATRIX_ROW ## VECSIZE(m, VECSIZE); */\
Gurvan 0:9cb9445a11f0 643 VECCLASS result; \
Gurvan 0:9cb9445a11f0 644 for (int i = 0; i < VECSIZE; ++i) \
Gurvan 0:9cb9445a11f0 645 result[i] = dot(MATRIX_ROW ## VECSIZE(m, i), v) + m.elem[i][VECSIZE]; \
Gurvan 0:9cb9445a11f0 646 return result; \
Gurvan 0:9cb9445a11f0 647 } \
Gurvan 0:9cb9445a11f0 648 /* computes VECCLASS(transpose(m) * vec4<T>(v, 0.0)) */ \
Gurvan 0:9cb9445a11f0 649 template <typename T> \
Gurvan 0:9cb9445a11f0 650 inline VECCLASS transform_vector_transpose(const MATCLASS & m, const VECCLASS& v) \
Gurvan 0:9cb9445a11f0 651 { \
Gurvan 0:9cb9445a11f0 652 VECCLASS result; \
Gurvan 0:9cb9445a11f0 653 for (int i = 0; i < VECSIZE; ++i) \
Gurvan 0:9cb9445a11f0 654 result[i] = dot(MATRIX_COL ## VECSIZE(m, i), v); \
Gurvan 0:9cb9445a11f0 655 return result; \
Gurvan 0:9cb9445a11f0 656 } \
Gurvan 0:9cb9445a11f0 657 /* computes VECCLASS(transpose(m) * vec4<T>(v, 1.0)) */ \
Gurvan 0:9cb9445a11f0 658 template <typename T> \
Gurvan 0:9cb9445a11f0 659 inline VECCLASS transform_point_transpose(const MATCLASS & m, const VECCLASS& v) \
Gurvan 0:9cb9445a11f0 660 { \
Gurvan 0:9cb9445a11f0 661 /*return transform_vector_transpose(m, v) + MATRIX_COL ## VECSIZE(m, VECSIZE); */\
Gurvan 0:9cb9445a11f0 662 VECCLASS result; \
Gurvan 0:9cb9445a11f0 663 for (int i = 0; i < VECSIZE; ++i) \
Gurvan 0:9cb9445a11f0 664 result[i] = dot(MATRIX_COL ## VECSIZE(m, i), v) + m.elem[VECSIZE][i]; \
Gurvan 0:9cb9445a11f0 665 return result; \
Gurvan 0:9cb9445a11f0 666 }
Gurvan 0:9cb9445a11f0 667
Gurvan 0:9cb9445a11f0 668 MAT_TRANFORMS_TEMPLATE(mat4<T>, vec3<T>, 3)
Gurvan 0:9cb9445a11f0 669 MAT_TRANFORMS_TEMPLATE(mat3<T>, vec2<T>, 2)
Gurvan 0:9cb9445a11f0 670
Gurvan 0:9cb9445a11f0 671 #define MAT_OUTERPRODUCT_TEMPLATE(MATCLASS, VECCLASS, MATSIZE) \
Gurvan 0:9cb9445a11f0 672 template <typename T> \
Gurvan 0:9cb9445a11f0 673 inline MATCLASS outer_product(const VECCLASS & v1, const VECCLASS & v2) \
Gurvan 0:9cb9445a11f0 674 { \
Gurvan 0:9cb9445a11f0 675 MATCLASS r; \
Gurvan 0:9cb9445a11f0 676 for ( int j = 0; j < MATSIZE; ++j ) \
Gurvan 0:9cb9445a11f0 677 for ( int k = 0; k < MATSIZE; ++k ) \
Gurvan 0:9cb9445a11f0 678 r.elem[j][k] = v1[j] * v2[k]; \
Gurvan 0:9cb9445a11f0 679 return r; \
Gurvan 0:9cb9445a11f0 680 }
Gurvan 0:9cb9445a11f0 681
Gurvan 0:9cb9445a11f0 682 MAT_OUTERPRODUCT_TEMPLATE(mat4<T>, vec4<T>, 4)
Gurvan 0:9cb9445a11f0 683 MAT_OUTERPRODUCT_TEMPLATE(mat3<T>, vec3<T>, 3)
Gurvan 0:9cb9445a11f0 684 MAT_OUTERPRODUCT_TEMPLATE(mat2<T>, vec2<T>, 2)
Gurvan 0:9cb9445a11f0 685
Gurvan 0:9cb9445a11f0 686 template <typename T>
Gurvan 0:9cb9445a11f0 687 inline mat4<T> translation_matrix(const T x, const T y, const T z)
Gurvan 0:9cb9445a11f0 688 {
Gurvan 0:9cb9445a11f0 689 mat4<T> r(T(1));
Gurvan 0:9cb9445a11f0 690 r.elem[0][3] = x;
Gurvan 0:9cb9445a11f0 691 r.elem[1][3] = y;
Gurvan 0:9cb9445a11f0 692 r.elem[2][3] = z;
Gurvan 0:9cb9445a11f0 693 return r;
Gurvan 0:9cb9445a11f0 694 }
Gurvan 0:9cb9445a11f0 695
Gurvan 0:9cb9445a11f0 696 template <typename T>
Gurvan 0:9cb9445a11f0 697 inline mat4<T> translation_matrix(const vec3<T>& v)
Gurvan 0:9cb9445a11f0 698 {
Gurvan 0:9cb9445a11f0 699 return translation_matrix(v.x, v.y, v.z);
Gurvan 0:9cb9445a11f0 700 }
Gurvan 0:9cb9445a11f0 701
Gurvan 0:9cb9445a11f0 702 template <typename T>
Gurvan 0:9cb9445a11f0 703 inline mat4<T> scaling_matrix(const T x, const T y, const T z)
Gurvan 0:9cb9445a11f0 704 {
Gurvan 0:9cb9445a11f0 705 mat4<T> r(T(0));
Gurvan 0:9cb9445a11f0 706 r.elem[0][0] = x;
Gurvan 0:9cb9445a11f0 707 r.elem[1][1] = y;
Gurvan 0:9cb9445a11f0 708 r.elem[2][2] = z;
Gurvan 0:9cb9445a11f0 709 r.elem[3][3] = T(1);
Gurvan 0:9cb9445a11f0 710 return r;
Gurvan 0:9cb9445a11f0 711 }
Gurvan 0:9cb9445a11f0 712
Gurvan 0:9cb9445a11f0 713 template <typename T>
Gurvan 0:9cb9445a11f0 714 inline mat4<T> scaling_matrix(const vec3<T>& v)
Gurvan 0:9cb9445a11f0 715 {
Gurvan 0:9cb9445a11f0 716 return scaling_matrix(v.x, v.y, v.z);
Gurvan 0:9cb9445a11f0 717 }
Gurvan 0:9cb9445a11f0 718
Gurvan 0:9cb9445a11f0 719 template <typename T>
Gurvan 0:9cb9445a11f0 720 inline mat4<T> rotation_matrix(const T angle, const vec3<T>& v)
Gurvan 0:9cb9445a11f0 721 {
Gurvan 0:9cb9445a11f0 722 const T a = angle * T(M_PI/180) ;
Gurvan 0:9cb9445a11f0 723 const vec3<T> u = normalize(v);
Gurvan 0:9cb9445a11f0 724
Gurvan 0:9cb9445a11f0 725 const mat3<T> S(
Gurvan 0:9cb9445a11f0 726 T(0), -u[2], u[1],
Gurvan 0:9cb9445a11f0 727 u[2], T(0), -u[0],
Gurvan 0:9cb9445a11f0 728 -u[1], u[0], T(0)
Gurvan 0:9cb9445a11f0 729 );
Gurvan 0:9cb9445a11f0 730
Gurvan 0:9cb9445a11f0 731 const mat3<T> uut = outer_product(u, u);
Gurvan 0:9cb9445a11f0 732 const mat3<T> R = uut + T(cos(a)) * (identity3<T>() - uut) + T(sin(a)) * S;
Gurvan 0:9cb9445a11f0 733
Gurvan 0:9cb9445a11f0 734 return mat4<T>(R);
Gurvan 0:9cb9445a11f0 735 }
Gurvan 0:9cb9445a11f0 736
Gurvan 0:9cb9445a11f0 737
Gurvan 0:9cb9445a11f0 738 template <typename T>
Gurvan 0:9cb9445a11f0 739 inline mat4<T> rotation_matrix(const T angle, const T x, const T y, const T z)
Gurvan 0:9cb9445a11f0 740 {
Gurvan 0:9cb9445a11f0 741 return rotation_matrix(angle, vec3<T>(x, y, z));
Gurvan 0:9cb9445a11f0 742 }
Gurvan 0:9cb9445a11f0 743
Gurvan 0:9cb9445a11f0 744 // Constructs a shear-matrix that shears component i by factor with
Gurvan 0:9cb9445a11f0 745 // Respect to component j.
Gurvan 0:9cb9445a11f0 746 template <typename T>
Gurvan 0:9cb9445a11f0 747 inline mat4<T> shear_matrix(const int i, const int j, const T factor)
Gurvan 0:9cb9445a11f0 748 {
Gurvan 0:9cb9445a11f0 749 mat4<T> m = identity4<T>();
Gurvan 0:9cb9445a11f0 750 m.elem[i][j] = factor;
Gurvan 0:9cb9445a11f0 751 return m;
Gurvan 0:9cb9445a11f0 752 }
Gurvan 0:9cb9445a11f0 753
Gurvan 0:9cb9445a11f0 754 template <typename T>
Gurvan 0:9cb9445a11f0 755 inline mat4<T> euler(const T head, const T pitch, const T roll)
Gurvan 0:9cb9445a11f0 756 {
Gurvan 0:9cb9445a11f0 757 return rotation_matrix(roll, T(0), T(0), T(1)) *
Gurvan 0:9cb9445a11f0 758 rotation_matrix(pitch, T(1), T(0), T(0)) *
Gurvan 0:9cb9445a11f0 759 rotation_matrix(head, T(0), T(1), T(0));
Gurvan 0:9cb9445a11f0 760 }
Gurvan 0:9cb9445a11f0 761
Gurvan 0:9cb9445a11f0 762 template <typename T>
Gurvan 0:9cb9445a11f0 763 inline mat4<T> frustum_matrix(const T l, const T r, const T b, const T t, const T n, const T f)
Gurvan 0:9cb9445a11f0 764 {
Gurvan 0:9cb9445a11f0 765 return mat4<T>(
Gurvan 0:9cb9445a11f0 766 (2 * n)/(r - l), T(0), (r + l)/(r - l), T(0),
Gurvan 0:9cb9445a11f0 767 T(0), (2 * n)/(t - b), (t + b)/(t - b), T(0),
Gurvan 0:9cb9445a11f0 768 T(0), T(0), -(f + n)/(f - n), -(2 * f * n)/(f - n),
Gurvan 0:9cb9445a11f0 769 T(0), T(0), -T(1), T(0)
Gurvan 0:9cb9445a11f0 770 );
Gurvan 0:9cb9445a11f0 771 }
Gurvan 0:9cb9445a11f0 772
Gurvan 0:9cb9445a11f0 773 template <typename T>
Gurvan 0:9cb9445a11f0 774 inline mat4<T> perspective_matrix(const T fovy, const T aspect, const T zNear, const T zFar)
Gurvan 0:9cb9445a11f0 775 {
Gurvan 0:9cb9445a11f0 776 const T dz = zFar - zNear;
Gurvan 0:9cb9445a11f0 777 const T rad = fovy / T(2) * T(M_PI/180);
Gurvan 0:9cb9445a11f0 778 const T s = sin(rad);
Gurvan 0:9cb9445a11f0 779
Gurvan 0:9cb9445a11f0 780 if ( ( dz == T(0) ) || ( s == T(0) ) || ( aspect == T(0) ) ) {
Gurvan 0:9cb9445a11f0 781 return identity4<T>();
Gurvan 0:9cb9445a11f0 782 }
Gurvan 0:9cb9445a11f0 783
Gurvan 0:9cb9445a11f0 784 const T cot = cos(rad) / s;
Gurvan 0:9cb9445a11f0 785
Gurvan 0:9cb9445a11f0 786 mat4<T> m = identity4<T>();
Gurvan 0:9cb9445a11f0 787 m[0] = cot / aspect;
Gurvan 0:9cb9445a11f0 788 m[5] = cot;
Gurvan 0:9cb9445a11f0 789 m[10] = -(zFar + zNear) / dz;
Gurvan 0:9cb9445a11f0 790 m[14] = T(-1);
Gurvan 0:9cb9445a11f0 791 m[11] = -2 * zNear * zFar / dz;
Gurvan 0:9cb9445a11f0 792 m[15] = T(0);
Gurvan 0:9cb9445a11f0 793
Gurvan 0:9cb9445a11f0 794 return m;
Gurvan 0:9cb9445a11f0 795 }
Gurvan 0:9cb9445a11f0 796
Gurvan 0:9cb9445a11f0 797 template <typename T>
Gurvan 0:9cb9445a11f0 798 inline mat4<T> ortho_matrix(const T l, const T r, const T b, const T t, const T n, const T f)
Gurvan 0:9cb9445a11f0 799 {
Gurvan 0:9cb9445a11f0 800 return mat4<T>(
Gurvan 0:9cb9445a11f0 801 T(2)/(r - l), T(0), T(0), -(r + l)/(r - l),
Gurvan 0:9cb9445a11f0 802 T(0), T(2)/(t - b), T(0), -(t + b)/(t - b),
Gurvan 0:9cb9445a11f0 803 T(0), T(0), -T(2)/(f - n), -(f + n)/(f - n),
Gurvan 0:9cb9445a11f0 804 T(0), T(0), T(0), T(1)
Gurvan 0:9cb9445a11f0 805 );
Gurvan 0:9cb9445a11f0 806 }
Gurvan 0:9cb9445a11f0 807
Gurvan 0:9cb9445a11f0 808 template <typename T>
Gurvan 0:9cb9445a11f0 809 inline mat4<T> lookat_matrix(const vec3<T>& eye, const vec3<T>& center, const vec3<T>& up) {
Gurvan 0:9cb9445a11f0 810 const vec3<T> forward = normalize(center - eye);
Gurvan 0:9cb9445a11f0 811 const vec3<T> side = normalize(cross(forward, up));
Gurvan 0:9cb9445a11f0 812
Gurvan 0:9cb9445a11f0 813 const vec3<T> up2 = cross(side, forward);
Gurvan 0:9cb9445a11f0 814
Gurvan 0:9cb9445a11f0 815 mat4<T> m = identity4<T>();
Gurvan 0:9cb9445a11f0 816
Gurvan 0:9cb9445a11f0 817 m.elem[0][0] = side[0];
Gurvan 0:9cb9445a11f0 818 m.elem[0][1] = side[1];
Gurvan 0:9cb9445a11f0 819 m.elem[0][2] = side[2];
Gurvan 0:9cb9445a11f0 820
Gurvan 0:9cb9445a11f0 821 m.elem[1][0] = up2[0];
Gurvan 0:9cb9445a11f0 822 m.elem[1][1] = up2[1];
Gurvan 0:9cb9445a11f0 823 m.elem[1][2] = up2[2];
Gurvan 0:9cb9445a11f0 824
Gurvan 0:9cb9445a11f0 825 m.elem[2][0] = -forward[0];
Gurvan 0:9cb9445a11f0 826 m.elem[2][1] = -forward[1];
Gurvan 0:9cb9445a11f0 827 m.elem[2][2] = -forward[2];
Gurvan 0:9cb9445a11f0 828
Gurvan 0:9cb9445a11f0 829 return m * translation_matrix(-eye);
Gurvan 0:9cb9445a11f0 830 }
Gurvan 0:9cb9445a11f0 831
Gurvan 0:9cb9445a11f0 832 template <typename T>
Gurvan 0:9cb9445a11f0 833 inline mat4<T> picking_matrix(const T x, const T y, const T dx, const T dy, int viewport[4]) {
Gurvan 0:9cb9445a11f0 834 if (dx <= 0 || dy <= 0) {
Gurvan 0:9cb9445a11f0 835 return identity4<T>();
Gurvan 0:9cb9445a11f0 836 }
Gurvan 0:9cb9445a11f0 837
Gurvan 0:9cb9445a11f0 838 mat4<T> r = translation_matrix((viewport[2] - 2 * (x - viewport[0])) / dx,
Gurvan 0:9cb9445a11f0 839 (viewport[3] - 2 * (y - viewport[1])) / dy, 0);
Gurvan 0:9cb9445a11f0 840 r *= scaling_matrix(viewport[2] / dx, viewport[2] / dy, 1);
Gurvan 0:9cb9445a11f0 841 return r;
Gurvan 0:9cb9445a11f0 842 }
Gurvan 0:9cb9445a11f0 843
Gurvan 0:9cb9445a11f0 844 // Constructs a shadow matrix. q is the light source and p is the plane.
Gurvan 0:9cb9445a11f0 845 template <typename T> inline mat4<T> shadow_matrix(const vec4<T>& q, const vec4<T>& p) {
Gurvan 0:9cb9445a11f0 846 mat4<T> m;
Gurvan 0:9cb9445a11f0 847
Gurvan 0:9cb9445a11f0 848 m.elem[0][0] = p.y * q[1] + p.z * q[2] + p.w * q[3];
Gurvan 0:9cb9445a11f0 849 m.elem[0][1] = -p.y * q[0];
Gurvan 0:9cb9445a11f0 850 m.elem[0][2] = -p.z * q[0];
Gurvan 0:9cb9445a11f0 851 m.elem[0][3] = -p.w * q[0];
Gurvan 0:9cb9445a11f0 852
Gurvan 0:9cb9445a11f0 853 m.elem[1][0] = -p.x * q[1];
Gurvan 0:9cb9445a11f0 854 m.elem[1][1] = p.x * q[0] + p.z * q[2] + p.w * q[3];
Gurvan 0:9cb9445a11f0 855 m.elem[1][2] = -p.z * q[1];
Gurvan 0:9cb9445a11f0 856 m.elem[1][3] = -p.w * q[1];
Gurvan 0:9cb9445a11f0 857
Gurvan 0:9cb9445a11f0 858
Gurvan 0:9cb9445a11f0 859 m.elem[2][0] = -p.x * q[2];
Gurvan 0:9cb9445a11f0 860 m.elem[2][1] = -p.y * q[2];
Gurvan 0:9cb9445a11f0 861 m.elem[2][2] = p.x * q[0] + p.y * q[1] + p.w * q[3];
Gurvan 0:9cb9445a11f0 862 m.elem[2][3] = -p.w * q[2];
Gurvan 0:9cb9445a11f0 863
Gurvan 0:9cb9445a11f0 864 m.elem[3][1] = -p.x * q[3];
Gurvan 0:9cb9445a11f0 865 m.elem[3][2] = -p.y * q[3];
Gurvan 0:9cb9445a11f0 866 m.elem[3][3] = -p.z * q[3];
Gurvan 0:9cb9445a11f0 867 m.elem[3][0] = p.x * q[0] + p.y * q[1] + p.z * q[2];
Gurvan 0:9cb9445a11f0 868
Gurvan 0:9cb9445a11f0 869 return m;
Gurvan 0:9cb9445a11f0 870 }
Gurvan 0:9cb9445a11f0 871
Gurvan 0:9cb9445a11f0 872 // Quaternion class
Gurvan 0:9cb9445a11f0 873 template <typename T>
Gurvan 0:9cb9445a11f0 874 struct quat {
Gurvan 0:9cb9445a11f0 875 vec3<T> v;
Gurvan 0:9cb9445a11f0 876 T w;
Gurvan 0:9cb9445a11f0 877
Gurvan 0:9cb9445a11f0 878 quat() {}
Gurvan 0:9cb9445a11f0 879 quat(const vec3<T>& iv, const T iw) : v(iv), w(iw) {}
Gurvan 0:9cb9445a11f0 880 quat(const T vx, const T vy, const T vz, const T iw) : v(vx, vy, vz), w(iw) {}
Gurvan 0:9cb9445a11f0 881 quat(const vec4<T>& i) : v(i.x, i.y, i.z), w(i.w) {}
Gurvan 0:9cb9445a11f0 882
Gurvan 0:9cb9445a11f0 883 operator const T* () const { return &(v[0]); }
Gurvan 0:9cb9445a11f0 884 operator T* () { return &(v[0]); }
Gurvan 0:9cb9445a11f0 885
Gurvan 0:9cb9445a11f0 886 quat& operator += (const quat& q) { v += q.v; w += q.w; return *this; }
Gurvan 0:9cb9445a11f0 887 quat& operator -= (const quat& q) { v -= q.v; w -= q.w; return *this; }
Gurvan 0:9cb9445a11f0 888
Gurvan 0:9cb9445a11f0 889 quat& operator *= (const T& s) { v *= s; w *= s; return *this; }
Gurvan 0:9cb9445a11f0 890 quat& operator /= (const T& s) { v /= s; w /= s; return *this; }
Gurvan 0:9cb9445a11f0 891
Gurvan 0:9cb9445a11f0 892 quat& operator *= (const quat& r)
Gurvan 0:9cb9445a11f0 893 {
Gurvan 0:9cb9445a11f0 894 //q1 x q2 = [s1,v1] x [s2,v2] = [(s1*s2 - v1*v2),(s1*v2 + s2*v1 + v1xv2)].
Gurvan 0:9cb9445a11f0 895 quat q;
Gurvan 0:9cb9445a11f0 896 q.v = cross(v, r.v) + r.w * v + w * r.v;
Gurvan 0:9cb9445a11f0 897 q.w = w * r.w - dot(v, r.v);
Gurvan 0:9cb9445a11f0 898 return *this = q;
Gurvan 0:9cb9445a11f0 899 }
Gurvan 0:9cb9445a11f0 900
Gurvan 0:9cb9445a11f0 901 quat& operator /= (const quat& q) { return (*this) *= inverse(q); }
Gurvan 0:9cb9445a11f0 902 };
Gurvan 0:9cb9445a11f0 903
Gurvan 0:9cb9445a11f0 904 // Quaternion functions
Gurvan 0:9cb9445a11f0 905
Gurvan 0:9cb9445a11f0 906 template <typename T>
Gurvan 0:9cb9445a11f0 907 inline quat<T> identityq()
Gurvan 0:9cb9445a11f0 908 {
Gurvan 0:9cb9445a11f0 909 return quat<T>(T(0), T(0), T(0), T(1));
Gurvan 0:9cb9445a11f0 910 }
Gurvan 0:9cb9445a11f0 911
Gurvan 0:9cb9445a11f0 912 template <typename T>
Gurvan 0:9cb9445a11f0 913 inline quat<T> conjugate(const quat<T>& q)
Gurvan 0:9cb9445a11f0 914 {
Gurvan 0:9cb9445a11f0 915 return quat<T>(-q.v, q.w);
Gurvan 0:9cb9445a11f0 916 }
Gurvan 0:9cb9445a11f0 917
Gurvan 0:9cb9445a11f0 918 template <typename T>
Gurvan 0:9cb9445a11f0 919 inline quat<T> inverse(const quat<T>& q)
Gurvan 0:9cb9445a11f0 920 {
Gurvan 0:9cb9445a11f0 921 const T l = dot(q, q);
Gurvan 0:9cb9445a11f0 922 if ( l > T(0) ) return conjugate(q) * inv(l);
Gurvan 0:9cb9445a11f0 923 else return identityq<T>();
Gurvan 0:9cb9445a11f0 924 }
Gurvan 0:9cb9445a11f0 925
Gurvan 0:9cb9445a11f0 926 // quaternion utility functions
Gurvan 0:9cb9445a11f0 927
Gurvan 0:9cb9445a11f0 928 // the input quaternion is assumed to be normalized
Gurvan 0:9cb9445a11f0 929 template <typename T>
Gurvan 0:9cb9445a11f0 930 inline mat3<T> quat_to_mat3(const quat<T>& q)
Gurvan 0:9cb9445a11f0 931 {
Gurvan 0:9cb9445a11f0 932 // const quat<T> q = normalize(qq);
Gurvan 0:9cb9445a11f0 933
Gurvan 0:9cb9445a11f0 934 const T xx = q[0] * q[0];
Gurvan 0:9cb9445a11f0 935 const T xy = q[0] * q[1];
Gurvan 0:9cb9445a11f0 936 const T xz = q[0] * q[2];
Gurvan 0:9cb9445a11f0 937 const T xw = q[0] * q[3];
Gurvan 0:9cb9445a11f0 938
Gurvan 0:9cb9445a11f0 939 const T yy = q[1] * q[1];
Gurvan 0:9cb9445a11f0 940 const T yz = q[1] * q[2];
Gurvan 0:9cb9445a11f0 941 const T yw = q[1] * q[3];
Gurvan 0:9cb9445a11f0 942
Gurvan 0:9cb9445a11f0 943 const T zz = q[2] * q[2];
Gurvan 0:9cb9445a11f0 944 const T zw = q[2] * q[3];
Gurvan 0:9cb9445a11f0 945
Gurvan 0:9cb9445a11f0 946 return mat3<T>(
Gurvan 0:9cb9445a11f0 947 1 - 2*(yy + zz), 2*(xy - zw), 2*(xz + yw),
Gurvan 0:9cb9445a11f0 948 2*(xy + zw), 1 - 2*(xx + zz), 2*(yz - xw),
Gurvan 0:9cb9445a11f0 949 2*(xz - yw), 2*(yz + xw), 1 - 2*(xx + yy)
Gurvan 0:9cb9445a11f0 950 );
Gurvan 0:9cb9445a11f0 951 }
Gurvan 0:9cb9445a11f0 952
Gurvan 0:9cb9445a11f0 953 // the input quat<T>ernion is assumed to be normalized
Gurvan 0:9cb9445a11f0 954 template <typename T>
Gurvan 0:9cb9445a11f0 955 inline mat4<T> quat_to_mat4(const quat<T>& q)
Gurvan 0:9cb9445a11f0 956 {
Gurvan 0:9cb9445a11f0 957 // const quat<T> q = normalize(qq);
Gurvan 0:9cb9445a11f0 958
Gurvan 0:9cb9445a11f0 959 return mat4<T>(quat_to_mat3(q));
Gurvan 0:9cb9445a11f0 960 }
Gurvan 0:9cb9445a11f0 961
Gurvan 0:9cb9445a11f0 962 template <typename T>
Gurvan 0:9cb9445a11f0 963 inline quat<T> mat_to_quat(const mat4<T>& m)
Gurvan 0:9cb9445a11f0 964 {
Gurvan 0:9cb9445a11f0 965 const T t = m.elem[0][0] + m.elem[1][1] + m.elem[2][2] + T(1);
Gurvan 0:9cb9445a11f0 966 quat<T> q;
Gurvan 0:9cb9445a11f0 967
Gurvan 0:9cb9445a11f0 968 if ( t > 0 ) {
Gurvan 0:9cb9445a11f0 969 const T s = T(0.5) / sqrt(t);
Gurvan 0:9cb9445a11f0 970 q[3] = T(0.25) * inv(s);
Gurvan 0:9cb9445a11f0 971 q[0] = (m.elem[2][1] - m.elem[1][2]) * s;
Gurvan 0:9cb9445a11f0 972 q[1] = (m.elem[0][2] - m.elem[2][0]) * s;
Gurvan 0:9cb9445a11f0 973 q[2] = (m.elem[1][0] - m.elem[0][1]) * s;
Gurvan 0:9cb9445a11f0 974 } else {
Gurvan 0:9cb9445a11f0 975 if ( m.elem[0][0] > m.elem[1][1] && m.elem[0][0] > m.elem[2][2] ) {
Gurvan 0:9cb9445a11f0 976 const T s = T(2) * sqrt( T(1) + m.elem[0][0] - m.elem[1][1] - m.elem[2][2]);
Gurvan 0:9cb9445a11f0 977 const T invs = inv(s);
Gurvan 0:9cb9445a11f0 978 q[0] = T(0.25) * s;
Gurvan 0:9cb9445a11f0 979 q[1] = (m.elem[0][1] + m.elem[1][0] ) * invs;
Gurvan 0:9cb9445a11f0 980 q[2] = (m.elem[0][2] + m.elem[2][0] ) * invs;
Gurvan 0:9cb9445a11f0 981 q[3] = (m.elem[1][2] - m.elem[2][1] ) * invs;
Gurvan 0:9cb9445a11f0 982 } else if (m.elem[1][1] > m.elem[2][2]) {
Gurvan 0:9cb9445a11f0 983 const T s = T(2) * sqrt( T(1) + m.elem[1][1] - m.elem[0][0] - m.elem[2][2]);
Gurvan 0:9cb9445a11f0 984 const T invs = inv(s);
Gurvan 0:9cb9445a11f0 985 q[0] = (m.elem[0][1] + m.elem[1][0] ) * invs;
Gurvan 0:9cb9445a11f0 986 q[1] = T(0.25) * s;
Gurvan 0:9cb9445a11f0 987 q[2] = (m.elem[1][2] + m.elem[2][1] ) * invs;
Gurvan 0:9cb9445a11f0 988 q[3] = (m.elem[0][2] - m.elem[2][0] ) * invs;
Gurvan 0:9cb9445a11f0 989 } else {
Gurvan 0:9cb9445a11f0 990 const T s = T(2) * sqrt( T(1) + m.elem[2][2] - m.elem[0][0] - m.elem[1][1] );
Gurvan 0:9cb9445a11f0 991 const T invs = inv(s);
Gurvan 0:9cb9445a11f0 992 q[0] = (m.elem[0][2] + m.elem[2][0] ) * invs;
Gurvan 0:9cb9445a11f0 993 q[1] = (m.elem[1][2] + m.elem[2][1] ) * invs;
Gurvan 0:9cb9445a11f0 994 q[2] = T(0.25) * s;
Gurvan 0:9cb9445a11f0 995 q[3] = (m.elem[0][1] - m.elem[1][0] ) * invs;
Gurvan 0:9cb9445a11f0 996 }
Gurvan 0:9cb9445a11f0 997 }
Gurvan 0:9cb9445a11f0 998
Gurvan 0:9cb9445a11f0 999 return q;
Gurvan 0:9cb9445a11f0 1000 }
Gurvan 0:9cb9445a11f0 1001
Gurvan 0:9cb9445a11f0 1002 template <typename T>
Gurvan 0:9cb9445a11f0 1003 inline quat<T> mat_to_quat(const mat3<T>& m)
Gurvan 0:9cb9445a11f0 1004 {
Gurvan 0:9cb9445a11f0 1005 return mat_to_quat(mat4<T>(m));
Gurvan 0:9cb9445a11f0 1006 }
Gurvan 0:9cb9445a11f0 1007
Gurvan 0:9cb9445a11f0 1008 // the angle is in radians
Gurvan 0:9cb9445a11f0 1009 template <typename T>
Gurvan 0:9cb9445a11f0 1010 inline quat<T> quat_from_axis_angle(const vec3<T>& axis, const T a)
Gurvan 0:9cb9445a11f0 1011 {
Gurvan 0:9cb9445a11f0 1012 quat<T> r;
Gurvan 0:9cb9445a11f0 1013 const T inv2 = inv(T(2));
Gurvan 0:9cb9445a11f0 1014 r.v = sin(a * inv2) * normalize(axis);
Gurvan 0:9cb9445a11f0 1015 r.w = cos(a * inv2);
Gurvan 0:9cb9445a11f0 1016
Gurvan 0:9cb9445a11f0 1017 return r;
Gurvan 0:9cb9445a11f0 1018 }
Gurvan 0:9cb9445a11f0 1019
Gurvan 0:9cb9445a11f0 1020 // the angle is in radians
Gurvan 0:9cb9445a11f0 1021 template <typename T>
Gurvan 0:9cb9445a11f0 1022 inline quat<T> quat_from_axis_angle(const T x, const T y, const T z, const T angle)
Gurvan 0:9cb9445a11f0 1023 {
Gurvan 0:9cb9445a11f0 1024 return quat_from_axis_angle<T>(vec3<T>(x, y, z), angle);
Gurvan 0:9cb9445a11f0 1025 }
Gurvan 0:9cb9445a11f0 1026
Gurvan 0:9cb9445a11f0 1027 // the angle is stored in radians
Gurvan 0:9cb9445a11f0 1028 template <typename T>
Gurvan 0:9cb9445a11f0 1029 inline void quat_to_axis_angle(const quat<T>& qq, vec3<T>* axis, T *angle)
Gurvan 0:9cb9445a11f0 1030 {
Gurvan 0:9cb9445a11f0 1031 quat<T> q = normalize(qq);
Gurvan 0:9cb9445a11f0 1032
Gurvan 0:9cb9445a11f0 1033 *angle = 2 * acos(q.w);
Gurvan 0:9cb9445a11f0 1034
Gurvan 0:9cb9445a11f0 1035 const T s = sin((*angle) * inv(T(2)));
Gurvan 0:9cb9445a11f0 1036 if ( s != T(0) )
Gurvan 0:9cb9445a11f0 1037 *axis = q.v * inv(s);
Gurvan 0:9cb9445a11f0 1038 else
Gurvan 0:9cb9445a11f0 1039 * axis = vec3<T>(T(0), T(0), T(0));
Gurvan 0:9cb9445a11f0 1040 }
Gurvan 0:9cb9445a11f0 1041
Gurvan 0:9cb9445a11f0 1042 // Spherical linear interpolation
Gurvan 0:9cb9445a11f0 1043 template <typename T>
Gurvan 0:9cb9445a11f0 1044 inline quat<T> slerp(const quat<T>& qq1, const quat<T>& qq2, const T t)
Gurvan 0:9cb9445a11f0 1045 {
Gurvan 0:9cb9445a11f0 1046 // slerp(q1,q2) = sin((1-t)*a)/sin(a) * q1 + sin(t*a)/sin(a) * q2
Gurvan 0:9cb9445a11f0 1047 const quat<T> q1 = normalize(qq1);
Gurvan 0:9cb9445a11f0 1048 const quat<T> q2 = normalize(qq2);
Gurvan 0:9cb9445a11f0 1049
Gurvan 0:9cb9445a11f0 1050 const T a = acos(dot(q1, q2));
Gurvan 0:9cb9445a11f0 1051 const T s = sin(a);
Gurvan 0:9cb9445a11f0 1052
Gurvan 0:9cb9445a11f0 1053 #define EPS T(1e-5)
Gurvan 0:9cb9445a11f0 1054
Gurvan 0:9cb9445a11f0 1055 if ( !(-EPS <= s && s <= EPS) ) {
Gurvan 0:9cb9445a11f0 1056 return sin((T(1)-t)*a)/s * q1 + sin(t*a)/s * q2;
Gurvan 0:9cb9445a11f0 1057 } else {
Gurvan 0:9cb9445a11f0 1058 // if the angle is to small use a linear interpolation
Gurvan 0:9cb9445a11f0 1059 return lerp(q1, q2, t);
Gurvan 0:9cb9445a11f0 1060 }
Gurvan 0:9cb9445a11f0 1061
Gurvan 0:9cb9445a11f0 1062 #undef EPS
Gurvan 0:9cb9445a11f0 1063 }
Gurvan 0:9cb9445a11f0 1064
Gurvan 0:9cb9445a11f0 1065 // Sperical quadtratic interpolation using a smooth cubic spline
Gurvan 0:9cb9445a11f0 1066 // The parameters a and b are the control points.
Gurvan 0:9cb9445a11f0 1067 template <typename T>
Gurvan 0:9cb9445a11f0 1068 inline quat<T> squad(
Gurvan 0:9cb9445a11f0 1069 const quat<T>& q0,
Gurvan 0:9cb9445a11f0 1070 const quat<T>& a,
Gurvan 0:9cb9445a11f0 1071 const quat<T>& b,
Gurvan 0:9cb9445a11f0 1072 const quat<T>& q1,
Gurvan 0:9cb9445a11f0 1073 const T t)
Gurvan 0:9cb9445a11f0 1074 {
Gurvan 0:9cb9445a11f0 1075 return slerp(slerp(q0, q1, t),slerp(a, b, t), 2 * t * (1 - t));
Gurvan 0:9cb9445a11f0 1076 }
Gurvan 0:9cb9445a11f0 1077
Gurvan 0:9cb9445a11f0 1078 #undef MOP_M_CLASS_TEMPLATE
Gurvan 0:9cb9445a11f0 1079 #undef MOP_M_TYPE_TEMPLATE
Gurvan 0:9cb9445a11f0 1080 #undef MOP_COMP_TEMPLATE
Gurvan 0:9cb9445a11f0 1081 #undef MOP_G_UMINUS_TEMPLATE
Gurvan 0:9cb9445a11f0 1082 #undef COMMON_OPERATORS
Gurvan 0:9cb9445a11f0 1083 #undef VECTOR_COMMON
Gurvan 0:9cb9445a11f0 1084 #undef FOP_G_SOURCE_TEMPLATE
Gurvan 0:9cb9445a11f0 1085 #undef FOP_G_CLASS_TEMPLATE
Gurvan 0:9cb9445a11f0 1086 #undef FOP_G_TYPE_TEMPLATE
Gurvan 0:9cb9445a11f0 1087 #undef VEC_QUAT_FUNC_TEMPLATE
Gurvan 0:9cb9445a11f0 1088 #undef VEC_FUNC_TEMPLATE
Gurvan 0:9cb9445a11f0 1089 #undef MATRIX_COL4
Gurvan 0:9cb9445a11f0 1090 #undef MATRIX_ROW4
Gurvan 0:9cb9445a11f0 1091 #undef MATRIX_COL3
Gurvan 0:9cb9445a11f0 1092 #undef MATRIX_ROW3
Gurvan 0:9cb9445a11f0 1093 #undef MATRIX_COL2
Gurvan 0:9cb9445a11f0 1094 #undef MATRIX_ROW2
Gurvan 0:9cb9445a11f0 1095 #undef MOP_M_MATRIX_MULTIPLY
Gurvan 0:9cb9445a11f0 1096 #undef MATRIX_CONSTRUCTOR_FROM_T
Gurvan 0:9cb9445a11f0 1097 #undef MATRIX_CONSTRUCTOR_FROM_LOWER
Gurvan 0:9cb9445a11f0 1098 #undef MATRIX_COMMON
Gurvan 0:9cb9445a11f0 1099 #undef MATRIX_CONSTRUCTOR_FROM_HIGHER
Gurvan 0:9cb9445a11f0 1100 #undef MAT_FUNC_TEMPLATE
Gurvan 0:9cb9445a11f0 1101 #undef MAT_FUNC_MINOR_TEMPLATE
Gurvan 0:9cb9445a11f0 1102 #undef MAT_ADJOINT_TEMPLATE
Gurvan 0:9cb9445a11f0 1103 #undef MAT_INVERSE_TEMPLATE
Gurvan 0:9cb9445a11f0 1104 #undef MAT_VEC_FUNCS_TEMPLATE
Gurvan 0:9cb9445a11f0 1105 #undef MAT_TRANFORMS_TEMPLATE
Gurvan 0:9cb9445a11f0 1106 #undef MAT_OUTERPRODUCT_TEMPLATE
Gurvan 0:9cb9445a11f0 1107 #undef FREE_MODIFYING_OPERATORS
Gurvan 0:9cb9445a11f0 1108 #undef FREE_OPERATORS
Gurvan 0:9cb9445a11f0 1109
Gurvan 0:9cb9445a11f0 1110 } // end namespace vmath
Gurvan 0:9cb9445a11f0 1111
Gurvan 0:9cb9445a11f0 1112 #endif
Gurvan 0:9cb9445a11f0 1113
Gurvan 0:9cb9445a11f0 1114
Gurvan 0:9cb9445a11f0 1115