aimstil  5.0.5
Matrix3.h
Go to the documentation of this file.
1 #ifndef TIL_MATRIX3_H
2 #define TIL_MATRIX3_H
3 
4 // includes from BOOST library
5 #include "boost/array.hpp"
6 
7 // includes from TIL library
8 #include "til/ghost_array.h"
9 #include "til/til_common.h"
10 #include "til/multi_array.h"
11 #include "til/numeric_array.h"
12 
13 
14 // Ignore specific warnings
15 #ifdef _MSC_VER
16 #pragma warning (push)
17 #pragma warning (disable : 4231) // nonstandard extension used : 'extern' before template explicit instantiation
18 #endif
19 
20 
21 // TODO: This is crap, this has to change entirely...
22 
23 namespace til
24 {
25  //---------------------------------------------------------------------------
26 
27  template < typename TStorage >
28  class matrix
29  {
30  public: // typedefs
32  typedef typename TStorage::value_type value_type;
33  typedef typename TStorage::reference reference;
34  typedef typename TStorage::const_reference const_reference;
35  typedef typename TStorage::range range;
36  typedef typename TStorage::const_range const_range;
37  public: // constructors
39  matrix() : m_data() {}
43  matrix(numeric_array<std::size_t,2> dims, value_type v) : m_data(dims, v) {}
45  matrix(const TStorage & data) : m_data(data) {}
46  public: // set & get
47  const TStorage & data() const { return m_data; }
48  TStorage & data() { return m_data; }
49  public: // functions
51  numeric_array<std::size_t,2> dims() const { return m_data.dims(); }
52  range whole_range() { return m_data.whole_range(); }
53  const_range whole_range() const { return m_data.whole_range(); }
54  public: // operators
56  {
57  return m_data(pos);
58  }
59  const_reference operator()(const numeric_array<std::size_t,2> & pos) const
60  {
61  return m_data(pos);
62  }
63 
64  private: // data
65  TStorage m_data;
66  };
67 
68  //---------------------------------------------------------------------------
69 
70  template < typename TStorage >
71  inline std::ostream &
72  operator<< (std::ostream & os, const matrix<TStorage> & m)
73  {
74  return m.data();
75  }
76 
77  //---------------------------------------------------------------------------
78 
80  template < std::size_t D, typename TPrec >
82  {
84  };
85 
86  //---------------------------------------------------------------------------
87 
89  template < typename T>
90  class Matrix3
91  {
92  public: // typedefs
93  typedef Matrix3<T> Self;
94  typedef T value_type;
96 
98  typedef typename Data::iterator iterator;
99  typedef typename Data::const_iterator const_iterator;
100 
101  public: // constructors & destructor
102 
104  Matrix3() { this->reset(); }
105 
108 
110  Matrix3(T xx, T xy, T xz, T yx, T yy, T yz, T zx, T zy, T zz)
111  {
112  (*this)(0,0) = xx;
113  (*this)(1,0) = xy;
114  (*this)(2,0) = xz;
115  (*this)(0,1) = yx;
116  (*this)(1,1) = yy;
117  (*this)(2,1) = yz;
118  (*this)(0,2) = zx;
119  (*this)(1,2) = zy;
120  (*this)(2,2) = zz;
121  }
122 
123  // TODO: actually, maybe all we need is a range.
124  //row_type row(std::
125 
126  /*virtual*/ ~Matrix3() {} // Denis: removed virtual to avoid link errors on Mac
127 
128  public: // set & get
129 
131  {
132  boost::array<std::size_t,2> d = { {3, 3} };
133  return d;
134  }
135 
136  const T & operator()(std::size_t i, std::size_t j) const
137  {
138  assert(i<3);
139  assert(j<3);
140  return m_values[i+3*j];
141  }
142 
143  T & operator()(std::size_t i, std::size_t j)
144  {
145  assert(i<3);
146  assert(j<3);
147  return m_values[i+3*j];
148  }
149 
150  // Reset the matrix to default values
151  void reset() { for (std::size_t i=0; i<3*3; ++i) m_values[i] = T(); }
152 
153  iterator begin() { return m_values.begin(); }
154  iterator end() { return m_values.end(); }
155  const_iterator begin() const { return m_values.begin(); }
156  const_iterator end() const { return m_values.end(); }
157 
158  public: // operators
159 
160  void operator+=(Self const & mat)
161  {
162  for (int j = 0; j < 3; ++j)
163  for (int i = 0; i < 3; ++i)
164  {
165  (*this)(i,j) += mat(i,j);
166  }
167  }
168 
169  void operator-=(Self const & mat)
170  {
171  for (int j = 0; j < 3; ++j)
172  for (int i = 0; i < 3; ++i)
173  {
174  (*this)(i,j) -= mat(i,j);
175  }
176  }
177 
178  private: // data
179 
180  boost::array<T,3*3> m_values;
181  };
182 
183  //---------------------------------------------------------------------------
184 
185  template < typename T >
186  inline Matrix3<T>
187  operator-(Matrix3<T> const & mat)
188  {
189  Matrix3<T> res;
190  for (int j = 0; j < 3; ++j)
191  for (int i = 0; i < 3; ++i)
192  {
193  res(i,j) = -mat(i,j);
194  }
195  return res;
196  }
197 
198  //---------------------------------------------------------------------------
199 
200  // EXPIMP_TEMPLATE template class TIL_API Matrix3<double>;
201 
202  //---------------------------------------------------------------------------
203 
204  template < typename T1, typename T2 >
207  {
209  for (std::size_t j = 0; j < 3; ++j)
210  {
211  res[j] = m(0,j) * v[0];
212  for (std::size_t i = 1; i < 3; ++i)
213  {
214  res[j] += m(i,j) * v[i];
215  }
216  }
217  return res;
218  }
219 
220  //---------------------------------------------------------------------------
221 
222  template < typename T1, typename T2 >
223  inline T2 operator*(const Matrix3<T1> & m, const T2 & v)
224  {
225  T2 res;
226  for (std::size_t j = 0; j < 3; ++j)
227  {
228  res[j] = m(0,j) * v[0];
229  for (std::size_t i = 1; i < 3; ++i)
230  {
231  res[j] += m(i,j) * v[i];
232  }
233  }
234  return res;
235  }
236 
237  //---------------------------------------------------------------------------
238 
239  template < typename T >
240  inline std::ostream &
241  operator<< (std::ostream & os, const Matrix3<T> & m)
242  {
243  for (int i = 0; i < 3; ++i)
244  {
245  os << (i == 0? "[ " : " ");
246  for (int j = 0; j < 3; ++j)
247  {
248  os << m(j,i) << " ";
249  }
250  os << (i == 2? "]\n":"\n");
251  }
252  return os;
253  }
254 
255  //---------------------------------------------------------------------------
256 
257 } // namespace til
258 
259 
260 #ifdef _MSC_VER
261 #pragma warning(pop)
262 #endif
263 
264 // package includes
265 #include "til/matrix3Tools.h"
266 
267 #endif
const_iterator end() const
Definition: Matrix3.h:156
T & operator()(std::size_t i, std::size_t j)
Definition: Matrix3.h:143
matrix(const TStorage &data)
Initialization from container.
Definition: Matrix3.h:45
numeric_array< std::size_t, 2 > dims() const
Returns matrix size.
Definition: Matrix3.h:51
TStorage & data()
Definition: Matrix3.h:48
Data::const_iterator const_iterator
Definition: Matrix3.h:99
Matrix3< T > operator-(Matrix3< T > const &mat)
Definition: Matrix3.h:187
TStorage::range range
Definition: Matrix3.h:35
const T & operator()(std::size_t i, std::size_t j) const
Definition: Matrix3.h:136
void operator-=(Self const &mat)
Definition: Matrix3.h:169
const TStorage & data() const
Definition: Matrix3.h:47
void operator+=(Self const &mat)
Definition: Matrix3.h:160
Belongs to package Box Do not include directly, include til/Box.h instead.
Definition: Accumulator.h:10
const_iterator begin() const
Definition: Matrix3.h:155
iterator end()
Definition: Matrix3.h:154
Matrix3(T xx, T xy, T xz, T yx, T yy, T yz, T zx, T zy, T zz)
Initialize with elements.
Definition: Matrix3.h:110
General macros, definitions and functions.
matrix()
Default contructor.
Definition: Matrix3.h:39
reference operator()(const numeric_array< std::size_t, 2 > &pos)
Definition: Matrix3.h:55
Matrix3< T > Self
Definition: Matrix3.h:93
TStorage::const_range const_range
Definition: Matrix3.h:36
boost::array< T, 3 *3 > Data
Definition: Matrix3.h:97
TStorage::reference reference
Definition: Matrix3.h:33
const_reference operator()(const numeric_array< std::size_t, 2 > &pos) const
Definition: Matrix3.h:59
Data::iterator iterator
Definition: Matrix3.h:98
A type used in some construtors to disable initialization.
Definition: til_common.h:85
INLINE numeric_array< typename combine< T1, T2 >::type, 3 > operator*(const Affine< T1 > &a, const numeric_array< T2, 3 > &v)
Multiplication between a 3D affine transform and a 3D vector.
Matrix3()
Empty matrix of zeros.
Definition: Matrix3.h:104
A mathematical matrix.
Definition: Matrix3.h:90
boost::array< T, 3 > row_type
Definition: Matrix3.h:95
matrix< TStorage > Self
Definition: Matrix3.h:31
boost::array< std::size_t, 2 > dims() const
Definition: Matrix3.h:130
A full, square matrix whose size is known at compile time.
Definition: Matrix3.h:81
range whole_range()
Definition: Matrix3.h:52
iterator begin()
Definition: Matrix3.h:153
TStorage::value_type value_type
Definition: Matrix3.h:32
matrix(numeric_array< std::size_t, 2 > dims, value_type v)
Initialization with size and fill value.
Definition: Matrix3.h:43
matrix(numeric_array< std::size_t, 2 > dims)
Initialization with size.
Definition: Matrix3.h:41
T value_type
Definition: Matrix3.h:94
TStorage::const_reference const_reference
Definition: Matrix3.h:34
Matrix3(NoInit)
No initialization.
Definition: Matrix3.h:107
const_range whole_range() const
Definition: Matrix3.h:53
matrix< multi_array< numeric_array< TPrec, D *D >, D > > type
Definition: Matrix3.h:83
void reset()
Definition: Matrix3.h:151