Made the dlib::vector object convertible to a dlib::matrix and

also gave it a constructor to convert a matrix to a vector as
well.

--HG--
extra : convert_revision : svn%3Afdd8eb12-d10e-0410-9acb-85c331704f74/trunk%402430
This commit is contained in:
Davis King 2008-07-24 02:54:04 +00:00
parent 583705bef5
commit a5a0e114d2
4 changed files with 88 additions and 1 deletions

View File

@ -9,6 +9,7 @@
#include "../serialize.h"
#include <functional>
#include <iostream>
#include "../matrix/matrix.h"
namespace dlib
{
@ -73,6 +74,35 @@ namespace dlib
// ---------------------------------------
template <typename EXP>
vector ( const matrix_exp<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 <long NR, long NC, typename MM>
operator matrix<T,NR, NC, MM> () const
{
matrix<T,3,1> m;
m(0) = x_value;
m(1) = y_value;
m(2) = z_value;
return m;
}
// ---------------------------------------
~vector (
){}

View File

@ -6,6 +6,7 @@
#include "../serialize.h"
#include <functional>
#include <iostream>
#include "../matrix/matrix_abstract.h"
namespace dlib
{
@ -80,6 +81,35 @@ namespace dlib
- #z() == v.z()
!*/
template <typename EXP>
vector (
const matrix_exp<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 <long NR, long NC, typename MM>
operator matrix<T,NR, NC, MM> (
) 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 (
);
/*!

View File

@ -9,7 +9,7 @@
#include <complex>
#include <limits>
#include "../pixel.h"
#include "../geometry.h"
#include "../geometry/rectangle.h"
#include "../stl_checked.h"
#include <vector>

View File

@ -8,6 +8,7 @@
#include <cstdlib>
#include <ctime>
#include <dlib/string.h>
#include <dlib/matrix.h>
#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<double> 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,"");
}