Original Post
I wrote this class a while back, and I decided to tidy it up a bit, I'm wondering if anyone has any suggestions or comments on efficiency, style, operations I should add or remove.
Thanks.
class __declspec(dllexport) Vector3
{
public:
//constructors
inline Vector3()
{
/* blank for efficient allocation in batches*/
}
inline Vector3(float x, float y, float z) : x(x), y(y), z(z)
{
// this->setValues(x,y,z);
}
/****************************
*
* Operations
*
*****************************/
//addition
inline const Vector3& add(const Vector3 &other)
{
this->x += other.getX();
this->y += other.getY();
this->z += other.getZ();
return *this;
}
inline static Vector3 add(const Vector3 &left, const Vector3 &right)
{
return Vector3(left.getX() + right.getX(), left.getY() + right.getY(), left.getZ() + right.getZ());
}
inline Vector3 operator +(const Vector3 &right) const
{
return Vector3(this->x + right.getX(), this->y + right.getY(), this->z + right.getZ());
}
inline const Vector3& operator +=(const Vector3 &right)
{
this->x += right.getX();
this->y += right.getY();
this->z += right.getZ();
return *this;
}
//subtraction
inline const Vector3& subtract(const Vector3 &other)
{
this->x -= other.getX();
this->y -= other.getY();
this->z -= other.getZ();
return *this;
}
inline static Vector3 subtract(const Vector3 &left, const Vector3 &right)
{
return Vector3(left.x - right.getX(), left.y - right.getY(),left.z - right.getZ());
}
inline Vector3 operator-(const Vector3 &right) const
{
return Vector3(this->x - right.getX(), this->y - right.getY(), this->z - right.getZ());
}
inline const Vector3& operator -=(const Vector3 &right)
{
this->x -= right.getX();
this->y -= right.getY();
this->z -= right.getZ();
return *this;
}
/* comparison */
inline bool operator==(const Vector3 &right) const
{
return ( (this->x == right.getX()) && (this->y == right.getY()) && (this->z == right.getZ()) );
}
// scalar multiplication
inline const Vector3& multiply(float scalar)
{
this->x *= scalar;
this->y *= scalar;
this->z *= scalar;
return *this;
}
inline static Vector3 multiply(const Vector3 &vector, float scalar)
{
return Vector3(vector.x * scalar, vector.y * scalar, vector.z * scalar);
}
inline Vector3 operator *(float scalar) const
{
return Vector3(this->x * scalar, this->y * scalar, this-> z * scalar);
}
inline friend Vector3 operator*(float scalar, const Vector3& vector)
{
return Vector3(vector.x * scalar, vector.y * scalar, vector.z * scalar);
}
inline const Vector3& operator *=(float scalar)
{
this->x *= scalar;
this->y *= scalar;
this->z *= scalar;
return *this;
}
//Vector3 multiply(const Vector3 &other);
//static Vector3 multiplty(const Vector3 &left, const Vector3 &right);
// dot products
inline float dotProduct(const Vector3 &other) const
{
return (this->x * other.getX()) + (this->y * other.getY()) + (this->z * other.getZ());
}
inline static float dotProduct(const Vector3 &left, const Vector3 &right)
{
return (left.getX() * right.getX()) + (left.getY() * right.getY()) + (left.getZ() * right.getZ());
}
//cross product
inline static Vector3 crossProduct(const Vector3 &left, const Vector3 &right)
{
// [ i j k ]
// [ l.x l.y l.z]
// [ r.x r.y r.z]
return Vector3((left.getY() * right.getZ()) - (right.getY() * left.getZ()),
(left.getX() * right.getZ()) - (right.getX() * left.getZ()),
(left.getX() * right.getY()) - (right.getX() * left.getY()));
}
inline const Vector3& crossProduct(const Vector3 &other)
{
this->x = (this->y * other.getZ()) + (other.getY() * this->z);
this->y = (this->x * other.getZ()) + (other.getX() * this->z);
this->z = (this->x * other.getY()) + (other.getX() * this->y);
return *this;
}
inline float magnitude() const
{
return sqrt( this->magnitudeSquared() );
}
inline float magnitudeSquared() const
{
return (this->x * this->x) + (this->y * this->y) + (this->z * this->z);
}
inline Vector3 normalize()
{
if(this->x != 0 || this->y != 0 || this->z != 0)
{
float mag = 1.0f/this->magnitude();
this->x *= mag;
this->y *= mag;
this->z *= mag;
}
else
this->setValues(0,0,0);
return *this;
}
inline static Vector3 normalize(const Vector3 &vector)
{
Vector3 temp(0,0,0);
if(vector.getX() != 0 || vector.getY() != 0 || vector.getZ() != 0)
{
float mag = 1.0f/vector.magnitude();
temp.setValues(vector.getZ() * mag,vector.getY() * mag,vector.getZ() * mag);
}
return temp;
}
inline Vector3 operator -() const
{
return Vector3(-this->x, -this->y, -this->z);
}
inline float getX() const
{
return this->x;
}
inline float getY() const
{
return this->y;
}
inline float getZ() const
{
return this->z;
}
inline void setX(float newX)
{
this->x = newX;
}
inline void setY(float newY)
{
this->y = newY;
}
inline void setZ(float newZ)
{
this->z = newZ;
}
inline void Vector3::setValues(float x, float y, float z)
{
this->x = x;
this->y = y;
this->z = z;
}
float x,y,z;
};
Thanks.