vnl_io_matrix_fixed.hxx
Go to the documentation of this file.
1 // This is core/vnl/io/vnl_io_matrix_fixed.hxx
2 #ifndef vnl_io_matrix_fixed_hxx_
3 #define vnl_io_matrix_fixed_hxx_
4 //:
5 // \file
6 
7 #include "vnl_io_matrix_fixed.h"
8 #include <vnl/vnl_matrix_fixed.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, unsigned m, unsigned n>
16 void vsl_b_write(vsl_b_ostream & os, const vnl_matrix_fixed<T,m,n> & 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_fixed causes segfault
24  if (p.size()>0)
25  vsl_block_binary_write(os, p.data_block(), m*n);
26 }
27 
28 //=================================================================================
29 //: Binary load self from stream.
30 template<class T, unsigned m, unsigned n>
31 void vsl_b_read(vsl_b_istream &is, vnl_matrix_fixed<T,m,n> & p)
32 {
33  if (!is) return;
34 
35  short v;
36  unsigned stream_m, stream_n;
37  vsl_b_read(is, v);
38  switch (v)
39  {
40  case 1:
41  vsl_b_read(is, stream_m);
42  vsl_b_read(is, stream_n);
43  if ( stream_n != n || stream_m != m ) {
44  std::cerr << "I/O ERROR: vsl_b_read(vsl_b_istream&, vnl_matrix_fixed<T>&)\n"
45  << " Expected size " << m << ',' << n << "; got " << stream_m << ',' << stream_n << '\n';
46  is.is().clear(std::ios::badbit); // Set an unrecoverable IO error on stream
47  return;
48  }
49  // Calling begin() on empty matrix_fixed causes segfault
50  if (m*n>0)
51  vsl_b_read_block_old(is, p.begin(), p.size());
52  break;
53 
54  case 2:
55  vsl_b_read(is, stream_m);
56  vsl_b_read(is, stream_n);
57  if ( stream_n != n || stream_m != m ) {
58  std::cerr << "I/O ERROR: vsl_b_read(vsl_b_istream&, vnl_matrix_fixed<T>&)\n"
59  << " Expected size " << m << ',' << n << "; got " << stream_m << ',' << stream_n << '\n';
60  is.is().clear(std::ios::badbit); // Set an unrecoverable IO error on stream
61  return;
62  }
63  // Calling begin() on empty matrix_fixed causes segfault
64  if (m*n>0)
65  vsl_block_binary_read(is, p.data_block(), m*n);
66  break;
67 
68  default:
69  std::cerr << "I/O ERROR: vsl_b_read(vsl_b_istream&, vnl_matrix_fixed<T>&)\n"
70  << " Unknown version number "<< v << '\n';
71  is.is().clear(std::ios::badbit); // Set an unrecoverable IO error on stream
72  return;
73  }
74 }
75 
76 //====================================================================================
77 //: Output a human readable summary to the stream
78 template<class T, unsigned nrows, unsigned ncols>
79 void vsl_print_summary(std::ostream & os,const vnl_matrix_fixed<T,nrows,ncols> & p)
80 {
81  os<<"Size: "<<p.rows()<<" x "<<p.cols()<<std::endl;
82 
83  unsigned int m = 5; unsigned int n = 5;
84 
85 
86  if (m>p.rows()) m=p.rows();
87  if (n>p.cols()) n=p.cols();
88 
89  vsl_indent_inc(os);
90  for (unsigned int i=0;i<m;i++)
91  {
92  os<<vsl_indent()<<" (";
93 
94  for ( unsigned int j=0; j<n; j++)
95  os<<p(i,j)<<' ';
96  if (p.cols()>n) os<<"...";
97  os<<")\n";
98  }
99  if (p.rows()>m) os <<vsl_indent()<<" (...\n";
100  vsl_indent_dec(os);
101 }
102 
103 
104 #define VNL_IO_MATRIX_FIXED_INSTANTIATE(T,m,n) \
105 template void vsl_print_summary(std::ostream &, const vnl_matrix_fixed<T,m,n > &); \
106 template void vsl_b_read(vsl_b_istream &, vnl_matrix_fixed<T,m,n > &); \
107 template void vsl_b_write(vsl_b_ostream &, const vnl_matrix_fixed<T,m,n > &)
108 
109 #endif // vnl_io_matrix_fixed_hxx_
unsigned int cols() const
Return the number of columns.
void vsl_print_summary(std::ostream &os, vnl_bignum const &b)
Print human readable summary of object to a stream.
T const * data_block() const
Access the contiguous block storing the elements in the matrix row-wise. O(1).
#define m
Definition: vnl_vector.h:43
Fixed size, stack-stored, space-efficient matrix.
Definition: vnl_fwd.h:23
unsigned int rows() const
Return the number of rows.
#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.
fixed size matrix
iterator begin()
Iterator pointing to start of data.
unsigned int size() const
Return the total number of elements stored by the matrix.