openscenegraph
Vec2f
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
19namespace osg {
20
28class Vec2f
29{
30 public:
31
33 typedef float value_type;
34
36 enum { num_components = 2 };
37
40
41
43 Vec2f() {_v[0]=0.0; _v[1]=0.0;}
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
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
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
164 {
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
178inline Vec2f componentMultiply(const Vec2f& lhs, const Vec2f& rhs)
179{
180 return Vec2f(lhs[0]*rhs[0], lhs[1]*rhs[1]);
181}
182
184inline 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
Definition Vec2f:29
bool valid() const
Definition Vec2f:74
Vec2f & operator*=(value_type rhs)
Definition Vec2f:91
void set(value_type x, value_type y)
Definition Vec2f:61
value_type _v[2]
Definition Vec2f:39
Vec2f & operator-=(const Vec2f &rhs)
Definition Vec2f:135
const Vec2f operator-() const
Definition Vec2f:143
value_type & x()
Definition Vec2f:67
float value_type
Definition Vec2f:33
bool operator!=(const Vec2f &v) const
Definition Vec2f:49
bool isNaN() const
Definition Vec2f:76
value_type * ptr()
Definition Vec2f:58
@ num_components
Definition Vec2f:36
value_type x() const
Definition Vec2f:70
Vec2f & operator+=(const Vec2f &rhs)
Definition Vec2f:121
Vec2f(value_type x, value_type y)
Definition Vec2f:44
value_type length() const
Definition Vec2f:149
value_type & operator[](int i)
Definition Vec2f:64
value_type normalize()
Definition Vec2f:163
void set(const Vec2f &rhs)
Definition Vec2f:62
bool operator<(const Vec2f &v) const
Definition Vec2f:51
Vec2f()
Definition Vec2f:43
const Vec2f operator+(const Vec2f &rhs) const
Definition Vec2f:113
Vec2f & operator/=(value_type rhs)
Definition Vec2f:105
value_type operator*(const Vec2f &rhs) const
Definition Vec2f:79
bool operator==(const Vec2f &v) const
Definition Vec2f:47
const value_type * ptr() const
Definition Vec2f:59
value_type y() const
Definition Vec2f:71
const Vec2f operator/(value_type rhs) const
Definition Vec2f:99
value_type & y()
Definition Vec2f:68
value_type length2(void) const
Definition Vec2f:155
author: Julien Valentin 2017 (mp3butcher@hotmail.com)
Definition AlphaFunc:19
Vec2d componentDivide(const Vec2d &lhs, const Vec2d &rhs)
Definition Vec2d:187
bool isNaN(float v)
Definition Math:133
Vec2d componentMultiply(const Vec2d &lhs, const Vec2d &rhs)
Definition Vec2d:181