Vec2d 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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_VEC2D
  14. #define OSG_VEC2D 1
  15. #include <osg/Vec2f>
  16. namespace osg {
  17. /** General purpose double pair, uses include representation of
  18. * texture coordinates.
  19. * No support yet added for double * Vec2d - is it necessary?
  20. * Need to define a non-member non-friend operator* etc.
  21. * BTW: Vec2d * double is okay
  22. */
  23. class Vec2d
  24. {
  25. public:
  26. /** Data type of vector components.*/
  27. typedef double value_type;
  28. /** Number of vector components. */
  29. enum { num_components = 2 };
  30. value_type _v[2];
  31. /** Constructor that sets all components of the vector to zero */
  32. Vec2d() {_v[0]=0.0; _v[1]=0.0;}
  33. Vec2d(value_type x,value_type y) { _v[0]=x; _v[1]=y; }
  34. inline Vec2d(const Vec2f& vec) { _v[0]=vec._v[0]; _v[1]=vec._v[1]; }
  35. inline operator Vec2f() const { return Vec2f(static_cast<float>(_v[0]),static_cast<float>(_v[1]));}
  36. inline bool operator == (const Vec2d& v) const { return _v[0]==v._v[0] && _v[1]==v._v[1]; }
  37. inline bool operator != (const Vec2d& v) const { return _v[0]!=v._v[0] || _v[1]!=v._v[1]; }
  38. inline bool operator < (const Vec2d& v) const
  39. {
  40. if (_v[0]<v._v[0]) return true;
  41. else if (_v[0]>v._v[0]) return false;
  42. else return (_v[1]<v._v[1]);
  43. }
  44. inline value_type* ptr() { return _v; }
  45. inline const value_type* ptr() const { return _v; }
  46. inline void set( value_type x, value_type y ) { _v[0]=x; _v[1]=y; }
  47. inline value_type& operator [] (int i) { return _v[i]; }
  48. inline value_type operator [] (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 x() const { return _v[0]; }
  52. inline value_type y() const { return _v[1]; }
  53. /** Returns true if all components have values that are not NaN. */
  54. inline bool valid() const { return !isNaN(); }
  55. /** Returns true if at least one component has value NaN. */
  56. inline bool isNaN() const { return osg::isNaN(_v[0]) || osg::isNaN(_v[1]); }
  57. /** Dot product. */
  58. inline value_type operator * (const Vec2d& rhs) const
  59. {
  60. return _v[0]*rhs._v[0]+_v[1]*rhs._v[1];
  61. }
  62. /** Multiply by scalar. */
  63. inline const Vec2d operator * (value_type rhs) const
  64. {
  65. return Vec2d(_v[0]*rhs, _v[1]*rhs);
  66. }
  67. /** Unary multiply by scalar. */
  68. inline Vec2d& operator *= (value_type rhs)
  69. {
  70. _v[0]*=rhs;
  71. _v[1]*=rhs;
  72. return *this;
  73. }
  74. /** Divide by scalar. */
  75. inline const Vec2d operator / (value_type rhs) const
  76. {
  77. return Vec2d(_v[0]/rhs, _v[1]/rhs);
  78. }
  79. /** Unary divide by scalar. */
  80. inline Vec2d& operator /= (value_type rhs)
  81. {
  82. _v[0]/=rhs;
  83. _v[1]/=rhs;
  84. return *this;
  85. }
  86. /** Binary vector add. */
  87. inline const Vec2d operator + (const Vec2d& rhs) const
  88. {
  89. return Vec2d(_v[0]+rhs._v[0], _v[1]+rhs._v[1]);
  90. }
  91. /** Unary vector add. Slightly more efficient because no temporary
  92. * intermediate object.
  93. */
  94. inline Vec2d& operator += (const Vec2d& rhs)
  95. {
  96. _v[0] += rhs._v[0];
  97. _v[1] += rhs._v[1];
  98. return *this;
  99. }
  100. /** Binary vector subtract. */
  101. inline const Vec2d operator - (const Vec2d& rhs) const
  102. {
  103. return Vec2d(_v[0]-rhs._v[0], _v[1]-rhs._v[1]);
  104. }
  105. /** Unary vector subtract. */
  106. inline Vec2d& operator -= (const Vec2d& rhs)
  107. {
  108. _v[0]-=rhs._v[0];
  109. _v[1]-=rhs._v[1];
  110. return *this;
  111. }
  112. /** Negation operator. Returns the negative of the Vec2d. */
  113. inline const Vec2d operator - () const
  114. {
  115. return Vec2d (-_v[0], -_v[1]);
  116. }
  117. /** Length of the vector = sqrt( vec . vec ) */
  118. inline value_type length() const
  119. {
  120. return sqrt( _v[0]*_v[0] + _v[1]*_v[1] );
  121. }
  122. /** Length squared of the vector = vec . vec */
  123. inline value_type length2( void ) const
  124. {
  125. return _v[0]*_v[0] + _v[1]*_v[1];
  126. }
  127. /** Normalize the vector so that it has length unity.
  128. * Returns the previous length of the vector.
  129. */
  130. inline value_type normalize()
  131. {
  132. value_type norm = Vec2d::length();
  133. if (norm>0.0)
  134. {
  135. value_type inv = 1.0/norm;
  136. _v[0] *= inv;
  137. _v[1] *= inv;
  138. }
  139. return( norm );
  140. }
  141. }; // end of class Vec2d
  142. /** multiply by vector components. */
  143. inline Vec2d componentMultiply(const Vec2d& lhs, const Vec2d& rhs)
  144. {
  145. return Vec2d(lhs[0]*rhs[0], lhs[1]*rhs[1]);
  146. }
  147. /** divide rhs components by rhs vector components. */
  148. inline Vec2d componentDivide(const Vec2d& lhs, const Vec2d& rhs)
  149. {
  150. return Vec2d(lhs[0]/rhs[0], lhs[1]/rhs[1]);
  151. }
  152. } // end of namespace osg
  153. #endif