#ifndef MSP_GEOMETRY_BOX_H_
#define MSP_GEOMETRY_BOX_H_
-#include <msp/linal/vector3.h>
#include "hyperbox.h"
namespace Msp {
public:
Box() { }
explicit Box(const LinAl::Vector<T, 3> &d): HyperBox<T, 3>(d) { }
- Box(T w, T h, T d): HyperBox<T, 3>(LinAl::Vector3<T>(w, h, d)) { }
+ Box(T w, T h, T d): HyperBox<T, 3>(LinAl::Vector<T, 3>(w, h, d)) { }
T get_width() const { return this->get_dimension(0); }
T get_height() const { return this->get_dimension(1); }
#ifndef MSP_GEOMETRY_RECTANGLE_H_
#define MSP_GEOMETRY_RECTANGLE_H_
-#include <msp/linal/vector2.h>
#include "hyperbox.h"
namespace Msp {
public:
Rectangle() { }
explicit Rectangle(const LinAl::Vector<T, 2> &d): HyperBox<T, 2>(d) { }
- Rectangle(T w, T h): HyperBox<T, 2>(LinAl::Vector2<T>(w, h)) { }
+ Rectangle(T w, T h): HyperBox<T, 2>(LinAl::Vector<T, 2>(w, h)) { }
T get_width() const { return this->get_dimension(0); }
T get_height() const { return this->get_dimension(1); }
#include "vector.h"
-#include "vector2.h"
-#include "vector3.h"
#include "matrix.h"
#include "squarematrix.h"
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];
-public:
- Vector();
- Vector(const T *d);
- template<typename U>
- Vector(const Vector<U, N> &v);
+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); }
+};
+
+/**
+A general mathematical vector.
+*/
+template<typename T, unsigned N>
+class Vector: public VectorComponents<T, N>
+{
+public:
+ Vector();
+ Vector(const T *);
+ Vector(T, T);
+ Vector(T, T, T);
+ template<typename U>
+ 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>
+inline Vector<T, N>::Vector(T x_, T y_)
+{
+ this->VectorComponents<T, 2>::x = x_;
+ this->VectorComponents<T, 2>::y = y_;
+}
+
+template<typename T, unsigned N>
+inline Vector<T, N>::Vector(T x_, T y_, T z_)
+{
+ this->VectorComponents<T, 3>::x = x_;
+ this->VectorComponents<T, 3>::y = y_;
+ this->VectorComponents<T, 3>::z = z_;
}
template<typename T, unsigned N>
template<typename U>
inline Vector<T, N>::Vector(const Vector<U, N> &v)
{
- std::copy(v.data, v.data+N, data);
+ 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
+++ /dev/null
-#ifndef MSP_LINAL_VECTOR2_H_
-#define MSP_LINAL_VECTOR2_H_
-
-#include "vector.h"
-
-namespace Msp {
-namespace LinAl {
-
-template<typename T>
-class Vector2: public Vector<T, 2>
-{
-public:
- Vector2() { }
- Vector2(const T *d): Vector<T, 2>(d) { }
- Vector2(T, T);
- template<typename U>
- Vector2(const Vector<U, 2> &v): Vector<T, 2>(v) { }
-};
-
-template<typename T>
-inline Vector2<T>::Vector2(T x, T y)
-{
- this->data[0] = x;
- this->data[1] = y;
-}
-
-} // namespace LinAl
-} // namespace Msp
-
-#endif
+++ /dev/null
-#ifndef MSP_LINAL_VECTOR3_H_
-#define MSP_LINAL_VECTOR3_H_
-
-#include "vector.h"
-
-namespace Msp {
-namespace LinAl {
-
-/**
-A three-dimensional vector, applicable for Euclidean space.
-*/
-template<typename T>
-class Vector3: public Vector<T, 3>
-{
-public:
- Vector3() { }
- Vector3(const T *d): Vector<T, 3>(d) { }
- Vector3(T, T, T);
- template<typename U>
- Vector3(const Vector<U, 3> &v): Vector<T, 3>(v) { }
-};
-
-template<typename T>
-inline Vector3<T>::Vector3(T x, T y, T z)
-{
- this->data[0] = x;
- this->data[1] = y;
- this->data[2] = z;
-}
-
-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[1]*v2[2]-v1[2]*v2[1], v1[2]*v2[0]-v1[0]*v2[2], v1[0]*v2[1]-v1[1]*v2[0]);
-}
-
-} // namespace LinAl
-} // namespace Msp
-
-#endif
#include <msp/geometry/box.h>
-#include <msp/linal/vector3.h>
#include <msp/test/test.h>
using namespace Msp;
void HyperBoxTests::ray_intersection()
{
Geometry::Box<double> box(2, 4, 3);
- Geometry::Ray<double, 3> ray(LinAl::Vector3<double>(10, 0, 0), LinAl::Vector3<double>(-1, 0, 0));
+ Geometry::Ray<double, 3> ray(LinAl::Vector<double, 3>(10, 0, 0), LinAl::Vector<double, 3>(-1, 0, 0));
EXPECT(box.check_intersection(ray));
- ray = Geometry::Ray<double, 3>(LinAl::Vector3<double>(10, 0, 0), LinAl::Vector3<double>(1, 0, 0));
+ ray = Geometry::Ray<double, 3>(LinAl::Vector<double, 3>(10, 0, 0), LinAl::Vector<double, 3>(1, 0, 0));
EXPECT(!box.check_intersection(ray));
- ray = Geometry::Ray<double, 3>(LinAl::Vector3<double>(9, 0, 11.45), LinAl::Vector3<double>(-1, 0, -1));
+ ray = Geometry::Ray<double, 3>(LinAl::Vector<double, 3>(9, 0, 11.45), LinAl::Vector<double, 3>(-1, 0, -1));
EXPECT(box.check_intersection(ray));
- ray = Geometry::Ray<double, 3>(LinAl::Vector3<double>(9, 0, 11.55), LinAl::Vector3<double>(-1, 0, -1));
+ ray = Geometry::Ray<double, 3>(LinAl::Vector<double, 3>(9, 0, 11.55), LinAl::Vector<double, 3>(-1, 0, -1));
EXPECT(!box.check_intersection(ray));
}
#include <msp/geometry/circle.h>
-#include <msp/linal/vector2.h>
#include <msp/test/test.h>
using namespace Msp;
void HyperSphereTests::ray_intersection()
{
Geometry::Circle<double> circle(1.5);
- Geometry::Ray<double, 2> ray(LinAl::Vector2<double>(2.5, 0), LinAl::Vector2<double>(-1, 0));
+ Geometry::Ray<double, 2> ray(LinAl::Vector<double, 2>(2.5, 0), LinAl::Vector<double, 2>(-1, 0));
EXPECT(circle.check_intersection(ray));
- ray = Geometry::Ray<double, 2>(LinAl::Vector2<double>(2.5, 0), LinAl::Vector2<double>(1, 0));
+ ray = Geometry::Ray<double, 2>(LinAl::Vector<double, 2>(2.5, 0), LinAl::Vector<double, 2>(1, 0));
EXPECT(!circle.check_intersection(ray));
- ray = Geometry::Ray<double, 2>(LinAl::Vector2<double>(2.5, 0), LinAl::Vector2<double>(-4, 2.95));
+ ray = Geometry::Ray<double, 2>(LinAl::Vector<double, 2>(2.5, 0), LinAl::Vector<double, 2>(-4, 2.95));
EXPECT(circle.check_intersection(ray));
- ray = Geometry::Ray<double, 2>(LinAl::Vector2<double>(2.5, 0), LinAl::Vector2<double>(-4, 3.05));
+ ray = Geometry::Ray<double, 2>(LinAl::Vector<double, 2>(2.5, 0), LinAl::Vector<double, 2>(-4, 3.05));
EXPECT(!circle.check_intersection(ray));
}
void TransformedShapeTests::ray_intersection()
{
Geometry::TransformedShape<double, 2> shape(Geometry::Rectangle<double>(2, 1), Geometry::AffineTransformation<double, 2>::rotation(Geometry::Angle<double>::from_degrees(45)));
- Geometry::Ray<double, 2> ray(LinAl::Vector2<double>(3, 1.05), LinAl::Vector2<double>(-1, 0));
+ Geometry::Ray<double, 2> ray(LinAl::Vector<double, 2>(3, 1.05), LinAl::Vector<double, 2>(-1, 0));
EXPECT(shape.check_intersection(ray));
- ray = Geometry::Ray<double, 2>(LinAl::Vector2<double>(2.65, 3.35), LinAl::Vector2<double>(-1, -1));
+ ray = Geometry::Ray<double, 2>(LinAl::Vector<double, 2>(2.65, 3.35), LinAl::Vector<double, 2>(-1, -1));
EXPECT(shape.check_intersection(ray));
- ray = Geometry::Ray<double, 2>(LinAl::Vector2<double>(2.6, 3.4), LinAl::Vector2<double>(-1, -1));
+ ray = Geometry::Ray<double, 2>(LinAl::Vector<double, 2>(2.6, 3.4), LinAl::Vector<double, 2>(-1, -1));
EXPECT(!shape.check_intersection(ray));
}