]> git.tdb.fi Git - libs/math.git/commitdiff
Redesign the Vector interface
authorMikko Rasa <tdb@tdb.fi>
Fri, 17 May 2013 10:55:57 +0000 (13:55 +0300)
committerMikko Rasa <tdb@tdb.fi>
Fri, 17 May 2013 11:02:36 +0000 (14:02 +0300)
The constructors from two and three components are now provided directly
by the Vector template, so Vector2 and Vector3 are no longer needed.  In
addition, two- and three-component vectors now have named members.

source/geometry/box.h
source/geometry/rectangle.h
source/linal/dummy.cpp
source/linal/vector.h
source/linal/vector2.h [deleted file]
source/linal/vector3.h [deleted file]
tests/box.cpp
tests/sphere.cpp
tests/transformedshape.cpp

index bfcfa2f6e02dc343461375f64713563b57750020..40f88dc4412bb805159fd72197f9c6406a43106b 100644 (file)
@@ -1,7 +1,6 @@
 #ifndef MSP_GEOMETRY_BOX_H_
 #define MSP_GEOMETRY_BOX_H_
 
-#include <msp/linal/vector3.h>
 #include "hyperbox.h"
 
 namespace Msp {
@@ -13,7 +12,7 @@ class Box: public HyperBox<T, 3>
 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); }
index 296ba6c936c959dcb94bb0174b2a1457bd23b562..dbebf8ab73d164f20367f37d2da476f39807e6db 100644 (file)
@@ -1,7 +1,6 @@
 #ifndef MSP_GEOMETRY_RECTANGLE_H_
 #define MSP_GEOMETRY_RECTANGLE_H_
 
-#include <msp/linal/vector2.h>
 #include "hyperbox.h"
 
 namespace Msp {
@@ -13,7 +12,7 @@ class Rectangle: public HyperBox<T, 2>
 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); }
index 25e76a37199cce8ec29c7fdd4bf72fadcbef5242..05e6ff2c39de45d549cd6346b34af29b8caa9568 100644 (file)
@@ -1,6 +1,4 @@
 #include "vector.h"
-#include "vector2.h"
-#include "vector3.h"
 #include "matrix.h"
 #include "squarematrix.h"
 
index 392e3a12772dcdc6e3b9f12dba937c7d38779e29..11292f7441dae7e7f69cefb58aadb73572f687e4 100644 (file)
@@ -8,22 +8,64 @@ namespace Msp {
 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);
@@ -38,27 +80,47 @@ public:
 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;
 }
 
@@ -79,7 +141,7 @@ 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;
 }
 
@@ -94,7 +156,7 @@ template<typename T, unsigned N>
 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;
 }
 
@@ -109,7 +171,7 @@ template<typename T, unsigned N>
 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;
 }
 
@@ -166,6 +228,18 @@ inline Vector<T, N> normalize(const Vector<T, N> &v)
        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
 
diff --git a/source/linal/vector2.h b/source/linal/vector2.h
deleted file mode 100644 (file)
index fed7721..0000000
+++ /dev/null
@@ -1,30 +0,0 @@
-#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
diff --git a/source/linal/vector3.h b/source/linal/vector3.h
deleted file mode 100644 (file)
index 4a623ca..0000000
+++ /dev/null
@@ -1,46 +0,0 @@
-#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
index 860bb4e511a059ab3fd5c3340c8fc1785dd4d109..4e49927df0d339f415d18a827621878ef51cf7a8 100644 (file)
@@ -1,5 +1,4 @@
 #include <msp/geometry/box.h>
-#include <msp/linal/vector3.h>
 #include <msp/test/test.h>
 
 using namespace Msp;
@@ -23,12 +22,12 @@ HyperBoxTests::HyperBoxTests()
 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));
 }
index 7d9a4bbcf7c7648cfdc144623913a6505a5155f4..71754f6cec7cde1346eaf3edad3f64ec032f9d4f 100644 (file)
@@ -1,5 +1,4 @@
 #include <msp/geometry/circle.h>
-#include <msp/linal/vector2.h>
 #include <msp/test/test.h>
 
 using namespace Msp;
@@ -23,12 +22,12 @@ HyperSphereTests::HyperSphereTests()
 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));
 }
index 5468a7330bca8788f63b1bc347528f266e6a5642..540e9b8baa60217497d61d76dd80d2a3ead44fc4 100644 (file)
@@ -23,10 +23,10 @@ TransformedShapeTests::TransformedShapeTests()
 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));
 }