Vec3s 5.2 KB

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