diff --git a/dlib/geometry/vector.h b/dlib/geometry/vector.h index 47cc86e3d..56ef5d67c 100644 --- a/dlib/geometry/vector.h +++ b/dlib/geometry/vector.h @@ -9,6 +9,7 @@ #include "../serialize.h" #include #include +#include "../matrix/matrix.h" namespace dlib { @@ -73,6 +74,35 @@ namespace dlib // --------------------------------------- + template + vector ( const matrix_exp& m) + { + // make sure requires clause is not broken + DLIB_ASSERT((m.nr() == 1 || m.nc() == 1) && m.size() == 3, + "\t vector(const matrix_exp& m)" + << "\n\t the given matrix is of the wrong size" + << "\n\t m.nr(): " << m.nr() + << "\n\t m.nc(): " << m.nc() + << "\n\t m.size(): " << m.size() + << "\n\t this: " << this + ); + x_value = m(0); + y_value = m(1); + z_value = m(2); + } + + template + operator matrix () const + { + matrix m; + m(0) = x_value; + m(1) = y_value; + m(2) = z_value; + return m; + } + + // --------------------------------------- + ~vector ( ){} diff --git a/dlib/geometry/vector_abstract.h b/dlib/geometry/vector_abstract.h index 3be990fd3..d30b33180 100644 --- a/dlib/geometry/vector_abstract.h +++ b/dlib/geometry/vector_abstract.h @@ -6,6 +6,7 @@ #include "../serialize.h" #include #include +#include "../matrix/matrix_abstract.h" namespace dlib { @@ -80,6 +81,35 @@ namespace dlib - #z() == v.z() !*/ + template + vector ( + const matrix_exp& m + ); + /*! + requires + - m.size() == 3 + - m.nr() == 1 || m.nc() == 1 (i.e. m must be a row or column matrix) + ensures + - #x() == m(0) + - #y() == m(1) + - #z() == m(2) + !*/ + + template + operator matrix ( + ) const; + /*! + ensures + - provides automatic conversions from a vector object to a column + matrix + - returns a matrix object m such that: + - m.nr() == 3 + - m.nc() == 1 + - m(0) == x() + - m(1) == y() + - m(2) == z() + !*/ + ~vector ( ); /*! diff --git a/dlib/matrix/matrix_utilities.h b/dlib/matrix/matrix_utilities.h index 6ec48a298..63cf422c4 100644 --- a/dlib/matrix/matrix_utilities.h +++ b/dlib/matrix/matrix_utilities.h @@ -9,7 +9,7 @@ #include #include #include "../pixel.h" -#include "../geometry.h" +#include "../geometry/rectangle.h" #include "../stl_checked.h" #include diff --git a/dlib/test/geometry.cpp b/dlib/test/geometry.cpp index ef5914627..c72920d78 100644 --- a/dlib/test/geometry.cpp +++ b/dlib/test/geometry.cpp @@ -8,6 +8,7 @@ #include #include #include +#include #include "tester.h" @@ -102,6 +103,32 @@ namespace DLIB_CASSERT(sin,""); DLIB_CASSERT(sin.get() == EOF,""); + + v1.x() = 1; + v1.y() = 2; + v1.z() = 3; + + matrix mv = v1; + DLIB_CASSERT(mv.nr() == 3,""); + DLIB_CASSERT(mv.nc() == 1,""); + DLIB_CASSERT(mv(0) == 1,""); + DLIB_CASSERT(mv(1) == 2,""); + DLIB_CASSERT(mv(2) == 3,""); + + set_all_elements(mv,0); + DLIB_CASSERT(mv(0) == 0,""); + DLIB_CASSERT(mv(1) == 0,""); + DLIB_CASSERT(mv(2) == 0,""); + + mv(0) = 5; + mv(1) = 6; + mv(2) = 7; + + v1 = mv; + DLIB_CASSERT(v1.x() == 5,""); + DLIB_CASSERT(v1.y() == 6,""); + DLIB_CASSERT(v1.z() == 7,""); + }