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
21/** General purpose float pair. Uses include representation of
22 * texture coordinates.
23 * No support yet added for float * Vec2f - is it necessary?
24 * Need to define a non-member non-friend operator* etc.
25 * BTW: Vec2f * float is okay
26*/
27
28class Vec2f
29{
30 public:
31
32 /** Data type of vector components.*/
33 typedef float value_type;
34
35 /** Number of vector components. */
36 enum { num_components = 2 };
37
38 /** Vec member variable. */
39 value_type _v[2];
40
41
42 /** Constructor that sets all components of the vector to zero */
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
73 /** Returns true if all components have values that are not NaN. */
74 inline bool valid() const { return !isNaN(); }
75 /** Returns true if at least one component has value NaN. */
76 inline bool isNaN() const { return osg::isNaN(_v[0]) || osg::isNaN(_v[1]); }
77
78 /** Dot product. */
79 inline value_type operator * (const Vec2f& rhs) const
80 {
81 return _v[0]*rhs._v[0]+_v[1]*rhs._v[1];
82 }
83
84 /** Multiply by scalar. */
85 inline const Vec2f operator * (value_type rhs) const
86 {
87 return Vec2f(_v[0]*rhs, _v[1]*rhs);
88 }
89
90 /** Unary multiply by scalar. */
91 inline Vec2f& operator *= (value_type rhs)
92 {
93 _v[0]*=rhs;
94 _v[1]*=rhs;
95 return *this;
96 }
97
98 /** Divide by scalar. */
99 inline const Vec2f operator / (value_type rhs) const
100 {
101 return Vec2f(_v[0]/rhs, _v[1]/rhs);
102 }
103
104 /** Unary divide by scalar. */
105 inline Vec2f& operator /= (value_type rhs)
106 {
107 _v[0]/=rhs;
108 _v[1]/=rhs;
109 return *this;
110 }
111
112 /** Binary vector add. */
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
118 /** Unary vector add. Slightly more efficient because no temporary
119 * intermediate object.
120 */
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
128 /** Binary vector subtract. */
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
134 /** Unary vector subtract. */
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
142 /** Negation operator. Returns the negative of the Vec2f. */
143 inline const Vec2f operator - () const
144 {
145 return Vec2f (-_v[0], -_v[1]);
146 }
147
148 /** Length of the vector = sqrt( vec . vec ) */
149 inline value_type length() const
150 {
151 return sqrtf( _v[0]*_v[0] + _v[1]*_v[1] );
152 }
153
154 /** Length squared of the vector = vec . vec */
155 inline value_type length2( void ) const
156 {
157 return _v[0]*_v[0] + _v[1]*_v[1];
158 }
159
160 /** Normalize the vector so that it has length unity.
161 * Returns the previous length of the vector.
162 */
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
177/** multiply by vector components. */
178inline Vec2f componentMultiply(const Vec2f& lhs, const Vec2f& rhs)
179{
180 return Vec2f(lhs[0]*rhs[0], lhs[1]*rhs[1]);
181}
182
183/** divide rhs components by rhs vector components. */
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