// ************************************************************************************************ // // BornAgain: simulate and fit reflection and scattering // //! @file Device/Mask/DetectorMask.cpp //! @brief Implements class DetectorMask. //! //! @homepage http://www.bornagainproject.org //! @license GNU General Public License v3 or higher (see COPYING) //! @copyright Forschungszentrum Jülich GmbH 2018 //! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS) // // ************************************************************************************************ #include "Device/Mask/DetectorMask.h" #include "Base/Axis/IAxis.h" #include "Device/Histo/Histogram2D.h" #include "Device/Mask/IShape2D.h" #include "Base/Types/ICloneable.h" class MaskPattern : public ICloneable { public: MaskPattern(IShape2D* shape_, bool doMask_) : shape(shape_), doMask(doMask_) {} ~MaskPattern() { delete shape; } MaskPattern* clone() const { return new MaskPattern(shape->clone(), doMask); } IShape2D* shape; // owning bool doMask; }; DetectorMask::DetectorMask() : m_number_of_masked_channels(0) { } DetectorMask::DetectorMask(const IAxis& xAxis, const IAxis& yAxis) { m_masked.clear(); m_masked.addAxis(xAxis); m_masked.addAxis(yAxis); process_masks(); } DetectorMask::~DetectorMask() = default; DetectorMask::DetectorMask(const DetectorMask& other) : m_stack(other.m_stack) , m_number_of_masked_channels(other.m_number_of_masked_channels) { m_masked.copyFrom(other.m_masked); } DetectorMask& DetectorMask::operator=(const DetectorMask& other) { if (this != &other) { m_stack = other.m_stack; m_masked.copyFrom(other.m_masked); m_number_of_masked_channels = other.m_number_of_masked_channels; } return *this; } void DetectorMask::addMask(const IShape2D& shape, bool mask_value) { m_stack.emplace_back(new MaskPattern(shape.clone(), mask_value)); m_masked.clear(); m_number_of_masked_channels = 0; } void DetectorMask::initMaskData(const IAxis& xAxis, const IAxis& yAxis) { m_masked.clear(); m_masked.addAxis(xAxis); m_masked.addAxis(yAxis); process_masks(); } bool DetectorMask::isMasked(size_t index) const { return m_number_of_masked_channels == 0 ? false : m_masked[index]; } Histogram2D* DetectorMask::createHistogram() const { Powerfield<double> data; data.copyShapeFrom(m_masked); for (size_t i = 0; i < m_masked.getAllocatedSize(); ++i) data[i] = static_cast<double>(m_masked[i]); return dynamic_cast<Histogram2D*>(IHistogram::createHistogram(data)); } bool DetectorMask::hasMasks() const { return !m_stack.empty(); } size_t DetectorMask::numberOfMasks() const { return m_stack.size(); } const IShape2D* DetectorMask::getMaskShape(size_t mask_index, bool& mask_value) const { if (mask_index >= numberOfMasks()) return nullptr; mask_value = m_stack[mask_index]->doMask; return m_stack[mask_index]->shape; } void DetectorMask::process_masks() { m_masked.setAllTo(false); if (!!m_stack.empty()) return; m_number_of_masked_channels = 0; for (size_t index = 0; index < m_masked.getAllocatedSize(); ++index) { Bin1D binx = m_masked.getAxisBin(index, 0); Bin1D biny = m_masked.getAxisBin(index, 1); // setting mask to the data starting from last shape added bool is_masked(false); for (size_t k = m_stack.size(); k > 0; --k) { const MaskPattern* const pat = m_stack[k - 1]; if (pat->shape->contains(binx, biny)) { if (pat->doMask) is_masked = true; m_masked[index] = pat->doMask; break; // index is covered by the shape, stop looking further } } if (is_masked) ++m_number_of_masked_channels; } }