BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
DetectorMask.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file Device/Detector/DetectorMask.cpp
6 //! @brief Implements class DetectorMask.
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 
18 
19 DetectorMask::DetectorMask() : m_number_of_masked_channels(0) {}
20 
22  : m_shapes(other.m_shapes)
23  , m_mask_of_shape(other.m_mask_of_shape)
24  , m_number_of_masked_channels(other.m_number_of_masked_channels)
25 {
27 }
28 
30 {
31  if (this != &other) {
32  m_shapes = other.m_shapes;
36  // DetectorMask tmp(other);
37  // tmp.swapContent(*this);
38  }
39  return *this;
40 }
41 
42 void DetectorMask::addMask(const IShape2D& shape, bool mask_value)
43 {
44  m_shapes.push_back(shape.clone());
45  m_mask_of_shape.push_back(mask_value);
48 }
49 
51 {
52  if (detector.dimension() != 2)
53  throw std::runtime_error("DetectorMask::initMaskData() -> Error. Attempt "
54  "to add masks to uninitialized detector.");
55 
56  ASSERT(m_shapes.size() == m_mask_of_shape.size());
58 
59  for (size_t dim = 0; dim < detector.dimension(); ++dim) {
60  const IAxis& axis = detector.axis(dim);
61  m_mask_data.addAxis(axis);
62  }
63 
64  process_masks();
65 }
66 
68 {
69  ASSERT(m_shapes.size() == m_mask_of_shape.size());
71 
72  for (size_t dim = 0; dim < data.rank(); ++dim)
73  m_mask_data.addAxis(data.axis(dim));
74 
75  process_masks();
76 }
77 
78 bool DetectorMask::isMasked(size_t index) const
79 {
80  return m_number_of_masked_channels == 0 ? false : m_mask_data[index];
81 }
82 
84 {
85  OutputData<double> data;
87  for (size_t i = 0; i < m_mask_data.getAllocatedSize(); ++i)
88  data[i] = static_cast<double>(m_mask_data[i]);
89  return dynamic_cast<Histogram2D*>(IHistogram::createHistogram(data));
90 }
91 
93 {
94  return m_shapes.size();
95 }
96 
97 const IShape2D* DetectorMask::getMaskShape(size_t mask_index, bool& mask_value) const
98 {
99  if (mask_index >= numberOfMasks())
100  return nullptr;
101  mask_value = m_mask_of_shape[mask_index];
102  return m_shapes[mask_index];
103 }
104 
106 {
107  m_mask_data.setAllTo(false);
108  if (!!m_shapes.empty())
109  return;
110 
112  for (size_t index = 0; index < m_mask_data.getAllocatedSize(); ++index) {
113  Bin1D binx = m_mask_data.getAxisBin(index, 0);
114  Bin1D biny = m_mask_data.getAxisBin(index, 1);
115  // setting mask to the data starting from last shape added
116  bool is_masked(false);
117  for (size_t i_shape = m_shapes.size(); i_shape > 0; --i_shape) {
118  const IShape2D* shape = m_shapes[i_shape - 1];
119  if (shape->contains(binx, biny)) {
120  if (m_mask_of_shape[i_shape - 1])
121  is_masked = true;
122  m_mask_data[index] = m_mask_of_shape[i_shape - 1];
123  break; // index is covered by the shape, stop looking further
124  }
125  }
126  if (is_masked)
128  }
129 }
#define ASSERT(condition)
Definition: Assert.h:31
Defines class Histogram2D.
Defines interface IDetector2D.
Defines class RegionOfInterest.
Collection of detector masks.
Definition: DetectorMask.h:28
void process_masks()
const IShape2D * getMaskShape(size_t mask_index, bool &mask_value) const
int m_number_of_masked_channels
Definition: DetectorMask.h:66
bool isMasked(size_t index) const
size_t numberOfMasks() const
Histogram2D * createHistogram() const
DetectorMask & operator=(const DetectorMask &other)
void addMask(const IShape2D &shape, bool mask_value)
Add mask to the stack of detector masks.
SafePointerVector< IShape2D > m_shapes
Definition: DetectorMask.h:63
void initMaskData(const IDetector2D &detector)
Init the map of masks for the given detector plane.
std::vector< bool > m_mask_of_shape
Definition: DetectorMask.h:64
OutputData< bool > m_mask_data
Definition: DetectorMask.h:65
Two dimensional histogram.
Definition: Histogram2D.h:24
Interface for one-dimensional axes.
Definition: IAxis.h:25
Abstract 2D detector interface.
Definition: IDetector2D.h:31
size_t dimension() const
Returns actual dimensionality of the detector (number of defined axes)
Definition: IDetector.cpp:46
const IAxis & axis(size_t index) const
Definition: IDetector.cpp:56
static IHistogram * createHistogram(const OutputData< double > &source)
Definition: IHistogram.cpp:243
Basic class for all shapes in 2D.
Definition: IShape2D.h:27
virtual IShape2D * clone() const =0
virtual bool contains(double x, double y) const =0
Returns true if point with given coordinates is inside or on border of the shape.
void copyFrom(const OutputData< T > &x)
Definition: OutputData.h:267
size_t rank() const
Returns number of dimensions.
Definition: OutputData.h:56
void setAllTo(const T &value)
Sets content of output data to specific value.
Definition: OutputData.h:476
const IAxis & axis(size_t serial_number) const
returns axis with given serial number
Definition: OutputData.h:318
void copyShapeFrom(const OutputData< U > &other)
Definition: OutputData.h:277
void addAxis(const IAxis &new_axis)
Definition: OutputData.h:295
Bin1D getAxisBin(size_t global_index, size_t i_selected_axis) const
Returns bin of selected axis for given global_index.
Definition: OutputData.h:452
size_t getAllocatedSize() const
Returns total size of data buffer (product of bin number in every dimension).
Definition: OutputData.h:59
void clear()
Sets object into initial state (no dimensions, data)
Definition: OutputData.h:470
size_t size() const
void push_back(T *pointer)
Definition: Bin.h:20