BornAgain  1.19.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 reflection and scattering
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.axis(0), detector.axis(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.rank() != 2)
31  throw std::runtime_error("RegionOfInterest::RegionOfInterest() -> Error. "
32  "Data is not two-dimensional.");
33 
34  initFrom(data.axis(0), data.axis(1));
35 }
36 
37 RegionOfInterest::RegionOfInterest(double xlow, double ylow, double xup, double yup)
38  : m_rectangle(new Rectangle(xlow, ylow, xup, yup))
39  , m_ax1(0)
40  , m_ay1(0)
41  , m_ax2(0)
42  , m_ay2(0)
43  , m_glob_index0(0)
44 {
45 }
46 
48 {
49  return new RegionOfInterest(*this);
50 }
51 
53 
55  : ICloneable()
56  , m_rectangle(other.m_rectangle->clone())
57  , m_ax1(other.m_ax1)
58  , m_ay1(other.m_ay1)
59  , m_ax2(other.m_ax2)
60  , m_ay2(other.m_ay2)
61  , m_glob_index0(other.m_glob_index0)
62  , m_detector_dims(other.m_detector_dims)
63  , m_roi_dims(other.m_roi_dims)
64 {
65 }
66 
68 {
69  return m_rectangle->getXlow();
70 }
71 
73 {
74  return m_rectangle->getYlow();
75 }
76 
78 {
79  return m_rectangle->getXup();
80 }
81 
83 {
84  return m_rectangle->getYup();
85 }
86 
87 size_t RegionOfInterest::detectorIndex(size_t roiIndex) const
88 {
91 }
92 
93 size_t RegionOfInterest::roiIndex(size_t globalIndex) const
94 {
95  size_t ny = ycoord(globalIndex, m_detector_dims);
96  if (ny < m_ay1 || ny > m_ay2)
97  throw std::runtime_error("RegionOfInterest::roiIndex() -> Error.");
98 
99  size_t nx = xcoord(globalIndex, m_detector_dims);
100  if (nx < m_ax1 || nx > m_ax2)
101  throw std::runtime_error("RegionOfInterest::roiIndex() -> Error.");
102 
103  return ny - m_ay1 + (nx - m_ax1) * m_roi_dims[1];
104 }
105 
107 {
108  return m_roi_dims[0] * m_roi_dims[1];
109 }
110 
112 {
113  return m_detector_dims[0] * m_detector_dims[1];
114 }
115 
116 bool RegionOfInterest::isInROI(size_t detectorIndex) const
117 {
118  size_t ny = ycoord(detectorIndex, m_detector_dims);
119  if (ny < m_ay1 || ny > m_ay2)
120  return false;
121  size_t nx = xcoord(detectorIndex, m_detector_dims);
122  if (nx < m_ax1 || nx > m_ax2)
123  return false;
124  return true;
125 }
126 
127 std::unique_ptr<IAxis> RegionOfInterest::clipAxisToRoi(size_t axis_index, const IAxis& axis) const
128 {
129  size_t nbin1 = (axis_index == 0 ? m_ax1 : m_ay1);
130  size_t nbin2 = (axis_index == 0 ? m_ax2 : m_ay2);
131  return std::unique_ptr<IAxis>(new FixedBinAxis(
132  axis.getName(), nbin2 - nbin1 + 1, axis.bin(nbin1).m_lower, axis.bin(nbin2).m_upper));
133 }
134 
135 void RegionOfInterest::initFrom(const IAxis& x_axis, const IAxis& y_axis)
136 {
137  m_detector_dims.push_back(x_axis.size());
138  m_detector_dims.push_back(y_axis.size());
139 
140  m_ax1 = x_axis.findClosestIndex(getXlow());
141  m_ax2 = x_axis.findClosestIndex(getXup());
142  m_ay1 = y_axis.findClosestIndex(getYlow());
143  m_ay2 = y_axis.findClosestIndex(getYup());
144 
145  m_roi_dims.push_back(m_ax2 - m_ax1 + 1);
146  m_roi_dims.push_back(m_ay2 - m_ay1 + 1);
147 
149 }
Defines interface IDetector2D.
Defines class Rectangle.
Defines class RegionOfInterest.
Axis with fixed bin size.
Definition: FixedBinAxis.h:23
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 bin(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 & axis(size_t index) const
Definition: IDetector.cpp:56
size_t rank() const
Returns number of dimensions.
Definition: OutputData.h:56
const IAxis & axis(size_t serial_number) const
returns axis with given serial number
Definition: OutputData.h:318
A rectangle, for use in detector masks.
Definition: Rectangle.h:25
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