Vec3f 6.8 KB

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