vnl_xio_matrix.hxx
Go to the documentation of this file.
1 // This is core/vnl/xio/vnl_xio_matrix.hxx
2 #ifndef vnl_xio_matrix_hxx_
3 #define vnl_xio_matrix_hxx_
4 
5 #include "vnl_xio_matrix.h"
6 #include <vnl/vnl_matrix.h>
7 #include <vsl/vsl_basic_xml_element.h>
8 
9 //=================================================================================
10 template<class T>
11 void x_write(std::ostream & os, vnl_matrix<T> const& M, std::string name)
12 {
13  vsl_basic_xml_element element(name);
14  element.add_attribute("rows", (int) M.rows());
15  element.add_attribute("cols", (int) M.cols());
16  for (unsigned int r=0; r<M.rows(); ++r) {
17  for (unsigned int c=0; c<M.cols(); ++c)
18  element.append_cdata(M.get(r,c));
19  }
20  element.x_write(os);
21 }
22 
23 //=================================================================================
24 template<class T>
25 void x_write_tree(std::ostream & os, vnl_matrix<T> const& M, std::string name)
26 {
27  vsl_basic_xml_element element(name);
28  element.add_attribute("rows", (int) M.rows());
29  element.add_attribute("cols", (int) M.cols());
30  for (unsigned int r=0; r<M.rows(); ++r) {
31  element.append_cdata("<row>");
32  for (unsigned int c=0; c<M.cols(); ++c) {
33  element.append_cdata("<cell>");
34  element.append_cdata(M.get(r,c));
35  element.append_cdata("</cell>");
36  }
37  element.append_cdata("</row>");
38  }
39  element.x_write(os);
40 }
41 
42 #undef VNL_XIO_MATRIX_INSTANTIATE
43 #define VNL_XIO_MATRIX_INSTANTIATE(T) \
44 template VNL_EXPORT void x_write(std::ostream &, vnl_matrix<T > const&, std::string); \
45 template VNL_EXPORT void x_write_tree(std::ostream &, vnl_matrix<T > const&, std::string)
46 
47 #endif // vnl_xio_matrix_hxx_
unsigned int cols() const
Return the number of columns.
Definition: vnl_matrix.h:183
XML write a vnl_matrix Two functions which write a valid XML fragment to an ostream.
An ordinary mathematical matrix.
void VNL_EXPORT x_write_tree(std::ostream &os, vnl_matrix< T > const &v, std::string name="vnl_matrix")
XML save vnl_matrix as a 3-level tree to stream.
T get(unsigned r, unsigned c) const
get element with boundary checks if error checking is on.
Definition: vnl_matrix.h:684
An ordinary mathematical matrix.
Definition: vnl_adjugate.h:22
unsigned int rows() const
Return the number of rows.
Definition: vnl_matrix.h:179
void VNL_EXPORT x_write(std::ostream &os, vnl_matrix< T > const &v, std::string name="vnl_matrix")
XML save vnl_matrix to stream.