BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
Unit.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/Support/Type/Unit.cpp
6 //! @brief Implements class Unit
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2021
11 //! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************************************
14 
15 #include "GUI/Support/Type/Unit.h"
16 #include "Base/Const/Units.h"
17 #include "Base/Util/Assert.h"
18 #include <QString>
19 
20 double convert(double d, Unit from, Unit to)
21 {
22  if (from == to)
23  return d;
24 
25  if (from == Unit::angstrom && to == Unit::nanometer)
26  return d / 10.0;
27 
28  if (from == Unit::nanometer && to == Unit::angstrom)
29  return d * 10.0;
30 
31  if (from == Unit::angstrom2 && to == Unit::nanometer2)
32  return d / 100.0;
33 
34  if (from == Unit::nanometer2 && to == Unit::angstrom2)
35  return d * 100.0;
36 
37  if (from == Unit::angstromMinus2 && to == Unit::nanometerMinus2)
38  return d * 100.0;
39 
40  if (from == Unit::nanometerMinus2 && to == Unit::angstromMinus2)
41  return d / 100.0;
42 
43  if (from == Unit::radiant && to == Unit::degree)
44  return Units::rad2deg(d);
45 
46  if (from == Unit::degree && to == Unit::radiant)
47  return Units::deg2rad(d);
48 
49  if (from == Unit::other || to == Unit::other) {
50  ASSERT(false); // no conversion possible
51  return d;
52  }
53 
54  ASSERT(false); // no conversion implemented
55  return d;
56 }
57 
58 QString unitAsString(const Unit& unit)
59 {
60  switch (unit) {
61  case Unit::unitless:
62  return "";
63  case Unit::nanometer:
64  return "nm";
65  case Unit::nanometer2:
66  return u8"nm\u00B2";
68  return u8"1/nm\u00B2";
69  case Unit::angstrom:
70  return u8"\u00c5";
71  case Unit::angstrom2:
72  return u8"\u00c5\u00B2";
74  return u8"1/\u00c5\u00B2";
75  case Unit::degree:
76  return "°";
77  case Unit::radiant:
78  return "rad";
79  case Unit::other:
80  ASSERT(false); // this function should not be called for Unit::other
81  return "";
82  default:
83  ASSERT(false); // no string implemented
84  return "";
85  }
86 }
QString unitAsString(const Unit &unit)
Returns the string for the given unit.
Definition: Unit.cpp:58
double convert(double d, Unit from, Unit to)
Convert the given value d from unit "from" to unit "to" Throws an exception if no conversion possible...
Definition: Unit.cpp:20
Defines class Unit.
Unit
Defines units, mainly to be able to convert between units.
Definition: Unit.h:26
@ nanometerMinus2
@ angstrom
@ other
The unit has no enum value defined in here (e.g. when defined as an explicit string)
@ nanometer2
@ angstrom2
@ radiant
@ angstromMinus2
@ unitless
@ nanometer
@ degree