Vec3d 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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_VEC3D
  14. #define OSG_VEC3D 1
  15. #include <osg/Vec2d>
  16. #include <osg/Vec3f>
  17. namespace osg {
  18. /** General purpose double triple for use as vertices, vectors and normals.
  19. * Provides general math operations from addition through to cross products.
  20. * No support yet added for double * Vec3d - is it necessary?
  21. * Need to define a non-member non-friend operator* etc.
  22. * Vec3d * double is okay
  23. */
  24. class Vec3d
  25. {
  26. public:
  27. /** Data type of vector components.*/
  28. typedef double value_type;
  29. /** Number of vector components. */
  30. enum { num_components = 3 };
  31. value_type _v[3];
  32. /** Constructor that sets all components of the vector to zero */
  33. Vec3d() { _v[0]=0.0; _v[1]=0.0; _v[2]=0.0;}
  34. inline Vec3d(const Vec3f& vec) { _v[0]=vec._v[0]; _v[1]=vec._v[1]; _v[2]=vec._v[2];}
  35. inline operator Vec3f() const { return Vec3f(static_cast<float>(_v[0]),static_cast<float>(_v[1]),static_cast<float>(_v[2]));}
  36. Vec3d(value_type x,value_type y,value_type z) { _v[0]=x; _v[1]=y; _v[2]=z; }
  37. Vec3d(const Vec2d& v2,value_type zz)
  38. {
  39. _v[0] = v2[0];
  40. _v[1] = v2[1];
  41. _v[2] = zz;
  42. }
  43. inline bool operator == (const Vec3d& v) const { return _v[0]==v._v[0] && _v[1]==v._v[1] && _v[2]==v._v[2]; }
  44. inline bool operator != (const Vec3d& v) const { return _v[0]!=v._v[0] || _v[1]!=v._v[1] || _v[2]!=v._v[2]; }
  45. inline bool operator < (const Vec3d& v) const
  46. {
  47. if (_v[0]<v._v[0]) return true;
  48. else if (_v[0]>v._v[0]) return false;
  49. else if (_v[1]<v._v[1]) return true;
  50. else if (_v[1]>v._v[1]) return false;
  51. else return (_v[2]<v._v[2]);
  52. }
  53. inline value_type* ptr() { return _v; }
  54. inline const value_type* ptr() const { return _v; }
  55. inline void set( value_type x, value_type y, value_type z)
  56. {
  57. _v[0]=x; _v[1]=y; _v[2]=z;
  58. }
  59. inline void set( const Vec3d& rhs)
  60. {
  61. _v[0]=rhs._v[0]; _v[1]=rhs._v[1]; _v[2]=rhs._v[2];
  62. }
  63. inline value_type& operator [] (int i) { return _v[i]; }
  64. inline value_type operator [] (int i) const { return _v[i]; }
  65. inline value_type& x() { return _v[0]; }
  66. inline value_type& y() { return _v[1]; }
  67. inline value_type& z() { return _v[2]; }
  68. inline value_type x() const { return _v[0]; }
  69. inline value_type y() const { return _v[1]; }
  70. inline value_type z() const { return _v[2]; }
  71. /** Returns true if all components have values that are not NaN. */
  72. inline bool valid() const { return !isNaN(); }
  73. /** Returns true if at least one component has value NaN. */
  74. inline bool isNaN() const { return osg::isNaN(_v[0]) || osg::isNaN(_v[1]) || osg::isNaN(_v[2]); }
  75. /** Dot product. */
  76. inline value_type operator * (const Vec3d& rhs) const
  77. {
  78. return _v[0]*rhs._v[0]+_v[1]*rhs._v[1]+_v[2]*rhs._v[2];
  79. }
  80. /** Cross product. */
  81. inline const Vec3d operator ^ (const Vec3d& rhs) const
  82. {
  83. return Vec3d(_v[1]*rhs._v[2]-_v[2]*rhs._v[1],
  84. _v[2]*rhs._v[0]-_v[0]*rhs._v[2] ,
  85. _v[0]*rhs._v[1]-_v[1]*rhs._v[0]);
  86. }
  87. /** Multiply by scalar. */
  88. inline const Vec3d operator * (value_type rhs) const
  89. {
  90. return Vec3d(_v[0]*rhs, _v[1]*rhs, _v[2]*rhs);
  91. }
  92. /** Unary multiply by scalar. */
  93. inline Vec3d& operator *= (value_type rhs)
  94. {
  95. _v[0]*=rhs;
  96. _v[1]*=rhs;
  97. _v[2]*=rhs;
  98. return *this;
  99. }
  100. /** Divide by scalar. */
  101. inline const Vec3d operator / (value_type rhs) const
  102. {
  103. return Vec3d(_v[0]/rhs, _v[1]/rhs, _v[2]/rhs);
  104. }
  105. /** Unary divide by scalar. */
  106. inline Vec3d& operator /= (value_type rhs)
  107. {
  108. _v[0]/=rhs;
  109. _v[1]/=rhs;
  110. _v[2]/=rhs;
  111. return *this;
  112. }
  113. /** Binary vector add. */
  114. inline const Vec3d operator + (const Vec3d& rhs) const
  115. {
  116. return Vec3d(_v[0]+rhs._v[0], _v[1]+rhs._v[1], _v[2]+rhs._v[2]);
  117. }
  118. /** Unary vector add. Slightly more efficient because no temporary
  119. * intermediate object.
  120. */
  121. inline Vec3d& operator += (const Vec3d& rhs)
  122. {
  123. _v[0] += rhs._v[0];
  124. _v[1] += rhs._v[1];
  125. _v[2] += rhs._v[2];
  126. return *this;
  127. }
  128. /** Binary vector subtract. */
  129. inline const Vec3d operator - (const Vec3d& rhs) const
  130. {
  131. return Vec3d(_v[0]-rhs._v[0], _v[1]-rhs._v[1], _v[2]-rhs._v[2]);
  132. }
  133. /** Unary vector subtract. */
  134. inline Vec3d& operator -= (const Vec3d& rhs)
  135. {
  136. _v[0]-=rhs._v[0];
  137. _v[1]-=rhs._v[1];
  138. _v[2]-=rhs._v[2];
  139. return *this;
  140. }
  141. /** Negation operator. Returns the negative of the Vec3d. */
  142. inline const Vec3d operator - () const
  143. {
  144. return Vec3d (-_v[0], -_v[1], -_v[2]);
  145. }
  146. /** Length of the vector = sqrt( vec . vec ) */
  147. inline value_type length() const
  148. {
  149. return sqrt( _v[0]*_v[0] + _v[1]*_v[1] + _v[2]*_v[2] );
  150. }
  151. /** Length squared of the vector = vec . vec */
  152. inline value_type length2() const
  153. {
  154. return _v[0]*_v[0] + _v[1]*_v[1] + _v[2]*_v[2];
  155. }
  156. /** Normalize the vector so that it has length unity.
  157. * Returns the previous length of the vector.
  158. * If the vector is zero length, it is left unchanged and zero is returned.
  159. */
  160. inline value_type normalize()
  161. {
  162. value_type norm = Vec3d::length();
  163. if (norm>0.0)
  164. {
  165. value_type inv = 1.0/norm;
  166. _v[0] *= inv;
  167. _v[1] *= inv;
  168. _v[2] *= inv;
  169. }
  170. return( norm );
  171. }
  172. }; // end of class Vec3d
  173. /** multiply by vector components. */
  174. inline Vec3d componentMultiply(const Vec3d& lhs, const Vec3d& rhs)
  175. {
  176. return Vec3d(lhs[0]*rhs[0], lhs[1]*rhs[1], lhs[2]*rhs[2]);
  177. }
  178. /** divide rhs components by rhs vector components. */
  179. inline Vec3d componentDivide(const Vec3d& lhs, const Vec3d& rhs)
  180. {
  181. return Vec3d(lhs[0]/rhs[0], lhs[1]/rhs[1], lhs[2]/rhs[2]);
  182. }
  183. } // end of namespace osg
  184. #endif