Vec2b 4.3 KB

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