float3.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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, y;
  18. si32 z;
  19. float3() : x(0), y(0), z(0) {}
  20. float3(const float X, const float Y, const si32 Z): x(X), y(Y), z(Z) {}
  21. float3(const float3 & copy) : x(copy.x), y(copy.y), z(copy.z) {}
  22. float3 & operator=(const float3 & copy) { x = copy.x; y = copy.y; z = copy.z; return *this; }
  23. // returns float3 with coordinates increased by corresponding coordinate of given float3
  24. float3 operator+(const float3 & i) const { return float3(x + i.x, y + i.y, z + i.z); }
  25. // returns float3 with coordinates increased by given numer
  26. float3 operator+(const float i) const { return float3(x + i, y + i, z + (si32)i); }
  27. // returns float3 with coordinates decreased by corresponding coordinate of given float3
  28. float3 operator-(const float3 & i) const { return float3(x - i.x, y - i.y, z - i.z); }
  29. // returns float3 with coordinates decreased by given numer
  30. float3 operator-(const float i) const { return float3(x - i, y - i, z - (si32)i); }
  31. // returns float3 with plane coordinates decreased by given numer
  32. float3 operator*(const float i) const {return float3(x * i, y * i, z);}
  33. // returns float3 with plane coordinates decreased by given numer
  34. float3 operator/(const float i) const {return float3(x / i, y / i, z);}
  35. // returns opposite position
  36. float3 operator-() const { return float3(-x, -y, -z); }
  37. // returns squared distance on Oxy plane (z coord is not used)
  38. double dist2dSQ(const float3 & o) const
  39. {
  40. const double dx = (x - o.x);
  41. const double dy = (y - o.y);
  42. return dx*dx + dy*dy;
  43. }
  44. double mag() const
  45. {
  46. return sqrt(x*x + y*y);
  47. }
  48. float3 unitVector() const
  49. {
  50. return float3(x, y, z) / (float)mag();
  51. }
  52. // returns distance on Oxy plane (z coord is not used)
  53. double dist2d(const float3 &other) const { return std::sqrt(dist2dSQ(other)); }
  54. bool areNeighbours(const float3 &other) const { return (dist2dSQ(other) < 4.0) && z == other.z; }
  55. float3& operator+=(const float3 & i)
  56. {
  57. x += i.x;
  58. y += i.y;
  59. z += i.z;
  60. return *this;
  61. }
  62. float3& operator+=(const float & i)
  63. {
  64. x += i;
  65. y += i;
  66. z += (si32)i;
  67. return *this;
  68. }
  69. float3& operator-=(const float3 & i)
  70. {
  71. x -= i.x;
  72. y -= i.y;
  73. z -= i.z;
  74. return *this;
  75. }
  76. float3& operator-=(const float & i)
  77. {
  78. x += i;
  79. y += i;
  80. z += (si32)i;
  81. return *this;
  82. }
  83. // scale on plane
  84. float3& operator*=(const float & i)
  85. {
  86. x *= i;
  87. y *= i;
  88. return *this;
  89. }
  90. // scale on plane
  91. float3& operator/=(const float & i)
  92. {
  93. x /= i;
  94. y /= i;
  95. return *this;
  96. }
  97. bool operator==(const float3 & i) const { return (x == i.x) && (y == i.y) && (z == i.z); }
  98. bool operator!=(const float3 & i) const { return (x != i.x) || (y != i.y) || (z != i.z); }
  99. bool operator<(const float3 & i) const
  100. {
  101. if (z<i.z)
  102. return true;
  103. if (z>i.z)
  104. return false;
  105. if (y<i.y)
  106. return true;
  107. if (y>i.y)
  108. return false;
  109. if (x<i.x)
  110. return true;
  111. if (x>i.x)
  112. return false;
  113. return false;
  114. }
  115. std::string toString() const
  116. {
  117. return "(" + std::to_string(x) +
  118. " " + std::to_string(y) +
  119. " " + std::to_string(z) + ")";
  120. }
  121. bool valid() const
  122. {
  123. return z >= 0; //minimal condition that needs to be fulfilled for tiles in the map
  124. }
  125. template <typename Handler> void serialize(Handler &h, const float version)
  126. {
  127. h & x;
  128. h & y;
  129. h & z;
  130. }
  131. };
  132. struct Shashfloat3
  133. {
  134. size_t operator()(float3 const& pos) const
  135. {
  136. size_t ret = std::hash<float>()(pos.x);
  137. vstd::hash_combine(ret, pos.y);
  138. vstd::hash_combine(ret, pos.z);
  139. return ret;
  140. }
  141. };
  142. VCMI_LIB_NAMESPACE_END