BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
RectangularDetectorItem.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/coregui/Models/RectangularDetectorItem.cpp
6 //! @brief Implements class RectangularDetectorItem
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 
22 
23 namespace {
24 const double default_detector_width = 20.0;
25 const double default_detector_height = 20.0;
26 const double default_detector_distance = 1000.0;
27 const QString tooltip_u0 = "u-coordinate of point of intersection of normal vector "
28  "and detector plane, \n in local detector coordinates";
29 const QString tooltip_v0 = "v-coordinate of point of intersection of normal vector "
30  "and detector plane, \n in local detector coordinates";
31 
32 const QString tooltip_dbeam_u0 = "u-coordinate of point where direct beam hits the detector, \n"
33  "in local detector coordinates [mm]";
34 const QString tooltip_dbeam_v0 = "v-coordinate of point where direct beam hits the detector, \n"
35  "in local detector coordinates [mm]";
36 
37 const QString tooltip_refbeam_u0 =
38  "u-coordinate of point where reflected beam hits the detector, \n"
39  "in local detector coordinates [mm]";
40 const QString tooltip_refbeam_v0 =
41  "v-coordinate of point where reflected beam hits the detector, \n"
42  "in local detector coordinates [mm]";
43 
44 const QString tooltip_samplex_u0 =
45  "u-coordinate of point where sample x-axis crosses the detector, \n"
46  "in local detector coordinates [mm]";
47 const QString tooltip_samplex_v0 =
48  "v-coordinate of point where sample x-axis crosses the detector, \n"
49  "in local detector coordinates [mm]";
50 
51 ComboProperty alignmentCombo()
52 {
53  ComboProperty result;
54  result << "Generic"
55  << "Perpendicular to direct beam"
56  << "Perpendicular to sample x-axis"
57  << "Perpendicular to reflected beam"
58  << "Perpendicular to reflected beam (dpos)";
59  result.setValue("Perpendicular to direct beam");
60  return result;
61 }
62 } // namespace
63 
64 const QString RectangularDetectorItem::P_X_AXIS = "X axis";
65 const QString RectangularDetectorItem::P_Y_AXIS = "Y axis";
66 const QString RectangularDetectorItem::P_ALIGNMENT = "Alignment";
67 const QString RectangularDetectorItem::P_NORMAL = "Normal vector";
68 const QString RectangularDetectorItem::P_DIRECTION = "Direction vector";
69 const QString RectangularDetectorItem::P_U0 = "u0";
70 const QString RectangularDetectorItem::P_V0 = "v0";
71 const QString RectangularDetectorItem::P_DBEAM_U0 = "u0 (dbeam)";
72 const QString RectangularDetectorItem::P_DBEAM_V0 = "v0 (dbeam)";
73 const QString RectangularDetectorItem::P_DISTANCE = "Distance";
74 
76  : DetectorItem("RectangularDetector"), m_is_constructed(false)
77 {
78  // axes parameters
79  auto axisItem = addProperty<BasicAxisItem>(P_X_AXIS);
80  axisItem->getItem(BasicAxisItem::P_TITLE)->setVisible(false);
81  axisItem->getItem(BasicAxisItem::P_MIN_DEG)->setVisible(false);
82  axisItem->setUpperBound(default_detector_width);
83  axisItem->getItem(BasicAxisItem::P_MAX_DEG)->setDisplayName("Width [mm]");
84  axisItem->getItem(BasicAxisItem::P_MAX_DEG)->setToolTip("Width of the detector in mm");
85 
86  axisItem = addProperty<BasicAxisItem>(P_Y_AXIS);
87  axisItem->getItem(BasicAxisItem::P_TITLE)->setVisible(false);
88  axisItem->getItem(BasicAxisItem::P_MIN_DEG)->setVisible(false);
89  axisItem->setUpperBound(default_detector_height);
90  axisItem->getItem(BasicAxisItem::P_MAX_DEG)->setDisplayName("Height [mm]");
91  axisItem->getItem(BasicAxisItem::P_MAX_DEG)->setToolTip("Height of the detector in mm");
92 
93  // alignment selector
94  addProperty(P_ALIGNMENT, alignmentCombo().variant());
95 
96  // alignment parameters
97  auto normalItem = addProperty<VectorItem>(P_NORMAL);
98  normalItem->setX(default_detector_distance);
99 
100  // direction
101  auto directionItem = addProperty<VectorItem>(P_DIRECTION);
102  directionItem->setY(-1.0);
103 
104  addProperty(P_U0, default_detector_width / 2.)
105  ->setToolTip(tooltip_u0)
108  addProperty(P_DBEAM_U0, default_detector_width / 2.)
109  ->setToolTip(tooltip_dbeam_u0)
112 
113  addProperty(P_DISTANCE, default_detector_distance)
114  ->setToolTip("Distance in [mm] from the sample origin to the detector plane");
115 
117 
119  m_is_constructed = true;
120 
121  mapper()->setOnPropertyChange([this](const QString& name) {
124  });
125 }
126 
127 void RectangularDetectorItem::setDetectorAlignment(const QString& alignment)
128 {
129  ComboProperty combo_property =
131 
132  if (!combo_property.getValues().contains(alignment))
133  throw GUIHelpers::Error(
134  "RectangularDetectorItem::setDetectorAlignment -> Unexpected alignment");
135 
136  combo_property.setValue(alignment);
138 }
139 
141 {
142  return xAxisItem()->binCount();
143 }
144 
146 {
147  return yAxisItem()->binCount();
148 }
149 
151 {
152  xAxisItem()->setBinCount(nx);
153 }
154 
156 {
157  yAxisItem()->setBinCount(ny);
158 }
159 
161 {
162  return item<BasicAxisItem>(P_X_AXIS);
163 }
164 
166 {
167  return const_cast<BasicAxisItem*>(
168  static_cast<const RectangularDetectorItem*>(this)->xAxisItem());
169 }
170 
172 {
173  return item<BasicAxisItem>(P_Y_AXIS);
174 }
175 
177 {
178  return const_cast<BasicAxisItem*>(
179  static_cast<const RectangularDetectorItem*>(this)->yAxisItem());
180 }
181 
182 std::unique_ptr<IDetector2D> RectangularDetectorItem::createDomainDetector() const
183 {
184  // basic axes parameters
185  size_t n_x = xAxisItem()->binCount();
186  double width = xAxisItem()->upperBound();
187 
188  size_t n_y = yAxisItem()->binCount();
189  double height = yAxisItem()->upperBound();
190 
191  auto result = std::make_unique<RectangularDetector>(n_x, width, n_y, height);
192 
193  // distance and alighnment
194  double u0 = getItemValue(P_U0).toDouble();
195  double v0 = getItemValue(P_V0).toDouble();
196  double dbeam_u0 = getItemValue(P_DBEAM_U0).toDouble();
197  double dbeam_v0 = getItemValue(P_DBEAM_V0).toDouble();
198  double distance = getItemValue(P_DISTANCE).toDouble();
199 
200  ComboProperty alignment = getItemValue(P_ALIGNMENT).value<ComboProperty>();
201 
202  if (alignment.getValue() == "Generic") {
203  result->setPosition(normalVector(), u0, v0, directionVector());
204  } else if (alignment.getValue() == "Perpendicular to direct beam") {
205  result->setPerpendicularToDirectBeam(distance, dbeam_u0, dbeam_v0);
206  } else if (alignment.getValue() == "Perpendicular to sample x-axis") {
207  result->setPerpendicularToSampleX(distance, u0, v0);
208  } else if (alignment.getValue() == "Perpendicular to reflected beam") {
209  result->setPerpendicularToReflectedBeam(distance, u0, v0);
210  } else if (alignment.getValue() == "Perpendicular to reflected beam (dpos)") {
211  result->setPerpendicularToReflectedBeam(distance);
212  result->setDirectBeamPosition(dbeam_u0, dbeam_v0);
213  }
214  return std::unique_ptr<IDetector2D>(result.release());
215 }
216 
217 //! updates property tooltips and visibility flags, depending from type of alignment selected
219 {
220  // hiding all alignment properties
221  ComboProperty alignment = getItemValue(P_ALIGNMENT).value<ComboProperty>();
222  QStringList prop_list;
223  prop_list << P_NORMAL << P_DIRECTION << P_U0 << P_V0 << P_DBEAM_U0 << P_DBEAM_V0 << P_DISTANCE;
224  for (auto prop : prop_list)
225  getItem(prop)->setVisible(false);
226 
227  // enabling some properties back, depending from detector alignment mode
228  if (alignment.getValue() == "Generic") {
229  getItem(P_NORMAL)->setVisible(true);
231  getItem(P_U0)->setVisible(true);
232  getItem(P_U0)->setToolTip(tooltip_u0);
233  getItem(P_V0)->setVisible(true);
234  getItem(P_V0)->setToolTip(tooltip_v0);
235  } else if (alignment.getValue() == "Perpendicular to sample x-axis") {
236  getItem(P_DISTANCE)->setVisible(true);
237  getItem(P_U0)->setVisible(true);
238  getItem(P_U0)->setToolTip(tooltip_samplex_u0);
239  getItem(P_V0)->setVisible(true);
240  getItem(P_V0)->setToolTip(tooltip_samplex_v0);
241  } else if (alignment.getValue() == "Perpendicular to direct beam") {
242  getItem(P_DISTANCE)->setVisible(true);
243  getItem(P_DBEAM_U0)->setVisible(true);
244  getItem(P_DBEAM_V0)->setVisible(true);
245  } else if (alignment.getValue() == "Perpendicular to reflected beam") {
246  getItem(P_DISTANCE)->setVisible(true);
247  getItem(P_U0)->setVisible(true);
248  getItem(P_U0)->setToolTip(tooltip_refbeam_u0);
249  getItem(P_V0)->setVisible(true);
250  getItem(P_V0)->setToolTip(tooltip_refbeam_v0);
251  } else if (alignment.getValue() == "Perpendicular to reflected beam (dpos)") {
252  getItem(P_DISTANCE)->setVisible(true);
253  getItem(P_DBEAM_U0)->setVisible(true);
254  getItem(P_DBEAM_V0)->setVisible(true);
255  }
256 }
257 
259 {
260  return item<VectorItem>(RectangularDetectorItem::P_NORMAL)->getVector();
261 }
262 
264 {
265  return item<VectorItem>(RectangularDetectorItem::P_DIRECTION)->getVector();
266 }
Defines various axis items.
Defines class ComboProperty.
Defines class GUIHelpers functions.
Defines class RectangularDetectorItem.
Defines class RectangularDetector.
Defines family of ResolutionFunctionItem.
Defines class VectorItem.
void setBinCount(int value)
Definition: AxesItems.cpp:37
static const QString P_MAX_DEG
Definition: AxesItems.h:28
static const QString P_TITLE
Definition: AxesItems.h:29
double upperBound() const
Definition: AxesItems.cpp:52
int binCount() const
Definition: AxesItems.cpp:32
static const QString P_MIN_DEG
Definition: AxesItems.h:27
Custom property to define list of string values with multiple selections.
Definition: ComboProperty.h:25
QStringList getValues() const
QVariant variant() const
Constructs variant enclosing given ComboProperty.
void setValue(const QString &name)
QString getValue() const
void register_resolution_function()
void setOnPropertyChange(std::function< void(QString)> f, const void *caller=0)
Definition: ModelMapper.cpp:35
static RealLimits limitless()
Creates an object withoud bounds (default)
Definition: RealLimits.cpp:130
static const QString P_X_AXIS
const BasicAxisItem * xAxisItem() const
std::unique_ptr< IDetector2D > createDomainDetector() const override
static const QString P_DIRECTION
int ySize() const override
returns the size of y-axis of the detector
void update_properties_appearance()
updates property tooltips and visibility flags, depending from type of alignment selected
static const QString P_Y_AXIS
void setDetectorAlignment(const QString &alignment)
static const QString P_NORMAL
static const QString P_DBEAM_V0
static const QString P_DISTANCE
void setYSize(int ny) override
sets the size of y-axis of the detector
static const QString P_ALIGNMENT
void setXSize(int nx) override
sets the size of x-axis of the detector
int xSize() const override
returns the size of x-axis of the detector
static const QString P_DBEAM_U0
const BasicAxisItem * yAxisItem() const
SessionItem * addProperty(const QString &name, const QVariant &variant)
Add new property item and register new tag.
void setVisible(bool enabled)
Flags accessors.
QVariant getItemValue(const QString &tag) const
Directly access value of item under given tag.
ModelMapper * mapper()
Returns the current model mapper of this item. Creates new one if necessary.
void setItemValue(const QString &tag, const QVariant &variant)
Directly set value of item under given tag.
SessionItem & setToolTip(const QString &tooltip)
SessionItem * getItem(const QString &tag="", int row=0) const
Returns item in given row of given tag.
SessionItem & setLimits(const RealLimits &value)
QString const & name(EShape k)
Definition: particles.cpp:21