From David Cullu, added various mathematical operators

This commit is contained in:
Robert Osfield 2007-12-17 17:43:57 +00:00
parent 49696d776a
commit d7a2728fea
3 changed files with 194 additions and 45 deletions

View File

@ -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 true;
if (_v[0]<v._v[0]) return true;
else if (_v[0]>v._v[0]) return false;
else return (_v[1]<v._v[1]);
}
@ -74,38 +74,69 @@ class Vec2s
return Vec2s(_v[0]*rhs, _v[1]*rhs);
}
inline Vec2s& operator *= (value_type rhs)
{
_v[0]*=rhs;
_v[1]*=rhs;
return *this;
}
inline Vec2s operator / (value_type rhs) const
{
return Vec2s(_v[0]/rhs, _v[1]/rhs);
}
inline Vec2s operator + (value_type rhs) const
inline Vec2s& operator /= (value_type rhs)
{
return Vec2s(_v[0]+rhs, _v[1]+rhs);
_v[0]/=rhs;
_v[1]/=rhs;
return *this;
}
inline Vec2s operator - (value_type rhs) const
inline Vec2s operator * (const Vec2s& rhs) const
{
return Vec2s(_v[0]-rhs, _v[1]-rhs);
return Vec2s(_v[0]*rhs._v[0], _v[1]*rhs._v[1]);
}
inline Vec2s& operator *= (const Vec2s& rhs)
{
_v[0] *= rhs._v[0];
_v[1] *= rhs._v[1];
return *this;
}
inline Vec2s operator + (const Vec2s& rhs) const
{
return Vec2s(_v[0]+rhs._v[0], _v[1]+rhs._v[1]);
}
inline Vec2s& operator += (const Vec2s& rhs)
{
_v[0] += rhs._v[0];
_v[1] += rhs._v[1];
return *this;
}
inline Vec2s operator - (const Vec2s& rhs) const
{
return Vec2s(_v[0]-rhs._v[0], _v[1]-rhs._v[1]);
}
inline Vec2s operator * (const Vec2s& rhs) const
inline Vec2s& operator -= (const Vec2s& rhs)
{
return Vec2s(_v[0]*rhs._v[0], _v[1]*rhs._v[1]);
_v[0]-=rhs._v[0];
_v[1]-=rhs._v[1];
return *this;
}
};
}
inline Vec2s operator - () const
{
return Vec2s (-_v[0], -_v[1]);
}
}; // end of class Vec2s
} // end of namespace osg
#endif

View File

@ -12,7 +12,7 @@
*/
#ifndef OSG_VEC3S
#define OSG_VEC3S 1
#define OSG_VEC3S 1
namespace osg {
@ -36,7 +36,7 @@ class Vec3s
inline bool operator != (const Vec3s& v) const { return _v[0]!=v._v[0] || _v[1]!=v._v[1] || _v[2]!=v._v[2]; }
inline bool operator < (const Vec3s& v) const
{
if (_v[0]<v._v[0]) return true;
if (_v[0]<v._v[0]) return true;
else if (_v[0]>v._v[0]) return false;
else if (_v[1]<v._v[1]) return true;
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

View File

@ -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 true;
if (_v[0]<v._v[0]) return true;
else if (_v[0]>v._v[0]) return false;
else if (_v[1]<v._v[1]) return true;
else if (_v[1]>v._v[1]) return false;
@ -52,7 +58,7 @@ class Vec4s
else return (_v[3]<v._v[3]);
}
inline value_type* ptr() { return _v; }
inline value_type* ptr() { return _v; }
inline const value_type* ptr() const { return _v; }
inline void set( value_type x, value_type y, value_type z, value_type w)
@ -83,42 +89,103 @@ class Vec4s
inline value_type b() const { return _v[2]; }
inline value_type a() const { return _v[3]; }
/** Multiply by scalar. */
inline Vec4s operator * (value_type rhs) const
{
return Vec4s(_v[0]*rhs, _v[1]*rhs, _v[2]*rhs, _v[3]*rhs);
}
/** Unary multiply by scalar. */
inline Vec4s& operator *= (value_type rhs)
{
_v[0]*=rhs;
_v[1]*=rhs;
_v[2]*=rhs;
_v[3]*=rhs;
return *this;
}
/** Divide by scalar. */
inline Vec4s operator / (value_type rhs) const
{
return Vec4s(_v[0]/rhs, _v[1]/rhs, _v[2]/rhs, _v[3]/rhs);
}
inline Vec4s operator + (value_type rhs) const
/** Unary divide by scalar. */
inline Vec4s& operator /= (value_type rhs)
{
return Vec4s(_v[0]+rhs, _v[1]+rhs, _v[2]+rhs, _v[3]+rhs);
_v[0]/=rhs;
_v[1]/=rhs;
_v[2]/=rhs;
_v[3]/=rhs;
return *this;
}
inline Vec4s operator - (value_type rhs) const
{
return Vec4s(_v[0]-rhs, _v[1]-rhs, _v[2]-rhs, _v[3]-rhs);
}
inline Vec4s operator + (const Vec4s& rhs) const
{
return Vec4s(_v[0]+rhs._v[0], _v[1]+rhs._v[1], _v[2]+rhs._v[2], _v[3]+rhs._v[3]);
}
inline Vec4s operator - (const Vec4s& rhs) const
{
return Vec4s(_v[0]-rhs._v[0], _v[1]-rhs._v[1], _v[2]-rhs._v[2], _v[3]-rhs._v[3]);
}
/** Binary vector multiply. */
inline Vec4s operator * (const Vec4s& rhs) const
{
return Vec4s(_v[0]*rhs._v[0], _v[1]*rhs._v[1], _v[2]*rhs._v[2], _v[3]*rhs._v[3]);
return Vec4s(_v[0]*rhs._v[0], _v[1]*rhs._v[1],
_v[2]*rhs._v[2], _v[3]*rhs._v[3]);
}
};
}
/** Unary vector multiply. Slightly more efficient because no temporary
* intermediate object.
*/
inline Vec4s& operator *= (const Vec4s& rhs)
{
_v[0] *= rhs._v[0];
_v[1] *= rhs._v[1];
_v[2] *= rhs._v[2];
_v[3] *= rhs._v[3];
return *this;
}
/** Binary vector add. */
inline Vec4s operator + (const Vec4s& rhs) const
{
return Vec4s(_v[0]+rhs._v[0], _v[1]+rhs._v[1],
_v[2]+rhs._v[2], _v[3]+rhs._v[3]);
}
/** Unary vector add. Slightly more efficient because no temporary
* intermediate object.
*/
inline Vec4s& operator += (const Vec4s& rhs)
{
_v[0] += rhs._v[0];
_v[1] += rhs._v[1];
_v[2] += rhs._v[2];
_v[3] += rhs._v[3];
return *this;
}
/** Binary vector subtract. */
inline Vec4s operator - (const Vec4s& rhs) const
{
return Vec4s(_v[0]-rhs._v[0], _v[1]-rhs._v[1],
_v[2]-rhs._v[2], _v[3]-rhs._v[3] );
}
/** Unary vector subtract. */
inline Vec4s& operator -= (const Vec4s& rhs)
{
_v[0]-=rhs._v[0];
_v[1]-=rhs._v[1];
_v[2]-=rhs._v[2];
_v[3]-=rhs._v[3];
return *this;
}
/** Negation operator. Returns the negative of the Vec4s. */
inline Vec4s operator - () const
{
return Vec4s (-_v[0], -_v[1], -_v[2], -_v[3]);
}
}; // end of class Vec4s
} // end of namespace osg
#endif