From d7a2728fea681a4d2ae8bffd085ddc024162adad Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Mon, 17 Dec 2007 17:43:57 +0000 Subject: [PATCH] From David Cullu, added various mathematical operators --- include/osg/Vec2s | 55 ++++++++++++++++++----- include/osg/Vec3s | 73 +++++++++++++++++++++++++----- include/osg/Vec4s | 111 +++++++++++++++++++++++++++++++++++++--------- 3 files changed, 194 insertions(+), 45 deletions(-) diff --git a/include/osg/Vec2s b/include/osg/Vec2s index 118e2e660..414a47ff8 100644 --- a/include/osg/Vec2s +++ b/include/osg/Vec2s @@ -12,7 +12,7 @@ */ #ifndef OSG_VEC2S -#define OSG_VEC2S 1 +#define OSG_VEC2S 1 namespace osg { @@ -36,7 +36,7 @@ class Vec2s inline bool operator != (const Vec2s& v) const { return _v[0]!=v._v[0] || _v[1]!=v._v[1]; } inline bool operator < (const Vec2s& v) const { - if (_v[0]v._v[0]) return false; else return (_v[1]v._v[0]) return false; else if (_v[1]v._v[1]) return false; @@ -81,37 +81,88 @@ class Vec3s return Vec3s(_v[0]*rhs, _v[1]*rhs, _v[2]*rhs); } + /** Unary multiply by scalar. */ + inline Vec3s& operator *= (value_type rhs) + { + _v[0]*=rhs; + _v[1]*=rhs; + _v[2]*=rhs; + return *this; + } + + /** Divide by scalar. */ inline Vec3s operator / (value_type rhs) const { return Vec3s(_v[0]/rhs, _v[1]/rhs, _v[2]/rhs); } - inline Vec3s operator + (value_type rhs) const + /** Unary divide by scalar. */ + inline Vec3s& operator /= (value_type rhs) { - return Vec3s(_v[0]+rhs, _v[1]+rhs, _v[2]+rhs); + _v[0]/=rhs; + _v[1]/=rhs; + _v[2]/=rhs; + return *this; } - inline Vec3s operator - (value_type rhs) const + /** Binary vector multiply. */ + inline Vec3s operator * (const Vec3s& rhs) const { - return Vec3s(_v[0]-rhs, _v[1]-rhs, _v[2]-rhs); + return Vec3s(_v[0]*rhs._v[0], _v[1]*rhs._v[1], _v[2]*rhs._v[2]); } + /** Unary vector multiply. Slightly more efficient because no temporary + * intermediate object. + */ + inline Vec3s& operator *= (const Vec3s& rhs) + { + _v[0] *= rhs._v[0]; + _v[1] *= rhs._v[1]; + _v[2] *= rhs._v[2]; + return *this; + } + + /** Binary vector add. */ inline Vec3s operator + (const Vec3s& rhs) const { return Vec3s(_v[0]+rhs._v[0], _v[1]+rhs._v[1], _v[2]+rhs._v[2]); } + /** Unary vector add. Slightly more efficient because no temporary + * intermediate object. + */ + inline Vec3s& operator += (const Vec3s& rhs) + { + _v[0] += rhs._v[0]; + _v[1] += rhs._v[1]; + _v[2] += rhs._v[2]; + return *this; + } + + /** Binary vector substract. */ inline Vec3s operator - (const Vec3s& rhs) const { return Vec3s(_v[0]-rhs._v[0], _v[1]-rhs._v[1], _v[2]-rhs._v[2]); } - - inline Vec3s operator * (const Vec3s& rhs) const + + /** Unary vector subtract. */ + inline Vec3s& operator -= (const Vec3s& rhs) { - return Vec3s(_v[0]*rhs._v[0], _v[1]*rhs._v[1], _v[2]*rhs._v[2]); + _v[0]-=rhs._v[0]; + _v[1]-=rhs._v[1]; + _v[2]-=rhs._v[2]; + return *this; } -}; -} + /** Negation operator. Returns the negative of the Vec3s. */ + inline Vec3s operator - () const + { + return Vec3s (-_v[0], -_v[1], -_v[2]); + } + +}; // end of class Vec3s + +} // end of namespace osg #endif + diff --git a/include/osg/Vec4s b/include/osg/Vec4s index 88525a941..68290ae33 100644 --- a/include/osg/Vec4s +++ b/include/osg/Vec4s @@ -12,10 +12,16 @@ */ #ifndef OSG_VEC4S -#define OSG_VEC4S 1 +#define OSG_VEC4S 1 namespace osg { +/** General purpose float quad. Uses include representation + * of color coordinates. + * No support yet added for float * Vec4f - is it necessary? + * Need to define a non-member non-friend operator* etc. + * Vec4f * float is okay +*/ class Vec4s { public: @@ -43,7 +49,7 @@ class Vec4s inline bool operator != (const Vec4s& v) const { return _v[0]!=v._v[0] || _v[1]!=v._v[1] || _v[2]!=v._v[2] || _v[3]!=v._v[3]; } inline bool operator < (const Vec4s& v) const { - if (_v[0]v._v[0]) return false; else if (_v[1]v._v[1]) return false; @@ -52,7 +58,7 @@ class Vec4s else return (_v[3]