Vec4f 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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_VEC4F
  14. #define OSG_VEC4F 1
  15. #include <osg/Vec3f>
  16. namespace osg {
  17. /** General purpose float quad. Uses include representation
  18. * of color coordinates.
  19. * No support yet added for float * Vec4f - is it necessary?
  20. * Need to define a non-member non-friend operator* etc.
  21. * Vec4f * float is okay
  22. */
  23. class Vec4f
  24. {
  25. public:
  26. /** Data type of vector components.*/
  27. typedef float value_type;
  28. /** Number of vector components. */
  29. enum { num_components = 4 };
  30. /** Vec member variable. */
  31. value_type _v[4];
  32. // Methods are defined here so that they are implicitly inlined
  33. /** Constructor that sets all components of the vector to zero */
  34. Vec4f() { _v[0]=0.0f; _v[1]=0.0f; _v[2]=0.0f; _v[3]=0.0f;}
  35. Vec4f(value_type x, value_type y, value_type z, value_type w)
  36. {
  37. _v[0]=x;
  38. _v[1]=y;
  39. _v[2]=z;
  40. _v[3]=w;
  41. }
  42. Vec4f(const Vec3f& v3,value_type w)
  43. {
  44. _v[0]=v3[0];
  45. _v[1]=v3[1];
  46. _v[2]=v3[2];
  47. _v[3]=w;
  48. }
  49. inline bool operator == (const Vec4f& v) const { return _v[0]==v._v[0] && _v[1]==v._v[1] && _v[2]==v._v[2] && _v[3]==v._v[3]; }
  50. inline bool operator != (const Vec4f& v) const { return _v[0]!=v._v[0] || _v[1]!=v._v[1] || _v[2]!=v._v[2] || _v[3]!=v._v[3]; }
  51. inline bool operator < (const Vec4f& v) const
  52. {
  53. if (_v[0]<v._v[0]) return true;
  54. else if (_v[0]>v._v[0]) return false;
  55. else if (_v[1]<v._v[1]) return true;
  56. else if (_v[1]>v._v[1]) return false;
  57. else if (_v[2]<v._v[2]) return true;
  58. else if (_v[2]>v._v[2]) return false;
  59. else return (_v[3]<v._v[3]);
  60. }
  61. inline value_type* ptr() { return _v; }
  62. inline const value_type* ptr() const { return _v; }
  63. inline void set( value_type x, value_type y, value_type z, value_type w)
  64. {
  65. _v[0]=x; _v[1]=y; _v[2]=z; _v[3]=w;
  66. }
  67. inline value_type& operator [] (unsigned int i) { return _v[i]; }
  68. inline value_type operator [] (unsigned int i) const { return _v[i]; }
  69. inline value_type& x() { return _v[0]; }
  70. inline value_type& y() { return _v[1]; }
  71. inline value_type& z() { return _v[2]; }
  72. inline value_type& w() { return _v[3]; }
  73. inline value_type x() const { return _v[0]; }
  74. inline value_type y() const { return _v[1]; }
  75. inline value_type z() const { return _v[2]; }
  76. inline value_type w() const { return _v[3]; }
  77. inline value_type& r() { return _v[0]; }
  78. inline value_type& g() { return _v[1]; }
  79. inline value_type& b() { return _v[2]; }
  80. inline value_type& a() { return _v[3]; }
  81. inline value_type r() const { return _v[0]; }
  82. inline value_type g() const { return _v[1]; }
  83. inline value_type b() const { return _v[2]; }
  84. inline value_type a() const { return _v[3]; }
  85. inline unsigned int asABGR() const
  86. {
  87. return (unsigned int)clampTo((_v[0]*255.0f),0.0f,255.0f)<<24 |
  88. (unsigned int)clampTo((_v[1]*255.0f),0.0f,255.0f)<<16 |
  89. (unsigned int)clampTo((_v[2]*255.0f),0.0f,255.0f)<<8 |
  90. (unsigned int)clampTo((_v[3]*255.0f),0.0f,255.0f);
  91. }
  92. inline unsigned int asRGBA() const
  93. {
  94. return (unsigned int)clampTo((_v[3]*255.0f),0.0f,255.0f)<<24 |
  95. (unsigned int)clampTo((_v[2]*255.0f),0.0f,255.0f)<<16 |
  96. (unsigned int)clampTo((_v[1]*255.0f),0.0f,255.0f)<<8 |
  97. (unsigned int)clampTo((_v[0]*255.0f),0.0f,255.0f);
  98. }
  99. /** Returns true if all components have values that are not NaN. */
  100. inline bool valid() const { return !isNaN(); }
  101. /** Returns true if at least one component has value NaN. */
  102. inline bool isNaN() const { return osg::isNaN(_v[0]) || osg::isNaN(_v[1]) || osg::isNaN(_v[2]) || osg::isNaN(_v[3]); }
  103. /** Dot product. */
  104. inline value_type operator * (const Vec4f& rhs) const
  105. {
  106. return _v[0]*rhs._v[0]+
  107. _v[1]*rhs._v[1]+
  108. _v[2]*rhs._v[2]+
  109. _v[3]*rhs._v[3] ;
  110. }
  111. /** Multiply by scalar. */
  112. inline Vec4f operator * (value_type rhs) const
  113. {
  114. return Vec4f(_v[0]*rhs, _v[1]*rhs, _v[2]*rhs, _v[3]*rhs);
  115. }
  116. /** Unary multiply by scalar. */
  117. inline Vec4f& operator *= (value_type rhs)
  118. {
  119. _v[0]*=rhs;
  120. _v[1]*=rhs;
  121. _v[2]*=rhs;
  122. _v[3]*=rhs;
  123. return *this;
  124. }
  125. /** Divide by scalar. */
  126. inline Vec4f operator / (value_type rhs) const
  127. {
  128. return Vec4f(_v[0]/rhs, _v[1]/rhs, _v[2]/rhs, _v[3]/rhs);
  129. }
  130. /** Unary divide by scalar. */
  131. inline Vec4f& operator /= (value_type rhs)
  132. {
  133. _v[0]/=rhs;
  134. _v[1]/=rhs;
  135. _v[2]/=rhs;
  136. _v[3]/=rhs;
  137. return *this;
  138. }
  139. /** Binary vector add. */
  140. inline Vec4f operator + (const Vec4f& rhs) const
  141. {
  142. return Vec4f(_v[0]+rhs._v[0], _v[1]+rhs._v[1],
  143. _v[2]+rhs._v[2], _v[3]+rhs._v[3]);
  144. }
  145. /** Unary vector add. Slightly more efficient because no temporary
  146. * intermediate object.
  147. */
  148. inline Vec4f& operator += (const Vec4f& rhs)
  149. {
  150. _v[0] += rhs._v[0];
  151. _v[1] += rhs._v[1];
  152. _v[2] += rhs._v[2];
  153. _v[3] += rhs._v[3];
  154. return *this;
  155. }
  156. /** Binary vector subtract. */
  157. inline Vec4f operator - (const Vec4f& rhs) const
  158. {
  159. return Vec4f(_v[0]-rhs._v[0], _v[1]-rhs._v[1],
  160. _v[2]-rhs._v[2], _v[3]-rhs._v[3] );
  161. }
  162. /** Unary vector subtract. */
  163. inline Vec4f& operator -= (const Vec4f& rhs)
  164. {
  165. _v[0]-=rhs._v[0];
  166. _v[1]-=rhs._v[1];
  167. _v[2]-=rhs._v[2];
  168. _v[3]-=rhs._v[3];
  169. return *this;
  170. }
  171. /** Negation operator. Returns the negative of the Vec4f. */
  172. inline const Vec4f operator - () const
  173. {
  174. return Vec4f (-_v[0], -_v[1], -_v[2], -_v[3]);
  175. }
  176. /** Length of the vector = sqrt( vec . vec ) */
  177. inline value_type length() const
  178. {
  179. return sqrtf( _v[0]*_v[0] + _v[1]*_v[1] + _v[2]*_v[2] + _v[3]*_v[3]);
  180. }
  181. /** Length squared of the vector = vec . vec */
  182. inline value_type length2() const
  183. {
  184. return _v[0]*_v[0] + _v[1]*_v[1] + _v[2]*_v[2] + _v[3]*_v[3];
  185. }
  186. /** Normalize the vector so that it has length unity.
  187. * Returns the previous length of the vector.
  188. */
  189. inline value_type normalize()
  190. {
  191. value_type norm = Vec4f::length();
  192. if (norm>0.0f)
  193. {
  194. value_type inv = 1.0f/norm;
  195. _v[0] *= inv;
  196. _v[1] *= inv;
  197. _v[2] *= inv;
  198. _v[3] *= inv;
  199. }
  200. return( norm );
  201. }
  202. }; // end of class Vec4f
  203. /** Compute the dot product of a (Vec3,1.0) and a Vec4f. */
  204. inline Vec4f::value_type operator * (const Vec3f& lhs,const Vec4f& rhs)
  205. {
  206. return lhs[0]*rhs[0]+lhs[1]*rhs[1]+lhs[2]*rhs[2]+rhs[3];
  207. }
  208. /** Compute the dot product of a Vec4f and a (Vec3,1.0). */
  209. inline Vec4f::value_type operator * (const Vec4f& lhs,const Vec3f& rhs)
  210. {
  211. return lhs[0]*rhs[0]+lhs[1]*rhs[1]+lhs[2]*rhs[2]+lhs[3];
  212. }
  213. /** multiply by vector components. */
  214. inline Vec4f componentMultiply(const Vec4f& lhs, const Vec4f& rhs)
  215. {
  216. return Vec4f(lhs[0]*rhs[0], lhs[1]*rhs[1], lhs[2]*rhs[2], lhs[3]*rhs[3]);
  217. }
  218. /** divide rhs components by rhs vector components. */
  219. inline Vec4f componentDivide(const Vec4f& lhs, const Vec4f& rhs)
  220. {
  221. return Vec4f(lhs[0]/rhs[0], lhs[1]/rhs[1], lhs[2]/rhs[2], lhs[3]/rhs[3]);
  222. }
  223. } // end of namespace osg
  224. #endif