Commit 1df8f39a authored by Davis King's avatar Davis King

Changed the checks in the matrix object so that you are allowed to

assign a matrix of matrices to another matrix of matrices so long
as long as the assignment makes sense.  Previously you were only
allowed to perform this assignment if the inner most matrix types
were exactly the same.

--HG--
extra : convert_revision : svn%3Afdd8eb12-d10e-0410-9acb-85c331704f74/trunk%402281
parent 06faf158
......@@ -10,6 +10,7 @@
#include <sstream>
#include <algorithm>
#include "../memory_manager.h"
#include "../is_kind.h"
#ifdef _MSC_VER
// Disable the following warnings for Visual Studio
......@@ -769,6 +770,24 @@ namespace dlib
const ref_type ref_;
};
// ----------------------------------------------------------------------------------------
template <typename T>
struct is_matrix<matrix_exp<T> > { static const bool value = true; };
template <typename T, long NR, long NC, typename mm>
struct is_matrix<matrix_ref<T,NR,NC,mm> > { static const bool value = true; };
template <typename T, long NR, long NC, typename mm>
struct is_matrix<matrix<T,NR,NC,mm> > { static const bool value = true; };
template <typename T>
struct is_matrix<T&> { static const bool value = is_matrix<T>::value; };
template <typename T>
struct is_matrix<const T&> { static const bool value = is_matrix<T>::value; };
template <typename T>
struct is_matrix<const T> { static const bool value = is_matrix<T>::value; };
/*
is_matrix<T>::value == 1 if T is a matrix type else 0
*/
// ----------------------------------------------------------------------------------------
// This template will perform the needed loop for element multiplication using whichever
......@@ -1493,7 +1512,11 @@ namespace dlib
const matrix_exp<EXP>& m
): matrix_exp<matrix_ref<T,num_rows,num_cols, mem_manager> >(ref_type(*this))
{
COMPILE_TIME_ASSERT((is_same_type<typename EXP::type,type>::value == true));
// You get an error on this line if the matrix m contains a type that isn't
// the same as the type contained in the target matrix.
COMPILE_TIME_ASSERT((is_same_type<typename EXP::type,type>::value == true) ||
(is_matrix<typename EXP::type>::value == true));
// The matrix you are trying to assign m to is a statically sized matrix and
// m's dimensions don't match that of *this.
COMPILE_TIME_ASSERT(EXP::NR == NR || NR == 0 || EXP::NR == 0);
......@@ -1749,8 +1772,9 @@ namespace dlib
const matrix_exp<EXP>& m
)
{
// The matrix you are trying to assign m to is a statically sized matrix and
// m's dimensions don't match that of *this.
// You get an error on this line if the matrix you are trying to
// assign m to is a statically sized matrix and m's dimensions don't
// match that of *this.
COMPILE_TIME_ASSERT(EXP::NR == NR || NR == 0 || EXP::NR == 0);
COMPILE_TIME_ASSERT(EXP::NC == NC || NC == 0 || EXP::NC == 0);
DLIB_ASSERT((NR == 0 || nr() == m.nr()) &&
......@@ -1763,7 +1787,11 @@ namespace dlib
<< "\n\tm.nc(): " << m.nc()
<< "\n\tthis: " << this
);
COMPILE_TIME_ASSERT((is_same_type<typename EXP::type,type>::value == true));
// You get an error on this line if the matrix m contains a type that isn't
// the same as the type contained in the target matrix.
COMPILE_TIME_ASSERT((is_same_type<typename EXP::type,type>::value == true) ||
(is_matrix<typename EXP::type>::value == true));
if (m.destructively_aliases(*this) == false)
{
set_size(m.nr(),m.nc());
......
......@@ -10,7 +10,6 @@
#include <limits>
#include "../pixel.h"
#include "../geometry.h"
#include "../is_kind.h"
#include "../stl_checked.h"
#include <vector>
......@@ -18,24 +17,6 @@
namespace dlib
{
// ----------------------------------------------------------------------------------------
template <typename T>
struct is_matrix<matrix_exp<T> > { static const bool value = true; };
template <typename T, long NR, long NC, typename mm>
struct is_matrix<matrix_ref<T,NR,NC,mm> > { static const bool value = true; };
template <typename T, long NR, long NC, typename mm>
struct is_matrix<matrix<T,NR,NC,mm> > { static const bool value = true; };
template <typename T>
struct is_matrix<T&> { static const bool value = is_matrix<T>::value; };
template <typename T>
struct is_matrix<const T&> { static const bool value = is_matrix<T>::value; };
template <typename T>
struct is_matrix<const T> { static const bool value = is_matrix<T>::value; };
/*
is_matrix<T>::value == 1 if T is a matrix type else 0
*/
// ----------------------------------------------------------------------------------------
/*
......@@ -1671,11 +1652,15 @@ namespace dlib
typename MM,
typename U
>
void set_all_elements (
typename disable_if<is_matrix<U>,void>::type set_all_elements (
matrix<T,NR,NC,MM>& m,
U value
const U& value
)
{
// The value you are trying to assign to each element of the m matrix
// doesn't have the appropriate type.
COMPILE_TIME_ASSERT(is_matrix<T>::value == is_matrix<U>::value);
for (long r = 0; r < m.nr(); ++r)
{
for (long c = 0; c < m.nc(); ++c)
......@@ -1685,6 +1670,29 @@ namespace dlib
}
}
// ----------------------------------------------------------------------------------------
template <
typename T,
long NR,
long NC,
typename MM,
typename U
>
typename enable_if<is_matrix<U>,void>::type set_all_elements (
matrix<T,NR,NC,MM>& m,
const U& value
)
{
for (long r = 0; r < m.nr(); ++r)
{
for (long c = 0; c < m.nc(); ++c)
{
m(r,c) = value;
}
}
}
// ----------------------------------------------------------------------------------------
template <
......
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