vnl_io_matrix.hxx
Go to the documentation of this file.
1 // This is core/vnl/io/vnl_io_matrix.hxx
2 #ifndef vnl_io_matrix_hxx_
3 #define vnl_io_matrix_hxx_
4 //:
5 // \file
6 
7 #include "vnl_io_matrix.h"
8 #include <vnl/vnl_matrix.h>
9 #include <vsl/vsl_b_read_block_old.h>
10 #include <vsl/vsl_block_binary.h>
11 #include <vsl/vsl_indent.h>
12 
13 //=================================================================================
14 //: Binary save self to stream.
15 template<class T>
16 void vsl_b_write(vsl_b_ostream & os, const vnl_matrix<T> & p)
17 {
18  constexpr short version_no = 2;
19  vsl_b_write(os, version_no);
20  vsl_b_write(os, p.rows());
21  vsl_b_write(os, p.cols());
22 
23  // Calling p.begin() on empty matrix causes segfault
24  if (p.size()>0)
25  vsl_block_binary_write(os, p.begin(), p.size());
26 }
27 
28 //=================================================================================
29 //: Binary load self from stream.
30 template<class T>
31 void vsl_b_read(vsl_b_istream &is, vnl_matrix<T> & p)
32 {
33  if (!is) return;
34 
35  short v;
36  unsigned m, n;
37  vsl_b_read(is, v);
38  switch (v)
39  {
40  case 1:
41  vsl_b_read(is, m);
42  vsl_b_read(is, n);
43  p.set_size(m, n);
44  // Calling begin() on empty matrix causes segfault
45  if (m*n>0)
46  vsl_b_read_block_old(is, p.begin(), p.size());
47  break;
48 
49  case 2:
50  vsl_b_read(is, m);
51  vsl_b_read(is, n);
52  p.set_size(m, n);
53  // Calling begin() on empty matrix causes segfault
54  if (m*n>0)
55  vsl_block_binary_read(is, p.data_block(), p.size());
56  break;
57 
58  default:
59  std::cerr << "I/O ERROR: vsl_b_read(vsl_b_istream&, vnl_matrix<T>&)\n"
60  << " Unknown version number "<< v << '\n';
61  is.is().clear(std::ios::badbit); // Set an unrecoverable IO error on stream
62  return;
63  }
64 }
65 
66 //====================================================================================
67 //: Output a human readable summary to the stream
68 template<class T>
69 void vsl_print_summary(std::ostream & os,const vnl_matrix<T> & p)
70 {
71  os<<"Size: "<<p.rows()<<" x "<<p.cols()<<std::endl;
72 
73  unsigned int m = 5; unsigned int n = 5;
74 
75 
76  if (m>p.rows()) m=p.rows();
77  if (n>p.cols()) n=p.cols();
78 
79  vsl_indent_inc(os);
80  for (unsigned int i=0;i<m;i++)
81  {
82  os<<vsl_indent()<<" (";
83 
84  for ( unsigned int j=0; j<n; j++)
85  os<<p(i,j)<<' ';
86  if (p.cols()>n) os<<"...";
87  os<<")\n";
88  }
89  if (p.rows()>m) os <<vsl_indent()<<" (...\n";
90  vsl_indent_dec(os);
91 }
92 
93 
94 #define VNL_IO_MATRIX_INSTANTIATE(T) \
95 template VNL_EXPORT void vsl_print_summary(std::ostream &, const vnl_matrix<T > &); \
96 template VNL_EXPORT void vsl_b_read(vsl_b_istream &, vnl_matrix<T > &); \
97 template VNL_EXPORT void vsl_b_write(vsl_b_ostream &, const vnl_matrix<T > &)
98 
99 #endif // vnl_io_matrix_hxx_
unsigned int cols() const
Return the number of columns.
Definition: vnl_matrix.h:183
void vsl_print_summary(std::ostream &os, vnl_bignum const &b)
Print human readable summary of object to a stream.
An ordinary mathematical matrix.
#define m
Definition: vnl_vector.h:43
iterator begin()
Iterator pointing to start of data.
Definition: vnl_matrix.h:620
#define v
Definition: vnl_vector.h:42
void vsl_b_read(vsl_b_istream &is, vnl_bignum &v)
Binary load vnl_bignum from stream.
void vsl_b_write(vsl_b_ostream &os, vnl_bignum const &v)
Binary save vnl_bignum to stream.
An ordinary mathematical matrix.
Definition: vnl_adjugate.h:22
T const * data_block() const
Access the contiguous block storing the elements in the matrix row-wise. O(1).
Definition: vnl_matrix.h:601
unsigned int size() const
Return the total number of elements stored by the matrix.
Definition: vnl_matrix.h:176
unsigned int rows() const
Return the number of rows.
Definition: vnl_matrix.h:179
bool set_size(unsigned r, unsigned c)
Resize to r rows by c columns. Old data lost.
Definition: vnl_matrix.hxx:390