BornAgain  1.19.79
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/Model/Device/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 
16 #include "Device/Detector/RectangularDetector.h"
22 #include "GUI/Util/ComboProperty.h"
23 #include "GUI/Util/Error.h"
24 
25 namespace {
26 
27 const double default_detector_width = 20.0;
28 const double default_detector_height = 20.0;
29 const double default_detector_distance = 1000.0;
30 
31 QString alignmentTitle(RectangularDetector::EDetectorArrangement a)
32 {
33  switch (a) {
34  case RectangularDetector::GENERIC:
35  return "Generic";
36  case RectangularDetector::PERPENDICULAR_TO_SAMPLE:
37  return "Perpendicular to sample x-axis";
38  case RectangularDetector::PERPENDICULAR_TO_DIRECT_BEAM:
39  return "Perpendicular to direct beam";
40  case RectangularDetector::PERPENDICULAR_TO_REFLECTED_BEAM:
41  return "Perpendicular to reflected beam";
42  case RectangularDetector::PERPENDICULAR_TO_REFLECTED_BEAM_DPOS:
43  return "Perpendicular to reflected beam (intersection unknown)";
44  default:
45  ASSERT(false);
46  }
47 }
48 
49 //! Returns the alignments as expected on the UI
50 QList<RectangularDetector::EDetectorArrangement> alignments()
51 {
52  return {RectangularDetector::GENERIC, RectangularDetector::PERPENDICULAR_TO_SAMPLE,
53  RectangularDetector::PERPENDICULAR_TO_DIRECT_BEAM,
54  RectangularDetector::PERPENDICULAR_TO_REFLECTED_BEAM,
55  RectangularDetector::PERPENDICULAR_TO_REFLECTED_BEAM_DPOS};
56 }
57 
58 void initResolutionFunction(ResolutionFunctionItem* newFunc, const ResolutionFunctionItem*)
59 {
60  newFunc->setUnit("mm");
61 }
62 
63 } // namespace
64 
66 {
68  "Resolution function", "Detector resolution function", "resolutionFunction",
69  initResolutionFunction);
70 
71  m_xSize = 100;
72  m_ySize = 100;
73  m_width.init("Width (x-axis)", "Width of the detector", default_detector_width, "mm", 3,
74  RealLimits::positive(), "width");
75  m_height.init("Height (y-axis)", "Height of the detector", default_detector_height, "mm", 3,
76  RealLimits::positive(), "height");
77 
78  m_normalVector.init(
79  "Normal vector",
80  "Normal of the detector plane with length equal to the sample detector distance",
81  Unit::unitless, "normalVector");
82  m_normalVector.r3().setX(default_detector_distance);
83  m_directionVector.init("Direction vector",
84  "Detector axis direction vector w.r.t. the sample coordinate system",
85  Unit::unitless, "directionVector");
86  m_directionVector.r3().setY(-1.0);
87 
88  m_u0.init("u0", "", default_detector_width / 2., "mm", 3, RealLimits::limitless(), "u0");
89  m_v0.init("v0", "", 0.0, "mm", 3, RealLimits::limitless(), "v0");
90  m_distance.init("Distance", "Distance from the sample origin to the detector plane",
91  default_detector_distance, "mm", "distance");
92 
93  m_detectorAlignment = RectangularDetector::PERPENDICULAR_TO_DIRECT_BEAM;
94 
96 }
97 
99 {
100  int alignment = m_detectorAlignment;
101 
102  s.assertVersion(0);
103  // base class members
104  Serialize::rwClass(s, "masks", m_maskItems);
105  Serialize::rwSelected<ResolutionFunctionItemCatalog>(s, m_resolutionFunction);
106 
107  // own members
108  Serialize::rwValue(s, "xSize", m_xSize);
109  Serialize::rwValue(s, "ySize", m_ySize);
110  Serialize::rwProperty(s, m_width);
111  Serialize::rwProperty(s, m_height);
112  Serialize::rwValue(s, "alignment", alignment);
113  Serialize::rwProperty(s, m_normalVector);
114  Serialize::rwProperty(s, m_directionVector);
115  Serialize::rwProperty(s, m_u0);
116  Serialize::rwProperty(s, m_v0);
117  Serialize::rwProperty(s, m_distance);
118 
119  if (s.xmlReader()) {
120  setDetectorAlignment(static_cast<RectangularDetector::EDetectorArrangement>(alignment));
122  }
123 }
124 
126  RectangularDetector::EDetectorArrangement alignment)
127 {
128  m_detectorAlignment = alignment;
129  updateTooltips();
130 }
131 
132 RectangularDetector::EDetectorArrangement RectangularDetectorItem::detectorAlignment() const
133 {
134  return m_detectorAlignment;
135 }
136 
138 {
139  return m_xSize;
140 }
141 
143 {
144  return m_ySize;
145 }
146 
148 {
149  m_xSize = static_cast<int>(nx);
150 }
151 
153 {
154  m_ySize = static_cast<int>(ny);
155 }
156 
159 {
161 
162  d.label = "Alignment";
163  for (auto a : alignments())
164  d.options << alignmentTitle(a);
165 
166  d.currentIndexSetter = [&](int newIndex) {
167  const_cast<RectangularDetectorItem*>(this)->setDetectorAlignment(alignments()[newIndex]);
168  };
169 
170  d.currentIndexGetter = [&]() { return alignments().indexOf(m_detectorAlignment); };
171  return d;
172 }
173 
174 QVector<std::variant<VectorDescriptor, DoubleDescriptor>>
176 {
177  if (m_detectorAlignment == RectangularDetector::GENERIC)
178  return {normalVector(), directionVector(), u0(), v0()};
179 
180  return {u0(), v0(), distance()};
181 }
182 
183 std::unique_ptr<IDetector> RectangularDetectorItem::createDomainDetector() const
184 {
185  auto result = std::make_unique<RectangularDetector>(xSize(), width(), ySize(), height());
186 
187  // distance and alignment
188  switch (m_detectorAlignment) {
189  case RectangularDetector::GENERIC:
190  result->setDetectorPosition(m_normalVector, m_u0, m_v0, m_directionVector);
191  break;
192  case RectangularDetector::PERPENDICULAR_TO_SAMPLE:
193  result->setPerpendicularToSampleX(m_distance, m_u0, m_v0);
194  break;
195  case RectangularDetector::PERPENDICULAR_TO_DIRECT_BEAM:
196  result->setPerpendicularToDirectBeam(m_distance, m_u0, m_v0);
197  break;
198  case RectangularDetector::PERPENDICULAR_TO_REFLECTED_BEAM:
199  result->setPerpendicularToReflectedBeam(m_distance, m_u0, m_v0);
200  break;
201  case RectangularDetector::PERPENDICULAR_TO_REFLECTED_BEAM_DPOS:
202  result->setPerpendicularToReflectedBeam(m_distance);
203  result->setDirectBeamPosition(m_u0, m_v0);
204  break;
205  default:
206  break;
207  }
208 
209  return std::unique_ptr<IDetector>(result.release());
210 }
211 
213 {
214  switch (m_detectorAlignment) {
215  case RectangularDetector::GENERIC:
216  m_u0.setTooltip(
217  "u-coordinate of point of intersection of normal vector and detector plane");
218  m_v0.setTooltip(
219  "v-coordinate of point of intersection of normal vector and detector plane");
220  break;
221  case RectangularDetector::PERPENDICULAR_TO_SAMPLE:
222  m_u0.setTooltip("u-coordinate of point where sample x-axis crosses the detector");
223  m_v0.setTooltip("v-coordinate of point where sample x-axis crosses the detector");
224  break;
225  case RectangularDetector::PERPENDICULAR_TO_REFLECTED_BEAM:
226  m_u0.setTooltip("u-coordinate of point where reflected beam hits the detector");
227  m_v0.setTooltip("v-coordinate of point where reflected beam hits the detector");
228  break;
229  case RectangularDetector::PERPENDICULAR_TO_DIRECT_BEAM: // fall-through!
230  case RectangularDetector::PERPENDICULAR_TO_REFLECTED_BEAM_DPOS:
231  m_u0.setTooltip("u-coordinate of point where direct beam hits the detector");
232  m_v0.setTooltip("v-coordinate of point where direct beam hits the detector");
233  break;
234  default:
235  break;
236  }
237 }
Defines various axis items.
Defines class ComboProperty.
Defines error class.
Defines class RectangularDetectorItem.
Defines class ResolutionFunctionItemCatalog.
Defines family of ResolutionFunctionItem.
Defines class Streamer.
@ unitless
Defines.
MaskItems m_maskItems
for creation of domain detector; only filled and relevant in jobs
Definition: DetectorItems.h:68
SelectionProperty< ResolutionFunctionItem * > m_resolutionFunction
Definition: DetectorItems.h:69
int ySize() const override
Returns the size of y-axis of the detector.
void setXSize(size_t nx) override
sets the size of x-axis of the detector
SelectionDescriptor< RectangularDetector::EDetectorArrangement > detectorAlignmentSelection() const
void serialize(Streamer &s) override
std::unique_ptr< IDetector > createDomainDetector() const override
RectangularDetector::EDetectorArrangement m_detectorAlignment
QVector< std::variant< VectorDescriptor, DoubleDescriptor > > alignmentPropertiesForUI() const
The properties of the currently active alignment. Sorted as expected on the UI.
void setYSize(size_t ny) override
sets the size of y-axis of the detector
void setDetectorAlignment(RectangularDetector::EDetectorArrangement alignment)
int xSize() const override
Returns the size of x-axis of the detector.
RectangularDetector::EDetectorArrangement detectorAlignment() const
virtual void setUnit(const std::variant< QString, Unit > &)
Set the unit of the distributed value.
Describes a selection (various possibilities and the current one).
function< int()> currentIndexGetter
Function to get currently selected option.
QString label
A label text (short, no trailing colon)
function< void(int)> currentIndexSetter
Function to set currently selected option.
QStringList options
List of options, usually presented as combo entries.
void initWithInitializer(const QString &label, const QString &tooltip, const QString &persistentTag, std::function< void(T newItem, const T oldItem)> initializer)
Initialize by means of a catalog class and an initializer function.
Supports serialization to or deserialization from QXmlStream.
Definition: Streamer.h:36
QXmlStreamReader * xmlReader()
Returns stream reader or nullptr.
Definition: Streamer.h:48
void assertVersion(unsigned expectedVersion) const
As reader, throws DeserializationException unless the expected version is read. As writer,...
Definition: Streamer.cpp:26
void rwProperty(Streamer &s, DoubleProperty &d)
void rwValue(Streamer &s, const QString &tag, bool &val)
Definition: Serialize.cpp:19
void rwClass(Streamer &s, const QString &tag, T &t)
Serializes an item from a class that provides the function void serialize(Streamer&).
Definition: Serialize.h:77