core/vnl/vnl_matlab_print.txx
Go to the documentation of this file.
00001 // This is core/vnl/vnl_matlab_print.txx
00002 // It is different from vnl_matlab_print.cxx
00003 #ifndef vnl_matlab_print_txx_
00004 #define vnl_matlab_print_txx_
00005 // \author fsm
00006 // Adapted from awf's MatOps class.
00007 
00008 #include "vnl_matlab_print.h"
00009 
00010 #include <vcl_iostream.h>
00011 
00012 #include <vnl/vnl_vector.h>
00013 #include <vnl/vnl_matrix.h>
00014 #include <vnl/vnl_vector_fixed.h>
00015 #include <vnl/vnl_matrix_fixed.h>
00016 #include <vnl/vnl_matrix_ref.h>
00017 #include <vnl/vnl_diag_matrix.h>
00018 #include <vnl/vnl_matlab_print_scalar.h>
00019 
00020 //--------------------------------------------------------------------------------
00021 
00022 template <class T>
00023 vcl_ostream &vnl_matlab_print(vcl_ostream& s,
00024                               T const* array,
00025                               unsigned length,
00026                               vnl_matlab_print_format format)
00027 {
00028   char buf[1024];
00029   for (unsigned j=0; j<length; j++ ) {
00030     // Format according to selected style
00031     // In both cases an exact 0 goes out as such
00032     vnl_matlab_print_scalar(array[j], buf, format);
00033     s << buf;
00034   }
00035 
00036   return s;
00037 }
00038 
00039 template <class T>
00040 vcl_ostream &vnl_matlab_print(vcl_ostream &s,
00041                               T const * const *array,
00042                               unsigned rows, unsigned cols,
00043                               vnl_matlab_print_format format)
00044 {
00045   for (unsigned i=0; i<rows; ++i)
00046     vnl_matlab_print(s, array[i], cols, format) << '\n';
00047   return s;
00048 }
00049 
00050 template <class T>
00051 vcl_ostream& vnl_matlab_print(vcl_ostream& s,
00052                               vnl_diag_matrix<T> const& D,
00053                               char const* variable_name,
00054                               vnl_matlab_print_format format)
00055 {
00056   if (variable_name)
00057     s << variable_name << " = diag([ ";
00058 
00059   vnl_matlab_print(s, D.begin(), D.size(), format);
00060 
00061   if (variable_name)
00062     s << " ])\n";
00063 
00064   return s;
00065 }
00066 
00067 template <class T>
00068 vcl_ostream& vnl_matlab_print(vcl_ostream& s,
00069                               vnl_matrix<T> const& M,
00070                               char const* variable_name,
00071                               vnl_matlab_print_format format)
00072 {
00073   if (variable_name)
00074     s << variable_name << " = [ ...\n";
00075 
00076   if (variable_name && M.rows() == 0)
00077     return s << "];\n";
00078 
00079   for (unsigned int i=0; i<M.rows(); i++ ) {
00080     vnl_matlab_print(s, M[i], M.cols(), format);
00081 
00082     if (variable_name && (i == M.rows()-1))
00083       s << " ]";
00084 
00085     s << '\n';
00086   }
00087 
00088   return s;
00089 }
00090 
00091 template <class T>
00092 vcl_ostream& vnl_matlab_print(vcl_ostream& s,
00093                               vnl_vector<T> const & v,
00094                               char const* variable_name,
00095                               vnl_matlab_print_format format)
00096 {
00097   if (variable_name)
00098     s << variable_name << " = [ ";
00099 
00100   vnl_matlab_print(s, v.begin(), v.size(), format);
00101 
00102   if (variable_name)
00103     s << " ]\n";
00104 
00105   return s;
00106 }
00107 
00108 template <class T, unsigned int n, unsigned int m>
00109 vcl_ostream& vnl_matlab_print(vcl_ostream& s,
00110                               vnl_matrix_fixed<T,n,m> const& M,
00111                               char const* variable_name,
00112                               vnl_matlab_print_format format)
00113 {
00114   if (variable_name)
00115     s << variable_name << " = [ ...\n";
00116 
00117   if (variable_name && M.rows() == 0)
00118     return s << "];\n";
00119 
00120   for (unsigned int i=0; i<n; ++i ) {
00121     vnl_matlab_print(s, M[i], m, format);
00122 
00123     if (variable_name && (i == n-1))
00124       s << " ]";
00125 
00126     s << '\n';
00127   }
00128 
00129   return s;
00130 }
00131 
00132 template <class T>
00133 vcl_ostream& vnl_matlab_print(vcl_ostream& s,
00134                               vnl_matrix_ref<T> const& M,
00135                               char const* variable_name,
00136                               vnl_matlab_print_format format)
00137 {
00138   if (variable_name)
00139     s << variable_name << " = [ ...\n";
00140 
00141   if (variable_name && M.rows() == 0)
00142     return s << "];\n";
00143 
00144   for (unsigned int i=0; i<M.rows(); ++i )
00145   {
00146     vnl_matlab_print(s, M[i], M.cols(), format);
00147 
00148     if (variable_name && (i == M.rows()-1))
00149       s << " ]";
00150 
00151     s << '\n';
00152   }
00153 
00154   return s;
00155 }
00156 
00157 template <class T, unsigned int n>
00158 vcl_ostream& vnl_matlab_print(vcl_ostream& s,
00159                               vnl_vector_fixed<T,n> const & v,
00160                               char const* variable_name,
00161                               vnl_matlab_print_format format)
00162 {
00163   if (variable_name)
00164     s << variable_name << " = [ ";
00165 
00166   vnl_matlab_print(s, v.begin(), n, format);
00167 
00168   if (variable_name)
00169     s << " ]\n";
00170 
00171   return s;
00172 }
00173 
00174 //--------------------------------------------------------------------------------
00175 
00176 #undef  VNL_MATLAB_PRINT_INSTANTIATE
00177 #define VNL_MATLAB_PRINT_INSTANTIATE(T) \
00178 template vcl_ostream &vnl_matlab_print(vcl_ostream &, T const*, unsigned, vnl_matlab_print_format); \
00179 template vcl_ostream &vnl_matlab_print(vcl_ostream &, T const* const*, unsigned, unsigned, vnl_matlab_print_format); \
00180 template vcl_ostream &vnl_matlab_print(vcl_ostream &, vnl_diag_matrix<T > const&, char const *, vnl_matlab_print_format); \
00181 template vcl_ostream &vnl_matlab_print(vcl_ostream &, vnl_matrix<T > const&, char const*, vnl_matlab_print_format); \
00182 template vcl_ostream &vnl_matlab_print(vcl_ostream &, vnl_vector<T > const&, char const*, vnl_matlab_print_format); \
00183 template vcl_ostream &vnl_matlab_print(vcl_ostream &, vnl_matrix_ref<T > const&, char const*, vnl_matlab_print_format); \
00184 template vcl_ostream &vnl_matlab_print(vcl_ostream &, vnl_matrix_fixed<T,2,2> const&, char const*, vnl_matlab_print_format); \
00185 template vcl_ostream &vnl_matlab_print(vcl_ostream &, vnl_matrix_fixed<T,2,3> const&, char const*, vnl_matlab_print_format); \
00186 template vcl_ostream &vnl_matlab_print(vcl_ostream &, vnl_matrix_fixed<T,3,2> const&, char const*, vnl_matlab_print_format); \
00187 template vcl_ostream &vnl_matlab_print(vcl_ostream &, vnl_matrix_fixed<T,3,3> const&, char const*, vnl_matlab_print_format); \
00188 template vcl_ostream &vnl_matlab_print(vcl_ostream &, vnl_matrix_fixed<T,2,4> const&, char const*, vnl_matlab_print_format); \
00189 template vcl_ostream &vnl_matlab_print(vcl_ostream &, vnl_matrix_fixed<T,3,4> const&, char const*, vnl_matlab_print_format); \
00190 template vcl_ostream &vnl_matlab_print(vcl_ostream &, vnl_matrix_fixed<T,4,3> const&, char const*, vnl_matlab_print_format); \
00191 template vcl_ostream &vnl_matlab_print(vcl_ostream &, vnl_matrix_fixed<T,4,4> const&, char const*, vnl_matlab_print_format); \
00192 template vcl_ostream &vnl_matlab_print(vcl_ostream &, vnl_matrix_fixed<T,6,8> const&, char const*, vnl_matlab_print_format); \
00193 template vcl_ostream &vnl_matlab_print(vcl_ostream &, vnl_vector_fixed<T,2> const&, char const*, vnl_matlab_print_format); \
00194 template vcl_ostream &vnl_matlab_print(vcl_ostream &, vnl_vector_fixed<T,3> const&, char const*, vnl_matlab_print_format); \
00195 template vcl_ostream &vnl_matlab_print(vcl_ostream &, vnl_vector_fixed<T,4> const&, char const*, vnl_matlab_print_format); \
00196 template vcl_ostream &vnl_matlab_print(vcl_ostream &, vnl_vector_fixed<T,5> const&, char const*, vnl_matlab_print_format); \
00197 template vcl_ostream &vnl_matlab_print(vcl_ostream &, vnl_vector_fixed<T,6> const&, char const*, vnl_matlab_print_format); \
00198 template vcl_ostream &vnl_matlab_print(vcl_ostream &, vnl_vector_fixed<T,7> const&, char const*, vnl_matlab_print_format)
00199 
00200 #endif // vnl_matlab_print_txx_