float3.h 4.0 KB

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