IMP  2.0.0
The Integrative Modeling Platform
CenteredMat.h
Go to the documentation of this file.
1 /**
2  * \file CenteredMat.h
3  * \brief Decorator for OpenCV matrix to use relative coordinates
4  * Copyright 2007-2013 IMP Inventors. All rights reserved.
5 */
6 
7 #ifndef IMPEM2D_CENTERED_MAT_H
8 #define IMPEM2D_CENTERED_MAT_H
9 
10 #include "IMP/em2d/em2d_config.h"
12 #include "IMP/exception.h"
13 #include "IMP/macros.h"
15 #include <iostream>
16 
17 IMPEM2D_BEGIN_NAMESPACE
18 
19 //! Decorator for a cv::Mat to use coordinates respect to a point
20 //! Almost always that point is the center
21 class CenteredMat {
22 
23 public:
24  /**
25  * Creates and CenteredMat for the matrx
26  * @param m The matrix
27  * @note The center pixel is not provided. It is assumed
28  * to be the center of the matrix
29  */
30  CenteredMat(cv::Mat &m) {
31  // m is not copied, just a reference is added.
32 
33  IMP_USAGE_CHECK( (m.rows != 0 && m.cols != 0),
34  "CenteredMat: Matrix passed is empty");
35  centered_ = m;
36  center_row_ = static_cast<int>(0.5*m.rows);
37  center_col_ = static_cast<int>(0.5*m.cols);
38  set_starts_and_ends();
39  }
40 
41  /**
42  * Creates a CenteredMat for the matrx
43  * @param m The matrix
44  * @param center_row - The row for the point used as center of CenteredMat
45  * @param center_col - The column for the point used as center of CenteredMat
46  * @return
47  */
48  CenteredMat(cv::Mat &m, int center_row, int center_col) {
49  centered_ = m;
50  if(center_row >= 0 && center_row < m.rows && center_col >=0 &&
51  center_col < m.cols) {
52  center_row_ = center_row;
53  center_col_ = center_col;
54  } else {
55  IMP_THROW("CenteredMat: Center index out of range ",ValueException);
56  }
57  set_starts_and_ends();
58  }
59 
60  /**
61  * get the starting value for a dimension.
62  //! For example, in a matrix of 5x5 centered at the origin (2,2), the
63  //! starting point will be (-2,2)
64  * @param i The dimension to use (0 - rows, 1 - columns)
65  * @return The starting point in the given dimension
66  */
67  int get_start(int i) const {
68  return start_[i];
69  }
70 
71  /**
72  * See get_start() function help. In the example of the 5x5 matrix,
73  * the end values would be (2,2)
74  * @param i The dimension to use (0 - rows, 1 - columns)
75  * @return The end point in the given dimension
76  */
77  int get_end(int i) const {
78  return end_[i];
79  }
80 
81  /**
82  * Returns true if the indices are in the matrix. Remember that the indices
83  * are those respect to the center of CenteredMat
84  * @param i Row
85  * @param j Column
86  * @return True of False
87  */
88  bool get_is_in_range(int i,int j) const {
89  if(i < get_start(0) || i > get_end(0)) return false;
90  if(j < get_start(1) || j > get_end(1)) return false;
91  return true;
92  }
93 
94  /**
95  * Returns the element (i,j) RELATIVE to the center. Remember then that
96  * the indices can be negative. For performance the indices out of range
97  * are NOT checked
98  * @param i row
99  * @param j column
100  * @return the value of the matrix at row i and column j
101  */
102  double& operator()(int i,int j) {
103 // if (!get_is_in_range(i,j)) {
104 // IMP_THROW("CenteredMat () : Index out of range",ValueException);
105 // }
106  return centered_.at<double>(center_row_+i,center_col_+j);
107  }
108 
109  /**
110  * Shows information about the class
111  * @param out Stream used to show the information
112  */
113  void do_show(std::ostream &out) const {
114  out << "Matrix of size: (" << centered_.rows <<","<< centered_.cols
115  <<") centered mat at: (" << center_row_ <<","<< center_col_ <<") start ("
116  << start_[0] <<","<< start_[1] << ") end (" << end_[0] <<","<< end_[1]
117  << ")" << std::endl;
118  }
119 
120  IMP_SHOWABLE_INLINE(CenteredMat, do_show(out));
121 
122 protected:
123 
124  /**
125  * Sets the starting and ending points for the object. See get_start() and
126  * get_end() for more help
127  */
129  start_[0] = -center_row_;
130  start_[1] = -center_col_;
131  end_[0]=centered_.rows - 1 - center_row_;
132  end_[1]=centered_.cols - 1 - center_col_;
133  }
134 
135 
136  int center_row_,center_col_; // center pixel
137  cv::Mat centered_;
138  // starts and ends
139  int start_[2],end_[2];
140 };
141 
142 IMPEM2D_END_NAMESPACE
143 
144 #endif /* IMPEM2D_CENTERED_MAT_H */