OSG  3.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Vec2f.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_VEC2F
15 #define OSG_VEC2F 1
16 
17 #include <osg/Math>
18 
19 namespace osg {
20 
28 class Vec2f
29 {
30  public:
31 
33  typedef float value_type;
34 
36  enum { num_components = 2 };
37 
39  value_type _v[2];
40 
41 
43  Vec2f() {_v[0]=0.0; _v[1]=0.0;}
44  Vec2f(value_type x,value_type y) { _v[0]=x; _v[1]=y; }
45 
46 
47  inline bool operator == (const Vec2f& v) const { return _v[0]==v._v[0] && _v[1]==v._v[1]; }
48 
49  inline bool operator != (const Vec2f& v) const { return _v[0]!=v._v[0] || _v[1]!=v._v[1]; }
50 
51  inline bool operator < (const Vec2f& v) const
52  {
53  if (_v[0]<v._v[0]) return true;
54  else if (_v[0]>v._v[0]) return false;
55  else return (_v[1]<v._v[1]);
56  }
57 
58  inline value_type * ptr() { return _v; }
59  inline const value_type * ptr() const { return _v; }
60 
61  inline void set( value_type x, value_type y ) { _v[0]=x; _v[1]=y; }
62  inline void set( const Vec2f& rhs) { _v[0]=rhs._v[0]; _v[1]=rhs._v[1]; }
63 
64  inline value_type & operator [] (int i) { return _v[i]; }
65  inline value_type operator [] (int i) const { return _v[i]; }
66 
67  inline value_type & x() { return _v[0]; }
68  inline value_type & y() { return _v[1]; }
69 
70  inline value_type x() const { return _v[0]; }
71  inline value_type y() const { return _v[1]; }
72 
74  inline bool valid() const { return !isNaN(); }
76  inline bool isNaN() const { return osg::isNaN(_v[0]) || osg::isNaN(_v[1]); }
77 
79  inline value_type operator * (const Vec2f& rhs) const
80  {
81  return _v[0]*rhs._v[0]+_v[1]*rhs._v[1];
82  }
83 
85  inline const Vec2f operator * (value_type rhs) const
86  {
87  return Vec2f(_v[0]*rhs, _v[1]*rhs);
88  }
89 
91  inline Vec2f& operator *= (value_type rhs)
92  {
93  _v[0]*=rhs;
94  _v[1]*=rhs;
95  return *this;
96  }
97 
99  inline const Vec2f operator / (value_type rhs) const
100  {
101  return Vec2f(_v[0]/rhs, _v[1]/rhs);
102  }
103 
105  inline Vec2f& operator /= (value_type rhs)
106  {
107  _v[0]/=rhs;
108  _v[1]/=rhs;
109  return *this;
110  }
111 
113  inline const Vec2f operator + (const Vec2f& rhs) const
114  {
115  return Vec2f(_v[0]+rhs._v[0], _v[1]+rhs._v[1]);
116  }
117 
121  inline Vec2f& operator += (const Vec2f& rhs)
122  {
123  _v[0] += rhs._v[0];
124  _v[1] += rhs._v[1];
125  return *this;
126  }
127 
129  inline const Vec2f operator - (const Vec2f& rhs) const
130  {
131  return Vec2f(_v[0]-rhs._v[0], _v[1]-rhs._v[1]);
132  }
133 
135  inline Vec2f& operator -= (const Vec2f& rhs)
136  {
137  _v[0]-=rhs._v[0];
138  _v[1]-=rhs._v[1];
139  return *this;
140  }
141 
143  inline const Vec2f operator - () const
144  {
145  return Vec2f (-_v[0], -_v[1]);
146  }
147 
149  inline value_type length() const
150  {
151  return sqrtf( _v[0]*_v[0] + _v[1]*_v[1] );
152  }
153 
155  inline value_type length2( void ) const
156  {
157  return _v[0]*_v[0] + _v[1]*_v[1];
158  }
159 
163  inline value_type normalize()
164  {
165  value_type norm = Vec2f::length();
166  if (norm>0.0)
167  {
168  value_type inv = 1.0f/norm;
169  _v[0] *= inv;
170  _v[1] *= inv;
171  }
172  return( norm );
173  }
174 
175 }; // end of class Vec2f
176 
178 inline Vec2f componentMultiply(const Vec2f& lhs, const Vec2f& rhs)
179 {
180  return Vec2f(lhs[0]*rhs[0], lhs[1]*rhs[1]);
181 }
182 
184 inline Vec2f componentDivide(const Vec2f& lhs, const Vec2f& rhs)
185 {
186  return Vec2f(lhs[0]/rhs[0], lhs[1]/rhs[1]);
187 }
188 
189 } // end of namespace osg
190 #endif
191 
value_type operator*(const Vec2f &rhs) const
Definition: Vec2f.h:79
bool valid() const
Definition: Vec2f.h:74
Vec2f & operator*=(value_type rhs)
Definition: Vec2f.h:91
value_type & y()
Definition: Vec2f.h:68
float value_type
Definition: Vec2f.h:33
Vec2f & operator/=(value_type rhs)
Definition: Vec2f.h:105
value_type length2(void) const
Definition: Vec2f.h:155
bool operator<(const Vec2f &v) const
Definition: Vec2f.h:51
value_type * ptr()
Definition: Vec2f.h:58
Vec2f()
Definition: Vec2f.h:43
value_type _v[2]
Definition: Vec2f.h:39
Vec2f & operator+=(const Vec2f &rhs)
Definition: Vec2f.h:121
Vec2d componentDivide(const Vec2d &lhs, const Vec2d &rhs)
Definition: Vec2d.h:187
value_type & x()
Definition: Vec2f.h:67
bool isNaN(float v)
Definition: Math.h:114
bool operator==(const Vec2f &v) const
Definition: Vec2f.h:47
value_type x() const
Definition: Vec2f.h:70
value_type & operator[](int i)
Definition: Vec2f.h:64
value_type y() const
Definition: Vec2f.h:71
bool isNaN() const
Definition: Vec2f.h:76
void set(const Vec2f &rhs)
Definition: Vec2f.h:62
const Vec2f operator/(value_type rhs) const
Definition: Vec2f.h:99
Vec2f & operator-=(const Vec2f &rhs)
Definition: Vec2f.h:135
bool operator!=(const Vec2f &v) const
Definition: Vec2f.h:49
value_type normalize()
Definition: Vec2f.h:163
Definition: AlphaFunc.h:19
void set(value_type x, value_type y)
Definition: Vec2f.h:61
value_type length() const
Definition: Vec2f.h:149
const Vec2f operator+(const Vec2f &rhs) const
Definition: Vec2f.h:113
Vec2f(value_type x, value_type y)
Definition: Vec2f.h:44
const value_type * ptr() const
Definition: Vec2f.h:59
Vec2d componentMultiply(const Vec2d &lhs, const Vec2d &rhs)
Definition: Vec2d.h:181
const Vec2f operator-() const
Definition: Vec2f.h:143