namespace LinAl {
/**
-A general mathematical vector.
+Base class to provide the components of a vector. This is used so that
+specializations with individual members can be provided in some dimensions.
*/
template<typename T, unsigned N>
-class Vector
+class VectorComponents
{
-protected:
+private:
T data[N];
+protected:
+ VectorComponents() { }
+
+public:
+ T &operator[](unsigned i) { return data[i]; }
+ const T &operator[](unsigned i) const { return data[i]; }
+};
+
+template<typename T>
+class VectorComponents<T, 2>
+{
+public:
+ T x, y;
+
+protected:
+ VectorComponents() { }
+
+public:
+ T &operator[](unsigned i) { return *(&x+i); }
+ const T &operator[](unsigned i) const { return *(&x+i); }
+};
+
+template<typename T>
+class VectorComponents<T, 3>
+{
+public:
+ T x, y, z;
+
+protected:
+ VectorComponents() { }
+
+public:
+ T &operator[](unsigned i) { return *(&x+i); }
+ const T &operator[](unsigned i) const { return *(&x+i); }
+};
+
+template<typename T>
+class VectorComponents<T, 4>
+{
+public:
+ T x, y, z, w;
+
+protected:
+ VectorComponents() { }
+
+public:
+ T &operator[](unsigned i) { return *(&x+i); }
+ const T &operator[](unsigned i) const { return *(&x+i); }
+};
+
+/**
+A general mathematical vector.
+*/
+template<typename T, unsigned N>
+class Vector: public VectorComponents<T, N>
+{
public:
Vector();
- Vector(const T *d);
+ Vector(const T *);
+ Vector(T, T);
+ Vector(T, T, T);
+ Vector(T, T, T, T);
template<typename U>
- Vector(const Vector<U, N> &v);
-
- T &operator[](unsigned i);
- const T &operator[](unsigned i) const;
+ Vector(const Vector<U, N> &);
Vector &operator*=(T);
Vector &operator/=(T);
template<typename T, unsigned N>
inline Vector<T, N>::Vector()
{
- std::fill(data, data+N, T());
+ for(unsigned i=0; i<N; ++i)
+ (*this)[i] = T();
}
template<typename T, unsigned N>
inline Vector<T, N>::Vector(const T *d)
{
- std::copy(d, d+N, data);
+ for(unsigned i=0; i<N; ++i)
+ (*this)[i] = d[i];
}
+/* The compiler won't instantiate these unless they are used. Trying to use
+them on the wrong class results in an error. */
template<typename T, unsigned N>
-template<typename U>
-inline Vector<T, N>::Vector(const Vector<U, N> &v)
+inline Vector<T, N>::Vector(T x_, T y_)
{
- std::copy(v.data, v.data+N, data);
+ this->VectorComponents<T, 2>::x = x_;
+ this->VectorComponents<T, 2>::y = y_;
}
template<typename T, unsigned N>
-T &Vector<T, N>::operator[](unsigned i)
+inline Vector<T, N>::Vector(T x_, T y_, T z_)
{
- return data[i];
+ this->VectorComponents<T, 3>::x = x_;
+ this->VectorComponents<T, 3>::y = y_;
+ this->VectorComponents<T, 3>::z = z_;
}
template<typename T, unsigned N>
-const T &Vector<T, N>::operator[](unsigned i) const
+inline Vector<T, N>::Vector(T x_, T y_, T z_, T w_)
{
- return data[i];
+ this->VectorComponents<T, 4>::x = x_;
+ this->VectorComponents<T, 4>::y = y_;
+ this->VectorComponents<T, 4>::z = z_;
+ this->VectorComponents<T, 4>::w = w_;
+}
+
+template<typename T, unsigned N>
+template<typename U>
+inline Vector<T, N>::Vector(const Vector<U, N> &v)
+{
+ for(unsigned i=0; i<N; ++i)
+ (*this)[i] = v[i];
}
template<typename T, unsigned N>
inline Vector<T, N> &Vector<T, N>::operator*=(T s)
{
for(unsigned i=0; i<N; ++i)
- data[i] *= s;
+ (*this)[i] *= s;
return *this;
}
inline Vector<T, N> &Vector<T, N>::operator/=(T s)
{
for(unsigned i=0; i<N; ++i)
- data[i] /= s;
+ (*this)[i] /= s;
return *this;
}
inline Vector<T, N> &Vector<T, N>::operator+=(const Vector<T, N> &v)
{
for(unsigned i=0; i<N; ++i)
- data[i] += v[i];
+ (*this)[i] += v[i];
return *this;
}
inline Vector<T, N> &Vector<T, N>::operator-=(const Vector<T, N> &v)
{
for(unsigned i=0; i<N; ++i)
- data[i] -= v[i];
+ (*this)[i] -= v[i];
return *this;
}
return r.normalize();
}
+template<typename T>
+inline T dot(const Vector<T, 3> &v1, const Vector<T, 3> &v2)
+{
+ return inner_product(v1, v2);
+}
+
+template<typename T>
+inline Vector<T, 3> cross(const Vector<T, 3> &v1, const Vector<T, 3> &v2)
+{
+ return Vector<T, 3>(v1.y*v2.z-v1.z*v2.y, v1.z*v2.x-v1.x*v2.z, v1.x*v2.y-v1.y*v2.x);
+}
+
} // namespace LinAl
} // namespace Msp