Vec4d 9.5 KB

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