BornAgain  1.19.79
Open-source research software to simulate and fit neutron and x-ray reflectometry and grazing-incidence small-angle scattering
DetectorMask.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file Device/Mask/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 
16 #include "Base/Axis/Bin.h"
17 #include "Base/Axis/IAxis.h"
18 #include "Device/Data/Datafield.h"
19 #include "Device/Mask/IShape2D.h"
20 
21 MaskPattern::MaskPattern(IShape2D* shape_, bool doMask_)
22  : shape(shape_)
23  , doMask(doMask_)
24 {
25 }
27 {
28  delete shape;
29 }
31 {
32  return new MaskPattern(shape->clone(), doMask);
33 }
34 
35 DetectorMask::DetectorMask(const IAxis& xAxis, const IAxis& yAxis)
36  : m_xAxis(xAxis.clone())
37  , m_yAxis(yAxis.clone())
38  , m_masked(xAxis.size() * yAxis.size(), false)
39 {
40 }
41 
42 DetectorMask::~DetectorMask() = default;
43 
45  : m_xAxis(other.m_xAxis->clone())
46  , m_yAxis(other.m_yAxis->clone())
47  , m_stack(other.m_stack)
48  , m_masked(other.m_masked)
49  , m_number_of_masked_channels(other.m_number_of_masked_channels)
50 {
51 }
52 
54 {
55  if (this != &other) {
56  m_stack = other.m_stack;
57  m_masked = other.m_masked;
59  }
60  return *this;
61 }
62 
63 void DetectorMask::addMask(const IShape2D& shape, bool mask_value)
64 {
65  m_stack.emplace_back(new MaskPattern(shape.clone(), mask_value));
66  process_masks();
67 }
68 
69 bool DetectorMask::isMasked(size_t i_flat) const
70 {
71  return m_number_of_masked_channels == 0 ? false : m_masked[i_flat];
72 }
73 
75 {
76  return !m_stack.empty();
77 }
78 
80 {
81  return m_stack.size();
82 }
83 
84 const MaskPattern* DetectorMask::patternAt(size_t iMask) const
85 {
86  return m_stack.at(iMask);
87 }
88 
90 {
92  m_masked.clear();
93  m_masked.resize(m_xAxis->size() * m_yAxis->size(), false);
94 
95  if (m_stack.empty())
96  return;
97 
99  for (size_t i_flat = 0; i_flat < m_masked.size(); ++i_flat) {
100  Bin1D binx = m_xAxis->bin((i_flat / m_yAxis->size()) % m_xAxis->size());
101  Bin1D biny = m_yAxis->bin(i_flat % m_yAxis->size());
102  // setting mask to the data starting from last shape added
103  bool is_masked(false);
104  for (int k = m_stack.size() - 1; k >= 0; --k) {
105  const MaskPattern* const pat = m_stack[k];
106  if (pat->shape->contains(binx, biny)) {
107  if (pat->doMask)
108  is_masked = true;
109  m_masked[i_flat] = pat->doMask;
110  break; // i_flat is covered by the shape, stop looking further
111  }
112  }
113  if (is_masked)
115  }
116 }
Defines structs Bin1D, Bin1DCVector.
Defines and implements templated class Datafield.
Defines class DetectorMask.
Defines interface IAxis.
Defines basic class for all 2D shapes.
Definition: Bin.h:20
Collection of detector masks.
Definition: DetectorMask.h:42
void process_masks()
const MaskPattern * patternAt(size_t iMask) const
const IAxis * m_yAxis
Definition: DetectorMask.h:72
int m_number_of_masked_channels
Definition: DetectorMask.h:78
bool isMasked(size_t index) const
bool hasMasks() const
Returns true if has masks.
size_t numberOfMasks() const
const IAxis * m_xAxis
Definition: DetectorMask.h:71
DetectorMask(const IAxis &xAxis, const IAxis &yAxis)
DetectorMask & operator=(const DetectorMask &other)
OwningVector< MaskPattern > m_stack
Definition: DetectorMask.h:73
std::vector< bool > m_masked
Definition: DetectorMask.h:77
void addMask(const IShape2D &shape, bool mask_value)
Add mask to the stack of detector masks. The value "true" means that the area will be excluded from t...
Abstract base class for one-dimensional axes.
Definition: IAxis.h:27
virtual Bin1D bin(size_t index) const =0
retrieve a 1d bin for the given index
virtual size_t size() const =0
Returns the number of bins.
Basic class for all shapes in 2D.
Definition: IShape2D.h:26
IShape2D * clone() const override=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.
MaskPattern(IShape2D *shape_, bool doMask_)
MaskPattern * clone() const
IShape2D * shape
Definition: DetectorMask.h:35
void emplace_back(T *e)
Definition: OwningVector.h:62
size_t size() const
Definition: OwningVector.h:70
bool empty() const
Definition: OwningVector.h:71
T *const & at(int i) const
Definition: OwningVector.h:73