float3.h 3.7 KB

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