Vector.cpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #include "client/math/Vector.h"
  2. template<>
  3. Vector<3>::Vector(float x, float y, float z) {
  4. values[0] = x;
  5. values[1] = y;
  6. values[2] = z;
  7. }
  8. template<>
  9. Vector<2>::Vector(float x, float y) {
  10. values[0] = x;
  11. values[1] = y;
  12. }
  13. template<>
  14. Vector<3>& Vector<3>::setAngles(float lengthAngle, float widthAngle) {
  15. lengthAngle *= (M_PI / 180.0f);
  16. widthAngle *= (M_PI / 180.0f);
  17. values[0] = cosf(widthAngle) * sinf(lengthAngle);
  18. values[1] = sinf(widthAngle);
  19. values[2] = cosf(widthAngle) * cosf(lengthAngle);
  20. return *this;
  21. }
  22. template<>
  23. Vector<3>::Vector(float lengthAngle, float widthAngle) {
  24. setAngles(lengthAngle, widthAngle);
  25. }
  26. template<>
  27. Vector<3>& Vector<3>::set(float x, float y, float z) {
  28. values[0] = x;
  29. values[1] = y;
  30. values[2] = z;
  31. return *this;
  32. }
  33. template<>
  34. Vector<3> Vector<3>::cross(const Vector<3>& other) const {
  35. return Vector<3>(
  36. values[1] * other.values[2] - values[2] * other.values[1],
  37. values[2] * other.values[0] - values[0] * other.values[2],
  38. values[0] * other.values[1] - values[1] * other.values[0]);
  39. }
  40. Vector<3> operator*(const Matrix& m, const Vector<3>& v) {
  41. const float* d = m.getValues();
  42. Vector<3> result(
  43. v[0] * d[0] + v[1] * d[4] + v[2] * d[8] + d[12],
  44. v[0] * d[1] + v[1] * d[5] + v[2] * d[9] + d[13],
  45. v[0] * d[2] + v[1] * d[6] + v[2] * d[10] + d[14]);
  46. result *= 1.0f / (v[1] * d[3] + v[1] * d[7] + v[2] * d[11] + d[15]);
  47. return result;
  48. }