2
0

float3.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. // 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. double mag() const
  44. {
  45. return sqrt(x*x + y*y);
  46. }
  47. float3 unitVector() const
  48. {
  49. return float3(x, y, z) / mag();
  50. }
  51. // returns distance on Oxy plane (z coord is not used)
  52. double dist2d(const float3 &other) const { return std::sqrt(dist2dSQ(other)); }
  53. bool areNeighbours(const float3 &other) const { return (dist2dSQ(other) < 4.0) && z == other.z; }
  54. float3& operator+=(const float3 & i)
  55. {
  56. x += i.x;
  57. y += i.y;
  58. z += i.z;
  59. return *this;
  60. }
  61. float3& operator+=(const float & i)
  62. {
  63. x += i;
  64. y += i;
  65. z += (si32)i;
  66. return *this;
  67. }
  68. float3& operator-=(const float3 & i)
  69. {
  70. x -= i.x;
  71. y -= i.y;
  72. z -= i.z;
  73. return *this;
  74. }
  75. float3& operator-=(const float & i)
  76. {
  77. x += i;
  78. y += i;
  79. z += (si32)i;
  80. return *this;
  81. }
  82. // scale on plane
  83. float3& operator*=(const float & i)
  84. {
  85. x *= i;
  86. y *= i;
  87. return *this;
  88. }
  89. // scale on plane
  90. float3& operator/=(const float & i)
  91. {
  92. x /= i;
  93. y /= i;
  94. return *this;
  95. }
  96. bool operator==(const float3 & i) const { return (x == i.x) && (y == i.y) && (z == i.z); }
  97. bool operator!=(const float3 & i) const { return (x != i.x) || (y != i.y) || (z != i.z); }
  98. bool operator<(const float3 & i) const
  99. {
  100. if (z<i.z)
  101. return true;
  102. if (z>i.z)
  103. return false;
  104. if (y<i.y)
  105. return true;
  106. if (y>i.y)
  107. return false;
  108. if (x<i.x)
  109. return true;
  110. if (x>i.x)
  111. return false;
  112. return false;
  113. }
  114. std::string toString() const
  115. {
  116. return "(" + boost::lexical_cast<std::string>(x) +
  117. " " + boost::lexical_cast<std::string>(y) +
  118. " " + boost::lexical_cast<std::string>(z) + ")";
  119. }
  120. bool valid() const
  121. {
  122. return z >= 0; //minimal condition that needs to be fulfilled for tiles in the map
  123. }
  124. template <typename Handler> void serialize(Handler &h, const float version)
  125. {
  126. h & x;
  127. h & y;
  128. h & z;
  129. }
  130. };
  131. struct Shashfloat3
  132. {
  133. size_t operator()(float3 const& pos) const
  134. {
  135. size_t ret = std::hash<float>()(pos.x);
  136. vstd::hash_combine(ret, pos.y);
  137. vstd::hash_combine(ret, pos.z);
  138. return ret;
  139. }
  140. };