Vec3ub 5.2 KB

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