Vec2s 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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_VEC2S
  14. #define OSG_VEC2S 1
  15. namespace osg {
  16. class Vec2s
  17. {
  18. public:
  19. /** Data type of vector components.*/
  20. typedef short value_type;
  21. /** Number of vector components. */
  22. enum { num_components = 2 };
  23. value_type _v[2];
  24. /** Constructor that sets all components of the vector to zero */
  25. Vec2s() { _v[0]=0; _v[1]=0; }
  26. Vec2s(value_type x, value_type y) { _v[0] = x; _v[1] = y; }
  27. inline bool operator == (const Vec2s& v) const { return _v[0]==v._v[0] && _v[1]==v._v[1]; }
  28. inline bool operator != (const Vec2s& v) const { return _v[0]!=v._v[0] || _v[1]!=v._v[1]; }
  29. inline bool operator < (const Vec2s& v) const
  30. {
  31. if (_v[0]<v._v[0]) return true;
  32. else if (_v[0]>v._v[0]) return false;
  33. else return (_v[1]<v._v[1]);
  34. }
  35. inline value_type* ptr() { return _v; }
  36. inline const value_type* ptr() const { return _v; }
  37. inline void set( value_type x, value_type y)
  38. {
  39. _v[0]=x; _v[1]=y;
  40. }
  41. inline void set( const Vec2s& rhs)
  42. {
  43. _v[0]=rhs._v[0]; _v[1]=rhs._v[1];
  44. }
  45. inline value_type& operator [] (int i) { return _v[i]; }
  46. inline value_type operator [] (int i) const { return _v[i]; }
  47. inline value_type& x() { return _v[0]; }
  48. inline value_type& y() { return _v[1]; }
  49. inline value_type x() const { return _v[0]; }
  50. inline value_type y() const { return _v[1]; }
  51. inline value_type& r() { return _v[0]; }
  52. inline value_type& g() { return _v[1]; }
  53. inline value_type r() const { return _v[0]; }
  54. inline value_type g() const { return _v[1]; }
  55. inline Vec2s operator * (value_type rhs) const
  56. {
  57. return Vec2s(_v[0]*rhs, _v[1]*rhs);
  58. }
  59. inline Vec2s& operator *= (value_type rhs)
  60. {
  61. _v[0]*=rhs;
  62. _v[1]*=rhs;
  63. return *this;
  64. }
  65. inline Vec2s operator / (value_type rhs) const
  66. {
  67. return Vec2s(_v[0]/rhs, _v[1]/rhs);
  68. }
  69. inline Vec2s& operator /= (value_type rhs)
  70. {
  71. _v[0]/=rhs;
  72. _v[1]/=rhs;
  73. return *this;
  74. }
  75. inline Vec2s operator * (const Vec2s& rhs) const
  76. {
  77. return Vec2s(_v[0]*rhs._v[0], _v[1]*rhs._v[1]);
  78. }
  79. inline Vec2s operator + (const Vec2s& rhs) const
  80. {
  81. return Vec2s(_v[0]+rhs._v[0], _v[1]+rhs._v[1]);
  82. }
  83. inline Vec2s& operator += (const Vec2s& rhs)
  84. {
  85. _v[0] += rhs._v[0];
  86. _v[1] += rhs._v[1];
  87. return *this;
  88. }
  89. inline Vec2s operator - (const Vec2s& rhs) const
  90. {
  91. return Vec2s(_v[0]-rhs._v[0], _v[1]-rhs._v[1]);
  92. }
  93. inline Vec2s& operator -= (const Vec2s& rhs)
  94. {
  95. _v[0]-=rhs._v[0];
  96. _v[1]-=rhs._v[1];
  97. return *this;
  98. }
  99. inline Vec2s operator - () const
  100. {
  101. return Vec2s (-_v[0], -_v[1]);
  102. }
  103. }; // end of class Vec2s
  104. /** multiply by vector components. */
  105. inline Vec2s componentMultiply(const Vec2s& lhs, const Vec2s& rhs)
  106. {
  107. return Vec2s(lhs[0]*rhs[0], lhs[1]*rhs[1]);
  108. }
  109. /** divide rhs components by rhs vector components. */
  110. inline Vec2s componentDivide(const Vec2s& lhs, const Vec2s& rhs)
  111. {
  112. return Vec2s(lhs[0]/rhs[0], lhs[1]/rhs[1]);
  113. }
  114. } // end of namespace osg
  115. #endif