float3.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * float3.h, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #pragma once
  11. VCMI_LIB_NAMESPACE_BEGIN
  12. // FIXME: Class doesn't contain three float values. Update name and description.
  13. /// Class which consists of three float values. Represents position virtual RMG (0;1) area.
  14. class float3
  15. {
  16. public:
  17. float x;
  18. float y;
  19. si32 z;
  20. float3() : x(0), y(0), z(0) {}
  21. float3(const float X, const float Y, const si32 Z): x(X), y(Y), z(Z) {}
  22. float3(const float3 & copy) : x(copy.x), y(copy.y), z(copy.z) {}
  23. float3 & operator=(const float3 & copy) { x = copy.x; y = copy.y; z = copy.z; return *this; }
  24. // returns float3 with coordinates increased by corresponding coordinate of given float3
  25. float3 operator+(const float3 & i) const { return float3(x + i.x, y + i.y, z + i.z); }
  26. // returns float3 with coordinates increased by given number
  27. float3 operator+(const float i) const { return float3(x + i, y + i, z + (si32)i); }
  28. // returns float3 with coordinates decreased by corresponding coordinate of given float3
  29. float3 operator-(const float3 & i) const { return float3(x - i.x, y - i.y, z - i.z); }
  30. // returns float3 with coordinates decreased by given number
  31. float3 operator-(const float i) const { return float3(x - i, y - i, z - (si32)i); }
  32. // returns float3 with plane coordinates decreased by given number
  33. float3 operator*(const float i) const {return float3(x * i, y * i, z);}
  34. // returns float3 with plane coordinates decreased by given number
  35. float3 operator/(const float i) const {return float3(x / i, y / i, z);}
  36. // returns opposite position
  37. float3 operator-() const { return float3(-x, -y, -z); }
  38. // returns squared distance on Oxy plane (z coord is not used)
  39. double dist2dSQ(const float3 & o) const
  40. {
  41. const double dx = (x - o.x);
  42. const double dy = (y - o.y);
  43. return dx*dx + dy*dy;
  44. }
  45. double mag() const
  46. {
  47. return sqrt(x*x + y*y);
  48. }
  49. float3 unitVector() const
  50. {
  51. return float3(x, y, z) / (float)mag();
  52. }
  53. // returns distance on Oxy plane (z coord is not used)
  54. double dist2d(const float3 &other) const { return std::sqrt(dist2dSQ(other)); }
  55. bool areNeighbours(const float3 &other) const { return (dist2dSQ(other) < 4.0) && z == other.z; }
  56. float3& operator+=(const float3 & i)
  57. {
  58. x += i.x;
  59. y += i.y;
  60. z += i.z;
  61. return *this;
  62. }
  63. float3& operator+=(const float & i)
  64. {
  65. x += i;
  66. y += i;
  67. z += (si32)i;
  68. return *this;
  69. }
  70. float3& operator-=(const float3 & i)
  71. {
  72. x -= i.x;
  73. y -= i.y;
  74. z -= i.z;
  75. return *this;
  76. }
  77. float3& operator-=(const float & i)
  78. {
  79. x += i;
  80. y += i;
  81. z += (si32)i;
  82. return *this;
  83. }
  84. // scale on plane
  85. float3& operator*=(const float & i)
  86. {
  87. x *= i;
  88. y *= i;
  89. return *this;
  90. }
  91. // scale on plane
  92. float3& operator/=(const float & i)
  93. {
  94. x /= i;
  95. y /= i;
  96. return *this;
  97. }
  98. std::string toString() const
  99. {
  100. return "(" + std::to_string(x) +
  101. " " + std::to_string(y) +
  102. " " + std::to_string(z) + ")";
  103. }
  104. bool valid() const
  105. {
  106. return z >= 0; //minimal condition that needs to be fulfilled for tiles in the map
  107. }
  108. template <typename Handler> void serialize(Handler &h, const float version)
  109. {
  110. h & x;
  111. h & y;
  112. h & z;
  113. }
  114. };
  115. struct Shashfloat3
  116. {
  117. size_t operator()(float3 const& pos) const
  118. {
  119. size_t ret = std::hash<float>()(pos.x);
  120. vstd::hash_combine(ret, pos.y);
  121. vstd::hash_combine(ret, pos.z);
  122. return ret;
  123. }
  124. };
  125. VCMI_LIB_NAMESPACE_END