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

@ -74,29 +74,23 @@ 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);
}
inline Vec2s operator - (value_type rhs) const
{
return Vec2s(_v[0]-rhs, _v[1]-rhs);
}
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
{
return Vec2s(_v[0]-rhs._v[0], _v[1]-rhs._v[1]);
_v[0]/=rhs;
_v[1]/=rhs;
return *this;
}
inline Vec2s operator * (const Vec2s& rhs) const
@ -104,8 +98,45 @@ class Vec2s
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)
{
_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

@ -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

@ -16,6 +16,12 @@
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:
@ -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