LocARNA-1.8.11
matrices.hh
1 #ifndef LOCARNA_MATRICES_HH
2 #define LOCARNA_MATRICES_HH
3 
4 #ifdef HAVE_CONFIG_H
5 # include <config.h>
6 #endif
7 
8 /* @file Define various generic matrix classes (with templated element
9  type): simple matrix, matrix with range restriction, matrix with
10  offset, rotatable matrix.
11  */
12 
13 #include <iostream>
14 #include <vector>
15 #include <assert.h>
16 
17 #include <algorithm>
18 
19 #include "matrix.hh"
20 
21 namespace LocARNA {
22 
23  // ----------------------------------------
34  template <class elem_t>
35  class RMatrix : public Matrix<elem_t> {
36  typedef typename Matrix<elem_t>::size_type size_type;
37  protected:
38  size_type xl_;
39  size_type xr_;
40  size_type yl_;
41  size_type yr_;
42 
43  size_type xfactor_;
44  size_type offset_;
45 
55  size_type addr(size_type i, size_type j) const {
56  assert(xl_<=i && i<=xr_);
57  assert(yl_<=j && j<=yr_);
58 
59  return i*xfactor_ + j - offset_;
60  }
61 
62  public:
63 
69  : Matrix<elem_t>()
70  {}
71 
72 
82  void
83  resize(size_type xdim, size_type ydim) {
84  this->xdim_=xdim;
85  this->ydim_=ydim;
86  this->mat_.resize(xdim*ydim);
87 
88  restrict(0,xdim-1,0,ydim-1);
89  }
90 
99  void restrict(size_type xl,size_type xr,size_type yl,size_type yr) {
100  assert(xl>=0);
101  assert(yl>=0);
102  assert(xr<this->xdim_);
103  assert(yr<this->ydim_);
104  assert(xl<=xr);
105  assert(yl<=yr);
106 
107  this->xl_=xl;
108  this->xr_=xr;
109  this->yl_=yl;
110  this->yr_=yr;
111 
112  this->xfactor_ = (yr-yl)+1;
113  this->offset_ = 0;
114  this->offset_ = addr(xl,yl);
115  }
116 
126  const elem_t & operator() (size_type i,size_type j) const {
127  return this->mat_[addr(i,j)];
128  }
129 
139  elem_t & operator() (size_type i,size_type j) {
140  return this->mat_[addr(i,j)];
141  }
142 
152  const elem_t get(size_type i,size_type j) const {
153  return this->mat_[addr(i,j)];
154  }
155 
164  void
165  set(size_type i,size_type j, const elem_t &x) {
166  this->mat_[addr(i,j)]=x;
167  }
168 
175  void
176  fill(const elem_t &val) {
177  for (size_type i=xl_; i<xr_; ++i)
178  for (size_type j=yl_; j<yr_; ++j)
179  this->mat_(i,j)=val;
180  }
181  };
182 
183 
184  // ----------------------------------------
186  template <class elem_t>
187  class OMatrix : public Matrix<elem_t> {
188  protected:
189  size_t off_;
190  size_t xoff_;
191  size_t yoff_;
192 
204  size_t addr(size_t i, size_t j) const {
205  assert(xoff_<=i && i<xoff_+this->xdim_);
206  assert(yoff_<=j && j<yoff_+this->ydim_);
207  return i*this->ydim_ + j - off_;
208  }
209 
210  public:
211 
218  : Matrix<elem_t>(),
219  off_(0),
220  xoff_(0),yoff_(0) {
221  }
222 
231  void
232  resize(size_t xdim, size_t ydim, size_t xoff=0, size_t yoff=0) {
233  xoff_=xoff;
234  yoff_=yoff;
235  off_=xoff*ydim+yoff;
236  this->xdim_=xdim;
237  this->ydim_=ydim;
238  this->mat_.resize(xdim*ydim);
239  }
240 
250  const elem_t &
251  operator() (size_t i,size_t j) const {
252  return this->mat_[addr(i,j)];
253  }
254 
264  elem_t &
265  operator() (size_t i,size_t j) {
266  return this->mat_[addr(i,j)];
267  }
268 
278  const elem_t get(size_t i,size_t j) const {
279  return this->mat_[addr(i,j)];
280  }
281 
290  void
291  set(size_t i,size_t j, const elem_t &x) {
292  this->mat_[addr(i,j)]=x;
293  }
294 
295  };
296 
297 
298  // ----------------------------------------
301  template <class elem_t>
302  class RotMatrix: public Matrix<elem_t> {
303 
304  protected:
305  size_t xrot_;
306  size_t yrot_;
307 
317  size_t rot(size_t x, size_t r, size_t d) {
318  assert(r<d);
319  return (x+d-r)%d;
320  }
321 
333  size_t addr(size_t i, size_t j) const {
334  assert(xrot_<=i && i<xrot_+this->xdim_);
335  assert(yrot_<=j && j<yrot_+this->ydim_);
336  return rot(i,xrot_,this->xdim_)*this->xdim_ + rot(j,yrot_,this->ydim_);
337  }
338 
339  public:
340 
347  Matrix<elem_t>(0),
348  xrot_(0),
349  yrot_(0) {
350  }
351 
360  void
361  resize(size_t xdim, size_t ydim, size_t xrot=0, size_t yrot=0) {
362  xrot_=xrot;
363  yrot_=yrot;
364  this->xdim_=xdim;
365  this->ydim_=ydim;
366  this->mat_.resize(xdim*ydim);
367  }
368 
376  void move(size_t xrot, size_t yrot) {
377  xrot_=xrot;
378  yrot_=yrot;
379  }
380 
390  const elem_t & operator() (size_t i,size_t j) const {
391  return this->mat_[addr(i,j)];
392  }
393 
403  elem_t & operator() (size_t i,size_t j) {
404  return this->mat_[addr(i,j)];
405  }
406 
416  const elem_t get(size_t i,size_t j) const {
417  return this->mat_[addr(i,j)];
418  }
419 
429  void
430  set(size_t i,size_t j, const elem_t &x) {
431  this->mat_[addr(i,j)]=x;
432  }
433 
434  };
435 
436 } // end namespace LocARNA
437 
438 #endif // LOCARNA_MATRICES_HH
void fill(const elem_t &val)
Fill the whole matrix with the given value.
Definition: matrices.hh:176
std::vector< elem_t > mat_
vector storing the matrix entries
Definition: matrix.hh:39
size_t addr(size_t i, size_t j) const
Definition: matrices.hh:333
size_type xr_
right end of restriction in first dimension
Definition: matrices.hh:39
size_type offset_
offset for index calculation
Definition: matrices.hh:44
size_type xdim_
first dimension
Definition: matrix.hh:40
Simple matrix class with restriction to a range.
Definition: matrices.hh:35
size_t xrot_
rotation in dimension 1
Definition: matrices.hh:305
size_type yl_
left end of restriction in second dimension
Definition: matrices.hh:40
size_t off_
combined offset for vector access
Definition: matrices.hh:189
size_t yrot_
rotation in dimension 2
Definition: matrices.hh:306
void resize(size_t xdim, size_t ydim, size_t xoff=0, size_t yoff=0)
Definition: matrices.hh:232
size_type addr(size_type i, size_type j) const
Definition: matrices.hh:55
Definition: aligner.cc:17
void move(size_t xrot, size_t yrot)
Definition: matrices.hh:376
A matrix class with rotation.
Definition: matrices.hh:302
size_type ydim_
second dimension
Definition: matrix.hh:41
void restrict(size_type xl, size_type xr, size_type yl, size_type yr)
Set new restricted range.
Definition: matrices.hh:99
elem_t elem_t
type of elements
Definition: matrix.hh:33
size_type yr_
right end of restriction in second dimension
Definition: matrices.hh:41
size_t addr(size_t i, size_t j) const
Computes address/index in 1D vector from 2D matrix indices.
Definition: matrices.hh:204
Simple matrix class with offset.
Definition: matrices.hh:187
size_type xfactor_
factor for index calculation
Definition: matrices.hh:43
size_t xoff_
offset in first dimension
Definition: matrices.hh:190
size_t yoff_
offset in second dimension
Definition: matrices.hh:191
RMatrix()
Definition: matrices.hh:68
simple 2D matrix class, provides access via operator (int,int)
Definition: aligner_impl.hh:19
void resize(size_type xdim, size_type ydim)
Definition: matrices.hh:83
size_t rot(size_t x, size_t r, size_t d)
Definition: matrices.hh:317
size_type xl_
left end of restriction in first dimension
Definition: matrices.hh:38
const elem_t & operator()(size_type i, size_type j) const
Definition: matrices.hh:126
RotMatrix()
Definition: matrices.hh:346
void resize(size_t xdim, size_t ydim, size_t xrot=0, size_t yrot=0)
Definition: matrices.hh:361
OMatrix()
Definition: matrices.hh:217