BornAgain  1.18.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 scattering at grazing incidence
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), m_mask_of_shape(other.m_mask_of_shape),
23  m_number_of_masked_channels(other.m_number_of_masked_channels)
24 {
26 }
27 
29 {
30  if (this != &other) {
31  m_shapes = other.m_shapes;
35  // DetectorMask tmp(other);
36  // tmp.swapContent(*this);
37  }
38  return *this;
39 }
40 
41 void DetectorMask::addMask(const IShape2D& shape, bool mask_value)
42 {
43  m_shapes.push_back(shape.clone());
44  m_mask_of_shape.push_back(mask_value);
47 }
48 
50 {
51  if (detector.dimension() != 2)
52  throw Exceptions::RuntimeErrorException("DetectorMask::initMaskData() -> Error. Attempt "
53  "to add masks to uninitialized detector.");
54 
55  ASSERT(m_shapes.size() == m_mask_of_shape.size());
57 
58  for (size_t dim = 0; dim < detector.dimension(); ++dim) {
59  const IAxis& axis = detector.getAxis(dim);
60  m_mask_data.addAxis(axis);
61  }
62 
63  process_masks();
64 }
65 
67 {
68  ASSERT(m_shapes.size() == m_mask_of_shape.size());
70 
71  for (size_t dim = 0; dim < data.getRank(); ++dim)
72  m_mask_data.addAxis(data.getAxis(dim));
73 
74  process_masks();
75 }
76 
77 bool DetectorMask::isMasked(size_t index) const
78 {
79  return m_number_of_masked_channels == 0 ? false : m_mask_data[index];
80 }
81 
83 {
84  OutputData<double> data;
86  for (size_t i = 0; i < m_mask_data.getAllocatedSize(); ++i)
87  data[i] = static_cast<double>(m_mask_data[i]);
88  return dynamic_cast<Histogram2D*>(IHistogram::createHistogram(data));
89 }
90 
92 {
93  m_shapes.clear();
94  m_mask_of_shape.clear();
96 }
97 
99 {
100  return m_shapes.size();
101 }
102 
103 const IShape2D* DetectorMask::getMaskShape(size_t mask_index, bool& mask_value) const
104 {
105  if (mask_index >= numberOfMasks())
106  return nullptr;
107  mask_value = m_mask_of_shape[mask_index];
108  return m_shapes[mask_index];
109 }
110 
112 {
113  m_mask_data.setAllTo(false);
114  if (!!m_shapes.empty())
115  return;
116 
118  for (size_t index = 0; index < m_mask_data.getAllocatedSize(); ++index) {
119  Bin1D binx = m_mask_data.getAxisBin(index, 0);
120  Bin1D biny = m_mask_data.getAxisBin(index, 1);
121  // setting mask to the data starting from last shape added
122  bool is_masked(false);
123  for (size_t i_shape = m_shapes.size(); i_shape > 0; --i_shape) {
124  const IShape2D* shape = m_shapes[i_shape - 1];
125  if (shape->contains(binx, biny)) {
126  if (m_mask_of_shape[i_shape - 1])
127  is_masked = true;
128  m_mask_data[index] = m_mask_of_shape[i_shape - 1];
129  break; // index is covered by the shape, stop looking further
130  }
131  }
132  if (is_masked)
134  }
135 }
#define ASSERT(condition)
Definition: Assert.h:26
Defines class Histogram2D.
Defines interface IDetector2D.
Defines class RegionOfInterest.
Collection of detector masks.
Definition: DetectorMask.h:29
void process_masks()
void removeMasks()
remove all masks and return object to initial state
const IShape2D * getMaskShape(size_t mask_index, bool &mask_value) const
int m_number_of_masked_channels
Definition: DetectorMask.h:70
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:67
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:68
OutputData< bool > m_mask_data
Definition: DetectorMask.h:69
Two dimensional histogram.
Definition: Histogram2D.h:25
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:44
const IAxis & getAxis(size_t index) const
Definition: IDetector.cpp:54
static IHistogram * createHistogram(const OutputData< double > &source)
Definition: IHistogram.cpp:233
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:261
void setAllTo(const T &value)
Sets content of output data to specific value.
Definition: OutputData.h:479
void copyShapeFrom(const OutputData< U > &other)
Definition: OutputData.h:271
size_t getRank() const
Returns number of dimensions.
Definition: OutputData.h:59
void addAxis(const IAxis &new_axis)
Definition: OutputData.h:289
Bin1D getAxisBin(size_t global_index, size_t i_selected_axis) const
Returns bin of selected axis for given global_index.
Definition: OutputData.h:455
const IAxis & getAxis(size_t serial_number) const
returns axis with given serial number
Definition: OutputData.h:314
size_t getAllocatedSize() const
Returns total size of data buffer (product of bin number in every dimension).
Definition: OutputData.h:62
void clear()
Sets object into initial state (no dimensions, data)
Definition: OutputData.h:473
size_t size() const
void push_back(T *pointer)
Definition: Bin.h:20