OSG  3.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Vec2d.h
Go to the documentation of this file.
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 
14 #ifndef OSG_VEC2D
15 #define OSG_VEC2D 1
16 
17 #include <osg/Vec2f>
18 
19 namespace osg {
20 
28 class Vec2d
29 {
30  public:
31 
33  typedef double value_type;
34 
36  enum { num_components = 2 };
37 
38  value_type _v[2];
39 
41  Vec2d() {_v[0]=0.0; _v[1]=0.0;}
42 
43  Vec2d(value_type x,value_type y) { _v[0]=x; _v[1]=y; }
44 
45  inline Vec2d(const Vec2f& vec) { _v[0]=vec._v[0]; _v[1]=vec._v[1]; }
46 
47  inline operator Vec2f() const { return Vec2f(static_cast<float>(_v[0]),static_cast<float>(_v[1]));}
48 
49 
50  inline bool operator == (const Vec2d& v) const { return _v[0]==v._v[0] && _v[1]==v._v[1]; }
51 
52  inline bool operator != (const Vec2d& v) const { return _v[0]!=v._v[0] || _v[1]!=v._v[1]; }
53 
54  inline bool operator < (const Vec2d& v) const
55  {
56  if (_v[0]<v._v[0]) return true;
57  else if (_v[0]>v._v[0]) return false;
58  else return (_v[1]<v._v[1]);
59  }
60 
61  inline value_type* ptr() { return _v; }
62  inline const value_type* ptr() const { return _v; }
63 
64  inline void set( value_type x, value_type y ) { _v[0]=x; _v[1]=y; }
65 
66  inline value_type& operator [] (int i) { return _v[i]; }
67  inline value_type operator [] (int i) const { return _v[i]; }
68 
69  inline value_type& x() { return _v[0]; }
70  inline value_type& y() { return _v[1]; }
71 
72  inline value_type x() const { return _v[0]; }
73  inline value_type y() const { return _v[1]; }
74 
76  inline bool valid() const { return !isNaN(); }
78  inline bool isNaN() const { return osg::isNaN(_v[0]) || osg::isNaN(_v[1]); }
79 
81  inline value_type operator * (const Vec2d& rhs) const
82  {
83  return _v[0]*rhs._v[0]+_v[1]*rhs._v[1];
84  }
85 
87  inline const Vec2d operator * (value_type rhs) const
88  {
89  return Vec2d(_v[0]*rhs, _v[1]*rhs);
90  }
91 
93  inline Vec2d& operator *= (value_type rhs)
94  {
95  _v[0]*=rhs;
96  _v[1]*=rhs;
97  return *this;
98  }
99 
101  inline const Vec2d operator / (value_type rhs) const
102  {
103  return Vec2d(_v[0]/rhs, _v[1]/rhs);
104  }
105 
107  inline Vec2d& operator /= (value_type rhs)
108  {
109  _v[0]/=rhs;
110  _v[1]/=rhs;
111  return *this;
112  }
113 
115  inline const Vec2d operator + (const Vec2d& rhs) const
116  {
117  return Vec2d(_v[0]+rhs._v[0], _v[1]+rhs._v[1]);
118  }
119 
123  inline Vec2d& operator += (const Vec2d& rhs)
124  {
125  _v[0] += rhs._v[0];
126  _v[1] += rhs._v[1];
127  return *this;
128  }
129 
131  inline const Vec2d operator - (const Vec2d& rhs) const
132  {
133  return Vec2d(_v[0]-rhs._v[0], _v[1]-rhs._v[1]);
134  }
135 
137  inline Vec2d& operator -= (const Vec2d& rhs)
138  {
139  _v[0]-=rhs._v[0];
140  _v[1]-=rhs._v[1];
141  return *this;
142  }
143 
145  inline const Vec2d operator - () const
146  {
147  return Vec2d (-_v[0], -_v[1]);
148  }
149 
151  inline value_type length() const
152  {
153  return sqrt( _v[0]*_v[0] + _v[1]*_v[1] );
154  }
155 
157  inline value_type length2( void ) const
158  {
159  return _v[0]*_v[0] + _v[1]*_v[1];
160  }
161 
165  inline value_type normalize()
166  {
167  value_type norm = Vec2d::length();
168  if (norm>0.0)
169  {
170  value_type inv = 1.0/norm;
171  _v[0] *= inv;
172  _v[1] *= inv;
173  }
174  return( norm );
175  }
176 
177 }; // end of class Vec2d
178 
179 
181 inline Vec2d componentMultiply(const Vec2d& lhs, const Vec2d& rhs)
182 {
183  return Vec2d(lhs[0]*rhs[0], lhs[1]*rhs[1]);
184 }
185 
187 inline Vec2d componentDivide(const Vec2d& lhs, const Vec2d& rhs)
188 {
189  return Vec2d(lhs[0]/rhs[0], lhs[1]/rhs[1]);
190 }
191 
192 } // end of namespace osg
193 #endif
value_type & y()
Definition: Vec2d.h:70
Vec2d & operator-=(const Vec2d &rhs)
Definition: Vec2d.h:137
value_type & x()
Definition: Vec2d.h:69
bool operator!=(const Vec2d &v) const
Definition: Vec2d.h:52
Vec2d & operator*=(value_type rhs)
Definition: Vec2d.h:93
value_type x() const
Definition: Vec2d.h:72
value_type operator*(const Vec2d &rhs) const
Definition: Vec2d.h:81
value_type _v[2]
Definition: Vec2d.h:38
const Vec2d operator/(value_type rhs) const
Definition: Vec2d.h:101
value_type _v[2]
Definition: Vec2f.h:39
value_type length() const
Definition: Vec2d.h:151
bool operator==(const Vec2d &v) const
Definition: Vec2d.h:50
Vec2d componentDivide(const Vec2d &lhs, const Vec2d &rhs)
Definition: Vec2d.h:187
Vec2d(const Vec2f &vec)
Definition: Vec2d.h:45
bool isNaN(float v)
Definition: Math.h:114
bool isNaN() const
Definition: Vec2d.h:78
double value_type
Definition: Vec2d.h:33
value_type & operator[](int i)
Definition: Vec2d.h:66
Vec2d & operator/=(value_type rhs)
Definition: Vec2d.h:107
const value_type * ptr() const
Definition: Vec2d.h:62
void set(value_type x, value_type y)
Definition: Vec2d.h:64
Vec2d()
Definition: Vec2d.h:41
const Vec2d operator+(const Vec2d &rhs) const
Definition: Vec2d.h:115
value_type * ptr()
Definition: Vec2d.h:61
value_type length2(void) const
Definition: Vec2d.h:157
value_type y() const
Definition: Vec2d.h:73
Definition: AlphaFunc.h:19
bool operator<(const Vec2d &v) const
Definition: Vec2d.h:54
value_type normalize()
Definition: Vec2d.h:165
const Vec2d operator-() const
Definition: Vec2d.h:145
Vec2d & operator+=(const Vec2d &rhs)
Definition: Vec2d.h:123
Vec2d(value_type x, value_type y)
Definition: Vec2d.h:43
bool valid() const
Definition: Vec2d.h:76
Vec2d componentMultiply(const Vec2d &lhs, const Vec2d &rhs)
Definition: Vec2d.h:181