1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
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.
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.
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
32 /** Data type of vector components.*/
33 typedef float value_type;
35 /** Number of vector components. */
36 enum { num_components = 2 };
38 /** Vec member variable. */
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; }
47 inline bool operator == (const Vec2f& v) const { return _v[0]==v._v[0] && _v[1]==v._v[1]; }
49 inline bool operator != (const Vec2f& v) const { return _v[0]!=v._v[0] || _v[1]!=v._v[1]; }
51 inline bool operator < (const Vec2f& v) const
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]);
58 inline value_type * ptr() { return _v; }
59 inline const value_type * ptr() const { return _v; }
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]; }
64 inline value_type & operator [] (int i) { return _v[i]; }
65 inline value_type operator [] (int i) const { return _v[i]; }
67 inline value_type & x() { return _v[0]; }
68 inline value_type & y() { return _v[1]; }
70 inline value_type x() const { return _v[0]; }
71 inline value_type y() const { return _v[1]; }
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]); }
79 inline value_type operator * (const Vec2f& rhs) const
81 return _v[0]*rhs._v[0]+_v[1]*rhs._v[1];
84 /** Multiply by scalar. */
85 inline const Vec2f operator * (value_type rhs) const
87 return Vec2f(_v[0]*rhs, _v[1]*rhs);
90 /** Unary multiply by scalar. */
91 inline Vec2f& operator *= (value_type rhs)
98 /** Divide by scalar. */
99 inline const Vec2f operator / (value_type rhs) const
101 return Vec2f(_v[0]/rhs, _v[1]/rhs);
104 /** Unary divide by scalar. */
105 inline Vec2f& operator /= (value_type rhs)
112 /** Binary vector add. */
113 inline const Vec2f operator + (const Vec2f& rhs) const
115 return Vec2f(_v[0]+rhs._v[0], _v[1]+rhs._v[1]);
118 /** Unary vector add. Slightly more efficient because no temporary
119 * intermediate object.
121 inline Vec2f& operator += (const Vec2f& rhs)
128 /** Binary vector subtract. */
129 inline const Vec2f operator - (const Vec2f& rhs) const
131 return Vec2f(_v[0]-rhs._v[0], _v[1]-rhs._v[1]);
134 /** Unary vector subtract. */
135 inline Vec2f& operator -= (const Vec2f& rhs)
142 /** Negation operator. Returns the negative of the Vec2f. */
143 inline const Vec2f operator - () const
145 return Vec2f (-_v[0], -_v[1]);
148 /** Length of the vector = sqrt( vec . vec ) */
149 inline value_type length() const
151 return sqrtf( _v[0]*_v[0] + _v[1]*_v[1] );
154 /** Length squared of the vector = vec . vec */
155 inline value_type length2( void ) const
157 return _v[0]*_v[0] + _v[1]*_v[1];
160 /** Normalize the vector so that it has length unity.
161 * Returns the previous length of the vector.
163 inline value_type normalize()
165 value_type norm = Vec2f::length();
168 value_type inv = 1.0f/norm;
175}; // end of class Vec2f
177/** multiply by vector components. */
178inline Vec2f componentMultiply(const Vec2f& lhs, const Vec2f& rhs)
180 return Vec2f(lhs[0]*rhs[0], lhs[1]*rhs[1]);
183/** divide rhs components by rhs vector components. */
184inline Vec2f componentDivide(const Vec2f& lhs, const Vec2f& rhs)
186 return Vec2f(lhs[0]/rhs[0], lhs[1]/rhs[1]);
189} // end of namespace osg