Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
SymmetricMatrix3x3.cpp
1 /* This file is part of the BIAS library (Basic ImageAlgorithmS).
2 
3  Copyright (C) 2003-2009 (see file CONTACT for details)
4  Multimediale Systeme der Informationsverarbeitung
5  Institut fuer Informatik
6  Christian-Albrechts-Universitaet Kiel
7 
8 
9  BIAS is free software; you can redistribute it and/or modify
10  it under the terms of the GNU Lesser General Public License as published by
11  the Free Software Foundation; either version 2.1 of the License, or
12  (at your option) any later version.
13 
14  BIAS is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU Lesser General Public License for more details.
18 
19  You should have received a copy of the GNU Lesser General Public License
20  along with BIAS; if not, write to the Free Software
21  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA*/
22 
23 #include "SymmetricMatrix3x3.hh"
24 
25 #include <Base/Common/CompareFloatingPoint.hh>
26 #include <Base/Math/Matrix3x3.hh>
27 #include <Base/Math/Matrix.hh>
28 #include <Base/Math/Utils.hh>
29 #include <Base/Debug/Exception.hh>
30 
31 #include <iostream>
32 #include <climits>
33 
34 using namespace std;
35 
36 namespace BIAS {
37 
38  /// @author woelk 11/2007 (c) www.vision-n.de
39  template <class T>
40  SymmetricMatrix3x3<T>::
41  SymmetricMatrix3x3(const SymmetricMatrix3x3<T>& src)
42  {
43  operator=(src);
44  }
45 
46 
47  template <class T>
50  {
51  if (! Equal(src[0][1], src[1][0]) ||
52  ! Equal(src[0][2], src[2][0]) ||
53  ! Equal(src[1][2], src[2][1])){
54  BEXCEPTION("cannot initialize SymmetricMatrix3x3 from non symmtric "
55  "Matrix3x3: "<<src<<endl);
56  }
57  Data_[0] = src[0][0];
58  Data_[1] = src[0][1];
59  Data_[2] = src[0][2];
60  Data_[3] = src[1][1];
61  Data_[4] = src[1][2];
62  Data_[5] = src[2][2];
63  }
64 
65 
66  template <class T>
69  {
70  if (src.num_cols()!=3u || src.num_rows()!=3u){
71  BEXCEPTION("cannot initialize SymmetricMatrix3x3 from matrix of different"
72  "size: "<<src.num_rows()<<"x"<<src.num_cols()<<endl);
73  }
74 
75  if (! Equal(src[0][1], src[1][0]) ||
76  ! Equal(src[0][2], src[2][0]) ||
77  ! Equal(src[1][2], src[2][1])){
78  BEXCEPTION("cannot initialize SymmetricMatrix3x3 from non symmtric "
79  "Matrix3x3: "<<src<<endl);
80  }
81  Data_[0] = src[0][0];
82  Data_[1] = src[0][1];
83  Data_[2] = src[0][2];
84  Data_[3] = src[1][1];
85  Data_[4] = src[1][2];
86  Data_[5] = src[2][2];
87  }
88 
89 
90  template <class T>
93  {
94  //std::cout << "destructor: ~SymmetricMatrix3x3()\n";
95  }
96 
97  template <class T>
100  {
101  memcpy((void *)Data_, (void *)src.Data_, 6*sizeof(T));
102  return *this;
103  }
104 
105  template <class T>
106  unsigned int SymmetricMatrix3x3<T>::
107  SymMatrix3x3ToIndex_(const unsigned row, const unsigned col) const
108  {
109  // access to (x,y) = access to (y, x): The matrix is symmetric
110  // switch such that c is always greater equal r
111  unsigned r, c;
112  if (col>=row){
113  r = row; c = col;
114  } else {
115  r = col; c = row;
116  }
117  BIASASSERT(c>=r);
118  switch (r){
119  case 0:
120  return c;
121  break;
122  case 1:
123  return c+2;
124  break;
125  case 2:
126  return 5;
127  break;
128  }
129  BIASABORT;
130  return numeric_limits<unsigned>::max();
131  }
132 
133 
134  template <class T>
136  operator()(const unsigned row, const unsigned col)
137  {
138  return Data_[SymMatrix3x3ToIndex_(row, col)];
139  }
140 
141 
142  template <class T>
143  const T& SymmetricMatrix3x3<T>::
144  operator()(const unsigned row, const unsigned col) const
145  {
146  return Data_[SymMatrix3x3ToIndex_(row, col)];
147  }
148 
149 
150  template <class T>
153  {
154  dst[0][0] = Data_[0];
155  dst[0][1] = dst[1][0] = Data_[1];
156  dst[0][2] = dst[2][0] = Data_[2];
157  dst[1][1] = Data_[3];
158  dst[1][2] = dst[2][1] = Data_[4];
159  dst[2][2] = Data_[5];
160  }
161 
162 
163  template <class T>
165  GetMatrix() const
166  {
167  Matrix3x3<T> tmp;
168  GetMatrix(tmp);
169  return tmp;
170  }
171 
172 
173  template <class T>
175  Write(std::ostream& os) const
176  {
177  os.write((const char*)(Data_), sizeof(T)*6);
178  if (!os.good()) return -1;
179  return 0;
180  }
181 
182 
183  template <class T>
185  Read(std::istream& is)
186  {
187  is.read((char*)(Data_), sizeof(T)*6);
188  if (!is.good()) return -1;
189  return 0;
190  }
191 
192 
193  /////////////////////////////////////////////////////////////////////////
194  // stream operators
195  /////////////////////////////////////////////////////////////////////////
196 
197  template <class T> BIASMathBase_EXPORT
198  std::ostream& operator<<(std::ostream& os, const SymmetricMatrix3x3<T>& mat)
199  {
200  if (IsConsoleStream(os)){
201  os << mat.Data_[0] << "\t" << mat.Data_[1] << "\t" << mat.Data_[2] << endl
202  << "--" << "\t" << mat.Data_[3] << "\t" << mat.Data_[4] << endl
203  << "--" << "\t" << "--" << "\t" << mat.Data_[5] << endl;
204  } else {
205  for (int i=0; i<6; i++)
206  os << mat.Data_[i] << " ";
207  }
208  return os;
209  }
210 
211 
212  template <class T> BIASMathBase_EXPORT
213  std::istream& operator>>(std::istream& is, SymmetricMatrix3x3<T>& mat)
214  {
215  /*if (IsConsoleStream(is)){
216  string tmp;
217  is >> mat.Data_[0] >> mat.Data_[1] >> mat.Data_[2]
218  >> tmp >> mat.Data_[3] >> mat.Data_[4]
219  >> tmp >> tmp >> mat.Data_[5];
220  } else {*/
221  for (int i=0; i<6; i++)
222  is >> mat.Data_[i] ;
223  //}
224  return is;
225  }
226 
227 
228 }
229 
230 #define INST(type) \
231  template class BIASMathBase_EXPORT BIAS::SymmetricMatrix3x3<type>; \
232  template BIASMathBase_EXPORT ostream& BIAS::operator<<<type>(ostream& os, const SymmetricMatrix3x3<type>& mat); \
233  template BIASMathBase_EXPORT istream& BIAS::operator>><type>(istream& is, SymmetricMatrix3x3<type>& mat);
234 
235 INST(unsigned char)
236 INST(char)
237 INST(short)
238 INST(unsigned short)
239 INST(int)
240 INST(unsigned int)
241 INST(float)
242 INST(double)
Subscript num_cols() const
Definition: cmat.h:320
T Data_[6]
The matrix data is stored as follows | Data_[0] Data_[1] Data_[2] | | Data_[1] Data_[3] Data_[4] | | ...
bool IsConsoleStream(const std::ostream &os)
Helper returning true on console ostreams like cerr, cout Useful to distinguish between console (cout...
Definition: Utils.hh:54
is a &#39;fixed size&#39; quadratic matrix of dim.
Definition: Matrix.hh:54
bool Equal(const T left, const T right, const T eps)
comparison function for floating point values See http://www.boost.org/libs/test/doc/components/test_...
matrix class with arbitrary size, indexing is row major.
Subscript num_rows() const
Definition: cmat.h:319
BIASCommon_EXPORT std::istream & operator>>(std::istream &is, BIAS::TimeStamp &ts)
Standard input operator for TimeStamps.
Definition: TimeStamp.cpp:157
is a &#39;fixed size&#39; symmetric quadratic matrix of dim.