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