Skip to content
Snippets Groups Projects
DetectorMask.cpp 3.63 KiB
Newer Older
//  ************************************************************************************************
//  BornAgain: simulate and fit reflection and scattering
Wuttke, Joachim's avatar
Wuttke, Joachim committed
//! @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)
//  ************************************************************************************************
Wuttke, Joachim's avatar
Wuttke, Joachim committed
#include "Device/Mask/DetectorMask.h"
#include "Base/Axis/IAxis.h"
#include "Device/Histo/Histogram2D.h"
#include "Device/Mask/IShape2D.h"
#include "Device/Data/Powerfield.h"
MaskPattern::MaskPattern(IShape2D* shape_, bool doMask_) : shape(shape_), doMask(doMask_) {}
MaskPattern::~MaskPattern() { delete shape; }
MaskPattern* MaskPattern::clone() const { return new MaskPattern(shape->clone(), doMask); }
DetectorMask::DetectorMask()
    : m_masked(new Powerfield<bool>)
    , m_number_of_masked_channels(0)
DetectorMask::DetectorMask(const IAxis& xAxis, const IAxis& yAxis)
    m_masked->addAxis(xAxis);
    m_masked->addAxis(yAxis);

//    m_masked.reset(new Powerfield<bool>(xAxis, yAxis));
DetectorMask::~DetectorMask() = default;

DetectorMask::DetectorMask(const DetectorMask& other)
Wuttke, Joachim's avatar
Wuttke, Joachim committed
    : m_stack(other.m_stack)
    , m_masked(other.m_masked->clone())
    , 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) {
Wuttke, Joachim's avatar
Wuttke, Joachim committed
        m_stack = other.m_stack;
        m_masked.reset(other.m_masked->clone());
        m_number_of_masked_channels = other.m_number_of_masked_channels;
    }
    return *this;
}

void DetectorMask::addMask(const IShape2D& shape, bool mask_value)
{
Wuttke, Joachim's avatar
Wuttke, Joachim committed
    m_stack.emplace_back(new MaskPattern(shape.clone(), mask_value));
void DetectorMask::initMaskData(const IAxis& xAxis, const IAxis& yAxis)
    m_masked->addAxis(xAxis);
    m_masked->addAxis(yAxis);
bool DetectorMask::isMasked(size_t index) const
{
    return m_number_of_masked_channels == 0 ? false : (*m_masked)[index];
Wuttke, Joachim's avatar
Wuttke, Joachim committed
bool DetectorMask::hasMasks() const
{
Wuttke, Joachim's avatar
Wuttke, Joachim committed
    return !m_stack.empty();
size_t DetectorMask::numberOfMasks() const
{
Wuttke, Joachim's avatar
Wuttke, Joachim committed
    return m_stack.size();
const IShape2D* DetectorMask::getMaskShape(size_t mask_index, bool& mask_value) const
{
    if (mask_index >= numberOfMasks())
Wuttke, Joachim's avatar
Wuttke, Joachim committed
    mask_value = m_stack[mask_index]->doMask;
    return m_stack[mask_index]->shape;
void DetectorMask::process_masks()
{
    m_masked->setAllTo(false);
Wuttke, Joachim's avatar
Wuttke, Joachim committed
    if (!!m_stack.empty())

    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);
Wuttke, Joachim's avatar
Wuttke, Joachim committed
        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)
                (*m_masked)[index] = pat->doMask;
                break; // index is covered by the shape, stop looking further
        if (is_masked)
            ++m_number_of_masked_channels;