int3.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. #pragma once
  2. /*
  3. * int3.h, part of VCMI engine
  4. *
  5. * Authors: listed in file AUTHORS in main folder
  6. *
  7. * License: GNU General Public License v2.0 or later
  8. * Full text of license available in license.txt file, in main folder
  9. *
  10. */
  11. /// Class which consists of three integer values. Represents position on adventure map.
  12. class int3
  13. {
  14. public:
  15. si32 x, y, z;
  16. //c-tor: x, y, z initialized to 0
  17. int3() : x(0), y(0), z(0) {} // I think that x, y, z should be left uninitialized.
  18. //c-tor: x, y, z initialized to i
  19. explicit int3(const si32 i) : x(i), y(i), z(i) {}
  20. //c-tor: x, y, z initialized to X, Y, Z
  21. int3(const si32 X, const si32 Y, const si32 Z) : x(X), y(Y), z(Z) {}
  22. int3(const int3 & c) : x(c.x), y(c.y), z(c.z) {} // Should be set to default (C++11)?
  23. int3 & operator=(const int3 & c) // Should be set to default (C++11)?
  24. {
  25. x = c.x;
  26. y = c.y;
  27. z = c.z;
  28. return *this;
  29. }
  30. int3 operator-() const { return int3(-x, -y, -z); }
  31. int3 operator+(const int3 & i) const { return int3(x + i.x, y + i.y, z + i.z); }
  32. int3 operator-(const int3 & i) const { return int3(x - i.x, y - i.y, z - i.z); }
  33. //returns int3 with coordinates increased by given number
  34. int3 operator+(const si32 i) const { return int3(x + i, y + i, z + i); }
  35. //returns int3 with coordinates decreased by given number
  36. int3 operator-(const si32 i) const { return int3(x - i, y - i, z - i); }
  37. int3 & operator+=(const int3 & i)
  38. {
  39. x += i.x;
  40. y += i.y;
  41. z += i.z;
  42. return *this;
  43. }
  44. int3 & operator-=(const int3 & i)
  45. {
  46. x -= i.x;
  47. y -= i.y;
  48. z -= i.z;
  49. return *this;
  50. }
  51. //increases all coordinates by given number
  52. int3 & operator+=(const si32 i)
  53. {
  54. x += i;
  55. y += i;
  56. z += i;
  57. return *this;
  58. }
  59. //decreases all coordinates by given number
  60. int3 & operator-=(const si32 i)
  61. {
  62. x -= i;
  63. y -= i;
  64. z -= i;
  65. return *this;
  66. }
  67. bool operator==(const int3 & i) const { return (x == i.x && y == i.y && z == i.z); }
  68. bool operator!=(const int3 & i) const { return (x != i.x || y != i.y || z != i.z); }
  69. bool operator<(const int3 & i) const
  70. {
  71. if (z < i.z)
  72. return true;
  73. if (z > i.z)
  74. return false;
  75. if (y < i.y)
  76. return true;
  77. if (y > i.y)
  78. return false;
  79. if (x < i.x)
  80. return true;
  81. if (x > i.x)
  82. return false;
  83. return false;
  84. }
  85. //returns squared distance on Oxy plane (z coord is not used)
  86. ui32 dist2dSQ(const int3 & o) const
  87. {
  88. const si32 dx = (x - o.x);
  89. const si32 dy = (y - o.y);
  90. return (ui32)(dx*dx) + (ui32)(dy*dy);
  91. }
  92. //returns distance on Oxy plane (z coord is not used)
  93. double dist2d(const int3 & o) const
  94. {
  95. return std::sqrt((double)dist2dSQ(o));
  96. }
  97. bool areNeighbours(const int3 & o) const
  98. {
  99. return (dist2dSQ(o) < 4) && (z == o.z);
  100. }
  101. //returns "(x y z)" string
  102. std::string operator ()() const //Change to int3::toString()?
  103. {
  104. std::string result("(");
  105. result += boost::lexical_cast<std::string>(x); result += ' ';
  106. result += boost::lexical_cast<std::string>(y); result += ' ';
  107. result += boost::lexical_cast<std::string>(z); result += ')';
  108. return result;
  109. }
  110. bool valid() const //Should be named "isValid"?
  111. {
  112. return z >= 0; //minimal condition that needs to be fulfilled for tiles in the map
  113. }
  114. template <typename Handler>
  115. void serialize(Handler &h, const int version)
  116. {
  117. h & x & y & z;
  118. }
  119. static std::array<int3, 8> getDirs()
  120. {
  121. return { { int3(0,1,0),int3(0,-1,0),int3(-1,0,0),int3(+1,0,0),
  122. int3(1,1,0),int3(-1,1,0),int3(1,-1,0),int3(-1,-1,0) } };
  123. }
  124. };
  125. inline std::ostream & operator<<(std::ostream & str, const int3 & sth)
  126. {
  127. return str << sth.x << ' ' << sth.y << ' ' << sth.z;
  128. }
  129. inline std::istream & operator>>(std::istream & str, int3 & dest)
  130. {
  131. return str >> dest.x >> dest.y >> dest.z;
  132. }
  133. //Why not normal function?
  134. struct ShashInt3
  135. {
  136. size_t operator()(int3 const& pos) const
  137. {
  138. size_t ret = std::hash<int>()(pos.x);
  139. vstd::hash_combine(ret, pos.y);
  140. vstd::hash_combine(ret, pos.z);
  141. return ret;
  142. }
  143. };
  144. template<typename Container>
  145. int3 findClosestTile (Container & container, int3 dest)
  146. {
  147. static_assert(std::is_same<typename Container::value_type, int3>::value,
  148. "findClosestTile requires <int3> container.");
  149. int3 result(-1, -1, -1);
  150. ui32 distance = std::numeric_limits<ui32>::max();
  151. for (const int3& tile : container)
  152. {
  153. const ui32 currentDistance = dest.dist2dSQ(tile);
  154. if (currentDistance < distance)
  155. {
  156. result = tile;
  157. distance = currentDistance;
  158. }
  159. }
  160. return result;
  161. }