Changeset 1047
- Timestamp:
- Nov 1, 2011, 6:55:23 PM (11 years ago)
- Location:
- trunk
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/matrix.cpp
r1046 r1047 54 54 } 55 55 56 template<> vec2 normalize(vec2 v)57 {58 float norm = v.len();59 if (!norm)60 return vec2(0);61 return v / norm;62 }63 64 template<> vec3 normalize(vec3 v)65 {66 float norm = v.len();67 if (!norm)68 return vec3(0);69 return v / norm;70 }71 72 template<> vec4 normalize(vec4 v)73 {74 float norm = v.len();75 if (!norm)76 return vec4(0.0f);77 return v / norm;78 }79 80 56 static inline float det3(float a, float b, float c, 81 57 float d, float e, float f, … … 152 128 } 153 129 130 template<> void quat::printf() const 131 { 132 Log::Debug("[ %6.6f %6.6f %6.6f %6.6f ]\n", x, y, z, w); 133 } 134 154 135 template<> void mat4::printf() const 155 136 { … … 183 164 } 184 165 166 template<> std::ostream &operator<<(std::ostream &stream, iquat const &v) 167 { 168 return stream << "(" << v.x << ", " << v.y << ", " 169 << v.z << ", " << v.w << ")"; 170 } 171 185 172 template<> std::ostream &operator<<(std::ostream &stream, vec2 const &v) 186 173 { … … 194 181 195 182 template<> std::ostream &operator<<(std::ostream &stream, vec4 const &v) 183 { 184 return stream << "(" << v.x << ", " << v.y << ", " 185 << v.z << ", " << v.w << ")"; 186 } 187 188 template<> std::ostream &operator<<(std::ostream &stream, quat const &v) 196 189 { 197 190 return stream << "(" << v.x << ", " << v.y << ", " -
trunk/src/matrix.h
r1046 r1047 25 25 { 26 26 27 #define VECTOR_OP(elems, op) \ 28 template<typename U> \ 29 inline Vec##elems<T> operator op(Vec##elems<U> const &val) const \ 30 { \ 31 Vec##elems<T> ret; \ 32 for (int n = 0; n < elems; n++) \ 27 #define VECTOR_OP(op) \ 28 inline type_t operator op(type_t const &val) const \ 29 { \ 30 type_t ret; \ 31 for (size_t n = 0; n < sizeof(*this) / sizeof(T); n++) \ 33 32 ret[n] = (*this)[n] op val[n]; \ 34 33 return ret; \ 35 34 } \ 36 35 \ 37 template<typename U> \ 38 inline Vec##elems<T> operator op##=(Vec##elems<U> const &val) \ 36 inline type_t operator op##=(type_t const &val) \ 39 37 { \ 40 38 return *this = (*this) op val; \ 41 39 } 42 40 43 #define BOOL_OP( elems,op, op2, ret) \44 inline bool operator op( Vec##elems<T>const &val) const \45 { \ 46 for ( int n = 0; n < elems; n++) \41 #define BOOL_OP(op, op2, ret) \ 42 inline bool operator op(type_t const &val) const \ 43 { \ 44 for (size_t n = 0; n < sizeof(*this) / sizeof(T); n++) \ 47 45 if (!((*this)[n] op2 val[n])) \ 48 46 return !ret; \ … … 50 48 } 51 49 52 #define SCALAR_OP( elems,op) \53 inline Vec##elems<T>operator op(T const &val) const \54 { \ 55 Vec##elems<T>ret; \56 for ( int n = 0; n < elems; n++) \50 #define SCALAR_OP(op) \ 51 inline type_t operator op(T const &val) const \ 52 { \ 53 type_t ret; \ 54 for (size_t n = 0; n < sizeof(*this) / sizeof(T); n++) \ 57 55 ret[n] = (*this)[n] op val; \ 58 56 return ret; \ 59 57 } \ 60 58 \ 61 inline Vec##elems<T>operator op##=(T const &val) \59 inline type_t operator op##=(T const &val) \ 62 60 { \ 63 61 return *this = (*this) op val; \ 64 62 } 65 63 66 #define LINEAR_OPS( elems) \64 #define LINEAR_OPS() \ 67 65 inline T& operator[](int n) { return *(&x + n); } \ 68 66 inline T const& operator[](int n) const { return *(&x + n); } \ 69 67 \ 70 VECTOR_OP( elems,-) \71 VECTOR_OP( elems,+) \72 \ 73 BOOL_OP( elems,==, ==, true) \74 BOOL_OP( elems,!=, ==, false) \75 \ 76 SCALAR_OP( elems,*) \77 SCALAR_OP( elems,/) \78 \ 79 inline Vec##elems<T>operator -() const \80 { \ 81 Vec##elems<T>ret; \82 for ( int n = 0; n < elems; n++) \68 VECTOR_OP(-) \ 69 VECTOR_OP(+) \ 70 \ 71 BOOL_OP(==, ==, true) \ 72 BOOL_OP(!=, ==, false) \ 73 \ 74 SCALAR_OP(*) \ 75 SCALAR_OP(/) \ 76 \ 77 inline type_t operator -() const \ 78 { \ 79 type_t ret; \ 80 for (size_t n = 0; n < sizeof(*this) / sizeof(T); n++) \ 83 81 ret[n] = -(*this)[n]; \ 84 82 return ret; \ … … 88 86 { \ 89 87 T acc = 0; \ 90 for ( int n = 0; n < elems; n++) \88 for (size_t n = 0; n < sizeof(*this) / sizeof(T); n++) \ 91 89 acc += (*this)[n] * (*this)[n]; \ 92 90 return acc; \ … … 99 97 } \ 100 98 \ 101 template<typename U> \102 friend Vec##elems<U> normalize(Vec##elems<U>); \103 \104 99 void printf() const; 105 100 101 #define QUATERNION_OPS() \ 102 inline type_t operator *(type_t const &val) const \ 103 { \ 104 type_t ret; \ 105 Vec3<T> v1(x, y, z); \ 106 Vec3<T> v2(val.x, val.y, val.z); \ 107 Vec3<T> v3 = cross(v1, v2) + w * v2 + val.w * v1; \ 108 ret.x = v3.x; \ 109 ret.y = v3.y; \ 110 ret.z = v3.z; \ 111 ret.w = w * val.w - dot(v1, v2); \ 112 return ret; \ 113 } \ 114 \ 115 inline type_t operator *=(type_t const &val) \ 116 { \ 117 return *this = (*this) * val; \ 118 } \ 119 \ 120 inline type_t operator ~() const \ 121 { \ 122 type_t ret; \ 123 for (int n = 0; n < 3; n++) \ 124 ret[n] = -(*this)[n]; \ 125 ret[3] = (*this)[3]; \ 126 return ret; \ 127 } \ 128 \ 129 inline T norm() const { return sqlen(); } 130 106 131 #define OTHER_OPS(elems) \ 107 VECTOR_OP( elems,*) \108 VECTOR_OP( elems,/) \109 \ 110 BOOL_OP( elems,<=, <=, true) \111 BOOL_OP( elems,>=, >=, true) \112 BOOL_OP( elems,<, <, true) \113 BOOL_OP( elems,>, >, true) \132 VECTOR_OP(*) \ 133 VECTOR_OP(/) \ 134 \ 135 BOOL_OP(<=, <=, true) \ 136 BOOL_OP(>=, >=, true) \ 137 BOOL_OP(<, <, true) \ 138 BOOL_OP(>, >, true) \ 114 139 \ 115 140 template<typename U> \ … … 117 142 { \ 118 143 Vec##elems<U> ret; \ 119 for ( int n = 0; n < elems; n++) \144 for (size_t n = 0; n < sizeof(*this) / sizeof(T); n++) \ 120 145 ret[n] = static_cast<U>((*this)[n]); \ 121 146 return ret; \ … … 196 221 template <typename T> struct Vec2 197 222 { 223 typedef Vec2<T> type_t; 224 198 225 inline Vec2() { } 199 226 explicit inline Vec2(T val) { x = y = val; } 200 227 inline Vec2(T _x, T _y) { x = _x; y = _y; } 201 228 202 LINEAR_OPS( 2)229 LINEAR_OPS() 203 230 OTHER_OPS(2) 204 231 … … 233 260 template <typename T> struct Vec3 234 261 { 262 typedef Vec3<T> type_t; 263 235 264 inline Vec3() { } 236 265 explicit inline Vec3(T val) { x = y = z = val; } … … 239 268 inline Vec3(T _x, Vec2<T> _yz) { x = _x; y = _yz.x; z = _yz.y; } 240 269 241 LINEAR_OPS( 3)270 LINEAR_OPS() 242 271 OTHER_OPS(3) 243 272 … … 276 305 template <typename T> struct Vec4 277 306 { 307 typedef Vec4<T> type_t; 308 278 309 inline Vec4() { } 279 310 explicit inline Vec4(T val) : x(val), y(val), z(val), w(val) { } … … 286 317 inline Vec4(T _x, Vec3<T> _yzw) : x(_x), y(_yzw.x), z(_yzw.y), w(_yzw.z) { } 287 318 288 LINEAR_OPS( 4)319 LINEAR_OPS() 289 320 OTHER_OPS(4) 290 321 … … 315 346 typedef Vec4<uint64_t> u64vec4; 316 347 317 #define SCALAR_GLOBAL(elems, op, U) \ 348 /* 349 * 4-element quaternions 350 */ 351 352 template <typename T> struct Quat 353 { 354 typedef Quat<T> type_t; 355 356 inline Quat() { } 357 inline Quat(T val) : x(0), y(0), z(0), w(val) { } 358 inline Quat(T _x, T _y, T _z, T _w) : x(_x), y(_y), z(_z), w(_w) { } 359 360 LINEAR_OPS() 361 QUATERNION_OPS() 362 363 #if !defined __ANDROID__ 364 template<typename U> 365 friend std::ostream &operator<<(std::ostream &stream, Quat<U> const &v); 366 #endif 367 368 T x, y, z, w; 369 }; 370 371 template<typename T> 372 static inline Quat<T> re(Quat<T> const &val) 373 { 374 return ~val / val.norm(); 375 } 376 377 template<typename T> 378 static inline Quat<T> operator /(T x, Quat<T> const &y) 379 { 380 return x * re(y); 381 } 382 383 template<typename T> 384 static inline Quat<T> operator /(Quat<T> x, Quat<T> const &y) 385 { 386 return x * re(y); 387 } 388 389 typedef Quat<half> f16quat; 390 typedef Quat<float> quat; 391 typedef Quat<int8_t> i8quat; 392 typedef Quat<uint8_t> u8quat; 393 typedef Quat<int16_t> i16quat; 394 typedef Quat<uint16_t> u16quat; 395 typedef Quat<int32_t> iquat; 396 typedef Quat<uint32_t> uquat; 397 typedef Quat<int64_t> i64quat; 398 typedef Quat<uint64_t> u64quat; 399 400 /* 401 * Common operators for all vector types, including quaternions 402 */ 403 404 #define SCALAR_GLOBAL(tname, op, U) \ 318 405 template<typename T> \ 319 static inline Vec##elems<U> operator op(U const &val, \ 320 Vec##elems<T> const &that) \ 321 { \ 322 Vec##elems<U> ret; \ 323 for (int n = 0; n < elems; n++) \ 406 static inline tname<U> operator op(U const &val, tname<T> const &that) \ 407 { \ 408 tname<U> ret; \ 409 for (unsigned int n = 0; n < sizeof(that) / sizeof(that[0]); n++) \ 324 410 ret[n] = val op that[n]; \ 325 411 return ret; \ 326 412 } 327 413 328 #define SCALAR_GLOBAL2(elems, op) \ 329 SCALAR_GLOBAL(elems, op, int) \ 330 SCALAR_GLOBAL(elems, op, float) 331 332 #define GLOBALS(elems) \ 333 SCALAR_GLOBAL2(elems, -) \ 334 SCALAR_GLOBAL2(elems, +) \ 335 SCALAR_GLOBAL2(elems, *) \ 336 SCALAR_GLOBAL2(elems, /) 337 338 GLOBALS(2) 339 GLOBALS(3) 340 GLOBALS(4) 414 #define SCALAR_GLOBAL2(tname, op) \ 415 SCALAR_GLOBAL(tname, op, int) \ 416 SCALAR_GLOBAL(tname, op, float) 417 418 #define GLOBALS(tname) \ 419 SCALAR_GLOBAL2(tname, *) \ 420 \ 421 template<typename T> \ 422 static inline tname<T> normalize(tname<T> const &val) \ 423 { \ 424 T norm = val.len(); \ 425 return norm ? val / norm : val * 0; \ 426 } 427 428 GLOBALS(Vec2) 429 GLOBALS(Vec3) 430 GLOBALS(Vec4) 431 GLOBALS(Quat) 341 432 342 433 /* … … 346 437 template <typename T> struct Mat4 347 438 { 439 typedef Mat4<T> type_t; 440 348 441 inline Mat4() { } 349 442 explicit inline Mat4(T val) -
trunk/test/Makefile.am
r1043 r1047 25 25 testsuite_SOURCES = testsuite.cpp \ 26 26 unit/matrix.cpp unit/half.cpp unit/trig.cpp unit/build.cpp \ 27 unit/real.cpp unit/image.cpp 27 unit/real.cpp unit/image.cpp unit/quat.cpp 28 28 testsuite_CPPFLAGS = @LOL_CFLAGS@ @PIPI_CFLAGS@ 29 29 testsuite_LDFLAGS = $(top_builddir)/src/liblol.a @LOL_LIBS@ @PIPI_LIBS@
Note: See TracChangeset
for help on using the changeset viewer.