Vec2f
Go to the documentation of this file.
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
*
* This library is open source and may be redistributed and/or modified under
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
* (at your option) any later version. The full license is in LICENSE file
* included with this distribution, and on the openscenegraph.org website.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* OpenSceneGraph Public License for more details.
*/
#ifndef OSG_VEC2F
#define OSG_VEC2F 1
#include <osg/Math>
namespace osg {
class Vec2f
{
public:
typedef float value_type;
enum { num_components = 2 };
value_type _v[2];
Vec2f() {_v[0]=0.0; _v[1]=0.0;}
Vec2f(value_type x,value_type y) { _v[0]=x; _v[1]=y; }
inline bool operator == (const Vec2f& v) const { return _v[0]==v._v[0] && _v[1]==v._v[1]; }
inline bool operator != (const Vec2f& v) const { return _v[0]!=v._v[0] || _v[1]!=v._v[1]; }
inline bool operator < (const Vec2f& v) const
{
if (_v[0]<v._v[0]) return true;
else if (_v[0]>v._v[0]) return false;
else return (_v[1]<v._v[1]);
}
inline value_type * ptr() { return _v; }
inline const value_type * ptr() const { return _v; }
inline void set( value_type x, value_type y ) { _v[0]=x; _v[1]=y; }
inline void set( const Vec2f& rhs) { _v[0]=rhs._v[0]; _v[1]=rhs._v[1]; }
inline value_type & operator [] (int i) { return _v[i]; }
inline value_type operator [] (int i) const { return _v[i]; }
inline value_type & x() { return _v[0]; }
inline value_type & y() { return _v[1]; }
inline value_type x() const { return _v[0]; }
inline value_type y() const { return _v[1]; }
inline bool valid() const { return !isNaN(); }
inline bool isNaN() const { return osg::isNaN(_v[0]) || osg::isNaN(_v[1]); }
inline value_type operator * (const Vec2f& rhs) const
{
return _v[0]*rhs._v[0]+_v[1]*rhs._v[1];
}
inline const Vec2f operator * (value_type rhs) const
{
return Vec2f(_v[0]*rhs, _v[1]*rhs);
}
inline Vec2f& operator *= (value_type rhs)
{
_v[0]*=rhs;
_v[1]*=rhs;
return *this;
}
inline const Vec2f operator / (value_type rhs) const
{
return Vec2f(_v[0]/rhs, _v[1]/rhs);
}
inline Vec2f& operator /= (value_type rhs)
{
_v[0]/=rhs;
_v[1]/=rhs;
return *this;
}
inline const Vec2f operator + (const Vec2f& rhs) const
{
return Vec2f(_v[0]+rhs._v[0], _v[1]+rhs._v[1]);
}
inline Vec2f& operator += (const Vec2f& rhs)
{
_v[0] += rhs._v[0];
_v[1] += rhs._v[1];
return *this;
}
inline const Vec2f operator - (const Vec2f& rhs) const
{
return Vec2f(_v[0]-rhs._v[0], _v[1]-rhs._v[1]);
}
inline Vec2f& operator -= (const Vec2f& rhs)
{
_v[0]-=rhs._v[0];
_v[1]-=rhs._v[1];
return *this;
}
inline const Vec2f operator - () const
{
return Vec2f (-_v[0], -_v[1]);
}
inline value_type length() const
{
return sqrtf( _v[0]*_v[0] + _v[1]*_v[1] );
}
inline value_type length2( void ) const
{
return _v[0]*_v[0] + _v[1]*_v[1];
}
inline value_type normalize()
{
value_type norm = Vec2f::length();
if (norm>0.0)
{
value_type inv = 1.0f/norm;
_v[0] *= inv;
_v[1] *= inv;
}
return( norm );
}
}; // end of class Vec2f
inline Vec2f componentMultiply(const Vec2f& lhs, const Vec2f& rhs)
{
return Vec2f(lhs[0]*rhs[0], lhs[1]*rhs[1]);
}
inline Vec2f componentDivide(const Vec2f& lhs, const Vec2f& rhs)
{
return Vec2f(lhs[0]/rhs[0], lhs[1]/rhs[1]);
}
} // end of namespace osg
#endif
Math
osg::Vec2f
Definition Vec2f:29
osg::Vec2f::valid
bool valid() const
Definition Vec2f:74
osg::Vec2f::operator*=
Vec2f & operator*=(value_type rhs)
Definition Vec2f:91
osg::Vec2f::set
void set(value_type x, value_type y)
Definition Vec2f:61
osg::Vec2f::_v
value_type _v[2]
Definition Vec2f:39
osg::Vec2f::operator-=
Vec2f & operator-=(const Vec2f &rhs)
Definition Vec2f:135
osg::Vec2f::operator-
const Vec2f operator-() const
Definition Vec2f:143
osg::Vec2f::x
value_type & x()
Definition Vec2f:67
osg::Vec2f::value_type
float value_type
Definition Vec2f:33
osg::Vec2f::operator!=
bool operator!=(const Vec2f &v) const
Definition Vec2f:49
osg::Vec2f::isNaN
bool isNaN() const
Definition Vec2f:76
osg::Vec2f::ptr
value_type * ptr()
Definition Vec2f:58
osg::Vec2f::x
value_type x() const
Definition Vec2f:70
osg::Vec2f::operator+=
Vec2f & operator+=(const Vec2f &rhs)
Definition Vec2f:121
osg::Vec2f::Vec2f
Vec2f(value_type x, value_type y)
Definition Vec2f:44
osg::Vec2f::num_components
@ num_components
Definition Vec2f:36
osg::Vec2f::length
value_type length() const
Definition Vec2f:149
osg::Vec2f::operator[]
value_type & operator[](int i)
Definition Vec2f:64
osg::Vec2f::normalize
value_type normalize()
Definition Vec2f:163
osg::Vec2f::set
void set(const Vec2f &rhs)
Definition Vec2f:62
osg::Vec2f::operator<
bool operator<(const Vec2f &v) const
Definition Vec2f:51
osg::Vec2f::Vec2f
Vec2f()
Definition Vec2f:43
osg::Vec2f::operator+
const Vec2f operator+(const Vec2f &rhs) const
Definition Vec2f:113
osg::Vec2f::operator/=
Vec2f & operator/=(value_type rhs)
Definition Vec2f:105
osg::Vec2f::operator*
value_type operator*(const Vec2f &rhs) const
Definition Vec2f:79
osg::Vec2f::operator==
bool operator==(const Vec2f &v) const
Definition Vec2f:47
osg::Vec2f::ptr
const value_type * ptr() const
Definition Vec2f:59
osg::Vec2f::y
value_type y() const
Definition Vec2f:71
osg::Vec2f::operator/
const Vec2f operator/(value_type rhs) const
Definition Vec2f:99
osg::Vec2f::y
value_type & y()
Definition Vec2f:68
osg::Vec2f::length2
value_type length2(void) const
Definition Vec2f:155
osg
Definition AlphaFunc:19
osg::componentDivide
Vec2d componentDivide(const Vec2d &lhs, const Vec2d &rhs)
Definition Vec2d:187
osg::isNaN
bool isNaN(float v)
Definition Math:133
osg::componentMultiply
Vec2d componentMultiply(const Vec2d &lhs, const Vec2d &rhs)
Definition Vec2d:181