IMP logo
IMP Reference Guide  2.6.1
The Integrative Modeling Platform
CenteredMat.h
Go to the documentation of this file.
1 /**
2  * \file IMP/em2d/CenteredMat.h
3  * \brief Decorator for OpenCV matrix to use relative coordinates
4  * Copyright 2007-2016 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"
14 #include "IMP/showable_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 matrix
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 matrix
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 { return start_[i]; }
68 
69  /**
70  * See get_start() function help. In the example of the 5x5 matrix,
71  * the end values would be (2,2)
72  * @param i The dimension to use (0 - rows, 1 - columns)
73  * @return The end point in the given dimension
74  */
75  int get_end(int i) const { return end_[i]; }
76 
77  //! Returns true if the indices are in the matrix.
78  /** Remember that the indices
79  are those respect to the center of CenteredMat
80  @param i Row
81  @param j Column
82  @return whether all the indices are in the matrix.
83  */
84  bool get_is_in_range(int i, int j) const {
85  if (i < get_start(0) || i > get_end(0)) return false;
86  if (j < get_start(1) || j > get_end(1)) return false;
87  return true;
88  }
89 
90  //! Returns the element (i,j) RELATIVE to the center.
91  /** Remember then that
92  * the indices can be negative. For performance the indices out of range
93  * are NOT checked
94  * @param i row
95  * @param j column
96  * @return the value of the matrix at row i and column j
97  */
98  double &operator()(int i, int j) {
99  // if (!get_is_in_range(i,j)) {
100  // IMP_THROW("CenteredMat () : Index out of range",ValueException);
101  // }
102  return centered_.at<double>(center_row_ + i, center_col_ + j);
103  }
104 
105  //! Shows information about the class
106  /** @param out Stream used to show the information
107  */
108  void do_show(std::ostream &out) const {
109  out << "Matrix of size: (" << centered_.rows << "," << centered_.cols
110  << ") centered mat at: (" << center_row_ << "," << center_col_
111  << ") start (" << start_[0] << "," << start_[1] << ") end (" << end_[0]
112  << "," << end_[1] << ")" << std::endl;
113  }
114 
115  IMP_SHOWABLE_INLINE(CenteredMat, do_show(out));
116 
117  protected:
118  //! Sets the starting and ending points for the object.
119  /** See get_start() and get_end() for more help
120  */
122  start_[0] = -center_row_;
123  start_[1] = -center_col_;
124  end_[0] = centered_.rows - 1 - center_row_;
125  end_[1] = centered_.cols - 1 - center_col_;
126  }
127 
128  int center_row_, center_col_; // center pixel
129  cv::Mat centered_;
130  // starts and ends
131  int start_[2], end_[2];
132 };
133 
134 IMPEM2D_END_NAMESPACE
135 
136 #endif /* IMPEM2D_CENTERED_MAT_H */
void set_starts_and_ends()
Sets the starting and ending points for the object.
Definition: CenteredMat.h:121
int get_start(int i) const
Definition: CenteredMat.h:67
#define IMP_SHOWABLE_INLINE(Name, how_to_show)
Declare the methods needed by an object that can be printed.
CenteredMat(cv::Mat &m)
Definition: CenteredMat.h:30
double & operator()(int i, int j)
Returns the element (i,j) RELATIVE to the center.
Definition: CenteredMat.h:98
Interface with OpenCV Copyright 2007-2016 IMP Inventors. All rights reserved.
Exception definitions and assertions.
Various general useful macros for IMP.
void do_show(std::ostream &out) const
Shows information about the class.
Definition: CenteredMat.h:108
CenteredMat(cv::Mat &m, int center_row, int center_col)
Definition: CenteredMat.h:48
int get_end(int i) const
Definition: CenteredMat.h:75
#define IMP_THROW(message, exception_name)
Throw an exception with a message.
Definition: check_macros.h:50
#define IMP_USAGE_CHECK(expr, message)
A runtime test for incorrect usage of a class or method.
Definition: check_macros.h:168
An exception for an invalid value being passed to IMP.
Definition: exception.h:137
Various general useful macros for IMP.
bool get_is_in_range(int i, int j) const
Returns true if the indices are in the matrix.
Definition: CenteredMat.h:84