vector.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. // This code is in the public domain -- [email protected]
  2. #ifndef NV_MATH_VECTOR_H
  3. #define NV_MATH_VECTOR_H
  4. #include "nvmath.h"
  5. namespace nv
  6. {
  7. class NVMATH_CLASS Vector2
  8. {
  9. public:
  10. typedef Vector2 const & Arg;
  11. Vector2();
  12. explicit Vector2(float f);
  13. Vector2(float x, float y);
  14. Vector2(Vector2::Arg v);
  15. //template <typename T> explicit Vector2(const T & v) : x(v.x), y(v.y) {}
  16. //template <typename T> operator T() const { return T(x, y); }
  17. const Vector2 & operator=(Vector2::Arg v);
  18. const float * ptr() const;
  19. void set(float x, float y);
  20. Vector2 operator-() const;
  21. void operator+=(Vector2::Arg v);
  22. void operator-=(Vector2::Arg v);
  23. void operator*=(float s);
  24. void operator*=(Vector2::Arg v);
  25. friend bool operator==(Vector2::Arg a, Vector2::Arg b);
  26. friend bool operator!=(Vector2::Arg a, Vector2::Arg b);
  27. union {
  28. struct {
  29. float x, y;
  30. };
  31. float component[2];
  32. };
  33. };
  34. class NVMATH_CLASS Vector3
  35. {
  36. public:
  37. typedef Vector3 const & Arg;
  38. Vector3();
  39. explicit Vector3(float x);
  40. //explicit Vector3(int x) : x(float(x)), y(float(x)), z(float(x)) {}
  41. Vector3(float x, float y, float z);
  42. Vector3(Vector2::Arg v, float z);
  43. Vector3(Vector3::Arg v);
  44. //template <typename T> explicit Vector3(const T & v) : x(v.x), y(v.y), z(v.z) {}
  45. //template <typename T> operator T() const { return T(x, y, z); }
  46. const Vector3 & operator=(Vector3::Arg v);
  47. Vector2 xy() const;
  48. const float * ptr() const;
  49. void set(float x, float y, float z);
  50. Vector3 operator-() const;
  51. void operator+=(Vector3::Arg v);
  52. void operator-=(Vector3::Arg v);
  53. void operator*=(float s);
  54. void operator/=(float s);
  55. void operator*=(Vector3::Arg v);
  56. void operator/=(Vector3::Arg v);
  57. friend bool operator==(Vector3::Arg a, Vector3::Arg b);
  58. friend bool operator!=(Vector3::Arg a, Vector3::Arg b);
  59. union {
  60. struct {
  61. float x, y, z;
  62. };
  63. float component[3];
  64. };
  65. };
  66. class NVMATH_CLASS Vector4
  67. {
  68. public:
  69. typedef Vector4 const & Arg;
  70. Vector4();
  71. explicit Vector4(float x);
  72. Vector4(float x, float y, float z, float w);
  73. Vector4(Vector2::Arg v, float z, float w);
  74. Vector4(Vector2::Arg v, Vector2::Arg u);
  75. Vector4(Vector3::Arg v, float w);
  76. Vector4(Vector4::Arg v);
  77. // Vector4(const Quaternion & v);
  78. //template <typename T> explicit Vector4(const T & v) : x(v.x), y(v.y), z(v.z), w(v.w) {}
  79. //template <typename T> operator T() const { return T(x, y, z, w); }
  80. const Vector4 & operator=(Vector4::Arg v);
  81. Vector2 xy() const;
  82. Vector2 zw() const;
  83. Vector3 xyz() const;
  84. const float * ptr() const;
  85. void set(float x, float y, float z, float w);
  86. Vector4 operator-() const;
  87. void operator+=(Vector4::Arg v);
  88. void operator-=(Vector4::Arg v);
  89. void operator*=(float s);
  90. void operator/=(float s);
  91. void operator*=(Vector4::Arg v);
  92. void operator/=(Vector4::Arg v);
  93. friend bool operator==(Vector4::Arg a, Vector4::Arg b);
  94. friend bool operator!=(Vector4::Arg a, Vector4::Arg b);
  95. union {
  96. struct {
  97. float x, y, z, w;
  98. };
  99. float component[4];
  100. };
  101. };
  102. } // nv namespace
  103. // If we had these functions, they would be ambiguous, the compiler would not know which one to pick:
  104. //template <typename T> Vector2 to(const T & v) { return Vector2(v.x, v.y); }
  105. //template <typename T> Vector3 to(const T & v) { return Vector3(v.x, v.y, v.z); }
  106. //template <typename T> Vector4 to(const T & v) { return Vector4(v.x, v.y, v.z, v.z); }
  107. // We could use a cast operator so that we could infer the expected type, but that doesn't work the same way in all compilers and produces horrible error messages.
  108. // Instead we simply have explicit casts:
  109. template <typename T> T to(const nv::Vector2 & v) { NV_COMPILER_CHECK(sizeof(T) == sizeof(nv::Vector2)); return T(v.x, v.y); }
  110. template <typename T> T to(const nv::Vector3 & v) { NV_COMPILER_CHECK(sizeof(T) == sizeof(nv::Vector3)); return T(v.x, v.y, v.z); }
  111. template <typename T> T to(const nv::Vector4 & v) { NV_COMPILER_CHECK(sizeof(T) == sizeof(nv::Vector4)); return T(v.x, v.y, v.z, v.w); }
  112. #endif // NV_MATH_VECTOR_H