int3.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #ifndef __INT3_H__
  2. #define __INT3_H__
  3. /*
  4. * int3.h, part of VCMI engine
  5. *
  6. * Authors: listed in file AUTHORS in main folder
  7. *
  8. * License: GNU General Public License v2.0 or later
  9. * Full text of license available in license.txt file, in main folder
  10. *
  11. */
  12. #include <cmath>
  13. class int3
  14. {
  15. public:
  16. si32 x,y,z;
  17. inline int3():x(0),y(0),z(0){}; //c-tor, x/y/z initialized to 0
  18. inline int3(const si32 & X, const si32 & Y, const si32 & Z):x(X),y(Y),z(Z){}; //c-tor
  19. inline int3(const int3 & val) : x(val.x), y(val.y), z(val.z){} //copy c-tor
  20. inline int3 operator=(const int3 & val) {x = val.x; y = val.y; z = val.z; return *this;} //assignemt operator
  21. ~int3() {} // d-tor - does nothing
  22. inline int3 operator+(const int3 & i) const //returns int3 with coordinates increased by corresponding coordinate of given int3
  23. {return int3(x+i.x,y+i.y,z+i.z);}
  24. inline int3 operator+(const si32 i) const //returns int3 with coordinates increased by given numer
  25. {return int3(x+i,y+i,z+i);}
  26. inline int3 operator-(const int3 & i) const //returns int3 with coordinates decreased by corresponding coordinate of given int3
  27. {return int3(x-i.x,y-i.y,z-i.z);}
  28. inline int3 operator-(const si32 i) const //returns int3 with coordinates decreased by given numer
  29. {return int3(x-i,y-i,z-i);}
  30. inline int3 operator-() const //returns opposite position
  31. {return int3(-x,-y,-z);}
  32. inline double dist2d(const int3 other) const //distance (z coord is not used)
  33. {return std::sqrt((double)(x-other.x)*(x-other.x) + (y-other.y)*(y-other.y));}
  34. inline void operator+=(const int3 & i)
  35. {
  36. x+=i.x;
  37. y+=i.y;
  38. z+=i.z;
  39. }
  40. inline void operator+=(const si32 & i)
  41. {
  42. x+=i;
  43. y+=i;
  44. z+=i;
  45. }
  46. inline void operator-=(const int3 & i)
  47. {
  48. x-=i.x;
  49. y-=i.y;
  50. z-=i.z;
  51. }
  52. inline void operator-=(const si32 & i)
  53. {
  54. x+=i;
  55. y+=i;
  56. z+=i;
  57. }
  58. inline bool operator==(const int3 & i) const
  59. {return (x==i.x) && (y==i.y) && (z==i.z);}
  60. inline bool operator!=(const int3 & i) const
  61. {return !(*this==i);}
  62. inline bool operator<(const int3 & i) const
  63. {
  64. if (z<i.z)
  65. return true;
  66. if (z>i.z)
  67. return false;
  68. if (y<i.y)
  69. return true;
  70. if (y>i.y)
  71. return false;
  72. if (x<i.x)
  73. return true;
  74. if (x>i.x)
  75. return false;
  76. return false;
  77. }
  78. template <typename Handler> void serialize(Handler &h, const int version)
  79. {
  80. h & x & y & z;
  81. }
  82. };
  83. inline std::istream & operator>>(std::istream & str, int3 & dest)
  84. {
  85. str>>dest.x>>dest.y>>dest.z;
  86. return str;
  87. }
  88. inline std::ostream & operator<<(std::ostream & str, const int3 & sth)
  89. {
  90. return str<<sth.x<<' '<<sth.y<<' '<<sth.z;
  91. }
  92. #endif // __INT3_H__