Vec2i 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
  2. *
  3. * This library is open source and may be redistributed and/or modified under
  4. * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
  5. * (at your option) any later version. The full license is in LICENSE file
  6. * included with this distribution, and on the openscenegraph.org website.
  7. *
  8. * This library is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * OpenSceneGraph Public License for more details.
  12. */
  13. #ifndef OSG_VEC2I
  14. #define OSG_VEC2I 1
  15. namespace osg {
  16. /** General purpose integer pair.
  17. */
  18. class Vec2i
  19. {
  20. public:
  21. /** Type of Vec class.*/
  22. typedef int value_type;
  23. /** Number of vector components. */
  24. enum { num_components = 2 };
  25. /** Vec member variable. */
  26. value_type _v[2];
  27. Vec2i() { _v[0]=0; _v[1]=0; }
  28. Vec2i(value_type x, value_type y) { _v[0] = x; _v[1] = y; }
  29. inline bool operator == (const Vec2i& v) const { return _v[0]==v._v[0] && _v[1]==v._v[1]; }
  30. inline bool operator != (const Vec2i& v) const { return _v[0]!=v._v[0] || _v[1]!=v._v[1]; }
  31. inline bool operator < (const Vec2i& v) const
  32. {
  33. if (_v[0]<v._v[0]) return true;
  34. else if (_v[0]>v._v[0]) return false;
  35. else return (_v[1]<v._v[1]);
  36. }
  37. inline value_type* ptr() { return _v; }
  38. inline const value_type* ptr() const { return _v; }
  39. inline void set( value_type x, value_type y)
  40. {
  41. _v[0]=x; _v[1]=y;
  42. }
  43. inline void set( const Vec2i& rhs)
  44. {
  45. _v[0]=rhs._v[0]; _v[1]=rhs._v[1];
  46. }
  47. inline value_type& operator [] (int i) { return _v[i]; }
  48. inline value_type operator [] (int i) const { return _v[i]; }
  49. inline value_type& x() { return _v[0]; }
  50. inline value_type& y() { return _v[1]; }
  51. inline value_type x() const { return _v[0]; }
  52. inline value_type y() const { return _v[1]; }
  53. inline value_type& r() { return _v[0]; }
  54. inline value_type& g() { return _v[1]; }
  55. inline value_type r() const { return _v[0]; }
  56. inline value_type g() const { return _v[1]; }
  57. inline Vec2i operator * (value_type rhs) const
  58. {
  59. return Vec2i(_v[0]*rhs, _v[1]*rhs);
  60. }
  61. inline Vec2i operator / (value_type rhs) const
  62. {
  63. return Vec2i(_v[0]/rhs, _v[1]/rhs);
  64. }
  65. inline Vec2i operator + (value_type rhs) const
  66. {
  67. return Vec2i(_v[0]+rhs, _v[1]+rhs);
  68. }
  69. inline Vec2i operator - (value_type rhs) const
  70. {
  71. return Vec2i(_v[0]-rhs, _v[1]-rhs);
  72. }
  73. inline Vec2i operator + (const Vec2i& rhs) const
  74. {
  75. return Vec2i(_v[0]+rhs._v[0], _v[1]+rhs._v[1]);
  76. }
  77. inline Vec2i operator - (const Vec2i& rhs) const
  78. {
  79. return Vec2i(_v[0]-rhs._v[0], _v[1]-rhs._v[1]);
  80. }
  81. inline Vec2i operator * (const Vec2i& rhs) const
  82. {
  83. return Vec2i(_v[0]*rhs._v[0], _v[1]*rhs._v[1]);
  84. }
  85. };
  86. }
  87. #endif