Changeset 1232
 Timestamp:
 Apr 16, 2012, 8:25:00 PM (9 years ago)
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

trunk/src/lol/math/vector.h
r1217 r1232 57 57 DECLARE_VECTOR_TYPEDEFS(Vec4, vec4) 58 58 DECLARE_VECTOR_TYPEDEFS(Quat, quat) 59 DECLARE_VECTOR_TYPEDEFS(Mat2, mat2) 60 DECLARE_VECTOR_TYPEDEFS(Mat3, mat3) 59 61 DECLARE_VECTOR_TYPEDEFS(Mat4, mat4) 60 62 … … 911 913 inline Quat(T X, T Y, T Z, T W) : x(X), y(Y), z(Z), w(W) {} 912 914 915 Quat(Mat3<T> const &m); 913 916 Quat(Mat4<T> const &m); 914 917 … … 1336 1339 1337 1340 /* 1341 * 2×2element matrices 1342 */ 1343 1344 template <typename T> struct Mat2 1345 { 1346 inline Mat2() {} 1347 inline Mat2(Vec2<T> V0, Vec2<T> V1) 1348 : v0(V0), v1(V1) {} 1349 1350 explicit inline Mat2(T val) 1351 : v0(val, (T)0), 1352 v1((T)0, val) {} 1353 1354 inline Vec2<T>& operator[](size_t n) { return (&v0)[n]; } 1355 inline Vec2<T> const& operator[](size_t n) const { return (&v0)[n]; } 1356 1357 T det() const; 1358 Mat2<T> invert() const; 1359 1360 /* Helpers for transformation matrices */ 1361 static Mat2<T> rotate(T angle); 1362 1363 static inline Mat2<T> rotate(Mat2<T> mat, T angle) 1364 { 1365 return rotate(angle) * mat; 1366 } 1367 1368 void printf() const; 1369 1370 #if !defined __ANDROID__ 1371 template<class U> 1372 friend std::ostream &operator<<(std::ostream &stream, Mat2<U> const &m); 1373 #endif 1374 1375 inline Mat2<T> operator +(Mat2<T> const m) const 1376 { 1377 return Mat2<T>(v0 + m[0], v1 + m[1]); 1378 } 1379 1380 inline Mat2<T> operator +=(Mat2<T> const m) 1381 { 1382 return *this = *this + m; 1383 } 1384 1385 inline Mat2<T> operator (Mat2<T> const m) const 1386 { 1387 return Mat2<T>(v0  m[0], v1  m[1]); 1388 } 1389 1390 inline Mat2<T> operator =(Mat2<T> const m) 1391 { 1392 return *this = *this  m; 1393 } 1394 1395 inline Mat2<T> operator *(Mat2<T> const m) const 1396 { 1397 return Mat2<T>(*this * m[0], *this * m[1]); 1398 } 1399 1400 inline Mat2<T> operator *=(Mat2<T> const m) 1401 { 1402 return *this = *this * m; 1403 } 1404 1405 inline Vec2<T> operator *(Vec2<T> const m) const 1406 { 1407 Vec2<T> ret; 1408 for (int j = 0; j < 2; j++) 1409 { 1410 T tmp = 0; 1411 for (int k = 0; k < 2; k++) 1412 tmp += (*this)[k][j] * m[k]; 1413 ret[j] = tmp; 1414 } 1415 return ret; 1416 } 1417 1418 Vec2<T> v0, v1; 1419 }; 1420 1421 /* 1422 * 3×3element matrices 1423 */ 1424 1425 template <typename T> struct Mat3 1426 { 1427 inline Mat3() {} 1428 inline Mat3(Vec3<T> V0, Vec3<T> V1, Vec3<T> V2) 1429 : v0(V0), v1(V1), v2(V2) {} 1430 1431 explicit inline Mat3(T val) 1432 : v0(val, (T)0, (T)0), 1433 v1((T)0, val, (T)0), 1434 v2((T)0, (T)0, val) {} 1435 1436 inline Vec3<T>& operator[](size_t n) { return (&v0)[n]; } 1437 inline Vec3<T> const& operator[](size_t n) const { return (&v0)[n]; } 1438 1439 T det() const; 1440 Mat3<T> invert() const; 1441 1442 /* Helpers for transformation matrices */ 1443 static Mat3<T> rotate(T angle, T x, T y, T z); 1444 static Mat3<T> rotate(T angle, Vec3<T> v); 1445 static Mat3<T> rotate(Quat<T> q); 1446 1447 static inline Mat3<T> rotate(Mat3<T> mat, T angle, Vec3<T> v) 1448 { 1449 return rotate(angle, v) * mat; 1450 } 1451 1452 void printf() const; 1453 1454 #if !defined __ANDROID__ 1455 template<class U> 1456 friend std::ostream &operator<<(std::ostream &stream, Mat3<U> const &m); 1457 #endif 1458 1459 inline Mat3<T> operator +(Mat3<T> const m) const 1460 { 1461 return Mat3<T>(v0 + m[0], v1 + m[1], v2 + m[2]); 1462 } 1463 1464 inline Mat3<T> operator +=(Mat3<T> const m) 1465 { 1466 return *this = *this + m; 1467 } 1468 1469 inline Mat3<T> operator (Mat3<T> const m) const 1470 { 1471 return Mat3<T>(v0  m[0], v1  m[1], v2  m[2]); 1472 } 1473 1474 inline Mat3<T> operator =(Mat3<T> const m) 1475 { 1476 return *this = *this  m; 1477 } 1478 1479 inline Mat3<T> operator *(Mat3<T> const m) const 1480 { 1481 return Mat3<T>(*this * m[0], *this * m[1], *this * m[2]); 1482 } 1483 1484 inline Mat3<T> operator *=(Mat3<T> const m) 1485 { 1486 return *this = *this * m; 1487 } 1488 1489 inline Vec3<T> operator *(Vec3<T> const m) const 1490 { 1491 Vec3<T> ret; 1492 for (int j = 0; j < 3; j++) 1493 { 1494 T tmp = 0; 1495 for (int k = 0; k < 3; k++) 1496 tmp += (*this)[k][j] * m[k]; 1497 ret[j] = tmp; 1498 } 1499 return ret; 1500 } 1501 1502 Vec3<T> v0, v1, v2; 1503 }; 1504 1505 /* 1338 1506 * 4×4element matrices 1339 1507 */
Note: See TracChangeset
for help on using the changeset viewer.