BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
Unit.h File Reference

Description

Defines class Unit.

Homepage:\n http://www.bornagainproject.org
License:\n GNU General Public License v3 or higher (see COPYING)
Authors
Scientific Computing Group at MLZ (see CITATION, AUTHORS)

Definition in file Unit.h.

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Enumerations

enum class  Unit {
  unitless , nanometer , nanometer2 , nanometerMinus2 ,
  angstrom , angstrom2 , angstromMinus2 , degree ,
  radiant , other
}
 Defines units, mainly to be able to convert between units. More...
 

Functions

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. More...
 
QString unitAsString (const Unit &unit)
 Returns the string for the given unit. More...
 

Enumeration Type Documentation

◆ Unit

enum Unit
strong

Defines units, mainly to be able to convert between units.

E.g. internal unit is nanometer, displayed unit is angstrom. Units which do not support conversion do not have to be part of the enum, since the relevant code parts support defining a unit via enum or via string

Enumerator
unitless 
nanometer 
nanometer2 
nanometerMinus2 
angstrom 
angstrom2 
angstromMinus2 
degree 
radiant 
other 

The unit has no enum value defined in here (e.g. when defined as an explicit string)

Definition at line 26 of file Unit.h.

26  {
27  unitless,
28  nanometer,
29  nanometer2,
31  angstrom,
32  angstrom2,
34  degree,
35  radiant,
36  other //!< The unit has no enum value defined in here (e.g. when defined as an explicit string)
37 };
@ 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

Function Documentation

◆ convert()

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 at line 20 of file Unit.cpp.

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 }

References angstrom, angstrom2, angstromMinus2, degree, nanometer, nanometer2, nanometerMinus2, other, and radiant.

Referenced by DoubleSpinBox::toBaseValue(), and DoubleSpinBox::toDisplayValue().

◆ unitAsString()

QString unitAsString ( const Unit unit)

Returns the string for the given unit.

Definition at line 58 of file Unit.cpp.

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 }

References angstrom, angstrom2, angstromMinus2, degree, nanometer, nanometer2, nanometerMinus2, other, radiant, and unitless.

Referenced by ParameterTreeBuilder::addDetector(), and DoubleSpinBox::displayUnitAsString().