BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
MaskUnitsConverter.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/Model/Device/MaskUnitsConverter.cpp
6 //! @brief Implements class MaskUnitsConverter
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/FrameUtil.h"
20 
22  : m_data(nullptr)
23  , m_direction(UNDEFINED)
24 {
25 }
26 
27 //! Converts all masks on board of IntensityDataItem into bin-fraction coordinates.
28 
30 {
32  convertIntensityDataItem(intensityData);
33 }
34 
35 //! Converts all masks on board of IntensityDataItem from bin-fraction coordinates to coordinates
36 //! of axes currently defined in Datafield.
37 
39 {
41  convertIntensityDataItem(intensityData);
42 }
43 
44 //! Converts all masks on board of IntensityDataItem from/to bin-fraction coordinates
45 
47 {
48  if (!intensityData || !intensityData->getDatafield())
49  return;
50 
51  m_data = intensityData->getDatafield();
52 
53  if (intensityData->maskContainerItem())
54  for (auto* maskItem : intensityData->maskContainerItem()->maskItems())
55  convertMask(maskItem);
56 
57  if (intensityData->projectionContainerItem())
58  for (SessionItem* item : intensityData->projectionContainerItem()->getItems())
59  if (auto* maskItem = dynamic_cast<MaskItem*>(item))
60  convertMask(maskItem);
61 }
62 
63 //! Converts single mask from/to bin-fraction coordinates
64 
66 {
67  if (auto* rectItem = dynamic_cast<RectangleItem*>(maskItem)) {
68  rectItem->setXLow(convert(rectItem->xLow(), Axis::X));
69  rectItem->setYLow(convert(rectItem->yLow(), Axis::Y));
70  rectItem->setXUp(convert(rectItem->xUp(), Axis::X));
71  rectItem->setYUp(convert(rectItem->yUp(), Axis::Y));
72  } else if (auto* poly = dynamic_cast<PolygonItem*>(maskItem)) {
73  for (PolygonPointItem* pointItem : poly->points()) {
74  pointItem->setPosX(convert(pointItem->posX(), Axis::X));
75  pointItem->setPosY(convert(pointItem->posY(), Axis::Y));
76  }
77  } else if (auto* vlineItem = dynamic_cast<VerticalLineItem*>(maskItem)) {
78  vlineItem->setPosX(convert(vlineItem->posX(), Axis::X));
79  } else if (auto* hlineItem = dynamic_cast<HorizontalLineItem*>(maskItem)) {
80  hlineItem->setPosY(convert(hlineItem->posY(), Axis::Y));
81  } else if (auto* ellItem = dynamic_cast<EllipseItem*>(maskItem)) {
82  double xc = ellItem->xCenter();
83  double yc = ellItem->yCenter();
84  double xR = ellItem->xRadius();
85  double yR = ellItem->yRadius();
86 
87  double x2 = xc + xR;
88  double y2 = yc + yR;
89 
90  if (m_direction == TO_NBINS) {
91  FramUtil::coordinatesToBinf(xc, yc, m_data->frame());
92  FramUtil::coordinatesToBinf(x2, y2, m_data->frame());
93  } else {
94  FramUtil::coordinatesFromBinf(xc, yc, m_data->frame());
95  FramUtil::coordinatesFromBinf(x2, y2, m_data->frame());
96  }
97  ellItem->setXCenter(xc);
98  ellItem->setYCenter(yc);
99  ellItem->setXRadius(x2 - xc);
100  ellItem->setYRadius(y2 - yc);
101  }
102 }
103 
104 //! Convert (x,y) coordinates registered as property (i.e. under P_XPOS, P_YPOS) from/to
105 //! bin-fraction coordinates. Result of operation are new values for registered properties.
106 
107 void MaskUnitsConverter::convertCoordinate(SessionItem* maskItem, const QString& xname,
108  const QString& yname)
109 {
110  if (maskItem->isTag(xname)) {
111  double x = convert(maskItem->getItemValue(xname).toDouble(), Axis::X);
112  maskItem->setItemValue(xname, x);
113  }
114  if (maskItem->isTag(yname)) {
115  double y = convert(maskItem->getItemValue(yname).toDouble(), Axis::Y);
116  maskItem->setItemValue(yname, y);
117  }
118 }
119 
120 //! Convert value of axis from/to bin-fraction coordinates.
121 
122 double MaskUnitsConverter::convert(double value, Axis axis)
123 {
124  auto axis_index = static_cast<size_t>(axis);
125 
126  ASSERT(m_data);
127  ASSERT(axis_index == 0 || axis_index == 1);
128 
129  if (m_direction == TO_NBINS)
130  return FramUtil::coordinateToBinf(value, m_data->axis(axis_index));
131  if (m_direction == FROM_NBINS)
132  return FramUtil::coordinateFromBinf(value, m_data->axis(axis_index));
133  ASSERT(0);
134 }
Defines class IntensityDataItem.
Defines MaskItems classes.
Defines class MaskUnitsConverter.
Defines items related to projections over color map.
Datafield * getDatafield()
Definition: DataItem.h:41
MaskContainerItem * maskContainerItem()
ProjectionContainerItem * projectionContainerItem()
QVector< MaskItem * > maskItems() const
Definition: MaskItems.cpp:38
A base class for all mask items.
Definition: MaskItems.h:27
void convertIntensityDataItem(IntensityDataItem *intensityData)
Converts all masks on board of IntensityDataItem from/to bin-fraction coordinates.
void convertMask(MaskItem *maskItem)
Converts single mask from/to bin-fraction coordinates.
EConvertionDirection m_direction
double convert(double value, Axis axis)
Convert value of axis from/to bin-fraction coordinates.
void convertToNbins(IntensityDataItem *intensityData)
Converts all masks on board of IntensityDataItem into bin-fraction coordinates.
const Datafield * m_data
void convertFromNbins(IntensityDataItem *intensityData)
Converts all masks on board of IntensityDataItem from bin-fraction coordinates to coordinates of axes...
void convertCoordinate(SessionItem *maskItem, const QString &xname, const QString &yname)
Convert (x,y) coordinates registered as property (i.e. under P_XPOS, P_YPOS) from/to bin-fraction coo...
Base class for a GUI data item.
Definition: SessionItem.h:204
bool isTag(const QString &name) const
Returns true if tag is available.
QVector< SessionItem * > getItems(const QString &tag="") const
Returns vector of all items of given tag.
QVariant getItemValue(const QString &tag) const
Directly access value of item under given tag.
void setItemValue(const QString &tag, const QVariant &variant) const
Directly set value of item under given tag.