BornAgain  1.18.0
Simulate and fit neutron and x-ray scattering at grazing incidence
RegionOfInterest.cpp
Go to the documentation of this file.
1 // ************************************************************************** //
2 //
3 // BornAgain: simulate and fit scattering at grazing incidence
4 //
5 //! @file Device/Detector/RegionOfInterest.cpp
6 //! @brief Implements class RegionOfInterest.
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2018
11 //! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************** //
14 
17 #include "Device/Mask/Rectangle.h"
18 
19 RegionOfInterest::RegionOfInterest(const IDetector2D& detector, double xlow, double ylow,
20  double xup, double yup)
21  : RegionOfInterest(xlow, ylow, xup, yup)
22 {
23  initFrom(detector.getAxis(0), detector.getAxis(1));
24 }
25 
26 RegionOfInterest::RegionOfInterest(const OutputData<double>& data, double xlow, double ylow,
27  double xup, double yup)
28  : RegionOfInterest(xlow, ylow, xup, yup)
29 {
30  if (data.getRank() != 2)
31  throw Exceptions::RuntimeErrorException("RegionOfInterest::RegionOfInterest() -> Error. "
32  "Data is not two-dimensional.");
33 
34  initFrom(data.getAxis(0), data.getAxis(1));
35 }
36 
37 RegionOfInterest::RegionOfInterest(double xlow, double ylow, double xup, double yup)
38  : m_rectangle(new Rectangle(xlow, ylow, xup, yup)), m_ax1(0), m_ay1(0), m_ax2(0), m_ay2(0),
39  m_glob_index0(0)
40 {
41 }
42 
44 {
45  return new RegionOfInterest(*this);
46 }
47 
49 
51  : ICloneable(), m_rectangle(other.m_rectangle->clone()), m_ax1(other.m_ax1), m_ay1(other.m_ay1),
52  m_ax2(other.m_ax2), m_ay2(other.m_ay2), m_glob_index0(other.m_glob_index0),
53  m_detector_dims(other.m_detector_dims), m_roi_dims(other.m_roi_dims)
54 {
55 }
56 
58 {
59  return m_rectangle->getXlow();
60 }
61 
63 {
64  return m_rectangle->getYlow();
65 }
66 
68 {
69  return m_rectangle->getXup();
70 }
71 
73 {
74  return m_rectangle->getYup();
75 }
76 
77 size_t RegionOfInterest::detectorIndex(size_t roiIndex) const
78 {
81 }
82 
83 size_t RegionOfInterest::roiIndex(size_t globalIndex) const
84 {
85  size_t ny = ycoord(globalIndex, m_detector_dims);
86  if (ny < m_ay1 || ny > m_ay2)
87  throw Exceptions::RuntimeErrorException("RegionOfInterest::roiIndex() -> Error.");
88 
89  size_t nx = xcoord(globalIndex, m_detector_dims);
90  if (nx < m_ax1 || nx > m_ax2)
91  throw Exceptions::RuntimeErrorException("RegionOfInterest::roiIndex() -> Error.");
92 
93  return ny - m_ay1 + (nx - m_ax1) * m_roi_dims[1];
94 }
95 
97 {
98  return m_roi_dims[0] * m_roi_dims[1];
99 }
100 
102 {
103  return m_detector_dims[0] * m_detector_dims[1];
104 }
105 
106 bool RegionOfInterest::isInROI(size_t detectorIndex) const
107 {
108  size_t ny = ycoord(detectorIndex, m_detector_dims);
109  if (ny < m_ay1 || ny > m_ay2)
110  return false;
111  size_t nx = xcoord(detectorIndex, m_detector_dims);
112  if (nx < m_ax1 || nx > m_ax2)
113  return false;
114  return true;
115 }
116 
117 std::unique_ptr<IAxis> RegionOfInterest::clipAxisToRoi(size_t axis_index, const IAxis& axis) const
118 {
119  size_t nbin1 = (axis_index == 0 ? m_ax1 : m_ay1);
120  size_t nbin2 = (axis_index == 0 ? m_ax2 : m_ay2);
121  return std::unique_ptr<IAxis>(new FixedBinAxis(
122  axis.getName(), nbin2 - nbin1 + 1, axis.getBin(nbin1).m_lower, axis.getBin(nbin2).m_upper));
123 }
124 
125 void RegionOfInterest::initFrom(const IAxis& x_axis, const IAxis& y_axis)
126 {
127  m_detector_dims.push_back(x_axis.size());
128  m_detector_dims.push_back(y_axis.size());
129 
130  m_ax1 = x_axis.findClosestIndex(getXlow());
131  m_ax2 = x_axis.findClosestIndex(getXup());
132  m_ay1 = y_axis.findClosestIndex(getYlow());
133  m_ay2 = y_axis.findClosestIndex(getYup());
134 
135  m_roi_dims.push_back(m_ax2 - m_ax1 + 1);
136  m_roi_dims.push_back(m_ay2 - m_ay1 + 1);
137 
139 }
Defines interface IDetector2D.
Defines class Rectangle.
Defines class RegionOfInterest.
Axis with fixed bin size.
Definition: FixedBinAxis.h:24
Interface for one-dimensional axes.
Definition: IAxis.h:25
virtual size_t findClosestIndex(double value) const =0
find bin index which is best match for given value
virtual Bin1D getBin(size_t index) const =0
retrieve a 1d bin for the given index
virtual size_t size() const =0
retrieve the number of bins
std::string getName() const
retrieve the label of the axis
Definition: IAxis.h:40
Interface for polymorphic classes that should not be copied, except by explicit cloning.
Definition: ICloneable.h:25
Abstract 2D detector interface.
Definition: IDetector2D.h:31
const IAxis & getAxis(size_t index) const
Definition: IDetector.cpp:54
size_t getRank() const
Returns number of dimensions.
Definition: OutputData.h:59
const IAxis & getAxis(size_t serial_number) const
returns axis with given serial number
Definition: OutputData.h:314
The rectangle shape having its axis aligned to the (non-rotated) coordinate system.
Definition: Rectangle.h:24
Defines rectangular area for the detector which will be simulated/fitted.
size_t detectorSize() const
Number of detector bins.
bool isInROI(size_t detectorIndex) const
std::unique_ptr< IAxis > clipAxisToRoi(size_t axis_index, const IAxis &axis) const
size_t xcoord(size_t index, const std::vector< size_t > &dims) const
size_t detectorIndex(size_t roiIndex) const
Converts roi index to the detector index.
void initFrom(const IAxis &x_axis, const IAxis &y_axis)
size_t m_ax1
Number of bins on detector axes corresponding to roi-rectangle.
size_t ycoord(size_t index, const std::vector< size_t > &dims) const
std::vector< size_t > m_detector_dims
size_t roiSize() const
Number of detector bins in ROI area.
std::vector< size_t > m_roi_dims
double getYlow() const
size_t roiIndex(size_t detectorIndex) const
Converts global detector index to ROI index.
double getYup() const
RegionOfInterest * clone() const
std::unique_ptr< Rectangle > m_rectangle
double getXlow() const
size_t m_glob_index0
Detector global index corresponding to the lower left corner of ROI.
RegionOfInterest(const IDetector2D &detector, double xlow, double ylow, double xup, double yup)
double getXup() const
double m_upper
upper bound of the bin
Definition: Bin.h:24
double m_lower
lower bound of the bin
Definition: Bin.h:23