diff --git a/src/basics/vector.cc b/src/basics/vector.cc index d9c2c73..ded5a3e 100644 --- a/src/basics/vector.cc +++ b/src/basics/vector.cc @@ -3,6 +3,8 @@ * Eryn Wells */ +#include + #include "basics/vector.hh" @@ -60,6 +62,50 @@ Vector4::Z() return mData[2]; } + +/* + * charles::basics::Vector4::length2 -- + */ +Double +Vector4::Length2() + const +{ + return mData[0] * mData[0] + mData[1] * mData[1] + mData[2] * mData[2]; +} + + +Double +Vector4::Length() + const +{ + return std::sqrt(Length2()); +} + + +Double +Vector4::Dot(const Vector4& rhs) + const +{ + return mData[0] * rhs.mData[0] + mData[1] * rhs.mData[1] + mData[2] + rhs.mData[2]; +} + + +Vector4 +Vector4::Cross(const Vector4& rhs) + const +{ + return Vector4(mData[1]*rhs.mData[2] - mData[2]*rhs.mData[1], + mData[2]*rhs.mData[0] - mData[0]*rhs.mData[2], + mData[0]*rhs.mData[1] - mData[1]*rhs.mData[0]); +} + + +Vector4& +Vector4::Normalize() +{ + return *this /= Length(); +} + } /* namespace basics */ } /* namespace charles */ diff --git a/src/basics/vector.hh b/src/basics/vector.hh index 3c8f8ff..2ef9df4 100644 --- a/src/basics/vector.hh +++ b/src/basics/vector.hh @@ -28,8 +28,27 @@ struct Vector4 Double& X(); Double& Y(); Double& Z(); + + /** Get the length-squared of this vector. */ + Double Length2() const; + + /** Get the length of this vector. */ + Double Length() const; + + /** Get the dot product of `this` and `rhs`. */ + Double Dot(const Vector4& rhs) const; + + /** Get the cross product of `this` and `rhs`. */ + Vector4 Cross(const Vector4& rhs) const; + + /** Normalize this vector. */ + Vector4& Normalize(); }; + +/** Normalize the given vector and return a copy of it. */ +Vector4& Normalized(const Vector4& v); + } /* namespace basics */ } /* namespace charles */