Commit a5a0e114 authored by Davis King's avatar Davis King

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
parent 583705be
......@@ -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 (
){}
......
......@@ -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 (
);
/*!
......
......@@ -9,7 +9,7 @@
#include <complex>
#include <limits>
#include "../pixel.h"
#include "../geometry.h"
#include "../geometry/rectangle.h"
#include "../stl_checked.h"
#include <vector>
......
......@@ -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,"");
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment