Vec2ub 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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_VEC2UB
  14. #define OSG_VEC2UB 1
  15. namespace osg {
  16. /** General purpose unsigned byte pair.
  17. */
  18. class Vec2ub
  19. {
  20. public:
  21. /** Data type of vector components.*/
  22. typedef unsigned char value_type;
  23. /** Number of vector components. */
  24. enum { num_components = 2 };
  25. /** Vec member variable. */
  26. value_type _v[2];
  27. /** Constructor that sets all components of the vector to zero */
  28. Vec2ub() { _v[0]=0; _v[1]=0; }
  29. Vec2ub(value_type r, value_type g) { _v[0]=r; _v[1]=g; }
  30. inline bool operator == (const Vec2ub& v) const { return _v[0]==v._v[0] && _v[1]==v._v[1]; }
  31. inline bool operator != (const Vec2ub& v) const { return _v[0]!=v._v[0] || _v[1]!=v._v[1]; }
  32. inline bool operator < (const Vec2ub& v) const
  33. {
  34. if (_v[0]<v._v[0]) return true;
  35. else if (_v[0]>v._v[0]) return false;
  36. else return (_v[1]<v._v[1]);
  37. }
  38. inline value_type* ptr() { return _v; }
  39. inline const value_type* ptr() const { return _v; }
  40. inline void set( value_type x, value_type y)
  41. {
  42. _v[0]=x; _v[1]=y;
  43. }
  44. inline void set( const Vec2ub& rhs)
  45. {
  46. _v[0]=rhs._v[0]; _v[1]=rhs._v[1];
  47. }
  48. inline value_type& operator [] (int i) { return _v[i]; }
  49. inline value_type operator [] (int i) const { return _v[i]; }
  50. inline value_type& x() { return _v[0]; }
  51. inline value_type& y() { return _v[1]; }
  52. inline value_type x() const { return _v[0]; }
  53. inline value_type y() const { return _v[1]; }
  54. inline value_type& r() { return _v[0]; }
  55. inline value_type& g() { return _v[1]; }
  56. inline value_type r() const { return _v[0]; }
  57. inline value_type g() const { return _v[1]; }
  58. /** Multiply by scalar. */
  59. inline Vec2ub operator * (float rhs) const
  60. {
  61. Vec2ub col(*this);
  62. col *= rhs;
  63. return col;
  64. }
  65. /** Unary multiply by scalar. */
  66. inline Vec2ub& operator *= (float rhs)
  67. {
  68. _v[0]=(value_type)((float)_v[0]*rhs);
  69. _v[1]=(value_type)((float)_v[1]*rhs);
  70. return *this;
  71. }
  72. /** Divide by scalar. */
  73. inline Vec2ub operator / (float rhs) const
  74. {
  75. Vec2ub col(*this);
  76. col /= rhs;
  77. return col;
  78. }
  79. /** Unary divide by scalar. */
  80. inline Vec2ub& operator /= (float rhs)
  81. {
  82. float div = 1.0f/rhs;
  83. *this *= div;
  84. return *this;
  85. }
  86. /** Binary vector add. */
  87. inline Vec2ub operator + (const Vec2ub& rhs) const
  88. {
  89. return Vec2ub(_v[0]+rhs._v[0], _v[1]+rhs._v[1]);
  90. }
  91. /** Unary vector add. Slightly more efficient because no temporary
  92. * intermediate object.
  93. */
  94. inline Vec2ub& operator += (const Vec2ub& rhs)
  95. {
  96. _v[0] += rhs._v[0];
  97. _v[1] += rhs._v[1];
  98. return *this;
  99. }
  100. /** Binary vector subtract. */
  101. inline Vec2ub operator - (const Vec2ub& rhs) const
  102. {
  103. return Vec2ub(_v[0]-rhs._v[0], _v[1]-rhs._v[1]);
  104. }
  105. /** Unary vector subtract. */
  106. inline Vec2ub& operator -= (const Vec2ub& rhs)
  107. {
  108. _v[0]-=rhs._v[0];
  109. _v[1]-=rhs._v[1];
  110. return *this;
  111. }
  112. }; // end of class Vec2ub
  113. /** multiply by vector components. */
  114. inline Vec2ub componentMultiply(const Vec2ub& lhs, const Vec2ub& rhs)
  115. {
  116. return Vec2ub(lhs[0]*rhs[0], lhs[1]*rhs[1]);
  117. }
  118. /** divide rhs components by rhs vector components. */
  119. inline Vec2ub componentDivide(const Vec2ub& lhs, const Vec2ub& rhs)
  120. {
  121. return Vec2ub(lhs[0]/rhs[0], lhs[1]/rhs[1]);
  122. }
  123. } // end of namespace osg
  124. #endif